[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * This file implements the Wiki links plugin for b2evolution 4 * 5 * Creates wiki links 6 * 7 * b2evolution - {@link http://b2evolution.net/} 8 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html} 9 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/} 10 * 11 * @package plugins 12 * @ignore 13 */ 14 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' ); 15 16 17 /** 18 * @package plugins 19 */ 20 class wikilinks_plugin extends Plugin 21 { 22 var $code = 'b2evWiLi'; 23 var $name = 'Wiki Links'; 24 var $priority = 35; 25 var $version = '1.9-dev'; 26 var $apply_rendering = 'opt-in'; 27 var $group = 'rendering'; 28 var $short_desc; 29 var $long_desc; 30 var $number_of_installs = 1; 31 32 /** 33 * Init 34 */ 35 function PluginInit( & $params ) 36 { 37 $this->short_desc = T_('Wiki Links converter'); 38 $this->long_desc = T_('WikiWord links are created with a CamelCased WikiWord, a ((link)), or a [[link ]].<br /> 39 CamelCased words will be exploded to camel_case which should then match a post url title.'); 40 } 41 42 43 /** 44 * Perform rendering 45 * 46 * @todo get rid of global $blog 47 * 48 * @param array Associative array of parameters 49 * 'data': the data (by reference). You probably want to modify this. 50 * 'format': see {@link format_to_output()}. Only 'htmlbody' and 'entityencoded' will arrive here. 51 * @return boolean true if we can render something for the required output format 52 */ 53 function RenderItemAsHtml( & $params ) 54 { 55 $content = & $params['data']; 56 57 global $ItemCache, $admin_url, $blog; 58 59 // Regular links: 60 $search = array( 61 // [[http://url]] : 62 '#\[\[((https?|mailto)://((?:[^<>{}\s\]]|,(?!\s))+?))\]\]#i', 63 // [[http://url text]] : 64 '#\[\[((https?|mailto)://([^<>{}\s\]]+)) ([^\n\r]+?)\]\]#i', 65 // ((http://url)) : 66 '#\(\(((https?|mailto)://((?:[^<>{}\s\]]|,(?!\s))+?))\)\)#i', 67 // ((http://url text)) : 68 '#\(\(((https?|mailto)://([^<>{}\s\]]+)) ([^\n\r]+?)\)\)#i', 69 ); 70 $replace = array( 71 '<a href="$1">$1</a>', 72 '<a href="$1">$4</a>', 73 '<a href="$1">$1</a>', 74 '<a href="$1">$4</a>' 75 ); 76 77 $content = preg_replace( $search, $replace, $content ); 78 79 /* QUESTION: fplanque, implementation of this planned? then use make_clickable() - or remove this comment 80 $ret = preg_replace("#([\n ])aim:([^,< \n\r]+)#i", "\\1<a href=\"aim:goim?screenname=\\2\\3&message=Hello\">\\2\\3</a>", $ret); 81 82 $ret = preg_replace("#([\n ])icq:([^,< \n\r]+)#i", "\\1<a href=\"http://wwp.icq.com/scripts/search.dll?to=\\2\\3\">\\2\\3</a>", $ret); 83 84 $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,< \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\">www.\\2.\\3\\4</a>", $ret); 85 86 $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^,< \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); */ 87 88 89 // WIKIWORDS: 90 91 $search_wikiwords = array(); 92 $replace_links = array(); 93 94 // STANDALONE WIKIWORDS: 95 $search = '/ 96 (?<= \s | ^ ) # Lookbehind for whitespace 97 ([A-Z]+[a-z0-9_]+([A-Z]+[A-Za-z0-9_]+)+) # WikiWord or WikiWordLong 98 (?= [\.,:;!\?] \s | \s | $ ) # Lookahead for whitespace or punctuation 99 /x'; // x = extended (spaces + comments allowed) 100 101 if( preg_match_all( $search, $content, $matches, PREG_SET_ORDER) ) 102 { 103 // Construct array of wikiwords to look up in post urltitles 104 $wikiwords = array(); 105 foreach( $matches as $match ) 106 { 107 // Convert the WikiWord to an urltitle 108 $WikiWord = $match[0]; 109 $Wiki_Word = preg_replace( '*([^A-Z_])([A-Z])*', '$1_$2', $WikiWord ); 110 $wiki_word = strtolower( $Wiki_Word ); 111 // echo '<br />Match: [', $WikiWord, '] -> [', $wiki_word, ']'; 112 $wikiwords[ $WikiWord ] = $wiki_word; 113 } 114 115 // Lookup all urltitles at once in DB and preload cache: 116 $ItemCache = & get_Cache( 'ItemCache' ); 117 $ItemCache->load_urltitle_array( $wikiwords ); 118 119 // Construct arrays for replacing wikiwords by links: 120 foreach( $wikiwords as $WikiWord => $wiki_word ) 121 { 122 // WikiWord 123 $search_wikiwords[] = '/ 124 (?<= \s | ^ ) # Lookbehind for whitespace or start 125 (?<! <span\ class="NonExistentWikiWord"> ) 126 '.$WikiWord.' # Specific WikiWord to replace 127 (?= [\.,:;!\?] \s | \s | $ ) # Lookahead for whitespace or end of string 128 /sx'; // s = dot matches newlines, x = extended (spaces + comments allowed) 129 130 131 // Find matching Item: 132 if( ($Item = & $ItemCache->get_by_urltitle( $wiki_word, false )) !== false ) 133 { // Item Found 134 $permalink = $Item->get_permanent_url(); 135 136 // WikiWord 137 $replace_links[] = '<a href="'.$permalink.'">'.$WikiWord.'</a>'; 138 139 } 140 else 141 { // Item not found 142 143 $create_link = isset($blog) ? ('<a href="'.$admin_url.'?ctrl=items&action=new&blog='.$blog.'&post_title='.preg_replace( '*([^A-Z_])([A-Z])*', '$1%20$2', $WikiWord ).'&post_urltitle='.$wiki_word.'" title="Create...">?</a>') : ''; 144 145 // WikiWord 146 $replace_links[] = '<span class="NonExistentWikiWord">'.$WikiWord.$create_link.'</span>'; 147 148 } 149 } 150 } 151 152 // BRACKETED WIKIWORDS: 153 $search = '/ 154 (?<= \(\( | \[\[ ) # Lookbehind for (( or [[ 155 ([A-Z]+[A-Za-z0-9_]*) # Anything from Wikiword to WikiWordLong 156 (?= ( \s .*? )? ( \)\) | \]\] ) ) # Lookahead for )) or ]] 157 /x'; // x = extended (spaces + comments allowed) 158 159 if( preg_match_all( $search, $content, $matches, PREG_SET_ORDER) ) 160 { 161 // Construct array of wikiwords to look up in post urltitles 162 $wikiwords = array(); 163 foreach( $matches as $match ) 164 { 165 // Convert the WikiWord to an urltitle 166 $WikiWord = $match[0]; 167 $Wiki_Word = preg_replace( '*([^A-Z_])([A-Z])*', '$1_$2', $WikiWord ); 168 $wiki_word = strtolower( $Wiki_Word ); 169 // echo '<br />Match: [', $WikiWord, '] -> [', $wiki_word, ']'; 170 $wikiwords[ $WikiWord ] = $wiki_word; 171 } 172 173 // Lookup all urltitles at once in DB and preload cache: 174 $ItemCache = & get_Cache( 'ItemCache' ); 175 $ItemCache->load_urltitle_array( $wikiwords ); 176 177 // Construct arrays for replacing wikiwords by links: 178 foreach( $wikiwords as $WikiWord => $wiki_word ) 179 { 180 // [[WikiWord text]] 181 $search_wikiwords[] = '* 182 \[\[ 183 '.$WikiWord.' # Specific WikiWord to replace 184 \s (.+?) 185 \]\] 186 *sx'; // s = dot matches newlines, x = extended (spaces + comments allowed) 187 188 // ((WikiWord text)) 189 $search_wikiwords[] = '* 190 \(\( 191 '.$WikiWord.' # Specific WikiWord to replace 192 \s (.+?) 193 \)\) 194 *sx'; // s = dot matches newlines, x = extended (spaces + comments allowed) 195 196 // [[Wikiword]] 197 $search_wikiwords[] = '* 198 \[\[ 199 '.$WikiWord.' # Specific WikiWord to replace 200 \]\] 201 *sx'; // s = dot matches newlines, x = extended (spaces + comments allowed) 202 203 // ((Wikiword)) 204 $search_wikiwords[] = '* 205 \(\( 206 '.$WikiWord.' # Specific WikiWord to replace 207 \)\) 208 *sx'; // s = dot matches newlines, x = extended (spaces + comments allowed) 209 210 211 // Find matching Item: 212 if( ($Item = & $ItemCache->get_by_urltitle( $wiki_word, false )) !== false ) 213 { // Item Found 214 $permalink = $Item->get_permanent_url(); 215 216 // [[WikiWord text]] 217 $replace_links[] = '<a href="'.$permalink.'">$1</a>'; 218 219 // ((WikiWord text)) 220 $replace_links[] = '<a href="'.$permalink.'">$1</a>'; 221 222 // [[Wikiword]] 223 $replace_links[] = '<a href="'.$permalink.'">'.$WikiWord.'</a>'; 224 225 // ((Wikiword)) 226 $replace_links[] = '<a href="'.$permalink.'">'.$WikiWord.'</a>'; 227 } 228 else 229 { // Item not found 230 231 $create_link = isset($blog) ? ('<a href="'.$admin_url.'?ctrl=items&action=new&blog='.$blog.'&post_title='.preg_replace( '*([^A-Z_])([A-Z])*', '$1%20$2', $WikiWord ).'&post_urltitle='.$wiki_word.'" title="Create...">?</a>') : ''; 232 233 // [[WikiWord text]] 234 $replace_links[] = '<span class="NonExistentWikiWord">$1'.$create_link.'</span>'; 235 236 // ((WikiWord text)) 237 $replace_links[] = '<span class="NonExistentWikiWord">$1'.$create_link.'</span>'; 238 239 // [[Wikiword]] 240 $replace_links[] = '<span class="NonExistentWikiWord">'.$WikiWord.$create_link.'</span>'; 241 242 // ((Wikiword)) 243 $replace_links[] = '<span class="NonExistentWikiWord">'.$WikiWord.$create_link.'</span>'; 244 } 245 } 246 } 247 248 // echo '<br />---'; 249 250 // pre_dump( $search_wikiwords ); 251 252 if( count( $search_wikiwords ) ) 253 { // We have set some links to replace: 254 $content = preg_replace( $search_wikiwords, $replace_links, $content ); 255 } 256 257 return true; 258 } 259 } 260 261 262 /* 263 * $Log: _wikilinks.plugin.php,v $ 264 * Revision 1.25 2007/04/26 00:11:04 fplanque 265 * (c) 2007 266 * 267 * Revision 1.24 2007/04/20 02:53:13 fplanque 268 * limited number of installs 269 * 270 * Revision 1.23 2006/12/26 03:19:12 fplanque 271 * assigned a few significant plugin groups 272 * 273 * Revision 1.22 2006/12/12 02:53:57 fplanque 274 * Activated new item/comments controllers + new editing navigation 275 * Some things are unfinished yet. Other things may need more testing. 276 * 277 * Revision 1.21 2006/08/19 07:56:32 fplanque 278 * Moved a lot of stuff out of the automatic instanciation in _main.inc 279 * 280 * Revision 1.20 2006/07/10 20:19:30 blueyed 281 * Fixed PluginInit behaviour. It now gets called on both installed and non-installed Plugins, but with the "is_installed" param appropriately set. 282 * 283 * Revision 1.19 2006/07/07 21:26:49 blueyed 284 * Bumped to 1.9-dev 285 * 286 * Revision 1.18 2006/07/06 19:56:29 fplanque 287 * no message 288 * 289 * Revision 1.17 2006/06/16 21:30:57 fplanque 290 * Started clean numbering of plugin versions (feel free do add dots...) 291 * 292 * Revision 1.16 2006/05/30 20:28:56 blueyed 293 * typo 294 * 295 * Revision 1.15 2006/05/30 19:39:56 fplanque 296 * plugin cleanup 297 * 298 * Revision 1.14 2006/04/11 21:22:26 fplanque 299 * partial cleanup 300 * 301 */ 302 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |