887dccf00e26984034a7f3f8d5242626324d0856
4 * File for Parser and related classes
11 * Variable substitution O(N^2) attack
13 * Without countermeasures, it would be possible to attack the parser by saving
14 * a page filled with a large number of inclusions of large pages. The size of
15 * the generated page would be proportional to the square of the input size.
16 * Hence, we limit the number of inclusions of any given page, thus bringing any
17 * attack back to O(N).
19 define( 'MAX_INCLUDE_REPEAT', 100 );
20 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
22 # Allowed values for $mOutputType
23 define( 'OT_HTML', 1 );
24 define( 'OT_WIKI', 2 );
25 define( 'OT_MSG' , 3 );
27 # string parameter for extractTags which will cause it
28 # to strip HTML comments in addition to regular
29 # <XML>-style tags. This should not be anything we
30 # may want to use in wikisyntax
31 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
33 # prefix for escaping, used in two functions at least
34 define( 'UNIQ_PREFIX', 'NaodW29');
36 # Constants needed for external link processing
37 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
38 define( 'HTTP_PROTOCOLS', 'http|https' );
39 # Everything except bracket, space, or control characters
40 define( 'EXT_LINK_URL_CLASS', '[^]\\x00-\\x20\\x7F]' );
41 define( 'INVERSE_EXT_LINK_URL_CLASS', '[\]\\x00-\\x20\\x7F]' );
43 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
44 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
45 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
46 define( 'EXT_LINK_BRACKETED', '/\[(('.URL_PROTOCOLS
.'):'.EXT_LINK_URL_CLASS
.'+) *('.EXT_LINK_TEXT_CLASS
.'*?)\]/S' );
47 define( 'EXT_IMAGE_REGEX',
48 '/^('.HTTP_PROTOCOLS
.':)'. # Protocol
49 '('.EXT_LINK_URL_CLASS
.'+)\\/'. # Hostname and path
50 '('.EXT_IMAGE_FNAME_CLASS
.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS
.')$/S' # Filename
56 * Processes wiki markup
59 * There are three main entry points into the Parser class:
61 * produces HTML output
63 * produces altered wiki markup.
65 * performs brace substitution on MediaWiki messages
68 * objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
70 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
73 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
74 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
77 * * only within ParserOptions
90 # Cleared with clearState():
91 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
92 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
95 var $mOptions, $mTitle, $mOutputType,
96 $mTemplates, // cache of already loaded templates, avoids
97 // multiple SQL queries for the same string
98 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
99 // in this path. Used for loop detection.
109 $this->mTemplates
= array();
110 $this->mTemplatePath
= array();
111 $this->mTagHooks
= array();
120 function clearState() {
121 $this->mOutput
= new ParserOutput
;
122 $this->mAutonumber
= 0;
123 $this->mLastSection
= "";
124 $this->mDTopen
= false;
125 $this->mVariables
= false;
126 $this->mIncludeCount
= array();
127 $this->mStripState
= array();
128 $this->mArgStack
= array();
129 $this->mInPre
= false;
133 * First pass--just handle <nowiki> sections, pass the rest off
134 * to internalParse() which does all the real work.
137 * @return ParserOutput a ParserOutput
139 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
141 $fname = 'Parser::parse';
142 wfProfileIn( $fname );
148 $this->mOptions
= $options;
149 $this->mTitle
=& $title;
150 $this->mOutputType
= OT_HTML
;
153 $text = $this->strip( $text, $this->mStripState
);
154 $text = $this->internalParse( $text, $linestart );
155 $text = $this->unstrip( $text, $this->mStripState
);
156 # Clean up special characters, only run once, next-to-last before doBlockLevels
159 # french spaces, last one Guillemet-left
160 # only if there is something before the space
161 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
162 # french spaces, Guillemet-right
163 "/(\\302\\253) /i"=>"\\1 ",
164 '/<hr *>/i' => '<hr />',
165 '/<br *>/i' => '<br />',
166 '/<center *>/i' => '<div class="center">',
167 '/<\\/center *>/i' => '</div>',
168 # Clean up spare ampersands; note that we probably ought to be
169 # more careful about named entities.
170 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
172 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
175 # french spaces, last one Guillemet-left
176 '/ (\\?|:|;|!|\\302\\273)/i' => ' \\1',
177 # french spaces, Guillemet-right
178 '/(\\302\\253) /i' => '\\1 ',
179 '/<center *>/i' => '<div class="center">',
180 '/<\\/center *>/i' => '</div>'
182 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
185 $text = $this->doBlockLevels( $text, $linestart );
186 $text = $this->unstripNoWiki( $text, $this->mStripState
);
187 $this->mOutput
->setText( $text );
188 wfProfileOut( $fname );
189 return $this->mOutput
;
193 * Get a random string
198 function getRandomString() {
199 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
203 * Replaces all occurrences of <$tag>content</$tag> in the text
204 * with a random marker and returns the new text. the output parameter
205 * $content will be an associative array filled with data on the form
206 * $unique_marker => content.
208 * If $content is already set, the additional entries will be appended
209 * If $tag is set to STRIP_COMMENTS, the function will extract
210 * <!-- HTML comments -->
215 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
216 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
223 while ( '' != $text ) {
224 if($tag==STRIP_COMMENTS
) {
225 $p = preg_split( '/<!--/i', $text, 2 );
227 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
230 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
233 if($tag==STRIP_COMMENTS
) {
234 $q = preg_split( '/-->/i', $p[1], 2 );
236 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
238 $marker = $rnd . sprintf('%08X', $n++
);
239 $content[$marker] = $q[0];
240 $stripped .= $marker;
248 * Strips and renders nowiki, pre, math, hiero
249 * If $render is set, performs necessary rendering operations on plugins
250 * Returns the text, and fills an array with data needed in unstrip()
251 * If the $state is already a valid strip state, it adds to the state
253 * @param bool $stripcomments when set, HTML comments <!-- like this -->
254 * will be stripped in addition to other tags. This is important
255 * for section editing, where these comments cause confusion when
256 * counting the sections in the wikisource
260 function strip( $text, &$state, $stripcomments = false ) {
261 $render = ($this->mOutputType
== OT_HTML
);
262 $html_content = array();
263 $nowiki_content = array();
264 $math_content = array();
265 $pre_content = array();
266 $comment_content = array();
267 $ext_content = array();
269 # Replace any instances of the placeholders
270 $uniq_prefix = UNIQ_PREFIX
;
271 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
274 global $wgRawHtml, $wgWhitelistEdit;
275 if( $wgRawHtml && $wgWhitelistEdit ) {
276 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
277 foreach( $html_content as $marker => $content ) {
279 # Raw and unchecked for validity.
280 $html_content[$marker] = $content;
282 $html_content[$marker] = '<html>'.$content.'</html>';
288 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
289 foreach( $nowiki_content as $marker => $content ) {
291 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
293 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
298 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
299 foreach( $math_content as $marker => $content ){
301 if( $this->mOptions
->getUseTeX() ) {
302 $math_content[$marker] = renderMath( $content );
304 $math_content[$marker] = '<math>'.$content.'<math>';
307 $math_content[$marker] = '<math>'.$content.'</math>';
312 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
313 foreach( $pre_content as $marker => $content ){
315 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
317 $pre_content[$marker] = '<pre>'.$content.'</pre>';
323 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
324 foreach( $comment_content as $marker => $content ){
325 $comment_content[$marker] = '<!--'.$content.'-->';
330 foreach ( $this->mTagHooks
as $tag => $callback ) {
331 $ext_contents[$tag] = array();
332 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
333 foreach( $ext_content[$tag] as $marker => $content ) {
335 $ext_content[$tag][$marker] = $callback( $content );
337 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
342 # Merge state with the pre-existing state, if there is one
344 $state['html'] = $state['html'] +
$html_content;
345 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
346 $state['math'] = $state['math'] +
$math_content;
347 $state['pre'] = $state['pre'] +
$pre_content;
348 $state['comment'] = $state['comment'] +
$comment_content;
350 foreach( $ext_content as $tag => $array ) {
351 if ( array_key_exists( $tag, $state ) ) {
352 $state[$tag] = $state[$tag] +
$array;
357 'html' => $html_content,
358 'nowiki' => $nowiki_content,
359 'math' => $math_content,
360 'pre' => $pre_content,
361 'comment' => $comment_content,
368 * restores pre, math, and heiro removed by strip()
370 * always call unstripNoWiki() after this one
373 function unstrip( $text, &$state ) {
374 # Must expand in reverse order, otherwise nested tags will be corrupted
375 $contentDict = end( $state );
376 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
377 if( key($state) != 'nowiki' && key($state) != 'html') {
378 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
379 $text = str_replace( key( $contentDict ), $content, $text );
388 * always call this after unstrip() to preserve the order
392 function unstripNoWiki( $text, &$state ) {
393 # Must expand in reverse order, otherwise nested tags will be corrupted
394 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
395 $text = str_replace( key( $state['nowiki'] ), $content, $text );
400 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
401 $text = str_replace( key( $state['html'] ), $content, $text );
409 * Add an item to the strip state
410 * Returns the unique tag which must be inserted into the stripped text
411 * The tag will be replaced with the original text in unstrip()
415 function insertStripItem( $text, &$state ) {
416 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
425 $state['item'][$rnd] = $text;
430 * Return allowed HTML attributes
434 function getHTMLattrs () {
435 $htmlattrs = array( # Allowed attributes--no scripting, etc.
436 'title', 'align', 'lang', 'dir', 'width', 'height',
437 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
438 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
439 /* FONT */ 'type', 'start', 'value', 'compact',
440 /* For various lists, mostly deprecated but safe */
441 'summary', 'width', 'border', 'frame', 'rules',
442 'cellspacing', 'cellpadding', 'valign', 'char',
443 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
444 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
445 'id', 'class', 'name', 'style' /* For CSS */
451 * Remove non approved attributes and javascript in css
455 function fixTagAttributes ( $t ) {
456 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
457 $htmlattrs = $this->getHTMLattrs() ;
459 # Strip non-approved attributes from the tag
461 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
462 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
465 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
467 # Strip javascript "expression" from stylesheets. Brute force approach:
468 # If anythin offensive is found, all attributes of the HTML tag are dropped
471 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
472 wfMungeToUtf8( $t ) ) )
481 * interface with html tidy, used if $wgUseTidy = true
486 function tidy ( $text ) {
487 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
488 global $wgInputEncoding, $wgOutputEncoding;
489 $fname = 'Parser::tidy';
490 wfProfileIn( $fname );
494 switch(strtoupper($wgOutputEncoding)) {
496 $opts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
499 $opts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
505 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
506 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
507 '<head><title>test</title></head><body>'.$text.'</body></html>';
508 $descriptorspec = array(
509 0 => array('pipe', 'r'),
510 1 => array('pipe', 'w'),
511 2 => array('file', '/dev/null', 'a')
513 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
514 if (is_resource($process)) {
515 fwrite($pipes[0], $wrappedtext);
517 while (!feof($pipes[1])) {
518 $cleansource .= fgets($pipes[1], 1024);
521 $return_value = proc_close($process);
524 wfProfileOut( $fname );
526 if( $cleansource == '' && $text != '') {
527 wfDebug( "Tidy error detected!\n" );
528 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
535 * parse the wiki syntax used to render tables
539 function doTableStuff ( $t ) {
540 $fname = 'Parser::doTableStuff';
541 wfProfileIn( $fname );
543 $t = explode ( "\n" , $t ) ;
544 $td = array () ; # Is currently a td tag open?
545 $ltd = array () ; # Was it TD or TH?
546 $tr = array () ; # Is currently a tr tag open?
547 $ltr = array () ; # tr attributes
548 $indent_level = 0; # indent level of the table
549 foreach ( $t AS $k => $x )
552 $fc = substr ( $x , 0 , 1 ) ;
553 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
554 $indent_level = strlen( $matches[1] );
556 str_repeat( '<dl><dd>', $indent_level ) .
557 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
558 array_push ( $td , false ) ;
559 array_push ( $ltd , '' ) ;
560 array_push ( $tr , false ) ;
561 array_push ( $ltr , '' ) ;
563 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
564 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
566 $l = array_pop ( $ltd ) ;
567 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
568 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
570 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
572 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
573 $x = substr ( $x , 1 ) ;
574 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
576 $l = array_pop ( $ltd ) ;
577 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
578 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
581 array_push ( $tr , false ) ;
582 array_push ( $td , false ) ;
583 array_push ( $ltd , '' ) ;
584 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
586 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) { # Caption
587 if ( '|+' == substr ( $x , 0 , 2 ) ) {
589 $x = substr ( $x , 1 ) ;
591 $after = substr ( $x , 1 ) ;
592 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
593 $after = explode ( '||' , $after ) ;
595 foreach ( $after AS $theline )
600 $tra = array_pop ( $ltr ) ;
601 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
602 array_push ( $tr , true ) ;
603 array_push ( $ltr , '' ) ;
606 $l = array_pop ( $ltd ) ;
607 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
608 if ( $fc == '|' ) $l = 'td' ;
609 else if ( $fc == '!' ) $l = 'th' ;
610 else if ( $fc == '+' ) $l = 'caption' ;
612 array_push ( $ltd , $l ) ;
613 $y = explode ( '|' , $theline , 2 ) ;
614 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
615 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
617 array_push ( $td , true ) ;
622 # Closing open td, tr && table
623 while ( count ( $td ) > 0 )
625 if ( array_pop ( $td ) ) $t[] = '</td>' ;
626 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
630 $t = implode ( "\n" , $t ) ;
631 # $t = $this->removeHTMLtags( $t );
632 wfProfileOut( $fname );
637 * Helper function for parse() that transforms wiki markup into
638 * HTML. Only called for $mOutputType == OT_HTML.
642 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
645 $fname = 'Parser::internalParse';
646 wfProfileIn( $fname );
648 $text = $this->removeHTMLtags( $text );
649 $text = $this->replaceVariables( $text, $args );
651 $text = $wgContLang->convert($text);
653 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
655 $text = $this->doHeadings( $text );
656 if($this->mOptions
->getUseDynamicDates()) {
657 global $wgDateFormatter;
658 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
660 $text = $this->doAllQuotes( $text );
661 $text = $this->replaceInternalLinks ( $text );
662 # Another call to replace links and images inside captions of images
663 $text = $this->replaceInternalLinks ( $text );
664 $text = $this->replaceExternalLinks( $text );
665 $text = $this->doMagicLinks( $text );
666 $text = $this->doTableStuff( $text );
667 $text = $this->formatHeadings( $text, $isMain );
668 $sk =& $this->mOptions
->getSkin();
669 $text = $sk->transformContent( $text );
671 wfProfileOut( $fname );
676 * Replace special strings like "ISBN xxx" and "RFC xxx" with
677 * magic external links.
681 function &doMagicLinks( &$text ) {
682 global $wgUseGeoMode;
683 $text = $this->magicISBN( $text );
684 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
685 $text = $this->magicGEO( $text );
687 $text = $this->magicRFC( $text );
692 * Parse ^^ tokens and return html
696 function doExponent ( $text ) {
697 $fname = 'Parser::doExponent';
698 wfProfileIn( $fname);
699 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
700 wfProfileOut( $fname);
705 * Parse headers and return html
709 function doHeadings( $text ) {
710 $fname = 'Parser::doHeadings';
711 wfProfileIn( $fname );
712 for ( $i = 6; $i >= 1; --$i ) {
713 $h = substr( '======', 0, $i );
714 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
715 "<h{$i}>\\1</h{$i}>\\2", $text );
717 wfProfileOut( $fname );
722 * Replace single quotes with HTML markup
724 * @return string the altered text
726 function doAllQuotes( $text ) {
727 $fname = 'Parser::doAllQuotes';
728 wfProfileIn( $fname );
730 $lines = explode( "\n", $text );
731 foreach ( $lines as $line ) {
732 $outtext .= $this->doQuotes ( $line ) . "\n";
734 $outtext = substr($outtext, 0,-1);
735 wfProfileOut( $fname );
740 * Helper function for doAllQuotes()
743 function doQuotes( $text ) {
744 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
745 if (count ($arr) == 1)
749 # First, do some preliminary work. This may shift some apostrophes from
750 # being mark-up to being text. It also counts the number of occurrences
751 # of bold and italics mark-ups.
759 # If there are ever four apostrophes, assume the first is supposed to
760 # be text, and the remaining three constitute mark-up for bold text.
761 if (strlen ($arr[$i]) == 4)
766 # If there are more than 5 apostrophes in a row, assume they're all
767 # text except for the last 5.
768 else if (strlen ($arr[$i]) > 5)
770 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
773 # Count the number of occurrences of bold and italics mark-ups.
774 # We are not counting sequences of five apostrophes.
775 if (strlen ($arr[$i]) == 2) $numitalics++
; else
776 if (strlen ($arr[$i]) == 3) $numbold++
; else
777 if (strlen ($arr[$i]) == 5) { $numitalics++
; $numbold++
; }
782 # If there is an odd number of both bold and italics, it is likely
783 # that one of the bold ones was meant to be an apostrophe followed
784 # by italics. Which one we cannot know for certain, but it is more
785 # likely to be one that has a single-letter word before it.
786 if (($numbold %
2 == 1) && ($numitalics %
2 == 1))
789 $firstsingleletterword = -1;
790 $firstmultiletterword = -1;
794 if (($i %
2 == 1) and (strlen ($r) == 3))
796 $x1 = substr ($arr[$i-1], -1);
797 $x2 = substr ($arr[$i-1], -2, 1);
799 if ($firstspace == -1) $firstspace = $i;
800 } else if ($x2 == ' ') {
801 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
803 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
809 # If there is a single-letter word, use it!
810 if ($firstsingleletterword > -1)
812 $arr [ $firstsingleletterword ] = "''";
813 $arr [ $firstsingleletterword-1 ] .= "'";
815 # If not, but there's a multi-letter word, use that one.
816 else if ($firstmultiletterword > -1)
818 $arr [ $firstmultiletterword ] = "''";
819 $arr [ $firstmultiletterword-1 ] .= "'";
821 # ... otherwise use the first one that has neither.
822 # (notice that it is possible for all three to be -1 if, for example,
823 # there is only one pentuple-apostrophe in the line)
824 else if ($firstspace > -1)
826 $arr [ $firstspace ] = "''";
827 $arr [ $firstspace-1 ] .= "'";
831 # Now let's actually convert our apostrophic mush to HTML!
840 if ($state == 'both')
847 if (strlen ($r) == 2)
850 { $output .= '</i>'; $state = ''; }
851 else if ($state == 'bi')
852 { $output .= '</i>'; $state = 'b'; }
853 else if ($state == 'ib')
854 { $output .= '</b></i><b>'; $state = 'b'; }
855 else if ($state == 'both')
856 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
857 else # $state can be 'b' or ''
858 { $output .= '<i>'; $state .= 'i'; }
860 else if (strlen ($r) == 3)
863 { $output .= '</b>'; $state = ''; }
864 else if ($state == 'bi')
865 { $output .= '</i></b><i>'; $state = 'i'; }
866 else if ($state == 'ib')
867 { $output .= '</b>'; $state = 'i'; }
868 else if ($state == 'both')
869 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
870 else # $state can be 'i' or ''
871 { $output .= '<b>'; $state .= 'b'; }
873 else if (strlen ($r) == 5)
876 { $output .= '</b><i>'; $state = 'i'; }
877 else if ($state == 'i')
878 { $output .= '</i><b>'; $state = 'b'; }
879 else if ($state == 'bi')
880 { $output .= '</i></b>'; $state = ''; }
881 else if ($state == 'ib')
882 { $output .= '</b></i>'; $state = ''; }
883 else if ($state == 'both')
884 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
885 else # ($state == '')
886 { $buffer = ''; $state = 'both'; }
891 # Now close all remaining tags. Notice that the order is important.
892 if ($state == 'b' ||
$state == 'ib')
894 if ($state == 'i' ||
$state == 'bi' ||
$state == 'ib')
898 if ($state == 'both')
899 $output .= '<b><i>'.$buffer.'</i></b>';
905 * Replace external links
907 * Note: we have to do external links before the internal ones,
908 * and otherwise take great care in the order of things here, so
909 * that we don't end up interpreting some URLs twice.
913 function replaceExternalLinks( $text ) {
914 $fname = 'Parser::replaceExternalLinks';
915 wfProfileIn( $fname );
917 $sk =& $this->mOptions
->getSkin();
918 $linktrail = wfMsgForContent('linktrail');
919 $bits = preg_split( EXT_LINK_BRACKETED
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
921 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
924 while ( $i<count( $bits ) ) {
926 $protocol = $bits[$i++
];
928 $trail = $bits[$i++
];
930 # If the link text is an image URL, replace it with an <img> tag
931 # This happened by accident in the original parser, but some people used it extensively
932 $img = $this->maybeMakeImageLink( $text );
933 if ( $img !== false ) {
939 # No link text, e.g. [http://domain.tld/some.link]
941 # Autonumber if allowed
942 if ( strpos( HTTP_PROTOCOLS
, $protocol ) !== false ) {
943 $text = '[' . ++
$this->mAutonumber
. ']';
945 # Otherwise just use the URL
946 $text = htmlspecialchars( $url );
949 # Have link text, e.g. [http://domain.tld/some.link text]s
951 if ( preg_match( $linktrail, $trail, $m2 ) ) {
957 $encUrl = htmlspecialchars( $url );
958 # Bit in parentheses showing the URL for the printable version
959 if( $url == $text ||
preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
962 # Expand the URL for printable version
963 if ( ! $sk->suppressUrlExpansion() ) {
964 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
970 # Process the trail (i.e. everything after this link up until start of the next link),
971 # replacing any non-bracketed links
972 $trail = $this->replaceFreeExternalLinks( $trail );
974 $la = $sk->getExternalLinkAttributes( $url, $text );
976 # Use the encoded URL
977 # This means that users can paste URLs directly into the text
978 # Funny characters like ö aren't valid in URLs anyway
979 # This was changed in August 2004
980 $s .= "<a href=\"{$url}\"{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
983 wfProfileOut( $fname );
988 * Replace anything that looks like a URL with a link
991 function replaceFreeExternalLinks( $text ) {
992 $bits = preg_split( '/((?:'.URL_PROTOCOLS
.'):)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
993 $s = array_shift( $bits );
996 $sk =& $this->mOptions
->getSkin();
998 while ( $i < count( $bits ) ){
999 $protocol = $bits[$i++
];
1000 $remainder = $bits[$i++
];
1002 if ( preg_match( '/^('.EXT_LINK_URL_CLASS
.'+)(.*)$/s', $remainder, $m ) ) {
1003 # Found some characters after the protocol that look promising
1004 $url = $protocol . $m[1];
1007 # Move trailing punctuation to $trail
1009 # If there is no left bracket, then consider right brackets fair game too
1010 if ( strpos( $url, '(' ) === false ) {
1014 $numSepChars = strspn( strrev( $url ), $sep );
1015 if ( $numSepChars ) {
1016 $trail = substr( $url, -$numSepChars ) . $trail;
1017 $url = substr( $url, 0, -$numSepChars );
1020 # Replace & from obsolete syntax with &
1021 $url = str_replace( '&', '&', $url );
1023 # Is this an external image?
1024 $text = $this->maybeMakeImageLink( $url );
1025 if ( $text === false ) {
1026 # Not an image, make a link
1027 $text = $sk->makeExternalLink( $url, $url );
1029 $s .= $text . $trail;
1031 $s .= $protocol . $remainder;
1038 * make an image if it's allowed
1041 function maybeMakeImageLink( $url ) {
1042 $sk =& $this->mOptions
->getSkin();
1044 if ( $this->mOptions
->getAllowExternalImages() ) {
1045 if ( preg_match( EXT_IMAGE_REGEX
, $url ) ) {
1047 $text = $sk->makeImage( htmlspecialchars( $url ) );
1054 * Process [[ ]] wikilinks
1058 function replaceInternalLinks( $s ) {
1059 global $wgLang, $wgContLang, $wgLinkCache;
1060 static $fname = 'Parser::replaceInternalLinks' ;
1061 wfProfileIn( $fname );
1063 wfProfileIn( $fname.'-setup' );
1065 # the % is needed to support urlencoded titles as well
1066 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1067 $sk =& $this->mOptions
->getSkin();
1069 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
1071 $a = explode( '[[', ' ' . $s );
1072 $s = array_shift( $a );
1073 $s = substr( $s, 1 );
1075 # Match a link having the form [[namespace:link|alternate]]trail
1077 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1078 # Match the end of a line for a word that's not followed by whitespace,
1079 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1080 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1082 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
1083 # Special and Media are pseudo-namespaces; no pages actually exist in them
1085 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
1087 if ( $useLinkPrefixExtension ) {
1088 if ( preg_match( $e2, $s, $m ) ) {
1089 $first_prefix = $m[2];
1092 $first_prefix = false;
1098 wfProfileOut( $fname.'-setup' );
1100 # start procedeeding each line
1101 foreach ( $a as $line ) {
1102 wfProfileIn( $fname.'-prefixhandling' );
1103 if ( $useLinkPrefixExtension ) {
1104 if ( preg_match( $e2, $s, $m ) ) {
1112 $prefix = $first_prefix;
1113 $first_prefix = false;
1116 wfProfileOut( $fname.'-prefixhandling' );
1118 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1120 # fix up urlencoded title texts
1121 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1123 } else { # Invalid form; output directly
1124 $s .= $prefix . '[[' . $line ;
1128 # Don't allow internal links to pages containing
1129 # PROTO: where PROTO is a valid URL protocol; these
1130 # should be external links.
1131 if (preg_match('/((?:'.URL_PROTOCOLS
.'):)/', $m[1])) {
1132 $s .= $prefix . '[[' . $line ;
1136 # Make subpage if necessary
1137 $link = $this->maybeDoSubpageLink( $m[1], $text );
1139 $noforce = (substr($m[1], 0, 1) != ':');
1141 # Strip off leading ':'
1142 $link = substr($link, 1);
1145 $wasblank = ( '' == $text );
1146 if( $wasblank ) $text = $link;
1148 $nt = Title
::newFromText( $link );
1150 $s .= $prefix . '[[' . $line;
1154 $ns = $nt->getNamespace();
1155 $iw = $nt->getInterWiki();
1157 # Link not escaped by : , create the various objects
1161 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1162 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1163 $tmp = $prefix . $trail ;
1164 $s .= (trim($tmp) == '')?
'': $tmp;
1168 if ( $ns == NS_IMAGE
) {
1169 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1170 $wgLinkCache->addImageLinkObj( $nt );
1174 if ( $ns == NS_CATEGORY
) {
1175 $t = $nt->getText() ;
1176 $nnt = Title
::newFromText ( Namespace::getCanonicalName(NS_CATEGORY
).':'.$t ) ;
1178 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1179 $pPLC=$sk->postParseLinkColour();
1180 $sk->postParseLinkColour( false );
1181 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1182 $sk->postParseLinkColour( $pPLC );
1183 $wgLinkCache->resume();
1186 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
1187 $sortkey = $this->mTitle
->getText();
1189 $sortkey = $this->mTitle
->getPrefixedText();
1194 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1195 $this->mOutput
->mCategoryLinks
[] = $t ;
1196 $s .= $prefix . $trail ;
1201 if( ( $nt->getPrefixedText() === $this->mTitle
->getPrefixedText() ) &&
1202 ( strpos( $link, '#' ) === FALSE ) ) {
1203 # Self-links are handled specially; generally de-link and change to bold.
1204 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1208 if( $ns == NS_MEDIA
) {
1209 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1210 $wgLinkCache->addImageLinkObj( $nt );
1212 } elseif( $ns == NS_SPECIAL
) {
1213 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1216 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1218 wfProfileOut( $fname );
1223 * Handle link to subpage if necessary
1224 * @param $target string the source of the link
1225 * @param &$text the link text, modified as necessary
1226 * @return string the full name of the link
1229 function maybeDoSubpageLink($target, &$text) {
1232 # :Foobar -- override special treatment of prefix (images, language links)
1233 # /Foobar -- convert to CurrentPage/Foobar
1234 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1235 global $wgNamespacesWithSubpages;
1237 $fname = 'Parser::maybeDoSubpageLink';
1238 wfProfileIn( $fname );
1239 # Look at the first character
1240 if( $target{0} == '/' ) {
1241 # / at end means we don't want the slash to be shown
1242 if(substr($target,-1,1)=='/') {
1243 $target=substr($target,1,-1);
1246 $noslash=substr($target,1);
1249 # Some namespaces don't allow subpages
1250 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) {
1251 # subpages allowed here
1252 $ret = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1253 if( '' === $text ) {
1255 } # this might be changed for ugliness reasons
1257 # no subpage allowed, use standard link
1265 wfProfileOut( $fname );
1270 * Used by doBlockLevels()
1273 /* private */ function closeParagraph() {
1275 if ( '' != $this->mLastSection
) {
1276 $result = '</' . $this->mLastSection
. ">\n";
1278 $this->mInPre
= false;
1279 $this->mLastSection
= '';
1282 # getCommon() returns the length of the longest common substring
1283 # of both arguments, starting at the beginning of both.
1285 /* private */ function getCommon( $st1, $st2 ) {
1286 $fl = strlen( $st1 );
1287 $shorter = strlen( $st2 );
1288 if ( $fl < $shorter ) { $shorter = $fl; }
1290 for ( $i = 0; $i < $shorter; ++
$i ) {
1291 if ( $st1{$i} != $st2{$i} ) { break; }
1295 # These next three functions open, continue, and close the list
1296 # element appropriate to the prefix character passed into them.
1298 /* private */ function openList( $char ) {
1299 $result = $this->closeParagraph();
1301 if ( '*' == $char ) { $result .= '<ul><li>'; }
1302 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1303 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1304 else if ( ';' == $char ) {
1305 $result .= '<dl><dt>';
1306 $this->mDTopen
= true;
1308 else { $result = '<!-- ERR 1 -->'; }
1313 /* private */ function nextItem( $char ) {
1314 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1315 else if ( ':' == $char ||
';' == $char ) {
1317 if ( $this->mDTopen
) { $close = '</dt>'; }
1318 if ( ';' == $char ) {
1319 $this->mDTopen
= true;
1320 return $close . '<dt>';
1322 $this->mDTopen
= false;
1323 return $close . '<dd>';
1326 return '<!-- ERR 2 -->';
1329 /* private */ function closeList( $char ) {
1330 if ( '*' == $char ) { $text = '</li></ul>'; }
1331 else if ( '#' == $char ) { $text = '</li></ol>'; }
1332 else if ( ':' == $char ) {
1333 if ( $this->mDTopen
) {
1334 $this->mDTopen
= false;
1335 $text = '</dt></dl>';
1337 $text = '</dd></dl>';
1340 else { return '<!-- ERR 3 -->'; }
1346 * Make lists from lines starting with ':', '*', '#', etc.
1349 * @return string the lists rendered as HTML
1351 function doBlockLevels( $text, $linestart ) {
1352 $fname = 'Parser::doBlockLevels';
1353 wfProfileIn( $fname );
1355 # Parsing through the text line by line. The main thing
1356 # happening here is handling of block-level elements p, pre,
1357 # and making lists from lines starting with * # : etc.
1359 $textLines = explode( "\n", $text );
1361 $lastPrefix = $output = $lastLine = '';
1362 $this->mDTopen
= $inBlockElem = false;
1364 $paragraphStack = false;
1366 if ( !$linestart ) {
1367 $output .= array_shift( $textLines );
1369 foreach ( $textLines as $oLine ) {
1370 $lastPrefixLength = strlen( $lastPrefix );
1371 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1372 $preOpenMatch = preg_match('/<pre/i', $oLine );
1373 if ( !$this->mInPre
) {
1374 # Multiple prefixes may abut each other for nested lists.
1375 $prefixLength = strspn( $oLine, '*#:;' );
1376 $pref = substr( $oLine, 0, $prefixLength );
1379 $pref2 = str_replace( ';', ':', $pref );
1380 $t = substr( $oLine, $prefixLength );
1381 $this->mInPre
= !empty($preOpenMatch);
1383 # Don't interpret any other prefixes in preformatted text
1385 $pref = $pref2 = '';
1390 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1391 # Same as the last item, so no need to deal with nesting or opening stuff
1392 $output .= $this->nextItem( substr( $pref, -1 ) );
1393 $paragraphStack = false;
1395 if ( substr( $pref, -1 ) == ';') {
1396 # The one nasty exception: definition lists work like this:
1397 # ; title : definition text
1398 # So we check for : in the remainder text to split up the
1399 # title and definition, without b0rking links.
1400 # Don't count ':' in a URL
1402 while (($colon = strpos($t, ':', $pos)) !== false) {
1403 $m1 = substr($t, 0, $colon);
1404 $m2 = substr($t, $colon +
1);
1405 if (!preg_match('/(?:'.URL_PROTOCOLS
.')$/', $m1)) {
1410 if( $colon !== false ) {
1412 $output .= $term . $this->nextItem( ':' );
1416 } elseif( $prefixLength ||
$lastPrefixLength ) {
1417 # Either open or close a level...
1418 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1419 $paragraphStack = false;
1421 while( $commonPrefixLength < $lastPrefixLength ) {
1422 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1423 --$lastPrefixLength;
1425 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1426 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1428 while ( $prefixLength > $commonPrefixLength ) {
1429 $char = substr( $pref, $commonPrefixLength, 1 );
1430 $output .= $this->openList( $char );
1432 if ( ';' == $char ) {
1433 # FIXME: This is dupe of code above
1435 while (($colon = strpos($t, ':', $pos)) !== false) {
1436 $m1 = substr($t, 0, $colon);
1437 $m2 = substr($t, $colon +
1);
1438 if (!preg_match('/(?:'.URL_PROTOCOLS
.')$/', $m1)) {
1443 if( $colon !== false ) {
1445 $output .= $term . $this->nextItem( ':' );
1449 ++
$commonPrefixLength;
1451 $lastPrefix = $pref2;
1453 if( 0 == $prefixLength ) {
1454 # No prefix (not in list)--go to paragraph mode
1455 $uniq_prefix = UNIQ_PREFIX
;
1456 // XXX: use a stack for nestable elements like span, table and div
1457 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1458 $closematch = preg_match(
1459 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1460 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1461 if ( $openmatch or $closematch ) {
1462 $paragraphStack = false;
1463 $output .= $this->closeParagraph();
1464 if($preOpenMatch and !$preCloseMatch) {
1465 $this->mInPre
= true;
1467 if ( $closematch ) {
1468 $inBlockElem = false;
1470 $inBlockElem = true;
1472 } else if ( !$inBlockElem && !$this->mInPre
) {
1473 if ( ' ' == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1475 if ($this->mLastSection
!= 'pre') {
1476 $paragraphStack = false;
1477 $output .= $this->closeParagraph().'<pre>';
1478 $this->mLastSection
= 'pre';
1480 $t = substr( $t, 1 );
1483 if ( '' == trim($t) ) {
1484 if ( $paragraphStack ) {
1485 $output .= $paragraphStack.'<br />';
1486 $paragraphStack = false;
1487 $this->mLastSection
= 'p';
1489 if ($this->mLastSection
!= 'p' ) {
1490 $output .= $this->closeParagraph();
1491 $this->mLastSection
= '';
1492 $paragraphStack = '<p>';
1494 $paragraphStack = '</p><p>';
1498 if ( $paragraphStack ) {
1499 $output .= $paragraphStack;
1500 $paragraphStack = false;
1501 $this->mLastSection
= 'p';
1502 } else if ($this->mLastSection
!= 'p') {
1503 $output .= $this->closeParagraph().'<p>';
1504 $this->mLastSection
= 'p';
1510 if ($paragraphStack === false) {
1514 while ( $prefixLength ) {
1515 $output .= $this->closeList( $pref2{$prefixLength-1} );
1518 if ( '' != $this->mLastSection
) {
1519 $output .= '</' . $this->mLastSection
. '>';
1520 $this->mLastSection
= '';
1523 wfProfileOut( $fname );
1528 * Return value of a magic variable (like PAGENAME)
1532 function getVariableValue( $index ) {
1533 global $wgContLang, $wgSitename, $wgServer;
1536 case MAG_CURRENTMONTH
:
1537 return $wgContLang->formatNum( date( 'm' ) );
1538 case MAG_CURRENTMONTHNAME
:
1539 return $wgContLang->getMonthName( date('n') );
1540 case MAG_CURRENTMONTHNAMEGEN
:
1541 return $wgContLang->getMonthNameGen( date('n') );
1542 case MAG_CURRENTDAY
:
1543 return $wgContLang->formatNum( date('j') );
1545 return $this->mTitle
->getText();
1547 return $this->mTitle
->getPartialURL();
1549 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1550 return $wgContLang->getNsText($this->mTitle
->getNamespace()); # Patch by Dori
1551 case MAG_CURRENTDAYNAME
:
1552 return $wgContLang->getWeekdayName( date('w')+
1 );
1553 case MAG_CURRENTYEAR
:
1554 return $wgContLang->formatNum( date( 'Y' ) );
1555 case MAG_CURRENTTIME
:
1556 return $wgContLang->time( wfTimestampNow(), false );
1557 case MAG_NUMBEROFARTICLES
:
1558 return $wgContLang->formatNum( wfNumberOfArticles() );
1569 * initialise the magic variables (like CURRENTMONTHNAME)
1573 function initialiseVariables() {
1574 $fname = 'Parser::initialiseVariables';
1575 wfProfileIn( $fname );
1576 global $wgVariableIDs;
1577 $this->mVariables
= array();
1578 foreach ( $wgVariableIDs as $id ) {
1579 $mw =& MagicWord
::get( $id );
1580 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1582 wfProfileOut( $fname );
1586 * Replace magic variables, templates, and template arguments
1587 * with the appropriate text. Templates are substituted recursively,
1588 * taking care to avoid infinite loops.
1590 * Note that the substitution depends on value of $mOutputType:
1591 * OT_WIKI: only {{subst:}} templates
1592 * OT_MSG: only magic variables
1593 * OT_HTML: all templates and magic variables
1595 * @param string $tex The text to transform
1596 * @param array $args Key-value pairs representing template parameters to substitute
1599 function replaceVariables( $text, $args = array() ) {
1600 global $wgLang, $wgScript, $wgArticlePath;
1602 # Prevent too big inclusions
1603 if(strlen($text)> MAX_INCLUDE_SIZE
)
1606 $fname = 'Parser::replaceVariables';
1607 wfProfileIn( $fname );
1609 $titleChars = Title
::legalChars();
1611 # This function is called recursively. To keep track of arguments we need a stack:
1612 array_push( $this->mArgStack
, $args );
1614 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1615 $GLOBALS['wgCurParser'] =& $this;
1617 # Variable substitution
1618 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", 'wfVariableSubstitution', $text );
1620 if ( $this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) {
1621 # Argument substitution
1622 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1624 # Template substitution
1625 $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1626 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1628 array_pop( $this->mArgStack
);
1630 wfProfileOut( $fname );
1635 * Replace magic variables
1638 function variableSubstitution( $matches ) {
1639 if ( !$this->mVariables
) {
1640 $this->initialiseVariables();
1643 if ( $this->mOutputType
== OT_WIKI
) {
1644 # Do only magic variables prefixed by SUBST
1645 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1646 if (!$mwSubst->matchStartAndRemove( $matches[1] ))
1648 # Note that if we don't substitute the variable below,
1649 # we don't remove the {{subst:}} magic word, in case
1650 # it is a template rather than a magic variable.
1652 if ( !$skip && array_key_exists( $matches[1], $this->mVariables
) ) {
1653 $text = $this->mVariables
[$matches[1]];
1654 $this->mOutput
->mContainsOldMagic
= true;
1656 $text = $matches[0];
1661 # Split template arguments
1662 function getTemplateArgs( $argsString ) {
1663 if ( $argsString === '' ) {
1667 $args = explode( '|', substr( $argsString, 1 ) );
1669 # If any of the arguments contains a '[[' but no ']]', it needs to be
1670 # merged with the next arg because the '|' character between belongs
1671 # to the link syntax and not the template parameter syntax.
1672 $argc = count($args);
1674 for ( $i = 0; $i < $argc-1; $i++
) {
1675 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1676 $args[$i] .= '|'.$args[$i+
1];
1677 array_splice($args, $i+
1, 1);
1687 * Return the text of a template, after recursively
1688 * replacing any variables or templates within the template.
1690 * @param array $matches The parts of the template
1691 * $matches[1]: the title, i.e. the part before the |
1692 * $matches[2]: the parameters (including a leading |), if any
1693 * @return string the text of the template
1696 function braceSubstitution( $matches ) {
1697 global $wgLinkCache, $wgContLang;
1698 $fname = 'Parser::braceSubstitution';
1705 # Need to know if the template comes at the start of a line,
1706 # to treat the beginning of the template like the beginning
1707 # of a line for tables and block-level elements.
1708 $linestart = $matches[1];
1710 # $part1 is the bit before the first |, and must contain only title characters
1711 # $args is a list of arguments, starting from index 0, not including $part1
1713 $part1 = $matches[2];
1714 # If the third subpattern matched anything, it will start with |
1716 $args = $this->getTemplateArgs($matches[3]);
1717 $argc = count( $args );
1719 # Don't parse {{{}}} because that's only for template arguments
1720 if ( $linestart === '{' ) {
1721 $text = $matches[0];
1728 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1729 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType
== OT_WIKI
) ) {
1730 # One of two possibilities is true:
1731 # 1) Found SUBST but not in the PST phase
1732 # 2) Didn't find SUBST and in the PST phase
1733 # In either case, return without further processing
1734 $text = $matches[0];
1740 # MSG, MSGNW and INT
1743 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1744 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1747 # Remove obsolete MSG:
1748 $mwMsg =& MagicWord
::get( MAG_MSG
);
1749 $mwMsg->matchStartAndRemove( $part1 );
1752 # Check if it is an internal message
1753 $mwInt =& MagicWord
::get( MAG_INT
);
1754 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1755 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1756 $text = $linestart . wfMsgReal( $part1, $args, true );
1764 # Check for NS: (namespace expansion)
1765 $mwNs = MagicWord
::get( MAG_NS
);
1766 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1767 if ( intval( $part1 ) ) {
1768 $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
1771 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1772 if ( !is_null( $index ) ) {
1773 $text = $linestart . $wgContLang->getNsText( $index );
1780 # LOCALURL and LOCALURLE
1782 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1783 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1785 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1786 $func = 'getLocalURL';
1787 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1788 $func = 'escapeLocalURL';
1793 if ( $func !== '' ) {
1794 $title = Title
::newFromText( $part1 );
1795 if ( !is_null( $title ) ) {
1797 $text = $linestart . $title->$func( $args[0] );
1799 $text = $linestart . $title->$func();
1807 if ( !$found && $argc == 1 ) {
1808 $mwGrammar =& MagicWord
::get( MAG_GRAMMAR
);
1809 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1810 $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
1815 # Template table test
1817 # Did we encounter this template already? If yes, it is in the cache
1818 # and we need to check for loops.
1819 if ( !$found && isset( $this->mTemplates
[$part1] ) ) {
1820 # set $text to cached message.
1821 $text = $linestart . $this->mTemplates
[$part1];
1824 # Infinite loop test
1825 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1828 $text .= '<!-- WARNING: template loop detected -->';
1832 # Load from database
1833 $itcamefromthedatabase = false;
1836 $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
1837 if ($subpage !== '') {
1838 $ns = $this->mTitle
->getNamespace();
1840 $title = Title
::newFromText( $part1, $ns );
1841 if ( !is_null( $title ) && !$title->isExternal() ) {
1842 # Check for excessive inclusion
1843 $dbk = $title->getPrefixedDBkey();
1844 if ( $this->incrementIncludeCount( $dbk ) ) {
1845 # This should never be reached.
1846 $article = new Article( $title );
1847 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1848 if ( $articleContent !== false ) {
1850 $text = $linestart . $articleContent;
1851 $itcamefromthedatabase = true;
1855 # If the title is valid but undisplayable, make a link to it
1856 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1857 $text = $linestart . '[['.$title->getPrefixedText().']]';
1861 # Template cache array insertion
1862 $this->mTemplates
[$part1] = $text;
1866 # Recursive parsing, escaping and link table handling
1867 # Only for HTML output
1868 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1869 $text = wfEscapeWikiText( $text );
1870 } elseif ( ($this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) && $found && !$noparse) {
1871 # Clean up argument array
1872 $assocArgs = array();
1874 foreach( $args as $arg ) {
1875 $eqpos = strpos( $arg, '=' );
1876 if ( $eqpos === false ) {
1877 $assocArgs[$index++
] = $arg;
1879 $name = trim( substr( $arg, 0, $eqpos ) );
1880 $value = trim( substr( $arg, $eqpos+
1 ) );
1881 if ( $value === false ) {
1884 if ( $name !== false ) {
1885 $assocArgs[$name] = $value;
1890 # Add a new element to the templace recursion path
1891 $this->mTemplatePath
[$part1] = 1;
1893 $text = $this->strip( $text, $this->mStripState
);
1894 $text = $this->removeHTMLtags( $text );
1895 $text = $this->replaceVariables( $text, $assocArgs );
1897 # Resume the link cache and register the inclusion as a link
1898 if ( $this->mOutputType
== OT_HTML
&& !is_null( $title ) ) {
1899 $wgLinkCache->addLinkObj( $title );
1902 # If the template begins with a table or block-level
1903 # element, it should be treated as beginning a new line.
1904 if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
1905 $text = "\n" . $text;
1909 # Empties the template path
1910 $this->mTemplatePath
= array();
1914 # replace ==section headers==
1915 # XXX this needs to go away once we have a better parser.
1916 if ( $this->mOutputType
!= OT_WIKI
&& $itcamefromthedatabase ) {
1917 if( !is_null( $title ) )
1918 $encodedname = base64_encode($title->getPrefixedDBkey());
1920 $encodedname = base64_encode("");
1921 $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
1922 PREG_SPLIT_DELIM_CAPTURE
);
1925 for( $i = 0; $i < count($m); $i +
= 2 ) {
1927 if (!isset($m[$i +
1]) ||
$m[$i +
1] == "") continue;
1929 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
1933 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
1934 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
1935 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
1942 # Empties the template path
1943 $this->mTemplatePath
= array();
1952 * Triple brace replacement -- used for template arguments
1955 function argSubstitution( $matches ) {
1956 $arg = trim( $matches[1] );
1957 $text = $matches[0];
1958 $inputArgs = end( $this->mArgStack
);
1960 if ( array_key_exists( $arg, $inputArgs ) ) {
1961 $text = $inputArgs[$arg];
1968 * Returns true if the function is allowed to include this entity
1971 function incrementIncludeCount( $dbk ) {
1972 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1973 $this->mIncludeCount
[$dbk] = 0;
1975 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1984 * Cleans up HTML, removes dangerous tags and attributes, and
1985 * removes HTML comments
1988 function removeHTMLtags( $text ) {
1989 global $wgUseTidy, $wgUserHtml;
1990 $fname = 'Parser::removeHTMLtags';
1991 wfProfileIn( $fname );
1994 $htmlpairs = array( # Tags that must be closed
1995 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1996 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1997 'strike', 'strong', 'tt', 'var', 'div', 'center',
1998 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1999 'ruby', 'rt' , 'rb' , 'rp', 'p'
2001 $htmlsingle = array(
2002 'br', 'hr', 'li', 'dt', 'dd'
2004 $htmlnest = array( # Tags that can be nested--??
2005 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
2006 'dl', 'font', 'big', 'small', 'sub', 'sup'
2008 $tabletags = array( # Can only appear inside table
2012 $htmlpairs = array();
2013 $htmlsingle = array();
2014 $htmlnest = array();
2015 $tabletags = array();
2018 $htmlsingle = array_merge( $tabletags, $htmlsingle );
2019 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
2021 $htmlattrs = $this->getHTMLattrs () ;
2023 # Remove HTML comments
2024 $text = $this->removeHTMLcomments( $text );
2026 $bits = explode( '<', $text );
2027 $text = array_shift( $bits );
2029 $tagstack = array(); $tablestack = array();
2030 foreach ( $bits as $x ) {
2031 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
2032 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2034 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2035 error_reporting( $prev );
2038 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2042 if ( ! in_array( $t, $htmlsingle ) &&
2043 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
2044 @array_push
( $tagstack, $ot );
2047 if ( $t == 'table' ) {
2048 $tagstack = array_pop( $tablestack );
2053 # Keep track for later
2054 if ( in_array( $t, $tabletags ) &&
2055 ! in_array( 'table', $tagstack ) ) {
2057 } else if ( in_array( $t, $tagstack ) &&
2058 ! in_array ( $t , $htmlnest ) ) {
2060 } else if ( ! in_array( $t, $htmlsingle ) ) {
2061 if ( $t == 'table' ) {
2062 array_push( $tablestack, $tagstack );
2063 $tagstack = array();
2065 array_push( $tagstack, $t );
2067 # Strip non-approved attributes from the tag
2068 $newparams = $this->fixTagAttributes($params);
2072 $rest = str_replace( '>', '>', $rest );
2073 $text .= "<$slash$t $newparams$brace$rest";
2077 $text .= '<' . str_replace( '>', '>', $x);
2079 # Close off any remaining tags
2080 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2082 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2085 # this might be possible using tidy itself
2086 foreach ( $bits as $x ) {
2087 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2089 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2090 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2091 $newparams = $this->fixTagAttributes($params);
2092 $rest = str_replace( '>', '>', $rest );
2093 $text .= "<$slash$t $newparams$brace$rest";
2095 $text .= '<' . str_replace( '>', '>', $x);
2099 wfProfileOut( $fname );
2104 * Remove '<!--', '-->', and everything between.
2105 * To avoid leaving blank lines, when a comment is both preceded
2106 * and followed by a newline (ignoring spaces), trim leading and
2107 * trailing spaces and one of the newlines.
2111 function removeHTMLcomments( $text ) {
2112 $fname='Parser::removeHTMLcomments';
2113 wfProfileIn( $fname );
2114 while (($start = strpos($text, '<!--')) !== false) {
2115 $end = strpos($text, '-->', $start +
4);
2116 if ($end === false) {
2117 # Unterminated comment; bail out
2123 # Trim space and newline if the comment is both
2124 # preceded and followed by a newline
2125 $spaceStart = max($start - 1, 0);
2126 $spaceLen = $end - $spaceStart;
2127 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2131 while (substr($text, $spaceStart +
$spaceLen, 1) === ' ')
2133 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart +
$spaceLen, 1) === "\n") {
2134 # Remove the comment, leading and trailing
2135 # spaces, and leave only one newline.
2136 $text = substr_replace($text, "\n", $spaceStart, $spaceLen +
1);
2139 # Remove just the comment.
2140 $text = substr_replace($text, '', $start, $end - $start);
2143 wfProfileOut( $fname );
2148 * This function accomplishes several tasks:
2149 * 1) Auto-number headings if that option is enabled
2150 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2151 * 3) Add a Table of contents on the top for users who have enabled the option
2152 * 4) Auto-anchor headings
2154 * It loops through all headlines, collects the necessary data, then splits up the
2155 * string and re-inserts the newly formatted headlines.
2158 /* private */ function formatHeadings( $text, $isMain=true ) {
2159 global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders;
2161 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
2162 $doShowToc = $this->mOptions
->getShowToc();
2163 $forceTocHere = false;
2164 if( !$this->mTitle
->userCanEdit() ) {
2166 $rightClickHack = 0;
2168 $showEditLink = $this->mOptions
->getEditSection();
2169 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
2172 # Inhibit editsection links if requested in the page
2173 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
2174 if( $esw->matchAndRemove( $text ) ) {
2177 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2179 $mw =& MagicWord
::get( MAG_NOTOC
);
2180 if( $mw->matchAndRemove( $text ) ) {
2184 # never add the TOC to the Main Page. This is an entry page that should not
2185 # be more than 1-2 screens large anyway
2186 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
2190 # Get all headlines for numbering them and adding funky stuff like [edit]
2191 # links - this is for later, but we need the number of headlines right now
2192 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2194 # if there are fewer than 4 headlines in the article, do not show TOC
2195 if( $numMatches < 4 ) {
2199 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2200 # override above conditions and always show TOC at that place
2201 $mw =& MagicWord
::get( MAG_TOC
);
2202 if ($mw->match( $text ) ) {
2204 $forceTocHere = true;
2206 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2207 # override above conditions and always show TOC above first header
2208 $mw =& MagicWord
::get( MAG_FORCETOC
);
2209 if ($mw->matchAndRemove( $text ) ) {
2216 # We need this to perform operations on the HTML
2217 $sk =& $this->mOptions
->getSkin();
2221 $sectionCount = 0; # headlineCount excluding template sections
2223 # Ugh .. the TOC should have neat indentation levels which can be
2224 # passed to the skin functions. These are determined here
2229 $sublevelCount = array();
2232 foreach( $matches[3] as $headline ) {
2234 $templatetitle = "";
2235 $templatesection = 0;
2237 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2239 $templatetitle = base64_decode($mat[1]);
2240 $templatesection = 1 +
(int)base64_decode($mat[2]);
2241 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2246 $prevlevel = $level;
2248 $level = $matches[1][$headlineCount];
2249 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
2250 # reset when we enter a new level
2251 $sublevelCount[$level] = 0;
2252 $toc .= $sk->tocIndent( $level - $prevlevel );
2253 $toclevel +
= $level - $prevlevel;
2255 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
2256 # reset when we step back a level
2257 $sublevelCount[$level+
1]=0;
2258 $toc .= $sk->tocUnindent( $prevlevel - $level );
2259 $toclevel -= $prevlevel - $level;
2261 # count number of headlines for each level
2262 @$sublevelCount[$level]++
;
2263 if( $doNumberHeadings ||
$doShowToc ) {
2265 for( $i = 1; $i <= $level; $i++
) {
2266 if( !empty( $sublevelCount[$i] ) ) {
2270 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
2276 # The canonized header is a version of the header text safe to use for links
2277 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2278 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
2279 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
2281 # Remove link placeholders by the link text.
2282 # <!--LINK number-->
2284 # link text with suffix
2285 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2286 "\$wgLinkHolders['texts'][\$1]",
2287 $canonized_headline );
2290 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2291 $tocline = trim( $canonized_headline );
2292 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
2293 $replacearray = array(
2297 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2298 $refer[$headlineCount] = $canonized_headline;
2300 # count how many in assoc. array so we can track dupes in anchors
2301 @$refers[$canonized_headline]++
;
2302 $refcount[$headlineCount]=$refers[$canonized_headline];
2304 # Prepend the number to the heading text
2306 if( $doNumberHeadings ||
$doShowToc ) {
2307 $tocline = $numbering . ' ' . $tocline;
2309 # Don't number the heading if it is the only one (looks silly)
2310 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2311 # the two are different if the line contains a link
2312 $headline=$numbering . ' ' . $headline;
2316 # Create the anchor for linking from the TOC to the section
2317 $anchor = $canonized_headline;
2318 if($refcount[$headlineCount] > 1 ) {
2319 $anchor .= '_' . $refcount[$headlineCount];
2321 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2322 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2324 if( $showEditLink && ( !$istemplate ||
$templatetitle !== "" ) ) {
2325 if ( empty( $head[$headlineCount] ) ) {
2326 $head[$headlineCount] = '';
2329 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2331 $head[$headlineCount] .= $sk->editSectionLink($sectionCount+
1);
2334 # Add the edit section span
2335 if( $rightClickHack ) {
2337 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2339 $headline = $sk->editSectionScript($sectionCount+
1,$headline);
2342 # give headline the correct <h#> tag
2343 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2351 $toclines = $headlineCount;
2352 $toc .= $sk->tocUnindent( $toclevel );
2353 $toc = $sk->tocTable( $toc );
2356 # split up and insert constructed headlines
2358 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2361 foreach( $blocks as $block ) {
2362 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2363 # This is the [edit] link that appears for the top block of text when
2364 # section editing is enabled
2366 # Disabled because it broke block formatting
2367 # For example, a bullet point in the top line
2368 # $full .= $sk->editSectionLink(0);
2371 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2372 # Top anchor now in skin
2376 if( !empty( $head[$i] ) ) {
2382 $mw =& MagicWord
::get( MAG_TOC
);
2383 return $mw->replace( $toc, $full );
2390 * Return an HTML link for the "ISBN 123456" text
2393 function magicISBN( $text ) {
2395 $fname = 'Parser::magicISBN';
2396 wfProfileIn( $fname );
2398 $a = split( 'ISBN ', ' '.$text );
2399 if ( count ( $a ) < 2 ) {
2400 wfProfileOut( $fname );
2403 $text = substr( array_shift( $a ), 1);
2404 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2406 foreach ( $a as $x ) {
2407 $isbn = $blank = '' ;
2408 while ( ' ' == $x{0} ) {
2410 $x = substr( $x, 1 );
2412 if ( $x == '' ) { # blank isbn
2413 $text .= "ISBN $blank";
2416 while ( strstr( $valid, $x{0} ) != false ) {
2418 $x = substr( $x, 1 );
2420 $num = str_replace( '-', '', $isbn );
2421 $num = str_replace( ' ', '', $num );
2424 $text .= "ISBN $blank$x";
2426 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2427 $text .= '<a href="' .
2428 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2429 "\" class=\"internal\">ISBN $isbn</a>";
2433 wfProfileOut( $fname );
2438 * Return an HTML link for the "GEO ..." text
2441 function magicGEO( $text ) {
2442 global $wgLang, $wgUseGeoMode;
2443 $fname = 'Parser::magicGEO';
2444 wfProfileIn( $fname );
2446 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2447 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2448 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2449 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2450 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2451 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2453 $a = split( 'GEO ', ' '.$text );
2454 if ( count ( $a ) < 2 ) {
2455 wfProfileOut( $fname );
2458 $text = substr( array_shift( $a ), 1);
2459 $valid = '0123456789.+-:';
2461 foreach ( $a as $x ) {
2462 $geo = $blank = '' ;
2463 while ( ' ' == $x{0} ) {
2465 $x = substr( $x, 1 );
2467 while ( strstr( $valid, $x{0} ) != false ) {
2469 $x = substr( $x, 1 );
2471 $num = str_replace( '+', '', $geo );
2472 $num = str_replace( ' ', '', $num );
2474 if ( '' == $num ||
count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2475 $text .= "GEO $blank$x";
2477 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2478 $text .= '<a href="' .
2479 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2480 "\" class=\"internal\">GEO $geo</a>";
2484 wfProfileOut( $fname );
2489 * Return an HTML link for the "RFC 1234" text
2491 * @param string $text text to be processed
2493 function magicRFC( $text ) {
2496 $valid = '0123456789';
2499 $a = split( 'RFC ', ' '.$text );
2500 if ( count ( $a ) < 2 ) return $text;
2501 $text = substr( array_shift( $a ), 1);
2503 /* Check if RFC keyword is preceed by [[.
2504 * This test is made here cause of the array_shift above
2505 * that prevent the test to be done in the foreach.
2507 if(substr($text, -2) == '[[') { $internal = true; }
2509 foreach ( $a as $x ) {
2510 /* token might be empty if we have RFC RFC 1234 */
2516 $rfc = $blank = '' ;
2518 /** remove and save whitespaces in $blank */
2519 while ( $x{0} == ' ' ) {
2521 $x = substr( $x, 1 );
2524 /** remove and save the rfc number in $rfc */
2525 while ( strstr( $valid, $x{0} ) != false ) {
2527 $x = substr( $x, 1 );
2531 /* call back stripped spaces*/
2532 $text .= "RFC $blank$x";
2533 } elseif( $internal) {
2535 $text .= "RFC $rfc$x";
2537 /* build the external link*/
2538 $url = wfmsg( 'rfcurl' );
2539 $url = str_replace( '$1', $rfc, $url);
2540 $sk =& $this->mOptions
->getSkin();
2541 $la = $sk->getExternalLinkAttributes( $url, 'RFC '.$rfc );
2542 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2545 /* Check if the next RFC keyword is preceed by [[ */
2546 $internal = (substr($x,-2) == '[[');
2552 * Transform wiki markup when saving a page by doing \r\n -> \n
2553 * conversion, substitting signatures, {{subst:}} templates, etc.
2555 * @param string $text the text to transform
2556 * @param Title &$title the Title object for the current article
2557 * @param User &$user the User object describing the current user
2558 * @param ParserOptions $options parsing options
2559 * @param bool $clearState whether to clear the parser state first
2560 * @return string the altered wiki markup
2563 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2564 $this->mOptions
= $options;
2565 $this->mTitle
=& $title;
2566 $this->mOutputType
= OT_WIKI
;
2568 if ( $clearState ) {
2569 $this->clearState();
2572 $stripState = false;
2576 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2580 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2581 "/<br *?>/i" => "<br />",
2583 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2585 $text = $this->strip( $text, $stripState, false );
2586 $text = $this->pstPass2( $text, $user );
2587 $text = $this->unstrip( $text, $stripState );
2588 $text = $this->unstripNoWiki( $text, $stripState );
2593 * Pre-save transform helper function
2596 function pstPass2( $text, &$user ) {
2597 global $wgLang, $wgContLang, $wgLocaltimezone, $wgCurParser;
2599 # Variable replacement
2600 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2601 $text = $this->replaceVariables( $text );
2605 $n = $user->getName();
2606 $k = $user->getOption( 'nickname' );
2607 if ( '' == $k ) { $k = $n; }
2608 if(isset($wgLocaltimezone)) {
2609 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2611 /* Note: this is an ugly timezone hack for the European wikis */
2612 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
2613 ' (' . date( 'T' ) . ')';
2614 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2616 $text = preg_replace( '/~~~~~/', $d, $text );
2617 $text = preg_replace( '/~~~~/', '[[' . $wgContLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2618 $text = preg_replace( '/~~~/', '[[' . $wgContLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2620 # Context links: [[|name]] and [[name (context)|]]
2622 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2623 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2624 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2625 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2627 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2628 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2629 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2630 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2632 $t = $this->mTitle
->getText();
2633 if ( preg_match( $conpat, $t, $m ) ) {
2636 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2637 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2638 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2640 if ( '' == $context ) {
2641 $text = preg_replace( $p2, '[[\\1]]', $text );
2643 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2646 # Trim trailing whitespace
2647 # MAG_END (__END__) tag allows for trailing
2648 # whitespace to be deliberately included
2649 $text = rtrim( $text );
2650 $mw =& MagicWord
::get( MAG_END
);
2651 $mw->matchAndRemove( $text );
2657 * Set up some variables which are usually set up in parse()
2658 * so that an external function can call some class members with confidence
2661 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2662 $this->mTitle
=& $title;
2663 $this->mOptions
= $options;
2664 $this->mOutputType
= $outputType;
2665 if ( $clearState ) {
2666 $this->clearState();
2671 * Transform a MediaWiki message by replacing magic variables.
2673 * @param string $text the text to transform
2674 * @param ParserOptions $options options
2675 * @return string the text with variables substituted
2678 function transformMsg( $text, $options ) {
2680 static $executing = false;
2682 # Guard against infinite recursion
2688 $this->mTitle
= $wgTitle;
2689 $this->mOptions
= $options;
2690 $this->mOutputType
= OT_MSG
;
2691 $this->clearState();
2692 $text = $this->replaceVariables( $text );
2699 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2700 * Callback will be called with the text within
2701 * Transform and return the text within
2704 function setHook( $tag, $callback ) {
2705 $oldVal = @$this->mTagHooks
[$tag];
2706 $this->mTagHooks
[$tag] = $callback;
2713 * @package MediaWiki
2717 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2718 var $mCacheTime; # Used in ParserCache
2720 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
2721 $containsOldMagic = false )
2723 $this->mText
= $text;
2724 $this->mLanguageLinks
= $languageLinks;
2725 $this->mCategoryLinks
= $categoryLinks;
2726 $this->mContainsOldMagic
= $containsOldMagic;
2727 $this->mCacheTime
= '';
2730 function getText() { return $this->mText
; }
2731 function getLanguageLinks() { return $this->mLanguageLinks
; }
2732 function getCategoryLinks() { return $this->mCategoryLinks
; }
2733 function getCacheTime() { return $this->mCacheTime
; }
2734 function containsOldMagic() { return $this->mContainsOldMagic
; }
2735 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2736 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2737 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2738 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2739 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2741 function merge( $other ) {
2742 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2743 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2744 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2750 * Set options of the Parser
2752 * @package MediaWiki
2756 # All variables are private
2757 var $mUseTeX; # Use texvc to expand <math> tags
2758 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2759 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2760 var $mAllowExternalImages; # Allow external images inline
2761 var $mSkin; # Reference to the preferred skin
2762 var $mDateFormat; # Date format index
2763 var $mEditSection; # Create "edit section" links
2764 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2765 var $mNumberHeadings; # Automatically number headings
2766 var $mShowToc; # Show table of contents
2768 function getUseTeX() { return $this->mUseTeX
; }
2769 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2770 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2771 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2772 function getSkin() { return $this->mSkin
; }
2773 function getDateFormat() { return $this->mDateFormat
; }
2774 function getEditSection() { return $this->mEditSection
; }
2775 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2776 function getNumberHeadings() { return $this->mNumberHeadings
; }
2777 function getShowToc() { return $this->mShowToc
; }
2779 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2780 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2781 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2782 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2783 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2784 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2785 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2786 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2787 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2789 function setSkin( &$x ) { $this->mSkin
=& $x; }
2791 # Get parser options
2792 /* static */ function newFromUser( &$user ) {
2793 $popts = new ParserOptions
;
2794 $popts->initialiseFromUser( $user );
2799 function initialiseFromUser( &$userInput ) {
2800 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2802 $fname = 'ParserOptions::initialiseFromUser';
2803 wfProfileIn( $fname );
2804 if ( !$userInput ) {
2806 $user->setLoaded( true );
2808 $user =& $userInput;
2811 $this->mUseTeX
= $wgUseTeX;
2812 $this->mUseDynamicDates
= $wgUseDynamicDates;
2813 $this->mInterwikiMagic
= $wgInterwikiMagic;
2814 $this->mAllowExternalImages
= $wgAllowExternalImages;
2815 wfProfileIn( $fname.'-skin' );
2816 $this->mSkin
=& $user->getSkin();
2817 wfProfileOut( $fname.'-skin' );
2818 $this->mDateFormat
= $user->getOption( 'date' );
2819 $this->mEditSection
= $user->getOption( 'editsection' );
2820 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2821 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2822 $this->mShowToc
= $user->getOption( 'showtoc' );
2823 wfProfileOut( $fname );
2829 # Regex callbacks, used in Parser::replaceVariables
2830 function wfBraceSubstitution( $matches ) {
2831 global $wgCurParser;
2832 return $wgCurParser->braceSubstitution( $matches );
2835 function wfArgSubstitution( $matches ) {
2836 global $wgCurParser;
2837 return $wgCurParser->argSubstitution( $matches );
2840 function wfVariableSubstitution( $matches ) {
2841 global $wgCurParser;
2842 return $wgCurParser->variableSubstitution( $matches );
2846 * Return the total number of articles
2848 function wfNumberOfArticles() {
2849 global $wgNumberOfArticles;
2852 return $wgNumberOfArticles;
2856 * Get various statistics from the database
2859 function wfLoadSiteStats() {
2860 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
2861 $fname = 'wfLoadSiteStats';
2863 if ( -1 != $wgNumberOfArticles ) return;
2864 $dbr =& wfGetDB( DB_SLAVE
);
2865 $s = $dbr->getArray( 'site_stats',
2866 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
2867 array( 'ss_row_id' => 1 ), $fname
2870 if ( $s === false ) {
2873 $wgTotalViews = $s->ss_total_views
;
2874 $wgTotalEdits = $s->ss_total_edits
;
2875 $wgNumberOfArticles = $s->ss_good_articles
;
2879 function wfEscapeHTMLTagsOnly( $in ) {
2881 array( '"', '>', '<' ),
2882 array( '"', '>', '<' ),