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 . '-' . $tag . 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] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
183 $pre_content[$marker] = "<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"] !== false ) {
551 if ( $state["em"] !== false && $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"] !== false ) {
569 if ( $state["strong"] !== false && $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"] !== false && $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"] !== false ) {
595 $s .= "</em><strong>";
596 $state["em"] = FALSE;
597 $state["strong"] = $token["pos"];
598 } elseif ( $state["strong"] !== false ) {
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() )
626 switch ( $token["type"] )
629 # simple text with no further markup
630 $txt = $token["text"];
633 # remember the tag opened with 3 [
637 # FIXME : Treat orphaned open tags (stack not empty when text is over)
639 array_push( $tokenStack, $token );
646 # get text from stack, glue it together, and call the code to handle a
649 if ( count( $tokenStack ) == 0 )
651 # stack empty. Found a ]] without an opening [[
655 $lastToken = array_pop( $tokenStack );
656 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
658 if( !empty( $lastToken["text"] ) ) {
659 $linkText = $lastToken["text"] . $linkText;
661 $lastToken = array_pop( $tokenStack );
664 $txt = $linkText ."]]";
666 if( isset( $lastToken["text"] ) ) {
667 $prefix = $lastToken["text"];
671 $nextToken = $tokenizer->previewToken();
672 if ( $nextToken["type"] == "text" )
674 # Preview just looks at it. Now we have to fetch it.
675 $nextToken = $tokenizer->nextToken();
676 $txt .= $nextToken["text"];
678 $fakestate = $this->mStripState
;
679 $txt = $this->handleInternalLink( $this->unstrip($txt,$fakestate), $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 return (trim($s) == '')?
'': $s;
848 if( $ns == $image ) {
849 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
850 $wgLinkCache->addImageLinkObj( $nt );
854 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
855 ( strpos( $link, "#" ) == FALSE ) ) {
856 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
861 $catns = strtoupper ( $nt->getDBkey () ) ;
862 $catns = explode ( ":" , $catns ) ;
863 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
865 if ( $catns == strtoupper($category) && $this->mOptions
->getUseCategoryMagic() ) {
866 $t = explode ( ":" , $nt->getText() ) ;
868 $t = implode ( ":" , $t ) ;
869 $t = $wgLang->ucFirst ( $t ) ;
870 $nnt = Title
::newFromText ( $category.":".$t ) ;
871 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
872 $this->mOutput
->mCategoryLinks
[] = $t ;
873 $s .= $prefix . $trail ;
877 if( $ns == $media ) {
878 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
879 $wgLinkCache->addImageLinkObj( $nt );
881 } elseif( $ns == $special ) {
882 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
885 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
887 wfProfileOut( $fname );
891 # Some functions here used by doBlockLevels()
893 /* private */ function closeParagraph()
896 if ( '' != $this->mLastSection
) {
897 $result = "</" . $this->mLastSection
. ">";
899 $this->mLastSection
= "";
902 # getCommon() returns the length of the longest common substring
903 # of both arguments, starting at the beginning of both.
905 /* private */ function getCommon( $st1, $st2 )
907 $fl = strlen( $st1 );
908 $shorter = strlen( $st2 );
909 if ( $fl < $shorter ) { $shorter = $fl; }
911 for ( $i = 0; $i < $shorter; ++
$i ) {
912 if ( $st1{$i} != $st2{$i} ) { break; }
916 # These next three functions open, continue, and close the list
917 # element appropriate to the prefix character passed into them.
919 /* private */ function openList( $char )
921 $result = $this->closeParagraph();
923 if ( "*" == $char ) { $result .= "<ul><li>"; }
924 else if ( "#" == $char ) { $result .= "<ol><li>"; }
925 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
926 else if ( ";" == $char ) {
927 $result .= "<dl><dt>";
928 $this->mDTopen
= true;
930 else { $result = "<!-- ERR 1 -->"; }
935 /* private */ function nextItem( $char )
937 if ( "*" == $char ||
"#" == $char ) { return "</li><li>"; }
938 else if ( ":" == $char ||
";" == $char ) {
940 if ( $this->mDTopen
) { $close = "</dt>"; }
941 if ( ";" == $char ) {
942 $this->mDTopen
= true;
943 return $close . "<dt>";
945 $this->mDTopen
= false;
946 return $close . "<dd>";
949 return "<!-- ERR 2 -->";
952 /* private */function closeList( $char )
954 if ( "*" == $char ) { $text = "</li></ul>"; }
955 else if ( "#" == $char ) { $text = "</li></ol>"; }
956 else if ( ":" == $char ) {
957 if ( $this->mDTopen
) {
958 $this->mDTopen
= false;
959 $text = "</dt></dl>";
961 $text = "</dd></dl>";
964 else { return "<!-- ERR 3 -->"; }
968 /* private */ function doBlockLevels( $text, $linestart )
970 $fname = "Parser::doBlockLevels";
971 wfProfileIn( $fname );
972 # Parsing through the text line by line. The main thing
973 # happening here is handling of block-level elements p, pre,
974 # and making lists from lines starting with * # : etc.
976 $a = explode( "\n", $text );
977 $lastPref = $text = '';
978 $this->mDTopen
= $inBlockElem = false;
980 if ( ! $linestart ) { $text .= array_shift( $a ); }
981 foreach ( $a as $t ) {
982 if ( "" != $text ) { $text .= "\n"; }
985 $opl = strlen( $lastPref );
986 $npl = strspn( $t, "*#:;" );
987 $pref = substr( $t, 0, $npl );
988 $pref2 = str_replace( ";", ":", $pref );
989 $t = substr( $t, $npl );
991 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
992 $text .= $this->nextItem( substr( $pref, -1 ) );
994 if ( ";" == substr( $pref, -1 ) ) {
995 $cpos = strpos( $t, ":" );
996 if ( ! ( false === $cpos ) ) {
997 $term = substr( $t, 0, $cpos );
998 $text .= $term . $this->nextItem( ":" );
999 $t = substr( $t, $cpos +
1 );
1002 } else if (0 != $npl ||
0 != $opl) {
1003 $cpl = $this->getCommon( $pref, $lastPref );
1005 while ( $cpl < $opl ) {
1006 $text .= $this->closeList( $lastPref{$opl-1} );
1009 if ( $npl <= $cpl && $cpl > 0 ) {
1010 $text .= $this->nextItem( $pref{$cpl-1} );
1012 while ( $npl > $cpl ) {
1013 $char = substr( $pref, $cpl, 1 );
1014 $text .= $this->openList( $char );
1016 if ( ";" == $char ) {
1017 $cpos = strpos( $t, ":" );
1018 if ( ! ( false === $cpos ) ) {
1019 $term = substr( $t, 0, $cpos );
1020 $text .= $term . $this->nextItem( ":" );
1021 $t = substr( $t, $cpos +
1 );
1028 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1030 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div)/i", $t ) ) {
1031 $text .= $this->closeParagraph();
1032 $inBlockElem = true;
1033 } else if ( preg_match("/(<hr|<\\/td|".$uniq_prefix."-pre)/i", $t ) ) {
1034 $text .= $this->closeParagraph();
1035 $inBlockElem = false;
1037 if ( ! $inBlockElem ) {
1038 if ( " " == $t{0} ) {
1039 $newSection = "pre";
1040 $text .= $this->closeParagraph();
1041 # $t = wfEscapeHTML( $t );
1043 else { $newSection = "p"; }
1045 if ( '' == trim( $oLine ) ) {
1046 if ( $this->mLastSection
!= 'p') {
1047 $text .= $this->closeParagraph();
1048 $text .= "<" . $newSection . ">";
1049 $this->mLastSection
= $newSection;
1050 } else if ( $this->mLastSection
== 'p' and '' == $oLine) {
1053 } else if ( $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 )
1429 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1430 $doShowToc = $this->mOptions
->getShowToc();
1431 if( !$this->mTitle
->userCanEdit() ) {
1433 $rightClickHack = 0;
1435 $showEditLink = $this->mOptions
->getEditSection();
1436 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1439 # Inhibit editsection links if requested in the page
1440 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1441 if( $esw->matchAndRemove( $text ) ) {
1444 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1446 $mw =& MagicWord
::get( MAG_NOTOC
);
1447 if( $mw->matchAndRemove( $text ) ) {
1451 # never add the TOC to the Main Page. This is an entry page that should not
1452 # be more than 1-2 screens large anyway
1453 if( $this->mTitle
->getPrefixedText() == wfMsg("mainpage") ) {
1457 # Get all headlines for numbering them and adding funky stuff like [edit]
1458 # links - this is for later, but we need the number of headlines right now
1459 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1461 # if there are fewer than 4 headlines in the article, do not show TOC
1462 if( $numMatches < 4 ) {
1466 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1467 # override above conditions and always show TOC
1468 $mw =& MagicWord
::get( MAG_FORCETOC
);
1469 if ($mw->matchAndRemove( $text ) ) {
1474 # We need this to perform operations on the HTML
1475 $sk =& $this->mOptions
->getSkin();
1480 # Ugh .. the TOC should have neat indentation levels which can be
1481 # passed to the skin functions. These are determined here
1486 $sublevelCount = array();
1489 foreach( $matches[3] as $headline ) {
1492 $prevlevel = $level;
1494 $level = $matches[1][$headlineCount];
1495 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1496 # reset when we enter a new level
1497 $sublevelCount[$level] = 0;
1498 $toc .= $sk->tocIndent( $level - $prevlevel );
1499 $toclevel +
= $level - $prevlevel;
1501 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1502 # reset when we step back a level
1503 $sublevelCount[$level+
1]=0;
1504 $toc .= $sk->tocUnindent( $prevlevel - $level );
1505 $toclevel -= $prevlevel - $level;
1507 # count number of headlines for each level
1508 @$sublevelCount[$level]++
;
1509 if( $doNumberHeadings ||
$doShowToc ) {
1511 for( $i = 1; $i <= $level; $i++
) {
1512 if( !empty( $sublevelCount[$i] ) ) {
1516 $numbering .= $sublevelCount[$i];
1522 # The canonized header is a version of the header text safe to use for links
1523 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1524 $canonized_headline = Parser
::unstrip( $headline, $this->mStripState
);
1527 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1528 $tocline = trim( $canonized_headline );
1529 $canonized_headline = preg_replace("/[ &\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1530 $refer[$headlineCount] = $canonized_headline;
1532 # count how many in assoc. array so we can track dupes in anchors
1533 @$refers[$canonized_headline]++
;
1534 $refcount[$headlineCount]=$refers[$canonized_headline];
1536 # Prepend the number to the heading text
1538 if( $doNumberHeadings ||
$doShowToc ) {
1539 $tocline = $numbering . " " . $tocline;
1541 # Don't number the heading if it is the only one (looks silly)
1542 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1543 # the two are different if the line contains a link
1544 $headline=$numbering . " " . $headline;
1548 # Create the anchor for linking from the TOC to the section
1549 $anchor = $canonized_headline;
1550 if($refcount[$headlineCount] > 1 ) {
1551 $anchor .= "_" . $refcount[$headlineCount];
1554 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1556 if( $showEditLink ) {
1557 if ( empty( $head[$headlineCount] ) ) {
1558 $head[$headlineCount] = "";
1560 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
1563 # Add the edit section span
1564 if( $rightClickHack ) {
1565 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
1568 # give headline the correct <h#> tag
1569 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1575 $toclines = $headlineCount;
1576 $toc .= $sk->tocUnindent( $toclevel );
1577 $toc = $sk->tocTable( $toc );
1580 # split up and insert constructed headlines
1582 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1585 foreach( $blocks as $block ) {
1586 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1587 # This is the [edit] link that appears for the top block of text when
1588 # section editing is enabled
1589 $full .= $sk->editSectionLink(0);
1592 if( $doShowToc && !$i) {
1593 # Top anchor now in skin
1597 if( !empty( $head[$i] ) ) {
1606 /* private */ function doMagicISBN( &$tokenizer )
1610 # Check whether next token is a text token
1611 # If yes, fetch it and convert the text into a
1612 # Special::BookSources link
1613 $token = $tokenizer->previewToken();
1614 while ( $token["type"] == "" )
1616 $tokenizer->nextToken();
1617 $token = $tokenizer->previewToken();
1619 if ( $token["type"] == "text" )
1621 $token = $tokenizer->nextToken();
1622 $x = $token["text"];
1623 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1625 $isbn = $blank = "" ;
1626 while ( " " == $x{0} ) {
1628 $x = substr( $x, 1 );
1630 while ( strstr( $valid, $x{0} ) != false ) {
1632 $x = substr( $x, 1 );
1634 $num = str_replace( "-", "", $isbn );
1635 $num = str_replace( " ", "", $num );
1638 $text = "ISBN $blank$x";
1640 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Booksources" );
1641 $text = "<a href=\"" .
1642 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1643 "\" class=\"internal\">ISBN $isbn</a>";
1651 /* private */ function doMagicRFC( &$tokenizer )
1655 # Check whether next token is a text token
1656 # If yes, fetch it and convert the text into a
1657 # link to an RFC source
1658 $token = $tokenizer->previewToken();
1659 while ( $token["type"] == "" )
1661 $tokenizer->nextToken();
1662 $token = $tokenizer->previewToken();
1664 if ( $token["type"] == "text" )
1666 $token = $tokenizer->nextToken();
1667 $x = $token["text"];
1668 $valid = "0123456789";
1670 $rfc = $blank = "" ;
1671 while ( " " == $x{0} ) {
1673 $x = substr( $x, 1 );
1675 while ( strstr( $valid, $x{0} ) != false ) {
1677 $x = substr( $x, 1 );
1681 $text .= "RFC $blank$x";
1683 $url = wfmsg( "rfcurl" );
1684 $url = str_replace( "$1", $rfc, $url);
1685 $sk =& $this->mOptions
->getSkin();
1686 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1687 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1695 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1697 $this->mOptions
= $options;
1698 $this->mTitle
=& $title;
1699 $this->mOutputType
= OT_WIKI
;
1701 if ( $clearState ) {
1702 $this->clearState();
1705 $stripState = false;
1706 $text = str_replace("\r\n", "\n", $text);
1707 $text = $this->strip( $text, $stripState, false );
1708 $text = $this->pstPass2( $text, $user );
1709 $text = $this->unstrip( $text, $stripState );
1713 /* private */ function pstPass2( $text, &$user )
1715 global $wgLang, $wgLocaltimezone, $wgCurParser;
1717 # Variable replacement
1718 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1719 $text = $this->replaceVariables( $text );
1723 $n = $user->getName();
1724 $k = $user->getOption( "nickname" );
1725 if ( "" == $k ) { $k = $n; }
1726 if(isset($wgLocaltimezone)) {
1727 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1729 /* Note: this is an ugly timezone hack for the European wikis */
1730 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1731 " (" . date( "T" ) . ")";
1732 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1734 $text = preg_replace( "/~~~~~/", $d, $text );
1735 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1736 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1737 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1738 Namespace::getUser() ) . ":$n|$k]]", $text );
1740 # Context links: [[|name]] and [[name (context)|]]
1742 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1743 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1744 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1745 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1747 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1748 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1749 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1750 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1751 # [[ns:page (cont)|]]
1753 $t = $this->mTitle
->getText();
1754 if ( preg_match( $conpat, $t, $m ) ) {
1757 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1758 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1759 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1761 if ( "" == $context ) {
1762 $text = preg_replace( $p2, "[[\\1]]", $text );
1764 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1768 $mw =& MagicWord::get( MAG_SUBST );
1769 $wgCurParser = $this->fork();
1770 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1771 $this->merge( $wgCurParser );
1774 # Trim trailing whitespace
1775 # MAG_END (__END__) tag allows for trailing
1776 # whitespace to be deliberately included
1777 $text = rtrim( $text );
1778 $mw =& MagicWord
::get( MAG_END
);
1779 $mw->matchAndRemove( $text );
1784 # Set up some variables which are usually set up in parse()
1785 # so that an external function can call some class members with confidence
1786 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1788 $this->mTitle
=& $title;
1789 $this->mOptions
= $options;
1790 $this->mOutputType
= $outputType;
1791 if ( $clearState ) {
1792 $this->clearState();
1796 function transformMsg( $text, $options ) {
1798 static $executing = false;
1800 # Guard against infinite recursion
1806 $this->mTitle
= $wgTitle;
1807 $this->mOptions
= $options;
1808 $this->mOutputType
= OT_MSG
;
1809 $this->clearState();
1810 $text = $this->replaceVariables( $text );
1819 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1821 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1822 $containsOldMagic = false )
1824 $this->mText
= $text;
1825 $this->mLanguageLinks
= $languageLinks;
1826 $this->mCategoryLinks
= $categoryLinks;
1827 $this->mContainsOldMagic
= $containsOldMagic;
1830 function getText() { return $this->mText
; }
1831 function getLanguageLinks() { return $this->mLanguageLinks
; }
1832 function getCategoryLinks() { return $this->mCategoryLinks
; }
1833 function containsOldMagic() { return $this->mContainsOldMagic
; }
1834 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
1835 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
1836 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
1837 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
1839 function merge( $other ) {
1840 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
1841 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
1842 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
1849 # All variables are private
1850 var $mUseTeX; # Use texvc to expand <math> tags
1851 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1852 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1853 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1854 var $mAllowExternalImages; # Allow external images inline
1855 var $mSkin; # Reference to the preferred skin
1856 var $mDateFormat; # Date format index
1857 var $mEditSection; # Create "edit section" links
1858 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1859 var $mNumberHeadings; # Automatically number headings
1860 var $mShowToc; # Show table of contents
1862 function getUseTeX() { return $this->mUseTeX
; }
1863 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
1864 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
1865 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
1866 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
1867 function getSkin() { return $this->mSkin
; }
1868 function getDateFormat() { return $this->mDateFormat
; }
1869 function getEditSection() { return $this->mEditSection
; }
1870 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
1871 function getNumberHeadings() { return $this->mNumberHeadings
; }
1872 function getShowToc() { return $this->mShowToc
; }
1874 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
1875 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
1876 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
1877 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
1878 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
1879 function setSkin( $x ) { return wfSetRef( $this->mSkin
, $x ); }
1880 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
1881 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
1882 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
1883 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
1884 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
1886 /* static */ function newFromUser( &$user )
1888 $popts = new ParserOptions
;
1889 $popts->initialiseFromUser( &$user );
1893 function initialiseFromUser( &$userInput )
1895 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1897 if ( !$userInput ) {
1899 $user->setLoaded( true );
1901 $user =& $userInput;
1904 $this->mUseTeX
= $wgUseTeX;
1905 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
1906 $this->mUseDynamicDates
= $wgUseDynamicDates;
1907 $this->mInterwikiMagic
= $wgInterwikiMagic;
1908 $this->mAllowExternalImages
= $wgAllowExternalImages;
1909 $this->mSkin
=& $user->getSkin();
1910 $this->mDateFormat
= $user->getOption( "date" );
1911 $this->mEditSection
= $user->getOption( "editsection" );
1912 $this->mEditSectionOnRightClick
= $user->getOption( "editsectiononrightclick" );
1913 $this->mNumberHeadings
= $user->getOption( "numberheadings" );
1914 $this->mShowToc
= $user->getOption( "showtoc" );
1920 # Regex callbacks, used in Parser::replaceVariables
1921 function wfBraceSubstitution( $matches )
1923 global $wgCurParser;
1924 return $wgCurParser->braceSubstitution( $matches );