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 );
44 define( "OT_MSG", 3 );
46 # prefix for escaping, used in two functions at least
47 define( "UNIQ_PREFIX", "NaodW29");
51 # Cleared with clearState():
52 var $mOutput, $mAutonumber, $mLastSection, $mDTopen, $mStripState = array();
53 var $mVariables, $mIncludeCount;
56 var $mOptions, $mTitle, $mOutputType;
65 $this->mOutput
= new ParserOutput
;
66 $this->mAutonumber
= 0;
67 $this->mLastSection
= "";
68 $this->mDTopen
= false;
69 $this->mVariables
= false;
70 $this->mIncludeCount
= array();
71 $this->mStripState
= array();
74 # First pass--just handle <nowiki> sections, pass the rest off
75 # to doWikiPass2() which does all the real work.
77 # Returns a ParserOutput
79 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
81 $fname = "Parser::parse";
82 wfProfileIn( $fname );
88 $this->mOptions
= $options;
89 $this->mTitle
=& $title;
90 $this->mOutputType
= OT_HTML
;
93 $text = $this->strip( $text, $this->mStripState
);
94 $text = $this->doWikiPass2( $text, $linestart );
95 $text = $this->unstrip( $text, $this->mStripState
);
97 $this->mOutput
->setText( $text );
98 wfProfileOut( $fname );
99 return $this->mOutput
;
102 /* static */ function getRandomString()
104 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
107 # Replaces all occurences of <$tag>content</$tag> in the text
108 # with a random marker and returns the new text. the output parameter
109 # $content will be an associative array filled with data on the form
110 # $unique_marker => content.
112 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
114 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
119 while ( "" != $text ) {
120 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
122 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
125 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
126 $marker = $rnd . sprintf("%08X", $n++
);
127 $content[$marker] = $q[0];
128 $stripped .= $marker;
135 # Strips <nowiki>, <pre> and <math>
136 # Returns the text, and fills an array with data needed in unstrip()
138 function strip( $text, &$state )
140 $render = ($this->mOutputType
== OT_HTML
);
141 $nowiki_content = array();
142 $hiero_content = array();
143 $math_content = array();
144 $pre_content = array();
146 # Replace any instances of the placeholders
147 $uniq_prefix = UNIQ_PREFIX
;
148 $text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
150 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
151 foreach( $nowiki_content as $marker => $content ){
153 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
155 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
159 if( $GLOBALS['wgUseWikiHiero'] ){
160 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
161 foreach( $hiero_content as $marker => $content ){
163 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
165 $hiero_content[$marker] = "<hiero>$content</hiero>";
170 if( $this->mOptions
->getUseTeX() ){
171 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
172 foreach( $math_content as $marker => $content ){
174 $math_content[$marker] = renderMath( $content );
176 $math_content[$marker] = "<math>$content</math>";
181 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
182 foreach( $pre_content as $marker => $content ){
184 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
186 $pre_content[$marker] = "<pre>$content</pre>";
190 # Must expand in reverse order, otherwise nested tags will be corrupted
191 $state = array( $pre_content, $math_content, $hiero_content, $nowiki_content );
195 function unstrip( $text, &$state )
197 foreach( $state as $content_dict ){
198 foreach( $content_dict as $marker => $content ){
199 $text = str_replace( $marker, $content, $text );
205 function categoryMagic ()
207 global $wgLang , $wgUser ;
208 if ( !$this->mOptions
->getUseCategoryMagic() ) return ;
209 $id = $this->mTitle
->getArticleID() ;
210 $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
211 $ti = $this->mTitle
->getText() ;
212 $ti = explode ( ":" , $ti , 2 ) ;
213 if ( $cat != $ti[0] ) return "" ;
214 $r = "<br break='all' />\n" ;
216 $articles = array() ;
217 $parents = array () ;
218 $children = array() ;
221 # $sk =& $this->mGetSkin();
222 $sk =& $wgUser->getSkin() ;
225 $sql1 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
226 $sql2 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
228 $res = wfQuery ( $sql1, DB_READ
) ;
229 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
231 $res = wfQuery ( $sql2, DB_READ
) ;
232 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
235 foreach ( $data AS $x )
237 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
238 if ( $t != "" ) $t .= ":" ;
239 $t .= $x->cur_title
;
241 $y = explode ( ":" , $t , 2 ) ;
242 if ( count ( $y ) == 2 && $y[0] == $cat ) {
243 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
245 array_push ( $articles , $sk->makeLink ( $t ) ) ;
248 wfFreeResult ( $res ) ;
251 if ( count ( $children ) > 0 )
253 asort ( $children ) ;
254 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
255 $r .= implode ( ", " , $children ) ;
259 if ( count ( $articles ) > 0 )
261 asort ( $articles ) ;
262 $h = wfMsg( "category_header", $ti[1] );
263 $r .= "<h2>{$h}</h2>\n" ;
264 $r .= implode ( ", " , $articles ) ;
271 function getHTMLattrs ()
273 $htmlattrs = array( # Allowed attributes--no scripting, etc.
274 "title", "align", "lang", "dir", "width", "height",
275 "bgcolor", "clear", /* BR */ "noshade", /* HR */
276 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
277 /* FONT */ "type", "start", "value", "compact",
278 /* For various lists, mostly deprecated but safe */
279 "summary", "width", "border", "frame", "rules",
280 "cellspacing", "cellpadding", "valign", "char",
281 "charoff", "colgroup", "col", "span", "abbr", "axis",
282 "headers", "scope", "rowspan", "colspan", /* Tables */
283 "id", "class", "name", "style" /* For CSS */
288 function fixTagAttributes ( $t )
290 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
291 $htmlattrs = $this->getHTMLattrs() ;
293 # Strip non-approved attributes from the tag
295 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
296 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
298 # Strip javascript "expression" from stylesheets. Brute force approach:
299 # If anythin offensive is found, all attributes of the HTML tag are dropped
302 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
303 wfMungeToUtf8( $t ) ) )
311 function doTableStuff ( $t )
313 $t = explode ( "\n" , $t ) ;
314 $td = array () ; # Is currently a td tag open?
315 $ltd = array () ; # Was it TD or TH?
316 $tr = array () ; # Is currently a tr tag open?
317 $ltr = array () ; # tr attributes
318 foreach ( $t AS $k => $x )
321 $fc = substr ( $x , 0 , 1 ) ;
322 if ( "{|" == substr ( $x , 0 , 2 ) )
324 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
325 array_push ( $td , false ) ;
326 array_push ( $ltd , "" ) ;
327 array_push ( $tr , false ) ;
328 array_push ( $ltr , "" ) ;
330 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
331 else if ( "|}" == substr ( $x , 0 , 2 ) )
334 $l = array_pop ( $ltd ) ;
335 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
336 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
340 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
342 $z = trim ( substr ( $x , 2 ) ) ;
343 $t[$k] = "<caption>{$z}</caption>\n" ;
345 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
347 $x = substr ( $x , 1 ) ;
348 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
350 $l = array_pop ( $ltd ) ;
351 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
352 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
355 array_push ( $tr , false ) ;
356 array_push ( $td , false ) ;
357 array_push ( $ltd , "" ) ;
358 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
360 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
362 if ( "|+" == substr ( $x , 0 , 2 ) )
365 $x = substr ( $x , 1 ) ;
367 $after = substr ( $x , 1 ) ;
368 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
369 $after = explode ( "||" , $after ) ;
371 foreach ( $after AS $theline )
376 $tra = array_pop ( $ltr ) ;
377 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
378 array_push ( $tr , true ) ;
379 array_push ( $ltr , "" ) ;
382 $l = array_pop ( $ltd ) ;
383 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
384 if ( $fc == "|" ) $l = "td" ;
385 else if ( $fc == "!" ) $l = "th" ;
386 else if ( $fc == "+" ) $l = "caption" ;
388 array_push ( $ltd , $l ) ;
389 $y = explode ( "|" , $theline , 2 ) ;
390 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
391 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
393 array_push ( $td , true ) ;
398 # Closing open td, tr && table
399 while ( count ( $td ) > 0 )
401 if ( array_pop ( $td ) ) $t[] = "</td>" ;
402 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
406 $t = implode ( "\n" , $t ) ;
407 # $t = $this->removeHTMLtags( $t );
411 # Well, OK, it's actually about 14 passes. But since all the
412 # hard lifting is done inside PHP's regex code, it probably
413 # wouldn't speed things up much to add a real parser.
415 function doWikiPass2( $text, $linestart )
417 $fname = "Parser::doWikiPass2";
418 wfProfileIn( $fname );
420 $text = $this->removeHTMLtags( $text );
421 $text = $this->replaceVariables( $text );
423 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
425 $text = $this->doHeadings( $text );
427 if($this->mOptions
->getUseDynamicDates()) {
428 global $wgDateFormatter;
429 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
432 $text = $this->replaceExternalLinks( $text );
433 $text = $this->doTokenizedParser ( $text );
435 $text = $this->doTableStuff ( $text ) ;
437 $text = $this->formatHeadings( $text );
439 $sk =& $this->mOptions
->getSkin();
440 $text = $sk->transformContent( $text );
442 "/<hr *>/i" => '<hr/>',
443 "/<br *>/i" => '<br/>',
444 "/<center *>/i"=>'<span style="text-align:center;">',
445 "/<\\/center *>/i" => '</span>'
447 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
449 # needs to be called last
450 $text = $this->doBlockLevels( $text, $linestart );
451 $text .= $this->categoryMagic () ;
453 wfProfileOut( $fname );
458 /* private */ function doHeadings( $text )
460 for ( $i = 6; $i >= 1; --$i ) {
461 $h = substr( "======", 0, $i );
462 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
463 "<h{$i}>\\1</h{$i}>\\2", $text );
468 # Note: we have to do external links before the internal ones,
469 # and otherwise take great care in the order of things here, so
470 # that we don't end up interpreting some URLs twice.
472 /* private */ function replaceExternalLinks( $text )
474 $fname = "Parser::replaceExternalLinks";
475 wfProfileIn( $fname );
476 $text = $this->subReplaceExternalLinks( $text, "http", true );
477 $text = $this->subReplaceExternalLinks( $text, "https", true );
478 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
479 $text = $this->subReplaceExternalLinks( $text, "irc", false );
480 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
481 $text = $this->subReplaceExternalLinks( $text, "news", false );
482 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
483 wfProfileOut( $fname );
487 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
489 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
490 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
492 # this is the list of separators that should be ignored if they
493 # are the last character of an URL but that should be included
494 # if they occur within the URL, e.g. "go to www.foo.com, where .."
495 # in this case, the last comma should not become part of the URL,
496 # but in "www.foo.com/123,2342,32.htm" it should.
498 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
499 $images = "gif|png|jpg|jpeg";
501 # PLEASE NOTE: The curly braces { } are not part of the regex,
502 # they are interpreted as part of the string (used to tell PHP
503 # that the content of the string should be inserted there).
504 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
505 "((?i){$images})([^{$uc}]|$)/";
507 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
508 $sk =& $this->mOptions
->getSkin();
510 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
511 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
512 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
514 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
515 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
516 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
518 $s = str_replace( $unique, $protocol, $s );
520 $a = explode( "[{$protocol}:", " " . $s );
521 $s = array_shift( $a );
522 $s = substr( $s, 1 );
524 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
525 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
527 foreach ( $a as $line ) {
528 if ( preg_match( $e1, $line, $m ) ) {
529 $link = "{$protocol}:{$m[1]}";
531 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
532 else { $text = wfEscapeHTML( $link ); }
533 } else if ( preg_match( $e2, $line, $m ) ) {
534 $link = "{$protocol}:{$m[1]}";
538 $s .= "[{$protocol}:" . $line;
541 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
544 # Expand the URL for printable version
545 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
547 $la = $sk->getExternalLinkAttributes( $link, $text );
548 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
554 /* private */ function handle3Quotes( &$state, $token )
556 if ( $state["strong"] !== false ) {
557 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
559 # ''' lala ''lala '''
560 $s = "</em></strong><em>";
564 $state["strong"] = FALSE;
567 $state["strong"] = $token["pos"];
572 /* private */ function handle2Quotes( &$state, $token )
574 if ( $state["em"] !== false ) {
575 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
577 # ''lala'''lala'' ....'''
578 $s = "</strong></em><strong>";
582 $state["em"] = FALSE;
585 $state["em"] = $token["pos"];
590 /* private */ function handle5Quotes( &$state, $token )
593 if ( $state["em"] !== false && $state["strong"] ) {
594 if ( $state["em"] < $state["strong"] ) {
595 $s .= "</strong></em>";
597 $s .= "</em></strong>";
599 $state["strong"] = $state["em"] = FALSE;
600 } elseif ( $state["em"] !== false ) {
601 $s .= "</em><strong>";
602 $state["em"] = FALSE;
603 $state["strong"] = $token["pos"];
604 } elseif ( $state["strong"] !== false ) {
605 $s .= "</strong><em>";
606 $state["strong"] = FALSE;
607 $state["em"] = $token["pos"];
608 } else { # not $em and not $strong
609 $s .= "<strong><em>";
610 $state["strong"] = $state["em"] = $token["pos"];
615 /* private */ function doTokenizedParser( $str )
617 global $wgLang; # for language specific parser hook
619 $tokenizer=Tokenizer
::newFromString( $str );
620 $tokenStack = array();
623 $state["em"] = FALSE;
624 $state["strong"] = FALSE;
628 # The tokenizer splits the text into tokens and returns them one by one.
629 # Every call to the tokenizer returns a new token.
630 while ( $token = $tokenizer->nextToken() )
632 switch ( $token["type"] )
635 # simple text with no further markup
636 $txt = $token["text"];
639 # remember the tag opened with 3 [
643 # FIXME : Treat orphaned open tags (stack not empty when text is over)
645 array_push( $tokenStack, $token );
652 # get text from stack, glue it together, and call the code to handle a
655 if ( count( $tokenStack ) == 0 )
657 # stack empty. Found a ]] without an opening [[
661 $lastToken = array_pop( $tokenStack );
662 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
664 if( !empty( $lastToken["text"] ) ) {
665 $linkText = $lastToken["text"] . $linkText;
667 $lastToken = array_pop( $tokenStack );
670 $txt = $linkText ."]]";
672 if( isset( $lastToken["text"] ) ) {
673 $prefix = $lastToken["text"];
677 $nextToken = $tokenizer->previewToken();
678 if ( $nextToken["type"] == "text" )
680 # Preview just looks at it. Now we have to fetch it.
681 $nextToken = $tokenizer->nextToken();
682 $txt .= $nextToken["text"];
684 $fakestate = $this->mStripState
;
685 $txt = $this->handleInternalLink( $this->unstrip($txt,$fakestate), $prefix );
687 # did the tag start with 3 [ ?
689 # show the first as text
695 $tagIsOpen = (count( $tokenStack ) != 0);
701 # This and the three next ones handle quotes
702 $txt = $this->handle3Quotes( $state, $token );
705 $txt = $this->handle2Quotes( $state, $token );
708 $txt = $this->handle5Quotes( $state, $token );
718 $txt = $this->doMagicRFC( $tokenizer );
725 $txt = $this->doMagicISBN( $tokenizer );
729 # Call language specific Hook.
730 $txt = $wgLang->processToken( $token, $tokenStack );
731 if ( NULL == $txt ) {
732 # An unkown token. Highlight.
733 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
734 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
738 # If we're parsing the interior of a link, don't append the interior to $s,
739 # but push it to the stack so it can be processed when a ]] token is found.
740 if ( $tagIsOpen && $txt != "" ) {
741 $token["type"] = "text";
742 $token["text"] = $txt;
743 array_push( $tokenStack, $token );
748 if ( count( $tokenStack ) != 0 )
750 # still objects on stack. opened [[ tag without closing ]] tag.
752 while ( $lastToken = array_pop( $tokenStack ) )
754 if ( $lastToken["type"] == "text" )
756 $txt = $lastToken["text"] . $txt;
758 $txt = $lastToken["type"] . $txt;
766 /* private */ function handleInternalLink( $line, $prefix )
768 global $wgLang, $wgLinkCache;
769 global $wgNamespacesWithSubpages, $wgLanguageCode;
770 static $fname = "Parser::handleInternalLink" ;
771 wfProfileIn( $fname );
773 wfProfileIn( "$fname-setup" );
775 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
776 $sk =& $this->mOptions
->getSkin();
778 # Match a link having the form [[namespace:link|alternate]]trail
780 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
781 # Match the end of a line for a word that's not followed by whitespace,
782 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
783 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
784 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
785 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
788 # Special and Media are pseudo-namespaces; no pages actually exist in them
789 static $image = FALSE;
790 static $special = FALSE;
791 static $media = FALSE;
792 static $category = FALSE;
793 if ( !$image ) { $image = Namespace::getImage(); }
794 if ( !$special ) { $special = Namespace::getSpecial(); }
795 if ( !$media ) { $media = Namespace::getMedia(); }
796 if ( !$category ) { $category = wfMsg ( "category" ) ; }
798 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
800 wfProfileOut( "$fname-setup" );
803 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
806 } else { # Invalid form; output directly
807 $s .= $prefix . "[[" . $line ;
813 :Foobar -- override special treatment of prefix (images, language links)
814 /Foobar -- convert to CurrentPage/Foobar
815 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
817 $c = substr($m[1],0,1);
818 $noforce = ($c != ":");
819 if( $c == "/" ) { # subpage
820 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
821 $m[1]=substr($m[1],1,strlen($m[1])-2);
824 $noslash=substr($m[1],1);
826 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
827 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
830 } # this might be changed for ugliness reasons
832 $link = $noslash; # no subpage allowed, use standard link
834 } elseif( $noforce ) { # no subpage
837 $link = substr( $m[1], 1 );
842 $nt = Title
::newFromText( $link );
844 $s .= $prefix . "[[" . $line;
847 $ns = $nt->getNamespace();
848 $iw = $nt->getInterWiki();
850 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
851 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
852 return (trim($s) == '')?
'': $s;
854 if( $ns == $image ) {
855 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
856 $wgLinkCache->addImageLinkObj( $nt );
860 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
861 ( strpos( $link, "#" ) == FALSE ) ) {
862 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
867 $catns = strtoupper ( $nt->getDBkey () ) ;
868 $catns = explode ( ":" , $catns ) ;
869 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
871 if ( $catns == strtoupper($category) && $this->mOptions
->getUseCategoryMagic() ) {
872 $t = explode ( ":" , $nt->getText() ) ;
874 $t = implode ( ":" , $t ) ;
875 $t = $wgLang->ucFirst ( $t ) ;
876 $nnt = Title
::newFromText ( $category.":".$t ) ;
877 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
878 $this->mOutput
->mCategoryLinks
[] = $t ;
879 $s .= $prefix . $trail ;
883 if( $ns == $media ) {
884 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
885 $wgLinkCache->addImageLinkObj( $nt );
887 } elseif( $ns == $special ) {
888 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
891 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
893 wfProfileOut( $fname );
897 # Some functions here used by doBlockLevels()
899 /* private */ function closeParagraph()
902 if ( '' != $this->mLastSection
) {
903 $result = "</" . $this->mLastSection
. ">";
905 $this->mLastSection
= "";
908 # getCommon() returns the length of the longest common substring
909 # of both arguments, starting at the beginning of both.
911 /* private */ function getCommon( $st1, $st2 )
913 $fl = strlen( $st1 );
914 $shorter = strlen( $st2 );
915 if ( $fl < $shorter ) { $shorter = $fl; }
917 for ( $i = 0; $i < $shorter; ++
$i ) {
918 if ( $st1{$i} != $st2{$i} ) { break; }
922 # These next three functions open, continue, and close the list
923 # element appropriate to the prefix character passed into them.
925 /* private */ function openList( $char )
927 $result = $this->closeParagraph();
929 if ( "*" == $char ) { $result .= "<ul><li>"; }
930 else if ( "#" == $char ) { $result .= "<ol><li>"; }
931 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
932 else if ( ";" == $char ) {
933 $result .= "<dl><dt>";
934 $this->mDTopen
= true;
936 else { $result = "<!-- ERR 1 -->"; }
941 /* private */ function nextItem( $char )
943 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
944 else if ( ":" == $char ||
";" == $char ) {
946 if ( $this->mDTopen
) { $close = "</dt>"; }
947 if ( ";" == $char ) {
948 $this->mDTopen
= true;
949 return $close . "<dt>";
951 $this->mDTopen
= false;
952 return $close . "<dd>";
955 return "<!-- ERR 2 -->";
958 /* private */function closeList( $char )
960 if ( "*" == $char ) { $text = "</li></ul>"; }
961 else if ( "#" == $char ) { $text = "</li></ol>"; }
962 else if ( ":" == $char ) {
963 if ( $this->mDTopen
) {
964 $this->mDTopen
= false;
965 $text = "</dt></dl>";
967 $text = "</dd></dl>";
970 else { return "<!-- ERR 3 -->"; }
974 /* private */ function doBlockLevels( $text, $linestart )
976 $fname = "Parser::doBlockLevels";
977 wfProfileIn( $fname );
978 # Parsing through the text line by line. The main thing
979 # happening here is handling of block-level elements p, pre,
980 # and making lists from lines starting with * # : etc.
982 $a = explode( "\n", $text );
983 $lastPref = $text = '';
984 $this->mDTopen
= $inBlockElem = false;
986 if ( ! $linestart ) { $text .= array_shift( $a ); }
987 foreach ( $a as $t ) {
988 if ( "" != $text ) { $text .= "\n"; }
991 $opl = strlen( $lastPref );
992 $npl = strspn( $t, "*#:;" );
993 $pref = substr( $t, 0, $npl );
994 $pref2 = str_replace( ";", ":", $pref );
995 $t = substr( $t, $npl );
997 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
998 $text .= $this->nextItem( substr( $pref, -1 ) );
1000 if ( ";" == substr( $pref, -1 ) ) {
1001 $cpos = strpos( $t, ":" );
1002 if ( ! ( false === $cpos ) ) {
1003 $term = substr( $t, 0, $cpos );
1004 $text .= $term . $this->nextItem( ":" );
1005 $t = substr( $t, $cpos +
1 );
1008 } else if (0 != $npl ||
0 != $opl) {
1009 $cpl = $this->getCommon( $pref, $lastPref );
1011 while ( $cpl < $opl ) {
1012 $text .= $this->closeList( $lastPref{$opl-1} );
1015 if ( $npl <= $cpl && $cpl > 0 ) {
1016 $text .= $this->nextItem( $pref{$cpl-1} );
1018 while ( $npl > $cpl ) {
1019 $char = substr( $pref, $cpl, 1 );
1020 $text .= $this->openList( $char );
1022 if ( ";" == $char ) {
1023 $cpos = strpos( $t, ":" );
1024 if ( ! ( false === $cpos ) ) {
1025 $term = substr( $t, 0, $cpos );
1026 $text .= $term . $this->nextItem( ":" );
1027 $t = substr( $t, $cpos +
1 );
1034 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1035 $uniq_prefix = UNIQ_PREFIX
;
1038 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div)/i", $t ) ) {
1039 $text .= $this->closeParagraph();
1040 $inBlockElem = true;
1041 } else if ( preg_match("/(<hr|<\\/td|".$uniq_prefix."-pre)/i", $t ) ) {
1042 $text .= $this->closeParagraph();
1043 $inBlockElem = false;
1045 if ( !$inBlockElem ) {
1046 if ( " " == $t{0} ) {
1047 $newSection = "pre";
1048 $text .= $this->closeParagraph();
1049 # $t = wfEscapeHTML( $t );
1051 else { $newSection = "p"; }
1053 if ( ( '' == trim( $oLine ) ) ||
( $this->mLastSection
== $newSection and $newSection != 'p' )) {
1054 $text .= $this->closeParagraph();
1055 $text .= "<" . $newSection . ">";
1056 $this->mLastSection
= $newSection;
1059 if ( $inBlockElem &&
1060 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|<\\/p<\\/div)/i", $t ) ) {
1061 $inBlockElem = false;
1067 $text .= $this->closeList( $pref2{$npl-1} );
1070 if ( "" != $this->mLastSection
) {
1071 $text .= "</" . $this->mLastSection
. ">";
1072 $this->mLastSection
= "";
1074 wfProfileOut( $fname );
1078 function getVariableValue( $index ) {
1079 global $wgLang, $wgSitename, $wgServer;
1082 case MAG_CURRENTMONTH
:
1084 case MAG_CURRENTMONTHNAME
:
1085 return $wgLang->getMonthName( date("n") );
1086 case MAG_CURRENTMONTHNAMEGEN
:
1087 return $wgLang->getMonthNameGen( date("n") );
1088 case MAG_CURRENTDAY
:
1090 case MAG_CURRENTDAYNAME
:
1091 return $wgLang->getWeekdayName( date("w")+
1 );
1092 case MAG_CURRENTYEAR
:
1094 case MAG_CURRENTTIME
:
1095 return $wgLang->time( wfTimestampNow(), false );
1096 case MAG_NUMBEROFARTICLES
:
1097 return wfNumberOfArticles();
1107 function initialiseVariables()
1109 global $wgVariableIDs;
1110 $this->mVariables
= array();
1111 foreach ( $wgVariableIDs as $id ) {
1112 $mw =& MagicWord
::get( $id );
1113 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1117 /* private */ function replaceVariables( $text )
1119 global $wgLang, $wgCurParser;
1120 global $wgScript, $wgArticlePath;
1122 $fname = "Parser::replaceVariables";
1123 wfProfileIn( $fname );
1126 if ( !$this->mVariables
) {
1127 $this->initialiseVariables();
1129 $titleChars = Title
::legalChars();
1130 $regex = "/{{([$titleChars\\|]*?)}}/s";
1132 # "Recursive" variable expansion: run it through a couple of passes
1133 for ( $i=0; $i<MAX_INCLUDE_REPEAT
&& !$bail; $i++
) {
1136 # It's impossible to rebind a global in PHP
1137 # Instead, we run the substitution on a copy, then merge the changed fields back in
1138 $wgCurParser = $this->fork();
1140 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1141 if ( $oldText == $text ) {
1144 $this->merge( $wgCurParser );
1150 # Returns a copy of this object except with various variables cleared
1151 # This copy can be re-merged with the parent after operations on the copy
1155 $copy->mOutput
= new ParserOutput
;
1159 # Merges a copy split off with fork()
1160 function merge( &$copy )
1162 $this->mOutput
->merge( $copy->mOutput
);
1164 # Merge include throttling arrays
1165 foreach( $copy->mIncludeCount
as $dbk => $count ) {
1166 if ( array_key_exists( $dbk, $this->mIncludeCount
) ) {
1167 $this->mIncludeCount
[$dbk] +
= $count;
1169 $this->mIncludeCount
[$dbk] = $count;
1174 function braceSubstitution( $matches )
1176 global $wgLinkCache, $wgLang;
1177 $fname = "Parser::braceSubstitution";
1181 $text = $matches[1];
1184 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1185 if ( $mwSubst->matchStartAndRemove( $text ) ) {
1186 if ( $this->mOutputType
!= OT_WIKI
) {
1187 # Invalid SUBST not replaced at PST time
1188 # Return without further processing
1189 $text = $matches[0];
1192 } elseif ( $this->mOutputType
== OT_WIKI
) {
1193 # SUBST not found in PST pass, do nothing
1194 $text = $matches[0];
1198 # MSG, MSGNW and INT
1201 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1202 if ( $mwMsgnw->matchStartAndRemove( $text ) ) {
1205 # Remove obsolete MSG:
1206 $mwMsg =& MagicWord
::get( MAG_MSG
);
1207 $mwMsg->matchStartAndRemove( $text );
1210 # Check if it is an internal message
1211 $mwInt =& MagicWord
::get( MAG_INT
);
1212 if ( $mwInt->matchStartAndRemove( $text ) ) {
1213 $text = wfMsg( $text );
1220 # Check for NS: (namespace expansion)
1221 $mwNs = MagicWord
::get( MAG_NS
);
1222 if ( $mwNs->matchStartAndRemove( $text ) ) {
1223 if ( intval( $text ) ) {
1224 $text = $wgLang->getNsText( intval( $text ) );
1227 $index = Namespace::getCanonicalIndex( strtolower( $text ) );
1228 if ( !is_null( $index ) ) {
1229 $text = $wgLang->getNsText( $index );
1236 # LOCALURL and LOCALURLE
1238 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1239 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1241 if ( $mwLocal->matchStartAndRemove( $text ) ) {
1242 $func = 'getLocalURL';
1243 } elseif ( $mwLocalE->matchStartAndRemove( $text ) ) {
1244 $func = 'escapeLocalURL';
1249 if ( $func !== '' ) {
1250 $args = explode( "|", $text );
1251 $n = count( $args );
1253 $title = Title
::newFromText( $args[0] );
1254 if ( !is_null( $title ) ) {
1256 $text = $title->$func( $args[1] );
1258 $text = $title->$func();
1266 # Check for a match against internal variables
1267 if ( !$found && array_key_exists( $text, $this->mVariables
) ) {
1268 $text = $this->mVariables
[$text];
1270 $this->mOutput
->mContainsOldMagic
= true;
1273 # Load from database
1275 $title = Title
::newFromText( $text, NS_TEMPLATE
);
1276 if ( is_object( $title ) && !$title->isExternal() ) {
1277 # Check for excessive inclusion
1278 $dbk = $title->getPrefixedDBkey();
1279 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1280 $this->mIncludeCount
[$dbk] = 0;
1282 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1283 $article = new Article( $title );
1284 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1285 if ( $articleContent !== false ) {
1287 $text = $articleContent;
1289 # Escaping and link table handling
1290 # Not required for preSaveTransform()
1291 if ( $this->mOutputType
== OT_HTML
) {
1293 $text = wfEscapeWikiText( $text );
1295 $text = $this->removeHTMLtags( $text );
1297 $wgLinkCache->suspend();
1298 $text = $this->doTokenizedParser( $text );
1299 $wgLinkCache->resume();
1300 $wgLinkCache->addLinkObj( $title );
1306 # If the title is valid but undisplayable, make a link to it
1307 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1308 $text = "[[" . $title->getPrefixedText() . "]]";
1321 # Cleans up HTML, removes dangerous tags and attributes
1322 /* private */ function removeHTMLtags( $text )
1324 $fname = "Parser::removeHTMLtags";
1325 wfProfileIn( $fname );
1326 $htmlpairs = array( # Tags that must be closed
1327 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1328 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1329 "strike", "strong", "tt", "var", "div", "center",
1330 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1331 "ruby", "rt" , "rb" , "rp", "p"
1333 $htmlsingle = array(
1334 "br", "hr", "li", "dt", "dd"
1336 $htmlnest = array( # Tags that can be nested--??
1337 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1338 "dl", "font", "big", "small", "sub", "sup"
1340 $tabletags = array( # Can only appear inside table
1344 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1345 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1347 $htmlattrs = $this->getHTMLattrs () ;
1349 # Remove HTML comments
1350 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1352 $bits = explode( "<", $text );
1353 $text = array_shift( $bits );
1354 $tagstack = array(); $tablestack = array();
1356 foreach ( $bits as $x ) {
1357 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1358 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1360 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1361 error_reporting( $prev );
1364 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1368 if ( ! in_array( $t, $htmlsingle ) &&
1369 ( $ot = array_pop( $tagstack ) ) != $t ) {
1370 array_push( $tagstack, $ot );
1373 if ( $t == "table" ) {
1374 $tagstack = array_pop( $tablestack );
1379 # Keep track for later
1380 if ( in_array( $t, $tabletags ) &&
1381 ! in_array( "table", $tagstack ) ) {
1383 } else if ( in_array( $t, $tagstack ) &&
1384 ! in_array ( $t , $htmlnest ) ) {
1386 } else if ( ! in_array( $t, $htmlsingle ) ) {
1387 if ( $t == "table" ) {
1388 array_push( $tablestack, $tagstack );
1389 $tagstack = array();
1391 array_push( $tagstack, $t );
1393 # Strip non-approved attributes from the tag
1394 $newparams = $this->fixTagAttributes($params);
1398 $rest = str_replace( ">", ">", $rest );
1399 $text .= "<$slash$t $newparams$brace$rest";
1403 $text .= "<" . str_replace( ">", ">", $x);
1405 # Close off any remaining tags
1406 while ( $t = array_pop( $tagstack ) ) {
1408 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1410 wfProfileOut( $fname );
1416 * This function accomplishes several tasks:
1417 * 1) Auto-number headings if that option is enabled
1418 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1419 * 3) Add a Table of contents on the top for users who have enabled the option
1420 * 4) Auto-anchor headings
1422 * It loops through all headlines, collects the necessary data, then splits up the
1423 * string and re-inserts the newly formatted headlines.
1427 /* private */ function formatHeadings( $text )
1430 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1431 $doShowToc = $this->mOptions
->getShowToc();
1432 if( !$this->mTitle
->userCanEdit() ) {
1434 $rightClickHack = 0;
1436 $showEditLink = $this->mOptions
->getEditSection();
1437 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1440 # Inhibit editsection links if requested in the page
1441 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1442 if( $esw->matchAndRemove( $text ) ) {
1445 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1447 $mw =& MagicWord
::get( MAG_NOTOC
);
1448 if( $mw->matchAndRemove( $text ) ) {
1452 # never add the TOC to the Main Page. This is an entry page that should not
1453 # be more than 1-2 screens large anyway
1454 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1458 # Get all headlines for numbering them and adding funky stuff like [edit]
1459 # links - this is for later, but we need the number of headlines right now
1460 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1462 # if there are fewer than 4 headlines in the article, do not show TOC
1463 if( $numMatches < 4 ) {
1467 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1468 # override above conditions and always show TOC
1469 $mw =& MagicWord
::get( MAG_FORCETOC
);
1470 if ($mw->matchAndRemove( $text ) ) {
1475 # We need this to perform operations on the HTML
1476 $sk =& $this->mOptions
->getSkin();
1481 # Ugh .. the TOC should have neat indentation levels which can be
1482 # passed to the skin functions. These are determined here
1487 $sublevelCount = array();
1490 foreach( $matches[3] as $headline ) {
1493 $prevlevel = $level;
1495 $level = $matches[1][$headlineCount];
1496 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1497 # reset when we enter a new level
1498 $sublevelCount[$level] = 0;
1499 $toc .= $sk->tocIndent( $level - $prevlevel );
1500 $toclevel +
= $level - $prevlevel;
1502 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1503 # reset when we step back a level
1504 $sublevelCount[$level+
1]=0;
1505 $toc .= $sk->tocUnindent( $prevlevel - $level );
1506 $toclevel -= $prevlevel - $level;
1508 # count number of headlines for each level
1509 @$sublevelCount[$level]++
;
1510 if( $doNumberHeadings ||
$doShowToc ) {
1512 for( $i = 1; $i <= $level; $i++
) {
1513 if( !empty( $sublevelCount[$i] ) ) {
1517 $numbering .= $sublevelCount[$i];
1523 # The canonized header is a version of the header text safe to use for links
1524 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1525 $canonized_headline = Parser
::unstrip( $headline, $this->mStripState
);
1528 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1529 $tocline = trim( $canonized_headline );
1530 $canonized_headline = preg_replace("/[ &\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1531 $refer[$headlineCount] = $canonized_headline;
1533 # count how many in assoc. array so we can track dupes in anchors
1534 @$refers[$canonized_headline]++
;
1535 $refcount[$headlineCount]=$refers[$canonized_headline];
1537 # Prepend the number to the heading text
1539 if( $doNumberHeadings ||
$doShowToc ) {
1540 $tocline = $numbering . " " . $tocline;
1542 # Don't number the heading if it is the only one (looks silly)
1543 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1544 # the two are different if the line contains a link
1545 $headline=$numbering . " " . $headline;
1549 # Create the anchor for linking from the TOC to the section
1550 $anchor = $canonized_headline;
1551 if($refcount[$headlineCount] > 1 ) {
1552 $anchor .= "_" . $refcount[$headlineCount];
1555 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1557 if( $showEditLink ) {
1558 if ( empty( $head[$headlineCount] ) ) {
1559 $head[$headlineCount] = "";
1561 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1564 # Add the edit section span
1565 if( $rightClickHack ) {
1566 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1569 # give headline the correct <h#> tag
1570 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1576 $toclines = $headlineCount;
1577 $toc .= $sk->tocUnindent( $toclevel );
1578 $toc = $sk->tocTable( $toc );
1581 # split up and insert constructed headlines
1583 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1586 foreach( $blocks as $block ) {
1587 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1588 # This is the [edit] link that appears for the top block of text when
1589 # section editing is enabled
1590 $full .= $sk->editSectionLink(0);
1593 if( $doShowToc && !$i) {
1594 # Top anchor now in skin
1598 if( !empty( $head[$i] ) ) {
1607 /* private */ function doMagicISBN( &$tokenizer )
1611 # Check whether next token is a text token
1612 # If yes, fetch it and convert the text into a
1613 # Special::BookSources link
1614 $token = $tokenizer->previewToken();
1615 while ( $token["type"] == "" )
1617 $tokenizer->nextToken();
1618 $token = $tokenizer->previewToken();
1620 if ( $token["type"] == "text" )
1622 $token = $tokenizer->nextToken();
1623 $x = $token["text"];
1624 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1626 $isbn = $blank = "" ;
1627 while ( " " == $x{0} ) {
1629 $x = substr( $x, 1 );
1631 while ( strstr( $valid, $x{0} ) != false ) {
1633 $x = substr( $x, 1 );
1635 $num = str_replace( "-", "", $isbn );
1636 $num = str_replace( " ", "", $num );
1639 $text = "ISBN $blank$x";
1641 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1642 $text = "<a href=\"" .
1643 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1644 "\" class=\"internal\">ISBN $isbn</a>";
1652 /* private */ function doMagicRFC( &$tokenizer )
1656 # Check whether next token is a text token
1657 # If yes, fetch it and convert the text into a
1658 # link to an RFC source
1659 $token = $tokenizer->previewToken();
1660 while ( $token["type"] == "" )
1662 $tokenizer->nextToken();
1663 $token = $tokenizer->previewToken();
1665 if ( $token["type"] == "text" )
1667 $token = $tokenizer->nextToken();
1668 $x = $token["text"];
1669 $valid = "0123456789";
1671 $rfc = $blank = "" ;
1672 while ( " " == $x{0} ) {
1674 $x = substr( $x, 1 );
1676 while ( strstr( $valid, $x{0} ) != false ) {
1678 $x = substr( $x, 1 );
1682 $text .= "RFC $blank$x";
1684 $url = wfmsg( "rfcurl" );
1685 $url = str_replace( "$1", $rfc, $url);
1686 $sk =& $this->mOptions
->getSkin();
1687 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1688 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1696 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1698 $this->mOptions
= $options;
1699 $this->mTitle
=& $title;
1700 $this->mOutputType
= OT_WIKI
;
1702 if ( $clearState ) {
1703 $this->clearState();
1706 $stripState = false;
1707 $text = str_replace("\r\n", "\n", $text);
1708 $text = $this->strip( $text, $stripState, false );
1709 $text = $this->pstPass2( $text, $user );
1710 $text = $this->unstrip( $text, $stripState );
1714 /* private */ function pstPass2( $text, &$user )
1716 global $wgLang, $wgLocaltimezone, $wgCurParser;
1718 # Variable replacement
1719 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1720 $text = $this->replaceVariables( $text );
1724 $n = $user->getName();
1725 $k = $user->getOption( "nickname" );
1726 if ( "" == $k ) { $k = $n; }
1727 if(isset($wgLocaltimezone)) {
1728 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1730 /* Note: this is an ugly timezone hack for the European wikis */
1731 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1732 " (" . date( "T" ) . ")";
1733 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1735 $text = preg_replace( "/~~~~~/", $d, $text );
1736 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1737 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1738 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1739 Namespace::getUser() ) . ":$n|$k]]", $text );
1741 # Context links: [[|name]] and [[name (context)|]]
1743 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1744 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1745 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1746 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1748 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1749 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1750 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1751 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1752 # [[ns:page (cont)|]]
1754 $t = $this->mTitle
->getText();
1755 if ( preg_match( $conpat, $t, $m ) ) {
1758 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1759 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1760 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1762 if ( "" == $context ) {
1763 $text = preg_replace( $p2, "[[\\1]]", $text );
1765 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1769 $mw =& MagicWord::get( MAG_SUBST );
1770 $wgCurParser = $this->fork();
1771 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1772 $this->merge( $wgCurParser );
1775 # Trim trailing whitespace
1776 # MAG_END (__END__) tag allows for trailing
1777 # whitespace to be deliberately included
1778 $text = rtrim( $text );
1779 $mw =& MagicWord
::get( MAG_END
);
1780 $mw->matchAndRemove( $text );
1785 # Set up some variables which are usually set up in parse()
1786 # so that an external function can call some class members with confidence
1787 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1789 $this->mTitle
=& $title;
1790 $this->mOptions
= $options;
1791 $this->mOutputType
= $outputType;
1792 if ( $clearState ) {
1793 $this->clearState();
1797 function transformMsg( $text, $options ) {
1799 static $executing = false;
1801 # Guard against infinite recursion
1807 $this->mTitle
= $wgTitle;
1808 $this->mOptions
= $options;
1809 $this->mOutputType
= OT_MSG
;
1810 $this->clearState();
1811 $text = $this->replaceVariables( $text );
1820 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1822 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1823 $containsOldMagic = false )
1825 $this->mText
= $text;
1826 $this->mLanguageLinks
= $languageLinks;
1827 $this->mCategoryLinks
= $categoryLinks;
1828 $this->mContainsOldMagic
= $containsOldMagic;
1831 function getText() { return $this->mText
; }
1832 function getLanguageLinks() { return $this->mLanguageLinks
; }
1833 function getCategoryLinks() { return $this->mCategoryLinks
; }
1834 function containsOldMagic() { return $this->mContainsOldMagic
; }
1835 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
1836 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
1837 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
1838 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
1840 function merge( $other ) {
1841 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
1842 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
1843 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
1850 # All variables are private
1851 var $mUseTeX; # Use texvc to expand <math> tags
1852 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1853 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1854 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1855 var $mAllowExternalImages; # Allow external images inline
1856 var $mSkin; # Reference to the preferred skin
1857 var $mDateFormat; # Date format index
1858 var $mEditSection; # Create "edit section" links
1859 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1860 var $mNumberHeadings; # Automatically number headings
1861 var $mShowToc; # Show table of contents
1863 function getUseTeX() { return $this->mUseTeX
; }
1864 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
1865 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
1866 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
1867 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
1868 function getSkin() { return $this->mSkin
; }
1869 function getDateFormat() { return $this->mDateFormat
; }
1870 function getEditSection() { return $this->mEditSection
; }
1871 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
1872 function getNumberHeadings() { return $this->mNumberHeadings
; }
1873 function getShowToc() { return $this->mShowToc
; }
1875 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
1876 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
1877 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
1878 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
1879 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
1880 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
1881 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
1882 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
1883 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
1884 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
1885 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
1887 /* static */ function newFromUser( &$user )
1889 $popts = new ParserOptions
;
1890 $popts->initialiseFromUser( &$user );
1894 function initialiseFromUser( &$userInput )
1896 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1898 if ( !$userInput ) {
1900 $user->setLoaded( true );
1902 $user =& $userInput;
1905 $this->mUseTeX
= $wgUseTeX;
1906 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
1907 $this->mUseDynamicDates
= $wgUseDynamicDates;
1908 $this->mInterwikiMagic
= $wgInterwikiMagic;
1909 $this->mAllowExternalImages
= $wgAllowExternalImages;
1910 $this->mSkin
=& $user->getSkin();
1911 $this->mDateFormat
= $user->getOption( "date" );
1912 $this->mEditSection
= $user->getOption( "editsection" );
1913 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
1914 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
1915 $this->mShowToc
= $user->getOption( "showtoc" );
1921 # Regex callbacks, used in Parser::replaceVariables
1922 function wfBraceSubstitution( $matches )
1924 global $wgCurParser;
1925 return $wgCurParser->braceSubstitution( $matches );