3 require_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 require_once('extensions/wikihiero/wikihiero.php');
8 if( $GLOBALS['wgUseTimeline'] ){
9 require_once('extensions/timeline/Timeline.php');
14 # Processes wiki markup
16 # There are two main entry points into the Parser class: parse() and preSaveTransform().
17 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
20 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
22 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
24 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
25 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
28 # * only within ParserOptions
31 #----------------------------------------
32 # Variable substitution O(N^2) attack
33 #-----------------------------------------
34 # Without countermeasures, it would be possible to attack the parser by saving a page
35 # filled with a large number of inclusions of large pages. The size of the generated
36 # page would be proportional to the square of the input size. Hence, we limit the number
37 # of inclusions of any given page, thus bringing any attack back to O(N).
40 define( "MAX_INCLUDE_REPEAT", 5 );
42 # Allowed values for $mOutputType
43 define( "OT_HTML", 1 );
44 define( "OT_WIKI", 2 );
45 define( "OT_MSG", 3 );
47 # string parameter for extractTags which will cause it
48 # to strip HTML comments in addition to regular
49 # <XML>-style tags. This should not be anything we
50 # may want to use in wikisyntax
51 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
53 # prefix for escaping, used in two functions at least
54 define( "UNIQ_PREFIX", "NaodW29");
58 # Cleared with clearState():
59 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
60 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
63 var $mOptions, $mTitle, $mOutputType;
72 $this->mOutput
= new ParserOutput
;
73 $this->mAutonumber
= 0;
74 $this->mLastSection
= "";
75 $this->mDTopen
= false;
76 $this->mVariables
= false;
77 $this->mIncludeCount
= array();
78 $this->mStripState
= array();
79 $this->mArgStack
= array();
82 # First pass--just handle <nowiki> sections, pass the rest off
83 # to internalParse() which does all the real work.
85 # Returns a ParserOutput
87 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
90 $fname = "Parser::parse";
91 wfProfileIn( $fname );
97 $this->mOptions
= $options;
98 $this->mTitle
=& $title;
99 $this->mOutputType
= OT_HTML
;
102 $text = $this->strip( $text, $this->mStripState
);
103 $text = $this->internalParse( $text, $linestart );
104 $text = $this->unstrip( $text, $this->mStripState
);
105 # Clean up special characters, only run once, next-to-last before doBlockLevels
108 "/<hr *>/i" => '<hr/>',
109 "/<br *>/i" => '<br/>',
110 "/<center *>/i"=>'<div class="center">',
111 "/<\\/center *>/i" => '</div>',
112 # Clean up spare ampersands; note that we probably ought to be
113 # more careful about named entities.
114 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
116 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
119 "/<center *>/i"=>'<div class="center">',
120 "/<\\/center *>/i" => '</div>'
122 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
125 $text = $this->doBlockLevels( $text, $linestart );
127 $text = $this->tidy($text);
129 $this->mOutput
->setText( $text );
130 wfProfileOut( $fname );
131 return $this->mOutput
;
134 /* static */ function getRandomString()
136 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
139 # Replaces all occurrences of <$tag>content</$tag> in the text
140 # with a random marker and returns the new text. the output parameter
141 # $content will be an associative array filled with data on the form
142 # $unique_marker => content.
144 # If $content is already set, the additional entries will be appended
146 # If $tag is set to STRIP_COMMENTS, the function will extract
147 # <!-- HTML comments -->
149 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
150 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
157 while ( "" != $text ) {
158 if($tag==STRIP_COMMENTS
) {
159 $p = preg_split( "/<!--/i", $text, 2 );
161 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
164 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
167 if($tag==STRIP_COMMENTS
) {
168 $q = preg_split( "/-->/i", $p[1], 2 );
170 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
172 $marker = $rnd . sprintf("%08X", $n++
);
173 $content[$marker] = $q[0];
174 $stripped .= $marker;
181 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
182 # If $render is set, performs necessary rendering operations on plugins
183 # Returns the text, and fills an array with data needed in unstrip()
184 # If the $state is already a valid strip state, it adds to the state
186 # When $stripcomments is set, HTML comments <!-- like this -->
187 # will be stripped in addition to other tags. This is important
188 # for section editing, where these comments cause confusion when
189 # counting the sections in the wikisource
190 function strip( $text, &$state, $stripcomments = false )
192 $render = ($this->mOutputType
== OT_HTML
);
193 $nowiki_content = array();
194 $hiero_content = array();
195 $math_content = array();
196 $pre_content = array();
197 $comment_content = array();
199 # Replace any instances of the placeholders
200 $uniq_prefix = UNIQ_PREFIX
;
201 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
203 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
204 foreach( $nowiki_content as $marker => $content ){
206 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
208 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
212 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
213 foreach( $hiero_content as $marker => $content ){
214 if( $render && $GLOBALS['wgUseWikiHiero']){
215 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
217 $hiero_content[$marker] = "<hiero>$content</hiero>";
221 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
222 foreach( $math_content as $marker => $content ){
224 if( $this->mOptions
->getUseTeX() ) {
225 $math_content[$marker] = renderMath( $content );
227 $math_content[$marker] = "<math>$content<math>";
230 $math_content[$marker] = "<math>$content</math>";
234 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
235 foreach( $pre_content as $marker => $content ){
237 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
239 $pre_content[$marker] = "<pre>$content</pre>";
243 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
244 foreach( $comment_content as $marker => $content ){
245 $comment_content[$marker] = "<!--$content-->";
249 # Merge state with the pre-existing state, if there is one
251 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
252 $state['hiero'] = $state['hiero'] +
$hiero_content;
253 $state['math'] = $state['math'] +
$math_content;
254 $state['pre'] = $state['pre'] +
$pre_content;
255 $state['comment'] = $state['comment'] +
$comment_content;
258 'nowiki' => $nowiki_content,
259 'hiero' => $hiero_content,
260 'math' => $math_content,
261 'pre' => $pre_content,
262 'comment' => $comment_content
268 function unstrip( $text, &$state )
270 # Must expand in reverse order, otherwise nested tags will be corrupted
271 $contentDict = end( $state );
272 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
273 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
274 $text = str_replace( key( $contentDict ), $content, $text );
281 # Add an item to the strip state
282 # Returns the unique tag which must be inserted into the stripped text
283 # The tag will be replaced with the original text in unstrip()
285 function insertStripItem( $text, &$state )
287 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
296 $state['item'][$rnd] = $text;
300 # This method generates the list of subcategories and pages for a category
301 function categoryMagic ()
303 global $wgLang , $wgUser ;
304 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
306 $cns = Namespace::getCategory() ;
307 if ( $this->mTitle
->getNamespace() != $cns ) return "" ; # This ain't a category page
309 $r = "<br style=\"clear:both;\"/>\n";
312 $sk =& $wgUser->getSkin() ;
314 $articles = array() ;
315 $children = array() ;
317 $id = $this->mTitle
->getArticleID() ;
320 $t = wfStrencode( $this->mTitle
->getDBKey() );
321 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
322 $res = wfQuery ( $sql, DB_READ
) ;
323 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
325 # For all pages that link to this category
326 foreach ( $data AS $x )
328 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
329 if ( $t != "" ) $t .= ":" ;
330 $t .= $x->cur_title
;
332 if ( $x->cur_namespace
== $cns ) {
333 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
335 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
338 wfFreeResult ( $res ) ;
340 # Showing subcategories
341 if ( count ( $children ) > 0 ) {
342 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
343 $r .= implode ( ", " , $children ) ;
346 # Showing pages in this category
347 if ( count ( $articles ) > 0 ) {
348 $ti = $this->mTitle
->getText() ;
349 $h = wfMsg( "category_header", $ti );
350 $r .= "<h2>{$h}</h2>\n" ;
351 $r .= implode ( ", " , $articles ) ;
358 function getHTMLattrs ()
360 $htmlattrs = array( # Allowed attributes--no scripting, etc.
361 "title", "align", "lang", "dir", "width", "height",
362 "bgcolor", "clear", /* BR */ "noshade", /* HR */
363 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
364 /* FONT */ "type", "start", "value", "compact",
365 /* For various lists, mostly deprecated but safe */
366 "summary", "width", "border", "frame", "rules",
367 "cellspacing", "cellpadding", "valign", "char",
368 "charoff", "colgroup", "col", "span", "abbr", "axis",
369 "headers", "scope", "rowspan", "colspan", /* Tables */
370 "id", "class", "name", "style" /* For CSS */
375 function fixTagAttributes ( $t )
377 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
378 $htmlattrs = $this->getHTMLattrs() ;
380 # Strip non-approved attributes from the tag
382 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
383 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
385 # Strip javascript "expression" from stylesheets. Brute force approach:
386 # If anythin offensive is found, all attributes of the HTML tag are dropped
389 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
390 wfMungeToUtf8( $t ) ) )
398 /* interface with html tidy, used if $wgUseTidy = true */
399 function tidy ( $text ) {
400 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
401 global $wgInputEncoding, $wgOutputEncoding;
403 switch(strtoupper($wgOutputEncoding)) {
405 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
408 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
411 $wgTidyOpts .= ' -raw';
414 $text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
415 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
416 '<head><title>test</title></head><body>'.$text.'</body></html>';
417 $descriptorspec = array(
418 0 => array("pipe", "r"),
419 1 => array("pipe", "w"),
420 2 => array("file", "/dev/null", "a")
422 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
423 if (is_resource($process)) {
424 fwrite($pipes[0], $text);
426 while (!feof($pipes[1])) {
427 $cleansource .= fgets($pipes[1], 1024);
430 $return_value = proc_close($process);
432 if( $cleansource == '' && $text != '') {
433 return '<h2>'.wfMsg('seriousxhtmlerrors').'</h2><pre>'.htmlspecialchars($text).'</pre>';
439 function doTableStuff ( $t )
441 $t = explode ( "\n" , $t ) ;
442 $td = array () ; # Is currently a td tag open?
443 $ltd = array () ; # Was it TD or TH?
444 $tr = array () ; # Is currently a tr tag open?
445 $ltr = array () ; # tr attributes
446 foreach ( $t AS $k => $x )
449 $fc = substr ( $x , 0 , 1 ) ;
450 if ( "{|" == substr ( $x , 0 , 2 ) )
452 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
453 array_push ( $td , false ) ;
454 array_push ( $ltd , "" ) ;
455 array_push ( $tr , false ) ;
456 array_push ( $ltr , "" ) ;
458 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
459 else if ( "|}" == substr ( $x , 0 , 2 ) )
462 $l = array_pop ( $ltd ) ;
463 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
464 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
468 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
470 $z = trim ( substr ( $x , 2 ) ) ;
471 $t[$k] = "<caption>{$z}</caption>\n" ;
473 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
475 $x = substr ( $x , 1 ) ;
476 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
478 $l = array_pop ( $ltd ) ;
479 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
480 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
483 array_push ( $tr , false ) ;
484 array_push ( $td , false ) ;
485 array_push ( $ltd , "" ) ;
486 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
488 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
490 if ( "|+" == substr ( $x , 0 , 2 ) )
493 $x = substr ( $x , 1 ) ;
495 $after = substr ( $x , 1 ) ;
496 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
497 $after = explode ( "||" , $after ) ;
499 foreach ( $after AS $theline )
504 $tra = array_pop ( $ltr ) ;
505 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
506 array_push ( $tr , true ) ;
507 array_push ( $ltr , "" ) ;
510 $l = array_pop ( $ltd ) ;
511 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
512 if ( $fc == "|" ) $l = "td" ;
513 else if ( $fc == "!" ) $l = "th" ;
514 else if ( $fc == "+" ) $l = "caption" ;
516 array_push ( $ltd , $l ) ;
517 $y = explode ( "|" , $theline , 2 ) ;
518 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
519 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
521 array_push ( $td , true ) ;
526 # Closing open td, tr && table
527 while ( count ( $td ) > 0 )
529 if ( array_pop ( $td ) ) $t[] = "</td>" ;
530 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
534 $t = implode ( "\n" , $t ) ;
535 # $t = $this->removeHTMLtags( $t );
539 function internalParse( $text, $linestart, $args = array(), $isMain=true )
541 $fname = "Parser::internalParse";
542 wfProfileIn( $fname );
544 $text = $this->removeHTMLtags( $text );
545 $text = $this->replaceVariables( $text, $args );
547 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
549 $text = $this->doHeadings( $text );
550 if($this->mOptions
->getUseDynamicDates()) {
551 global $wgDateFormatter;
552 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
554 $text = $this->replaceExternalLinks( $text );
555 $text = $this->doTokenizedParser ( $text );
556 $text = $this->doTableStuff ( $text ) ;
557 $text = $this->formatHeadings( $text, $isMain );
558 $sk =& $this->mOptions
->getSkin();
559 $text = $sk->transformContent( $text );
561 if ( !isset ( $this->categoryMagicDone
) ) {
562 $text .= $this->categoryMagic () ;
563 $this->categoryMagicDone
= true ;
566 wfProfileOut( $fname );
571 /* private */ function doHeadings( $text )
573 for ( $i = 6; $i >= 1; --$i ) {
574 $h = substr( "======", 0, $i );
575 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
576 "<h{$i}>\\1</h{$i}>\\2", $text );
581 # Note: we have to do external links before the internal ones,
582 # and otherwise take great care in the order of things here, so
583 # that we don't end up interpreting some URLs twice.
585 /* private */ function replaceExternalLinks( $text )
587 $fname = "Parser::replaceExternalLinks";
588 wfProfileIn( $fname );
589 $text = $this->subReplaceExternalLinks( $text, "http", true );
590 $text = $this->subReplaceExternalLinks( $text, "https", true );
591 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
592 $text = $this->subReplaceExternalLinks( $text, "irc", false );
593 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
594 $text = $this->subReplaceExternalLinks( $text, "news", false );
595 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
596 wfProfileOut( $fname );
600 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
602 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
603 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
605 # this is the list of separators that should be ignored if they
606 # are the last character of an URL but that should be included
607 # if they occur within the URL, e.g. "go to www.foo.com, where .."
608 # in this case, the last comma should not become part of the URL,
609 # but in "www.foo.com/123,2342,32.htm" it should.
611 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
612 $images = "gif|png|jpg|jpeg";
614 # PLEASE NOTE: The curly braces { } are not part of the regex,
615 # they are interpreted as part of the string (used to tell PHP
616 # that the content of the string should be inserted there).
617 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
618 "((?i){$images})([^{$uc}]|$)/";
620 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
621 $sk =& $this->mOptions
->getSkin();
623 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
624 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
625 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
627 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
628 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
629 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
631 $s = str_replace( $unique, $protocol, $s );
633 $a = explode( "[{$protocol}:", " " . $s );
634 $s = array_shift( $a );
635 $s = substr( $s, 1 );
637 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
638 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
640 foreach ( $a as $line ) {
641 if ( preg_match( $e1, $line, $m ) ) {
642 $link = "{$protocol}:{$m[1]}";
644 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
645 else { $text = wfEscapeHTML( $link ); }
646 } else if ( preg_match( $e2, $line, $m ) ) {
647 $link = "{$protocol}:{$m[1]}";
651 $s .= "[{$protocol}:" . $line;
654 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
657 # Expand the URL for printable version
658 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
660 $la = $sk->getExternalLinkAttributes( $link, $text );
661 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
667 /* private */ function handle4Quotes( &$state, $token )
669 /* This one makes some assumptions.
670 * '''Caesar''''s army => <strong>Caesar</strong>'s army
671 * ''''Caesar'''' was a roman emperor => '<strong>Caesar</strong>' was a roman emperor
672 * These assumptions might be wrong, but any other assumption might be wrong, too.
674 if ( $state["strong"] !== false ) {
675 return $this->handle3Quotes( $state, $token ) . "'";
677 return "'" . $this->handle3Quotes( $state, $token );
682 /* private */ function handle3Quotes( &$state, $token )
684 if ( $state["strong"] !== false ) {
685 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
687 # ''' lala ''lala '''
688 $s = "</em></strong><em>";
692 $state["strong"] = FALSE;
695 $state["strong"] = $token["pos"];
700 /* private */ function handle2Quotes( &$state, $token )
702 if ( $state["em"] !== false ) {
703 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
705 # ''lala'''lala'' ....'''
706 $s = "</strong></em><strong>";
710 $state["em"] = FALSE;
713 $state["em"] = $token["pos"];
719 /* private */ function handle5Quotes( &$state, $token )
722 if ( $state["em"] !== false && $state["strong"] !== false ) {
723 if ( $state["em"] < $state["strong"] ) {
724 $s .= "</strong></em>";
726 $s .= "</em></strong>";
728 $state["strong"] = $state["em"] = FALSE;
729 } elseif ( $state["em"] !== false ) {
730 $s .= "</em><strong>";
731 $state["em"] = FALSE;
732 $state["strong"] = $token["pos"];
733 } elseif ( $state["strong"] !== false ) {
734 $s .= "</strong><em>";
735 $state["strong"] = FALSE;
736 $state["em"] = $token["pos"];
737 } else { # not $em and not $strong
738 $s .= "<strong><em>";
739 $state["strong"] = $state["em"] = $token["pos"];
744 /* private */ function doTokenizedParser( $str )
746 global $wgLang; # for language specific parser hook
747 global $wgUploadDirectory, $wgUseTimeline;
749 $tokenizer=Tokenizer
::newFromString( $str );
750 $tokenStack = array();
753 $state["em"] = FALSE;
754 $state["strong"] = FALSE;
758 # The tokenizer splits the text into tokens and returns them one by one.
759 # Every call to the tokenizer returns a new token.
760 while ( $token = $tokenizer->nextToken() )
762 switch ( $token["type"] )
765 # simple text with no further markup
766 $txt = $token["text"];
769 # Text that contains blanks that have to be converted to
770 # non-breakable spaces for French.
771 # U+202F NARROW NO-BREAK SPACE might be a better choice, but
772 # browser support for Unicode spacing is poor.
773 $txt = str_replace( " ", " ", $token["text"] );
776 # remember the tag opened with 3 [
780 # FIXME : Treat orphaned open tags (stack not empty when text is over)
782 array_push( $tokenStack, $token );
789 # get text from stack, glue it together, and call the code to handle a
792 if ( count( $tokenStack ) == 0 )
794 # stack empty. Found a ]] without an opening [[
798 $lastToken = array_pop( $tokenStack );
799 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
801 if( !empty( $lastToken["text"] ) ) {
802 $linkText = $lastToken["text"] . $linkText;
804 $lastToken = array_pop( $tokenStack );
807 $txt = $linkText ."]]";
809 if( isset( $lastToken["text"] ) ) {
810 $prefix = $lastToken["text"];
814 $nextToken = $tokenizer->previewToken();
815 if ( $nextToken["type"] == "text" )
817 # Preview just looks at it. Now we have to fetch it.
818 $nextToken = $tokenizer->nextToken();
819 $txt .= $nextToken["text"];
821 $txt = $this->handleInternalLink( $this->unstrip($txt,$this->mStripState
), $prefix );
823 # did the tag start with 3 [ ?
825 # show the first as text
831 $tagIsOpen = (count( $tokenStack ) != 0);
837 # This and the four next ones handle quotes
838 $txt = $this->handle3Quotes( $state, $token );
841 $txt = $this->handle2Quotes( $state, $token );
844 $txt = $this->handle5Quotes( $state, $token );
847 $txt = $this->handle4Quotes( $state, $token );
854 #heading- used to close all unbalanced bold or em tags in this section
856 if( $state['em'] !== false and
857 ( $state['strong'] === false or $state['em'] > $state['strong'] ) )
860 $state['em'] = false;
862 if ( $state['strong'] !== false ) $txt .= '</strong>';
863 if ( $state['em'] !== false ) $txt .= '</em>';
864 $state['strong'] = $state['em'] = false;
870 $txt = $this->doMagicRFC( $tokenizer );
877 $txt = $this->doMagicISBN( $tokenizer );
881 if ( $wgUseTimeline &&
882 "" != ( $timelinesrc = $tokenizer->readAllUntil("</timeline>") ) )
884 $txt = renderTimeline( $timelinesrc );
890 # Call language specific Hook.
891 $txt = $wgLang->processToken( $token, $tokenStack );
892 if ( NULL == $txt ) {
893 # An unkown token. Highlight.
894 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
895 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
899 # If we're parsing the interior of a link, don't append the interior to $s,
900 # but push it to the stack so it can be processed when a ]] token is found.
901 if ( $tagIsOpen && $txt != "" ) {
902 $token["type"] = "text";
903 $token["text"] = $txt;
904 array_push( $tokenStack, $token );
910 # make 100% sure all strong and em tags are closed
911 # doBlockLevels often messes the last bit up though, but invalid nesting is better than unclosed tags
912 # tidy solves this though
913 if( $state['em'] !== false and
914 ( $state['strong'] === false or $state['em'] > $state['strong'] ) )
917 $state['em'] = false;
919 if ( $state['strong'] !== false ) $s .= '</strong>';
920 if ( $state['em'] !== false ) $s .= '</em>';
922 if ( count( $tokenStack ) != 0 )
924 # still objects on stack. opened [[ tag without closing ]] tag.
926 while ( $lastToken = array_pop( $tokenStack ) )
928 if ( $lastToken["type"] == "text" )
930 $txt = $lastToken["text"] . $txt;
932 $txt = $lastToken["type"] . $txt;
940 /* private */ function handleInternalLink( $line, $prefix )
942 global $wgLang, $wgLinkCache;
943 global $wgNamespacesWithSubpages, $wgLanguageCode;
944 static $fname = "Parser::handleInternalLink" ;
945 wfProfileIn( $fname );
947 wfProfileIn( "$fname-setup" );
949 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
950 $sk =& $this->mOptions
->getSkin();
952 # Match a link having the form [[namespace:link|alternate]]trail
954 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
955 # Match the end of a line for a word that's not followed by whitespace,
956 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
957 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
958 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
959 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
962 # Special and Media are pseudo-namespaces; no pages actually exist in them
963 static $image = FALSE;
964 static $special = FALSE;
965 static $media = FALSE;
966 static $category = FALSE;
967 if ( !$image ) { $image = Namespace::getImage(); }
968 if ( !$special ) { $special = Namespace::getSpecial(); }
969 if ( !$media ) { $media = Namespace::getMedia(); }
970 if ( !$category ) { $category = Namespace::getCategory(); }
972 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
974 wfProfileOut( "$fname-setup" );
977 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
980 } else { # Invalid form; output directly
981 $s .= $prefix . "[[" . $line ;
987 :Foobar -- override special treatment of prefix (images, language links)
988 /Foobar -- convert to CurrentPage/Foobar
989 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
991 $c = substr($m[1],0,1);
992 $noforce = ($c != ":");
993 if( $c == "/" ) { # subpage
994 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
995 $m[1]=substr($m[1],1,strlen($m[1])-2);
998 $noslash=substr($m[1],1);
1000 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
1001 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
1004 } # this might be changed for ugliness reasons
1006 $link = $noslash; # no subpage allowed, use standard link
1008 } elseif( $noforce ) { # no subpage
1011 $link = substr( $m[1], 1 );
1013 $wasblank = ( "" == $text );
1017 $nt = Title
::newFromText( $link );
1019 $s .= $prefix . "[[" . $line;
1022 $ns = $nt->getNamespace();
1023 $iw = $nt->getInterWiki();
1025 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1026 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
1027 $s .= $prefix . $trail ;
1028 return (trim($s) == '')?
'': $s;
1030 if( $ns == $image ) {
1031 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1032 $wgLinkCache->addImageLinkObj( $nt );
1035 if ( $ns == $category ) {
1036 $t = $nt->getText() ;
1037 $nnt = Title
::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1039 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1040 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
1041 $wgLinkCache->resume();
1043 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
1044 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1045 $this->mOutput
->mCategoryLinks
[] = $t ;
1046 $s .= $prefix . $trail ;
1050 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1051 ( strpos( $link, "#" ) == FALSE ) ) {
1052 # Self-links are handled specially; generally de-link and change to bold.
1053 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, "", $trail );
1057 if( $ns == $media ) {
1058 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1059 $wgLinkCache->addImageLinkObj( $nt );
1061 } elseif( $ns == $special ) {
1062 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
1065 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
1067 wfProfileOut( $fname );
1071 # Some functions here used by doBlockLevels()
1073 /* private */ function closeParagraph()
1076 if ( '' != $this->mLastSection
) {
1077 $result = "</" . $this->mLastSection
. ">\n";
1079 $this->mInPre
= false;
1080 $this->mLastSection
= "";
1083 # getCommon() returns the length of the longest common substring
1084 # of both arguments, starting at the beginning of both.
1086 /* private */ function getCommon( $st1, $st2 )
1088 $fl = strlen( $st1 );
1089 $shorter = strlen( $st2 );
1090 if ( $fl < $shorter ) { $shorter = $fl; }
1092 for ( $i = 0; $i < $shorter; ++
$i ) {
1093 if ( $st1{$i} != $st2{$i} ) { break; }
1097 # These next three functions open, continue, and close the list
1098 # element appropriate to the prefix character passed into them.
1100 /* private */ function openList( $char )
1102 $result = $this->closeParagraph();
1104 if ( "*" == $char ) { $result .= "<ul><li>"; }
1105 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1106 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1107 else if ( ";" == $char ) {
1108 $result .= "<dl><dt>";
1109 $this->mDTopen
= true;
1111 else { $result = "<!-- ERR 1 -->"; }
1116 /* private */ function nextItem( $char )
1118 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
1119 else if ( ":" == $char ||
";" == $char ) {
1121 if ( $this->mDTopen
) { $close = "</dt>"; }
1122 if ( ";" == $char ) {
1123 $this->mDTopen
= true;
1124 return $close . "<dt>";
1126 $this->mDTopen
= false;
1127 return $close . "<dd>";
1130 return "<!-- ERR 2 -->";
1133 /* private */function closeList( $char )
1135 if ( "*" == $char ) { $text = "</li></ul>"; }
1136 else if ( "#" == $char ) { $text = "</li></ol>"; }
1137 else if ( ":" == $char ) {
1138 if ( $this->mDTopen
) {
1139 $this->mDTopen
= false;
1140 $text = "</dt></dl>";
1142 $text = "</dd></dl>";
1145 else { return "<!-- ERR 3 -->"; }
1149 /* private */ function doBlockLevels( $text, $linestart ) {
1150 $fname = "Parser::doBlockLevels";
1151 wfProfileIn( $fname );
1153 # Parsing through the text line by line. The main thing
1154 # happening here is handling of block-level elements p, pre,
1155 # and making lists from lines starting with * # : etc.
1157 $textLines = explode( "\n", $text );
1159 $lastPrefix = $output = $lastLine = '';
1160 $this->mDTopen
= $inBlockElem = false;
1162 $paragraphStack = false;
1164 if ( !$linestart ) {
1165 $output .= array_shift( $textLines );
1167 foreach ( $textLines as $oLine ) {
1168 $lastPrefixLength = strlen( $lastPrefix );
1169 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1170 $preOpenMatch = preg_match("/<pre/i", $oLine );
1171 if (!$this->mInPre
) {
1172 $this->mInPre
= !empty($preOpenMatch);
1174 if ( !$this->mInPre
) {
1175 # Multiple prefixes may abut each other for nested lists.
1176 $prefixLength = strspn( $oLine, "*#:;" );
1177 $pref = substr( $oLine, 0, $prefixLength );
1180 $pref2 = str_replace( ";", ":", $pref );
1181 $t = substr( $oLine, $prefixLength );
1183 # Don't interpret any other prefixes in preformatted text
1185 $pref = $pref2 = '';
1190 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1191 # Same as the last item, so no need to deal with nesting or opening stuff
1192 $output .= $this->nextItem( substr( $pref, -1 ) );
1193 $paragraphStack = false;
1195 if ( ";" == substr( $pref, -1 ) ) {
1196 # The one nasty exception: definition lists work like this:
1197 # ; title : definition text
1198 # So we check for : in the remainder text to split up the
1199 # title and definition, without b0rking links.
1200 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1201 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1203 $output .= $term . $this->nextItem( ":" );
1207 } elseif( $prefixLength ||
$lastPrefixLength ) {
1208 # Either open or close a level...
1209 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1210 $paragraphStack = false;
1212 while( $commonPrefixLength < $lastPrefixLength ) {
1213 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1214 --$lastPrefixLength;
1216 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1217 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1219 while ( $prefixLength > $commonPrefixLength ) {
1220 $char = substr( $pref, $commonPrefixLength, 1 );
1221 $output .= $this->openList( $char );
1223 if ( ";" == $char ) {
1224 # FIXME: This is dupe of code above
1225 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1227 $output .= $term . $this->nextItem( ":" );
1231 ++
$commonPrefixLength;
1233 $lastPrefix = $pref2;
1235 if( 0 == $prefixLength ) {
1236 # No prefix (not in list)--go to paragraph mode
1237 $uniq_prefix = UNIQ_PREFIX
;
1238 // XXX: use a stack for nestable elements like span, table and div
1239 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1240 $closematch = preg_match(
1241 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1242 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1243 if ( $openmatch or $closematch ) {
1244 $paragraphStack = false;
1245 $output .= $this->closeParagraph();
1246 if($preOpenMatch and !$preCloseMatch) {
1247 $this->mInPre
= true;
1249 if ( $closematch ) {
1250 $inBlockElem = false;
1252 $inBlockElem = true;
1254 } else if ( !$inBlockElem ) {
1255 if ( " " == $t{0} and trim($t) != '' ) {
1257 if ($this->mLastSection
!= 'pre') {
1258 $paragraphStack = false;
1259 $output .= $this->closeParagraph().'<pre>';
1260 $this->mLastSection
= 'pre';
1264 if ( '' == trim($t) ) {
1265 if ( $paragraphStack ) {
1266 $output .= $paragraphStack.'<br/>';
1267 $paragraphStack = false;
1268 $this->mLastSection
= 'p';
1270 if ($this->mLastSection
!= 'p' ) {
1271 $output .= $this->closeParagraph();
1272 $this->mLastSection
= '';
1273 $paragraphStack = "<p>";
1275 $paragraphStack = '</p><p>';
1279 if ( $paragraphStack ) {
1280 $output .= $paragraphStack;
1281 $paragraphStack = false;
1282 $this->mLastSection
= 'p';
1283 } else if ($this->mLastSection
!= 'p') {
1284 $output .= $this->closeParagraph().'<p>';
1285 $this->mLastSection
= 'p';
1291 if ($paragraphStack === false) {
1295 while ( $prefixLength ) {
1296 $output .= $this->closeList( $pref2{$prefixLength-1} );
1299 if ( "" != $this->mLastSection
) {
1300 $output .= "</" . $this->mLastSection
. ">";
1301 $this->mLastSection
= "";
1304 wfProfileOut( $fname );
1308 function getVariableValue( $index ) {
1309 global $wgLang, $wgSitename, $wgServer;
1312 case MAG_CURRENTMONTH
:
1314 case MAG_CURRENTMONTHNAME
:
1315 return $wgLang->getMonthName( date("n") );
1316 case MAG_CURRENTMONTHNAMEGEN
:
1317 return $wgLang->getMonthNameGen( date("n") );
1318 case MAG_CURRENTDAY
:
1321 return $this->mTitle
->getText();
1323 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1324 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1325 case MAG_CURRENTDAYNAME
:
1326 return $wgLang->getWeekdayName( date("w")+
1 );
1327 case MAG_CURRENTYEAR
:
1329 case MAG_CURRENTTIME
:
1330 return $wgLang->time( wfTimestampNow(), false );
1331 case MAG_NUMBEROFARTICLES
:
1332 return wfNumberOfArticles();
1342 function initialiseVariables()
1344 global $wgVariableIDs;
1345 $this->mVariables
= array();
1346 foreach ( $wgVariableIDs as $id ) {
1347 $mw =& MagicWord
::get( $id );
1348 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1352 /* private */ function replaceVariables( $text, $args = array() )
1354 global $wgLang, $wgScript, $wgArticlePath;
1356 $fname = "Parser::replaceVariables";
1357 wfProfileIn( $fname );
1360 if ( !$this->mVariables
) {
1361 $this->initialiseVariables();
1363 $titleChars = Title
::legalChars();
1364 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1366 # This function is called recursively. To keep track of arguments we need a stack:
1367 array_push( $this->mArgStack
, $args );
1369 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1370 $GLOBALS['wgCurParser'] =& $this;
1371 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1373 array_pop( $this->mArgStack
);
1378 function braceSubstitution( $matches )
1380 global $wgLinkCache, $wgLang;
1381 $fname = "Parser::braceSubstitution";
1386 # $newline is an optional newline character before the braces
1387 # $part1 is the bit before the first |, and must contain only title characters
1388 # $args is a list of arguments, starting from index 0, not including $part1
1390 $newline = $matches[1];
1391 $part1 = $matches[2];
1392 # If the third subpattern matched anything, it will start with |
1393 if ( $matches[3] !== "" ) {
1394 $args = explode( "|", substr( $matches[3], 1 ) );
1398 $argc = count( $args );
1401 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1402 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1403 if ( $this->mOutputType
!= OT_WIKI
) {
1404 # Invalid SUBST not replaced at PST time
1405 # Return without further processing
1406 $text = $matches[0];
1409 } elseif ( $this->mOutputType
== OT_WIKI
) {
1410 # SUBST not found in PST pass, do nothing
1411 $text = $matches[0];
1415 # MSG, MSGNW and INT
1418 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1419 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1422 # Remove obsolete MSG:
1423 $mwMsg =& MagicWord
::get( MAG_MSG
);
1424 $mwMsg->matchStartAndRemove( $part1 );
1427 # Check if it is an internal message
1428 $mwInt =& MagicWord
::get( MAG_INT
);
1429 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1430 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1431 $text = wfMsgReal( $part1, $args, true );
1439 # Check for NS: (namespace expansion)
1440 $mwNs = MagicWord
::get( MAG_NS
);
1441 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1442 if ( intval( $part1 ) ) {
1443 $text = $wgLang->getNsText( intval( $part1 ) );
1446 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1447 if ( !is_null( $index ) ) {
1448 $text = $wgLang->getNsText( $index );
1455 # LOCALURL and LOCALURLE
1457 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1458 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1460 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1461 $func = 'getLocalURL';
1462 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1463 $func = 'escapeLocalURL';
1468 if ( $func !== '' ) {
1469 $title = Title
::newFromText( $part1 );
1470 if ( !is_null( $title ) ) {
1472 $text = $title->$func( $args[0] );
1474 $text = $title->$func();
1481 # Internal variables
1482 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1483 $text = $this->mVariables
[$part1];
1485 $this->mOutput
->mContainsOldMagic
= true;
1488 # Arguments input from the caller
1489 $inputArgs = end( $this->mArgStack
);
1490 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1491 $text = $inputArgs[$part1];
1495 # Load from database
1497 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1498 if ( !is_null( $title ) && !$title->isExternal() ) {
1499 # Check for excessive inclusion
1500 $dbk = $title->getPrefixedDBkey();
1501 if ( $this->incrementIncludeCount( $dbk ) ) {
1502 $article = new Article( $title );
1503 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1504 if ( $articleContent !== false ) {
1506 $text = $articleContent;
1511 # If the title is valid but undisplayable, make a link to it
1512 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1513 $text = "[[" . $title->getPrefixedText() . "]]";
1519 # Recursive parsing, escaping and link table handling
1520 # Only for HTML output
1521 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1522 $text = wfEscapeWikiText( $text );
1523 } elseif ( $this->mOutputType
== OT_HTML
&& $found ) {
1524 # Clean up argument array
1525 $assocArgs = array();
1527 foreach( $args as $arg ) {
1528 $eqpos = strpos( $arg, "=" );
1529 if ( $eqpos === false ) {
1530 $assocArgs[$index++
] = $arg;
1532 $name = trim( substr( $arg, 0, $eqpos ) );
1533 $value = trim( substr( $arg, $eqpos+
1 ) );
1534 if ( $value === false ) {
1537 if ( $name !== false ) {
1538 $assocArgs[$name] = $value;
1543 # Do not enter included links in link table
1544 if ( !is_null( $title ) ) {
1545 $wgLinkCache->suspend();
1548 # Run full parser on the included text
1549 $text = $this->strip( $text, $this->mStripState
);
1550 $text = $this->internalParse( $text, (bool)$newline, $assocArgs, false );
1551 if(!empty($newline)) $text = "\n".$text;
1553 # Add the result to the strip state for re-inclusion after
1554 # the rest of the processing
1555 $text = $this->insertStripItem( $text, $this->mStripState
);
1557 # Resume the link cache and register the inclusion as a link
1558 if ( !is_null( $title ) ) {
1559 $wgLinkCache->resume();
1560 $wgLinkCache->addLinkObj( $title );
1571 # Returns true if the function is allowed to include this entity
1572 function incrementIncludeCount( $dbk )
1574 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1575 $this->mIncludeCount
[$dbk] = 0;
1577 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1585 # Cleans up HTML, removes dangerous tags and attributes
1586 /* private */ function removeHTMLtags( $text )
1588 global $wgUseTidy, $wgUserHtml;
1589 $fname = "Parser::removeHTMLtags";
1590 wfProfileIn( $fname );
1593 $htmlpairs = array( # Tags that must be closed
1594 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1595 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1596 "strike", "strong", "tt", "var", "div", "center",
1597 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1598 "ruby", "rt" , "rb" , "rp", "p"
1600 $htmlsingle = array(
1601 "br", "hr", "li", "dt", "dd"
1603 $htmlnest = array( # Tags that can be nested--??
1604 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1605 "dl", "font", "big", "small", "sub", "sup"
1607 $tabletags = array( # Can only appear inside table
1611 $htmlpairs = array();
1612 $htmlsingle = array();
1613 $htmlnest = array();
1614 $tabletags = array();
1617 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1618 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1620 $htmlattrs = $this->getHTMLattrs () ;
1622 # Remove HTML comments
1623 $text = preg_replace( "/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU", "$2", $text );
1625 $bits = explode( "<", $text );
1626 $text = array_shift( $bits );
1628 $tagstack = array(); $tablestack = array();
1629 foreach ( $bits as $x ) {
1630 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1631 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1633 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1634 error_reporting( $prev );
1637 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1641 if ( ! in_array( $t, $htmlsingle ) &&
1642 ( count($tagstack) && $ot = array_pop( $tagstack ) ) != $t ) {
1643 if(!empty($ot)) array_push( $tagstack, $ot );
1646 if ( $t == "table" ) {
1647 $tagstack = array_pop( $tablestack );
1652 # Keep track for later
1653 if ( in_array( $t, $tabletags ) &&
1654 ! in_array( "table", $tagstack ) ) {
1656 } else if ( in_array( $t, $tagstack ) &&
1657 ! in_array ( $t , $htmlnest ) ) {
1659 } else if ( ! in_array( $t, $htmlsingle ) ) {
1660 if ( $t == "table" ) {
1661 array_push( $tablestack, $tagstack );
1662 $tagstack = array();
1664 array_push( $tagstack, $t );
1666 # Strip non-approved attributes from the tag
1667 $newparams = $this->fixTagAttributes($params);
1671 $rest = str_replace( ">", ">", $rest );
1672 $text .= "<$slash$t $newparams$brace$rest";
1676 $text .= "<" . str_replace( ">", ">", $x);
1678 # Close off any remaining tags
1679 while ( $t = array_pop( $tagstack ) ) {
1681 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1684 # this might be possible using tidy itself
1685 foreach ( $bits as $x ) {
1686 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1688 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1689 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1690 $newparams = $this->fixTagAttributes($params);
1691 $rest = str_replace( ">", ">", $rest );
1692 $text .= "<$slash$t $newparams$brace$rest";
1694 $text .= "<" . str_replace( ">", ">", $x);
1698 wfProfileOut( $fname );
1705 * This function accomplishes several tasks:
1706 * 1) Auto-number headings if that option is enabled
1707 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1708 * 3) Add a Table of contents on the top for users who have enabled the option
1709 * 4) Auto-anchor headings
1711 * It loops through all headlines, collects the necessary data, then splits up the
1712 * string and re-inserts the newly formatted headlines.
1716 /* private */ function formatHeadings( $text, $isMain=true )
1718 global $wgInputEncoding;
1720 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1721 $doShowToc = $this->mOptions
->getShowToc();
1722 if( !$this->mTitle
->userCanEdit() ) {
1724 $rightClickHack = 0;
1726 $showEditLink = $this->mOptions
->getEditSection();
1727 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1730 # Inhibit editsection links if requested in the page
1731 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1732 if( $esw->matchAndRemove( $text ) ) {
1735 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1737 $mw =& MagicWord
::get( MAG_NOTOC
);
1738 if( $mw->matchAndRemove( $text ) ) {
1742 # never add the TOC to the Main Page. This is an entry page that should not
1743 # be more than 1-2 screens large anyway
1744 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1748 # Get all headlines for numbering them and adding funky stuff like [edit]
1749 # links - this is for later, but we need the number of headlines right now
1750 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1752 # if there are fewer than 4 headlines in the article, do not show TOC
1753 if( $numMatches < 4 ) {
1757 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1758 # override above conditions and always show TOC
1759 $mw =& MagicWord
::get( MAG_FORCETOC
);
1760 if ($mw->matchAndRemove( $text ) ) {
1765 # We need this to perform operations on the HTML
1766 $sk =& $this->mOptions
->getSkin();
1771 # Ugh .. the TOC should have neat indentation levels which can be
1772 # passed to the skin functions. These are determined here
1777 $sublevelCount = array();
1780 foreach( $matches[3] as $headline ) {
1783 $prevlevel = $level;
1785 $level = $matches[1][$headlineCount];
1786 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1787 # reset when we enter a new level
1788 $sublevelCount[$level] = 0;
1789 $toc .= $sk->tocIndent( $level - $prevlevel );
1790 $toclevel +
= $level - $prevlevel;
1792 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1793 # reset when we step back a level
1794 $sublevelCount[$level+
1]=0;
1795 $toc .= $sk->tocUnindent( $prevlevel - $level );
1796 $toclevel -= $prevlevel - $level;
1798 # count number of headlines for each level
1799 @$sublevelCount[$level]++
;
1800 if( $doNumberHeadings ||
$doShowToc ) {
1802 for( $i = 1; $i <= $level; $i++
) {
1803 if( !empty( $sublevelCount[$i] ) ) {
1807 $numbering .= $sublevelCount[$i];
1813 # The canonized header is a version of the header text safe to use for links
1814 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1815 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1818 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1819 $tocline = trim( $canonized_headline );
1820 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', urlencode( do_html_entity_decode( $tocline, ENT_COMPAT
, $wgInputEncoding ) ) );
1821 # strip out urlencoded (inserted for french spaces, e.g. first space in 'something : something')
1822 $canonized_headline = str_replace('%C2%A0','_', $canonized_headline);
1823 $refer[$headlineCount] = $canonized_headline;
1825 # count how many in assoc. array so we can track dupes in anchors
1826 @$refers[$canonized_headline]++
;
1827 $refcount[$headlineCount]=$refers[$canonized_headline];
1829 # Prepend the number to the heading text
1831 if( $doNumberHeadings ||
$doShowToc ) {
1832 $tocline = $numbering . " " . $tocline;
1834 # Don't number the heading if it is the only one (looks silly)
1835 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1836 # the two are different if the line contains a link
1837 $headline=$numbering . " " . $headline;
1841 # Create the anchor for linking from the TOC to the section
1842 $anchor = $canonized_headline;
1843 if($refcount[$headlineCount] > 1 ) {
1844 $anchor .= "_" . $refcount[$headlineCount];
1847 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1849 if( $showEditLink ) {
1850 if ( empty( $head[$headlineCount] ) ) {
1851 $head[$headlineCount] = "";
1853 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1856 # Add the edit section span
1857 if( $rightClickHack ) {
1858 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1861 # give headline the correct <h#> tag
1862 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1868 $toclines = $headlineCount;
1869 $toc .= $sk->tocUnindent( $toclevel );
1870 $toc = $sk->tocTable( $toc );
1873 # split up and insert constructed headlines
1875 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1878 foreach( $blocks as $block ) {
1879 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1880 # This is the [edit] link that appears for the top block of text when
1881 # section editing is enabled
1883 # Disabled because it broke block formatting
1884 # For example, a bullet point in the top line
1885 # $full .= $sk->editSectionLink(0);
1888 if( $doShowToc && !$i && $isMain) {
1889 # Top anchor now in skin
1893 if( !empty( $head[$i] ) ) {
1902 /* private */ function doMagicISBN( &$tokenizer )
1906 # Check whether next token is a text token
1907 # If yes, fetch it and convert the text into a
1908 # Special::BookSources link
1909 $token = $tokenizer->previewToken();
1910 while ( $token["type"] == "" )
1912 $tokenizer->nextToken();
1913 $token = $tokenizer->previewToken();
1915 if ( $token["type"] == "text" )
1917 $token = $tokenizer->nextToken();
1918 $x = $token["text"];
1919 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1921 $isbn = $blank = "" ;
1922 while ( " " == $x{0} ) {
1924 $x = substr( $x, 1 );
1926 while ( strstr( $valid, $x{0} ) != false ) {
1928 $x = substr( $x, 1 );
1930 $num = str_replace( "-", "", $isbn );
1931 $num = str_replace( " ", "", $num );
1934 $text = "ISBN $blank$x";
1936 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1937 $text = "<a href=\"" .
1938 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1939 "\" class=\"internal\">ISBN $isbn</a>";
1947 /* private */ function doMagicRFC( &$tokenizer )
1951 # Check whether next token is a text token
1952 # If yes, fetch it and convert the text into a
1953 # link to an RFC source
1954 $token = $tokenizer->previewToken();
1955 while ( $token["type"] == "" )
1957 $tokenizer->nextToken();
1958 $token = $tokenizer->previewToken();
1960 if ( $token["type"] == "text" )
1962 $token = $tokenizer->nextToken();
1963 $x = $token["text"];
1964 $valid = "0123456789";
1966 $rfc = $blank = "" ;
1967 while ( " " == $x{0} ) {
1969 $x = substr( $x, 1 );
1971 while ( strstr( $valid, $x{0} ) != false ) {
1973 $x = substr( $x, 1 );
1977 $text .= "RFC $blank$x";
1979 $url = wfmsg( "rfcurl" );
1980 $url = str_replace( "$1", $rfc, $url);
1981 $sk =& $this->mOptions
->getSkin();
1982 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1983 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1991 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1993 $this->mOptions
= $options;
1994 $this->mTitle
=& $title;
1995 $this->mOutputType
= OT_WIKI
;
1997 if ( $clearState ) {
1998 $this->clearState();
2001 $stripState = false;
2005 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2008 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2009 "/<br *?>/i" => "<br/>",
2011 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2012 $text = $this->strip( $text, $stripState, false );
2013 $text = $this->pstPass2( $text, $user );
2014 $text = $this->unstrip( $text, $stripState );
2018 /* private */ function pstPass2( $text, &$user )
2020 global $wgLang, $wgLocaltimezone, $wgCurParser;
2022 # Variable replacement
2023 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2024 $text = $this->replaceVariables( $text );
2028 $n = $user->getName();
2029 $k = $user->getOption( "nickname" );
2030 if ( "" == $k ) { $k = $n; }
2031 if(isset($wgLocaltimezone)) {
2032 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
2034 /* Note: this is an ugly timezone hack for the European wikis */
2035 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
2036 " (" . date( "T" ) . ")";
2037 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
2039 $text = preg_replace( "/~~~~~/", $d, $text );
2040 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
2041 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2042 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
2043 Namespace::getUser() ) . ":$n|$k]]", $text );
2045 # Context links: [[|name]] and [[name (context)|]]
2047 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2048 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2049 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2050 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2052 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2053 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2054 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2055 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2056 # [[ns:page (cont)|]]
2058 $t = $this->mTitle
->getText();
2059 if ( preg_match( $conpat, $t, $m ) ) {
2062 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
2063 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
2064 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
2066 if ( "" == $context ) {
2067 $text = preg_replace( $p2, "[[\\1]]", $text );
2069 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2073 $mw =& MagicWord::get( MAG_SUBST );
2074 $wgCurParser = $this->fork();
2075 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2076 $this->merge( $wgCurParser );
2079 # Trim trailing whitespace
2080 # MAG_END (__END__) tag allows for trailing
2081 # whitespace to be deliberately included
2082 $text = rtrim( $text );
2083 $mw =& MagicWord
::get( MAG_END
);
2084 $mw->matchAndRemove( $text );
2089 # Set up some variables which are usually set up in parse()
2090 # so that an external function can call some class members with confidence
2091 function startExternalParse( &$title, $options, $outputType, $clearState = true )
2093 $this->mTitle
=& $title;
2094 $this->mOptions
= $options;
2095 $this->mOutputType
= $outputType;
2096 if ( $clearState ) {
2097 $this->clearState();
2101 function transformMsg( $text, $options ) {
2103 static $executing = false;
2105 # Guard against infinite recursion
2111 $this->mTitle
= $wgTitle;
2112 $this->mOptions
= $options;
2113 $this->mOutputType
= OT_MSG
;
2114 $this->clearState();
2115 $text = $this->replaceVariables( $text );
2124 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2126 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2127 $containsOldMagic = false )
2129 $this->mText
= $text;
2130 $this->mLanguageLinks
= $languageLinks;
2131 $this->mCategoryLinks
= $categoryLinks;
2132 $this->mContainsOldMagic
= $containsOldMagic;
2135 function getText() { return $this->mText
; }
2136 function getLanguageLinks() { return $this->mLanguageLinks
; }
2137 function getCategoryLinks() { return $this->mCategoryLinks
; }
2138 function containsOldMagic() { return $this->mContainsOldMagic
; }
2139 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2140 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2141 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2142 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2144 function merge( $other ) {
2145 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2146 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2147 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2154 # All variables are private
2155 var $mUseTeX; # Use texvc to expand <math> tags
2156 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2157 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2158 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2159 var $mAllowExternalImages; # Allow external images inline
2160 var $mSkin; # Reference to the preferred skin
2161 var $mDateFormat; # Date format index
2162 var $mEditSection; # Create "edit section" links
2163 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2164 var $mNumberHeadings; # Automatically number headings
2165 var $mShowToc; # Show table of contents
2167 function getUseTeX() { return $this->mUseTeX
; }
2168 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2169 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2170 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2171 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2172 function getSkin() { return $this->mSkin
; }
2173 function getDateFormat() { return $this->mDateFormat
; }
2174 function getEditSection() { return $this->mEditSection
; }
2175 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2176 function getNumberHeadings() { return $this->mNumberHeadings
; }
2177 function getShowToc() { return $this->mShowToc
; }
2179 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2180 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2181 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2182 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2183 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2184 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
2185 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2186 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2187 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2188 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2189 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2191 /* static */ function newFromUser( &$user )
2193 $popts = new ParserOptions
;
2194 $popts->initialiseFromUser( $user );
2198 function initialiseFromUser( &$userInput )
2200 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2202 if ( !$userInput ) {
2204 $user->setLoaded( true );
2206 $user =& $userInput;
2209 $this->mUseTeX
= $wgUseTeX;
2210 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2211 $this->mUseDynamicDates
= $wgUseDynamicDates;
2212 $this->mInterwikiMagic
= $wgInterwikiMagic;
2213 $this->mAllowExternalImages
= $wgAllowExternalImages;
2214 $this->mSkin
=& $user->getSkin();
2215 $this->mDateFormat
= $user->getOption( "date" );
2216 $this->mEditSection
= $user->getOption( "editsection" );
2217 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
2218 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
2219 $this->mShowToc
= $user->getOption( "showtoc" );
2225 # Regex callbacks, used in Parser::replaceVariables
2226 function wfBraceSubstitution( $matches )
2228 global $wgCurParser;
2229 return $wgCurParser->braceSubstitution( $matches );