3 // require_once('Tokenizer.php');
6 * File for Parser and related classes
13 * Variable substitution O(N^2) attack
15 * Without countermeasures, it would be possible to attack the parser by saving
16 * a page filled with a large number of inclusions of large pages. The size of
17 * the generated page would be proportional to the square of the input size.
18 * Hence, we limit the number of inclusions of any given page, thus bringing any
19 * attack back to O(N).
21 define( 'MAX_INCLUDE_REPEAT', 100 );
22 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
24 # Allowed values for $mOutputType
25 define( 'OT_HTML', 1 );
26 define( 'OT_WIKI', 2 );
27 define( 'OT_MSG' , 3 );
29 # string parameter for extractTags which will cause it
30 # to strip HTML comments in addition to regular
31 # <XML>-style tags. This should not be anything we
32 # may want to use in wikisyntax
33 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
35 # prefix for escaping, used in two functions at least
36 define( 'UNIQ_PREFIX', 'NaodW29');
38 # Constants needed for external link processing
39 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
40 define( 'HTTP_PROTOCOLS', 'http|https' );
41 # Everything except bracket, space, or control characters
42 define( 'EXT_LINK_URL_CLASS', '[^]\\x00-\\x20\\x7F]' );
43 define( 'INVERSE_EXT_LINK_URL_CLASS', '[\]\\x00-\\x20\\x7F]' );
45 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
46 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
47 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
48 define( 'EXT_LINK_BRACKETED', '/\[(('.URL_PROTOCOLS
.'):'.EXT_LINK_URL_CLASS
.'+) *('.EXT_LINK_TEXT_CLASS
.'*?)\]/S' );
49 define( 'EXT_IMAGE_REGEX',
50 '/^('.HTTP_PROTOCOLS
.':)'. # Protocol
51 '('.EXT_LINK_URL_CLASS
.'+)\\/'. # Hostname and path
52 '('.EXT_IMAGE_FNAME_CLASS
.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS
.')$/S' # Filename
58 * Processes wiki markup
61 * There are three main entry points into the Parser class:
63 * produces HTML output
65 * produces altered wiki markup.
67 * performs brace substitution on MediaWiki messages
70 * objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
72 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
75 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
76 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
79 * * only within ParserOptions
92 # Cleared with clearState():
93 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
94 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
97 var $mOptions, $mTitle, $mOutputType,
98 $mTemplates, // cache of already loaded templates, avoids
99 // multiple SQL queries for the same string
100 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
101 // in this path. Used for loop detection.
111 $this->mTemplates
= array();
112 $this->mTemplatePath
= array();
113 $this->mTagHooks
= array();
122 function clearState() {
123 $this->mOutput
= new ParserOutput
;
124 $this->mAutonumber
= 0;
125 $this->mLastSection
= "";
126 $this->mDTopen
= false;
127 $this->mVariables
= false;
128 $this->mIncludeCount
= array();
129 $this->mStripState
= array();
130 $this->mArgStack
= array();
131 $this->mInPre
= false;
135 * First pass--just handle <nowiki> sections, pass the rest off
136 * to internalParse() which does all the real work.
139 * @return ParserOutput a ParserOutput
141 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
143 $fname = 'Parser::parse';
144 wfProfileIn( $fname );
150 $this->mOptions
= $options;
151 $this->mTitle
=& $title;
152 $this->mOutputType
= OT_HTML
;
155 $text = $this->strip( $text, $this->mStripState
);
156 $text = $this->internalParse( $text, $linestart );
157 $text = $this->unstrip( $text, $this->mStripState
);
158 # Clean up special characters, only run once, next-to-last before doBlockLevels
161 # french spaces, last one Guillemet-left
162 # only if there is something before the space
163 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
164 # french spaces, Guillemet-right
165 "/(\\302\\253) /i"=>"\\1 ",
166 '/<hr *>/i' => '<hr />',
167 '/<br *>/i' => '<br />',
168 '/<center *>/i' => '<div class="center">',
169 '/<\\/center *>/i' => '</div>',
170 # Clean up spare ampersands; note that we probably ought to be
171 # more careful about named entities.
172 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
174 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
177 # french spaces, last one Guillemet-left
178 '/ (\\?|:|;|!|\\302\\273)/i' => ' \\1',
179 # french spaces, Guillemet-right
180 '/(\\302\\253) /i' => '\\1 ',
181 '/<center *>/i' => '<div class="center">',
182 '/<\\/center *>/i' => '</div>'
184 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
187 $text = $this->doBlockLevels( $text, $linestart );
188 $text = $this->unstripNoWiki( $text, $this->mStripState
);
190 $text = $this->tidy($text);
192 $this->mOutput
->setText( $text );
193 wfProfileOut( $fname );
194 return $this->mOutput
;
198 * Get a random string
203 function getRandomString() {
204 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
208 * Replaces all occurrences of <$tag>content</$tag> in the text
209 * with a random marker and returns the new text. the output parameter
210 * $content will be an associative array filled with data on the form
211 * $unique_marker => content.
213 * If $content is already set, the additional entries will be appended
214 * If $tag is set to STRIP_COMMENTS, the function will extract
215 * <!-- HTML comments -->
220 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
221 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
228 while ( '' != $text ) {
229 if($tag==STRIP_COMMENTS
) {
230 $p = preg_split( '/<!--/i', $text, 2 );
232 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
235 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
238 if($tag==STRIP_COMMENTS
) {
239 $q = preg_split( '/-->/i', $p[1], 2 );
241 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
243 $marker = $rnd . sprintf('%08X', $n++
);
244 $content[$marker] = $q[0];
245 $stripped .= $marker;
253 * Strips and renders nowiki, pre, math, hiero
254 * If $render is set, performs necessary rendering operations on plugins
255 * Returns the text, and fills an array with data needed in unstrip()
256 * If the $state is already a valid strip state, it adds to the state
258 * @param bool $stripcomments when set, HTML comments <!-- like this -->
259 * will be stripped in addition to other tags. This is important
260 * for section editing, where these comments cause confusion when
261 * counting the sections in the wikisource
265 function strip( $text, &$state, $stripcomments = false ) {
266 $render = ($this->mOutputType
== OT_HTML
);
267 $html_content = array();
268 $nowiki_content = array();
269 $math_content = array();
270 $pre_content = array();
271 $comment_content = array();
272 $ext_content = array();
274 # Replace any instances of the placeholders
275 $uniq_prefix = UNIQ_PREFIX
;
276 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
279 global $wgRawHtml, $wgWhitelistEdit;
280 if( $wgRawHtml && $wgWhitelistEdit ) {
281 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
282 foreach( $html_content as $marker => $content ) {
284 # Raw and unchecked for validity.
285 $html_content[$marker] = $content;
287 $html_content[$marker] = '<html>'.$content.'</html>';
293 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
294 foreach( $nowiki_content as $marker => $content ) {
296 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
298 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
303 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
304 foreach( $math_content as $marker => $content ){
306 if( $this->mOptions
->getUseTeX() ) {
307 $math_content[$marker] = renderMath( $content );
309 $math_content[$marker] = '<math>'.$content.'<math>';
312 $math_content[$marker] = '<math>'.$content.'</math>';
317 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
318 foreach( $pre_content as $marker => $content ){
320 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
322 $pre_content[$marker] = '<pre>'.$content.'</pre>';
328 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
329 foreach( $comment_content as $marker => $content ){
330 $comment_content[$marker] = '<!--'.$content.'-->';
335 foreach ( $this->mTagHooks
as $tag => $callback ) {
336 $ext_contents[$tag] = array();
337 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
338 foreach( $ext_content[$tag] as $marker => $content ) {
340 $ext_content[$tag][$marker] = $callback( $content );
342 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
347 # Merge state with the pre-existing state, if there is one
349 $state['html'] = $state['html'] +
$html_content;
350 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
351 $state['math'] = $state['math'] +
$math_content;
352 $state['pre'] = $state['pre'] +
$pre_content;
353 $state['comment'] = $state['comment'] +
$comment_content;
355 foreach( $ext_content as $tag => $array ) {
356 if ( array_key_exists( $tag, $state ) ) {
357 $state[$tag] = $state[$tag] +
$array;
362 'html' => $html_content,
363 'nowiki' => $nowiki_content,
364 'math' => $math_content,
365 'pre' => $pre_content,
366 'comment' => $comment_content,
373 * restores pre, math, and heiro removed by strip()
375 * always call unstripNoWiki() after this one
378 function unstrip( $text, &$state ) {
379 # Must expand in reverse order, otherwise nested tags will be corrupted
380 $contentDict = end( $state );
381 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
382 if( key($state) != 'nowiki' && key($state) != 'html') {
383 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
384 $text = str_replace( key( $contentDict ), $content, $text );
393 * always call this after unstrip() to preserve the order
397 function unstripNoWiki( $text, &$state ) {
398 # Must expand in reverse order, otherwise nested tags will be corrupted
399 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
400 $text = str_replace( key( $state['nowiki'] ), $content, $text );
405 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
406 $text = str_replace( key( $state['html'] ), $content, $text );
414 * Add an item to the strip state
415 * Returns the unique tag which must be inserted into the stripped text
416 * The tag will be replaced with the original text in unstrip()
420 function insertStripItem( $text, &$state ) {
421 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
430 $state['item'][$rnd] = $text;
435 * Return allowed HTML attributes
439 function getHTMLattrs () {
440 $htmlattrs = array( # Allowed attributes--no scripting, etc.
441 'title', 'align', 'lang', 'dir', 'width', 'height',
442 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
443 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
444 /* FONT */ 'type', 'start', 'value', 'compact',
445 /* For various lists, mostly deprecated but safe */
446 'summary', 'width', 'border', 'frame', 'rules',
447 'cellspacing', 'cellpadding', 'valign', 'char',
448 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
449 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
450 'id', 'class', 'name', 'style' /* For CSS */
456 * Remove non approved attributes and javascript in css
460 function fixTagAttributes ( $t ) {
461 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
462 $htmlattrs = $this->getHTMLattrs() ;
464 # Strip non-approved attributes from the tag
466 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
467 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
470 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
472 # Strip javascript "expression" from stylesheets. Brute force approach:
473 # If anythin offensive is found, all attributes of the HTML tag are dropped
476 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
477 wfMungeToUtf8( $t ) ) )
486 * interface with html tidy, used if $wgUseTidy = true
490 function tidy ( $text ) {
491 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
492 global $wgInputEncoding, $wgOutputEncoding;
493 $fname = 'Parser::tidy';
494 wfProfileIn( $fname );
497 switch(strtoupper($wgOutputEncoding)) {
499 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
502 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
505 $wgTidyOpts .= ' -raw';
508 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
509 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
510 '<head><title>test</title></head><body>'.$text.'</body></html>';
511 $descriptorspec = array(
512 0 => array('pipe', 'r'),
513 1 => array('pipe', 'w'),
514 2 => array('file', '/dev/null', 'a')
516 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
517 if (is_resource($process)) {
518 fwrite($pipes[0], $wrappedtext);
520 while (!feof($pipes[1])) {
521 $cleansource .= fgets($pipes[1], 1024);
524 $return_value = proc_close($process);
527 wfProfileOut( $fname );
529 if( $cleansource == '' && $text != '') {
530 wfDebug( "Tidy error detected!\n" );
531 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
538 * parse the wiki syntax used to render tables
542 function doTableStuff ( $t ) {
543 $fname = 'Parser::doTableStuff';
544 wfProfileIn( $fname );
546 $t = explode ( "\n" , $t ) ;
547 $td = array () ; # Is currently a td tag open?
548 $ltd = array () ; # Was it TD or TH?
549 $tr = array () ; # Is currently a tr tag open?
550 $ltr = array () ; # tr attributes
551 $indent_level = 0; # indent level of the table
552 foreach ( $t AS $k => $x )
555 $fc = substr ( $x , 0 , 1 ) ;
556 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
557 $indent_level = strlen( $matches[1] );
559 str_repeat( '<dl><dd>', $indent_level ) .
560 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
561 array_push ( $td , false ) ;
562 array_push ( $ltd , '' ) ;
563 array_push ( $tr , false ) ;
564 array_push ( $ltr , '' ) ;
566 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
567 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
569 $l = array_pop ( $ltd ) ;
570 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
571 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
573 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
575 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
576 $x = substr ( $x , 1 ) ;
577 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
579 $l = array_pop ( $ltd ) ;
580 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
581 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
584 array_push ( $tr , false ) ;
585 array_push ( $td , false ) ;
586 array_push ( $ltd , '' ) ;
587 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
589 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) { # Caption
590 if ( '|+' == substr ( $x , 0 , 2 ) ) {
592 $x = substr ( $x , 1 ) ;
594 $after = substr ( $x , 1 ) ;
595 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
596 $after = explode ( '||' , $after ) ;
598 foreach ( $after AS $theline )
603 $tra = array_pop ( $ltr ) ;
604 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
605 array_push ( $tr , true ) ;
606 array_push ( $ltr , '' ) ;
609 $l = array_pop ( $ltd ) ;
610 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
611 if ( $fc == '|' ) $l = 'td' ;
612 else if ( $fc == '!' ) $l = 'th' ;
613 else if ( $fc == '+' ) $l = 'caption' ;
615 array_push ( $ltd , $l ) ;
616 $y = explode ( '|' , $theline , 2 ) ;
617 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
618 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
620 array_push ( $td , true ) ;
625 # Closing open td, tr && table
626 while ( count ( $td ) > 0 )
628 if ( array_pop ( $td ) ) $t[] = '</td>' ;
629 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
633 $t = implode ( "\n" , $t ) ;
634 # $t = $this->removeHTMLtags( $t );
635 wfProfileOut( $fname );
640 * Helper function for parse() that transforms wiki markup into
641 * HTML. Only called for $mOutputType == OT_HTML.
645 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
648 $fname = 'Parser::internalParse';
649 wfProfileIn( $fname );
651 $text = $this->removeHTMLtags( $text );
652 $text = $this->replaceVariables( $text, $args );
654 $text = $wgContLang->convert($text);
656 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
658 $text = $this->doHeadings( $text );
659 if($this->mOptions
->getUseDynamicDates()) {
660 global $wgDateFormatter;
661 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
663 $text = $this->doAllQuotes( $text );
664 $text = $this->replaceExternalLinks( $text );
665 $text = $this->doMagicLinks( $text );
666 $text = $this->replaceInternalLinks ( $text );
667 # Another call to replace links and images inside captions of images
668 $text = $this->replaceInternalLinks ( $text );
670 $text = $this->doTableStuff( $text );
671 $text = $this->formatHeadings( $text, $isMain );
672 $sk =& $this->mOptions
->getSkin();
673 $text = $sk->transformContent( $text );
675 wfProfileOut( $fname );
680 * Replace special strings like "ISBN xxx" and "RFC xxx" with
681 * magic external links.
685 function &doMagicLinks( &$text ) {
686 global $wgUseGeoMode;
687 $text = $this->magicISBN( $text );
688 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
689 $text = $this->magicGEO( $text );
691 $text = $this->magicRFC( $text );
696 * Parse ^^ tokens and return html
700 function doExponent ( $text ) {
701 $fname = 'Parser::doExponent';
702 wfProfileIn( $fname);
703 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
704 wfProfileOut( $fname);
709 * Parse headers and return html
713 function doHeadings( $text ) {
714 $fname = 'Parser::doHeadings';
715 wfProfileIn( $fname );
716 for ( $i = 6; $i >= 1; --$i ) {
717 $h = substr( '======', 0, $i );
718 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
719 "<h{$i}>\\1</h{$i}>\\2", $text );
721 wfProfileOut( $fname );
726 * Replace single quotes with HTML markup
728 * @return string the altered text
730 function doAllQuotes( $text ) {
731 $fname = 'Parser::doAllQuotes';
732 wfProfileIn( $fname );
734 $lines = explode( "\n", $text );
735 foreach ( $lines as $line ) {
736 $outtext .= $this->doQuotes ( $line ) . "\n";
738 $outtext = substr($outtext, 0,-1);
739 wfProfileOut( $fname );
744 * Helper function for doAllQuotes()
747 function doQuotes( $text ) {
748 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
749 if (count ($arr) == 1)
753 # First, do some preliminary work. This may shift some apostrophes from
754 # being mark-up to being text. It also counts the number of occurrences
755 # of bold and italics mark-ups.
763 # If there are ever four apostrophes, assume the first is supposed to
764 # be text, and the remaining three constitute mark-up for bold text.
765 if (strlen ($arr[$i]) == 4)
770 # If there are more than 5 apostrophes in a row, assume they're all
771 # text except for the last 5.
772 else if (strlen ($arr[$i]) > 5)
774 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
777 # Count the number of occurrences of bold and italics mark-ups.
778 # We are not counting sequences of five apostrophes.
779 if (strlen ($arr[$i]) == 2) $numitalics++
; else
780 if (strlen ($arr[$i]) == 3) $numbold++
; else
781 if (strlen ($arr[$i]) == 5) { $numitalics++
; $numbold++
; }
786 # If there is an odd number of both bold and italics, it is likely
787 # that one of the bold ones was meant to be an apostrophe followed
788 # by italics. Which one we cannot know for certain, but it is more
789 # likely to be one that has a single-letter word before it.
790 if (($numbold %
2 == 1) && ($numitalics %
2 == 1))
793 $firstsingleletterword = -1;
794 $firstmultiletterword = -1;
798 if (($i %
2 == 1) and (strlen ($r) == 3))
800 $x1 = substr ($arr[$i-1], -1);
801 $x2 = substr ($arr[$i-1], -2, 1);
803 if ($firstspace == -1) $firstspace = $i;
804 } else if ($x2 == ' ') {
805 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
807 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
813 # If there is a single-letter word, use it!
814 if ($firstsingleletterword > -1)
816 $arr [ $firstsingleletterword ] = "''";
817 $arr [ $firstsingleletterword-1 ] .= "'";
819 # If not, but there's a multi-letter word, use that one.
820 else if ($firstmultiletterword > -1)
822 $arr [ $firstmultiletterword ] = "''";
823 $arr [ $firstmultiletterword-1 ] .= "'";
825 # ... otherwise use the first one that has neither.
826 # (notice that it is possible for all three to be -1 if, for example,
827 # there is only one pentuple-apostrophe in the line)
828 else if ($firstspace > -1)
830 $arr [ $firstspace ] = "''";
831 $arr [ $firstspace-1 ] .= "'";
835 # Now let's actually convert our apostrophic mush to HTML!
844 if ($state == 'both')
851 if (strlen ($r) == 2)
854 { $output .= '</i>'; $state = ''; }
855 else if ($state == 'bi')
856 { $output .= '</i>'; $state = 'b'; }
857 else if ($state == 'ib')
858 { $output .= '</b></i><b>'; $state = 'b'; }
859 else if ($state == 'both')
860 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
861 else # $state can be 'b' or ''
862 { $output .= '<i>'; $state .= 'i'; }
864 else if (strlen ($r) == 3)
867 { $output .= '</b>'; $state = ''; }
868 else if ($state == 'bi')
869 { $output .= '</i></b><i>'; $state = 'i'; }
870 else if ($state == 'ib')
871 { $output .= '</b>'; $state = 'i'; }
872 else if ($state == 'both')
873 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
874 else # $state can be 'i' or ''
875 { $output .= '<b>'; $state .= 'b'; }
877 else if (strlen ($r) == 5)
880 { $output .= '</b><i>'; $state = 'i'; }
881 else if ($state == 'i')
882 { $output .= '</i><b>'; $state = 'b'; }
883 else if ($state == 'bi')
884 { $output .= '</i></b>'; $state = ''; }
885 else if ($state == 'ib')
886 { $output .= '</b></i>'; $state = ''; }
887 else if ($state == 'both')
888 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
889 else # ($state == '')
890 { $buffer = ''; $state = 'both'; }
895 # Now close all remaining tags. Notice that the order is important.
896 if ($state == 'b' ||
$state == 'ib')
898 if ($state == 'i' ||
$state == 'bi' ||
$state == 'ib')
902 if ($state == 'both')
903 $output .= '<b><i>'.$buffer.'</i></b>';
909 * Replace external links
911 * Note: we have to do external links before the internal ones,
912 * and otherwise take great care in the order of things here, so
913 * that we don't end up interpreting some URLs twice.
917 function replaceExternalLinks( $text ) {
918 $fname = 'Parser::replaceExternalLinks';
919 wfProfileIn( $fname );
921 $sk =& $this->mOptions
->getSkin();
922 $linktrail = wfMsg('linktrail');
923 $bits = preg_split( EXT_LINK_BRACKETED
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
925 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
928 while ( $i<count( $bits ) ) {
930 $protocol = $bits[$i++
];
932 $trail = $bits[$i++
];
934 # If the link text is an image URL, replace it with an <img> tag
935 # This happened by accident in the original parser, but some people used it extensively
936 $img = $this->maybeMakeImageLink( $text );
937 if ( $img !== false ) {
943 # No link text, e.g. [http://domain.tld/some.link]
945 # Autonumber if allowed
946 if ( strpos( HTTP_PROTOCOLS
, $protocol ) !== false ) {
947 $text = '[' . ++
$this->mAutonumber
. ']';
949 # Otherwise just use the URL
950 $text = htmlspecialchars( $url );
953 # Have link text, e.g. [http://domain.tld/some.link text]s
955 if ( preg_match( $linktrail, $trail, $m2 ) ) {
961 $encUrl = htmlspecialchars( $url );
962 # Bit in parentheses showing the URL for the printable version
963 if( $url == $text ||
preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
966 # Expand the URL for printable version
967 if ( ! $sk->suppressUrlExpansion() ) {
968 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
974 # Process the trail (i.e. everything after this link up until start of the next link),
975 # replacing any non-bracketed links
976 $trail = $this->replaceFreeExternalLinks( $trail );
978 $la = $sk->getExternalLinkAttributes( $url, $text );
980 # Use the encoded URL
981 # This means that users can paste URLs directly into the text
982 # Funny characters like ö aren't valid in URLs anyway
983 # This was changed in August 2004
984 $s .= "<a href=\"{$url}\"{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
987 wfProfileOut( $fname );
992 * Replace anything that looks like a URL with a link
995 function replaceFreeExternalLinks( $text ) {
996 $bits = preg_split( '/((?:'.URL_PROTOCOLS
.'):)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
997 $s = array_shift( $bits );
1000 $sk =& $this->mOptions
->getSkin();
1002 while ( $i < count( $bits ) ){
1003 $protocol = $bits[$i++
];
1004 $remainder = $bits[$i++
];
1006 if ( preg_match( '/^('.EXT_LINK_URL_CLASS
.'+)(.*)$/s', $remainder, $m ) ) {
1007 # Found some characters after the protocol that look promising
1008 $url = $protocol . $m[1];
1011 # Move trailing punctuation to $trail
1013 # If there is no left bracket, then consider right brackets fair game too
1014 if ( strpos( $url, '(' ) === false ) {
1018 $numSepChars = strspn( strrev( $url ), $sep );
1019 if ( $numSepChars ) {
1020 $trail = substr( $url, -$numSepChars ) . $trail;
1021 $url = substr( $url, 0, -$numSepChars );
1024 # Replace & from obsolete syntax with &
1025 $url = str_replace( '&', '&', $url );
1027 # Is this an external image?
1028 $text = $this->maybeMakeImageLink( $url );
1029 if ( $text === false ) {
1030 # Not an image, make a link
1031 $text = $sk->makeExternalLink( $url, $url );
1033 $s .= $text . $trail;
1035 $s .= $protocol . $remainder;
1042 * make an image if it's allowed
1045 function maybeMakeImageLink( $url ) {
1046 $sk =& $this->mOptions
->getSkin();
1048 if ( $this->mOptions
->getAllowExternalImages() ) {
1049 if ( preg_match( EXT_IMAGE_REGEX
, $url ) ) {
1051 $text = $sk->makeImage( htmlspecialchars( $url ) );
1058 * Process [[ ]] wikilinks
1062 function replaceInternalLinks( $s ) {
1063 global $wgLang, $wgLinkCache;
1064 global $wgNamespacesWithSubpages;
1065 static $fname = 'Parser::replaceInternalLinks' ;
1066 wfProfileIn( $fname );
1068 wfProfileIn( $fname.'-setup' );
1070 # the % is needed to support urlencoded titles as well
1071 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1072 $sk =& $this->mOptions
->getSkin();
1074 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
1076 $a = explode( '[[', ' ' . $s );
1077 $s = array_shift( $a );
1078 $s = substr( $s, 1 );
1080 # Match a link having the form [[namespace:link|alternate]]trail
1082 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1083 # Match the end of a line for a word that's not followed by whitespace,
1084 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1085 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1087 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1088 # Special and Media are pseudo-namespaces; no pages actually exist in them
1090 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
1092 if ( $useLinkPrefixExtension ) {
1093 if ( preg_match( $e2, $s, $m ) ) {
1094 $first_prefix = $m[2];
1097 $first_prefix = false;
1103 wfProfileOut( $fname.'-setup' );
1105 # start procedeeding each line
1106 foreach ( $a as $line ) {
1107 wfProfileIn( $fname.'-prefixhandling' );
1108 if ( $useLinkPrefixExtension ) {
1109 if ( preg_match( $e2, $s, $m ) ) {
1117 $prefix = $first_prefix;
1118 $first_prefix = false;
1121 wfProfileOut( $fname.'-prefixhandling' );
1123 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1125 # fix up urlencoded title texts
1126 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1128 } else { # Invalid form; output directly
1129 $s .= $prefix . '[[' . $line ;
1135 # :Foobar -- override special treatment of prefix (images, language links)
1136 # /Foobar -- convert to CurrentPage/Foobar
1137 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1139 # Look at the first character
1140 $c = substr($m[1],0,1);
1141 $noforce = ($c != ':');
1145 # / at end means we don't want the slash to be shown
1146 if(substr($m[1],-1,1)=='/') {
1147 $m[1]=substr($m[1],1,strlen($m[1])-2);
1150 $noslash=substr($m[1],1);
1153 # Some namespaces don't allow subpages
1154 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) {
1155 # subpages allowed here
1156 $link = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1159 } # this might be changed for ugliness reasons
1161 # no subpage allowed, use standard link
1165 } elseif( $noforce ) { # no subpage
1168 # We don't want to keep the first character
1169 $link = substr( $m[1], 1 );
1172 $wasblank = ( '' == $text );
1173 if( $wasblank ) $text = $link;
1175 $nt = Title
::newFromText( $link );
1177 $s .= $prefix . '[[' . $line;
1181 $ns = $nt->getNamespace();
1182 $iw = $nt->getInterWiki();
1184 # Link not escaped by : , create the various objects
1188 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1189 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1190 $tmp = $prefix . $trail ;
1191 $s .= (trim($tmp) == '')?
'': $tmp;
1195 if ( $ns == NS_IMAGE
) {
1196 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1197 $wgLinkCache->addImageLinkObj( $nt );
1201 if ( $ns == NS_CATEGORY
) {
1202 $t = $nt->getText() ;
1203 $nnt = Title
::newFromText ( Namespace::getCanonicalName(NS_CATEGORY
).':'.$t ) ;
1205 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1206 $pPLC=$sk->postParseLinkColour();
1207 $sk->postParseLinkColour( false );
1208 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1209 $sk->postParseLinkColour( $pPLC );
1210 $wgLinkCache->resume();
1213 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
1214 $sortkey = $this->mTitle
->getText();
1216 $sortkey = $this->mTitle
->getPrefixedText();
1221 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1222 $this->mOutput
->mCategoryLinks
[] = $t ;
1223 $s .= $prefix . $trail ;
1228 if( ( $nt->getPrefixedText() === $this->mTitle
->getPrefixedText() ) &&
1229 ( strpos( $link, '#' ) === FALSE ) ) {
1230 # Self-links are handled specially; generally de-link and change to bold.
1231 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1235 if( $ns == NS_MEDIA
) {
1236 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1237 $wgLinkCache->addImageLinkObj( $nt );
1239 } elseif( $ns == NS_SPECIAL
) {
1240 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1243 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1245 wfProfileOut( $fname );
1250 * Used by doBlockLevels()
1253 /* private */ function closeParagraph() {
1255 if ( '' != $this->mLastSection
) {
1256 $result = '</' . $this->mLastSection
. ">\n";
1258 $this->mInPre
= false;
1259 $this->mLastSection
= '';
1262 # getCommon() returns the length of the longest common substring
1263 # of both arguments, starting at the beginning of both.
1265 /* private */ function getCommon( $st1, $st2 ) {
1266 $fl = strlen( $st1 );
1267 $shorter = strlen( $st2 );
1268 if ( $fl < $shorter ) { $shorter = $fl; }
1270 for ( $i = 0; $i < $shorter; ++
$i ) {
1271 if ( $st1{$i} != $st2{$i} ) { break; }
1275 # These next three functions open, continue, and close the list
1276 # element appropriate to the prefix character passed into them.
1278 /* private */ function openList( $char ) {
1279 $result = $this->closeParagraph();
1281 if ( '*' == $char ) { $result .= '<ul><li>'; }
1282 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1283 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1284 else if ( ';' == $char ) {
1285 $result .= '<dl><dt>';
1286 $this->mDTopen
= true;
1288 else { $result = '<!-- ERR 1 -->'; }
1293 /* private */ function nextItem( $char ) {
1294 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1295 else if ( ':' == $char ||
';' == $char ) {
1297 if ( $this->mDTopen
) { $close = '</dt>'; }
1298 if ( ';' == $char ) {
1299 $this->mDTopen
= true;
1300 return $close . '<dt>';
1302 $this->mDTopen
= false;
1303 return $close . '<dd>';
1306 return '<!-- ERR 2 -->';
1309 /* private */ function closeList( $char ) {
1310 if ( '*' == $char ) { $text = '</li></ul>'; }
1311 else if ( '#' == $char ) { $text = '</li></ol>'; }
1312 else if ( ':' == $char ) {
1313 if ( $this->mDTopen
) {
1314 $this->mDTopen
= false;
1315 $text = '</dt></dl>';
1317 $text = '</dd></dl>';
1320 else { return '<!-- ERR 3 -->'; }
1326 * Make lists from lines starting with ':', '*', '#', etc.
1329 * @return string the lists rendered as HTML
1331 function doBlockLevels( $text, $linestart ) {
1332 $fname = 'Parser::doBlockLevels';
1333 wfProfileIn( $fname );
1335 # Parsing through the text line by line. The main thing
1336 # happening here is handling of block-level elements p, pre,
1337 # and making lists from lines starting with * # : etc.
1339 $textLines = explode( "\n", $text );
1341 $lastPrefix = $output = $lastLine = '';
1342 $this->mDTopen
= $inBlockElem = false;
1344 $paragraphStack = false;
1346 if ( !$linestart ) {
1347 $output .= array_shift( $textLines );
1349 foreach ( $textLines as $oLine ) {
1350 $lastPrefixLength = strlen( $lastPrefix );
1351 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1352 $preOpenMatch = preg_match('/<pre/i', $oLine );
1353 if ( !$this->mInPre
) {
1354 # Multiple prefixes may abut each other for nested lists.
1355 $prefixLength = strspn( $oLine, '*#:;' );
1356 $pref = substr( $oLine, 0, $prefixLength );
1359 $pref2 = str_replace( ';', ':', $pref );
1360 $t = substr( $oLine, $prefixLength );
1361 $this->mInPre
= !empty($preOpenMatch);
1363 # Don't interpret any other prefixes in preformatted text
1365 $pref = $pref2 = '';
1370 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1371 # Same as the last item, so no need to deal with nesting or opening stuff
1372 $output .= $this->nextItem( substr( $pref, -1 ) );
1373 $paragraphStack = false;
1375 if ( substr( $pref, -1 ) == ';') {
1376 # The one nasty exception: definition lists work like this:
1377 # ; title : definition text
1378 # So we check for : in the remainder text to split up the
1379 # title and definition, without b0rking links.
1380 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1381 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1383 $output .= $term . $this->nextItem( ':' );
1387 } elseif( $prefixLength ||
$lastPrefixLength ) {
1388 # Either open or close a level...
1389 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1390 $paragraphStack = false;
1392 while( $commonPrefixLength < $lastPrefixLength ) {
1393 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1394 --$lastPrefixLength;
1396 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1397 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1399 while ( $prefixLength > $commonPrefixLength ) {
1400 $char = substr( $pref, $commonPrefixLength, 1 );
1401 $output .= $this->openList( $char );
1403 if ( ';' == $char ) {
1404 # FIXME: This is dupe of code above
1405 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1407 $output .= $term . $this->nextItem( ':' );
1411 ++
$commonPrefixLength;
1413 $lastPrefix = $pref2;
1415 if( 0 == $prefixLength ) {
1416 # No prefix (not in list)--go to paragraph mode
1417 $uniq_prefix = UNIQ_PREFIX
;
1418 // XXX: use a stack for nestable elements like span, table and div
1419 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1420 $closematch = preg_match(
1421 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1422 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1423 if ( $openmatch or $closematch ) {
1424 $paragraphStack = false;
1425 $output .= $this->closeParagraph();
1426 if($preOpenMatch and !$preCloseMatch) {
1427 $this->mInPre
= true;
1429 if ( $closematch ) {
1430 $inBlockElem = false;
1432 $inBlockElem = true;
1434 } else if ( !$inBlockElem && !$this->mInPre
) {
1435 if ( ' ' == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1437 if ($this->mLastSection
!= 'pre') {
1438 $paragraphStack = false;
1439 $output .= $this->closeParagraph().'<pre>';
1440 $this->mLastSection
= 'pre';
1442 $t = substr( $t, 1 );
1445 if ( '' == trim($t) ) {
1446 if ( $paragraphStack ) {
1447 $output .= $paragraphStack.'<br />';
1448 $paragraphStack = false;
1449 $this->mLastSection
= 'p';
1451 if ($this->mLastSection
!= 'p' ) {
1452 $output .= $this->closeParagraph();
1453 $this->mLastSection
= '';
1454 $paragraphStack = '<p>';
1456 $paragraphStack = '</p><p>';
1460 if ( $paragraphStack ) {
1461 $output .= $paragraphStack;
1462 $paragraphStack = false;
1463 $this->mLastSection
= 'p';
1464 } else if ($this->mLastSection
!= 'p') {
1465 $output .= $this->closeParagraph().'<p>';
1466 $this->mLastSection
= 'p';
1472 if ($paragraphStack === false) {
1476 while ( $prefixLength ) {
1477 $output .= $this->closeList( $pref2{$prefixLength-1} );
1480 if ( '' != $this->mLastSection
) {
1481 $output .= '</' . $this->mLastSection
. '>';
1482 $this->mLastSection
= '';
1485 wfProfileOut( $fname );
1490 * Return value of a magic variable (like PAGENAME)
1494 function getVariableValue( $index ) {
1495 global $wgLang, $wgSitename, $wgServer;
1498 case MAG_CURRENTMONTH
:
1499 return $wgLang->formatNum( date( 'm' ) );
1500 case MAG_CURRENTMONTHNAME
:
1501 return $wgLang->getMonthName( date('n') );
1502 case MAG_CURRENTMONTHNAMEGEN
:
1503 return $wgLang->getMonthNameGen( date('n') );
1504 case MAG_CURRENTDAY
:
1505 return $wgLang->formatNum( date('j') );
1507 return $this->mTitle
->getText();
1509 return $this->mTitle
->getPartialURL();
1511 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1512 return $wgLang->getNsText($this->mTitle
->getNamespace()); # Patch by Dori
1513 case MAG_CURRENTDAYNAME
:
1514 return $wgLang->getWeekdayName( date('w')+
1 );
1515 case MAG_CURRENTYEAR
:
1516 return $wgLang->formatNum( date( 'Y' ) );
1517 case MAG_CURRENTTIME
:
1518 return $wgLang->time( wfTimestampNow(), false );
1519 case MAG_NUMBEROFARTICLES
:
1520 return $wgLang->formatNum( wfNumberOfArticles() );
1531 * initialise the magic variables (like CURRENTMONTHNAME)
1535 function initialiseVariables() {
1536 global $wgVariableIDs;
1537 $this->mVariables
= array();
1538 foreach ( $wgVariableIDs as $id ) {
1539 $mw =& MagicWord
::get( $id );
1540 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1545 * Replace magic variables, templates, and template arguments
1546 * with the appropriate text. Templates are substituted recursively,
1547 * taking care to avoid infinite loops.
1549 * Note that the substitution depends on value of $mOutputType:
1550 * OT_WIKI: only {{subst:}} templates
1551 * OT_MSG: only magic variables
1552 * OT_HTML: all templates and magic variables
1554 * @param string $tex The text to transform
1555 * @param array $args Key-value pairs representing template parameters to substitute
1558 function replaceVariables( $text, $args = array() ) {
1559 global $wgLang, $wgScript, $wgArticlePath;
1561 # Prevent too big inclusions
1562 if(strlen($text)> MAX_INCLUDE_SIZE
)
1565 $fname = 'Parser::replaceVariables';
1566 wfProfileIn( $fname );
1568 $titleChars = Title
::legalChars();
1570 # This function is called recursively. To keep track of arguments we need a stack:
1571 array_push( $this->mArgStack
, $args );
1573 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1574 $GLOBALS['wgCurParser'] =& $this;
1576 # Variable substitution
1577 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", 'wfVariableSubstitution', $text );
1579 if ( $this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) {
1580 # Argument substitution
1581 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1583 # Template substitution
1584 $regex = '/{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1585 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1587 array_pop( $this->mArgStack
);
1589 wfProfileOut( $fname );
1594 * Replace magic variables
1597 function variableSubstitution( $matches ) {
1598 if ( !$this->mVariables
) {
1599 $this->initialiseVariables();
1602 if ( $this->mOutputType
== OT_WIKI
) {
1603 # Do only magic variables prefixed by SUBST
1604 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1605 if (!$mwSubst->matchStartAndRemove( $matches[1] ))
1607 # Note that if we don't substitute the variable below,
1608 # we don't remove the {{subst:}} magic word, in case
1609 # it is a template rather than a magic variable.
1611 if ( !$skip && array_key_exists( $matches[1], $this->mVariables
) ) {
1612 $text = $this->mVariables
[$matches[1]];
1613 $this->mOutput
->mContainsOldMagic
= true;
1615 $text = $matches[0];
1620 # Split template arguments
1621 function getTemplateArgs( $argsString ) {
1622 if ( $argsString === '' ) {
1626 $args = explode( '|', substr( $argsString, 1 ) );
1628 # If any of the arguments contains a '[[' but no ']]', it needs to be
1629 # merged with the next arg because the '|' character between belongs
1630 # to the link syntax and not the template parameter syntax.
1631 $argc = count($args);
1633 for ( $i = 0; $i < $argc-1; $i++
) {
1634 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1635 $args[$i] .= '|'.$args[$i+
1];
1636 array_splice($args, $i+
1, 1);
1646 * Return the text of a template, after recursively
1647 * replacing any variables or templates within the template.
1649 * @param array $matches The parts of the template
1650 * $matches[1]: the title, i.e. the part before the |
1651 * $matches[2]: the parameters (including a leading |), if any
1652 * @return string the text of the template
1655 function braceSubstitution( $matches ) {
1656 global $wgLinkCache, $wgLang;
1657 $fname = 'Parser::braceSubstitution';
1661 $itcamefromthedatabase = false;
1665 # $part1 is the bit before the first |, and must contain only title characters
1666 # $args is a list of arguments, starting from index 0, not including $part1
1668 $part1 = $matches[1];
1669 # If the second subpattern matched anything, it will start with |
1671 $args = $this->getTemplateArgs($matches[2]);
1672 $argc = count( $args );
1675 if ( strpos( $matches[0], '{{{' ) !== false ) {
1676 $text = $matches[0];
1683 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1684 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType
== OT_WIKI
) ) {
1685 # One of two possibilities is true:
1686 # 1) Found SUBST but not in the PST phase
1687 # 2) Didn't find SUBST and in the PST phase
1688 # In either case, return without further processing
1689 $text = $matches[0];
1695 # MSG, MSGNW and INT
1698 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1699 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1702 # Remove obsolete MSG:
1703 $mwMsg =& MagicWord
::get( MAG_MSG
);
1704 $mwMsg->matchStartAndRemove( $part1 );
1707 # Check if it is an internal message
1708 $mwInt =& MagicWord
::get( MAG_INT
);
1709 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1710 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1711 $text = wfMsgReal( $part1, $args, true );
1719 # Check for NS: (namespace expansion)
1720 $mwNs = MagicWord
::get( MAG_NS
);
1721 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1722 if ( intval( $part1 ) ) {
1723 $text = $wgLang->getNsText( intval( $part1 ) );
1726 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1727 if ( !is_null( $index ) ) {
1728 $text = $wgLang->getNsText( $index );
1735 # LOCALURL and LOCALURLE
1737 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1738 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1740 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1741 $func = 'getLocalURL';
1742 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1743 $func = 'escapeLocalURL';
1748 if ( $func !== '' ) {
1749 $title = Title
::newFromText( $part1 );
1750 if ( !is_null( $title ) ) {
1752 $text = $title->$func( $args[0] );
1754 $text = $title->$func();
1762 if ( !$found && $argc == 1 ) {
1763 $mwGrammar =& MagicWord
::get( MAG_GRAMMAR
);
1764 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1765 $text = $wgLang->convertGrammar( $args[0], $part1 );
1770 # Template table test
1772 # Did we encounter this template already? If yes, it is in the cache
1773 # and we need to check for loops.
1774 if ( isset( $this->mTemplates
[$part1] ) ) {
1775 # Infinite loop test
1776 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1780 # set $text to cached message.
1781 $text = $this->mTemplates
[$part1];
1785 # Load from database
1787 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1788 if ( !is_null( $title ) && !$title->isExternal() ) {
1789 # Check for excessive inclusion
1790 $dbk = $title->getPrefixedDBkey();
1791 if ( $this->incrementIncludeCount( $dbk ) ) {
1792 # This should never be reached.
1793 $article = new Article( $title );
1794 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1795 if ( $articleContent !== false ) {
1797 $text = $articleContent;
1798 $itcamefromthedatabase = true;
1802 # If the title is valid but undisplayable, make a link to it
1803 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1804 $text = '[['.$title->getPrefixedText().']]';
1808 # Template cache array insertion
1809 $this->mTemplates
[$part1] = $text;
1813 # Recursive parsing, escaping and link table handling
1814 # Only for HTML output
1815 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1816 $text = wfEscapeWikiText( $text );
1817 } elseif ( ($this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) && $found && !$noparse) {
1818 # Clean up argument array
1819 $assocArgs = array();
1821 foreach( $args as $arg ) {
1822 $eqpos = strpos( $arg, '=' );
1823 if ( $eqpos === false ) {
1824 $assocArgs[$index++
] = $arg;
1826 $name = trim( substr( $arg, 0, $eqpos ) );
1827 $value = trim( substr( $arg, $eqpos+
1 ) );
1828 if ( $value === false ) {
1831 if ( $name !== false ) {
1832 $assocArgs[$name] = $value;
1837 # Add a new element to the templace recursion path
1838 $this->mTemplatePath
[$part1] = 1;
1840 $text = $this->strip( $text, $this->mStripState
);
1841 $text = $this->removeHTMLtags( $text );
1842 $text = $this->replaceVariables( $text, $assocArgs );
1844 # Resume the link cache and register the inclusion as a link
1845 if ( $this->mOutputType
== OT_HTML
&& !is_null( $title ) ) {
1846 $wgLinkCache->addLinkObj( $title );
1850 # Empties the template path
1851 $this->mTemplatePath
= array();
1855 # replace ==section headers==
1856 # XXX this needs to go away once we have a better parser.
1857 if ( $this->mOutputType
!= OT_WIKI
&& $itcamefromthedatabase ) {
1858 if( !is_null( $title ) )
1859 $encodedname = base64_encode($title->getPrefixedDBkey());
1861 $encodedname = base64_encode("");
1862 $matches = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
1863 PREG_SPLIT_DELIM_CAPTURE
);
1866 for( $i = 0; $i < count($matches); $i +
= 2 ) {
1867 $text .= $matches[$i];
1868 if (!isset($matches[$i +
1]) ||
$matches[$i +
1] == "") continue;
1869 $hl = $matches[$i +
1];
1870 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
1874 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
1875 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
1876 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
1886 * Triple brace replacement -- used for template arguments
1889 function argSubstitution( $matches ) {
1890 $arg = trim( $matches[1] );
1891 $text = $matches[0];
1892 $inputArgs = end( $this->mArgStack
);
1894 if ( array_key_exists( $arg, $inputArgs ) ) {
1895 $text = $inputArgs[$arg];
1902 * Returns true if the function is allowed to include this entity
1905 function incrementIncludeCount( $dbk ) {
1906 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1907 $this->mIncludeCount
[$dbk] = 0;
1909 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1918 * Cleans up HTML, removes dangerous tags and attributes, and
1919 * removes HTML comments
1922 function removeHTMLtags( $text ) {
1923 global $wgUseTidy, $wgUserHtml;
1924 $fname = 'Parser::removeHTMLtags';
1925 wfProfileIn( $fname );
1928 $htmlpairs = array( # Tags that must be closed
1929 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1930 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1931 'strike', 'strong', 'tt', 'var', 'div', 'center',
1932 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1933 'ruby', 'rt' , 'rb' , 'rp', 'p'
1935 $htmlsingle = array(
1936 'br', 'hr', 'li', 'dt', 'dd'
1938 $htmlnest = array( # Tags that can be nested--??
1939 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1940 'dl', 'font', 'big', 'small', 'sub', 'sup'
1942 $tabletags = array( # Can only appear inside table
1946 $htmlpairs = array();
1947 $htmlsingle = array();
1948 $htmlnest = array();
1949 $tabletags = array();
1952 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1953 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1955 $htmlattrs = $this->getHTMLattrs () ;
1957 # Remove HTML comments
1958 $text = $this->removeHTMLcomments( $text );
1960 $bits = explode( '<', $text );
1961 $text = array_shift( $bits );
1963 $tagstack = array(); $tablestack = array();
1964 foreach ( $bits as $x ) {
1965 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1966 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1968 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1969 error_reporting( $prev );
1972 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1976 if ( ! in_array( $t, $htmlsingle ) &&
1977 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
1978 @array_push
( $tagstack, $ot );
1981 if ( $t == 'table' ) {
1982 $tagstack = array_pop( $tablestack );
1987 # Keep track for later
1988 if ( in_array( $t, $tabletags ) &&
1989 ! in_array( 'table', $tagstack ) ) {
1991 } else if ( in_array( $t, $tagstack ) &&
1992 ! in_array ( $t , $htmlnest ) ) {
1994 } else if ( ! in_array( $t, $htmlsingle ) ) {
1995 if ( $t == 'table' ) {
1996 array_push( $tablestack, $tagstack );
1997 $tagstack = array();
1999 array_push( $tagstack, $t );
2001 # Strip non-approved attributes from the tag
2002 $newparams = $this->fixTagAttributes($params);
2006 $rest = str_replace( '>', '>', $rest );
2007 $text .= "<$slash$t $newparams$brace$rest";
2011 $text .= '<' . str_replace( '>', '>', $x);
2013 # Close off any remaining tags
2014 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2016 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2019 # this might be possible using tidy itself
2020 foreach ( $bits as $x ) {
2021 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2023 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2024 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2025 $newparams = $this->fixTagAttributes($params);
2026 $rest = str_replace( '>', '>', $rest );
2027 $text .= "<$slash$t $newparams$brace$rest";
2029 $text .= '<' . str_replace( '>', '>', $x);
2033 wfProfileOut( $fname );
2038 * Remove '<!--', '-->', and everything between.
2039 * To avoid leaving blank lines, when a comment is both preceded
2040 * and followed by a newline (ignoring spaces), trim leading and
2041 * trailing spaces and one of the newlines.
2045 function removeHTMLcomments( $text ) {
2046 $fname='Parser::removeHTMLcomments';
2047 wfProfileIn( $fname );
2048 while (($start = strpos($text, '<!--')) !== false) {
2049 $end = strpos($text, '-->', $start +
4);
2050 if ($end === false) {
2051 # Unterminated comment; bail out
2057 # Trim space and newline if the comment is both
2058 # preceded and followed by a newline
2059 $spaceStart = max($start - 1, 0);
2060 $spaceLen = $end - $spaceStart;
2061 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2065 while (substr($text, $spaceStart +
$spaceLen, 1) === ' ')
2067 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart +
$spaceLen, 1) === "\n") {
2068 # Remove the comment, leading and trailing
2069 # spaces, and leave only one newline.
2070 $text = substr_replace($text, "\n", $spaceStart, $spaceLen +
1);
2073 # Remove just the comment.
2074 $text = substr_replace($text, '', $start, $end - $start);
2077 wfProfileOut( $fname );
2082 * This function accomplishes several tasks:
2083 * 1) Auto-number headings if that option is enabled
2084 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2085 * 3) Add a Table of contents on the top for users who have enabled the option
2086 * 4) Auto-anchor headings
2088 * It loops through all headlines, collects the necessary data, then splits up the
2089 * string and re-inserts the newly formatted headlines.
2092 /* private */ function formatHeadings( $text, $isMain=true ) {
2093 global $wgInputEncoding, $wgMaxTocLevel, $wgLang, $wgLinkHolders;
2095 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
2096 $doShowToc = $this->mOptions
->getShowToc();
2097 $forceTocHere = false;
2098 if( !$this->mTitle
->userCanEdit() ) {
2100 $rightClickHack = 0;
2102 $showEditLink = $this->mOptions
->getEditSection();
2103 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
2106 # Inhibit editsection links if requested in the page
2107 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
2108 if( $esw->matchAndRemove( $text ) ) {
2111 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2113 $mw =& MagicWord
::get( MAG_NOTOC
);
2114 if( $mw->matchAndRemove( $text ) ) {
2118 # never add the TOC to the Main Page. This is an entry page that should not
2119 # be more than 1-2 screens large anyway
2120 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
2124 # Get all headlines for numbering them and adding funky stuff like [edit]
2125 # links - this is for later, but we need the number of headlines right now
2126 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2128 # if there are fewer than 4 headlines in the article, do not show TOC
2129 if( $numMatches < 4 ) {
2133 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2134 # override above conditions and always show TOC at that place
2135 $mw =& MagicWord
::get( MAG_TOC
);
2136 if ($mw->match( $text ) ) {
2138 $forceTocHere = true;
2140 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2141 # override above conditions and always show TOC above first header
2142 $mw =& MagicWord
::get( MAG_FORCETOC
);
2143 if ($mw->matchAndRemove( $text ) ) {
2150 # We need this to perform operations on the HTML
2151 $sk =& $this->mOptions
->getSkin();
2155 $sectionCount = 0; # headlineCount excluding template sections
2157 # Ugh .. the TOC should have neat indentation levels which can be
2158 # passed to the skin functions. These are determined here
2163 $sublevelCount = array();
2166 foreach( $matches[3] as $headline ) {
2168 $templatetitle = "";
2169 $templatesection = 0;
2171 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2173 $templatetitle = base64_decode($mat[1]);
2174 $templatesection = 1 +
(int)base64_decode($mat[2]);
2175 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2180 $prevlevel = $level;
2182 $level = $matches[1][$headlineCount];
2183 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
2184 # reset when we enter a new level
2185 $sublevelCount[$level] = 0;
2186 $toc .= $sk->tocIndent( $level - $prevlevel );
2187 $toclevel +
= $level - $prevlevel;
2189 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
2190 # reset when we step back a level
2191 $sublevelCount[$level+
1]=0;
2192 $toc .= $sk->tocUnindent( $prevlevel - $level );
2193 $toclevel -= $prevlevel - $level;
2195 # count number of headlines for each level
2196 @$sublevelCount[$level]++
;
2197 if( $doNumberHeadings ||
$doShowToc ) {
2199 for( $i = 1; $i <= $level; $i++
) {
2200 if( !empty( $sublevelCount[$i] ) ) {
2204 $numbering .= $wgLang->formatNum( $sublevelCount[$i] );
2210 # The canonized header is a version of the header text safe to use for links
2211 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2212 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
2213 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
2215 # Remove link placeholders by the link text.
2216 # <!--LINK number-->
2218 # link text with suffix
2219 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2220 "\$wgLinkHolders['texts'][\$1]",
2221 $canonized_headline );
2224 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2225 $tocline = trim( $canonized_headline );
2226 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
2227 $replacearray = array(
2231 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2232 $refer[$headlineCount] = $canonized_headline;
2234 # count how many in assoc. array so we can track dupes in anchors
2235 @$refers[$canonized_headline]++
;
2236 $refcount[$headlineCount]=$refers[$canonized_headline];
2238 # Prepend the number to the heading text
2240 if( $doNumberHeadings ||
$doShowToc ) {
2241 $tocline = $numbering . ' ' . $tocline;
2243 # Don't number the heading if it is the only one (looks silly)
2244 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2245 # the two are different if the line contains a link
2246 $headline=$numbering . ' ' . $headline;
2250 # Create the anchor for linking from the TOC to the section
2251 $anchor = $canonized_headline;
2252 if($refcount[$headlineCount] > 1 ) {
2253 $anchor .= '_' . $refcount[$headlineCount];
2255 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2256 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2258 if( $showEditLink && ( !$istemplate ||
$templatetitle !== "" ) ) {
2259 if ( empty( $head[$headlineCount] ) ) {
2260 $head[$headlineCount] = '';
2263 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2265 $head[$headlineCount] .= $sk->editSectionLink($sectionCount+
1);
2268 # Add the edit section span
2269 if( $rightClickHack ) {
2271 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2273 $headline = $sk->editSectionScript($sectionCount+
1,$headline);
2276 # give headline the correct <h#> tag
2277 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2285 $toclines = $headlineCount;
2286 $toc .= $sk->tocUnindent( $toclevel );
2287 $toc = $sk->tocTable( $toc );
2290 # split up and insert constructed headlines
2292 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2295 foreach( $blocks as $block ) {
2296 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2297 # This is the [edit] link that appears for the top block of text when
2298 # section editing is enabled
2300 # Disabled because it broke block formatting
2301 # For example, a bullet point in the top line
2302 # $full .= $sk->editSectionLink(0);
2305 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2306 # Top anchor now in skin
2310 if( !empty( $head[$i] ) ) {
2316 $mw =& MagicWord
::get( MAG_TOC
);
2317 return $mw->replace( $toc, $full );
2324 * Return an HTML link for the "ISBN 123456" text
2327 function magicISBN( $text ) {
2329 $fname = 'Parser::magicISBN';
2330 wfProfileIn( $fname );
2332 $a = split( 'ISBN ', ' '.$text );
2333 if ( count ( $a ) < 2 ) {
2334 wfProfileOut( $fname );
2337 $text = substr( array_shift( $a ), 1);
2338 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2340 foreach ( $a as $x ) {
2341 $isbn = $blank = '' ;
2342 while ( ' ' == $x{0} ) {
2344 $x = substr( $x, 1 );
2346 if ( $x == '' ) { # blank isbn
2347 $text .= "ISBN $blank";
2350 while ( strstr( $valid, $x{0} ) != false ) {
2352 $x = substr( $x, 1 );
2354 $num = str_replace( '-', '', $isbn );
2355 $num = str_replace( ' ', '', $num );
2358 $text .= "ISBN $blank$x";
2360 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2361 $text .= '<a href="' .
2362 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2363 "\" class=\"internal\">ISBN $isbn</a>";
2367 wfProfileOut( $fname );
2372 * Return an HTML link for the "GEO ..." text
2375 function magicGEO( $text ) {
2376 global $wgLang, $wgUseGeoMode;
2377 $fname = 'Parser::magicGEO';
2378 wfProfileIn( $fname );
2380 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2381 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2382 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2383 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2384 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2385 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2387 $a = split( 'GEO ', ' '.$text );
2388 if ( count ( $a ) < 2 ) {
2389 wfProfileOut( $fname );
2392 $text = substr( array_shift( $a ), 1);
2393 $valid = '0123456789.+-:';
2395 foreach ( $a as $x ) {
2396 $geo = $blank = '' ;
2397 while ( ' ' == $x{0} ) {
2399 $x = substr( $x, 1 );
2401 while ( strstr( $valid, $x{0} ) != false ) {
2403 $x = substr( $x, 1 );
2405 $num = str_replace( '+', '', $geo );
2406 $num = str_replace( ' ', '', $num );
2408 if ( '' == $num ||
count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2409 $text .= "GEO $blank$x";
2411 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2412 $text .= '<a href="' .
2413 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2414 "\" class=\"internal\">GEO $geo</a>";
2418 wfProfileOut( $fname );
2423 * Return an HTML link for the "RFC 1234" text
2425 * @param string $text text to be processed
2427 function magicRFC( $text ) {
2430 $valid = '0123456789';
2433 $a = split( 'RFC ', ' '.$text );
2434 if ( count ( $a ) < 2 ) return $text;
2435 $text = substr( array_shift( $a ), 1);
2437 /* Check if RFC keyword is preceed by [[.
2438 * This test is made here cause of the array_shift above
2439 * that prevent the test to be done in the foreach.
2441 if(substr($text, -2) == '[[') { $internal = true; }
2443 foreach ( $a as $x ) {
2444 /* token might be empty if we have RFC RFC 1234 */
2450 $rfc = $blank = '' ;
2452 /** remove and save whitespaces in $blank */
2453 while ( $x{0} == ' ' ) {
2455 $x = substr( $x, 1 );
2458 /** remove and save the rfc number in $rfc */
2459 while ( strstr( $valid, $x{0} ) != false ) {
2461 $x = substr( $x, 1 );
2465 /* call back stripped spaces*/
2466 $text .= "RFC $blank$x";
2467 } elseif( $internal) {
2469 $text .= "RFC $rfc$x";
2471 /* build the external link*/
2472 $url = wfmsg( 'rfcurl' );
2473 $url = str_replace( '$1', $rfc, $url);
2474 $sk =& $this->mOptions
->getSkin();
2475 $la = $sk->getExternalLinkAttributes( $url, 'RFC '.$rfc );
2476 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2479 /* Check if the next RFC keyword is preceed by [[ */
2480 $internal = (substr($x,-2) == '[[');
2486 * Transform wiki markup when saving a page by doing \r\n -> \n
2487 * conversion, substitting signatures, {{subst:}} templates, etc.
2489 * @param string $text the text to transform
2490 * @param Title &$title the Title object for the current article
2491 * @param User &$user the User object describing the current user
2492 * @param ParserOptions $options parsing options
2493 * @param bool $clearState whether to clear the parser state first
2494 * @return string the altered wiki markup
2497 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2498 $this->mOptions
= $options;
2499 $this->mTitle
=& $title;
2500 $this->mOutputType
= OT_WIKI
;
2502 if ( $clearState ) {
2503 $this->clearState();
2506 $stripState = false;
2510 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2514 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2515 "/<br *?>/i" => "<br />",
2517 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2519 $text = $this->strip( $text, $stripState, false );
2520 $text = $this->pstPass2( $text, $user );
2521 $text = $this->unstrip( $text, $stripState );
2522 $text = $this->unstripNoWiki( $text, $stripState );
2527 * Pre-save transform helper function
2530 function pstPass2( $text, &$user ) {
2531 global $wgLang, $wgLocaltimezone, $wgCurParser;
2533 # Variable replacement
2534 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2535 $text = $this->replaceVariables( $text );
2539 $n = $user->getName();
2540 $k = $user->getOption( 'nickname' );
2541 if ( '' == $k ) { $k = $n; }
2542 if(isset($wgLocaltimezone)) {
2543 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2545 /* Note: this is an ugly timezone hack for the European wikis */
2546 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2547 ' (' . date( 'T' ) . ')';
2548 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2550 $text = preg_replace( '/~~~~~/', $d, $text );
2551 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2552 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2554 # Context links: [[|name]] and [[name (context)|]]
2556 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2557 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2558 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2559 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2561 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2562 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2563 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2564 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2566 $t = $this->mTitle
->getText();
2567 if ( preg_match( $conpat, $t, $m ) ) {
2570 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2571 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2572 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2574 if ( '' == $context ) {
2575 $text = preg_replace( $p2, '[[\\1]]', $text );
2577 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2580 # Trim trailing whitespace
2581 # MAG_END (__END__) tag allows for trailing
2582 # whitespace to be deliberately included
2583 $text = rtrim( $text );
2584 $mw =& MagicWord
::get( MAG_END
);
2585 $mw->matchAndRemove( $text );
2591 * Set up some variables which are usually set up in parse()
2592 * so that an external function can call some class members with confidence
2595 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2596 $this->mTitle
=& $title;
2597 $this->mOptions
= $options;
2598 $this->mOutputType
= $outputType;
2599 if ( $clearState ) {
2600 $this->clearState();
2605 * Transform a MediaWiki message by replacing magic variables.
2607 * @param string $text the text to transform
2608 * @param ParserOptions $options options
2609 * @return string the text with variables substituted
2612 function transformMsg( $text, $options ) {
2614 static $executing = false;
2616 # Guard against infinite recursion
2622 $this->mTitle
= $wgTitle;
2623 $this->mOptions
= $options;
2624 $this->mOutputType
= OT_MSG
;
2625 $this->clearState();
2626 $text = $this->replaceVariables( $text );
2633 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2634 * Callback will be called with the text within
2635 * Transform and return the text within
2638 function setHook( $tag, $callback ) {
2639 $oldVal = @$this->mTagHooks
[$tag];
2640 $this->mTagHooks
[$tag] = $callback;
2647 * @package MediaWiki
2651 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2652 var $mCacheTime; # Used in ParserCache
2654 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
2655 $containsOldMagic = false )
2657 $this->mText
= $text;
2658 $this->mLanguageLinks
= $languageLinks;
2659 $this->mCategoryLinks
= $categoryLinks;
2660 $this->mContainsOldMagic
= $containsOldMagic;
2661 $this->mCacheTime
= '';
2664 function getText() { return $this->mText
; }
2665 function getLanguageLinks() { return $this->mLanguageLinks
; }
2666 function getCategoryLinks() { return $this->mCategoryLinks
; }
2667 function getCacheTime() { return $this->mCacheTime
; }
2668 function containsOldMagic() { return $this->mContainsOldMagic
; }
2669 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2670 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2671 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2672 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2673 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2675 function merge( $other ) {
2676 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2677 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2678 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2684 * Set options of the Parser
2686 * @package MediaWiki
2690 # All variables are private
2691 var $mUseTeX; # Use texvc to expand <math> tags
2692 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2693 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2694 var $mAllowExternalImages; # Allow external images inline
2695 var $mSkin; # Reference to the preferred skin
2696 var $mDateFormat; # Date format index
2697 var $mEditSection; # Create "edit section" links
2698 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2699 var $mNumberHeadings; # Automatically number headings
2700 var $mShowToc; # Show table of contents
2702 function getUseTeX() { return $this->mUseTeX
; }
2703 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2704 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2705 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2706 function getSkin() { return $this->mSkin
; }
2707 function getDateFormat() { return $this->mDateFormat
; }
2708 function getEditSection() { return $this->mEditSection
; }
2709 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2710 function getNumberHeadings() { return $this->mNumberHeadings
; }
2711 function getShowToc() { return $this->mShowToc
; }
2713 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2714 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2715 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2716 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2717 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2718 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2719 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2720 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2721 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2723 function setSkin( &$x ) { $this->mSkin
=& $x; }
2725 # Get parser options
2726 /* static */ function newFromUser( &$user ) {
2727 $popts = new ParserOptions
;
2728 $popts->initialiseFromUser( $user );
2733 function initialiseFromUser( &$userInput ) {
2734 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2736 $fname = 'ParserOptions::initialiseFromUser';
2737 wfProfileIn( $fname );
2738 if ( !$userInput ) {
2740 $user->setLoaded( true );
2742 $user =& $userInput;
2745 $this->mUseTeX
= $wgUseTeX;
2746 $this->mUseDynamicDates
= $wgUseDynamicDates;
2747 $this->mInterwikiMagic
= $wgInterwikiMagic;
2748 $this->mAllowExternalImages
= $wgAllowExternalImages;
2749 wfProfileIn( $fname.'-skin' );
2750 $this->mSkin
=& $user->getSkin();
2751 wfProfileOut( $fname.'-skin' );
2752 $this->mDateFormat
= $user->getOption( 'date' );
2753 $this->mEditSection
= $user->getOption( 'editsection' );
2754 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2755 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2756 $this->mShowToc
= $user->getOption( 'showtoc' );
2757 wfProfileOut( $fname );
2763 # Regex callbacks, used in Parser::replaceVariables
2764 function wfBraceSubstitution( $matches ) {
2765 global $wgCurParser;
2766 return $wgCurParser->braceSubstitution( $matches );
2769 function wfArgSubstitution( $matches ) {
2770 global $wgCurParser;
2771 return $wgCurParser->argSubstitution( $matches );
2774 function wfVariableSubstitution( $matches ) {
2775 global $wgCurParser;
2776 return $wgCurParser->variableSubstitution( $matches );
2780 * Return the total number of articles
2782 function wfNumberOfArticles() {
2783 global $wgNumberOfArticles;
2786 return $wgNumberOfArticles;
2790 * Get various statistics from the database
2793 function wfLoadSiteStats() {
2794 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
2795 $fname = 'wfLoadSiteStats';
2797 if ( -1 != $wgNumberOfArticles ) return;
2798 $dbr =& wfGetDB( DB_SLAVE
);
2799 $s = $dbr->getArray( 'site_stats',
2800 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
2801 array( 'ss_row_id' => 1 ), $fname
2804 if ( $s === false ) {
2807 $wgTotalViews = $s->ss_total_views
;
2808 $wgTotalEdits = $s->ss_total_edits
;
2809 $wgNumberOfArticles = $s->ss_good_articles
;
2813 function wfEscapeHTMLTagsOnly( $in ) {
2815 array( '"', '>', '<' ),
2816 array( '"', '>', '<' ),