accidentially removed nl fix
[mediawiki.git] / includes / Parser.php
blobb119c52d1a5a75c0d8108d91f724b5eb016c27ed
1 <?php
3 require_once('Tokenizer.php');
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 require_once('extensions/wikihiero/wikihiero.php');
8 if( $GLOBALS['wgUseTimeline'] ){
9 require_once('extensions/timeline/Timeline.php');
12 # PHP Parser
14 # Processes wiki markup
16 # There are two main entry points into the Parser class: parse() and preSaveTransform().
17 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
19 # Globals used:
20 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
22 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
24 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
25 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
26 # $wgLocaltimezone
28 # * only within ParserOptions
31 #----------------------------------------
32 # Variable substitution O(N^2) attack
33 #-----------------------------------------
34 # Without countermeasures, it would be possible to attack the parser by saving a page
35 # filled with a large number of inclusions of large pages. The size of the generated
36 # page would be proportional to the square of the input size. Hence, we limit the number
37 # of inclusions of any given page, thus bringing any attack back to O(N).
40 define( "MAX_INCLUDE_REPEAT", 5 );
42 # Allowed values for $mOutputType
43 define( "OT_HTML", 1 );
44 define( "OT_WIKI", 2 );
45 define( "OT_MSG", 3 );
47 # string parameter for extractTags which will cause it
48 # to strip HTML comments in addition to regular
49 # <XML>-style tags. This should not be anything we
50 # may want to use in wikisyntax
51 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
53 # prefix for escaping, used in two functions at least
54 define( "UNIQ_PREFIX", "NaodW29");
56 class Parser
58 # Cleared with clearState():
59 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
60 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
62 # Temporary:
63 var $mOptions, $mTitle, $mOutputType;
65 function Parser()
67 $this->clearState();
70 function clearState()
72 $this->mOutput = new ParserOutput;
73 $this->mAutonumber = 0;
74 $this->mLastSection = "";
75 $this->mDTopen = false;
76 $this->mVariables = false;
77 $this->mIncludeCount = array();
78 $this->mStripState = array();
79 $this->mArgStack = array();
82 # First pass--just handle <nowiki> sections, pass the rest off
83 # to internalParse() which does all the real work.
85 # Returns a ParserOutput
87 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
89 global $wgUseTidy;
90 $fname = "Parser::parse";
91 wfProfileIn( $fname );
93 if ( $clearState ) {
94 $this->clearState();
97 $this->mOptions = $options;
98 $this->mTitle =& $title;
99 $this->mOutputType = OT_HTML;
101 $stripState = NULL;
102 $text = $this->strip( $text, $this->mStripState );
103 $text = $this->internalParse( $text, $linestart );
104 $text = $this->unstrip( $text, $this->mStripState );
105 # Clean up special characters, only run once, next-to-last before doBlockLevels
106 if(!$wgUseTidy) {
107 $fixtags = array(
108 "/<hr *>/i" => '<hr/>',
109 "/<br *>/i" => '<br/>',
110 "/<center *>/i"=>'<div class="center">',
111 "/<\\/center *>/i" => '</div>',
112 # Clean up spare ampersands; note that we probably ought to be
113 # more careful about named entities.
114 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
116 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
117 } else {
118 $fixtags = array(
119 "/<center *>/i"=>'<div class="center">',
120 "/<\\/center *>/i" => '</div>'
122 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
124 # only once and last
125 $text = $this->doBlockLevels( $text, $linestart );
126 if($wgUseTidy) {
127 $text = $this->tidy($text);
129 $this->mOutput->setText( $text );
130 wfProfileOut( $fname );
131 return $this->mOutput;
134 /* static */ function getRandomString()
136 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
139 # Replaces all occurrences of <$tag>content</$tag> in the text
140 # with a random marker and returns the new text. the output parameter
141 # $content will be an associative array filled with data on the form
142 # $unique_marker => content.
144 # If $content is already set, the additional entries will be appended
146 # If $tag is set to STRIP_COMMENTS, the function will extract
147 # <!-- HTML comments -->
149 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
150 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
151 if ( !$content ) {
152 $content = array( );
154 $n = 1;
155 $stripped = "";
157 while ( "" != $text ) {
158 if($tag==STRIP_COMMENTS) {
159 $p = preg_split( "/<!--/i", $text, 2 );
160 } else {
161 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
163 $stripped .= $p[0];
164 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) {
165 $text = "";
166 } else {
167 if($tag==STRIP_COMMENTS) {
168 $q = preg_split( "/-->/i", $p[1], 2 );
169 } else {
170 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
172 $marker = $rnd . sprintf("%08X", $n++);
173 $content[$marker] = $q[0];
174 $stripped .= $marker;
175 $text = $q[1];
178 return $stripped;
181 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
182 # If $render is set, performs necessary rendering operations on plugins
183 # Returns the text, and fills an array with data needed in unstrip()
184 # If the $state is already a valid strip state, it adds to the state
186 # When $stripcomments is set, HTML comments <!-- like this -->
187 # will be stripped in addition to other tags. This is important
188 # for section editing, where these comments cause confusion when
189 # counting the sections in the wikisource
190 function strip( $text, &$state, $stripcomments = false )
192 $render = ($this->mOutputType == OT_HTML);
193 $nowiki_content = array();
194 $hiero_content = array();
195 $math_content = array();
196 $pre_content = array();
197 $comment_content = array();
199 # Replace any instances of the placeholders
200 $uniq_prefix = UNIQ_PREFIX;
201 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
203 $text = Parser::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
204 foreach( $nowiki_content as $marker => $content ){
205 if( $render ){
206 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
207 } else {
208 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
212 $text = Parser::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
213 foreach( $hiero_content as $marker => $content ){
214 if( $render && $GLOBALS['wgUseWikiHiero']){
215 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML);
216 } else {
217 $hiero_content[$marker] = "<hiero>$content</hiero>";
221 $text = Parser::extractTags("math", $text, $math_content, $uniq_prefix);
222 foreach( $math_content as $marker => $content ){
223 if( $render ) {
224 if( $this->mOptions->getUseTeX() ) {
225 $math_content[$marker] = renderMath( $content );
226 } else {
227 $math_content[$marker] = "&lt;math&gt;$content&lt;math&gt;";
229 } else {
230 $math_content[$marker] = "<math>$content</math>";
234 $text = Parser::extractTags("pre", $text, $pre_content, $uniq_prefix);
235 foreach( $pre_content as $marker => $content ){
236 if( $render ){
237 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
238 } else {
239 $pre_content[$marker] = "<pre>$content</pre>";
242 if($stripcomments) {
243 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
244 foreach( $comment_content as $marker => $content ){
245 $comment_content[$marker] = "<!--$content-->";
249 # Merge state with the pre-existing state, if there is one
250 if ( $state ) {
251 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
252 $state['hiero'] = $state['hiero'] + $hiero_content;
253 $state['math'] = $state['math'] + $math_content;
254 $state['pre'] = $state['pre'] + $pre_content;
255 $state['comment'] = $state['comment'] + $comment_content;
256 } else {
257 $state = array(
258 'nowiki' => $nowiki_content,
259 'hiero' => $hiero_content,
260 'math' => $math_content,
261 'pre' => $pre_content,
262 'comment' => $comment_content
265 return $text;
268 function unstrip( $text, &$state )
270 # Must expand in reverse order, otherwise nested tags will be corrupted
271 $contentDict = end( $state );
272 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
273 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
274 $text = str_replace( key( $contentDict ), $content, $text );
278 return $text;
281 # Add an item to the strip state
282 # Returns the unique tag which must be inserted into the stripped text
283 # The tag will be replaced with the original text in unstrip()
285 function insertStripItem( $text, &$state )
287 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
288 if ( !$state ) {
289 $state = array(
290 'nowiki' => array(),
291 'hiero' => array(),
292 'math' => array(),
293 'pre' => array()
296 $state['item'][$rnd] = $text;
297 return $rnd;
300 # This method generates the list of subcategories and pages for a category
301 function categoryMagic ()
303 global $wgLang , $wgUser ;
304 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
306 $cns = Namespace::getCategory() ;
307 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
309 $r = "<br style=\"clear:both;\"/>\n";
312 $sk =& $wgUser->getSkin() ;
314 $articles = array() ;
315 $children = array() ;
316 $data = array () ;
317 $id = $this->mTitle->getArticleID() ;
319 # For existing categories
320 if( $id ) {
321 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
322 $res = wfQuery ( $sql, DB_READ ) ;
323 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
324 } else {
325 # For non-existing categories
326 $t = wfStrencode( $this->mTitle->getPrefixedDBKey() );
327 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to='$t' AND bl_from=cur_id" ;
328 $res = wfQuery ( $sql, DB_READ ) ;
329 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
332 # For all pages that link to this category
333 foreach ( $data AS $x )
335 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
336 if ( $t != "" ) $t .= ":" ;
337 $t .= $x->cur_title ;
339 if ( $x->cur_namespace == $cns ) {
340 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
341 } else {
342 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
345 wfFreeResult ( $res ) ;
347 # Showing subcategories
348 if ( count ( $children ) > 0 )
350 asort ( $children ) ;
351 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
352 $r .= implode ( ", " , $children ) ;
355 # Showing pages in this category
356 if ( count ( $articles ) > 0 )
358 $ti = $this->mTitle->getText() ;
359 asort ( $articles ) ;
360 $h = wfMsg( "category_header", $ti );
361 $r .= "<h2>{$h}</h2>\n" ;
362 $r .= implode ( ", " , $articles ) ;
366 return $r ;
369 function getHTMLattrs ()
371 $htmlattrs = array( # Allowed attributes--no scripting, etc.
372 "title", "align", "lang", "dir", "width", "height",
373 "bgcolor", "clear", /* BR */ "noshade", /* HR */
374 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
375 /* FONT */ "type", "start", "value", "compact",
376 /* For various lists, mostly deprecated but safe */
377 "summary", "width", "border", "frame", "rules",
378 "cellspacing", "cellpadding", "valign", "char",
379 "charoff", "colgroup", "col", "span", "abbr", "axis",
380 "headers", "scope", "rowspan", "colspan", /* Tables */
381 "id", "class", "name", "style" /* For CSS */
383 return $htmlattrs ;
386 function fixTagAttributes ( $t )
388 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
389 $htmlattrs = $this->getHTMLattrs() ;
391 # Strip non-approved attributes from the tag
392 $t = preg_replace(
393 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
394 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
395 $t);
396 # Strip javascript "expression" from stylesheets. Brute force approach:
397 # If anythin offensive is found, all attributes of the HTML tag are dropped
399 if( preg_match(
400 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
401 wfMungeToUtf8( $t ) ) )
403 $t="";
406 return trim ( $t ) ;
409 /* interface with html tidy, used if $wgUseTidy = true */
410 function tidy ( $text ) {
411 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
412 global $wgInputEncoding, $wgOutputEncoding;
413 $cleansource = '';
414 switch(strtoupper($wgOutputEncoding)) {
415 case 'ISO-8859-1':
416 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
417 break;
418 case 'UTF-8':
419 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
420 break;
421 default:
422 $wgTidyOpts .= ' -raw';
425 $text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
426 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
427 '<head><title>test</title></head><body>'.$text.'</body></html>';
428 $descriptorspec = array(
429 0 => array("pipe", "r"),
430 1 => array("pipe", "w"),
431 2 => array("file", "/dev/null", "a")
433 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
434 if (is_resource($process)) {
435 fwrite($pipes[0], $text);
436 fclose($pipes[0]);
437 while (!feof($pipes[1])) {
438 $cleansource .= fgets($pipes[1], 1024);
440 fclose($pipes[1]);
441 $return_value = proc_close($process);
443 if( $cleansource == '' && $text != '') {
444 return '<h2>'.wfMsg('seriousxhtmlerrors').'</h2><pre>'.htmlspecialchars($text).'</pre>';
445 } else {
446 return $cleansource;
450 function doTableStuff ( $t )
452 $t = explode ( "\n" , $t ) ;
453 $td = array () ; # Is currently a td tag open?
454 $ltd = array () ; # Was it TD or TH?
455 $tr = array () ; # Is currently a tr tag open?
456 $ltr = array () ; # tr attributes
457 foreach ( $t AS $k => $x )
459 $x = trim ( $x ) ;
460 $fc = substr ( $x , 0 , 1 ) ;
461 if ( "{|" == substr ( $x , 0 , 2 ) )
463 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
464 array_push ( $td , false ) ;
465 array_push ( $ltd , "" ) ;
466 array_push ( $tr , false ) ;
467 array_push ( $ltr , "" ) ;
469 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
470 else if ( "|}" == substr ( $x , 0 , 2 ) )
472 $z = "</table>\n" ;
473 $l = array_pop ( $ltd ) ;
474 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
475 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
476 array_pop ( $ltr ) ;
477 $t[$k] = $z ;
479 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
481 $z = trim ( substr ( $x , 2 ) ) ;
482 $t[$k] = "<caption>{$z}</caption>\n" ;
484 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
486 $x = substr ( $x , 1 ) ;
487 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
488 $z = "" ;
489 $l = array_pop ( $ltd ) ;
490 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
491 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
492 array_pop ( $ltr ) ;
493 $t[$k] = $z ;
494 array_push ( $tr , false ) ;
495 array_push ( $td , false ) ;
496 array_push ( $ltd , "" ) ;
497 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
499 else if ( "|" == $fc || "!" == $fc || "|+" == substr ( $x , 0 , 2 ) ) # Caption
501 if ( "|+" == substr ( $x , 0 , 2 ) )
503 $fc = "+" ;
504 $x = substr ( $x , 1 ) ;
506 $after = substr ( $x , 1 ) ;
507 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
508 $after = explode ( "||" , $after ) ;
509 $t[$k] = "" ;
510 foreach ( $after AS $theline )
512 $z = "" ;
513 if ( $fc != "+" )
515 $tra = array_pop ( $ltr ) ;
516 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
517 array_push ( $tr , true ) ;
518 array_push ( $ltr , "" ) ;
521 $l = array_pop ( $ltd ) ;
522 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
523 if ( $fc == "|" ) $l = "td" ;
524 else if ( $fc == "!" ) $l = "th" ;
525 else if ( $fc == "+" ) $l = "caption" ;
526 else $l = "" ;
527 array_push ( $ltd , $l ) ;
528 $y = explode ( "|" , $theline , 2 ) ;
529 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
530 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
531 $t[$k] .= $y ;
532 array_push ( $td , true ) ;
537 # Closing open td, tr && table
538 while ( count ( $td ) > 0 )
540 if ( array_pop ( $td ) ) $t[] = "</td>" ;
541 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
542 $t[] = "</table>" ;
545 $t = implode ( "\n" , $t ) ;
546 # $t = $this->removeHTMLtags( $t );
547 return $t ;
550 function internalParse( $text, $linestart, $args = array(), $isMain=true )
552 $fname = "Parser::internalParse";
553 wfProfileIn( $fname );
555 $text = $this->removeHTMLtags( $text );
556 $text = $this->replaceVariables( $text, $args );
558 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
560 $text = $this->doHeadings( $text );
561 if($this->mOptions->getUseDynamicDates()) {
562 global $wgDateFormatter;
563 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
565 $text = $this->replaceExternalLinks( $text );
566 $text = $this->doTokenizedParser ( $text );
567 $text = $this->doTableStuff ( $text ) ;
568 $text = $this->formatHeadings( $text, $isMain );
569 $sk =& $this->mOptions->getSkin();
570 $text = $sk->transformContent( $text );
572 if ( !isset ( $this->categoryMagicDone ) ) {
573 $text .= $this->categoryMagic () ;
574 $this->categoryMagicDone = true ;
577 wfProfileOut( $fname );
578 return $text;
582 /* private */ function doHeadings( $text )
584 for ( $i = 6; $i >= 1; --$i ) {
585 $h = substr( "======", 0, $i );
586 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
587 "<h{$i}>\\1</h{$i}>\\2", $text );
589 return $text;
592 # Note: we have to do external links before the internal ones,
593 # and otherwise take great care in the order of things here, so
594 # that we don't end up interpreting some URLs twice.
596 /* private */ function replaceExternalLinks( $text )
598 $fname = "Parser::replaceExternalLinks";
599 wfProfileIn( $fname );
600 $text = $this->subReplaceExternalLinks( $text, "http", true );
601 $text = $this->subReplaceExternalLinks( $text, "https", true );
602 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
603 $text = $this->subReplaceExternalLinks( $text, "irc", false );
604 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
605 $text = $this->subReplaceExternalLinks( $text, "news", false );
606 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
607 wfProfileOut( $fname );
608 return $text;
611 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
613 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
614 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
616 # this is the list of separators that should be ignored if they
617 # are the last character of an URL but that should be included
618 # if they occur within the URL, e.g. "go to www.foo.com, where .."
619 # in this case, the last comma should not become part of the URL,
620 # but in "www.foo.com/123,2342,32.htm" it should.
621 $sep = ",;\.:";
622 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
623 $images = "gif|png|jpg|jpeg";
625 # PLEASE NOTE: The curly braces { } are not part of the regex,
626 # they are interpreted as part of the string (used to tell PHP
627 # that the content of the string should be inserted there).
628 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
629 "((?i){$images})([^{$uc}]|$)/";
631 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
632 $sk =& $this->mOptions->getSkin();
634 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
635 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
636 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
638 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
639 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
640 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
641 "</a>\\5", $s );
642 $s = str_replace( $unique, $protocol, $s );
644 $a = explode( "[{$protocol}:", " " . $s );
645 $s = array_shift( $a );
646 $s = substr( $s, 1 );
648 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
649 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
651 foreach ( $a as $line ) {
652 if ( preg_match( $e1, $line, $m ) ) {
653 $link = "{$protocol}:{$m[1]}";
654 $trail = $m[2];
655 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
656 else { $text = wfEscapeHTML( $link ); }
657 } else if ( preg_match( $e2, $line, $m ) ) {
658 $link = "{$protocol}:{$m[1]}";
659 $text = $m[2];
660 $trail = $m[3];
661 } else {
662 $s .= "[{$protocol}:" . $line;
663 continue;
665 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
666 $paren = "";
667 } else {
668 # Expand the URL for printable version
669 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
671 $la = $sk->getExternalLinkAttributes( $link, $text );
672 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
675 return $s;
678 /* private */ function handle4Quotes( &$state, $token )
680 /* This one makes some assumptions.
681 * '''Caesar''''s army => <strong>Caesar</strong>'s army
682 * ''''Caesar'''' was a roman emperor => '<strong>Caesar</strong>' was a roman emperor
683 * These assumptions might be wrong, but any other assumption might be wrong, too.
684 * So here we go */
685 if ( $state["strong"] !== false ) {
686 return $this->handle3Quotes( $state, $token ) . "'";
687 } else {
688 return "'" . $this->handle3Quotes( $state, $token );
693 /* private */ function handle3Quotes( &$state, $token )
695 if ( $state["strong"] !== false ) {
696 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
698 # ''' lala ''lala '''
699 $s = "</em></strong><em>";
700 } else {
701 $s = "</strong>";
703 $state["strong"] = FALSE;
704 } else {
705 $s = "<strong>";
706 $state["strong"] = $token["pos"];
708 return $s;
711 /* private */ function handle2Quotes( &$state, $token )
713 if ( $state["em"] !== false ) {
714 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
716 # ''lala'''lala'' ....'''
717 $s = "</strong></em><strong>";
718 } else {
719 $s = "</em>";
721 $state["em"] = FALSE;
722 } else {
723 $s = "<em>";
724 $state["em"] = $token["pos"];
727 return $s;
730 /* private */ function handle5Quotes( &$state, $token )
732 $s = "";
733 if ( $state["em"] !== false && $state["strong"] !== false ) {
734 if ( $state["em"] < $state["strong"] ) {
735 $s .= "</strong></em>";
736 } else {
737 $s .= "</em></strong>";
739 $state["strong"] = $state["em"] = FALSE;
740 } elseif ( $state["em"] !== false ) {
741 $s .= "</em><strong>";
742 $state["em"] = FALSE;
743 $state["strong"] = $token["pos"];
744 } elseif ( $state["strong"] !== false ) {
745 $s .= "</strong><em>";
746 $state["strong"] = FALSE;
747 $state["em"] = $token["pos"];
748 } else { # not $em and not $strong
749 $s .= "<strong><em>";
750 $state["strong"] = $state["em"] = $token["pos"];
752 return $s;
755 /* private */ function doTokenizedParser( $str )
757 global $wgLang; # for language specific parser hook
758 global $wgUploadDirectory, $wgUseTimeline;
760 $tokenizer=Tokenizer::newFromString( $str );
761 $tokenStack = array();
763 $s="";
764 $state["em"] = FALSE;
765 $state["strong"] = FALSE;
766 $tagIsOpen = FALSE;
767 $threeopen = false;
769 # The tokenizer splits the text into tokens and returns them one by one.
770 # Every call to the tokenizer returns a new token.
771 while ( $token = $tokenizer->nextToken() )
773 switch ( $token["type"] )
775 case "text":
776 # simple text with no further markup
777 $txt = $token["text"];
778 break;
779 case "blank":
780 # Text that contains blanks that have to be converted to
781 # non-breakable spaces for French.
782 # U+202F NARROW NO-BREAK SPACE might be a better choice, but
783 # browser support for Unicode spacing is poor.
784 $txt = str_replace( " ", "&nbsp;", $token["text"] );
785 break;
786 case "[[[":
787 # remember the tag opened with 3 [
788 $threeopen = true;
789 case "[[":
790 # link opening tag.
791 # FIXME : Treat orphaned open tags (stack not empty when text is over)
792 $tagIsOpen = TRUE;
793 array_push( $tokenStack, $token );
794 $txt="";
795 break;
797 case "]]]":
798 case "]]":
799 # link close tag.
800 # get text from stack, glue it together, and call the code to handle a
801 # link
803 if ( count( $tokenStack ) == 0 )
805 # stack empty. Found a ]] without an opening [[
806 $txt = "]]";
807 } else {
808 $linkText = "";
809 $lastToken = array_pop( $tokenStack );
810 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
812 if( !empty( $lastToken["text"] ) ) {
813 $linkText = $lastToken["text"] . $linkText;
815 $lastToken = array_pop( $tokenStack );
818 $txt = $linkText ."]]";
820 if( isset( $lastToken["text"] ) ) {
821 $prefix = $lastToken["text"];
822 } else {
823 $prefix = "";
825 $nextToken = $tokenizer->previewToken();
826 if ( $nextToken["type"] == "text" )
828 # Preview just looks at it. Now we have to fetch it.
829 $nextToken = $tokenizer->nextToken();
830 $txt .= $nextToken["text"];
832 $txt = $this->handleInternalLink( $this->unstrip($txt,$this->mStripState), $prefix );
834 # did the tag start with 3 [ ?
835 if($threeopen) {
836 # show the first as text
837 $txt = "[".$txt;
838 $threeopen=false;
842 $tagIsOpen = (count( $tokenStack ) != 0);
843 break;
844 case "----":
845 $txt = "\n<hr />\n";
846 break;
847 case "'''":
848 # This and the four next ones handle quotes
849 $txt = $this->handle3Quotes( $state, $token );
850 break;
851 case "''":
852 $txt = $this->handle2Quotes( $state, $token );
853 break;
854 case "'''''":
855 $txt = $this->handle5Quotes( $state, $token );
856 break;
857 case "''''":
858 $txt = $this->handle4Quotes( $state, $token );
859 break;
860 case "":
861 # empty token
862 $txt="";
863 break;
864 case "h":
865 #heading- used to close all unbalanced bold or em tags in this section
866 $txt = '';
867 if( $state['em'] !== false and
868 ( $state['strong'] === false or $state['em'] > $state['strong'] ) )
870 $s .= '</em>';
871 $state['em'] = false;
873 if ( $state['strong'] !== false ) $txt .= '</strong>';
874 if ( $state['em'] !== false ) $txt .= '</em>';
875 $state['strong'] = $state['em'] = false;
876 break;
877 case "RFC ":
878 if ( $tagIsOpen ) {
879 $txt = "RFC ";
880 } else {
881 $txt = $this->doMagicRFC( $tokenizer );
883 break;
884 case "ISBN ":
885 if ( $tagIsOpen ) {
886 $txt = "ISBN ";
887 } else {
888 $txt = $this->doMagicISBN( $tokenizer );
890 break;
891 case "<timeline>":
892 if ( $wgUseTimeline &&
893 "" != ( $timelinesrc = $tokenizer->readAllUntil("&lt;/timeline&gt;") ) )
895 $txt = renderTimeline( $timelinesrc );
896 } else {
897 $txt=$token["text"];
899 break;
900 default:
901 # Call language specific Hook.
902 $txt = $wgLang->processToken( $token, $tokenStack );
903 if ( NULL == $txt ) {
904 # An unkown token. Highlight.
905 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
906 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
908 break;
910 # If we're parsing the interior of a link, don't append the interior to $s,
911 # but push it to the stack so it can be processed when a ]] token is found.
912 if ( $tagIsOpen && $txt != "" ) {
913 $token["type"] = "text";
914 $token["text"] = $txt;
915 array_push( $tokenStack, $token );
916 } else {
917 $s .= $txt;
919 } #end while
921 # make 100% sure all strong and em tags are closed
922 # doBlockLevels often messes the last bit up though, but invalid nesting is better than unclosed tags
923 # tidy solves this though
924 if( $state['em'] !== false and
925 ( $state['strong'] === false or $state['em'] > $state['strong'] ) )
927 $s .= '</em>';
928 $state['em'] = false;
930 if ( $state['strong'] !== false ) $s .= '</strong>';
931 if ( $state['em'] !== false ) $s .= '</em>';
933 if ( count( $tokenStack ) != 0 )
935 # still objects on stack. opened [[ tag without closing ]] tag.
936 $txt = "";
937 while ( $lastToken = array_pop( $tokenStack ) )
939 if ( $lastToken["type"] == "text" )
941 $txt = $lastToken["text"] . $txt;
942 } else {
943 $txt = $lastToken["type"] . $txt;
946 $s .= $txt;
948 return $s;
951 /* private */ function handleInternalLink( $line, $prefix )
953 global $wgLang, $wgLinkCache;
954 global $wgNamespacesWithSubpages, $wgLanguageCode;
955 static $fname = "Parser::handleInternalLink" ;
956 wfProfileIn( $fname );
958 wfProfileIn( "$fname-setup" );
959 static $tc = FALSE;
960 if ( !$tc ) { $tc = Title::legalChars() . "#"; }
961 $sk =& $this->mOptions->getSkin();
963 # Match a link having the form [[namespace:link|alternate]]trail
964 static $e1 = FALSE;
965 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
966 # Match the end of a line for a word that's not followed by whitespace,
967 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
968 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
969 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
970 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
973 # Special and Media are pseudo-namespaces; no pages actually exist in them
974 static $image = FALSE;
975 static $special = FALSE;
976 static $media = FALSE;
977 static $category = FALSE;
978 if ( !$image ) { $image = Namespace::getImage(); }
979 if ( !$special ) { $special = Namespace::getSpecial(); }
980 if ( !$media ) { $media = Namespace::getMedia(); }
981 if ( !$category ) { $category = Namespace::getCategory(); }
983 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
985 wfProfileOut( "$fname-setup" );
986 $s = "";
988 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
989 $text = $m[2];
990 $trail = $m[3];
991 } else { # Invalid form; output directly
992 $s .= $prefix . "[[" . $line ;
993 return $s;
996 /* Valid link forms:
997 Foobar -- normal
998 :Foobar -- override special treatment of prefix (images, language links)
999 /Foobar -- convert to CurrentPage/Foobar
1000 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1002 $c = substr($m[1],0,1);
1003 $noforce = ($c != ":");
1004 if( $c == "/" ) { # subpage
1005 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
1006 $m[1]=substr($m[1],1,strlen($m[1])-2);
1007 $noslash=$m[1];
1008 } else {
1009 $noslash=substr($m[1],1);
1011 if($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]) { # subpages allowed here
1012 $link = $this->mTitle->getPrefixedText(). "/" . trim($noslash);
1013 if( "" == $text ) {
1014 $text= $m[1];
1015 } # this might be changed for ugliness reasons
1016 } else {
1017 $link = $noslash; # no subpage allowed, use standard link
1019 } elseif( $noforce ) { # no subpage
1020 $link = $m[1];
1021 } else {
1022 $link = substr( $m[1], 1 );
1024 if( "" == $text )
1025 $text = $link;
1027 $nt = Title::newFromText( $link );
1028 if( !$nt ) {
1029 $s .= $prefix . "[[" . $line;
1030 return $s;
1032 $ns = $nt->getNamespace();
1033 $iw = $nt->getInterWiki();
1034 if( $noforce ) {
1035 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1036 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
1037 $s .= $prefix . $trail ;
1038 return (trim($s) == '')? '': $s;
1040 if( $ns == $image ) {
1041 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1042 $wgLinkCache->addImageLinkObj( $nt );
1043 return $s;
1045 if ( $ns == $category ) {
1046 $t = $nt->getText() ;
1047 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1048 $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
1049 $this->mOutput->mCategoryLinks[] = $t ;
1050 $s .= $prefix . $trail ;
1051 return $s ;
1054 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1055 ( strpos( $link, "#" ) == FALSE ) ) {
1056 # Self-links are handled specially; generally de-link and change to bold.
1057 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, "", $trail );
1058 return $s;
1061 if( $ns == $media ) {
1062 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1063 $wgLinkCache->addImageLinkObj( $nt );
1064 return $s;
1065 } elseif( $ns == $special ) {
1066 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
1067 return $s;
1069 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
1071 wfProfileOut( $fname );
1072 return $s;
1075 # Some functions here used by doBlockLevels()
1077 /* private */ function closeParagraph()
1079 $result = "";
1080 if ( '' != $this->mLastSection ) {
1081 $result = "</" . $this->mLastSection . ">\n";
1083 $this->mInPre = false;
1084 $this->mLastSection = "";
1085 return $result;
1087 # getCommon() returns the length of the longest common substring
1088 # of both arguments, starting at the beginning of both.
1090 /* private */ function getCommon( $st1, $st2 )
1092 $fl = strlen( $st1 );
1093 $shorter = strlen( $st2 );
1094 if ( $fl < $shorter ) { $shorter = $fl; }
1096 for ( $i = 0; $i < $shorter; ++$i ) {
1097 if ( $st1{$i} != $st2{$i} ) { break; }
1099 return $i;
1101 # These next three functions open, continue, and close the list
1102 # element appropriate to the prefix character passed into them.
1104 /* private */ function openList( $char )
1106 $result = $this->closeParagraph();
1108 if ( "*" == $char ) { $result .= "<ul><li>"; }
1109 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1110 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1111 else if ( ";" == $char ) {
1112 $result .= "<dl><dt>";
1113 $this->mDTopen = true;
1115 else { $result = "<!-- ERR 1 -->"; }
1117 return $result;
1120 /* private */ function nextItem( $char )
1122 if ( "*" == $char || "#" == $char ) { return "</li><li>"; }
1123 else if ( ":" == $char || ";" == $char ) {
1124 $close = "</dd>";
1125 if ( $this->mDTopen ) { $close = "</dt>"; }
1126 if ( ";" == $char ) {
1127 $this->mDTopen = true;
1128 return $close . "<dt>";
1129 } else {
1130 $this->mDTopen = false;
1131 return $close . "<dd>";
1134 return "<!-- ERR 2 -->";
1137 /* private */function closeList( $char )
1139 if ( "*" == $char ) { $text = "</li></ul>"; }
1140 else if ( "#" == $char ) { $text = "</li></ol>"; }
1141 else if ( ":" == $char ) {
1142 if ( $this->mDTopen ) {
1143 $this->mDTopen = false;
1144 $text = "</dt></dl>";
1145 } else {
1146 $text = "</dd></dl>";
1149 else { return "<!-- ERR 3 -->"; }
1150 return $text."\n";
1153 /* private */ function doBlockLevels( $text, $linestart ) {
1154 $fname = "Parser::doBlockLevels";
1155 wfProfileIn( $fname );
1157 # Parsing through the text line by line. The main thing
1158 # happening here is handling of block-level elements p, pre,
1159 # and making lists from lines starting with * # : etc.
1161 $textLines = explode( "\n", $text );
1163 $lastPrefix = $output = $lastLine = '';
1164 $this->mDTopen = $inBlockElem = false;
1165 $prefixLength = 0;
1166 $paragraphStack = false;
1168 if ( !$linestart ) {
1169 $output .= array_shift( $textLines );
1171 foreach ( $textLines as $oLine ) {
1172 $lastPrefixLength = strlen( $lastPrefix );
1173 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1174 $preOpenMatch = preg_match("/<pre/i", $oLine );
1175 if (!$this->mInPre) {
1176 $this->mInPre = !empty($preOpenMatch);
1178 if ( !$this->mInPre ) {
1179 # Multiple prefixes may abut each other for nested lists.
1180 $prefixLength = strspn( $oLine, "*#:;" );
1181 $pref = substr( $oLine, 0, $prefixLength );
1183 # eh?
1184 $pref2 = str_replace( ";", ":", $pref );
1185 $t = substr( $oLine, $prefixLength );
1186 } else {
1187 # Don't interpret any other prefixes in preformatted text
1188 $prefixLength = 0;
1189 $pref = $pref2 = '';
1190 $t = $oLine;
1193 # List generation
1194 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1195 # Same as the last item, so no need to deal with nesting or opening stuff
1196 $output .= $this->nextItem( substr( $pref, -1 ) );
1197 $paragraphStack = false;
1199 if ( ";" == substr( $pref, -1 ) ) {
1200 # The one nasty exception: definition lists work like this:
1201 # ; title : definition text
1202 # So we check for : in the remainder text to split up the
1203 # title and definition, without b0rking links.
1204 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1205 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1206 $term = $match[1];
1207 $output .= $term . $this->nextItem( ":" );
1208 $t = $match[2];
1211 } elseif( $prefixLength || $lastPrefixLength ) {
1212 # Either open or close a level...
1213 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1214 $paragraphStack = false;
1216 while( $commonPrefixLength < $lastPrefixLength ) {
1217 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1218 --$lastPrefixLength;
1220 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1221 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1223 while ( $prefixLength > $commonPrefixLength ) {
1224 $char = substr( $pref, $commonPrefixLength, 1 );
1225 $output .= $this->openList( $char );
1227 if ( ";" == $char ) {
1228 # FIXME: This is dupe of code above
1229 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1230 $term = $match[1];
1231 $output .= $term . $this->nextItem( ":" );
1232 $t = $match[2];
1235 ++$commonPrefixLength;
1237 $lastPrefix = $pref2;
1239 if( 0 == $prefixLength ) {
1240 # No prefix (not in list)--go to paragraph mode
1241 $uniq_prefix = UNIQ_PREFIX;
1242 // XXX: use a stack for nestable elements like span, table and div
1243 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p|<ul|<li)/i", $t );
1244 $closematch = preg_match(
1245 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1246 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre|<\\/li|<\\/ul)/i", $t );
1247 if ( $openmatch or $closematch ) {
1248 $paragraphStack = false;
1249 $output .= $this->closeParagraph();
1250 if($preOpenMatch and !$preCloseMatch) {
1251 $this->mInPre = true;
1253 if ( $closematch ) {
1254 $inBlockElem = false;
1255 } else {
1256 $inBlockElem = true;
1258 } else if ( !$inBlockElem ) {
1259 if ( " " == $t{0} ) {
1260 // pre
1261 if ($this->mLastSection != 'pre') {
1262 $paragraphStack = false;
1263 $output .= $this->closeParagraph().'<pre>';
1264 $this->mLastSection = 'pre';
1266 } else {
1267 // paragraph
1268 if ( '' == trim($t) ) {
1269 if ( $paragraphStack ) {
1270 $output .= $paragraphStack.'<br/>';
1271 $paragraphStack = false;
1272 $this->mLastSection = 'p';
1273 } else {
1274 if ($this->mLastSection != 'p' ) {
1275 $output .= $this->closeParagraph();
1276 $this->mLastSection = '';
1277 $paragraphStack = "<p>";
1278 } else {
1279 $paragraphStack = '</p><p>';
1282 } else {
1283 if ( $paragraphStack ) {
1284 $output .= $paragraphStack;
1285 $paragraphStack = false;
1286 $this->mLastSection = 'p';
1287 } else if ($this->mLastSection != 'p') {
1288 $output .= $this->closeParagraph().'<p>';
1289 $this->mLastSection = 'p';
1295 if ($paragraphStack === false) {
1296 $output .= $t."\n";
1299 while ( $prefixLength ) {
1300 $output .= $this->closeList( $pref2{$prefixLength-1} );
1301 --$prefixLength;
1303 if ( "" != $this->mLastSection ) {
1304 $output .= "</" . $this->mLastSection . ">";
1305 $this->mLastSection = "";
1308 wfProfileOut( $fname );
1309 return $output;
1312 function getVariableValue( $index ) {
1313 global $wgLang, $wgSitename, $wgServer;
1315 switch ( $index ) {
1316 case MAG_CURRENTMONTH:
1317 return date( "m" );
1318 case MAG_CURRENTMONTHNAME:
1319 return $wgLang->getMonthName( date("n") );
1320 case MAG_CURRENTMONTHNAMEGEN:
1321 return $wgLang->getMonthNameGen( date("n") );
1322 case MAG_CURRENTDAY:
1323 return date("j");
1324 case MAG_PAGENAME:
1325 return $this->mTitle->getText();
1326 case MAG_NAMESPACE:
1327 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1328 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1329 case MAG_CURRENTDAYNAME:
1330 return $wgLang->getWeekdayName( date("w")+1 );
1331 case MAG_CURRENTYEAR:
1332 return date( "Y" );
1333 case MAG_CURRENTTIME:
1334 return $wgLang->time( wfTimestampNow(), false );
1335 case MAG_NUMBEROFARTICLES:
1336 return wfNumberOfArticles();
1337 case MAG_SITENAME:
1338 return $wgSitename;
1339 case MAG_SERVER:
1340 return $wgServer;
1341 default:
1342 return NULL;
1346 function initialiseVariables()
1348 global $wgVariableIDs;
1349 $this->mVariables = array();
1350 foreach ( $wgVariableIDs as $id ) {
1351 $mw =& MagicWord::get( $id );
1352 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1356 /* private */ function replaceVariables( $text, $args = array() )
1358 global $wgLang, $wgScript, $wgArticlePath;
1360 $fname = "Parser::replaceVariables";
1361 wfProfileIn( $fname );
1363 $bail = false;
1364 if ( !$this->mVariables ) {
1365 $this->initialiseVariables();
1367 $titleChars = Title::legalChars();
1368 $regex = "/(\\n?){{([$titleChars]*?)(\\|.*?|)}}/s";
1370 # This function is called recursively. To keep track of arguments we need a stack:
1371 array_push( $this->mArgStack, $args );
1373 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1374 $GLOBALS['wgCurParser'] =& $this;
1375 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1377 array_pop( $this->mArgStack );
1379 return $text;
1382 function braceSubstitution( $matches )
1384 global $wgLinkCache, $wgLang;
1385 $fname = "Parser::braceSubstitution";
1386 $found = false;
1387 $nowiki = false;
1388 $title = NULL;
1390 # $newline is an optional newline character before the braces
1391 # $part1 is the bit before the first |, and must contain only title characters
1392 # $args is a list of arguments, starting from index 0, not including $part1
1394 $newline = $matches[1];
1395 $part1 = $matches[2];
1396 # If the third subpattern matched anything, it will start with |
1397 if ( $matches[3] !== "" ) {
1398 $args = explode( "|", substr( $matches[3], 1 ) );
1399 } else {
1400 $args = array();
1402 $argc = count( $args );
1404 # SUBST
1405 $mwSubst =& MagicWord::get( MAG_SUBST );
1406 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1407 if ( $this->mOutputType != OT_WIKI ) {
1408 # Invalid SUBST not replaced at PST time
1409 # Return without further processing
1410 $text = $matches[0];
1411 $found = true;
1413 } elseif ( $this->mOutputType == OT_WIKI ) {
1414 # SUBST not found in PST pass, do nothing
1415 $text = $matches[0];
1416 $found = true;
1419 # MSG, MSGNW and INT
1420 if ( !$found ) {
1421 # Check for MSGNW:
1422 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1423 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1424 $nowiki = true;
1425 } else {
1426 # Remove obsolete MSG:
1427 $mwMsg =& MagicWord::get( MAG_MSG );
1428 $mwMsg->matchStartAndRemove( $part1 );
1431 # Check if it is an internal message
1432 $mwInt =& MagicWord::get( MAG_INT );
1433 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1434 if ( $this->incrementIncludeCount( "int:$part1" ) ) {
1435 $text = wfMsgReal( $part1, $args, true );
1436 $found = true;
1441 # NS
1442 if ( !$found ) {
1443 # Check for NS: (namespace expansion)
1444 $mwNs = MagicWord::get( MAG_NS );
1445 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1446 if ( intval( $part1 ) ) {
1447 $text = $wgLang->getNsText( intval( $part1 ) );
1448 $found = true;
1449 } else {
1450 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1451 if ( !is_null( $index ) ) {
1452 $text = $wgLang->getNsText( $index );
1453 $found = true;
1459 # LOCALURL and LOCALURLE
1460 if ( !$found ) {
1461 $mwLocal = MagicWord::get( MAG_LOCALURL );
1462 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1464 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1465 $func = 'getLocalURL';
1466 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1467 $func = 'escapeLocalURL';
1468 } else {
1469 $func = '';
1472 if ( $func !== '' ) {
1473 $title = Title::newFromText( $part1 );
1474 if ( !is_null( $title ) ) {
1475 if ( $argc > 0 ) {
1476 $text = $title->$func( $args[0] );
1477 } else {
1478 $text = $title->$func();
1480 $found = true;
1485 # Internal variables
1486 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1487 $text = $this->mVariables[$part1];
1488 $found = true;
1489 $this->mOutput->mContainsOldMagic = true;
1492 # Arguments input from the caller
1493 $inputArgs = end( $this->mArgStack );
1494 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1495 $text = $inputArgs[$part1];
1496 $found = true;
1499 # Load from database
1500 if ( !$found ) {
1501 $title = Title::newFromText( $part1, NS_TEMPLATE );
1502 if ( !is_null( $title ) && !$title->isExternal() ) {
1503 # Check for excessive inclusion
1504 $dbk = $title->getPrefixedDBkey();
1505 if ( $this->incrementIncludeCount( $dbk ) ) {
1506 $article = new Article( $title );
1507 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1508 if ( $articleContent !== false ) {
1509 $found = true;
1510 $text = $articleContent;
1515 # If the title is valid but undisplayable, make a link to it
1516 if ( $this->mOutputType == OT_HTML && !$found ) {
1517 $text = "[[" . $title->getPrefixedText() . "]]";
1518 $found = true;
1523 # Recursive parsing, escaping and link table handling
1524 # Only for HTML output
1525 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1526 $text = wfEscapeWikiText( $text );
1527 } elseif ( $this->mOutputType == OT_HTML && $found ) {
1528 # Clean up argument array
1529 $assocArgs = array();
1530 $index = 1;
1531 foreach( $args as $arg ) {
1532 $eqpos = strpos( $arg, "=" );
1533 if ( $eqpos === false ) {
1534 $assocArgs[$index++] = $arg;
1535 } else {
1536 $name = trim( substr( $arg, 0, $eqpos ) );
1537 $value = trim( substr( $arg, $eqpos+1 ) );
1538 if ( $value === false ) {
1539 $value = "";
1541 if ( $name !== false ) {
1542 $assocArgs[$name] = $value;
1547 # Do not enter included links in link table
1548 if ( !is_null( $title ) ) {
1549 $wgLinkCache->suspend();
1552 # Run full parser on the included text
1553 $text = $this->strip( $text, $this->mStripState );
1554 $text = $this->internalParse( $text, (bool)$newline, $assocArgs, false );
1555 if(!empty($newline)) $text = "\n".$text;
1557 # Add the result to the strip state for re-inclusion after
1558 # the rest of the processing
1559 $text = $this->insertStripItem( $text, $this->mStripState );
1561 # Resume the link cache and register the inclusion as a link
1562 if ( !is_null( $title ) ) {
1563 $wgLinkCache->resume();
1564 $wgLinkCache->addLinkObj( $title );
1568 if ( !$found ) {
1569 return $matches[0];
1570 } else {
1571 return $text;
1575 # Returns true if the function is allowed to include this entity
1576 function incrementIncludeCount( $dbk )
1578 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1579 $this->mIncludeCount[$dbk] = 0;
1581 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1582 return true;
1583 } else {
1584 return false;
1589 # Cleans up HTML, removes dangerous tags and attributes
1590 /* private */ function removeHTMLtags( $text )
1592 global $wgUseTidy, $wgUserHtml;
1593 $fname = "Parser::removeHTMLtags";
1594 wfProfileIn( $fname );
1596 if( $wgUserHtml ) {
1597 $htmlpairs = array( # Tags that must be closed
1598 "b", "del", "i", "ins", "u", "font", "big", "small", "sub", "sup", "h1",
1599 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1600 "strike", "strong", "tt", "var", "div", "center",
1601 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1602 "ruby", "rt" , "rb" , "rp", "p"
1604 $htmlsingle = array(
1605 "br", "hr", "li", "dt", "dd"
1607 $htmlnest = array( # Tags that can be nested--??
1608 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1609 "dl", "font", "big", "small", "sub", "sup"
1611 $tabletags = array( # Can only appear inside table
1612 "td", "th", "tr"
1614 } else {
1615 $htmlpairs = array();
1616 $htmlsingle = array();
1617 $htmlnest = array();
1618 $tabletags = array();
1621 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1622 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1624 $htmlattrs = $this->getHTMLattrs () ;
1626 # Remove HTML comments
1627 $text = preg_replace( "/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU", "$2", $text );
1629 $bits = explode( "<", $text );
1630 $text = array_shift( $bits );
1631 if(!$wgUseTidy) {
1632 $tagstack = array(); $tablestack = array();
1633 foreach ( $bits as $x ) {
1634 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1635 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1636 $x, $regs );
1637 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1638 error_reporting( $prev );
1640 $badtag = 0 ;
1641 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1642 # Check our stack
1643 if ( $slash ) {
1644 # Closing a tag...
1645 if ( ! in_array( $t, $htmlsingle ) &&
1646 ( count($tagstack) && $ot = array_pop( $tagstack ) ) != $t ) {
1647 if(!empty($ot)) array_push( $tagstack, $ot );
1648 $badtag = 1;
1649 } else {
1650 if ( $t == "table" ) {
1651 $tagstack = array_pop( $tablestack );
1653 $newparams = "";
1655 } else {
1656 # Keep track for later
1657 if ( in_array( $t, $tabletags ) &&
1658 ! in_array( "table", $tagstack ) ) {
1659 $badtag = 1;
1660 } else if ( in_array( $t, $tagstack ) &&
1661 ! in_array ( $t , $htmlnest ) ) {
1662 $badtag = 1 ;
1663 } else if ( ! in_array( $t, $htmlsingle ) ) {
1664 if ( $t == "table" ) {
1665 array_push( $tablestack, $tagstack );
1666 $tagstack = array();
1668 array_push( $tagstack, $t );
1670 # Strip non-approved attributes from the tag
1671 $newparams = $this->fixTagAttributes($params);
1674 if ( ! $badtag ) {
1675 $rest = str_replace( ">", "&gt;", $rest );
1676 $text .= "<$slash$t $newparams$brace$rest";
1677 continue;
1680 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1682 # Close off any remaining tags
1683 while ( $t = array_pop( $tagstack ) ) {
1684 $text .= "</$t>\n";
1685 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1687 } else {
1688 # this might be possible using tidy itself
1689 foreach ( $bits as $x ) {
1690 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1691 $x, $regs );
1692 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1693 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1694 $newparams = $this->fixTagAttributes($params);
1695 $rest = str_replace( ">", "&gt;", $rest );
1696 $text .= "<$slash$t $newparams$brace$rest";
1697 } else {
1698 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1702 wfProfileOut( $fname );
1703 return $text;
1709 * This function accomplishes several tasks:
1710 * 1) Auto-number headings if that option is enabled
1711 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1712 * 3) Add a Table of contents on the top for users who have enabled the option
1713 * 4) Auto-anchor headings
1715 * It loops through all headlines, collects the necessary data, then splits up the
1716 * string and re-inserts the newly formatted headlines.
1720 /* private */ function formatHeadings( $text, $isMain=true )
1722 global $wgInputEncoding;
1724 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1725 $doShowToc = $this->mOptions->getShowToc();
1726 if( !$this->mTitle->userCanEdit() ) {
1727 $showEditLink = 0;
1728 $rightClickHack = 0;
1729 } else {
1730 $showEditLink = $this->mOptions->getEditSection();
1731 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1734 # Inhibit editsection links if requested in the page
1735 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1736 if( $esw->matchAndRemove( $text ) ) {
1737 $showEditLink = 0;
1739 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1740 # do not add TOC
1741 $mw =& MagicWord::get( MAG_NOTOC );
1742 if( $mw->matchAndRemove( $text ) ) {
1743 $doShowToc = 0;
1746 # never add the TOC to the Main Page. This is an entry page that should not
1747 # be more than 1-2 screens large anyway
1748 if( $this->mTitle->getPrefixedText() == wfMsg("mainpage") ) {
1749 $doShowToc = 0;
1752 # Get all headlines for numbering them and adding funky stuff like [edit]
1753 # links - this is for later, but we need the number of headlines right now
1754 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1756 # if there are fewer than 4 headlines in the article, do not show TOC
1757 if( $numMatches < 4 ) {
1758 $doShowToc = 0;
1761 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1762 # override above conditions and always show TOC
1763 $mw =& MagicWord::get( MAG_FORCETOC );
1764 if ($mw->matchAndRemove( $text ) ) {
1765 $doShowToc = 1;
1769 # We need this to perform operations on the HTML
1770 $sk =& $this->mOptions->getSkin();
1772 # headline counter
1773 $headlineCount = 0;
1775 # Ugh .. the TOC should have neat indentation levels which can be
1776 # passed to the skin functions. These are determined here
1777 $toclevel = 0;
1778 $toc = "";
1779 $full = "";
1780 $head = array();
1781 $sublevelCount = array();
1782 $level = 0;
1783 $prevlevel = 0;
1784 foreach( $matches[3] as $headline ) {
1785 $numbering = "";
1786 if( $level ) {
1787 $prevlevel = $level;
1789 $level = $matches[1][$headlineCount];
1790 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1791 # reset when we enter a new level
1792 $sublevelCount[$level] = 0;
1793 $toc .= $sk->tocIndent( $level - $prevlevel );
1794 $toclevel += $level - $prevlevel;
1796 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1797 # reset when we step back a level
1798 $sublevelCount[$level+1]=0;
1799 $toc .= $sk->tocUnindent( $prevlevel - $level );
1800 $toclevel -= $prevlevel - $level;
1802 # count number of headlines for each level
1803 @$sublevelCount[$level]++;
1804 if( $doNumberHeadings || $doShowToc ) {
1805 $dot = 0;
1806 for( $i = 1; $i <= $level; $i++ ) {
1807 if( !empty( $sublevelCount[$i] ) ) {
1808 if( $dot ) {
1809 $numbering .= ".";
1811 $numbering .= $sublevelCount[$i];
1812 $dot = 1;
1817 # The canonized header is a version of the header text safe to use for links
1818 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1819 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
1821 # strip out HTML
1822 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1823 $tocline = trim( $canonized_headline );
1824 $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', urlencode( do_html_entity_decode( $tocline, ENT_COMPAT, $wgInputEncoding ) ) );
1825 $refer[$headlineCount] = $canonized_headline;
1827 # count how many in assoc. array so we can track dupes in anchors
1828 @$refers[$canonized_headline]++;
1829 $refcount[$headlineCount]=$refers[$canonized_headline];
1831 # Prepend the number to the heading text
1833 if( $doNumberHeadings || $doShowToc ) {
1834 $tocline = $numbering . " " . $tocline;
1836 # Don't number the heading if it is the only one (looks silly)
1837 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1838 # the two are different if the line contains a link
1839 $headline=$numbering . " " . $headline;
1843 # Create the anchor for linking from the TOC to the section
1844 $anchor = $canonized_headline;
1845 if($refcount[$headlineCount] > 1 ) {
1846 $anchor .= "_" . $refcount[$headlineCount];
1848 if( $doShowToc ) {
1849 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1851 if( $showEditLink ) {
1852 if ( empty( $head[$headlineCount] ) ) {
1853 $head[$headlineCount] = "";
1855 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
1858 # Add the edit section span
1859 if( $rightClickHack ) {
1860 $headline = $sk->editSectionScript($headlineCount+1,$headline);
1863 # give headline the correct <h#> tag
1864 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1866 $headlineCount++;
1869 if( $doShowToc ) {
1870 $toclines = $headlineCount;
1871 $toc .= $sk->tocUnindent( $toclevel );
1872 $toc = $sk->tocTable( $toc );
1875 # split up and insert constructed headlines
1877 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1878 $i = 0;
1880 foreach( $blocks as $block ) {
1881 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1882 # This is the [edit] link that appears for the top block of text when
1883 # section editing is enabled
1885 # Disabled because it broke block formatting
1886 # For example, a bullet point in the top line
1887 # $full .= $sk->editSectionLink(0);
1889 $full .= $block;
1890 if( $doShowToc && !$i && $isMain) {
1891 # Top anchor now in skin
1892 $full = $full.$toc;
1895 if( !empty( $head[$i] ) ) {
1896 $full .= $head[$i];
1898 $i++;
1901 return $full;
1904 /* private */ function doMagicISBN( &$tokenizer )
1906 global $wgLang;
1908 # Check whether next token is a text token
1909 # If yes, fetch it and convert the text into a
1910 # Special::BookSources link
1911 $token = $tokenizer->previewToken();
1912 while ( $token["type"] == "" )
1914 $tokenizer->nextToken();
1915 $token = $tokenizer->previewToken();
1917 if ( $token["type"] == "text" )
1919 $token = $tokenizer->nextToken();
1920 $x = $token["text"];
1921 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1923 $isbn = $blank = "" ;
1924 while ( " " == $x{0} ) {
1925 $blank .= " ";
1926 $x = substr( $x, 1 );
1928 while ( strstr( $valid, $x{0} ) != false ) {
1929 $isbn .= $x{0};
1930 $x = substr( $x, 1 );
1932 $num = str_replace( "-", "", $isbn );
1933 $num = str_replace( " ", "", $num );
1935 if ( "" == $num ) {
1936 $text = "ISBN $blank$x";
1937 } else {
1938 $titleObj = Title::makeTitle( NS_SPECIAL, "Booksources" );
1939 $text = "<a href=\"" .
1940 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1941 "\" class=\"internal\">ISBN $isbn</a>";
1942 $text .= $x;
1944 } else {
1945 $text = "ISBN ";
1947 return $text;
1949 /* private */ function doMagicRFC( &$tokenizer )
1951 global $wgLang;
1953 # Check whether next token is a text token
1954 # If yes, fetch it and convert the text into a
1955 # link to an RFC source
1956 $token = $tokenizer->previewToken();
1957 while ( $token["type"] == "" )
1959 $tokenizer->nextToken();
1960 $token = $tokenizer->previewToken();
1962 if ( $token["type"] == "text" )
1964 $token = $tokenizer->nextToken();
1965 $x = $token["text"];
1966 $valid = "0123456789";
1968 $rfc = $blank = "" ;
1969 while ( " " == $x{0} ) {
1970 $blank .= " ";
1971 $x = substr( $x, 1 );
1973 while ( strstr( $valid, $x{0} ) != false ) {
1974 $rfc .= $x{0};
1975 $x = substr( $x, 1 );
1978 if ( "" == $rfc ) {
1979 $text .= "RFC $blank$x";
1980 } else {
1981 $url = wfmsg( "rfcurl" );
1982 $url = str_replace( "$1", $rfc, $url);
1983 $sk =& $this->mOptions->getSkin();
1984 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1985 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1987 } else {
1988 $text = "RFC ";
1990 return $text;
1993 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1995 $this->mOptions = $options;
1996 $this->mTitle =& $title;
1997 $this->mOutputType = OT_WIKI;
1999 if ( $clearState ) {
2000 $this->clearState();
2003 $stripState = false;
2004 $pairs = array(
2005 "\r\n" => "\n",
2007 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2008 // now with regexes
2009 $pairs = array(
2010 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2011 "/<br *?>/i" => "<br/>",
2013 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2014 $text = $this->strip( $text, $stripState, false );
2015 $text = $this->pstPass2( $text, $user );
2016 $text = $this->unstrip( $text, $stripState );
2017 return $text;
2020 /* private */ function pstPass2( $text, &$user )
2022 global $wgLang, $wgLocaltimezone, $wgCurParser;
2024 # Variable replacement
2025 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2026 $text = $this->replaceVariables( $text );
2028 # Signatures
2030 $n = $user->getName();
2031 $k = $user->getOption( "nickname" );
2032 if ( "" == $k ) { $k = $n; }
2033 if(isset($wgLocaltimezone)) {
2034 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
2036 /* Note: this is an ugly timezone hack for the European wikis */
2037 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
2038 " (" . date( "T" ) . ")";
2039 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
2041 $text = preg_replace( "/~~~~~/", $d, $text );
2042 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
2043 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2044 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
2045 Namespace::getUser() ) . ":$n|$k]]", $text );
2047 # Context links: [[|name]] and [[name (context)|]]
2049 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2050 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2051 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2052 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2054 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2055 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2056 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2057 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2058 # [[ns:page (cont)|]]
2059 $context = "";
2060 $t = $this->mTitle->getText();
2061 if ( preg_match( $conpat, $t, $m ) ) {
2062 $context = $m[2];
2064 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
2065 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
2066 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
2068 if ( "" == $context ) {
2069 $text = preg_replace( $p2, "[[\\1]]", $text );
2070 } else {
2071 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2075 $mw =& MagicWord::get( MAG_SUBST );
2076 $wgCurParser = $this->fork();
2077 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2078 $this->merge( $wgCurParser );
2081 # Trim trailing whitespace
2082 # MAG_END (__END__) tag allows for trailing
2083 # whitespace to be deliberately included
2084 $text = rtrim( $text );
2085 $mw =& MagicWord::get( MAG_END );
2086 $mw->matchAndRemove( $text );
2088 return $text;
2091 # Set up some variables which are usually set up in parse()
2092 # so that an external function can call some class members with confidence
2093 function startExternalParse( &$title, $options, $outputType, $clearState = true )
2095 $this->mTitle =& $title;
2096 $this->mOptions = $options;
2097 $this->mOutputType = $outputType;
2098 if ( $clearState ) {
2099 $this->clearState();
2103 function transformMsg( $text, $options ) {
2104 global $wgTitle;
2105 static $executing = false;
2107 # Guard against infinite recursion
2108 if ( $executing ) {
2109 return $text;
2111 $executing = true;
2113 $this->mTitle = $wgTitle;
2114 $this->mOptions = $options;
2115 $this->mOutputType = OT_MSG;
2116 $this->clearState();
2117 $text = $this->replaceVariables( $text );
2119 $executing = false;
2120 return $text;
2124 class ParserOutput
2126 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2128 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2129 $containsOldMagic = false )
2131 $this->mText = $text;
2132 $this->mLanguageLinks = $languageLinks;
2133 $this->mCategoryLinks = $categoryLinks;
2134 $this->mContainsOldMagic = $containsOldMagic;
2137 function getText() { return $this->mText; }
2138 function getLanguageLinks() { return $this->mLanguageLinks; }
2139 function getCategoryLinks() { return $this->mCategoryLinks; }
2140 function containsOldMagic() { return $this->mContainsOldMagic; }
2141 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2142 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2143 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2144 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2146 function merge( $other ) {
2147 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2148 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2149 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2154 class ParserOptions
2156 # All variables are private
2157 var $mUseTeX; # Use texvc to expand <math> tags
2158 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2159 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2160 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2161 var $mAllowExternalImages; # Allow external images inline
2162 var $mSkin; # Reference to the preferred skin
2163 var $mDateFormat; # Date format index
2164 var $mEditSection; # Create "edit section" links
2165 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2166 var $mNumberHeadings; # Automatically number headings
2167 var $mShowToc; # Show table of contents
2169 function getUseTeX() { return $this->mUseTeX; }
2170 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2171 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2172 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2173 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2174 function getSkin() { return $this->mSkin; }
2175 function getDateFormat() { return $this->mDateFormat; }
2176 function getEditSection() { return $this->mEditSection; }
2177 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2178 function getNumberHeadings() { return $this->mNumberHeadings; }
2179 function getShowToc() { return $this->mShowToc; }
2181 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2182 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2183 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2184 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2185 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2186 function setSkin( $x ) { return wfSetRef( $this->mSkin, $x ); }
2187 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2188 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2189 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2190 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2191 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2193 /* static */ function newFromUser( &$user )
2195 $popts = new ParserOptions;
2196 $popts->initialiseFromUser( $user );
2197 return $popts;
2200 function initialiseFromUser( &$userInput )
2202 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2204 if ( !$userInput ) {
2205 $user = new User;
2206 $user->setLoaded( true );
2207 } else {
2208 $user =& $userInput;
2211 $this->mUseTeX = $wgUseTeX;
2212 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2213 $this->mUseDynamicDates = $wgUseDynamicDates;
2214 $this->mInterwikiMagic = $wgInterwikiMagic;
2215 $this->mAllowExternalImages = $wgAllowExternalImages;
2216 $this->mSkin =& $user->getSkin();
2217 $this->mDateFormat = $user->getOption( "date" );
2218 $this->mEditSection = $user->getOption( "editsection" );
2219 $this->mEditSectionOnRightClick = $user->getOption( "editsectiononrightclick" );
2220 $this->mNumberHeadings = $user->getOption( "numberheadings" );
2221 $this->mShowToc = $user->getOption( "showtoc" );
2227 # Regex callbacks, used in Parser::replaceVariables
2228 function wfBraceSubstitution( $matches )
2230 global $wgCurParser;
2231 return $wgCurParser->braceSubstitution( $matches );