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