Dori's patch: require confirmation for protection/unprotection like it's already...
[mediawiki.git] / includes / Parser.php
blob27bdaaf6cce73520cb9f9ba4e68bed3cdd65cf29
1 <?php
3 include_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 include_once('wikihiero.php');
9 # PHP Parser
11 # Processes wiki markup
13 # There are two main entry points into the Parser class: parse() and preSaveTransform().
14 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
16 # Globals used:
17 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
19 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
21 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
22 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
23 # $wgLocaltimezone
25 # * only within ParserOptions
28 #----------------------------------------
29 # Variable substitution O(N^2) attack
30 #-----------------------------------------
31 # Without countermeasures, it would be possible to attack the parser by saving a page
32 # filled with a large number of inclusions of large pages. The size of the generated
33 # page would be proportional to the square of the input size. Hence, we limit the number
34 # of inclusions of any given page, thus bringing any attack back to O(N).
37 define( "MAX_INCLUDE_REPEAT", 5 );
39 # Allowed values for $mOutputType
40 define( "OT_HTML", 1 );
41 define( "OT_WIKI", 2 );
42 define( "OT_MSG", 3 );
44 # prefix for escaping, used in two functions at least
45 define( "UNIQ_PREFIX", "NaodW29");
47 class Parser
49 # Cleared with clearState():
50 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
51 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
53 # Temporary:
54 var $mOptions, $mTitle, $mOutputType;
56 function Parser()
58 $this->clearState();
61 function clearState()
63 $this->mOutput = new ParserOutput;
64 $this->mAutonumber = 0;
65 $this->mLastSection = "";
66 $this->mDTopen = false;
67 $this->mVariables = false;
68 $this->mIncludeCount = array();
69 $this->mStripState = array();
70 $this->mArgStack = array();
73 # First pass--just handle <nowiki> sections, pass the rest off
74 # to internalParse() which does all the real work.
76 # Returns a ParserOutput
78 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
80 $fname = "Parser::parse";
81 wfProfileIn( $fname );
83 if ( $clearState ) {
84 $this->clearState();
87 $this->mOptions = $options;
88 $this->mTitle =& $title;
89 $this->mOutputType = OT_HTML;
91 $stripState = NULL;
92 $text = $this->strip( $text, $this->mStripState );
93 $text = $this->internalParse( $text, $linestart );
94 $text = $this->unstrip( $text, $this->mStripState );
95 # Clean up special characters, only run once, next-to-last before doBlockLevels
96 $fixtags = array(
97 "/<hr *>/i" => '<hr/>',
98 "/<br *>/i" => '<br/>',
99 "/<center *>/i"=>'<div class="center">',
100 "/<\\/center *>/i" => '</div>',
101 # Clean up spare ampersands; note that we probably ought to be
102 # more careful about named entities.
103 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
105 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
107 # only once and last
108 $text = $this->doBlockLevels( $text, $linestart );
110 $this->mOutput->setText( $text );
111 wfProfileOut( $fname );
112 return $this->mOutput;
115 /* static */ function getRandomString()
117 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
120 # Replaces all occurrences of <$tag>content</$tag> in the text
121 # with a random marker and returns the new text. the output parameter
122 # $content will be an associative array filled with data on the form
123 # $unique_marker => content.
125 # If $content is already set, the additional entries will be appended
127 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
128 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
129 if ( !$content ) {
130 $content = array( );
132 $n = 1;
133 $stripped = "";
135 while ( "" != $text ) {
136 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
137 $stripped .= $p[0];
138 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) {
139 $text = "";
140 } else {
141 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
142 $marker = $rnd . sprintf("%08X", $n++);
143 $content[$marker] = $q[0];
144 $stripped .= $marker;
145 $text = $q[1];
148 return $stripped;
151 # Strips <nowiki>, <pre> and <math>
152 # Returns the text, and fills an array with data needed in unstrip()
153 # If the $state is already a valid strip state, it adds to the state
155 function strip( $text, &$state )
157 $render = ($this->mOutputType == OT_HTML);
158 $nowiki_content = array();
159 $hiero_content = array();
160 $math_content = array();
161 $pre_content = array();
162 $item_content = array();
164 # Replace any instances of the placeholders
165 $uniq_prefix = UNIQ_PREFIX;
166 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
168 $text = Parser::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
169 foreach( $nowiki_content as $marker => $content ){
170 if( $render ){
171 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
172 } else {
173 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
177 if( $GLOBALS['wgUseWikiHiero'] ){
178 $text = Parser::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
179 foreach( $hiero_content as $marker => $content ){
180 if( $render ){
181 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML);
182 } else {
183 $hiero_content[$marker] = "<hiero>$content</hiero>";
188 if( $this->mOptions->getUseTeX() ){
189 $text = Parser::extractTags("math", $text, $math_content, $uniq_prefix);
190 foreach( $math_content as $marker => $content ){
191 if( $render ){
192 $math_content[$marker] = renderMath( $content );
193 } else {
194 $math_content[$marker] = "<math>$content</math>";
199 $text = Parser::extractTags("pre", $text, $pre_content, $uniq_prefix);
200 foreach( $pre_content as $marker => $content ){
201 if( $render ){
202 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
203 } else {
204 $pre_content[$marker] = "<pre>$content</pre>";
208 # Merge state with the pre-existing state, if there is one
209 if ( $state ) {
210 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
211 $state['hiero'] = $state['hiero'] + $hiero_content;
212 $state['math'] = $state['math'] + $math_content;
213 $state['pre'] = $state['pre'] + $pre_content;
214 } else {
215 $state = array(
216 'nowiki' => $nowiki_content,
217 'hiero' => $hiero_content,
218 'math' => $math_content,
219 'pre' => $pre_content,
220 'item' => $item_content
223 return $text;
226 function unstrip( $text, &$state )
228 # Must expand in reverse order, otherwise nested tags will be corrupted
229 $contentDict = end( $state );
230 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
231 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
232 $text = str_replace( key( $contentDict ), $content, $text );
236 return $text;
239 # Add an item to the strip state
240 # Returns the unique tag which must be inserted into the stripped text
241 # The tag will be replaced with the original text in unstrip()
243 function insertStripItem( $text, &$state )
245 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
246 if ( !$state ) {
247 $state = array(
248 'nowiki' => array(),
249 'hiero' => array(),
250 'math' => array(),
251 'pre' => array(),
252 'item' => array()
255 $state['item'][$rnd] = $text;
256 return $rnd;
259 function categoryMagic ()
261 global $wgLang , $wgUser ;
262 if ( !$this->mOptions->getUseCategoryMagic() ) return ;
263 $id = $this->mTitle->getArticleID() ;
264 if ( $this->mTitle->getNamespace() != Namespace::getCategory() ) return "" ;
265 $ti = $this->mTitle->getText() ;
266 $r = "<br style=\"clear:both;\"/>\n";
268 $articles = array() ;
269 $parents = array () ;
270 $children = array() ;
272 $sk =& $wgUser->getSkin() ;
274 $data = array () ;
275 $sql1 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
276 $sql2 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
278 $res = wfQuery ( $sql1, DB_READ ) ;
279 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
281 $res = wfQuery ( $sql2, DB_READ ) ;
282 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
284 foreach ( $data AS $x )
286 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
287 if ( $t != "" ) $t .= ":" ;
288 $t .= $x->cur_title ;
290 $y = explode ( ":" , $t , 2 ) ;
291 if ( count ( $y ) == 2 && $y[0] == $cat ) {
292 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
293 } else {
294 array_push ( $articles , $sk->makeLink ( $t ) ) ;
297 wfFreeResult ( $res ) ;
299 # Children
300 if ( count ( $children ) > 0 )
302 asort ( $children ) ;
303 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
304 $r .= implode ( ", " , $children ) ;
307 # Articles
308 if ( count ( $articles ) > 0 )
310 asort ( $articles ) ;
311 $h = wfMsg( "category_header", $ti );
312 $r .= "<h2>{$h}</h2>\n" ;
313 $r .= implode ( ", " , $articles ) ;
317 return $r ;
320 function getHTMLattrs ()
322 $htmlattrs = array( # Allowed attributes--no scripting, etc.
323 "title", "align", "lang", "dir", "width", "height",
324 "bgcolor", "clear", /* BR */ "noshade", /* HR */
325 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
326 /* FONT */ "type", "start", "value", "compact",
327 /* For various lists, mostly deprecated but safe */
328 "summary", "width", "border", "frame", "rules",
329 "cellspacing", "cellpadding", "valign", "char",
330 "charoff", "colgroup", "col", "span", "abbr", "axis",
331 "headers", "scope", "rowspan", "colspan", /* Tables */
332 "id", "class", "name", "style" /* For CSS */
334 return $htmlattrs ;
337 function fixTagAttributes ( $t )
339 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
340 $htmlattrs = $this->getHTMLattrs() ;
342 # Strip non-approved attributes from the tag
343 $t = preg_replace(
344 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
345 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
346 $t);
347 # Strip javascript "expression" from stylesheets. Brute force approach:
348 # If anythin offensive is found, all attributes of the HTML tag are dropped
350 if( preg_match(
351 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
352 wfMungeToUtf8( $t ) ) )
354 $t="";
357 return trim ( $t ) ;
360 function doTableStuff ( $t )
362 $t = explode ( "\n" , $t ) ;
363 $td = array () ; # Is currently a td tag open?
364 $ltd = array () ; # Was it TD or TH?
365 $tr = array () ; # Is currently a tr tag open?
366 $ltr = array () ; # tr attributes
367 foreach ( $t AS $k => $x )
369 $x = trim ( $x ) ;
370 $fc = substr ( $x , 0 , 1 ) ;
371 if ( "{|" == substr ( $x , 0 , 2 ) )
373 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
374 array_push ( $td , false ) ;
375 array_push ( $ltd , "" ) ;
376 array_push ( $tr , false ) ;
377 array_push ( $ltr , "" ) ;
379 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
380 else if ( "|}" == substr ( $x , 0 , 2 ) )
382 $z = "</table>\n" ;
383 $l = array_pop ( $ltd ) ;
384 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
385 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
386 array_pop ( $ltr ) ;
387 $t[$k] = $z ;
389 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
391 $z = trim ( substr ( $x , 2 ) ) ;
392 $t[$k] = "<caption>{$z}</caption>\n" ;
394 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
396 $x = substr ( $x , 1 ) ;
397 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
398 $z = "" ;
399 $l = array_pop ( $ltd ) ;
400 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
401 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
402 array_pop ( $ltr ) ;
403 $t[$k] = $z ;
404 array_push ( $tr , false ) ;
405 array_push ( $td , false ) ;
406 array_push ( $ltd , "" ) ;
407 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
409 else if ( "|" == $fc || "!" == $fc || "|+" == substr ( $x , 0 , 2 ) ) # Caption
411 if ( "|+" == substr ( $x , 0 , 2 ) )
413 $fc = "+" ;
414 $x = substr ( $x , 1 ) ;
416 $after = substr ( $x , 1 ) ;
417 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
418 $after = explode ( "||" , $after ) ;
419 $t[$k] = "" ;
420 foreach ( $after AS $theline )
422 $z = "" ;
423 if ( $fc != "+" )
425 $tra = array_pop ( $ltr ) ;
426 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
427 array_push ( $tr , true ) ;
428 array_push ( $ltr , "" ) ;
431 $l = array_pop ( $ltd ) ;
432 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
433 if ( $fc == "|" ) $l = "td" ;
434 else if ( $fc == "!" ) $l = "th" ;
435 else if ( $fc == "+" ) $l = "caption" ;
436 else $l = "" ;
437 array_push ( $ltd , $l ) ;
438 $y = explode ( "|" , $theline , 2 ) ;
439 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
440 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
441 $t[$k] .= $y ;
442 array_push ( $td , true ) ;
447 # Closing open td, tr && table
448 while ( count ( $td ) > 0 )
450 if ( array_pop ( $td ) ) $t[] = "</td>" ;
451 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
452 $t[] = "</table>" ;
455 $t = implode ( "\n" , $t ) ;
456 # $t = $this->removeHTMLtags( $t );
457 return $t ;
460 function internalParse( $text, $linestart, $args = array() )
462 $fname = "Parser::internalParse";
463 wfProfileIn( $fname );
465 $text = $this->removeHTMLtags( $text );
466 $text = $this->replaceVariables( $text, $args );
468 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
470 $text = $this->doHeadings( $text );
471 if($this->mOptions->getUseDynamicDates()) {
472 global $wgDateFormatter;
473 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
475 $text = $this->replaceExternalLinks( $text );
476 $text = $this->doTokenizedParser ( $text );
477 $text = $this->doTableStuff ( $text ) ;
478 $text = $this->formatHeadings( $text );
479 $sk =& $this->mOptions->getSkin();
480 $text = $sk->transformContent( $text );
482 if ( !isset ( $this->categoryMagicDone ) ) {
483 $text .= $this->categoryMagic () ;
484 $this->categoryMagicDone = true ;
487 wfProfileOut( $fname );
488 return $text;
492 /* private */ function doHeadings( $text )
494 for ( $i = 6; $i >= 1; --$i ) {
495 $h = substr( "======", 0, $i );
496 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
497 "<h{$i}>\\1</h{$i}>\\2", $text );
499 return $text;
502 # Note: we have to do external links before the internal ones,
503 # and otherwise take great care in the order of things here, so
504 # that we don't end up interpreting some URLs twice.
506 /* private */ function replaceExternalLinks( $text )
508 $fname = "Parser::replaceExternalLinks";
509 wfProfileIn( $fname );
510 $text = $this->subReplaceExternalLinks( $text, "http", true );
511 $text = $this->subReplaceExternalLinks( $text, "https", true );
512 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
513 $text = $this->subReplaceExternalLinks( $text, "irc", false );
514 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
515 $text = $this->subReplaceExternalLinks( $text, "news", false );
516 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
517 wfProfileOut( $fname );
518 return $text;
521 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
523 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
524 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
526 # this is the list of separators that should be ignored if they
527 # are the last character of an URL but that should be included
528 # if they occur within the URL, e.g. "go to www.foo.com, where .."
529 # in this case, the last comma should not become part of the URL,
530 # but in "www.foo.com/123,2342,32.htm" it should.
531 $sep = ",;\.:";
532 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
533 $images = "gif|png|jpg|jpeg";
535 # PLEASE NOTE: The curly braces { } are not part of the regex,
536 # they are interpreted as part of the string (used to tell PHP
537 # that the content of the string should be inserted there).
538 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
539 "((?i){$images})([^{$uc}]|$)/";
541 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
542 $sk =& $this->mOptions->getSkin();
544 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
545 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
546 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
548 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
549 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
550 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
551 "</a>\\5", $s );
552 $s = str_replace( $unique, $protocol, $s );
554 $a = explode( "[{$protocol}:", " " . $s );
555 $s = array_shift( $a );
556 $s = substr( $s, 1 );
558 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
559 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
561 foreach ( $a as $line ) {
562 if ( preg_match( $e1, $line, $m ) ) {
563 $link = "{$protocol}:{$m[1]}";
564 $trail = $m[2];
565 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
566 else { $text = wfEscapeHTML( $link ); }
567 } else if ( preg_match( $e2, $line, $m ) ) {
568 $link = "{$protocol}:{$m[1]}";
569 $text = $m[2];
570 $trail = $m[3];
571 } else {
572 $s .= "[{$protocol}:" . $line;
573 continue;
575 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
576 $paren = "";
577 } else {
578 # Expand the URL for printable version
579 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
581 $la = $sk->getExternalLinkAttributes( $link, $text );
582 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
585 return $s;
588 /* private */ function handle3Quotes( &$state, $token )
590 if ( $state["strong"] !== false ) {
591 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
593 # ''' lala ''lala '''
594 $s = "</em></strong><em>";
595 } else {
596 $s = "</strong>";
598 $state["strong"] = FALSE;
599 } else {
600 $s = "<strong>";
601 $state["strong"] = isset($token["pos"]) ? $token["pos"] : true;
603 return $s;
606 /* private */ function handle2Quotes( &$state, $token )
608 if ( $state["em"] !== false ) {
609 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
611 # ''lala'''lala'' ....'''
612 $s = "</strong></em><strong>";
613 } else {
614 $s = "</em>";
616 $state["em"] = FALSE;
617 } else {
618 $s = "<em>";
619 $state["em"] = isset($token["pos"]) ? $token["pos"] : true;
622 return $s;
625 /* private */ function handle5Quotes( &$state, $token )
627 $s = "";
628 if ( $state["em"] !== false && $state["strong"] !== false ) {
629 if ( $state["em"] < $state["strong"] ) {
630 $s .= "</strong></em>";
631 } else {
632 $s .= "</em></strong>";
634 $state["strong"] = $state["em"] = FALSE;
635 } elseif ( $state["em"] !== false ) {
636 $s .= "</em><strong>";
637 $state["em"] = FALSE;
638 $state["strong"] = $token["pos"];
639 } elseif ( $state["strong"] !== false ) {
640 $s .= "</strong><em>";
641 $state["strong"] = FALSE;
642 $state["em"] = $token["pos"];
643 } else { # not $em and not $strong
644 $s .= "<strong><em>";
645 $state["strong"] = $state["em"] = isset($token["pos"]) ? $token["pos"] : true;
647 return $s;
650 /* private */ function doTokenizedParser( $str )
652 global $wgLang; # for language specific parser hook
654 $tokenizer=Tokenizer::newFromString( $str );
655 $tokenStack = array();
657 $s="";
658 $state["em"] = FALSE;
659 $state["strong"] = FALSE;
660 $tagIsOpen = FALSE;
661 $threeopen = false;
663 # The tokenizer splits the text into tokens and returns them one by one.
664 # Every call to the tokenizer returns a new token.
665 while ( $token = $tokenizer->nextToken() )
667 switch ( $token["type"] )
669 case "text":
670 # simple text with no further markup
671 $txt = $token["text"];
672 break;
673 case "[[[":
674 # remember the tag opened with 3 [
675 $threeopen = true;
676 case "[[":
677 # link opening tag.
678 # FIXME : Treat orphaned open tags (stack not empty when text is over)
679 $tagIsOpen = TRUE;
680 array_push( $tokenStack, $token );
681 $txt="";
682 break;
684 case "]]]":
685 case "]]":
686 # link close tag.
687 # get text from stack, glue it together, and call the code to handle a
688 # link
690 if ( count( $tokenStack ) == 0 )
692 # stack empty. Found a ]] without an opening [[
693 $txt = "]]";
694 } else {
695 $linkText = "";
696 $lastToken = array_pop( $tokenStack );
697 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
699 if( !empty( $lastToken["text"] ) ) {
700 $linkText = $lastToken["text"] . $linkText;
702 $lastToken = array_pop( $tokenStack );
705 $txt = $linkText ."]]";
707 if( isset( $lastToken["text"] ) ) {
708 $prefix = $lastToken["text"];
709 } else {
710 $prefix = "";
712 $nextToken = $tokenizer->previewToken();
713 if ( $nextToken["type"] == "text" )
715 # Preview just looks at it. Now we have to fetch it.
716 $nextToken = $tokenizer->nextToken();
717 $txt .= $nextToken["text"];
719 $txt = $this->handleInternalLink( $this->unstrip($txt,$this->mStripState), $prefix );
721 # did the tag start with 3 [ ?
722 if($threeopen) {
723 # show the first as text
724 $txt = "[".$txt;
725 $threeopen=false;
729 $tagIsOpen = (count( $tokenStack ) != 0);
730 break;
731 case "----":
732 $txt = "\n<hr />\n";
733 break;
734 case "'''":
735 # This and the three next ones handle quotes
736 $txt = $this->handle3Quotes( $state, $token );
737 break;
738 case "''":
739 $txt = $this->handle2Quotes( $state, $token );
740 break;
741 case "'''''":
742 $txt = $this->handle5Quotes( $state, $token );
743 break;
744 case "":
745 # empty token
746 $txt="";
747 break;
748 case "RFC ":
749 if ( $tagIsOpen ) {
750 $txt = "RFC ";
751 } else {
752 $txt = $this->doMagicRFC( $tokenizer );
754 break;
755 case "ISBN ":
756 if ( $tagIsOpen ) {
757 $txt = "ISBN ";
758 } else {
759 $txt = $this->doMagicISBN( $tokenizer );
761 break;
762 default:
763 # Call language specific Hook.
764 $txt = $wgLang->processToken( $token, $tokenStack );
765 if ( NULL == $txt ) {
766 # An unkown token. Highlight.
767 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
768 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
770 break;
772 # If we're parsing the interior of a link, don't append the interior to $s,
773 # but push it to the stack so it can be processed when a ]] token is found.
774 if ( $tagIsOpen && $txt != "" ) {
775 $token["type"] = "text";
776 $token["text"] = $txt;
777 array_push( $tokenStack, $token );
778 } else {
779 $s .= $txt;
781 } #end while
782 if ( count( $tokenStack ) != 0 )
784 # still objects on stack. opened [[ tag without closing ]] tag.
785 $txt = "";
786 while ( $lastToken = array_pop( $tokenStack ) )
788 if ( $lastToken["type"] == "text" )
790 $txt = $lastToken["text"] . $txt;
791 } else {
792 $txt = $lastToken["type"] . $txt;
795 $s .= $txt;
797 return $s;
800 /* private */ function handleInternalLink( $line, $prefix )
802 global $wgLang, $wgLinkCache;
803 global $wgNamespacesWithSubpages, $wgLanguageCode;
804 static $fname = "Parser::handleInternalLink" ;
805 wfProfileIn( $fname );
807 wfProfileIn( "$fname-setup" );
808 static $tc = FALSE;
809 if ( !$tc ) { $tc = Title::legalChars() . "#"; }
810 $sk =& $this->mOptions->getSkin();
812 # Match a link having the form [[namespace:link|alternate]]trail
813 static $e1 = FALSE;
814 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
815 # Match the end of a line for a word that's not followed by whitespace,
816 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
817 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
818 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
819 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
822 # Special and Media are pseudo-namespaces; no pages actually exist in them
823 static $image = FALSE;
824 static $special = FALSE;
825 static $media = FALSE;
826 static $category = FALSE;
827 if ( !$image ) { $image = Namespace::getImage(); }
828 if ( !$special ) { $special = Namespace::getSpecial(); }
829 if ( !$media ) { $media = Namespace::getMedia(); }
830 if ( !$category ) { $category = Namespace::getCategory(); ; }
832 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
834 wfProfileOut( "$fname-setup" );
835 $s = "";
837 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
838 $text = $m[2];
839 $trail = $m[3];
840 } else { # Invalid form; output directly
841 $s .= $prefix . "[[" . $line ;
842 return $s;
845 /* Valid link forms:
846 Foobar -- normal
847 :Foobar -- override special treatment of prefix (images, language links)
848 /Foobar -- convert to CurrentPage/Foobar
849 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
851 $c = substr($m[1],0,1);
852 $noforce = ($c != ":");
853 if( $c == "/" ) { # subpage
854 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
855 $m[1]=substr($m[1],1,strlen($m[1])-2);
856 $noslash=$m[1];
857 } else {
858 $noslash=substr($m[1],1);
860 if($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]) { # subpages allowed here
861 $link = $this->mTitle->getPrefixedText(). "/" . trim($noslash);
862 if( "" == $text ) {
863 $text= $m[1];
864 } # this might be changed for ugliness reasons
865 } else {
866 $link = $noslash; # no subpage allowed, use standard link
868 } elseif( $noforce ) { # no subpage
869 $link = $m[1];
870 } else {
871 $link = substr( $m[1], 1 );
873 if( "" == $text )
874 $text = $link;
876 $nt = Title::newFromText( $link );
877 if( !$nt ) {
878 $s .= $prefix . "[[" . $line;
879 return $s;
881 $ns = $nt->getNamespace();
882 $iw = $nt->getInterWiki();
883 if( $noforce ) {
884 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
885 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
886 return (trim($s) == '')? '': $s;
888 if( $ns == $image ) {
889 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
890 $wgLinkCache->addImageLinkObj( $nt );
891 return $s;
893 if ( $ns == $category ) {
894 $t = $nt->getText() ;
895 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
896 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
897 $this->mOutput->mCategoryLinks[] = $t ;
898 $s .= $prefix . $trail ;
899 return $s ;
902 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
903 ( strpos( $link, "#" ) == FALSE ) ) {
904 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
905 return $s;
908 if( $ns == $media ) {
909 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
910 $wgLinkCache->addImageLinkObj( $nt );
911 return $s;
912 } elseif( $ns == $special ) {
913 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
914 return $s;
916 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
918 wfProfileOut( $fname );
919 return $s;
922 # Some functions here used by doBlockLevels()
924 /* private */ function closeParagraph()
926 $result = "";
927 if ( '' != $this->mLastSection ) {
928 $result = "</" . $this->mLastSection . ">\n";
930 $this->mInPre = false;
931 $this->mLastSection = "";
932 return $result;
934 # getCommon() returns the length of the longest common substring
935 # of both arguments, starting at the beginning of both.
937 /* private */ function getCommon( $st1, $st2 )
939 $fl = strlen( $st1 );
940 $shorter = strlen( $st2 );
941 if ( $fl < $shorter ) { $shorter = $fl; }
943 for ( $i = 0; $i < $shorter; ++$i ) {
944 if ( $st1{$i} != $st2{$i} ) { break; }
946 return $i;
948 # These next three functions open, continue, and close the list
949 # element appropriate to the prefix character passed into them.
951 /* private */ function openList( $char )
953 $result = $this->closeParagraph();
955 if ( "*" == $char ) { $result .= "<ul><li>"; }
956 else if ( "#" == $char ) { $result .= "<ol><li>"; }
957 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
958 else if ( ";" == $char ) {
959 $result .= "<dl><dt>";
960 $this->mDTopen = true;
962 else { $result = "<!-- ERR 1 -->"; }
964 return $result;
967 /* private */ function nextItem( $char )
969 if ( "*" == $char || "#" == $char ) { return "</li><li>"; }
970 else if ( ":" == $char || ";" == $char ) {
971 $close = "</dd>";
972 if ( $this->mDTopen ) { $close = "</dt>"; }
973 if ( ";" == $char ) {
974 $this->mDTopen = true;
975 return $close . "<dt>";
976 } else {
977 $this->mDTopen = false;
978 return $close . "<dd>";
981 return "<!-- ERR 2 -->";
984 /* private */function closeList( $char )
986 if ( "*" == $char ) { $text = "</li></ul>"; }
987 else if ( "#" == $char ) { $text = "</li></ol>"; }
988 else if ( ":" == $char ) {
989 if ( $this->mDTopen ) {
990 $this->mDTopen = false;
991 $text = "</dt></dl>";
992 } else {
993 $text = "</dd></dl>";
996 else { return "<!-- ERR 3 -->"; }
997 return $text."\n";
1000 /* private */ function doBlockLevels( $text, $linestart )
1002 $fname = "Parser::doBlockLevels";
1003 wfProfileIn( $fname );
1004 # Parsing through the text line by line. The main thing
1005 # happening here is handling of block-level elements p, pre,
1006 # and making lists from lines starting with * # : etc.
1008 $a = explode( "\n", $text );
1010 $lastPref = $text = $lastLine = '';
1011 $this->mDTopen = $inBlockElem = false;
1012 $npl = 0;
1013 $pstack = false;
1015 if ( ! $linestart ) { $text .= array_shift( $a ); }
1016 foreach ( $a as $t ) {
1017 $oLine = $t;
1018 $opl = strlen( $lastPref );
1019 $preCloseMatch = preg_match("/<\\/pre/i", $t );
1020 $preOpenMatch = preg_match("/<pre/i", $t );
1021 if (!$this->mInPre) {
1022 $this->mInPre = !empty($preOpenMatch);
1024 if ( !$this->mInPre ) {
1025 $npl = strspn( $t, "*#:;" );
1026 $pref = substr( $t, 0, $npl );
1027 $pref2 = str_replace( ";", ":", $pref );
1028 $t = substr( $t, $npl );
1029 } else {
1030 $npl = 0;
1031 $pref = $pref2 = '';
1034 // list generation
1035 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
1036 $text .= $this->nextItem( substr( $pref, -1 ) );
1037 if ( $pstack ) { $pstack = false; }
1039 if ( ";" == substr( $pref, -1 ) ) {
1040 $cpos = strpos( $t, ":" );
1041 if ( false !== $cpos ) {
1042 $term = substr( $t, 0, $cpos );
1043 $text .= $term . $this->nextItem( ":" );
1044 $t = substr( $t, $cpos + 1 );
1047 } else if (0 != $npl || 0 != $opl) {
1048 $cpl = $this->getCommon( $pref, $lastPref );
1049 if ( $pstack ) { $pstack = false; }
1051 while ( $cpl < $opl ) {
1052 $text .= $this->closeList( $lastPref{$opl-1} );
1053 --$opl;
1055 if ( $npl <= $cpl && $cpl > 0 ) {
1056 $text .= $this->nextItem( $pref{$cpl-1} );
1058 while ( $npl > $cpl ) {
1059 $char = substr( $pref, $cpl, 1 );
1060 $text .= $this->openList( $char );
1062 if ( ";" == $char ) {
1063 $cpos = strpos( $t, ":" );
1064 if ( ! ( false === $cpos ) ) {
1065 $term = substr( $t, 0, $cpos );
1066 $text .= $term . $this->nextItem( ":" );
1067 $t = substr( $t, $cpos + 1 );
1070 ++$cpl;
1072 $lastPref = $pref2;
1074 if ( 0 == $npl ) { # No prefix (not in list)--go to paragraph mode
1075 $uniq_prefix = UNIQ_PREFIX;
1076 // XXX: use a stack for nestable elements like span, table and div
1077 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1078 $closematch = preg_match(
1079 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1080 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1081 if ( $openmatch or $closematch ) {
1082 if ( $pstack ) { $pstack = false; }
1083 $text .= $this->closeParagraph();
1084 if($preOpenMatch and !$preCloseMatch) {
1085 $this->mInPre = true;
1087 if ( $closematch ) {
1088 $inBlockElem = false;
1089 } else {
1090 $inBlockElem = true;
1092 } else if ( !$inBlockElem ) {
1093 if ( " " == $t{0} ) {
1094 // pre
1095 if ($this->mLastSection != 'pre') {
1096 $pstack = false;
1097 $text .= $this->closeParagraph().'<pre>';
1098 $this->mLastSection = 'pre';
1100 } else {
1101 // paragraph
1102 if ( '' == trim($t) ) {
1103 if ( $pstack ) {
1104 $text .= $pstack.'<br/>';
1105 $pstack = false;
1106 $this->mLastSection = 'p';
1107 } else {
1108 if ($this->mLastSection != 'p' ) {
1109 $text .= $this->closeParagraph();
1110 $this->mLastSection = '';
1111 $pstack = "<p>";
1112 } else {
1113 $pstack = '</p><p>';
1116 } else {
1117 if ( $pstack ) {
1118 $text .= $pstack;
1119 $pstack = false;
1120 $this->mLastSection = 'p';
1121 } else if ($this->mLastSection != 'p') {
1122 $text .= $this->closeParagraph().'<p>';
1123 $this->mLastSection = 'p';
1129 if ($pstack === false) {
1130 $text .= $t."\n";
1133 while ( $npl ) {
1134 $text .= $this->closeList( $pref2{$npl-1} );
1135 --$npl;
1137 if ( "" != $this->mLastSection ) {
1138 $text .= "</" . $this->mLastSection . ">";
1139 $this->mLastSection = "";
1142 wfProfileOut( $fname );
1143 return $text;
1146 function getVariableValue( $index ) {
1147 global $wgLang, $wgSitename, $wgServer;
1149 switch ( $index ) {
1150 case MAG_CURRENTMONTH:
1151 return date( "m" );
1152 case MAG_CURRENTMONTHNAME:
1153 return $wgLang->getMonthName( date("n") );
1154 case MAG_CURRENTMONTHNAMEGEN:
1155 return $wgLang->getMonthNameGen( date("n") );
1156 case MAG_CURRENTDAY:
1157 return date("j");
1158 case MAG_CURRENTDAYNAME:
1159 return $wgLang->getWeekdayName( date("w")+1 );
1160 case MAG_CURRENTYEAR:
1161 return date( "Y" );
1162 case MAG_CURRENTTIME:
1163 return $wgLang->time( wfTimestampNow(), false );
1164 case MAG_NUMBEROFARTICLES:
1165 return wfNumberOfArticles();
1166 case MAG_SITENAME:
1167 return $wgSitename;
1168 case MAG_SERVER:
1169 return $wgServer;
1170 default:
1171 return NULL;
1175 function initialiseVariables()
1177 global $wgVariableIDs;
1178 $this->mVariables = array();
1179 foreach ( $wgVariableIDs as $id ) {
1180 $mw =& MagicWord::get( $id );
1181 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1185 /* private */ function replaceVariables( $text, $args = array() )
1187 global $wgLang, $wgScript, $wgArticlePath;
1189 $fname = "Parser::replaceVariables";
1190 wfProfileIn( $fname );
1192 $bail = false;
1193 if ( !$this->mVariables ) {
1194 $this->initialiseVariables();
1196 $titleChars = Title::legalChars();
1197 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1199 # This function is called recursively. To keep track of arguments we need a stack:
1200 array_push( $this->mArgStack, $args );
1202 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1203 $GLOBALS['wgCurParser'] =& $this;
1204 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1206 array_pop( $this->mArgStack );
1208 return $text;
1211 function braceSubstitution( $matches )
1213 global $wgLinkCache, $wgLang;
1214 $fname = "Parser::braceSubstitution";
1215 $found = false;
1216 $nowiki = false;
1217 $title = NULL;
1219 # $newline is an optional newline character before the braces
1220 # $part1 is the bit before the first |, and must contain only title characters
1221 # $args is a list of arguments, starting from index 0, not including $part1
1223 $newline = $matches[1];
1224 $part1 = $matches[2];
1225 # If the third subpattern matched anything, it will start with |
1226 if ( $matches[3] !== "" ) {
1227 $args = explode( "|", substr( $matches[3], 1 ) );
1228 } else {
1229 $args = array();
1231 $argc = count( $args );
1233 # SUBST
1234 $mwSubst =& MagicWord::get( MAG_SUBST );
1235 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1236 if ( $this->mOutputType != OT_WIKI ) {
1237 # Invalid SUBST not replaced at PST time
1238 # Return without further processing
1239 $text = $matches[0];
1240 $found = true;
1242 } elseif ( $this->mOutputType == OT_WIKI ) {
1243 # SUBST not found in PST pass, do nothing
1244 $text = $matches[0];
1245 $found = true;
1248 # MSG, MSGNW and INT
1249 if ( !$found ) {
1250 # Check for MSGNW:
1251 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1252 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1253 $nowiki = true;
1254 } else {
1255 # Remove obsolete MSG:
1256 $mwMsg =& MagicWord::get( MAG_MSG );
1257 $mwMsg->matchStartAndRemove( $part1 );
1260 # Check if it is an internal message
1261 $mwInt =& MagicWord::get( MAG_INT );
1262 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1263 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1264 $text = wfMsgReal( $part1, $args, true );
1265 $found = true;
1270 # NS
1271 if ( !$found ) {
1272 # Check for NS: (namespace expansion)
1273 $mwNs = MagicWord::get( MAG_NS );
1274 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1275 if ( intval( $part1 ) ) {
1276 $text = $wgLang->getNsText( intval( $part1 ) );
1277 $found = true;
1278 } else {
1279 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1280 if ( !is_null( $index ) ) {
1281 $text = $wgLang->getNsText( $index );
1282 $found = true;
1288 # LOCALURL and LOCALURLE
1289 if ( !$found ) {
1290 $mwLocal = MagicWord::get( MAG_LOCALURL );
1291 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1293 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1294 $func = 'getLocalURL';
1295 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1296 $func = 'escapeLocalURL';
1297 } else {
1298 $func = '';
1301 if ( $func !== '' ) {
1302 $title = Title::newFromText( $part1 );
1303 if ( !is_null( $title ) ) {
1304 if ( $argc > 0 ) {
1305 $text = $title->$func( $args[0] );
1306 } else {
1307 $text = $title->$func();
1309 $found = true;
1314 # Internal variables
1315 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1316 $text = $this->mVariables[$part1];
1317 $found = true;
1318 $this->mOutput->mContainsOldMagic = true;
1321 # Arguments input from the caller
1322 $inputArgs = end( $this->mArgStack );
1323 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1324 $text = $inputArgs[$part1];
1325 $found = true;
1328 # Load from database
1329 if ( !$found ) {
1330 $title = Title::newFromText( $part1, NS_TEMPLATE );
1331 if ( !is_null( $title ) && !$title->isExternal() ) {
1332 # Check for excessive inclusion
1333 $dbk = $title->getPrefixedDBkey();
1334 if ( $this->incrementIncludeCount( $dbk ) ) {
1335 $article = new Article( $title );
1336 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1337 if ( $articleContent !== false ) {
1338 $found = true;
1339 $text = $articleContent;
1344 # If the title is valid but undisplayable, make a link to it
1345 if ( $this->mOutputType == OT_HTML && !$found ) {
1346 $text = "[[" . $title->getPrefixedText() . "]]";
1347 $found = true;
1352 # Recursive parsing, escaping and link table handling
1353 # Only for HTML output
1354 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1355 $text = wfEscapeWikiText( $text );
1356 } elseif ( $this->mOutputType == OT_HTML && $found ) {
1357 # Clean up argument array
1358 $assocArgs = array();
1359 $index = 1;
1360 foreach( $args as $arg ) {
1361 $eqpos = strpos( $arg, "=" );
1362 if ( $eqpos === false ) {
1363 $assocArgs[$index++] = $arg;
1364 } else {
1365 $name = trim( substr( $arg, 0, $eqpos ) );
1366 $value = trim( substr( $arg, $eqpos+1 ) );
1367 if ( $value === false ) {
1368 $value = "";
1370 if ( $name !== false ) {
1371 $assocArgs[$name] = $value;
1376 # Do not enter included links in link table
1377 if ( !is_null( $title ) ) {
1378 $wgLinkCache->suspend();
1381 # Run full parser on the included text
1382 $text = $this->strip( $text, $this->mStripState );
1383 $text = $this->internalParse( $text, (bool)$newline, $assocArgs );
1385 # Add the result to the strip state for re-inclusion after
1386 # the rest of the processing
1387 $text = $this->insertStripItem( $text, $this->mStripState );
1389 # Resume the link cache and register the inclusion as a link
1390 if ( !is_null( $title ) ) {
1391 $wgLinkCache->resume();
1392 $wgLinkCache->addLinkObj( $title );
1396 if ( !$found ) {
1397 return $matches[0];
1398 } else {
1399 return $newline . $text;
1403 # Returns true if the function is allowed to include this entity
1404 function incrementIncludeCount( $dbk )
1406 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1407 $this->mIncludeCount[$dbk] = 0;
1409 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1410 return true;
1411 } else {
1412 return false;
1417 # Cleans up HTML, removes dangerous tags and attributes
1418 /* private */ function removeHTMLtags( $text )
1420 $fname = "Parser::removeHTMLtags";
1421 wfProfileIn( $fname );
1422 $htmlpairs = array( # Tags that must be closed
1423 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1424 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1425 "strike", "strong", "tt", "var", "div", "center",
1426 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1427 "ruby", "rt" , "rb" , "rp", "p"
1429 $htmlsingle = array(
1430 "br", "hr", "li", "dt", "dd"
1432 $htmlnest = array( # Tags that can be nested--??
1433 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1434 "dl", "font", "big", "small", "sub", "sup"
1436 $tabletags = array( # Can only appear inside table
1437 "td", "th", "tr"
1440 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1441 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1443 $htmlattrs = $this->getHTMLattrs () ;
1445 # Remove HTML comments
1446 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1448 $bits = explode( "<", $text );
1449 $text = array_shift( $bits );
1450 $tagstack = array(); $tablestack = array();
1452 foreach ( $bits as $x ) {
1453 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1454 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1455 $x, $regs );
1456 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1457 error_reporting( $prev );
1459 $badtag = 0 ;
1460 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1461 # Check our stack
1462 if ( $slash ) {
1463 # Closing a tag...
1464 if ( ! in_array( $t, $htmlsingle ) &&
1465 ( $ot = array_pop( $tagstack ) ) != $t ) {
1466 array_push( $tagstack, $ot );
1467 $badtag = 1;
1468 } else {
1469 if ( $t == "table" ) {
1470 $tagstack = array_pop( $tablestack );
1472 $newparams = "";
1474 } else {
1475 # Keep track for later
1476 if ( in_array( $t, $tabletags ) &&
1477 ! in_array( "table", $tagstack ) ) {
1478 $badtag = 1;
1479 } else if ( in_array( $t, $tagstack ) &&
1480 ! in_array ( $t , $htmlnest ) ) {
1481 $badtag = 1 ;
1482 } else if ( ! in_array( $t, $htmlsingle ) ) {
1483 if ( $t == "table" ) {
1484 array_push( $tablestack, $tagstack );
1485 $tagstack = array();
1487 array_push( $tagstack, $t );
1489 # Strip non-approved attributes from the tag
1490 $newparams = $this->fixTagAttributes($params);
1493 if ( ! $badtag ) {
1494 $rest = str_replace( ">", "&gt;", $rest );
1495 $text .= "<$slash$t $newparams$brace$rest";
1496 continue;
1499 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1501 # Close off any remaining tags
1502 while ( $t = array_pop( $tagstack ) ) {
1503 $text .= "</$t>\n";
1504 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1506 wfProfileOut( $fname );
1507 return $text;
1512 * This function accomplishes several tasks:
1513 * 1) Auto-number headings if that option is enabled
1514 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1515 * 3) Add a Table of contents on the top for users who have enabled the option
1516 * 4) Auto-anchor headings
1518 * It loops through all headlines, collects the necessary data, then splits up the
1519 * string and re-inserts the newly formatted headlines.
1523 /* private */ function formatHeadings( $text )
1525 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1526 $doShowToc = $this->mOptions->getShowToc();
1527 if( !$this->mTitle->userCanEdit() ) {
1528 $showEditLink = 0;
1529 $rightClickHack = 0;
1530 } else {
1531 $showEditLink = $this->mOptions->getEditSection();
1532 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1535 # Inhibit editsection links if requested in the page
1536 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1537 if( $esw->matchAndRemove( $text ) ) {
1538 $showEditLink = 0;
1540 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1541 # do not add TOC
1542 $mw =& MagicWord::get( MAG_NOTOC );
1543 if( $mw->matchAndRemove( $text ) ) {
1544 $doShowToc = 0;
1547 # never add the TOC to the Main Page. This is an entry page that should not
1548 # be more than 1-2 screens large anyway
1549 if( $this->mTitle->getPrefixedText() == wfMsg("mainpage") ) {
1550 $doShowToc = 0;
1553 # Get all headlines for numbering them and adding funky stuff like [edit]
1554 # links - this is for later, but we need the number of headlines right now
1555 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1557 # if there are fewer than 4 headlines in the article, do not show TOC
1558 if( $numMatches < 4 ) {
1559 $doShowToc = 0;
1562 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1563 # override above conditions and always show TOC
1564 $mw =& MagicWord::get( MAG_FORCETOC );
1565 if ($mw->matchAndRemove( $text ) ) {
1566 $doShowToc = 1;
1570 # We need this to perform operations on the HTML
1571 $sk =& $this->mOptions->getSkin();
1573 # headline counter
1574 $headlineCount = 0;
1576 # Ugh .. the TOC should have neat indentation levels which can be
1577 # passed to the skin functions. These are determined here
1578 $toclevel = 0;
1579 $toc = "";
1580 $full = "";
1581 $head = array();
1582 $sublevelCount = array();
1583 $level = 0;
1584 $prevlevel = 0;
1585 foreach( $matches[3] as $headline ) {
1586 $numbering = "";
1587 if( $level ) {
1588 $prevlevel = $level;
1590 $level = $matches[1][$headlineCount];
1591 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1592 # reset when we enter a new level
1593 $sublevelCount[$level] = 0;
1594 $toc .= $sk->tocIndent( $level - $prevlevel );
1595 $toclevel += $level - $prevlevel;
1597 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1598 # reset when we step back a level
1599 $sublevelCount[$level+1]=0;
1600 $toc .= $sk->tocUnindent( $prevlevel - $level );
1601 $toclevel -= $prevlevel - $level;
1603 # count number of headlines for each level
1604 @$sublevelCount[$level]++;
1605 if( $doNumberHeadings || $doShowToc ) {
1606 $dot = 0;
1607 for( $i = 1; $i <= $level; $i++ ) {
1608 if( !empty( $sublevelCount[$i] ) ) {
1609 if( $dot ) {
1610 $numbering .= ".";
1612 $numbering .= $sublevelCount[$i];
1613 $dot = 1;
1618 # The canonized header is a version of the header text safe to use for links
1619 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1620 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
1622 # strip out HTML
1623 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1624 $tocline = trim( $canonized_headline );
1625 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1626 $refer[$headlineCount] = $canonized_headline;
1628 # count how many in assoc. array so we can track dupes in anchors
1629 @$refers[$canonized_headline]++;
1630 $refcount[$headlineCount]=$refers[$canonized_headline];
1632 # Prepend the number to the heading text
1634 if( $doNumberHeadings || $doShowToc ) {
1635 $tocline = $numbering . " " . $tocline;
1637 # Don't number the heading if it is the only one (looks silly)
1638 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1639 # the two are different if the line contains a link
1640 $headline=$numbering . " " . $headline;
1644 # Create the anchor for linking from the TOC to the section
1645 $anchor = $canonized_headline;
1646 if($refcount[$headlineCount] > 1 ) {
1647 $anchor .= "_" . $refcount[$headlineCount];
1649 if( $doShowToc ) {
1650 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1652 if( $showEditLink ) {
1653 if ( empty( $head[$headlineCount] ) ) {
1654 $head[$headlineCount] = "";
1656 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
1659 # Add the edit section span
1660 if( $rightClickHack ) {
1661 $headline = $sk->editSectionScript($headlineCount+1,$headline);
1664 # give headline the correct <h#> tag
1665 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1667 $headlineCount++;
1670 if( $doShowToc ) {
1671 $toclines = $headlineCount;
1672 $toc .= $sk->tocUnindent( $toclevel );
1673 $toc = $sk->tocTable( $toc );
1676 # split up and insert constructed headlines
1678 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1679 $i = 0;
1681 foreach( $blocks as $block ) {
1682 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1683 # This is the [edit] link that appears for the top block of text when
1684 # section editing is enabled
1686 # Disabled because it broke block formatting
1687 # For example, a bullet point in the top line
1688 # $full .= $sk->editSectionLink(0);
1690 $full .= $block;
1691 if( $doShowToc && !$i) {
1692 # Top anchor now in skin
1693 $full = $full.$toc;
1696 if( !empty( $head[$i] ) ) {
1697 $full .= $head[$i];
1699 $i++;
1702 return $full;
1705 /* private */ function doMagicISBN( &$tokenizer )
1707 global $wgLang;
1709 # Check whether next token is a text token
1710 # If yes, fetch it and convert the text into a
1711 # Special::BookSources link
1712 $token = $tokenizer->previewToken();
1713 while ( $token["type"] == "" )
1715 $tokenizer->nextToken();
1716 $token = $tokenizer->previewToken();
1718 if ( $token["type"] == "text" )
1720 $token = $tokenizer->nextToken();
1721 $x = $token["text"];
1722 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1724 $isbn = $blank = "" ;
1725 while ( " " == $x{0} ) {
1726 $blank .= " ";
1727 $x = substr( $x, 1 );
1729 while ( strstr( $valid, $x{0} ) != false ) {
1730 $isbn .= $x{0};
1731 $x = substr( $x, 1 );
1733 $num = str_replace( "-", "", $isbn );
1734 $num = str_replace( " ", "", $num );
1736 if ( "" == $num ) {
1737 $text = "ISBN $blank$x";
1738 } else {
1739 $titleObj = Title::makeTitle( NS_SPECIAL, "Booksources" );
1740 $text = "<a href=\"" .
1741 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1742 "\" class=\"internal\">ISBN $isbn</a>";
1743 $text .= $x;
1745 } else {
1746 $text = "ISBN ";
1748 return $text;
1750 /* private */ function doMagicRFC( &$tokenizer )
1752 global $wgLang;
1754 # Check whether next token is a text token
1755 # If yes, fetch it and convert the text into a
1756 # link to an RFC source
1757 $token = $tokenizer->previewToken();
1758 while ( $token["type"] == "" )
1760 $tokenizer->nextToken();
1761 $token = $tokenizer->previewToken();
1763 if ( $token["type"] == "text" )
1765 $token = $tokenizer->nextToken();
1766 $x = $token["text"];
1767 $valid = "0123456789";
1769 $rfc = $blank = "" ;
1770 while ( " " == $x{0} ) {
1771 $blank .= " ";
1772 $x = substr( $x, 1 );
1774 while ( strstr( $valid, $x{0} ) != false ) {
1775 $rfc .= $x{0};
1776 $x = substr( $x, 1 );
1779 if ( "" == $rfc ) {
1780 $text .= "RFC $blank$x";
1781 } else {
1782 $url = wfmsg( "rfcurl" );
1783 $url = str_replace( "$1", $rfc, $url);
1784 $sk =& $this->mOptions->getSkin();
1785 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1786 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1788 } else {
1789 $text = "RFC ";
1791 return $text;
1794 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1796 $this->mOptions = $options;
1797 $this->mTitle =& $title;
1798 $this->mOutputType = OT_WIKI;
1800 if ( $clearState ) {
1801 $this->clearState();
1804 $stripState = false;
1805 $pairs = array(
1806 "\r\n" => "\n",
1808 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
1809 // now with regexes
1810 $pairs = array(
1811 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
1812 "/<br *?>/i" => "<br/>",
1814 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
1815 $text = $this->strip( $text, $stripState, false );
1816 $text = $this->pstPass2( $text, $user );
1817 $text = $this->unstrip( $text, $stripState );
1818 return $text;
1821 /* private */ function pstPass2( $text, &$user )
1823 global $wgLang, $wgLocaltimezone, $wgCurParser;
1825 # Variable replacement
1826 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1827 $text = $this->replaceVariables( $text );
1829 # Signatures
1831 $n = $user->getName();
1832 $k = $user->getOption( "nickname" );
1833 if ( "" == $k ) { $k = $n; }
1834 if(isset($wgLocaltimezone)) {
1835 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1837 /* Note: this is an ugly timezone hack for the European wikis */
1838 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1839 " (" . date( "T" ) . ")";
1840 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1842 $text = preg_replace( "/~~~~~/", $d, $text );
1843 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1844 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1845 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1846 Namespace::getUser() ) . ":$n|$k]]", $text );
1848 # Context links: [[|name]] and [[name (context)|]]
1850 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1851 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1852 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1853 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1855 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1856 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1857 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1858 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1859 # [[ns:page (cont)|]]
1860 $context = "";
1861 $t = $this->mTitle->getText();
1862 if ( preg_match( $conpat, $t, $m ) ) {
1863 $context = $m[2];
1865 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1866 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1867 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1869 if ( "" == $context ) {
1870 $text = preg_replace( $p2, "[[\\1]]", $text );
1871 } else {
1872 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1876 $mw =& MagicWord::get( MAG_SUBST );
1877 $wgCurParser = $this->fork();
1878 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1879 $this->merge( $wgCurParser );
1882 # Trim trailing whitespace
1883 # MAG_END (__END__) tag allows for trailing
1884 # whitespace to be deliberately included
1885 $text = rtrim( $text );
1886 $mw =& MagicWord::get( MAG_END );
1887 $mw->matchAndRemove( $text );
1889 return $text;
1892 # Set up some variables which are usually set up in parse()
1893 # so that an external function can call some class members with confidence
1894 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1896 $this->mTitle =& $title;
1897 $this->mOptions = $options;
1898 $this->mOutputType = $outputType;
1899 if ( $clearState ) {
1900 $this->clearState();
1904 function transformMsg( $text, $options ) {
1905 global $wgTitle;
1906 static $executing = false;
1908 # Guard against infinite recursion
1909 if ( $executing ) {
1910 return $text;
1912 $executing = true;
1914 $this->mTitle = $wgTitle;
1915 $this->mOptions = $options;
1916 $this->mOutputType = OT_MSG;
1917 $this->clearState();
1918 $text = $this->replaceVariables( $text );
1920 $executing = false;
1921 return $text;
1925 class ParserOutput
1927 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1929 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1930 $containsOldMagic = false )
1932 $this->mText = $text;
1933 $this->mLanguageLinks = $languageLinks;
1934 $this->mCategoryLinks = $categoryLinks;
1935 $this->mContainsOldMagic = $containsOldMagic;
1938 function getText() { return $this->mText; }
1939 function getLanguageLinks() { return $this->mLanguageLinks; }
1940 function getCategoryLinks() { return $this->mCategoryLinks; }
1941 function containsOldMagic() { return $this->mContainsOldMagic; }
1942 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
1943 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
1944 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
1945 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
1947 function merge( $other ) {
1948 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
1949 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
1950 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
1955 class ParserOptions
1957 # All variables are private
1958 var $mUseTeX; # Use texvc to expand <math> tags
1959 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1960 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1961 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1962 var $mAllowExternalImages; # Allow external images inline
1963 var $mSkin; # Reference to the preferred skin
1964 var $mDateFormat; # Date format index
1965 var $mEditSection; # Create "edit section" links
1966 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1967 var $mNumberHeadings; # Automatically number headings
1968 var $mShowToc; # Show table of contents
1970 function getUseTeX() { return $this->mUseTeX; }
1971 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
1972 function getUseDynamicDates() { return $this->mUseDynamicDates; }
1973 function getInterwikiMagic() { return $this->mInterwikiMagic; }
1974 function getAllowExternalImages() { return $this->mAllowExternalImages; }
1975 function getSkin() { return $this->mSkin; }
1976 function getDateFormat() { return $this->mDateFormat; }
1977 function getEditSection() { return $this->mEditSection; }
1978 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
1979 function getNumberHeadings() { return $this->mNumberHeadings; }
1980 function getShowToc() { return $this->mShowToc; }
1982 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
1983 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
1984 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
1985 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
1986 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
1987 function setSkin( $x ) { return wfSetRef( $this->mSkin, $x ); }
1988 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
1989 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
1990 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
1991 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
1992 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
1994 /* static */ function newFromUser( &$user )
1996 $popts = new ParserOptions;
1997 $popts->initialiseFromUser( $user );
1998 return $popts;
2001 function initialiseFromUser( &$userInput )
2003 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2005 if ( !$userInput ) {
2006 $user = new User;
2007 $user->setLoaded( true );
2008 } else {
2009 $user =& $userInput;
2012 $this->mUseTeX = $wgUseTeX;
2013 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2014 $this->mUseDynamicDates = $wgUseDynamicDates;
2015 $this->mInterwikiMagic = $wgInterwikiMagic;
2016 $this->mAllowExternalImages = $wgAllowExternalImages;
2017 $this->mSkin =& $user->getSkin();
2018 $this->mDateFormat = $user->getOption( "date" );
2019 $this->mEditSection = $user->getOption( "editsection" );
2020 $this->mEditSectionOnRightClick = $user->getOption( "editsectiononrightclick" );
2021 $this->mNumberHeadings = $user->getOption( "numberheadings" );
2022 $this->mShowToc = $user->getOption( "showtoc" );
2028 # Regex callbacks, used in Parser::replaceVariables
2029 function wfBraceSubstitution( $matches )
2031 global $wgCurParser;
2032 return $wgCurParser->braceSubstitution( $matches );