3 include_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 include_once('wikihiero.php');
8 if( $GLOBALS['wgUseTimeline'] ){
9 include_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 ){
223 if( $render && $this->mOptions
->getUseTeX() ){
224 $math_content[$marker] = renderMath( $content );
226 $math_content[$marker] = "<math>$content</math>";
230 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
231 foreach( $pre_content as $marker => $content ){
233 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
235 $pre_content[$marker] = "<pre>$content</pre>";
239 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
240 foreach( $comment_content as $marker => $content ){
241 $comment_content[$marker] = "<!--$content-->";
245 # Merge state with the pre-existing state, if there is one
247 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
248 $state['hiero'] = $state['hiero'] +
$hiero_content;
249 $state['math'] = $state['math'] +
$math_content;
250 $state['pre'] = $state['pre'] +
$pre_content;
251 $state['comment'] = $state['comment'] +
$comment_content;
254 'nowiki' => $nowiki_content,
255 'hiero' => $hiero_content,
256 'math' => $math_content,
257 'pre' => $pre_content,
258 'comment' => $comment_content
264 function unstrip( $text, &$state )
266 # Must expand in reverse order, otherwise nested tags will be corrupted
267 $contentDict = end( $state );
268 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
269 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
270 $text = str_replace( key( $contentDict ), $content, $text );
277 # Add an item to the strip state
278 # Returns the unique tag which must be inserted into the stripped text
279 # The tag will be replaced with the original text in unstrip()
281 function insertStripItem( $text, &$state )
283 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
292 $state['item'][$rnd] = $text;
296 # This method generates the list of subcategories and pages for a category
297 function categoryMagic ()
299 global $wgLang , $wgUser ;
300 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
302 $cns = Namespace::getCategory() ;
303 if ( $this->mTitle
->getNamespace() != $cns ) return "" ; # This ain't a category page
305 $r = "<br style=\"clear:both;\"/>\n";
308 $sk =& $wgUser->getSkin() ;
310 $articles = array() ;
311 $children = array() ;
313 $id = $this->mTitle
->getArticleID() ;
315 # For existing categories
317 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
318 $res = wfQuery ( $sql, DB_READ
) ;
319 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
321 # For non-existing categories
322 $t = wfStrencode( $this->mTitle
->getPrefixedDBKey() );
323 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to='$t' AND bl_from=cur_id" ;
324 $res = wfQuery ( $sql, DB_READ
) ;
325 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
328 # For all pages that link to this category
329 foreach ( $data AS $x )
331 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
332 if ( $t != "" ) $t .= ":" ;
333 $t .= $x->cur_title
;
335 if ( $x->cur_namespace
== $cns ) {
336 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
338 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
341 wfFreeResult ( $res ) ;
343 # Showing subcategories
344 if ( count ( $children ) > 0 )
346 asort ( $children ) ;
347 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
348 $r .= implode ( ", " , $children ) ;
351 # Showing pages in this category
352 if ( count ( $articles ) > 0 )
354 $ti = $this->mTitle
->getText() ;
355 asort ( $articles ) ;
356 $h = wfMsg( "category_header", $ti );
357 $r .= "<h2>{$h}</h2>\n" ;
358 $r .= implode ( ", " , $articles ) ;
365 function getHTMLattrs ()
367 $htmlattrs = array( # Allowed attributes--no scripting, etc.
368 "title", "align", "lang", "dir", "width", "height",
369 "bgcolor", "clear", /* BR */ "noshade", /* HR */
370 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
371 /* FONT */ "type", "start", "value", "compact",
372 /* For various lists, mostly deprecated but safe */
373 "summary", "width", "border", "frame", "rules",
374 "cellspacing", "cellpadding", "valign", "char",
375 "charoff", "colgroup", "col", "span", "abbr", "axis",
376 "headers", "scope", "rowspan", "colspan", /* Tables */
377 "id", "class", "name", "style" /* For CSS */
382 function fixTagAttributes ( $t )
384 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
385 $htmlattrs = $this->getHTMLattrs() ;
387 # Strip non-approved attributes from the tag
389 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
390 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
392 # Strip javascript "expression" from stylesheets. Brute force approach:
393 # If anythin offensive is found, all attributes of the HTML tag are dropped
396 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
397 wfMungeToUtf8( $t ) ) )
405 /* interface with html tidy, used if $wgUseTidy = true */
406 function tidy ( $text ) {
407 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
409 $descriptorspec = array(
410 0 => array("pipe", "r"),
411 1 => array("pipe", "w"),
412 2 => array("file", "/dev/null", "a")
414 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
415 if (is_resource($process)) {
416 fwrite($pipes[0], $text);
418 while (!feof($pipes[1])) {
419 $cleansource .= fgets($pipes[1], 1024);
422 $return_value = proc_close($process);
424 if( $cleansource == '') {
427 return preg_replace("/(^.*<body[^>]*>|<\\/body[^>]*>.*$)/s", '', $cleansource);
432 function doTableStuff ( $t )
434 $t = explode ( "\n" , $t ) ;
435 $td = array () ; # Is currently a td tag open?
436 $ltd = array () ; # Was it TD or TH?
437 $tr = array () ; # Is currently a tr tag open?
438 $ltr = array () ; # tr attributes
439 foreach ( $t AS $k => $x )
442 $fc = substr ( $x , 0 , 1 ) ;
443 if ( "{|" == substr ( $x , 0 , 2 ) )
445 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
446 array_push ( $td , false ) ;
447 array_push ( $ltd , "" ) ;
448 array_push ( $tr , false ) ;
449 array_push ( $ltr , "" ) ;
451 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
452 else if ( "|}" == substr ( $x , 0 , 2 ) )
455 $l = array_pop ( $ltd ) ;
456 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
457 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
461 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
463 $z = trim ( substr ( $x , 2 ) ) ;
464 $t[$k] = "<caption>{$z}</caption>\n" ;
466 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
468 $x = substr ( $x , 1 ) ;
469 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
471 $l = array_pop ( $ltd ) ;
472 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
473 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
476 array_push ( $tr , false ) ;
477 array_push ( $td , false ) ;
478 array_push ( $ltd , "" ) ;
479 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
481 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
483 if ( "|+" == substr ( $x , 0 , 2 ) )
486 $x = substr ( $x , 1 ) ;
488 $after = substr ( $x , 1 ) ;
489 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
490 $after = explode ( "||" , $after ) ;
492 foreach ( $after AS $theline )
497 $tra = array_pop ( $ltr ) ;
498 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
499 array_push ( $tr , true ) ;
500 array_push ( $ltr , "" ) ;
503 $l = array_pop ( $ltd ) ;
504 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
505 if ( $fc == "|" ) $l = "td" ;
506 else if ( $fc == "!" ) $l = "th" ;
507 else if ( $fc == "+" ) $l = "caption" ;
509 array_push ( $ltd , $l ) ;
510 $y = explode ( "|" , $theline , 2 ) ;
511 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
512 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
514 array_push ( $td , true ) ;
519 # Closing open td, tr && table
520 while ( count ( $td ) > 0 )
522 if ( array_pop ( $td ) ) $t[] = "</td>" ;
523 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
527 $t = implode ( "\n" , $t ) ;
528 # $t = $this->removeHTMLtags( $t );
532 function internalParse( $text, $linestart, $args = array() )
534 $fname = "Parser::internalParse";
535 wfProfileIn( $fname );
537 $text = $this->removeHTMLtags( $text );
538 $text = $this->replaceVariables( $text, $args );
540 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
542 $text = $this->doHeadings( $text );
543 if($this->mOptions
->getUseDynamicDates()) {
544 global $wgDateFormatter;
545 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
547 $text = $this->replaceExternalLinks( $text );
548 $text = $this->doTokenizedParser ( $text );
549 $text = $this->doTableStuff ( $text ) ;
550 $text = $this->formatHeadings( $text );
551 $sk =& $this->mOptions
->getSkin();
552 $text = $sk->transformContent( $text );
554 if ( !isset ( $this->categoryMagicDone
) ) {
555 $text .= $this->categoryMagic () ;
556 $this->categoryMagicDone
= true ;
559 wfProfileOut( $fname );
564 /* private */ function doHeadings( $text )
566 for ( $i = 6; $i >= 1; --$i ) {
567 $h = substr( "======", 0, $i );
568 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
569 "<h{$i}>\\1</h{$i}>\\2", $text );
574 # Note: we have to do external links before the internal ones,
575 # and otherwise take great care in the order of things here, so
576 # that we don't end up interpreting some URLs twice.
578 /* private */ function replaceExternalLinks( $text )
580 $fname = "Parser::replaceExternalLinks";
581 wfProfileIn( $fname );
582 $text = $this->subReplaceExternalLinks( $text, "http", true );
583 $text = $this->subReplaceExternalLinks( $text, "https", true );
584 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
585 $text = $this->subReplaceExternalLinks( $text, "irc", false );
586 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
587 $text = $this->subReplaceExternalLinks( $text, "news", false );
588 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
589 wfProfileOut( $fname );
593 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
595 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
596 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
598 # this is the list of separators that should be ignored if they
599 # are the last character of an URL but that should be included
600 # if they occur within the URL, e.g. "go to www.foo.com, where .."
601 # in this case, the last comma should not become part of the URL,
602 # but in "www.foo.com/123,2342,32.htm" it should.
604 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
605 $images = "gif|png|jpg|jpeg";
607 # PLEASE NOTE: The curly braces { } are not part of the regex,
608 # they are interpreted as part of the string (used to tell PHP
609 # that the content of the string should be inserted there).
610 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
611 "((?i){$images})([^{$uc}]|$)/";
613 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
614 $sk =& $this->mOptions
->getSkin();
616 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
617 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
618 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
620 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
621 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
622 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
624 $s = str_replace( $unique, $protocol, $s );
626 $a = explode( "[{$protocol}:", " " . $s );
627 $s = array_shift( $a );
628 $s = substr( $s, 1 );
630 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
631 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
633 foreach ( $a as $line ) {
634 if ( preg_match( $e1, $line, $m ) ) {
635 $link = "{$protocol}:{$m[1]}";
637 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
638 else { $text = wfEscapeHTML( $link ); }
639 } else if ( preg_match( $e2, $line, $m ) ) {
640 $link = "{$protocol}:{$m[1]}";
644 $s .= "[{$protocol}:" . $line;
647 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
650 # Expand the URL for printable version
651 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
653 $la = $sk->getExternalLinkAttributes( $link, $text );
654 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
660 /* private */ function handle3Quotes( &$state, $token )
662 if ( $state["strong"] !== false ) {
663 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
665 # ''' lala ''lala '''
666 $s = "</em></strong><em>";
670 $state["strong"] = FALSE;
673 $state["strong"] = isset($token["pos"]) ?
$token["pos"] : true;
678 /* private */ function handle2Quotes( &$state, $token )
680 if ( $state["em"] !== false ) {
681 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
683 # ''lala'''lala'' ....'''
684 $s = "</strong></em><strong>";
688 $state["em"] = FALSE;
691 $state["em"] = isset($token["pos"]) ?
$token["pos"] : true;
697 /* private */ function handle5Quotes( &$state, $token )
700 if ( $state["em"] !== false && $state["strong"] !== false ) {
701 if ( $state["em"] < $state["strong"] ) {
702 $s .= "</strong></em>";
704 $s .= "</em></strong>";
706 $state["strong"] = $state["em"] = FALSE;
707 } elseif ( $state["em"] !== false ) {
708 $s .= "</em><strong>";
709 $state["em"] = FALSE;
710 $state["strong"] = $token["pos"];
711 } elseif ( $state["strong"] !== false ) {
712 $s .= "</strong><em>";
713 $state["strong"] = FALSE;
714 $state["em"] = $token["pos"];
715 } else { # not $em and not $strong
716 $s .= "<strong><em>";
717 $state["strong"] = $state["em"] = isset($token["pos"]) ?
$token["pos"] : true;
722 /* private */ function doTokenizedParser( $str )
724 global $wgLang; # for language specific parser hook
725 global $wgUploadDirectory, $wgUseTimeline;
727 $tokenizer=Tokenizer
::newFromString( $str );
728 $tokenStack = array();
731 $state["em"] = FALSE;
732 $state["strong"] = FALSE;
736 # The tokenizer splits the text into tokens and returns them one by one.
737 # Every call to the tokenizer returns a new token.
738 while ( $token = $tokenizer->nextToken() )
740 switch ( $token["type"] )
743 # simple text with no further markup
744 $txt = $token["text"];
747 # Text that contains blanks that have to be converted to
748 # non-breakable spaces for French.
749 # U+202F NARROW NO-BREAK SPACE might be a better choice, but
750 # browser support for Unicode spacing is poor.
751 $txt = str_replace( " ", " ", $token["text"] );
754 # remember the tag opened with 3 [
758 # FIXME : Treat orphaned open tags (stack not empty when text is over)
760 array_push( $tokenStack, $token );
767 # get text from stack, glue it together, and call the code to handle a
770 if ( count( $tokenStack ) == 0 )
772 # stack empty. Found a ]] without an opening [[
776 $lastToken = array_pop( $tokenStack );
777 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
779 if( !empty( $lastToken["text"] ) ) {
780 $linkText = $lastToken["text"] . $linkText;
782 $lastToken = array_pop( $tokenStack );
785 $txt = $linkText ."]]";
787 if( isset( $lastToken["text"] ) ) {
788 $prefix = $lastToken["text"];
792 $nextToken = $tokenizer->previewToken();
793 if ( $nextToken["type"] == "text" )
795 # Preview just looks at it. Now we have to fetch it.
796 $nextToken = $tokenizer->nextToken();
797 $txt .= $nextToken["text"];
799 $txt = $this->handleInternalLink( $this->unstrip($txt,$this->mStripState
), $prefix );
801 # did the tag start with 3 [ ?
803 # show the first as text
809 $tagIsOpen = (count( $tokenStack ) != 0);
815 # This and the three next ones handle quotes
816 $txt = $this->handle3Quotes( $state, $token );
819 $txt = $this->handle2Quotes( $state, $token );
822 $txt = $this->handle5Quotes( $state, $token );
832 $txt = $this->doMagicRFC( $tokenizer );
839 $txt = $this->doMagicISBN( $tokenizer );
843 if ( $wgUseTimeline &&
844 "" != ( $timelinesrc = $tokenizer->readAllUntil("</timeline>") ) )
846 $txt = renderTimeline( $timelinesrc );
852 # Call language specific Hook.
853 $txt = $wgLang->processToken( $token, $tokenStack );
854 if ( NULL == $txt ) {
855 # An unkown token. Highlight.
856 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
857 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
861 # If we're parsing the interior of a link, don't append the interior to $s,
862 # but push it to the stack so it can be processed when a ]] token is found.
863 if ( $tagIsOpen && $txt != "" ) {
864 $token["type"] = "text";
865 $token["text"] = $txt;
866 array_push( $tokenStack, $token );
871 if ( count( $tokenStack ) != 0 )
873 # still objects on stack. opened [[ tag without closing ]] tag.
875 while ( $lastToken = array_pop( $tokenStack ) )
877 if ( $lastToken["type"] == "text" )
879 $txt = $lastToken["text"] . $txt;
881 $txt = $lastToken["type"] . $txt;
889 /* private */ function handleInternalLink( $line, $prefix )
891 global $wgLang, $wgLinkCache;
892 global $wgNamespacesWithSubpages, $wgLanguageCode;
893 static $fname = "Parser::handleInternalLink" ;
894 wfProfileIn( $fname );
896 wfProfileIn( "$fname-setup" );
898 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
899 $sk =& $this->mOptions
->getSkin();
901 # Match a link having the form [[namespace:link|alternate]]trail
903 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
904 # Match the end of a line for a word that's not followed by whitespace,
905 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
906 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
907 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
908 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
911 # Special and Media are pseudo-namespaces; no pages actually exist in them
912 static $image = FALSE;
913 static $special = FALSE;
914 static $media = FALSE;
915 static $category = FALSE;
916 if ( !$image ) { $image = Namespace::getImage(); }
917 if ( !$special ) { $special = Namespace::getSpecial(); }
918 if ( !$media ) { $media = Namespace::getMedia(); }
919 if ( !$category ) { $category = Namespace::getCategory(); }
921 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
923 wfProfileOut( "$fname-setup" );
926 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
929 } else { # Invalid form; output directly
930 $s .= $prefix . "[[" . $line ;
936 :Foobar -- override special treatment of prefix (images, language links)
937 /Foobar -- convert to CurrentPage/Foobar
938 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
940 $c = substr($m[1],0,1);
941 $noforce = ($c != ":");
942 if( $c == "/" ) { # subpage
943 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
944 $m[1]=substr($m[1],1,strlen($m[1])-2);
947 $noslash=substr($m[1],1);
949 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
950 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
953 } # this might be changed for ugliness reasons
955 $link = $noslash; # no subpage allowed, use standard link
957 } elseif( $noforce ) { # no subpage
960 $link = substr( $m[1], 1 );
965 $nt = Title
::newFromText( $link );
967 $s .= $prefix . "[[" . $line;
970 $ns = $nt->getNamespace();
971 $iw = $nt->getInterWiki();
973 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
974 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
975 $s .= $prefix . $trail ;
976 return (trim($s) == '')?
'': $s;
978 if( $ns == $image ) {
979 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
980 $wgLinkCache->addImageLinkObj( $nt );
983 if ( $ns == $category ) {
984 $t = $nt->getText() ;
985 $nnt = Title
::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
986 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
987 $this->mOutput
->mCategoryLinks
[] = $t ;
988 $s .= $prefix . $trail ;
992 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
993 ( strpos( $link, "#" ) == FALSE ) ) {
994 # Self-links are handled specially; generally de-link and change to bold.
995 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, "", $trail );
999 if( $ns == $media ) {
1000 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1001 $wgLinkCache->addImageLinkObj( $nt );
1003 } elseif( $ns == $special ) {
1004 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
1007 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
1009 wfProfileOut( $fname );
1013 # Some functions here used by doBlockLevels()
1015 /* private */ function closeParagraph()
1018 if ( '' != $this->mLastSection
) {
1019 $result = "</" . $this->mLastSection
. ">\n";
1021 $this->mInPre
= false;
1022 $this->mLastSection
= "";
1025 # getCommon() returns the length of the longest common substring
1026 # of both arguments, starting at the beginning of both.
1028 /* private */ function getCommon( $st1, $st2 )
1030 $fl = strlen( $st1 );
1031 $shorter = strlen( $st2 );
1032 if ( $fl < $shorter ) { $shorter = $fl; }
1034 for ( $i = 0; $i < $shorter; ++
$i ) {
1035 if ( $st1{$i} != $st2{$i} ) { break; }
1039 # These next three functions open, continue, and close the list
1040 # element appropriate to the prefix character passed into them.
1042 /* private */ function openList( $char )
1044 $result = $this->closeParagraph();
1046 if ( "*" == $char ) { $result .= "<ul><li>"; }
1047 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1048 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1049 else if ( ";" == $char ) {
1050 $result .= "<dl><dt>";
1051 $this->mDTopen
= true;
1053 else { $result = "<!-- ERR 1 -->"; }
1058 /* private */ function nextItem( $char )
1060 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
1061 else if ( ":" == $char ||
";" == $char ) {
1063 if ( $this->mDTopen
) { $close = "</dt>"; }
1064 if ( ";" == $char ) {
1065 $this->mDTopen
= true;
1066 return $close . "<dt>";
1068 $this->mDTopen
= false;
1069 return $close . "<dd>";
1072 return "<!-- ERR 2 -->";
1075 /* private */function closeList( $char )
1077 if ( "*" == $char ) { $text = "</li></ul>"; }
1078 else if ( "#" == $char ) { $text = "</li></ol>"; }
1079 else if ( ":" == $char ) {
1080 if ( $this->mDTopen
) {
1081 $this->mDTopen
= false;
1082 $text = "</dt></dl>";
1084 $text = "</dd></dl>";
1087 else { return "<!-- ERR 3 -->"; }
1091 /* private */ function doBlockLevels( $text, $linestart ) {
1092 $fname = "Parser::doBlockLevels";
1093 wfProfileIn( $fname );
1095 # Parsing through the text line by line. The main thing
1096 # happening here is handling of block-level elements p, pre,
1097 # and making lists from lines starting with * # : etc.
1099 $textLines = explode( "\n", $text );
1101 $lastPrefix = $output = $lastLine = '';
1102 $this->mDTopen
= $inBlockElem = false;
1104 $paragraphStack = false;
1106 if ( !$linestart ) {
1107 $output .= array_shift( $textLines );
1109 foreach ( $textLines as $oLine ) {
1110 $lastPrefixLength = strlen( $lastPrefix );
1111 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1112 $preOpenMatch = preg_match("/<pre/i", $oLine );
1113 if (!$this->mInPre
) {
1114 $this->mInPre
= !empty($preOpenMatch);
1116 if ( !$this->mInPre
) {
1117 # Multiple prefixes may abut each other for nested lists.
1118 $prefixLength = strspn( $oLine, "*#:;" );
1119 $pref = substr( $oLine, 0, $prefixLength );
1122 $pref2 = str_replace( ";", ":", $pref );
1123 $t = substr( $oLine, $prefixLength );
1125 # Don't interpret any other prefixes in preformatted text
1127 $pref = $pref2 = '';
1132 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1133 # Same as the last item, so no need to deal with nesting or opening stuff
1134 $output .= $this->nextItem( substr( $pref, -1 ) );
1135 $paragraphStack = false;
1137 if ( ";" == substr( $pref, -1 ) ) {
1138 # The one nasty exception: definition lists work like this:
1139 # ; title : definition text
1140 # So we check for : in the remainder text to split up the
1141 # title and definition, without b0rking links.
1142 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1143 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1145 $output .= $term . $this->nextItem( ":" );
1149 } elseif( $prefixLength ||
$lastPrefixLength ) {
1150 # Either open or close a level...
1151 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1152 $paragraphStack = false;
1154 while( $commonPrefixLength < $lastPrefixLength ) {
1155 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1156 --$lastPrefixLength;
1158 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1159 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1161 while ( $prefixLength > $commonPrefixLength ) {
1162 $char = substr( $pref, $commonPrefixLength, 1 );
1163 $output .= $this->openList( $char );
1165 if ( ";" == $char ) {
1166 # FIXME: This is dupe of code above
1167 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1169 $output .= $term . $this->nextItem( ":" );
1173 ++
$commonPrefixLength;
1175 $lastPrefix = $pref2;
1177 if( 0 == $prefixLength ) {
1178 # No prefix (not in list)--go to paragraph mode
1179 $uniq_prefix = UNIQ_PREFIX
;
1180 // XXX: use a stack for nestable elements like span, table and div
1181 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1182 $closematch = preg_match(
1183 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1184 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1185 if ( $openmatch or $closematch ) {
1186 $paragraphStack = false;
1187 $output .= $this->closeParagraph();
1188 if($preOpenMatch and !$preCloseMatch) {
1189 $this->mInPre
= true;
1191 if ( $closematch ) {
1192 $inBlockElem = false;
1194 $inBlockElem = true;
1196 } else if ( !$inBlockElem ) {
1197 if ( " " == $t{0} ) {
1199 if ($this->mLastSection
!= 'pre') {
1200 $paragraphStack = false;
1201 $output .= $this->closeParagraph().'<pre>';
1202 $this->mLastSection
= 'pre';
1206 if ( '' == trim($t) ) {
1207 if ( $paragraphStack ) {
1208 $output .= $paragraphStack.'<br/>';
1209 $paragraphStack = false;
1210 $this->mLastSection
= 'p';
1212 if ($this->mLastSection
!= 'p' ) {
1213 $output .= $this->closeParagraph();
1214 $this->mLastSection
= '';
1215 $paragraphStack = "<p>";
1217 $paragraphStack = '</p><p>';
1221 if ( $paragraphStack ) {
1222 $output .= $paragraphStack;
1223 $paragraphStack = false;
1224 $this->mLastSection
= 'p';
1225 } else if ($this->mLastSection
!= 'p') {
1226 $output .= $this->closeParagraph().'<p>';
1227 $this->mLastSection
= 'p';
1233 if ($paragraphStack === false) {
1237 while ( $prefixLength ) {
1238 $output .= $this->closeList( $pref2{$prefixLength-1} );
1241 if ( "" != $this->mLastSection
) {
1242 $output .= "</" . $this->mLastSection
. ">";
1243 $this->mLastSection
= "";
1246 wfProfileOut( $fname );
1250 function getVariableValue( $index ) {
1251 global $wgLang, $wgSitename, $wgServer;
1254 case MAG_CURRENTMONTH
:
1256 case MAG_CURRENTMONTHNAME
:
1257 return $wgLang->getMonthName( date("n") );
1258 case MAG_CURRENTMONTHNAMEGEN
:
1259 return $wgLang->getMonthNameGen( date("n") );
1260 case MAG_CURRENTDAY
:
1263 return $this->mTitle
->getText();
1265 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1266 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1267 case MAG_CURRENTDAYNAME
:
1268 return $wgLang->getWeekdayName( date("w")+
1 );
1269 case MAG_CURRENTYEAR
:
1271 case MAG_CURRENTTIME
:
1272 return $wgLang->time( wfTimestampNow(), false );
1273 case MAG_NUMBEROFARTICLES
:
1274 return wfNumberOfArticles();
1284 function initialiseVariables()
1286 global $wgVariableIDs;
1287 $this->mVariables
= array();
1288 foreach ( $wgVariableIDs as $id ) {
1289 $mw =& MagicWord
::get( $id );
1290 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1294 /* private */ function replaceVariables( $text, $args = array() )
1296 global $wgLang, $wgScript, $wgArticlePath;
1298 $fname = "Parser::replaceVariables";
1299 wfProfileIn( $fname );
1302 if ( !$this->mVariables
) {
1303 $this->initialiseVariables();
1305 $titleChars = Title
::legalChars();
1306 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1308 # This function is called recursively. To keep track of arguments we need a stack:
1309 array_push( $this->mArgStack
, $args );
1311 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1312 $GLOBALS['wgCurParser'] =& $this;
1313 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1315 array_pop( $this->mArgStack
);
1320 function braceSubstitution( $matches )
1322 global $wgLinkCache, $wgLang;
1323 $fname = "Parser::braceSubstitution";
1328 # $newline is an optional newline character before the braces
1329 # $part1 is the bit before the first |, and must contain only title characters
1330 # $args is a list of arguments, starting from index 0, not including $part1
1332 $newline = $matches[1];
1333 $part1 = $matches[2];
1334 # If the third subpattern matched anything, it will start with |
1335 if ( $matches[3] !== "" ) {
1336 $args = explode( "|", substr( $matches[3], 1 ) );
1340 $argc = count( $args );
1343 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1344 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1345 if ( $this->mOutputType
!= OT_WIKI
) {
1346 # Invalid SUBST not replaced at PST time
1347 # Return without further processing
1348 $text = $matches[0];
1351 } elseif ( $this->mOutputType
== OT_WIKI
) {
1352 # SUBST not found in PST pass, do nothing
1353 $text = $matches[0];
1357 # MSG, MSGNW and INT
1360 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1361 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1364 # Remove obsolete MSG:
1365 $mwMsg =& MagicWord
::get( MAG_MSG
);
1366 $mwMsg->matchStartAndRemove( $part1 );
1369 # Check if it is an internal message
1370 $mwInt =& MagicWord
::get( MAG_INT
);
1371 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1372 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1373 $text = wfMsgReal( $part1, $args, true );
1381 # Check for NS: (namespace expansion)
1382 $mwNs = MagicWord
::get( MAG_NS
);
1383 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1384 if ( intval( $part1 ) ) {
1385 $text = $wgLang->getNsText( intval( $part1 ) );
1388 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1389 if ( !is_null( $index ) ) {
1390 $text = $wgLang->getNsText( $index );
1397 # LOCALURL and LOCALURLE
1399 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1400 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1402 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1403 $func = 'getLocalURL';
1404 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1405 $func = 'escapeLocalURL';
1410 if ( $func !== '' ) {
1411 $title = Title
::newFromText( $part1 );
1412 if ( !is_null( $title ) ) {
1414 $text = $title->$func( $args[0] );
1416 $text = $title->$func();
1423 # Internal variables
1424 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1425 $text = $this->mVariables
[$part1];
1427 $this->mOutput
->mContainsOldMagic
= true;
1430 # Arguments input from the caller
1431 $inputArgs = end( $this->mArgStack
);
1432 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1433 $text = $inputArgs[$part1];
1437 # Load from database
1439 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1440 if ( !is_null( $title ) && !$title->isExternal() ) {
1441 # Check for excessive inclusion
1442 $dbk = $title->getPrefixedDBkey();
1443 if ( $this->incrementIncludeCount( $dbk ) ) {
1444 $article = new Article( $title );
1445 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1446 if ( $articleContent !== false ) {
1448 $text = $articleContent;
1453 # If the title is valid but undisplayable, make a link to it
1454 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1455 $text = "[[" . $title->getPrefixedText() . "]]";
1461 # Recursive parsing, escaping and link table handling
1462 # Only for HTML output
1463 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1464 $text = wfEscapeWikiText( $text );
1465 } elseif ( $this->mOutputType
== OT_HTML
&& $found ) {
1466 # Clean up argument array
1467 $assocArgs = array();
1469 foreach( $args as $arg ) {
1470 $eqpos = strpos( $arg, "=" );
1471 if ( $eqpos === false ) {
1472 $assocArgs[$index++
] = $arg;
1474 $name = trim( substr( $arg, 0, $eqpos ) );
1475 $value = trim( substr( $arg, $eqpos+
1 ) );
1476 if ( $value === false ) {
1479 if ( $name !== false ) {
1480 $assocArgs[$name] = $value;
1485 # Do not enter included links in link table
1486 if ( !is_null( $title ) ) {
1487 $wgLinkCache->suspend();
1490 # Run full parser on the included text
1491 $text = $this->strip( $text, $this->mStripState
);
1492 $text = $this->internalParse( $text, (bool)$newline, $assocArgs );
1494 # Add the result to the strip state for re-inclusion after
1495 # the rest of the processing
1496 $text = $this->insertStripItem( $text, $this->mStripState
);
1498 # Resume the link cache and register the inclusion as a link
1499 if ( !is_null( $title ) ) {
1500 $wgLinkCache->resume();
1501 $wgLinkCache->addLinkObj( $title );
1512 # Returns true if the function is allowed to include this entity
1513 function incrementIncludeCount( $dbk )
1515 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1516 $this->mIncludeCount
[$dbk] = 0;
1518 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1526 # Cleans up HTML, removes dangerous tags and attributes
1527 /* private */ function removeHTMLtags( $text )
1529 $fname = "Parser::removeHTMLtags";
1530 wfProfileIn( $fname );
1531 $htmlpairs = array( # Tags that must be closed
1532 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1533 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1534 "strike", "strong", "tt", "var", "div", "center",
1535 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1536 "ruby", "rt" , "rb" , "rp", "p"
1538 $htmlsingle = array(
1539 "br", "hr", "li", "dt", "dd"
1541 $htmlnest = array( # Tags that can be nested--??
1542 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1543 "dl", "font", "big", "small", "sub", "sup"
1545 $tabletags = array( # Can only appear inside table
1549 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1550 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1552 $htmlattrs = $this->getHTMLattrs () ;
1554 # Remove HTML comments
1555 $text = preg_replace( "/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU", "$2", $text );
1557 $bits = explode( "<", $text );
1558 $text = array_shift( $bits );
1559 $tagstack = array(); $tablestack = array();
1561 foreach ( $bits as $x ) {
1562 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1563 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1565 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1566 error_reporting( $prev );
1569 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1573 if ( ! in_array( $t, $htmlsingle ) &&
1574 ( $ot = array_pop( $tagstack ) ) != $t ) {
1575 array_push( $tagstack, $ot );
1578 if ( $t == "table" ) {
1579 $tagstack = array_pop( $tablestack );
1584 # Keep track for later
1585 if ( in_array( $t, $tabletags ) &&
1586 ! in_array( "table", $tagstack ) ) {
1588 } else if ( in_array( $t, $tagstack ) &&
1589 ! in_array ( $t , $htmlnest ) ) {
1591 } else if ( ! in_array( $t, $htmlsingle ) ) {
1592 if ( $t == "table" ) {
1593 array_push( $tablestack, $tagstack );
1594 $tagstack = array();
1596 array_push( $tagstack, $t );
1598 # Strip non-approved attributes from the tag
1599 $newparams = $this->fixTagAttributes($params);
1603 $rest = str_replace( ">", ">", $rest );
1604 $text .= "<$slash$t $newparams$brace$rest";
1608 $text .= "<" . str_replace( ">", ">", $x);
1610 # Close off any remaining tags
1611 while ( $t = array_pop( $tagstack ) ) {
1613 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1615 wfProfileOut( $fname );
1621 * This function accomplishes several tasks:
1622 * 1) Auto-number headings if that option is enabled
1623 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1624 * 3) Add a Table of contents on the top for users who have enabled the option
1625 * 4) Auto-anchor headings
1627 * It loops through all headlines, collects the necessary data, then splits up the
1628 * string and re-inserts the newly formatted headlines.
1632 /* private */ function formatHeadings( $text )
1634 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1635 $doShowToc = $this->mOptions
->getShowToc();
1636 if( !$this->mTitle
->userCanEdit() ) {
1638 $rightClickHack = 0;
1640 $showEditLink = $this->mOptions
->getEditSection();
1641 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1644 # Inhibit editsection links if requested in the page
1645 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1646 if( $esw->matchAndRemove( $text ) ) {
1649 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1651 $mw =& MagicWord
::get( MAG_NOTOC
);
1652 if( $mw->matchAndRemove( $text ) ) {
1656 # never add the TOC to the Main Page. This is an entry page that should not
1657 # be more than 1-2 screens large anyway
1658 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1662 # Get all headlines for numbering them and adding funky stuff like [edit]
1663 # links - this is for later, but we need the number of headlines right now
1664 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1666 # if there are fewer than 4 headlines in the article, do not show TOC
1667 if( $numMatches < 4 ) {
1671 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1672 # override above conditions and always show TOC
1673 $mw =& MagicWord
::get( MAG_FORCETOC
);
1674 if ($mw->matchAndRemove( $text ) ) {
1679 # We need this to perform operations on the HTML
1680 $sk =& $this->mOptions
->getSkin();
1685 # Ugh .. the TOC should have neat indentation levels which can be
1686 # passed to the skin functions. These are determined here
1691 $sublevelCount = array();
1694 foreach( $matches[3] as $headline ) {
1697 $prevlevel = $level;
1699 $level = $matches[1][$headlineCount];
1700 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1701 # reset when we enter a new level
1702 $sublevelCount[$level] = 0;
1703 $toc .= $sk->tocIndent( $level - $prevlevel );
1704 $toclevel +
= $level - $prevlevel;
1706 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1707 # reset when we step back a level
1708 $sublevelCount[$level+
1]=0;
1709 $toc .= $sk->tocUnindent( $prevlevel - $level );
1710 $toclevel -= $prevlevel - $level;
1712 # count number of headlines for each level
1713 @$sublevelCount[$level]++
;
1714 if( $doNumberHeadings ||
$doShowToc ) {
1716 for( $i = 1; $i <= $level; $i++
) {
1717 if( !empty( $sublevelCount[$i] ) ) {
1721 $numbering .= $sublevelCount[$i];
1727 # The canonized header is a version of the header text safe to use for links
1728 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1729 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1732 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1733 $tocline = trim( $canonized_headline );
1734 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1735 $refer[$headlineCount] = $canonized_headline;
1737 # count how many in assoc. array so we can track dupes in anchors
1738 @$refers[$canonized_headline]++
;
1739 $refcount[$headlineCount]=$refers[$canonized_headline];
1741 # Prepend the number to the heading text
1743 if( $doNumberHeadings ||
$doShowToc ) {
1744 $tocline = $numbering . " " . $tocline;
1746 # Don't number the heading if it is the only one (looks silly)
1747 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1748 # the two are different if the line contains a link
1749 $headline=$numbering . " " . $headline;
1753 # Create the anchor for linking from the TOC to the section
1754 $anchor = $canonized_headline;
1755 if($refcount[$headlineCount] > 1 ) {
1756 $anchor .= "_" . $refcount[$headlineCount];
1759 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1761 if( $showEditLink ) {
1762 if ( empty( $head[$headlineCount] ) ) {
1763 $head[$headlineCount] = "";
1765 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1768 # Add the edit section span
1769 if( $rightClickHack ) {
1770 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1773 # give headline the correct <h#> tag
1774 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1780 $toclines = $headlineCount;
1781 $toc .= $sk->tocUnindent( $toclevel );
1782 $toc = $sk->tocTable( $toc );
1785 # split up and insert constructed headlines
1787 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1790 foreach( $blocks as $block ) {
1791 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1792 # This is the [edit] link that appears for the top block of text when
1793 # section editing is enabled
1795 # Disabled because it broke block formatting
1796 # For example, a bullet point in the top line
1797 # $full .= $sk->editSectionLink(0);
1800 if( $doShowToc && !$i) {
1801 # Top anchor now in skin
1805 if( !empty( $head[$i] ) ) {
1814 /* private */ function doMagicISBN( &$tokenizer )
1818 # Check whether next token is a text token
1819 # If yes, fetch it and convert the text into a
1820 # Special::BookSources link
1821 $token = $tokenizer->previewToken();
1822 while ( $token["type"] == "" )
1824 $tokenizer->nextToken();
1825 $token = $tokenizer->previewToken();
1827 if ( $token["type"] == "text" )
1829 $token = $tokenizer->nextToken();
1830 $x = $token["text"];
1831 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1833 $isbn = $blank = "" ;
1834 while ( " " == $x{0} ) {
1836 $x = substr( $x, 1 );
1838 while ( strstr( $valid, $x{0} ) != false ) {
1840 $x = substr( $x, 1 );
1842 $num = str_replace( "-", "", $isbn );
1843 $num = str_replace( " ", "", $num );
1846 $text = "ISBN $blank$x";
1848 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1849 $text = "<a href=\"" .
1850 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1851 "\" class=\"internal\">ISBN $isbn</a>";
1859 /* private */ function doMagicRFC( &$tokenizer )
1863 # Check whether next token is a text token
1864 # If yes, fetch it and convert the text into a
1865 # link to an RFC source
1866 $token = $tokenizer->previewToken();
1867 while ( $token["type"] == "" )
1869 $tokenizer->nextToken();
1870 $token = $tokenizer->previewToken();
1872 if ( $token["type"] == "text" )
1874 $token = $tokenizer->nextToken();
1875 $x = $token["text"];
1876 $valid = "0123456789";
1878 $rfc = $blank = "" ;
1879 while ( " " == $x{0} ) {
1881 $x = substr( $x, 1 );
1883 while ( strstr( $valid, $x{0} ) != false ) {
1885 $x = substr( $x, 1 );
1889 $text .= "RFC $blank$x";
1891 $url = wfmsg( "rfcurl" );
1892 $url = str_replace( "$1", $rfc, $url);
1893 $sk =& $this->mOptions
->getSkin();
1894 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1895 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1903 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1905 $this->mOptions
= $options;
1906 $this->mTitle
=& $title;
1907 $this->mOutputType
= OT_WIKI
;
1909 if ( $clearState ) {
1910 $this->clearState();
1913 $stripState = false;
1917 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
1920 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
1921 "/<br *?>/i" => "<br/>",
1923 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
1924 $text = $this->strip( $text, $stripState, false );
1925 $text = $this->pstPass2( $text, $user );
1926 $text = $this->unstrip( $text, $stripState );
1930 /* private */ function pstPass2( $text, &$user )
1932 global $wgLang, $wgLocaltimezone, $wgCurParser;
1934 # Variable replacement
1935 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1936 $text = $this->replaceVariables( $text );
1940 $n = $user->getName();
1941 $k = $user->getOption( "nickname" );
1942 if ( "" == $k ) { $k = $n; }
1943 if(isset($wgLocaltimezone)) {
1944 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1946 /* Note: this is an ugly timezone hack for the European wikis */
1947 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1948 " (" . date( "T" ) . ")";
1949 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1951 $text = preg_replace( "/~~~~~/", $d, $text );
1952 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1953 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1954 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1955 Namespace::getUser() ) . ":$n|$k]]", $text );
1957 # Context links: [[|name]] and [[name (context)|]]
1959 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1960 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1961 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1962 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1964 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1965 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1966 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1967 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1968 # [[ns:page (cont)|]]
1970 $t = $this->mTitle
->getText();
1971 if ( preg_match( $conpat, $t, $m ) ) {
1974 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1975 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1976 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1978 if ( "" == $context ) {
1979 $text = preg_replace( $p2, "[[\\1]]", $text );
1981 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1985 $mw =& MagicWord::get( MAG_SUBST );
1986 $wgCurParser = $this->fork();
1987 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1988 $this->merge( $wgCurParser );
1991 # Trim trailing whitespace
1992 # MAG_END (__END__) tag allows for trailing
1993 # whitespace to be deliberately included
1994 $text = rtrim( $text );
1995 $mw =& MagicWord
::get( MAG_END
);
1996 $mw->matchAndRemove( $text );
2001 # Set up some variables which are usually set up in parse()
2002 # so that an external function can call some class members with confidence
2003 function startExternalParse( &$title, $options, $outputType, $clearState = true )
2005 $this->mTitle
=& $title;
2006 $this->mOptions
= $options;
2007 $this->mOutputType
= $outputType;
2008 if ( $clearState ) {
2009 $this->clearState();
2013 function transformMsg( $text, $options ) {
2015 static $executing = false;
2017 # Guard against infinite recursion
2023 $this->mTitle
= $wgTitle;
2024 $this->mOptions
= $options;
2025 $this->mOutputType
= OT_MSG
;
2026 $this->clearState();
2027 $text = $this->replaceVariables( $text );
2036 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2038 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2039 $containsOldMagic = false )
2041 $this->mText
= $text;
2042 $this->mLanguageLinks
= $languageLinks;
2043 $this->mCategoryLinks
= $categoryLinks;
2044 $this->mContainsOldMagic
= $containsOldMagic;
2047 function getText() { return $this->mText
; }
2048 function getLanguageLinks() { return $this->mLanguageLinks
; }
2049 function getCategoryLinks() { return $this->mCategoryLinks
; }
2050 function containsOldMagic() { return $this->mContainsOldMagic
; }
2051 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2052 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2053 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2054 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2056 function merge( $other ) {
2057 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2058 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2059 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2066 # All variables are private
2067 var $mUseTeX; # Use texvc to expand <math> tags
2068 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2069 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2070 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2071 var $mAllowExternalImages; # Allow external images inline
2072 var $mSkin; # Reference to the preferred skin
2073 var $mDateFormat; # Date format index
2074 var $mEditSection; # Create "edit section" links
2075 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2076 var $mNumberHeadings; # Automatically number headings
2077 var $mShowToc; # Show table of contents
2079 function getUseTeX() { return $this->mUseTeX
; }
2080 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2081 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2082 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2083 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2084 function getSkin() { return $this->mSkin
; }
2085 function getDateFormat() { return $this->mDateFormat
; }
2086 function getEditSection() { return $this->mEditSection
; }
2087 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2088 function getNumberHeadings() { return $this->mNumberHeadings
; }
2089 function getShowToc() { return $this->mShowToc
; }
2091 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2092 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2093 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2094 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2095 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2096 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
2097 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2098 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2099 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2100 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2101 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2103 /* static */ function newFromUser( &$user )
2105 $popts = new ParserOptions
;
2106 $popts->initialiseFromUser( $user );
2110 function initialiseFromUser( &$userInput )
2112 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2114 if ( !$userInput ) {
2116 $user->setLoaded( true );
2118 $user =& $userInput;
2121 $this->mUseTeX
= $wgUseTeX;
2122 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2123 $this->mUseDynamicDates
= $wgUseDynamicDates;
2124 $this->mInterwikiMagic
= $wgInterwikiMagic;
2125 $this->mAllowExternalImages
= $wgAllowExternalImages;
2126 $this->mSkin
=& $user->getSkin();
2127 $this->mDateFormat
= $user->getOption( "date" );
2128 $this->mEditSection
= $user->getOption( "editsection" );
2129 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
2130 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
2131 $this->mShowToc
= $user->getOption( "showtoc" );
2137 # Regex callbacks, used in Parser::replaceVariables
2138 function wfBraceSubstitution( $matches )
2140 global $wgCurParser;
2141 return $wgCurParser->braceSubstitution( $matches );