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 );
48 # Cleared with clearState():
49 var $mOutput, $mAutonumber, $mLastSection, $mDTopen, $mStripState = array();
50 var $mVariables, $mIncludeCount;
53 var $mOptions, $mTitle, $mOutputType;
62 $this->mOutput
= new ParserOutput
;
63 $this->mAutonumber
= 0;
64 $this->mLastSection
= "";
65 $this->mDTopen
= false;
66 $this->mVariables
= false;
67 $this->mIncludeCount
= array();
68 $this->mStripState
= array();
71 # First pass--just handle <nowiki> sections, pass the rest off
72 # to doWikiPass2() which does all the real work.
74 # Returns a ParserOutput
76 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
78 $fname = "Parser::parse";
79 wfProfileIn( $fname );
85 $this->mOptions
= $options;
86 $this->mTitle
=& $title;
87 $this->mOutputType
= OT_HTML
;
90 $text = $this->strip( $text, $this->mStripState
);
91 $text = $this->doWikiPass2( $text, $linestart );
92 $text = $this->unstrip( $text, $this->mStripState
);
94 $this->mOutput
->setText( $text );
95 wfProfileOut( $fname );
96 return $this->mOutput
;
99 /* static */ function getRandomString()
101 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
104 # Replaces all occurences of <$tag>content</$tag> in the text
105 # with a random marker and returns the new text. the output parameter
106 # $content will be an associative array filled with data on the form
107 # $unique_marker => content.
109 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
111 $rnd = $uniq_prefix . Parser
::getRandomString();
116 while ( "" != $text ) {
117 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
119 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) {
122 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
123 $marker = $rnd . sprintf("%08X", $n++
);
124 $content[$marker] = $q[0];
125 $stripped .= $marker;
132 # Strips <nowiki>, <pre> and <math>
133 # Returns the text, and fills an array with data needed in unstrip()
135 function strip( $text, &$state )
137 $render = ($this->mOutputType
== OT_HTML
);
138 $nowiki_content = array();
139 $hiero_content = array();
140 $math_content = array();
141 $pre_content = array();
143 # Replace any instances of the placeholders
144 $uniq_prefix = "NaodW29";
145 $text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
147 $text = Parser
::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
148 foreach( $nowiki_content as $marker => $content ){
150 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
152 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
156 if( $GLOBALS['wgUseWikiHiero'] ){
157 $text = Parser
::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
158 foreach( $hiero_content as $marker => $content ){
160 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML
);
162 $hiero_content[$marker] = "<hiero>$content</hiero>";
167 if( $this->mOptions
->getUseTeX() ){
168 $text = Parser
::extractTags("math", $text, $math_content, $uniq_prefix);
169 foreach( $math_content as $marker => $content ){
171 $math_content[$marker] = renderMath( $content );
173 $math_content[$marker] = "<math>$content</math>";
178 $text = Parser
::extractTags("pre", $text, $pre_content, $uniq_prefix);
179 foreach( $pre_content as $marker => $content ){
181 $pre_content[$marker] = "\n<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
183 $pre_content[$marker] = "\n<pre>$content</pre>";
187 # Must expand in reverse order, otherwise nested tags will be corrupted
188 $state = array( $pre_content, $math_content, $hiero_content, $nowiki_content );
192 function unstrip( $text, &$state )
194 foreach( $state as $content_dict ){
195 foreach( $content_dict as $marker => $content ){
196 $text = str_replace( $marker, $content, $text );
202 function categoryMagic ()
204 global $wgLang , $wgUser ;
205 if ( !$this->mOptions
->getUseCategoryMagic() ) return ;
206 $id = $this->mTitle
->getArticleID() ;
207 $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
208 $ti = $this->mTitle
->getText() ;
209 $ti = explode ( ":" , $ti , 2 ) ;
210 if ( $cat != $ti[0] ) return "" ;
211 $r = "<br break='all' />\n" ;
213 $articles = array() ;
214 $parents = array () ;
215 $children = array() ;
218 # $sk =& $this->mGetSkin();
219 $sk =& $wgUser->getSkin() ;
222 $sql1 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
223 $sql2 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
225 $res = wfQuery ( $sql1, DB_READ
) ;
226 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
228 $res = wfQuery ( $sql2, DB_READ
) ;
229 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
232 foreach ( $data AS $x )
234 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
235 if ( $t != "" ) $t .= ":" ;
236 $t .= $x->cur_title
;
238 $y = explode ( ":" , $t , 2 ) ;
239 if ( count ( $y ) == 2 && $y[0] == $cat ) {
240 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
242 array_push ( $articles , $sk->makeLink ( $t ) ) ;
245 wfFreeResult ( $res ) ;
248 if ( count ( $children ) > 0 )
250 asort ( $children ) ;
251 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
252 $r .= implode ( ", " , $children ) ;
256 if ( count ( $articles ) > 0 )
258 asort ( $articles ) ;
259 $h = wfMsg( "category_header", $ti[1] );
260 $r .= "<h2>{$h}</h2>\n" ;
261 $r .= implode ( ", " , $articles ) ;
268 function getHTMLattrs ()
270 $htmlattrs = array( # Allowed attributes--no scripting, etc.
271 "title", "align", "lang", "dir", "width", "height",
272 "bgcolor", "clear", /* BR */ "noshade", /* HR */
273 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
274 /* FONT */ "type", "start", "value", "compact",
275 /* For various lists, mostly deprecated but safe */
276 "summary", "width", "border", "frame", "rules",
277 "cellspacing", "cellpadding", "valign", "char",
278 "charoff", "colgroup", "col", "span", "abbr", "axis",
279 "headers", "scope", "rowspan", "colspan", /* Tables */
280 "id", "class", "name", "style" /* For CSS */
285 function fixTagAttributes ( $t )
287 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
288 $htmlattrs = $this->getHTMLattrs() ;
290 # Strip non-approved attributes from the tag
292 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
293 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
295 # Strip javascript "expression" from stylesheets. Brute force approach:
296 # If anythin offensive is found, all attributes of the HTML tag are dropped
299 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
300 wfMungeToUtf8( $t ) ) )
308 function doTableStuff ( $t )
310 $t = explode ( "\n" , $t ) ;
311 $td = array () ; # Is currently a td tag open?
312 $ltd = array () ; # Was it TD or TH?
313 $tr = array () ; # Is currently a tr tag open?
314 $ltr = array () ; # tr attributes
315 foreach ( $t AS $k => $x )
318 $fc = substr ( $x , 0 , 1 ) ;
319 if ( "{|" == substr ( $x , 0 , 2 ) )
321 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
322 array_push ( $td , false ) ;
323 array_push ( $ltd , "" ) ;
324 array_push ( $tr , false ) ;
325 array_push ( $ltr , "" ) ;
327 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
328 else if ( "|}" == substr ( $x , 0 , 2 ) )
331 $l = array_pop ( $ltd ) ;
332 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
333 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
337 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
339 $z = trim ( substr ( $x , 2 ) ) ;
340 $t[$k] = "<caption>{$z}</caption>\n" ;
342 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
344 $x = substr ( $x , 1 ) ;
345 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
347 $l = array_pop ( $ltd ) ;
348 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
349 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
352 array_push ( $tr , false ) ;
353 array_push ( $td , false ) ;
354 array_push ( $ltd , "" ) ;
355 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
357 else if ( "|" == $fc ||
"!" == $fc ||
"|+" == substr ( $x , 0 , 2 ) ) # Caption
359 if ( "|+" == substr ( $x , 0 , 2 ) )
362 $x = substr ( $x , 1 ) ;
364 $after = substr ( $x , 1 ) ;
365 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
366 $after = explode ( "||" , $after ) ;
368 foreach ( $after AS $theline )
373 $tra = array_pop ( $ltr ) ;
374 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
375 array_push ( $tr , true ) ;
376 array_push ( $ltr , "" ) ;
379 $l = array_pop ( $ltd ) ;
380 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
381 if ( $fc == "|" ) $l = "td" ;
382 else if ( $fc == "!" ) $l = "th" ;
383 else if ( $fc == "+" ) $l = "caption" ;
385 array_push ( $ltd , $l ) ;
386 $y = explode ( "|" , $theline , 2 ) ;
387 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
388 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
390 array_push ( $td , true ) ;
395 # Closing open td, tr && table
396 while ( count ( $td ) > 0 )
398 if ( array_pop ( $td ) ) $t[] = "</td>" ;
399 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
403 $t = implode ( "\n" , $t ) ;
404 # $t = $this->removeHTMLtags( $t );
408 # Well, OK, it's actually about 14 passes. But since all the
409 # hard lifting is done inside PHP's regex code, it probably
410 # wouldn't speed things up much to add a real parser.
412 function doWikiPass2( $text, $linestart )
414 $fname = "Parser::doWikiPass2";
415 wfProfileIn( $fname );
417 $text = $this->removeHTMLtags( $text );
418 $text = $this->replaceVariables( $text );
420 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
422 $text = $this->doHeadings( $text );
424 if($this->mOptions
->getUseDynamicDates()) {
425 global $wgDateFormatter;
426 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
429 $text = $this->replaceExternalLinks( $text );
430 $text = $this->doTokenizedParser ( $text );
431 $text = $this->doTableStuff ( $text ) ;
433 $text = $this->formatHeadings( $text );
435 $sk =& $this->mOptions
->getSkin();
436 $text = $sk->transformContent( $text );
438 "/<hr *>/i" => '<hr/>',
439 "/<br *>/i" => '<br/>',
440 "/<center *>/i"=>'<span style="text-align:center;">',
441 "/<\\/center *>/i" => '</span>'
443 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
444 $text = $this->doBlockLevels( $text, $linestart );
445 $text .= $this->categoryMagic () ;
447 wfProfileOut( $fname );
452 /* private */ function doHeadings( $text )
454 for ( $i = 6; $i >= 1; --$i ) {
455 $h = substr( "======", 0, $i );
456 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
457 "<h{$i}>\\1</h{$i}>\\2", $text );
462 # Note: we have to do external links before the internal ones,
463 # and otherwise take great care in the order of things here, so
464 # that we don't end up interpreting some URLs twice.
466 /* private */ function replaceExternalLinks( $text )
468 $fname = "Parser::replaceExternalLinks";
469 wfProfileIn( $fname );
470 $text = $this->subReplaceExternalLinks( $text, "http", true );
471 $text = $this->subReplaceExternalLinks( $text, "https", true );
472 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
473 $text = $this->subReplaceExternalLinks( $text, "irc", false );
474 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
475 $text = $this->subReplaceExternalLinks( $text, "news", false );
476 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
477 wfProfileOut( $fname );
481 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
483 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
484 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
486 # this is the list of separators that should be ignored if they
487 # are the last character of an URL but that should be included
488 # if they occur within the URL, e.g. "go to www.foo.com, where .."
489 # in this case, the last comma should not become part of the URL,
490 # but in "www.foo.com/123,2342,32.htm" it should.
492 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
493 $images = "gif|png|jpg|jpeg";
495 # PLEASE NOTE: The curly braces { } are not part of the regex,
496 # they are interpreted as part of the string (used to tell PHP
497 # that the content of the string should be inserted there).
498 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
499 "((?i){$images})([^{$uc}]|$)/";
501 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
502 $sk =& $this->mOptions
->getSkin();
504 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
505 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
506 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
508 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
509 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
510 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
512 $s = str_replace( $unique, $protocol, $s );
514 $a = explode( "[{$protocol}:", " " . $s );
515 $s = array_shift( $a );
516 $s = substr( $s, 1 );
518 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
519 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
521 foreach ( $a as $line ) {
522 if ( preg_match( $e1, $line, $m ) ) {
523 $link = "{$protocol}:{$m[1]}";
525 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
526 else { $text = wfEscapeHTML( $link ); }
527 } else if ( preg_match( $e2, $line, $m ) ) {
528 $link = "{$protocol}:{$m[1]}";
532 $s .= "[{$protocol}:" . $line;
535 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
538 # Expand the URL for printable version
539 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
541 $la = $sk->getExternalLinkAttributes( $link, $text );
542 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
548 /* private */ function handle3Quotes( &$state, $token )
550 if ( $state["strong"] ) {
551 if ( $state["em"] && $state["em"] > $state["strong"] )
553 # ''' lala ''lala '''
554 $s = "</em></strong><em>";
558 $state["strong"] = FALSE;
561 $state["strong"] = $token["pos"];
566 /* private */ function handle2Quotes( &$state, $token )
568 if ( $state["em"] ) {
569 if ( $state["strong"] && $state["strong"] > $state["em"] )
571 # ''lala'''lala'' ....'''
572 $s = "</strong></em><strong>";
576 $state["em"] = FALSE;
579 $state["em"] = $token["pos"];
584 /* private */ function handle5Quotes( &$state, $token )
587 if ( $state["em"] && $state["strong"] ) {
588 if ( $state["em"] < $state["strong"] ) {
589 $s .= "</strong></em>";
591 $s .= "</em></strong>";
593 $state["strong"] = $state["em"] = FALSE;
594 } elseif ( $state["em"] ) {
595 $s .= "</em><strong>";
596 $state["em"] = FALSE;
597 $state["strong"] = $token["pos"];
598 } elseif ( $state["strong"] ) {
599 $s .= "</strong><em>";
600 $state["strong"] = FALSE;
601 $state["em"] = $token["pos"];
602 } else { # not $em and not $strong
603 $s .= "<strong><em>";
604 $state["strong"] = $state["em"] = $token["pos"];
609 /* private */ function doTokenizedParser( $str )
611 global $wgLang; # for language specific parser hook
613 $tokenizer=Tokenizer
::newFromString( $str );
614 $tokenStack = array();
617 $state["em"] = FALSE;
618 $state["strong"] = FALSE;
622 # The tokenizer splits the text into tokens and returns them one by one.
623 # Every call to the tokenizer returns a new token.
624 while ( $token = $tokenizer->nextToken() )
627 switch ( $token["type"] )
630 # simple text with no further markup
631 $txt = $token["text"];
634 # remember the tag opened with 3 [
638 # FIXME : Treat orphaned open tags (stack not empty when text is over)
640 array_push( $tokenStack, $token );
647 # get text from stack, glue it together, and call the code to handle a
650 if ( count( $tokenStack ) == 0 )
652 # stack empty. Found a ]] without an opening [[
656 $lastToken = array_pop( $tokenStack );
657 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
659 if( !empty( $lastToken["text"] ) ) {
660 $linkText = $lastToken["text"] . $linkText;
662 $lastToken = array_pop( $tokenStack );
665 $txt = $linkText ."]]";
667 if( isset( $lastToken["text"] ) ) {
668 $prefix = $lastToken["text"];
672 $nextToken = $tokenizer->previewToken();
673 if ( $nextToken["type"] == "text" )
675 # Preview just looks at it. Now we have to fetch it.
676 $nextToken = $tokenizer->nextToken();
677 $txt .= $nextToken["text"];
679 $txt = $this->handleInternalLink( $txt, $prefix );
681 # did the tag start with 3 [ ?
683 # show the first as text
689 $tagIsOpen = (count( $tokenStack ) != 0);
695 # This and the three next ones handle quotes
696 $txt = $this->handle3Quotes( $state, $token );
699 $txt = $this->handle2Quotes( $state, $token );
702 $txt = $this->handle5Quotes( $state, $token );
712 $txt = $this->doMagicRFC( $tokenizer );
719 $txt = $this->doMagicISBN( $tokenizer );
723 # Call language specific Hook.
724 $txt = $wgLang->processToken( $token, $tokenStack );
725 if ( NULL == $txt ) {
726 # An unkown token. Highlight.
727 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
728 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
732 # If we're parsing the interior of a link, don't append the interior to $s,
733 # but push it to the stack so it can be processed when a ]] token is found.
734 if ( $tagIsOpen && $txt != "" ) {
735 $token["type"] = "text";
736 $token["text"] = $txt;
737 array_push( $tokenStack, $token );
742 if ( count( $tokenStack ) != 0 )
744 # still objects on stack. opened [[ tag without closing ]] tag.
746 while ( $lastToken = array_pop( $tokenStack ) )
748 if ( $lastToken["type"] == "text" )
750 $txt = $lastToken["text"] . $txt;
752 $txt = $lastToken["type"] . $txt;
760 /* private */ function handleInternalLink( $line, $prefix )
762 global $wgLang, $wgLinkCache;
763 global $wgNamespacesWithSubpages, $wgLanguageCode;
764 static $fname = "Parser::handleInternalLink" ;
765 wfProfileIn( $fname );
767 wfProfileIn( "$fname-setup" );
769 if ( !$tc ) { $tc = Title
::legalChars() . "#"; }
770 $sk =& $this->mOptions
->getSkin();
772 # Match a link having the form [[namespace:link|alternate]]trail
774 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
775 # Match the end of a line for a word that's not followed by whitespace,
776 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
777 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
778 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
779 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
782 # Special and Media are pseudo-namespaces; no pages actually exist in them
783 static $image = FALSE;
784 static $special = FALSE;
785 static $media = FALSE;
786 static $category = FALSE;
787 if ( !$image ) { $image = Namespace::getImage(); }
788 if ( !$special ) { $special = Namespace::getSpecial(); }
789 if ( !$media ) { $media = Namespace::getMedia(); }
790 if ( !$category ) { $category = wfMsg ( "category" ) ; }
792 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
794 wfProfileOut( "$fname-setup" );
797 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
800 } else { # Invalid form; output directly
801 $s .= $prefix . "[[" . $line ;
807 :Foobar -- override special treatment of prefix (images, language links)
808 /Foobar -- convert to CurrentPage/Foobar
809 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
811 $c = substr($m[1],0,1);
812 $noforce = ($c != ":");
813 if( $c == "/" ) { # subpage
814 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
815 $m[1]=substr($m[1],1,strlen($m[1])-2);
818 $noslash=substr($m[1],1);
820 if($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]) { # subpages allowed here
821 $link = $this->mTitle
->getPrefixedText(). "/" . trim($noslash);
824 } # this might be changed for ugliness reasons
826 $link = $noslash; # no subpage allowed, use standard link
828 } elseif( $noforce ) { # no subpage
831 $link = substr( $m[1], 1 );
836 $nt = Title
::newFromText( $link );
838 $s .= $prefix . "[[" . $line;
841 $ns = $nt->getNamespace();
842 $iw = $nt->getInterWiki();
844 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
845 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
846 $s .= $prefix . $trail;
849 if( $ns == $image ) {
850 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
851 $wgLinkCache->addImageLinkObj( $nt );
855 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
856 ( strpos( $link, "#" ) == FALSE ) ) {
857 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
862 $catns = strtoupper ( $nt->getDBkey () ) ;
863 $catns = explode ( ":" , $catns ) ;
864 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
866 if ( $catns == strtoupper($category) && $this->mOptions
->getUseCategoryMagic() ) {
867 $t = explode ( ":" , $nt->getText() ) ;
869 $t = implode ( ":" , $t ) ;
870 $t = $wgLang->ucFirst ( $t ) ;
871 $nnt = Title
::newFromText ( $category.":".$t ) ;
872 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
873 $this->mOutput
->mCategoryLinks
[] = $t ;
874 $s .= $prefix . $trail ;
878 if( $ns == $media ) {
879 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
880 $wgLinkCache->addImageLinkObj( $nt );
882 } elseif( $ns == $special ) {
883 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
886 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
888 wfProfileOut( $fname );
892 # Some functions here used by doBlockLevels()
894 /* private */ function closeParagraph()
897 if ( '' != $this->mLastSection
) {
898 $result = "</" . $this->mLastSection
. ">";
900 $this->mLastSection
= "";
903 # getCommon() returns the length of the longest common substring
904 # of both arguments, starting at the beginning of both.
906 /* private */ function getCommon( $st1, $st2 )
908 $fl = strlen( $st1 );
909 $shorter = strlen( $st2 );
910 if ( $fl < $shorter ) { $shorter = $fl; }
912 for ( $i = 0; $i < $shorter; ++
$i ) {
913 if ( $st1{$i} != $st2{$i} ) { break; }
917 # These next three functions open, continue, and close the list
918 # element appropriate to the prefix character passed into them.
920 /* private */ function openList( $char )
922 $result = $this->closeParagraph();
924 if ( "*" == $char ) { $result .= "<ul><li>"; }
925 else if ( "#" == $char ) { $result .= "<ol><li>"; }
926 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
927 else if ( ";" == $char ) {
928 $result .= "<dl><dt>";
929 $this->mDTopen
= true;
931 else { $result = "<!-- ERR 1 -->"; }
936 /* private */ function nextItem( $char )
938 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
939 else if ( ":" == $char ||
";" == $char ) {
941 if ( $this->mDTopen
) { $close = "</dt>"; }
942 if ( ";" == $char ) {
943 $this->mDTopen
= true;
944 return $close . "<dt>";
946 $this->mDTopen
= false;
947 return $close . "<dd>";
950 return "<!-- ERR 2 -->";
953 /* private */function closeList( $char )
955 if ( "*" == $char ) { $text = "</li></ul>"; }
956 else if ( "#" == $char ) { $text = "</li></ol>"; }
957 else if ( ":" == $char ) {
958 if ( $this->mDTopen
) {
959 $this->mDTopen
= false;
960 $text = "</dt></dl>";
962 $text = "</dd></dl>";
965 else { return "<!-- ERR 3 -->"; }
969 /* private */ function doBlockLevels( $text, $linestart )
971 $fname = "Parser::doBlockLevels";
972 wfProfileIn( $fname );
973 # Parsing through the text line by line. The main thing
974 # happening here is handling of block-level elements p, pre,
975 # and making lists from lines starting with * # : etc.
977 $a = explode( "\n", $text );
979 $lastPref = $text = '';
980 $this->mDTopen
= $inBlockElem = false;
982 if ( ! $linestart ) { $text .= array_shift( $a ); }
983 foreach ( $a as $t ) {
984 if ( "" != $text ) { $text .= "\n"; }
987 $opl = strlen( $lastPref );
988 $npl = strspn( $t, "*#:;" );
989 $pref = substr( $t, 0, $npl );
990 $pref2 = str_replace( ";", ":", $pref );
991 $t = substr( $t, $npl );
993 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
994 $text .= $this->nextItem( substr( $pref, -1 ) );
996 if ( ";" == substr( $pref, -1 ) ) {
997 $cpos = strpos( $t, ":" );
998 if ( ! ( false === $cpos ) ) {
999 $term = substr( $t, 0, $cpos );
1000 $text .= $term . $this->nextItem( ":" );
1001 $t = substr( $t, $cpos +
1 );
1004 } else if (0 != $npl ||
0 != $opl) {
1005 $cpl = $this->getCommon( $pref, $lastPref );
1007 while ( $cpl < $opl ) {
1008 $text .= $this->closeList( $lastPref{$opl-1} );
1011 if ( $npl <= $cpl && $cpl > 0 ) {
1012 $text .= $this->nextItem( $pref{$cpl-1} );
1014 while ( $npl > $cpl ) {
1015 $char = substr( $pref, $cpl, 1 );
1016 $text .= $this->openList( $char );
1018 if ( ";" == $char ) {
1019 $cpos = strpos( $t, ":" );
1020 if ( ! ( false === $cpos ) ) {
1021 $term = substr( $t, 0, $cpos );
1022 $text .= $term . $this->nextItem( ":" );
1023 $t = substr( $t, $cpos +
1 );
1030 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1032 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre)/i", $t ) ) {
1033 $text .= $this->closeParagraph();
1034 $inBlockElem = true;
1035 } else if ( preg_match("/(<hr|<\\/td)/i", $t ) ) {
1036 $text .= $this->closeParagraph();
1037 $inBlockElem = false;
1039 if ( ! $inBlockElem ) {
1040 if ( " " == $t{0} ) {
1041 $newSection = "pre";
1042 $text .= $this->closeParagraph();
1043 # $t = wfEscapeHTML( $t );
1045 else { $newSection = "p"; }
1047 if ( '' == trim( $oLine ) ) {
1048 if ( $this->mLastSection
!= 'p') {
1049 $text .= $this->closeParagraph();
1050 $text .= "<" . $newSection . ">";
1051 $this->mLastSection
= $newSection;
1052 } else if ( $this->mLastSection
== 'p') {
1055 } else if ( $this->mLastSection
== $newSection and $newSection != 'p' ) {
1056 $text .= $this->closeParagraph();
1057 $text .= "<" . $newSection . ">";
1058 $this->mLastSection
= $newSection;
1061 if ( $inBlockElem &&
1062 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|<\\/p<\\/div|<\\/pre)/i", $t ) ) {
1063 $inBlockElem = false;
1069 $text .= $this->closeList( $pref2{$npl-1} );
1072 if ( "" != $this->mLastSection
) {
1073 $text .= "</" . $this->mLastSection
. ">";
1074 $this->mLastSection
= "";
1076 wfProfileOut( $fname );
1080 function getVariableValue( $index ) {
1081 global $wgLang, $wgSitename, $wgServer;
1084 case MAG_CURRENTMONTH
:
1086 case MAG_CURRENTMONTHNAME
:
1087 return $wgLang->getMonthName( date("n") );
1088 case MAG_CURRENTMONTHNAMEGEN
:
1089 return $wgLang->getMonthNameGen( date("n") );
1090 case MAG_CURRENTDAY
:
1092 case MAG_CURRENTDAYNAME
:
1093 return $wgLang->getWeekdayName( date("w")+
1 );
1094 case MAG_CURRENTYEAR
:
1096 case MAG_CURRENTTIME
:
1097 return $wgLang->time( wfTimestampNow(), false );
1098 case MAG_NUMBEROFARTICLES
:
1099 return wfNumberOfArticles();
1109 function initialiseVariables()
1111 global $wgVariableIDs;
1112 $this->mVariables
= array();
1113 foreach ( $wgVariableIDs as $id ) {
1114 $mw =& MagicWord
::get( $id );
1115 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1119 /* private */ function replaceVariables( $text )
1121 global $wgLang, $wgCurParser;
1122 global $wgScript, $wgArticlePath;
1124 $fname = "Parser::replaceVariables";
1125 wfProfileIn( $fname );
1128 if ( !$this->mVariables
) {
1129 $this->initialiseVariables();
1131 $titleChars = Title
::legalChars();
1132 $regex = "/{{([$titleChars\\|]*?)}}/s";
1134 # "Recursive" variable expansion: run it through a couple of passes
1135 for ( $i=0; $i<MAX_INCLUDE_REPEAT
&& !$bail; $i++
) {
1138 # It's impossible to rebind a global in PHP
1139 # Instead, we run the substitution on a copy, then merge the changed fields back in
1140 $wgCurParser = $this->fork();
1142 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1143 if ( $oldText == $text ) {
1146 $this->merge( $wgCurParser );
1152 # Returns a copy of this object except with various variables cleared
1153 # This copy can be re-merged with the parent after operations on the copy
1157 $copy->mOutput
= new ParserOutput
;
1161 # Merges a copy split off with fork()
1162 function merge( &$copy )
1164 $this->mOutput
->merge( $copy->mOutput
);
1166 # Merge include throttling arrays
1167 foreach( $copy->mIncludeCount
as $dbk => $count ) {
1168 if ( array_key_exists( $dbk, $this->mIncludeCount
) ) {
1169 $this->mIncludeCount
[$dbk] +
= $count;
1171 $this->mIncludeCount
[$dbk] = $count;
1176 function braceSubstitution( $matches )
1178 global $wgLinkCache, $wgLang;
1179 $fname = "Parser::braceSubstitution";
1183 $text = $matches[1];
1186 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1187 if ( $mwSubst->matchStartAndRemove( $text ) ) {
1188 if ( $this->mOutputType
!= OT_WIKI
) {
1189 # Invalid SUBST not replaced at PST time
1190 # Return without further processing
1191 $text = $matches[0];
1194 } elseif ( $this->mOutputType
== OT_WIKI
) {
1195 # SUBST not found in PST pass, do nothing
1196 $text = $matches[0];
1200 # MSG, MSGNW and INT
1203 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1204 if ( $mwMsgnw->matchStartAndRemove( $text ) ) {
1207 # Remove obsolete MSG:
1208 $mwMsg =& MagicWord
::get( MAG_MSG
);
1209 $mwMsg->matchStartAndRemove( $text );
1212 # Check if it is an internal message
1213 $mwInt =& MagicWord
::get( MAG_INT
);
1214 if ( $mwInt->matchStartAndRemove( $text ) ) {
1215 $text = wfMsg( $text );
1222 # Check for NS: (namespace expansion)
1223 $mwNs = MagicWord
::get( MAG_NS
);
1224 if ( $mwNs->matchStartAndRemove( $text ) ) {
1225 if ( intval( $text ) ) {
1226 $text = $wgLang->getNsText( intval( $text ) );
1229 $index = Namespace::getCanonicalIndex( strtolower( $text ) );
1230 if ( !is_null( $index ) ) {
1231 $text = $wgLang->getNsText( $index );
1238 # LOCALURL and LOCALURLE
1240 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1241 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1243 if ( $mwLocal->matchStartAndRemove( $text ) ) {
1244 $func = 'getLocalURL';
1245 } elseif ( $mwLocalE->matchStartAndRemove( $text ) ) {
1246 $func = 'escapeLocalURL';
1251 if ( $func !== '' ) {
1252 $args = explode( "|", $text );
1253 $n = count( $args );
1255 $title = Title
::newFromText( $args[0] );
1256 if ( !is_null( $title ) ) {
1258 $text = $title->$func( $args[1] );
1260 $text = $title->$func();
1268 # Check for a match against internal variables
1269 if ( !$found && array_key_exists( $text, $this->mVariables
) ) {
1270 $text = $this->mVariables
[$text];
1272 $this->mOutput
->mContainsOldMagic
= true;
1275 # Load from database
1277 $title = Title
::newFromText( $text, NS_TEMPLATE
);
1278 if ( !is_null( $text ) && !$title->isExternal() ) {
1279 # Check for excessive inclusion
1280 $dbk = $title->getPrefixedDBkey();
1281 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1282 $this->mIncludeCount
[$dbk] = 0;
1284 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1285 $article = new Article( $title );
1286 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1287 if ( $articleContent !== false ) {
1289 $text = $articleContent;
1291 # Escaping and link table handling
1292 # Not required for preSaveTransform()
1293 if ( $this->mOutputType
== OT_HTML
) {
1295 $text = wfEscapeWikiText( $text );
1297 $text = $this->removeHTMLtags( $text );
1299 $wgLinkCache->suspend();
1300 $text = $this->doTokenizedParser( $text );
1301 $wgLinkCache->resume();
1302 $wgLinkCache->addLinkObj( $title );
1308 # If the title is valid but undisplayable, make a link to it
1309 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1310 $text = "[[" . $title->getPrefixedText() . "]]";
1323 # Cleans up HTML, removes dangerous tags and attributes
1324 /* private */ function removeHTMLtags( $text )
1326 $fname = "Parser::removeHTMLtags";
1327 wfProfileIn( $fname );
1328 $htmlpairs = array( # Tags that must be closed
1329 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1330 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1331 "strike", "strong", "tt", "var", "div", "center",
1332 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1333 "ruby", "rt" , "rb" , "rp", "p"
1335 $htmlsingle = array(
1336 "br", "hr", "li", "dt", "dd"
1338 $htmlnest = array( # Tags that can be nested--??
1339 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1340 "dl", "font", "big", "small", "sub", "sup"
1342 $tabletags = array( # Can only appear inside table
1346 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1347 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1349 $htmlattrs = $this->getHTMLattrs () ;
1351 # Remove HTML comments
1352 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1354 $bits = explode( "<", $text );
1355 $text = array_shift( $bits );
1356 $tagstack = array(); $tablestack = array();
1358 foreach ( $bits as $x ) {
1359 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1360 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1362 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1363 error_reporting( $prev );
1366 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1370 if ( ! in_array( $t, $htmlsingle ) &&
1371 ( $ot = array_pop( $tagstack ) ) != $t ) {
1372 array_push( $tagstack, $ot );
1375 if ( $t == "table" ) {
1376 $tagstack = array_pop( $tablestack );
1381 # Keep track for later
1382 if ( in_array( $t, $tabletags ) &&
1383 ! in_array( "table", $tagstack ) ) {
1385 } else if ( in_array( $t, $tagstack ) &&
1386 ! in_array ( $t , $htmlnest ) ) {
1388 } else if ( ! in_array( $t, $htmlsingle ) ) {
1389 if ( $t == "table" ) {
1390 array_push( $tablestack, $tagstack );
1391 $tagstack = array();
1393 array_push( $tagstack, $t );
1395 # Strip non-approved attributes from the tag
1396 $newparams = $this->fixTagAttributes($params);
1400 $rest = str_replace( ">", ">", $rest );
1401 $text .= "<$slash$t $newparams$brace$rest";
1405 $text .= "<" . str_replace( ">", ">", $x);
1407 # Close off any remaining tags
1408 while ( $t = array_pop( $tagstack ) ) {
1410 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1412 wfProfileOut( $fname );
1418 * This function accomplishes several tasks:
1419 * 1) Auto-number headings if that option is enabled
1420 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1421 * 3) Add a Table of contents on the top for users who have enabled the option
1422 * 4) Auto-anchor headings
1424 * It loops through all headlines, collects the necessary data, then splits up the
1425 * string and re-inserts the newly formatted headlines.
1429 /* private */ function formatHeadings( $text )
1431 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1432 $doShowToc = $this->mOptions
->getShowToc();
1433 if( !$this->mTitle
->userCanEdit() ) {
1435 $rightClickHack = 0;
1437 $showEditLink = $this->mOptions
->getEditSection();
1438 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1441 # Inhibit editsection links if requested in the page
1442 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1443 if( $esw->matchAndRemove( $text ) ) {
1446 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1448 $mw =& MagicWord
::get( MAG_NOTOC
);
1449 if( $mw->matchAndRemove( $text ) ) {
1453 # never add the TOC to the Main Page. This is an entry page that should not
1454 # be more than 1-2 screens large anyway
1455 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1459 # Get all headlines for numbering them and adding funky stuff like [edit]
1460 # links - this is for later, but we need the number of headlines right now
1461 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1463 # if there are fewer than 4 headlines in the article, do not show TOC
1464 if( $numMatches < 4 ) {
1468 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1469 # override above conditions and always show TOC
1470 $mw =& MagicWord
::get( MAG_FORCETOC
);
1471 if ($mw->matchAndRemove( $text ) ) {
1476 # We need this to perform operations on the HTML
1477 $sk =& $this->mOptions
->getSkin();
1482 # Ugh .. the TOC should have neat indentation levels which can be
1483 # passed to the skin functions. These are determined here
1488 $sublevelCount = array();
1491 foreach( $matches[3] as $headline ) {
1494 $prevlevel = $level;
1496 $level = $matches[1][$headlineCount];
1497 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1498 # reset when we enter a new level
1499 $sublevelCount[$level] = 0;
1500 $toc .= $sk->tocIndent( $level - $prevlevel );
1501 $toclevel +
= $level - $prevlevel;
1503 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1504 # reset when we step back a level
1505 $sublevelCount[$level+
1]=0;
1506 $toc .= $sk->tocUnindent( $prevlevel - $level );
1507 $toclevel -= $prevlevel - $level;
1509 # count number of headlines for each level
1510 @$sublevelCount[$level]++
;
1511 if( $doNumberHeadings ||
$doShowToc ) {
1513 for( $i = 1; $i <= $level; $i++
) {
1514 if( !empty( $sublevelCount[$i] ) ) {
1518 $numbering .= $sublevelCount[$i];
1524 # The canonized header is a version of the header text safe to use for links
1525 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1526 $canonized_headline = Parser
::unstrip( $headline, $this->mStripState
);
1529 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1530 $tocline = trim( $canonized_headline );
1531 $canonized_headline = preg_replace("/[ &\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1532 $refer[$headlineCount] = $canonized_headline;
1534 # count how many in assoc. array so we can track dupes in anchors
1535 @$refers[$canonized_headline]++
;
1536 $refcount[$headlineCount]=$refers[$canonized_headline];
1538 # Prepend the number to the heading text
1540 if( $doNumberHeadings ||
$doShowToc ) {
1541 $tocline = $numbering . " " . $tocline;
1543 # Don't number the heading if it is the only one (looks silly)
1544 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1545 # the two are different if the line contains a link
1546 $headline=$numbering . " " . $headline;
1550 # Create the anchor for linking from the TOC to the section
1551 $anchor = $canonized_headline;
1552 if($refcount[$headlineCount] > 1 ) {
1553 $anchor .= "_" . $refcount[$headlineCount];
1556 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1558 if( $showEditLink ) {
1559 if ( empty( $head[$headlineCount] ) ) {
1560 $head[$headlineCount] = "";
1562 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1565 # Add the edit section span
1566 if( $rightClickHack ) {
1567 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1570 # give headline the correct <h#> tag
1571 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1577 $toclines = $headlineCount;
1578 $toc .= $sk->tocUnindent( $toclevel );
1579 $toc = $sk->tocTable( $toc );
1582 # split up and insert constructed headlines
1584 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1587 foreach( $blocks as $block ) {
1588 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1589 # This is the [edit] link that appears for the top block of text when
1590 # section editing is enabled
1591 $full .= $sk->editSectionLink(0);
1594 if( $doShowToc && !$i) {
1595 # Let's add a top anchor just in case we want to link to the top of the page
1596 $full = "<a name=\"top\"></a>".$full.$toc;
1599 if( !empty( $head[$i] ) ) {
1608 /* private */ function doMagicISBN( &$tokenizer )
1612 # Check whether next token is a text token
1613 # If yes, fetch it and convert the text into a
1614 # Special::BookSources link
1615 $token = $tokenizer->previewToken();
1616 while ( $token["type"] == "" )
1618 $tokenizer->nextToken();
1619 $token = $tokenizer->previewToken();
1621 if ( $token["type"] == "text" )
1623 $token = $tokenizer->nextToken();
1624 $x = $token["text"];
1625 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1627 $isbn = $blank = "" ;
1628 while ( " " == $x{0} ) {
1630 $x = substr( $x, 1 );
1632 while ( strstr( $valid, $x{0} ) != false ) {
1634 $x = substr( $x, 1 );
1636 $num = str_replace( "-", "", $isbn );
1637 $num = str_replace( " ", "", $num );
1640 $text = "ISBN $blank$x";
1642 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1643 $text = "<a href=\"" .
1644 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1645 "\" class=\"internal\">ISBN $isbn</a>";
1653 /* private */ function doMagicRFC( &$tokenizer )
1657 # Check whether next token is a text token
1658 # If yes, fetch it and convert the text into a
1659 # link to an RFC source
1660 $token = $tokenizer->previewToken();
1661 while ( $token["type"] == "" )
1663 $tokenizer->nextToken();
1664 $token = $tokenizer->previewToken();
1666 if ( $token["type"] == "text" )
1668 $token = $tokenizer->nextToken();
1669 $x = $token["text"];
1670 $valid = "0123456789";
1672 $rfc = $blank = "" ;
1673 while ( " " == $x{0} ) {
1675 $x = substr( $x, 1 );
1677 while ( strstr( $valid, $x{0} ) != false ) {
1679 $x = substr( $x, 1 );
1683 $text .= "RFC $blank$x";
1685 $url = wfmsg( "rfcurl" );
1686 $url = str_replace( "$1", $rfc, $url);
1687 $sk =& $this->mOptions
->getSkin();
1688 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1689 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1697 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1699 $this->mOptions
= $options;
1700 $this->mTitle
=& $title;
1701 $this->mOutputType
= OT_WIKI
;
1703 if ( $clearState ) {
1704 $this->clearState();
1707 $stripState = false;
1708 $text = str_replace("\r\n", "\n", $text);
1709 $text = $this->strip( $text, $stripState, false );
1710 $text = $this->pstPass2( $text, $user );
1711 $text = $this->unstrip( $text, $stripState );
1715 /* private */ function pstPass2( $text, &$user )
1717 global $wgLang, $wgLocaltimezone, $wgCurParser;
1719 # Variable replacement
1720 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1721 $text = $this->replaceVariables( $text );
1725 $n = $user->getName();
1726 $k = $user->getOption( "nickname" );
1727 if ( "" == $k ) { $k = $n; }
1728 if(isset($wgLocaltimezone)) {
1729 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1731 /* Note: this is an ugly timezone hack for the European wikis */
1732 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1733 " (" . date( "T" ) . ")";
1734 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1736 $text = preg_replace( "/~~~~~/", $d, $text );
1737 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1738 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1739 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1740 Namespace::getUser() ) . ":$n|$k]]", $text );
1742 # Context links: [[|name]] and [[name (context)|]]
1744 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1745 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1746 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1747 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1749 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1750 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1751 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1752 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1753 # [[ns:page (cont)|]]
1755 $t = $this->mTitle
->getText();
1756 if ( preg_match( $conpat, $t, $m ) ) {
1759 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1760 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1761 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1763 if ( "" == $context ) {
1764 $text = preg_replace( $p2, "[[\\1]]", $text );
1766 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1770 $mw =& MagicWord::get( MAG_SUBST );
1771 $wgCurParser = $this->fork();
1772 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1773 $this->merge( $wgCurParser );
1776 # Trim trailing whitespace
1777 # MAG_END (__END__) tag allows for trailing
1778 # whitespace to be deliberately included
1779 $text = rtrim( $text );
1780 $mw =& MagicWord
::get( MAG_END
);
1781 $mw->matchAndRemove( $text );
1786 # Set up some variables which are usually set up in parse()
1787 # so that an external function can call some class members with confidence
1788 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1790 $this->mTitle
=& $title;
1791 $this->mOptions
= $options;
1792 $this->mOutputType
= $outputType;
1793 if ( $clearState ) {
1794 $this->clearState();
1798 function transformMsg( $text, $options ) {
1800 static $executing = false;
1802 # Guard against infinite recursion
1808 $this->mTitle
= $wgTitle;
1809 $this->mOptions
= $options;
1810 $this->mOutputType
= OT_MSG
;
1811 $this->clearState();
1812 $text = $this->replaceVariables( $text );
1821 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1823 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1824 $containsOldMagic = false )
1826 $this->mText
= $text;
1827 $this->mLanguageLinks
= $languageLinks;
1828 $this->mCategoryLinks
= $categoryLinks;
1829 $this->mContainsOldMagic
= $containsOldMagic;
1832 function getText() { return $this->mText
; }
1833 function getLanguageLinks() { return $this->mLanguageLinks
; }
1834 function getCategoryLinks() { return $this->mCategoryLinks
; }
1835 function containsOldMagic() { return $this->mContainsOldMagic
; }
1836 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
1837 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
1838 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
1839 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
1841 function merge( $other ) {
1842 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
1843 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
1844 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
1851 # All variables are private
1852 var $mUseTeX; # Use texvc to expand <math> tags
1853 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1854 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1855 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1856 var $mAllowExternalImages; # Allow external images inline
1857 var $mSkin; # Reference to the preferred skin
1858 var $mDateFormat; # Date format index
1859 var $mEditSection; # Create "edit section" links
1860 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1861 var $mNumberHeadings; # Automatically number headings
1862 var $mShowToc; # Show table of contents
1864 function getUseTeX() { return $this->mUseTeX
; }
1865 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
1866 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
1867 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
1868 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
1869 function getSkin() { return $this->mSkin
; }
1870 function getDateFormat() { return $this->mDateFormat
; }
1871 function getEditSection() { return $this->mEditSection
; }
1872 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
1873 function getNumberHeadings() { return $this->mNumberHeadings
; }
1874 function getShowToc() { return $this->mShowToc
; }
1876 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
1877 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
1878 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
1879 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
1880 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
1881 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
1882 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
1883 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
1884 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
1885 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
1886 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
1888 /* static */ function newFromUser( &$user )
1890 $popts = new ParserOptions
;
1891 $popts->initialiseFromUser( &$user );
1895 function initialiseFromUser( &$userInput )
1897 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1899 if ( !$userInput ) {
1901 $user->setLoaded( true );
1903 $user =& $userInput;
1906 $this->mUseTeX
= $wgUseTeX;
1907 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
1908 $this->mUseDynamicDates
= $wgUseDynamicDates;
1909 $this->mInterwikiMagic
= $wgInterwikiMagic;
1910 $this->mAllowExternalImages
= $wgAllowExternalImages;
1911 $this->mSkin
=& $user->getSkin();
1912 $this->mDateFormat
= $user->getOption( "date" );
1913 $this->mEditSection
= $user->getOption( "editsection" );
1914 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
1915 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
1916 $this->mShowToc
= $user->getOption( "showtoc" );
1922 # Regex callbacks, used in Parser::replaceVariables
1923 function wfBraceSubstitution( $matches )
1925 global $wgCurParser;
1926 return $wgCurParser->braceSubstitution( $matches );