forked from b2evolution/b2evolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_wikilinks.plugin.php
More file actions
379 lines (318 loc) · 12.4 KB
/
_wikilinks.plugin.php
File metadata and controls
379 lines (318 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* This file implements the Wiki links plugin for b2evolution
*
* Creates wiki links
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/gnu-gpl-license}
* @copyright (c)2003-2015 by Francois Planque - {@link http://fplanque.com/}
*
* @package plugins
* @ignore
*/
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
/**
* @package plugins
*/
class wikilinks_plugin extends Plugin
{
var $code = 'b2evWiLi';
var $name = 'Wiki Links';
var $priority = 35;
var $version = '5.0.0';
var $group = 'rendering';
var $short_desc;
var $long_desc;
var $help_topic = 'wiki-links-plugin';
var $number_of_installs = 1;
/**
* Init
*/
function PluginInit( & $params )
{
$this->short_desc = T_('Wiki Links converter');
$this->long_desc = T_('You can create links with [[CamelCase]] or ((CamelCase)) which will try to link to the category or the post with the slug "camel-case". See manual for more.');
}
/**
* Define here default collection/blog settings that are to be made available in the backoffice.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::get_coll_setting_definitions()}.
*/
function get_coll_setting_definitions( & $params )
{
$default_values = array(
'default_post_rendering' => 'opt-in'
);
if( !empty( $params['blog_type'] ) && $params['blog_type'] != 'forum' )
{ // Set the default settings depends on blog type
$default_values['default_comment_rendering'] = 'never';
}
$default_params = array_merge( $params, $default_values );
return array_merge( parent::get_coll_setting_definitions( $default_params ),
array(
'link_without_brackets' => array(
'label' => $this->T_('Links without brackets'),
'type' => 'checkbox',
'defaultvalue' => 0,
'note' => $this->T_('Enable this to create the links from words like WikiWord without brackets [[]]'),
)
) );
}
/**
* Perform rendering
*
* @param array Associative array of parameters
* 'data': the data (by reference). You probably want to modify this.
* 'format': see {@link format_to_output()}. Only 'htmlbody' and 'entityencoded' will arrive here.
* @return boolean true if we can render something for the required output format
*/
function RenderItemAsHtml( & $params )
{
$content = & $params['data'];
if( !empty( $params['Item'] ) )
{ // Get Item from params
$Item = & $params['Item'];
}
elseif( !empty( $params['Comment'] ) )
{ // Get Item from Comment
$Comment = & $params['Comment'];
$Item = & $Comment->get_Item();
}
else
{ // Item and Comment are not defined, Exit here
return;
}
$item_Blog = & $Item->get_Blog();
return $this->render_content( $content, $item_Blog );
}
/**
* Perform rendering of Message content
*
* NOTE: Use default coll settings of comments as messages settings
*
* @see Plugin::RenderMessageAsHtml()
*/
function RenderMessageAsHtml( & $params )
{
$content = & $params['data'];
return $this->render_content( $content, NULL, true );
}
/**
* Render content of Item, Comment, Message
*
* @todo get rid of global $blog
*
* @param string Content
* @param object Blog
* @param boolean Allow empty Blog
* return boolean
*/
function render_content( & $content, $item_Blog = NULL, $allow_null_blog = false )
{
global $ItemCache, $admin_url, $blog, $evo_charset;
$regexp_modifier = '';
if( $evo_charset == 'utf-8' )
{ // Add this modifier to work with UTF-8 strings correctly
$regexp_modifier = 'u';
}
// Regular links:
$search = array(
// [[http://url]] :
'#\[\[((https?|mailto)://((?:[^<>{}\s\]]|,(?!\s))+?))\]\]#i',
// [[http://url text]] :
'#\[\[((https?|mailto)://([^<>{}\s\]]+)) ([^\n\r]+?)\]\]#i',
// ((http://url)) :
'#\(\(((https?|mailto)://((?:[^<>{}\s\]]|,(?!\s))+?))\)\)#i',
// ((http://url text)) :
'#\(\(((https?|mailto)://([^<>{}\s\]]+)) ([^\n\r]+?)\)\)#i',
);
$replace = array(
'<a href="$1">$1</a>',
'<a href="$1">$4</a>',
'<a href="$1">$1</a>',
'<a href="$1">$4</a>'
);
$content = replace_content_outcode( $search, $replace, $content );
/* QUESTION: fplanque, implementation of this planned? then use make_clickable() - or remove this comment
$ret = preg_replace("#([\n ])aim:([^,< \n\r]+)#i", "\\1<a href=\"aim:goim?screenname=\\2\\3&message=Hello\">\\2\\3</a>", $ret);
$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);
$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);
$ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^,< \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); */
// To use function replace_special_chars()
load_funcs('locales/_charset.funcs.php');
// WIKIWORDS:
$search_wikiwords = array();
$replace_links = array();
if( $this->get_coll_setting( 'link_without_brackets', $item_Blog, $allow_null_blog ) )
{ // Create the links from standalone WikiWords
// STANDALONE WIKIWORDS:
$search = '/
(?<= \s | ^ ) # Lookbehind for whitespace
([\p{Lu}]+[\p{Ll}0-9_]+([\p{Lu}]+[\p{L}0-9_]+)+) # WikiWord or WikiWordLong
(?= [\.,:;!\?] \s | \s | $ ) # Lookahead for whitespace or punctuation
/x'.$regexp_modifier; // x = extended (spaces + comments allowed)
if( preg_match_all( $search, $content, $matches, PREG_SET_ORDER) )
{
// Construct array of wikiwords to look up in post urltitles
$wikiwords = array();
foreach( $matches as $match )
{
// Convert the WikiWord to an urltitle
$WikiWord = $match[0];
$Wiki_Word = preg_replace( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '$1-$2', $WikiWord );
$wiki_word = utf8_strtolower( $Wiki_Word );
// echo '<br />Match: [', $WikiWord, '] -> [', $wiki_word, ']';
$wiki_word = replace_special_chars( $wiki_word );
$wikiwords[ $WikiWord ] = $wiki_word;
}
// Lookup all urltitles at once in DB and preload cache:
$ItemCache = & get_ItemCache();
$ItemCache->load_urltitle_array( $wikiwords );
// Construct arrays for replacing wikiwords by links:
foreach( $wikiwords as $WikiWord => $wiki_word )
{
// WikiWord
$search_wikiwords[] = '/
(?<= \s | ^ ) # Lookbehind for whitespace or start
(?<! <span\ class="NonExistentWikiWord"> )
'.$WikiWord.' # Specific WikiWord to replace
(?= [\.,:;!\?] \s | \s | $ ) # Lookahead for whitespace or end of string
/sx'; // s = dot matches newlines, x = extended (spaces + comments allowed)
// Find matching Item:
if( ($Item = & $ItemCache->get_by_urltitle( $wiki_word, false )) !== false )
{ // Item Found
$permalink = $Item->get_permanent_url();
// WikiWord
$replace_links[] = '<a href="'.$permalink.'">'.$Item->get( 'title' ).'</a>';
}
else
{ // Item not found
$create_link = isset($blog) ? ('<a href="'.$admin_url.'?ctrl=items&action=new&blog='.$blog.'&post_title='.preg_replace( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '$1%20$2', $WikiWord ).'&post_urltitle='.$wiki_word.'" title="Create...">?</a>') : '';
// WikiWord
$replace_links[] = '<span class="NonExistentWikiWord">'.$WikiWord.$create_link.'</span>';
}
}
}
}
// BRACKETED WIKIWORDS:
$search = '/
(?<= \(\( | \[\[ ) # Lookbehind for (( or [[
([\p{L}0-9#]+[\p{L}0-9#_\-]*) # Anything from Wikiword to WikiWordLong
(?= ( \s .*? )? ( \)\) | \]\] ) ) # Lookahead for )) or ]]
/x'.$regexp_modifier; // x = extended (spaces + comments allowed)
if( preg_match_all( $search, $content, $matches, PREG_SET_ORDER) )
{
// Construct array of wikiwords to look up in post urltitles
$wikiwords = array();
foreach( $matches as $match )
{
// Convert the WikiWord to an urltitle
$WikiWord = $match[0];
if( preg_match( '/^[\p{Ll}0-9#_\-]+$/'.$regexp_modifier, $WikiWord ) )
{ // This WikiWord already matches a slug format
$Wiki_Word = $WikiWord;
$wiki_word = $Wiki_Word;
}
else
{ // Convert WikiWord to slug format
$Wiki_Word = preg_replace( array( '*([^\p{Lu}#_])([\p{Lu}#])*'.$regexp_modifier, '*([^0-9])([0-9])*'.$regexp_modifier ), '$1-$2', $WikiWord );
$wiki_word = utf8_strtolower( $Wiki_Word );
}
// Remove additional params from $wiki_word, it should be cleared. We keep the params in $WikiWord and parse them below.
$wiki_word = preg_replace( '/^([^#]+)(#.+)?$/i', '$1', $wiki_word );
$wiki_word = replace_special_chars( $wiki_word );
$wikiwords[ $WikiWord ] = $wiki_word;
}
// Lookup all urltitles at once in DB and preload cache:
$ChapterCache = & get_ChapterCache();
$ChapterCache->load_urlname_array( $wikiwords );
$ItemCache = & get_ItemCache();
$ItemCache->load_urltitle_array( $wikiwords );
// Construct arrays for replacing wikiwords by links:
foreach( $wikiwords as $WikiWord => $wiki_word )
{
// Parse wiki word to find additional param for atrr "id"
$url_params = '';
preg_match( '/^([^#]+)(#(.+))?$/i', $WikiWord, $WikiWord_match );
if( isset( $WikiWord_match[3] ) )
{ // wiki word has attr "id"
$url_params .= '#'.$WikiWord_match[3];
}
// Fix for regexp
$WikiWord = str_replace( '#', '\#', $WikiWord );
// [[WikiWord text]]
$search_wikiwords[] = '*
\[\[
'.$WikiWord.' # Specific WikiWord to replace
\s (.+?)
\]\]
*sx'; // s = dot matches newlines, x = extended (spaces + comments allowed)
// ((WikiWord text))
$search_wikiwords[] = '*
\(\(
'.$WikiWord.' # Specific WikiWord to replace
\s (.+?)
\)\)
*sx'; // s = dot matches newlines, x = extended (spaces + comments allowed)
// [[Wikiword]]
$search_wikiwords[] = '*
\[\[
'.$WikiWord.' # Specific WikiWord to replace
\]\]
*sx'; // s = dot matches newlines, x = extended (spaces + comments allowed)
// ((Wikiword))
$search_wikiwords[] = '*
\(\(
'.$WikiWord.' # Specific WikiWord to replace
\)\)
*sx'; // s = dot matches newlines, x = extended (spaces + comments allowed)
// Use title of wiki word without attribute part
$WikiWord = $WikiWord_match[1];
// Find matching Chapter or Item:
$permalink = '';
$link_text = preg_replace( array( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '*([^0-9])([0-9])*'.$regexp_modifier ), '$1 $2', $WikiWord );
$link_text = ucwords( str_replace( '-', ' ', $link_text ) );
if( ($Chapter = & $ChapterCache->get_by_urlname( $wiki_word, false )) !== false )
{ // Chapter is found
$permalink = $Chapter->get_permanent_url();
$existing_link_text = $Chapter->get( 'name' );
}
elseif( ($Item = & $ItemCache->get_by_urltitle( $wiki_word, false )) !== false )
{ // Item is found
$permalink = $Item->get_permanent_url();
$existing_link_text = $Item->get( 'title' );
}
if( !empty( $permalink ) )
{ // Chapter or Item are found
// [[WikiWord text]]
$replace_links[] = '<a href="'.$permalink.$url_params.'">$1</a>';
// ((WikiWord text))
$replace_links[] = '<a href="'.$permalink.$url_params.'">$1</a>';
// [[Wikiword]]
$replace_links[] = '<a href="'.$permalink.$url_params.'">'.$existing_link_text.'</a>';
// ((Wikiword))
$replace_links[] = '<a href="'.$permalink.$url_params.'">'.$link_text.'</a>';
}
else
{ // Chapter and Item are not found
$create_link = isset($blog) ? ('<a href="'.$admin_url.'?ctrl=items&action=new&blog='.$blog.'&post_title='.preg_replace( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '$1%20$2', $WikiWord ).'&post_urltitle='.$wiki_word.'" title="Create...">?</a>') : '';
// [[WikiWord text]]
$replace_links[] = '<span class="NonExistentWikiWord">$1'.$create_link.'</span>';
// ((WikiWord text))
$replace_links[] = '<span class="NonExistentWikiWord">$1'.$create_link.'</span>';
// [[Wikiword]]
$replace_links[] = '<span class="NonExistentWikiWord">'.$link_text.$create_link.'</span>';
// ((Wikiword))
$replace_links[] = '<span class="NonExistentWikiWord">'.$link_text.$create_link.'</span>';
}
}
}
// echo '<br />---';
// pre_dump( $search_wikiwords );
$content = replace_content_outcode( $search_wikiwords, $replace_links, $content );
return true;
}
}
?>