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).
20 define( 'MAX_INCLUDE_REPEAT', 100 );
21 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
23 # Allowed values for $mOutputType
24 define( 'OT_HTML', 1 );
25 define( 'OT_WIKI', 2 );
26 define( 'OT_MSG' , 3 );
28 # string parameter for extractTags which will cause it
29 # to strip HTML comments in addition to regular
30 # <XML>-style tags. This should not be anything we
31 # may want to use in wikisyntax
32 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
34 # prefix for escaping, used in two functions at least
35 define( 'UNIQ_PREFIX', 'NaodW29');
37 # Constants needed for external link processing
38 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
39 define( 'HTTP_PROTOCOLS', 'http|https' );
40 # Everything except bracket, space, or control characters
41 define( '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 ) {
140 global $wgUseTidy, $wgContLang;
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
);
155 $text = $this->internalParse( $text, $linestart );
156 $text = $this->unstrip( $text, $this->mStripState
);
157 # Clean up special characters, only run once, next-to-last before doBlockLevels
160 # french spaces, last one Guillemet-left
161 # only if there is something before the space
162 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
163 # french spaces, Guillemet-right
164 "/(\\302\\253) /i"=>"\\1 ",
165 '/<hr *>/i' => '<hr />',
166 '/<br *>/i' => '<br />',
167 '/<center *>/i' => '<div class="center">',
168 '/<\\/center *>/i' => '</div>',
169 # Clean up spare ampersands; note that we probably ought to be
170 # more careful about named entities.
171 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
173 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
176 # french spaces, last one Guillemet-left
177 '/ (\\?|:|;|!|\\302\\273)/i' => ' \\1',
178 # french spaces, Guillemet-right
179 '/(\\302\\253) /i' => '\\1 ',
180 '/<center *>/i' => '<div class="center">',
181 '/<\\/center *>/i' => '</div>'
183 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
186 $text = $this->doBlockLevels( $text, $linestart );
188 $text = $wgContLang->convert($text);
190 $text = $this->unstripNoWiki( $text, $this->mStripState
);
191 $this->mOutput
->setText( $text );
192 wfProfileOut( $fname );
193 return $this->mOutput
;
197 * Get a random string
202 function getRandomString() {
203 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
207 * Replaces all occurrences of <$tag>content</$tag> in the text
208 * with a random marker and returns the new text. the output parameter
209 * $content will be an associative array filled with data on the form
210 * $unique_marker => content.
212 * If $content is already set, the additional entries will be appended
213 * If $tag is set to STRIP_COMMENTS, the function will extract
214 * <!-- HTML comments -->
219 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
220 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
227 while ( '' != $text ) {
228 if($tag==STRIP_COMMENTS
) {
229 $p = preg_split( '/<!--/i', $text, 2 );
231 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
234 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
237 if($tag==STRIP_COMMENTS
) {
238 $q = preg_split( '/-->/i', $p[1], 2 );
240 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
242 $marker = $rnd . sprintf('%08X', $n++
);
243 $content[$marker] = $q[0];
244 $stripped .= $marker;
252 * Strips and renders nowiki, pre, math, hiero
253 * If $render is set, performs necessary rendering operations on plugins
254 * Returns the text, and fills an array with data needed in unstrip()
255 * If the $state is already a valid strip state, it adds to the state
257 * @param bool $stripcomments when set, HTML comments <!-- like this -->
258 * will be stripped in addition to other tags. This is important
259 * for section editing, where these comments cause confusion when
260 * counting the sections in the wikisource
264 function strip( $text, &$state, $stripcomments = false ) {
265 $render = ($this->mOutputType
== OT_HTML
);
266 $html_content = array();
267 $nowiki_content = array();
268 $math_content = array();
269 $pre_content = array();
270 $comment_content = array();
271 $ext_content = array();
273 # Replace any instances of the placeholders
274 $uniq_prefix = UNIQ_PREFIX
;
275 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
278 global $wgRawHtml, $wgWhitelistEdit;
279 if( $wgRawHtml && $wgWhitelistEdit ) {
280 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
281 foreach( $html_content as $marker => $content ) {
283 # Raw and unchecked for validity.
284 $html_content[$marker] = $content;
286 $html_content[$marker] = '<html>'.$content.'</html>';
292 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
293 foreach( $nowiki_content as $marker => $content ) {
295 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
297 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
302 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
303 foreach( $math_content as $marker => $content ){
305 if( $this->mOptions
->getUseTeX() ) {
306 $math_content[$marker] = renderMath( $content );
308 $math_content[$marker] = '<math>'.$content.'<math>';
311 $math_content[$marker] = '<math>'.$content.'</math>';
316 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
317 foreach( $pre_content as $marker => $content ){
319 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
321 $pre_content[$marker] = '<pre>'.$content.'</pre>';
327 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
328 foreach( $comment_content as $marker => $content ){
329 $comment_content[$marker] = '<!--'.$content.'-->';
334 foreach ( $this->mTagHooks
as $tag => $callback ) {
335 $ext_contents[$tag] = array();
336 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
337 foreach( $ext_content[$tag] as $marker => $content ) {
339 $ext_content[$tag][$marker] = $callback( $content );
341 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
346 # Merge state with the pre-existing state, if there is one
348 $state['html'] = $state['html'] +
$html_content;
349 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
350 $state['math'] = $state['math'] +
$math_content;
351 $state['pre'] = $state['pre'] +
$pre_content;
352 $state['comment'] = $state['comment'] +
$comment_content;
354 foreach( $ext_content as $tag => $array ) {
355 if ( array_key_exists( $tag, $state ) ) {
356 $state[$tag] = $state[$tag] +
$array;
361 'html' => $html_content,
362 'nowiki' => $nowiki_content,
363 'math' => $math_content,
364 'pre' => $pre_content,
365 'comment' => $comment_content,
372 * restores pre, math, and heiro removed by strip()
374 * always call unstripNoWiki() after this one
377 function unstrip( $text, &$state ) {
378 # Must expand in reverse order, otherwise nested tags will be corrupted
379 $contentDict = end( $state );
380 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
381 if( key($state) != 'nowiki' && key($state) != 'html') {
382 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
383 $text = str_replace( key( $contentDict ), $content, $text );
392 * always call this after unstrip() to preserve the order
396 function unstripNoWiki( $text, &$state ) {
397 # Must expand in reverse order, otherwise nested tags will be corrupted
398 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
399 $text = str_replace( key( $state['nowiki'] ), $content, $text );
404 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
405 $text = str_replace( key( $state['html'] ), $content, $text );
413 * Add an item to the strip state
414 * Returns the unique tag which must be inserted into the stripped text
415 * The tag will be replaced with the original text in unstrip()
419 function insertStripItem( $text, &$state ) {
420 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
429 $state['item'][$rnd] = $text;
434 * Return allowed HTML attributes
438 function getHTMLattrs () {
439 $htmlattrs = array( # Allowed attributes--no scripting, etc.
440 'title', 'align', 'lang', 'dir', 'width', 'height',
441 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
442 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
443 /* FONT */ 'type', 'start', 'value', 'compact',
444 /* For various lists, mostly deprecated but safe */
445 'summary', 'width', 'border', 'frame', 'rules',
446 'cellspacing', 'cellpadding', 'valign', 'char',
447 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
448 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
449 'id', 'class', 'name', 'style' /* For CSS */
455 * Remove non approved attributes and javascript in css
459 function fixTagAttributes ( $t ) {
460 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
461 $htmlattrs = $this->getHTMLattrs() ;
463 # Strip non-approved attributes from the tag
465 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
466 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
469 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
471 # Strip javascript "expression" from stylesheets. Brute force approach:
472 # If anythin offensive is found, all attributes of the HTML tag are dropped
475 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
476 wfMungeToUtf8( $t ) ) )
485 * 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 );
498 switch(strtoupper($wgOutputEncoding)) {
500 $opts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
503 $opts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
509 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
510 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
511 '<head><title>test</title></head><body>'.$text.'</body></html>';
512 $descriptorspec = array(
513 0 => array('pipe', 'r'),
514 1 => array('pipe', 'w'),
515 2 => array('file', '/dev/null', 'a')
517 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
518 if (is_resource($process)) {
519 fwrite($pipes[0], $wrappedtext);
521 while (!feof($pipes[1])) {
522 $cleansource .= fgets($pipes[1], 1024);
525 $return_value = proc_close($process);
528 wfProfileOut( $fname );
530 if( $cleansource == '' && $text != '') {
531 wfDebug( "Tidy error detected!\n" );
532 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
539 * parse the wiki syntax used to render tables
543 function doTableStuff ( $t ) {
544 $fname = 'Parser::doTableStuff';
545 wfProfileIn( $fname );
547 $t = explode ( "\n" , $t ) ;
548 $td = array () ; # Is currently a td tag open?
549 $ltd = array () ; # Was it TD or TH?
550 $tr = array () ; # Is currently a tr tag open?
551 $ltr = array () ; # tr attributes
552 $indent_level = 0; # indent level of the table
553 foreach ( $t AS $k => $x )
556 $fc = substr ( $x , 0 , 1 ) ;
557 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
558 $indent_level = strlen( $matches[1] );
560 str_repeat( '<dl><dd>', $indent_level ) .
561 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
562 array_push ( $td , false ) ;
563 array_push ( $ltd , '' ) ;
564 array_push ( $tr , false ) ;
565 array_push ( $ltr , '' ) ;
567 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
568 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
570 $l = array_pop ( $ltd ) ;
571 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
572 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
574 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
576 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
577 $x = substr ( $x , 1 ) ;
578 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
580 $l = array_pop ( $ltd ) ;
581 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
582 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
585 array_push ( $tr , false ) ;
586 array_push ( $td , false ) ;
587 array_push ( $ltd , '' ) ;
588 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
590 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) { # Caption
592 if ( '|+' == substr ( $x , 0 , 2 ) ) {
594 $x = substr ( $x , 1 ) ;
596 $after = substr ( $x , 1 ) ;
597 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
598 $after = explode ( '||' , $after ) ;
601 # Loop through each table cell
602 foreach ( $after AS $theline )
607 $tra = array_pop ( $ltr ) ;
608 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
609 array_push ( $tr , true ) ;
610 array_push ( $ltr , '' ) ;
613 $l = array_pop ( $ltd ) ;
614 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
615 if ( $fc == '|' ) $l = 'td' ;
616 else if ( $fc == '!' ) $l = 'th' ;
617 else if ( $fc == '+' ) $l = 'caption' ;
619 array_push ( $ltd , $l ) ;
622 $y = explode ( '|' , $theline , 2 ) ;
623 # Note that a '|' inside an invalid link should not
624 # be mistaken as delimiting cell parameters
625 if ( strpos( $y[0], '[[' ) !== false ) {
626 $y = array ($theline);
628 if ( count ( $y ) == 1 )
629 $y = "{$z}<{$l}>{$y[0]}" ;
630 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
632 array_push ( $td , true ) ;
637 # Closing open td, tr && table
638 while ( count ( $td ) > 0 )
640 if ( array_pop ( $td ) ) $t[] = '</td>' ;
641 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
645 $t = implode ( "\n" , $t ) ;
646 # $t = $this->removeHTMLtags( $t );
647 wfProfileOut( $fname );
652 * Helper function for parse() that transforms wiki markup into
653 * HTML. Only called for $mOutputType == OT_HTML.
657 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
660 $fname = 'Parser::internalParse';
661 wfProfileIn( $fname );
663 $text = $this->removeHTMLtags( $text );
664 $text = $this->replaceVariables( $text, $args );
666 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
668 $text = $this->doHeadings( $text );
669 if($this->mOptions
->getUseDynamicDates()) {
670 global $wgDateFormatter;
671 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
673 $text = $this->doAllQuotes( $text );
674 $text = $this->replaceInternalLinks ( $text );
675 $text = $this->replaceExternalLinks( $text );
676 $text = $this->doMagicLinks( $text );
677 $text = $this->doTableStuff( $text );
678 $text = $this->formatHeadings( $text, $isMain );
679 $sk =& $this->mOptions
->getSkin();
680 $text = $sk->transformContent( $text );
682 wfProfileOut( $fname );
687 * Replace special strings like "ISBN xxx" and "RFC xxx" with
688 * magic external links.
692 function &doMagicLinks( &$text ) {
693 global $wgUseGeoMode;
694 $text = $this->magicISBN( $text );
695 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
696 $text = $this->magicGEO( $text );
698 $text = $this->magicRFC( $text );
703 * Parse ^^ tokens and return html
707 function doExponent ( $text ) {
708 $fname = 'Parser::doExponent';
709 wfProfileIn( $fname);
710 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
711 wfProfileOut( $fname);
716 * Parse headers and return html
720 function doHeadings( $text ) {
721 $fname = 'Parser::doHeadings';
722 wfProfileIn( $fname );
723 for ( $i = 6; $i >= 1; --$i ) {
724 $h = substr( '======', 0, $i );
725 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
726 "<h{$i}>\\1</h{$i}>\\2", $text );
728 wfProfileOut( $fname );
733 * Replace single quotes with HTML markup
735 * @return string the altered text
737 function doAllQuotes( $text ) {
738 $fname = 'Parser::doAllQuotes';
739 wfProfileIn( $fname );
741 $lines = explode( "\n", $text );
742 foreach ( $lines as $line ) {
743 $outtext .= $this->doQuotes ( $line ) . "\n";
745 $outtext = substr($outtext, 0,-1);
746 wfProfileOut( $fname );
751 * Helper function for doAllQuotes()
754 function doQuotes( $text ) {
755 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
756 if (count ($arr) == 1)
760 # First, do some preliminary work. This may shift some apostrophes from
761 # being mark-up to being text. It also counts the number of occurrences
762 # of bold and italics mark-ups.
770 # If there are ever four apostrophes, assume the first is supposed to
771 # be text, and the remaining three constitute mark-up for bold text.
772 if (strlen ($arr[$i]) == 4)
777 # If there are more than 5 apostrophes in a row, assume they're all
778 # text except for the last 5.
779 else if (strlen ($arr[$i]) > 5)
781 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
784 # Count the number of occurrences of bold and italics mark-ups.
785 # We are not counting sequences of five apostrophes.
786 if (strlen ($arr[$i]) == 2) $numitalics++
; else
787 if (strlen ($arr[$i]) == 3) $numbold++
; else
788 if (strlen ($arr[$i]) == 5) { $numitalics++
; $numbold++
; }
793 # If there is an odd number of both bold and italics, it is likely
794 # that one of the bold ones was meant to be an apostrophe followed
795 # by italics. Which one we cannot know for certain, but it is more
796 # likely to be one that has a single-letter word before it.
797 if (($numbold %
2 == 1) && ($numitalics %
2 == 1))
800 $firstsingleletterword = -1;
801 $firstmultiletterword = -1;
805 if (($i %
2 == 1) and (strlen ($r) == 3))
807 $x1 = substr ($arr[$i-1], -1);
808 $x2 = substr ($arr[$i-1], -2, 1);
810 if ($firstspace == -1) $firstspace = $i;
811 } else if ($x2 == ' ') {
812 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
814 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
820 # If there is a single-letter word, use it!
821 if ($firstsingleletterword > -1)
823 $arr [ $firstsingleletterword ] = "''";
824 $arr [ $firstsingleletterword-1 ] .= "'";
826 # If not, but there's a multi-letter word, use that one.
827 else if ($firstmultiletterword > -1)
829 $arr [ $firstmultiletterword ] = "''";
830 $arr [ $firstmultiletterword-1 ] .= "'";
832 # ... otherwise use the first one that has neither.
833 # (notice that it is possible for all three to be -1 if, for example,
834 # there is only one pentuple-apostrophe in the line)
835 else if ($firstspace > -1)
837 $arr [ $firstspace ] = "''";
838 $arr [ $firstspace-1 ] .= "'";
842 # Now let's actually convert our apostrophic mush to HTML!
851 if ($state == 'both')
858 if (strlen ($r) == 2)
861 { $output .= '</i>'; $state = ''; }
862 else if ($state == 'bi')
863 { $output .= '</i>'; $state = 'b'; }
864 else if ($state == 'ib')
865 { $output .= '</b></i><b>'; $state = 'b'; }
866 else if ($state == 'both')
867 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
868 else # $state can be 'b' or ''
869 { $output .= '<i>'; $state .= 'i'; }
871 else if (strlen ($r) == 3)
874 { $output .= '</b>'; $state = ''; }
875 else if ($state == 'bi')
876 { $output .= '</i></b><i>'; $state = 'i'; }
877 else if ($state == 'ib')
878 { $output .= '</b>'; $state = 'i'; }
879 else if ($state == 'both')
880 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
881 else # $state can be 'i' or ''
882 { $output .= '<b>'; $state .= 'b'; }
884 else if (strlen ($r) == 5)
887 { $output .= '</b><i>'; $state = 'i'; }
888 else if ($state == 'i')
889 { $output .= '</i><b>'; $state = 'b'; }
890 else if ($state == 'bi')
891 { $output .= '</i></b>'; $state = ''; }
892 else if ($state == 'ib')
893 { $output .= '</b></i>'; $state = ''; }
894 else if ($state == 'both')
895 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
896 else # ($state == '')
897 { $buffer = ''; $state = 'both'; }
902 # Now close all remaining tags. Notice that the order is important.
903 if ($state == 'b' ||
$state == 'ib')
905 if ($state == 'i' ||
$state == 'bi' ||
$state == 'ib')
909 if ($state == 'both')
910 $output .= '<b><i>'.$buffer.'</i></b>';
916 * Replace external links
918 * Note: we have to do external links before the internal ones,
919 * and otherwise take great care in the order of things here, so
920 * that we don't end up interpreting some URLs twice.
924 function replaceExternalLinks( $text ) {
925 $fname = 'Parser::replaceExternalLinks';
926 wfProfileIn( $fname );
928 $sk =& $this->mOptions
->getSkin();
929 $linktrail = wfMsgForContent('linktrail');
930 $bits = preg_split( EXT_LINK_BRACKETED
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
932 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
935 while ( $i<count( $bits ) ) {
937 $protocol = $bits[$i++
];
939 $trail = $bits[$i++
];
941 # The characters '<' and '>' (which were escaped by
942 # removeHTMLtags()) should not be included in
943 # URLs, per RFC 2396.
944 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE
)) {
945 $text = substr($url, $m2[0][1]) . ' ' . $text;
946 $url = substr($url, 0, $m2[0][1]);
949 # If the link text is an image URL, replace it with an <img> tag
950 # This happened by accident in the original parser, but some people used it extensively
951 $img = $this->maybeMakeImageLink( $text );
952 if ( $img !== false ) {
958 # No link text, e.g. [http://domain.tld/some.link]
960 # Autonumber if allowed
961 if ( strpos( HTTP_PROTOCOLS
, $protocol ) !== false ) {
962 $text = '[' . ++
$this->mAutonumber
. ']';
964 # Otherwise just use the URL
965 $text = htmlspecialchars( $url );
968 # Have link text, e.g. [http://domain.tld/some.link text]s
970 if ( preg_match( $linktrail, $trail, $m2 ) ) {
976 $encUrl = htmlspecialchars( $url );
977 # Bit in parentheses showing the URL for the printable version
978 if( $url == $text ||
preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
981 # Expand the URL for printable version
982 if ( ! $sk->suppressUrlExpansion() ) {
983 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
989 # Process the trail (i.e. everything after this link up until start of the next link),
990 # replacing any non-bracketed links
991 $trail = $this->replaceFreeExternalLinks( $trail );
993 # Use the encoded URL
994 # This means that users can paste URLs directly into the text
995 # Funny characters like ö aren't valid in URLs anyway
996 # This was changed in August 2004
997 $s .= $sk->makeExternalLink( $url, $text, false ) . $dtrail. $paren . $trail;
1000 wfProfileOut( $fname );
1005 * Replace anything that looks like a URL with a link
1008 function replaceFreeExternalLinks( $text ) {
1009 $bits = preg_split( '/((?:'.URL_PROTOCOLS
.'):)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1010 $s = array_shift( $bits );
1013 $sk =& $this->mOptions
->getSkin();
1015 while ( $i < count( $bits ) ){
1016 $protocol = $bits[$i++
];
1017 $remainder = $bits[$i++
];
1019 if ( preg_match( '/^('.EXT_LINK_URL_CLASS
.'+)(.*)$/s', $remainder, $m ) ) {
1020 # Found some characters after the protocol that look promising
1021 $url = $protocol . $m[1];
1024 # The characters '<' and '>' (which were escaped by
1025 # removeHTMLtags()) should not be included in
1026 # URLs, per RFC 2396.
1027 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE
)) {
1028 $trail = substr($url, $m2[0][1]) . $trail;
1029 $url = substr($url, 0, $m2[0][1]);
1032 # Move trailing punctuation to $trail
1034 # If there is no left bracket, then consider right brackets fair game too
1035 if ( strpos( $url, '(' ) === false ) {
1039 $numSepChars = strspn( strrev( $url ), $sep );
1040 if ( $numSepChars ) {
1041 $trail = substr( $url, -$numSepChars ) . $trail;
1042 $url = substr( $url, 0, -$numSepChars );
1045 # Replace & from obsolete syntax with &.
1046 # All HTML entities will be escaped by makeExternalLink()
1047 # or maybeMakeImageLink()
1048 $url = str_replace( '&', '&', $url );
1050 # Is this an external image?
1051 $text = $this->maybeMakeImageLink( $url );
1052 if ( $text === false ) {
1053 # Not an image, make a link
1054 $text = $sk->makeExternalLink( $url, $url );
1056 $s .= $text . $trail;
1058 $s .= $protocol . $remainder;
1065 * make an image if it's allowed
1068 function maybeMakeImageLink( $url ) {
1069 $sk =& $this->mOptions
->getSkin();
1071 if ( $this->mOptions
->getAllowExternalImages() ) {
1072 if ( preg_match( EXT_IMAGE_REGEX
, $url ) ) {
1074 $text = $sk->makeImage( htmlspecialchars( $url ) );
1081 * Process [[ ]] wikilinks
1086 function replaceInternalLinks( $s ) {
1087 global $wgLang, $wgContLang, $wgLinkCache;
1088 static $fname = 'Parser::replaceInternalLinks' ;
1089 # use a counter to prevent too much unknown links from
1090 # being checked for different language variants.
1091 static $convertCount;
1092 wfProfileIn( $fname );
1094 wfProfileIn( $fname.'-setup' );
1096 # the % is needed to support urlencoded titles as well
1097 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1098 $sk =& $this->mOptions
->getSkin();
1100 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
1102 #split the entire text string on occurences of [[
1103 $a = explode( '[[', ' ' . $s );
1104 #get the first element (all text up to first [[), and remove the space we added
1105 $s = array_shift( $a );
1106 $s = substr( $s, 1 );
1108 # Match a link having the form [[namespace:link|alternate]]trail
1110 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1111 # Match cases where there is no "]]", which might still be images
1112 static $e1_img = FALSE;
1113 if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
1114 # Match the end of a line for a word that's not followed by whitespace,
1115 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1116 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1118 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
1120 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
1122 if ( $useLinkPrefixExtension ) {
1123 if ( preg_match( $e2, $s, $m ) ) {
1124 $first_prefix = $m[2];
1127 $first_prefix = false;
1133 wfProfileOut( $fname.'-setup' );
1135 # Loop for each link
1136 for ($k = 0; isset( $a[$k] ); $k++
) {
1138 wfProfileIn( $fname.'-prefixhandling' );
1139 if ( $useLinkPrefixExtension ) {
1140 if ( preg_match( $e2, $s, $m ) ) {
1148 $prefix = $first_prefix;
1149 $first_prefix = false;
1152 wfProfileOut( $fname.'-prefixhandling' );
1154 $might_be_img = false;
1156 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1158 # fix up urlencoded title texts
1159 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1161 } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
1162 $might_be_img = true;
1164 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1166 } else { # Invalid form; output directly
1167 $s .= $prefix . '[[' . $line ;
1171 # Don't allow internal links to pages containing
1172 # PROTO: where PROTO is a valid URL protocol; these
1173 # should be external links.
1174 if (preg_match('/((?:'.URL_PROTOCOLS
.'):)/', $m[1])) {
1175 $s .= $prefix . '[[' . $line ;
1179 # Make subpage if necessary
1180 $link = $this->maybeDoSubpageLink( $m[1], $text );
1182 $noforce = (substr($m[1], 0, 1) != ':');
1184 # Strip off leading ':'
1185 $link = substr($link, 1);
1188 $nt = Title
::newFromText( $link );
1190 $s .= $prefix . '[[' . $line;
1194 //check other language variants of the link
1195 //if the article does not exist
1197 $variants = $wgContLang->getVariants();
1199 if(sizeof($variants) > 1 && $convertCount < 200) {
1201 if($nt->getArticleID() == 0) {
1202 foreach ( $variants as $v ) {
1203 if($v == $wgContLang->getPreferredVariant())
1206 $varlink = $wgContLang->autoConvert($link, $v);
1207 $varnt = Title
::newFromText($varlink);
1208 if($varnt && $varnt->getArticleID()>0) {
1213 if($varnt && $varnt->getArticleID()>0) {
1219 $ns = $nt->getNamespace();
1220 $iw = $nt->getInterWiki();
1222 if ($might_be_img) { # if this is actually an invalid link
1223 if ($ns == NS_IMAGE
&& $noforce) { #but might be an image
1225 while (isset ($a[$k+
1]) ) {
1226 #look at the next 'line' to see if we can close it there
1227 $next_line = array_shift(array_splice( $a, $k +
1, 1) );
1228 if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
1229 # the first ]] closes the inner link, the second the image
1231 $text .= '[[' . $m[1];
1234 } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
1235 #if there's exactly one ]] that's fine, we'll keep looking
1236 $text .= '[[' . $m[0];
1238 #if $next_line is invalid too, we need look no further
1239 $text .= '[[' . $next_line;
1244 # we couldn't find the end of this imageLink, so output it raw
1245 #but don't ignore what might be perfectly normal links in the text we've examined
1246 $text = $this->replaceInternalLinks($text);
1247 $s .= $prefix . '[[' . $link . '|' . $text;
1248 # note: no $trail, because without an end, there *is* no trail
1251 } else { #it's not an image, so output it raw
1252 $s .= $prefix . '[[' . $link . '|' . $text;
1253 # note: no $trail, because without an end, there *is* no trail
1258 $wasblank = ( '' == $text );
1259 if( $wasblank ) $text = $link;
1262 # Link not escaped by : , create the various objects
1266 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1267 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1268 $tmp = $prefix . $trail ;
1269 $s .= (trim($tmp) == '')?
'': $tmp;
1273 if ( $ns == NS_IMAGE
) {
1274 # recursively parse links inside the image caption
1275 # actually, this will parse them in any other parameters, too,
1276 # but it might be hard to fix that, and it doesn't matter ATM
1277 $text = $this->replaceExternalLinks($text);
1278 $text = $this->replaceInternalLinks($text);
1280 # replace the image with a link-holder so that replaceExternalLinks() can't mess with it
1281 $s .= $prefix . $this->insertStripItem( $sk->makeImageLinkObj( $nt, $text ), $this->mStripState
) . $trail;
1282 $wgLinkCache->addImageLinkObj( $nt );
1286 if ( $ns == NS_CATEGORY
) {
1287 $t = $nt->getText() ;
1289 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1290 $pPLC=$sk->postParseLinkColour();
1291 $sk->postParseLinkColour( false );
1292 $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
1293 $sk->postParseLinkColour( $pPLC );
1294 $wgLinkCache->resume();
1297 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
1298 $sortkey = $this->mTitle
->getText();
1300 $sortkey = $this->mTitle
->getPrefixedText();
1305 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1306 $this->mOutput
->mCategoryLinks
[] = $t ;
1307 $s .= $prefix . $trail ;
1312 $text = $wgContLang->convert($text);
1314 if( ( $nt->getPrefixedText() === $this->mTitle
->getPrefixedText() ) &&
1315 ( $nt->getFragment() === '' ) ) {
1316 # Self-links are handled specially; generally de-link and change to bold.
1317 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1321 # Special and Media are pseudo-namespaces; no pages actually exist in them
1322 if( $ns == NS_MEDIA
) {
1323 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1324 $wgLinkCache->addImageLinkObj( $nt );
1326 } elseif( $ns == NS_SPECIAL
) {
1327 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1330 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1332 wfProfileOut( $fname );
1337 * Handle link to subpage if necessary
1338 * @param $target string the source of the link
1339 * @param &$text the link text, modified as necessary
1340 * @return string the full name of the link
1343 function maybeDoSubpageLink($target, &$text) {
1346 # :Foobar -- override special treatment of prefix (images, language links)
1347 # /Foobar -- convert to CurrentPage/Foobar
1348 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1349 global $wgNamespacesWithSubpages;
1351 $fname = 'Parser::maybeDoSubpageLink';
1352 wfProfileIn( $fname );
1353 # Look at the first character
1354 if( $target{0} == '/' ) {
1355 # / at end means we don't want the slash to be shown
1356 if(substr($target,-1,1)=='/') {
1357 $target=substr($target,1,-1);
1360 $noslash=substr($target,1);
1363 # Some namespaces don't allow subpages
1364 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) {
1365 # subpages allowed here
1366 $ret = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1367 if( '' === $text ) {
1369 } # this might be changed for ugliness reasons
1371 # no subpage allowed, use standard link
1379 wfProfileOut( $fname );
1384 * Used by doBlockLevels()
1387 /* private */ function closeParagraph() {
1389 if ( '' != $this->mLastSection
) {
1390 $result = '</' . $this->mLastSection
. ">\n";
1392 $this->mInPre
= false;
1393 $this->mLastSection
= '';
1396 # getCommon() returns the length of the longest common substring
1397 # of both arguments, starting at the beginning of both.
1399 /* private */ function getCommon( $st1, $st2 ) {
1400 $fl = strlen( $st1 );
1401 $shorter = strlen( $st2 );
1402 if ( $fl < $shorter ) { $shorter = $fl; }
1404 for ( $i = 0; $i < $shorter; ++
$i ) {
1405 if ( $st1{$i} != $st2{$i} ) { break; }
1409 # These next three functions open, continue, and close the list
1410 # element appropriate to the prefix character passed into them.
1412 /* private */ function openList( $char ) {
1413 $result = $this->closeParagraph();
1415 if ( '*' == $char ) { $result .= '<ul><li>'; }
1416 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1417 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1418 else if ( ';' == $char ) {
1419 $result .= '<dl><dt>';
1420 $this->mDTopen
= true;
1422 else { $result = '<!-- ERR 1 -->'; }
1427 /* private */ function nextItem( $char ) {
1428 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1429 else if ( ':' == $char ||
';' == $char ) {
1431 if ( $this->mDTopen
) { $close = '</dt>'; }
1432 if ( ';' == $char ) {
1433 $this->mDTopen
= true;
1434 return $close . '<dt>';
1436 $this->mDTopen
= false;
1437 return $close . '<dd>';
1440 return '<!-- ERR 2 -->';
1443 /* private */ function closeList( $char ) {
1444 if ( '*' == $char ) { $text = '</li></ul>'; }
1445 else if ( '#' == $char ) { $text = '</li></ol>'; }
1446 else if ( ':' == $char ) {
1447 if ( $this->mDTopen
) {
1448 $this->mDTopen
= false;
1449 $text = '</dt></dl>';
1451 $text = '</dd></dl>';
1454 else { return '<!-- ERR 3 -->'; }
1460 * Make lists from lines starting with ':', '*', '#', etc.
1463 * @return string the lists rendered as HTML
1465 function doBlockLevels( $text, $linestart ) {
1466 $fname = 'Parser::doBlockLevels';
1467 wfProfileIn( $fname );
1469 # Parsing through the text line by line. The main thing
1470 # happening here is handling of block-level elements p, pre,
1471 # and making lists from lines starting with * # : etc.
1473 $textLines = explode( "\n", $text );
1475 $lastPrefix = $output = $lastLine = '';
1476 $this->mDTopen
= $inBlockElem = false;
1478 $paragraphStack = false;
1480 if ( !$linestart ) {
1481 $output .= array_shift( $textLines );
1483 foreach ( $textLines as $oLine ) {
1484 $lastPrefixLength = strlen( $lastPrefix );
1485 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1486 $preOpenMatch = preg_match('/<pre/i', $oLine );
1487 if ( !$this->mInPre
) {
1488 # Multiple prefixes may abut each other for nested lists.
1489 $prefixLength = strspn( $oLine, '*#:;' );
1490 $pref = substr( $oLine, 0, $prefixLength );
1493 $pref2 = str_replace( ';', ':', $pref );
1494 $t = substr( $oLine, $prefixLength );
1495 $this->mInPre
= !empty($preOpenMatch);
1497 # Don't interpret any other prefixes in preformatted text
1499 $pref = $pref2 = '';
1504 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1505 # Same as the last item, so no need to deal with nesting or opening stuff
1506 $output .= $this->nextItem( substr( $pref, -1 ) );
1507 $paragraphStack = false;
1509 if ( substr( $pref, -1 ) == ';') {
1510 # The one nasty exception: definition lists work like this:
1511 # ; title : definition text
1512 # So we check for : in the remainder text to split up the
1513 # title and definition, without b0rking links.
1514 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1516 $output .= $term . $this->nextItem( ':' );
1519 } elseif( $prefixLength ||
$lastPrefixLength ) {
1520 # Either open or close a level...
1521 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1522 $paragraphStack = false;
1524 while( $commonPrefixLength < $lastPrefixLength ) {
1525 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1526 --$lastPrefixLength;
1528 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1529 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1531 while ( $prefixLength > $commonPrefixLength ) {
1532 $char = substr( $pref, $commonPrefixLength, 1 );
1533 $output .= $this->openList( $char );
1535 if ( ';' == $char ) {
1536 # FIXME: This is dupe of code above
1537 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1539 $output .= $term . $this->nextItem( ':' );
1542 ++
$commonPrefixLength;
1544 $lastPrefix = $pref2;
1546 if( 0 == $prefixLength ) {
1547 # No prefix (not in list)--go to paragraph mode
1548 $uniq_prefix = UNIQ_PREFIX
;
1549 // XXX: use a stack for nestable elements like span, table and div
1550 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1551 $closematch = preg_match(
1552 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1553 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1554 if ( $openmatch or $closematch ) {
1555 $paragraphStack = false;
1556 $output .= $this->closeParagraph();
1557 if($preOpenMatch and !$preCloseMatch) {
1558 $this->mInPre
= true;
1560 if ( $closematch ) {
1561 $inBlockElem = false;
1563 $inBlockElem = true;
1565 } else if ( !$inBlockElem && !$this->mInPre
) {
1566 if ( ' ' == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1568 if ($this->mLastSection
!= 'pre') {
1569 $paragraphStack = false;
1570 $output .= $this->closeParagraph().'<pre>';
1571 $this->mLastSection
= 'pre';
1573 $t = substr( $t, 1 );
1576 if ( '' == trim($t) ) {
1577 if ( $paragraphStack ) {
1578 $output .= $paragraphStack.'<br />';
1579 $paragraphStack = false;
1580 $this->mLastSection
= 'p';
1582 if ($this->mLastSection
!= 'p' ) {
1583 $output .= $this->closeParagraph();
1584 $this->mLastSection
= '';
1585 $paragraphStack = '<p>';
1587 $paragraphStack = '</p><p>';
1591 if ( $paragraphStack ) {
1592 $output .= $paragraphStack;
1593 $paragraphStack = false;
1594 $this->mLastSection
= 'p';
1595 } else if ($this->mLastSection
!= 'p') {
1596 $output .= $this->closeParagraph().'<p>';
1597 $this->mLastSection
= 'p';
1603 if ($paragraphStack === false) {
1607 while ( $prefixLength ) {
1608 $output .= $this->closeList( $pref2{$prefixLength-1} );
1611 if ( '' != $this->mLastSection
) {
1612 $output .= '</' . $this->mLastSection
. '>';
1613 $this->mLastSection
= '';
1616 wfProfileOut( $fname );
1621 * Split up a string on ':', ignoring any occurences inside
1622 * <a>..</a> or <span>...</span>
1623 * @param $str string the string to split
1624 * @param &$before string set to everything before the ':'
1625 * @param &$after string set to everything after the ':'
1626 * return string the position of the ':', or false if none found
1628 function findColonNoLinks($str, &$before, &$after) {
1629 # I wonder if we should make this count all tags, not just <a>
1630 # and <span>. That would prevent us from matching a ':' that
1631 # comes in the middle of italics other such formatting....
1633 $fname = 'Parser::findColonNoLinks';
1634 wfProfileIn( $fname );
1637 $colon = strpos($str, ':', $pos);
1639 if ($colon !== false) {
1640 $before = substr($str, 0, $colon);
1641 $after = substr($str, $colon +
1);
1643 # Skip any ':' within <a> or <span> pairs
1644 $a = substr_count($before, '<a');
1645 $s = substr_count($before, '<span');
1646 $ca = substr_count($before, '</a>');
1647 $cs = substr_count($before, '</span>');
1649 if ($a <= $ca and $s <= $cs) {
1650 # Tags are balanced before ':'; ok
1655 } while ($colon !== false);
1656 wfProfileOut( $fname );
1661 * Return value of a magic variable (like PAGENAME)
1665 function getVariableValue( $index ) {
1666 global $wgContLang, $wgSitename, $wgServer;
1669 case MAG_CURRENTMONTH
:
1670 return $wgContLang->formatNum( date( 'm' ) );
1671 case MAG_CURRENTMONTHNAME
:
1672 return $wgContLang->getMonthName( date('n') );
1673 case MAG_CURRENTMONTHNAMEGEN
:
1674 return $wgContLang->getMonthNameGen( date('n') );
1675 case MAG_CURRENTDAY
:
1676 return $wgContLang->formatNum( date('j') );
1678 return $this->mTitle
->getText();
1680 return $this->mTitle
->getPartialURL();
1682 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1683 return $wgContLang->getNsText($this->mTitle
->getNamespace()); # Patch by Dori
1684 case MAG_CURRENTDAYNAME
:
1685 return $wgContLang->getWeekdayName( date('w')+
1 );
1686 case MAG_CURRENTYEAR
:
1687 return $wgContLang->formatNum( date( 'Y' ) );
1688 case MAG_CURRENTTIME
:
1689 return $wgContLang->time( wfTimestampNow(), false );
1690 case MAG_NUMBEROFARTICLES
:
1691 return $wgContLang->formatNum( wfNumberOfArticles() );
1702 * initialise the magic variables (like CURRENTMONTHNAME)
1706 function initialiseVariables() {
1707 $fname = 'Parser::initialiseVariables';
1708 wfProfileIn( $fname );
1709 global $wgVariableIDs;
1710 $this->mVariables
= array();
1711 foreach ( $wgVariableIDs as $id ) {
1712 $mw =& MagicWord
::get( $id );
1713 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1715 wfProfileOut( $fname );
1719 * Replace magic variables, templates, and template arguments
1720 * with the appropriate text. Templates are substituted recursively,
1721 * taking care to avoid infinite loops.
1723 * Note that the substitution depends on value of $mOutputType:
1724 * OT_WIKI: only {{subst:}} templates
1725 * OT_MSG: only magic variables
1726 * OT_HTML: all templates and magic variables
1728 * @param string $tex The text to transform
1729 * @param array $args Key-value pairs representing template parameters to substitute
1732 function replaceVariables( $text, $args = array() ) {
1733 global $wgLang, $wgScript, $wgArticlePath;
1735 # Prevent too big inclusions
1736 if(strlen($text)> MAX_INCLUDE_SIZE
)
1739 $fname = 'Parser::replaceVariables';
1740 wfProfileIn( $fname );
1742 $titleChars = Title
::legalChars();
1744 # This function is called recursively. To keep track of arguments we need a stack:
1745 array_push( $this->mArgStack
, $args );
1747 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1748 $GLOBALS['wgCurParser'] =& $this;
1750 # Variable substitution
1751 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", 'wfVariableSubstitution', $text );
1753 if ( $this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) {
1754 # Argument substitution
1755 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1757 # Template substitution
1758 $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1759 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1761 array_pop( $this->mArgStack
);
1763 wfProfileOut( $fname );
1768 * Replace magic variables
1771 function variableSubstitution( $matches ) {
1772 if ( !$this->mVariables
) {
1773 $this->initialiseVariables();
1776 if ( $this->mOutputType
== OT_WIKI
) {
1777 # Do only magic variables prefixed by SUBST
1778 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1779 if (!$mwSubst->matchStartAndRemove( $matches[1] ))
1781 # Note that if we don't substitute the variable below,
1782 # we don't remove the {{subst:}} magic word, in case
1783 # it is a template rather than a magic variable.
1785 if ( !$skip && array_key_exists( $matches[1], $this->mVariables
) ) {
1786 $text = $this->mVariables
[$matches[1]];
1787 $this->mOutput
->mContainsOldMagic
= true;
1789 $text = $matches[0];
1794 # Split template arguments
1795 function getTemplateArgs( $argsString ) {
1796 if ( $argsString === '' ) {
1800 $args = explode( '|', substr( $argsString, 1 ) );
1802 # If any of the arguments contains a '[[' but no ']]', it needs to be
1803 # merged with the next arg because the '|' character between belongs
1804 # to the link syntax and not the template parameter syntax.
1805 $argc = count($args);
1807 for ( $i = 0; $i < $argc-1; $i++
) {
1808 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1809 $args[$i] .= '|'.$args[$i+
1];
1810 array_splice($args, $i+
1, 1);
1820 * Return the text of a template, after recursively
1821 * replacing any variables or templates within the template.
1823 * @param array $matches The parts of the template
1824 * $matches[1]: the title, i.e. the part before the |
1825 * $matches[2]: the parameters (including a leading |), if any
1826 * @return string the text of the template
1829 function braceSubstitution( $matches ) {
1830 global $wgLinkCache, $wgContLang;
1831 $fname = 'Parser::braceSubstitution';
1838 # Need to know if the template comes at the start of a line,
1839 # to treat the beginning of the template like the beginning
1840 # of a line for tables and block-level elements.
1841 $linestart = $matches[1];
1843 # $part1 is the bit before the first |, and must contain only title characters
1844 # $args is a list of arguments, starting from index 0, not including $part1
1846 $part1 = $matches[2];
1847 # If the third subpattern matched anything, it will start with |
1849 $args = $this->getTemplateArgs($matches[3]);
1850 $argc = count( $args );
1852 # Don't parse {{{}}} because that's only for template arguments
1853 if ( $linestart === '{' ) {
1854 $text = $matches[0];
1861 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1862 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType
== OT_WIKI
) ) {
1863 # One of two possibilities is true:
1864 # 1) Found SUBST but not in the PST phase
1865 # 2) Didn't find SUBST and in the PST phase
1866 # In either case, return without further processing
1867 $text = $matches[0];
1873 # MSG, MSGNW and INT
1876 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1877 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1880 # Remove obsolete MSG:
1881 $mwMsg =& MagicWord
::get( MAG_MSG
);
1882 $mwMsg->matchStartAndRemove( $part1 );
1885 # Check if it is an internal message
1886 $mwInt =& MagicWord
::get( MAG_INT
);
1887 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1888 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1889 $text = $linestart . wfMsgReal( $part1, $args, true );
1897 # Check for NS: (namespace expansion)
1898 $mwNs = MagicWord
::get( MAG_NS
);
1899 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1900 if ( intval( $part1 ) ) {
1901 $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
1904 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1905 if ( !is_null( $index ) ) {
1906 $text = $linestart . $wgContLang->getNsText( $index );
1913 # LOCALURL and LOCALURLE
1915 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1916 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1918 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1919 $func = 'getLocalURL';
1920 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1921 $func = 'escapeLocalURL';
1926 if ( $func !== '' ) {
1927 $title = Title
::newFromText( $part1 );
1928 if ( !is_null( $title ) ) {
1930 $text = $linestart . $title->$func( $args[0] );
1932 $text = $linestart . $title->$func();
1940 if ( !$found && $argc == 1 ) {
1941 $mwGrammar =& MagicWord
::get( MAG_GRAMMAR
);
1942 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1943 $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
1948 # Template table test
1950 # Did we encounter this template already? If yes, it is in the cache
1951 # and we need to check for loops.
1952 if ( !$found && isset( $this->mTemplates
[$part1] ) ) {
1953 # set $text to cached message.
1954 $text = $linestart . $this->mTemplates
[$part1];
1957 # Infinite loop test
1958 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1961 $text .= '<!-- WARNING: template loop detected -->';
1965 # Load from database
1966 $itcamefromthedatabase = false;
1969 $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
1970 if ($subpage !== '') {
1971 $ns = $this->mTitle
->getNamespace();
1973 $title = Title
::newFromText( $part1, $ns );
1974 if ( !is_null( $title ) && !$title->isExternal() ) {
1975 # Check for excessive inclusion
1976 $dbk = $title->getPrefixedDBkey();
1977 if ( $this->incrementIncludeCount( $dbk ) ) {
1978 # This should never be reached.
1979 $article = new Article( $title );
1980 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1981 if ( $articleContent !== false ) {
1983 $text = $linestart . $articleContent;
1984 $itcamefromthedatabase = true;
1988 # If the title is valid but undisplayable, make a link to it
1989 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1990 $text = $linestart . '[['.$title->getPrefixedText().']]';
1994 # Template cache array insertion
1995 $this->mTemplates
[$part1] = $text;
1999 # Recursive parsing, escaping and link table handling
2000 # Only for HTML output
2001 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
2002 $text = wfEscapeWikiText( $text );
2003 } elseif ( ($this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) && $found && !$noparse) {
2004 # Clean up argument array
2005 $assocArgs = array();
2007 foreach( $args as $arg ) {
2008 $eqpos = strpos( $arg, '=' );
2009 if ( $eqpos === false ) {
2010 $assocArgs[$index++
] = $arg;
2012 $name = trim( substr( $arg, 0, $eqpos ) );
2013 $value = trim( substr( $arg, $eqpos+
1 ) );
2014 if ( $value === false ) {
2017 if ( $name !== false ) {
2018 $assocArgs[$name] = $value;
2023 # Add a new element to the templace recursion path
2024 $this->mTemplatePath
[$part1] = 1;
2026 $text = $this->strip( $text, $this->mStripState
);
2027 $text = $this->removeHTMLtags( $text );
2028 $text = $this->replaceVariables( $text, $assocArgs );
2030 # Resume the link cache and register the inclusion as a link
2031 if ( $this->mOutputType
== OT_HTML
&& !is_null( $title ) ) {
2032 $wgLinkCache->addLinkObj( $title );
2035 # If the template begins with a table or block-level
2036 # element, it should be treated as beginning a new line.
2037 if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
2038 $text = "\n" . $text;
2042 # Empties the template path
2043 $this->mTemplatePath
= array();
2047 # replace ==section headers==
2048 # XXX this needs to go away once we have a better parser.
2049 if ( $this->mOutputType
!= OT_WIKI
&& $itcamefromthedatabase ) {
2050 if( !is_null( $title ) )
2051 $encodedname = base64_encode($title->getPrefixedDBkey());
2053 $encodedname = base64_encode("");
2054 $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
2055 PREG_SPLIT_DELIM_CAPTURE
);
2058 for( $i = 0; $i < count($m); $i +
= 2 ) {
2060 if (!isset($m[$i +
1]) ||
$m[$i +
1] == "") continue;
2062 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
2066 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
2067 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
2068 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
2075 # Empties the template path
2076 $this->mTemplatePath
= array();
2085 * Triple brace replacement -- used for template arguments
2088 function argSubstitution( $matches ) {
2089 $arg = trim( $matches[1] );
2090 $text = $matches[0];
2091 $inputArgs = end( $this->mArgStack
);
2093 if ( array_key_exists( $arg, $inputArgs ) ) {
2094 $text = $inputArgs[$arg];
2101 * Returns true if the function is allowed to include this entity
2104 function incrementIncludeCount( $dbk ) {
2105 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
2106 $this->mIncludeCount
[$dbk] = 0;
2108 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
2117 * Cleans up HTML, removes dangerous tags and attributes, and
2118 * removes HTML comments
2121 function removeHTMLtags( $text ) {
2122 global $wgUseTidy, $wgUserHtml;
2123 $fname = 'Parser::removeHTMLtags';
2124 wfProfileIn( $fname );
2127 $htmlpairs = array( # Tags that must be closed
2128 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
2129 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
2130 'strike', 'strong', 'tt', 'var', 'div', 'center',
2131 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
2132 'ruby', 'rt' , 'rb' , 'rp', 'p'
2134 $htmlsingle = array(
2135 'br', 'hr', 'li', 'dt', 'dd'
2137 $htmlnest = array( # Tags that can be nested--??
2138 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
2139 'dl', 'font', 'big', 'small', 'sub', 'sup'
2141 $tabletags = array( # Can only appear inside table
2145 $htmlpairs = array();
2146 $htmlsingle = array();
2147 $htmlnest = array();
2148 $tabletags = array();
2151 $htmlsingle = array_merge( $tabletags, $htmlsingle );
2152 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
2154 $htmlattrs = $this->getHTMLattrs () ;
2156 # Remove HTML comments
2157 $text = $this->removeHTMLcomments( $text );
2159 $bits = explode( '<', $text );
2160 $text = array_shift( $bits );
2162 $tagstack = array(); $tablestack = array();
2163 foreach ( $bits as $x ) {
2164 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
2165 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2167 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2168 error_reporting( $prev );
2171 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2175 if ( ! in_array( $t, $htmlsingle ) &&
2176 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
2177 @array_push
( $tagstack, $ot );
2180 if ( $t == 'table' ) {
2181 $tagstack = array_pop( $tablestack );
2186 # Keep track for later
2187 if ( in_array( $t, $tabletags ) &&
2188 ! in_array( 'table', $tagstack ) ) {
2190 } else if ( in_array( $t, $tagstack ) &&
2191 ! in_array ( $t , $htmlnest ) ) {
2193 } else if ( ! in_array( $t, $htmlsingle ) ) {
2194 if ( $t == 'table' ) {
2195 array_push( $tablestack, $tagstack );
2196 $tagstack = array();
2198 array_push( $tagstack, $t );
2200 # Strip non-approved attributes from the tag
2201 $newparams = $this->fixTagAttributes($params);
2205 $rest = str_replace( '>', '>', $rest );
2206 $text .= "<$slash$t $newparams$brace$rest";
2210 $text .= '<' . str_replace( '>', '>', $x);
2212 # Close off any remaining tags
2213 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2215 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2218 # this might be possible using tidy itself
2219 foreach ( $bits as $x ) {
2220 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2222 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2223 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2224 $newparams = $this->fixTagAttributes($params);
2225 $rest = str_replace( '>', '>', $rest );
2226 $text .= "<$slash$t $newparams$brace$rest";
2228 $text .= '<' . str_replace( '>', '>', $x);
2232 wfProfileOut( $fname );
2237 * Remove '<!--', '-->', and everything between.
2238 * To avoid leaving blank lines, when a comment is both preceded
2239 * and followed by a newline (ignoring spaces), trim leading and
2240 * trailing spaces and one of the newlines.
2244 function removeHTMLcomments( $text ) {
2245 $fname='Parser::removeHTMLcomments';
2246 wfProfileIn( $fname );
2247 while (($start = strpos($text, '<!--')) !== false) {
2248 $end = strpos($text, '-->', $start +
4);
2249 if ($end === false) {
2250 # Unterminated comment; bail out
2256 # Trim space and newline if the comment is both
2257 # preceded and followed by a newline
2258 $spaceStart = max($start - 1, 0);
2259 $spaceLen = $end - $spaceStart;
2260 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2264 while (substr($text, $spaceStart +
$spaceLen, 1) === ' ')
2266 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart +
$spaceLen, 1) === "\n") {
2267 # Remove the comment, leading and trailing
2268 # spaces, and leave only one newline.
2269 $text = substr_replace($text, "\n", $spaceStart, $spaceLen +
1);
2272 # Remove just the comment.
2273 $text = substr_replace($text, '', $start, $end - $start);
2276 wfProfileOut( $fname );
2281 * This function accomplishes several tasks:
2282 * 1) Auto-number headings if that option is enabled
2283 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2284 * 3) Add a Table of contents on the top for users who have enabled the option
2285 * 4) Auto-anchor headings
2287 * It loops through all headlines, collects the necessary data, then splits up the
2288 * string and re-inserts the newly formatted headlines.
2291 /* private */ function formatHeadings( $text, $isMain=true ) {
2292 global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders;
2294 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
2295 $doShowToc = $this->mOptions
->getShowToc();
2296 $forceTocHere = false;
2297 if( !$this->mTitle
->userCanEdit() ) {
2299 $rightClickHack = 0;
2301 $showEditLink = $this->mOptions
->getEditSection();
2302 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
2305 # Inhibit editsection links if requested in the page
2306 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
2307 if( $esw->matchAndRemove( $text ) ) {
2310 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2312 $mw =& MagicWord
::get( MAG_NOTOC
);
2313 if( $mw->matchAndRemove( $text ) ) {
2317 # never add the TOC to the Main Page. This is an entry page that should not
2318 # be more than 1-2 screens large anyway
2319 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
2323 # Get all headlines for numbering them and adding funky stuff like [edit]
2324 # links - this is for later, but we need the number of headlines right now
2325 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2327 # if there are fewer than 4 headlines in the article, do not show TOC
2328 if( $numMatches < 4 ) {
2332 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2333 # override above conditions and always show TOC at that place
2334 $mw =& MagicWord
::get( MAG_TOC
);
2335 if ($mw->match( $text ) ) {
2337 $forceTocHere = true;
2339 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2340 # override above conditions and always show TOC above first header
2341 $mw =& MagicWord
::get( MAG_FORCETOC
);
2342 if ($mw->matchAndRemove( $text ) ) {
2349 # We need this to perform operations on the HTML
2350 $sk =& $this->mOptions
->getSkin();
2354 $sectionCount = 0; # headlineCount excluding template sections
2356 # Ugh .. the TOC should have neat indentation levels which can be
2357 # passed to the skin functions. These are determined here
2362 $sublevelCount = array();
2365 foreach( $matches[3] as $headline ) {
2367 $templatetitle = "";
2368 $templatesection = 0;
2370 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2372 $templatetitle = base64_decode($mat[1]);
2373 $templatesection = 1 +
(int)base64_decode($mat[2]);
2374 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2379 $prevlevel = $level;
2381 $level = $matches[1][$headlineCount];
2382 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
2383 # reset when we enter a new level
2384 $sublevelCount[$level] = 0;
2385 $toc .= $sk->tocIndent( $level - $prevlevel );
2386 $toclevel +
= $level - $prevlevel;
2388 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
2389 # reset when we step back a level
2390 $sublevelCount[$level+
1]=0;
2391 $toc .= $sk->tocUnindent( $prevlevel - $level );
2392 $toclevel -= $prevlevel - $level;
2394 # count number of headlines for each level
2395 @$sublevelCount[$level]++
;
2396 if( $doNumberHeadings ||
$doShowToc ) {
2398 for( $i = 1; $i <= $level; $i++
) {
2399 if( !empty( $sublevelCount[$i] ) ) {
2403 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
2409 # The canonized header is a version of the header text safe to use for links
2410 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2411 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
2412 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
2414 # Remove link placeholders by the link text.
2415 # <!--LINK number-->
2417 # link text with suffix
2418 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2419 "\$wgLinkHolders['texts'][\$1]",
2420 $canonized_headline );
2423 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2424 $tocline = trim( $canonized_headline );
2425 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
2426 $replacearray = array(
2430 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2431 $refer[$headlineCount] = $canonized_headline;
2433 # count how many in assoc. array so we can track dupes in anchors
2434 @$refers[$canonized_headline]++
;
2435 $refcount[$headlineCount]=$refers[$canonized_headline];
2437 # Prepend the number to the heading text
2439 if( $doNumberHeadings ||
$doShowToc ) {
2440 $tocline = $numbering . ' ' . $tocline;
2442 # Don't number the heading if it is the only one (looks silly)
2443 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2444 # the two are different if the line contains a link
2445 $headline=$numbering . ' ' . $headline;
2449 # Create the anchor for linking from the TOC to the section
2450 $anchor = $canonized_headline;
2451 if($refcount[$headlineCount] > 1 ) {
2452 $anchor .= '_' . $refcount[$headlineCount];
2454 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2455 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2457 if( $showEditLink && ( !$istemplate ||
$templatetitle !== "" ) ) {
2458 if ( empty( $head[$headlineCount] ) ) {
2459 $head[$headlineCount] = '';
2462 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2464 $head[$headlineCount] .= $sk->editSectionLink($sectionCount+
1);
2467 # Add the edit section span
2468 if( $rightClickHack ) {
2470 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2472 $headline = $sk->editSectionScript($sectionCount+
1,$headline);
2475 # give headline the correct <h#> tag
2476 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2484 $toclines = $headlineCount;
2485 $toc .= $sk->tocUnindent( $toclevel );
2486 $toc = $sk->tocTable( $toc );
2489 # split up and insert constructed headlines
2491 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2494 foreach( $blocks as $block ) {
2495 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2496 # This is the [edit] link that appears for the top block of text when
2497 # section editing is enabled
2499 # Disabled because it broke block formatting
2500 # For example, a bullet point in the top line
2501 # $full .= $sk->editSectionLink(0);
2504 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2505 # Top anchor now in skin
2509 if( !empty( $head[$i] ) ) {
2515 $mw =& MagicWord
::get( MAG_TOC
);
2516 return $mw->replace( $toc, $full );
2523 * Return an HTML link for the "ISBN 123456" text
2526 function magicISBN( $text ) {
2528 $fname = 'Parser::magicISBN';
2529 wfProfileIn( $fname );
2531 $a = split( 'ISBN ', ' '.$text );
2532 if ( count ( $a ) < 2 ) {
2533 wfProfileOut( $fname );
2536 $text = substr( array_shift( $a ), 1);
2537 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2539 foreach ( $a as $x ) {
2540 $isbn = $blank = '' ;
2541 while ( ' ' == $x{0} ) {
2543 $x = substr( $x, 1 );
2545 if ( $x == '' ) { # blank isbn
2546 $text .= "ISBN $blank";
2549 while ( strstr( $valid, $x{0} ) != false ) {
2551 $x = substr( $x, 1 );
2553 $num = str_replace( '-', '', $isbn );
2554 $num = str_replace( ' ', '', $num );
2557 $text .= "ISBN $blank$x";
2559 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2560 $text .= '<a href="' .
2561 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2562 "\" class=\"internal\">ISBN $isbn</a>";
2566 wfProfileOut( $fname );
2571 * Return an HTML link for the "GEO ..." text
2574 function magicGEO( $text ) {
2575 global $wgLang, $wgUseGeoMode;
2576 $fname = 'Parser::magicGEO';
2577 wfProfileIn( $fname );
2579 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2580 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2581 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2582 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2583 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2584 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2586 $a = split( 'GEO ', ' '.$text );
2587 if ( count ( $a ) < 2 ) {
2588 wfProfileOut( $fname );
2591 $text = substr( array_shift( $a ), 1);
2592 $valid = '0123456789.+-:';
2594 foreach ( $a as $x ) {
2595 $geo = $blank = '' ;
2596 while ( ' ' == $x{0} ) {
2598 $x = substr( $x, 1 );
2600 while ( strstr( $valid, $x{0} ) != false ) {
2602 $x = substr( $x, 1 );
2604 $num = str_replace( '+', '', $geo );
2605 $num = str_replace( ' ', '', $num );
2607 if ( '' == $num ||
count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2608 $text .= "GEO $blank$x";
2610 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2611 $text .= '<a href="' .
2612 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2613 "\" class=\"internal\">GEO $geo</a>";
2617 wfProfileOut( $fname );
2622 * Return an HTML link for the "RFC 1234" text
2624 * @param string $text text to be processed
2626 function magicRFC( $text ) {
2629 $valid = '0123456789';
2632 $a = split( 'RFC ', ' '.$text );
2633 if ( count ( $a ) < 2 ) return $text;
2634 $text = substr( array_shift( $a ), 1);
2636 /* Check if RFC keyword is preceed by [[.
2637 * This test is made here cause of the array_shift above
2638 * that prevent the test to be done in the foreach.
2640 if(substr($text, -2) == '[[') { $internal = true; }
2642 foreach ( $a as $x ) {
2643 /* token might be empty if we have RFC RFC 1234 */
2649 $rfc = $blank = '' ;
2651 /** remove and save whitespaces in $blank */
2652 while ( $x{0} == ' ' ) {
2654 $x = substr( $x, 1 );
2657 /** remove and save the rfc number in $rfc */
2658 while ( strstr( $valid, $x{0} ) != false ) {
2660 $x = substr( $x, 1 );
2664 /* call back stripped spaces*/
2665 $text .= "RFC $blank$x";
2666 } elseif( $internal) {
2668 $text .= "RFC $rfc$x";
2670 /* build the external link*/
2671 $url = wfmsg( 'rfcurl' );
2672 $url = str_replace( '$1', $rfc, $url);
2673 $sk =& $this->mOptions
->getSkin();
2674 $la = $sk->getExternalLinkAttributes( $url, 'RFC '.$rfc );
2675 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2678 /* Check if the next RFC keyword is preceed by [[ */
2679 $internal = (substr($x,-2) == '[[');
2685 * Transform wiki markup when saving a page by doing \r\n -> \n
2686 * conversion, substitting signatures, {{subst:}} templates, etc.
2688 * @param string $text the text to transform
2689 * @param Title &$title the Title object for the current article
2690 * @param User &$user the User object describing the current user
2691 * @param ParserOptions $options parsing options
2692 * @param bool $clearState whether to clear the parser state first
2693 * @return string the altered wiki markup
2696 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2697 $this->mOptions
= $options;
2698 $this->mTitle
=& $title;
2699 $this->mOutputType
= OT_WIKI
;
2701 if ( $clearState ) {
2702 $this->clearState();
2705 $stripState = false;
2709 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2713 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2714 "/<br *?>/i" => "<br />",
2716 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2718 $text = $this->strip( $text, $stripState, false );
2719 $text = $this->pstPass2( $text, $user );
2720 $text = $this->unstrip( $text, $stripState );
2721 $text = $this->unstripNoWiki( $text, $stripState );
2726 * Pre-save transform helper function
2729 function pstPass2( $text, &$user ) {
2730 global $wgLang, $wgContLang, $wgLocaltimezone, $wgCurParser;
2732 # Variable replacement
2733 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2734 $text = $this->replaceVariables( $text );
2738 $n = $user->getName();
2739 $k = $user->getOption( 'nickname' );
2740 if ( '' == $k ) { $k = $n; }
2741 if(isset($wgLocaltimezone)) {
2742 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2744 /* Note: this is an ugly timezone hack for the European wikis */
2745 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
2746 ' (' . date( 'T' ) . ')';
2747 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2749 $text = preg_replace( '/~~~~~/', $d, $text );
2750 $text = preg_replace( '/~~~~/', '[[' . $wgContLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2751 $text = preg_replace( '/~~~/', '[[' . $wgContLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2753 # Context links: [[|name]] and [[name (context)|]]
2755 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2756 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2757 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2758 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2760 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2761 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2762 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2763 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2765 $t = $this->mTitle
->getText();
2766 if ( preg_match( $conpat, $t, $m ) ) {
2769 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2770 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2771 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2773 if ( '' == $context ) {
2774 $text = preg_replace( $p2, '[[\\1]]', $text );
2776 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2779 # Trim trailing whitespace
2780 # MAG_END (__END__) tag allows for trailing
2781 # whitespace to be deliberately included
2782 $text = rtrim( $text );
2783 $mw =& MagicWord
::get( MAG_END
);
2784 $mw->matchAndRemove( $text );
2790 * Set up some variables which are usually set up in parse()
2791 * so that an external function can call some class members with confidence
2794 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2795 $this->mTitle
=& $title;
2796 $this->mOptions
= $options;
2797 $this->mOutputType
= $outputType;
2798 if ( $clearState ) {
2799 $this->clearState();
2804 * Transform a MediaWiki message by replacing magic variables.
2806 * @param string $text the text to transform
2807 * @param ParserOptions $options options
2808 * @return string the text with variables substituted
2811 function transformMsg( $text, $options ) {
2813 static $executing = false;
2815 # Guard against infinite recursion
2821 $this->mTitle
= $wgTitle;
2822 $this->mOptions
= $options;
2823 $this->mOutputType
= OT_MSG
;
2824 $this->clearState();
2825 $text = $this->replaceVariables( $text );
2832 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2833 * Callback will be called with the text within
2834 * Transform and return the text within
2837 function setHook( $tag, $callback ) {
2838 $oldVal = @$this->mTagHooks
[$tag];
2839 $this->mTagHooks
[$tag] = $callback;
2846 * @package MediaWiki
2850 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2851 var $mCacheTime; # Used in ParserCache
2853 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
2854 $containsOldMagic = false )
2856 $this->mText
= $text;
2857 $this->mLanguageLinks
= $languageLinks;
2858 $this->mCategoryLinks
= $categoryLinks;
2859 $this->mContainsOldMagic
= $containsOldMagic;
2860 $this->mCacheTime
= '';
2863 function getText() { return $this->mText
; }
2864 function getLanguageLinks() { return $this->mLanguageLinks
; }
2865 function getCategoryLinks() { return $this->mCategoryLinks
; }
2866 function getCacheTime() { return $this->mCacheTime
; }
2867 function containsOldMagic() { return $this->mContainsOldMagic
; }
2868 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2869 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2870 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2871 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2872 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2874 function merge( $other ) {
2875 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2876 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2877 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2883 * Set options of the Parser
2885 * @package MediaWiki
2889 # All variables are private
2890 var $mUseTeX; # Use texvc to expand <math> tags
2891 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2892 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2893 var $mAllowExternalImages; # Allow external images inline
2894 var $mSkin; # Reference to the preferred skin
2895 var $mDateFormat; # Date format index
2896 var $mEditSection; # Create "edit section" links
2897 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2898 var $mNumberHeadings; # Automatically number headings
2899 var $mShowToc; # Show table of contents
2901 function getUseTeX() { return $this->mUseTeX
; }
2902 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2903 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2904 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2905 function getSkin() { return $this->mSkin
; }
2906 function getDateFormat() { return $this->mDateFormat
; }
2907 function getEditSection() { return $this->mEditSection
; }
2908 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2909 function getNumberHeadings() { return $this->mNumberHeadings
; }
2910 function getShowToc() { return $this->mShowToc
; }
2912 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2913 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2914 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2915 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2916 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2917 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2918 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2919 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2920 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2922 function setSkin( &$x ) { $this->mSkin
=& $x; }
2924 # Get parser options
2925 /* static */ function newFromUser( &$user ) {
2926 $popts = new ParserOptions
;
2927 $popts->initialiseFromUser( $user );
2932 function initialiseFromUser( &$userInput ) {
2933 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2935 $fname = 'ParserOptions::initialiseFromUser';
2936 wfProfileIn( $fname );
2937 if ( !$userInput ) {
2939 $user->setLoaded( true );
2941 $user =& $userInput;
2944 $this->mUseTeX
= $wgUseTeX;
2945 $this->mUseDynamicDates
= $wgUseDynamicDates;
2946 $this->mInterwikiMagic
= $wgInterwikiMagic;
2947 $this->mAllowExternalImages
= $wgAllowExternalImages;
2948 wfProfileIn( $fname.'-skin' );
2949 $this->mSkin
=& $user->getSkin();
2950 wfProfileOut( $fname.'-skin' );
2951 $this->mDateFormat
= $user->getOption( 'date' );
2952 $this->mEditSection
= $user->getOption( 'editsection' );
2953 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2954 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2955 $this->mShowToc
= $user->getOption( 'showtoc' );
2956 wfProfileOut( $fname );
2962 # Regex callbacks, used in Parser::replaceVariables
2963 function wfBraceSubstitution( $matches ) {
2964 global $wgCurParser;
2965 return $wgCurParser->braceSubstitution( $matches );
2968 function wfArgSubstitution( $matches ) {
2969 global $wgCurParser;
2970 return $wgCurParser->argSubstitution( $matches );
2973 function wfVariableSubstitution( $matches ) {
2974 global $wgCurParser;
2975 return $wgCurParser->variableSubstitution( $matches );
2979 * Return the total number of articles
2981 function wfNumberOfArticles() {
2982 global $wgNumberOfArticles;
2985 return $wgNumberOfArticles;
2989 * Get various statistics from the database
2992 function wfLoadSiteStats() {
2993 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
2994 $fname = 'wfLoadSiteStats';
2996 if ( -1 != $wgNumberOfArticles ) return;
2997 $dbr =& wfGetDB( DB_SLAVE
);
2998 $s = $dbr->getArray( 'site_stats',
2999 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
3000 array( 'ss_row_id' => 1 ), $fname
3003 if ( $s === false ) {
3006 $wgTotalViews = $s->ss_total_views
;
3007 $wgTotalEdits = $s->ss_total_edits
;
3008 $wgNumberOfArticles = $s->ss_good_articles
;
3012 function wfEscapeHTMLTagsOnly( $in ) {
3014 array( '"', '>', '<' ),
3015 array( '"', '>', '<' ),