-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWikilogParser.php
More file actions
636 lines (557 loc) · 19.2 KB
/
WikilogParser.php
File metadata and controls
636 lines (557 loc) · 19.2 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
/**
* MediaWiki Wikilog extension
* Copyright © 2008-2010 Juliano F. Ravasi
* http://www.mediawiki.org/wiki/Extension:Wikilog
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* @file
* @ingroup Extensions
* @author Juliano F. Ravasi < dev juliano info >
*/
if ( !defined( 'MEDIAWIKI' ) )
die();
/**
* This class holds the parser functions that hooks into the Parser in order
* to collect Wikilog metadata.
*/
class WikilogParser
{
/**
* Anchor printed when a --more-- separator is substituted.
*/
const MORE_ANCHOR = "<span id=\"wl-more\"></span>";
/**
* True if parsing articles with feed output specific settings.
* This is an horrible hack needed because of many MediaWiki misdesigns.
*/
static private $feedParsing = false;
/**
* True if we are expanding local URLs (in order to render stand-alone,
* base-less feeds). This is an horrible hack needed because of many
* MediaWiki misdesigns.
*/
static private $expandingUrls = false;
###
## Parser hooks
#
/**
* ParserFirstCallInit hook handler function.
*/
public static function FirstCallInit( $parser ) {
$mwSummary =& MagicWord::get( 'wlk-summary' );
foreach ( $mwSummary->getSynonyms() as $tagname ) {
$parser->setHook( $tagname, array( 'WikilogParser', 'summary' ) );
}
$parser->setFunctionHook( 'wl-settings', array( 'WikilogParser', 'settings' ), SFH_NO_HASH );
$parser->setFunctionHook( 'wl-publish', array( 'WikilogParser', 'publish' ), SFH_NO_HASH );
$parser->setFunctionHook( 'wl-comment', array( 'WikilogParser', 'comment' ), SFH_NO_HASH );
$parser->setFunctionHook( 'wl-author', array( 'WikilogParser', 'author' ), SFH_NO_HASH );
$parser->setFunctionHook( 'wl-tags', array( 'WikilogParser', 'tags' ), SFH_NO_HASH );
$parser->setFunctionHook( 'wl-info', array( 'WikilogParser', 'info' ), SFH_NO_HASH );
return true;
}
/**
* ParserClearState hook handler function.
*/
public static function ClearState( $parser ) {
# These two parser attributes contain our private information.
# They take a piggyback ride on the parser object.
$parser->mExtWikilog = new WikilogParserOutput;
$parser->mExtWikilogInfo = null;
# Disable TOC in feeds.
if ( self::$feedParsing ) {
$parser->mShowToc = false;
}
return true;
}
/**
* ParserBeforeStrip hook handler function.
*/
public static function BeforeStrip( $parser, $text, $stripState ) {
global $wgUser;
# Do nothing if a title is not set.
if ( ! ( $title = $parser->getTitle() ) )
return true;
# Do nothing if it is not a wikilog article.
if ( ! ( $parser->mExtWikilogInfo = Wikilog::getWikilogInfo( $title ) ) )
return true;
if ( $parser->mExtWikilogInfo->isItem() ) {
# By default, use the item name as the default sort in categories.
# This can be overriden by {{DEFAULTSORT:...}} if the user wants.
$parser->setDefaultSort( $parser->mExtWikilogInfo->getItemName() );
}
return true;
}
/**
* ParserAfterTidy hook handler function.
*/
public static function AfterTidy( $parser, $text ) {
$parser->mOutput->mExtWikilog = $parser->mExtWikilog;
return true;
}
/**
* InternalParseBeforeLinks hook handler function. Called after nowiki,
* comments and templates are treated.
* For wikilog pages, look for the "--more--" marker and extract the
* article summary before it. If not found, look for the first heading
* and use the text before it (intro section).
*/
public static function InternalParseBeforeLinks( $parser, &$text, $stripState ) {
if ( $parser->mExtWikilogInfo && $parser->mExtWikilogInfo->isItem() ) {
static $moreRegex = false;
if ( $moreRegex === false ) {
$mwMore =& MagicWord::get( 'wlk-more' );
$words = $mwMore->getBaseRegex();
$flags = $mwMore->getRegexCase();
$moreRegex = "/(?<=^|\\n)--+ *(?:$words) *--+\s*/$flags";
}
/**
* Find and replace the --more-- marker. Extract summary.
* We do it anyway even if the summary is already set, in order
* to replace the marker with an invisible anchor.
*/
$p = preg_split( $moreRegex, $text, 2 );
if ( count( $p ) > 1 ) {
self::trySetSummary( $parser, trim( $p[0] ) );
$anchor = $parser->insertStripItem( self::MORE_ANCHOR );
$text = $p[0] . $anchor . $p[1];
} elseif ( !$parser->mExtWikilog->mSummary ) {
/*
* Otherwise, make a summary from the intro section.
* Why we don't use $parser->getSection()? Because it has the
* side-effect of clearing the parser state, which is bad here
* since this hook happens during parsing. Instead, we
* anticipate the $parser->doHeadings() call and extract the
* text before the first heading.
*/
$text = $parser->doHeadings( $text );
$p = preg_split( '/<(h[1-6])\\b.*?>.*?<\\/\\1\\s*>/i', $text, 2 );
if ( count( $p ) > 1 ) {
self::trySetSummary( $parser, trim( $p[0] ) );
}
}
}
return true;
}
/**
* GetLocalURL hook handler function.
* Expands local URL @a $url if self::$expandingUrls is true.
*/
public static function GetLocalURL( $title, &$url, $query ) {
if ( self::$expandingUrls ) {
$url = wfExpandUrl( $url );
}
return true;
}
/**
* GetFullURL hook handler function.
* Fix some brain-damage in Title::getFullURL() (as of MW 1.13) that
* prepends $wgServer to URL without using wfExpandUrl(), in part because
* we want (above in Wikilog::GetLocalURL()) to return an absolute URL
* from Title::getLocalURL() in situations where action != 'render'.
* @todo Report this bug to MediaWiki bugzilla.
*/
public static function GetFullURL( $title, &$url, $query ) {
global $wgServer;
if ( self::$expandingUrls ) {
$l = strlen( $wgServer );
if ( substr( $url, 0, 2 * $l ) == $wgServer . $wgServer ) {
$url = substr( $url, $l );
}
}
return true;
}
###
## Parser tags and functions
#
/**
* Summary tag parser hook handler.
*/
public static function summary( $text, $params, $parser ) {
$mwHidden =& MagicWord::get( 'wlk-hidden' );
# Remove extra space to make block rendering easier.
$text = trim( $text );
self::trySetSummary( $parser, $text );
$hidden = WikilogUtils::arrayMagicKeyGet( $params, $mwHidden );
return $hidden ? '<!-- -->' : $parser->recursiveTagParse( $text );
}
/**
* {{wl-settings:...}} parser function handler.
*/
public static function settings( $parser /* ... */ ) {
global $wgOut;
self::checkNamespace( $parser );
$mwIcon =& MagicWord::get( 'wlk-icon' );
$mwLogo =& MagicWord::get( 'wlk-logo' );
$mwSubtitle =& MagicWord::get( 'wlk-subtitle' );
$args = array_slice( func_get_args(), 1 );
foreach ( $args as $arg ) {
$parts = array_map( 'trim', explode( '=', $arg, 2 ) );
if ( empty( $parts[0] ) ) continue;
if ( count( $parts ) < 2 ) $parts[1] = '';
list( $key, $value ) = $parts;
if ( $mwIcon->matchStart( $key ) ) {
if ( ( $icon = self::parseImageLink( $parser, $value ) ) ) {
$parser->mExtWikilog->mIcon = $icon->getTitle();
}
} elseif ( $mwLogo->matchStart( $key ) ) {
if ( ( $logo = self::parseImageLink( $parser, $value ) ) ) {
$parser->mExtWikilog->mLogo = $logo->getTitle();
}
} elseif ( $mwSubtitle->matchStart( $key ) ) {
$popt = $parser->getOptions();
$popt->enableLimitReport( false );
$output = $parser->parse( $value, $parser->getTitle(), $popt, true, false );
$parser->mExtWikilog->mSummary = $output->getText();
} else {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-invalid-param', htmlspecialchars( $key ) )->text() )->text();
$parser->mOutput->addWarning( $warning );
}
}
return '<!-- -->';
}
/**
* {{wl-publish:...}} parser function handler.
*/
public static function publish( $parser, $pubdate /*, $author... */ ) {
self::checkNamespace( $parser );
$parser->mExtWikilog->mPublish = true;
$args = array_slice( func_get_args(), 2 );
// First argument is the publish date
if ( !is_null( $pubdate ) ) {
wfSuppressWarnings(); // Shut up E_STRICT warnings about timezone.
$ts = strtotime( $pubdate );
wfRestoreWarnings();
if ( $ts > 0 ) {
$parser->mExtWikilog->mPubDate = wfTimestamp( TS_MW, $ts );
} else {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-invalid-date', $pubdate )->text() )->text();
$parser->mOutput->addWarning( $warning );
}
}
// Remaining arguments are author names
foreach ( $args as $name ) {
if ( !self::tryAddAuthor( $parser, $name ) ) {
break;
}
}
return '<!-- -->';
}
/**
* {{wl-comment: parent comment title | anonymous author name }} parser function handler.
*/
public static function comment( $parser, $parent, $anon_name = NULL ) {
if ( $anon_name === '' ) {
$anon_name = NULL;
}
$parser->mExtWikilog->mComment = array( $parent, $anon_name );
return '<!-- -->';
}
/**
* {{wl-author:...}} parser function handler.
*/
public static function author( $parser /*, $author... */ ) {
self::checkNamespace( $parser );
$args = array_slice( func_get_args(), 1 );
foreach ( $args as $name ) {
if ( !self::tryAddAuthor( $parser, $name ) )
break;
}
return '<!-- -->';
}
/**
* {{wl-tags:...}} parser function handler.
*/
public static function tags( $parser /*, $tag... */ ) {
self::checkNamespace( $parser );
$args = array_slice( func_get_args(), 1 );
foreach ( $args as $tag ) {
if ( !self::tryAddTag( $parser, $tag ) )
break;
}
return '<!-- -->';
}
/**
* {{wl-info:...}} parser function handler.
* Provides general information about the extension.
*/
public static function info( $parser, $id /*, $tag... */ ) {
global $wgWikilogNamespaces, $wgWikilogEnableTags;
global $wgWikilogEnableComments;
global $wgContLang;
$args = array_slice( func_get_args(), 2 );
switch ( $id ) {
case 'num-namespaces':
return count( $wgWikilogNamespaces );
case 'all-namespaces':
$namespaces = array();
foreach ( $wgWikilogNamespaces as $ns )
$namespaces[] = $wgContLang->getFormattedNsText( $ns );
return $wgContLang->listToText( $namespaces );
case 'namespace-by-index':
$index = empty( $args ) ? 0 : intval( array_shift( $args ) );
if ( isset( $wgWikilogNamespaces[$index] ) ) {
return $wgContLang->getFormattedNsText( $wgWikilogNamespaces[$index] );
} else {
return '';
}
case 'tags-enabled':
return $wgWikilogEnableTags ? '*' : '';
case 'comments-enabled':
return $wgWikilogEnableComments ? '*' : '';
default:
return '';
}
}
###
## Wikilog parser settings.
#
/**
* Enable special wikilog feed parsing.
*
* This function changes the parser behavior in order to output
*
* The proper way to use this function is:
* @code
* $saveFeedParse = WikilogParser::enableFeedParsing();
* # ...code that uses $wgParser in order to parse articles...
* WikilogParser::enableFeedParsing( $saveFeedParse );
* @endcode
*
* @note Using this function changes the behavior of Parser. When enabled,
* parsed content should be cached under a different key.
*/
public static function enableFeedParsing( $enable = true ) {
$prev = self::$feedParsing;
self::$feedParsing = $enable;
return $prev;
}
/**
* Enable expansion of local URLs.
*
* In order to output stand-alone content with all absolute links, it is
* necessary to expand local URLs. MediaWiki tries to do this in a few
* places by sniffing into the 'action' GET request parameter, but this
* fails in many ways. This function tries to remedy this.
*
* This function pre-expands all base URL fragments used by MediaWiki,
* and also enables URL expansion in the Wikilog::GetLocalURL hook.
* The original values of all URLs are saved when $enable = true, and
* restored back when $enabled = false.
*
* The proper way to use this function is:
* @code
* $saveExpUrls = WikilogParser::expandLocalUrls();
* # ...code that uses $wgParser in order to parse articles...
* WikilogParser::expandLocalUrls( $saveExpUrls );
* @endcode
*
* @note Using this function changes the behavior of Parser. When enabled,
* parsed content should be cached under a different key.
*/
public static function expandLocalUrls( $enable = true ) {
global $wgScriptPath, $wgUploadPath, $wgStylePath, $wgMathPath, $wgLocalFileRepo;
static $originalPaths = null;
$prev = self::$expandingUrls;
if ( $enable ) {
if ( !self::$expandingUrls ) {
self::$expandingUrls = true;
# Save original values.
$originalPaths = array( $wgScriptPath, $wgUploadPath,
$wgStylePath, $wgMathPath, $wgLocalFileRepo['url'] );
# Expand paths.
$wgScriptPath = wfExpandUrl( $wgScriptPath );
$wgUploadPath = wfExpandUrl( $wgUploadPath );
$wgStylePath = wfExpandUrl( $wgStylePath );
$wgMathPath = wfExpandUrl( $wgMathPath );
$wgLocalFileRepo['url'] = wfExpandUrl( $wgLocalFileRepo['url'] );
# Destroy existing RepoGroup, if any.
RepoGroup::destroySingleton();
}
} else {
if ( self::$expandingUrls ) {
self::$expandingUrls = false;
# Restore original values.
list( $wgScriptPath, $wgUploadPath, $wgStylePath, $wgMathPath,
$wgLocalFileRepo['url'] ) = $originalPaths;
# Destroy existing RepoGroup, if any.
RepoGroup::destroySingleton();
}
}
return $prev;
}
###
## Internal stuff.
#
/**
* Set the article summary, ignore if already set.
* @return True if set, false otherwise.
*/
private static function trySetSummary( $parser, $text ) {
if ( !$parser->mExtWikilog->mSummary ) {
$oldOpt = $parser->getOptions();
$popt = clone $oldOpt;
$popt->enableLimitReport( false );
// Fool some extensions like Cite to get a clean section output
$popt->setIsSectionPreview( true );
$output = $parser->parse( $text, $parser->getTitle(), $popt, true, false );
$parser->mExtWikilog->mSummary = $output->getText();
$parser->mOptions = $oldOpt;
// wfDebug( "Wikilog summary set to:\n----\n" . $parser->mExtWikilog->mSummary . "\n----\n" );
return true;
} else {
return false;
}
}
/**
* Adds an author to the current article. If too many authors, warns.
* @return False on overflow, true otherwise.
*/
private static function tryAddAuthor( $parser, $name ) {
global $wgWikilogMaxAuthors;
if ( count( $parser->mExtWikilog->mAuthors ) >= $wgWikilogMaxAuthors ) {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-too-many-authors' )->text() )->text();
$parser->mOutput->addWarning( $warning );
return false;
}
$user = User::newFromName( $name );
if ( $user ) {
$parser->mExtWikilog->mAuthors[$user->getName()] = $user->getID();
}
else {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-invalid-author', $name )->text() )->text();
$parser->mOutput->addWarning( $warning );
}
return true;
}
/**
* Adds a tag to the current article. If too many tags, warns.
* @return False on overflow, true otherwise.
*/
private static function tryAddTag( $parser, $tag ) {
global $wgWikilogMaxTags;
static $tcre = false;
if ( !$tcre ) { $tcre = '/[^' . Title::legalChars() . ']/'; }
if ( count( $parser->mExtWikilog->mTags ) >= $wgWikilogMaxTags ) {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-too-many-tags' )->text() )->text();
$parser->mOutput->addWarning( $warning );
return false;
}
if ( !empty( $tag ) && !preg_match( $tcre, $tag ) ) {
$parser->mExtWikilog->mTags[$tag] = 1;
}
else {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-invalid-tag', $tag )->text() )->text();
$parser->mOutput->addWarning( $warning );
}
return true;
}
/**
* Check if the calling parser function is being executed in Wikilog
* context. Generates a parser warning if it isn't.
*/
private static function checkNamespace( $parser ) {
global $wgWikilogNamespaces;
static $tested = false;
if ( !$tested ) {
$title = $parser->getTitle();
if ( !in_array( $title->getNamespace(), $wgWikilogNamespaces ) ) {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-out-of-context' )->text() )->text();
$parser->mOutput->addWarning( $warning );
}
$tested = true;
}
}
/**
* Parses an image link.
* Wrapper around parseMediaLink() that only returns images. Parser
* warnings are generated if the file is not an image, or if it is
* invalid.
*
* @return File instance, or NULL.
*/
private static function parseImageLink( $parser, $text ) {
$obj = self::parseMediaLink( $parser, $text );
if ( !$obj ) {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-invalid-file', htmlspecialchars( $text ) )->text() )->text();
$parser->mOutput->addWarning( $warning );
return null;
}
list( $t1, $t2, $file ) = $obj;
if ( !$file ) {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-file-not-found', htmlspecialchars( $t1 ) )->text() )->text();
$parser->mOutput->addWarning( $warning );
return null;
}
$type = $file->getMediaType();
if ( $type != MEDIATYPE_BITMAP && $type != MEDIATYPE_DRAWING ) {
$warning = wfMessage( 'wikilog-error-msg', wfMessage( 'wikilog-not-an-image', $file->getName() )->text() )->text();
$parser->mOutput->addWarning( $warning );
return null;
}
return $file;
}
/**
* Parses a media link.
* This is a very small subset of Parser::replaceInternalLinks() that
* parses a single image or media link, and returns the parsed text,
* as well as a File instance of the referenced media, if available.
*
* @return Three-element array containing the matched parts of the link,
* and the file object, or NULL.
*/
private static function parseMediaLink( $parser, $text ) {
$tc = Title::legalChars();
if ( !preg_match( "/\\[\\[([{$tc}]+)(?:\\|(.+?))?]]/", $text, $m ) )
return null;
$nt = Title::newFromText( $m[1] );
if ( !$nt )
return null;
$ns = $nt->getNamespace();
if ( $ns == NS_IMAGE || $ns == NS_MEDIA ) {
$parser->mOutput->addLink( $nt );
return @ array( $m[1], $m[2], wfFindFile( $nt ) );
} else {
return null;
}
}
}
/**
* Wikilog parser output. This class is first attached to the Parser as
* $parser->mExtWikilog, and then copied to the parser output
* $popt->mExtWikilog in WikilogParser::AfterTidy().
*/
class WikilogParserOutput
{
/* Item and Wikilog metadata */
public $mSummary = false;
public $mAuthors = array();
public $mTags = array();
/* Item metadata */
public $mPublish = false;
public $mPubDate = null;
/* Wikilog settings */
public $mIcon = null;
public $mLogo = null;
/* Comment metadata */
public $mComment = null;
/* Accessor functions, lacking... */
public function getAuthors() { return $this->mAuthors; }
public function getTags() { return $this->mTags; }
}