3 include_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 include_once('wikihiero.php');
11 # Processes wiki markup
13 # There are two main entry points into the Parser class: parse() and preSaveTransform().
14 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
17 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
19 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
21 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
22 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
25 # * only within ParserOptions
28 #----------------------------------------
29 # Variable substitution O(N^2) attack
30 #-----------------------------------------
31 # Without countermeasures, it would be possible to attack the parser by saving a page
32 # filled with a large number of inclusions of large pages. The size of the generated
33 # page would be proportional to the square of the input size. Hence, we limit the number
34 # of inclusions of any given page, thus bringing any attack back to O(N).
36 define( "MAX_INCLUDE_REPEAT", 5 );
38 # Recursion depth of variable/inclusion evaluation
39 define( "MAX_INCLUDE_PASSES", 3 );
41 # Allowed values for $mOutputType
42 define( "OT_HTML", 1 );
43 define( "OT_WIKI", 2 );
47 # Cleared with clearState():
48 var $mOutput, $mAutonumber, $mLastSection, $mDTopen, $mStripState = array();
49 var $mVariables, $mIncludeCount;
52 var $mOptions, $mTitle, $mOutputType;
61 $this->mOutput
= new ParserOutput
;
62 $this->mAutonumber
= 0;
63 $this->mLastSection
= "";
64 $this->mDTopen
= false;
65 $this->mVariables
= false;
66 $this->mIncludeCount
= array();
67 $this->mStripState
= array();
70 # First pass--just handle <nowiki> sections, pass the rest off
71 # to doWikiPass2() which does all the real work.
73 # Returns a ParserOutput
75 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
77 $fname = "Parser::parse";
78 wfProfileIn( $fname );
84 $this->mOptions
= $options;
85 $this->mTitle
=& $title;
86 $this->mOutputType
= OT_HTML
;
89 $text = $this->strip( $text, $this->mStripState
);
90 $text = $this->doWikiPass2( $text, $linestart );
91 $text = $this->unstrip( $text, $this->mStripState
);
93 $this->mOutput
->setText( $text );
94 wfProfileOut( $fname );
95 return $this->mOutput
;
98 /* static */ function getRandomString()
100 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
103 # Replaces all occurences of <$tag>content</$tag> in the text
104 # with a random marker and returns the new text. the output parameter
105 # $content will be an associative array filled with data on the form
106 # $unique_marker => content.
108 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
110 $rnd = $uniq_prefix . Parser
::getRandomString();
115 while ( "" != $text ) {
116 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
118 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
121 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
122 $marker = $rnd . sprintf("%08X", $n++
);
123 $content[$marker] = $q[0];
124 $stripped .= $marker;
131 # Strips <nowiki>, <pre> and <math>
132 # Returns the text, and fills an array with data needed in unstrip()
134 function strip( $text, &$state )
136 $render = ($this->mOutputType
== OT_HTML
);
137 $nowiki_content = array();
138 $hiero_content = array();
139 $math_content = array();
140 $pre_content = array();
142 # Replace any instances of the placeholders
143 $uniq_prefix = "NaodW29";
144 $text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
146 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
147 foreach( $nowiki_content as $marker => $content ){
149 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
151 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
155 if( $GLOBALS['wgUseWikiHiero'] ){
156 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
157 foreach( $hiero_content as $marker => $content ){
159 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
161 $hiero_content[$marker] = "<hiero>$content</hiero>";
166 if( $this->mOptions
->getUseTeX() ){
167 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
168 foreach( $math_content as $marker => $content ){
170 $math_content[$marker] = renderMath( $content );
172 $math_content[$marker] = "<math>$content</math>";
177 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
178 foreach( $pre_content as $marker => $content ){
180 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
182 $pre_content[$marker] = "<pre>$content</pre>";
186 # Must expand in reverse order, otherwise nested tags will be corrupted
187 $state = array( $pre_content, $math_content, $hiero_content, $nowiki_content );
191 function unstrip( $text, &$state )
193 foreach( $state as $content_dict ){
194 foreach( $content_dict as $marker => $content ){
195 $text = str_replace( $marker, $content, $text );
201 function categoryMagic ()
203 global $wgLang , $wgUser ;
204 if ( !$this->mOptions
->getUseCategoryMagic() ) return ;
205 $id = $this->mTitle
->getArticleID() ;
206 $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
207 $ti = $this->mTitle
->getText() ;
208 $ti = explode ( ":" , $ti , 2 ) ;
209 if ( $cat != $ti[0] ) return "" ;
210 $r = "<br break='all'/>\n" ;
212 $articles = array() ;
213 $parents = array () ;
214 $children = array() ;
217 # $sk =& $this->mGetSkin();
218 $sk =& $wgUser->getSkin() ;
222 $sql = "SELECT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
224 $sql = "SELECT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
227 $res = wfQuery ( $sql, DB_READ
) ;
228 while ( $x = wfFetchObject ( $res ) )
231 # $t->newFromDBkey ( $x->l_from ) ;
232 # $t = $t->getText() ;
233 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
234 if ( $t != "" ) $t .= ":" ;
235 $t .= $x->cur_title
;
237 $y = explode ( ":" , $t , 2 ) ;
238 if ( count ( $y ) == 2 && $y[0] == $cat ) {
239 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
241 array_push ( $articles , $sk->makeLink ( $t ) ) ;
244 wfFreeResult ( $res ) ;
247 if ( count ( $children ) > 0 )
249 asort ( $children ) ;
250 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
251 $r .= implode ( ", " , $children ) ;
255 if ( count ( $articles ) > 0 )
257 asort ( $articles ) ;
258 $h = wfMsg( "category_header", $ti[1] );
259 $r .= "<h2>{$h}</h2>\n" ;
260 $r .= implode ( ", " , $articles ) ;
267 function getHTMLattrs ()
269 $htmlattrs = array( # Allowed attributes--no scripting, etc.
270 "title", "align", "lang", "dir", "width", "height",
271 "bgcolor", "clear", /* BR */ "noshade", /* HR */
272 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
273 /* FONT */ "type", "start", "value", "compact",
274 /* For various lists, mostly deprecated but safe */
275 "summary", "width", "border", "frame", "rules",
276 "cellspacing", "cellpadding", "valign", "char",
277 "charoff", "colgroup", "col", "span", "abbr", "axis",
278 "headers", "scope", "rowspan", "colspan", /* Tables */
279 "id", "class", "name", "style" /* For CSS */
284 function fixTagAttributes ( $t )
286 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
287 $htmlattrs = $this->getHTMLattrs() ;
289 # Strip non-approved attributes from the tag
291 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
292 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
294 # Strip javascript "expression" from stylesheets. Brute force approach:
295 # If anythin offensive is found, all attributes of the HTML tag are dropped
298 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
299 wfMungeToUtf8( $t ) ) )
307 function doTableStuff ( $t )
309 $t = explode ( "\n" , $t ) ;
310 $td = array () ; # Is currently a td tag open?
311 $ltd = array () ; # Was it TD or TH?
312 $tr = array () ; # Is currently a tr tag open?
313 $ltr = array () ; # tr attributes
314 foreach ( $t AS $k => $x )
317 $fc = substr ( $x , 0 , 1 ) ;
318 if ( "{|" == substr ( $x , 0 , 2 ) )
320 $t[$k] = "<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
321 array_push ( $td , false ) ;
322 array_push ( $ltd , "" ) ;
323 array_push ( $tr , false ) ;
324 array_push ( $ltr , "" ) ;
326 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
327 else if ( "|}" == substr ( $x , 0 , 2 ) )
330 $l = array_pop ( $ltd ) ;
331 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
332 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
336 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
338 $z = trim ( substr ( $x , 2 ) ) ;
339 $t[$k] = "<caption>{$z}</caption>\n" ;
341 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
343 $x = substr ( $x , 1 ) ;
344 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
346 $l = array_pop ( $ltd ) ;
347 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
348 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
351 array_push ( $tr , false ) ;
352 array_push ( $td , false ) ;
353 array_push ( $ltd , "" ) ;
354 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
356 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
358 if ( "|+" == substr ( $x , 0 , 2 ) )
361 $x = substr ( $x , 1 ) ;
363 $after = substr ( $x , 1 ) ;
364 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
365 $after = explode ( "||" , $after ) ;
367 foreach ( $after AS $theline )
372 $tra = array_pop ( $ltr ) ;
373 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
374 array_push ( $tr , true ) ;
375 array_push ( $ltr , "" ) ;
378 $l = array_pop ( $ltd ) ;
379 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
380 if ( $fc == "|" ) $l = "TD" ;
381 else if ( $fc == "!" ) $l = "TH" ;
382 else if ( $fc == "+" ) $l = "CAPTION" ;
384 array_push ( $ltd , $l ) ;
385 $y = explode ( "|" , $theline , 2 ) ;
386 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
387 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
389 array_push ( $td , true ) ;
394 # Closing open td, tr && table
395 while ( count ( $td ) > 0 )
397 if ( array_pop ( $td ) ) $t[] = "</td>" ;
398 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
402 $t = implode ( "\n" , $t ) ;
403 # $t = $this->removeHTMLtags( $t );
407 # Well, OK, it's actually about 14 passes. But since all the
408 # hard lifting is done inside PHP's regex code, it probably
409 # wouldn't speed things up much to add a real parser.
411 function doWikiPass2( $text, $linestart )
413 $fname = "Parser::doWikiPass2";
414 wfProfileIn( $fname );
416 $text = $this->removeHTMLtags( $text );
417 $text = $this->replaceVariables( $text );
419 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
420 $text = str_replace ( "<HR>", "<hr/>", $text );
422 $text = $this->doHeadings( $text );
423 $text = $this->doBlockLevels( $text, $linestart );
425 if($this->mOptions
->getUseDynamicDates()) {
426 global $wgDateFormatter;
427 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
430 $text = $this->replaceExternalLinks( $text );
431 $text = $this->doTokenizedParser ( $text );
432 $text = $this->doTableStuff ( $text ) ;
434 $text = $this->formatHeadings( $text );
436 $sk =& $this->mOptions
->getSkin();
437 $text = $sk->transformContent( $text );
438 $text .= $this->categoryMagic () ;
440 wfProfileOut( $fname );
445 /* private */ function doHeadings( $text )
447 for ( $i = 6; $i >= 1; --$i ) {
448 $h = substr( "======", 0, $i );
449 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
450 "<h{$i}>\\1</h{$i}>\\2", $text );
455 # Note: we have to do external links before the internal ones,
456 # and otherwise take great care in the order of things here, so
457 # that we don't end up interpreting some URLs twice.
459 /* private */ function replaceExternalLinks( $text )
461 $fname = "Parser::replaceExternalLinks";
462 wfProfileIn( $fname );
463 $text = $this->subReplaceExternalLinks( $text, "http", true );
464 $text = $this->subReplaceExternalLinks( $text, "https", true );
465 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
466 $text = $this->subReplaceExternalLinks( $text, "irc", false );
467 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
468 $text = $this->subReplaceExternalLinks( $text, "news", false );
469 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
470 wfProfileOut( $fname );
474 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
476 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
477 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
479 # this is the list of separators that should be ignored if they
480 # are the last character of an URL but that should be included
481 # if they occur within the URL, e.g. "go to www.foo.com, where .."
482 # in this case, the last comma should not become part of the URL,
483 # but in "www.foo.com/123,2342,32.htm" it should.
485 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
486 $images = "gif|png|jpg|jpeg";
488 # PLEASE NOTE: The curly braces { } are not part of the regex,
489 # they are interpreted as part of the string (used to tell PHP
490 # that the content of the string should be inserted there).
491 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
492 "((?i){$images})([^{$uc}]|$)/";
494 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
495 $sk =& $this->mOptions
->getSkin();
497 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
498 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
499 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
501 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
502 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
503 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
505 $s = str_replace( $unique, $protocol, $s );
507 $a = explode( "[{$protocol}:", " " . $s );
508 $s = array_shift( $a );
509 $s = substr( $s, 1 );
511 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
512 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
514 foreach ( $a as $line ) {
515 if ( preg_match( $e1, $line, $m ) ) {
516 $link = "{$protocol}:{$m[1]}";
518 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
519 else { $text = wfEscapeHTML( $link ); }
520 } else if ( preg_match( $e2, $line, $m ) ) {
521 $link = "{$protocol}:{$m[1]}";
525 $s .= "[{$protocol}:" . $line;
528 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
531 # Expand the URL for printable version
532 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
534 $la = $sk->getExternalLinkAttributes( $link, $text );
535 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
541 /* private */ function handle3Quotes( &$state, $token )
543 if ( $state["strong"] ) {
544 if ( $state["em"] && $state["em"] > $state["strong"] )
546 # ''' lala ''lala '''
547 $s = "</em></strong><em>";
551 $state["strong"] = FALSE;
554 $state["strong"] = $token["pos"];
559 /* private */ function handle2Quotes( &$state, $token )
561 if ( $state["em"] ) {
562 if ( $state["strong"] && $state["strong"] > $state["em"] )
564 # ''lala'''lala'' ....'''
565 $s = "</strong></em><strong>";
569 $state["em"] = FALSE;
572 $state["em"] = $token["pos"];
577 /* private */ function handle5Quotes( &$state, $token )
580 if ( $state["em"] && $state["strong"] ) {
581 if ( $state["em"] < $state["strong"] ) {
582 $s .= "</strong></em>";
584 $s .= "</em></strong>";
586 $state["strong"] = $state["em"] = FALSE;
587 } elseif ( $state["em"] ) {
588 $s .= "</em><strong>";
589 $state["em"] = FALSE;
590 $state["strong"] = $token["pos"];
591 } elseif ( $state["strong"] ) {
592 $s .= "</strong><em>";
593 $state["strong"] = FALSE;
594 $state["em"] = $token["pos"];
595 } else { # not $em and not $strong
596 $s .= "<strong><em>";
597 $state["strong"] = $state["em"] = $token["pos"];
602 /* private */ function doTokenizedParser( $str )
604 global $wgLang; # for language specific parser hook
606 $tokenizer=Tokenizer
::newFromString( $str );
607 $tokenStack = array();
610 $state["em"] = FALSE;
611 $state["strong"] = FALSE;
615 # The tokenizer splits the text into tokens and returns them one by one.
616 # Every call to the tokenizer returns a new token.
617 while ( $token = $tokenizer->nextToken() )
620 switch ( $token["type"] )
623 # simple text with no further markup
624 $txt = $token["text"];
627 # remember the tag opened with 3 [
631 # FIXME : Treat orphaned open tags (stack not empty when text is over)
633 array_push( $tokenStack, $token );
640 # get text from stack, glue it together, and call the code to handle a
643 if ( count( $tokenStack ) == 0 )
645 # stack empty. Found a ]] without an opening [[
649 $lastToken = array_pop( $tokenStack );
650 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
652 if( !empty( $lastToken["text"] ) ) {
653 $linkText = $lastToken["text"] . $linkText;
655 $lastToken = array_pop( $tokenStack );
658 $txt = $linkText ."]]";
660 if( isset( $lastToken["text"] ) ) {
661 $prefix = $lastToken["text"];
665 $nextToken = $tokenizer->previewToken();
666 if ( $nextToken["type"] == "text" )
668 # Preview just looks at it. Now we have to fetch it.
669 $nextToken = $tokenizer->nextToken();
670 $txt .= $nextToken["text"];
672 $txt = $this->handleInternalLink( $txt, $prefix );
674 # did the tag start with 3 [ ?
676 # show the first as text
682 $tagIsOpen = (count( $tokenStack ) != 0);
688 # This and the three next ones handle quotes
689 $txt = $this->handle3Quotes( $state, $token );
692 $txt = $this->handle2Quotes( $state, $token );
695 $txt = $this->handle5Quotes( $state, $token );
705 $txt = $this->doMagicRFC( $tokenizer );
712 $txt = $this->doMagicISBN( $tokenizer );
716 # Call language specific Hook.
717 $txt = $wgLang->processToken( $token, $tokenStack );
718 if ( NULL == $txt ) {
719 # An unkown token. Highlight.
720 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
721 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
725 # If we're parsing the interior of a link, don't append the interior to $s,
726 # but push it to the stack so it can be processed when a ]] token is found.
727 if ( $tagIsOpen && $txt != "" ) {
728 $token["type"] = "text";
729 $token["text"] = $txt;
730 array_push( $tokenStack, $token );
735 if ( count( $tokenStack ) != 0 )
737 # still objects on stack. opened [[ tag without closing ]] tag.
739 while ( $lastToken = array_pop( $tokenStack ) )
741 if ( $lastToken["type"] == "text" )
743 $txt = $lastToken["text"] . $txt;
745 $txt = $lastToken["type"] . $txt;
753 /* private */ function handleInternalLink( $line, $prefix )
755 global $wgLang, $wgLinkCache;
756 global $wgNamespacesWithSubpages, $wgLanguageCode;
757 static $fname = "Parser::handleInternalLink" ;
758 wfProfileIn( $fname );
760 wfProfileIn( "$fname-setup" );
762 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
763 $sk =& $this->mOptions
->getSkin();
765 # Match a link having the form [[namespace:link|alternate]]trail
767 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
768 # Match the end of a line for a word that's not followed by whitespace,
769 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
770 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
771 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
772 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
775 # Special and Media are pseudo-namespaces; no pages actually exist in them
776 static $image = FALSE;
777 static $special = FALSE;
778 static $media = FALSE;
779 static $category = FALSE;
780 if ( !$image ) { $image = Namespace::getImage(); }
781 if ( !$special ) { $special = Namespace::getSpecial(); }
782 if ( !$media ) { $media = Namespace::getMedia(); }
783 if ( !$category ) { $category = wfMsg ( "category" ) ; }
785 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
787 wfProfileOut( "$fname-setup" );
790 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
793 } else { # Invalid form; output directly
794 $s .= $prefix . "[[" . $line ;
800 :Foobar -- override special treatment of prefix (images, language links)
801 /Foobar -- convert to CurrentPage/Foobar
802 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
804 $c = substr($m[1],0,1);
805 $noforce = ($c != ":");
806 if( $c == "/" ) { # subpage
807 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
808 $m[1]=substr($m[1],1,strlen($m[1])-2);
811 $noslash=substr($m[1],1);
813 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
814 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
817 } # this might be changed for ugliness reasons
819 $link = $noslash; # no subpage allowed, use standard link
821 } elseif( $noforce ) { # no subpage
824 $link = substr( $m[1], 1 );
829 $nt = Title
::newFromText( $link );
831 $s .= $prefix . "[[" . $line;
834 $ns = $nt->getNamespace();
835 $iw = $nt->getInterWiki();
837 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
838 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
839 $s .= $prefix . $trail;
842 if( $ns == $image ) {
843 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
844 $wgLinkCache->addImageLinkObj( $nt );
848 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
849 ( strpos( $link, "#" ) == FALSE ) ) {
850 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
855 $catns = strtoupper ( $nt->getDBkey () ) ;
856 $catns = explode ( ":" , $catns ) ;
857 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
859 if ( $catns == strtoupper($category) && $this->mOptions
->getUseCategoryMagic() ) {
860 $t = explode ( ":" , $nt->getText() ) ;
862 $t = implode ( ":" , $t ) ;
863 $t = $wgLang->ucFirst ( $t ) ;
864 $nnt = Title
::newFromText ( $category.":".$t ) ;
865 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
866 $this->mOutput
->mCategoryLinks
[] = $t ;
867 $s .= $prefix . $trail ;
870 if( $ns == $media ) {
871 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
872 $wgLinkCache->addImageLinkObj( $nt );
874 } elseif( $ns == $special ) {
875 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
878 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
880 wfProfileOut( $fname );
884 # Some functions here used by doBlockLevels()
886 /* private */ function closeParagraph()
889 if ( 0 != strcmp( "", $this->mLastSection
) ) {
890 $result = "</" . $this->mLastSection
. ">";
892 $this->mLastSection
= "";
895 # getCommon() returns the length of the longest common substring
896 # of both arguments, starting at the beginning of both.
898 /* private */ function getCommon( $st1, $st2 )
900 $fl = strlen( $st1 );
901 $shorter = strlen( $st2 );
902 if ( $fl < $shorter ) { $shorter = $fl; }
904 for ( $i = 0; $i < $shorter; ++
$i ) {
905 if ( $st1{$i} != $st2{$i} ) { break; }
909 # These next three functions open, continue, and close the list
910 # element appropriate to the prefix character passed into them.
912 /* private */ function openList( $char )
914 $result = $this->closeParagraph();
916 if ( "*" == $char ) { $result .= "<ul><li>"; }
917 else if ( "#" == $char ) { $result .= "<ol><li>"; }
918 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
919 else if ( ";" == $char ) {
920 $result .= "<dl><dt>";
921 $this->mDTopen
= true;
923 else { $result = "<!-- ERR 1 -->"; }
928 /* private */ function nextItem( $char )
930 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
931 else if ( ":" == $char ||
";" == $char ) {
933 if ( $this->mDTopen
) { $close = "</dt>"; }
934 if ( ";" == $char ) {
935 $this->mDTopen
= true;
936 return $close . "<dt>";
938 $this->mDTopen
= false;
939 return $close . "<dd>";
942 return "<!-- ERR 2 -->";
945 /* private */function closeList( $char )
947 if ( "*" == $char ) { $text = "</li></ul>"; }
948 else if ( "#" == $char ) { $text = "</li></ol>"; }
949 else if ( ":" == $char ) {
950 if ( $this->mDTopen
) {
951 $this->mDTopen
= false;
952 $text = "</dt></dl>";
954 $text = "</dd></dl>";
957 else { return "<!-- ERR 3 -->"; }
961 /* private */ function doBlockLevels( $text, $linestart )
963 $fname = "Parser::doBlockLevels";
964 wfProfileIn( $fname );
965 # Parsing through the text line by line. The main thing
966 # happening here is handling of block-level elements p, pre,
967 # and making lists from lines starting with * # : etc.
969 $a = explode( "\n", $text );
970 $text = $lastPref = "";
971 $this->mDTopen
= $inBlockElem = false;
973 if ( ! $linestart ) { $text .= array_shift( $a ); }
974 foreach ( $a as $t ) {
975 if ( "" != $text ) { $text .= "\n"; }
978 $opl = strlen( $lastPref );
979 $npl = strspn( $t, "*#:;" );
980 $pref = substr( $t, 0, $npl );
981 $pref2 = str_replace( ";", ":", $pref );
982 $t = substr( $t, $npl );
984 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
985 $text .= $this->nextItem( substr( $pref, -1 ) );
987 if ( ";" == substr( $pref, -1 ) ) {
988 $cpos = strpos( $t, ":" );
989 if ( ! ( false === $cpos ) ) {
990 $term = substr( $t, 0, $cpos );
991 $text .= $term . $this->nextItem( ":" );
992 $t = substr( $t, $cpos +
1 );
995 } else if (0 != $npl ||
0 != $opl) {
996 $cpl = $this->getCommon( $pref, $lastPref );
998 while ( $cpl < $opl ) {
999 $text .= $this->closeList( $lastPref{$opl-1} );
1002 if ( $npl <= $cpl && $cpl > 0 ) {
1003 $text .= $this->nextItem( $pref{$cpl-1} );
1005 while ( $npl > $cpl ) {
1006 $char = substr( $pref, $cpl, 1 );
1007 $text .= $this->openList( $char );
1009 if ( ";" == $char ) {
1010 $cpos = strpos( $t, ":" );
1011 if ( ! ( false === $cpos ) ) {
1012 $term = substr( $t, 0, $cpos );
1013 $text .= $term . $this->nextItem( ":" );
1014 $t = substr( $t, $cpos +
1 );
1021 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1023 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<p)/i", $t ) ) {
1024 $text .= $this->closeParagraph();
1025 $inBlockElem = true;
1027 if ( ! $inBlockElem ) {
1028 if ( " " == $t{0} ) {
1029 $newSection = "pre";
1030 # $t = wfEscapeHTML( $t );
1032 else { $newSection = "p"; }
1034 if ( 0 == strcmp( "", trim( $oLine ) ) ) {
1035 $text .= $this->closeParagraph();
1036 $text .= "<" . $newSection . ">";
1037 } else if ( 0 != strcmp( $this->mLastSection
,
1039 $text .= $this->closeParagraph();
1040 if ( 0 != strcmp( "p", $newSection ) ) {
1041 $text .= "<" . $newSection . ">";
1044 $this->mLastSection
= $newSection;
1046 if ( $inBlockElem &&
1047 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|<\\/p)/i", $t ) ) {
1048 $inBlockElem = false;
1054 $text .= $this->closeList( $pref2{$npl-1} );
1057 if ( "" != $this->mLastSection
) {
1058 if ( "p" != $this->mLastSection
) {
1059 $text .= "</" . $this->mLastSection
. ">";
1061 $this->mLastSection
= "";
1063 wfProfileOut( $fname );
1067 function getVariableValue( $index ) {
1071 case MAG_CURRENTMONTH
:
1073 case MAG_CURRENTMONTHNAME
:
1074 return $wgLang->getMonthName( date("n") );
1075 case MAG_CURRENTMONTHNAMEGEN
:
1076 return $wgLang->getMonthNameGen( date("n") );
1077 case MAG_CURRENTDAY
:
1079 case MAG_CURRENTDAYNAME
:
1080 return $wgLang->getWeekdayName( date("w")+
1 );
1081 case MAG_CURRENTYEAR
:
1083 case MAG_CURRENTTIME
:
1084 return $wgLang->time( wfTimestampNow(), false );
1085 case MAG_NUMBEROFARTICLES
:
1086 return wfNumberOfArticles();
1092 function initialiseVariables()
1094 global $wgVariableIDs;
1095 $this->mVariables
= array();
1096 foreach ( $wgVariableIDs as $id ) {
1097 $mw =& MagicWord
::get( $id );
1098 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1102 /* private */ function replaceVariables( $text )
1104 global $wgLang, $wgCurParser;
1105 global $wgScript, $wgArticlePath;
1107 $fname = "Parser::replaceVariables";
1108 wfProfileIn( $fname );
1111 if ( !$this->mVariables
) {
1112 $this->initialiseVariables();
1114 $titleChars = Title
::legalChars();
1115 $regex = "/{{([$titleChars]*?)}}/s";
1117 # "Recursive" variable expansion: run it through a couple of passes
1118 for ( $i=0; $i<MAX_INCLUDE_REPEAT
&& !$bail; $i++
) {
1121 # It's impossible to rebind a global in PHP
1122 # Instead, we run the substitution on a copy, then merge the changed fields back in
1123 $wgCurParser = $this->fork();
1125 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1126 if ( $oldText == $text ) {
1129 $this->merge( $wgCurParser );
1135 # Returns a copy of this object except with various variables cleared
1136 # This copy can be re-merged with the parent after operations on the copy
1140 $copy->mOutput
= new ParserOutput
;
1144 # Merges a copy split off with fork()
1145 function merge( &$copy )
1147 $this->mOutput
->merge( $copy->mOutput
);
1149 # Merge include throttling arrays
1150 foreach( $copy->mIncludeCount
as $dbk => $count ) {
1151 if ( array_key_exists( $dbk, $this->mIncludeCount
) ) {
1152 $this->mIncludeCount
[$dbk] +
= $count;
1154 $this->mIncludeCount
[$dbk] = $count;
1159 function braceSubstitution( $matches )
1161 global $wgLinkCache;
1162 $fname = "Parser::braceSubstitution";
1166 $text = $matches[1];
1169 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1170 if ( $mwSubst->matchStartAndRemove( $text ) ) {
1171 if ( $this->mOutputType
== OT_HTML
) {
1172 # Invalid SUBST not replaced at PST time
1173 # Return without further processing
1174 $text = $matches[0];
1177 } elseif ( $this->mOutputType
== OT_WIKI
) {
1178 # SUBST not found in PST pass, do nothing
1179 $text = $matches[0];
1186 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1187 if ( $mwMsgnw->matchStartAndRemove( $text ) ) {
1190 # Remove obsolete MSG:
1191 $mwMsg =& MagicWord
::get( MAG_MSG
);
1192 $mwMsg->matchStartAndRemove( $text );
1195 # Check if it is an internal message
1196 $mwInt =& MagicWord
::get( MAG_INT
);
1197 if ( $mwInt->matchStartAndRemove( $text ) ) {
1198 $text = wfMsg( $text );
1203 # Check for a match against internal variables
1204 if ( !$found && array_key_exists( $text, $this->mVariables
) ) {
1205 $text = $this->mVariables
[$text];
1207 $this->mOutput
->mContainsOldMagic
= true;
1210 # Load from database
1212 $title = Title
::newFromText( $text, NS_TEMPLATE
);
1213 if ( !is_null( $text ) && !$title->isExternal() ) {
1214 # Check for excessive inclusion
1215 $dbk = $title->getPrefixedDBkey();
1216 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1217 $this->mIncludeCount
[$dbk] = 0;
1219 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1220 $article = new Article( $title );
1221 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1222 if ( $articleContent !== false ) {
1224 $text = $articleContent;
1226 # Escaping and link table handling
1227 # Not required for preSaveTransform()
1228 if ( $this->mOutputType
== OT_HTML
) {
1230 $text = wfEscapeWikiText( $text );
1232 $text = $this->removeHTMLtags( $text );
1234 $wgLinkCache->suspend();
1235 $text = $this->doTokenizedParser( $text );
1236 $wgLinkCache->resume();
1237 $wgLinkCache->addLinkObj( $title );
1243 # If the title is valid but undisplayable, make a link to it
1244 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1245 $text = "[[" . $title->getPrefixedText() . "]]";
1258 # Cleans up HTML, removes dangerous tags and attributes
1259 /* private */ function removeHTMLtags( $text )
1261 $fname = "Parser::removeHTMLtags";
1262 wfProfileIn( $fname );
1263 $htmlpairs = array( # Tags that must be closed
1264 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1265 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1266 "strike", "strong", "tt", "var", "div", "center",
1267 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1268 "ruby", "rt" , "rb" , "rp", "p"
1270 $htmlsingle = array(
1271 "br", "hr", "li", "dt", "dd", "hr/"
1273 $htmlnest = array( # Tags that can be nested--??
1274 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1275 "dl", "font", "big", "small", "sub", "sup"
1277 $tabletags = array( # Can only appear inside table
1281 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1282 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1284 $htmlattrs = $this->getHTMLattrs () ;
1286 # Remove HTML comments
1287 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1289 $bits = explode( "<", $text );
1290 $text = array_shift( $bits );
1291 $tagstack = array(); $tablestack = array();
1293 foreach ( $bits as $x ) {
1294 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1295 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1297 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1298 error_reporting( $prev );
1301 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1305 if ( ! in_array( $t, $htmlsingle ) &&
1306 ( $ot = array_pop( $tagstack ) ) != $t ) {
1307 array_push( $tagstack, $ot );
1310 if ( $t == "table" ) {
1311 $tagstack = array_pop( $tablestack );
1316 # Keep track for later
1317 if ( in_array( $t, $tabletags ) &&
1318 ! in_array( "table", $tagstack ) ) {
1320 } else if ( in_array( $t, $tagstack ) &&
1321 ! in_array ( $t , $htmlnest ) ) {
1323 } else if ( ! in_array( $t, $htmlsingle ) ) {
1324 if ( $t == "table" ) {
1325 array_push( $tablestack, $tagstack );
1326 $tagstack = array();
1328 array_push( $tagstack, $t );
1330 # Strip non-approved attributes from the tag
1331 $newparams = $this->fixTagAttributes($params);
1335 $rest = str_replace( ">", ">", $rest );
1336 $text .= "<$slash$t $newparams$brace$rest";
1340 $text .= "<" . str_replace( ">", ">", $x);
1342 # Close off any remaining tags
1343 while ( $t = array_pop( $tagstack ) ) {
1345 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1347 wfProfileOut( $fname );
1353 * This function accomplishes several tasks:
1354 * 1) Auto-number headings if that option is enabled
1355 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1356 * 3) Add a Table of contents on the top for users who have enabled the option
1357 * 4) Auto-anchor headings
1359 * It loops through all headlines, collects the necessary data, then splits up the
1360 * string and re-inserts the newly formatted headlines.
1364 /* private */ function formatHeadings( $text )
1366 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1367 $doShowToc = $this->mOptions
->getShowToc();
1368 if( !$this->mTitle
->userCanEdit() ) {
1370 $rightClickHack = 0;
1372 $showEditLink = $this->mOptions
->getEditSection();
1373 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1376 # Inhibit editsection links if requested in the page
1377 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1378 if( $esw->matchAndRemove( $text ) ) {
1381 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1383 $mw =& MagicWord
::get( MAG_NOTOC
);
1384 if( $mw->matchAndRemove( $text ) ) {
1388 # never add the TOC to the Main Page. This is an entry page that should not
1389 # be more than 1-2 screens large anyway
1390 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1394 # We need this to perform operations on the HTML
1395 $sk =& $this->mOptions
->getSkin();
1397 # Get all headlines for numbering them and adding funky stuff like [edit]
1399 preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1404 # Ugh .. the TOC should have neat indentation levels which can be
1405 # passed to the skin functions. These are determined here
1410 $sublevelCount = array();
1413 foreach( $matches[3] as $headline ) {
1416 $prevlevel = $level;
1418 $level = $matches[1][$headlineCount];
1419 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1420 # reset when we enter a new level
1421 $sublevelCount[$level] = 0;
1422 $toc .= $sk->tocIndent( $level - $prevlevel );
1423 $toclevel +
= $level - $prevlevel;
1425 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1426 # reset when we step back a level
1427 $sublevelCount[$level+
1]=0;
1428 $toc .= $sk->tocUnindent( $prevlevel - $level );
1429 $toclevel -= $prevlevel - $level;
1431 # count number of headlines for each level
1432 @$sublevelCount[$level]++
;
1433 if( $doNumberHeadings ||
$doShowToc ) {
1435 for( $i = 1; $i <= $level; $i++
) {
1436 if( !empty( $sublevelCount[$i] ) ) {
1440 $numbering .= $sublevelCount[$i];
1446 # The canonized header is a version of the header text safe to use for links
1447 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1448 $canonized_headline = Parser
::unstrip( $headline, $this->mStripState
);
1451 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1453 $tocline = trim( $canonized_headline );
1454 $canonized_headline = str_replace( '"', "", $canonized_headline );
1455 $canonized_headline = str_replace( " ", "_", trim( $canonized_headline) );
1456 $refer[$headlineCount] = $canonized_headline;
1458 # count how many in assoc. array so we can track dupes in anchors
1459 @$refers[$canonized_headline]++
;
1460 $refcount[$headlineCount]=$refers[$canonized_headline];
1462 # Prepend the number to the heading text
1464 if( $doNumberHeadings ||
$doShowToc ) {
1465 $tocline = $numbering . " " . $tocline;
1467 # Don't number the heading if it is the only one (looks silly)
1468 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1469 # the two are different if the line contains a link
1470 $headline=$numbering . " " . $headline;
1474 # Create the anchor for linking from the TOC to the section
1475 $anchor = $canonized_headline;
1476 if($refcount[$headlineCount] > 1 ) {
1477 $anchor .= "_" . $refcount[$headlineCount];
1480 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1482 if( $showEditLink ) {
1483 if ( empty( $head[$headlineCount] ) ) {
1484 $head[$headlineCount] = "";
1486 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1490 # the headline might have a link
1491 if( preg_match( "/(.*)<a(.*)/", $headline, $headlinematches ) ) {
1492 # if so give an anchor name to the already existent link
1493 $headline = $headlinematches[1]
1494 . "<a name=\"$anchor\" " . $headlinematches[2];
1496 # else create an anchor link for the headline
1497 $headline = "<a name=\"$anchor\">$headline</a>";
1500 # give headline the correct <h#> tag
1501 @$head[$headlineCount] .= "<h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1503 # Add the edit section link
1504 if( $rightClickHack ) {
1505 $head[$headlineCount] = $sk->editSectionScript($headlineCount+
1,$head[$headlineCount]);
1512 $toclines = $headlineCount;
1513 $toc .= $sk->tocUnindent( $toclevel );
1514 $toc = $sk->tocTable( $toc );
1517 # split up and insert constructed headlines
1519 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1522 foreach( $blocks as $block ) {
1523 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1524 # This is the [edit] link that appears for the top block of text when
1525 # section editing is enabled
1526 $full .= $sk->editSectionLink(0);
1529 if( $doShowToc && $toclines>3 && !$i) {
1530 # Let's add a top anchor just in case we want to link to the top of the page
1531 $full = "<a name=\"top\"></a>".$full.$toc;
1534 if( !empty( $head[$i] ) ) {
1543 /* private */ function doMagicISBN( &$tokenizer )
1547 # Check whether next token is a text token
1548 # If yes, fetch it and convert the text into a
1549 # Special::BookSources link
1550 $token = $tokenizer->previewToken();
1551 while ( $token["type"] == "" )
1553 $tokenizer->nextToken();
1554 $token = $tokenizer->previewToken();
1556 if ( $token["type"] == "text" )
1558 $token = $tokenizer->nextToken();
1559 $x = $token["text"];
1560 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1562 $isbn = $blank = "" ;
1563 while ( " " == $x{0} ) {
1565 $x = substr( $x, 1 );
1567 while ( strstr( $valid, $x{0} ) != false ) {
1569 $x = substr( $x, 1 );
1571 $num = str_replace( "-", "", $isbn );
1572 $num = str_replace( " ", "", $num );
1575 $text = "ISBN $blank$x";
1577 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1578 $text = "<a href=\"" .
1579 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1580 "\" class=\"internal\">ISBN $isbn</a>";
1588 /* private */ function doMagicRFC( &$tokenizer )
1592 # Check whether next token is a text token
1593 # If yes, fetch it and convert the text into a
1594 # link to an RFC source
1595 $token = $tokenizer->previewToken();
1596 while ( $token["type"] == "" )
1598 $tokenizer->nextToken();
1599 $token = $tokenizer->previewToken();
1601 if ( $token["type"] == "text" )
1603 $token = $tokenizer->nextToken();
1604 $x = $token["text"];
1605 $valid = "0123456789";
1607 $rfc = $blank = "" ;
1608 while ( " " == $x{0} ) {
1610 $x = substr( $x, 1 );
1612 while ( strstr( $valid, $x{0} ) != false ) {
1614 $x = substr( $x, 1 );
1618 $text .= "RFC $blank$x";
1620 $url = wfmsg( "rfcurl" );
1621 $url = str_replace( "$1", $rfc, $url);
1622 $sk =& $this->mOptions
->getSkin();
1623 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1624 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1632 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1634 $this->mOptions
= $options;
1635 $this->mTitle
=& $title;
1636 $this->mOutputType
= OT_WIKI
;
1638 if ( $clearState ) {
1639 $this->clearState();
1642 $stripState = false;
1643 $text = str_replace("\r\n", "\n", $text);
1644 $text = $this->strip( $text, $stripState, false );
1645 $text = $this->pstPass2( $text, $user );
1646 $text = $this->unstrip( $text, $stripState );
1650 /* private */ function pstPass2( $text, &$user )
1652 global $wgLang, $wgLocaltimezone, $wgCurParser;
1654 # Variable replacement
1655 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1656 $text = $this->replaceVariables( $text );
1660 $n = $user->getName();
1661 $k = $user->getOption( "nickname" );
1662 if ( "" == $k ) { $k = $n; }
1663 if(isset($wgLocaltimezone)) {
1664 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1666 /* Note: this is an ugly timezone hack for the European wikis */
1667 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1668 " (" . date( "T" ) . ")";
1669 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1671 $text = preg_replace( "/~~~~~/", $d, $text );
1672 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1673 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1674 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1675 Namespace::getUser() ) . ":$n|$k]]", $text );
1677 # Context links: [[|name]] and [[name (context)|]]
1679 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1680 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1681 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1682 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1684 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1685 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1686 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1687 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1688 # [[ns:page (cont)|]]
1690 $t = $this->mTitle
->getText();
1691 if ( preg_match( $conpat, $t, $m ) ) {
1694 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1695 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1696 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1698 if ( "" == $context ) {
1699 $text = preg_replace( $p2, "[[\\1]]", $text );
1701 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1705 $mw =& MagicWord::get( MAG_SUBST );
1706 $wgCurParser = $this->fork();
1707 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1708 $this->merge( $wgCurParser );
1711 # Trim trailing whitespace
1712 # MAG_END (__END__) tag allows for trailing
1713 # whitespace to be deliberately included
1714 $text = rtrim( $text );
1715 $mw =& MagicWord
::get( MAG_END
);
1716 $mw->matchAndRemove( $text );
1721 # Set up some variables which are usually set up in parse()
1722 # so that an external function can call some class members with confidence
1723 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1725 $this->mTitle
=& $title;
1726 $this->mOptions
= $options;
1727 $this->mOutputType
= $outputType;
1728 if ( $clearState ) {
1729 $this->clearState();
1736 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1738 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1739 $containsOldMagic = false )
1741 $this->mText
= $text;
1742 $this->mLanguageLinks
= $languageLinks;
1743 $this->mCategoryLinks
= $categoryLinks;
1744 $this->mContainsOldMagic
= $containsOldMagic;
1747 function getText() { return $this->mText
; }
1748 function getLanguageLinks() { return $this->mLanguageLinks
; }
1749 function getCategoryLinks() { return $this->mCategoryLinks
; }
1750 function containsOldMagic() { return $this->mContainsOldMagic
; }
1751 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
1752 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
1753 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
1754 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
1756 function merge( $other ) {
1757 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
1758 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
1759 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
1766 # All variables are private
1767 var $mUseTeX; # Use texvc to expand <math> tags
1768 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1769 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1770 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1771 var $mAllowExternalImages; # Allow external images inline
1772 var $mSkin; # Reference to the preferred skin
1773 var $mDateFormat; # Date format index
1774 var $mEditSection; # Create "edit section" links
1775 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1776 var $mNumberHeadings; # Automatically number headings
1777 var $mShowToc; # Show table of contents
1779 function getUseTeX() { return $this->mUseTeX
; }
1780 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
1781 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
1782 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
1783 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
1784 function getSkin() { return $this->mSkin
; }
1785 function getDateFormat() { return $this->mDateFormat
; }
1786 function getEditSection() { return $this->mEditSection
; }
1787 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
1788 function getNumberHeadings() { return $this->mNumberHeadings
; }
1789 function getShowToc() { return $this->mShowToc
; }
1791 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
1792 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
1793 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
1794 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
1795 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
1796 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
1797 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
1798 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
1799 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
1800 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
1801 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
1803 /* static */ function newFromUser( &$user )
1805 $popts = new ParserOptions
;
1806 $popts->initialiseFromUser( &$user );
1810 function initialiseFromUser( &$userInput )
1812 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1814 if ( !$userInput ) {
1817 $user =& $userInput;
1820 $this->mUseTeX
= $wgUseTeX;
1821 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
1822 $this->mUseDynamicDates
= $wgUseDynamicDates;
1823 $this->mInterwikiMagic
= $wgInterwikiMagic;
1824 $this->mAllowExternalImages
= $wgAllowExternalImages;
1825 $this->mSkin
=& $user->getSkin();
1826 $this->mDateFormat
= $user->getOption( "date" );
1827 $this->mEditSection
= $user->getOption( "editsection" );
1828 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
1829 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
1830 $this->mShowToc
= $user->getOption( "showtoc" );
1836 # Regex callbacks, used in Parser::replaceVariables
1837 function wfBraceSubstitution( $matches )
1839 global $wgCurParser;
1840 return $wgCurParser->braceSubstitution( $matches );