5 * File for Parser and related classes
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).
18 * There are five main entry points into the Parser class:
20 * produces HTML output
22 * produces altered wiki markup.
24 * removes HTML comments and expands templates
26 * Cleans a signature before saving it to preferences
28 * Extracts sections from an article for section editing
31 * objects: $wgLang, $wgContLang
33 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
36 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
37 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
38 * $wgLocaltimezone, $wgAllowSpecialInclusion*,
41 * * only within ParserOptions
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.
53 const VERSION
= '1.6.4';
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;
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';
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;
76 // Flags for preprocessToDom
77 const PTD_FOR_INCLUSION
= 1;
83 var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables,
84 $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerSuffix,
85 $mExtLinkBracketedRegex;
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;
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
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;
125 * Do various kinds of initialisation on the first call of the parser
127 function firstCallInit() {
128 if ( !$this->mFirstCall
) {
131 $this->mFirstCall
= false;
133 wfProfileIn( __METHOD__
);
134 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
136 $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
138 # Syntax for arguments (see self::setFunctionHook):
139 # "name for lookup in localized magic words array",
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
);
172 if ( $wgAllowDisplayTitle ) {
173 $this->setFunctionHook( 'displaytitle', array( 'CoreParserFunctions', 'displaytitle' ), SFH_NO_HASH
);
175 if ( $wgAllowSlowParserFunctions ) {
176 $this->setFunctionHook( 'pagesinnamespace', array( 'CoreParserFunctions', 'pagesinnamespace' ), SFH_NO_HASH
);
179 $this->initialiseVariables();
181 wfRunHooks( 'ParserFirstCallInit', array( &$this ) );
182 wfProfileOut( __METHOD__
);
190 function clearState() {
191 wfProfileIn( __METHOD__
);
192 if ( $this->mFirstCall
) {
193 $this->firstCallInit();
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(
207 $this->mLinkHolders
= array(
208 'namespaces' => array(),
210 'queries' => array(),
214 $this->mRevisionTimestamp
= $this->mRevisionId
= null;
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
223 * Must not consist of all title characters, or else it will change
224 * the behaviour of <nowiki> in a link.
226 #$this->mUniqPrefix = "\x07UNIQ" . Parser::getRandomString();
227 $this->mUniqPrefix
= "\x7fUNIQ" . Parser
::getRandomString();
229 # Clear these on every parse, bug 4549
230 $this->mTplExpandCache
= $this->mTplRedirCache
= $this->mTplDomCache
= array();
232 $this->mShowToc
= true;
233 $this->mForceTocPosition
= false;
234 $this->mIncludeSizes
= array(
238 $this->mPPNodeCount
= 0;
239 $this->mDefaultSort
= false;
240 $this->mHeadings
= array();
242 wfRunHooks( 'ParserClearState', array( &$this ) );
243 wfProfileOut( __METHOD__
);
246 function setOutputType( $ot ) {
247 $this->mOutputType
= $ot;
250 'html' => $ot == OT_HTML
,
251 'wiki' => $ot == OT_WIKI
,
252 'pre' => $ot == OT_PREPROCESS
,
257 * Set the context title
259 function setTitle( $t ) {
260 if ( !$t ||
$t instanceof FakeTitle
) {
261 $t = Title
::newFromText( 'NO TITLE' );
263 if ( strval( $t->getFragment() ) !== '' ) {
264 # Strip the fragment to avoid various odd effects
265 $this->mTitle
= clone $t;
266 $this->mTitle
->setFragment( '' );
273 * Accessor for mUniqPrefix.
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.
285 //throw new MWException( "Accessing uninitialized mUniqPrefix" );
287 return $this->mUniqPrefix
;
291 * Convert wikitext to HTML
292 * Do not call this function recursively.
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
302 public function parse( $text, &$title, $options, $linestart = true, $clearState = true, $revid = null ) {
304 * First pass--just handle <nowiki> sections, pass the rest off
305 * to internalParse() which does all the real work.
308 global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang;
309 $fname = 'Parser::parse-' . wfGetCaller();
310 wfProfileIn( __METHOD__
);
311 wfProfileIn( $fname );
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;
325 $this->setOutputType( OT_HTML
);
326 wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState
) );
328 wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState
) );
329 $text = $this->internalParse( $text );
330 $text = $this->mStripState
->unstripGeneral( $text );
332 # Clean up special characters, only run once, next-to-last before doBlockLevels
334 # french spaces, last one Guillemet-left
335 # only if there is something before the space
336 '/(.) (?=\\?|:|;|!|%|\\302\\273)/' => '\\1 \\2',
337 # french spaces, Guillemet-right
338 '/(\\302\\253) /' => '\\1 ',
340 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
343 $text = $this->doBlockLevels( $text, $linestart );
345 $this->replaceLinkHolders( $text );
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 );
353 $text = $this->mStripState
->unstripNoWiki( $text );
355 wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
357 //!JF Move to its own function
359 $uniq_prefix = $this->mUniqPrefix
;
361 $elements = array_keys( $this->mTransparentTagHooks
);
362 $text = Parser
::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
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 ) );
373 $this->mStripState
->general
->setPair( $marker, $output );
375 $text = $this->mStripState
->unstripGeneral( $text );
377 $text = Sanitizer
::normalizeCharReferences( $text );
379 if (($wgUseTidy and $this->mOptions
->mTidy
) or $wgAlwaysUseTidy) {
380 $text = Parser
::tidy($text);
382 # attempt to sanitize at least some nesting problems
383 # (bug #2702 and quite a few others)
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<div\\5>\\6</div>\\8\\9',
398 # remove empty italic or bold tag pairs, some
399 # introduced by rules above
400 '/<([bi])><\/\\1>/' => '',
403 $text = preg_replace(
404 array_keys( $tidyregs ),
405 array_values( $tidyregs ),
409 wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
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();
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";
422 $this->mOutput
->setText( $text );
423 $this->mRevisionId
= $oldRevisionId;
424 $this->mRevisionTimestamp
= $oldRevisionTimestamp;
425 wfProfileOut( $fname );
426 wfProfileOut( __METHOD__
);
428 return $this->mOutput
;
432 * Recursive parser entry point that can be called from an extension tag
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__
);
445 * Expand templates and variables in the text, producing valid, static wikitext.
446 * Also removes comments.
448 function preprocess( $text, $title, $options, $revid = null ) {
449 wfProfileIn( __METHOD__
);
451 $this->setOutputType( OT_PREPROCESS
);
452 $this->mOptions
= $options;
453 $this->setTitle( $title );
454 if( $revid !== null ) {
455 $this->mRevisionId
= $revid;
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__
);
466 * Get a random string
471 function getRandomString() {
472 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
475 function &getTitle() { return $this->mTitle
; }
476 function getOptions() { return $this->mOptions
; }
478 function getFunctionLang() {
479 global $wgLang, $wgContLang;
480 return $this->mOptions
->getInterfaceMessage() ?
$wgLang : $wgContLang;
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
488 * 'UNIQ-xxxxx' => array(
491 * array( 'param' => 'x' ),
492 * '<element param="x">tag content</element>' ) )
494 * @param $elements list of element names. Comments are always extracted.
495 * @param $text Source text string.
496 * @param $uniq_prefix
501 function extractTagsAndParams($elements, $text, &$matches, $uniq_prefix = ''){
506 $taglist = implode( '|', $elements );
507 $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
509 while ( '' != $text ) {
510 $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE
);
512 if( count( $p ) < 5 ) {
515 if( count( $p ) > 5 ) {
529 $marker = "$uniq_prefix-$element-" . sprintf('%08X', $n++
) . $this->mMarkerSuffix
;
530 $stripped .= $marker;
532 if ( $close === '/>' ) {
533 // Empty element tag, <tag />
538 if( $element == '!--' ) {
541 $end = "/(<\\/$element\\s*>)/i";
543 $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE
);
545 if( count( $q ) < 3 ) {
546 # No end tag -- let it run out to the end of the text.
555 $matches[$marker] = array( $element,
557 Sanitizer
::decodeTagAttributes( $attributes ),
558 "<$element$attributes$close$content$tail" );
564 * Get a list of strippable XML-like elements
566 function getStripList() {
568 $elements = $this->mStripList
;
570 $elements[] = 'html';
572 if( $this->mOptions
->getUseTeX() ) {
573 $elements[] = 'math';
579 * @deprecated use replaceVariables
581 function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
586 * Restores pre, math, and other extensions removed by strip()
588 * always call unstripNoWiki() after this one
590 * @deprecated use $this->mStripState->unstrip()
592 function unstrip( $text, $state ) {
593 return $state->unstripGeneral( $text );
597 * Always call this after unstrip() to preserve the order
600 * @deprecated use $this->mStripState->unstrip()
602 function unstripNoWiki( $text, $state ) {
603 return $state->unstripNoWiki( $text );
607 * @deprecated use $this->mStripState->unstripBoth()
609 function unstripForHTML( $text ) {
610 return $this->mStripState
->unstripBoth( $text );
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()
620 function insertStripItem( $text ) {
622 $rnd = "{$this->mUniqPrefix}-item-$n-{$this->mMarkerSuffix}";
624 $this->mStripState
->general
->setPair( $rnd, $text );
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.
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.
637 * @param string $text Hideous HTML input
638 * @return string Corrected HTML output
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 );
650 $correctedtext = Parser
::externalTidy( $wrappedtext );
652 if( is_null( $correctedtext ) ) {
653 wfDebug( "Tidy error detected!\n" );
654 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
656 return $correctedtext;
660 * Spawn an external HTML tidy process and get corrected markup back from it.
665 function externalTidy( $text ) {
666 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
667 $fname = 'Parser::externalTidy';
668 wfProfileIn( $fname );
673 $descriptorspec = array(
674 0 => array('pipe', 'r'),
675 1 => array('pipe', 'w'),
676 2 => array('file', wfGetNull(), 'a')
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);
688 while (!feof($pipes[1])) {
689 $cleansource .= fgets($pipes[1], 1024);
692 proc_close($process);
695 wfProfileOut( $fname );
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.
707 * Use the HTML tidy PECL extension to use the tidy library in-process,
708 * saving the overhead of spawning a new process.
710 * 'pear install tidy' should be able to compile the extension module.
715 function internalTidy( $text ) {
716 global $wgTidyConf, $IP, $wgDebugTidy;
717 $fname = 'Parser::internalTidy';
718 wfProfileIn( $fname );
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
728 $cleansource = tidy_get_output( $tidy );
730 if ( $wgDebugTidy && $tidy->getStatus() > 0 ) {
731 $cleansource .= "<!--\nTidy reports:\n" .
732 str_replace( '-->', '-->', $tidy->errorBuffer
) .
736 wfProfileOut( $fname );
741 * parse the wiki syntax used to render tables
745 function doTableStuff ( $text ) {
746 $fname = 'Parser::doTableStuff';
747 wfProfileIn( $fname );
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 )
758 $line = trim ( $line );
760 if( $line == '' ) { // empty line, go to next line
763 $first_character = $line{0};
766 if ( preg_match( '/^(:*)\{\|(.*)$/' , $line , $matches ) ) {
767 // First check if we are starting a new table
768 $indent_level = strlen( $matches[1] );
770 $attributes = $this->mStripState
->unstripBoth( $matches[2] );
771 $attributes = Sanitizer
::fixTagAttributes ( $attributes , 'table' );
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
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 );
787 if ( !array_pop ( $has_opened_tr ) ) {
788 $line = "<tr><td></td></tr>{$line}";
791 if ( array_pop ( $tr_history ) ) {
792 $line = "</tr>{$line}";
795 if ( array_pop ( $td_history ) ) {
796 $line = "</{$last_tag}>{$line}";
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 );
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 );
811 $last_tag = array_pop ( $last_tag_history );
812 array_pop ( $has_opened_tr );
813 array_push ( $has_opened_tr , true );
815 if ( array_pop ( $tr_history ) ) {
819 if ( array_pop ( $td_history ) ) {
820 $line = "</{$last_tag}>{$line}";
823 $lines[$key] = $line;
824 array_push ( $tr_history , false );
825 array_push ( $td_history , false );
826 array_push ( $last_tag_history , '' );
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 );
835 $line = substr ( $line , 1 );
837 if ( $first_character == '!' ) {
838 $line = str_replace ( '!!' , '||' , $line );
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 );
849 // Loop through each table cell
850 foreach ( $cells as $cell )
853 if ( $first_character != '+' )
855 $tr_after = array_pop ( $tr_attributes );
856 if ( !array_pop ( $tr_history ) ) {
857 $previous = "<tr{$tr_after}>\n";
859 array_push ( $tr_history , true );
860 array_push ( $tr_attributes , '' );
861 array_pop ( $has_opened_tr );
862 array_push ( $has_opened_tr , true );
865 $last_tag = array_pop ( $last_tag_history );
867 if ( array_pop ( $td_history ) ) {
868 $previous = "</{$last_tag}>{$previous}";
871 if ( $first_character == '|' ) {
873 } else if ( $first_character == '!' ) {
875 } else if ( $first_character == '+' ) {
876 $last_tag = 'caption';
881 array_push ( $last_tag_history , $last_tag );
883 // A cell could contain both parameters and data
884 $cell_data = explode ( '|' , $cell , 2 );
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]}";
893 $attributes = $this->mStripState
->unstripBoth( $cell_data[0] );
894 $attributes = Sanitizer
::fixTagAttributes( $attributes , $last_tag );
895 $cell = "{$previous}<{$last_tag}{$attributes}>{$cell_data[1]}";
898 $lines[$key] .= $cell;
899 array_push ( $td_history , true );
904 // Closing open td, tr && table
905 while ( count ( $td_history ) > 0 )
907 if ( array_pop ( $td_history ) ) {
910 if ( array_pop ( $tr_history ) ) {
913 if ( !array_pop ( $has_opened_tr ) ) {
914 $lines[] = "<tr><td></td></tr>" ;
917 $lines[] = '</table>' ;
920 $output = implode ( "\n" , $lines ) ;
922 // special case: don't return empty table
923 if( $output == "<table>\n<tr><td></td></tr>\n</table>" ) {
927 wfProfileOut( $fname );
933 * Helper function for parse() that transforms wiki markup into
934 * HTML. Only called for $mOutputType == OT_HTML.
938 function internalParse( $text ) {
940 $fname = 'Parser::internalParse';
941 wfProfileIn( $fname );
943 # Hook to suspend the parser in this state
944 if ( !wfRunHooks( 'ParserBeforeInternalParse', array( &$this, &$text, &$this->mStripState
) ) ) {
945 wfProfileOut( $fname );
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
) );
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
957 $text = $this->doTableStuff( $text );
959 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
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 );
968 $text = $this->doAllQuotes( $text );
969 $text = $this->replaceInternalLinks( $text );
970 $text = $this->replaceExternalLinks( $text );
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);
976 $text = $this->doMagicLinks( $text );
977 $text = $this->formatHeadings( $text, $isMain );
979 wfProfileOut( $fname );
984 * Replace special strings like "ISBN xxx" and "RFC xxx" with
985 * magic external links.
989 function doMagicLinks( $text ) {
990 wfProfileIn( __METHOD__
);
991 $text = preg_replace_callback(
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
1001 )!x', array( &$this, 'magicLinkCallback' ), $text );
1002 wfProfileOut( __METHOD__
);
1006 function magicLinkCallback( $m ) {
1007 if ( substr( $m[0], 0, 1 ) == '<' ) {
1010 } elseif ( substr( $m[0], 0, 4 ) == 'ISBN' ) {
1012 $num = strtr( $isbn, array(
1017 $titleObj = SpecialPage
::getTitleFor( 'Booksources' );
1018 $text = '<a href="' .
1019 $titleObj->escapeLocalUrl( "isbn=$num" ) .
1020 "\" class=\"internal\">ISBN $isbn</a>";
1022 if ( substr( $m[0], 0, 3 ) == 'RFC' ) {
1026 } elseif ( substr( $m[0], 0, 4 ) == 'PMID' ) {
1028 $urlmsg = 'pubmedurl';
1031 throw new MWException( __METHOD__
.': unrecognised match type "' .
1032 substr($m[0], 0, 20 ) . '"' );
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>";
1044 * Parse headers and return html
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 );
1056 wfProfileOut( $fname );
1061 * Replace single quotes with HTML markup
1063 * @return string the altered text
1065 function doAllQuotes( $text ) {
1066 $fname = 'Parser::doAllQuotes';
1067 wfProfileIn( $fname );
1069 $lines = explode( "\n", $text );
1070 foreach ( $lines as $line ) {
1071 $outtext .= $this->doQuotes ( $line ) . "\n";
1073 $outtext = substr($outtext, 0,-1);
1074 wfProfileOut( $fname );
1079 * Helper function for doAllQuotes()
1081 public function doQuotes( $text ) {
1082 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1083 if ( count( $arr ) == 1 )
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.
1093 foreach ( $arr as $r )
1095 if ( ( $i %
2 ) == 1 )
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 )
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 )
1108 $arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
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++
; }
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 ) )
1127 $firstsingleletterword = -1;
1128 $firstmultiletterword = -1;
1130 foreach ( $arr as $r )
1132 if ( ( $i %
2 == 1 ) and ( strlen( $r ) == 3 ) )
1134 $x1 = substr ($arr[$i-1], -1);
1135 $x2 = substr ($arr[$i-1], -2, 1);
1137 if ($firstspace == -1) $firstspace = $i;
1138 } else if ($x2 == ' ') {
1139 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
1141 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
1147 # If there is a single-letter word, use it!
1148 if ($firstsingleletterword > -1)
1150 $arr [ $firstsingleletterword ] = "''";
1151 $arr [ $firstsingleletterword-1 ] .= "'";
1153 # If not, but there's a multi-letter word, use that one.
1154 else if ($firstmultiletterword > -1)
1156 $arr [ $firstmultiletterword ] = "''";
1157 $arr [ $firstmultiletterword-1 ] .= "'";
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)
1164 $arr [ $firstspace ] = "''";
1165 $arr [ $firstspace-1 ] .= "'";
1169 # Now let's actually convert our apostrophic mush to HTML!
1174 foreach ($arr as $r)
1178 if ($state == 'both')
1185 if (strlen ($r) == 2)
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'; }
1198 else if (strlen ($r) == 3)
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'; }
1211 else if (strlen ($r) == 5)
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'; }
1229 # Now close all remaining tags. Notice that the order is important.
1230 if ($state == 'b' ||
$state == 'ib')
1232 if ($state == 'i' ||
$state == 'bi' ||
$state == 'ib')
1236 # There might be lonely ''''', so make sure we have a buffer
1237 if ($state == 'both' && $buffer)
1238 $output .= '<b><i>'.$buffer.'</i></b>';
1244 * Replace external links
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.
1251 function replaceExternalLinks( $text ) {
1253 $fname = 'Parser::replaceExternalLinks';
1254 wfProfileIn( $fname );
1256 $sk = $this->mOptions
->getSkin();
1258 $bits = preg_split( $this->mExtLinkBracketedRegex
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1260 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
1263 while ( $i<count( $bits ) ) {
1265 $protocol = $bits[$i++
];
1266 $text = $bits[$i++
];
1267 $trail = $bits[$i++
];
1269 # The characters '<' and '>' (which were escaped by
1270 # removeHTMLtags()) should not be included in
1271 # URLs, per RFC 2396.
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]);
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 ) {
1287 # Set linktype for CSS - if URL==text, link is essentially free
1288 $linktype = ($text == $url) ?
'free' : 'text';
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';
1297 # Otherwise just use the URL
1298 $text = htmlspecialchars( $url );
1302 # Have link text, e.g. [http://domain.tld/some.link text]s
1304 list( $dtrail, $trail ) = Linker
::splitTrail( $trail );
1307 $text = $wgContLang->markNoConversion($text);
1309 $url = Sanitizer
::cleanUrl( $url );
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 );
1315 # Use the encoded URL
1316 # This means that users can paste URLs directly into the text
1317 # Funny characters like ö 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;
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 );
1328 wfProfileOut( $fname );
1333 * Replace anything that looks like a URL with a link
1336 function replaceFreeExternalLinks( $text ) {
1338 $fname = 'Parser::replaceFreeExternalLinks';
1339 wfProfileIn( $fname );
1341 $bits = preg_split( '/(\b(?:' . wfUrlProtocols() . '))/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1342 $s = array_shift( $bits );
1345 $sk = $this->mOptions
->getSkin();
1347 while ( $i < count( $bits ) ){
1348 $protocol = $bits[$i++
];
1349 $remainder = $bits[$i++
];
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];
1357 # special case: handle urls as url args:
1358 # http://www.example.com/foo?=http://www.example.com/bar
1359 if(strlen($trail) == 0 &&
1361 preg_match('/^'. wfUrlProtocols() . '$/S', $bits[$i]) &&
1362 preg_match( '/^('.self
::EXT_LINK_URL_CLASS
.'+)(.*)$/s', $bits[$i +
1], $m ))
1365 $url .= $bits[$i] . $m[1]; # protocol, url as arg to previous link
1370 # The characters '<' and '>' (which were escaped by
1371 # removeHTMLtags()) should not be included in
1372 # URLs, per RFC 2396.
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]);
1379 # Move trailing punctuation to $trail
1381 # If there is no left bracket, then consider right brackets fair game too
1382 if ( strpos( $url, '(' ) === false ) {
1386 $numSepChars = strspn( strrev( $url ), $sep );
1387 if ( $numSepChars ) {
1388 $trail = substr( $url, -$numSepChars ) . $trail;
1389 $url = substr( $url, 0, -$numSepChars );
1392 $url = Sanitizer
::cleanUrl( $url );
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 );
1404 $s .= $text . $trail;
1406 $s .= $protocol . $remainder;
1409 wfProfileOut( $fname );
1414 * Replace unusual URL escape codes with their equivalent characters
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.
1423 static function replaceUnusualEscapes( $url ) {
1424 return preg_replace_callback( '/%[0-9A-Fa-f]{2}/',
1425 array( 'Parser', 'replaceUnusualEscapesCallback' ), $url );
1429 * Callback function used in replaceUnusualEscapes().
1430 * Replaces unusual URL escape codes with their equivalent character
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
1442 // Yes, leave it escaped
1448 * make an image if it's allowed, either through the global
1449 * option or through the exception
1452 function maybeMakeExternalImage( $url ) {
1453 $sk = $this->mOptions
->getSkin();
1454 $imagesfrom = $this->mOptions
->getAllowExternalImagesFrom();
1455 $imagesexception = !empty($imagesfrom);
1457 if ( $this->mOptions
->getAllowExternalImages()
1458 ||
( $imagesexception && strpos( $url, $imagesfrom ) === 0 ) ) {
1459 if ( preg_match( self
::EXT_IMAGE_REGEX
, $url ) ) {
1461 $text = $sk->makeExternalImage( htmlspecialchars( $url ) );
1468 * Process [[ ]] wikilinks
1472 function replaceInternalLinks( $s ) {
1474 static $fname = 'Parser::replaceInternalLinks' ;
1476 wfProfileIn( $fname );
1478 wfProfileIn( $fname.'-setup' );
1480 # the % is needed to support urlencoded titles as well
1481 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1483 $sk = $this->mOptions
->getSkin();
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 );
1491 # Match a link having the form [[namespace:link|alternate]]trail
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"; }
1498 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
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' );
1506 if( is_null( $this->mTitle
) ) {
1507 throw new MWException( __METHOD__
.": \$this->mTitle is null\n" );
1509 $nottalk = !$this->mTitle
->isTalkPage();
1511 if ( $useLinkPrefixExtension ) {
1513 if ( preg_match( $e2, $s, $m ) ) {
1514 $first_prefix = $m[2];
1516 $first_prefix = false;
1522 if($wgContLang->hasVariants()) {
1523 $selflink = $wgContLang->convertLinkToAllVariants($this->mTitle
->getPrefixedText());
1525 $selflink = array($this->mTitle
->getPrefixedText());
1527 $useSubpages = $this->areSubpagesAllowed();
1528 wfProfileOut( $fname.'-setup' );
1530 # Loop for each link
1531 for ($k = 0; isset( $a[$k] ); $k++
) {
1533 if ( $useLinkPrefixExtension ) {
1534 wfProfileIn( $fname.'-prefixhandling' );
1535 if ( preg_match( $e2, $s, $m ) ) {
1543 $prefix = $first_prefix;
1544 $first_prefix = false;
1546 wfProfileOut( $fname.'-prefixhandling' );
1549 $might_be_img = false;
1551 wfProfileIn( "$fname-e1" );
1552 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
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
1559 # Still some problems for cases where the ] is meant to be outside punctuation,
1560 # and no image is in sight. See bug 2095.
1563 substr( $m[3], 0, 1 ) === ']' &&
1564 strpos($text, '[') !== false
1567 $text .= ']'; # so that replaceExternalLinks($text) works later
1568 $m[3] = substr( $m[3], 1 );
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('<', '>'), urldecode($m[1]) );
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;
1579 if ( strpos( $m[1], '%' ) !== false ) {
1580 $m[1] = urldecode($m[1]);
1583 } else { # Invalid form; output directly
1584 $s .= $prefix . '[[' . $line ;
1585 wfProfileOut( "$fname-e1" );
1588 wfProfileOut( "$fname-e1" );
1589 wfProfileIn( "$fname-misc" );
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 ;
1599 # Make subpage if necessary
1600 if( $useSubpages ) {
1601 $link = $this->maybeDoSubpageLink( $m[1], $text );
1606 $noforce = (substr($m[1], 0, 1) != ':');
1608 # Strip off leading ':'
1609 $link = substr($link, 1);
1612 wfProfileOut( "$fname-misc" );
1613 wfProfileIn( "$fname-title" );
1614 $nt = Title
::newFromText( $this->mStripState
->unstripNoWiki($link) );
1616 $s .= $prefix . '[[' . $line;
1617 wfProfileOut( "$fname-title" );
1621 $ns = $nt->getNamespace();
1622 $iw = $nt->getInterWiki();
1623 wfProfileOut( "$fname-title" );
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
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
1637 $text .= "[[{$m[0]}]]{$m[1]}";
1640 } elseif ( count( $m ) == 2 ) {
1641 #if there's exactly one ]] that's fine, we'll keep looking
1642 $text .= "[[{$m[0]}]]{$m[1]}";
1644 #if $next_line is invalid too, we need look no further
1645 $text .= '[[' . $next_line;
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" );
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" );
1664 wfProfileOut( "$fname-might_be_img" );
1667 $wasblank = ( '' == $text );
1668 if( $wasblank ) $text = $link;
1670 # Link not escaped by : , create the various objects
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" );
1682 wfProfileOut( "$fname-interwiki" );
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);
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() );
1697 wfProfileOut( "$fname-image" );
1700 # We still need to record the image's presence on the page
1701 $this->mOutput
->addImage( $nt->getDBkey() );
1703 wfProfileOut( "$fname-image" );
1707 if ( $ns == NS_CATEGORY
) {
1708 wfProfileIn( "$fname-category" );
1709 $s = rtrim($s . "\n"); # bug 87
1712 $sortkey = $this->getDefaultSort();
1716 $sortkey = Sanitizer
::decodeCharReferences( $sortkey );
1717 $sortkey = str_replace( "\n", '', $sortkey );
1718 $sortkey = $wgContLang->convertCategoryKey( $sortkey );
1719 $this->mOutput
->addCategory( $nt->getDBkey(), $sortkey );
1722 * Strip the whitespace Category links produce, see bug 87
1723 * @todo We might want to use trim($tmp, "\n") here.
1725 $s .= trim($prefix . $trail, "\n") == '' ?
'': $prefix . $trail;
1727 wfProfileOut( "$fname-category" );
1732 # Self-link checking
1733 if( $nt->getFragment() === '' ) {
1734 if( in_array( $nt->getPrefixedText(), $selflink, true ) ) {
1735 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
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() );
1747 } elseif( $ns == NS_SPECIAL
) {
1748 if( SpecialPage
::exists( $nt->getDBkey() ) ) {
1749 $s .= $this->makeKnownLinkHolder( $nt, $text, '', $trail, $prefix );
1751 $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
1754 } elseif( $ns == NS_IMAGE
) {
1755 $img = wfFindFile( $nt );
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 );
1765 $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
1767 wfProfileOut( $fname );
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.
1778 function makeLinkHolder( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
1779 wfProfileIn( __METHOD__
);
1780 if ( ! is_object($nt) ) {
1782 $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
1784 # Separate the link trail from the rest of the link
1785 list( $inside, $trail ) = Linker
::splitTrail( $trail );
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}";
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;
1798 $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
1801 wfProfileOut( __METHOD__
);
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.
1813 * @param string $text
1814 * @param string $query
1815 * @param string $trail
1816 * @param string $prefix
1817 * @return string HTML-wikitext mix oh yuck
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;
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.
1830 * In particular this is important when using action=render, which causes
1831 * full URLs to be included.
1833 * Oh man I hate our multi-layer parser!
1835 * @param string more-or-less HTML
1836 * @return string less-or-more HTML with NOPARSE bits
1838 function armorLinks( $text ) {
1839 return preg_replace( '/\b(' . wfUrlProtocols() . ')/',
1840 "{$this->mUniqPrefix}NOPARSE$1", $text );
1844 * Return true if subpage links should be expanded on this page.
1847 function areSubpagesAllowed() {
1848 # Some namespaces don't allow subpages
1849 global $wgNamespacesWithSubpages;
1850 return !empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]);
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
1860 function maybeDoSubpageLink($target, &$text) {
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
1869 $fname = 'Parser::maybeDoSubpageLink';
1870 wfProfileIn( $fname );
1871 $ret = $target; # default return value is no change
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 );
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
1889 $trailingSlashes = preg_match_all( '%(/+)$%', $target, $m );
1890 if( $trailingSlashes ) {
1891 $noslash = $target = substr( $target, 1, -strlen($m[0][0]) );
1893 $noslash = substr( $target, 1 );
1896 $ret = $this->mTitle
->getPrefixedText(). '/' . trim($noslash) . $suffix;
1897 if( '' === $text ) {
1898 $text = $target . $suffix;
1899 } # this might be changed for ugliness reasons
1901 # check for .. subpage backlinks
1903 $nodotdot = $target;
1904 while( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1906 $nodotdot = substr( $nodotdot, 3 );
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;
1919 $nodotdot = trim( $nodotdot );
1920 if( $nodotdot != '' ) {
1921 $ret .= '/' . $nodotdot;
1929 wfProfileOut( $fname );
1934 * Used by doBlockLevels()
1937 /* private */ function closeParagraph() {
1939 if ( '' != $this->mLastSection
) {
1940 $result = '</' . $this->mLastSection
. ">\n";
1942 $this->mInPre
= false;
1943 $this->mLastSection
= '';
1946 # getCommon() returns the length of the longest common substring
1947 # of both arguments, starting at the beginning of both.
1949 /* private */ function getCommon( $st1, $st2 ) {
1950 $fl = strlen( $st1 );
1951 $shorter = strlen( $st2 );
1952 if ( $fl < $shorter ) { $shorter = $fl; }
1954 for ( $i = 0; $i < $shorter; ++
$i ) {
1955 if ( $st1{$i} != $st2{$i} ) { break; }
1959 # These next three functions open, continue, and close the list
1960 # element appropriate to the prefix character passed into them.
1962 /* private */ function openList( $char ) {
1963 $result = $this->closeParagraph();
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;
1972 else { $result = '<!-- ERR 1 -->'; }
1977 /* private */ function nextItem( $char ) {
1978 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1979 else if ( ':' == $char ||
';' == $char ) {
1981 if ( $this->mDTopen
) { $close = '</dt>'; }
1982 if ( ';' == $char ) {
1983 $this->mDTopen
= true;
1984 return $close . '<dt>';
1986 $this->mDTopen
= false;
1987 return $close . '<dd>';
1990 return '<!-- ERR 2 -->';
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>';
2001 $text = '</dd></dl>';
2004 else { return '<!-- ERR 3 -->'; }
2010 * Make lists from lines starting with ':', '*', '#', etc.
2013 * @return string the lists rendered as HTML
2015 function doBlockLevels( $text, $linestart ) {
2016 $fname = 'Parser::doBlockLevels';
2017 wfProfileIn( $fname );
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.
2023 $textLines = explode( "\n", $text );
2025 $lastPrefix = $output = '';
2026 $this->mDTopen
= $inBlockElem = false;
2028 $paragraphStack = false;
2030 if ( !$linestart ) {
2031 $output .= array_shift( $textLines );
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 );
2043 $pref2 = str_replace( ';', ':', $pref );
2044 $t = substr( $oLine, $prefixLength );
2045 $this->mInPre
= !empty($preOpenMatch);
2047 # Don't interpret any other prefixes in preformatted text
2049 $pref = $pref2 = '';
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;
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.
2065 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
2067 $output .= $term . $this->nextItem( ':' );
2070 } elseif( $prefixLength ||
$lastPrefixLength ) {
2071 # Either open or close a level...
2072 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
2073 $paragraphStack = false;
2075 while( $commonPrefixLength < $lastPrefixLength ) {
2076 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
2077 --$lastPrefixLength;
2079 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
2080 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
2082 while ( $prefixLength > $commonPrefixLength ) {
2083 $char = substr( $pref, $commonPrefixLength, 1 );
2084 $output .= $this->openList( $char );
2086 if ( ';' == $char ) {
2087 # FIXME: This is dupe of code above
2088 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
2090 $output .= $term . $this->nextItem( ':' );
2093 ++
$commonPrefixLength;
2095 $lastPrefix = $pref2;
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;
2112 if ( $closematch ) {
2113 $inBlockElem = false;
2115 $inBlockElem = true;
2117 } else if ( !$inBlockElem && !$this->mInPre
) {
2118 if ( ' ' == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
2120 if ($this->mLastSection
!= 'pre') {
2121 $paragraphStack = false;
2122 $output .= $this->closeParagraph().'<pre>';
2123 $this->mLastSection
= 'pre';
2125 $t = substr( $t, 1 );
2128 if ( '' == trim($t) ) {
2129 if ( $paragraphStack ) {
2130 $output .= $paragraphStack.'<br />';
2131 $paragraphStack = false;
2132 $this->mLastSection
= 'p';
2134 if ($this->mLastSection
!= 'p' ) {
2135 $output .= $this->closeParagraph();
2136 $this->mLastSection
= '';
2137 $paragraphStack = '<p>';
2139 $paragraphStack = '</p><p>';
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';
2154 wfProfileOut( "$fname-paragraph" );
2156 // somewhere above we forget to get out of pre block (bug 785)
2157 if($preCloseMatch && $this->mInPre
) {
2158 $this->mInPre
= false;
2160 if ($paragraphStack === false) {
2164 while ( $prefixLength ) {
2165 $output .= $this->closeList( $pref2{$prefixLength-1} );
2168 if ( '' != $this->mLastSection
) {
2169 $output .= '</' . $this->mLastSection
. '>';
2170 $this->mLastSection
= '';
2173 wfProfileOut( $fname );
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
2185 function findColonNoLinks($str, &$before, &$after) {
2186 $fname = 'Parser::findColonNoLinks';
2187 wfProfileIn( $fname );
2189 $pos = strpos( $str, ':' );
2190 if( $pos === false ) {
2192 wfProfileOut( $fname );
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 );
2205 // Ugly state machine to walk through avoiding tags.
2206 $state = self
::COLON_STATE_TEXT
;
2208 $len = strlen( $str );
2209 for( $i = 0; $i < $len; $i++
) {
2213 // (Using the number is a performance hack for common cases)
2214 case 0: // self::COLON_STATE_TEXT:
2217 // Could be either a <start> tag or an </end> tag
2218 $state = self
::COLON_STATE_TAGSTART
;
2223 $before = substr( $str, 0, $i );
2224 $after = substr( $str, $i +
1 );
2225 wfProfileOut( $fname );
2228 // Embedded in a tag; don't break it.
2231 // Skip ahead looking for something interesting
2232 $colon = strpos( $str, ':', $i );
2233 if( $colon === false ) {
2234 // Nothing else interesting
2235 wfProfileOut( $fname );
2238 $lt = strpos( $str, '<', $i );
2239 if( $stack === 0 ) {
2240 if( $lt === false ||
$colon < $lt ) {
2242 $before = substr( $str, 0, $colon );
2243 $after = substr( $str, $colon +
1 );
2244 wfProfileOut( $fname );
2248 if( $lt === false ) {
2249 // Nothing else interesting to find; abort!
2250 // We're nested, but there's no close tags left. Abort!
2253 // Skip ahead to next tag start
2255 $state = self
::COLON_STATE_TAGSTART
;
2258 case 1: // self::COLON_STATE_TAG:
2263 $state = self
::COLON_STATE_TEXT
;
2266 // Slash may be followed by >?
2267 $state = self
::COLON_STATE_TAGSLASH
;
2273 case 2: // self::COLON_STATE_TAGSTART:
2276 $state = self
::COLON_STATE_CLOSETAG
;
2279 $state = self
::COLON_STATE_COMMENT
;
2282 // Illegal early close? This shouldn't happen D:
2283 $state = self
::COLON_STATE_TEXT
;
2286 $state = self
::COLON_STATE_TAG
;
2289 case 3: // self::COLON_STATE_CLOSETAG:
2294 wfDebug( "Invalid input in $fname; too many close tags\n" );
2295 wfProfileOut( $fname );
2298 $state = self
::COLON_STATE_TEXT
;
2301 case self
::COLON_STATE_TAGSLASH
:
2303 // Yes, a self-closed tag <blah/>
2304 $state = self
::COLON_STATE_TEXT
;
2306 // Probably we're jumping the gun, and this is an attribute
2307 $state = self
::COLON_STATE_TAG
;
2310 case 5: // self::COLON_STATE_COMMENT:
2312 $state = self
::COLON_STATE_COMMENTDASH
;
2315 case self
::COLON_STATE_COMMENTDASH
:
2317 $state = self
::COLON_STATE_COMMENTDASHDASH
;
2319 $state = self
::COLON_STATE_COMMENT
;
2322 case self
::COLON_STATE_COMMENTDASHDASH
:
2324 $state = self
::COLON_STATE_TEXT
;
2326 $state = self
::COLON_STATE_COMMENT
;
2330 throw new MWException( "State machine error in $fname" );
2334 wfDebug( "Invalid input in $fname; not enough close tags (stack $stack, state $state)\n" );
2337 wfProfileOut( $fname );
2342 * Return value of a magic variable (like PAGENAME)
2346 function getVariableValue( $index ) {
2347 global $wgContLang, $wgSitename, $wgServer, $wgServerName, $wgScriptPath;
2350 * Some of these require message or data lookups and can be
2351 * expensive to check many times.
2353 static $varCache = array();
2354 if ( wfRunHooks( 'ParserGetVariableValueVarCache', array( &$this, &$varCache ) ) ) {
2355 if ( isset( $varCache[$index] ) ) {
2356 return $varCache[$index];
2361 wfRunHooks( 'ParserGetVariableValueTs', array( &$this, &$ts ) );
2364 global $wgLocaltimezone;
2365 if ( isset( $wgLocaltimezone ) ) {
2366 $oldtz = getenv( 'TZ' );
2367 putenv( 'TZ='.$wgLocaltimezone );
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 );
2383 wfRestoreWarnings();
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 ) );
2395 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'j', $ts ) );
2397 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'd', $ts ) );
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 );
2407 return $varCache[$index] = $wgContLang->formatNum( $localDay );
2409 return $varCache[$index] = $wgContLang->formatNum( $localDay2 );
2411 return wfEscapeWikiText( $this->mTitle
->getText() );
2413 return $this->mTitle
->getPartialURL();
2414 case 'fullpagename':
2415 return wfEscapeWikiText( $this->mTitle
->getPrefixedText() );
2416 case 'fullpagenamee':
2417 return $this->mTitle
->getPrefixedURL();
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() );
2433 case 'talkpagenamee':
2434 if( $this->mTitle
->canTalk() ) {
2435 $talkPage = $this->mTitle
->getTalkPage();
2436 return $talkPage->getPrefixedUrl();
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();
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
;
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();
2483 return str_replace('_',' ',$wgContLang->getNsText( $this->mTitle
->getNamespace() ) );
2485 return wfUrlencode( $wgContLang->getNsText( $this->mTitle
->getNamespace() ) );
2487 return $this->mTitle
->canTalk() ?
str_replace('_',' ',$this->mTitle
->getTalkNsText()) : '';
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 );
2497 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'Y', $ts ), true );
2499 return $varCache[$index] = $wgContLang->time( wfTimestamp( TS_MW
, $ts ), false, false );
2501 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'H', $ts ), true );
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 ) );
2507 return $varCache[$index] = $wgContLang->formatNum( gmdate( 'w', $ts ) );
2508 case 'localdayname':
2509 return $varCache[$index] = $wgContLang->getWeekdayName( $localDayOfWeek +
1 );
2511 return $varCache[$index] = $wgContLang->formatNum( $localYear, true );
2513 return $varCache[$index] = $wgContLang->time( $localTimestamp, false, false );
2515 return $varCache[$index] = $wgContLang->formatNum( $localHour, true );
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 );
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();
2545 return $wgServerName;
2547 return $wgScriptPath;
2548 case 'directionmark':
2549 return $wgContLang->getDirMark();
2550 case 'contentlanguage':
2551 global $wgContLanguageCode;
2552 return $wgContLanguageCode;
2555 if ( wfRunHooks( 'ParserGetVariableValueSwitch', array( &$this, &$varCache, &$index, &$ret ) ) )
2563 * initialise the magic variables (like CURRENTMONTHNAME)
2567 function initialiseVariables() {
2568 $fname = 'Parser::initialiseVariables';
2569 wfProfileIn( $fname );
2570 $variableIDs = MagicWord
::getVariableIDs();
2572 $this->mVariables
= new MagicWordArray( $variableIDs );
2573 wfProfileOut( $fname );
2577 * Preprocess some wikitext and return the document tree.
2578 * This is the ghost of replace_variables().
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.
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.
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().
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.
2598 function preprocessToDom ( $text, $flags = 0 ) {
2599 wfProfileIn( __METHOD__
);
2600 wfProfileIn( __METHOD__
.'-makexml' );
2614 'names' => array( 2 => null ),
2620 $forInclusion = $flags & self
::PTD_FOR_INCLUSION
;
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;
2632 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
2633 $ignoredElements = array( 'includeonly' );
2634 $xmlishElements[] = 'includeonly';
2636 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
2638 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
2639 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
2641 $stack = new PPDStack
;
2643 $searchBase = '[{<';
2644 $revText = strrev( $text ); // For fast reverse searches
2646 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start
2647 $accum =& $stack->getAccum(); # Current text accumulator
2649 $findEquals = false; # True to find equals signs in arguments
2650 $findPipe = false; # True to take notice of pipe characters
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
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>';
2666 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
2667 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) . '</ignore>';
2669 $findOnlyinclude = false;
2672 if ( $fakeLineStart ) {
2673 $found = 'line-start';
2676 # Find next opening brace, closing brace or pipe
2677 $search = $searchBase;
2678 if ( $stack->top
=== false ) {
2679 $currentClosing = '';
2681 $currentClosing = $stack->top
->close
;
2682 $search .= $currentClosing;
2687 if ( $findEquals ) {
2688 // First equals will be for the template
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.
2699 // Easier to just break some section edit links.
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;
2709 if ( $i >= strlen( $text ) ) {
2710 if ( $currentClosing == "\n" ) {
2711 // Do a past-the-end run to finish off the heading
2713 $found = 'line-end';
2719 $curChar = $text[$i];
2720 if ( $curChar == '|' ) {
2722 } elseif ( $curChar == '=' ) {
2724 } elseif ( $curChar == '<' ) {
2726 } elseif ( $curChar == "\n" ) {
2728 $found = 'line-end';
2730 $found = 'line-start';
2732 } elseif ( $curChar == $currentClosing ) {
2734 } elseif ( isset( $rules[$curChar] ) ) {
2736 $rule = $rules[$curChar];
2738 # Some versions of PHP have a strcspn which stops on null characters
2739 # Ignore and continue
2746 if ( $found == 'angle' ) {
2748 // Handle </onlyinclude>
2749 if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) {
2750 $findOnlyinclude = true;
2754 // Determine element name
2755 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
2756 // Element name missing or not listed
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.
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 );
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" )
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 );
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;
2801 // No line to eat, just take the comment itself
2807 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
2808 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
2812 $name = $matches[1];
2813 $attrStart = $i +
strlen( $name ) +
1;
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
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;
2834 if ( $text[$tagEndPos-1] == '/' ) {
2835 $attrEnd = $tagEndPos - 1;
2837 $i = $tagEndPos +
1;
2840 $attrEnd = $tagEndPos;
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>';
2847 // No end tag -- let it run out to the end of the text.
2848 $inner = substr( $text, $tagEndPos +
1 );
2849 $i = strlen( $text );
2853 // <includeonly> and <noinclude> just become <ignore> tags
2854 if ( in_array( $name, $ignoredElements ) ) {
2855 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
2861 if ( $attrEnd <= $attrStart ) {
2864 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
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>';
2873 $accum .= $close . '</ext>';
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;
2886 $count = strspn( $text, '=', $i, 6 );
2891 'parts' => array( str_repeat( '=', $count ) ),
2893 'count' => $count );
2894 $stack->push( $piece );
2895 $accum =& $stack->getAccum();
2896 extract( $stack->getFlags() );
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
2908 $count = $piece->count
;
2909 if ( preg_match( "/\s*(=+)/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 // First find out how many equals signs there really are (don't stop at 6)
2914 $count = strlen( $m[1] );
2918 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
2921 $count = min( strlen( $m[1] ), $count );
2924 // Normal match, output <h>
2925 $element = "<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
2928 // Single equals sign on its own line, count=0
2932 // No match, no <h>, just pass down the inner text
2937 $accum =& $stack->getAccum();
2938 extract( $stack->getFlags() );
2940 // Append the result to the enclosing accumulator
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
2949 elseif ( $found == 'open' ) {
2950 # count opening brace characters
2951 $count = strspn( $text, $curChar, $i );
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
2958 'close' => $rule['end'],
2960 'parts' => array( '' ),
2962 'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
2965 $stack->push( $piece );
2966 $accum =& $stack->getAccum();
2967 extract( $stack->getFlags() );
2969 # Add literal brace(s)
2970 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
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 );
2981 # check for maximum matching characters (if there are 5 closing
2982 # characters, we will probably need only 3 - depending on the rules)
2984 $rule = $rules[$piece->open
];
2985 if ( $count > $rule['max'] ) {
2986 # The specified maximum exists in the callback array, unless the caller
2988 $matchingCount = $rule['max'];
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'] ) ) {
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 ) );
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 );
3013 # Create XML element
3014 # Note: $parts is already XML, does not need to be encoded further
3015 $parts = $piece->parts
;
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"';
3027 $element = "<$name$attr>";
3028 $element .= "<title>$title</title>";
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>";
3037 $element .= "<part><name index=\"$argIndex\" /><value>$part</value></part>";
3041 $element .= "</$name>";
3044 # Advance input pointer
3045 $i +
= $matchingCount;
3049 $accum =& $stack->getAccum();
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'];
3059 $enclosingAccum =& $accum;
3060 while ( $piece->count
) {
3061 if ( array_key_exists( $piece->count
, $names ) ) {
3062 $stack->push( $piece );
3063 $accum =& $stack->getAccum();
3069 $enclosingAccum .= str_repeat( $piece->open
, $skippedBraces );
3072 extract( $stack->getFlags() );
3074 # Add XML element to the enclosing accumulator
3078 elseif ( $found == 'pipe' ) {
3079 $findEquals = true; // shortcut for getFlags()
3080 $stack->top
->addPart();
3081 $accum =& $stack->getAccum();
3085 elseif ( $found == 'equals' ) {
3086 $findEquals = false; // shortcut for getFlags()
3087 $partsCount = count( $stack->top
->parts
);
3088 $stack->top
->eqpos
[$partsCount - 1] = strlen( $accum );
3094 # Output any remaining unclosed brackets
3095 foreach ( $stack->stack
as $piece ) {
3096 if ( $piece->open
== "\n" ) {
3097 $stack->topAccum
.= $piece->parts
[0];
3099 $stack->topAccum
.= str_repeat( $piece->open
, $piece->count
) . implode( '|', $piece->parts
);
3102 $stack->topAccum
.= '</root>';
3103 $xml = $stack->topAccum
;
3105 wfProfileOut( __METHOD__
.'-makexml' );
3106 wfProfileIn( __METHOD__
.'-loadXML' );
3107 $dom = new DOMDocument
;
3108 wfSuppressWarnings();
3109 $result = $dom->loadXML( $xml );
3110 wfRestoreWarnings();
3112 // Try running the XML through UtfNormal to get rid of invalid characters
3113 $xml = UtfNormal
::cleanUp( $xml );
3114 $result = $dom->loadXML( $xml );
3116 throw new MWException( __METHOD__
.' generated invalid XML' );
3119 wfProfileOut( __METHOD__
.'-loadXML' );
3120 wfProfileOut( __METHOD__
);
3125 * Return a three-element array: leading whitespace, string contents, trailing whitespace
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 );
3133 $w2 = substr( $ltrimmed, -$diff );
3137 return array( $w1, $trimmed, $w2 );
3141 * Replace magic variables, templates, and template arguments
3142 * with the appropriate text. Templates are substituted recursively,
3143 * taking care to avoid infinite loops.
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
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
3155 function replaceVariables( $text, $frame = false, $argsOnly = false ) {
3156 # Prevent too big inclusions
3157 if( strlen( $text ) > $this->mOptions
->getMaxIncludeSize() ) {
3161 $fname = __METHOD__
;
3162 wfProfileIn( $fname );
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' );
3170 $dom = $this->preprocessToDom( $text );
3171 $flags = $argsOnly ? PPFrame
::NO_TEMPLATES
: 0;
3172 $text = $frame->expand( $dom, $flags );
3174 wfProfileOut( $fname );
3178 /// Clean up argument array - refactored in 1.9 so parserfunctions can use it, too.
3179 static function createAssocArgs( $args ) {
3180 $assocArgs = array();
3182 foreach( $args as $arg ) {
3183 $eqpos = strpos( $arg, '=' );
3184 if ( $eqpos === false ) {
3185 $assocArgs[$index++
] = $arg;
3187 $name = trim( substr( $arg, 0, $eqpos ) );
3188 $value = trim( substr( $arg, $eqpos+
1 ) );
3189 if ( $value === false ) {
3192 if ( $name !== false ) {
3193 $assocArgs[$name] = $value;
3202 * Return the text of a template, after recursively
3203 * replacing any variables or templates within the template.
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
3213 function braceSubstitution( $piece, $frame ) {
3214 global $wgContLang, $wgLang, $wgAllowDisplayTitle, $wgNonincludableNamespaces;
3215 $fname = __METHOD__
;
3216 wfProfileIn( $fname );
3217 wfProfileIn( __METHOD__
.'-setup' );
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
3226 # Title object, where $text came from
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 );
3235 # Original title text preserved for various purposes
3236 $originalTitle = $part1;
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' );
3243 wfProfileIn( __METHOD__
.'-modifiers' );
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 ) . '}}';
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;
3267 # MSG, MSGNW and RAW
3270 $mwMsgnw =& MagicWord
::get( 'msgnw' );
3271 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
3274 # Remove obsolete MSG:
3275 $mwMsg =& MagicWord
::get( 'msg' );
3276 $mwMsg->matchStartAndRemove( $part1 );
3280 $mwRaw =& MagicWord
::get( 'raw' );
3281 if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
3282 $forceRawInterwiki = true;
3285 wfProfileOut( __METHOD__
.'-modifiers' );
3289 wfProfileIn( __METHOD__
. '-pfunc' );
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];
3298 # Case insensitive functions
3299 $function = strtolower( $function );
3300 if ( isset( $this->mFunctionSynonyms
[0][$function] ) ) {
3301 $function = $this->mFunctionSynonyms
[0][$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 ) {
3317 $allArgs[] = $funcArgs;
3319 # Convert arguments to plain text
3320 foreach ( $args as $arg ) {
3321 $funcArgs[] = trim( $frame->expand( $arg ) );
3323 $allArgs = array_merge( $initialArgs, $funcArgs );
3326 $result = call_user_func_array( $callback, $allArgs );
3329 if ( is_array( $result ) ) {
3330 if ( isset( $result[0] ) ) {
3332 unset( $result[0] );
3335 // Extract flags into the local scope
3336 // This allows callers to set flags such as nowiki, found, etc.
3343 wfProfileOut( __METHOD__
. '-pfunc' );
3346 # Finish mangling title and then check for loops.
3347 # Set $title to a Title object and $titleText to the PDBK
3350 # Split the title into page and subpage
3352 $part1 = $this->maybeDoSubpageLink( $part1, $subpage );
3353 if ($subpage !== '') {
3354 $ns = $this->mTitle
->getNamespace();
3356 $title = Title
::newFromText( $part1, $ns );
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);
3363 # Do infinite loop check
3364 if ( !$frame->loopCheck( $title ) ) {
3366 $text = "<span class=\"error\">Template loop detected: [[$titleText]]</span>";
3367 wfDebug( __METHOD__
.": template loop broken at '$titleText'\n" );
3369 # Do recursion depth check
3370 $limit = $this->mOptions
->getMaxTemplateDepth();
3371 if ( $frame->depth
>= $limit ) {
3373 $text = "<span class=\"error\">Template recursion depth limit exceeded ($limit)</span>";
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 ) ) {
3387 $this->disableCache();
3389 } else if ( $wgNonincludableNamespaces && in_array( $title->getNamespace(), $wgNonincludableNamespaces ) ) {
3390 $found = false; //access denied
3391 wfDebug( "$fname: template inclusion denied for " . $title->getPrefixedDBkey() );
3393 list( $text, $title ) = $this->getTemplateDom( $title );
3394 if ( $text !== false ) {
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]]";
3405 } elseif ( $title->isTrans() ) {
3406 // Interwiki transclusion
3407 if ( $this->ot
['html'] && !$forceRawInterwiki ) {
3408 $text = $this->interwikiTransclude( $title, 'render' );
3411 $text = $this->interwikiTransclude( $title, 'raw' );
3412 // Preprocess it like a template
3413 $text = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3418 wfProfileOut( __METHOD__
. '-loadtpl' );
3421 # If we haven't found text to substitute by now, we're done
3422 # Recover the source wikitext and return it
3424 $text = '{{' . $frame->implode( '|', $titleWithSpaces, $args ) . '}}';
3425 wfProfileOut( $fname );
3429 # Expand DOM-style return values in a child frame
3431 # Clean up argument array
3432 $newFrame = $frame->newChild( $args, $title );
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];
3439 $text = $newFrame->expand( $text );
3440 $this->mTplExpandCache
[$titleText] = $text;
3443 # Uncached expansion
3444 $text = $newFrame->expand( $text );
3448 # Replace raw HTML by a placeholder
3449 # Add a blank line preceding, to prevent it from mucking up
3450 # immediately preceding headings
3452 $text = "\n\n" . $this->insertStripItem( $text );
3454 # Escape nowiki-style return values
3455 elseif ( $nowiki && ( $this->ot
['html'] ||
$this->ot
['pre'] ) ) {
3456 $text = wfEscapeWikiText( $text );
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;
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 -->' );
3471 wfProfileOut( $fname );
3476 * Get the semi-parsed DOM representation of a template with a given title,
3477 * and its redirect destination title. Cached.
3479 function getTemplateDom( $title ) {
3480 $cacheTitle = $title;
3481 $titleText = $title->getPrefixedDBkey();
3483 if ( isset( $this->mTplRedirCache
[$titleText] ) ) {
3484 list( $ns, $dbk ) = $this->mTplRedirCache
[$titleText];
3485 $title = Title
::makeTitle( $ns, $dbk );
3486 $titleText = $title->getPrefixedDBkey();
3488 if ( isset( $this->mTplDomCache
[$titleText] ) ) {
3489 return array( $this->mTplDomCache
[$titleText], $title );
3492 // Cache miss, go to the database
3493 list( $text, $title ) = $this->fetchTemplateAndTitle( $title );
3495 if ( $text === false ) {
3496 $this->mTplDomCache
[$titleText] = false;
3497 return array( false, $title );
3500 $dom = $this->preprocessToDom( $text, self
::PTD_FOR_INCLUSION
);
3501 $this->mTplDomCache
[ $titleText ] = $dom;
3503 if (! $title->equals($cacheTitle)) {
3504 $this->mTplRedirCache
[$cacheTitle->getPrefixedDBkey()] =
3505 array( $title->getNamespace(),$cdb = $title->getDBkey() );
3508 return array( $dom, $title );
3512 * Fetch the unparsed text of a template and register a reference to it.
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'] );
3524 return array($text,$finalTitle);
3527 function fetchTemplate( $title ) {
3528 $rv = $this->fetchTemplateAndTitle($title);
3533 * Static function to get a template
3534 * Can be overridden via ParserOptions::setTemplateCallback().
3536 static function statelessFetchTemplate( $title ) {
3537 $text = $skip = false;
3538 $finalTitle = $title;
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 ) );
3551 'page_id' => $title->getArticleID(),
3555 $rev = $id ? Revision
::newFromId( $id ) : Revision
::newFromTitle( $title );
3556 $rev_id = $rev ?
$rev->getId() : 0;
3560 'page_id' => $title->getArticleID(),
3561 'rev_id' => $rev_id );
3564 $text = $rev->getText();
3565 } elseif( $title->getNamespace() == NS_MEDIAWIKI
) {
3567 $message = $wgLang->lcfirst( $title->getText() );
3568 $text = wfMsgForContentNoTrans( $message );
3569 if( wfEmptyMsg( $message, $text ) ) {
3576 if ( $text === false ) {
3580 $finalTitle = $title;
3581 $title = Title
::newFromRedirect( $text );
3585 'finalTitle' => $finalTitle,
3590 * Transclude an interwiki link.
3592 function interwikiTransclude( $title, $action ) {
3593 global $wgEnableScaryTranscluding;
3595 if (!$wgEnableScaryTranscluding)
3596 return wfMsg('scarytranscludedisabled');
3598 $url = $title->getFullUrl( "action=$action" );
3600 if (strlen($url) > 255)
3601 return wfMsg('scarytranscludetoolong');
3602 return $this->fetchScaryTemplateMaybeFromCache($url);
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));
3611 $time = $obj->tc_time
;
3612 $text = $obj->tc_contents
;
3613 if ($time && time() < $time +
$wgTranscludeCacheExpiry ) {
3618 $text = Http
::get($url);
3620 return wfMsg('scarytranscludefailed', $url);
3622 $dbw = wfGetDB(DB_MASTER
);
3623 $dbw->replace('transcache', array('tc_url'), array(
3625 'tc_time' => time(),
3626 'tc_contents' => $text));
3632 * Triple brace replacement -- used for template arguments
3635 function argSubstitution( $piece, $frame ) {
3636 wfProfileIn( __METHOD__
);
3639 $parts = $piece['parts'];
3640 $nameWithSpaces = $frame->expand( $piece['title'] );
3641 $argName = trim( $nameWithSpaces );
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 ) );
3648 if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) {
3649 $error = '<!-- WARNING: argument omitted, expansion size too large -->';
3652 if ( $text === false ) {
3654 $text = '{{{' . $frame->implode( '|', $nameWithSpaces, $parts ) . '}}}';
3656 if ( $error !== false ) {
3660 wfProfileOut( __METHOD__
);
3665 * Return the text to be used for a given extension tag.
3666 * This is the ghost of strip().
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
3676 function extensionSubstitution( $params, $frame ) {
3677 global $wgRawHtml, $wgContLang;
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'] );
3684 $marker = "{$this->mUniqPrefix}-$name-" . sprintf('%08X', $n++
) . $this->mMarkerSuffix
;
3686 if ( $this->ot
['html'] ) {
3687 $name = strtolower( $name );
3689 $attributes = Sanitizer
::decodeTagAttributes( $attrText );
3690 if ( isset( $params['attributes'] ) ) {
3691 $attributes = $attributes +
$params['attributes'];
3699 throw new MWException( '<html> extension tag encountered unexpectedly' );
3702 $output = Xml
::escapeTagsOnly( $content );
3705 $output = $wgContLang->armourMath(
3706 MathRenderer
::renderMath( $content, $attributes ) );
3709 $output = $this->renderImageGallery( $content, $attributes );
3712 if( isset( $this->mTagHooks
[$name] ) ) {
3713 $output = call_user_func_array( $this->mTagHooks
[$name],
3714 array( $content, $attributes, $this ) );
3716 throw new MWException( "Invalid call hook $name" );
3720 if ( is_null( $attrText ) ) {
3723 if ( isset( $params['attributes'] ) ) {
3724 foreach ( $params['attributes'] as $attrName => $attrValue ) {
3725 $attrText .= ' ' . htmlspecialchars( $attrName ) . '="' .
3726 htmlspecialchars( $attrValue ) . '"';
3729 if ( $content === null ) {
3730 $output = "<$name$attrText/>";
3732 $close = is_null( $params['close'] ) ?
'' : $frame->expand( $params['close'] );
3733 $output = "<$name$attrText>$content$close";
3737 if ( $name == 'html' ||
$name == 'nowiki' ) {
3738 $this->mStripState
->nowiki
->setPair( $marker, $output );
3740 $this->mStripState
->general
->setPair( $marker, $output );
3746 * Increment an include size counter
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
3752 function incrementIncludeSize( $type, $size ) {
3753 if ( $this->mIncludeSizes
[$type] +
$size > $this->mOptions
->getMaxIncludeSize( $type ) ) {
3756 $this->mIncludeSizes
[$type] +
= $size;
3762 * Detect __NOGALLERY__ magic word and set a placeholder
3764 function stripNoGallery( &$text ) {
3765 # if the string __NOGALLERY__ (not case-sensitive) occurs in the HTML,
3767 $mw = MagicWord
::get( 'nogallery' );
3768 $this->mOutput
->mNoGallery
= $mw->matchAndRemove( $text ) ;
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.
3777 * Any additional __TOC__ magic words left over will be discarded
3778 * as there can only be one TOC on the page.
3780 function stripToc( $text ) {
3781 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
3783 $mw = MagicWord
::get( 'notoc' );
3784 if( $mw->matchAndRemove( $text ) ) {
3785 $this->mShowToc
= false;
3788 $mw = MagicWord
::get( 'toc' );
3789 if( $mw->match( $text ) ) {
3790 $this->mShowToc
= true;
3791 $this->mForceTocPosition
= true;
3793 // Set a placeholder. At the end we'll fill it in with the TOC.
3794 $text = $mw->replace( '<!--MWTOC-->', $text, 1 );
3796 // Only keep the first one.
3797 $text = $mw->replace( '', $text );
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
3809 * It loops through all headlines, collects the necessary data, then splits up the
3810 * string and re-inserts the newly formatted headlines.
3812 * @param string $text
3813 * @param boolean $isMain
3816 function formatHeadings( $text, $isMain=true ) {
3817 global $wgMaxTocLevel, $wgContLang;
3819 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
3820 if( !$this->mTitle
->quickUserCan( 'edit' ) ) {
3823 $showEditLink = $this->mOptions
->getEditSection();
3826 # Inhibit editsection links if requested in the page
3827 $esw =& MagicWord
::get( 'noeditsection' );
3828 if( $esw->matchAndRemove( $text ) ) {
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
3835 $numMatches = preg_match_all( '/<H(?P<level>[1-6])(?P<attrib>.*?'.'>)(?P<header>.*?)<\/H[1-6] *>/i', $text, $matches );
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
);
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 );
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;
3856 # We need this to perform operations on the HTML
3857 $sk = $this->mOptions
->getSkin();
3863 # Ugh .. the TOC should have neat indentation levels which can be
3864 # passed to the skin functions. These are determined here
3868 $sublevelCount = array();
3869 $levelCount = array();
3875 $markerRegex = "{$this->mUniqPrefix}-h-(\d+)-{$this->mMarkerSuffix}";
3876 $baseTitleText = $this->mTitle
->getPrefixedDBkey();
3879 foreach( $matches[3] as $headline ) {
3880 $isTemplate = false;
3882 $sectionIndex = false;
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);
3893 $prevlevel = $level;
3894 $prevtoclevel = $toclevel;
3896 $level = $matches[1][$headlineCount];
3898 if( $doNumberHeadings ||
$enoughToc ) {
3900 if ( $level > $prevlevel ) {
3901 # Increase TOC level
3903 $sublevelCount[$toclevel] = 0;
3904 if( $toclevel<$wgMaxTocLevel ) {
3905 $prevtoclevel = $toclevel;
3906 $toc .= $sk->tocIndent();
3910 elseif ( $level < $prevlevel && $toclevel > 1 ) {
3911 # Decrease TOC level, find level to jump to
3913 if ( $toclevel == 2 && $level <= $levelCount[1] ) {
3914 # Can only go down to level 1
3917 for ($i = $toclevel; $i > 0; $i--) {
3918 if ( $levelCount[$i] == $level ) {
3919 # Found last matching level
3923 elseif ( $levelCount[$i] < $level ) {
3924 # Found first matching level below current level
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 );
3935 $toc .= $sk->tocLineEnd();
3940 # No change in level, end TOC line
3941 if( $toclevel<$wgMaxTocLevel ) {
3942 $toc .= $sk->tocLineEnd();
3946 $levelCount[$toclevel] = $level;
3948 # count number of headlines for each level
3949 @$sublevelCount[$toclevel]++
;
3951 for( $i = 1; $i <= $toclevel; $i++
) {
3952 if( !empty( $sublevelCount[$i] ) ) {
3956 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
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 );
3966 # Remove link placeholders by the link text.
3967 # <!--LINK number-->
3969 # link text with suffix
3970 $safeHeadline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
3971 "\$this->mLinkHolders['texts'][\$1]",
3973 $safeHeadline = preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
3974 "\$this->mInterwikiLinkHolders['texts'][\$1]",
3977 # Strip out HTML (other than plain <sup> and <sub>: bug 8393)
3978 $tocline = preg_replace(
3979 array( '#<(?!/?(sup|sub)).*?'.'>#', '#<(/?(sup|sub)).*?'.'>#' ),
3983 $tocline = trim( $tocline );
3985 # For the anchor, strip out HTML-y stuff period
3986 $safeHeadline = preg_replace( '/<.*?'.'>/', '', $safeHeadline );
3987 $safeHeadline = trim( $safeHeadline );
3989 # Save headline for section edit hint before it's escaped
3990 $headlineHint = $safeHeadline;
3991 $safeHeadline = Sanitizer
::escapeId( $safeHeadline );
3992 $refers[$headlineCount] = $safeHeadline;
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];
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;
4004 # Create the anchor for linking from the TOC to the section
4005 $anchor = $safeHeadline;
4006 if($refcount[$headlineCount] > 1 ) {
4007 $anchor .= '_' . $refcount[$headlineCount];
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 );
4013 # give headline the correct <h#> tag
4014 if( $showEditLink && $sectionIndex !== false ) {
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");
4020 $editlink = $sk->editSectionLink($this->mTitle
, $sectionIndex, $headlineHint);
4025 $head[$headlineCount] = $sk->makeHeadline( $level, $matches['attrib'][$headlineCount], $anchor, $headline, $editlink );
4030 $this->mOutput
->setSections( $tocraw );
4032 # Never ever show TOC if no headers
4033 if( $numVisible < 1 ) {
4038 if( $prevtoclevel > 0 && $prevtoclevel < $wgMaxTocLevel ) {
4039 $toc .= $sk->tocUnindent( $prevtoclevel - 1 );
4041 $toc = $sk->tocList( $toc );
4044 # split up and insert constructed headlines
4046 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
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
4054 # Disabled because it broke block formatting
4055 # For example, a bullet point in the top line
4056 # $full .= $sk->editSectionLink(0);
4059 if( $enoughToc && !$i && $isMain && !$this->mForceTocPosition
) {
4060 # Top anchor now in skin
4064 if( !empty( $head[$i] ) ) {
4069 if( $this->mForceTocPosition
) {
4070 return str_replace( '<!--MWTOC-->', $toc, $full );
4077 * Transform wiki markup when saving a page by doing \r\n -> \n
4078 * conversion, substitting signatures, {{subst:}} templates, etc.
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
4088 function preSaveTransform( $text, &$title, $user, $options, $clearState = true ) {
4089 $this->mOptions
= $options;
4090 $this->setTitle( $title );
4091 $this->setOutputType( OT_WIKI
);
4093 if ( $clearState ) {
4094 $this->clearState();
4100 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
4101 $text = $this->pstPass2( $text, $user );
4102 $text = $this->mStripState
->unstripBoth( $text );
4107 * Pre-save transform helper function
4110 function pstPass2( $text, $user ) {
4111 global $wgContLang, $wgLocaltimezone;
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.
4118 if ( isset( $wgLocaltimezone ) ) {
4119 $oldtz = getenv( 'TZ' );
4120 putenv( 'TZ='.$wgLocaltimezone );
4122 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) .
4123 ' (' . date( 'T' ) . ')';
4124 if ( isset( $wgLocaltimezone ) ) {
4125 putenv( 'TZ='.$oldtz );
4128 # Variable replacement
4129 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
4130 $text = $this->replaceVariables( $text );
4132 # Strip out <nowiki> etc. added via replaceVariables
4133 #$text = $this->strip( $text, $this->mStripState, false, array( 'gallery' ) );
4136 $sigText = $this->getUserSig( $user );
4137 $text = strtr( $text, array(
4139 '~~~~' => "$sigText $d",
4143 # Context links: [[|name]] and [[name (context)|]]
4145 global $wgLegalTitleChars;
4146 $tc = "[$wgLegalTitleChars]";
4147 $nc = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
4149 $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\))\\|]]/"; # [[ns:page (context)|]]
4150 $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\)|)(, $tc+|)\\|]]/"; # [[ns:page (context), context|]]
4151 $p2 = "/\[\[\\|($tc+)]]/"; # [[|page]]
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 );
4157 $t = $this->mTitle
->getText();
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 );
4164 # if there's no context, don't bother duplicating the title
4165 $text = preg_replace( $p2, '[[\\1]]', $text );
4168 # Trim trailing whitespace
4169 $text = rtrim( $text );
4175 * Fetch the user's signature text, if any, and normalize to
4176 * validated, ready-to-insert wikitext.
4182 function getUserSig( &$user ) {
4183 global $wgMaxSigChars;
4185 $username = $user->getName();
4186 $nickname = $user->getOption( 'nickname' );
4187 $nickname = $nickname === '' ?
$username : $nickname;
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 );
4198 # Failed to validate; fall back to the default
4199 $nickname = $username;
4200 wfDebug( "Parser::getUserSig: $username has bad XML tags in signature.\n" );
4204 // Make sure nickname doesnt get a sig in a sig
4205 $nickname = $this->cleanSigInSig( $nickname );
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 );
4213 return wfMsgExt( 'signature', array( 'content', 'parsemag' ), $userText, $nickText );
4218 * Check that the user's signature contains no bad XML
4220 * @param string $text
4221 * @return mixed An expanded string, or false if invalid.
4223 function validateSig( $text ) {
4224 return( wfIsWellFormedXmlFragment( $text ) ?
$text : false );
4228 * Clean up signature text
4230 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures @see cleanSigInSig
4231 * 2) Substitute all transclusions
4233 * @param string $text
4234 * @param $parsing Whether we're cleaning (preferences save) or parsing
4235 * @return string Signature text
4237 function cleanSig( $text, $parsing = false ) {
4240 $this->clearState();
4241 $this->setTitle( $wgTitle );
4242 $this->mOptions
= new ParserOptions
;
4243 $this->setOutputType
= OT_PREPROCESS
;
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 );
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
);
4259 $text = $this->mStripState
->unstripBoth( $text );
4266 * Strip ~~~, ~~~~ and ~~~~~ out of signatures
4267 * @param string $text
4268 * @return string Signature text with /~{3,5}/ removed
4270 function cleanSigInSig( $text ) {
4271 $text = preg_replace( '/~{3,5}/', '', $text );
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
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();
4290 * Wrapper for preprocess()
4292 * @param string $text the text to preprocess
4293 * @param ParserOptions $options options
4297 function transformMsg( $text, $options ) {
4299 static $executing = false;
4301 $fname = "Parser::transformMsg";
4303 # Guard against infinite recursion
4309 wfProfileIn($fname);
4310 $text = $this->preprocess( $text, $wgTitle, $options );
4313 wfProfileOut($fname);
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 ) { ... }
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
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
4330 * @return The old value of the mTagHooks array associated with the hook
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;
4341 function setTransparentTagHook( $tag, $callback ) {
4342 $tag = strtolower( $tag );
4343 $oldVal = isset( $this->mTransparentTagHooks
[$tag] ) ?
$this->mTransparentTagHooks
[$tag] : null;
4344 $this->mTransparentTagHooks
[$tag] = $callback;
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 ) { ... }
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
4359 * nowiki Wiki markup in the return value should be escaped
4360 * isHTML The returned text is HTML, armour it against wikitext transformation
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:...}}
4369 * @return The old callback function for this name, if any
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 );
4375 # Add to function cache
4376 $mw = MagicWord
::get( $id );
4378 throw new MWException( 'Parser::setFunctionHook() expecting a magic word identifier.' );
4380 $synonyms = $mw->getSynonyms();
4381 $sensitive = intval( $mw->isCaseSensitive() );
4383 foreach ( $synonyms as $syn ) {
4385 if ( !$sensitive ) {
4386 $syn = strtolower( $syn );
4389 if ( !( $flags & SFH_NO_HASH
) ) {
4392 # Remove trailing colon
4393 if ( substr( $syn, -1, 1 ) == ':' ) {
4394 $syn = substr( $syn, 0, -1 );
4396 $this->mFunctionSynonyms
[$sensitive][$syn] = $id;
4402 * Get all registered function hook identifiers
4406 function getFunctionHooks() {
4407 return array_keys( $this->mFunctionHooks
);
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
4416 function replaceLinkHolders( &$text, $options = 0 ) {
4420 $fname = 'Parser::replaceLinkHolders';
4421 wfProfileIn( $fname );
4425 $linkcolour_ids = array();
4426 $sk = $this->mOptions
->getSkin();
4427 $linkCache =& LinkCache
::singleton();
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');
4436 asort( $this->mLinkHolders
['namespaces'] );
4441 foreach ( $this->mLinkHolders
['namespaces'] as $key => $ns ) {
4443 $title = $this->mLinkHolders
['titles'][$key];
4445 # Skip invalid entries.
4446 # Result will be ugly, but prevents crash.
4447 if ( is_null( $title ) ) {
4450 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
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';
4463 # Not in the link cache, add it to the query
4464 if ( !isset( $current ) ) {
4466 $query = "SELECT page_id, page_namespace, page_title";
4467 if ( $threshold > 0 ) {
4468 $query .= ', page_len, page_is_redirect';
4470 $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
4471 } elseif ( $current != $ns ) {
4473 $query .= ")) OR (page_namespace=$ns AND page_title IN(";
4478 $query .= $dbr->addQuotes( $this->mLinkHolders
['dbkeys'][$key] );
4483 if ( $options & RLH_FOR_UPDATE
) {
4484 $query .= ' FOR UPDATE';
4487 $res = $dbr->query( $query, $fname );
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;
4500 //pass an array of page_ids to an extension
4501 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
4503 wfProfileOut( $fname.'-check' );
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
4512 $categories = $this->mOutput
->getCategoryLinks();
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 ) )
4520 $pdbk = $title->getPrefixedDBkey();
4521 $titleText = $title->getText();
4523 // generate all variants of the link title text
4524 $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
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;
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;
4553 if(!$linkBatch->isEmpty()){
4555 $titleClause = $linkBatch->constructSet('page', $dbr);
4557 $variantQuery = "SELECT page_id, page_namespace, page_title";
4558 if ( $threshold > 0 ) {
4559 $variantQuery .= ', page_len, page_is_redirect';
4562 $variantQuery .= " FROM $page WHERE $titleClause";
4563 if ( $options & RLH_FOR_UPDATE
) {
4564 $variantQuery .= ' FOR UPDATE';
4567 $varRes = $dbr->query( $variantQuery, $fname );
4569 // for each found variants, figure out link holders and replace
4570 while ( $s = $dbr->fetchObject($varRes) ) {
4572 $variantTitle = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
4573 $varPdbk = $variantTitle->getPrefixedDBkey();
4574 $vardbk = $variantTitle->getDBkey();
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
);
4583 // loop over link holders
4584 foreach($holderKeys as $key){
4585 $title = $this->mLinkHolders
['titles'][$key];
4586 if ( is_null( $title ) ) continue;
4588 $pdbk = $title->getPrefixedDBkey();
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();
4595 // set pdbk and colour
4596 $pdbks[$key] = $varPdbk;
4597 $colours[$varPdbk] = $sk->getLinkColour( $s, $threshold );
4598 $linkcolour_ids[$s->page_id
] = $pdbk;
4600 wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
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;
4611 // rebuild the categories in original order (if there are replacements)
4612 if(count($varCategories)>0){
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;
4621 $this->mOutput
->setCategoryLinks($newCats);
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] );
4641 $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
4642 $this->mLinkHolders
['texts'][$key],
4643 $this->mLinkHolders
['queries'][$key] );
4646 $replacer = new HashtableReplacer( $replacePairs, 1 );
4647 wfProfileOut( $fname.'-construct' );
4650 wfProfileIn( $fname.'-replace' );
4651 $text = preg_replace_callback(
4652 '/(<!--LINK .*?-->)/',
4656 wfProfileOut( $fname.'-replace' );
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 );
4669 $replacer = new HashtableReplacer( $replacePairs, 1 );
4671 $text = preg_replace_callback(
4672 '/<!--IWLINK (.*?)-->/',
4675 wfProfileOut( $fname.'-interwiki' );
4678 wfProfileOut( $fname );
4683 * Replace <!--LINK--> link placeholders with plain text of links
4684 * (not HTML-formatted).
4685 * @param string $text
4688 function replaceLinkHoldersText( $text ) {
4689 $fname = 'Parser::replaceLinkHoldersText';
4690 wfProfileIn( $fname );
4692 $text = preg_replace_callback(
4693 '/<!--(LINK|IWLINK) (.*?)-->/',
4694 array( &$this, 'replaceLinkHoldersTextCallback' ),
4697 wfProfileOut( $fname );
4702 * @param array $matches
4706 function replaceLinkHoldersTextCallback( $matches ) {
4707 $type = $matches[1];
4709 if( $type == 'LINK' ) {
4710 if( isset( $this->mLinkHolders
['texts'][$key] ) ) {
4711 return $this->mLinkHolders
['texts'][$key];
4713 } elseif( $type == 'IWLINK' ) {
4714 if( isset( $this->mInterwikiLinkHolders
['texts'][$key] ) ) {
4715 return $this->mInterwikiLinkHolders
['texts'][$key];
4722 * Tag hook handler for 'pre'.
4724 function renderPreTag( $text, $attribs ) {
4725 // Backwards-compatibility hack
4726 $content = StringUtils
::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
4728 $attribs = Sanitizer
::validateTagAttributes( $attribs, 'pre' );
4729 return wfOpenElement( 'pre', $attribs ) .
4730 Xml
::escapeTagsOnly( $content ) .
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
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
;
4754 if( isset( $params['caption'] ) ) {
4755 $caption = $params['caption'];
4756 $caption = htmlspecialchars( $caption );
4757 $caption = $this->replaceInternalLinks( $caption );
4758 $ig->setCaptionHtml( $caption );
4760 if( isset( $params['perrow'] ) ) {
4761 $ig->setPerRow( $params['perrow'] );
4763 if( isset( $params['widths'] ) ) {
4764 $ig->setWidths( $params['widths'] );
4766 if( isset( $params['heights'] ) ) {
4767 $ig->setHeights( $params['heights'] );
4770 wfRunHooks( 'BeforeParserrenderImageGallery', array( &$this, &$ig ) );
4772 $lines = explode( "\n", $text );
4773 foreach ( $lines as $line ) {
4774 # match lines like these:
4775 # Image:someimage.jpg|This is some image
4777 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
4779 if ( count( $matches ) == 0 ) {
4782 $tp = Title
::newFromText( $matches[1] );
4784 if( is_null( $nt ) ) {
4785 # Bogus title. Ignore these so we don't bomb out later.
4788 if ( isset( $matches[3] ) ) {
4789 $label = $matches[3];
4794 $html = $this->recursiveTagParse( trim( $label ) );
4796 $ig->add( $nt, $html );
4798 # Only add real images (bug #5586)
4799 if ( $nt->getNamespace() == NS_IMAGE
) {
4800 $this->mOutput
->addImage( $nt->getDBkey() );
4803 return $ig->toHTML();
4806 function getImageParams( $handler ) {
4808 $handlerClass = get_class( $handler );
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' ),
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 );
4832 // Add handler params
4833 $paramMap = $internalParamMap;
4835 $handlerParamMap = $handler->getParamMap();
4836 foreach ( $handlerParamMap as $magic => $paramName ) {
4837 $paramMap[$magic] = array( 'handler', $paramName );
4840 $this->mImageParams
[$handlerClass] = $paramMap;
4841 $this->mImageParamsMagicArray
[$handlerClass] = new MagicWordArray( array_keys( $paramMap ) );
4843 return array( $this->mImageParams
[$handlerClass], $this->mImageParamsMagicArray
[$handlerClass] );
4847 * Parse image options text and use it to make an image
4849 function makeImage( $title, $options ) {
4850 # @TODO: let the MediaHandler specify its transform parameters
4852 # Check if the options text is of the form "options|alt text"
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):
4874 $parts = array_map( 'trim', explode( '|', $options) );
4875 $sk = $this->mOptions
->getSkin();
4877 # Give extensions a chance to select the file revision for us
4878 $skip = $time = false;
4879 wfRunHooks( 'BeforeParserMakeImageLinkObj', array( &$this, &$title, &$skip, &$time ) );
4882 return $sk->makeLinkObj( $title );
4886 $file = wfFindFile( $title, $time );
4887 $handler = $file ?
$file->getHandler() : false;
4889 list( $paramMap, $mwArray ) = $this->getImageParams( $handler );
4891 # Process the input parameters
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;
4901 // Special case; width and height come in one variable together
4902 if( $type == 'handler' && $paramName == 'width' ) {
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] );
4908 $params[$type]['width'] = intval( $value );
4916 # Process alignment parameters
4917 if ( $params['horizAlign'] ) {
4918 $params['frame']['align'] = key( $params['horizAlign'] );
4920 if ( $params['vertAlign'] ) {
4921 $params['frame']['valign'] = key( $params['vertAlign'] );
4924 # Validate the handler parameters
4926 foreach ( $params['handler'] as $name => $value ) {
4927 if ( !$handler->validateParam( $name, $value ) ) {
4928 unset( $params['handler'][$name] );
4933 # Strip bad stuff out of the alt text
4934 $alt = $this->replaceLinkHoldersText( $caption );
4936 # make sure there are no placeholders in thumbnail attributes
4937 # that are later expanded to html- so expand them now and
4939 $alt = $this->mStripState
->unstripBoth( $alt );
4940 $alt = Sanitizer
::stripAllTags( $alt );
4942 $params['frame']['alt'] = $alt;
4943 $params['frame']['caption'] = $caption;
4945 # Linker does the rest
4946 $ret = $sk->makeImageLink2( $title, $file, $params['frame'], $params['handler'] );
4948 # Give the handler a chance to modify the parser object
4950 $handler->parserTransformHook( $this, $file );
4957 * Set a flag in the output object indicating that the content is dynamic and
4958 * shouldn't be cached.
4960 function disableCache() {
4961 wfDebug( "Parser output marked as uncacheable.\n" );
4962 $this->mOutput
->mCacheTime
= -1;
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
4973 function attributeStripCallback( &$text, $frame = false ) {
4974 $text = $this->replaceVariables( $text, $frame );
4975 $text = $this->mStripState
->unstripBoth( $text );
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 ); }
4992 function getTags() { return array_merge( array_keys($this->mTransparentTagHooks
), array_keys( $this->mTagHooks
) ); }
4997 * Break wikitext input into sections, and either pull or replace
4998 * some particular section's text.
5000 * External callers should use the getSection and replaceSection methods.
5002 * @param string $text Page wikitext
5003 * @param string $section A section identifier string of the form:
5004 * <flag1> - <flag2> - ... - <section number>
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).
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.
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.
5021 private function extractSections( $text, $section, $mode, $newText='' ) {
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
);
5029 $frame = new PPFrame( $this );
5031 // Process section extraction flags
5033 $sectionParts = explode( '-', $section );
5034 $sectionIndex = array_pop( $sectionParts );
5035 foreach ( $sectionParts as $part ) {
5036 if ( $part == 'T' ) {
5037 $flags |
= self
::PTD_FOR_INCLUSION
;
5040 // Preprocess the text
5041 $dom = $this->preprocessToDom( $text, $flags );
5042 $root = $dom->documentElement
;
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
;
5048 // Find the target section
5049 if ( $sectionIndex == 0 ) {
5050 // Section zero doesn't nest, level=big
5051 $targetLevel = 1000;
5054 if ( $node->nodeName
== 'h' ) {
5055 if ( $curIndex +
1 == $sectionIndex ) {
5060 if ( $mode == 'replace' ) {
5061 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5063 $node = $node->nextSibling
;
5066 $targetLevel = $node->getAttribute( 'level' );
5072 if ( $mode == 'get' ) {
5079 // Find the end of the section, including nested sections
5081 if ( $node->nodeName
== 'h' ) {
5083 $curLevel = $node->getAttribute( 'level' );
5084 if ( $curIndex != $sectionIndex && $curLevel <= $targetLevel ) {
5088 if ( $mode == 'get' ) {
5089 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5091 $node = $node->nextSibling
;
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";
5101 $outText .= $frame->expand( $node, PPFrame
::RECOVER_ORIG
);
5102 $node = $node->nextSibling
;
5106 if ( is_string( $outText ) ) {
5107 // Re-insert stripped tags
5108 $outText = trim( $this->mStripState
->unstripBoth( $outText ) );
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).
5119 * If a section contains subsections, these are also returned.
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
5126 public function getSection( $text, $section, $deftext='' ) {
5127 return $this->extractSections( $text, $section, "get", $deftext );
5130 public function replaceSection( $oldtext, $section, $text ) {
5131 return $this->extractSections( $oldtext, $section, "replace", $text );
5135 * Get the timestamp associated with the current revision, adjusted for
5136 * the default server-local timestamp
5138 function getRevisionTimestamp() {
5139 if ( is_null( $this->mRevisionTimestamp
) ) {
5140 wfProfileIn( __METHOD__
);
5142 $dbr = wfGetDB( DB_SLAVE
);
5143 $timestamp = $dbr->selectField( 'revision', 'rev_timestamp',
5144 array( 'rev_id' => $this->mRevisionId
), __METHOD__
);
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
5150 $timestamp = wfTimestamp( TS_MW
, $timestamp );
5152 // The cryptic '' timezone parameter tells to use the site-default
5153 // timezone offset instead of the user settings.
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, '' );
5160 wfProfileOut( __METHOD__
);
5162 return $this->mRevisionTimestamp
;
5166 * Mutator for $mDefaultSort
5168 * @param $sort New value
5170 public function setDefaultSort( $sort ) {
5171 $this->mDefaultSort
= $sort;
5175 * Accessor for $mDefaultSort
5176 * Will use the title/prefixed title if none is set
5180 public function getDefaultSort() {
5181 if( $this->mDefaultSort
!== false ) {
5182 return $this->mDefaultSort
;
5184 return $this->mTitle
->getNamespace() == NS_CATEGORY
5185 ?
$this->mTitle
->getText()
5186 : $this->mTitle
->getPrefixedText();
5191 * Try to guess the section anchor name based on a wikitext fragment
5192 * presumably extracted from a heading, for example "Header" from
5195 public function guessSectionNameFromWikiText( $text ) {
5196 # Strip out wikitext links(they break the anchor)
5197 $text = $this->stripSectionName( $text );
5198 $headline = Sanitizer
::decodeCharReferences( $text );
5200 $headline = StringUtils
::delimiterReplace( '<', '>', '', $headline );
5201 $headline = trim( $headline );
5202 $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
5203 $replacearray = array(
5208 array_keys( $replacearray ),
5209 array_values( $replacearray ),
5214 * Strips a text string of wikitext for use in a section anchor
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.
5223 * @param $text string Text string to be stripped of wikitext
5224 * for use in a Section anchor
5225 * @return Filtered text string
5227 public function stripSectionName( $text ) {
5228 # Strip internal link markup
5229 $text = preg_replace('/\[\[:?([^[|]+)\|([^[]+)\]\]/','$2',$text);
5230 $text = preg_replace('/\[\[:?([^[]+)\|?\]\]/','$1',$text);
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);
5237 # Parse wikitext quotes (italics & bold)
5238 $text = $this->doQuotes($text);
5241 $text = StringUtils
::delimiterReplace( '<', '>', '', $text );
5246 * strip/replaceVariables/unstrip for preprocessor regression testing
5248 function srvus( $text ) {
5249 $text = $this->replaceVariables( $text );
5250 $text = $this->mStripState
->unstripBoth( $text );
5251 $text = Sanitizer
::removeHTMLtags( $text );
5257 * @todo document, briefly.
5258 * @addtogroup Parser
5261 var $general, $nowiki;
5263 function __construct() {
5264 $this->general
= new ReplacementArray
;
5265 $this->nowiki
= new ReplacementArray
;
5268 function unstripGeneral( $text ) {
5269 wfProfileIn( __METHOD__
);
5272 $text = $this->general
->replace( $text );
5273 } while ( $text != $oldText );
5274 wfProfileOut( __METHOD__
);
5278 function unstripNoWiki( $text ) {
5279 wfProfileIn( __METHOD__
);
5282 $text = $this->nowiki
->replace( $text );
5283 } while ( $text != $oldText );
5284 wfProfileOut( __METHOD__
);
5288 function unstripBoth( $text ) {
5289 wfProfileIn( __METHOD__
);
5292 $text = $this->general
->replace( $text );
5293 $text = $this->nowiki
->replace( $text );
5294 } while ( $text != $oldText );
5295 wfProfileOut( __METHOD__
);
5301 * @todo document, briefly.
5302 * @addtogroup Parser
5304 class OnlyIncludeReplacer
{
5307 function replace( $matches ) {
5308 if ( substr( $matches[1], -1 ) == "\n" ) {
5309 $this->output
.= substr( $matches[1], 0, -1 );
5311 $this->output
.= $matches[1];
5317 * An expansion frame, used as a context to expand the result of preprocessToDom()
5320 var $parser, $title;
5324 * Hashtable listing templates which are disallowed for expansion in this frame,
5325 * having been encountered previously in parent frames.
5330 * Recursion depth of this frame, top = 0
5335 const NO_TEMPLATES
= 2;
5336 const STRIP_COMMENTS
= 4;
5337 const NO_IGNORE
= 8;
5339 const RECOVER_ORIG
= 11;
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
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();
5355 * Create a new child frame
5356 * $args is optionally a DOMNodeList containing the template arguments
5358 function newChild( $args = false, $title = false ) {
5359 $namedArgs = array();
5360 $numberedArgs = array();
5361 if ( $title === false ) {
5362 $title = $this->title
;
5364 if ( $args !== false ) {
5366 foreach ( $args as $arg ) {
5368 $xpath = new DOMXPath( $arg->ownerDocument
);
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] );
5380 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame
::STRIP_COMMENTS
) );
5381 $namedArgs[$name] = $value->item( 0 );
5382 unset( $numberedArgs[$name] );
5386 return new PPTemplateFrame( $this->parser
, $this, $numberedArgs, $namedArgs, $title );
5390 * Expand a DOMNode describing a preprocessed document into plain wikitext,
5391 * using the current context
5392 * @param $root the node
5394 function expand( $root, $flags = 0 ) {
5395 if ( is_string( $root ) ) {
5399 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->mMaxPPNodeCount
)
5401 return '<span class="error">Node-count limit exceeded</span>';
5404 if ( is_array( $root ) ||
$root instanceof DOMNodeList
) {
5406 foreach ( $root as $node ) {
5407 $s .= $this->expand( $node, $flags );
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 ) . '}}';
5421 $lineStart = $root->getAttribute( 'lineStart' );
5425 'lineStart' => $lineStart,
5426 'text' => 'FIXME' );
5427 $s = $this->parser
->braceSubstitution( $params, $this );
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 ) . '}}}';
5438 $params = array( 'title' => $title, 'parts' => $parts, 'text' => 'FIXME' );
5439 $s = $this->parser
->argSubstitution( $params, $this );
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
) )
5449 $s = $root->textContent
;
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
;
5461 } elseif ( $root->nodeName
== 'ext' ) {
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 );
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,
5474 $s = $this->parser
->extensionSubstitution( $params, $this );
5475 } elseif ( $root->nodeName
== 'h' ) {
5477 $s = $this->expand( $root->childNodes
, $flags );
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, '' );
5491 # Generic recursive expansion
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 );
5502 throw new MWException( __METHOD__
.': Invalid parameter type' );
5507 function implodeWithFlags( $sep, $flags /*, ... */ ) {
5508 $args = array_slice( func_get_args(), 2 );
5512 foreach ( $args as $root ) {
5513 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
5514 $root = array( $root );
5516 foreach ( $root as $node ) {
5522 $s .= $this->expand( $node, $flags );
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 );
5535 * Split an <arg> or <template> node into a three-element array:
5536 * DOMNode name, string index and DOMNode value
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__
);
5545 $name = $names->item( 0 );
5546 $index = $name->getAttribute( 'index' );
5547 return array( $name, $index, $values->item( 0 ) );
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.
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__
);
5564 'name' => $names->item( 0 ),
5565 'attr' => $attrs->item( 0 ) );
5566 if ( $inners->length
) {
5567 $parts['inner'] = $inners->item( 0 );
5569 if ( $closes->length
) {
5570 $parts['close'] = $closes->item( 0 );
5575 function __toString() {
5579 function getPDBK( $level = false ) {
5580 if ( $level === false ) {
5581 return $this->title
->getPrefixedDBkey();
5583 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
5588 * Returns true if there are no arguments in this frame
5590 function isEmpty() {
5594 function getArgument( $name ) {
5599 * Returns true if the infinite loop check is OK, false if a loop is detected
5601 function loopCheck( $title ) {
5602 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
5607 * Expansion frame with template arguments
5609 class PPTemplateFrame
extends PPFrame
{
5610 var $numberedArgs, $namedArgs, $parent;
5611 var $numberedExpansionCache, $namedExpansionCache;
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;
5626 $this->depth
= $parent->depth +
1;
5627 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
5630 function __toString() {
5633 $args = $this->numberedArgs +
$this->namedArgs
;
5634 foreach ( $args as $name => $value ) {
5640 $s .= "\"$name\":\"" .
5641 str_replace( '"', '\\"', $value->ownerDocument
->saveXML( $value ) ) . '"';
5647 * Returns true if there are no arguments in this frame
5649 function isEmpty() {
5650 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
5653 function getNumberedArgument( $index ) {
5654 if ( !isset( $this->numberedArgs
[$index] ) ) {
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
);
5661 return $this->numberedExpansionCache
[$index];
5664 function getNamedArgument( $name ) {
5665 if ( !isset( $this->namedArgs
[$name] ) ) {
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
) );
5673 return $this->namedExpansionCache
[$name];
5676 function getArgument( $name ) {
5677 $text = $this->getNumberedArgument( $name );
5678 if ( $text === false ) {
5679 $text = $this->getNamedArgument( $name );
5686 * Stack class to help Parser::preprocessToDom()
5689 var $stack, $topAccum, $top;
5691 function __construct() {
5692 $this->stack
= array();
5693 $this->topAccum
= '';
5697 function &getAccum() {
5698 if ( count( $this->stack
) ) {
5699 return $this->top
->getAccum();
5701 return $this->topAccum
;
5705 function push( $data ) {
5706 if ( $data instanceof PPDStackElement
) {
5707 $this->stack
[] = $data;
5709 $this->stack
[] = new PPDStackElement( $data );
5711 $this->top
=& $this->stack
[ count( $this->stack
) - 1 ];
5715 if ( !count( $this->stack
) ) {
5716 throw new MWException( __METHOD__
.': no elements remaining' );
5718 $temp = array_pop( $this->stack
);
5719 if ( count( $this->stack
) ) {
5720 $this->top
=& $this->stack
[ count( $this->stack
) - 1 ];
5726 function getFlags() {
5727 if ( !count( $this->stack
) ) {
5729 'findEquals' => false,
5730 'findPipe' => false,
5731 'inHeading' => false,
5734 return $this->top
->getFlags();
5739 class PPDStackElement
{
5740 var $open, $close, $count, $parts, $eqpos, $lineStart;
5742 function __construct( $data = array() ) {
5743 $this->parts
= array( '' );
5744 $this->eqpos
= array();
5746 foreach ( $data as $name => $value ) {
5747 $this->$name = $value;
5751 function &getAccum() {
5752 return $this->parts
[count($this->parts
) - 1];
5755 function addPart( $s = '' ) {
5756 $this->parts
[] = $s;
5759 function getFlags() {
5760 $partCount = count( $this->parts
);
5761 $findPipe = $this->open
!= "\n" && $this->open
!= '[';
5763 'findPipe' => $findPipe,
5764 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->eqpos
[$partCount - 1] ),
5765 'inHeading' => $this->open
== "\n",