6b8c27502ddb24322ae4761b3d06be58df740d29
3 // require_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 require_once('extensions/wikihiero/wikihiero.php');
8 if( $GLOBALS['wgUseTimeline'] ){
9 require_once('extensions/timeline/Timeline.php');
14 # Processes wiki markup
16 # There are two main entry points into the Parser class: parse() and preSaveTransform().
17 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
20 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
22 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
24 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
25 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
28 # * only within ParserOptions
31 #----------------------------------------
32 # Variable substitution O(N^2) attack
33 #-----------------------------------------
34 # Without countermeasures, it would be possible to attack the parser by saving a page
35 # filled with a large number of inclusions of large pages. The size of the generated
36 # page would be proportional to the square of the input size. Hence, we limit the number
37 # of inclusions of any given page, thus bringing any attack back to O(N).
40 define( "MAX_INCLUDE_REPEAT", 5 );
42 # Allowed values for $mOutputType
43 define( "OT_HTML", 1 );
44 define( "OT_WIKI", 2 );
45 define( "OT_MSG", 3 );
47 # string parameter for extractTags which will cause it
48 # to strip HTML comments in addition to regular
49 # <XML>-style tags. This should not be anything we
50 # may want to use in wikisyntax
51 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
53 # prefix for escaping, used in two functions at least
54 define( "UNIQ_PREFIX", "NaodW29");
58 # Cleared with clearState():
59 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
60 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
63 var $mOptions, $mTitle, $mOutputType;
72 $this->mOutput
= new ParserOutput
;
73 $this->mAutonumber
= 0;
74 $this->mLastSection
= "";
75 $this->mDTopen
= false;
76 $this->mVariables
= false;
77 $this->mIncludeCount
= array();
78 $this->mStripState
= array();
79 $this->mArgStack
= array();
82 # First pass--just handle <nowiki> sections, pass the rest off
83 # to internalParse() which does all the real work.
85 # Returns a ParserOutput
87 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
90 $fname = "Parser::parse";
91 wfProfileIn( $fname );
97 $this->mOptions
= $options;
98 $this->mTitle
=& $title;
99 $this->mOutputType
= OT_HTML
;
102 $text = $this->strip( $text, $this->mStripState
);
103 $text = $this->internalParse( $text, $linestart );
104 $text = $this->unstrip( $text, $this->mStripState
);
105 # Clean up special characters, only run once, next-to-last before doBlockLevels
108 "/<hr *>/i" => '<hr/>',
109 "/<br *>/i" => '<br/>',
110 "/<center *>/i"=>'<div class="center">',
111 "/<\\/center *>/i" => '</div>',
112 # Clean up spare ampersands; note that we probably ought to be
113 # more careful about named entities.
114 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
116 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
119 "/<center *>/i"=>'<div class="center">',
120 "/<\\/center *>/i" => '</div>'
122 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
125 $text = $this->doBlockLevels( $text, $linestart );
127 $text = $this->tidy($text);
129 $this->mOutput
->setText( $text );
130 wfProfileOut( $fname );
131 return $this->mOutput
;
134 /* static */ function getRandomString()
136 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
139 # Replaces all occurrences of <$tag>content</$tag> in the text
140 # with a random marker and returns the new text. the output parameter
141 # $content will be an associative array filled with data on the form
142 # $unique_marker => content.
144 # If $content is already set, the additional entries will be appended
146 # If $tag is set to STRIP_COMMENTS, the function will extract
147 # <!-- HTML comments -->
149 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
150 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
157 while ( "" != $text ) {
158 if($tag==STRIP_COMMENTS
) {
159 $p = preg_split( "/<!--/i", $text, 2 );
161 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
164 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
167 if($tag==STRIP_COMMENTS
) {
168 $q = preg_split( "/-->/i", $p[1], 2 );
170 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
172 $marker = $rnd . sprintf("%08X", $n++
);
173 $content[$marker] = $q[0];
174 $stripped .= $marker;
181 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
182 # If $render is set, performs necessary rendering operations on plugins
183 # Returns the text, and fills an array with data needed in unstrip()
184 # If the $state is already a valid strip state, it adds to the state
186 # When $stripcomments is set, HTML comments <!-- like this -->
187 # will be stripped in addition to other tags. This is important
188 # for section editing, where these comments cause confusion when
189 # counting the sections in the wikisource
190 function strip( $text, &$state, $stripcomments = false )
192 $render = ($this->mOutputType
== OT_HTML
);
193 $nowiki_content = array();
194 $hiero_content = array();
195 $math_content = array();
196 $pre_content = array();
197 $comment_content = array();
199 # Replace any instances of the placeholders
200 $uniq_prefix = UNIQ_PREFIX
;
201 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
203 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
204 foreach( $nowiki_content as $marker => $content ){
206 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
208 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
212 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
213 foreach( $hiero_content as $marker => $content ){
214 if( $render && $GLOBALS['wgUseWikiHiero']){
215 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
217 $hiero_content[$marker] = "<hiero>$content</hiero>";
221 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
222 foreach( $math_content as $marker => $content ){
224 if( $this->mOptions
->getUseTeX() ) {
225 $math_content[$marker] = renderMath( $content );
227 $math_content[$marker] = "<math>$content<math>";
230 $math_content[$marker] = "<math>$content</math>";
234 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
235 foreach( $pre_content as $marker => $content ){
237 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
239 $pre_content[$marker] = "<pre>$content</pre>";
243 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
244 foreach( $comment_content as $marker => $content ){
245 $comment_content[$marker] = "<!--$content-->";
249 # Merge state with the pre-existing state, if there is one
251 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
252 $state['hiero'] = $state['hiero'] +
$hiero_content;
253 $state['math'] = $state['math'] +
$math_content;
254 $state['pre'] = $state['pre'] +
$pre_content;
255 $state['comment'] = $state['comment'] +
$comment_content;
258 'nowiki' => $nowiki_content,
259 'hiero' => $hiero_content,
260 'math' => $math_content,
261 'pre' => $pre_content,
262 'comment' => $comment_content
268 function unstrip( $text, &$state )
270 # Must expand in reverse order, otherwise nested tags will be corrupted
271 $contentDict = end( $state );
272 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
273 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
274 $text = str_replace( key( $contentDict ), $content, $text );
281 # Add an item to the strip state
282 # Returns the unique tag which must be inserted into the stripped text
283 # The tag will be replaced with the original text in unstrip()
285 function insertStripItem( $text, &$state )
287 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
296 $state['item'][$rnd] = $text;
300 # This method generates the list of subcategories and pages for a category
301 function categoryMagic ()
303 global $wgLang , $wgUser ;
304 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
306 $cns = Namespace::getCategory() ;
307 if ( $this->mTitle
->getNamespace() != $cns ) return "" ; # This ain't a category page
309 $r = "<br style=\"clear:both;\"/>\n";
312 $sk =& $wgUser->getSkin() ;
314 $articles = array() ;
315 $children = array() ;
317 $id = $this->mTitle
->getArticleID() ;
320 $t = wfStrencode( $this->mTitle
->getDBKey() );
321 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
322 $res = wfQuery ( $sql, DB_READ
) ;
323 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
325 # For all pages that link to this category
326 foreach ( $data AS $x )
328 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
329 if ( $t != "" ) $t .= ":" ;
330 $t .= $x->cur_title
;
332 if ( $x->cur_namespace
== $cns ) {
333 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
335 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
338 wfFreeResult ( $res ) ;
340 # Showing subcategories
341 if ( count ( $children ) > 0 ) {
342 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
343 $r .= implode ( ", " , $children ) ;
346 # Showing pages in this category
347 if ( count ( $articles ) > 0 ) {
348 $ti = $this->mTitle
->getText() ;
349 $h = wfMsg( "category_header", $ti );
350 $r .= "<h2>{$h}</h2>\n" ;
351 $r .= implode ( ", " , $articles ) ;
358 function getHTMLattrs ()
360 $htmlattrs = array( # Allowed attributes--no scripting, etc.
361 "title", "align", "lang", "dir", "width", "height",
362 "bgcolor", "clear", /* BR */ "noshade", /* HR */
363 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
364 /* FONT */ "type", "start", "value", "compact",
365 /* For various lists, mostly deprecated but safe */
366 "summary", "width", "border", "frame", "rules",
367 "cellspacing", "cellpadding", "valign", "char",
368 "charoff", "colgroup", "col", "span", "abbr", "axis",
369 "headers", "scope", "rowspan", "colspan", /* Tables */
370 "id", "class", "name", "style" /* For CSS */
375 function fixTagAttributes ( $t )
377 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
378 $htmlattrs = $this->getHTMLattrs() ;
380 # Strip non-approved attributes from the tag
382 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
383 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
385 # Strip javascript "expression" from stylesheets. Brute force approach:
386 # If anythin offensive is found, all attributes of the HTML tag are dropped
389 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
390 wfMungeToUtf8( $t ) ) )
398 /* interface with html tidy, used if $wgUseTidy = true */
399 function tidy ( $text ) {
400 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
401 global $wgInputEncoding, $wgOutputEncoding;
403 switch(strtoupper($wgOutputEncoding)) {
405 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
408 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
411 $wgTidyOpts .= ' -raw';
414 $text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
415 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
416 '<head><title>test</title></head><body>'.$text.'</body></html>';
417 $descriptorspec = array(
418 0 => array("pipe", "r"),
419 1 => array("pipe", "w"),
420 2 => array("file", "/dev/null", "a")
422 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
423 if (is_resource($process)) {
424 fwrite($pipes[0], $text);
426 while (!feof($pipes[1])) {
427 $cleansource .= fgets($pipes[1], 1024);
430 $return_value = proc_close($process);
432 if( $cleansource == '' && $text != '') {
433 return '<h2>'.wfMsg('seriousxhtmlerrors').'</h2><pre>'.htmlspecialchars($text).'</pre>';
439 function doTableStuff ( $t )
441 $t = explode ( "\n" , $t ) ;
442 $td = array () ; # Is currently a td tag open?
443 $ltd = array () ; # Was it TD or TH?
444 $tr = array () ; # Is currently a tr tag open?
445 $ltr = array () ; # tr attributes
446 foreach ( $t AS $k => $x )
449 $fc = substr ( $x , 0 , 1 ) ;
450 if ( "{|" == substr ( $x , 0 , 2 ) )
452 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
453 array_push ( $td , false ) ;
454 array_push ( $ltd , "" ) ;
455 array_push ( $tr , false ) ;
456 array_push ( $ltr , "" ) ;
458 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
459 else if ( "|}" == substr ( $x , 0 , 2 ) )
462 $l = array_pop ( $ltd ) ;
463 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
464 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
468 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
470 $z = trim ( substr ( $x , 2 ) ) ;
471 $t[$k] = "<caption>{$z}</caption>\n" ;
473 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
475 $x = substr ( $x , 1 ) ;
476 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
478 $l = array_pop ( $ltd ) ;
479 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
480 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
483 array_push ( $tr , false ) ;
484 array_push ( $td , false ) ;
485 array_push ( $ltd , "" ) ;
486 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
488 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
490 if ( "|+" == substr ( $x , 0 , 2 ) )
493 $x = substr ( $x , 1 ) ;
495 $after = substr ( $x , 1 ) ;
496 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
497 $after = explode ( "||" , $after ) ;
499 foreach ( $after AS $theline )
504 $tra = array_pop ( $ltr ) ;
505 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
506 array_push ( $tr , true ) ;
507 array_push ( $ltr , "" ) ;
510 $l = array_pop ( $ltd ) ;
511 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
512 if ( $fc == "|" ) $l = "td" ;
513 else if ( $fc == "!" ) $l = "th" ;
514 else if ( $fc == "+" ) $l = "caption" ;
516 array_push ( $ltd , $l ) ;
517 $y = explode ( "|" , $theline , 2 ) ;
518 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
519 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
521 array_push ( $td , true ) ;
526 # Closing open td, tr && table
527 while ( count ( $td ) > 0 )
529 if ( array_pop ( $td ) ) $t[] = "</td>" ;
530 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
534 $t = implode ( "\n" , $t ) ;
535 # $t = $this->removeHTMLtags( $t );
539 # Parses the text and adds the result to the strip state
540 # Returns the strip tag
541 function stripParse( $text, $linestart, $args )
543 $text = $this->strip( $text, $this->mStripState
);
544 $text = $this->internalParse( $text, $linestart, $args, false );
546 $text = "\n" . $text;
548 return $this->insertStripItem( $text, $this->mStripState
);
551 function internalParse( $text, $linestart, $args = array(), $isMain=true )
553 $fname = "Parser::internalParse";
554 wfProfileIn( $fname );
556 $text = $this->removeHTMLtags( $text );
557 $text = $this->replaceVariables( $text, $args );
559 $text = preg_replace( "/(^|\n)-----*/", "\\1<hr/>", $text );
561 $text = $this->doHeadings( $text );
562 if($this->mOptions
->getUseDynamicDates()) {
563 global $wgDateFormatter;
564 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
566 $text = $this->doAllQuotes( $text );
567 $text = $this->replaceExternalLinks( $text );
568 $text = $this->replaceInternalLinks ( $text );
569 //$text = $this->doTokenizedParser ( $text );
570 $text = $this->doTableStuff ( $text ) ;
571 $text = $this->magicISBN( $text );
572 $text = $this->magicRFC( $text );
573 $text = $this->formatHeadings( $text, $isMain );
574 $sk =& $this->mOptions
->getSkin();
575 $text = $sk->transformContent( $text );
577 if ( !isset ( $this->categoryMagicDone
) ) {
578 $text .= $this->categoryMagic () ;
579 $this->categoryMagicDone
= true ;
582 wfProfileOut( $fname );
587 /* private */ function doHeadings( $text )
589 for ( $i = 6; $i >= 1; --$i ) {
590 $h = substr( "======", 0, $i );
591 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
592 "<h{$i}>\\1</h{$i}>\\2", $text );
597 /* private */ function doAllQuotes( $text )
600 $lines = explode( "\r\n", $text );
601 foreach ( $lines as $line ) {
602 $outtext .= $this->doQuotes ( "", $line, "" ) . "\r\n";
607 /* private */ function doQuotes( $pre, $text, $mode )
609 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
610 $m1_strong = ($m[1] == "") ?
"" : "<strong>{$m[1]}</strong>";
611 $m1_em = ($m[1] == "") ?
"" : "<em>{$m[1]}</em>";
612 if ( substr ($m[2], 0, 1) == "'" ) {
613 $m[2] = substr ($m[2], 1);
615 return $this->doQuotes ( $m[1], $m[2], ($m[1] == "") ?
"both" : "emstrong" );
616 } else if ($mode == "strong") {
617 return $m1_strong . $this->doQuotes ( "", $m[2], "" );
618 } else if (($mode == "emstrong") ||
($mode == "both")) {
619 return $this->doQuotes ( "", $pre.$m1_strong.$m[2], "em" );
620 } else if ($mode == "strongem") {
621 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( "", $m[2], "em" );
623 return $m[1] . $this->doQuotes ( "", $m[2], "strong" );
626 if ($mode == "strong") {
627 return $this->doQuotes ( $m[1], $m[2], ($m[1] == "") ?
"both" : "strongem" );
628 } else if ($mode == "em") {
629 return $m1_em . $this->doQuotes ( "", $m[2], "" );
630 } else if ($mode == "emstrong") {
631 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( "", $m[2], "strong" );
632 } else if (($mode == "strongem") ||
($mode == "both")) {
633 return $this->doQuotes ( "", $pre.$m1_em.$m[2], "strong" );
635 return $m[1] . $this->doQuotes ( "", $m[2], "em" );
639 $text_strong = ($text == "") ?
"" : "<strong>{$text}</strong>";
640 $text_em = ($text == "") ?
"" : "<em>{$text}</em>";
643 } else if ($mode == "em") {
644 return $pre . $text_em;
645 } else if ($mode == "strong") {
646 return $pre . $text_strong;
647 } else if ($mode == "strongem") {
648 return (($pre == "") && ($text == "")) ?
"" : "<strong>{$pre}{$text_em}</strong>";
650 return (($pre == "") && ($text == "")) ?
"" : "<em>{$pre}{$text_strong}</em>";
655 # Note: we have to do external links before the internal ones,
656 # and otherwise take great care in the order of things here, so
657 # that we don't end up interpreting some URLs twice.
659 /* private */ function replaceExternalLinks( $text )
661 $fname = "Parser::replaceExternalLinks";
662 wfProfileIn( $fname );
663 $text = $this->subReplaceExternalLinks( $text, "http", true );
664 $text = $this->subReplaceExternalLinks( $text, "https", true );
665 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
666 $text = $this->subReplaceExternalLinks( $text, "irc", false );
667 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
668 $text = $this->subReplaceExternalLinks( $text, "news", false );
669 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
670 wfProfileOut( $fname );
674 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
676 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
677 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
679 # this is the list of separators that should be ignored if they
680 # are the last character of an URL but that should be included
681 # if they occur within the URL, e.g. "go to www.foo.com, where .."
682 # in this case, the last comma should not become part of the URL,
683 # but in "www.foo.com/123,2342,32.htm" it should.
685 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
686 $images = "gif|png|jpg|jpeg";
688 # PLEASE NOTE: The curly braces { } are not part of the regex,
689 # they are interpreted as part of the string (used to tell PHP
690 # that the content of the string should be inserted there).
691 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
692 "((?i){$images})([^{$uc}]|$)/";
694 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
695 $sk =& $this->mOptions
->getSkin();
697 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
698 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
699 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
701 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
702 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
703 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
705 $s = str_replace( $unique, $protocol, $s );
707 $a = explode( "[{$protocol}:", " " . $s );
708 $s = array_shift( $a );
709 $s = substr( $s, 1 );
711 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
712 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
714 foreach ( $a as $line ) {
715 if ( preg_match( $e1, $line, $m ) ) {
716 $link = "{$protocol}:{$m[1]}";
718 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
719 else { $text = wfEscapeHTML( $link ); }
720 } else if ( preg_match( $e2, $line, $m ) ) {
721 $link = "{$protocol}:{$m[1]}";
725 $s .= "[{$protocol}:" . $line;
728 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
731 # Expand the URL for printable version
732 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
734 $la = $sk->getExternalLinkAttributes( $link, $text );
735 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
742 /* private */ function replaceInternalLinks( $s )
744 global $wgLang, $wgLinkCache;
745 global $wgNamespacesWithSubpages, $wgLanguageCode;
746 static $fname = "Parser::replaceInternalLink" ;
747 wfProfileIn( $fname );
749 wfProfileIn( "$fname-setup" );
751 # the % is needed to support urlencoded titles as well
752 if ( !$tc ) { $tc = Title
::legalChars() . "#%"; }
753 $sk =& $this->mOptions
->getSkin();
755 $a = explode( "[[", " " . $s );
756 $s = array_shift( $a );
757 $s = substr( $s, 1 );
759 # Match a link having the form [[namespace:link|alternate]]trail
761 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
762 # Match the end of a line for a word that's not followed by whitespace,
763 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
764 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
765 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
766 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
769 # Special and Media are pseudo-namespaces; no pages actually exist in them
770 static $image = FALSE;
771 static $special = FALSE;
772 static $media = FALSE;
773 static $category = FALSE;
774 if ( !$image ) { $image = Namespace::getImage(); }
775 if ( !$special ) { $special = Namespace::getSpecial(); }
776 if ( !$media ) { $media = Namespace::getMedia(); }
777 if ( !$category ) { $category = Namespace::getCategory(); }
779 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
781 if ( $wgLang->linkPrefixExtension() && preg_match( $e2, $s, $m ) ) {
788 wfProfileOut( "$fname-setup" );
790 foreach ( $a as $line ) {
791 $prefix = $new_prefix;
793 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
795 # fix up urlencoded title texts
796 if(preg_match("/%/", $m[1] )) $m[1] = urldecode($m[1]);
798 } else { # Invalid form; output directly
799 $s .= $prefix . "[[" . $line ;
800 wfProfileOut( $fname );
806 :Foobar -- override special treatment of prefix (images, language links)
807 /Foobar -- convert to CurrentPage/Foobar
808 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
810 $c = substr($m[1],0,1);
811 $noforce = ($c != ":");
812 if( $c == "/" ) { # subpage
813 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
814 $m[1]=substr($m[1],1,strlen($m[1])-2);
817 $noslash=substr($m[1],1);
819 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) { # subpages allowed here
820 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
823 } # this might be changed for ugliness reasons
825 $link = $noslash; # no subpage allowed, use standard link
827 } elseif( $noforce ) { # no subpage
830 $link = substr( $m[1], 1 );
832 $wasblank = ( "" == $text );
836 $nt = Title
::newFromText( $link );
838 $s .= $prefix . "[[" . $line;
839 wfProfileOut( $fname );
842 $ns = $nt->getNamespace();
843 $iw = $nt->getInterWiki();
845 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
846 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
847 $s .= $prefix . $trail ;
848 wfProfileOut( $fname );
849 return (trim($s) == '')?
'': $s;
852 if ( $ns == $image ) {
853 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
854 $wgLinkCache->addImageLinkObj( $nt );
855 wfProfileOut( $fname );
857 } else if ( $ns == $category ) {
858 $t = $nt->getText() ;
859 $nnt = Title
::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
861 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
862 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
863 $wgLinkCache->resume();
865 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
866 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
867 $this->mOutput
->mCategoryLinks
[] = $t ;
868 $s .= $prefix . $trail ;
869 wfProfileOut( $fname );
873 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
874 ( strpos( $link, "#" ) == FALSE ) ) {
875 # Self-links are handled specially; generally de-link and change to bold.
876 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, "", $trail );
877 wfProfileOut( $fname );
881 if( $ns == $media ) {
882 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
883 $wgLinkCache->addImageLinkObj( $nt );
884 wfProfileOut( $fname );
886 } elseif( $ns == $special ) {
887 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
888 wfProfileOut( $fname );
891 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
893 wfProfileOut( $fname );
897 # Some functions here used by doBlockLevels()
899 /* private */ function closeParagraph()
902 if ( '' != $this->mLastSection
) {
903 $result = "</" . $this->mLastSection
. ">\n";
905 $this->mInPre
= false;
906 $this->mLastSection
= "";
909 # getCommon() returns the length of the longest common substring
910 # of both arguments, starting at the beginning of both.
912 /* private */ function getCommon( $st1, $st2 )
914 $fl = strlen( $st1 );
915 $shorter = strlen( $st2 );
916 if ( $fl < $shorter ) { $shorter = $fl; }
918 for ( $i = 0; $i < $shorter; ++
$i ) {
919 if ( $st1{$i} != $st2{$i} ) { break; }
923 # These next three functions open, continue, and close the list
924 # element appropriate to the prefix character passed into them.
926 /* private */ function openList( $char )
928 $result = $this->closeParagraph();
930 if ( "*" == $char ) { $result .= "<ul><li>"; }
931 else if ( "#" == $char ) { $result .= "<ol><li>"; }
932 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
933 else if ( ";" == $char ) {
934 $result .= "<dl><dt>";
935 $this->mDTopen
= true;
937 else { $result = "<!-- ERR 1 -->"; }
942 /* private */ function nextItem( $char )
944 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
945 else if ( ":" == $char ||
";" == $char ) {
947 if ( $this->mDTopen
) { $close = "</dt>"; }
948 if ( ";" == $char ) {
949 $this->mDTopen
= true;
950 return $close . "<dt>";
952 $this->mDTopen
= false;
953 return $close . "<dd>";
956 return "<!-- ERR 2 -->";
959 /* private */function closeList( $char )
961 if ( "*" == $char ) { $text = "</li></ul>"; }
962 else if ( "#" == $char ) { $text = "</li></ol>"; }
963 else if ( ":" == $char ) {
964 if ( $this->mDTopen
) {
965 $this->mDTopen
= false;
966 $text = "</dt></dl>";
968 $text = "</dd></dl>";
971 else { return "<!-- ERR 3 -->"; }
975 /* private */ function doBlockLevels( $text, $linestart ) {
976 $fname = "Parser::doBlockLevels";
977 wfProfileIn( $fname );
979 # Parsing through the text line by line. The main thing
980 # happening here is handling of block-level elements p, pre,
981 # and making lists from lines starting with * # : etc.
983 $textLines = explode( "\n", $text );
985 $lastPrefix = $output = $lastLine = '';
986 $this->mDTopen
= $inBlockElem = false;
988 $paragraphStack = false;
991 $output .= array_shift( $textLines );
993 foreach ( $textLines as $oLine ) {
994 $lastPrefixLength = strlen( $lastPrefix );
995 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
996 $preOpenMatch = preg_match("/<pre/i", $oLine );
997 if (!$this->mInPre
) {
998 $this->mInPre
= !empty($preOpenMatch);
1000 if ( !$this->mInPre
) {
1001 # Multiple prefixes may abut each other for nested lists.
1002 $prefixLength = strspn( $oLine, "*#:;" );
1003 $pref = substr( $oLine, 0, $prefixLength );
1006 $pref2 = str_replace( ";", ":", $pref );
1007 $t = substr( $oLine, $prefixLength );
1009 # Don't interpret any other prefixes in preformatted text
1011 $pref = $pref2 = '';
1016 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1017 # Same as the last item, so no need to deal with nesting or opening stuff
1018 $output .= $this->nextItem( substr( $pref, -1 ) );
1019 $paragraphStack = false;
1021 if ( ";" == substr( $pref, -1 ) ) {
1022 # The one nasty exception: definition lists work like this:
1023 # ; title : definition text
1024 # So we check for : in the remainder text to split up the
1025 # title and definition, without b0rking links.
1026 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1027 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1029 $output .= $term . $this->nextItem( ":" );
1033 } elseif( $prefixLength ||
$lastPrefixLength ) {
1034 # Either open or close a level...
1035 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1036 $paragraphStack = false;
1038 while( $commonPrefixLength < $lastPrefixLength ) {
1039 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1040 --$lastPrefixLength;
1042 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1043 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1045 while ( $prefixLength > $commonPrefixLength ) {
1046 $char = substr( $pref, $commonPrefixLength, 1 );
1047 $output .= $this->openList( $char );
1049 if ( ";" == $char ) {
1050 # FIXME: This is dupe of code above
1051 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1053 $output .= $term . $this->nextItem( ":" );
1057 ++
$commonPrefixLength;
1059 $lastPrefix = $pref2;
1061 if( 0 == $prefixLength ) {
1062 # No prefix (not in list)--go to paragraph mode
1063 $uniq_prefix = UNIQ_PREFIX
;
1064 // XXX: use a stack for nestable elements like span, table and div
1065 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1066 $closematch = preg_match(
1067 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1068 "<div|<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1069 if ( $openmatch or $closematch ) {
1070 $paragraphStack = false;
1071 $output .= $this->closeParagraph();
1072 if($preOpenMatch and !$preCloseMatch) {
1073 $this->mInPre
= true;
1075 if ( $closematch ) {
1076 $inBlockElem = false;
1078 $inBlockElem = true;
1080 } else if ( !$inBlockElem && !$this->mInPre
) {
1081 if ( " " == $t{0} and trim($t) != '' ) {
1083 if ($this->mLastSection
!= 'pre') {
1084 $paragraphStack = false;
1085 $output .= $this->closeParagraph().'<pre>';
1086 $this->mLastSection
= 'pre';
1090 if ( '' == trim($t) ) {
1091 if ( $paragraphStack ) {
1092 $output .= $paragraphStack.'<br/>';
1093 $paragraphStack = false;
1094 $this->mLastSection
= 'p';
1096 if ($this->mLastSection
!= 'p' ) {
1097 $output .= $this->closeParagraph();
1098 $this->mLastSection
= '';
1099 $paragraphStack = "<p>";
1101 $paragraphStack = '</p><p>';
1105 if ( $paragraphStack ) {
1106 $output .= $paragraphStack;
1107 $paragraphStack = false;
1108 $this->mLastSection
= 'p';
1109 } else if ($this->mLastSection
!= 'p') {
1110 $output .= $this->closeParagraph().'<p>';
1111 $this->mLastSection
= 'p';
1117 if ($paragraphStack === false) {
1121 while ( $prefixLength ) {
1122 $output .= $this->closeList( $pref2{$prefixLength-1} );
1125 if ( "" != $this->mLastSection
) {
1126 $output .= "</" . $this->mLastSection
. ">";
1127 $this->mLastSection
= "";
1130 wfProfileOut( $fname );
1134 function getVariableValue( $index ) {
1135 global $wgLang, $wgSitename, $wgServer;
1138 case MAG_CURRENTMONTH
:
1140 case MAG_CURRENTMONTHNAME
:
1141 return $wgLang->getMonthName( date("n") );
1142 case MAG_CURRENTMONTHNAMEGEN
:
1143 return $wgLang->getMonthNameGen( date("n") );
1144 case MAG_CURRENTDAY
:
1147 return $this->mTitle
->getText();
1149 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1150 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1151 case MAG_CURRENTDAYNAME
:
1152 return $wgLang->getWeekdayName( date("w")+
1 );
1153 case MAG_CURRENTYEAR
:
1155 case MAG_CURRENTTIME
:
1156 return $wgLang->time( wfTimestampNow(), false );
1157 case MAG_NUMBEROFARTICLES
:
1158 return wfNumberOfArticles();
1168 function initialiseVariables()
1170 global $wgVariableIDs;
1171 $this->mVariables
= array();
1172 foreach ( $wgVariableIDs as $id ) {
1173 $mw =& MagicWord
::get( $id );
1174 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1178 /* private */ function replaceVariables( $text, $args = array() )
1180 global $wgLang, $wgScript, $wgArticlePath;
1182 $fname = "Parser::replaceVariables";
1183 wfProfileIn( $fname );
1186 if ( !$this->mVariables
) {
1187 $this->initialiseVariables();
1189 $titleChars = Title
::legalChars();
1191 # This function is called recursively. To keep track of arguments we need a stack:
1192 array_push( $this->mArgStack
, $args );
1194 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1195 $GLOBALS['wgCurParser'] =& $this;
1197 # Argument substitution
1198 if ( $this->mOutputType
== OT_HTML
) {
1199 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", "wfArgSubstitution", $text );
1202 # Double brace substitution
1203 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1204 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1206 array_pop( $this->mArgStack
);
1208 wfProfileOut( $fname );
1212 function braceSubstitution( $matches )
1214 global $wgLinkCache, $wgLang;
1215 $fname = "Parser::braceSubstitution";
1222 # $newline is an optional newline character before the braces
1223 # $part1 is the bit before the first |, and must contain only title characters
1224 # $args is a list of arguments, starting from index 0, not including $part1
1226 $newline = $matches[1];
1227 $part1 = $matches[2];
1228 # If the third subpattern matched anything, it will start with |
1229 if ( $matches[3] !== "" ) {
1230 $args = explode( "|", substr( $matches[3], 1 ) );
1234 $argc = count( $args );
1237 if ( strpos( $matches[0], "{{{" ) !== false ) {
1238 $text = $matches[0];
1245 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1246 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1247 if ( $this->mOutputType
!= OT_WIKI
) {
1248 # Invalid SUBST not replaced at PST time
1249 # Return without further processing
1250 $text = $matches[0];
1254 } elseif ( $this->mOutputType
== OT_WIKI
) {
1255 # SUBST not found in PST pass, do nothing
1256 $text = $matches[0];
1261 # MSG, MSGNW and INT
1264 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1265 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1268 # Remove obsolete MSG:
1269 $mwMsg =& MagicWord
::get( MAG_MSG
);
1270 $mwMsg->matchStartAndRemove( $part1 );
1273 # Check if it is an internal message
1274 $mwInt =& MagicWord
::get( MAG_INT
);
1275 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1276 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1277 $text = wfMsgReal( $part1, $args, true );
1285 # Check for NS: (namespace expansion)
1286 $mwNs = MagicWord
::get( MAG_NS
);
1287 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1288 if ( intval( $part1 ) ) {
1289 $text = $wgLang->getNsText( intval( $part1 ) );
1292 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1293 if ( !is_null( $index ) ) {
1294 $text = $wgLang->getNsText( $index );
1301 # LOCALURL and LOCALURLE
1303 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1304 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1306 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1307 $func = 'getLocalURL';
1308 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1309 $func = 'escapeLocalURL';
1314 if ( $func !== '' ) {
1315 $title = Title
::newFromText( $part1 );
1316 if ( !is_null( $title ) ) {
1318 $text = $title->$func( $args[0] );
1320 $text = $title->$func();
1327 # Internal variables
1328 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1329 $text = $this->mVariables
[$part1];
1331 $this->mOutput
->mContainsOldMagic
= true;
1334 # Arguments input from the caller
1335 $inputArgs = end( $this->mArgStack );
1336 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1337 $text = $inputArgs[$part1];
1341 # Load from database
1343 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1344 if ( !is_null( $title ) && !$title->isExternal() ) {
1345 # Check for excessive inclusion
1346 $dbk = $title->getPrefixedDBkey();
1347 if ( $this->incrementIncludeCount( $dbk ) ) {
1348 $article = new Article( $title );
1349 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1350 if ( $articleContent !== false ) {
1352 $text = $articleContent;
1357 # If the title is valid but undisplayable, make a link to it
1358 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1359 $text = "[[" . $title->getPrefixedText() . "]]";
1365 # Recursive parsing, escaping and link table handling
1366 # Only for HTML output
1367 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1368 $text = wfEscapeWikiText( $text );
1369 } elseif ( $this->mOutputType
== OT_HTML
&& $found && !$noparse) {
1370 # Clean up argument array
1371 $assocArgs = array();
1373 foreach( $args as $arg ) {
1374 $eqpos = strpos( $arg, "=" );
1375 if ( $eqpos === false ) {
1376 $assocArgs[$index++
] = $arg;
1378 $name = trim( substr( $arg, 0, $eqpos ) );
1379 $value = trim( substr( $arg, $eqpos+
1 ) );
1380 if ( $value === false ) {
1383 if ( $name !== false ) {
1384 $assocArgs[$name] = $value;
1389 # Do not enter included links in link table
1390 if ( !is_null( $title ) ) {
1391 $wgLinkCache->suspend();
1394 # Run full parser on the included text
1395 $text = $this->stripParse( $text, (bool)$newline, $assocArgs );
1397 # Resume the link cache and register the inclusion as a link
1398 if ( !is_null( $title ) ) {
1399 $wgLinkCache->resume();
1400 $wgLinkCache->addLinkObj( $title );
1411 # Triple brace replacement -- used for template arguments
1412 function argSubstitution( $matches )
1414 $newline = $matches[1];
1415 $arg = trim( $matches[2] );
1416 $text = $matches[0];
1417 $inputArgs = end( $this->mArgStack
);
1419 if ( array_key_exists( $arg, $inputArgs ) ) {
1420 $text = $this->stripParse( $inputArgs[$arg], (bool)$newline, array() );
1426 # Returns true if the function is allowed to include this entity
1427 function incrementIncludeCount( $dbk )
1429 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1430 $this->mIncludeCount
[$dbk] = 0;
1432 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1440 # Cleans up HTML, removes dangerous tags and attributes
1441 /* private */ function removeHTMLtags( $text )
1443 global $wgUseTidy, $wgUserHtml;
1444 $fname = "Parser::removeHTMLtags";
1445 wfProfileIn( $fname );
1448 $htmlpairs = array( # Tags that must be closed
1449 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1450 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1451 "strike", "strong", "tt", "var", "div", "center",
1452 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1453 "ruby", "rt" , "rb" , "rp", "p"
1455 $htmlsingle = array(
1456 "br", "hr", "li", "dt", "dd"
1458 $htmlnest = array( # Tags that can be nested--??
1459 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1460 "dl", "font", "big", "small", "sub", "sup"
1462 $tabletags = array( # Can only appear inside table
1466 $htmlpairs = array();
1467 $htmlsingle = array();
1468 $htmlnest = array();
1469 $tabletags = array();
1472 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1473 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1475 $htmlattrs = $this->getHTMLattrs () ;
1477 # Remove HTML comments
1478 $text = preg_replace( "/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU", "$2", $text );
1480 $bits = explode( "<", $text );
1481 $text = array_shift( $bits );
1483 $tagstack = array(); $tablestack = array();
1484 foreach ( $bits as $x ) {
1485 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1486 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1488 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1489 error_reporting( $prev );
1492 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1496 if ( ! in_array( $t, $htmlsingle ) &&
1497 ( count($tagstack) && $ot = array_pop( $tagstack ) ) != $t ) {
1498 if(!empty($ot)) array_push( $tagstack, $ot );
1501 if ( $t == "table" ) {
1502 $tagstack = array_pop( $tablestack );
1507 # Keep track for later
1508 if ( in_array( $t, $tabletags ) &&
1509 ! in_array( "table", $tagstack ) ) {
1511 } else if ( in_array( $t, $tagstack ) &&
1512 ! in_array ( $t , $htmlnest ) ) {
1514 } else if ( ! in_array( $t, $htmlsingle ) ) {
1515 if ( $t == "table" ) {
1516 array_push( $tablestack, $tagstack );
1517 $tagstack = array();
1519 array_push( $tagstack, $t );
1521 # Strip non-approved attributes from the tag
1522 $newparams = $this->fixTagAttributes($params);
1526 $rest = str_replace( ">", ">", $rest );
1527 $text .= "<$slash$t $newparams$brace$rest";
1531 $text .= "<" . str_replace( ">", ">", $x);
1533 # Close off any remaining tags
1534 while ( $t = array_pop( $tagstack ) ) {
1536 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1539 # this might be possible using tidy itself
1540 foreach ( $bits as $x ) {
1541 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1543 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1544 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1545 $newparams = $this->fixTagAttributes($params);
1546 $rest = str_replace( ">", ">", $rest );
1547 $text .= "<$slash$t $newparams$brace$rest";
1549 $text .= "<" . str_replace( ">", ">", $x);
1553 wfProfileOut( $fname );
1560 * This function accomplishes several tasks:
1561 * 1) Auto-number headings if that option is enabled
1562 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1563 * 3) Add a Table of contents on the top for users who have enabled the option
1564 * 4) Auto-anchor headings
1566 * It loops through all headlines, collects the necessary data, then splits up the
1567 * string and re-inserts the newly formatted headlines.
1571 /* private */ function formatHeadings( $text, $isMain=true )
1573 global $wgInputEncoding;
1575 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1576 $doShowToc = $this->mOptions
->getShowToc();
1577 if( !$this->mTitle
->userCanEdit() ) {
1579 $rightClickHack = 0;
1581 $showEditLink = $this->mOptions
->getEditSection();
1582 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1585 # Inhibit editsection links if requested in the page
1586 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1587 if( $esw->matchAndRemove( $text ) ) {
1590 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1592 $mw =& MagicWord
::get( MAG_NOTOC
);
1593 if( $mw->matchAndRemove( $text ) ) {
1597 # never add the TOC to the Main Page. This is an entry page that should not
1598 # be more than 1-2 screens large anyway
1599 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1603 # Get all headlines for numbering them and adding funky stuff like [edit]
1604 # links - this is for later, but we need the number of headlines right now
1605 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1607 # if there are fewer than 4 headlines in the article, do not show TOC
1608 if( $numMatches < 4 ) {
1612 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1613 # override above conditions and always show TOC
1614 $mw =& MagicWord
::get( MAG_FORCETOC
);
1615 if ($mw->matchAndRemove( $text ) ) {
1620 # We need this to perform operations on the HTML
1621 $sk =& $this->mOptions
->getSkin();
1626 # Ugh .. the TOC should have neat indentation levels which can be
1627 # passed to the skin functions. These are determined here
1632 $sublevelCount = array();
1635 foreach( $matches[3] as $headline ) {
1638 $prevlevel = $level;
1640 $level = $matches[1][$headlineCount];
1641 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1642 # reset when we enter a new level
1643 $sublevelCount[$level] = 0;
1644 $toc .= $sk->tocIndent( $level - $prevlevel );
1645 $toclevel +
= $level - $prevlevel;
1647 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1648 # reset when we step back a level
1649 $sublevelCount[$level+
1]=0;
1650 $toc .= $sk->tocUnindent( $prevlevel - $level );
1651 $toclevel -= $prevlevel - $level;
1653 # count number of headlines for each level
1654 @$sublevelCount[$level]++
;
1655 if( $doNumberHeadings ||
$doShowToc ) {
1657 for( $i = 1; $i <= $level; $i++
) {
1658 if( !empty( $sublevelCount[$i] ) ) {
1662 $numbering .= $sublevelCount[$i];
1668 # The canonized header is a version of the header text safe to use for links
1669 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1670 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1673 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1674 $tocline = trim( $canonized_headline );
1675 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', urlencode( do_html_entity_decode( $tocline, ENT_COMPAT
, $wgInputEncoding ) ) );
1676 # strip out urlencoded (inserted for french spaces, e.g. first space in 'something : something')
1677 $canonized_headline = str_replace('%C2%A0','_', $canonized_headline);
1678 $refer[$headlineCount] = $canonized_headline;
1680 # count how many in assoc. array so we can track dupes in anchors
1681 @$refers[$canonized_headline]++
;
1682 $refcount[$headlineCount]=$refers[$canonized_headline];
1684 # Prepend the number to the heading text
1686 if( $doNumberHeadings ||
$doShowToc ) {
1687 $tocline = $numbering . " " . $tocline;
1689 # Don't number the heading if it is the only one (looks silly)
1690 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1691 # the two are different if the line contains a link
1692 $headline=$numbering . " " . $headline;
1696 # Create the anchor for linking from the TOC to the section
1697 $anchor = $canonized_headline;
1698 if($refcount[$headlineCount] > 1 ) {
1699 $anchor .= "_" . $refcount[$headlineCount];
1702 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1704 if( $showEditLink ) {
1705 if ( empty( $head[$headlineCount] ) ) {
1706 $head[$headlineCount] = "";
1708 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1711 # Add the edit section span
1712 if( $rightClickHack ) {
1713 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1716 # give headline the correct <h#> tag
1717 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1723 $toclines = $headlineCount;
1724 $toc .= $sk->tocUnindent( $toclevel );
1725 $toc = $sk->tocTable( $toc );
1728 # split up and insert constructed headlines
1730 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1733 foreach( $blocks as $block ) {
1734 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1735 # This is the [edit] link that appears for the top block of text when
1736 # section editing is enabled
1738 # Disabled because it broke block formatting
1739 # For example, a bullet point in the top line
1740 # $full .= $sk->editSectionLink(0);
1743 if( $doShowToc && !$i && $isMain) {
1744 # Top anchor now in skin
1748 if( !empty( $head[$i] ) ) {
1757 /* private */ function magicISBN( $text )
1761 $a = split( "ISBN ", " $text" );
1762 if ( count ( $a ) < 2 ) return $text;
1763 $text = substr( array_shift( $a ), 1);
1764 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1766 foreach ( $a as $x ) {
1767 $isbn = $blank = "" ;
1768 while ( " " == $x{0} ) {
1770 $x = substr( $x, 1 );
1772 while ( strstr( $valid, $x{0} ) != false ) {
1774 $x = substr( $x, 1 );
1776 $num = str_replace( "-", "", $isbn );
1777 $num = str_replace( " ", "", $num );
1780 $text .= "ISBN $blank$x";
1782 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1783 $text .= "<a href=\"" .
1784 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1785 "\" class=\"internal\">ISBN $isbn</a>";
1791 /* private */ function magicRFC( $text )
1795 $a = split( "ISBN ", " $text" );
1796 if ( count ( $a ) < 2 ) return $text;
1797 $text = substr( array_shift( $a ), 1);
1798 $valid = "0123456789";
1800 foreach ( $a as $x ) {
1801 $rfc = $blank = "" ;
1802 while ( " " == $x{0} ) {
1804 $x = substr( $x, 1 );
1806 while ( strstr( $valid, $x{0} ) != false ) {
1808 $x = substr( $x, 1 );
1812 $text .= "RFC $blank$x";
1814 $url = wfmsg( "rfcurl" );
1815 $url = str_replace( "$1", $rfc, $url);
1816 $sk =& $this->mOptions
->getSkin();
1817 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1818 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1824 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1826 $this->mOptions
= $options;
1827 $this->mTitle
=& $title;
1828 $this->mOutputType
= OT_WIKI
;
1830 if ( $clearState ) {
1831 $this->clearState();
1834 $stripState = false;
1838 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
1841 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
1842 "/<br *?>/i" => "<br/>",
1844 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
1845 $text = $this->strip( $text, $stripState, false );
1846 $text = $this->pstPass2( $text, $user );
1847 $text = $this->unstrip( $text, $stripState );
1851 /* private */ function pstPass2( $text, &$user )
1853 global $wgLang, $wgLocaltimezone, $wgCurParser;
1855 # Variable replacement
1856 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1857 $text = $this->replaceVariables( $text );
1861 $n = $user->getName();
1862 $k = $user->getOption( "nickname" );
1863 if ( "" == $k ) { $k = $n; }
1864 if(isset($wgLocaltimezone)) {
1865 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1867 /* Note: this is an ugly timezone hack for the European wikis */
1868 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1869 " (" . date( "T" ) . ")";
1870 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1872 $text = preg_replace( "/~~~~~/", $d, $text );
1873 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1874 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1875 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1876 Namespace::getUser() ) . ":$n|$k]]", $text );
1878 # Context links: [[|name]] and [[name (context)|]]
1880 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1881 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1882 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1883 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1885 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1886 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1887 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1888 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1889 # [[ns:page (cont)|]]
1891 $t = $this->mTitle
->getText();
1892 if ( preg_match( $conpat, $t, $m ) ) {
1895 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1896 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1897 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1899 if ( "" == $context ) {
1900 $text = preg_replace( $p2, "[[\\1]]", $text );
1902 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1906 $mw =& MagicWord::get( MAG_SUBST );
1907 $wgCurParser = $this->fork();
1908 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1909 $this->merge( $wgCurParser );
1912 # Trim trailing whitespace
1913 # MAG_END (__END__) tag allows for trailing
1914 # whitespace to be deliberately included
1915 $text = rtrim( $text );
1916 $mw =& MagicWord
::get( MAG_END
);
1917 $mw->matchAndRemove( $text );
1922 # Set up some variables which are usually set up in parse()
1923 # so that an external function can call some class members with confidence
1924 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1926 $this->mTitle
=& $title;
1927 $this->mOptions
= $options;
1928 $this->mOutputType
= $outputType;
1929 if ( $clearState ) {
1930 $this->clearState();
1934 function transformMsg( $text, $options ) {
1936 static $executing = false;
1938 # Guard against infinite recursion
1944 $this->mTitle
= $wgTitle;
1945 $this->mOptions
= $options;
1946 $this->mOutputType
= OT_MSG
;
1947 $this->clearState();
1948 $text = $this->replaceVariables( $text );
1957 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1959 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1960 $containsOldMagic = false )
1962 $this->mText
= $text;
1963 $this->mLanguageLinks
= $languageLinks;
1964 $this->mCategoryLinks
= $categoryLinks;
1965 $this->mContainsOldMagic
= $containsOldMagic;
1968 function getText() { return $this->mText
; }
1969 function getLanguageLinks() { return $this->mLanguageLinks
; }
1970 function getCategoryLinks() { return $this->mCategoryLinks
; }
1971 function containsOldMagic() { return $this->mContainsOldMagic
; }
1972 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
1973 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
1974 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
1975 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
1977 function merge( $other ) {
1978 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
1979 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
1980 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
1987 # All variables are private
1988 var $mUseTeX; # Use texvc to expand <math> tags
1989 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1990 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1991 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1992 var $mAllowExternalImages; # Allow external images inline
1993 var $mSkin; # Reference to the preferred skin
1994 var $mDateFormat; # Date format index
1995 var $mEditSection; # Create "edit section" links
1996 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1997 var $mNumberHeadings; # Automatically number headings
1998 var $mShowToc; # Show table of contents
2000 function getUseTeX() { return $this->mUseTeX
; }
2001 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2002 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2003 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2004 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2005 function getSkin() { return $this->mSkin
; }
2006 function getDateFormat() { return $this->mDateFormat
; }
2007 function getEditSection() { return $this->mEditSection
; }
2008 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2009 function getNumberHeadings() { return $this->mNumberHeadings
; }
2010 function getShowToc() { return $this->mShowToc
; }
2012 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2013 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2014 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2015 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2016 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2017 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
2018 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2019 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2020 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2021 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2022 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2024 /* static */ function newFromUser( &$user )
2026 $popts = new ParserOptions
;
2027 $popts->initialiseFromUser( $user );
2031 function initialiseFromUser( &$userInput )
2033 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2035 if ( !$userInput ) {
2037 $user->setLoaded( true );
2039 $user =& $userInput;
2042 $this->mUseTeX
= $wgUseTeX;
2043 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2044 $this->mUseDynamicDates
= $wgUseDynamicDates;
2045 $this->mInterwikiMagic
= $wgInterwikiMagic;
2046 $this->mAllowExternalImages
= $wgAllowExternalImages;
2047 $this->mSkin
=& $user->getSkin();
2048 $this->mDateFormat
= $user->getOption( "date" );
2049 $this->mEditSection
= $user->getOption( "editsection" );
2050 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
2051 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
2052 $this->mShowToc
= $user->getOption( "showtoc" );
2058 # Regex callbacks, used in Parser::replaceVariables
2059 function wfBraceSubstitution( $matches )
2061 global $wgCurParser;
2062 return $wgCurParser->braceSubstitution( $matches );
2065 function wfArgSubstitution( $matches )
2067 global $wgCurParser;
2068 return $wgCurParser->argSubstitution( $matches );