3 include_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 include_once('wikihiero.php');
8 if( $GLOBALS['wgUseTimeline'] ){
9 include_once('extensions/timeline/Timeline.php');
14 # Processes wiki markup
16 # There are two main entry points into the Parser class: parse() and preSaveTransform().
17 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
20 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
22 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
24 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
25 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
28 # * only within ParserOptions
31 #----------------------------------------
32 # Variable substitution O(N^2) attack
33 #-----------------------------------------
34 # Without countermeasures, it would be possible to attack the parser by saving a page
35 # filled with a large number of inclusions of large pages. The size of the generated
36 # page would be proportional to the square of the input size. Hence, we limit the number
37 # of inclusions of any given page, thus bringing any attack back to O(N).
40 define( "MAX_INCLUDE_REPEAT", 5 );
42 # Allowed values for $mOutputType
43 define( "OT_HTML", 1 );
44 define( "OT_WIKI", 2 );
45 define( "OT_MSG", 3 );
47 # string parameter for extractTags which will cause it
48 # to strip HTML comments in addition to regular
49 # <XML>-style tags. This should not be anything we
50 # may want to use in wikisyntax
51 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
53 # prefix for escaping, used in two functions at least
54 define( "UNIQ_PREFIX", "NaodW29");
58 # Cleared with clearState():
59 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
60 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
63 var $mOptions, $mTitle, $mOutputType;
72 $this->mOutput
= new ParserOutput
;
73 $this->mAutonumber
= 0;
74 $this->mLastSection
= "";
75 $this->mDTopen
= false;
76 $this->mVariables
= false;
77 $this->mIncludeCount
= array();
78 $this->mStripState
= array();
79 $this->mArgStack
= array();
82 # First pass--just handle <nowiki> sections, pass the rest off
83 # to internalParse() which does all the real work.
85 # Returns a ParserOutput
87 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
90 $fname = "Parser::parse";
91 wfProfileIn( $fname );
97 $this->mOptions
= $options;
98 $this->mTitle
=& $title;
99 $this->mOutputType
= OT_HTML
;
102 $text = $this->strip( $text, $this->mStripState
);
103 $text = $this->internalParse( $text, $linestart );
104 $text = $this->unstrip( $text, $this->mStripState
);
105 # Clean up special characters, only run once, next-to-last before doBlockLevels
108 "/<hr *>/i" => '<hr/>',
109 "/<br *>/i" => '<br/>',
110 "/<center *>/i"=>'<div class="center">',
111 "/<\\/center *>/i" => '</div>',
112 # Clean up spare ampersands; note that we probably ought to be
113 # more careful about named entities.
114 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
116 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
119 "/<center *>/i"=>'<div class="center">',
120 "/<\\/center *>/i" => '</div>'
122 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
125 $text = $this->doBlockLevels( $text, $linestart );
127 $text = $this->tidy($text);
129 $this->mOutput
->setText( $text );
130 wfProfileOut( $fname );
131 return $this->mOutput
;
134 /* static */ function getRandomString()
136 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
139 # Replaces all occurrences of <$tag>content</$tag> in the text
140 # with a random marker and returns the new text. the output parameter
141 # $content will be an associative array filled with data on the form
142 # $unique_marker => content.
144 # If $content is already set, the additional entries will be appended
146 # If $tag is set to STRIP_COMMENTS, the function will extract
147 # <!-- HTML comments -->
149 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
150 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
157 while ( "" != $text ) {
158 if($tag==STRIP_COMMENTS
) {
159 $p = preg_split( "/<!--/i", $text, 2 );
161 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
164 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
167 if($tag==STRIP_COMMENTS
) {
168 $q = preg_split( "/-->/i", $p[1], 2 );
170 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
172 $marker = $rnd . sprintf("%08X", $n++
);
173 $content[$marker] = $q[0];
174 $stripped .= $marker;
181 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
182 # If $render is set, performs necessary rendering operations on plugins
183 # Returns the text, and fills an array with data needed in unstrip()
184 # If the $state is already a valid strip state, it adds to the state
186 # When $stripcomments is set, HTML comments <!-- like this -->
187 # will be stripped in addition to other tags. This is important
188 # for section editing, where these comments cause confusion when
189 # counting the sections in the wikisource
190 function strip( $text, &$state, $stripcomments = false )
192 $render = ($this->mOutputType
== OT_HTML
);
193 $nowiki_content = array();
194 $hiero_content = array();
195 $math_content = array();
196 $pre_content = array();
197 $comment_content = array();
199 # Replace any instances of the placeholders
200 $uniq_prefix = UNIQ_PREFIX
;
201 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
203 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
204 foreach( $nowiki_content as $marker => $content ){
206 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
208 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
212 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
213 foreach( $hiero_content as $marker => $content ){
214 if( $render && $GLOBALS['wgUseWikiHiero']){
215 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
217 $hiero_content[$marker] = "<hiero>$content</hiero>";
221 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
222 foreach( $math_content as $marker => $content ){
223 if( $render && $this->mOptions
->getUseTeX() ){
224 $math_content[$marker] = renderMath( $content );
226 $math_content[$marker] = "<math>$content</math>";
230 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
231 foreach( $pre_content as $marker => $content ){
233 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
235 $pre_content[$marker] = "<pre>$content</pre>";
239 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
240 foreach( $comment_content as $marker => $content ){
241 $comment_content[$marker] = "<!--$content-->";
245 # Merge state with the pre-existing state, if there is one
247 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
248 $state['hiero'] = $state['hiero'] +
$hiero_content;
249 $state['math'] = $state['math'] +
$math_content;
250 $state['pre'] = $state['pre'] +
$pre_content;
251 $state['comment'] = $state['comment'] +
$comment_content;
254 'nowiki' => $nowiki_content,
255 'hiero' => $hiero_content,
256 'math' => $math_content,
257 'pre' => $pre_content,
258 'comment' => $comment_content
264 function unstrip( $text, &$state )
266 # Must expand in reverse order, otherwise nested tags will be corrupted
267 $contentDict = end( $state );
268 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
269 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
270 $text = str_replace( key( $contentDict ), $content, $text );
277 # Add an item to the strip state
278 # Returns the unique tag which must be inserted into the stripped text
279 # The tag will be replaced with the original text in unstrip()
281 function insertStripItem( $text, &$state )
283 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
292 $state['item'][$rnd] = $text;
296 # This method generates the list of subcategories and pages for a category
297 function categoryMagic ()
299 global $wgLang , $wgUser ;
300 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
302 $cns = Namespace::getCategory() ;
303 if ( $this->mTitle
->getNamespace() != $cns ) return "" ; # This ain't a category page
305 $r = "<br style=\"clear:both;\"/>\n";
308 $sk =& $wgUser->getSkin() ;
310 $articles = array() ;
311 $children = array() ;
313 $id = $this->mTitle
->getArticleID() ;
315 # For existing categories
317 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
318 $res = wfQuery ( $sql, DB_READ
) ;
319 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
321 # For non-existing categories
322 $t = wfStrencode( $this->mTitle
->getPrefixedDBKey() );
323 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to='$t' AND bl_from=cur_id" ;
324 $res = wfQuery ( $sql, DB_READ
) ;
325 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
328 # For all pages that link to this category
329 foreach ( $data AS $x )
331 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
332 if ( $t != "" ) $t .= ":" ;
333 $t .= $x->cur_title
;
335 if ( $x->cur_namespace
== $cns ) {
336 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
338 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
341 wfFreeResult ( $res ) ;
343 # Showing subcategories
344 if ( count ( $children ) > 0 )
346 asort ( $children ) ;
347 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
348 $r .= implode ( ", " , $children ) ;
351 # Showing pages in this category
352 if ( count ( $articles ) > 0 )
354 $ti = $this->mTitle
->getText() ;
355 asort ( $articles ) ;
356 $h = wfMsg( "category_header", $ti );
357 $r .= "<h2>{$h}</h2>\n" ;
358 $r .= implode ( ", " , $articles ) ;
365 function getHTMLattrs ()
367 $htmlattrs = array( # Allowed attributes--no scripting, etc.
368 "title", "align", "lang", "dir", "width", "height",
369 "bgcolor", "clear", /* BR */ "noshade", /* HR */
370 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
371 /* FONT */ "type", "start", "value", "compact",
372 /* For various lists, mostly deprecated but safe */
373 "summary", "width", "border", "frame", "rules",
374 "cellspacing", "cellpadding", "valign", "char",
375 "charoff", "colgroup", "col", "span", "abbr", "axis",
376 "headers", "scope", "rowspan", "colspan", /* Tables */
377 "id", "class", "name", "style" /* For CSS */
382 function fixTagAttributes ( $t )
384 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
385 $htmlattrs = $this->getHTMLattrs() ;
387 # Strip non-approved attributes from the tag
389 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
390 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
392 # Strip javascript "expression" from stylesheets. Brute force approach:
393 # If anythin offensive is found, all attributes of the HTML tag are dropped
396 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
397 wfMungeToUtf8( $t ) ) )
405 /* interface with html tidy, used if $wgUseTidy = true */
406 function tidy ( $text ) {
407 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
409 $text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
410 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
411 '<head><title>test</title></head><body>'.$text.'</body></html>';
412 $descriptorspec = array(
413 0 => array("pipe", "r"),
414 1 => array("pipe", "w"),
415 2 => array("pipe", "w")
417 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
418 if (is_resource($process)) {
419 fwrite($pipes[0], $text);
421 while (!feof($pipes[1])) {
422 $cleansource .= fgets($pipes[1], 1024);
425 $return_value = proc_close($process);
426 if($return_value == 2) {
428 while (!feof($pipes[2])) {
429 $errors .= fgets($pipes[2], 1024);
434 if( $cleansource == '' and !empty($errors)) {
435 return '<pre>'.htmlspecialchars($errors).'</pre>';
441 function doTableStuff ( $t )
443 $t = explode ( "\n" , $t ) ;
444 $td = array () ; # Is currently a td tag open?
445 $ltd = array () ; # Was it TD or TH?
446 $tr = array () ; # Is currently a tr tag open?
447 $ltr = array () ; # tr attributes
448 foreach ( $t AS $k => $x )
451 $fc = substr ( $x , 0 , 1 ) ;
452 if ( "{|" == substr ( $x , 0 , 2 ) )
454 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
455 array_push ( $td , false ) ;
456 array_push ( $ltd , "" ) ;
457 array_push ( $tr , false ) ;
458 array_push ( $ltr , "" ) ;
460 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
461 else if ( "|}" == substr ( $x , 0 , 2 ) )
464 $l = array_pop ( $ltd ) ;
465 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
466 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
470 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
472 $z = trim ( substr ( $x , 2 ) ) ;
473 $t[$k] = "<caption>{$z}</caption>\n" ;
475 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
477 $x = substr ( $x , 1 ) ;
478 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
480 $l = array_pop ( $ltd ) ;
481 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
482 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
485 array_push ( $tr , false ) ;
486 array_push ( $td , false ) ;
487 array_push ( $ltd , "" ) ;
488 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
490 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
492 if ( "|+" == substr ( $x , 0 , 2 ) )
495 $x = substr ( $x , 1 ) ;
497 $after = substr ( $x , 1 ) ;
498 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
499 $after = explode ( "||" , $after ) ;
501 foreach ( $after AS $theline )
506 $tra = array_pop ( $ltr ) ;
507 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
508 array_push ( $tr , true ) ;
509 array_push ( $ltr , "" ) ;
512 $l = array_pop ( $ltd ) ;
513 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
514 if ( $fc == "|" ) $l = "td" ;
515 else if ( $fc == "!" ) $l = "th" ;
516 else if ( $fc == "+" ) $l = "caption" ;
518 array_push ( $ltd , $l ) ;
519 $y = explode ( "|" , $theline , 2 ) ;
520 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
521 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
523 array_push ( $td , true ) ;
528 # Closing open td, tr && table
529 while ( count ( $td ) > 0 )
531 if ( array_pop ( $td ) ) $t[] = "</td>" ;
532 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
536 $t = implode ( "\n" , $t ) ;
537 # $t = $this->removeHTMLtags( $t );
541 function internalParse( $text, $linestart, $args = array() )
543 $fname = "Parser::internalParse";
544 wfProfileIn( $fname );
546 $text = $this->removeHTMLtags( $text );
547 $text = $this->replaceVariables( $text, $args );
549 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
551 $text = $this->doHeadings( $text );
552 if($this->mOptions
->getUseDynamicDates()) {
553 global $wgDateFormatter;
554 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
556 $text = $this->replaceExternalLinks( $text );
557 $text = $this->doTokenizedParser ( $text );
558 $text = $this->doTableStuff ( $text ) ;
559 $text = $this->formatHeadings( $text );
560 $sk =& $this->mOptions
->getSkin();
561 $text = $sk->transformContent( $text );
563 if ( !isset ( $this->categoryMagicDone
) ) {
564 $text .= $this->categoryMagic () ;
565 $this->categoryMagicDone
= true ;
568 wfProfileOut( $fname );
573 /* private */ function doHeadings( $text )
575 for ( $i = 6; $i >= 1; --$i ) {
576 $h = substr( "======", 0, $i );
577 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
578 "<h{$i}>\\1</h{$i}>\\2", $text );
583 # Note: we have to do external links before the internal ones,
584 # and otherwise take great care in the order of things here, so
585 # that we don't end up interpreting some URLs twice.
587 /* private */ function replaceExternalLinks( $text )
589 $fname = "Parser::replaceExternalLinks";
590 wfProfileIn( $fname );
591 $text = $this->subReplaceExternalLinks( $text, "http", true );
592 $text = $this->subReplaceExternalLinks( $text, "https", true );
593 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
594 $text = $this->subReplaceExternalLinks( $text, "irc", false );
595 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
596 $text = $this->subReplaceExternalLinks( $text, "news", false );
597 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
598 wfProfileOut( $fname );
602 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
604 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
605 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
607 # this is the list of separators that should be ignored if they
608 # are the last character of an URL but that should be included
609 # if they occur within the URL, e.g. "go to www.foo.com, where .."
610 # in this case, the last comma should not become part of the URL,
611 # but in "www.foo.com/123,2342,32.htm" it should.
613 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
614 $images = "gif|png|jpg|jpeg";
616 # PLEASE NOTE: The curly braces { } are not part of the regex,
617 # they are interpreted as part of the string (used to tell PHP
618 # that the content of the string should be inserted there).
619 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
620 "((?i){$images})([^{$uc}]|$)/";
622 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
623 $sk =& $this->mOptions
->getSkin();
625 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
626 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
627 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
629 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
630 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
631 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
633 $s = str_replace( $unique, $protocol, $s );
635 $a = explode( "[{$protocol}:", " " . $s );
636 $s = array_shift( $a );
637 $s = substr( $s, 1 );
639 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
640 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
642 foreach ( $a as $line ) {
643 if ( preg_match( $e1, $line, $m ) ) {
644 $link = "{$protocol}:{$m[1]}";
646 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
647 else { $text = wfEscapeHTML( $link ); }
648 } else if ( preg_match( $e2, $line, $m ) ) {
649 $link = "{$protocol}:{$m[1]}";
653 $s .= "[{$protocol}:" . $line;
656 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
659 # Expand the URL for printable version
660 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
662 $la = $sk->getExternalLinkAttributes( $link, $text );
663 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
669 /* private */ function handle3Quotes( &$state, $token )
671 if ( $state["strong"] !== false ) {
672 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
674 # ''' lala ''lala '''
675 $s = "</em></strong><em>";
679 $state["strong"] = FALSE;
682 $state["strong"] = isset($token["pos"]) ?
$token["pos"] : true;
687 /* private */ function handle2Quotes( &$state, $token )
689 if ( $state["em"] !== false ) {
690 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
692 # ''lala'''lala'' ....'''
693 $s = "</strong></em><strong>";
697 $state["em"] = FALSE;
700 $state["em"] = isset($token["pos"]) ?
$token["pos"] : true;
706 /* private */ function handle5Quotes( &$state, $token )
709 if ( $state["em"] !== false && $state["strong"] !== false ) {
710 if ( $state["em"] < $state["strong"] ) {
711 $s .= "</strong></em>";
713 $s .= "</em></strong>";
715 $state["strong"] = $state["em"] = FALSE;
716 } elseif ( $state["em"] !== false ) {
717 $s .= "</em><strong>";
718 $state["em"] = FALSE;
719 $state["strong"] = $token["pos"];
720 } elseif ( $state["strong"] !== false ) {
721 $s .= "</strong><em>";
722 $state["strong"] = FALSE;
723 $state["em"] = $token["pos"];
724 } else { # not $em and not $strong
725 $s .= "<strong><em>";
726 $state["strong"] = $state["em"] = isset($token["pos"]) ?
$token["pos"] : true;
731 /* private */ function doTokenizedParser( $str )
733 global $wgLang; # for language specific parser hook
734 global $wgUploadDirectory, $wgUseTimeline;
736 $tokenizer=Tokenizer
::newFromString( $str );
737 $tokenStack = array();
740 $state["em"] = FALSE;
741 $state["strong"] = FALSE;
745 # The tokenizer splits the text into tokens and returns them one by one.
746 # Every call to the tokenizer returns a new token.
747 while ( $token = $tokenizer->nextToken() )
749 switch ( $token["type"] )
752 # simple text with no further markup
753 $txt = $token["text"];
756 # Text that contains blanks that have to be converted to
757 # non-breakable spaces for French.
758 # U+202F NARROW NO-BREAK SPACE might be a better choice, but
759 # browser support for Unicode spacing is poor.
760 $txt = str_replace( " ", " ", $token["text"] );
763 # remember the tag opened with 3 [
767 # FIXME : Treat orphaned open tags (stack not empty when text is over)
769 array_push( $tokenStack, $token );
776 # get text from stack, glue it together, and call the code to handle a
779 if ( count( $tokenStack ) == 0 )
781 # stack empty. Found a ]] without an opening [[
785 $lastToken = array_pop( $tokenStack );
786 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
788 if( !empty( $lastToken["text"] ) ) {
789 $linkText = $lastToken["text"] . $linkText;
791 $lastToken = array_pop( $tokenStack );
794 $txt = $linkText ."]]";
796 if( isset( $lastToken["text"] ) ) {
797 $prefix = $lastToken["text"];
801 $nextToken = $tokenizer->previewToken();
802 if ( $nextToken["type"] == "text" )
804 # Preview just looks at it. Now we have to fetch it.
805 $nextToken = $tokenizer->nextToken();
806 $txt .= $nextToken["text"];
808 $txt = $this->handleInternalLink( $this->unstrip($txt,$this->mStripState
), $prefix );
810 # did the tag start with 3 [ ?
812 # show the first as text
818 $tagIsOpen = (count( $tokenStack ) != 0);
824 # This and the three next ones handle quotes
825 $txt = $this->handle3Quotes( $state, $token );
828 $txt = $this->handle2Quotes( $state, $token );
831 $txt = $this->handle5Quotes( $state, $token );
841 $txt = $this->doMagicRFC( $tokenizer );
848 $txt = $this->doMagicISBN( $tokenizer );
852 if ( $wgUseTimeline &&
853 "" != ( $timelinesrc = $tokenizer->readAllUntil("</timeline>") ) )
855 $txt = renderTimeline( $timelinesrc );
861 # Call language specific Hook.
862 $txt = $wgLang->processToken( $token, $tokenStack );
863 if ( NULL == $txt ) {
864 # An unkown token. Highlight.
865 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
866 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
870 # If we're parsing the interior of a link, don't append the interior to $s,
871 # but push it to the stack so it can be processed when a ]] token is found.
872 if ( $tagIsOpen && $txt != "" ) {
873 $token["type"] = "text";
874 $token["text"] = $txt;
875 array_push( $tokenStack, $token );
880 if ( count( $tokenStack ) != 0 )
882 # still objects on stack. opened [[ tag without closing ]] tag.
884 while ( $lastToken = array_pop( $tokenStack ) )
886 if ( $lastToken["type"] == "text" )
888 $txt = $lastToken["text"] . $txt;
890 $txt = $lastToken["type"] . $txt;
898 /* private */ function handleInternalLink( $line, $prefix )
900 global $wgLang, $wgLinkCache;
901 global $wgNamespacesWithSubpages, $wgLanguageCode;
902 static $fname = "Parser::handleInternalLink" ;
903 wfProfileIn( $fname );
905 wfProfileIn( "$fname-setup" );
907 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
908 $sk =& $this->mOptions
->getSkin();
910 # Match a link having the form [[namespace:link|alternate]]trail
912 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
913 # Match the end of a line for a word that's not followed by whitespace,
914 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
915 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
916 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
917 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
920 # Special and Media are pseudo-namespaces; no pages actually exist in them
921 static $image = FALSE;
922 static $special = FALSE;
923 static $media = FALSE;
924 static $category = FALSE;
925 if ( !$image ) { $image = Namespace::getImage(); }
926 if ( !$special ) { $special = Namespace::getSpecial(); }
927 if ( !$media ) { $media = Namespace::getMedia(); }
928 if ( !$category ) { $category = Namespace::getCategory(); }
930 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
932 wfProfileOut( "$fname-setup" );
935 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
938 } else { # Invalid form; output directly
939 $s .= $prefix . "[[" . $line ;
945 :Foobar -- override special treatment of prefix (images, language links)
946 /Foobar -- convert to CurrentPage/Foobar
947 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
949 $c = substr($m[1],0,1);
950 $noforce = ($c != ":");
951 if( $c == "/" ) { # subpage
952 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
953 $m[1]=substr($m[1],1,strlen($m[1])-2);
956 $noslash=substr($m[1],1);
958 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
959 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
962 } # this might be changed for ugliness reasons
964 $link = $noslash; # no subpage allowed, use standard link
966 } elseif( $noforce ) { # no subpage
969 $link = substr( $m[1], 1 );
974 $nt = Title
::newFromText( $link );
976 $s .= $prefix . "[[" . $line;
979 $ns = $nt->getNamespace();
980 $iw = $nt->getInterWiki();
982 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
983 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
984 $s .= $prefix . $trail ;
985 return (trim($s) == '')?
'': $s;
987 if( $ns == $image ) {
988 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
989 $wgLinkCache->addImageLinkObj( $nt );
992 if ( $ns == $category ) {
993 $t = $nt->getText() ;
994 $nnt = Title
::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
995 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
996 $this->mOutput
->mCategoryLinks
[] = $t ;
997 $s .= $prefix . $trail ;
1001 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1002 ( strpos( $link, "#" ) == FALSE ) ) {
1003 # Self-links are handled specially; generally de-link and change to bold.
1004 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, "", $trail );
1008 if( $ns == $media ) {
1009 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1010 $wgLinkCache->addImageLinkObj( $nt );
1012 } elseif( $ns == $special ) {
1013 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
1016 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
1018 wfProfileOut( $fname );
1022 # Some functions here used by doBlockLevels()
1024 /* private */ function closeParagraph()
1027 if ( '' != $this->mLastSection
) {
1028 $result = "</" . $this->mLastSection
. ">\n";
1030 $this->mInPre
= false;
1031 $this->mLastSection
= "";
1034 # getCommon() returns the length of the longest common substring
1035 # of both arguments, starting at the beginning of both.
1037 /* private */ function getCommon( $st1, $st2 )
1039 $fl = strlen( $st1 );
1040 $shorter = strlen( $st2 );
1041 if ( $fl < $shorter ) { $shorter = $fl; }
1043 for ( $i = 0; $i < $shorter; ++
$i ) {
1044 if ( $st1{$i} != $st2{$i} ) { break; }
1048 # These next three functions open, continue, and close the list
1049 # element appropriate to the prefix character passed into them.
1051 /* private */ function openList( $char )
1053 $result = $this->closeParagraph();
1055 if ( "*" == $char ) { $result .= "<ul><li>"; }
1056 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1057 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1058 else if ( ";" == $char ) {
1059 $result .= "<dl><dt>";
1060 $this->mDTopen
= true;
1062 else { $result = "<!-- ERR 1 -->"; }
1067 /* private */ function nextItem( $char )
1069 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
1070 else if ( ":" == $char ||
";" == $char ) {
1072 if ( $this->mDTopen
) { $close = "</dt>"; }
1073 if ( ";" == $char ) {
1074 $this->mDTopen
= true;
1075 return $close . "<dt>";
1077 $this->mDTopen
= false;
1078 return $close . "<dd>";
1081 return "<!-- ERR 2 -->";
1084 /* private */function closeList( $char )
1086 if ( "*" == $char ) { $text = "</li></ul>"; }
1087 else if ( "#" == $char ) { $text = "</li></ol>"; }
1088 else if ( ":" == $char ) {
1089 if ( $this->mDTopen
) {
1090 $this->mDTopen
= false;
1091 $text = "</dt></dl>";
1093 $text = "</dd></dl>";
1096 else { return "<!-- ERR 3 -->"; }
1100 /* private */ function doBlockLevels( $text, $linestart ) {
1101 $fname = "Parser::doBlockLevels";
1102 wfProfileIn( $fname );
1104 # Parsing through the text line by line. The main thing
1105 # happening here is handling of block-level elements p, pre,
1106 # and making lists from lines starting with * # : etc.
1108 $textLines = explode( "\n", $text );
1110 $lastPrefix = $output = $lastLine = '';
1111 $this->mDTopen
= $inBlockElem = false;
1113 $paragraphStack = false;
1115 if ( !$linestart ) {
1116 $output .= array_shift( $textLines );
1118 foreach ( $textLines as $oLine ) {
1119 $lastPrefixLength = strlen( $lastPrefix );
1120 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1121 $preOpenMatch = preg_match("/<pre/i", $oLine );
1122 if (!$this->mInPre
) {
1123 $this->mInPre
= !empty($preOpenMatch);
1125 if ( !$this->mInPre
) {
1126 # Multiple prefixes may abut each other for nested lists.
1127 $prefixLength = strspn( $oLine, "*#:;" );
1128 $pref = substr( $oLine, 0, $prefixLength );
1131 $pref2 = str_replace( ";", ":", $pref );
1132 $t = substr( $oLine, $prefixLength );
1134 # Don't interpret any other prefixes in preformatted text
1136 $pref = $pref2 = '';
1141 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1142 # Same as the last item, so no need to deal with nesting or opening stuff
1143 $output .= $this->nextItem( substr( $pref, -1 ) );
1144 $paragraphStack = false;
1146 if ( ";" == substr( $pref, -1 ) ) {
1147 # The one nasty exception: definition lists work like this:
1148 # ; title : definition text
1149 # So we check for : in the remainder text to split up the
1150 # title and definition, without b0rking links.
1151 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1152 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1154 $output .= $term . $this->nextItem( ":" );
1158 } elseif( $prefixLength ||
$lastPrefixLength ) {
1159 # Either open or close a level...
1160 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1161 $paragraphStack = false;
1163 while( $commonPrefixLength < $lastPrefixLength ) {
1164 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1165 --$lastPrefixLength;
1167 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1168 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1170 while ( $prefixLength > $commonPrefixLength ) {
1171 $char = substr( $pref, $commonPrefixLength, 1 );
1172 $output .= $this->openList( $char );
1174 if ( ";" == $char ) {
1175 # FIXME: This is dupe of code above
1176 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1178 $output .= $term . $this->nextItem( ":" );
1182 ++
$commonPrefixLength;
1184 $lastPrefix = $pref2;
1186 if( 0 == $prefixLength ) {
1187 # No prefix (not in list)--go to paragraph mode
1188 $uniq_prefix = UNIQ_PREFIX
;
1189 // XXX: use a stack for nestable elements like span, table and div
1190 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1191 $closematch = preg_match(
1192 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1193 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1194 if ( $openmatch or $closematch ) {
1195 $paragraphStack = false;
1196 $output .= $this->closeParagraph();
1197 if($preOpenMatch and !$preCloseMatch) {
1198 $this->mInPre
= true;
1200 if ( $closematch ) {
1201 $inBlockElem = false;
1203 $inBlockElem = true;
1205 } else if ( !$inBlockElem ) {
1206 if ( " " == $t{0} ) {
1208 if ($this->mLastSection
!= 'pre') {
1209 $paragraphStack = false;
1210 $output .= $this->closeParagraph().'<pre>';
1211 $this->mLastSection
= 'pre';
1215 if ( '' == trim($t) ) {
1216 if ( $paragraphStack ) {
1217 $output .= $paragraphStack.'<br/>';
1218 $paragraphStack = false;
1219 $this->mLastSection
= 'p';
1221 if ($this->mLastSection
!= 'p' ) {
1222 $output .= $this->closeParagraph();
1223 $this->mLastSection
= '';
1224 $paragraphStack = "<p>";
1226 $paragraphStack = '</p><p>';
1230 if ( $paragraphStack ) {
1231 $output .= $paragraphStack;
1232 $paragraphStack = false;
1233 $this->mLastSection
= 'p';
1234 } else if ($this->mLastSection
!= 'p') {
1235 $output .= $this->closeParagraph().'<p>';
1236 $this->mLastSection
= 'p';
1242 if ($paragraphStack === false) {
1246 while ( $prefixLength ) {
1247 $output .= $this->closeList( $pref2{$prefixLength-1} );
1250 if ( "" != $this->mLastSection
) {
1251 $output .= "</" . $this->mLastSection
. ">";
1252 $this->mLastSection
= "";
1255 wfProfileOut( $fname );
1259 function getVariableValue( $index ) {
1260 global $wgLang, $wgSitename, $wgServer;
1263 case MAG_CURRENTMONTH
:
1265 case MAG_CURRENTMONTHNAME
:
1266 return $wgLang->getMonthName( date("n") );
1267 case MAG_CURRENTMONTHNAMEGEN
:
1268 return $wgLang->getMonthNameGen( date("n") );
1269 case MAG_CURRENTDAY
:
1272 return $this->mTitle
->getText();
1274 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1275 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1276 case MAG_CURRENTDAYNAME
:
1277 return $wgLang->getWeekdayName( date("w")+
1 );
1278 case MAG_CURRENTYEAR
:
1280 case MAG_CURRENTTIME
:
1281 return $wgLang->time( wfTimestampNow(), false );
1282 case MAG_NUMBEROFARTICLES
:
1283 return wfNumberOfArticles();
1293 function initialiseVariables()
1295 global $wgVariableIDs;
1296 $this->mVariables
= array();
1297 foreach ( $wgVariableIDs as $id ) {
1298 $mw =& MagicWord
::get( $id );
1299 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1303 /* private */ function replaceVariables( $text, $args = array() )
1305 global $wgLang, $wgScript, $wgArticlePath;
1307 $fname = "Parser::replaceVariables";
1308 wfProfileIn( $fname );
1311 if ( !$this->mVariables
) {
1312 $this->initialiseVariables();
1314 $titleChars = Title
::legalChars();
1315 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1317 # This function is called recursively. To keep track of arguments we need a stack:
1318 array_push( $this->mArgStack
, $args );
1320 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1321 $GLOBALS['wgCurParser'] =& $this;
1322 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1324 array_pop( $this->mArgStack
);
1329 function braceSubstitution( $matches )
1331 global $wgLinkCache, $wgLang;
1332 $fname = "Parser::braceSubstitution";
1337 # $newline is an optional newline character before the braces
1338 # $part1 is the bit before the first |, and must contain only title characters
1339 # $args is a list of arguments, starting from index 0, not including $part1
1341 $newline = $matches[1];
1342 $part1 = $matches[2];
1343 # If the third subpattern matched anything, it will start with |
1344 if ( $matches[3] !== "" ) {
1345 $args = explode( "|", substr( $matches[3], 1 ) );
1349 $argc = count( $args );
1352 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1353 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1354 if ( $this->mOutputType
!= OT_WIKI
) {
1355 # Invalid SUBST not replaced at PST time
1356 # Return without further processing
1357 $text = $matches[0];
1360 } elseif ( $this->mOutputType
== OT_WIKI
) {
1361 # SUBST not found in PST pass, do nothing
1362 $text = $matches[0];
1366 # MSG, MSGNW and INT
1369 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1370 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1373 # Remove obsolete MSG:
1374 $mwMsg =& MagicWord
::get( MAG_MSG
);
1375 $mwMsg->matchStartAndRemove( $part1 );
1378 # Check if it is an internal message
1379 $mwInt =& MagicWord
::get( MAG_INT
);
1380 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1381 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1382 $text = wfMsgReal( $part1, $args, true );
1390 # Check for NS: (namespace expansion)
1391 $mwNs = MagicWord
::get( MAG_NS
);
1392 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1393 if ( intval( $part1 ) ) {
1394 $text = $wgLang->getNsText( intval( $part1 ) );
1397 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1398 if ( !is_null( $index ) ) {
1399 $text = $wgLang->getNsText( $index );
1406 # LOCALURL and LOCALURLE
1408 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1409 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1411 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1412 $func = 'getLocalURL';
1413 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1414 $func = 'escapeLocalURL';
1419 if ( $func !== '' ) {
1420 $title = Title
::newFromText( $part1 );
1421 if ( !is_null( $title ) ) {
1423 $text = $title->$func( $args[0] );
1425 $text = $title->$func();
1432 # Internal variables
1433 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1434 $text = $this->mVariables
[$part1];
1436 $this->mOutput
->mContainsOldMagic
= true;
1439 # Arguments input from the caller
1440 $inputArgs = end( $this->mArgStack
);
1441 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1442 $text = $inputArgs[$part1];
1446 # Load from database
1448 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1449 if ( !is_null( $title ) && !$title->isExternal() ) {
1450 # Check for excessive inclusion
1451 $dbk = $title->getPrefixedDBkey();
1452 if ( $this->incrementIncludeCount( $dbk ) ) {
1453 $article = new Article( $title );
1454 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1455 if ( $articleContent !== false ) {
1457 $text = $articleContent;
1462 # If the title is valid but undisplayable, make a link to it
1463 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1464 $text = "[[" . $title->getPrefixedText() . "]]";
1470 # Recursive parsing, escaping and link table handling
1471 # Only for HTML output
1472 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1473 $text = wfEscapeWikiText( $text );
1474 } elseif ( $this->mOutputType
== OT_HTML
&& $found ) {
1475 # Clean up argument array
1476 $assocArgs = array();
1478 foreach( $args as $arg ) {
1479 $eqpos = strpos( $arg, "=" );
1480 if ( $eqpos === false ) {
1481 $assocArgs[$index++
] = $arg;
1483 $name = trim( substr( $arg, 0, $eqpos ) );
1484 $value = trim( substr( $arg, $eqpos+
1 ) );
1485 if ( $value === false ) {
1488 if ( $name !== false ) {
1489 $assocArgs[$name] = $value;
1494 # Do not enter included links in link table
1495 if ( !is_null( $title ) ) {
1496 $wgLinkCache->suspend();
1499 # Run full parser on the included text
1500 $text = $this->strip( $text, $this->mStripState
);
1501 $text = $this->internalParse( $text, (bool)$newline, $assocArgs );
1503 # Add the result to the strip state for re-inclusion after
1504 # the rest of the processing
1505 $text = $this->insertStripItem( $text, $this->mStripState
);
1507 # Resume the link cache and register the inclusion as a link
1508 if ( !is_null( $title ) ) {
1509 $wgLinkCache->resume();
1510 $wgLinkCache->addLinkObj( $title );
1521 # Returns true if the function is allowed to include this entity
1522 function incrementIncludeCount( $dbk )
1524 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1525 $this->mIncludeCount
[$dbk] = 0;
1527 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1535 # Cleans up HTML, removes dangerous tags and attributes
1536 /* private */ function removeHTMLtags( $text )
1539 $fname = "Parser::removeHTMLtags";
1540 wfProfileIn( $fname );
1541 $htmlpairs = array( # Tags that must be closed
1542 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1543 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1544 "strike", "strong", "tt", "var", "div", "center",
1545 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1546 "ruby", "rt" , "rb" , "rp", "p"
1548 $htmlsingle = array(
1549 "br", "hr", "li", "dt", "dd"
1551 $htmlnest = array( # Tags that can be nested--??
1552 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1553 "dl", "font", "big", "small", "sub", "sup"
1555 $tabletags = array( # Can only appear inside table
1559 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1560 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1562 $htmlattrs = $this->getHTMLattrs () ;
1564 # Remove HTML comments
1565 $text = preg_replace( "/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU", "$2", $text );
1567 $bits = explode( "<", $text );
1568 $text = array_shift( $bits );
1570 $tagstack = array(); $tablestack = array();
1571 foreach ( $bits as $x ) {
1572 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1573 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1575 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1576 error_reporting( $prev );
1579 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1583 if ( ! in_array( $t, $htmlsingle ) &&
1584 ( count($tagstack) && $ot = array_pop( $tagstack ) ) != $t ) {
1585 if(!empty($ot)) array_push( $tagstack, $ot );
1588 if ( $t == "table" ) {
1589 $tagstack = array_pop( $tablestack );
1594 # Keep track for later
1595 if ( in_array( $t, $tabletags ) &&
1596 ! in_array( "table", $tagstack ) ) {
1598 } else if ( in_array( $t, $tagstack ) &&
1599 ! in_array ( $t , $htmlnest ) ) {
1601 } else if ( ! in_array( $t, $htmlsingle ) ) {
1602 if ( $t == "table" ) {
1603 array_push( $tablestack, $tagstack );
1604 $tagstack = array();
1606 array_push( $tagstack, $t );
1608 # Strip non-approved attributes from the tag
1609 $newparams = $this->fixTagAttributes($params);
1613 $rest = str_replace( ">", ">", $rest );
1614 $text .= "<$slash$t $newparams$brace$rest";
1618 $text .= "<" . str_replace( ">", ">", $x);
1620 # Close off any remaining tags
1621 while ( $t = array_pop( $tagstack ) ) {
1623 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1626 # this might be possible using tidy itself
1627 foreach ( $bits as $x ) {
1628 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1630 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1631 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1632 $newparams = $this->fixTagAttributes($params);
1633 $rest = str_replace( ">", ">", $rest );
1634 $text .= "<$slash$t $newparams$brace$rest";
1636 $text .= "<" . str_replace( ">", ">", $x);
1640 wfProfileOut( $fname );
1647 * This function accomplishes several tasks:
1648 * 1) Auto-number headings if that option is enabled
1649 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1650 * 3) Add a Table of contents on the top for users who have enabled the option
1651 * 4) Auto-anchor headings
1653 * It loops through all headlines, collects the necessary data, then splits up the
1654 * string and re-inserts the newly formatted headlines.
1658 /* private */ function formatHeadings( $text )
1660 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1661 $doShowToc = $this->mOptions
->getShowToc();
1662 if( !$this->mTitle
->userCanEdit() ) {
1664 $rightClickHack = 0;
1666 $showEditLink = $this->mOptions
->getEditSection();
1667 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1670 # Inhibit editsection links if requested in the page
1671 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1672 if( $esw->matchAndRemove( $text ) ) {
1675 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1677 $mw =& MagicWord
::get( MAG_NOTOC
);
1678 if( $mw->matchAndRemove( $text ) ) {
1682 # never add the TOC to the Main Page. This is an entry page that should not
1683 # be more than 1-2 screens large anyway
1684 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1688 # Get all headlines for numbering them and adding funky stuff like [edit]
1689 # links - this is for later, but we need the number of headlines right now
1690 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1692 # if there are fewer than 4 headlines in the article, do not show TOC
1693 if( $numMatches < 4 ) {
1697 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1698 # override above conditions and always show TOC
1699 $mw =& MagicWord
::get( MAG_FORCETOC
);
1700 if ($mw->matchAndRemove( $text ) ) {
1705 # We need this to perform operations on the HTML
1706 $sk =& $this->mOptions
->getSkin();
1711 # Ugh .. the TOC should have neat indentation levels which can be
1712 # passed to the skin functions. These are determined here
1717 $sublevelCount = array();
1720 foreach( $matches[3] as $headline ) {
1723 $prevlevel = $level;
1725 $level = $matches[1][$headlineCount];
1726 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1727 # reset when we enter a new level
1728 $sublevelCount[$level] = 0;
1729 $toc .= $sk->tocIndent( $level - $prevlevel );
1730 $toclevel +
= $level - $prevlevel;
1732 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1733 # reset when we step back a level
1734 $sublevelCount[$level+
1]=0;
1735 $toc .= $sk->tocUnindent( $prevlevel - $level );
1736 $toclevel -= $prevlevel - $level;
1738 # count number of headlines for each level
1739 @$sublevelCount[$level]++
;
1740 if( $doNumberHeadings ||
$doShowToc ) {
1742 for( $i = 1; $i <= $level; $i++
) {
1743 if( !empty( $sublevelCount[$i] ) ) {
1747 $numbering .= $sublevelCount[$i];
1753 # The canonized header is a version of the header text safe to use for links
1754 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1755 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1758 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1759 $tocline = trim( $canonized_headline );
1760 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1761 $refer[$headlineCount] = $canonized_headline;
1763 # count how many in assoc. array so we can track dupes in anchors
1764 @$refers[$canonized_headline]++
;
1765 $refcount[$headlineCount]=$refers[$canonized_headline];
1767 # Prepend the number to the heading text
1769 if( $doNumberHeadings ||
$doShowToc ) {
1770 $tocline = $numbering . " " . $tocline;
1772 # Don't number the heading if it is the only one (looks silly)
1773 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1774 # the two are different if the line contains a link
1775 $headline=$numbering . " " . $headline;
1779 # Create the anchor for linking from the TOC to the section
1780 $anchor = $canonized_headline;
1781 if($refcount[$headlineCount] > 1 ) {
1782 $anchor .= "_" . $refcount[$headlineCount];
1785 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1787 if( $showEditLink ) {
1788 if ( empty( $head[$headlineCount] ) ) {
1789 $head[$headlineCount] = "";
1791 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1794 # Add the edit section span
1795 if( $rightClickHack ) {
1796 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1799 # give headline the correct <h#> tag
1800 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1806 $toclines = $headlineCount;
1807 $toc .= $sk->tocUnindent( $toclevel );
1808 $toc = $sk->tocTable( $toc );
1811 # split up and insert constructed headlines
1813 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1816 foreach( $blocks as $block ) {
1817 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1818 # This is the [edit] link that appears for the top block of text when
1819 # section editing is enabled
1821 # Disabled because it broke block formatting
1822 # For example, a bullet point in the top line
1823 # $full .= $sk->editSectionLink(0);
1826 if( $doShowToc && !$i) {
1827 # Top anchor now in skin
1831 if( !empty( $head[$i] ) ) {
1840 /* private */ function doMagicISBN( &$tokenizer )
1844 # Check whether next token is a text token
1845 # If yes, fetch it and convert the text into a
1846 # Special::BookSources link
1847 $token = $tokenizer->previewToken();
1848 while ( $token["type"] == "" )
1850 $tokenizer->nextToken();
1851 $token = $tokenizer->previewToken();
1853 if ( $token["type"] == "text" )
1855 $token = $tokenizer->nextToken();
1856 $x = $token["text"];
1857 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1859 $isbn = $blank = "" ;
1860 while ( " " == $x{0} ) {
1862 $x = substr( $x, 1 );
1864 while ( strstr( $valid, $x{0} ) != false ) {
1866 $x = substr( $x, 1 );
1868 $num = str_replace( "-", "", $isbn );
1869 $num = str_replace( " ", "", $num );
1872 $text = "ISBN $blank$x";
1874 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1875 $text = "<a href=\"" .
1876 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1877 "\" class=\"internal\">ISBN $isbn</a>";
1885 /* private */ function doMagicRFC( &$tokenizer )
1889 # Check whether next token is a text token
1890 # If yes, fetch it and convert the text into a
1891 # link to an RFC source
1892 $token = $tokenizer->previewToken();
1893 while ( $token["type"] == "" )
1895 $tokenizer->nextToken();
1896 $token = $tokenizer->previewToken();
1898 if ( $token["type"] == "text" )
1900 $token = $tokenizer->nextToken();
1901 $x = $token["text"];
1902 $valid = "0123456789";
1904 $rfc = $blank = "" ;
1905 while ( " " == $x{0} ) {
1907 $x = substr( $x, 1 );
1909 while ( strstr( $valid, $x{0} ) != false ) {
1911 $x = substr( $x, 1 );
1915 $text .= "RFC $blank$x";
1917 $url = wfmsg( "rfcurl" );
1918 $url = str_replace( "$1", $rfc, $url);
1919 $sk =& $this->mOptions
->getSkin();
1920 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1921 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1929 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1931 $this->mOptions
= $options;
1932 $this->mTitle
=& $title;
1933 $this->mOutputType
= OT_WIKI
;
1935 if ( $clearState ) {
1936 $this->clearState();
1939 $stripState = false;
1943 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
1946 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
1947 "/<br *?>/i" => "<br/>",
1949 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
1950 $text = $this->strip( $text, $stripState, false );
1951 $text = $this->pstPass2( $text, $user );
1952 $text = $this->unstrip( $text, $stripState );
1956 /* private */ function pstPass2( $text, &$user )
1958 global $wgLang, $wgLocaltimezone, $wgCurParser;
1960 # Variable replacement
1961 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1962 $text = $this->replaceVariables( $text );
1966 $n = $user->getName();
1967 $k = $user->getOption( "nickname" );
1968 if ( "" == $k ) { $k = $n; }
1969 if(isset($wgLocaltimezone)) {
1970 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1972 /* Note: this is an ugly timezone hack for the European wikis */
1973 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1974 " (" . date( "T" ) . ")";
1975 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1977 $text = preg_replace( "/~~~~~/", $d, $text );
1978 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1979 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1980 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1981 Namespace::getUser() ) . ":$n|$k]]", $text );
1983 # Context links: [[|name]] and [[name (context)|]]
1985 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1986 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1987 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1988 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1990 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1991 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1992 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1993 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1994 # [[ns:page (cont)|]]
1996 $t = $this->mTitle
->getText();
1997 if ( preg_match( $conpat, $t, $m ) ) {
2000 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
2001 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
2002 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
2004 if ( "" == $context ) {
2005 $text = preg_replace( $p2, "[[\\1]]", $text );
2007 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2011 $mw =& MagicWord::get( MAG_SUBST );
2012 $wgCurParser = $this->fork();
2013 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2014 $this->merge( $wgCurParser );
2017 # Trim trailing whitespace
2018 # MAG_END (__END__) tag allows for trailing
2019 # whitespace to be deliberately included
2020 $text = rtrim( $text );
2021 $mw =& MagicWord
::get( MAG_END
);
2022 $mw->matchAndRemove( $text );
2027 # Set up some variables which are usually set up in parse()
2028 # so that an external function can call some class members with confidence
2029 function startExternalParse( &$title, $options, $outputType, $clearState = true )
2031 $this->mTitle
=& $title;
2032 $this->mOptions
= $options;
2033 $this->mOutputType
= $outputType;
2034 if ( $clearState ) {
2035 $this->clearState();
2039 function transformMsg( $text, $options ) {
2041 static $executing = false;
2043 # Guard against infinite recursion
2049 $this->mTitle
= $wgTitle;
2050 $this->mOptions
= $options;
2051 $this->mOutputType
= OT_MSG
;
2052 $this->clearState();
2053 $text = $this->replaceVariables( $text );
2062 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2064 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2065 $containsOldMagic = false )
2067 $this->mText
= $text;
2068 $this->mLanguageLinks
= $languageLinks;
2069 $this->mCategoryLinks
= $categoryLinks;
2070 $this->mContainsOldMagic
= $containsOldMagic;
2073 function getText() { return $this->mText
; }
2074 function getLanguageLinks() { return $this->mLanguageLinks
; }
2075 function getCategoryLinks() { return $this->mCategoryLinks
; }
2076 function containsOldMagic() { return $this->mContainsOldMagic
; }
2077 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2078 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2079 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2080 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2082 function merge( $other ) {
2083 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2084 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2085 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2092 # All variables are private
2093 var $mUseTeX; # Use texvc to expand <math> tags
2094 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2095 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2096 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2097 var $mAllowExternalImages; # Allow external images inline
2098 var $mSkin; # Reference to the preferred skin
2099 var $mDateFormat; # Date format index
2100 var $mEditSection; # Create "edit section" links
2101 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2102 var $mNumberHeadings; # Automatically number headings
2103 var $mShowToc; # Show table of contents
2105 function getUseTeX() { return $this->mUseTeX
; }
2106 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2107 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2108 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2109 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2110 function getSkin() { return $this->mSkin
; }
2111 function getDateFormat() { return $this->mDateFormat
; }
2112 function getEditSection() { return $this->mEditSection
; }
2113 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2114 function getNumberHeadings() { return $this->mNumberHeadings
; }
2115 function getShowToc() { return $this->mShowToc
; }
2117 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2118 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2119 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2120 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2121 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2122 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
2123 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2124 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2125 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2126 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2127 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2129 /* static */ function newFromUser( &$user )
2131 $popts = new ParserOptions
;
2132 $popts->initialiseFromUser( $user );
2136 function initialiseFromUser( &$userInput )
2138 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2140 if ( !$userInput ) {
2142 $user->setLoaded( true );
2144 $user =& $userInput;
2147 $this->mUseTeX
= $wgUseTeX;
2148 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2149 $this->mUseDynamicDates
= $wgUseDynamicDates;
2150 $this->mInterwikiMagic
= $wgInterwikiMagic;
2151 $this->mAllowExternalImages
= $wgAllowExternalImages;
2152 $this->mSkin
=& $user->getSkin();
2153 $this->mDateFormat
= $user->getOption( "date" );
2154 $this->mEditSection
= $user->getOption( "editsection" );
2155 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
2156 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
2157 $this->mShowToc
= $user->getOption( "showtoc" );
2163 # Regex callbacks, used in Parser::replaceVariables
2164 function wfBraceSubstitution( $matches )
2166 global $wgCurParser;
2167 return $wgCurParser->braceSubstitution( $matches );