Try to escape unaccompanied ampersands.
[lhc/web/wiklou.git] / includes / Parser.php
1 <?php
2
3 include_once('Tokenizer.php');
4
5 if( $GLOBALS['wgUseWikiHiero'] ){
6 include_once('wikihiero.php');
7 }
8
9 # PHP Parser
10 #
11 # Processes wiki markup
12 #
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.
15 #
16 # Globals used:
17 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
18 #
19 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
20 #
21 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
22 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
23 # $wgLocaltimezone
24 #
25 # * only within ParserOptions
26 #
27 #
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).
35 #
36 define( "MAX_INCLUDE_REPEAT", 5 );
37
38 # Recursion depth of variable/inclusion evaluation
39 define( "MAX_INCLUDE_PASSES", 3 );
40
41 # Allowed values for $mOutputType
42 define( "OT_HTML", 1 );
43 define( "OT_WIKI", 2 );
44 define( "OT_MSG", 3 );
45
46 # prefix for escaping, used in two functions at least
47 define( "UNIQ_PREFIX", "NaodW29");
48
49 class Parser
50 {
51 # Cleared with clearState():
52 var $mOutput, $mAutonumber, $mLastSection, $mDTopen, $mStripState = array();
53 var $mVariables, $mIncludeCount;
54
55 # Temporary:
56 var $mOptions, $mTitle, $mOutputType;
57
58 function Parser()
59 {
60 $this->clearState();
61 }
62
63 function clearState()
64 {
65 $this->mOutput = new ParserOutput;
66 $this->mAutonumber = 0;
67 $this->mLastSection = "";
68 $this->mDTopen = false;
69 $this->mVariables = false;
70 $this->mIncludeCount = array();
71 $this->mStripState = array();
72 }
73
74 # First pass--just handle <nowiki> sections, pass the rest off
75 # to doWikiPass2() which does all the real work.
76 #
77 # Returns a ParserOutput
78 #
79 function parse( $text, &$title, $options, $linestart = true, $clearState = true )
80 {
81 $fname = "Parser::parse";
82 wfProfileIn( $fname );
83
84 if ( $clearState ) {
85 $this->clearState();
86 }
87
88 $this->mOptions = $options;
89 $this->mTitle =& $title;
90 $this->mOutputType = OT_HTML;
91
92 $stripState = NULL;
93 $text = $this->strip( $text, $this->mStripState );
94 $text = $this->doWikiPass2( $text, $linestart );
95 $text = $this->unstrip( $text, $this->mStripState );
96
97 $this->mOutput->setText( $text );
98 wfProfileOut( $fname );
99 return $this->mOutput;
100 }
101
102 /* static */ function getRandomString()
103 {
104 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
105 }
106
107 # Replaces all occurences of <$tag>content</$tag> in the text
108 # with a random marker and returns the new text. the output parameter
109 # $content will be an associative array filled with data on the form
110 # $unique_marker => content.
111
112 # If $content is already set, the additional entries will be appended
113
114 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
115 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
116 if ( !$content ) {
117 $content = array( );
118 }
119 $n = 1;
120 $stripped = "";
121
122 while ( "" != $text ) {
123 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
124 $stripped .= $p[0];
125 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) {
126 $text = "";
127 } else {
128 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
129 $marker = $rnd . sprintf("%08X", $n++);
130 $content[$marker] = $q[0];
131 $stripped .= $marker;
132 $text = $q[1];
133 }
134 }
135 return $stripped;
136 }
137
138 # Strips <nowiki>, <pre> and <math>
139 # Returns the text, and fills an array with data needed in unstrip()
140 # If the $state is already a valid strip state, it adds to the state
141 #
142 function strip( $text, &$state )
143 {
144 $render = ($this->mOutputType == OT_HTML);
145 if ( $state ) {
146 $nowiki_content = $state['nowiki'];
147 $hiero_content = $state['hiero'];
148 $math_content = $state['math'];
149 $pre_content = $state['pre'];
150 $item_content = $state['item'];
151 } else {
152 $nowiki_content = array();
153 $hiero_content = array();
154 $math_content = array();
155 $pre_content = array();
156 $item_content = array();
157 }
158
159 # Replace any instances of the placeholders
160 $uniq_prefix = UNIQ_PREFIX;
161 $text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
162
163 $text = Parser::extractTags("nowiki", $text, $nowiki_content, $uniq_prefix);
164 foreach( $nowiki_content as $marker => $content ){
165 if( $render ){
166 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
167 } else {
168 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
169 }
170 }
171
172 if( $GLOBALS['wgUseWikiHiero'] ){
173 $text = Parser::extractTags("hiero", $text, $hiero_content, $uniq_prefix);
174 foreach( $hiero_content as $marker => $content ){
175 if( $render ){
176 $hiero_content[$marker] = WikiHiero( $content, WH_MODE_HTML);
177 } else {
178 $hiero_content[$marker] = "<hiero>$content</hiero>";
179 }
180 }
181 }
182
183 if( $this->mOptions->getUseTeX() ){
184 $text = Parser::extractTags("math", $text, $math_content, $uniq_prefix);
185 foreach( $math_content as $marker => $content ){
186 if( $render ){
187 $math_content[$marker] = renderMath( $content );
188 } else {
189 $math_content[$marker] = "<math>$content</math>";
190 }
191 }
192 }
193
194 $text = Parser::extractTags("pre", $text, $pre_content, $uniq_prefix);
195 foreach( $pre_content as $marker => $content ){
196 if( $render ){
197 $pre_content[$marker] = "<pre>" . wfEscapeHTMLTagsOnly( $content ) . "</pre>";
198 } else {
199 $pre_content[$marker] = "<pre>$content</pre>";
200 }
201 }
202
203 $state = array(
204 'nowiki' => $nowiki_content,
205 'hiero' => $hiero_content,
206 'math' => $math_content,
207 'pre' => $pre_content,
208 'item' => $item_content
209 );
210 return $text;
211 }
212
213 function unstrip( $text, &$state )
214 {
215 # Must expand in reverse order, otherwise nested tags will be corrupted
216 /*
217 $dicts = array( 'item', 'pre', 'math', 'hiero', 'nowiki' );
218 foreach ( $dicts as $dictName ) {
219 $content_dict = $state[$dictName];
220 foreach( $content_dict as $marker => $content ){
221 $text = str_replace( $marker, $content, $text );
222 }
223 }*/
224
225 $contentDict = end( $state );
226 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
227 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
228 $text = str_replace( key( $contentDict ), $content, $text );
229 }
230 }
231
232 return $text;
233 }
234
235 # Add an item to the strip state
236 # Returns the unique tag which must be inserted into the stripped text
237 # The tag will be replaced with the original text in unstrip()
238
239 function insertStripItem( $text, &$state )
240 {
241 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
242 if ( !$state ) {
243 $state = array(
244 'nowiki' => array(),
245 'hiero' => array(),
246 'math' => array(),
247 'pre' => array(),
248 'item' => array()
249 );
250 }
251 $state['item'][$rnd] = $text;
252 return $rnd;
253 }
254
255 function categoryMagic ()
256 {
257 global $wgLang , $wgUser ;
258 if ( !$this->mOptions->getUseCategoryMagic() ) return ;
259 $id = $this->mTitle->getArticleID() ;
260 $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
261 $ti = $this->mTitle->getText() ;
262 $ti = explode ( ":" , $ti , 2 ) ;
263 if ( $cat != $ti[0] ) return "" ;
264 $r = '<br style="clear:both;"/>\n';
265
266 $articles = array() ;
267 $parents = array () ;
268 $children = array() ;
269
270
271 # $sk =& $this->mGetSkin();
272 $sk =& $wgUser->getSkin() ;
273
274 $data = array () ;
275 $sql1 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
276 $sql2 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
277
278 $res = wfQuery ( $sql1, DB_READ ) ;
279 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
280
281 $res = wfQuery ( $sql2, DB_READ ) ;
282 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
283
284
285 foreach ( $data AS $x )
286 {
287 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
288 if ( $t != "" ) $t .= ":" ;
289 $t .= $x->cur_title ;
290
291 $y = explode ( ":" , $t , 2 ) ;
292 if ( count ( $y ) == 2 && $y[0] == $cat ) {
293 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
294 } else {
295 array_push ( $articles , $sk->makeLink ( $t ) ) ;
296 }
297 }
298 wfFreeResult ( $res ) ;
299
300 # Children
301 if ( count ( $children ) > 0 )
302 {
303 asort ( $children ) ;
304 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
305 $r .= implode ( ", " , $children ) ;
306 }
307
308 # Articles
309 if ( count ( $articles ) > 0 )
310 {
311 asort ( $articles ) ;
312 $h = wfMsg( "category_header", $ti[1] );
313 $r .= "<h2>{$h}</h2>\n" ;
314 $r .= implode ( ", " , $articles ) ;
315 }
316
317
318 return $r ;
319 }
320
321 function getHTMLattrs ()
322 {
323 $htmlattrs = array( # Allowed attributes--no scripting, etc.
324 "title", "align", "lang", "dir", "width", "height",
325 "bgcolor", "clear", /* BR */ "noshade", /* HR */
326 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
327 /* FONT */ "type", "start", "value", "compact",
328 /* For various lists, mostly deprecated but safe */
329 "summary", "width", "border", "frame", "rules",
330 "cellspacing", "cellpadding", "valign", "char",
331 "charoff", "colgroup", "col", "span", "abbr", "axis",
332 "headers", "scope", "rowspan", "colspan", /* Tables */
333 "id", "class", "name", "style" /* For CSS */
334 );
335 return $htmlattrs ;
336 }
337
338 function fixTagAttributes ( $t )
339 {
340 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
341 $htmlattrs = $this->getHTMLattrs() ;
342
343 # Strip non-approved attributes from the tag
344 $t = preg_replace(
345 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
346 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
347 $t);
348 # Strip javascript "expression" from stylesheets. Brute force approach:
349 # If anythin offensive is found, all attributes of the HTML tag are dropped
350
351 if( preg_match(
352 "/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is",
353 wfMungeToUtf8( $t ) ) )
354 {
355 $t="";
356 }
357
358 return trim ( $t ) ;
359 }
360
361 function doTableStuff ( $t )
362 {
363 $t = explode ( "\n" , $t ) ;
364 $td = array () ; # Is currently a td tag open?
365 $ltd = array () ; # Was it TD or TH?
366 $tr = array () ; # Is currently a tr tag open?
367 $ltr = array () ; # tr attributes
368 foreach ( $t AS $k => $x )
369 {
370 $x = rtrim ( $x ) ;
371 $fc = substr ( $x , 0 , 1 ) ;
372 if ( "{|" == substr ( $x , 0 , 2 ) )
373 {
374 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . ">" ;
375 array_push ( $td , false ) ;
376 array_push ( $ltd , "" ) ;
377 array_push ( $tr , false ) ;
378 array_push ( $ltr , "" ) ;
379 }
380 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
381 else if ( "|}" == substr ( $x , 0 , 2 ) )
382 {
383 $z = "</table>\n" ;
384 $l = array_pop ( $ltd ) ;
385 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
386 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
387 array_pop ( $ltr ) ;
388 $t[$k] = $z ;
389 }
390 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
391 {
392 $z = trim ( substr ( $x , 2 ) ) ;
393 $t[$k] = "<caption>{$z}</caption>\n" ;
394 }*/
395 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
396 {
397 $x = substr ( $x , 1 ) ;
398 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
399 $z = "" ;
400 $l = array_pop ( $ltd ) ;
401 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
402 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
403 array_pop ( $ltr ) ;
404 $t[$k] = $z ;
405 array_push ( $tr , false ) ;
406 array_push ( $td , false ) ;
407 array_push ( $ltd , "" ) ;
408 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
409 }
410 else if ( "|" == $fc || "!" == $fc || "|+" == substr ( $x , 0 , 2 ) ) # Caption
411 {
412 if ( "|+" == substr ( $x , 0 , 2 ) )
413 {
414 $fc = "+" ;
415 $x = substr ( $x , 1 ) ;
416 }
417 $after = substr ( $x , 1 ) ;
418 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
419 $after = explode ( "||" , $after ) ;
420 $t[$k] = "" ;
421 foreach ( $after AS $theline )
422 {
423 $z = "" ;
424 if ( $fc != "+" )
425 {
426 $tra = array_pop ( $ltr ) ;
427 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
428 array_push ( $tr , true ) ;
429 array_push ( $ltr , "" ) ;
430 }
431
432 $l = array_pop ( $ltd ) ;
433 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
434 if ( $fc == "|" ) $l = "td" ;
435 else if ( $fc == "!" ) $l = "th" ;
436 else if ( $fc == "+" ) $l = "caption" ;
437 else $l = "" ;
438 array_push ( $ltd , $l ) ;
439 $y = explode ( "|" , $theline , 2 ) ;
440 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
441 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
442 $t[$k] .= $y ;
443 array_push ( $td , true ) ;
444 }
445 }
446 }
447
448 # Closing open td, tr && table
449 while ( count ( $td ) > 0 )
450 {
451 if ( array_pop ( $td ) ) $t[] = "</td>" ;
452 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
453 $t[] = "</table>" ;
454 }
455
456 $t = implode ( "\n" , $t ) ;
457 # $t = $this->removeHTMLtags( $t );
458 return $t ;
459 }
460
461 # Well, OK, it's actually about 14 passes. But since all the
462 # hard lifting is done inside PHP's regex code, it probably
463 # wouldn't speed things up much to add a real parser.
464 #
465 function doWikiPass2( $text, $linestart )
466 {
467 $fname = "Parser::doWikiPass2";
468 wfProfileIn( $fname );
469
470 $text = $this->removeHTMLtags( $text );
471 $text = $this->replaceVariables( $text );
472
473 # $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
474
475 $text = $this->doHeadings( $text );
476
477 if($this->mOptions->getUseDynamicDates()) {
478 global $wgDateFormatter;
479 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
480 }
481
482 $text = $this->replaceExternalLinks( $text );
483 $text = $this->doTokenizedParser ( $text );
484
485 $text = $this->doTableStuff ( $text ) ;
486
487 $text = $this->formatHeadings( $text );
488
489 $sk =& $this->mOptions->getSkin();
490 $text = $sk->transformContent( $text );
491 $fixtags = array(
492 "/<hr *>/i" => '<hr/>',
493 "/<br *>/i" => '<br/>',
494 "/<center *>/i"=>'<span style="text-align:center;">',
495 "/<\\/center *>/i" => '</span>'
496 );
497 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
498
499 # Clean up spare ampersands; note that we probably ought to be
500 # more careful about named entities.
501 $text = preg_replace(
502 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/',
503 '&amp;',
504 $text );
505
506 $text .= $this->categoryMagic () ;
507
508 # needs to be called last
509 $text = $this->doBlockLevels( $text, $linestart );
510
511 wfProfileOut( $fname );
512 return $text;
513 }
514
515
516 /* private */ function doHeadings( $text )
517 {
518 for ( $i = 6; $i >= 1; --$i ) {
519 $h = substr( "======", 0, $i );
520 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
521 "<h{$i}>\\1</h{$i}>\\2", $text );
522 }
523 return $text;
524 }
525
526 # Note: we have to do external links before the internal ones,
527 # and otherwise take great care in the order of things here, so
528 # that we don't end up interpreting some URLs twice.
529
530 /* private */ function replaceExternalLinks( $text )
531 {
532 $fname = "Parser::replaceExternalLinks";
533 wfProfileIn( $fname );
534 $text = $this->subReplaceExternalLinks( $text, "http", true );
535 $text = $this->subReplaceExternalLinks( $text, "https", true );
536 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
537 $text = $this->subReplaceExternalLinks( $text, "irc", false );
538 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
539 $text = $this->subReplaceExternalLinks( $text, "news", false );
540 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
541 wfProfileOut( $fname );
542 return $text;
543 }
544
545 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
546 {
547 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
548 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
549
550 # this is the list of separators that should be ignored if they
551 # are the last character of an URL but that should be included
552 # if they occur within the URL, e.g. "go to www.foo.com, where .."
553 # in this case, the last comma should not become part of the URL,
554 # but in "www.foo.com/123,2342,32.htm" it should.
555 $sep = ",;\.:";
556 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
557 $images = "gif|png|jpg|jpeg";
558
559 # PLEASE NOTE: The curly braces { } are not part of the regex,
560 # they are interpreted as part of the string (used to tell PHP
561 # that the content of the string should be inserted there).
562 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
563 "((?i){$images})([^{$uc}]|$)/";
564
565 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
566 $sk =& $this->mOptions->getSkin();
567
568 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
569 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
570 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
571 }
572 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
573 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
574 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
575 "</a>\\5", $s );
576 $s = str_replace( $unique, $protocol, $s );
577
578 $a = explode( "[{$protocol}:", " " . $s );
579 $s = array_shift( $a );
580 $s = substr( $s, 1 );
581
582 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
583 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
584
585 foreach ( $a as $line ) {
586 if ( preg_match( $e1, $line, $m ) ) {
587 $link = "{$protocol}:{$m[1]}";
588 $trail = $m[2];
589 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
590 else { $text = wfEscapeHTML( $link ); }
591 } else if ( preg_match( $e2, $line, $m ) ) {
592 $link = "{$protocol}:{$m[1]}";
593 $text = $m[2];
594 $trail = $m[3];
595 } else {
596 $s .= "[{$protocol}:" . $line;
597 continue;
598 }
599 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
600 $paren = "";
601 } else {
602 # Expand the URL for printable version
603 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
604 }
605 $la = $sk->getExternalLinkAttributes( $link, $text );
606 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
607
608 }
609 return $s;
610 }
611
612 /* private */ function handle3Quotes( &$state, $token )
613 {
614 if ( $state["strong"] !== false ) {
615 if ( $state["em"] !== false && $state["em"] > $state["strong"] )
616 {
617 # ''' lala ''lala '''
618 $s = "</em></strong><em>";
619 } else {
620 $s = "</strong>";
621 }
622 $state["strong"] = FALSE;
623 } else {
624 $s = "<strong>";
625 $state["strong"] = $token["pos"];
626 }
627 return $s;
628 }
629
630 /* private */ function handle2Quotes( &$state, $token )
631 {
632 if ( $state["em"] !== false ) {
633 if ( $state["strong"] !== false && $state["strong"] > $state["em"] )
634 {
635 # ''lala'''lala'' ....'''
636 $s = "</strong></em><strong>";
637 } else {
638 $s = "</em>";
639 }
640 $state["em"] = FALSE;
641 } else {
642 $s = "<em>";
643 $state["em"] = $token["pos"];
644 }
645 return $s;
646 }
647
648 /* private */ function handle5Quotes( &$state, $token )
649 {
650 $s = "";
651 if ( $state["em"] !== false && $state["strong"] !== false ) {
652 if ( $state["em"] < $state["strong"] ) {
653 $s .= "</strong></em>";
654 } else {
655 $s .= "</em></strong>";
656 }
657 $state["strong"] = $state["em"] = FALSE;
658 } elseif ( $state["em"] !== false ) {
659 $s .= "</em><strong>";
660 $state["em"] = FALSE;
661 $state["strong"] = $token["pos"];
662 } elseif ( $state["strong"] !== false ) {
663 $s .= "</strong><em>";
664 $state["strong"] = FALSE;
665 $state["em"] = $token["pos"];
666 } else { # not $em and not $strong
667 $s .= "<strong><em>";
668 $state["strong"] = $state["em"] = $token["pos"];
669 }
670 return $s;
671 }
672
673 /* private */ function doTokenizedParser( $str )
674 {
675 global $wgLang; # for language specific parser hook
676
677 $tokenizer=Tokenizer::newFromString( $str );
678 $tokenStack = array();
679
680 $s="";
681 $state["em"] = FALSE;
682 $state["strong"] = FALSE;
683 $tagIsOpen = FALSE;
684 $threeopen = false;
685
686 # The tokenizer splits the text into tokens and returns them one by one.
687 # Every call to the tokenizer returns a new token.
688 while ( $token = $tokenizer->nextToken() )
689 {
690 switch ( $token["type"] )
691 {
692 case "text":
693 # simple text with no further markup
694 $txt = $token["text"];
695 break;
696 case "[[[":
697 # remember the tag opened with 3 [
698 $threeopen = true;
699 case "[[":
700 # link opening tag.
701 # FIXME : Treat orphaned open tags (stack not empty when text is over)
702 $tagIsOpen = TRUE;
703 array_push( $tokenStack, $token );
704 $txt="";
705 break;
706
707 case "]]]":
708 case "]]":
709 # link close tag.
710 # get text from stack, glue it together, and call the code to handle a
711 # link
712
713 if ( count( $tokenStack ) == 0 )
714 {
715 # stack empty. Found a ]] without an opening [[
716 $txt = "]]";
717 } else {
718 $linkText = "";
719 $lastToken = array_pop( $tokenStack );
720 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
721 {
722 if( !empty( $lastToken["text"] ) ) {
723 $linkText = $lastToken["text"] . $linkText;
724 }
725 $lastToken = array_pop( $tokenStack );
726 }
727
728 $txt = $linkText ."]]";
729
730 if( isset( $lastToken["text"] ) ) {
731 $prefix = $lastToken["text"];
732 } else {
733 $prefix = "";
734 }
735 $nextToken = $tokenizer->previewToken();
736 if ( $nextToken["type"] == "text" )
737 {
738 # Preview just looks at it. Now we have to fetch it.
739 $nextToken = $tokenizer->nextToken();
740 $txt .= $nextToken["text"];
741 }
742 $fakestate = $this->mStripState;
743 $txt = $this->handleInternalLink( $this->unstrip($txt,$fakestate), $prefix );
744
745 # did the tag start with 3 [ ?
746 if($threeopen) {
747 # show the first as text
748 $txt = "[".$txt;
749 $threeopen=false;
750 }
751
752 }
753 $tagIsOpen = (count( $tokenStack ) != 0);
754 break;
755 case "----":
756 $txt = "\n<hr />\n";
757 break;
758 case "'''":
759 # This and the three next ones handle quotes
760 $txt = $this->handle3Quotes( $state, $token );
761 break;
762 case "''":
763 $txt = $this->handle2Quotes( $state, $token );
764 break;
765 case "'''''":
766 $txt = $this->handle5Quotes( $state, $token );
767 break;
768 case "":
769 # empty token
770 $txt="";
771 break;
772 case "RFC ":
773 if ( $tagIsOpen ) {
774 $txt = "RFC ";
775 } else {
776 $txt = $this->doMagicRFC( $tokenizer );
777 }
778 break;
779 case "ISBN ":
780 if ( $tagIsOpen ) {
781 $txt = "ISBN ";
782 } else {
783 $txt = $this->doMagicISBN( $tokenizer );
784 }
785 break;
786 default:
787 # Call language specific Hook.
788 $txt = $wgLang->processToken( $token, $tokenStack );
789 if ( NULL == $txt ) {
790 # An unkown token. Highlight.
791 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
792 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
793 }
794 break;
795 }
796 # If we're parsing the interior of a link, don't append the interior to $s,
797 # but push it to the stack so it can be processed when a ]] token is found.
798 if ( $tagIsOpen && $txt != "" ) {
799 $token["type"] = "text";
800 $token["text"] = $txt;
801 array_push( $tokenStack, $token );
802 } else {
803 $s .= $txt;
804 }
805 } #end while
806 if ( count( $tokenStack ) != 0 )
807 {
808 # still objects on stack. opened [[ tag without closing ]] tag.
809 $txt = "";
810 while ( $lastToken = array_pop( $tokenStack ) )
811 {
812 if ( $lastToken["type"] == "text" )
813 {
814 $txt = $lastToken["text"] . $txt;
815 } else {
816 $txt = $lastToken["type"] . $txt;
817 }
818 }
819 $s .= $txt;
820 }
821 return $s;
822 }
823
824 /* private */ function handleInternalLink( $line, $prefix )
825 {
826 global $wgLang, $wgLinkCache;
827 global $wgNamespacesWithSubpages, $wgLanguageCode;
828 static $fname = "Parser::handleInternalLink" ;
829 wfProfileIn( $fname );
830
831 wfProfileIn( "$fname-setup" );
832 static $tc = FALSE;
833 if ( !$tc ) { $tc = Title::legalChars() . "#"; }
834 $sk =& $this->mOptions->getSkin();
835
836 # Match a link having the form [[namespace:link|alternate]]trail
837 static $e1 = FALSE;
838 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
839 # Match the end of a line for a word that's not followed by whitespace,
840 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
841 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
842 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
843 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
844
845
846 # Special and Media are pseudo-namespaces; no pages actually exist in them
847 static $image = FALSE;
848 static $special = FALSE;
849 static $media = FALSE;
850 static $category = FALSE;
851 if ( !$image ) { $image = Namespace::getImage(); }
852 if ( !$special ) { $special = Namespace::getSpecial(); }
853 if ( !$media ) { $media = Namespace::getMedia(); }
854 if ( !$category ) { $category = wfMsg ( "category" ) ; }
855
856 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
857
858 wfProfileOut( "$fname-setup" );
859 $s = "";
860
861 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
862 $text = $m[2];
863 $trail = $m[3];
864 } else { # Invalid form; output directly
865 $s .= $prefix . "[[" . $line ;
866 return $s;
867 }
868
869 /* Valid link forms:
870 Foobar -- normal
871 :Foobar -- override special treatment of prefix (images, language links)
872 /Foobar -- convert to CurrentPage/Foobar
873 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
874 */
875 $c = substr($m[1],0,1);
876 $noforce = ($c != ":");
877 if( $c == "/" ) { # subpage
878 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
879 $m[1]=substr($m[1],1,strlen($m[1])-2);
880 $noslash=$m[1];
881 } else {
882 $noslash=substr($m[1],1);
883 }
884 if($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]) { # subpages allowed here
885 $link = $this->mTitle->getPrefixedText(). "/" . trim($noslash);
886 if( "" == $text ) {
887 $text= $m[1];
888 } # this might be changed for ugliness reasons
889 } else {
890 $link = $noslash; # no subpage allowed, use standard link
891 }
892 } elseif( $noforce ) { # no subpage
893 $link = $m[1];
894 } else {
895 $link = substr( $m[1], 1 );
896 }
897 if( "" == $text )
898 $text = $link;
899
900 $nt = Title::newFromText( $link );
901 if( !$nt ) {
902 $s .= $prefix . "[[" . $line;
903 return $s;
904 }
905 $ns = $nt->getNamespace();
906 $iw = $nt->getInterWiki();
907 if( $noforce ) {
908 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
909 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
910 return (trim($s) == '')? '': $s;
911 }
912 if( $ns == $image ) {
913 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
914 $wgLinkCache->addImageLinkObj( $nt );
915 return $s;
916 }
917 }
918 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
919 ( strpos( $link, "#" ) == FALSE ) ) {
920 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
921 return $s;
922 }
923
924 # Category feature
925 $catns = strtoupper ( $nt->getDBkey () ) ;
926 $catns = explode ( ":" , $catns ) ;
927 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
928 else $catns = "" ;
929 if ( $catns == strtoupper($category) && $this->mOptions->getUseCategoryMagic() ) {
930 $t = explode ( ":" , $nt->getText() ) ;
931 array_shift ( $t ) ;
932 $t = implode ( ":" , $t ) ;
933 $t = $wgLang->ucFirst ( $t ) ;
934 $nnt = Title::newFromText ( $category.":".$t ) ;
935 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
936 $this->mOutput->mCategoryLinks[] = $t ;
937 $s .= $prefix . $trail ;
938 return $s ;
939 }
940
941 if( $ns == $media ) {
942 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
943 $wgLinkCache->addImageLinkObj( $nt );
944 return $s;
945 } elseif( $ns == $special ) {
946 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
947 return $s;
948 }
949 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
950
951 wfProfileOut( $fname );
952 return $s;
953 }
954
955 # Some functions here used by doBlockLevels()
956 #
957 /* private */ function closeParagraph()
958 {
959 $result = "";
960 if ( '' != $this->mLastSection ) {
961 $result = "</" . $this->mLastSection . ">\n";
962 }
963 $this->mLastSection = "";
964 return $result;
965 }
966 # getCommon() returns the length of the longest common substring
967 # of both arguments, starting at the beginning of both.
968 #
969 /* private */ function getCommon( $st1, $st2 )
970 {
971 $fl = strlen( $st1 );
972 $shorter = strlen( $st2 );
973 if ( $fl < $shorter ) { $shorter = $fl; }
974
975 for ( $i = 0; $i < $shorter; ++$i ) {
976 if ( $st1{$i} != $st2{$i} ) { break; }
977 }
978 return $i;
979 }
980 # These next three functions open, continue, and close the list
981 # element appropriate to the prefix character passed into them.
982 #
983 /* private */ function openList( $char )
984 {
985 $result = $this->closeParagraph();
986
987 if ( "*" == $char ) { $result .= "<ul><li>"; }
988 else if ( "#" == $char ) { $result .= "<ol><li>"; }
989 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
990 else if ( ";" == $char ) {
991 $result .= "<dl><dt>";
992 $this->mDTopen = true;
993 }
994 else { $result = "<!-- ERR 1 -->"; }
995
996 return $result;
997 }
998
999 /* private */ function nextItem( $char )
1000 {
1001 if ( "*" == $char || "#" == $char ) { return "</li><li>"; }
1002 else if ( ":" == $char || ";" == $char ) {
1003 $close = "</dd>";
1004 if ( $this->mDTopen ) { $close = "</dt>"; }
1005 if ( ";" == $char ) {
1006 $this->mDTopen = true;
1007 return $close . "<dt>";
1008 } else {
1009 $this->mDTopen = false;
1010 return $close . "<dd>";
1011 }
1012 }
1013 return "<!-- ERR 2 -->";
1014 }
1015
1016 /* private */function closeList( $char )
1017 {
1018 if ( "*" == $char ) { $text = "</li></ul>"; }
1019 else if ( "#" == $char ) { $text = "</li></ol>"; }
1020 else if ( ":" == $char ) {
1021 if ( $this->mDTopen ) {
1022 $this->mDTopen = false;
1023 $text = "</dt></dl>";
1024 } else {
1025 $text = "</dd></dl>";
1026 }
1027 }
1028 else { return "<!-- ERR 3 -->"; }
1029 return $text."\n";
1030 }
1031
1032 /* private */ function doBlockLevels( $text, $linestart )
1033 {
1034 $fname = "Parser::doBlockLevels";
1035 wfProfileIn( $fname );
1036 # Parsing through the text line by line. The main thing
1037 # happening here is handling of block-level elements p, pre,
1038 # and making lists from lines starting with * # : etc.
1039 #
1040 $a = explode( "\n", $text );
1041 $lastPref = $text = $lastLine = '';
1042 $this->mDTopen = $inBlockElem = false;
1043
1044 if ( ! $linestart ) { $text .= array_shift( $a ); }
1045 foreach ( $a as $t ) {
1046 if ( "" != $text ) { $text .= "\n"; }
1047
1048 $oLine = $t;
1049 $opl = strlen( $lastPref );
1050 $npl = strspn( $t, "*#:;" );
1051 $pref = substr( $t, 0, $npl );
1052 $pref2 = str_replace( ";", ":", $pref );
1053 $t = substr( $t, $npl );
1054
1055 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
1056 $text .= $this->nextItem( substr( $pref, -1 ) );
1057
1058 if ( ";" == substr( $pref, -1 ) ) {
1059 $cpos = strpos( $t, ":" );
1060 if ( ! ( false === $cpos ) ) {
1061 $term = substr( $t, 0, $cpos );
1062 $text .= $term . $this->nextItem( ":" );
1063 $t = substr( $t, $cpos + 1 );
1064 }
1065 }
1066 } else if (0 != $npl || 0 != $opl) {
1067 $cpl = $this->getCommon( $pref, $lastPref );
1068
1069 while ( $cpl < $opl ) {
1070 $text .= $this->closeList( $lastPref{$opl-1} );
1071 --$opl;
1072 }
1073 if ( $npl <= $cpl && $cpl > 0 ) {
1074 $text .= $this->nextItem( $pref{$cpl-1} );
1075 }
1076 while ( $npl > $cpl ) {
1077 $char = substr( $pref, $cpl, 1 );
1078 $text .= $this->openList( $char );
1079
1080 if ( ";" == $char ) {
1081 $cpos = strpos( $t, ":" );
1082 if ( ! ( false === $cpos ) ) {
1083 $term = substr( $t, 0, $cpos );
1084 $text .= $term . $this->nextItem( ":" );
1085 $t = substr( $t, $cpos + 1 );
1086 }
1087 }
1088 ++$cpl;
1089 }
1090 $lastPref = $pref2;
1091 }
1092 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1093 $uniq_prefix = UNIQ_PREFIX;
1094 // XXX: use a stack for nestable elements like span, table and div
1095 $openmatch = preg_match("/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre|<tr|<td|<p)/i", $t );
1096 $closematch = preg_match(
1097 "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|".
1098 "<\\/div|<hr|<\\/td|<\\/pre|<\\/p|".$uniq_prefix."-pre)/i", $t );
1099 if ( $openmatch or $closematch ) {
1100 $text .= $this->closeParagraph();
1101 if ( $closematch ) {
1102 $inBlockElem = false;
1103 } else {
1104 $inBlockElem = true;
1105 }
1106 } else if ( !$inBlockElem ) {
1107 if ( " " == $t{0} ) {
1108 $newSection = "pre";
1109 if ($this->mLastSection != 'pre') {
1110 $text .= $this->closeParagraph();
1111 $text .= "<" . $newSection . ">";
1112 $this->mLastSection = $newSection;
1113 }
1114 } else {
1115 $newSection = "p";
1116 if ( '' == trim($t) ) {
1117 if ( '' == trim($lastLine) ) {
1118 $text .= $this->closeParagraph();
1119 $text .= "<" . $newSection . "><br/>";
1120 $this->mLastSection = $newSection;
1121 } else {
1122 $t = '';
1123 }
1124 } else if ($this->mLastSection != $newSection) {
1125 $text .= $this->closeParagraph();
1126 $text .= "<" . $newSection . ">";
1127 $this->mLastSection = $newSection;
1128 }
1129 }
1130
1131 }
1132 }
1133 $lastLine = $t;
1134 $text .= $t;
1135 }
1136 while ( $npl ) {
1137 $text .= $this->closeList( $pref2{$npl-1} );
1138 --$npl;
1139 }
1140 if ( "" != $this->mLastSection ) {
1141 $text .= "</" . $this->mLastSection . ">";
1142 $this->mLastSection = "";
1143 }
1144 wfProfileOut( $fname );
1145 return $text;
1146 }
1147
1148 function getVariableValue( $index ) {
1149 global $wgLang, $wgSitename, $wgServer;
1150
1151 switch ( $index ) {
1152 case MAG_CURRENTMONTH:
1153 return date( "m" );
1154 case MAG_CURRENTMONTHNAME:
1155 return $wgLang->getMonthName( date("n") );
1156 case MAG_CURRENTMONTHNAMEGEN:
1157 return $wgLang->getMonthNameGen( date("n") );
1158 case MAG_CURRENTDAY:
1159 return date("j");
1160 case MAG_CURRENTDAYNAME:
1161 return $wgLang->getWeekdayName( date("w")+1 );
1162 case MAG_CURRENTYEAR:
1163 return date( "Y" );
1164 case MAG_CURRENTTIME:
1165 return $wgLang->time( wfTimestampNow(), false );
1166 case MAG_NUMBEROFARTICLES:
1167 return wfNumberOfArticles();
1168 case MAG_SITENAME:
1169 return $wgSitename;
1170 case MAG_SERVER:
1171 return $wgServer;
1172 default:
1173 return NULL;
1174 }
1175 }
1176
1177 function initialiseVariables()
1178 {
1179 global $wgVariableIDs;
1180 $this->mVariables = array();
1181 foreach ( $wgVariableIDs as $id ) {
1182 $mw =& MagicWord::get( $id );
1183 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1184 }
1185 }
1186
1187 /* private */ function replaceVariables( $text )
1188 {
1189 global $wgLang, $wgCurParser;
1190 global $wgScript, $wgArticlePath;
1191
1192 $fname = "Parser::replaceVariables";
1193 wfProfileIn( $fname );
1194
1195 $bail = false;
1196 if ( !$this->mVariables ) {
1197 $this->initialiseVariables();
1198 }
1199 $titleChars = Title::legalChars();
1200 $regex = "/{{([$titleChars\\|]*?)}}/s";
1201
1202 # "Recursive" variable expansion: run it through a couple of passes
1203 for ( $i=0; $i<MAX_INCLUDE_REPEAT && !$bail; $i++ ) {
1204 $oldText = $text;
1205
1206 # It's impossible to rebind a global in PHP
1207 # Instead, we run the substitution on a copy, then merge the changed fields back in
1208 $wgCurParser = $this->fork();
1209
1210 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1211 if ( $oldText == $text ) {
1212 $bail = true;
1213 }
1214 $this->merge( $wgCurParser );
1215 }
1216
1217 return $text;
1218 }
1219
1220 # Returns a copy of this object except with various variables cleared
1221 # This copy can be re-merged with the parent after operations on the copy
1222 function fork()
1223 {
1224 $copy = $this;
1225 $copy->mOutput = new ParserOutput;
1226 return $copy;
1227 }
1228
1229 # Merges a copy split off with fork()
1230 function merge( &$copy )
1231 {
1232 # Output objects
1233 $this->mOutput->merge( $copy->mOutput );
1234
1235 # Include throttling arrays
1236 foreach( $copy->mIncludeCount as $dbk => $count ) {
1237 if ( array_key_exists( $dbk, $this->mIncludeCount ) ) {
1238 $this->mIncludeCount[$dbk] += $count;
1239 } else {
1240 $this->mIncludeCount[$dbk] = $count;
1241 }
1242 }
1243
1244 # Strip states
1245 foreach( $copy->mStripState as $dictName => $contentDict ) {
1246 $this->mStripState[$dictName] += $contentDict;
1247 }
1248 }
1249
1250 function braceSubstitution( $matches )
1251 {
1252 global $wgLinkCache, $wgLang;
1253 $fname = "Parser::braceSubstitution";
1254 $found = false;
1255 $nowiki = false;
1256
1257 $text = $matches[1];
1258
1259 # SUBST
1260 $mwSubst =& MagicWord::get( MAG_SUBST );
1261 if ( $mwSubst->matchStartAndRemove( $text ) ) {
1262 if ( $this->mOutputType != OT_WIKI ) {
1263 # Invalid SUBST not replaced at PST time
1264 # Return without further processing
1265 $text = $matches[0];
1266 $found = true;
1267 }
1268 } elseif ( $this->mOutputType == OT_WIKI ) {
1269 # SUBST not found in PST pass, do nothing
1270 $text = $matches[0];
1271 $found = true;
1272 }
1273
1274 # MSG, MSGNW and INT
1275 if ( !$found ) {
1276 # Check for MSGNW:
1277 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1278 if ( $mwMsgnw->matchStartAndRemove( $text ) ) {
1279 $nowiki = true;
1280 } else {
1281 # Remove obsolete MSG:
1282 $mwMsg =& MagicWord::get( MAG_MSG );
1283 $mwMsg->matchStartAndRemove( $text );
1284 }
1285
1286 # Check if it is an internal message
1287 $mwInt =& MagicWord::get( MAG_INT );
1288 if ( $mwInt->matchStartAndRemove( $text ) ) {
1289 $text = wfMsg( $text );
1290 $found = true;
1291 }
1292 }
1293
1294 # NS
1295 if ( !$found ) {
1296 # Check for NS: (namespace expansion)
1297 $mwNs = MagicWord::get( MAG_NS );
1298 if ( $mwNs->matchStartAndRemove( $text ) ) {
1299 if ( intval( $text ) ) {
1300 $text = $wgLang->getNsText( intval( $text ) );
1301 $found = true;
1302 } else {
1303 $index = Namespace::getCanonicalIndex( strtolower( $text ) );
1304 if ( !is_null( $index ) ) {
1305 $text = $wgLang->getNsText( $index );
1306 $found = true;
1307 }
1308 }
1309 }
1310 }
1311
1312 # LOCALURL and LOCALURLE
1313 if ( !$found ) {
1314 $mwLocal = MagicWord::get( MAG_LOCALURL );
1315 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1316
1317 if ( $mwLocal->matchStartAndRemove( $text ) ) {
1318 $func = 'getLocalURL';
1319 } elseif ( $mwLocalE->matchStartAndRemove( $text ) ) {
1320 $func = 'escapeLocalURL';
1321 } else {
1322 $func = '';
1323 }
1324
1325 if ( $func !== '' ) {
1326 $args = explode( "|", $text );
1327 $n = count( $args );
1328 if ( $n > 0 ) {
1329 $title = Title::newFromText( $args[0] );
1330 if ( !is_null( $title ) ) {
1331 if ( $n > 1 ) {
1332 $text = $title->$func( $args[1] );
1333 } else {
1334 $text = $title->$func();
1335 }
1336 $found = true;
1337 }
1338 }
1339 }
1340 }
1341
1342 # Check for a match against internal variables
1343 if ( !$found && array_key_exists( $text, $this->mVariables ) ) {
1344 $text = $this->mVariables[$text];
1345 $found = true;
1346 $this->mOutput->mContainsOldMagic = true;
1347 }
1348
1349 # Load from database
1350 if ( !$found ) {
1351 $title = Title::newFromText( $text, NS_TEMPLATE );
1352 if ( is_object( $title ) && !$title->isExternal() ) {
1353 # Check for excessive inclusion
1354 $dbk = $title->getPrefixedDBkey();
1355 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1356 $this->mIncludeCount[$dbk] = 0;
1357 }
1358 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1359 $article = new Article( $title );
1360 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1361 if ( $articleContent !== false ) {
1362 $found = true;
1363 $text = $articleContent;
1364
1365 # Escaping and link table handling
1366 # Not required for preSaveTransform()
1367 if ( $this->mOutputType == OT_HTML ) {
1368 if ( $nowiki ) {
1369 $text = wfEscapeWikiText( $text );
1370 } else {
1371 $text = $this->removeHTMLtags( $text );
1372 }
1373 # Do not enter included links in link table
1374 $wgLinkCache->suspend();
1375
1376 # Run full parser on the included text
1377 $text = $this->strip( $text, $this->mStripState );
1378 $text = $this->doWikiPass2( $text, true );
1379
1380 # Add the result to the strip state for re-inclusion after
1381 # the rest of the processing
1382 $text = $this->insertStripItem( $text, $this->mStripState );
1383
1384 # Resume the link cache and register the inclusion as a link
1385 $wgLinkCache->resume();
1386 $wgLinkCache->addLinkObj( $title );
1387
1388 }
1389 }
1390 }
1391
1392 # If the title is valid but undisplayable, make a link to it
1393 if ( $this->mOutputType == OT_HTML && !$found ) {
1394 $text = "[[" . $title->getPrefixedText() . "]]";
1395 $found = true;
1396 }
1397 }
1398 }
1399
1400 if ( !$found ) {
1401 return $matches[0];
1402 } else {
1403 return $text;
1404 }
1405 }
1406
1407 # Cleans up HTML, removes dangerous tags and attributes
1408 /* private */ function removeHTMLtags( $text )
1409 {
1410 $fname = "Parser::removeHTMLtags";
1411 wfProfileIn( $fname );
1412 $htmlpairs = array( # Tags that must be closed
1413 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1414 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1415 "strike", "strong", "tt", "var", "div", "center",
1416 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1417 "ruby", "rt" , "rb" , "rp", "p"
1418 );
1419 $htmlsingle = array(
1420 "br", "hr", "li", "dt", "dd"
1421 );
1422 $htmlnest = array( # Tags that can be nested--??
1423 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1424 "dl", "font", "big", "small", "sub", "sup"
1425 );
1426 $tabletags = array( # Can only appear inside table
1427 "td", "th", "tr"
1428 );
1429
1430 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1431 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1432
1433 $htmlattrs = $this->getHTMLattrs () ;
1434
1435 # Remove HTML comments
1436 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1437
1438 $bits = explode( "<", $text );
1439 $text = array_shift( $bits );
1440 $tagstack = array(); $tablestack = array();
1441
1442 foreach ( $bits as $x ) {
1443 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1444 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1445 $x, $regs );
1446 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1447 error_reporting( $prev );
1448
1449 $badtag = 0 ;
1450 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1451 # Check our stack
1452 if ( $slash ) {
1453 # Closing a tag...
1454 if ( ! in_array( $t, $htmlsingle ) &&
1455 ( $ot = array_pop( $tagstack ) ) != $t ) {
1456 array_push( $tagstack, $ot );
1457 $badtag = 1;
1458 } else {
1459 if ( $t == "table" ) {
1460 $tagstack = array_pop( $tablestack );
1461 }
1462 $newparams = "";
1463 }
1464 } else {
1465 # Keep track for later
1466 if ( in_array( $t, $tabletags ) &&
1467 ! in_array( "table", $tagstack ) ) {
1468 $badtag = 1;
1469 } else if ( in_array( $t, $tagstack ) &&
1470 ! in_array ( $t , $htmlnest ) ) {
1471 $badtag = 1 ;
1472 } else if ( ! in_array( $t, $htmlsingle ) ) {
1473 if ( $t == "table" ) {
1474 array_push( $tablestack, $tagstack );
1475 $tagstack = array();
1476 }
1477 array_push( $tagstack, $t );
1478 }
1479 # Strip non-approved attributes from the tag
1480 $newparams = $this->fixTagAttributes($params);
1481
1482 }
1483 if ( ! $badtag ) {
1484 $rest = str_replace( ">", "&gt;", $rest );
1485 $text .= "<$slash$t $newparams$brace$rest";
1486 continue;
1487 }
1488 }
1489 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1490 }
1491 # Close off any remaining tags
1492 while ( $t = array_pop( $tagstack ) ) {
1493 $text .= "</$t>\n";
1494 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1495 }
1496 wfProfileOut( $fname );
1497 return $text;
1498 }
1499
1500 /*
1501 *
1502 * This function accomplishes several tasks:
1503 * 1) Auto-number headings if that option is enabled
1504 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1505 * 3) Add a Table of contents on the top for users who have enabled the option
1506 * 4) Auto-anchor headings
1507 *
1508 * It loops through all headlines, collects the necessary data, then splits up the
1509 * string and re-inserts the newly formatted headlines.
1510 *
1511 */
1512
1513 /* private */ function formatHeadings( $text )
1514 {
1515 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1516 $doShowToc = $this->mOptions->getShowToc();
1517 if( !$this->mTitle->userCanEdit() ) {
1518 $showEditLink = 0;
1519 $rightClickHack = 0;
1520 } else {
1521 $showEditLink = $this->mOptions->getEditSection();
1522 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1523 }
1524
1525 # Inhibit editsection links if requested in the page
1526 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1527 if( $esw->matchAndRemove( $text ) ) {
1528 $showEditLink = 0;
1529 }
1530 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1531 # do not add TOC
1532 $mw =& MagicWord::get( MAG_NOTOC );
1533 if( $mw->matchAndRemove( $text ) ) {
1534 $doShowToc = 0;
1535 }
1536
1537 # never add the TOC to the Main Page. This is an entry page that should not
1538 # be more than 1-2 screens large anyway
1539 if( $this->mTitle->getPrefixedText() == wfMsg("mainpage") ) {
1540 $doShowToc = 0;
1541 }
1542
1543 # Get all headlines for numbering them and adding funky stuff like [edit]
1544 # links - this is for later, but we need the number of headlines right now
1545 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1546
1547 # if there are fewer than 4 headlines in the article, do not show TOC
1548 if( $numMatches < 4 ) {
1549 $doShowToc = 0;
1550 }
1551
1552 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1553 # override above conditions and always show TOC
1554 $mw =& MagicWord::get( MAG_FORCETOC );
1555 if ($mw->matchAndRemove( $text ) ) {
1556 $doShowToc = 1;
1557 }
1558
1559
1560 # We need this to perform operations on the HTML
1561 $sk =& $this->mOptions->getSkin();
1562
1563 # headline counter
1564 $headlineCount = 0;
1565
1566 # Ugh .. the TOC should have neat indentation levels which can be
1567 # passed to the skin functions. These are determined here
1568 $toclevel = 0;
1569 $toc = "";
1570 $full = "";
1571 $head = array();
1572 $sublevelCount = array();
1573 $level = 0;
1574 $prevlevel = 0;
1575 foreach( $matches[3] as $headline ) {
1576 $numbering = "";
1577 if( $level ) {
1578 $prevlevel = $level;
1579 }
1580 $level = $matches[1][$headlineCount];
1581 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1582 # reset when we enter a new level
1583 $sublevelCount[$level] = 0;
1584 $toc .= $sk->tocIndent( $level - $prevlevel );
1585 $toclevel += $level - $prevlevel;
1586 }
1587 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1588 # reset when we step back a level
1589 $sublevelCount[$level+1]=0;
1590 $toc .= $sk->tocUnindent( $prevlevel - $level );
1591 $toclevel -= $prevlevel - $level;
1592 }
1593 # count number of headlines for each level
1594 @$sublevelCount[$level]++;
1595 if( $doNumberHeadings || $doShowToc ) {
1596 $dot = 0;
1597 for( $i = 1; $i <= $level; $i++ ) {
1598 if( !empty( $sublevelCount[$i] ) ) {
1599 if( $dot ) {
1600 $numbering .= ".";
1601 }
1602 $numbering .= $sublevelCount[$i];
1603 $dot = 1;
1604 }
1605 }
1606 }
1607
1608 # The canonized header is a version of the header text safe to use for links
1609 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1610 $canonized_headline = Parser::unstrip( $headline, $this->mStripState );
1611
1612 # strip out HTML
1613 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1614 $tocline = trim( $canonized_headline );
1615 $canonized_headline = preg_replace("/[ &\\/<>\\(\\)\\[\\]=,+']+/", '_', html_entity_decode( $tocline));
1616 $refer[$headlineCount] = $canonized_headline;
1617
1618 # count how many in assoc. array so we can track dupes in anchors
1619 @$refers[$canonized_headline]++;
1620 $refcount[$headlineCount]=$refers[$canonized_headline];
1621
1622 # Prepend the number to the heading text
1623
1624 if( $doNumberHeadings || $doShowToc ) {
1625 $tocline = $numbering . " " . $tocline;
1626
1627 # Don't number the heading if it is the only one (looks silly)
1628 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1629 # the two are different if the line contains a link
1630 $headline=$numbering . " " . $headline;
1631 }
1632 }
1633
1634 # Create the anchor for linking from the TOC to the section
1635 $anchor = $canonized_headline;
1636 if($refcount[$headlineCount] > 1 ) {
1637 $anchor .= "_" . $refcount[$headlineCount];
1638 }
1639 if( $doShowToc ) {
1640 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1641 }
1642 if( $showEditLink ) {
1643 if ( empty( $head[$headlineCount] ) ) {
1644 $head[$headlineCount] = "";
1645 }
1646 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
1647 }
1648
1649 # Add the edit section span
1650 if( $rightClickHack ) {
1651 $headline = $sk->editSectionScript($headlineCount+1,$headline);
1652 }
1653
1654 # give headline the correct <h#> tag
1655 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1656
1657 $headlineCount++;
1658 }
1659
1660 if( $doShowToc ) {
1661 $toclines = $headlineCount;
1662 $toc .= $sk->tocUnindent( $toclevel );
1663 $toc = $sk->tocTable( $toc );
1664 }
1665
1666 # split up and insert constructed headlines
1667
1668 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1669 $i = 0;
1670
1671 foreach( $blocks as $block ) {
1672 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1673 # This is the [edit] link that appears for the top block of text when
1674 # section editing is enabled
1675 $full .= $sk->editSectionLink(0);
1676 }
1677 $full .= $block;
1678 if( $doShowToc && !$i) {
1679 # Top anchor now in skin
1680 $full = $full.$toc;
1681 }
1682
1683 if( !empty( $head[$i] ) ) {
1684 $full .= $head[$i];
1685 }
1686 $i++;
1687 }
1688
1689 return $full;
1690 }
1691
1692 /* private */ function doMagicISBN( &$tokenizer )
1693 {
1694 global $wgLang;
1695
1696 # Check whether next token is a text token
1697 # If yes, fetch it and convert the text into a
1698 # Special::BookSources link
1699 $token = $tokenizer->previewToken();
1700 while ( $token["type"] == "" )
1701 {
1702 $tokenizer->nextToken();
1703 $token = $tokenizer->previewToken();
1704 }
1705 if ( $token["type"] == "text" )
1706 {
1707 $token = $tokenizer->nextToken();
1708 $x = $token["text"];
1709 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1710
1711 $isbn = $blank = "" ;
1712 while ( " " == $x{0} ) {
1713 $blank .= " ";
1714 $x = substr( $x, 1 );
1715 }
1716 while ( strstr( $valid, $x{0} ) != false ) {
1717 $isbn .= $x{0};
1718 $x = substr( $x, 1 );
1719 }
1720 $num = str_replace( "-", "", $isbn );
1721 $num = str_replace( " ", "", $num );
1722
1723 if ( "" == $num ) {
1724 $text = "ISBN $blank$x";
1725 } else {
1726 $titleObj = Title::makeTitle( NS_SPECIAL, "Booksources" );
1727 $text = "<a href=\"" .
1728 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1729 "\" class=\"internal\">ISBN $isbn</a>";
1730 $text .= $x;
1731 }
1732 } else {
1733 $text = "ISBN ";
1734 }
1735 return $text;
1736 }
1737 /* private */ function doMagicRFC( &$tokenizer )
1738 {
1739 global $wgLang;
1740
1741 # Check whether next token is a text token
1742 # If yes, fetch it and convert the text into a
1743 # link to an RFC source
1744 $token = $tokenizer->previewToken();
1745 while ( $token["type"] == "" )
1746 {
1747 $tokenizer->nextToken();
1748 $token = $tokenizer->previewToken();
1749 }
1750 if ( $token["type"] == "text" )
1751 {
1752 $token = $tokenizer->nextToken();
1753 $x = $token["text"];
1754 $valid = "0123456789";
1755
1756 $rfc = $blank = "" ;
1757 while ( " " == $x{0} ) {
1758 $blank .= " ";
1759 $x = substr( $x, 1 );
1760 }
1761 while ( strstr( $valid, $x{0} ) != false ) {
1762 $rfc .= $x{0};
1763 $x = substr( $x, 1 );
1764 }
1765
1766 if ( "" == $rfc ) {
1767 $text .= "RFC $blank$x";
1768 } else {
1769 $url = wfmsg( "rfcurl" );
1770 $url = str_replace( "$1", $rfc, $url);
1771 $sk =& $this->mOptions->getSkin();
1772 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1773 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1774 }
1775 } else {
1776 $text = "RFC ";
1777 }
1778 return $text;
1779 }
1780
1781 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1782 {
1783 $this->mOptions = $options;
1784 $this->mTitle =& $title;
1785 $this->mOutputType = OT_WIKI;
1786
1787 if ( $clearState ) {
1788 $this->clearState();
1789 }
1790
1791 $stripState = false;
1792 $pairs = array(
1793 "\r\n" => "\n",
1794 );
1795 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
1796 // now with regexes
1797 $pairs = array(
1798 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
1799 "/<br *?>/i" => "<br/>",
1800 );
1801 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
1802 $text = $this->strip( $text, $stripState, false );
1803 $text = $this->pstPass2( $text, $user );
1804 $text = $this->unstrip( $text, $stripState );
1805 return $text;
1806 }
1807
1808 /* private */ function pstPass2( $text, &$user )
1809 {
1810 global $wgLang, $wgLocaltimezone, $wgCurParser;
1811
1812 # Variable replacement
1813 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1814 $text = $this->replaceVariables( $text );
1815
1816 # Signatures
1817 #
1818 $n = $user->getName();
1819 $k = $user->getOption( "nickname" );
1820 if ( "" == $k ) { $k = $n; }
1821 if(isset($wgLocaltimezone)) {
1822 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1823 }
1824 /* Note: this is an ugly timezone hack for the European wikis */
1825 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1826 " (" . date( "T" ) . ")";
1827 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1828
1829 $text = preg_replace( "/~~~~~/", $d, $text );
1830 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1831 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1832 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1833 Namespace::getUser() ) . ":$n|$k]]", $text );
1834
1835 # Context links: [[|name]] and [[name (context)|]]
1836 #
1837 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1838 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1839 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1840 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1841
1842 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1843 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1844 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1845 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1846 # [[ns:page (cont)|]]
1847 $context = "";
1848 $t = $this->mTitle->getText();
1849 if ( preg_match( $conpat, $t, $m ) ) {
1850 $context = $m[2];
1851 }
1852 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1853 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1854 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1855
1856 if ( "" == $context ) {
1857 $text = preg_replace( $p2, "[[\\1]]", $text );
1858 } else {
1859 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1860 }
1861
1862 /*
1863 $mw =& MagicWord::get( MAG_SUBST );
1864 $wgCurParser = $this->fork();
1865 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1866 $this->merge( $wgCurParser );
1867 */
1868
1869 # Trim trailing whitespace
1870 # MAG_END (__END__) tag allows for trailing
1871 # whitespace to be deliberately included
1872 $text = rtrim( $text );
1873 $mw =& MagicWord::get( MAG_END );
1874 $mw->matchAndRemove( $text );
1875
1876 return $text;
1877 }
1878
1879 # Set up some variables which are usually set up in parse()
1880 # so that an external function can call some class members with confidence
1881 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1882 {
1883 $this->mTitle =& $title;
1884 $this->mOptions = $options;
1885 $this->mOutputType = $outputType;
1886 if ( $clearState ) {
1887 $this->clearState();
1888 }
1889 }
1890
1891 function transformMsg( $text, $options ) {
1892 global $wgTitle;
1893 static $executing = false;
1894
1895 # Guard against infinite recursion
1896 if ( $executing ) {
1897 return $text;
1898 }
1899 $executing = true;
1900
1901 $this->mTitle = $wgTitle;
1902 $this->mOptions = $options;
1903 $this->mOutputType = OT_MSG;
1904 $this->clearState();
1905 $text = $this->replaceVariables( $text );
1906
1907 $executing = false;
1908 return $text;
1909 }
1910 }
1911
1912 class ParserOutput
1913 {
1914 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1915
1916 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1917 $containsOldMagic = false )
1918 {
1919 $this->mText = $text;
1920 $this->mLanguageLinks = $languageLinks;
1921 $this->mCategoryLinks = $categoryLinks;
1922 $this->mContainsOldMagic = $containsOldMagic;
1923 }
1924
1925 function getText() { return $this->mText; }
1926 function getLanguageLinks() { return $this->mLanguageLinks; }
1927 function getCategoryLinks() { return $this->mCategoryLinks; }
1928 function containsOldMagic() { return $this->mContainsOldMagic; }
1929 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
1930 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
1931 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
1932 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
1933
1934 function merge( $other ) {
1935 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
1936 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
1937 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
1938 }
1939
1940 }
1941
1942 class ParserOptions
1943 {
1944 # All variables are private
1945 var $mUseTeX; # Use texvc to expand <math> tags
1946 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1947 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1948 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1949 var $mAllowExternalImages; # Allow external images inline
1950 var $mSkin; # Reference to the preferred skin
1951 var $mDateFormat; # Date format index
1952 var $mEditSection; # Create "edit section" links
1953 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1954 var $mNumberHeadings; # Automatically number headings
1955 var $mShowToc; # Show table of contents
1956
1957 function getUseTeX() { return $this->mUseTeX; }
1958 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
1959 function getUseDynamicDates() { return $this->mUseDynamicDates; }
1960 function getInterwikiMagic() { return $this->mInterwikiMagic; }
1961 function getAllowExternalImages() { return $this->mAllowExternalImages; }
1962 function getSkin() { return $this->mSkin; }
1963 function getDateFormat() { return $this->mDateFormat; }
1964 function getEditSection() { return $this->mEditSection; }
1965 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
1966 function getNumberHeadings() { return $this->mNumberHeadings; }
1967 function getShowToc() { return $this->mShowToc; }
1968
1969 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
1970 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
1971 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
1972 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
1973 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
1974 function setSkin( $x ) { return wfSetRef( $this->mSkin, $x ); }
1975 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
1976 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
1977 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
1978 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
1979 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
1980
1981 /* static */ function newFromUser( &$user )
1982 {
1983 $popts = new ParserOptions;
1984 $popts->initialiseFromUser( &$user );
1985 return $popts;
1986 }
1987
1988 function initialiseFromUser( &$userInput )
1989 {
1990 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1991
1992 if ( !$userInput ) {
1993 $user = new User;
1994 $user->setLoaded( true );
1995 } else {
1996 $user =& $userInput;
1997 }
1998
1999 $this->mUseTeX = $wgUseTeX;
2000 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2001 $this->mUseDynamicDates = $wgUseDynamicDates;
2002 $this->mInterwikiMagic = $wgInterwikiMagic;
2003 $this->mAllowExternalImages = $wgAllowExternalImages;
2004 $this->mSkin =& $user->getSkin();
2005 $this->mDateFormat = $user->getOption( "date" );
2006 $this->mEditSection = $user->getOption( "editsection" );
2007 $this->mEditSectionOnRightClick = $user->getOption( "editsectiononrightclick" );
2008 $this->mNumberHeadings = $user->getOption( "numberheadings" );
2009 $this->mShowToc = $user->getOption( "showtoc" );
2010 }
2011
2012
2013 }
2014
2015 # Regex callbacks, used in Parser::replaceVariables
2016 function wfBraceSubstitution( $matches )
2017 {
2018 global $wgCurParser;
2019 return $wgCurParser->braceSubstitution( $matches );
2020 }
2021
2022 ?>