* Eliminated message mode (OT_MSG), using OT_PREPROCESS instead. As proposed on wikit...
[lhc/web/wiklou.git] / includes / Parser.php
1 <?php
2
3 /**
4 *
5 * File for Parser and related classes
6 *
7 * @addtogroup Parser
8 */
9
10
11 /**
12 * PHP Parser - Processes wiki markup (which uses a more user-friendly
13 * syntax, such as "[[link]]" for making links), and provides a one-way
14 * transformation of that wiki markup it into XHTML output / markup
15 * (which in turn the browser understands, and can display).
16 *
17 * <pre>
18 * There are five main entry points into the Parser class:
19 * parse()
20 * produces HTML output
21 * preSaveTransform().
22 * produces altered wiki markup.
23 * preprocess()
24 * removes HTML comments and expands templates
25 * cleanSig()
26 * Cleans a signature before saving it to preferences
27 * extractSections()
28 * Extracts sections from an article for section editing
29 *
30 * Globals used:
31 * objects: $wgLang, $wgContLang
32 *
33 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
34 *
35 * settings:
36 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
37 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
38 * $wgLocaltimezone, $wgAllowSpecialInclusion*,
39 * $wgMaxArticleSize*
40 *
41 * * only within ParserOptions
42 * </pre>
43 *
44 * @addtogroup Parser
45 */
46 class Parser
47 {
48 /**
49 * Update this version number when the ParserOutput format
50 * changes in an incompatible way, so the parser cache
51 * can automatically discard old data.
52 */
53 const VERSION = '1.6.4';
54
55 # Flags for Parser::setFunctionHook
56 # Also available as global constants from Defines.php
57 const SFH_NO_HASH = 1;
58 const SFH_OBJECT_ARGS = 2;
59
60 # Constants needed for external link processing
61 # Everything except bracket, space, or control characters
62 const EXT_LINK_URL_CLASS = '[^][<>"\\x00-\\x20\\x7F]';
63 const EXT_IMAGE_REGEX = '/^(http:\/\/|https:\/\/)([^][<>"\\x00-\\x20\\x7F]+)
64 \\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)gif|png|jpg|jpeg)$/Sx';
65
66 // State constants for the definition list colon extraction
67 const COLON_STATE_TEXT = 0;
68 const COLON_STATE_TAG = 1;
69 const COLON_STATE_TAGSTART = 2;
70 const COLON_STATE_CLOSETAG = 3;
71 const COLON_STATE_TAGSLASH = 4;
72 const COLON_STATE_COMMENT = 5;
73 const COLON_STATE_COMMENTDASH = 6;
74 const COLON_STATE_COMMENTDASHDASH = 7;
75
76 // Flags for preprocessToDom
77 const PTD_FOR_INCLUSION = 1;
78
79 /**#@+
80 * @private
81 */
82 # Persistent:
83 var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables,
84 $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerSuffix,
85 $mExtLinkBracketedRegex;
86
87 # Cleared with clearState():
88 var $mOutput, $mAutonumber, $mDTopen, $mStripState;
89 var $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
90 var $mInterwikiLinkHolders, $mLinkHolders;
91 var $mIncludeSizes, $mPPNodeCount, $mDefaultSort;
92 var $mTplExpandCache; // empty-frame expansion cache
93 var $mTplRedirCache, $mTplDomCache, $mHeadings;
94
95 # Temporary
96 # These are variables reset at least once per parse regardless of $clearState
97 var $mOptions, // ParserOptions object
98 $mTitle, // Title context, used for self-link rendering and similar things
99 $mOutputType, // Output type, one of the OT_xxx constants
100 $ot, // Shortcut alias, see setOutputType()
101 $mRevisionId, // ID to display in {{REVISIONID}} tags
102 $mRevisionTimestamp, // The timestamp of the specified revision ID
103 $mRevIdForTs; // The revision ID which was used to fetch the timestamp
104
105 /**#@-*/
106
107 /**
108 * Constructor
109 *
110 * @public
111 */
112 function __construct( $conf = array() ) {
113 $this->mTagHooks = array();
114 $this->mTransparentTagHooks = array();
115 $this->mFunctionHooks = array();
116 $this->mFunctionSynonyms = array( 0 => array(), 1 => array() );
117 $this->mStripList = array( 'nowiki', 'gallery' );
118 $this->mMarkerSuffix = "-QINU\x7f";
119 $this->mExtLinkBracketedRegex = '/\[(\b(' . wfUrlProtocols() . ')'.
120 '[^][<>"\\x00-\\x20\\x7F]+) *([^\]\\x0a\\x0d]*?)\]/S';
121 $this->mFirstCall = true;
122 }
123
124 /**
125 * Do various kinds of initialisation on the first call of the parser
126 */
127 function firstCallInit() {
128 if ( !$this->mFirstCall ) {
129 return;
130 }
131 $this->mFirstCall = false;
132
133 wfProfileIn( __METHOD__ );
134 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
135
136 $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
137
138 # Syntax for arguments (see self::setFunctionHook):
139 # "name for lookup in localized magic words array",
140 # function callback,
141 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
142 # instead of {{#int:...}})
143 $this->setFunctionHook( 'int', array( 'CoreParserFunctions', 'intFunction' ), SFH_NO_HASH );
144 $this->setFunctionHook( 'ns', array( 'CoreParserFunctions', 'ns' ), SFH_NO_HASH );
145 $this->setFunctionHook( 'urlencode', array( 'CoreParserFunctions', 'urlencode' ), SFH_NO_HASH );
146 $this->setFunctionHook( 'lcfirst', array( 'CoreParserFunctions', 'lcfirst' ), SFH_NO_HASH );
147 $this->setFunctionHook( 'ucfirst', array( 'CoreParserFunctions', 'ucfirst' ), SFH_NO_HASH );
148 $this->setFunctionHook( 'lc', array( 'CoreParserFunctions', 'lc' ), SFH_NO_HASH );
149 $this->setFunctionHook( 'uc', array( 'CoreParserFunctions', 'uc' ), SFH_NO_HASH );
150 $this->setFunctionHook( 'localurl', array( 'CoreParserFunctions', 'localurl' ), SFH_NO_HASH );
151 $this->setFunctionHook( 'localurle', array( 'CoreParserFunctions', 'localurle' ), SFH_NO_HASH );
152 $this->setFunctionHook( 'fullurl', array( 'CoreParserFunctions', 'fullurl' ), SFH_NO_HASH );
153 $this->setFunctionHook( 'fullurle', array( 'CoreParserFunctions', 'fullurle' ), SFH_NO_HASH );
154 $this->setFunctionHook( 'formatnum', array( 'CoreParserFunctions', 'formatnum' ), SFH_NO_HASH );
155 $this->setFunctionHook( 'grammar', array( 'CoreParserFunctions', 'grammar' ), SFH_NO_HASH );
156 $this->setFunctionHook( 'plural', array( 'CoreParserFunctions', 'plural' ), SFH_NO_HASH );
157 $this->setFunctionHook( 'numberofpages', array( 'CoreParserFunctions', 'numberofpages' ), SFH_NO_HASH );
158 $this->setFunctionHook( 'numberofusers', array( 'CoreParserFunctions', 'numberofusers' ), SFH_NO_HASH );
159 $this->setFunctionHook( 'numberofarticles', array( 'CoreParserFunctions', 'numberofarticles' ), SFH_NO_HASH );
160 $this->setFunctionHook( 'numberoffiles', array( 'CoreParserFunctions', 'numberoffiles' ), SFH_NO_HASH );
161 $this->setFunctionHook( 'numberofadmins', array( 'CoreParserFunctions', 'numberofadmins' ), SFH_NO_HASH );
162 $this->setFunctionHook( 'numberofedits', array( 'CoreParserFunctions', 'numberofedits' ), SFH_NO_HASH );
163 $this->setFunctionHook( 'language', array( 'CoreParserFunctions', 'language' ), SFH_NO_HASH );
164 $this->setFunctionHook( 'padleft', array( 'CoreParserFunctions', 'padleft' ), SFH_NO_HASH );
165 $this->setFunctionHook( 'padright', array( 'CoreParserFunctions', 'padright' ), SFH_NO_HASH );
166 $this->setFunctionHook( 'anchorencode', array( 'CoreParserFunctions', 'anchorencode' ), SFH_NO_HASH );
167 $this->setFunctionHook( 'special', array( 'CoreParserFunctions', 'special' ) );
168 $this->setFunctionHook( 'defaultsort', array( 'CoreParserFunctions', 'defaultsort' ), SFH_NO_HASH );
169 $this->setFunctionHook( 'filepath', array( 'CoreParserFunctions', 'filepath' ), SFH_NO_HASH );
170 $this->setFunctionHook( 'tag', array( 'CoreParserFunctions', 'tagObj' ), SFH_OBJECT_ARGS );
171
172 if ( $wgAllowDisplayTitle ) {
173 $this->setFunctionHook( 'displaytitle', array( 'CoreParserFunctions', 'displaytitle' ), SFH_NO_HASH );
174 }
175 if ( $wgAllowSlowParserFunctions ) {
176 $this->setFunctionHook( 'pagesinnamespace', array( 'CoreParserFunctions', 'pagesinnamespace' ), SFH_NO_HASH );
177 }
178
179 $this->initialiseVariables();
180
181 wfRunHooks( 'ParserFirstCallInit', array( &$this ) );
182 wfProfileOut( __METHOD__ );
183 }
184
185 /**
186 * Clear Parser state
187 *
188 * @private
189 */
190 function clearState() {
191 wfProfileIn( __METHOD__ );
192 if ( $this->mFirstCall ) {
193 $this->firstCallInit();
194 }
195 $this->mOutput = new ParserOutput;
196 $this->mAutonumber = 0;
197 $this->mLastSection = '';
198 $this->mDTopen = false;
199 $this->mIncludeCount = array();
200 $this->mStripState = new StripState;
201 $this->mArgStack = false;
202 $this->mInPre = false;
203 $this->mInterwikiLinkHolders = array(
204 'texts' => array(),
205 'titles' => array()
206 );
207 $this->mLinkHolders = array(
208 'namespaces' => array(),
209 'dbkeys' => array(),
210 'queries' => array(),
211 'texts' => array(),
212 'titles' => array()
213 );
214 $this->mRevisionTimestamp = $this->mRevisionId = null;
215
216 /**
217 * Prefix for temporary replacement strings for the multipass parser.
218 * \x07 should never appear in input as it's disallowed in XML.
219 * Using it at the front also gives us a little extra robustness
220 * since it shouldn't match when butted up against identifier-like
221 * string constructs.
222 *
223 * Must not consist of all title characters, or else it will change
224 * the behaviour of <nowiki> in a link.
225 */
226 #$this->mUniqPrefix = "\x07UNIQ" . Parser::getRandomString();
227 $this->mUniqPrefix = "\x7fUNIQ" . Parser::getRandomString();
228
229 # Clear these on every parse, bug 4549
230 $this->mTplExpandCache = $this->mTplRedirCache = $this->mTplDomCache = array();
231
232 $this->mShowToc = true;
233 $this->mForceTocPosition = false;
234 $this->mIncludeSizes = array(
235 'post-expand' => 0,
236 'arg' => 0,
237 );
238 $this->mPPNodeCount = 0;
239 $this->mDefaultSort = false;
240 $this->mHeadings = array();
241
242 wfRunHooks( 'ParserClearState', array( &$this ) );
243 wfProfileOut( __METHOD__ );
244 }
245
246 function setOutputType( $ot ) {
247 $this->mOutputType = $ot;
248 // Shortcut alias
249 $this->ot = array(
250 'html' => $ot == OT_HTML,
251 'wiki' => $ot == OT_WIKI,
252 'pre' => $ot == OT_PREPROCESS,
253 );
254 }
255
256 /**
257 * Set the context title
258 */
259 function setTitle( $t ) {
260 if ( !$t || $t instanceof FakeTitle ) {
261 $t = Title::newFromText( 'NO TITLE' );
262 }
263 if ( strval( $t->getFragment() ) !== '' ) {
264 # Strip the fragment to avoid various odd effects
265 $this->mTitle = clone $t;
266 $this->mTitle->setFragment( '' );
267 } else {
268 $this->mTitle = $t;
269 }
270 }
271
272 /**
273 * Accessor for mUniqPrefix.
274 *
275 * @public
276 */
277 function uniqPrefix() {
278 if( !isset( $this->mUniqPrefix ) ) {
279 // @fixme this is probably *horribly wrong*
280 // LanguageConverter seems to want $wgParser's uniqPrefix, however
281 // if this is called for a parser cache hit, the parser may not
282 // have ever been initialized in the first place.
283 // Not really sure what the heck is supposed to be going on here.
284 return '';
285 //throw new MWException( "Accessing uninitialized mUniqPrefix" );
286 }
287 return $this->mUniqPrefix;
288 }
289
290 /**
291 * Convert wikitext to HTML
292 * Do not call this function recursively.
293 *
294 * @param string $text Text we want to parse
295 * @param Title &$title A title object
296 * @param array $options
297 * @param boolean $linestart
298 * @param boolean $clearState
299 * @param int $revid number to pass in {{REVISIONID}}
300 * @return ParserOutput a ParserOutput
301 */
302 public function parse( $text, &$title, $options, $linestart = true, $clearState = true, $revid = null ) {
303 /**
304 * First pass--just handle <nowiki> sections, pass the rest off
305 * to internalParse() which does all the real work.
306 */
307
308 global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang;
309 $fname = 'Parser::parse-' . wfGetCaller();
310 wfProfileIn( __METHOD__ );
311 wfProfileIn( $fname );
312
313 if ( $clearState ) {
314 $this->clearState();
315 }
316
317 $this->mOptions = $options;
318 $this->setTitle( $title );
319 $oldRevisionId = $this->mRevisionId;
320 $oldRevisionTimestamp = $this->mRevisionTimestamp;
321 if( $revid !== null ) {
322 $this->mRevisionId = $revid;
323 $this->mRevisionTimestamp = null;
324 }
325 $this->setOutputType( OT_HTML );
326 wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
327 # No more strip!
328 wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
329 $text = $this->internalParse( $text );
330 $text = $this->mStripState->unstripGeneral( $text );
331
332 # Clean up special characters, only run once, next-to-last before doBlockLevels
333 $fixtags = array(
334 # french spaces, last one Guillemet-left
335 # only if there is something before the space
336 '/(.) (?=\\?|:|;|!|%|\\302\\273)/' => '\\1&nbsp;\\2',
337 # french spaces, Guillemet-right
338 '/(\\302\\253) /' => '\\1&nbsp;',
339 );
340 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
341
342 # only once and last
343 $text = $this->doBlockLevels( $text, $linestart );
344
345 $this->replaceLinkHolders( $text );
346
347 # the position of the parserConvert() call should not be changed. it
348 # assumes that the links are all replaced and the only thing left
349 # is the <nowiki> mark.
350 # Side-effects: this calls $this->mOutput->setTitleText()
351 $text = $wgContLang->parserConvert( $text, $this );
352
353 $text = $this->mStripState->unstripNoWiki( $text );
354
355 wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
356
357 //!JF Move to its own function
358
359 $uniq_prefix = $this->mUniqPrefix;
360 $matches = array();
361 $elements = array_keys( $this->mTransparentTagHooks );
362 $text = Parser::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
363
364 foreach( $matches as $marker => $data ) {
365 list( $element, $content, $params, $tag ) = $data;
366 $tagName = strtolower( $element );
367 if( isset( $this->mTransparentTagHooks[$tagName] ) ) {
368 $output = call_user_func_array( $this->mTransparentTagHooks[$tagName],
369 array( $content, $params, $this ) );
370 } else {
371 $output = $tag;
372 }
373 $this->mStripState->general->setPair( $marker, $output );
374 }
375 $text = $this->mStripState->unstripGeneral( $text );
376
377 $text = Sanitizer::normalizeCharReferences( $text );
378
379 if (($wgUseTidy and $this->mOptions->mTidy) or $wgAlwaysUseTidy) {
380 $text = Parser::tidy($text);
381 } else {
382 # attempt to sanitize at least some nesting problems
383 # (bug #2702 and quite a few others)
384 $tidyregs = array(
385 # ''Something [http://www.cool.com cool''] -->
386 # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
387 '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
388 '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
389 # fix up an anchor inside another anchor, only
390 # at least for a single single nested link (bug 3695)
391 '/(<a[^>]+>)([^<]*)(<a[^>]+>[^<]*)<\/a>(.*)<\/a>/' =>
392 '\\1\\2</a>\\3</a>\\1\\4</a>',
393 # fix div inside inline elements- doBlockLevels won't wrap a line which
394 # contains a div, so fix it up here; replace
395 # div with escaped text
396 '/(<([aib]) [^>]+>)([^<]*)(<div([^>]*)>)(.*)(<\/div>)([^<]*)(<\/\\2>)/' =>
397 '\\1\\3&lt;div\\5&gt;\\6&lt;/div&gt;\\8\\9',
398 # remove empty italic or bold tag pairs, some
399 # introduced by rules above
400 '/<([bi])><\/\\1>/' => '',
401 );
402
403 $text = preg_replace(
404 array_keys( $tidyregs ),
405 array_values( $tidyregs ),
406 $text );
407 }
408
409 wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
410
411 # Information on include size limits, for the benefit of users who try to skirt them
412 if ( $this->mOptions->getEnableLimitReport() ) {
413 $max = $this->mOptions->getMaxIncludeSize();
414 $limitReport =
415 "NewPP limit report\n" .
416 "Preprocessor node count: {$this->mPPNodeCount}/{$this->mOptions->mMaxPPNodeCount}\n" .
417 "Post-expand include size: {$this->mIncludeSizes['post-expand']}/$max bytes\n" .
418 "Template argument size: {$this->mIncludeSizes['arg']}/$max bytes\n";
419 wfRunHooks( 'ParserLimitReport', array( $this, &$limitReport ) );
420 $text .= "\n<!-- \n$limitReport-->\n";
421 }
422 $this->mOutput->setText( $text );
423 $this->mRevisionId = $oldRevisionId;
424 $this->mRevisionTimestamp = $oldRevisionTimestamp;
425 wfProfileOut( $fname );
426 wfProfileOut( __METHOD__ );
427
428 return $this->mOutput;
429 }
430
431 /**
432 * Recursive parser entry point that can be called from an extension tag
433 * hook.
434 */
435 function recursiveTagParse( $text ) {
436 wfProfileIn( __METHOD__ );
437 wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
438 wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
439 $text = $this->internalParse( $text );
440 wfProfileOut( __METHOD__ );
441 return $text;
442 }
443
444 /**
445 * Expand templates and variables in the text, producing valid, static wikitext.
446 * Also removes comments.
447 */
448 function preprocess( $text, $title, $options, $revid = null ) {
449 wfProfileIn( __METHOD__ );
450 $this->clearState();
451 $this->setOutputType( OT_PREPROCESS );
452 $this->mOptions = $options;
453 $this->setTitle( $title );
454 if( $revid !== null ) {
455 $this->mRevisionId = $revid;
456 }
457 wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
458 wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
459 $text = $this->replaceVariables( $text );
460 $text = $this->mStripState->unstripBoth( $text );
461 wfProfileOut( __METHOD__ );
462 return $text;
463 }
464
465 /**
466 * Get a random string
467 *
468 * @private
469 * @static
470 */
471 function getRandomString() {
472 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
473 }
474
475 function &getTitle() { return $this->mTitle; }
476 function getOptions() { return $this->mOptions; }
477
478 function getFunctionLang() {
479 global $wgLang, $wgContLang;
480 return $this->mOptions->getInterfaceMessage() ? $wgLang : $wgContLang;
481 }
482
483 /**
484 * Replaces all occurrences of HTML-style comments and the given tags
485 * in the text with a random marker and returns teh next text. The output
486 * parameter $matches will be an associative array filled with data in
487 * the form:
488 * 'UNIQ-xxxxx' => array(
489 * 'element',
490 * 'tag content',
491 * array( 'param' => 'x' ),
492 * '<element param="x">tag content</element>' ) )
493 *
494 * @param $elements list of element names. Comments are always extracted.
495 * @param $text Source text string.
496 * @param $uniq_prefix
497 *
498 * @public
499 * @static
500 */
501 function extractTagsAndParams($elements, $text, &$matches, $uniq_prefix = ''){
502 static $n = 1;
503 $stripped = '';
504 $matches = array();
505
506 $taglist = implode( '|', $elements );
507 $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
508
509 while ( '' != $text ) {
510 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
511 $stripped .= $p[0];
512 if( count( $p ) < 5 ) {
513 break;
514 }
515 if( count( $p ) > 5 ) {
516 // comment
517 $element = $p[4];
518 $attributes = '';
519 $close = '';
520 $inside = $p[5];
521 } else {
522 // tag
523 $element = $p[1];
524 $attributes = $p[2];
525 $close = $p[3];
526 $inside = $p[4];
527 }
528
529 $marker = "$uniq_prefix-$element-" . sprintf('%08X', $n++) . $this->mMarkerSuffix;
530 $stripped .= $marker;
531
532 if ( $close === '/>' ) {
533 // Empty element tag, <tag />
534 $content = null;
535 $text = $inside;
536 $tail = null;
537 } else {
538 if( $element == '!--' ) {
539 $end = '/(-->)/';
540 } else {
541 $end = "/(<\\/$element\\s*>)/i";
542 }
543 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE );
544 $content = $q[0];
545 if( count( $q ) < 3 ) {
546 # No end tag -- let it run out to the end of the text.
547 $tail = '';
548 $text = '';
549 } else {
550 $tail = $q[1];
551 $text = $q[2];
552 }
553 }
554
555 $matches[$marker] = array( $element,
556 $content,
557 Sanitizer::decodeTagAttributes( $attributes ),
558 "<$element$attributes$close$content$tail" );
559 }
560 return $stripped;
561 }
562
563 /**
564 * Get a list of strippable XML-like elements
565 */
566 function getStripList() {
567 global $wgRawHtml;
568 $elements = $this->mStripList;
569 if( $wgRawHtml ) {
570 $elements[] = 'html';
571 }
572 if( $this->mOptions->getUseTeX() ) {
573 $elements[] = 'math';
574 }
575 return $elements;
576 }
577
578 /**
579 * @deprecated use replaceVariables
580 */
581 function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
582 return $text;
583 }
584
585 /**
586 * Restores pre, math, and other extensions removed by strip()
587 *
588 * always call unstripNoWiki() after this one
589 * @private
590 * @deprecated use $this->mStripState->unstrip()
591 */
592 function unstrip( $text, $state ) {
593 return $state->unstripGeneral( $text );
594 }
595
596 /**
597 * Always call this after unstrip() to preserve the order
598 *
599 * @private
600 * @deprecated use $this->mStripState->unstrip()
601 */
602 function unstripNoWiki( $text, $state ) {
603 return $state->unstripNoWiki( $text );
604 }
605
606 /**
607 * @deprecated use $this->mStripState->unstripBoth()
608 */
609 function unstripForHTML( $text ) {
610 return $this->mStripState->unstripBoth( $text );
611 }
612
613 /**
614 * Add an item to the strip state
615 * Returns the unique tag which must be inserted into the stripped text
616 * The tag will be replaced with the original text in unstrip()
617 *
618 * @private
619 */
620 function insertStripItem( $text ) {
621 static $n = 0;
622 $rnd = "{$this->mUniqPrefix}-item-$n-{$this->mMarkerSuffix}";
623 ++$n;
624 $this->mStripState->general->setPair( $rnd, $text );
625 return $rnd;
626 }
627
628 /**
629 * Interface with html tidy, used if $wgUseTidy = true.
630 * If tidy isn't able to correct the markup, the original will be
631 * returned in all its glory with a warning comment appended.
632 *
633 * Either the external tidy program or the in-process tidy extension
634 * will be used depending on availability. Override the default
635 * $wgTidyInternal setting to disable the internal if it's not working.
636 *
637 * @param string $text Hideous HTML input
638 * @return string Corrected HTML output
639 * @public
640 * @static
641 */
642 function tidy( $text ) {
643 global $wgTidyInternal;
644 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
645 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
646 '<head><title>test</title></head><body>'.$text.'</body></html>';
647 if( $wgTidyInternal ) {
648 $correctedtext = Parser::internalTidy( $wrappedtext );
649 } else {
650 $correctedtext = Parser::externalTidy( $wrappedtext );
651 }
652 if( is_null( $correctedtext ) ) {
653 wfDebug( "Tidy error detected!\n" );
654 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
655 }
656 return $correctedtext;
657 }
658
659 /**
660 * Spawn an external HTML tidy process and get corrected markup back from it.
661 *
662 * @private
663 * @static
664 */
665 function externalTidy( $text ) {
666 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
667 $fname = 'Parser::externalTidy';
668 wfProfileIn( $fname );
669
670 $cleansource = '';
671 $opts = ' -utf8';
672
673 $descriptorspec = array(
674 0 => array('pipe', 'r'),
675 1 => array('pipe', 'w'),
676 2 => array('file', wfGetNull(), 'a')
677 );
678 $pipes = array();
679 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
680 if (is_resource($process)) {
681 // Theoretically, this style of communication could cause a deadlock
682 // here. If the stdout buffer fills up, then writes to stdin could
683 // block. This doesn't appear to happen with tidy, because tidy only
684 // writes to stdout after it's finished reading from stdin. Search
685 // for tidyParseStdin and tidySaveStdout in console/tidy.c
686 fwrite($pipes[0], $text);
687 fclose($pipes[0]);
688 while (!feof($pipes[1])) {
689 $cleansource .= fgets($pipes[1], 1024);
690 }
691 fclose($pipes[1]);
692 proc_close($process);
693 }
694
695 wfProfileOut( $fname );
696
697 if( $cleansource == '' && $text != '') {
698 // Some kind of error happened, so we couldn't get the corrected text.
699 // Just give up; we'll use the source text and append a warning.
700 return null;
701 } else {
702 return $cleansource;
703 }
704 }
705
706 /**
707 * Use the HTML tidy PECL extension to use the tidy library in-process,
708 * saving the overhead of spawning a new process.
709 *
710 * 'pear install tidy' should be able to compile the extension module.
711 *
712 * @private
713 * @static
714 */
715 function internalTidy( $text ) {
716 global $wgTidyConf, $IP, $wgDebugTidy;
717 $fname = 'Parser::internalTidy';
718 wfProfileIn( $fname );
719
720 $tidy = new tidy;
721 $tidy->parseString( $text, $wgTidyConf, 'utf8' );
722 $tidy->cleanRepair();
723 if( $tidy->getStatus() == 2 ) {
724 // 2 is magic number for fatal error
725 // http://www.php.net/manual/en/function.tidy-get-status.php
726 $cleansource = null;
727 } else {
728 $cleansource = tidy_get_output( $tidy );
729 }
730 if ( $wgDebugTidy && $tidy->getStatus() > 0 ) {
731 $cleansource .= "<!--\nTidy reports:\n" .
732 str_replace( '-->', '--&gt;', $tidy->errorBuffer ) .
733 "\n-->";
734 }
735
736 wfProfileOut( $fname );
737 return $cleansource;
738 }
739
740 /**
741 * parse the wiki syntax used to render tables
742 *
743 * @private
744 */
745 function doTableStuff ( $text ) {
746 $fname = 'Parser::doTableStuff';
747 wfProfileIn( $fname );
748
749 $lines = explode ( "\n" , $text );
750 $td_history = array (); // Is currently a td tag open?
751 $last_tag_history = array (); // Save history of last lag activated (td, th or caption)
752 $tr_history = array (); // Is currently a tr tag open?
753 $tr_attributes = array (); // history of tr attributes
754 $has_opened_tr = array(); // Did this table open a <tr> element?
755 $indent_level = 0; // indent level of the table
756 foreach ( $lines as $key => $line )
757 {
758 $line = trim ( $line );
759
760 if( $line == '' ) { // empty line, go to next line
761 continue;
762 }
763 $first_character = $line{0};
764 $matches = array();
765
766 if ( preg_match( '/^(:*)\{\|(.*)$/' , $line , $matches ) ) {
767 // First check if we are starting a new table
768 $indent_level = strlen( $matches[1] );
769
770 $attributes = $this->mStripState->unstripBoth( $matches[2] );
771 $attributes = Sanitizer::fixTagAttributes ( $attributes , 'table' );
772
773 $lines[$key] = str_repeat( '<dl><dd>' , $indent_level ) . "<table{$attributes}>";
774 array_push ( $td_history , false );
775 array_push ( $last_tag_history , '' );
776 array_push ( $tr_history , false );
777 array_push ( $tr_attributes , '' );
778 array_push ( $has_opened_tr , false );
779 } else if ( count ( $td_history ) == 0 ) {
780 // Don't do any of the following
781 continue;
782 } else if ( substr ( $line , 0 , 2 ) == '|}' ) {
783 // We are ending a table
784 $line = '</table>' . substr ( $line , 2 );
785 $last_tag = array_pop ( $last_tag_history );
786
787 if ( !array_pop ( $has_opened_tr ) ) {
788 $line = "<tr><td></td></tr>{$line}";
789 }
790
791 if ( array_pop ( $tr_history ) ) {
792 $line = "</tr>{$line}";
793 }
794
795 if ( array_pop ( $td_history ) ) {
796 $line = "</{$last_tag}>{$line}";
797 }
798 array_pop ( $tr_attributes );
799 $lines[$key] = $line . str_repeat( '</dd></dl>' , $indent_level );
800 } else if ( substr ( $line , 0 , 2 ) == '|-' ) {
801 // Now we have a table row
802 $line = preg_replace( '#^\|-+#', '', $line );
803
804 // Whats after the tag is now only attributes
805 $attributes = $this->mStripState->unstripBoth( $line );
806 $attributes = Sanitizer::fixTagAttributes ( $attributes , 'tr' );
807 array_pop ( $tr_attributes );
808 array_push ( $tr_attributes , $attributes );
809
810 $line = '';
811 $last_tag = array_pop ( $last_tag_history );
812 array_pop ( $has_opened_tr );
813 array_push ( $has_opened_tr , true );
814
815 if ( array_pop ( $tr_history ) ) {
816 $line = '</tr>';
817 }
818
819 if ( array_pop ( $td_history ) ) {
820 $line = "</{$last_tag}>{$line}";
821 }
822
823 $lines[$key] = $line;
824 array_push ( $tr_history , false );
825 array_push ( $td_history , false );
826 array_push ( $last_tag_history , '' );
827 }
828 else if ( $first_character == '|' || $first_character == '!' || substr ( $line , 0 , 2 ) == '|+' ) {
829 // This might be cell elements, td, th or captions
830 if ( substr ( $line , 0 , 2 ) == '|+' ) {
831 $first_character = '+';
832 $line = substr ( $line , 1 );
833 }
834
835 $line = substr ( $line , 1 );
836
837 if ( $first_character == '!' ) {
838 $line = str_replace ( '!!' , '||' , $line );
839 }
840
841 // Split up multiple cells on the same line.
842 // FIXME : This can result in improper nesting of tags processed
843 // by earlier parser steps, but should avoid splitting up eg
844 // attribute values containing literal "||".
845 $cells = StringUtils::explodeMarkup( '||' , $line );
846
847 $lines[$key] = '';
848
849 // Loop through each table cell
850 foreach ( $cells as $cell )
851 {
852 $previous = '';
853 if ( $first_character != '+' )
854 {
855 $tr_after = array_pop ( $tr_attributes );
856 if ( !array_pop ( $tr_history ) ) {
857 $previous = "<tr{$tr_after}>\n";
858 }
859 array_push ( $tr_history , true );
860 array_push ( $tr_attributes , '' );
861 array_pop ( $has_opened_tr );
862 array_push ( $has_opened_tr , true );
863 }
864
865 $last_tag = array_pop ( $last_tag_history );
866
867 if ( array_pop ( $td_history ) ) {
868 $previous = "</{$last_tag}>{$previous}";
869 }
870
871 if ( $first_character == '|' ) {
872 $last_tag = 'td';
873 } else if ( $first_character == '!' ) {
874 $last_tag = 'th';
875 } else if ( $first_character == '+' ) {
876 $last_tag = 'caption';
877 } else {
878 $last_tag = '';
879 }
880
881 array_push ( $last_tag_history , $last_tag );
882
883 // A cell could contain both parameters and data
884 $cell_data = explode ( '|' , $cell , 2 );
885
886 // Bug 553: Note that a '|' inside an invalid link should not
887 // be mistaken as delimiting cell parameters
888 if ( strpos( $cell_data[0], '[[' ) !== false ) {
889 $cell = "{$previous}<{$last_tag}>{$cell}";
890 } else if ( count ( $cell_data ) == 1 )
891 $cell = "{$previous}<{$last_tag}>{$cell_data[0]}";
892 else {
893 $attributes = $this->mStripState->unstripBoth( $cell_data[0] );
894 $attributes = Sanitizer::fixTagAttributes( $attributes , $last_tag );
895 $cell = "{$previous}<{$last_tag}{$attributes}>{$cell_data[1]}";
896 }
897
898 $lines[$key] .= $cell;
899 array_push ( $td_history , true );
900 }
901 }
902 }
903
904 // Closing open td, tr && table
905 while ( count ( $td_history ) > 0 )
906 {
907 if ( array_pop ( $td_history ) ) {
908 $lines[] = '</td>' ;
909 }
910 if ( array_pop ( $tr_history ) ) {
911 $lines[] = '</tr>' ;
912 }
913 if ( !array_pop ( $has_opened_tr ) ) {
914 $lines[] = "<tr><td></td></tr>" ;
915 }
916
917 $lines[] = '</table>' ;
918 }
919
920 $output = implode ( "\n" , $lines ) ;
921
922 // special case: don't return empty table
923 if( $output == "<table>\n<tr><td></td></tr>\n</table>" ) {
924 $output = '';
925 }
926
927 wfProfileOut( $fname );
928
929 return $output;
930 }
931
932 /**
933 * Helper function for parse() that transforms wiki markup into
934 * HTML. Only called for $mOutputType == OT_HTML.
935 *
936 * @private
937 */
938 function internalParse( $text ) {
939 $isMain = true;
940 $fname = 'Parser::internalParse';
941 wfProfileIn( $fname );
942
943 # Hook to suspend the parser in this state
944 if ( !wfRunHooks( 'ParserBeforeInternalParse', array( &$this, &$text, &$this->mStripState ) ) ) {
945 wfProfileOut( $fname );
946 return $text ;
947 }
948
949 $text = $this->replaceVariables( $text );
950 $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'attributeStripCallback' ), false, array_keys( $this->mTransparentTagHooks ) );
951 wfRunHooks( 'InternalParseBeforeLinks', array( &$this, &$text, &$this->mStripState ) );
952
953 // Tables need to come after variable replacement for things to work
954 // properly; putting them before other transformations should keep
955 // exciting things like link expansions from showing up in surprising
956 // places.
957 $text = $this->doTableStuff( $text );
958
959 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
960
961 $text = $this->stripToc( $text );
962 $this->stripNoGallery( $text );
963 $text = $this->doHeadings( $text );
964 if($this->mOptions->getUseDynamicDates()) {
965 $df =& DateFormatter::getInstance();
966 $text = $df->reformat( $this->mOptions->getDateFormat(), $text );
967 }
968 $text = $this->doAllQuotes( $text );
969 $text = $this->replaceInternalLinks( $text );
970 $text = $this->replaceExternalLinks( $text );
971
972 # replaceInternalLinks may sometimes leave behind
973 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
974 $text = str_replace($this->mUniqPrefix."NOPARSE", "", $text);
975
976 $text = $this->doMagicLinks( $text );
977 $text = $this->formatHeadings( $text, $isMain );
978
979 wfProfileOut( $fname );
980 return $text;
981 }
982
983 /**
984 * Replace special strings like "ISBN xxx" and "RFC xxx" with
985 * magic external links.
986 *
987 * @private
988 */
989 function doMagicLinks( $text ) {
990 wfProfileIn( __METHOD__ );
991 $text = preg_replace_callback(
992 '!(?: # Start cases
993 <a.*?</a> | # Skip link text
994 <.*?> | # Skip stuff inside HTML elements
995 (?:RFC|PMID)\s+([0-9]+) | # RFC or PMID, capture number as m[1]
996 ISBN\s+(\b # ISBN, capture number as m[2]
997 (?: 97[89] [\ \-]? )? # optional 13-digit ISBN prefix
998 (?: [0-9] [\ \-]? ){9} # 9 digits with opt. delimiters
999 [0-9Xx] # check digit
1000 \b)
1001 )!x', array( &$this, 'magicLinkCallback' ), $text );
1002 wfProfileOut( __METHOD__ );
1003 return $text;
1004 }
1005
1006 function magicLinkCallback( $m ) {
1007 if ( substr( $m[0], 0, 1 ) == '<' ) {
1008 # Skip HTML element
1009 return $m[0];
1010 } elseif ( substr( $m[0], 0, 4 ) == 'ISBN' ) {
1011 $isbn = $m[2];
1012 $num = strtr( $isbn, array(
1013 '-' => '',
1014 ' ' => '',
1015 'x' => 'X',
1016 ));
1017 $titleObj = SpecialPage::getTitleFor( 'Booksources' );
1018 $text = '<a href="' .
1019 $titleObj->escapeLocalUrl( "isbn=$num" ) .
1020 "\" class=\"internal\">ISBN $isbn</a>";
1021 } else {
1022 if ( substr( $m[0], 0, 3 ) == 'RFC' ) {
1023 $keyword = 'RFC';
1024 $urlmsg = 'rfcurl';
1025 $id = $m[1];
1026 } elseif ( substr( $m[0], 0, 4 ) == 'PMID' ) {
1027 $keyword = 'PMID';
1028 $urlmsg = 'pubmedurl';
1029 $id = $m[1];
1030 } else {
1031 throw new MWException( __METHOD__.': unrecognised match type "' .
1032 substr($m[0], 0, 20 ) . '"' );
1033 }
1034
1035 $url = wfMsg( $urlmsg, $id);
1036 $sk = $this->mOptions->getSkin();
1037 $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
1038 $text = "<a href=\"{$url}\"{$la}>{$keyword} {$id}</a>";
1039 }
1040 return $text;
1041 }
1042
1043 /**
1044 * Parse headers and return html
1045 *
1046 * @private
1047 */
1048 function doHeadings( $text ) {
1049 $fname = 'Parser::doHeadings';
1050 wfProfileIn( $fname );
1051 for ( $i = 6; $i >= 1; --$i ) {
1052 $h = str_repeat( '=', $i );
1053 $text = preg_replace( "/^$h(.+)$h\\s*$/m",
1054 "<h$i>\\1</h$i>", $text );
1055 }
1056 wfProfileOut( $fname );
1057 return $text;
1058 }
1059
1060 /**
1061 * Replace single quotes with HTML markup
1062 * @private
1063 * @return string the altered text
1064 */
1065 function doAllQuotes( $text ) {
1066 $fname = 'Parser::doAllQuotes';
1067 wfProfileIn( $fname );
1068 $outtext = '';
1069 $lines = explode( "\n", $text );
1070 foreach ( $lines as $line ) {
1071 $outtext .= $this->doQuotes ( $line ) . "\n";
1072 }
1073 $outtext = substr($outtext, 0,-1);
1074 wfProfileOut( $fname );
1075 return $outtext;
1076 }
1077
1078 /**
1079 * Helper function for doAllQuotes()
1080 */
1081 public function doQuotes( $text ) {
1082 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1083 if ( count( $arr ) == 1 )
1084 return $text;
1085 else
1086 {
1087 # First, do some preliminary work. This may shift some apostrophes from
1088 # being mark-up to being text. It also counts the number of occurrences
1089 # of bold and italics mark-ups.
1090 $i = 0;
1091 $numbold = 0;
1092 $numitalics = 0;
1093 foreach ( $arr as $r )
1094 {
1095 if ( ( $i % 2 ) == 1 )
1096 {
1097 # If there are ever four apostrophes, assume the first is supposed to
1098 # be text, and the remaining three constitute mark-up for bold text.
1099 if ( strlen( $arr[$i] ) == 4 )
1100 {
1101 $arr[$i-1] .= "'";
1102 $arr[$i] = "'''";
1103 }
1104 # If there are more than 5 apostrophes in a row, assume they're all
1105 # text except for the last 5.
1106 else if ( strlen( $arr[$i] ) > 5 )
1107 {
1108 $arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
1109 $arr[$i] = "'''''";
1110 }
1111 # Count the number of occurrences of bold and italics mark-ups.
1112 # We are not counting sequences of five apostrophes.
1113 if ( strlen( $arr[$i] ) == 2 ) { $numitalics++; }
1114 else if ( strlen( $arr[$i] ) == 3 ) { $numbold++; }
1115 else if ( strlen( $arr[$i] ) == 5 ) { $numitalics++; $numbold++; }
1116 }
1117 $i++;
1118 }
1119
1120 # If there is an odd number of both bold and italics, it is likely
1121 # that one of the bold ones was meant to be an apostrophe followed
1122 # by italics. Which one we cannot know for certain, but it is more
1123 # likely to be one that has a single-letter word before it.
1124 if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) )
1125 {
1126 $i = 0;
1127 $firstsingleletterword = -1;
1128 $firstmultiletterword = -1;
1129 $firstspace = -1;
1130 foreach ( $arr as $r )
1131 {
1132 if ( ( $i % 2 == 1 ) and ( strlen( $r ) == 3 ) )
1133 {
1134 $x1 = substr ($arr[$i-1], -1);
1135 $x2 = substr ($arr[$i-1], -2, 1);
1136 if ($x1 == ' ') {
1137 if ($firstspace == -1) $firstspace = $i;
1138 } else if ($x2 == ' ') {
1139 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
1140 } else {
1141 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
1142 }
1143 }
1144 $i++;
1145 }
1146
1147 # If there is a single-letter word, use it!
1148 if ($firstsingleletterword > -1)
1149 {
1150 $arr [ $firstsingleletterword ] = "''";
1151 $arr [ $firstsingleletterword-1 ] .= "'";
1152 }
1153 # If not, but there's a multi-letter word, use that one.
1154 else if ($firstmultiletterword > -1)
1155 {
1156 $arr [ $firstmultiletterword ] = "''";
1157 $arr [ $firstmultiletterword-1 ] .= "'";
1158 }
1159 # ... otherwise use the first one that has neither.
1160 # (notice that it is possible for all three to be -1 if, for example,
1161 # there is only one pentuple-apostrophe in the line)
1162 else if ($firstspace > -1)
1163 {
1164 $arr [ $firstspace ] = "''";
1165 $arr [ $firstspace-1 ] .= "'";
1166 }
1167 }
1168
1169 # Now let's actually convert our apostrophic mush to HTML!
1170 $output = '';
1171 $buffer = '';
1172 $state = '';
1173 $i = 0;
1174 foreach ($arr as $r)
1175 {
1176 if (($i % 2) == 0)
1177 {
1178 if ($state == 'both')
1179 $buffer .= $r;
1180 else
1181 $output .= $r;
1182 }
1183 else
1184 {
1185 if (strlen ($r) == 2)
1186 {
1187 if ($state == 'i')
1188 { $output .= '</i>'; $state = ''; }
1189 else if ($state == 'bi')
1190 { $output .= '</i>'; $state = 'b'; }
1191 else if ($state == 'ib')
1192 { $output .= '</b></i><b>'; $state = 'b'; }
1193 else if ($state == 'both')
1194 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
1195 else # $state can be 'b' or ''
1196 { $output .= '<i>'; $state .= 'i'; }
1197 }
1198 else if (strlen ($r) == 3)
1199 {
1200 if ($state == 'b')
1201 { $output .= '</b>'; $state = ''; }
1202 else if ($state == 'bi')
1203 { $output .= '</i></b><i>'; $state = 'i'; }
1204 else if ($state == 'ib')
1205 { $output .= '</b>'; $state = 'i'; }
1206 else if ($state == 'both')
1207 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
1208 else # $state can be 'i' or ''
1209 { $output .= '<b>'; $state .= 'b'; }
1210 }
1211 else if (strlen ($r) == 5)
1212 {
1213 if ($state == 'b')
1214 { $output .= '</b><i>'; $state = 'i'; }
1215 else if ($state == 'i')
1216 { $output .= '</i><b>'; $state = 'b'; }
1217 else if ($state == 'bi')
1218 { $output .= '</i></b>'; $state = ''; }
1219 else if ($state == 'ib')
1220 { $output .= '</b></i>'; $state = ''; }
1221 else if ($state == 'both')
1222 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
1223 else # ($state == '')
1224 { $buffer = ''; $state = 'both'; }
1225 }
1226 }
1227 $i++;
1228 }
1229 # Now close all remaining tags. Notice that the order is important.
1230 if ($state == 'b' || $state == 'ib')
1231 $output .= '</b>';
1232 if ($state == 'i' || $state == 'bi' || $state == 'ib')
1233 $output .= '</i>';
1234 if ($state == 'bi')
1235 $output .= '</b>';
1236 # There might be lonely ''''', so make sure we have a buffer
1237 if ($state == 'both' && $buffer)
1238 $output .= '<b><i>'.$buffer.'</i></b>';
1239 return $output;
1240 }
1241 }
1242
1243 /**
1244 * Replace external links
1245 *
1246 * Note: this is all very hackish and the order of execution matters a lot.
1247 * Make sure to run maintenance/parserTests.php if you change this code.
1248 *
1249 * @private
1250 */
1251 function replaceExternalLinks( $text ) {
1252 global $wgContLang;
1253 $fname = 'Parser::replaceExternalLinks';
1254 wfProfileIn( $fname );
1255
1256 $sk = $this->mOptions->getSkin();
1257
1258 $bits = preg_split( $this->mExtLinkBracketedRegex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1259
1260 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
1261
1262 $i = 0;
1263 while ( $i<count( $bits ) ) {
1264 $url = $bits[$i++];
1265 $protocol = $bits[$i++];
1266 $text = $bits[$i++];
1267 $trail = $bits[$i++];
1268
1269 # The characters '<' and '>' (which were escaped by
1270 # removeHTMLtags()) should not be included in
1271 # URLs, per RFC 2396.
1272 $m2 = array();
1273 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
1274 $text = substr($url, $m2[0][1]) . ' ' . $text;
1275 $url = substr($url, 0, $m2[0][1]);
1276 }
1277
1278 # If the link text is an image URL, replace it with an <img> tag
1279 # This happened by accident in the original parser, but some people used it extensively
1280 $img = $this->maybeMakeExternalImage( $text );
1281 if ( $img !== false ) {
1282 $text = $img;
1283 }
1284
1285 $dtrail = '';
1286
1287 # Set linktype for CSS - if URL==text, link is essentially free
1288 $linktype = ($text == $url) ? 'free' : 'text';
1289
1290 # No link text, e.g. [http://domain.tld/some.link]
1291 if ( $text == '' ) {
1292 # Autonumber if allowed. See bug #5918
1293 if ( strpos( wfUrlProtocols(), substr($protocol, 0, strpos($protocol, ':')) ) !== false ) {
1294 $text = '[' . ++$this->mAutonumber . ']';
1295 $linktype = 'autonumber';
1296 } else {
1297 # Otherwise just use the URL
1298 $text = htmlspecialchars( $url );
1299 $linktype = 'free';
1300 }
1301 } else {
1302 # Have link text, e.g. [http://domain.tld/some.link text]s
1303 # Check for trail
1304 list( $dtrail, $trail ) = Linker::splitTrail( $trail );
1305 }
1306
1307 $text = $wgContLang->markNoConversion($text);
1308
1309 $url = Sanitizer::cleanUrl( $url );
1310
1311 # Process the trail (i.e. everything after this link up until start of the next link),
1312 # replacing any non-bracketed links
1313 $trail = $this->replaceFreeExternalLinks( $trail );
1314
1315 # Use the encoded URL
1316 # This means that users can paste URLs directly into the text
1317 # Funny characters like &ouml; aren't valid in URLs anyway
1318 # This was changed in August 2004
1319 $s .= $sk->makeExternalLink( $url, $text, false, $linktype, $this->mTitle->getNamespace() ) . $dtrail . $trail;
1320
1321 # Register link in the output object.
1322 # Replace unnecessary URL escape codes with the referenced character
1323 # This prevents spammers from hiding links from the filters
1324 $pasteurized = Parser::replaceUnusualEscapes( $url );
1325 $this->mOutput->addExternalLink( $pasteurized );
1326 }
1327
1328 wfProfileOut( $fname );
1329 return $s;
1330 }
1331
1332 /**
1333 * Replace anything that looks like a URL with a link
1334 * @private
1335 */
1336 function replaceFreeExternalLinks( $text ) {
1337 global $wgContLang;
1338 $fname = 'Parser::replaceFreeExternalLinks';
1339 wfProfileIn( $fname );
1340
1341 $bits = preg_split( '/(\b(?:' . wfUrlProtocols() . '))/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1342 $s = array_shift( $bits );
1343 $i = 0;
1344
1345 $sk = $this->mOptions->getSkin();
1346
1347 while ( $i < count( $bits ) ){
1348 $protocol = $bits[$i++];
1349 $remainder = $bits[$i++];
1350
1351 $m = array();
1352 if ( preg_match( '/^('.self::EXT_LINK_URL_CLASS.'+)(.*)$/s', $remainder, $m ) ) {
1353 # Found some characters after the protocol that look promising
1354 $url = $protocol . $m[1];
1355 $trail = $m[2];
1356
1357 # special case: handle urls as url args:
1358 # http://www.example.com/foo?=http://www.example.com/bar
1359 if(strlen($trail) == 0 &&
1360 isset($bits[$i]) &&
1361 preg_match('/^'. wfUrlProtocols() . '$/S', $bits[$i]) &&
1362 preg_match( '/^('.self::EXT_LINK_URL_CLASS.'+)(.*)$/s', $bits[$i + 1], $m ))
1363 {
1364 # add protocol, arg
1365 $url .= $bits[$i] . $m[1]; # protocol, url as arg to previous link
1366 $i += 2;
1367 $trail = $m[2];
1368 }
1369
1370 # The characters '<' and '>' (which were escaped by
1371 # removeHTMLtags()) should not be included in
1372 # URLs, per RFC 2396.
1373 $m2 = array();
1374 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
1375 $trail = substr($url, $m2[0][1]) . $trail;
1376 $url = substr($url, 0, $m2[0][1]);
1377 }
1378
1379 # Move trailing punctuation to $trail
1380 $sep = ',;\.:!?';
1381 # If there is no left bracket, then consider right brackets fair game too
1382 if ( strpos( $url, '(' ) === false ) {
1383 $sep .= ')';
1384 }
1385
1386 $numSepChars = strspn( strrev( $url ), $sep );
1387 if ( $numSepChars ) {
1388 $trail = substr( $url, -$numSepChars ) . $trail;
1389 $url = substr( $url, 0, -$numSepChars );
1390 }
1391
1392 $url = Sanitizer::cleanUrl( $url );
1393
1394 # Is this an external image?
1395 $text = $this->maybeMakeExternalImage( $url );
1396 if ( $text === false ) {
1397 # Not an image, make a link
1398 $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free', $this->mTitle->getNamespace() );
1399 # Register it in the output object...
1400 # Replace unnecessary URL escape codes with their equivalent characters
1401 $pasteurized = Parser::replaceUnusualEscapes( $url );
1402 $this->mOutput->addExternalLink( $pasteurized );
1403 }
1404 $s .= $text . $trail;
1405 } else {
1406 $s .= $protocol . $remainder;
1407 }
1408 }
1409 wfProfileOut( $fname );
1410 return $s;
1411 }
1412
1413 /**
1414 * Replace unusual URL escape codes with their equivalent characters
1415 * @param string
1416 * @return string
1417 * @static
1418 * @todo This can merge genuinely required bits in the path or query string,
1419 * breaking legit URLs. A proper fix would treat the various parts of
1420 * the URL differently; as a workaround, just use the output for
1421 * statistical records, not for actual linking/output.
1422 */
1423 static function replaceUnusualEscapes( $url ) {
1424 return preg_replace_callback( '/%[0-9A-Fa-f]{2}/',
1425 array( 'Parser', 'replaceUnusualEscapesCallback' ), $url );
1426 }
1427
1428 /**
1429 * Callback function used in replaceUnusualEscapes().
1430 * Replaces unusual URL escape codes with their equivalent character
1431 * @static
1432 * @private
1433 */
1434 private static function replaceUnusualEscapesCallback( $matches ) {
1435 $char = urldecode( $matches[0] );
1436 $ord = ord( $char );
1437 // Is it an unsafe or HTTP reserved character according to RFC 1738?
1438 if ( $ord > 32 && $ord < 127 && strpos( '<>"#{}|\^~[]`;/?', $char ) === false ) {
1439 // No, shouldn't be escaped
1440 return $char;
1441 } else {
1442 // Yes, leave it escaped
1443 return $matches[0];
1444 }
1445 }
1446
1447 /**
1448 * make an image if it's allowed, either through the global
1449 * option or through the exception
1450 * @private
1451 */
1452 function maybeMakeExternalImage( $url ) {
1453 $sk = $this->mOptions->getSkin();
1454 $imagesfrom = $this->mOptions->getAllowExternalImagesFrom();
1455 $imagesexception = !empty($imagesfrom);
1456 $text = false;
1457 if ( $this->mOptions->getAllowExternalImages()
1458 || ( $imagesexception && strpos( $url, $imagesfrom ) === 0 ) ) {
1459 if ( preg_match( self::EXT_IMAGE_REGEX, $url ) ) {
1460 # Image found
1461 $text = $sk->makeExternalImage( htmlspecialchars( $url ) );
1462 }
1463 }
1464 return $text;
1465 }
1466
1467 /**
1468 * Process [[ ]] wikilinks
1469 *
1470 * @private
1471 */
1472 function replaceInternalLinks( $s ) {
1473 global $wgContLang;
1474 static $fname = 'Parser::replaceInternalLinks' ;
1475
1476 wfProfileIn( $fname );
1477
1478 wfProfileIn( $fname.'-setup' );
1479 static $tc = FALSE;
1480 # the % is needed to support urlencoded titles as well
1481 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1482
1483 $sk = $this->mOptions->getSkin();
1484
1485 #split the entire text string on occurences of [[
1486 $a = explode( '[[', ' ' . $s );
1487 #get the first element (all text up to first [[), and remove the space we added
1488 $s = array_shift( $a );
1489 $s = substr( $s, 1 );
1490
1491 # Match a link having the form [[namespace:link|alternate]]trail
1492 static $e1 = FALSE;
1493 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD"; }
1494 # Match cases where there is no "]]", which might still be images
1495 static $e1_img = FALSE;
1496 if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
1497
1498 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
1499 $e2 = null;
1500 if ( $useLinkPrefixExtension ) {
1501 # Match the end of a line for a word that's not followed by whitespace,
1502 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1503 $e2 = wfMsgForContent( 'linkprefix' );
1504 }
1505
1506 if( is_null( $this->mTitle ) ) {
1507 throw new MWException( __METHOD__.": \$this->mTitle is null\n" );
1508 }
1509 $nottalk = !$this->mTitle->isTalkPage();
1510
1511 if ( $useLinkPrefixExtension ) {
1512 $m = array();
1513 if ( preg_match( $e2, $s, $m ) ) {
1514 $first_prefix = $m[2];
1515 } else {
1516 $first_prefix = false;
1517 }
1518 } else {
1519 $prefix = '';
1520 }
1521
1522 if($wgContLang->hasVariants()) {
1523 $selflink = $wgContLang->convertLinkToAllVariants($this->mTitle->getPrefixedText());
1524 } else {
1525 $selflink = array($this->mTitle->getPrefixedText());
1526 }
1527 $useSubpages = $this->areSubpagesAllowed();
1528 wfProfileOut( $fname.'-setup' );
1529
1530 # Loop for each link
1531 for ($k = 0; isset( $a[$k] ); $k++) {
1532 $line = $a[$k];
1533 if ( $useLinkPrefixExtension ) {
1534 wfProfileIn( $fname.'-prefixhandling' );
1535 if ( preg_match( $e2, $s, $m ) ) {
1536 $prefix = $m[2];
1537 $s = $m[1];
1538 } else {
1539 $prefix='';
1540 }
1541 # first link
1542 if($first_prefix) {
1543 $prefix = $first_prefix;
1544 $first_prefix = false;
1545 }
1546 wfProfileOut( $fname.'-prefixhandling' );
1547 }
1548
1549 $might_be_img = false;
1550
1551 wfProfileIn( "$fname-e1" );
1552 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1553 $text = $m[2];
1554 # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
1555 # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
1556 # the real problem is with the $e1 regex
1557 # See bug 1300.
1558 #
1559 # Still some problems for cases where the ] is meant to be outside punctuation,
1560 # and no image is in sight. See bug 2095.
1561 #
1562 if( $text !== '' &&
1563 substr( $m[3], 0, 1 ) === ']' &&
1564 strpos($text, '[') !== false
1565 )
1566 {
1567 $text .= ']'; # so that replaceExternalLinks($text) works later
1568 $m[3] = substr( $m[3], 1 );
1569 }
1570 # fix up urlencoded title texts
1571 if( strpos( $m[1], '%' ) !== false ) {
1572 # Should anchors '#' also be rejected?
1573 $m[1] = str_replace( array('<', '>'), array('&lt;', '&gt;'), urldecode($m[1]) );
1574 }
1575 $trail = $m[3];
1576 } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
1577 $might_be_img = true;
1578 $text = $m[2];
1579 if ( strpos( $m[1], '%' ) !== false ) {
1580 $m[1] = urldecode($m[1]);
1581 }
1582 $trail = "";
1583 } else { # Invalid form; output directly
1584 $s .= $prefix . '[[' . $line ;
1585 wfProfileOut( "$fname-e1" );
1586 continue;
1587 }
1588 wfProfileOut( "$fname-e1" );
1589 wfProfileIn( "$fname-misc" );
1590
1591 # Don't allow internal links to pages containing
1592 # PROTO: where PROTO is a valid URL protocol; these
1593 # should be external links.
1594 if (preg_match('/^\b(?:' . wfUrlProtocols() . ')/', $m[1])) {
1595 $s .= $prefix . '[[' . $line ;
1596 continue;
1597 }
1598
1599 # Make subpage if necessary
1600 if( $useSubpages ) {
1601 $link = $this->maybeDoSubpageLink( $m[1], $text );
1602 } else {
1603 $link = $m[1];
1604 }
1605
1606 $noforce = (substr($m[1], 0, 1) != ':');
1607 if (!$noforce) {
1608 # Strip off leading ':'
1609 $link = substr($link, 1);
1610 }
1611
1612 wfProfileOut( "$fname-misc" );
1613 wfProfileIn( "$fname-title" );
1614 $nt = Title::newFromText( $this->mStripState->unstripNoWiki($link) );
1615 if( !$nt ) {
1616 $s .= $prefix . '[[' . $line;
1617 wfProfileOut( "$fname-title" );
1618 continue;
1619 }
1620
1621 $ns = $nt->getNamespace();
1622 $iw = $nt->getInterWiki();
1623 wfProfileOut( "$fname-title" );
1624
1625 if ($might_be_img) { # if this is actually an invalid link
1626 wfProfileIn( "$fname-might_be_img" );
1627 if ($ns == NS_IMAGE && $noforce) { #but might be an image
1628 $found = false;
1629 while (isset ($a[$k+1]) ) {
1630 #look at the next 'line' to see if we can close it there
1631 $spliced = array_splice( $a, $k + 1, 1 );
1632 $next_line = array_shift( $spliced );
1633 $m = explode( ']]', $next_line, 3 );
1634 if ( count( $m ) == 3 ) {
1635 # the first ]] closes the inner link, the second the image
1636 $found = true;
1637 $text .= "[[{$m[0]}]]{$m[1]}";
1638 $trail = $m[2];
1639 break;
1640 } elseif ( count( $m ) == 2 ) {
1641 #if there's exactly one ]] that's fine, we'll keep looking
1642 $text .= "[[{$m[0]}]]{$m[1]}";
1643 } else {
1644 #if $next_line is invalid too, we need look no further
1645 $text .= '[[' . $next_line;
1646 break;
1647 }
1648 }
1649 if ( !$found ) {
1650 # we couldn't find the end of this imageLink, so output it raw
1651 #but don't ignore what might be perfectly normal links in the text we've examined
1652 $text = $this->replaceInternalLinks($text);
1653 $s .= "{$prefix}[[$link|$text";
1654 # note: no $trail, because without an end, there *is* no trail
1655 wfProfileOut( "$fname-might_be_img" );
1656 continue;
1657 }
1658 } else { #it's not an image, so output it raw
1659 $s .= "{$prefix}[[$link|$text";
1660 # note: no $trail, because without an end, there *is* no trail
1661 wfProfileOut( "$fname-might_be_img" );
1662 continue;
1663 }
1664 wfProfileOut( "$fname-might_be_img" );
1665 }
1666
1667 $wasblank = ( '' == $text );
1668 if( $wasblank ) $text = $link;
1669
1670 # Link not escaped by : , create the various objects
1671 if( $noforce ) {
1672
1673 # Interwikis
1674 wfProfileIn( "$fname-interwiki" );
1675 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1676 $this->mOutput->addLanguageLink( $nt->getFullText() );
1677 $s = rtrim($s . $prefix);
1678 $s .= trim($trail, "\n") == '' ? '': $prefix . $trail;
1679 wfProfileOut( "$fname-interwiki" );
1680 continue;
1681 }
1682 wfProfileOut( "$fname-interwiki" );
1683
1684 if ( $ns == NS_IMAGE ) {
1685 wfProfileIn( "$fname-image" );
1686 if ( !wfIsBadImage( $nt->getDBkey(), $this->mTitle ) ) {
1687 # recursively parse links inside the image caption
1688 # actually, this will parse them in any other parameters, too,
1689 # but it might be hard to fix that, and it doesn't matter ATM
1690 $text = $this->replaceExternalLinks($text);
1691 $text = $this->replaceInternalLinks($text);
1692
1693 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
1694 $s .= $prefix . $this->armorLinks( $this->makeImage( $nt, $text ) ) . $trail;
1695 $this->mOutput->addImage( $nt->getDBkey() );
1696
1697 wfProfileOut( "$fname-image" );
1698 continue;
1699 } else {
1700 # We still need to record the image's presence on the page
1701 $this->mOutput->addImage( $nt->getDBkey() );
1702 }
1703 wfProfileOut( "$fname-image" );
1704
1705 }
1706
1707 if ( $ns == NS_CATEGORY ) {
1708 wfProfileIn( "$fname-category" );
1709 $s = rtrim($s . "\n"); # bug 87
1710
1711 if ( $wasblank ) {
1712 $sortkey = $this->getDefaultSort();
1713 } else {
1714 $sortkey = $text;
1715 }
1716 $sortkey = Sanitizer::decodeCharReferences( $sortkey );
1717 $sortkey = str_replace( "\n", '', $sortkey );
1718 $sortkey = $wgContLang->convertCategoryKey( $sortkey );
1719 $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
1720
1721 /**
1722 * Strip the whitespace Category links produce, see bug 87
1723 * @todo We might want to use trim($tmp, "\n") here.
1724 */
1725 $s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
1726
1727 wfProfileOut( "$fname-category" );
1728 continue;
1729 }
1730 }
1731
1732 # Self-link checking
1733 if( $nt->getFragment() === '' ) {
1734 if( in_array( $nt->getPrefixedText(), $selflink, true ) ) {
1735 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1736 continue;
1737 }
1738 }
1739
1740 # Special and Media are pseudo-namespaces; no pages actually exist in them
1741 if( $ns == NS_MEDIA ) {
1742 $link = $sk->makeMediaLinkObj( $nt, $text );
1743 # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
1744 $s .= $prefix . $this->armorLinks( $link ) . $trail;
1745 $this->mOutput->addImage( $nt->getDBkey() );
1746 continue;
1747 } elseif( $ns == NS_SPECIAL ) {
1748 if( SpecialPage::exists( $nt->getDBkey() ) ) {
1749 $s .= $this->makeKnownLinkHolder( $nt, $text, '', $trail, $prefix );
1750 } else {
1751 $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
1752 }
1753 continue;
1754 } elseif( $ns == NS_IMAGE ) {
1755 $img = wfFindFile( $nt );
1756 if( $img ) {
1757 // Force a blue link if the file exists; may be a remote
1758 // upload on the shared repository, and we want to see its
1759 // auto-generated page.
1760 $s .= $this->makeKnownLinkHolder( $nt, $text, '', $trail, $prefix );
1761 $this->mOutput->addLink( $nt );
1762 continue;
1763 }
1764 }
1765 $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
1766 }
1767 wfProfileOut( $fname );
1768 return $s;
1769 }
1770
1771 /**
1772 * Make a link placeholder. The text returned can be later resolved to a real link with
1773 * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
1774 * parsing of interwiki links, and secondly to allow all existence checks and
1775 * article length checks (for stub links) to be bundled into a single query.
1776 *
1777 */
1778 function makeLinkHolder( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1779 wfProfileIn( __METHOD__ );
1780 if ( ! is_object($nt) ) {
1781 # Fail gracefully
1782 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
1783 } else {
1784 # Separate the link trail from the rest of the link
1785 list( $inside, $trail ) = Linker::splitTrail( $trail );
1786
1787 if ( $nt->isExternal() ) {
1788 $nr = array_push( $this->mInterwikiLinkHolders['texts'], $prefix.$text.$inside );
1789 $this->mInterwikiLinkHolders['titles'][] = $nt;
1790 $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
1791 } else {
1792 $nr = array_push( $this->mLinkHolders['namespaces'], $nt->getNamespace() );
1793 $this->mLinkHolders['dbkeys'][] = $nt->getDBkey();
1794 $this->mLinkHolders['queries'][] = $query;
1795 $this->mLinkHolders['texts'][] = $prefix.$text.$inside;
1796 $this->mLinkHolders['titles'][] = $nt;
1797
1798 $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
1799 }
1800 }
1801 wfProfileOut( __METHOD__ );
1802 return $retVal;
1803 }
1804
1805 /**
1806 * Render a forced-blue link inline; protect against double expansion of
1807 * URLs if we're in a mode that prepends full URL prefixes to internal links.
1808 * Since this little disaster has to split off the trail text to avoid
1809 * breaking URLs in the following text without breaking trails on the
1810 * wiki links, it's been made into a horrible function.
1811 *
1812 * @param Title $nt
1813 * @param string $text
1814 * @param string $query
1815 * @param string $trail
1816 * @param string $prefix
1817 * @return string HTML-wikitext mix oh yuck
1818 */
1819 function makeKnownLinkHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1820 list( $inside, $trail ) = Linker::splitTrail( $trail );
1821 $sk = $this->mOptions->getSkin();
1822 $link = $sk->makeKnownLinkObj( $nt, $text, $query, $inside, $prefix );
1823 return $this->armorLinks( $link ) . $trail;
1824 }
1825
1826 /**
1827 * Insert a NOPARSE hacky thing into any inline links in a chunk that's
1828 * going to go through further parsing steps before inline URL expansion.
1829 *
1830 * In particular this is important when using action=render, which causes
1831 * full URLs to be included.
1832 *
1833 * Oh man I hate our multi-layer parser!
1834 *
1835 * @param string more-or-less HTML
1836 * @return string less-or-more HTML with NOPARSE bits
1837 */
1838 function armorLinks( $text ) {
1839 return preg_replace( '/\b(' . wfUrlProtocols() . ')/',
1840 "{$this->mUniqPrefix}NOPARSE$1", $text );
1841 }
1842
1843 /**
1844 * Return true if subpage links should be expanded on this page.
1845 * @return bool
1846 */
1847 function areSubpagesAllowed() {
1848 # Some namespaces don't allow subpages
1849 global $wgNamespacesWithSubpages;
1850 return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
1851 }
1852
1853 /**
1854 * Handle link to subpage if necessary
1855 * @param string $target the source of the link
1856 * @param string &$text the link text, modified as necessary
1857 * @return string the full name of the link
1858 * @private
1859 */
1860 function maybeDoSubpageLink($target, &$text) {
1861 # Valid link forms:
1862 # Foobar -- normal
1863 # :Foobar -- override special treatment of prefix (images, language links)
1864 # /Foobar -- convert to CurrentPage/Foobar
1865 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1866 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1867 # ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
1868
1869 $fname = 'Parser::maybeDoSubpageLink';
1870 wfProfileIn( $fname );
1871 $ret = $target; # default return value is no change
1872
1873 # Some namespaces don't allow subpages,
1874 # so only perform processing if subpages are allowed
1875 if( $this->areSubpagesAllowed() ) {
1876 $hash = strpos( $target, '#' );
1877 if( $hash !== false ) {
1878 $suffix = substr( $target, $hash );
1879 $target = substr( $target, 0, $hash );
1880 } else {
1881 $suffix = '';
1882 }
1883 # bug 7425
1884 $target = trim( $target );
1885 # Look at the first character
1886 if( $target != '' && $target{0} == '/' ) {
1887 # / at end means we don't want the slash to be shown
1888 $m = array();
1889 $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
1890 if( $trailingSlashes ) {
1891 $noslash = $target = substr( $target, 1, -strlen($m[0][0]) );
1892 } else {
1893 $noslash = substr( $target, 1 );
1894 }
1895
1896 $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash) . $suffix;
1897 if( '' === $text ) {
1898 $text = $target . $suffix;
1899 } # this might be changed for ugliness reasons
1900 } else {
1901 # check for .. subpage backlinks
1902 $dotdotcount = 0;
1903 $nodotdot = $target;
1904 while( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1905 ++$dotdotcount;
1906 $nodotdot = substr( $nodotdot, 3 );
1907 }
1908 if($dotdotcount > 0) {
1909 $exploded = explode( '/', $this->mTitle->GetPrefixedText() );
1910 if( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1911 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1912 # / at the end means don't show full path
1913 if( substr( $nodotdot, -1, 1 ) == '/' ) {
1914 $nodotdot = substr( $nodotdot, 0, -1 );
1915 if( '' === $text ) {
1916 $text = $nodotdot . $suffix;
1917 }
1918 }
1919 $nodotdot = trim( $nodotdot );
1920 if( $nodotdot != '' ) {
1921 $ret .= '/' . $nodotdot;
1922 }
1923 $ret .= $suffix;
1924 }
1925 }
1926 }
1927 }
1928
1929 wfProfileOut( $fname );
1930 return $ret;
1931 }
1932
1933 /**#@+
1934 * Used by doBlockLevels()
1935 * @private
1936 */
1937 /* private */ function closeParagraph() {
1938 $result = '';
1939 if ( '' != $this->mLastSection ) {
1940 $result = '</' . $this->mLastSection . ">\n";
1941 }
1942 $this->mInPre = false;
1943 $this->mLastSection = '';
1944 return $result;
1945 }
1946 # getCommon() returns the length of the longest common substring
1947 # of both arguments, starting at the beginning of both.
1948 #
1949 /* private */ function getCommon( $st1, $st2 ) {
1950 $fl = strlen( $st1 );
1951 $shorter = strlen( $st2 );
1952 if ( $fl < $shorter ) { $shorter = $fl; }
1953
1954 for ( $i = 0; $i < $shorter; ++$i ) {
1955 if ( $st1{$i} != $st2{$i} ) { break; }
1956 }
1957 return $i;
1958 }
1959 # These next three functions open, continue, and close the list
1960 # element appropriate to the prefix character passed into them.
1961 #
1962 /* private */ function openList( $char ) {
1963 $result = $this->closeParagraph();
1964
1965 if ( '*' == $char ) { $result .= '<ul><li>'; }
1966 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1967 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1968 else if ( ';' == $char ) {
1969 $result .= '<dl><dt>';
1970 $this->mDTopen = true;
1971 }
1972 else { $result = '<!-- ERR 1 -->'; }
1973
1974 return $result;
1975 }
1976
1977 /* private */ function nextItem( $char ) {
1978 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1979 else if ( ':' == $char || ';' == $char ) {
1980 $close = '</dd>';
1981 if ( $this->mDTopen ) { $close = '</dt>'; }
1982 if ( ';' == $char ) {
1983 $this->mDTopen = true;
1984 return $close . '<dt>';
1985 } else {
1986 $this->mDTopen = false;
1987 return $close . '<dd>';
1988 }
1989 }
1990 return '<!-- ERR 2 -->';
1991 }
1992
1993 /* private */ function closeList( $char ) {
1994 if ( '*' == $char ) { $text = '</li></ul>'; }
1995 else if ( '#' == $char ) { $text = '</li></ol>'; }
1996 else if ( ':' == $char ) {
1997 if ( $this->mDTopen ) {
1998 $this->mDTopen = false;
1999 $text = '</dt></dl>';
2000 } else {
2001 $text = '</dd></dl>';
2002 }
2003 }
2004 else { return '<!-- ERR 3 -->'; }
2005 return $text."\n";
2006 }
2007 /**#@-*/
2008
2009 /**
2010 * Make lists from lines starting with ':', '*', '#', etc.
2011 *
2012 * @private
2013 * @return string the lists rendered as HTML
2014 */
2015 function doBlockLevels( $text, $linestart ) {
2016 $fname = 'Parser::doBlockLevels';
2017 wfProfileIn( $fname );
2018
2019 # Parsing through the text line by line. The main thing
2020 # happening here is handling of block-level elements p, pre,
2021 # and making lists from lines starting with * # : etc.
2022 #
2023 $textLines = explode( "\n", $text );
2024
2025 $lastPrefix = $output = '';
2026 $this->mDTopen = $inBlockElem = false;
2027 $prefixLength = 0;
2028 $paragraphStack = false;
2029
2030 if ( !$linestart ) {
2031 $output .= array_shift( $textLines );
2032 }
2033 foreach ( $textLines as $oLine ) {
2034 $lastPrefixLength = strlen( $lastPrefix );
2035 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
2036 $preOpenMatch = preg_match('/<pre/i', $oLine );
2037 if ( !$this->mInPre ) {
2038 # Multiple prefixes may abut each other for nested lists.
2039 $prefixLength = strspn( $oLine, '*#:;' );
2040 $pref = substr( $oLine, 0, $prefixLength );
2041
2042 # eh?
2043 $pref2 = str_replace( ';', ':', $pref );
2044 $t = substr( $oLine, $prefixLength );
2045 $this->mInPre = !empty($preOpenMatch);
2046 } else {
2047 # Don't interpret any other prefixes in preformatted text
2048 $prefixLength = 0;
2049 $pref = $pref2 = '';
2050 $t = $oLine;
2051 }
2052
2053 # List generation
2054 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
2055 # Same as the last item, so no need to deal with nesting or opening stuff
2056 $output .= $this->nextItem( substr( $pref, -1 ) );
2057 $paragraphStack = false;
2058
2059 if ( substr( $pref, -1 ) == ';') {
2060 # The one nasty exception: definition lists work like this:
2061 # ; title : definition text
2062 # So we check for : in the remainder text to split up the
2063 # title and definition, without b0rking links.
2064 $term = $t2 = '';
2065 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
2066 $t = $t2;
2067 $output .= $term . $this->nextItem( ':' );
2068 }
2069 }
2070 } elseif( $prefixLength || $lastPrefixLength ) {
2071 # Either open or close a level...
2072 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
2073 $paragraphStack = false;
2074
2075 while( $commonPrefixLength < $lastPrefixLength ) {
2076 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
2077 --$lastPrefixLength;
2078 }
2079 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
2080 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
2081 }
2082 while ( $prefixLength > $commonPrefixLength ) {
2083 $char = substr( $pref, $commonPrefixLength, 1 );
2084 $output .= $this->openList( $char );
2085
2086 if ( ';' == $char ) {
2087 # FIXME: This is dupe of code above
2088 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
2089 $t = $t2;
2090 $output .= $term . $this->nextItem( ':' );
2091 }
2092 }
2093 ++$commonPrefixLength;
2094 }
2095 $lastPrefix = $pref2;
2096 }
2097 if( 0 == $prefixLength ) {
2098 wfProfileIn( "$fname-paragraph" );
2099 # No prefix (not in list)--go to paragraph mode
2100 // XXX: use a stack for nestable elements like span, table and div
2101 $openmatch = preg_match('/(?:<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<ol|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
2102 $closematch = preg_match(
2103 '/(?:<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
2104 '<td|<th|<\\/?div|<hr|<\\/pre|<\\/p|'.$this->mUniqPrefix.'-pre|<\\/li|<\\/ul|<\\/ol|<\\/?center)/iS', $t );
2105 if ( $openmatch or $closematch ) {
2106 $paragraphStack = false;
2107 # TODO bug 5718: paragraph closed
2108 $output .= $this->closeParagraph();
2109 if ( $preOpenMatch and !$preCloseMatch ) {
2110 $this->mInPre = true;
2111 }
2112 if ( $closematch ) {
2113 $inBlockElem = false;
2114 } else {
2115 $inBlockElem = true;
2116 }
2117 } else if ( !$inBlockElem && !$this->mInPre ) {
2118 if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
2119 // pre
2120 if ($this->mLastSection != 'pre') {
2121 $paragraphStack = false;
2122 $output .= $this->closeParagraph().'<pre>';
2123 $this->mLastSection = 'pre';
2124 }
2125 $t = substr( $t, 1 );
2126 } else {
2127 // paragraph
2128 if ( '' == trim($t) ) {
2129 if ( $paragraphStack ) {
2130 $output .= $paragraphStack.'<br />';
2131 $paragraphStack = false;
2132 $this->mLastSection = 'p';
2133 } else {
2134 if ($this->mLastSection != 'p' ) {
2135 $output .= $this->closeParagraph();
2136 $this->mLastSection = '';
2137 $paragraphStack = '<p>';
2138 } else {
2139 $paragraphStack = '</p><p>';
2140 }
2141 }
2142 } else {
2143 if ( $paragraphStack ) {
2144 $output .= $paragraphStack;
2145 $paragraphStack = false;
2146 $this->mLastSection = 'p';
2147 } else if ($this->mLastSection != 'p') {
2148 $output .= $this->closeParagraph().'<p>';
2149 $this->mLastSection = 'p';
2150 }
2151 }
2152 }
2153 }
2154 wfProfileOut( "$fname-paragraph" );
2155 }
2156 // somewhere above we forget to get out of pre block (bug 785)
2157 if($preCloseMatch && $this->mInPre) {
2158 $this->mInPre = false;
2159 }
2160 if ($paragraphStack === false) {
2161 $output .= $t."\n";
2162 }
2163 }
2164 while ( $prefixLength ) {
2165 $output .= $this->closeList( $pref2{$prefixLength-1} );
2166 --$prefixLength;
2167 }
2168 if ( '' != $this->mLastSection ) {
2169 $output .= '</' . $this->mLastSection . '>';
2170 $this->mLastSection = '';
2171 }
2172
2173 wfProfileOut( $fname );
2174 return $output;
2175 }
2176
2177 /**
2178 * Split up a string on ':', ignoring any occurences inside tags
2179 * to prevent illegal overlapping.
2180 * @param string $str the string to split
2181 * @param string &$before set to everything before the ':'
2182 * @param string &$after set to everything after the ':'
2183 * return string the position of the ':', or false if none found
2184 */
2185 function findColonNoLinks($str, &$before, &$after) {
2186 $fname = 'Parser::findColonNoLinks';
2187 wfProfileIn( $fname );
2188
2189 $pos = strpos( $str, ':' );
2190 if( $pos === false ) {
2191 // Nothing to find!
2192 wfProfileOut( $fname );
2193 return false;
2194 }
2195
2196 $lt = strpos( $str, '<' );
2197 if( $lt === false || $lt > $pos ) {
2198 // Easy; no tag nesting to worry about
2199 $before = substr( $str, 0, $pos );
2200 $after = substr( $str, $pos+1 );
2201 wfProfileOut( $fname );
2202 return $pos;
2203 }
2204
2205 // Ugly state machine to walk through avoiding tags.
2206 $state = self::COLON_STATE_TEXT;
2207 $stack = 0;
2208 $len = strlen( $str );
2209 for( $i = 0; $i < $len; $i++ ) {
2210 $c = $str{$i};
2211
2212 switch( $state ) {
2213 // (Using the number is a performance hack for common cases)
2214 case 0: // self::COLON_STATE_TEXT:
2215 switch( $c ) {
2216 case "<":
2217 // Could be either a <start> tag or an </end> tag
2218 $state = self::COLON_STATE_TAGSTART;
2219 break;
2220 case ":":
2221 if( $stack == 0 ) {
2222 // We found it!
2223 $before = substr( $str, 0, $i );
2224 $after = substr( $str, $i + 1 );
2225 wfProfileOut( $fname );
2226 return $i;
2227 }
2228 // Embedded in a tag; don't break it.
2229 break;
2230 default:
2231 // Skip ahead looking for something interesting
2232 $colon = strpos( $str, ':', $i );
2233 if( $colon === false ) {
2234 // Nothing else interesting
2235 wfProfileOut( $fname );
2236 return false;
2237 }
2238 $lt = strpos( $str, '<', $i );
2239 if( $stack === 0 ) {
2240 if( $lt === false || $colon < $lt ) {
2241 // We found it!
2242 $before = substr( $str, 0, $colon );
2243 $after = substr( $str, $colon + 1 );
2244 wfProfileOut( $fname );
2245 return $i;
2246 }
2247 }
2248 if( $lt === false ) {
2249 // Nothing else interesting to find; abort!
2250 // We're nested, but there's no close tags left. Abort!
2251 break 2;
2252 }
2253 // Skip ahead to next tag start
2254 $i = $lt;
2255 $state = self::COLON_STATE_TAGSTART;
2256 }
2257 break;
2258 case 1: // self::COLON_STATE_TAG:
2259 // In a <tag>
2260 switch( $c ) {
2261 case ">":
2262 $stack++;
2263 $state = self::COLON_STATE_TEXT;
2264 break;
2265 case "/":
2266 // Slash may be followed by >?
2267 $state = self::COLON_STATE_TAGSLASH;
2268 break;
2269 default:
2270 // ignore
2271 }
2272 break;
2273 case 2: // self::COLON_STATE_TAGSTART:
2274 switch( $c ) {
2275 case "/":
2276 $state = self::COLON_STATE_CLOSETAG;
2277 break;
2278 case "!":
2279 $state = self::COLON_STATE_COMMENT;
2280 break;
2281 case ">":
2282 // Illegal early close? This shouldn't happen D:
2283 $state = self::COLON_STATE_TEXT;
2284 break;
2285 default:
2286 $state = self::COLON_STATE_TAG;
2287 }
2288 break;
2289 case 3: // self::COLON_STATE_CLOSETAG:
2290 // In a </tag>
2291 if( $c == ">" ) {
2292 $stack--;
2293 if( $stack < 0 ) {
2294 wfDebug( "Invalid input in $fname; too many close tags\n" );
2295 wfProfileOut( $fname );
2296 return false;
2297 }
2298 $state = self::COLON_STATE_TEXT;
2299 }
2300 break;
2301 case self::COLON_STATE_TAGSLASH:
2302 if( $c == ">" ) {
2303 // Yes, a self-closed tag <blah/>
2304 $state = self::COLON_STATE_TEXT;
2305 } else {
2306 // Probably we're jumping the gun, and this is an attribute
2307 $state = self::COLON_STATE_TAG;
2308 }
2309 break;
2310 case 5: // self::COLON_STATE_COMMENT:
2311 if( $c == "-" ) {
2312 $state = self::COLON_STATE_COMMENTDASH;
2313 }
2314 break;
2315 case self::COLON_STATE_COMMENTDASH:
2316 if( $c == "-" ) {
2317 $state = self::COLON_STATE_COMMENTDASHDASH;
2318 } else {
2319 $state = self::COLON_STATE_COMMENT;
2320 }
2321 break;
2322 case self::COLON_STATE_COMMENTDASHDASH:
2323 if( $c == ">" ) {
2324 $state = self::COLON_STATE_TEXT;
2325 } else {
2326 $state = self::COLON_STATE_COMMENT;
2327 }
2328 break;
2329 default:
2330 throw new MWException( "State machine error in $fname" );
2331 }
2332 }
2333 if( $stack > 0 ) {
2334 wfDebug( "Invalid input in $fname; not enough close tags (stack $stack, state $state)\n" );
2335 return false;
2336 }
2337 wfProfileOut( $fname );
2338 return false;
2339 }
2340
2341 /**
2342 * Return value of a magic variable (like PAGENAME)
2343 *
2344 * @private
2345 */
2346 function getVariableValue( $index ) {
2347 global $wgContLang, $wgSitename, $wgServer, $wgServerName, $wgScriptPath;
2348
2349 /**
2350 * Some of these require message or data lookups and can be
2351 * expensive to check many times.
2352 */
2353 static $varCache = array();
2354 if ( wfRunHooks( 'ParserGetVariableValueVarCache', array( &$this, &$varCache ) ) ) {
2355 if ( isset( $varCache[$index] ) ) {
2356 return $varCache[$index];
2357 }
2358 }
2359
2360 $ts = time();
2361 wfRunHooks( 'ParserGetVariableValueTs', array( &$this, &$ts ) );
2362
2363 # Use the time zone
2364 global $wgLocaltimezone;
2365 if ( isset( $wgLocaltimezone ) ) {
2366 $oldtz = getenv( 'TZ' );
2367 putenv( 'TZ='.$wgLocaltimezone );
2368 }
2369
2370 wfSuppressWarnings(); // E_STRICT system time bitching
2371 $localTimestamp = date( 'YmdHis', $ts );
2372 $localMonth = date( 'm', $ts );
2373 $localMonthName = date( 'n', $ts );
2374 $localDay = date( 'j', $ts );
2375 $localDay2 = date( 'd', $ts );
2376 $localDayOfWeek = date( 'w', $ts );
2377 $localWeek = date( 'W', $ts );
2378 $localYear = date( 'Y', $ts );
2379 $localHour = date( 'H', $ts );
2380 if ( isset( $wgLocaltimezone ) ) {
2381 putenv( 'TZ='.$oldtz );
2382 }
2383 wfRestoreWarnings();
2384
2385 switch ( $index ) {
2386 case 'currentmonth':
2387 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'm', $ts ) );
2388 case 'currentmonthname':
2389 return $varCache[$index] = $wgContLang->getMonthName( gmdate( 'n', $ts ) );
2390 case 'currentmonthnamegen':
2391 return $varCache[$index] = $wgContLang->getMonthNameGen( gmdate( 'n', $ts ) );
2392 case 'currentmonthabbrev':
2393 return $varCache[$index] = $wgContLang->getMonthAbbreviation( gmdate( 'n', $ts ) );
2394 case 'currentday':
2395 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'j', $ts ) );
2396 case 'currentday2':
2397 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'd', $ts ) );
2398 case 'localmonth':
2399 return $varCache[$index] = $wgContLang->formatNum( $localMonth );
2400 case 'localmonthname':
2401 return $varCache[$index] = $wgContLang->getMonthName( $localMonthName );
2402 case 'localmonthnamegen':
2403 return $varCache[$index] = $wgContLang->getMonthNameGen( $localMonthName );
2404 case 'localmonthabbrev':
2405 return $varCache[$index] = $wgContLang->getMonthAbbreviation( $localMonthName );
2406 case 'localday':
2407 return $varCache[$index] = $wgContLang->formatNum( $localDay );
2408 case 'localday2':
2409 return $varCache[$index] = $wgContLang->formatNum( $localDay2 );
2410 case 'pagename':
2411 return wfEscapeWikiText( $this->mTitle->getText() );
2412 case 'pagenamee':
2413 return $this->mTitle->getPartialURL();
2414 case 'fullpagename':
2415 return wfEscapeWikiText( $this->mTitle->getPrefixedText() );
2416 case 'fullpagenamee':
2417 return $this->mTitle->getPrefixedURL();
2418 case 'subpagename':
2419 return wfEscapeWikiText( $this->mTitle->getSubpageText() );
2420 case 'subpagenamee':
2421 return $this->mTitle->getSubpageUrlForm();
2422 case 'basepagename':
2423 return wfEscapeWikiText( $this->mTitle->getBaseText() );
2424 case 'basepagenamee':
2425 return wfUrlEncode( str_replace( ' ', '_', $this->mTitle->getBaseText() ) );
2426 case 'talkpagename':
2427 if( $this->mTitle->canTalk() ) {
2428 $talkPage = $this->mTitle->getTalkPage();
2429 return wfEscapeWikiText( $talkPage->getPrefixedText() );
2430 } else {
2431 return '';
2432 }
2433 case 'talkpagenamee':
2434 if( $this->mTitle->canTalk() ) {
2435 $talkPage = $this->mTitle->getTalkPage();
2436 return $talkPage->getPrefixedUrl();
2437 } else {
2438 return '';
2439 }
2440 case 'subjectpagename':
2441 $subjPage = $this->mTitle->getSubjectPage();
2442 return wfEscapeWikiText( $subjPage->getPrefixedText() );
2443 case 'subjectpagenamee':
2444 $subjPage = $this->mTitle->getSubjectPage();
2445 return $subjPage->getPrefixedUrl();
2446 case 'revisionid':
2447 // Let the edit saving system know we should parse the page
2448 // *after* a revision ID has been assigned.
2449 $this->mOutput->setFlag( 'vary-revision' );
2450 wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision...\n" );
2451 return $this->mRevisionId;
2452 case 'revisionday':
2453 // Let the edit saving system know we should parse the page
2454 // *after* a revision ID has been assigned. This is for null edits.
2455 $this->mOutput->setFlag( 'vary-revision' );
2456 wfDebug( __METHOD__ . ": {{REVISIONDAY}} used, setting vary-revision...\n" );
2457 return intval( substr( $this->getRevisionTimestamp(), 6, 2 ) );
2458 case 'revisionday2':
2459 // Let the edit saving system know we should parse the page
2460 // *after* a revision ID has been assigned. This is for null edits.
2461 $this->mOutput->setFlag( 'vary-revision' );
2462 wfDebug( __METHOD__ . ": {{REVISIONDAY2}} used, setting vary-revision...\n" );
2463 return substr( $this->getRevisionTimestamp(), 6, 2 );
2464 case 'revisionmonth':
2465 // Let the edit saving system know we should parse the page
2466 // *after* a revision ID has been assigned. This is for null edits.
2467 $this->mOutput->setFlag( 'vary-revision' );
2468 wfDebug( __METHOD__ . ": {{REVISIONMONTH}} used, setting vary-revision...\n" );
2469 return intval( substr( $this->getRevisionTimestamp(), 4, 2 ) );
2470 case 'revisionyear':
2471 // Let the edit saving system know we should parse the page
2472 // *after* a revision ID has been assigned. This is for null edits.
2473 $this->mOutput->setFlag( 'vary-revision' );
2474 wfDebug( __METHOD__ . ": {{REVISIONYEAR}} used, setting vary-revision...\n" );
2475 return substr( $this->getRevisionTimestamp(), 0, 4 );
2476 case 'revisiontimestamp':
2477 // Let the edit saving system know we should parse the page
2478 // *after* a revision ID has been assigned. This is for null edits.
2479 $this->mOutput->setFlag( 'vary-revision' );
2480 wfDebug( __METHOD__ . ": {{REVISIONTIMESTAMP}} used, setting vary-revision...\n" );
2481 return $this->getRevisionTimestamp();
2482 case 'namespace':
2483 return str_replace('_',' ',$wgContLang->getNsText( $this->mTitle->getNamespace() ) );
2484 case 'namespacee':
2485 return wfUrlencode( $wgContLang->getNsText( $this->mTitle->getNamespace() ) );
2486 case 'talkspace':
2487 return $this->mTitle->canTalk() ? str_replace('_',' ',$this->mTitle->getTalkNsText()) : '';
2488 case 'talkspacee':
2489 return $this->mTitle->canTalk() ? wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
2490 case 'subjectspace':
2491 return $this->mTitle->getSubjectNsText();
2492 case 'subjectspacee':
2493 return( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
2494 case 'currentdayname':
2495 return $varCache[$index] = $wgContLang->getWeekdayName( gmdate( 'w', $ts ) + 1 );
2496 case 'currentyear':
2497 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'Y', $ts ), true );
2498 case 'currenttime':
2499 return $varCache[$index] = $wgContLang->time( wfTimestamp( TS_MW, $ts ), false, false );
2500 case 'currenthour':
2501 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'H', $ts ), true );
2502 case 'currentweek':
2503 // @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
2504 // int to remove the padding
2505 return $varCache[$index] = $wgContLang->formatNum( (int)gmdate( 'W', $ts ) );
2506 case 'currentdow':
2507 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'w', $ts ) );
2508 case 'localdayname':
2509 return $varCache[$index] = $wgContLang->getWeekdayName( $localDayOfWeek + 1 );
2510 case 'localyear':
2511 return $varCache[$index] = $wgContLang->formatNum( $localYear, true );
2512 case 'localtime':
2513 return $varCache[$index] = $wgContLang->time( $localTimestamp, false, false );
2514 case 'localhour':
2515 return $varCache[$index] = $wgContLang->formatNum( $localHour, true );
2516 case 'localweek':
2517 // @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
2518 // int to remove the padding
2519 return $varCache[$index] = $wgContLang->formatNum( (int)$localWeek );
2520 case 'localdow':
2521 return $varCache[$index] = $wgContLang->formatNum( $localDayOfWeek );
2522 case 'numberofarticles':
2523 return $varCache[$index] = $wgContLang->formatNum( SiteStats::articles() );
2524 case 'numberoffiles':
2525 return $varCache[$index] = $wgContLang->formatNum( SiteStats::images() );
2526 case 'numberofusers':
2527 return $varCache[$index] = $wgContLang->formatNum( SiteStats::users() );
2528 case 'numberofpages':
2529 return $varCache[$index] = $wgContLang->formatNum( SiteStats::pages() );
2530 case 'numberofadmins':
2531 return $varCache[$index] = $wgContLang->formatNum( SiteStats::admins() );
2532 case 'numberofedits':
2533 return $varCache[$index] = $wgContLang->formatNum( SiteStats::edits() );
2534 case 'currenttimestamp':
2535 return $varCache[$index] = wfTimestampNow();
2536 case 'localtimestamp':
2537 return $varCache[$index] = $localTimestamp;
2538 case 'currentversion':
2539 return $varCache[$index] = SpecialVersion::getVersion();
2540 case 'sitename':
2541 return $wgSitename;
2542 case 'server':
2543 return $wgServer;
2544 case 'servername':
2545 return $wgServerName;
2546 case 'scriptpath':
2547 return $wgScriptPath;
2548 case 'directionmark':
2549 return $wgContLang->getDirMark();
2550 case 'contentlanguage':
2551 global $wgContLanguageCode;
2552 return $wgContLanguageCode;
2553 default:
2554 $ret = null;
2555 if ( wfRunHooks( 'ParserGetVariableValueSwitch', array( &$this, &$varCache, &$index, &$ret ) ) )
2556 return $ret;
2557 else
2558 return null;
2559 }
2560 }
2561
2562 /**
2563 * initialise the magic variables (like CURRENTMONTHNAME)
2564 *
2565 * @private
2566 */
2567 function initialiseVariables() {
2568 $fname = 'Parser::initialiseVariables';
2569 wfProfileIn( $fname );
2570 $variableIDs = MagicWord::getVariableIDs();
2571
2572 $this->mVariables = new MagicWordArray( $variableIDs );
2573 wfProfileOut( $fname );
2574 }
2575
2576 /**
2577 * Preprocess some wikitext and return the document tree.
2578 * This is the ghost of replace_variables().
2579 *
2580 * @param string $text The text to parse
2581 * @param integer flags Bitwise combination of:
2582 * self::PTD_FOR_INCLUSION Handle <noinclude>/<includeonly> as if the text is being
2583 * included. Default is to assume a direct page view.
2584 *
2585 * The generated DOM tree must depend only on the input text and the flags.
2586 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
2587 *
2588 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
2589 * change in the DOM tree for a given text, must be passed through the section identifier
2590 * in the section edit link and thus back to extractSections().
2591 *
2592 * The output of this function is currently only cached in process memory, but a persistent
2593 * cache may be implemented at a later date which takes further advantage of these strict
2594 * dependency requirements.
2595 *
2596 * @private
2597 */
2598 function preprocessToDom ( $text, $flags = 0 ) {
2599 wfProfileIn( __METHOD__ );
2600 wfProfileIn( __METHOD__.'-makexml' );
2601
2602 $rules = array(
2603 '{' => array(
2604 'end' => '}',
2605 'names' => array(
2606 2 => 'template',
2607 3 => 'tplarg',
2608 ),
2609 'min' => 2,
2610 'max' => 3,
2611 ),
2612 '[' => array(
2613 'end' => ']',
2614 'names' => array( 2 => null ),
2615 'min' => 2,
2616 'max' => 2,
2617 )
2618 );
2619
2620 $forInclusion = $flags & self::PTD_FOR_INCLUSION;
2621
2622 $xmlishElements = $this->getStripList();
2623 $enableOnlyinclude = false;
2624 if ( $forInclusion ) {
2625 $ignoredTags = array( 'includeonly', '/includeonly' );
2626 $ignoredElements = array( 'noinclude' );
2627 $xmlishElements[] = 'noinclude';
2628 if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
2629 $enableOnlyinclude = true;
2630 }
2631 } else {
2632 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
2633 $ignoredElements = array( 'includeonly' );
2634 $xmlishElements[] = 'includeonly';
2635 }
2636 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
2637
2638 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
2639 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
2640
2641 $stack = new PPDStack;
2642
2643 $searchBase = '[{<';
2644 $revText = strrev( $text ); // For fast reverse searches
2645
2646 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start
2647 $accum =& $stack->getAccum(); # Current text accumulator
2648 $accum = '<root>';
2649 $findEquals = false; # True to find equals signs in arguments
2650 $findPipe = false; # True to take notice of pipe characters
2651 $headingIndex = 1;
2652 $inHeading = false; # True if $i is inside a possible heading
2653 $noMoreGT = false; # True if there are no more greater-than (>) signs right of $i
2654 $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude>
2655 $fakeLineStart = true; # Do a line-start run without outputting an LF character
2656
2657 while ( true ) {
2658 if ( $findOnlyinclude ) {
2659 // Ignore all input up to the next <onlyinclude>
2660 $startPos = strpos( $text, '<onlyinclude>', $i );
2661 if ( $startPos === false ) {
2662 // Ignored section runs to the end
2663 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i ) ) . '</ignore>';
2664 break;
2665 }
2666 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
2667 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) . '</ignore>';
2668 $i = $tagEndPos;
2669 $findOnlyinclude = false;
2670 }
2671
2672 if ( $fakeLineStart ) {
2673 $found = 'line-start';
2674 $curChar = '';
2675 } else {
2676 # Find next opening brace, closing brace or pipe
2677 $search = $searchBase;
2678 if ( $stack->top === false ) {
2679 $currentClosing = '';
2680 } else {
2681 $currentClosing = $stack->top->close;
2682 $search .= $currentClosing;
2683 }
2684 if ( $findPipe ) {
2685 $search .= '|';
2686 }
2687 if ( $findEquals ) {
2688 // First equals will be for the template
2689 $search .= '=';
2690 } else {
2691 // Look for headings
2692 // We can't look for headings when $findEquals is true, because the ambiguity
2693 // between template name/value separators and heading starts would be unresolved
2694 // until the closing double-brace is found. This would mean either infinite
2695 // backtrack, or creating and updating two separate tree structures until the
2696 // end of the ambiguity -- one tree structure assuming a heading, and the other
2697 // assuming a template argument.
2698 //
2699 // Easier to just break some section edit links.
2700 $search .= "\n";
2701 }
2702 $rule = null;
2703 # Output literal section, advance input counter
2704 $literalLength = strcspn( $text, $search, $i );
2705 if ( $literalLength > 0 ) {
2706 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
2707 $i += $literalLength;
2708 }
2709 if ( $i >= strlen( $text ) ) {
2710 if ( $currentClosing == "\n" ) {
2711 // Do a past-the-end run to finish off the heading
2712 $curChar = '';
2713 $found = 'line-end';
2714 } else {
2715 # All done
2716 break;
2717 }
2718 } else {
2719 $curChar = $text[$i];
2720 if ( $curChar == '|' ) {
2721 $found = 'pipe';
2722 } elseif ( $curChar == '=' ) {
2723 $found = 'equals';
2724 } elseif ( $curChar == '<' ) {
2725 $found = 'angle';
2726 } elseif ( $curChar == "\n" ) {
2727 if ( $inHeading ) {
2728 $found = 'line-end';
2729 } else {
2730 $found = 'line-start';
2731 }
2732 } elseif ( $curChar == $currentClosing ) {
2733 $found = 'close';
2734 } elseif ( isset( $rules[$curChar] ) ) {
2735 $found = 'open';
2736 $rule = $rules[$curChar];
2737 } else {
2738 # Some versions of PHP have a strcspn which stops on null characters
2739 # Ignore and continue
2740 ++$i;
2741 continue;
2742 }
2743 }
2744 }
2745
2746 if ( $found == 'angle' ) {
2747 $matches = false;
2748 // Handle </onlyinclude>
2749 if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) {
2750 $findOnlyinclude = true;
2751 continue;
2752 }
2753
2754 // Determine element name
2755 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
2756 // Element name missing or not listed
2757 $accum .= '&lt;';
2758 ++$i;
2759 continue;
2760 }
2761 // Handle comments
2762 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
2763 // To avoid leaving blank lines, when a comment is both preceded
2764 // and followed by a newline (ignoring spaces), trim leading and
2765 // trailing spaces and one of the newlines.
2766
2767 // Find the end
2768 $endPos = strpos( $text, '-->', $i + 4 );
2769 if ( $endPos === false ) {
2770 // Unclosed comment in input, runs to end
2771 $inner = substr( $text, $i );
2772 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
2773 $i = strlen( $text );
2774 } else {
2775 // Search backwards for leading whitespace
2776 $wsStart = $i ? ( $i - strspn( $revText, ' ', strlen( $text ) - $i ) ) : 0;
2777 // Search forwards for trailing whitespace
2778 // $wsEnd will be the position of the last space
2779 $wsEnd = $endPos + 2 + strspn( $text, ' ', $endPos + 3 );
2780 // Eat the line if possible
2781 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
2782 // the overall start. That's not how Sanitizer::removeHTMLcomments() does it, but
2783 // it's a possible beneficial b/c break.
2784 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
2785 && substr( $text, $wsEnd + 1, 1 ) == "\n" )
2786 {
2787 $startPos = $wsStart;
2788 $endPos = $wsEnd + 1;
2789 // Remove leading whitespace from the end of the accumulator
2790 // Sanity check first though
2791 $wsLength = $i - $wsStart;
2792 if ( $wsLength > 0 && substr( $accum, -$wsLength ) === str_repeat( ' ', $wsLength ) ) {
2793 $accum = substr( $accum, 0, -$wsLength );
2794 }
2795 // Do a line-start run next time to look for headings after the comment,
2796 // but only if stack->top===false, because headings don't exist at deeper levels.
2797 if ( $stack->top === false ) {
2798 $fakeLineStart = true;
2799 }
2800 } else {
2801 // No line to eat, just take the comment itself
2802 $startPos = $i;
2803 $endPos += 2;
2804 }
2805
2806 $i = $endPos + 1;
2807 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
2808 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
2809 }
2810 continue;
2811 }
2812 $name = $matches[1];
2813 $attrStart = $i + strlen( $name ) + 1;
2814
2815 // Find end of tag
2816 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
2817 if ( $tagEndPos === false ) {
2818 // Infinite backtrack
2819 // Disable tag search to prevent worst-case O(N^2) performance
2820 $noMoreGT = true;
2821 $accum .= '&lt;';
2822 ++$i;
2823 continue;
2824 }
2825
2826 // Handle ignored tags
2827 if ( in_array( $name, $ignoredTags ) ) {
2828 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) ) . '</ignore>';
2829 $i = $tagEndPos + 1;
2830 continue;
2831 }
2832
2833 $tagStartPos = $i;
2834 if ( $text[$tagEndPos-1] == '/' ) {
2835 $attrEnd = $tagEndPos - 1;
2836 $inner = null;
2837 $i = $tagEndPos + 1;
2838 $close = '';
2839 } else {
2840 $attrEnd = $tagEndPos;
2841 // Find closing tag
2842 if ( preg_match( "/<\/$name\s*>/i", $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) ) {
2843 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
2844 $i = $matches[0][1] + strlen( $matches[0][0] );
2845 $close = '<close>' . htmlspecialchars( $matches[0][0] ) . '</close>';
2846 } else {
2847 // No end tag -- let it run out to the end of the text.
2848 $inner = substr( $text, $tagEndPos + 1 );
2849 $i = strlen( $text );
2850 $close = '';
2851 }
2852 }
2853 // <includeonly> and <noinclude> just become <ignore> tags
2854 if ( in_array( $name, $ignoredElements ) ) {
2855 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
2856 . '</ignore>';
2857 continue;
2858 }
2859
2860 $accum .= '<ext>';
2861 if ( $attrEnd <= $attrStart ) {
2862 $attr = '';
2863 } else {
2864 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
2865 }
2866 $accum .= '<name>' . htmlspecialchars( $name ) . '</name>' .
2867 // Note that the attr element contains the whitespace between name and attribute,
2868 // this is necessary for precise reconstruction during pre-save transform.
2869 '<attr>' . htmlspecialchars( $attr ) . '</attr>';
2870 if ( $inner !== null ) {
2871 $accum .= '<inner>' . htmlspecialchars( $inner ) . '</inner>';
2872 }
2873 $accum .= $close . '</ext>';
2874 }
2875
2876 elseif ( $found == 'line-start' ) {
2877 // Is this the start of a heading?
2878 // Line break belongs before the heading element in any case
2879 if ( $fakeLineStart ) {
2880 $fakeLineStart = false;
2881 } else {
2882 $accum .= $curChar;
2883 $i++;
2884 }
2885
2886 $count = strspn( $text, '=', $i, 6 );
2887 if ( $count > 0 ) {
2888 $piece = array(
2889 'open' => "\n",
2890 'close' => "\n",
2891 'parts' => array( str_repeat( '=', $count ) ),
2892 'startPos' => $i,
2893 'count' => $count );
2894 $stack->push( $piece );
2895 $accum =& $stack->getAccum();
2896 extract( $stack->getFlags() );
2897 $i += $count;
2898 }
2899 }
2900
2901 elseif ( $found == 'line-end' ) {
2902 $piece = $stack->top;
2903 // A heading must be open, otherwise \n wouldn't have been in the search list
2904 assert( $piece->open == "\n" );
2905 // Search back through the input to see if it has a proper close
2906 // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
2907 $m = false;
2908 $count = $piece->count;
2909 if ( preg_match( "/\s*(={{$count}})/A", $revText, $m, 0, strlen( $text ) - $i ) ) {
2910 if ( $i - strlen( $m[0] ) == $piece->startPos ) {
2911 // This is just a single string of equals signs on its own line
2912 // Replicate the doHeadings behaviour /={count}(.+)={count}/
2913 if ( $count < 3 ) {
2914 $count = 0;
2915 } elseif ( $count % 2 ) {
2916 $count = ( $count - 1 ) / 2;
2917 } else {
2918 $count = $count / 2 - 1;
2919 }
2920 } else {
2921 $count = min( strlen( $m[1] ), $count );
2922 }
2923 if ( $count > 0 ) {
2924 // Normal match, output <h>
2925 $element = "<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
2926 $headingIndex++;
2927 } else {
2928 // Single equals sign on its own line, count=0
2929 $element = $accum;
2930 }
2931 } else {
2932 // No match, no <h>, just pass down the inner text
2933 $element = $accum;
2934 }
2935 // Unwind the stack
2936 $stack->pop();
2937 $accum =& $stack->getAccum();
2938 extract( $stack->getFlags() );
2939
2940 // Append the result to the enclosing accumulator
2941 $accum .= $element;
2942 // Note that we do NOT increment the input pointer.
2943 // This is because the closing linebreak could be the opening linebreak of
2944 // another heading. Infinite loops are avoided because the next iteration MUST
2945 // hit the heading open case above, which unconditionally increments the
2946 // input pointer.
2947 }
2948
2949 elseif ( $found == 'open' ) {
2950 # count opening brace characters
2951 $count = strspn( $text, $curChar, $i );
2952
2953 # we need to add to stack only if opening brace count is enough for one of the rules
2954 if ( $count >= $rule['min'] ) {
2955 # Add it to the stack
2956 $piece = array(
2957 'open' => $curChar,
2958 'close' => $rule['end'],
2959 'count' => $count,
2960 'parts' => array( '' ),
2961 'eqpos' => array(),
2962 'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
2963 );
2964
2965 $stack->push( $piece );
2966 $accum =& $stack->getAccum();
2967 extract( $stack->getFlags() );
2968 } else {
2969 # Add literal brace(s)
2970 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
2971 }
2972 $i += $count;
2973 }
2974
2975 elseif ( $found == 'close' ) {
2976 $piece = $stack->top;
2977 # lets check if there are enough characters for closing brace
2978 $maxCount = $piece->count;
2979 $count = strspn( $text, $curChar, $i, $maxCount );
2980
2981 # check for maximum matching characters (if there are 5 closing
2982 # characters, we will probably need only 3 - depending on the rules)
2983 $matchingCount = 0;
2984 $rule = $rules[$piece->open];
2985 if ( $count > $rule['max'] ) {
2986 # The specified maximum exists in the callback array, unless the caller
2987 # has made an error
2988 $matchingCount = $rule['max'];
2989 } else {
2990 # Count is less than the maximum
2991 # Skip any gaps in the callback array to find the true largest match
2992 # Need to use array_key_exists not isset because the callback can be null
2993 $matchingCount = $count;
2994 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
2995 --$matchingCount;
2996 }
2997 }
2998
2999 if ($matchingCount <= 0) {
3000 # No matching element found in callback array
3001 # Output a literal closing brace and continue
3002 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
3003 $i += $count;
3004 continue;
3005 }
3006 $name = $rule['names'][$matchingCount];
3007 if ( $name === null ) {
3008 // No element, just literal text
3009 $element = str_repeat( $piece->open, $matchingCount ) .
3010 implode( '|', $piece->parts ) .
3011 str_repeat( $rule['end'], $matchingCount );
3012 } else {
3013 # Create XML element
3014 # Note: $parts is already XML, does not need to be encoded further
3015 $parts = $piece->parts;
3016 $title = $parts[0];
3017 unset( $parts[0] );
3018
3019 # The invocation is at the start of the line if lineStart is set in
3020 # the stack, and all opening brackets are used up.
3021 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
3022 $attr = ' lineStart="1"';
3023 } else {
3024 $attr = '';
3025 }
3026
3027 $element = "<$name$attr>";
3028 $element .= "<title>$title</title>";
3029 $argIndex = 1;
3030 foreach ( $parts as $partIndex => $part ) {
3031 if ( isset( $piece->eqpos[$partIndex] ) ) {
3032 $eqpos = $piece->eqpos[$partIndex];
3033 $argName = substr( $part, 0, $eqpos );
3034 $argValue = substr( $part, $eqpos + 1 );
3035 $element .= "<part><name>$argName</name>=<value>$argValue</value></part>";
3036 } else {
3037 $element .= "<part><name index=\"$argIndex\" /><value>$part</value></part>";
3038 $argIndex++;
3039 }
3040 }
3041 $element .= "</$name>";
3042 }
3043
3044 # Advance input pointer
3045 $i += $matchingCount;
3046
3047 # Unwind the stack
3048 $stack->pop();
3049 $accum =& $stack->getAccum();
3050
3051 # Re-add the old stack element if it still has unmatched opening characters remaining
3052 if ($matchingCount < $piece->count) {
3053 $piece->parts = array( '' );
3054 $piece->count -= $matchingCount;
3055 $piece->eqpos = array();
3056 # do we still qualify for any callback with remaining count?
3057 $names = $rules[$piece->open]['names'];
3058 $skippedBraces = 0;
3059 $enclosingAccum =& $accum;
3060 while ( $piece->count ) {
3061 if ( array_key_exists( $piece->count, $names ) ) {
3062 $stack->push( $piece );
3063 $accum =& $stack->getAccum();
3064 break;
3065 }
3066 --$piece->count;
3067 $skippedBraces ++;
3068 }
3069 $enclosingAccum .= str_repeat( $piece->open, $skippedBraces );
3070 }
3071
3072 extract( $stack->getFlags() );
3073
3074 # Add XML element to the enclosing accumulator
3075 $accum .= $element;
3076 }
3077
3078 elseif ( $found == 'pipe' ) {
3079 $findEquals = true; // shortcut for getFlags()
3080 $stack->top->addPart();
3081 $accum =& $stack->getAccum();
3082 ++$i;
3083 }
3084
3085 elseif ( $found == 'equals' ) {
3086 $findEquals = false; // shortcut for getFlags()
3087 $partsCount = count( $stack->top->parts );
3088 $stack->top->eqpos[$partsCount - 1] = strlen( $accum );
3089 $accum .= '=';
3090 ++$i;
3091 }
3092 }
3093
3094 # Output any remaining unclosed brackets
3095 foreach ( $stack->stack as $piece ) {
3096 if ( $piece->open == "\n" ) {
3097 $stack->topAccum .= $piece->parts[0];
3098 } else {
3099 $stack->topAccum .= str_repeat( $piece->open, $piece->count ) . implode( '|', $piece->parts );
3100 }
3101 }
3102 $stack->topAccum .= '</root>';
3103 $xml = $stack->topAccum;
3104
3105 wfProfileOut( __METHOD__.'-makexml' );
3106 wfProfileIn( __METHOD__.'-loadXML' );
3107 $dom = new DOMDocument;
3108 wfSuppressWarnings();
3109 $result = $dom->loadXML( $xml );
3110 wfRestoreWarnings();
3111 if ( !$result ) {
3112 // Try running the XML through UtfNormal to get rid of invalid characters
3113 $xml = UtfNormal::cleanUp( $xml );
3114 $result = $dom->loadXML( $xml );
3115 if ( !$result ) {
3116 throw new MWException( __METHOD__.' generated invalid XML' );
3117 }
3118 }
3119 wfProfileOut( __METHOD__.'-loadXML' );
3120 wfProfileOut( __METHOD__ );
3121 return $dom;
3122 }
3123
3124 /*
3125 * Return a three-element array: leading whitespace, string contents, trailing whitespace
3126 */
3127 public static function splitWhitespace( $s ) {
3128 $ltrimmed = ltrim( $s );
3129 $w1 = substr( $s, 0, strlen( $s ) - strlen( $ltrimmed ) );
3130 $trimmed = rtrim( $ltrimmed );
3131 $diff = strlen( $ltrimmed ) - strlen( $trimmed );
3132 if ( $diff > 0 ) {
3133 $w2 = substr( $ltrimmed, -$diff );
3134 } else {
3135 $w2 = '';
3136 }
3137 return array( $w1, $trimmed, $w2 );
3138 }
3139
3140 /**
3141 * Replace magic variables, templates, and template arguments
3142 * with the appropriate text. Templates are substituted recursively,
3143 * taking care to avoid infinite loops.
3144 *
3145 * Note that the substitution depends on value of $mOutputType:
3146 * OT_WIKI: only {{subst:}} templates
3147 * OT_PREPROCESS: templates but not extension tags
3148 * OT_HTML: all templates and extension tags
3149 *
3150 * @param string $tex The text to transform
3151 * @param PPFrame $frame Object describing the arguments passed to the template
3152 * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
3153 * @private
3154 */
3155 function replaceVariables( $text, $frame = false, $argsOnly = false ) {
3156 # Prevent too big inclusions
3157 if( strlen( $text ) > $this->mOptions->getMaxIncludeSize() ) {
3158 return $text;
3159 }
3160
3161 $fname = __METHOD__;
3162 wfProfileIn( $fname );
3163
3164 if ( $frame === false ) {
3165 $frame = new PPFrame( $this );
3166 } elseif ( !( $frame instanceof PPFrame ) ) {
3167 throw new MWException( __METHOD__ . ' called using the old argument format' );
3168 }
3169
3170 $dom = $this->preprocessToDom( $text );
3171 $flags = $argsOnly ? PPFrame::NO_TEMPLATES : 0;
3172 $text = $frame->expand( $dom, $flags );
3173
3174 wfProfileOut( $fname );
3175 return $text;
3176 }
3177
3178 /// Clean up argument array - refactored in 1.9 so parserfunctions can use it, too.
3179 static function createAssocArgs( $args ) {
3180 $assocArgs = array();
3181 $index = 1;
3182 foreach( $args as $arg ) {
3183 $eqpos = strpos( $arg, '=' );
3184 if ( $eqpos === false ) {
3185 $assocArgs[$index++] = $arg;
3186 } else {
3187 $name = trim( substr( $arg, 0, $eqpos ) );
3188 $value = trim( substr( $arg, $eqpos+1 ) );
3189 if ( $value === false ) {
3190 $value = '';
3191 }
3192 if ( $name !== false ) {
3193 $assocArgs[$name] = $value;
3194 }
3195 }
3196 }
3197
3198 return $assocArgs;
3199 }
3200
3201 /**
3202 * Return the text of a template, after recursively
3203 * replacing any variables or templates within the template.
3204 *
3205 * @param array $piece The parts of the template
3206 * $piece['text']: matched text
3207 * $piece['title']: the title, i.e. the part before the |
3208 * $piece['parts']: the parameter array
3209 * @param PPFrame The current frame, contains template arguments
3210 * @return string the text of the template
3211 * @private
3212 */
3213 function braceSubstitution( $piece, $frame ) {
3214 global $wgContLang, $wgLang, $wgAllowDisplayTitle, $wgNonincludableNamespaces;
3215 $fname = __METHOD__;
3216 wfProfileIn( $fname );
3217 wfProfileIn( __METHOD__.'-setup' );
3218
3219 # Flags
3220 $found = false; # $text has been filled
3221 $nowiki = false; # wiki markup in $text should be escaped
3222 $isHTML = false; # $text is HTML, armour it against wikitext transformation
3223 $forceRawInterwiki = false; # Force interwiki transclusion to be done in raw mode not rendered
3224 $isDOM = false; # $text is a DOM node needing expansion
3225
3226 # Title object, where $text came from
3227 $title = NULL;
3228
3229 # $part1 is the bit before the first |, and must contain only title characters.
3230 # Various prefixes will be stripped from it later.
3231 $titleWithSpaces = $frame->expand( $piece['title'] );
3232 $part1 = trim( $titleWithSpaces );
3233 $titleText = false;
3234
3235 # Original title text preserved for various purposes
3236 $originalTitle = $part1;
3237
3238 # $args is a list of argument nodes, starting from index 0, not including $part1
3239 $args = (null == $piece['parts']) ? array() : $piece['parts'];
3240 wfProfileOut( __METHOD__.'-setup' );
3241
3242 # SUBST
3243 wfProfileIn( __METHOD__.'-modifiers' );
3244 if ( !$found ) {
3245 $mwSubst =& MagicWord::get( 'subst' );
3246 if ( $mwSubst->matchStartAndRemove( $part1 ) xor $this->ot['wiki'] ) {
3247 # One of two possibilities is true:
3248 # 1) Found SUBST but not in the PST phase
3249 # 2) Didn't find SUBST and in the PST phase
3250 # In either case, return without further processing
3251 $text = '{{' . $frame->implode( '|', $titleWithSpaces, $args ) . '}}';
3252 $found = true;
3253 }
3254 }
3255
3256 # Variables
3257 if ( !$found && $args->length == 0 ) {
3258 $id = $this->mVariables->matchStartToEnd( $part1 );
3259 if ( $id !== false ) {
3260 $text = $this->getVariableValue( $id );
3261 if (MagicWord::getCacheTTL($id)>-1)
3262 $this->mOutput->mContainsOldMagic = true;
3263 $found = true;
3264 }
3265 }
3266
3267 # MSG, MSGNW and RAW
3268 if ( !$found ) {
3269 # Check for MSGNW:
3270 $mwMsgnw =& MagicWord::get( 'msgnw' );
3271 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3272 $nowiki = true;
3273 } else {
3274 # Remove obsolete MSG:
3275 $mwMsg =& MagicWord::get( 'msg' );
3276 $mwMsg->matchStartAndRemove( $part1 );
3277 }
3278
3279 # Check for RAW:
3280 $mwRaw =& MagicWord::get( 'raw' );
3281 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3282 $forceRawInterwiki = true;
3283 }
3284 }
3285 wfProfileOut( __METHOD__.'-modifiers' );
3286
3287 # Parser functions
3288 if ( !$found ) {
3289 wfProfileIn( __METHOD__ . '-pfunc' );
3290
3291 $colonPos = strpos( $part1, ':' );
3292 if ( $colonPos !== false ) {
3293 # Case sensitive functions
3294 $function = substr( $part1, 0, $colonPos );
3295 if ( isset( $this->mFunctionSynonyms[1][$function] ) ) {
3296 $function = $this->mFunctionSynonyms[1][$function];
3297 } else {
3298 # Case insensitive functions
3299 $function = strtolower( $function );
3300 if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
3301 $function = $this->mFunctionSynonyms[0][$function];
3302 } else {
3303 $function = false;
3304 }
3305 }
3306 if ( $function ) {
3307 list( $callback, $flags ) = $this->mFunctionHooks[$function];
3308 $initialArgs = array( &$this );
3309 $funcArgs = array( trim( substr( $part1, $colonPos + 1 ) ) );
3310 if ( $flags & SFH_OBJECT_ARGS ) {
3311 # Add a frame parameter, and pass the arguments as an array
3312 $allArgs = $initialArgs;
3313 $allArgs[] = $frame;
3314 foreach ( $args as $arg ) {
3315 $funcArgs[] = $arg;
3316 }
3317 $allArgs[] = $funcArgs;
3318 } else {
3319 # Convert arguments to plain text
3320 foreach ( $args as $arg ) {
3321 $funcArgs[] = trim( $frame->expand( $arg ) );
3322 }
3323 $allArgs = array_merge( $initialArgs, $funcArgs );
3324 }
3325
3326 $result = call_user_func_array( $callback, $allArgs );
3327 $found = true;
3328
3329 if ( is_array( $result ) ) {
3330 if ( isset( $result[0] ) ) {
3331 $text = $result[0];
3332 unset( $result[0] );
3333 }
3334
3335 // Extract flags into the local scope
3336 // This allows callers to set flags such as nowiki, found, etc.
3337 extract( $result );
3338 } else {
3339 $text = $result;
3340 }
3341 }
3342 }
3343 wfProfileOut( __METHOD__ . '-pfunc' );
3344 }
3345
3346 # Finish mangling title and then check for loops.
3347 # Set $title to a Title object and $titleText to the PDBK
3348 if ( !$found ) {
3349 $ns = NS_TEMPLATE;
3350 # Split the title into page and subpage
3351 $subpage = '';
3352 $part1 = $this->maybeDoSubpageLink( $part1, $subpage );
3353 if ($subpage !== '') {
3354 $ns = $this->mTitle->getNamespace();
3355 }
3356 $title = Title::newFromText( $part1, $ns );
3357 if ( $title ) {
3358 $titleText = $title->getPrefixedText();
3359 # Check for language variants if the template is not found
3360 if($wgContLang->hasVariants() && $title->getArticleID() == 0){
3361 $wgContLang->findVariantLink($part1, $title);
3362 }
3363 # Do infinite loop check
3364 if ( !$frame->loopCheck( $title ) ) {
3365 $found = true;
3366 $text = "<span class=\"error\">Template loop detected: [[$titleText]]</span>";
3367 wfDebug( __METHOD__.": template loop broken at '$titleText'\n" );
3368 }
3369 # Do recursion depth check
3370 $limit = $this->mOptions->getMaxTemplateDepth();
3371 if ( $frame->depth >= $limit ) {
3372 $found = true;
3373 $text = "<span class=\"error\">Template recursion depth limit exceeded ($limit)</span>";
3374 }
3375 }
3376 }
3377
3378 # Load from database
3379 if ( !$found && $title ) {
3380 wfProfileIn( __METHOD__ . '-loadtpl' );
3381 if ( !$title->isExternal() ) {
3382 if ( $title->getNamespace() == NS_SPECIAL && $this->mOptions->getAllowSpecialInclusion() && $this->ot['html'] ) {
3383 $text = SpecialPage::capturePath( $title );
3384 if ( is_string( $text ) ) {
3385 $found = true;
3386 $isHTML = true;
3387 $this->disableCache();
3388 }
3389 } else if ( $wgNonincludableNamespaces && in_array( $title->getNamespace(), $wgNonincludableNamespaces ) ) {
3390 $found = false; //access denied
3391 wfDebug( "$fname: template inclusion denied for " . $title->getPrefixedDBkey() );
3392 } else {
3393 list( $text, $title ) = $this->getTemplateDom( $title );
3394 if ( $text !== false ) {
3395 $found = true;
3396 $isDOM = true;
3397 }
3398 }
3399
3400 # If the title is valid but undisplayable, make a link to it
3401 if ( !$found && ( $this->ot['html'] || $this->ot['pre'] ) ) {
3402 $text = "[[:$titleText]]";
3403 $found = true;
3404 }
3405 } elseif ( $title->isTrans() ) {
3406 // Interwiki transclusion
3407 if ( $this->ot['html'] && !$forceRawInterwiki ) {
3408 $text = $this->interwikiTransclude( $title, 'render' );
3409 $isHTML = true;
3410 } else {
3411 $text = $this->interwikiTransclude( $title, 'raw' );
3412 // Preprocess it like a template
3413 $text = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
3414 $isDOM = true;
3415 }
3416 $found = true;
3417 }
3418 wfProfileOut( __METHOD__ . '-loadtpl' );
3419 }
3420
3421 # If we haven't found text to substitute by now, we're done
3422 # Recover the source wikitext and return it
3423 if ( !$found ) {
3424 $text = '{{' . $frame->implode( '|', $titleWithSpaces, $args ) . '}}';
3425 wfProfileOut( $fname );
3426 return $text;
3427 }
3428
3429 # Expand DOM-style return values in a child frame
3430 if ( $isDOM ) {
3431 # Clean up argument array
3432 $newFrame = $frame->newChild( $args, $title );
3433
3434 if ( $titleText !== false && $newFrame->isEmpty() ) {
3435 # Expansion is eligible for the empty-frame cache
3436 if ( isset( $this->mTplExpandCache[$titleText] ) ) {
3437 $text = $this->mTplExpandCache[$titleText];
3438 } else {
3439 $text = $newFrame->expand( $text );
3440 $this->mTplExpandCache[$titleText] = $text;
3441 }
3442 } else {
3443 # Uncached expansion
3444 $text = $newFrame->expand( $text );
3445 }
3446 }
3447
3448 # Replace raw HTML by a placeholder
3449 # Add a blank line preceding, to prevent it from mucking up
3450 # immediately preceding headings
3451 if ( $isHTML ) {
3452 $text = "\n\n" . $this->insertStripItem( $text );
3453 }
3454 # Escape nowiki-style return values
3455 elseif ( $nowiki && ( $this->ot['html'] || $this->ot['pre'] ) ) {
3456 $text = wfEscapeWikiText( $text );
3457 }
3458 # Bug 529: if the template begins with a table or block-level
3459 # element, it should be treated as beginning a new line.
3460 # This behaviour is somewhat controversial.
3461 elseif ( !$piece['lineStart'] && preg_match('/^(?:{\\||:|;|#|\*)/', $text)) /*}*/{
3462 $text = "\n" . $text;
3463 }
3464
3465 if ( !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) {
3466 # Error, oversize inclusion
3467 $text = "[[$originalTitle]]" .
3468 $this->insertStripItem( '<!-- WARNING: template omitted, post-expand include size too large -->' );
3469 }
3470
3471 wfProfileOut( $fname );
3472 return $text;
3473 }
3474
3475 /**
3476 * Get the semi-parsed DOM representation of a template with a given title,
3477 * and its redirect destination title. Cached.
3478 */
3479 function getTemplateDom( $title ) {
3480 $cacheTitle = $title;
3481 $titleText = $title->getPrefixedDBkey();
3482
3483 if ( isset( $this->mTplRedirCache[$titleText] ) ) {
3484 list( $ns, $dbk ) = $this->mTplRedirCache[$titleText];
3485 $title = Title::makeTitle( $ns, $dbk );
3486 $titleText = $title->getPrefixedDBkey();
3487 }
3488 if ( isset( $this->mTplDomCache[$titleText] ) ) {
3489 return array( $this->mTplDomCache[$titleText], $title );
3490 }
3491
3492 // Cache miss, go to the database
3493 list( $text, $title ) = $this->fetchTemplateAndTitle( $title );
3494
3495 if ( $text === false ) {
3496 $this->mTplDomCache[$titleText] = false;
3497 return array( false, $title );
3498 }
3499
3500 $dom = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION );
3501 $this->mTplDomCache[ $titleText ] = $dom;
3502
3503 if (! $title->equals($cacheTitle)) {
3504 $this->mTplRedirCache[$cacheTitle->getPrefixedDBkey()] =
3505 array( $title->getNamespace(),$cdb = $title->getDBkey() );
3506 }
3507
3508 return array( $dom, $title );
3509 }
3510
3511 /**
3512 * Fetch the unparsed text of a template and register a reference to it.
3513 */
3514 function fetchTemplateAndTitle( $title ) {
3515 $templateCb = $this->mOptions->getTemplateCallback();
3516 $stuff = call_user_func( $templateCb, $title );
3517 $text = $stuff['text'];
3518 $finalTitle = isset( $stuff['finalTitle'] ) ? $stuff['finalTitle'] : $title;
3519 if ( isset( $stuff['deps'] ) ) {
3520 foreach ( $stuff['deps'] as $dep ) {
3521 $this->mOutput->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] );
3522 }
3523 }
3524 return array($text,$finalTitle);
3525 }
3526
3527 function fetchTemplate( $title ) {
3528 $rv = $this->fetchTemplateAndTitle($title);
3529 return $rv[0];
3530 }
3531
3532 /**
3533 * Static function to get a template
3534 * Can be overridden via ParserOptions::setTemplateCallback().
3535 */
3536 static function statelessFetchTemplate( $title ) {
3537 $text = $skip = false;
3538 $finalTitle = $title;
3539 $deps = array();
3540
3541 // Loop to fetch the article, with up to 1 redirect
3542 for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
3543 # Give extensions a chance to select the revision instead
3544 $id = false; // Assume current
3545 wfRunHooks( 'BeforeParserFetchTemplateAndtitle', array( false, &$title, &$skip, &$id ) );
3546
3547 if( $skip ) {
3548 $text = false;
3549 $deps[] = array(
3550 'title' => $title,
3551 'page_id' => $title->getArticleID(),
3552 'rev_id' => null );
3553 break;
3554 }
3555 $rev = $id ? Revision::newFromId( $id ) : Revision::newFromTitle( $title );
3556 $rev_id = $rev ? $rev->getId() : 0;
3557
3558 $deps[] = array(
3559 'title' => $title,
3560 'page_id' => $title->getArticleID(),
3561 'rev_id' => $rev_id );
3562
3563 if( $rev ) {
3564 $text = $rev->getText();
3565 } elseif( $title->getNamespace() == NS_MEDIAWIKI ) {
3566 global $wgLang;
3567 $message = $wgLang->lcfirst( $title->getText() );
3568 $text = wfMsgForContentNoTrans( $message );
3569 if( wfEmptyMsg( $message, $text ) ) {
3570 $text = false;
3571 break;
3572 }
3573 } else {
3574 break;
3575 }
3576 if ( $text === false ) {
3577 break;
3578 }
3579 // Redirect?
3580 $finalTitle = $title;
3581 $title = Title::newFromRedirect( $text );
3582 }
3583 return array(
3584 'text' => $text,
3585 'finalTitle' => $finalTitle,
3586 'deps' => $deps );
3587 }
3588
3589 /**
3590 * Transclude an interwiki link.
3591 */
3592 function interwikiTransclude( $title, $action ) {
3593 global $wgEnableScaryTranscluding;
3594
3595 if (!$wgEnableScaryTranscluding)
3596 return wfMsg('scarytranscludedisabled');
3597
3598 $url = $title->getFullUrl( "action=$action" );
3599
3600 if (strlen($url) > 255)
3601 return wfMsg('scarytranscludetoolong');
3602 return $this->fetchScaryTemplateMaybeFromCache($url);
3603 }
3604
3605 function fetchScaryTemplateMaybeFromCache($url) {
3606 global $wgTranscludeCacheExpiry;
3607 $dbr = wfGetDB(DB_SLAVE);
3608 $obj = $dbr->selectRow('transcache', array('tc_time', 'tc_contents'),
3609 array('tc_url' => $url));
3610 if ($obj) {
3611 $time = $obj->tc_time;
3612 $text = $obj->tc_contents;
3613 if ($time && time() < $time + $wgTranscludeCacheExpiry ) {
3614 return $text;
3615 }
3616 }
3617
3618 $text = Http::get($url);
3619 if (!$text)
3620 return wfMsg('scarytranscludefailed', $url);
3621
3622 $dbw = wfGetDB(DB_MASTER);
3623 $dbw->replace('transcache', array('tc_url'), array(
3624 'tc_url' => $url,
3625 'tc_time' => time(),
3626 'tc_contents' => $text));
3627 return $text;
3628 }
3629
3630
3631 /**
3632 * Triple brace replacement -- used for template arguments
3633 * @private
3634 */
3635 function argSubstitution( $piece, $frame ) {
3636 wfProfileIn( __METHOD__ );
3637
3638 $error = false;
3639 $parts = $piece['parts'];
3640 $nameWithSpaces = $frame->expand( $piece['title'] );
3641 $argName = trim( $nameWithSpaces );
3642
3643 $text = $frame->getArgument( $argName );
3644 if ( $text === false && ( $this->ot['html'] || $this->ot['pre'] ) && $parts->length > 0 ) {
3645 # No match in frame, use the supplied default
3646 $text = $frame->expand( $parts->item( 0 ) );
3647 }
3648 if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
3649 $error = '<!-- WARNING: argument omitted, expansion size too large -->';
3650 }
3651
3652 if ( $text === false ) {
3653 # No match anywhere
3654 $text = '{{{' . $frame->implode( '|', $nameWithSpaces, $parts ) . '}}}';
3655 }
3656 if ( $error !== false ) {
3657 $text .= $error;
3658 }
3659
3660 wfProfileOut( __METHOD__ );
3661 return $text;
3662 }
3663
3664 /**
3665 * Return the text to be used for a given extension tag.
3666 * This is the ghost of strip().
3667 *
3668 * @param array $params Associative array of parameters:
3669 * name DOMNode for the tag name
3670 * attr DOMNode for unparsed text where tag attributes are thought to be
3671 * attributes Optional associative array of parsed attributes
3672 * inner Contents of extension element
3673 * noClose Original text did not have a close tag
3674 * @param PPFrame $frame
3675 */
3676 function extensionSubstitution( $params, $frame ) {
3677 global $wgRawHtml, $wgContLang;
3678 static $n = 1;
3679
3680 $name = $frame->expand( $params['name'] );
3681 $attrText = !isset( $params['attr'] ) ? null : $frame->expand( $params['attr'] );
3682 $content = !isset( $params['inner'] ) ? null : $frame->expand( $params['inner'] );
3683
3684 $marker = "{$this->mUniqPrefix}-$name-" . sprintf('%08X', $n++) . $this->mMarkerSuffix;
3685
3686 if ( $this->ot['html'] ) {
3687 $name = strtolower( $name );
3688
3689 $attributes = Sanitizer::decodeTagAttributes( $attrText );
3690 if ( isset( $params['attributes'] ) ) {
3691 $attributes = $attributes + $params['attributes'];
3692 }
3693 switch ( $name ) {
3694 case 'html':
3695 if( $wgRawHtml ) {
3696 $output = $content;
3697 break;
3698 } else {
3699 throw new MWException( '<html> extension tag encountered unexpectedly' );
3700 }
3701 case 'nowiki':
3702 $output = Xml::escapeTagsOnly( $content );
3703 break;
3704 case 'math':
3705 $output = $wgContLang->armourMath(
3706 MathRenderer::renderMath( $content, $attributes ) );
3707 break;
3708 case 'gallery':
3709 $output = $this->renderImageGallery( $content, $attributes );
3710 break;
3711 default:
3712 if( isset( $this->mTagHooks[$name] ) ) {
3713 $output = call_user_func_array( $this->mTagHooks[$name],
3714 array( $content, $attributes, $this ) );
3715 } else {
3716 throw new MWException( "Invalid call hook $name" );
3717 }
3718 }
3719 } else {
3720 if ( is_null( $attrText ) ) {
3721 $attrText = '';
3722 }
3723 if ( isset( $params['attributes'] ) ) {
3724 foreach ( $params['attributes'] as $attrName => $attrValue ) {
3725 $attrText .= ' ' . htmlspecialchars( $attrName ) . '="' .
3726 htmlspecialchars( $attrValue ) . '"';
3727 }
3728 }
3729 if ( $content === null ) {
3730 $output = "<$name$attrText/>";
3731 } else {
3732 $close = is_null( $params['close'] ) ? '' : $frame->expand( $params['close'] );
3733 $output = "<$name$attrText>$content$close";
3734 }
3735 }
3736
3737 if ( $name == 'html' || $name == 'nowiki' ) {
3738 $this->mStripState->nowiki->setPair( $marker, $output );
3739 } else {
3740 $this->mStripState->general->setPair( $marker, $output );
3741 }
3742 return $marker;
3743 }
3744
3745 /**
3746 * Increment an include size counter
3747 *
3748 * @param string $type The type of expansion
3749 * @param integer $size The size of the text
3750 * @return boolean False if this inclusion would take it over the maximum, true otherwise
3751 */
3752 function incrementIncludeSize( $type, $size ) {
3753 if ( $this->mIncludeSizes[$type] + $size > $this->mOptions->getMaxIncludeSize( $type ) ) {
3754 return false;
3755 } else {
3756 $this->mIncludeSizes[$type] += $size;
3757 return true;
3758 }
3759 }
3760
3761 /**
3762 * Detect __NOGALLERY__ magic word and set a placeholder
3763 */
3764 function stripNoGallery( &$text ) {
3765 # if the string __NOGALLERY__ (not case-sensitive) occurs in the HTML,
3766 # do not add TOC
3767 $mw = MagicWord::get( 'nogallery' );
3768 $this->mOutput->mNoGallery = $mw->matchAndRemove( $text ) ;
3769 }
3770
3771 /**
3772 * Find the first __TOC__ magic word and set a <!--MWTOC-->
3773 * placeholder that will then be replaced by the real TOC in
3774 * ->formatHeadings, this works because at this points real
3775 * comments will have already been discarded by the sanitizer.
3776 *
3777 * Any additional __TOC__ magic words left over will be discarded
3778 * as there can only be one TOC on the page.
3779 */
3780 function stripToc( $text ) {
3781 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
3782 # do not add TOC
3783 $mw = MagicWord::get( 'notoc' );
3784 if( $mw->matchAndRemove( $text ) ) {
3785 $this->mShowToc = false;
3786 }
3787
3788 $mw = MagicWord::get( 'toc' );
3789 if( $mw->match( $text ) ) {
3790 $this->mShowToc = true;
3791 $this->mForceTocPosition = true;
3792
3793 // Set a placeholder. At the end we'll fill it in with the TOC.
3794 $text = $mw->replace( '<!--MWTOC-->', $text, 1 );
3795
3796 // Only keep the first one.
3797 $text = $mw->replace( '', $text );
3798 }
3799 return $text;
3800 }
3801
3802 /**
3803 * This function accomplishes several tasks:
3804 * 1) Auto-number headings if that option is enabled
3805 * 2) Add an [edit] link to sections for users who have enabled the option and can edit the page
3806 * 3) Add a Table of contents on the top for users who have enabled the option
3807 * 4) Auto-anchor headings
3808 *
3809 * It loops through all headlines, collects the necessary data, then splits up the
3810 * string and re-inserts the newly formatted headlines.
3811 *
3812 * @param string $text
3813 * @param boolean $isMain
3814 * @private
3815 */
3816 function formatHeadings( $text, $isMain=true ) {
3817 global $wgMaxTocLevel, $wgContLang;
3818
3819 $doNumberHeadings = $this->mOptions->getNumberHeadings();
3820 if( !$this->mTitle->quickUserCan( 'edit' ) ) {
3821 $showEditLink = 0;
3822 } else {
3823 $showEditLink = $this->mOptions->getEditSection();
3824 }
3825
3826 # Inhibit editsection links if requested in the page
3827 $esw =& MagicWord::get( 'noeditsection' );
3828 if( $esw->matchAndRemove( $text ) ) {
3829 $showEditLink = 0;
3830 }
3831
3832 # Get all headlines for numbering them and adding funky stuff like [edit]
3833 # links - this is for later, but we need the number of headlines right now
3834 $matches = array();
3835 $numMatches = preg_match_all( '/<H(?P<level>[1-6])(?P<attrib>.*?'.'>)(?P<header>.*?)<\/H[1-6] *>/i', $text, $matches );
3836
3837 # if there are fewer than 4 headlines in the article, do not show TOC
3838 # unless it's been explicitly enabled.
3839 $enoughToc = $this->mShowToc &&
3840 (($numMatches >= 4) || $this->mForceTocPosition);
3841
3842 # Allow user to stipulate that a page should have a "new section"
3843 # link added via __NEWSECTIONLINK__
3844 $mw =& MagicWord::get( 'newsectionlink' );
3845 if( $mw->matchAndRemove( $text ) )
3846 $this->mOutput->setNewSection( true );
3847
3848 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
3849 # override above conditions and always show TOC above first header
3850 $mw =& MagicWord::get( 'forcetoc' );
3851 if ($mw->matchAndRemove( $text ) ) {
3852 $this->mShowToc = true;
3853 $enoughToc = true;
3854 }
3855
3856 # We need this to perform operations on the HTML
3857 $sk = $this->mOptions->getSkin();
3858
3859 # headline counter
3860 $headlineCount = 0;
3861 $numVisible = 0;
3862
3863 # Ugh .. the TOC should have neat indentation levels which can be
3864 # passed to the skin functions. These are determined here
3865 $toc = '';
3866 $full = '';
3867 $head = array();
3868 $sublevelCount = array();
3869 $levelCount = array();
3870 $toclevel = 0;
3871 $level = 0;
3872 $prevlevel = 0;
3873 $toclevel = 0;
3874 $prevtoclevel = 0;
3875 $markerRegex = "{$this->mUniqPrefix}-h-(\d+)-{$this->mMarkerSuffix}";
3876 $baseTitleText = $this->mTitle->getPrefixedDBkey();
3877 $tocraw = array();
3878
3879 foreach( $matches[3] as $headline ) {
3880 $isTemplate = false;
3881 $titleText = false;
3882 $sectionIndex = false;
3883 $numbering = '';
3884 $markerMatches = array();
3885 if (preg_match("/^$markerRegex/", $headline, $markerMatches)) {
3886 $serial = $markerMatches[1];
3887 list( $titleText, $sectionIndex ) = $this->mHeadings[$serial];
3888 $isTemplate = ($titleText != $baseTitleText);
3889 $headline = preg_replace("/^$markerRegex/", "", $headline);
3890 }
3891
3892 if( $toclevel ) {
3893 $prevlevel = $level;
3894 $prevtoclevel = $toclevel;
3895 }
3896 $level = $matches[1][$headlineCount];
3897
3898 if( $doNumberHeadings || $enoughToc ) {
3899
3900 if ( $level > $prevlevel ) {
3901 # Increase TOC level
3902 $toclevel++;
3903 $sublevelCount[$toclevel] = 0;
3904 if( $toclevel<$wgMaxTocLevel ) {
3905 $prevtoclevel = $toclevel;
3906 $toc .= $sk->tocIndent();
3907 $numVisible++;
3908 }
3909 }
3910 elseif ( $level < $prevlevel && $toclevel > 1 ) {
3911 # Decrease TOC level, find level to jump to
3912
3913 if ( $toclevel == 2 && $level <= $levelCount[1] ) {
3914 # Can only go down to level 1
3915 $toclevel = 1;
3916 } else {
3917 for ($i = $toclevel; $i > 0; $i--) {
3918 if ( $levelCount[$i] == $level ) {
3919 # Found last matching level
3920 $toclevel = $i;
3921 break;
3922 }
3923 elseif ( $levelCount[$i] < $level ) {
3924 # Found first matching level below current level
3925 $toclevel = $i + 1;
3926 break;
3927 }
3928 }
3929 }
3930 if( $toclevel<$wgMaxTocLevel ) {
3931 if($prevtoclevel < $wgMaxTocLevel) {
3932 # Unindent only if the previous toc level was shown :p
3933 $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
3934 } else {
3935 $toc .= $sk->tocLineEnd();
3936 }
3937 }
3938 }
3939 else {
3940 # No change in level, end TOC line
3941 if( $toclevel<$wgMaxTocLevel ) {
3942 $toc .= $sk->tocLineEnd();
3943 }
3944 }
3945
3946 $levelCount[$toclevel] = $level;
3947
3948 # count number of headlines for each level
3949 @$sublevelCount[$toclevel]++;
3950 $dot = 0;
3951 for( $i = 1; $i <= $toclevel; $i++ ) {
3952 if( !empty( $sublevelCount[$i] ) ) {
3953 if( $dot ) {
3954 $numbering .= '.';
3955 }
3956 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
3957 $dot = 1;
3958 }
3959 }
3960 }
3961
3962 # The safe header is a version of the header text safe to use for links
3963 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
3964 $safeHeadline = $this->mStripState->unstripBoth( $headline );
3965
3966 # Remove link placeholders by the link text.
3967 # <!--LINK number-->
3968 # turns into
3969 # link text with suffix
3970 $safeHeadline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
3971 "\$this->mLinkHolders['texts'][\$1]",
3972 $safeHeadline );
3973 $safeHeadline = preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
3974 "\$this->mInterwikiLinkHolders['texts'][\$1]",
3975 $safeHeadline );
3976
3977 # Strip out HTML (other than plain <sup> and <sub>: bug 8393)
3978 $tocline = preg_replace(
3979 array( '#<(?!/?(sup|sub)).*?'.'>#', '#<(/?(sup|sub)).*?'.'>#' ),
3980 array( '', '<$1>'),
3981 $safeHeadline
3982 );
3983 $tocline = trim( $tocline );
3984
3985 # For the anchor, strip out HTML-y stuff period
3986 $safeHeadline = preg_replace( '/<.*?'.'>/', '', $safeHeadline );
3987 $safeHeadline = trim( $safeHeadline );
3988
3989 # Save headline for section edit hint before it's escaped
3990 $headlineHint = $safeHeadline;
3991 $safeHeadline = Sanitizer::escapeId( $safeHeadline );
3992 $refers[$headlineCount] = $safeHeadline;
3993
3994 # count how many in assoc. array so we can track dupes in anchors
3995 isset( $refers[$safeHeadline] ) ? $refers[$safeHeadline]++ : $refers[$safeHeadline] = 1;
3996 $refcount[$headlineCount] = $refers[$safeHeadline];
3997
3998 # Don't number the heading if it is the only one (looks silly)
3999 if( $doNumberHeadings && count( $matches[3] ) > 1) {
4000 # the two are different if the line contains a link
4001 $headline=$numbering . ' ' . $headline;
4002 }
4003
4004 # Create the anchor for linking from the TOC to the section
4005 $anchor = $safeHeadline;
4006 if($refcount[$headlineCount] > 1 ) {
4007 $anchor .= '_' . $refcount[$headlineCount];
4008 }
4009 if( $enoughToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
4010 $toc .= $sk->tocLine($anchor, $tocline, $numbering, $toclevel);
4011 $tocraw[] = array( 'toclevel' => $toclevel, 'level' => $level, 'line' => $tocline, 'number' => $numbering );
4012 }
4013 # give headline the correct <h#> tag
4014 if( $showEditLink && $sectionIndex !== false ) {
4015 if( $isTemplate ) {
4016 # Put a T flag in the section identifier, to indicate to extractSections()
4017 # that sections inside <includeonly> should be counted.
4018 $editlink = $sk->editSectionLinkForOther($titleText, "T-$sectionIndex");
4019 } else {
4020 $editlink = $sk->editSectionLink($this->mTitle, $sectionIndex, $headlineHint);
4021 }
4022 } else {
4023 $editlink = '';
4024 }
4025 $head[$headlineCount] = $sk->makeHeadline( $level, $matches['attrib'][$headlineCount], $anchor, $headline, $editlink );
4026
4027 $headlineCount++;
4028 }
4029
4030 $this->mOutput->setSections( $tocraw );
4031
4032 # Never ever show TOC if no headers
4033 if( $numVisible < 1 ) {
4034 $enoughToc = false;
4035 }
4036
4037 if( $enoughToc ) {
4038 if( $prevtoclevel > 0 && $prevtoclevel < $wgMaxTocLevel ) {
4039 $toc .= $sk->tocUnindent( $prevtoclevel - 1 );
4040 }
4041 $toc = $sk->tocList( $toc );
4042 }
4043
4044 # split up and insert constructed headlines
4045
4046 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
4047 $i = 0;
4048
4049 foreach( $blocks as $block ) {
4050 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
4051 # This is the [edit] link that appears for the top block of text when
4052 # section editing is enabled
4053
4054 # Disabled because it broke block formatting
4055 # For example, a bullet point in the top line
4056 # $full .= $sk->editSectionLink(0);
4057 }
4058 $full .= $block;
4059 if( $enoughToc && !$i && $isMain && !$this->mForceTocPosition ) {
4060 # Top anchor now in skin
4061 $full = $full.$toc;
4062 }
4063
4064 if( !empty( $head[$i] ) ) {
4065 $full .= $head[$i];
4066 }
4067 $i++;
4068 }
4069 if( $this->mForceTocPosition ) {
4070 return str_replace( '<!--MWTOC-->', $toc, $full );
4071 } else {
4072 return $full;
4073 }
4074 }
4075
4076 /**
4077 * Transform wiki markup when saving a page by doing \r\n -> \n
4078 * conversion, substitting signatures, {{subst:}} templates, etc.
4079 *
4080 * @param string $text the text to transform
4081 * @param Title &$title the Title object for the current article
4082 * @param User &$user the User object describing the current user
4083 * @param ParserOptions $options parsing options
4084 * @param bool $clearState whether to clear the parser state first
4085 * @return string the altered wiki markup
4086 * @public
4087 */
4088 function preSaveTransform( $text, &$title, $user, $options, $clearState = true ) {
4089 $this->mOptions = $options;
4090 $this->setTitle( $title );
4091 $this->setOutputType( OT_WIKI );
4092
4093 if ( $clearState ) {
4094 $this->clearState();
4095 }
4096
4097 $pairs = array(
4098 "\r\n" => "\n",
4099 );
4100 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
4101 $text = $this->pstPass2( $text, $user );
4102 $text = $this->mStripState->unstripBoth( $text );
4103 return $text;
4104 }
4105
4106 /**
4107 * Pre-save transform helper function
4108 * @private
4109 */
4110 function pstPass2( $text, $user ) {
4111 global $wgContLang, $wgLocaltimezone;
4112
4113 /* Note: This is the timestamp saved as hardcoded wikitext to
4114 * the database, we use $wgContLang here in order to give
4115 * everyone the same signature and use the default one rather
4116 * than the one selected in each user's preferences.
4117 */
4118 if ( isset( $wgLocaltimezone ) ) {
4119 $oldtz = getenv( 'TZ' );
4120 putenv( 'TZ='.$wgLocaltimezone );
4121 }
4122 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) .
4123 ' (' . date( 'T' ) . ')';
4124 if ( isset( $wgLocaltimezone ) ) {
4125 putenv( 'TZ='.$oldtz );
4126 }
4127
4128 # Variable replacement
4129 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4130 $text = $this->replaceVariables( $text );
4131
4132 # Strip out <nowiki> etc. added via replaceVariables
4133 #$text = $this->strip( $text, $this->mStripState, false, array( 'gallery' ) );
4134
4135 # Signatures
4136 $sigText = $this->getUserSig( $user );
4137 $text = strtr( $text, array(
4138 '~~~~~' => $d,
4139 '~~~~' => "$sigText $d",
4140 '~~~' => $sigText
4141 ) );
4142
4143 # Context links: [[|name]] and [[name (context)|]]
4144 #
4145 global $wgLegalTitleChars;
4146 $tc = "[$wgLegalTitleChars]";
4147 $nc = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
4148
4149 $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\))\\|]]/"; # [[ns:page (context)|]]
4150 $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\)|)(, $tc+|)\\|]]/"; # [[ns:page (context), context|]]
4151 $p2 = "/\[\[\\|($tc+)]]/"; # [[|page]]
4152
4153 # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
4154 $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
4155 $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
4156
4157 $t = $this->mTitle->getText();
4158 $m = array();
4159 if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
4160 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4161 } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && '' != "$m[1]$m[2]" ) {
4162 $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
4163 } else {
4164 # if there's no context, don't bother duplicating the title
4165 $text = preg_replace( $p2, '[[\\1]]', $text );
4166 }
4167
4168 # Trim trailing whitespace
4169 $text = rtrim( $text );
4170
4171 return $text;
4172 }
4173
4174 /**
4175 * Fetch the user's signature text, if any, and normalize to
4176 * validated, ready-to-insert wikitext.
4177 *
4178 * @param User $user
4179 * @return string
4180 * @private
4181 */
4182 function getUserSig( &$user ) {
4183 global $wgMaxSigChars;
4184
4185 $username = $user->getName();
4186 $nickname = $user->getOption( 'nickname' );
4187 $nickname = $nickname === '' ? $username : $nickname;
4188
4189 if( mb_strlen( $nickname ) > $wgMaxSigChars ) {
4190 $nickname = $username;
4191 wfDebug( __METHOD__ . ": $username has overlong signature.\n" );
4192 } elseif( $user->getBoolOption( 'fancysig' ) !== false ) {
4193 # Sig. might contain markup; validate this
4194 if( $this->validateSig( $nickname ) !== false ) {
4195 # Validated; clean up (if needed) and return it
4196 return $this->cleanSig( $nickname, true );
4197 } else {
4198 # Failed to validate; fall back to the default
4199 $nickname = $username;
4200 wfDebug( "Parser::getUserSig: $username has bad XML tags in signature.\n" );
4201 }
4202 }
4203
4204 // Make sure nickname doesnt get a sig in a sig
4205 $nickname = $this->cleanSigInSig( $nickname );
4206
4207 # If we're still here, make it a link to the user page
4208 $userText = wfEscapeWikiText( $username );
4209 $nickText = wfEscapeWikiText( $nickname );
4210 if ( $user->isAnon() ) {
4211 return wfMsgExt( 'signature-anon', array( 'content', 'parsemag' ), $userText, $nickText );
4212 } else {
4213 return wfMsgExt( 'signature', array( 'content', 'parsemag' ), $userText, $nickText );
4214 }
4215 }
4216
4217 /**
4218 * Check that the user's signature contains no bad XML
4219 *
4220 * @param string $text
4221 * @return mixed An expanded string, or false if invalid.
4222 */
4223 function validateSig( $text ) {
4224 return( wfIsWellFormedXmlFragment( $text ) ? $text : false );
4225 }
4226
4227 /**
4228 * Clean up signature text
4229 *
4230 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures @see cleanSigInSig
4231 * 2) Substitute all transclusions
4232 *
4233 * @param string $text
4234 * @param $parsing Whether we're cleaning (preferences save) or parsing
4235 * @return string Signature text
4236 */
4237 function cleanSig( $text, $parsing = false ) {
4238 if ( !$parsing ) {
4239 global $wgTitle;
4240 $this->clearState();
4241 $this->setTitle( $wgTitle );
4242 $this->mOptions = new ParserOptions;
4243 $this->setOutputType = OT_PREPROCESS;
4244 }
4245
4246 # FIXME: regex doesn't respect extension tags or nowiki
4247 # => Move this logic to braceSubstitution()
4248 $substWord = MagicWord::get( 'subst' );
4249 $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
4250 $substText = '{{' . $substWord->getSynonym( 0 );
4251
4252 $text = preg_replace( $substRegex, $substText, $text );
4253 $text = $this->cleanSigInSig( $text );
4254 $dom = $this->preprocessToDom( $text );
4255 $frame = new PPFrame( $this );
4256 $text = $frame->expand( $dom->documentElement );
4257
4258 if ( !$parsing ) {
4259 $text = $this->mStripState->unstripBoth( $text );
4260 }
4261
4262 return $text;
4263 }
4264
4265 /**
4266 * Strip ~~~, ~~~~ and ~~~~~ out of signatures
4267 * @param string $text
4268 * @return string Signature text with /~{3,5}/ removed
4269 */
4270 function cleanSigInSig( $text ) {
4271 $text = preg_replace( '/~{3,5}/', '', $text );
4272 return $text;
4273 }
4274
4275 /**
4276 * Set up some variables which are usually set up in parse()
4277 * so that an external function can call some class members with confidence
4278 * @public
4279 */
4280 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
4281 $this->setTitle( $title );
4282 $this->mOptions = $options;
4283 $this->setOutputType( $outputType );
4284 if ( $clearState ) {
4285 $this->clearState();
4286 }
4287 }
4288
4289 /**
4290 * Wrapper for preprocess()
4291 *
4292 * @param string $text the text to preprocess
4293 * @param ParserOptions $options options
4294 * @return string
4295 * @public
4296 */
4297 function transformMsg( $text, $options ) {
4298 global $wgTitle;
4299 static $executing = false;
4300
4301 $fname = "Parser::transformMsg";
4302
4303 # Guard against infinite recursion
4304 if ( $executing ) {
4305 return $text;
4306 }
4307 $executing = true;
4308
4309 wfProfileIn($fname);
4310 $text = $this->preprocess( $text, $wgTitle, $options );
4311
4312 $executing = false;
4313 wfProfileOut($fname);
4314 return $text;
4315 }
4316
4317 /**
4318 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
4319 * The callback should have the following form:
4320 * function myParserHook( $text, $params, &$parser ) { ... }
4321 *
4322 * Transform and return $text. Use $parser for any required context, e.g. use
4323 * $parser->getTitle() and $parser->getOptions() not $wgTitle or $wgOut->mParserOptions
4324 *
4325 * @public
4326 *
4327 * @param mixed $tag The tag to use, e.g. 'hook' for <hook>
4328 * @param mixed $callback The callback function (and object) to use for the tag
4329 *
4330 * @return The old value of the mTagHooks array associated with the hook
4331 */
4332 function setHook( $tag, $callback ) {
4333 $tag = strtolower( $tag );
4334 $oldVal = isset( $this->mTagHooks[$tag] ) ? $this->mTagHooks[$tag] : null;
4335 $this->mTagHooks[$tag] = $callback;
4336 $this->mStripList[] = $tag;
4337
4338 return $oldVal;
4339 }
4340
4341 function setTransparentTagHook( $tag, $callback ) {
4342 $tag = strtolower( $tag );
4343 $oldVal = isset( $this->mTransparentTagHooks[$tag] ) ? $this->mTransparentTagHooks[$tag] : null;
4344 $this->mTransparentTagHooks[$tag] = $callback;
4345
4346 return $oldVal;
4347 }
4348
4349 /**
4350 * Create a function, e.g. {{sum:1|2|3}}
4351 * The callback function should have the form:
4352 * function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }
4353 *
4354 * The callback may either return the text result of the function, or an array with the text
4355 * in element 0, and a number of flags in the other elements. The names of the flags are
4356 * specified in the keys. Valid flags are:
4357 * found The text returned is valid, stop processing the template. This
4358 * is on by default.
4359 * nowiki Wiki markup in the return value should be escaped
4360 * isHTML The returned text is HTML, armour it against wikitext transformation
4361 *
4362 * @public
4363 *
4364 * @param string $id The magic word ID
4365 * @param mixed $callback The callback function (and object) to use
4366 * @param integer $flags a combination of the following flags:
4367 * SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
4368 *
4369 * @return The old callback function for this name, if any
4370 */
4371 function setFunctionHook( $id, $callback, $flags = 0 ) {
4372 $oldVal = isset( $this->mFunctionHooks[$id] ) ? $this->mFunctionHooks[$id][0] : null;
4373 $this->mFunctionHooks[$id] = array( $callback, $flags );
4374
4375 # Add to function cache
4376 $mw = MagicWord::get( $id );
4377 if( !$mw )
4378 throw new MWException( 'Parser::setFunctionHook() expecting a magic word identifier.' );
4379
4380 $synonyms = $mw->getSynonyms();
4381 $sensitive = intval( $mw->isCaseSensitive() );
4382
4383 foreach ( $synonyms as $syn ) {
4384 # Case
4385 if ( !$sensitive ) {
4386 $syn = strtolower( $syn );
4387 }
4388 # Add leading hash
4389 if ( !( $flags & SFH_NO_HASH ) ) {
4390 $syn = '#' . $syn;
4391 }
4392 # Remove trailing colon
4393 if ( substr( $syn, -1, 1 ) == ':' ) {
4394 $syn = substr( $syn, 0, -1 );
4395 }
4396 $this->mFunctionSynonyms[$sensitive][$syn] = $id;
4397 }
4398 return $oldVal;
4399 }
4400
4401 /**
4402 * Get all registered function hook identifiers
4403 *
4404 * @return array
4405 */
4406 function getFunctionHooks() {
4407 return array_keys( $this->mFunctionHooks );
4408 }
4409
4410 /**
4411 * Replace <!--LINK--> link placeholders with actual links, in the buffer
4412 * Placeholders created in Skin::makeLinkObj()
4413 * Returns an array of link CSS classes, indexed by PDBK.
4414 * $options is a bit field, RLH_FOR_UPDATE to select for update
4415 */
4416 function replaceLinkHolders( &$text, $options = 0 ) {
4417 global $wgUser;
4418 global $wgContLang;
4419
4420 $fname = 'Parser::replaceLinkHolders';
4421 wfProfileIn( $fname );
4422
4423 $pdbks = array();
4424 $colours = array();
4425 $linkcolour_ids = array();
4426 $sk = $this->mOptions->getSkin();
4427 $linkCache =& LinkCache::singleton();
4428
4429 if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
4430 wfProfileIn( $fname.'-check' );
4431 $dbr = wfGetDB( DB_SLAVE );
4432 $page = $dbr->tableName( 'page' );
4433 $threshold = $wgUser->getOption('stubthreshold');
4434
4435 # Sort by namespace
4436 asort( $this->mLinkHolders['namespaces'] );
4437
4438 # Generate query
4439 $query = false;
4440 $current = null;
4441 foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
4442 # Make title object
4443 $title = $this->mLinkHolders['titles'][$key];
4444
4445 # Skip invalid entries.
4446 # Result will be ugly, but prevents crash.
4447 if ( is_null( $title ) ) {
4448 continue;
4449 }
4450 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
4451
4452 # Check if it's a static known link, e.g. interwiki
4453 if ( $title->isAlwaysKnown() ) {
4454 $colours[$pdbk] = '';
4455 } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
4456 $colours[$pdbk] = '';
4457 $this->mOutput->addLink( $title, $id );
4458 } elseif ( $linkCache->isBadLink( $pdbk ) ) {
4459 $colours[$pdbk] = 'new';
4460 } elseif ( $title->getNamespace() == NS_SPECIAL && !SpecialPage::exists( $pdbk ) ) {
4461 $colours[$pdbk] = 'new';
4462 } else {
4463 # Not in the link cache, add it to the query
4464 if ( !isset( $current ) ) {
4465 $current = $ns;
4466 $query = "SELECT page_id, page_namespace, page_title";
4467 if ( $threshold > 0 ) {
4468 $query .= ', page_len, page_is_redirect';
4469 }
4470 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
4471 } elseif ( $current != $ns ) {
4472 $current = $ns;
4473 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
4474 } else {
4475 $query .= ', ';
4476 }
4477
4478 $query .= $dbr->addQuotes( $this->mLinkHolders['dbkeys'][$key] );
4479 }
4480 }
4481 if ( $query ) {
4482 $query .= '))';
4483 if ( $options & RLH_FOR_UPDATE ) {
4484 $query .= ' FOR UPDATE';
4485 }
4486
4487 $res = $dbr->query( $query, $fname );
4488
4489 # Fetch data and form into an associative array
4490 # non-existent = broken
4491 while ( $s = $dbr->fetchObject($res) ) {
4492 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
4493 $pdbk = $title->getPrefixedDBkey();
4494 $linkCache->addGoodLinkObj( $s->page_id, $title );
4495 $this->mOutput->addLink( $title, $s->page_id );
4496 $colours[$pdbk] = $sk->getLinkColour( $s, $threshold );
4497 //add id to the extension todolist
4498 $linkcolour_ids[$s->page_id] = $pdbk;
4499 }
4500 //pass an array of page_ids to an extension
4501 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
4502 }
4503 wfProfileOut( $fname.'-check' );
4504
4505 # Do a second query for different language variants of links and categories
4506 if($wgContLang->hasVariants()){
4507 $linkBatch = new LinkBatch();
4508 $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
4509 $categoryMap = array(); // maps $category_variant => $category (dbkeys)
4510 $varCategories = array(); // category replacements oldDBkey => newDBkey
4511
4512 $categories = $this->mOutput->getCategoryLinks();
4513
4514 // Add variants of links to link batch
4515 foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
4516 $title = $this->mLinkHolders['titles'][$key];
4517 if ( is_null( $title ) )
4518 continue;
4519
4520 $pdbk = $title->getPrefixedDBkey();
4521 $titleText = $title->getText();
4522
4523 // generate all variants of the link title text
4524 $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
4525
4526 // if link was not found (in first query), add all variants to query
4527 if ( !isset($colours[$pdbk]) ){
4528 foreach($allTextVariants as $textVariant){
4529 if($textVariant != $titleText){
4530 $variantTitle = Title::makeTitle( $ns, $textVariant );
4531 if(is_null($variantTitle)) continue;
4532 $linkBatch->addObj( $variantTitle );
4533 $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
4534 }
4535 }
4536 }
4537 }
4538
4539 // process categories, check if a category exists in some variant
4540 foreach( $categories as $category ){
4541 $variants = $wgContLang->convertLinkToAllVariants($category);
4542 foreach($variants as $variant){
4543 if($variant != $category){
4544 $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
4545 if(is_null($variantTitle)) continue;
4546 $linkBatch->addObj( $variantTitle );
4547 $categoryMap[$variant] = $category;
4548 }
4549 }
4550 }
4551
4552
4553 if(!$linkBatch->isEmpty()){
4554 // construct query
4555 $titleClause = $linkBatch->constructSet('page', $dbr);
4556
4557 $variantQuery = "SELECT page_id, page_namespace, page_title";
4558 if ( $threshold > 0 ) {
4559 $variantQuery .= ', page_len, page_is_redirect';
4560 }
4561
4562 $variantQuery .= " FROM $page WHERE $titleClause";
4563 if ( $options & RLH_FOR_UPDATE ) {
4564 $variantQuery .= ' FOR UPDATE';
4565 }
4566
4567 $varRes = $dbr->query( $variantQuery, $fname );
4568
4569 // for each found variants, figure out link holders and replace
4570 while ( $s = $dbr->fetchObject($varRes) ) {
4571
4572 $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
4573 $varPdbk = $variantTitle->getPrefixedDBkey();
4574 $vardbk = $variantTitle->getDBkey();
4575
4576 $holderKeys = array();
4577 if(isset($variantMap[$varPdbk])){
4578 $holderKeys = $variantMap[$varPdbk];
4579 $linkCache->addGoodLinkObj( $s->page_id, $variantTitle );
4580 $this->mOutput->addLink( $variantTitle, $s->page_id );
4581 }
4582
4583 // loop over link holders
4584 foreach($holderKeys as $key){
4585 $title = $this->mLinkHolders['titles'][$key];
4586 if ( is_null( $title ) ) continue;
4587
4588 $pdbk = $title->getPrefixedDBkey();
4589
4590 if(!isset($colours[$pdbk])){
4591 // found link in some of the variants, replace the link holder data
4592 $this->mLinkHolders['titles'][$key] = $variantTitle;
4593 $this->mLinkHolders['dbkeys'][$key] = $variantTitle->getDBkey();
4594
4595 // set pdbk and colour
4596 $pdbks[$key] = $varPdbk;
4597 $colours[$varPdbk] = $sk->getLinkColour( $s, $threshold );
4598 $linkcolour_ids[$s->page_id] = $pdbk;
4599 }
4600 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
4601 }
4602
4603 // check if the object is a variant of a category
4604 if(isset($categoryMap[$vardbk])){
4605 $oldkey = $categoryMap[$vardbk];
4606 if($oldkey != $vardbk)
4607 $varCategories[$oldkey]=$vardbk;
4608 }
4609 }
4610
4611 // rebuild the categories in original order (if there are replacements)
4612 if(count($varCategories)>0){
4613 $newCats = array();
4614 $originalCats = $this->mOutput->getCategories();
4615 foreach($originalCats as $cat => $sortkey){
4616 // make the replacement
4617 if( array_key_exists($cat,$varCategories) )
4618 $newCats[$varCategories[$cat]] = $sortkey;
4619 else $newCats[$cat] = $sortkey;
4620 }
4621 $this->mOutput->setCategoryLinks($newCats);
4622 }
4623 }
4624 }
4625
4626 # Construct search and replace arrays
4627 wfProfileIn( $fname.'-construct' );
4628 $replacePairs = array();
4629 foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
4630 $pdbk = $pdbks[$key];
4631 $searchkey = "<!--LINK $key-->";
4632 $title = $this->mLinkHolders['titles'][$key];
4633 if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] == 'new' ) {
4634 $linkCache->addBadLinkObj( $title );
4635 $colours[$pdbk] = 'new';
4636 $this->mOutput->addLink( $title, 0 );
4637 $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
4638 $this->mLinkHolders['texts'][$key],
4639 $this->mLinkHolders['queries'][$key] );
4640 } else {
4641 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
4642 $this->mLinkHolders['texts'][$key],
4643 $this->mLinkHolders['queries'][$key] );
4644 }
4645 }
4646 $replacer = new HashtableReplacer( $replacePairs, 1 );
4647 wfProfileOut( $fname.'-construct' );
4648
4649 # Do the thing
4650 wfProfileIn( $fname.'-replace' );
4651 $text = preg_replace_callback(
4652 '/(<!--LINK .*?-->)/',
4653 $replacer->cb(),
4654 $text);
4655
4656 wfProfileOut( $fname.'-replace' );
4657 }
4658
4659 # Now process interwiki link holders
4660 # This is quite a bit simpler than internal links
4661 if ( !empty( $this->mInterwikiLinkHolders['texts'] ) ) {
4662 wfProfileIn( $fname.'-interwiki' );
4663 # Make interwiki link HTML
4664 $replacePairs = array();
4665 foreach( $this->mInterwikiLinkHolders['texts'] as $key => $link ) {
4666 $title = $this->mInterwikiLinkHolders['titles'][$key];
4667 $replacePairs[$key] = $sk->makeLinkObj( $title, $link );
4668 }
4669 $replacer = new HashtableReplacer( $replacePairs, 1 );
4670
4671 $text = preg_replace_callback(
4672 '/<!--IWLINK (.*?)-->/',
4673 $replacer->cb(),
4674 $text );
4675 wfProfileOut( $fname.'-interwiki' );
4676 }
4677
4678 wfProfileOut( $fname );
4679 return $colours;
4680 }
4681
4682 /**
4683 * Replace <!--LINK--> link placeholders with plain text of links
4684 * (not HTML-formatted).
4685 * @param string $text
4686 * @return string
4687 */
4688 function replaceLinkHoldersText( $text ) {
4689 $fname = 'Parser::replaceLinkHoldersText';
4690 wfProfileIn( $fname );
4691
4692 $text = preg_replace_callback(
4693 '/<!--(LINK|IWLINK) (.*?)-->/',
4694 array( &$this, 'replaceLinkHoldersTextCallback' ),
4695 $text );
4696
4697 wfProfileOut( $fname );
4698 return $text;
4699 }
4700
4701 /**
4702 * @param array $matches
4703 * @return string
4704 * @private
4705 */
4706 function replaceLinkHoldersTextCallback( $matches ) {
4707 $type = $matches[1];
4708 $key = $matches[2];
4709 if( $type == 'LINK' ) {
4710 if( isset( $this->mLinkHolders['texts'][$key] ) ) {
4711 return $this->mLinkHolders['texts'][$key];
4712 }
4713 } elseif( $type == 'IWLINK' ) {
4714 if( isset( $this->mInterwikiLinkHolders['texts'][$key] ) ) {
4715 return $this->mInterwikiLinkHolders['texts'][$key];
4716 }
4717 }
4718 return $matches[0];
4719 }
4720
4721 /**
4722 * Tag hook handler for 'pre'.
4723 */
4724 function renderPreTag( $text, $attribs ) {
4725 // Backwards-compatibility hack
4726 $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
4727
4728 $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
4729 return wfOpenElement( 'pre', $attribs ) .
4730 Xml::escapeTagsOnly( $content ) .
4731 '</pre>';
4732 }
4733
4734 /**
4735 * Renders an image gallery from a text with one line per image.
4736 * text labels may be given by using |-style alternative text. E.g.
4737 * Image:one.jpg|The number "1"
4738 * Image:tree.jpg|A tree
4739 * given as text will return the HTML of a gallery with two images,
4740 * labeled 'The number "1"' and
4741 * 'A tree'.
4742 */
4743 function renderImageGallery( $text, $params ) {
4744 $ig = new ImageGallery();
4745 $ig->setContextTitle( $this->mTitle );
4746 $ig->setShowBytes( false );
4747 $ig->setShowFilename( false );
4748 $ig->setParser( $this );
4749 $ig->setHideBadImages();
4750 $ig->setAttributes( Sanitizer::validateTagAttributes( $params, 'table' ) );
4751 $ig->useSkin( $this->mOptions->getSkin() );
4752 $ig->mRevisionId = $this->mRevisionId;
4753
4754 if( isset( $params['caption'] ) ) {
4755 $caption = $params['caption'];
4756 $caption = htmlspecialchars( $caption );
4757 $caption = $this->replaceInternalLinks( $caption );
4758 $ig->setCaptionHtml( $caption );
4759 }
4760 if( isset( $params['perrow'] ) ) {
4761 $ig->setPerRow( $params['perrow'] );
4762 }
4763 if( isset( $params['widths'] ) ) {
4764 $ig->setWidths( $params['widths'] );
4765 }
4766 if( isset( $params['heights'] ) ) {
4767 $ig->setHeights( $params['heights'] );
4768 }
4769
4770 wfRunHooks( 'BeforeParserrenderImageGallery', array( &$this, &$ig ) );
4771
4772 $lines = explode( "\n", $text );
4773 foreach ( $lines as $line ) {
4774 # match lines like these:
4775 # Image:someimage.jpg|This is some image
4776 $matches = array();
4777 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
4778 # Skip empty lines
4779 if ( count( $matches ) == 0 ) {
4780 continue;
4781 }
4782 $tp = Title::newFromText( $matches[1] );
4783 $nt =& $tp;
4784 if( is_null( $nt ) ) {
4785 # Bogus title. Ignore these so we don't bomb out later.
4786 continue;
4787 }
4788 if ( isset( $matches[3] ) ) {
4789 $label = $matches[3];
4790 } else {
4791 $label = '';
4792 }
4793
4794 $html = $this->recursiveTagParse( trim( $label ) );
4795
4796 $ig->add( $nt, $html );
4797
4798 # Only add real images (bug #5586)
4799 if ( $nt->getNamespace() == NS_IMAGE ) {
4800 $this->mOutput->addImage( $nt->getDBkey() );
4801 }
4802 }
4803 return $ig->toHTML();
4804 }
4805
4806 function getImageParams( $handler ) {
4807 if ( $handler ) {
4808 $handlerClass = get_class( $handler );
4809 } else {
4810 $handlerClass = '';
4811 }
4812 if ( !isset( $this->mImageParams[$handlerClass] ) ) {
4813 // Initialise static lists
4814 static $internalParamNames = array(
4815 'horizAlign' => array( 'left', 'right', 'center', 'none' ),
4816 'vertAlign' => array( 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
4817 'bottom', 'text-bottom' ),
4818 'frame' => array( 'thumbnail', 'manualthumb', 'framed', 'frameless',
4819 'upright', 'border' ),
4820 );
4821 static $internalParamMap;
4822 if ( !$internalParamMap ) {
4823 $internalParamMap = array();
4824 foreach ( $internalParamNames as $type => $names ) {
4825 foreach ( $names as $name ) {
4826 $magicName = str_replace( '-', '_', "img_$name" );
4827 $internalParamMap[$magicName] = array( $type, $name );
4828 }
4829 }
4830 }
4831
4832 // Add handler params
4833 $paramMap = $internalParamMap;
4834 if ( $handler ) {
4835 $handlerParamMap = $handler->getParamMap();
4836 foreach ( $handlerParamMap as $magic => $paramName ) {
4837 $paramMap[$magic] = array( 'handler', $paramName );
4838 }
4839 }
4840 $this->mImageParams[$handlerClass] = $paramMap;
4841 $this->mImageParamsMagicArray[$handlerClass] = new MagicWordArray( array_keys( $paramMap ) );
4842 }
4843 return array( $this->mImageParams[$handlerClass], $this->mImageParamsMagicArray[$handlerClass] );
4844 }
4845
4846 /**
4847 * Parse image options text and use it to make an image
4848 */
4849 function makeImage( $title, $options ) {
4850 # @TODO: let the MediaHandler specify its transform parameters
4851 #
4852 # Check if the options text is of the form "options|alt text"
4853 # Options are:
4854 # * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
4855 # * left no resizing, just left align. label is used for alt= only
4856 # * right same, but right aligned
4857 # * none same, but not aligned
4858 # * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
4859 # * center center the image
4860 # * framed Keep original image size, no magnify-button.
4861 # * frameless like 'thumb' but without a frame. Keeps user preferences for width
4862 # * upright reduce width for upright images, rounded to full __0 px
4863 # * border draw a 1px border around the image
4864 # vertical-align values (no % or length right now):
4865 # * baseline
4866 # * sub
4867 # * super
4868 # * top
4869 # * text-top
4870 # * middle
4871 # * bottom
4872 # * text-bottom
4873
4874 $parts = array_map( 'trim', explode( '|', $options) );
4875 $sk = $this->mOptions->getSkin();
4876
4877 # Give extensions a chance to select the file revision for us
4878 $skip = $time = false;
4879 wfRunHooks( 'BeforeParserMakeImageLinkObj', array( &$this, &$title, &$skip, &$time ) );
4880
4881 if ( $skip ) {
4882 return $sk->makeLinkObj( $title );
4883 }
4884
4885 # Get parameter map
4886 $file = wfFindFile( $title, $time );
4887 $handler = $file ? $file->getHandler() : false;
4888
4889 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
4890
4891 # Process the input parameters
4892 $caption = '';
4893 $params = array( 'frame' => array(), 'handler' => array(),
4894 'horizAlign' => array(), 'vertAlign' => array() );
4895 foreach( $parts as $part ) {
4896 list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
4897 if ( isset( $paramMap[$magicName] ) ) {
4898 list( $type, $paramName ) = $paramMap[$magicName];
4899 $params[$type][$paramName] = $value;
4900
4901 // Special case; width and height come in one variable together
4902 if( $type == 'handler' && $paramName == 'width' ) {
4903 $m = array();
4904 if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $value, $m ) ) {
4905 $params[$type]['width'] = intval( $m[1] );
4906 $params[$type]['height'] = intval( $m[2] );
4907 } else {
4908 $params[$type]['width'] = intval( $value );
4909 }
4910 }
4911 } else {
4912 $caption = $part;
4913 }
4914 }
4915
4916 # Process alignment parameters
4917 if ( $params['horizAlign'] ) {
4918 $params['frame']['align'] = key( $params['horizAlign'] );
4919 }
4920 if ( $params['vertAlign'] ) {
4921 $params['frame']['valign'] = key( $params['vertAlign'] );
4922 }
4923
4924 # Validate the handler parameters
4925 if ( $handler ) {
4926 foreach ( $params['handler'] as $name => $value ) {
4927 if ( !$handler->validateParam( $name, $value ) ) {
4928 unset( $params['handler'][$name] );
4929 }
4930 }
4931 }
4932
4933 # Strip bad stuff out of the alt text
4934 $alt = $this->replaceLinkHoldersText( $caption );
4935
4936 # make sure there are no placeholders in thumbnail attributes
4937 # that are later expanded to html- so expand them now and
4938 # remove the tags
4939 $alt = $this->mStripState->unstripBoth( $alt );
4940 $alt = Sanitizer::stripAllTags( $alt );
4941
4942 $params['frame']['alt'] = $alt;
4943 $params['frame']['caption'] = $caption;
4944
4945 # Linker does the rest
4946 $ret = $sk->makeImageLink2( $title, $file, $params['frame'], $params['handler'] );
4947
4948 # Give the handler a chance to modify the parser object
4949 if ( $handler ) {
4950 $handler->parserTransformHook( $this, $file );
4951 }
4952
4953 return $ret;
4954 }
4955
4956 /**
4957 * Set a flag in the output object indicating that the content is dynamic and
4958 * shouldn't be cached.
4959 */
4960 function disableCache() {
4961 wfDebug( "Parser output marked as uncacheable.\n" );
4962 $this->mOutput->mCacheTime = -1;
4963 }
4964
4965 /**#@+
4966 * Callback from the Sanitizer for expanding items found in HTML attribute
4967 * values, so they can be safely tested and escaped.
4968 * @param string $text
4969 * @param PPFrame $frame
4970 * @return string
4971 * @private
4972 */
4973 function attributeStripCallback( &$text, $frame = false ) {
4974 $text = $this->replaceVariables( $text, $frame );
4975 $text = $this->mStripState->unstripBoth( $text );
4976 return $text;
4977 }
4978
4979 /**#@-*/
4980
4981 /**#@+
4982 * Accessor/mutator
4983 */
4984 function Title( $x = NULL ) { return wfSetVar( $this->mTitle, $x ); }
4985 function Options( $x = NULL ) { return wfSetVar( $this->mOptions, $x ); }
4986 function OutputType( $x = NULL ) { return wfSetVar( $this->mOutputType, $x ); }
4987 /**#@-*/
4988
4989 /**#@+
4990 * Accessor
4991 */
4992 function getTags() { return array_merge( array_keys($this->mTransparentTagHooks), array_keys( $this->mTagHooks ) ); }
4993 /**#@-*/
4994
4995
4996 /**
4997 * Break wikitext input into sections, and either pull or replace
4998 * some particular section's text.
4999 *
5000 * External callers should use the getSection and replaceSection methods.
5001 *
5002 * @param string $text Page wikitext
5003 * @param string $section A section identifier string of the form:
5004 * <flag1> - <flag2> - ... - <section number>
5005 *
5006 * Currently the only recognised flag is "T", which means the target section number
5007 * was derived during a template inclusion parse, in other words this is a template
5008 * section edit link. If no flags are given, it was an ordinary section edit link.
5009 * This flag is required to avoid a section numbering mismatch when a section is
5010 * enclosed by <includeonly> (bug 6563).
5011 *
5012 * The section number 0 pulls the text before the first heading; other numbers will
5013 * pull the given section along with its lower-level subsections. If the section is
5014 * not found, $mode=get will return $newtext, and $mode=replace will return $text.
5015 *
5016 * @param string $mode One of "get" or "replace"
5017 * @param string $newText Replacement text for section data.
5018 * @return string for "get", the extracted section text.
5019 * for "replace", the whole page with the section replaced.
5020 */
5021 private function extractSections( $text, $section, $mode, $newText='' ) {
5022 global $wgTitle;
5023 $this->clearState();
5024 $this->setTitle( $wgTitle ); // not generally used but removes an ugly failure mode
5025 $this->mOptions = new ParserOptions;
5026 $this->setOutputType( OT_WIKI );
5027 $curIndex = 0;
5028 $outText = '';
5029 $frame = new PPFrame( $this );
5030
5031 // Process section extraction flags
5032 $flags = 0;
5033 $sectionParts = explode( '-', $section );
5034 $sectionIndex = array_pop( $sectionParts );
5035 foreach ( $sectionParts as $part ) {
5036 if ( $part == 'T' ) {
5037 $flags |= self::PTD_FOR_INCLUSION;
5038 }
5039 }
5040 // Preprocess the text
5041 $dom = $this->preprocessToDom( $text, $flags );
5042 $root = $dom->documentElement;
5043
5044 // <h> nodes indicate section breaks
5045 // They can only occur at the top level, so we can find them by iterating the root's children
5046 $node = $root->firstChild;
5047
5048 // Find the target section
5049 if ( $sectionIndex == 0 ) {
5050 // Section zero doesn't nest, level=big
5051 $targetLevel = 1000;
5052 } else {
5053 while ( $node ) {
5054 if ( $node->nodeName == 'h' ) {
5055 if ( $curIndex + 1 == $sectionIndex ) {
5056 break;
5057 }
5058 $curIndex++;
5059 }
5060 if ( $mode == 'replace' ) {
5061 $outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG );
5062 }
5063 $node = $node->nextSibling;
5064 }
5065 if ( $node ) {
5066 $targetLevel = $node->getAttribute( 'level' );
5067 }
5068 }
5069
5070 if ( !$node ) {
5071 // Not found
5072 if ( $mode == 'get' ) {
5073 return $newText;
5074 } else {
5075 return $text;
5076 }
5077 }
5078
5079 // Find the end of the section, including nested sections
5080 do {
5081 if ( $node->nodeName == 'h' ) {
5082 $curIndex++;
5083 $curLevel = $node->getAttribute( 'level' );
5084 if ( $curIndex != $sectionIndex && $curLevel <= $targetLevel ) {
5085 break;
5086 }
5087 }
5088 if ( $mode == 'get' ) {
5089 $outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG );
5090 }
5091 $node = $node->nextSibling;
5092 } while ( $node );
5093
5094 // Write out the remainder (in replace mode only)
5095 if ( $mode == 'replace' ) {
5096 // Output the replacement text
5097 // Add two newlines on -- trailing whitespace in $newText is conventionally
5098 // stripped by the editor, so we need both newlines to restore the paragraph gap
5099 $outText .= $newText . "\n\n";
5100 while ( $node ) {
5101 $outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG );
5102 $node = $node->nextSibling;
5103 }
5104 }
5105
5106 if ( is_string( $outText ) ) {
5107 // Re-insert stripped tags
5108 $outText = trim( $this->mStripState->unstripBoth( $outText ) );
5109 }
5110
5111 return $outText;
5112 }
5113
5114 /**
5115 * This function returns the text of a section, specified by a number ($section).
5116 * A section is text under a heading like == Heading == or \<h1\>Heading\</h1\>, or
5117 * the first section before any such heading (section 0).
5118 *
5119 * If a section contains subsections, these are also returned.
5120 *
5121 * @param string $text text to look in
5122 * @param string $section section identifier
5123 * @param string $deftext default to return if section is not found
5124 * @return string text of the requested section
5125 */
5126 public function getSection( $text, $section, $deftext='' ) {
5127 return $this->extractSections( $text, $section, "get", $deftext );
5128 }
5129
5130 public function replaceSection( $oldtext, $section, $text ) {
5131 return $this->extractSections( $oldtext, $section, "replace", $text );
5132 }
5133
5134 /**
5135 * Get the timestamp associated with the current revision, adjusted for
5136 * the default server-local timestamp
5137 */
5138 function getRevisionTimestamp() {
5139 if ( is_null( $this->mRevisionTimestamp ) ) {
5140 wfProfileIn( __METHOD__ );
5141 global $wgContLang;
5142 $dbr = wfGetDB( DB_SLAVE );
5143 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
5144 array( 'rev_id' => $this->mRevisionId ), __METHOD__ );
5145
5146 // Normalize timestamp to internal MW format for timezone processing.
5147 // This has the added side-effect of replacing a null value with
5148 // the current time, which gives us more sensible behavior for
5149 // previews.
5150 $timestamp = wfTimestamp( TS_MW, $timestamp );
5151
5152 // The cryptic '' timezone parameter tells to use the site-default
5153 // timezone offset instead of the user settings.
5154 //
5155 // Since this value will be saved into the parser cache, served
5156 // to other users, and potentially even used inside links and such,
5157 // it needs to be consistent for all visitors.
5158 $this->mRevisionTimestamp = $wgContLang->userAdjust( $timestamp, '' );
5159
5160 wfProfileOut( __METHOD__ );
5161 }
5162 return $this->mRevisionTimestamp;
5163 }
5164
5165 /**
5166 * Mutator for $mDefaultSort
5167 *
5168 * @param $sort New value
5169 */
5170 public function setDefaultSort( $sort ) {
5171 $this->mDefaultSort = $sort;
5172 }
5173
5174 /**
5175 * Accessor for $mDefaultSort
5176 * Will use the title/prefixed title if none is set
5177 *
5178 * @return string
5179 */
5180 public function getDefaultSort() {
5181 if( $this->mDefaultSort !== false ) {
5182 return $this->mDefaultSort;
5183 } else {
5184 return $this->mTitle->getNamespace() == NS_CATEGORY
5185 ? $this->mTitle->getText()
5186 : $this->mTitle->getPrefixedText();
5187 }
5188 }
5189
5190 /**
5191 * Try to guess the section anchor name based on a wikitext fragment
5192 * presumably extracted from a heading, for example "Header" from
5193 * "== Header ==".
5194 */
5195 public function guessSectionNameFromWikiText( $text ) {
5196 # Strip out wikitext links(they break the anchor)
5197 $text = $this->stripSectionName( $text );
5198 $headline = Sanitizer::decodeCharReferences( $text );
5199 # strip out HTML
5200 $headline = StringUtils::delimiterReplace( '<', '>', '', $headline );
5201 $headline = trim( $headline );
5202 $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
5203 $replacearray = array(
5204 '%3A' => ':',
5205 '%' => '.'
5206 );
5207 return str_replace(
5208 array_keys( $replacearray ),
5209 array_values( $replacearray ),
5210 $sectionanchor );
5211 }
5212
5213 /**
5214 * Strips a text string of wikitext for use in a section anchor
5215 *
5216 * Accepts a text string and then removes all wikitext from the
5217 * string and leaves only the resultant text (i.e. the result of
5218 * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of
5219 * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended
5220 * to create valid section anchors by mimicing the output of the
5221 * parser when headings are parsed.
5222 *
5223 * @param $text string Text string to be stripped of wikitext
5224 * for use in a Section anchor
5225 * @return Filtered text string
5226 */
5227 public function stripSectionName( $text ) {
5228 # Strip internal link markup
5229 $text = preg_replace('/\[\[:?([^[|]+)\|([^[]+)\]\]/','$2',$text);
5230 $text = preg_replace('/\[\[:?([^[]+)\|?\]\]/','$1',$text);
5231
5232 # Strip external link markup (FIXME: Not Tolerant to blank link text
5233 # I.E. [http://www.mediawiki.org] will render as [1] or something depending
5234 # on how many empty links there are on the page - need to figure that out.
5235 $text = preg_replace('/\[(?:' . wfUrlProtocols() . ')([^ ]+?) ([^[]+)\]/','$2',$text);
5236
5237 # Parse wikitext quotes (italics & bold)
5238 $text = $this->doQuotes($text);
5239
5240 # Strip HTML tags
5241 $text = StringUtils::delimiterReplace( '<', '>', '', $text );
5242 return $text;
5243 }
5244
5245 /**
5246 * strip/replaceVariables/unstrip for preprocessor regression testing
5247 */
5248 function srvus( $text ) {
5249 $text = $this->replaceVariables( $text );
5250 $text = $this->mStripState->unstripBoth( $text );
5251 $text = Sanitizer::removeHTMLtags( $text );
5252 return $text;
5253 }
5254 }
5255
5256 /**
5257 * @todo document, briefly.
5258 * @addtogroup Parser
5259 */
5260 class StripState {
5261 var $general, $nowiki;
5262
5263 function __construct() {
5264 $this->general = new ReplacementArray;
5265 $this->nowiki = new ReplacementArray;
5266 }
5267
5268 function unstripGeneral( $text ) {
5269 wfProfileIn( __METHOD__ );
5270 do {
5271 $oldText = $text;
5272 $text = $this->general->replace( $text );
5273 } while ( $text != $oldText );
5274 wfProfileOut( __METHOD__ );
5275 return $text;
5276 }
5277
5278 function unstripNoWiki( $text ) {
5279 wfProfileIn( __METHOD__ );
5280 do {
5281 $oldText = $text;
5282 $text = $this->nowiki->replace( $text );
5283 } while ( $text != $oldText );
5284 wfProfileOut( __METHOD__ );
5285 return $text;
5286 }
5287
5288 function unstripBoth( $text ) {
5289 wfProfileIn( __METHOD__ );
5290 do {
5291 $oldText = $text;
5292 $text = $this->general->replace( $text );
5293 $text = $this->nowiki->replace( $text );
5294 } while ( $text != $oldText );
5295 wfProfileOut( __METHOD__ );
5296 return $text;
5297 }
5298 }
5299
5300 /**
5301 * @todo document, briefly.
5302 * @addtogroup Parser
5303 */
5304 class OnlyIncludeReplacer {
5305 var $output = '';
5306
5307 function replace( $matches ) {
5308 if ( substr( $matches[1], -1 ) == "\n" ) {
5309 $this->output .= substr( $matches[1], 0, -1 );
5310 } else {
5311 $this->output .= $matches[1];
5312 }
5313 }
5314 }
5315
5316 /**
5317 * An expansion frame, used as a context to expand the result of preprocessToDom()
5318 */
5319 class PPFrame {
5320 var $parser, $title;
5321 var $titleCache;
5322
5323 /**
5324 * Hashtable listing templates which are disallowed for expansion in this frame,
5325 * having been encountered previously in parent frames.
5326 */
5327 var $loopCheckHash;
5328
5329 /**
5330 * Recursion depth of this frame, top = 0
5331 */
5332 var $depth;
5333
5334 const NO_ARGS = 1;
5335 const NO_TEMPLATES = 2;
5336 const STRIP_COMMENTS = 4;
5337 const NO_IGNORE = 8;
5338
5339 const RECOVER_ORIG = 11;
5340
5341 /**
5342 * Construct a new preprocessor frame.
5343 * @param Parser $parser The parent parser
5344 * @param Title $title The context title, or false if there isn't one
5345 */
5346 function __construct( $parser ) {
5347 $this->parser = $parser;
5348 $this->title = $parser->mTitle;
5349 $this->titleCache = array( $this->title ? $this->title->getPrefixedDBkey() : false );
5350 $this->loopCheckHash = array();
5351 $this->depth = 0;
5352 }
5353
5354 /**
5355 * Create a new child frame
5356 * $args is optionally a DOMNodeList containing the template arguments
5357 */
5358 function newChild( $args = false, $title = false ) {
5359 $namedArgs = array();
5360 $numberedArgs = array();
5361 if ( $title === false ) {
5362 $title = $this->title;
5363 }
5364 if ( $args !== false ) {
5365 $xpath = false;
5366 foreach ( $args as $arg ) {
5367 if ( !$xpath ) {
5368 $xpath = new DOMXPath( $arg->ownerDocument );
5369 }
5370
5371 $nameNodes = $xpath->query( 'name', $arg );
5372 $value = $xpath->query( 'value', $arg );
5373 if ( $nameNodes->item( 0 )->hasAttributes() ) {
5374 // Numbered parameter
5375 $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent;
5376 $numberedArgs[$index] = $value->item( 0 );
5377 unset( $namedArgs[$index] );
5378 } else {
5379 // Named parameter
5380 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame::STRIP_COMMENTS ) );
5381 $namedArgs[$name] = $value->item( 0 );
5382 unset( $numberedArgs[$name] );
5383 }
5384 }
5385 }
5386 return new PPTemplateFrame( $this->parser, $this, $numberedArgs, $namedArgs, $title );
5387 }
5388
5389 /**
5390 * Expand a DOMNode describing a preprocessed document into plain wikitext,
5391 * using the current context
5392 * @param $root the node
5393 */
5394 function expand( $root, $flags = 0 ) {
5395 if ( is_string( $root ) ) {
5396 return $root;
5397 }
5398
5399 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->mMaxPPNodeCount )
5400 {
5401 return '<span class="error">Node-count limit exceeded</span>';
5402 }
5403
5404 if ( is_array( $root ) || $root instanceof DOMNodeList ) {
5405 $s = '';
5406 foreach ( $root as $node ) {
5407 $s .= $this->expand( $node, $flags );
5408 }
5409 } elseif ( $root instanceof DOMNode ) {
5410 if ( $root->nodeType == XML_TEXT_NODE ) {
5411 $s = $root->nodeValue;
5412 } elseif ( $root->nodeName == 'template' ) {
5413 # Double-brace expansion
5414 $xpath = new DOMXPath( $root->ownerDocument );
5415 $titles = $xpath->query( 'title', $root );
5416 $title = $titles->item( 0 );
5417 $parts = $xpath->query( 'part', $root );
5418 if ( $flags & self::NO_TEMPLATES ) {
5419 $s = '{{' . $this->implodeWithFlags( '|', $flags, $title, $parts ) . '}}';
5420 } else {
5421 $lineStart = $root->getAttribute( 'lineStart' );
5422 $params = array(
5423 'title' => $title,
5424 'parts' => $parts,
5425 'lineStart' => $lineStart,
5426 'text' => 'FIXME' );
5427 $s = $this->parser->braceSubstitution( $params, $this );
5428 }
5429 } elseif ( $root->nodeName == 'tplarg' ) {
5430 # Triple-brace expansion
5431 $xpath = new DOMXPath( $root->ownerDocument );
5432 $titles = $xpath->query( 'title', $root );
5433 $title = $titles->item( 0 );
5434 $parts = $xpath->query( 'part', $root );
5435 if ( $flags & self::NO_ARGS ) {
5436 $s = '{{{' . $this->implodeWithFlags( '|', $flags, $title, $parts ) . '}}}';
5437 } else {
5438 $params = array( 'title' => $title, 'parts' => $parts, 'text' => 'FIXME' );
5439 $s = $this->parser->argSubstitution( $params, $this );
5440 }
5441 } elseif ( $root->nodeName == 'comment' ) {
5442 # HTML-style comment
5443 if ( $this->parser->ot['html']
5444 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
5445 || ( $flags & self::STRIP_COMMENTS ) )
5446 {
5447 $s = '';
5448 } else {
5449 $s = $root->textContent;
5450 }
5451 } elseif ( $root->nodeName == 'ignore' ) {
5452 # Output suppression used by <includeonly> etc.
5453 # OT_WIKI will only respect <ignore> in substed templates.
5454 # The other output types respect it unless NO_IGNORE is set.
5455 # extractSections() sets NO_IGNORE and so never respects it.
5456 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & self::NO_IGNORE ) ) {
5457 $s = $root->textContent;
5458 } else {
5459 $s = '';
5460 }
5461 } elseif ( $root->nodeName == 'ext' ) {
5462 # Extension tag
5463 $xpath = new DOMXPath( $root->ownerDocument );
5464 $names = $xpath->query( 'name', $root );
5465 $attrs = $xpath->query( 'attr', $root );
5466 $inners = $xpath->query( 'inner', $root );
5467 $closes = $xpath->query( 'close', $root );
5468 $params = array(
5469 'name' => $names->item( 0 ),
5470 'attr' => $attrs->length > 0 ? $attrs->item( 0 ) : null,
5471 'inner' => $inners->length > 0 ? $inners->item( 0 ) : null,
5472 'close' => $closes->length > 0 ? $closes->item( 0 ) : null,
5473 );
5474 $s = $this->parser->extensionSubstitution( $params, $this );
5475 } elseif ( $root->nodeName == 'h' ) {
5476 # Heading
5477 $s = $this->expand( $root->childNodes, $flags );
5478
5479 if ( $this->parser->ot['html'] ) {
5480 # Insert heading index marker
5481 $headingIndex = $root->getAttribute( 'i' );
5482 $titleText = $this->title->getPrefixedDBkey();
5483 $this->parser->mHeadings[] = array( $titleText, $headingIndex );
5484 $serial = count( $this->parser->mHeadings ) - 1;
5485 $marker = "{$this->parser->mUniqPrefix}-h-$serial-{$this->parser->mMarkerSuffix}";
5486 $count = $root->getAttribute( 'level' );
5487 $s = substr( $s, 0, $count ) . $marker . substr( $s, $count );
5488 $this->parser->mStripState->general->setPair( $marker, '' );
5489 }
5490 } else {
5491 # Generic recursive expansion
5492 $s = '';
5493 for ( $node = $root->firstChild; $node; $node = $node->nextSibling ) {
5494 if ( $node->nodeType == XML_TEXT_NODE ) {
5495 $s .= $node->nodeValue;
5496 } elseif ( $node->nodeType == XML_ELEMENT_NODE ) {
5497 $s .= $this->expand( $node, $flags );
5498 }
5499 }
5500 }
5501 } else {
5502 throw new MWException( __METHOD__.': Invalid parameter type' );
5503 }
5504 return $s;
5505 }
5506
5507 function implodeWithFlags( $sep, $flags /*, ... */ ) {
5508 $args = array_slice( func_get_args(), 2 );
5509
5510 $first = true;
5511 $s = '';
5512 foreach ( $args as $root ) {
5513 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
5514 $root = array( $root );
5515 }
5516 foreach ( $root as $node ) {
5517 if ( $first ) {
5518 $first = false;
5519 } else {
5520 $s .= $sep;
5521 }
5522 $s .= $this->expand( $node, $flags );
5523 }
5524 }
5525 return $s;
5526 }
5527
5528 function implode( $sep /*, ... */ ) {
5529 $args = func_get_args();
5530 $args = array_merge( array_slice( $args, 0, 1 ), array( 0 ), array_slice( $args, 1 ) );
5531 return call_user_func_array( array( $this, 'implodeWithFlags' ), $args );
5532 }
5533
5534 /**
5535 * Split an <arg> or <template> node into a three-element array:
5536 * DOMNode name, string index and DOMNode value
5537 */
5538 function splitBraceNode( $node ) {
5539 $xpath = new DOMXPath( $node->ownerDocument );
5540 $names = $xpath->query( 'name', $node );
5541 $values = $xpath->query( 'value', $node );
5542 if ( !$names->length || !$values->length ) {
5543 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
5544 }
5545 $name = $names->item( 0 );
5546 $index = $name->getAttribute( 'index' );
5547 return array( $name, $index, $values->item( 0 ) );
5548 }
5549
5550 /**
5551 * Split an <ext> node into an associative array containing name, attr, inner and close
5552 * All values in the resulting array are DOMNodes. Inner and close are optional.
5553 */
5554 function splitExtNode( $node ) {
5555 $xpath = new DOMXPath( $node->ownerDocument );
5556 $names = $xpath->query( 'name', $node );
5557 $attrs = $xpath->query( 'attr', $node );
5558 $inners = $xpath->query( 'inner', $node );
5559 $closes = $xpath->query( 'close', $node );
5560 if ( !$names->length || !$attrs->length ) {
5561 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
5562 }
5563 $parts = array(
5564 'name' => $names->item( 0 ),
5565 'attr' => $attrs->item( 0 ) );
5566 if ( $inners->length ) {
5567 $parts['inner'] = $inners->item( 0 );
5568 }
5569 if ( $closes->length ) {
5570 $parts['close'] = $closes->item( 0 );
5571 }
5572 return $parts;
5573 }
5574
5575 function __toString() {
5576 return 'frame{}';
5577 }
5578
5579 function getPDBK( $level = false ) {
5580 if ( $level === false ) {
5581 return $this->title->getPrefixedDBkey();
5582 } else {
5583 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
5584 }
5585 }
5586
5587 /**
5588 * Returns true if there are no arguments in this frame
5589 */
5590 function isEmpty() {
5591 return true;
5592 }
5593
5594 function getArgument( $name ) {
5595 return false;
5596 }
5597
5598 /**
5599 * Returns true if the infinite loop check is OK, false if a loop is detected
5600 */
5601 function loopCheck( $title ) {
5602 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
5603 }
5604 }
5605
5606 /**
5607 * Expansion frame with template arguments
5608 */
5609 class PPTemplateFrame extends PPFrame {
5610 var $numberedArgs, $namedArgs, $parent;
5611 var $numberedExpansionCache, $namedExpansionCache;
5612
5613 function __construct( $parser, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
5614 $this->parser = $parser;
5615 $this->parent = $parent;
5616 $this->numberedArgs = $numberedArgs;
5617 $this->namedArgs = $namedArgs;
5618 $this->title = $title;
5619 $pdbk = $title ? $title->getPrefixedDBkey() : false;
5620 $this->titleCache = $parent->titleCache;
5621 $this->titleCache[] = $pdbk;
5622 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
5623 if ( $pdbk !== false ) {
5624 $this->loopCheckHash[$pdbk] = true;
5625 }
5626 $this->depth = $parent->depth + 1;
5627 $this->numberedExpansionCache = $this->namedExpansionCache = array();
5628 }
5629
5630 function __toString() {
5631 $s = 'tplframe{';
5632 $first = true;
5633 $args = $this->numberedArgs + $this->namedArgs;
5634 foreach ( $args as $name => $value ) {
5635 if ( $first ) {
5636 $first = false;
5637 } else {
5638 $s .= ', ';
5639 }
5640 $s .= "\"$name\":\"" .
5641 str_replace( '"', '\\"', $value->ownerDocument->saveXML( $value ) ) . '"';
5642 }
5643 $s .= '}';
5644 return $s;
5645 }
5646 /**
5647 * Returns true if there are no arguments in this frame
5648 */
5649 function isEmpty() {
5650 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
5651 }
5652
5653 function getNumberedArgument( $index ) {
5654 if ( !isset( $this->numberedArgs[$index] ) ) {
5655 return false;
5656 }
5657 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
5658 # No trimming for unnamed arguments
5659 $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], self::STRIP_COMMENTS );
5660 }
5661 return $this->numberedExpansionCache[$index];
5662 }
5663
5664 function getNamedArgument( $name ) {
5665 if ( !isset( $this->namedArgs[$name] ) ) {
5666 return false;
5667 }
5668 if ( !isset( $this->namedExpansionCache[$name] ) ) {
5669 # Trim named arguments post-expand, for backwards compatibility
5670 $this->namedExpansionCache[$name] = trim(
5671 $this->parent->expand( $this->namedArgs[$name], self::STRIP_COMMENTS ) );
5672 }
5673 return $this->namedExpansionCache[$name];
5674 }
5675
5676 function getArgument( $name ) {
5677 $text = $this->getNumberedArgument( $name );
5678 if ( $text === false ) {
5679 $text = $this->getNamedArgument( $name );
5680 }
5681 return $text;
5682 }
5683 }
5684
5685 /**
5686 * Stack class to help Parser::preprocessToDom()
5687 */
5688 class PPDStack {
5689 var $stack, $topAccum, $top;
5690
5691 function __construct() {
5692 $this->stack = array();
5693 $this->topAccum = '';
5694 $this->top = false;
5695 }
5696
5697 function &getAccum() {
5698 if ( count( $this->stack ) ) {
5699 return $this->top->getAccum();
5700 } else {
5701 return $this->topAccum;
5702 }
5703 }
5704
5705 function push( $data ) {
5706 if ( $data instanceof PPDStackElement ) {
5707 $this->stack[] = $data;
5708 } else {
5709 $this->stack[] = new PPDStackElement( $data );
5710 }
5711 $this->top =& $this->stack[ count( $this->stack ) - 1 ];
5712 }
5713
5714 function pop() {
5715 if ( !count( $this->stack ) ) {
5716 throw new MWException( __METHOD__.': no elements remaining' );
5717 }
5718 $temp = array_pop( $this->stack );
5719 if ( count( $this->stack ) ) {
5720 $this->top =& $this->stack[ count( $this->stack ) - 1 ];
5721 } else {
5722 $this->top = false;
5723 }
5724 }
5725
5726 function getFlags() {
5727 if ( !count( $this->stack ) ) {
5728 return array(
5729 'findEquals' => false,
5730 'findPipe' => false,
5731 'inHeading' => false,
5732 );
5733 } else {
5734 return $this->top->getFlags();
5735 }
5736 }
5737 }
5738
5739 class PPDStackElement {
5740 var $open, $close, $count, $parts, $eqpos, $lineStart;
5741
5742 function __construct( $data = array() ) {
5743 $this->parts = array( '' );
5744 $this->eqpos = array();
5745
5746 foreach ( $data as $name => $value ) {
5747 $this->$name = $value;
5748 }
5749 }
5750
5751 function &getAccum() {
5752 return $this->parts[count($this->parts) - 1];
5753 }
5754
5755 function addPart( $s = '' ) {
5756 $this->parts[] = $s;
5757 }
5758
5759 function getFlags() {
5760 $partCount = count( $this->parts );
5761 $findPipe = $this->open != "\n" && $this->open != '[';
5762 return array(
5763 'findPipe' => $findPipe,
5764 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->eqpos[$partCount - 1] ),
5765 'inHeading' => $this->open == "\n",
5766 );
5767 }
5768 }