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 )
89 $fname = "Parser::parse";
90 wfProfileIn( $fname );
96 $this->mOptions
= $options;
97 $this->mTitle
=& $title;
98 $this->mOutputType
= OT_HTML
;
101 $text = $this->strip( $text, $this->mStripState
);
102 $text = $this->internalParse( $text, $linestart );
103 $text = $this->unstrip( $text, $this->mStripState
);
104 # Clean up special characters, only run once, next-to-last before doBlockLevels
106 "/<hr *>/i" => '<hr/>',
107 "/<br *>/i" => '<br/>',
108 "/<center *>/i"=>'<div class="center">',
109 "/<\\/center *>/i" => '</div>',
110 # Clean up spare ampersands; note that we probably ought to be
111 # more careful about named entities.
112 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
114 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
117 $text = $this->doBlockLevels( $text, $linestart );
119 $this->mOutput
->setText( $text );
120 wfProfileOut( $fname );
121 return $this->mOutput
;
124 /* static */ function getRandomString()
126 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
129 # Replaces all occurrences of <$tag>content</$tag> in the text
130 # with a random marker and returns the new text. the output parameter
131 # $content will be an associative array filled with data on the form
132 # $unique_marker => content.
134 # If $content is already set, the additional entries will be appended
136 # If $tag is set to STRIP_COMMENTS, the function will extract
137 # <!-- HTML comments -->
139 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
140 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
147 while ( "" != $text ) {
148 if($tag==STRIP_COMMENTS
) {
149 $p = preg_split( "/<!--/i", $text, 2 );
151 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
154 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
157 if($tag==STRIP_COMMENTS
) {
158 $q = preg_split( "/-->/i", $p[1], 2 );
160 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
162 $marker = $rnd . sprintf("%08X", $n++
);
163 $content[$marker] = $q[0];
164 $stripped .= $marker;
171 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
172 # If $render is set, performs necessary rendering operations on plugins
173 # Returns the text, and fills an array with data needed in unstrip()
174 # If the $state is already a valid strip state, it adds to the state
176 # When $stripcomments is set, HTML comments <!-- like this -->
177 # will be stripped in addition to other tags. This is important
178 # for section editing, where these comments cause confusion when
179 # counting the sections in the wikisource
180 function strip( $text, &$state, $stripcomments = false )
182 $render = ($this->mOutputType
== OT_HTML
);
183 $nowiki_content = array();
184 $hiero_content = array();
185 $math_content = array();
186 $pre_content = array();
187 $comment_content = array();
189 # Replace any instances of the placeholders
190 $uniq_prefix = UNIQ_PREFIX
;
191 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
193 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
194 foreach( $nowiki_content as $marker => $content ){
196 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
198 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
202 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
203 foreach( $hiero_content as $marker => $content ){
204 if( $render && $GLOBALS['wgUseWikiHiero']){
205 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
207 $hiero_content[$marker] = "<hiero>$content</hiero>";
211 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
212 foreach( $math_content as $marker => $content ){
213 if( $render && $this->mOptions
->getUseTeX() ){
214 $math_content[$marker] = renderMath( $content );
216 $math_content[$marker] = "<math>$content</math>";
220 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
221 foreach( $pre_content as $marker => $content ){
223 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
225 $pre_content[$marker] = "<pre>$content</pre>";
229 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
230 foreach( $comment_content as $marker => $content ){
231 $comment_content[$marker] = "<!--$content-->";
235 # Merge state with the pre-existing state, if there is one
237 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
238 $state['hiero'] = $state['hiero'] +
$hiero_content;
239 $state['math'] = $state['math'] +
$math_content;
240 $state['pre'] = $state['pre'] +
$pre_content;
241 $state['comment'] = $state['comment'] +
$comment_content;
244 'nowiki' => $nowiki_content,
245 'hiero' => $hiero_content,
246 'math' => $math_content,
247 'pre' => $pre_content,
248 'comment' => $comment_content
254 function unstrip( $text, &$state )
256 # Must expand in reverse order, otherwise nested tags will be corrupted
257 $contentDict = end( $state );
258 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
259 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
260 $text = str_replace( key( $contentDict ), $content, $text );
267 # Add an item to the strip state
268 # Returns the unique tag which must be inserted into the stripped text
269 # The tag will be replaced with the original text in unstrip()
271 function insertStripItem( $text, &$state )
273 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
282 $state['item'][$rnd] = $text;
286 # This method generates the list of subcategories and pages for a category
287 function categoryMagic ()
289 global $wgLang , $wgUser ;
290 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
292 $cns = Namespace::getCategory() ;
293 if ( $this->mTitle
->getNamespace() != $cns ) return "" ; # This ain't a category page
295 $r = "<br style=\"clear:both;\"/>\n";
298 $sk =& $wgUser->getSkin() ;
300 $articles = array() ;
301 $children = array() ;
303 $id = $this->mTitle
->getArticleID() ;
305 # For existing categories
307 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
308 $res = wfQuery ( $sql, DB_READ
) ;
309 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
311 # For non-existing categories
312 $t = wfStrencode( $this->mTitle
->getPrefixedDBKey() );
313 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to='$t' AND bl_from=cur_id" ;
314 $res = wfQuery ( $sql, DB_READ
) ;
315 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
318 # For all pages that link to this category
319 foreach ( $data AS $x )
321 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
322 if ( $t != "" ) $t .= ":" ;
323 $t .= $x->cur_title
;
325 if ( $x->cur_namespace
== $cns ) {
326 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
328 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
331 wfFreeResult ( $res ) ;
333 # Showing subcategories
334 if ( count ( $children ) > 0 )
336 asort ( $children ) ;
337 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
338 $r .= implode ( ", " , $children ) ;
341 # Showing pages in this category
342 if ( count ( $articles ) > 0 )
344 $ti = $this->mTitle
->getText() ;
345 asort ( $articles ) ;
346 $h = wfMsg( "category_header", $ti );
347 $r .= "<h2>{$h}</h2>\n" ;
348 $r .= implode ( ", " , $articles ) ;
355 function getHTMLattrs ()
357 $htmlattrs = array( # Allowed attributes--no scripting, etc.
358 "title", "align", "lang", "dir", "width", "height",
359 "bgcolor", "clear", /* BR */ "noshade", /* HR */
360 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
361 /* FONT */ "type", "start", "value", "compact",
362 /* For various lists, mostly deprecated but safe */
363 "summary", "width", "border", "frame", "rules",
364 "cellspacing", "cellpadding", "valign", "char",
365 "charoff", "colgroup", "col", "span", "abbr", "axis",
366 "headers", "scope", "rowspan", "colspan", /* Tables */
367 "id", "class", "name", "style" /* For CSS */
372 function fixTagAttributes ( $t )
374 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
375 $htmlattrs = $this->getHTMLattrs() ;
377 # Strip non-approved attributes from the tag
379 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
380 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
382 # Strip javascript "expression" from stylesheets. Brute force approach:
383 # If anythin offensive is found, all attributes of the HTML tag are dropped
386 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
387 wfMungeToUtf8( $t ) ) )
395 function doTableStuff ( $t )
397 $t = explode ( "\n" , $t ) ;
398 $td = array () ; # Is currently a td tag open?
399 $ltd = array () ; # Was it TD or TH?
400 $tr = array () ; # Is currently a tr tag open?
401 $ltr = array () ; # tr attributes
402 foreach ( $t AS $k => $x )
405 $fc = substr ( $x , 0 , 1 ) ;
406 if ( "{|" == substr ( $x , 0 , 2 ) )
408 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
409 array_push ( $td , false ) ;
410 array_push ( $ltd , "" ) ;
411 array_push ( $tr , false ) ;
412 array_push ( $ltr , "" ) ;
414 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
415 else if ( "|}" == substr ( $x , 0 , 2 ) )
418 $l = array_pop ( $ltd ) ;
419 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
420 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
424 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
426 $z = trim ( substr ( $x , 2 ) ) ;
427 $t[$k] = "<caption>{$z}</caption>\n" ;
429 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
431 $x = substr ( $x , 1 ) ;
432 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
434 $l = array_pop ( $ltd ) ;
435 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
436 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
439 array_push ( $tr , false ) ;
440 array_push ( $td , false ) ;
441 array_push ( $ltd , "" ) ;
442 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
444 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
446 if ( "|+" == substr ( $x , 0 , 2 ) )
449 $x = substr ( $x , 1 ) ;
451 $after = substr ( $x , 1 ) ;
452 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
453 $after = explode ( "||" , $after ) ;
455 foreach ( $after AS $theline )
460 $tra = array_pop ( $ltr ) ;
461 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
462 array_push ( $tr , true ) ;
463 array_push ( $ltr , "" ) ;
466 $l = array_pop ( $ltd ) ;
467 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
468 if ( $fc == "|" ) $l = "td" ;
469 else if ( $fc == "!" ) $l = "th" ;
470 else if ( $fc == "+" ) $l = "caption" ;
472 array_push ( $ltd , $l ) ;
473 $y = explode ( "|" , $theline , 2 ) ;
474 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
475 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
477 array_push ( $td , true ) ;
482 # Closing open td, tr && table
483 while ( count ( $td ) > 0 )
485 if ( array_pop ( $td ) ) $t[] = "</td>" ;
486 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
490 $t = implode ( "\n" , $t ) ;
491 # $t = $this->removeHTMLtags( $t );
495 function internalParse( $text, $linestart, $args = array() )
497 $fname = "Parser::internalParse";
498 wfProfileIn( $fname );
500 $text = $this->removeHTMLtags( $text );
501 $text = $this->replaceVariables( $text, $args );
503 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
505 $text = $this->doHeadings( $text );
506 if($this->mOptions
->getUseDynamicDates()) {
507 global $wgDateFormatter;
508 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
510 $text = $this->replaceExternalLinks( $text );
511 $text = $this->doTokenizedParser ( $text );
512 $text = $this->doTableStuff ( $text ) ;
513 $text = $this->formatHeadings( $text );
514 $sk =& $this->mOptions
->getSkin();
515 $text = $sk->transformContent( $text );
517 if ( !isset ( $this->categoryMagicDone
) ) {
518 $text .= $this->categoryMagic () ;
519 $this->categoryMagicDone
= true ;
522 wfProfileOut( $fname );
527 /* private */ function doHeadings( $text )
529 for ( $i = 6; $i >= 1; --$i ) {
530 $h = substr( "======", 0, $i );
531 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
532 "<h{$i}>\\1</h{$i}>\\2", $text );
537 # Note: we have to do external links before the internal ones,
538 # and otherwise take great care in the order of things here, so
539 # that we don't end up interpreting some URLs twice.
541 /* private */ function replaceExternalLinks( $text )
543 $fname = "Parser::replaceExternalLinks";
544 wfProfileIn( $fname );
545 $text = $this->subReplaceExternalLinks( $text, "http", true );
546 $text = $this->subReplaceExternalLinks( $text, "https", true );
547 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
548 $text = $this->subReplaceExternalLinks( $text, "irc", false );
549 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
550 $text = $this->subReplaceExternalLinks( $text, "news", false );
551 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
552 wfProfileOut( $fname );
556 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
558 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
559 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
561 # this is the list of separators that should be ignored if they
562 # are the last character of an URL but that should be included
563 # if they occur within the URL, e.g. "go to www.foo.com, where .."
564 # in this case, the last comma should not become part of the URL,
565 # but in "www.foo.com/123,2342,32.htm" it should.
567 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
568 $images = "gif|png|jpg|jpeg";
570 # PLEASE NOTE: The curly braces { } are not part of the regex,
571 # they are interpreted as part of the string (used to tell PHP
572 # that the content of the string should be inserted there).
573 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
574 "((?i){$images})([^{$uc}]|$)/";
576 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
577 $sk =& $this->mOptions
->getSkin();
579 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
580 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
581 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
583 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
584 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
585 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
587 $s = str_replace( $unique, $protocol, $s );
589 $a = explode( "[{$protocol}:", " " . $s );
590 $s = array_shift( $a );
591 $s = substr( $s, 1 );
593 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
594 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
596 foreach ( $a as $line ) {
597 if ( preg_match( $e1, $line, $m ) ) {
598 $link = "{$protocol}:{$m[1]}";
600 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
601 else { $text = wfEscapeHTML( $link ); }
602 } else if ( preg_match( $e2, $line, $m ) ) {
603 $link = "{$protocol}:{$m[1]}";
607 $s .= "[{$protocol}:" . $line;
610 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
613 # Expand the URL for printable version
614 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
616 $la = $sk->getExternalLinkAttributes( $link, $text );
617 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
623 /* private */ function handle3Quotes( &$state, $token )
625 if ( $state["strong"] !== false ) {
626 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
628 # ''' lala ''lala '''
629 $s = "</em></strong><em>";
633 $state["strong"] = FALSE;
636 $state["strong"] = isset($token["pos"]) ?
$token["pos"] : true;
641 /* private */ function handle2Quotes( &$state, $token )
643 if ( $state["em"] !== false ) {
644 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
646 # ''lala'''lala'' ....'''
647 $s = "</strong></em><strong>";
651 $state["em"] = FALSE;
654 $state["em"] = isset($token["pos"]) ?
$token["pos"] : true;
660 /* private */ function handle5Quotes( &$state, $token )
663 if ( $state["em"] !== false && $state["strong"] !== false ) {
664 if ( $state["em"] < $state["strong"] ) {
665 $s .= "</strong></em>";
667 $s .= "</em></strong>";
669 $state["strong"] = $state["em"] = FALSE;
670 } elseif ( $state["em"] !== false ) {
671 $s .= "</em><strong>";
672 $state["em"] = FALSE;
673 $state["strong"] = $token["pos"];
674 } elseif ( $state["strong"] !== false ) {
675 $s .= "</strong><em>";
676 $state["strong"] = FALSE;
677 $state["em"] = $token["pos"];
678 } else { # not $em and not $strong
679 $s .= "<strong><em>";
680 $state["strong"] = $state["em"] = isset($token["pos"]) ?
$token["pos"] : true;
685 /* private */ function doTokenizedParser( $str )
687 global $wgLang; # for language specific parser hook
688 global $wgUploadDirectory, $wgUseTimeline;
690 $tokenizer=Tokenizer
::newFromString( $str );
691 $tokenStack = array();
694 $state["em"] = FALSE;
695 $state["strong"] = FALSE;
699 # The tokenizer splits the text into tokens and returns them one by one.
700 # Every call to the tokenizer returns a new token.
701 while ( $token = $tokenizer->nextToken() )
703 switch ( $token["type"] )
706 # simple text with no further markup
707 $txt = $token["text"];
710 # Text that contains blanks that have to be converted to
711 # non-breakable spaces for French.
712 # U+202F NARROW NO-BREAK SPACE might be a better choice, but
713 # browser support for Unicode spacing is poor.
714 $txt = str_replace( " ", " ", $token["text"] );
717 # remember the tag opened with 3 [
721 # FIXME : Treat orphaned open tags (stack not empty when text is over)
723 array_push( $tokenStack, $token );
730 # get text from stack, glue it together, and call the code to handle a
733 if ( count( $tokenStack ) == 0 )
735 # stack empty. Found a ]] without an opening [[
739 $lastToken = array_pop( $tokenStack );
740 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
742 if( !empty( $lastToken["text"] ) ) {
743 $linkText = $lastToken["text"] . $linkText;
745 $lastToken = array_pop( $tokenStack );
748 $txt = $linkText ."]]";
750 if( isset( $lastToken["text"] ) ) {
751 $prefix = $lastToken["text"];
755 $nextToken = $tokenizer->previewToken();
756 if ( $nextToken["type"] == "text" )
758 # Preview just looks at it. Now we have to fetch it.
759 $nextToken = $tokenizer->nextToken();
760 $txt .= $nextToken["text"];
762 $txt = $this->handleInternalLink( $this->unstrip($txt,$this->mStripState
), $prefix );
764 # did the tag start with 3 [ ?
766 # show the first as text
772 $tagIsOpen = (count( $tokenStack ) != 0);
778 # This and the three next ones handle quotes
779 $txt = $this->handle3Quotes( $state, $token );
782 $txt = $this->handle2Quotes( $state, $token );
785 $txt = $this->handle5Quotes( $state, $token );
795 $txt = $this->doMagicRFC( $tokenizer );
802 $txt = $this->doMagicISBN( $tokenizer );
806 if ( $wgUseTimeline &&
807 "" != ( $timelinesrc = $tokenizer->readAllUntil("</timeline>") ) )
809 $txt = renderTimeline( $timelinesrc );
815 # Call language specific Hook.
816 $txt = $wgLang->processToken( $token, $tokenStack );
817 if ( NULL == $txt ) {
818 # An unkown token. Highlight.
819 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
820 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
824 # If we're parsing the interior of a link, don't append the interior to $s,
825 # but push it to the stack so it can be processed when a ]] token is found.
826 if ( $tagIsOpen && $txt != "" ) {
827 $token["type"] = "text";
828 $token["text"] = $txt;
829 array_push( $tokenStack, $token );
834 if ( count( $tokenStack ) != 0 )
836 # still objects on stack. opened [[ tag without closing ]] tag.
838 while ( $lastToken = array_pop( $tokenStack ) )
840 if ( $lastToken["type"] == "text" )
842 $txt = $lastToken["text"] . $txt;
844 $txt = $lastToken["type"] . $txt;
852 /* private */ function handleInternalLink( $line, $prefix )
854 global $wgLang, $wgLinkCache;
855 global $wgNamespacesWithSubpages, $wgLanguageCode;
856 static $fname = "Parser::handleInternalLink" ;
857 wfProfileIn( $fname );
859 wfProfileIn( "$fname-setup" );
861 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
862 $sk =& $this->mOptions
->getSkin();
864 # Match a link having the form [[namespace:link|alternate]]trail
866 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
867 # Match the end of a line for a word that's not followed by whitespace,
868 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
869 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
870 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
871 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
874 # Special and Media are pseudo-namespaces; no pages actually exist in them
875 static $image = FALSE;
876 static $special = FALSE;
877 static $media = FALSE;
878 static $category = FALSE;
879 if ( !$image ) { $image = Namespace::getImage(); }
880 if ( !$special ) { $special = Namespace::getSpecial(); }
881 if ( !$media ) { $media = Namespace::getMedia(); }
882 if ( !$category ) { $category = Namespace::getCategory(); ; }
884 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
886 wfProfileOut( "$fname-setup" );
889 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
892 } else { # Invalid form; output directly
893 $s .= $prefix . "[[" . $line ;
899 :Foobar -- override special treatment of prefix (images, language links)
900 /Foobar -- convert to CurrentPage/Foobar
901 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
903 $c = substr($m[1],0,1);
904 $noforce = ($c != ":");
905 if( $c == "/" ) { # subpage
906 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
907 $m[1]=substr($m[1],1,strlen($m[1])-2);
910 $noslash=substr($m[1],1);
912 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
913 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
916 } # this might be changed for ugliness reasons
918 $link = $noslash; # no subpage allowed, use standard link
920 } elseif( $noforce ) { # no subpage
923 $link = substr( $m[1], 1 );
928 $nt = Title
::newFromText( $link );
930 $s .= $prefix . "[[" . $line;
933 $ns = $nt->getNamespace();
934 $iw = $nt->getInterWiki();
936 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
937 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
938 return (trim($s) == '')?
'': $s;
940 if( $ns == $image ) {
941 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
942 $wgLinkCache->addImageLinkObj( $nt );
945 if ( $ns == $category ) {
946 $t = $nt->getText() ;
947 $nnt = Title
::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
948 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
949 $this->mOutput
->mCategoryLinks
[] = $t ;
950 $s .= $prefix . $trail ;
954 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
955 ( strpos( $link, "#" ) == FALSE ) ) {
956 # Self-links are handled specially; generally de-link and change to bold.
957 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, "", $trail );
961 if( $ns == $media ) {
962 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
963 $wgLinkCache->addImageLinkObj( $nt );
965 } elseif( $ns == $special ) {
966 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
969 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
971 wfProfileOut( $fname );
975 # Some functions here used by doBlockLevels()
977 /* private */ function closeParagraph()
980 if ( '' != $this->mLastSection
) {
981 $result = "</" . $this->mLastSection
. ">\n";
983 $this->mInPre
= false;
984 $this->mLastSection
= "";
987 # getCommon() returns the length of the longest common substring
988 # of both arguments, starting at the beginning of both.
990 /* private */ function getCommon( $st1, $st2 )
992 $fl = strlen( $st1 );
993 $shorter = strlen( $st2 );
994 if ( $fl < $shorter ) { $shorter = $fl; }
996 for ( $i = 0; $i < $shorter; ++
$i ) {
997 if ( $st1{$i} != $st2{$i} ) { break; }
1001 # These next three functions open, continue, and close the list
1002 # element appropriate to the prefix character passed into them.
1004 /* private */ function openList( $char )
1006 $result = $this->closeParagraph();
1008 if ( "*" == $char ) { $result .= "<ul><li>"; }
1009 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1010 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1011 else if ( ";" == $char ) {
1012 $result .= "<dl><dt>";
1013 $this->mDTopen
= true;
1015 else { $result = "<!-- ERR 1 -->"; }
1020 /* private */ function nextItem( $char )
1022 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
1023 else if ( ":" == $char ||
";" == $char ) {
1025 if ( $this->mDTopen
) { $close = "</dt>"; }
1026 if ( ";" == $char ) {
1027 $this->mDTopen
= true;
1028 return $close . "<dt>";
1030 $this->mDTopen
= false;
1031 return $close . "<dd>";
1034 return "<!-- ERR 2 -->";
1037 /* private */function closeList( $char )
1039 if ( "*" == $char ) { $text = "</li></ul>"; }
1040 else if ( "#" == $char ) { $text = "</li></ol>"; }
1041 else if ( ":" == $char ) {
1042 if ( $this->mDTopen
) {
1043 $this->mDTopen
= false;
1044 $text = "</dt></dl>";
1046 $text = "</dd></dl>";
1049 else { return "<!-- ERR 3 -->"; }
1053 /* private */ function doBlockLevels( $text, $linestart ) {
1054 $fname = "Parser::doBlockLevels";
1055 wfProfileIn( $fname );
1057 # Parsing through the text line by line. The main thing
1058 # happening here is handling of block-level elements p, pre,
1059 # and making lists from lines starting with * # : etc.
1061 $textLines = explode( "\n", $text );
1063 $lastPrefix = $output = $lastLine = '';
1064 $this->mDTopen
= $inBlockElem = false;
1066 $paragraphStack = false;
1068 if ( !$linestart ) {
1069 $output .= array_shift( $textLines );
1071 foreach ( $textLines as $oLine ) {
1072 $lastPrefixLength = strlen( $lastPrefix );
1073 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1074 $preOpenMatch = preg_match("/<pre/i", $oLine );
1075 if (!$this->mInPre
) {
1076 $this->mInPre
= !empty($preOpenMatch);
1078 if ( !$this->mInPre
) {
1079 # Multiple prefixes may abut each other for nested lists.
1080 $prefixLength = strspn( $oLine, "*#:;" );
1081 $pref = substr( $oLine, 0, $prefixLength );
1084 $pref2 = str_replace( ";", ":", $pref );
1085 $t = substr( $oLine, $prefixLength );
1087 # Don't interpret any other prefixes in preformatted text
1089 $pref = $pref2 = '';
1094 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1095 # Same as the last item, so no need to deal with nesting or opening stuff
1096 $output .= $this->nextItem( substr( $pref, -1 ) );
1097 $paragraphStack = false;
1099 if ( ";" == substr( $pref, -1 ) ) {
1100 # The one nasty exception: definition lists work like this:
1101 # ; title : definition text
1102 # So we check for : in the remainder text to split up the
1103 # title and definition, without b0rking links.
1104 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1105 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1107 $output .= $term . $this->nextItem( ":" );
1111 } elseif( $prefixLength ||
$lastPrefixLength ) {
1112 # Either open or close a level...
1113 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1114 $paragraphStack = false;
1116 while( $commonPrefixLength < $lastPrefixLength ) {
1117 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1118 --$lastPrefixLength;
1120 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1121 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1123 while ( $prefixLength > $commonPrefixLength ) {
1124 $char = substr( $pref, $commonPrefixLength, 1 );
1125 $output .= $this->openList( $char );
1127 if ( ";" == $char ) {
1128 # FIXME: This is dupe of code above
1129 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1131 $output .= $term . $this->nextItem( ":" );
1135 ++
$commonPrefixLength;
1137 $lastPrefix = $pref2;
1139 if( 0 == $prefixLength ) {
1140 # No prefix (not in list)--go to paragraph mode
1141 $uniq_prefix = UNIQ_PREFIX
;
1142 // XXX: use a stack for nestable elements like span, table and div
1143 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1144 $closematch = preg_match(
1145 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1146 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1147 if ( $openmatch or $closematch ) {
1148 $paragraphStack = false;
1149 $output .= $this->closeParagraph();
1150 if($preOpenMatch and !$preCloseMatch) {
1151 $this->mInPre
= true;
1153 if ( $closematch ) {
1154 $inBlockElem = false;
1156 $inBlockElem = true;
1158 } else if ( !$inBlockElem ) {
1159 if ( " " == $t{0} ) {
1161 if ($this->mLastSection
!= 'pre') {
1162 $paragraphStack = false;
1163 $output .= $this->closeParagraph().'<pre>';
1164 $this->mLastSection
= 'pre';
1168 if ( '' == trim($t) ) {
1169 if ( $paragraphStack ) {
1170 $output .= $paragraphStack.'<br/>';
1171 $paragraphStack = false;
1172 $this->mLastSection
= 'p';
1174 if ($this->mLastSection
!= 'p' ) {
1175 $output .= $this->closeParagraph();
1176 $this->mLastSection
= '';
1177 $paragraphStack = "<p>";
1179 $paragraphStack = '</p><p>';
1183 if ( $paragraphStack ) {
1184 $output .= $paragraphStack;
1185 $paragraphStack = false;
1186 $this->mLastSection
= 'p';
1187 } else if ($this->mLastSection
!= 'p') {
1188 $output .= $this->closeParagraph().'<p>';
1189 $this->mLastSection
= 'p';
1195 if ($paragraphStack === false) {
1199 while ( $prefixLength ) {
1200 $output .= $this->closeList( $pref2{$prefixLength-1} );
1203 if ( "" != $this->mLastSection
) {
1204 $output .= "</" . $this->mLastSection
. ">";
1205 $this->mLastSection
= "";
1208 wfProfileOut( $fname );
1212 function getVariableValue( $index ) {
1213 global $wgLang, $wgSitename, $wgServer;
1216 case MAG_CURRENTMONTH
:
1218 case MAG_CURRENTMONTHNAME
:
1219 return $wgLang->getMonthName( date("n") );
1220 case MAG_CURRENTMONTHNAMEGEN
:
1221 return $wgLang->getMonthNameGen( date("n") );
1222 case MAG_CURRENTDAY
:
1225 return $this->mTitle
->getText();
1227 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1228 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1229 case MAG_CURRENTDAYNAME
:
1230 return $wgLang->getWeekdayName( date("w")+
1 );
1231 case MAG_CURRENTYEAR
:
1233 case MAG_CURRENTTIME
:
1234 return $wgLang->time( wfTimestampNow(), false );
1235 case MAG_NUMBEROFARTICLES
:
1236 return wfNumberOfArticles();
1246 function initialiseVariables()
1248 global $wgVariableIDs;
1249 $this->mVariables
= array();
1250 foreach ( $wgVariableIDs as $id ) {
1251 $mw =& MagicWord
::get( $id );
1252 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1256 /* private */ function replaceVariables( $text, $args = array() )
1258 global $wgLang, $wgScript, $wgArticlePath;
1260 $fname = "Parser::replaceVariables";
1261 wfProfileIn( $fname );
1264 if ( !$this->mVariables
) {
1265 $this->initialiseVariables();
1267 $titleChars = Title
::legalChars();
1268 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1270 # This function is called recursively. To keep track of arguments we need a stack:
1271 array_push( $this->mArgStack
, $args );
1273 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1274 $GLOBALS['wgCurParser'] =& $this;
1275 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1277 array_pop( $this->mArgStack
);
1282 function braceSubstitution( $matches )
1284 global $wgLinkCache, $wgLang;
1285 $fname = "Parser::braceSubstitution";
1290 # $newline is an optional newline character before the braces
1291 # $part1 is the bit before the first |, and must contain only title characters
1292 # $args is a list of arguments, starting from index 0, not including $part1
1294 $newline = $matches[1];
1295 $part1 = $matches[2];
1296 # If the third subpattern matched anything, it will start with |
1297 if ( $matches[3] !== "" ) {
1298 $args = explode( "|", substr( $matches[3], 1 ) );
1302 $argc = count( $args );
1305 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1306 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1307 if ( $this->mOutputType
!= OT_WIKI
) {
1308 # Invalid SUBST not replaced at PST time
1309 # Return without further processing
1310 $text = $matches[0];
1313 } elseif ( $this->mOutputType
== OT_WIKI
) {
1314 # SUBST not found in PST pass, do nothing
1315 $text = $matches[0];
1319 # MSG, MSGNW and INT
1322 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1323 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1326 # Remove obsolete MSG:
1327 $mwMsg =& MagicWord
::get( MAG_MSG
);
1328 $mwMsg->matchStartAndRemove( $part1 );
1331 # Check if it is an internal message
1332 $mwInt =& MagicWord
::get( MAG_INT
);
1333 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1334 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1335 $text = wfMsgReal( $part1, $args, true );
1343 # Check for NS: (namespace expansion)
1344 $mwNs = MagicWord
::get( MAG_NS
);
1345 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1346 if ( intval( $part1 ) ) {
1347 $text = $wgLang->getNsText( intval( $part1 ) );
1350 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1351 if ( !is_null( $index ) ) {
1352 $text = $wgLang->getNsText( $index );
1359 # LOCALURL and LOCALURLE
1361 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1362 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1364 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1365 $func = 'getLocalURL';
1366 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1367 $func = 'escapeLocalURL';
1372 if ( $func !== '' ) {
1373 $title = Title
::newFromText( $part1 );
1374 if ( !is_null( $title ) ) {
1376 $text = $title->$func( $args[0] );
1378 $text = $title->$func();
1385 # Internal variables
1386 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1387 $text = $this->mVariables
[$part1];
1389 $this->mOutput
->mContainsOldMagic
= true;
1392 # Arguments input from the caller
1393 $inputArgs = end( $this->mArgStack
);
1394 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1395 $text = $inputArgs[$part1];
1399 # Load from database
1401 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1402 if ( !is_null( $title ) && !$title->isExternal() ) {
1403 # Check for excessive inclusion
1404 $dbk = $title->getPrefixedDBkey();
1405 if ( $this->incrementIncludeCount( $dbk ) ) {
1406 $article = new Article( $title );
1407 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1408 if ( $articleContent !== false ) {
1410 $text = $articleContent;
1415 # If the title is valid but undisplayable, make a link to it
1416 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1417 $text = "[[" . $title->getPrefixedText() . "]]";
1423 # Recursive parsing, escaping and link table handling
1424 # Only for HTML output
1425 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1426 $text = wfEscapeWikiText( $text );
1427 } elseif ( $this->mOutputType
== OT_HTML
&& $found ) {
1428 # Clean up argument array
1429 $assocArgs = array();
1431 foreach( $args as $arg ) {
1432 $eqpos = strpos( $arg, "=" );
1433 if ( $eqpos === false ) {
1434 $assocArgs[$index++
] = $arg;
1436 $name = trim( substr( $arg, 0, $eqpos ) );
1437 $value = trim( substr( $arg, $eqpos+
1 ) );
1438 if ( $value === false ) {
1441 if ( $name !== false ) {
1442 $assocArgs[$name] = $value;
1447 # Do not enter included links in link table
1448 if ( !is_null( $title ) ) {
1449 $wgLinkCache->suspend();
1452 # Run full parser on the included text
1453 $text = $this->strip( $text, $this->mStripState
);
1454 $text = $this->internalParse( $text, (bool)$newline, $assocArgs );
1456 # Add the result to the strip state for re-inclusion after
1457 # the rest of the processing
1458 $text = $this->insertStripItem( $text, $this->mStripState
);
1460 # Resume the link cache and register the inclusion as a link
1461 if ( !is_null( $title ) ) {
1462 $wgLinkCache->resume();
1463 $wgLinkCache->addLinkObj( $title );
1470 return $newline . $text;
1474 # Returns true if the function is allowed to include this entity
1475 function incrementIncludeCount( $dbk )
1477 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1478 $this->mIncludeCount
[$dbk] = 0;
1480 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1488 # Cleans up HTML, removes dangerous tags and attributes
1489 /* private */ function removeHTMLtags( $text )
1491 $fname = "Parser::removeHTMLtags";
1492 wfProfileIn( $fname );
1493 $htmlpairs = array( # Tags that must be closed
1494 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1495 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1496 "strike", "strong", "tt", "var", "div", "center",
1497 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1498 "ruby", "rt" , "rb" , "rp", "p"
1500 $htmlsingle = array(
1501 "br", "hr", "li", "dt", "dd"
1503 $htmlnest = array( # Tags that can be nested--??
1504 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1505 "dl", "font", "big", "small", "sub", "sup"
1507 $tabletags = array( # Can only appear inside table
1511 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1512 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1514 $htmlattrs = $this->getHTMLattrs () ;
1516 # Remove HTML comments
1517 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1519 $bits = explode( "<", $text );
1520 $text = array_shift( $bits );
1521 $tagstack = array(); $tablestack = array();
1523 foreach ( $bits as $x ) {
1524 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1525 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1527 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1528 error_reporting( $prev );
1531 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1535 if ( ! in_array( $t, $htmlsingle ) &&
1536 ( $ot = array_pop( $tagstack ) ) != $t ) {
1537 array_push( $tagstack, $ot );
1540 if ( $t == "table" ) {
1541 $tagstack = array_pop( $tablestack );
1546 # Keep track for later
1547 if ( in_array( $t, $tabletags ) &&
1548 ! in_array( "table", $tagstack ) ) {
1550 } else if ( in_array( $t, $tagstack ) &&
1551 ! in_array ( $t , $htmlnest ) ) {
1553 } else if ( ! in_array( $t, $htmlsingle ) ) {
1554 if ( $t == "table" ) {
1555 array_push( $tablestack, $tagstack );
1556 $tagstack = array();
1558 array_push( $tagstack, $t );
1560 # Strip non-approved attributes from the tag
1561 $newparams = $this->fixTagAttributes($params);
1565 $rest = str_replace( ">", ">", $rest );
1566 $text .= "<$slash$t $newparams$brace$rest";
1570 $text .= "<" . str_replace( ">", ">", $x);
1572 # Close off any remaining tags
1573 while ( $t = array_pop( $tagstack ) ) {
1575 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1577 wfProfileOut( $fname );
1583 * This function accomplishes several tasks:
1584 * 1) Auto-number headings if that option is enabled
1585 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1586 * 3) Add a Table of contents on the top for users who have enabled the option
1587 * 4) Auto-anchor headings
1589 * It loops through all headlines, collects the necessary data, then splits up the
1590 * string and re-inserts the newly formatted headlines.
1594 /* private */ function formatHeadings( $text )
1596 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1597 $doShowToc = $this->mOptions
->getShowToc();
1598 if( !$this->mTitle
->userCanEdit() ) {
1600 $rightClickHack = 0;
1602 $showEditLink = $this->mOptions
->getEditSection();
1603 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1606 # Inhibit editsection links if requested in the page
1607 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1608 if( $esw->matchAndRemove( $text ) ) {
1611 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1613 $mw =& MagicWord
::get( MAG_NOTOC
);
1614 if( $mw->matchAndRemove( $text ) ) {
1618 # never add the TOC to the Main Page. This is an entry page that should not
1619 # be more than 1-2 screens large anyway
1620 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1624 # Get all headlines for numbering them and adding funky stuff like [edit]
1625 # links - this is for later, but we need the number of headlines right now
1626 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1628 # if there are fewer than 4 headlines in the article, do not show TOC
1629 if( $numMatches < 4 ) {
1633 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1634 # override above conditions and always show TOC
1635 $mw =& MagicWord
::get( MAG_FORCETOC
);
1636 if ($mw->matchAndRemove( $text ) ) {
1641 # We need this to perform operations on the HTML
1642 $sk =& $this->mOptions
->getSkin();
1647 # Ugh .. the TOC should have neat indentation levels which can be
1648 # passed to the skin functions. These are determined here
1653 $sublevelCount = array();
1656 foreach( $matches[3] as $headline ) {
1659 $prevlevel = $level;
1661 $level = $matches[1][$headlineCount];
1662 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1663 # reset when we enter a new level
1664 $sublevelCount[$level] = 0;
1665 $toc .= $sk->tocIndent( $level - $prevlevel );
1666 $toclevel +
= $level - $prevlevel;
1668 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1669 # reset when we step back a level
1670 $sublevelCount[$level+
1]=0;
1671 $toc .= $sk->tocUnindent( $prevlevel - $level );
1672 $toclevel -= $prevlevel - $level;
1674 # count number of headlines for each level
1675 @$sublevelCount[$level]++
;
1676 if( $doNumberHeadings ||
$doShowToc ) {
1678 for( $i = 1; $i <= $level; $i++
) {
1679 if( !empty( $sublevelCount[$i] ) ) {
1683 $numbering .= $sublevelCount[$i];
1689 # The canonized header is a version of the header text safe to use for links
1690 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1691 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1694 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1695 $tocline = trim( $canonized_headline );
1696 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1697 $refer[$headlineCount] = $canonized_headline;
1699 # count how many in assoc. array so we can track dupes in anchors
1700 @$refers[$canonized_headline]++
;
1701 $refcount[$headlineCount]=$refers[$canonized_headline];
1703 # Prepend the number to the heading text
1705 if( $doNumberHeadings ||
$doShowToc ) {
1706 $tocline = $numbering . " " . $tocline;
1708 # Don't number the heading if it is the only one (looks silly)
1709 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1710 # the two are different if the line contains a link
1711 $headline=$numbering . " " . $headline;
1715 # Create the anchor for linking from the TOC to the section
1716 $anchor = $canonized_headline;
1717 if($refcount[$headlineCount] > 1 ) {
1718 $anchor .= "_" . $refcount[$headlineCount];
1721 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1723 if( $showEditLink ) {
1724 if ( empty( $head[$headlineCount] ) ) {
1725 $head[$headlineCount] = "";
1727 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1730 # Add the edit section span
1731 if( $rightClickHack ) {
1732 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1735 # give headline the correct <h#> tag
1736 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1742 $toclines = $headlineCount;
1743 $toc .= $sk->tocUnindent( $toclevel );
1744 $toc = $sk->tocTable( $toc );
1747 # split up and insert constructed headlines
1749 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1752 foreach( $blocks as $block ) {
1753 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1754 # This is the [edit] link that appears for the top block of text when
1755 # section editing is enabled
1757 # Disabled because it broke block formatting
1758 # For example, a bullet point in the top line
1759 # $full .= $sk->editSectionLink(0);
1762 if( $doShowToc && !$i) {
1763 # Top anchor now in skin
1767 if( !empty( $head[$i] ) ) {
1776 /* private */ function doMagicISBN( &$tokenizer )
1780 # Check whether next token is a text token
1781 # If yes, fetch it and convert the text into a
1782 # Special::BookSources link
1783 $token = $tokenizer->previewToken();
1784 while ( $token["type"] == "" )
1786 $tokenizer->nextToken();
1787 $token = $tokenizer->previewToken();
1789 if ( $token["type"] == "text" )
1791 $token = $tokenizer->nextToken();
1792 $x = $token["text"];
1793 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1795 $isbn = $blank = "" ;
1796 while ( " " == $x{0} ) {
1798 $x = substr( $x, 1 );
1800 while ( strstr( $valid, $x{0} ) != false ) {
1802 $x = substr( $x, 1 );
1804 $num = str_replace( "-", "", $isbn );
1805 $num = str_replace( " ", "", $num );
1808 $text = "ISBN $blank$x";
1810 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1811 $text = "<a href=\"" .
1812 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1813 "\" class=\"internal\">ISBN $isbn</a>";
1821 /* private */ function doMagicRFC( &$tokenizer )
1825 # Check whether next token is a text token
1826 # If yes, fetch it and convert the text into a
1827 # link to an RFC source
1828 $token = $tokenizer->previewToken();
1829 while ( $token["type"] == "" )
1831 $tokenizer->nextToken();
1832 $token = $tokenizer->previewToken();
1834 if ( $token["type"] == "text" )
1836 $token = $tokenizer->nextToken();
1837 $x = $token["text"];
1838 $valid = "0123456789";
1840 $rfc = $blank = "" ;
1841 while ( " " == $x{0} ) {
1843 $x = substr( $x, 1 );
1845 while ( strstr( $valid, $x{0} ) != false ) {
1847 $x = substr( $x, 1 );
1851 $text .= "RFC $blank$x";
1853 $url = wfmsg( "rfcurl" );
1854 $url = str_replace( "$1", $rfc, $url);
1855 $sk =& $this->mOptions
->getSkin();
1856 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1857 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1865 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1867 $this->mOptions
= $options;
1868 $this->mTitle
=& $title;
1869 $this->mOutputType
= OT_WIKI
;
1871 if ( $clearState ) {
1872 $this->clearState();
1875 $stripState = false;
1879 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
1882 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
1883 "/<br *?>/i" => "<br/>",
1885 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
1886 $text = $this->strip( $text, $stripState, false );
1887 $text = $this->pstPass2( $text, $user );
1888 $text = $this->unstrip( $text, $stripState );
1892 /* private */ function pstPass2( $text, &$user )
1894 global $wgLang, $wgLocaltimezone, $wgCurParser;
1896 # Variable replacement
1897 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1898 $text = $this->replaceVariables( $text );
1902 $n = $user->getName();
1903 $k = $user->getOption( "nickname" );
1904 if ( "" == $k ) { $k = $n; }
1905 if(isset($wgLocaltimezone)) {
1906 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1908 /* Note: this is an ugly timezone hack for the European wikis */
1909 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1910 " (" . date( "T" ) . ")";
1911 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1913 $text = preg_replace( "/~~~~~/", $d, $text );
1914 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1915 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1916 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1917 Namespace::getUser() ) . ":$n|$k]]", $text );
1919 # Context links: [[|name]] and [[name (context)|]]
1921 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1922 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1923 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1924 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1926 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1927 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1928 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1929 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1930 # [[ns:page (cont)|]]
1932 $t = $this->mTitle
->getText();
1933 if ( preg_match( $conpat, $t, $m ) ) {
1936 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1937 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1938 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1940 if ( "" == $context ) {
1941 $text = preg_replace( $p2, "[[\\1]]", $text );
1943 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1947 $mw =& MagicWord::get( MAG_SUBST );
1948 $wgCurParser = $this->fork();
1949 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1950 $this->merge( $wgCurParser );
1953 # Trim trailing whitespace
1954 # MAG_END (__END__) tag allows for trailing
1955 # whitespace to be deliberately included
1956 $text = rtrim( $text );
1957 $mw =& MagicWord
::get( MAG_END
);
1958 $mw->matchAndRemove( $text );
1963 # Set up some variables which are usually set up in parse()
1964 # so that an external function can call some class members with confidence
1965 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1967 $this->mTitle
=& $title;
1968 $this->mOptions
= $options;
1969 $this->mOutputType
= $outputType;
1970 if ( $clearState ) {
1971 $this->clearState();
1975 function transformMsg( $text, $options ) {
1977 static $executing = false;
1979 # Guard against infinite recursion
1985 $this->mTitle
= $wgTitle;
1986 $this->mOptions
= $options;
1987 $this->mOutputType
= OT_MSG
;
1988 $this->clearState();
1989 $text = $this->replaceVariables( $text );
1998 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2000 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2001 $containsOldMagic = false )
2003 $this->mText
= $text;
2004 $this->mLanguageLinks
= $languageLinks;
2005 $this->mCategoryLinks
= $categoryLinks;
2006 $this->mContainsOldMagic
= $containsOldMagic;
2009 function getText() { return $this->mText
; }
2010 function getLanguageLinks() { return $this->mLanguageLinks
; }
2011 function getCategoryLinks() { return $this->mCategoryLinks
; }
2012 function containsOldMagic() { return $this->mContainsOldMagic
; }
2013 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2014 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2015 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2016 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2018 function merge( $other ) {
2019 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2020 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2021 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2028 # All variables are private
2029 var $mUseTeX; # Use texvc to expand <math> tags
2030 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2031 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2032 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2033 var $mAllowExternalImages; # Allow external images inline
2034 var $mSkin; # Reference to the preferred skin
2035 var $mDateFormat; # Date format index
2036 var $mEditSection; # Create "edit section" links
2037 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2038 var $mNumberHeadings; # Automatically number headings
2039 var $mShowToc; # Show table of contents
2041 function getUseTeX() { return $this->mUseTeX
; }
2042 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2043 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2044 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2045 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2046 function getSkin() { return $this->mSkin
; }
2047 function getDateFormat() { return $this->mDateFormat
; }
2048 function getEditSection() { return $this->mEditSection
; }
2049 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2050 function getNumberHeadings() { return $this->mNumberHeadings
; }
2051 function getShowToc() { return $this->mShowToc
; }
2053 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2054 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2055 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2056 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2057 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2058 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
2059 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2060 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2061 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2062 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2063 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2065 /* static */ function newFromUser( &$user )
2067 $popts = new ParserOptions
;
2068 $popts->initialiseFromUser( $user );
2072 function initialiseFromUser( &$userInput )
2074 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2076 if ( !$userInput ) {
2078 $user->setLoaded( true );
2080 $user =& $userInput;
2083 $this->mUseTeX
= $wgUseTeX;
2084 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2085 $this->mUseDynamicDates
= $wgUseDynamicDates;
2086 $this->mInterwikiMagic
= $wgInterwikiMagic;
2087 $this->mAllowExternalImages
= $wgAllowExternalImages;
2088 $this->mSkin
=& $user->getSkin();
2089 $this->mDateFormat
= $user->getOption( "date" );
2090 $this->mEditSection
= $user->getOption( "editsection" );
2091 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
2092 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
2093 $this->mShowToc
= $user->getOption( "showtoc" );
2099 # Regex callbacks, used in Parser::replaceVariables
2100 function wfBraceSubstitution( $matches )
2102 global $wgCurParser;
2103 return $wgCurParser->braceSubstitution( $matches );