3 include_once('Tokenizer.php');
7 # Processes wiki markup
9 # There are two main entry points into the Parser class: parse() and preSaveTransform().
10 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
13 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
15 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
17 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
18 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
21 # * only within ParserOptions
24 #----------------------------------------
25 # Variable substitution O(N^2) attack
26 #-----------------------------------------
27 # Without countermeasures, it would be possible to attack the parser by saving a page
28 # filled with a large number of inclusions of large pages. The size of the generated
29 # page would be proportional to the square of the input size. Hence, we limit the number
30 # of inclusions of any given page, thus bringing any attack back to O(N).
32 define( "MAX_INCLUDE_REPEAT", 5 );
34 # Recursion depth of variable/inclusion evaluation
35 define( "MAX_INCLUDE_PASSES", 3 );
37 # Allowed values for $mOutputType
38 define( "OT_HTML", 1 );
39 define( "OT_WIKI", 2 );
43 # Cleared with clearState():
44 var $mOutput, $mAutonumber, $mLastSection, $mDTopen, $mStripState;
45 var $mVariables, $mIncludeCount;
48 var $mOptions, $mTitle, $mOutputType;
57 $this->mOutput
= new ParserOutput
;
58 $this->mAutonumber
= 0;
59 $this->mLastSection
= "";
60 $this->mDTopen
= false;
61 $this->mStripState
= false;
62 $this->mVariables
= false;
63 $this->mIncludeCount
= array();
66 # First pass--just handle <nowiki> sections, pass the rest off
67 # to doWikiPass2() which does all the real work.
69 # Returns a ParserOutput
71 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
73 $fname = "Parser::parse";
74 wfProfileIn( $fname );
80 $this->mOptions
= $options;
81 $this->mTitle
=& $title;
82 $this->mOutputType
= OT_HTML
;
85 $text = $this->strip( $text, $this->mStripState
);
86 $text = $this->doWikiPass2( $text, $linestart );
87 $text = $this->unstrip( $text, $this->mStripState
);
89 $this->mOutput
->setText( $text );
90 wfProfileOut( $fname );
91 return $this->mOutput
;
94 /* static */ function getRandomString()
96 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
99 # Strips <nowiki>, <pre> and <math>
100 # Returns the text, and fills an array with data needed in unstrip()
102 function strip( $text, &$state )
107 'nwunq' => Parser
::getRandomString(),
108 'mathlist' => array(),
110 'mathunq' => Parser
::getRandomString(),
111 'prelist' => array(),
113 'preunq' => Parser
::getRandomString()
115 $render = ($this->mOutputType
== OT_HTML
);
120 # Replace any instances of the placeholders
121 $text = str_replace( $state['nwunq'], wfHtmlEscapeFirst( $state['nwunq'] ), $text );
122 $text = str_replace( $state['mathunq'], wfHtmlEscapeFirst( $state['mathunq'] ), $text );
123 $text = str_replace( $state['preunq'], wfHtmlEscapeFirst( $state['preunq'] ), $text );
125 while ( "" != $text ) {
126 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
128 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
131 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
135 $state['nwlist'][$state['nwsecs']] = wfEscapeHTMLTagsOnly($q[0]);
137 $state['nwlist'][$state['nwsecs']] = "<nowiki>{$q[0]}</nowiki>";
140 $stripped .= $state['nwunq'] . sprintf("%08X", $state['nwsecs']);
145 if( $this->mOptions
->getUseTeX() ) {
146 while ( "" != $stripped ) {
147 $p = preg_split( "/<\\s*math\\s*>/i", $stripped, 2 );
149 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
152 $q = preg_split( "/<\\/\\s*math\\s*>/i", $p[1], 2 );
153 ++
$state['mathsecs'];
156 $state['mathlist'][$state['mathsecs']] = renderMath($q[0]);
158 $state['mathlist'][$state['mathsecs']] = "<math>{$q[0]}</math>";
161 $stripped2 .= $state['mathunq'] . sprintf("%08X", $state['mathsecs']);
166 $stripped2 = $stripped;
169 while ( "" != $stripped2 ) {
170 $p = preg_split( "/<\\s*pre\\s*>/i", $stripped2, 2 );
172 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
175 $q = preg_split( "/<\\/\\s*pre\\s*>/i", $p[1], 2 );
179 $state['prelist'][$state['presecs']] = "<pre>". wfEscapeHTMLTagsOnly($q[0]). "</pre>\n";
181 $state['prelist'][$state['presecs']] = "<pre>{$q[0]}</pre>";
184 $stripped3 .= $state['preunq'] . sprintf("%08X", $state['presecs']);
191 function unstrip( $text, &$state )
193 for ( $i = 1; $i <= $state['presecs']; ++
$i ) {
194 $text = str_replace( $state['preunq'] . sprintf("%08X", $i), $state['prelist'][$i], $text );
197 for ( $i = 1; $i <= $state['mathsecs']; ++
$i ) {
198 $text = str_replace( $state['mathunq'] . sprintf("%08X", $i), $state['mathlist'][$i], $text );
201 for ( $i = 1; $i <= $state['nwsecs']; ++
$i ) {
202 $text = str_replace( $state['nwunq'] . sprintf("%08X", $i), $state['nwlist'][$i], $text );
207 function categoryMagic ()
209 global $wgLang , $wgUser ;
210 if ( !$this->mOptions
->getUseCategoryMagic() ) return ;
211 $id = $this->mTitle
->getArticleID() ;
212 $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
213 $ti = $this->mTitle
->getText() ;
214 $ti = explode ( ":" , $ti , 2 ) ;
215 if ( $cat != $ti[0] ) return "" ;
216 $r = "<br break=all>\n" ;
218 $articles = array() ;
219 $parents = array () ;
220 $children = array() ;
223 # $sk =& $this->mGetSkin();
224 $sk =& $wgUser->getSkin() ;
228 $sql = "SELECT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
230 $sql = "SELECT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
233 $res = wfQuery ( $sql, DB_READ
) ;
234 while ( $x = wfFetchObject ( $res ) )
237 # $t->newFromDBkey ( $x->l_from ) ;
238 # $t = $t->getText() ;
239 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
240 if ( $t != "" ) $t .= ":" ;
241 $t .= $x->cur_title
;
243 $y = explode ( ":" , $t , 2 ) ;
244 if ( count ( $y ) == 2 && $y[0] == $cat ) {
245 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
247 array_push ( $articles , $sk->makeLink ( $t ) ) ;
250 wfFreeResult ( $res ) ;
253 if ( count ( $children ) > 0 )
255 asort ( $children ) ;
256 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
257 $r .= implode ( ", " , $children ) ;
261 if ( count ( $articles ) > 0 )
263 asort ( $articles ) ;
264 $h = wfMsg( "category_header", $ti[1] );
265 $r .= "<h2>{$h}</h2>\n" ;
266 $r .= implode ( ", " , $articles ) ;
273 function getHTMLattrs ()
275 $htmlattrs = array( # Allowed attributes--no scripting, etc.
276 "title", "align", "lang", "dir", "width", "height",
277 "bgcolor", "clear", /* BR */ "noshade", /* HR */
278 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
279 /* FONT */ "type", "start", "value", "compact",
280 /* For various lists, mostly deprecated but safe */
281 "summary", "width", "border", "frame", "rules",
282 "cellspacing", "cellpadding", "valign", "char",
283 "charoff", "colgroup", "col", "span", "abbr", "axis",
284 "headers", "scope", "rowspan", "colspan", /* Tables */
285 "id", "class", "name", "style" /* For CSS */
290 function fixTagAttributes ( $t )
292 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
293 $htmlattrs = $this->getHTMLattrs() ;
295 # Strip non-approved attributes from the tag
297 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
298 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
300 # Strip javascript "expression" from stylesheets. Brute force approach:
301 # If anythin offensive is found, all attributes of the HTML tag are dropped
304 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
305 wfMungeToUtf8( $t ) ) )
313 function doTableStuff ( $t )
315 $t = explode ( "\n" , $t ) ;
316 $td = array () ; # Is currently a td tag open?
317 $ltd = array () ; # Was it TD or TH?
318 $tr = array () ; # Is currently a tr tag open?
319 $ltr = array () ; # tr attributes
320 foreach ( $t AS $k => $x )
323 $fc = substr ( $x , 0 , 1 ) ;
324 if ( "{|" == substr ( $x , 0 , 2 ) )
326 $t[$k] = "<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
327 array_push ( $td , false ) ;
328 array_push ( $ltd , "" ) ;
329 array_push ( $tr , false ) ;
330 array_push ( $ltr , "" ) ;
332 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
333 else if ( "|}" == substr ( $x , 0 , 2 ) )
336 $l = array_pop ( $ltd ) ;
337 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
338 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
342 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
344 $z = trim ( substr ( $x , 2 ) ) ;
345 $t[$k] = "<caption>{$z}</caption>\n" ;
347 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
349 $x = substr ( $x , 1 ) ;
350 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
352 $l = array_pop ( $ltd ) ;
353 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
354 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
357 array_push ( $tr , false ) ;
358 array_push ( $td , false ) ;
359 array_push ( $ltd , "" ) ;
360 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
362 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
364 if ( "|+" == substr ( $x , 0 , 2 ) )
367 $x = substr ( $x , 1 ) ;
369 $after = substr ( $x , 1 ) ;
370 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
371 $after = explode ( "||" , $after ) ;
373 foreach ( $after AS $theline )
378 $tra = array_pop ( $ltr ) ;
379 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
380 array_push ( $tr , true ) ;
381 array_push ( $ltr , "" ) ;
384 $l = array_pop ( $ltd ) ;
385 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
386 if ( $fc == "|" ) $l = "TD" ;
387 else if ( $fc == "!" ) $l = "TH" ;
388 else if ( $fc == "+" ) $l = "CAPTION" ;
390 array_push ( $ltd , $l ) ;
391 $y = explode ( "|" , $theline , 2 ) ;
392 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
393 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
395 array_push ( $td , true ) ;
400 # Closing open td, tr && table
401 while ( count ( $td ) > 0 )
403 if ( array_pop ( $td ) ) $t[] = "</td>" ;
404 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
408 $t = implode ( "\n" , $t ) ;
409 # $t = $this->removeHTMLtags( $t );
413 # Well, OK, it's actually about 14 passes. But since all the
414 # hard lifting is done inside PHP's regex code, it probably
415 # wouldn't speed things up much to add a real parser.
417 function doWikiPass2( $text, $linestart )
419 $fname = "Parser::doWikiPass2";
420 wfProfileIn( $fname );
422 $text = $this->removeHTMLtags( $text );
423 $text = $this->replaceVariables( $text );
425 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
426 $text = str_replace ( "<HR>", "<hr>", $text );
428 $text = $this->doHeadings( $text );
429 $text = $this->doBlockLevels( $text, $linestart );
431 if($this->mOptions
->getUseDynamicDates()) {
432 global $wgDateFormatter;
433 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
436 $text = $this->replaceExternalLinks( $text );
437 $text = $this->replaceInternalLinks ( $text );
438 $text = $this->doTableStuff ( $text ) ;
440 $text = $this->formatHeadings( $text );
442 $sk =& $this->mOptions
->getSkin();
443 $text = $sk->transformContent( $text );
444 $text .= $this->categoryMagic () ;
446 wfProfileOut( $fname );
451 /* private */ function doHeadings( $text )
453 for ( $i = 6; $i >= 1; --$i ) {
454 $h = substr( "======", 0, $i );
455 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
456 "<h{$i}>\\1</h{$i}>\\2", $text );
461 # Note: we have to do external links before the internal ones,
462 # and otherwise take great care in the order of things here, so
463 # that we don't end up interpreting some URLs twice.
465 /* private */ function replaceExternalLinks( $text )
467 $fname = "Parser::replaceExternalLinks";
468 wfProfileIn( $fname );
469 $text = $this->subReplaceExternalLinks( $text, "http", true );
470 $text = $this->subReplaceExternalLinks( $text, "https", true );
471 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
472 $text = $this->subReplaceExternalLinks( $text, "irc", false );
473 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
474 $text = $this->subReplaceExternalLinks( $text, "news", false );
475 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
476 wfProfileOut( $fname );
480 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
482 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
483 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
485 # this is the list of separators that should be ignored if they
486 # are the last character of an URL but that should be included
487 # if they occur within the URL, e.g. "go to www.foo.com, where .."
488 # in this case, the last comma should not become part of the URL,
489 # but in "www.foo.com/123,2342,32.htm" it should.
491 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
492 $images = "gif|png|jpg|jpeg";
494 # PLEASE NOTE: The curly braces { } are not part of the regex,
495 # they are interpreted as part of the string (used to tell PHP
496 # that the content of the string should be inserted there).
497 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
498 "((?i){$images})([^{$uc}]|$)/";
500 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
501 $sk =& $this->mOptions
->getSkin();
503 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
504 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
505 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
507 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
508 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
509 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
511 $s = str_replace( $unique, $protocol, $s );
513 $a = explode( "[{$protocol}:", " " . $s );
514 $s = array_shift( $a );
515 $s = substr( $s, 1 );
517 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
518 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
520 foreach ( $a as $line ) {
521 if ( preg_match( $e1, $line, $m ) ) {
522 $link = "{$protocol}:{$m[1]}";
524 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
525 else { $text = wfEscapeHTML( $link ); }
526 } else if ( preg_match( $e2, $line, $m ) ) {
527 $link = "{$protocol}:{$m[1]}";
531 $s .= "[{$protocol}:" . $line;
534 if ( $this->mOptions
->getPrintable() ) $paren = " (<i>" . htmlspecialchars ( $link ) . "</i>)";
536 $la = $sk->getExternalLinkAttributes( $link, $text );
537 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
543 /* private */ function handle3Quotes( &$state, $token )
545 if ( $state["strong"] ) {
546 if ( $state["em"] && $state["em"] > $state["strong"] )
548 # ''' lala ''lala '''
549 $s = "</em></strong><em>";
553 $state["strong"] = FALSE;
556 $state["strong"] = $token["pos"];
561 /* private */ function handle2Quotes( &$state, $token )
563 if ( $state["em"] ) {
564 if ( $state["strong"] && $state["strong"] > $state["em"] )
566 # ''lala'''lala'' ....'''
567 $s = "</strong></em><strong>";
571 $state["em"] = FALSE;
574 $state["em"] = $token["pos"];
579 /* private */ function handle5Quotes( &$state, $token )
581 if ( $state["em"] && $state["strong"] ) {
582 if ( $state["em"] < $state["strong"] ) {
583 $s .= "</strong></em>";
585 $s .= "</em></strong>";
587 $state["strong"] = $state["em"] = FALSE;
588 } elseif ( $state["em"] ) {
589 $s .= "</em><strong>";
590 $state["em"] = FALSE;
591 $state["strong"] = $token["pos"];
592 } elseif ( $state["strong"] ) {
593 $s .= "</strong><em>";
594 $state["strong"] = FALSE;
595 $state["em"] = $token["pos"];
596 } else { # not $em and not $strong
597 $s .= "<strong><em>";
598 $state["strong"] = $state["em"] = $token["pos"];
603 /* private */ function replaceInternalLinks( $str )
605 global $wgLang; # for language specific parser hook
607 $tokenizer=Tokenizer
::newFromString( $str );
608 $tokenStack = array();
611 $state["em"] = FALSE;
612 $state["strong"] = FALSE;
616 # The tokenizer splits the text into tokens and returns them one by one.
617 # Every call to the tokenizer returns a new token.
618 while ( $token = $tokenizer->nextToken() )
621 switch ( $token["type"] )
624 # simple text with no further markup
625 $txt = $token["text"];
628 # remember the tag opened with 3 [
632 # FIXME : Treat orphaned open tags (stack not empty when text is over)
634 array_push( $tokenStack, $token );
641 # get text from stack, glue it together, and call the code to handle a
644 if ( count( $tokenStack ) == 0 )
646 # stack empty. Found a ]] without an opening [[
650 $lastToken = array_pop( $tokenStack );
651 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
653 if( !empty( $lastToken["text"] ) ) {
654 $linkText = $lastToken["text"] . $linkText;
656 $lastToken = array_pop( $tokenStack );
659 $txt = $linkText ."]]";
661 if( isset( $lastToken["text"] ) ) {
662 $prefix = $lastToken["text"];
666 $nextToken = $tokenizer->previewToken();
667 if ( $nextToken["type"] == "text" )
669 # Preview just looks at it. Now we have to fetch it.
670 $nextToken = $tokenizer->nextToken();
671 $txt .= $nextToken["text"];
673 $txt = $this->handleInternalLink( $txt, $prefix );
675 # did the tag start with 3 [ ?
677 # show the first as text
683 $tagIsOpen = (count( $tokenStack ) != 0);
689 # This and the three next ones handle quotes
690 $txt = $this->handle3Quotes( $state, $token );
693 $txt = $this->handle2Quotes( $state, $token );
696 $txt = $this->handle5Quotes( $state, $token );
706 $txt = $this->doMagicRFC( $tokenizer );
713 $txt = $this->doMagicISBN( $tokenizer );
717 # Call language specific Hook.
718 $txt = $wgLang->processToken( $token, $tokenStack );
719 if ( NULL == $txt ) {
720 # An unkown token. Highlight.
721 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
722 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
726 # If we're parsing the interior of a link, don't append the interior to $s,
727 # but push it to the stack so it can be processed when a ]] token is found.
728 if ( $tagIsOpen && $txt != "" ) {
729 $token["type"] = "text";
730 $token["text"] = $txt;
731 array_push( $tokenStack, $token );
736 if ( count( $tokenStack ) != 0 )
738 # still objects on stack. opened [[ tag without closing ]] tag.
740 while ( $lastToken = array_pop( $tokenStack ) )
742 if ( $lastToken["type"] == "text" )
744 $txt = $lastToken["text"] . $txt;
746 $txt = $lastToken["type"] . $txt;
754 /* private */ function handleInternalLink( $line, $prefix )
756 global $wgLang, $wgLinkCache;
757 global $wgNamespacesWithSubpages, $wgLanguageCode;
758 static $fname = "Parser::replaceInternalLinks" ;
759 wfProfileIn( $fname );
761 wfProfileIn( "$fname-setup" );
763 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
764 $sk =& $this->mOptions
->getSkin();
766 # Match a link having the form [[namespace:link|alternate]]trail
768 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
769 # Match the end of a line for a word that's not followed by whitespace,
770 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
771 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
772 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
773 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
776 # Special and Media are pseudo-namespaces; no pages actually exist in them
777 static $image = FALSE;
778 static $special = FALSE;
779 static $media = FALSE;
780 static $category = FALSE;
781 if ( !$image ) { $image = Namespace::getImage(); }
782 if ( !$special ) { $special = Namespace::getSpecial(); }
783 if ( !$media ) { $media = Namespace::getMedia(); }
784 if ( !$category ) { $category = wfMsg ( "category" ) ; }
786 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
788 wfProfileOut( "$fname-setup" );
791 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
794 } else { # Invalid form; output directly
795 $s .= $prefix . "[[" . $line ;
801 :Foobar -- override special treatment of prefix (images, language links)
802 /Foobar -- convert to CurrentPage/Foobar
803 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
805 $c = substr($m[1],0,1);
806 $noforce = ($c != ":");
807 if( $c == "/" ) { # subpage
808 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
809 $m[1]=substr($m[1],1,strlen($m[1])-2);
812 $noslash=substr($m[1],1);
814 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
815 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
818 } # this might be changed for ugliness reasons
820 $link = $noslash; # no subpage allowed, use standard link
822 } elseif( $noforce ) { # no subpage
825 $link = substr( $m[1], 1 );
830 $nt = Title
::newFromText( $link );
832 $s .= $prefix . "[[" . $line;
835 $ns = $nt->getNamespace();
836 $iw = $nt->getInterWiki();
838 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
839 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
840 $s .= $prefix . $trail;
843 if( $ns == $image ) {
844 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
845 $wgLinkCache->addImageLinkObj( $nt );
849 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
850 ( strpos( $link, "#" ) == FALSE ) ) {
851 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
856 $catns = strtoupper ( $nt->getDBkey () ) ;
857 $catns = explode ( ":" , $catns ) ;
858 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
860 if ( $catns == strtoupper($category) && $this->mOptions
->getUseCategoryMagic() ) {
861 $t = explode ( ":" , $nt->getText() ) ;
863 $t = implode ( ":" , $t ) ;
864 $t = $wgLang->ucFirst ( $t ) ;
865 $nnt = Title
::newFromText ( $category.":".$t ) ;
866 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
867 $this->mOutput
->mCategoryLinks
[] = $t ;
868 $s .= $prefix . $trail ;
871 if( $ns == $media ) {
872 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
873 $wgLinkCache->addImageLinkObj( $nt );
875 } elseif( $ns == $special ) {
876 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
879 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
881 wfProfileOut( $fname );
885 # Some functions here used by doBlockLevels()
887 /* private */ function closeParagraph()
890 if ( 0 != strcmp( "p", $this->mLastSection
) &&
891 0 != strcmp( "", $this->mLastSection
) ) {
892 $result = "</" . $this->mLastSection
. ">";
894 $this->mLastSection
= "";
897 # getCommon() returns the length of the longest common substring
898 # of both arguments, starting at the beginning of both.
900 /* private */ function getCommon( $st1, $st2 )
902 $fl = strlen( $st1 );
903 $shorter = strlen( $st2 );
904 if ( $fl < $shorter ) { $shorter = $fl; }
906 for ( $i = 0; $i < $shorter; ++
$i ) {
907 if ( $st1{$i} != $st2{$i} ) { break; }
911 # These next three functions open, continue, and close the list
912 # element appropriate to the prefix character passed into them.
914 /* private */ function openList( $char )
916 $result = $this->closeParagraph();
918 if ( "*" == $char ) { $result .= "<ul><li>"; }
919 else if ( "#" == $char ) { $result .= "<ol><li>"; }
920 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
921 else if ( ";" == $char ) {
922 $result .= "<dl><dt>";
923 $this->mDTopen
= true;
925 else { $result = "<!-- ERR 1 -->"; }
930 /* private */ function nextItem( $char )
932 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
933 else if ( ":" == $char ||
";" == $char ) {
935 if ( $this->mDTopen
) { $close = "</dt>"; }
936 if ( ";" == $char ) {
937 $this->mDTopen
= true;
938 return $close . "<dt>";
940 $this->mDTopen
= false;
941 return $close . "<dd>";
944 return "<!-- ERR 2 -->";
947 /* private */function closeList( $char )
949 if ( "*" == $char ) { $text = "</li></ul>"; }
950 else if ( "#" == $char ) { $text = "</li></ol>"; }
951 else if ( ":" == $char ) {
952 if ( $this->mDTopen
) {
953 $this->mDTopen
= false;
954 $text = "</dt></dl>";
956 $text = "</dd></dl>";
959 else { return "<!-- ERR 3 -->"; }
963 /* private */ function doBlockLevels( $text, $linestart )
965 $fname = "Parser::doBlockLevels";
966 wfProfileIn( $fname );
967 # Parsing through the text line by line. The main thing
968 # happening here is handling of block-level elements p, pre,
969 # and making lists from lines starting with * # : etc.
971 $a = explode( "\n", $text );
972 $text = $lastPref = "";
973 $this->mDTopen
= $inBlockElem = false;
975 if ( ! $linestart ) { $text .= array_shift( $a ); }
976 foreach ( $a as $t ) {
977 if ( "" != $text ) { $text .= "\n"; }
980 $opl = strlen( $lastPref );
981 $npl = strspn( $t, "*#:;" );
982 $pref = substr( $t, 0, $npl );
983 $pref2 = str_replace( ";", ":", $pref );
984 $t = substr( $t, $npl );
986 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
987 $text .= $this->nextItem( substr( $pref, -1 ) );
989 if ( ";" == substr( $pref, -1 ) ) {
990 $cpos = strpos( $t, ":" );
991 if ( ! ( false === $cpos ) ) {
992 $term = substr( $t, 0, $cpos );
993 $text .= $term . $this->nextItem( ":" );
994 $t = substr( $t, $cpos +
1 );
997 } else if (0 != $npl ||
0 != $opl) {
998 $cpl = $this->getCommon( $pref, $lastPref );
1000 while ( $cpl < $opl ) {
1001 $text .= $this->closeList( $lastPref{$opl-1} );
1004 if ( $npl <= $cpl && $cpl > 0 ) {
1005 $text .= $this->nextItem( $pref{$cpl-1} );
1007 while ( $npl > $cpl ) {
1008 $char = substr( $pref, $cpl, 1 );
1009 $text .= $this->openList( $char );
1011 if ( ";" == $char ) {
1012 $cpos = strpos( $t, ":" );
1013 if ( ! ( false === $cpos ) ) {
1014 $term = substr( $t, 0, $cpos );
1015 $text .= $term . $this->nextItem( ":" );
1016 $t = substr( $t, $cpos +
1 );
1023 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1025 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6)/i", $t ) ) {
1026 $text .= $this->closeParagraph();
1027 $inBlockElem = true;
1029 if ( ! $inBlockElem ) {
1030 if ( " " == $t{0} ) {
1031 $newSection = "pre";
1032 # $t = wfEscapeHTML( $t );
1034 else { $newSection = "p"; }
1036 if ( 0 == strcmp( "", trim( $oLine ) ) ) {
1037 $text .= $this->closeParagraph();
1038 $text .= "<" . $newSection . ">";
1039 } else if ( 0 != strcmp( $this->mLastSection
,
1041 $text .= $this->closeParagraph();
1042 if ( 0 != strcmp( "p", $newSection ) ) {
1043 $text .= "<" . $newSection . ">";
1046 $this->mLastSection
= $newSection;
1048 if ( $inBlockElem &&
1049 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6)/i", $t ) ) {
1050 $inBlockElem = false;
1056 $text .= $this->closeList( $pref2{$npl-1} );
1059 if ( "" != $this->mLastSection
) {
1060 if ( "p" != $this->mLastSection
) {
1061 $text .= "</" . $this->mLastSection
. ">";
1063 $this->mLastSection
= "";
1065 wfProfileOut( $fname );
1069 function getVariableValue( $index ) {
1073 case MAG_CURRENTMONTH
:
1075 case MAG_CURRENTMONTHNAME
:
1076 return $wgLang->getMonthName( date("n") );
1077 case MAG_CURRENTMONTHNAMEGEN
:
1078 return $wgLang->getMonthNameGen( date("n") );
1079 case MAG_CURRENTDAY
:
1081 case MAG_CURRENTDAYNAME
:
1082 return $wgLang->getWeekdayName( date("w")+
1 );
1083 case MAG_CURRENTYEAR
:
1085 case MAG_CURRENTTIME
:
1086 return $wgLang->time( wfTimestampNow(), false );
1087 case MAG_NUMBEROFARTICLES
:
1088 return wfNumberOfArticles();
1094 function initialiseVariables()
1096 global $wgVariableIDs;
1097 $this->mVariables
= array();
1099 foreach ( $wgVariableIDs as $id ) {
1100 $mw =& MagicWord
::get( $id );
1101 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1105 /* private */ function replaceVariables( $text )
1107 global $wgLang, $wgCurParser;
1108 global $wgScript, $wgArticlePath;
1110 $fname = "Parser::replaceVariables";
1111 wfProfileIn( $fname );
1114 if ( !$this->mVariables
) {
1115 $this->initialiseVariables();
1117 $titleChars = Title
::legalChars();
1118 $regex = "/{{([$titleChars]*?)}}/s";
1120 # "Recursive" variable expansion: run it through a couple of passes
1121 for ( $i=0; $i<MAX_INCLUDE_REPEAT
&& !$bail; $i++
) {
1124 # It's impossible to rebind a global in PHP
1125 # Instead, we run the substitution on a copy, then merge the changed fields back in
1126 $wgCurParser = $this->fork();
1128 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1129 if ( $oldText == $text ) {
1132 $this->merge( $wgCurParser );
1138 # Returns a copy of this object except with various variables cleared
1139 # This copy can be re-merged with the parent after operations on the copy
1143 $copy->mOutput
= new ParserOutput
;
1147 # Merges a copy split off with fork()
1148 function merge( &$copy )
1150 $this->mOutput
->merge( $copy->mOutput
);
1152 # Merge include throttling arrays
1153 foreach( $copy->mIncludeCount
as $dbk => $count ) {
1154 if ( array_key_exists( $dbk, $this->mIncludeCount
) ) {
1155 $this->mIncludeCount
[$dbk] +
= $count;
1157 $this->mIncludeCount
[$dbk] = $count;
1162 function braceSubstitution( $matches )
1164 global $wgLinkCache;
1165 $fname = "Parser::braceSubstitution";
1169 $text = $matches[1];
1172 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1173 if ( $mwSubst->matchStartAndRemove( $text ) ) {
1174 if ( $this->mOutputType
== OT_HTML
) {
1175 # Invalid SUBST not replaced at PST time
1176 # Return without further processing
1177 $text = $matches[0];
1180 } elseif ( $this->mOutputType
== OT_WIKI
) {
1181 # SUBST not found in PST pass, do nothing
1182 $text = $matches[0];
1189 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1190 if ( $mwMsgnw->matchStartAndRemove( $text ) ) {
1193 # Remove obsolete MSG:
1194 $mwMsg =& MagicWord
::get( MAG_MSG
);
1195 $mwMsg->matchStartAndRemove( $text );
1198 # Check if it is an internal message
1199 $mwInt =& MagicWord
::get( MAG_INT
);
1200 if ( $mwInt->matchStartAndRemove( $text ) ) {
1201 $text = wfMsg( $text );
1206 # Check for a match against internal variables
1207 if ( !$found && array_key_exists( $text, $this->mVariables
) ) {
1208 $text = $this->mVariables
[$text];
1210 $this->mOutput
->mContainsOldMagic
= true;
1213 # Load from database
1215 $title = Title
::newFromText( $text, NS_TEMPLATE
);
1216 if ( !is_null( $text ) && !$title->isExternal() ) {
1217 # Check for excessive inclusion
1218 $dbk = $title->getPrefixedDBkey();
1219 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1220 $this->mIncludeCount
[$dbk] = 0;
1222 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1223 $row = wfGetArray( "cur", array("cur_text"), array(
1224 "cur_namespace" => $title->getNamespace(),
1225 "cur_title" => $title->getDBkey() ), $fname );
1226 if ( $row !== false ) {
1228 $text = $row->cur_text
;
1230 # Escaping and link table handling
1231 # Not required for preSaveTransform()
1232 if ( $this->mOutputType
== OT_HTML
) {
1234 $text = wfEscapeWikiText( $text );
1236 $text = $this->removeHTMLtags( $text );
1238 $wgLinkCache->suspend();
1239 $text = $this->replaceInternalLinks( $text );
1240 $wgLinkCache->resume();
1241 $wgLinkCache->addLinkObj( $title );
1247 # If the title is valid but undisplayable, make a link to it
1248 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1249 $text = "[[" . $title->getPrefixedText() . "]]";
1262 # Cleans up HTML, removes dangerous tags and attributes
1263 /* private */ function removeHTMLtags( $text )
1265 $fname = "Parser::removeHTMLtags";
1266 wfProfileIn( $fname );
1267 $htmlpairs = array( # Tags that must be closed
1268 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1269 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1270 "strike", "strong", "tt", "var", "div", "center",
1271 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1272 "ruby", "rt" , "rb" , "rp"
1274 $htmlsingle = array(
1275 "br", "p", "hr", "li", "dt", "dd"
1277 $htmlnest = array( # Tags that can be nested--??
1278 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1279 "dl", "font", "big", "small", "sub", "sup"
1281 $tabletags = array( # Can only appear inside table
1285 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1286 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1288 $htmlattrs = $this->getHTMLattrs () ;
1290 # Remove HTML comments
1291 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1293 $bits = explode( "<", $text );
1294 $text = array_shift( $bits );
1295 $tagstack = array(); $tablestack = array();
1297 foreach ( $bits as $x ) {
1298 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1299 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1301 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1302 error_reporting( $prev );
1305 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1309 if ( ! in_array( $t, $htmlsingle ) &&
1310 ( $ot = array_pop( $tagstack ) ) != $t ) {
1311 array_push( $tagstack, $ot );
1314 if ( $t == "table" ) {
1315 $tagstack = array_pop( $tablestack );
1320 # Keep track for later
1321 if ( in_array( $t, $tabletags ) &&
1322 ! in_array( "table", $tagstack ) ) {
1324 } else if ( in_array( $t, $tagstack ) &&
1325 ! in_array ( $t , $htmlnest ) ) {
1327 } else if ( ! in_array( $t, $htmlsingle ) ) {
1328 if ( $t == "table" ) {
1329 array_push( $tablestack, $tagstack );
1330 $tagstack = array();
1332 array_push( $tagstack, $t );
1334 # Strip non-approved attributes from the tag
1335 $newparams = $this->fixTagAttributes($params);
1339 $rest = str_replace( ">", ">", $rest );
1340 $text .= "<$slash$t $newparams$brace$rest";
1344 $text .= "<" . str_replace( ">", ">", $x);
1346 # Close off any remaining tags
1347 while ( $t = array_pop( $tagstack ) ) {
1349 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1351 wfProfileOut( $fname );
1357 * This function accomplishes several tasks:
1358 * 1) Auto-number headings if that option is enabled
1359 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1360 * 3) Add a Table of contents on the top for users who have enabled the option
1361 * 4) Auto-anchor headings
1363 * It loops through all headlines, collects the necessary data, then splits up the
1364 * string and re-inserts the newly formatted headlines.
1367 /* private */ function formatHeadings( $text )
1369 $nh=$this->mOptions
->getNumberHeadings();
1370 $st=$this->mOptions
->getShowToc();
1371 if(!$this->mTitle
->userCanEdit()) {
1375 $es=$this->mOptions
->getEditSection();
1376 $esr=$this->mOptions
->getEditSectionOnRightClick();
1379 # Inhibit editsection links if requested in the page
1380 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1381 if ($esw->matchAndRemove( $text )) {
1384 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1386 $mw =& MagicWord
::get( MAG_NOTOC
);
1387 if ($mw->matchAndRemove( $text ))
1392 # never add the TOC to the Main Page. This is an entry page that should not
1393 # be more than 1-2 screens large anyway
1394 if($this->mTitle
->getPrefixedText()==wfMsg("mainpage")) {$st=0;}
1396 # We need this to perform operations on the HTML
1397 $sk =& $this->mOptions
->getSkin();
1399 # Get all headlines for numbering them and adding funky stuff like [edit]
1401 preg_match_all("/<H([1-6])(.*?>)(.*?)<\/H[1-6]>/i",$text,$matches);
1406 # Ugh .. the TOC should have neat indentation levels which can be
1407 # passed to the skin functions. These are determined here
1412 foreach($matches[3] as $headline) {
1413 if($level) { $prevlevel=$level;}
1414 $level=$matches[1][$c];
1415 if(($nh||
$st) && $prevlevel && $level>$prevlevel) {
1417 $h[$level]=0; // reset when we enter a new level
1418 $toc.=$sk->tocIndent($level-$prevlevel);
1419 $toclevel+
=$level-$prevlevel;
1422 if(($nh||
$st) && $level<$prevlevel) {
1423 $h[$level+
1]=0; // reset when we step back a level
1424 $toc.=$sk->tocUnindent($prevlevel-$level);
1425 $toclevel-=$prevlevel-$level;
1428 $h[$level]++
; // count number of headlines for each level
1431 for($i=1;$i<=$level;$i++
) {
1433 if($dot) {$numbering.=".";}
1440 // The canonized header is a version of the header text safe to use for links
1441 // Avoid insertion of weird stuff like <math> by expanding the relevant sections
1442 $canonized_headline=Parser
::unstrip( $headline, $this->mStripState
);
1443 $canonized_headline=preg_replace("/<.*?>/","",$canonized_headline); // strip out HTML
1444 $tocline = trim( $canonized_headline );
1445 $canonized_headline=str_replace('"',"",$canonized_headline);
1446 $canonized_headline=str_replace(" ","_",trim($canonized_headline));
1447 $refer[$c]=$canonized_headline;
1448 $refers[$canonized_headline]++
; // count how many in assoc. array so we can track dupes in anchors
1449 $refcount[$c]=$refers[$canonized_headline];
1451 // Prepend the number to the heading text
1454 $tocline=$numbering ." ". $tocline;
1456 // Don't number the heading if it is the only one (looks silly)
1457 if($nh && count($matches[3]) > 1) {
1458 $headline=$numbering . " " . $headline; // the two are different if the line contains a link
1462 // Create the anchor for linking from the TOC to the section
1463 $anchor=$canonized_headline;
1464 if($refcount[$c]>1) {$anchor.="_".$refcount[$c];}
1466 $toc.=$sk->tocLine($anchor,$tocline,$toclevel);
1469 $head[$c].=$sk->editSectionLink($c+
1);
1473 // the headline might have a link
1474 if(preg_match("/(.*)<a(.*)/",$headline, $headlinematches))
1476 // if so give an anchor name to the already existent link
1477 $headline = $headlinematches[1]
1478 ."<a name=\"".$anchor."\" ".$headlinematches[2];
1480 // else create an anchor link for the headline
1481 $headline = "<a name=\"".$anchor."\">"
1486 // give headline the correct <h#> tag
1487 $head[$c].="<h".$level.$matches[2][$c] .$headline."</h".$level.">";
1489 // Add the edit section link
1492 $head[$c]=$sk->editSectionScript($c+
1,$head[$c]);
1502 $toc.=$sk->tocUnindent($toclevel);
1503 $toc=$sk->tocTable($toc);
1506 // split up and insert constructed headlines
1508 $blocks=preg_split("/<H[1-6].*?>.*?<\/H[1-6]>/i",$text);
1511 foreach($blocks as $block) {
1512 if(($es) && $c>0 && $i==0) {
1513 # This is the [edit] link that appears for the top block of text when
1514 # section editing is enabled
1515 $full.=$sk->editSectionLink(0);
1518 if($st && $toclines>3 && !$i) {
1519 # Let's add a top anchor just in case we want to link to the top of the page
1520 $full="<a name=\"top\"></a>".$full.$toc;
1523 if( !empty( $head[$i] ) ) {
1532 /* private */ function doMagicISBN( &$tokenizer )
1536 # Check whether next token is a text token
1537 # If yes, fetch it and convert the text into a
1538 # Special::BookSources link
1539 $token = $tokenizer->previewToken();
1540 while ( $token["type"] == "" )
1542 $tokenizer->nextToken();
1543 $token = $tokenizer->previewToken();
1545 if ( $token["type"] == "text" )
1547 $token = $tokenizer->nextToken();
1548 $x = $token["text"];
1549 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1551 $isbn = $blank = "" ;
1552 while ( " " == $x{0} ) {
1554 $x = substr( $x, 1 );
1556 while ( strstr( $valid, $x{0} ) != false ) {
1558 $x = substr( $x, 1 );
1560 $num = str_replace( "-", "", $isbn );
1561 $num = str_replace( " ", "", $num );
1564 $text = "ISBN $blank$x";
1566 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1567 $text = "<a href=\"" .
1568 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1569 "\" class=\"internal\">ISBN $isbn</a>";
1577 /* private */ function doMagicRFC( &$tokenizer )
1581 # Check whether next token is a text token
1582 # If yes, fetch it and convert the text into a
1583 # link to an RFC source
1584 $token = $tokenizer->previewToken();
1585 while ( $token["type"] == "" )
1587 $tokenizer->nextToken();
1588 $token = $tokenizer->previewToken();
1590 if ( $token["type"] == "text" )
1592 $token = $tokenizer->nextToken();
1593 $x = $token["text"];
1594 $valid = "0123456789";
1596 $rfc = $blank = "" ;
1597 while ( " " == $x{0} ) {
1599 $x = substr( $x, 1 );
1601 while ( strstr( $valid, $x{0} ) != false ) {
1603 $x = substr( $x, 1 );
1607 $text .= "RFC $blank$x";
1609 $url = wfmsg( "rfcurl" );
1610 $url = str_replace( "$1", $rfc, $url);
1611 $sk =& $this->mOptions
->getSkin();
1612 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1613 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1621 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1623 $this->mOptions
= $options;
1624 $this->mTitle
= $title;
1625 $this->mOutputType
= OT_WIKI
;
1627 if ( $clearState ) {
1628 $this->clearState();
1631 $stripState = false;
1632 $text = str_replace("\r\n", "\n", $text);
1633 $text = $this->strip( $text, $stripState, false );
1634 $text = $this->pstPass2( $text, $user );
1635 $text = $this->unstrip( $text, $stripState );
1639 /* private */ function pstPass2( $text, &$user )
1641 global $wgLang, $wgLocaltimezone, $wgCurParser;
1643 # Variable replacement
1644 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1645 $text = $this->replaceVariables( $text );
1649 $n = $user->getName();
1650 $k = $user->getOption( "nickname" );
1651 if ( "" == $k ) { $k = $n; }
1652 if(isset($wgLocaltimezone)) {
1653 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1655 /* Note: this is an ugly timezone hack for the European wikis */
1656 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1657 " (" . date( "T" ) . ")";
1658 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1660 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1661 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1662 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1663 Namespace::getUser() ) . ":$n|$k]]", $text );
1665 # Context links: [[|name]] and [[name (context)|]]
1667 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1668 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1669 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1670 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1672 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1673 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1674 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1675 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1676 # [[ns:page (cont)|]]
1678 $t = $this->mTitle
->getText();
1679 if ( preg_match( $conpat, $t, $m ) ) {
1682 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1683 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1684 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1686 if ( "" == $context ) {
1687 $text = preg_replace( $p2, "[[\\1]]", $text );
1689 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1693 $mw =& MagicWord::get( MAG_SUBST );
1694 $wgCurParser = $this->fork();
1695 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1696 $this->merge( $wgCurParser );
1699 # Trim trailing whitespace
1700 # MAG_END (__END__) tag allows for trailing
1701 # whitespace to be deliberately included
1702 $text = rtrim( $text );
1703 $mw =& MagicWord
::get( MAG_END
);
1704 $mw->matchAndRemove( $text );
1714 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1716 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1717 $containsOldMagic = false )
1719 $this->mText
= $text;
1720 $this->mLanguageLinks
= $languageLinks;
1721 $this->mCategoryLinks
= $categoryLinks;
1722 $this->mContainsOldMagic
= $containsOldMagic;
1725 function getText() { return $this->mText
; }
1726 function getLanguageLinks() { return $this->mLanguageLinks
; }
1727 function getCategoryLinks() { return $this->mCategoryLinks
; }
1728 function containsOldMagic() { return $this->mContainsOldMagic
; }
1729 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
1730 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
1731 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
1732 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
1734 function merge( $other ) {
1735 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
1736 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
1737 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
1744 # All variables are private
1745 var $mUseTeX; # Use texvc to expand <math> tags
1746 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1747 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1748 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1749 var $mAllowExternalImages; # Allow external images inline
1750 var $mSkin; # Reference to the preferred skin
1751 var $mDateFormat; # Date format index
1752 var $mEditSection; # Create "edit section" links
1753 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1754 var $mPrintable; # Generate printable output
1755 var $mNumberHeadings; # Automatically number headings
1756 var $mShowToc; # Show table of contents
1758 function getUseTeX() { return $this->mUseTeX
; }
1759 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
1760 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
1761 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
1762 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
1763 function getSkin() { return $this->mSkin
; }
1764 function getDateFormat() { return $this->mDateFormat
; }
1765 function getEditSection() { return $this->mEditSection
; }
1766 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
1767 function getPrintable() { return $this->mPrintable
; }
1768 function getNumberHeadings() { return $this->mNumberHeadings
; }
1769 function getShowToc() { return $this->mShowToc
; }
1771 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
1772 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
1773 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
1774 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
1775 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
1776 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
1777 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
1778 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
1779 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
1780 function setPrintable( $x ) { return wfSetVar( $this->mPrintable
, $x ); }
1781 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
1782 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
1784 /* static */ function newFromUser( &$user )
1786 $popts = new ParserOptions
;
1787 $popts->initialiseFromUser( &$user );
1791 function initialiseFromUser( &$userInput )
1793 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1795 if ( !$userInput ) {
1798 $user =& $userInput;
1801 $this->mUseTeX
= $wgUseTeX;
1802 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
1803 $this->mUseDynamicDates
= $wgUseDynamicDates;
1804 $this->mInterwikiMagic
= $wgInterwikiMagic;
1805 $this->mAllowExternalImages
= $wgAllowExternalImages;
1806 $this->mSkin
=& $user->getSkin();
1807 $this->mDateFormat
= $user->getOption( "date" );
1808 $this->mEditSection
= $user->getOption( "editsection" );
1809 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
1810 $this->mPrintable
= false;
1811 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
1812 $this->mShowToc
= $user->getOption( "showtoc" );
1818 # Regex callbacks, used in Parser::replaceVariables
1819 function wfBraceSubstitution( $matches )
1821 global $wgCurParser;
1822 return $wgCurParser->braceSubstitution( $matches );