93d92bda87a7e9b55aa4740ae48c5a0e850655ea
[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 # Must expand in reverse order, otherwise nested tags will be corrupted
187 $state = array( $pre_content, $math_content, $hiero_content, $nowiki_content );
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 $data = array () ;
221 $sql1 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
222 $sql2 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
223
224 $res = wfQuery ( $sql1, DB_READ ) ;
225 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
226
227 $res = wfQuery ( $sql2, DB_READ ) ;
228 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
229
230
231 foreach ( $data AS $x )
232 {
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] = "\n<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
424 if($this->mOptions->getUseDynamicDates()) {
425 global $wgDateFormatter;
426 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
427 }
428
429 $text = $this->replaceExternalLinks( $text );
430 $text = $this->doTokenizedParser ( $text );
431 $text = $this->doTableStuff ( $text ) ;
432
433 $text = $this->formatHeadings( $text );
434
435 $sk =& $this->mOptions->getSkin();
436 $text = $sk->transformContent( $text );
437 $text = $this->doBlockLevels( $text, $linestart );
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 $s = "";
580 if ( $state["em"] && $state["strong"] ) {
581 if ( $state["em"] < $state["strong"] ) {
582 $s .= "</strong></em>";
583 } else {
584 $s .= "</em></strong>";
585 }
586 $state["strong"] = $state["em"] = FALSE;
587 } elseif ( $state["em"] ) {
588 $s .= "</em><strong>";
589 $state["em"] = FALSE;
590 $state["strong"] = $token["pos"];
591 } elseif ( $state["strong"] ) {
592 $s .= "</strong><em>";
593 $state["strong"] = FALSE;
594 $state["em"] = $token["pos"];
595 } else { # not $em and not $strong
596 $s .= "<strong><em>";
597 $state["strong"] = $state["em"] = $token["pos"];
598 }
599 return $s;
600 }
601
602 /* private */ function doTokenizedParser( $str )
603 {
604 global $wgLang; # for language specific parser hook
605
606 $tokenizer=Tokenizer::newFromString( $str );
607 $tokenStack = array();
608
609 $s="";
610 $state["em"] = FALSE;
611 $state["strong"] = FALSE;
612 $tagIsOpen = FALSE;
613 $threeopen = false;
614
615 # The tokenizer splits the text into tokens and returns them one by one.
616 # Every call to the tokenizer returns a new token.
617 while ( $token = $tokenizer->nextToken() )
618 {
619 $threeopen = false;
620 switch ( $token["type"] )
621 {
622 case "text":
623 # simple text with no further markup
624 $txt = $token["text"];
625 break;
626 case "[[[":
627 # remember the tag opened with 3 [
628 $threeopen = true;
629 case "[[":
630 # link opening tag.
631 # FIXME : Treat orphaned open tags (stack not empty when text is over)
632 $tagIsOpen = TRUE;
633 array_push( $tokenStack, $token );
634 $txt="";
635 break;
636
637 case "]]]":
638 case "]]":
639 # link close tag.
640 # get text from stack, glue it together, and call the code to handle a
641 # link
642
643 if ( count( $tokenStack ) == 0 )
644 {
645 # stack empty. Found a ]] without an opening [[
646 $txt = "]]";
647 } else {
648 $linkText = "";
649 $lastToken = array_pop( $tokenStack );
650 while ( !(($lastToken["type"] == "[[[") or ($lastToken["type"] == "[[")) )
651 {
652 if( !empty( $lastToken["text"] ) ) {
653 $linkText = $lastToken["text"] . $linkText;
654 }
655 $lastToken = array_pop( $tokenStack );
656 }
657
658 $txt = $linkText ."]]";
659
660 if( isset( $lastToken["text"] ) ) {
661 $prefix = $lastToken["text"];
662 } else {
663 $prefix = "";
664 }
665 $nextToken = $tokenizer->previewToken();
666 if ( $nextToken["type"] == "text" )
667 {
668 # Preview just looks at it. Now we have to fetch it.
669 $nextToken = $tokenizer->nextToken();
670 $txt .= $nextToken["text"];
671 }
672 $txt = $this->handleInternalLink( $txt, $prefix );
673
674 # did the tag start with 3 [ ?
675 if($threeopen) {
676 # show the first as text
677 $txt = "[".$txt;
678 $threeopen=false;
679 }
680
681 }
682 $tagIsOpen = (count( $tokenStack ) != 0);
683 break;
684 case "----":
685 $txt = "\n<hr />\n";
686 break;
687 case "'''":
688 # This and the three next ones handle quotes
689 $txt = $this->handle3Quotes( $state, $token );
690 break;
691 case "''":
692 $txt = $this->handle2Quotes( $state, $token );
693 break;
694 case "'''''":
695 $txt = $this->handle5Quotes( $state, $token );
696 break;
697 case "":
698 # empty token
699 $txt="";
700 break;
701 case "RFC ":
702 if ( $tagIsOpen ) {
703 $txt = "RFC ";
704 } else {
705 $txt = $this->doMagicRFC( $tokenizer );
706 }
707 break;
708 case "ISBN ":
709 if ( $tagIsOpen ) {
710 $txt = "ISBN ";
711 } else {
712 $txt = $this->doMagicISBN( $tokenizer );
713 }
714 break;
715 default:
716 # Call language specific Hook.
717 $txt = $wgLang->processToken( $token, $tokenStack );
718 if ( NULL == $txt ) {
719 # An unkown token. Highlight.
720 $txt = "<font color=\"#FF0000\"><b>".$token["type"]."</b></font>";
721 $txt .= "<font color=\"#FFFF00\"><b>".$token["text"]."</b></font>";
722 }
723 break;
724 }
725 # If we're parsing the interior of a link, don't append the interior to $s,
726 # but push it to the stack so it can be processed when a ]] token is found.
727 if ( $tagIsOpen && $txt != "" ) {
728 $token["type"] = "text";
729 $token["text"] = $txt;
730 array_push( $tokenStack, $token );
731 } else {
732 $s .= $txt;
733 }
734 } #end while
735 if ( count( $tokenStack ) != 0 )
736 {
737 # still objects on stack. opened [[ tag without closing ]] tag.
738 $txt = "";
739 while ( $lastToken = array_pop( $tokenStack ) )
740 {
741 if ( $lastToken["type"] == "text" )
742 {
743 $txt = $lastToken["text"] . $txt;
744 } else {
745 $txt = $lastToken["type"] . $txt;
746 }
747 }
748 $s .= $txt;
749 }
750 return $s;
751 }
752
753 /* private */ function handleInternalLink( $line, $prefix )
754 {
755 global $wgLang, $wgLinkCache;
756 global $wgNamespacesWithSubpages, $wgLanguageCode;
757 static $fname = "Parser::handleInternalLink" ;
758 wfProfileIn( $fname );
759
760 wfProfileIn( "$fname-setup" );
761 static $tc = FALSE;
762 if ( !$tc ) { $tc = Title::legalChars() . "#"; }
763 $sk =& $this->mOptions->getSkin();
764
765 # Match a link having the form [[namespace:link|alternate]]trail
766 static $e1 = FALSE;
767 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
768 # Match the end of a line for a word that's not followed by whitespace,
769 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
770 #$e2 = "/^(.*)\\b(\\w+)\$/suD";
771 #$e2 = "/^(.*\\s)(\\S+)\$/suD";
772 static $e2 = '/^(.*\s)([a-zA-Z\x80-\xff]+)$/sD';
773
774
775 # Special and Media are pseudo-namespaces; no pages actually exist in them
776 static $image = FALSE;
777 static $special = FALSE;
778 static $media = FALSE;
779 static $category = FALSE;
780 if ( !$image ) { $image = Namespace::getImage(); }
781 if ( !$special ) { $special = Namespace::getSpecial(); }
782 if ( !$media ) { $media = Namespace::getMedia(); }
783 if ( !$category ) { $category = wfMsg ( "category" ) ; }
784
785 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
786
787 wfProfileOut( "$fname-setup" );
788 $s = "";
789
790 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
791 $text = $m[2];
792 $trail = $m[3];
793 } else { # Invalid form; output directly
794 $s .= $prefix . "[[" . $line ;
795 return $s;
796 }
797
798 /* Valid link forms:
799 Foobar -- normal
800 :Foobar -- override special treatment of prefix (images, language links)
801 /Foobar -- convert to CurrentPage/Foobar
802 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
803 */
804 $c = substr($m[1],0,1);
805 $noforce = ($c != ":");
806 if( $c == "/" ) { # subpage
807 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
808 $m[1]=substr($m[1],1,strlen($m[1])-2);
809 $noslash=$m[1];
810 } else {
811 $noslash=substr($m[1],1);
812 }
813 if($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]) { # subpages allowed here
814 $link = $this->mTitle->getPrefixedText(). "/" . trim($noslash);
815 if( "" == $text ) {
816 $text= $m[1];
817 } # this might be changed for ugliness reasons
818 } else {
819 $link = $noslash; # no subpage allowed, use standard link
820 }
821 } elseif( $noforce ) { # no subpage
822 $link = $m[1];
823 } else {
824 $link = substr( $m[1], 1 );
825 }
826 if( "" == $text )
827 $text = $link;
828
829 $nt = Title::newFromText( $link );
830 if( !$nt ) {
831 $s .= $prefix . "[[" . $line;
832 return $s;
833 }
834 $ns = $nt->getNamespace();
835 $iw = $nt->getInterWiki();
836 if( $noforce ) {
837 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
838 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
839 $s .= $prefix . $trail;
840 return $s;
841 }
842 if( $ns == $image ) {
843 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
844 $wgLinkCache->addImageLinkObj( $nt );
845 return $s;
846 }
847 }
848 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
849 ( strpos( $link, "#" ) == FALSE ) ) {
850 $s .= $prefix . "<strong>" . $text . "</strong>" . $trail;
851 return $s;
852 }
853
854 # Category feature
855 $catns = strtoupper ( $nt->getDBkey () ) ;
856 $catns = explode ( ":" , $catns ) ;
857 if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
858 else $catns = "" ;
859 if ( $catns == strtoupper($category) && $this->mOptions->getUseCategoryMagic() ) {
860 $t = explode ( ":" , $nt->getText() ) ;
861 array_shift ( $t ) ;
862 $t = implode ( ":" , $t ) ;
863 $t = $wgLang->ucFirst ( $t ) ;
864 $nnt = Title::newFromText ( $category.":".$t ) ;
865 $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
866 $this->mOutput->mCategoryLinks[] = $t ;
867 $s .= $prefix . $trail ;
868 return $s ;
869 }
870
871 if( $ns == $media ) {
872 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
873 $wgLinkCache->addImageLinkObj( $nt );
874 return $s;
875 } elseif( $ns == $special ) {
876 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, "", $trail );
877 return $s;
878 }
879 $s .= $sk->makeLinkObj( $nt, $text, "", $trail , $prefix );
880
881 wfProfileOut( $fname );
882 return $s;
883 }
884
885 # Some functions here used by doBlockLevels()
886 #
887 /* private */ function closeParagraph()
888 {
889 $result = "";
890 if ( '' != $this->mLastSection ) {
891 $result = "</" . $this->mLastSection . ">";
892 }
893 $this->mLastSection = "";
894 return $result."\n";
895 }
896 # getCommon() returns the length of the longest common substring
897 # of both arguments, starting at the beginning of both.
898 #
899 /* private */ function getCommon( $st1, $st2 )
900 {
901 $fl = strlen( $st1 );
902 $shorter = strlen( $st2 );
903 if ( $fl < $shorter ) { $shorter = $fl; }
904
905 for ( $i = 0; $i < $shorter; ++$i ) {
906 if ( $st1{$i} != $st2{$i} ) { break; }
907 }
908 return $i;
909 }
910 # These next three functions open, continue, and close the list
911 # element appropriate to the prefix character passed into them.
912 #
913 /* private */ function openList( $char )
914 {
915 $result = $this->closeParagraph();
916
917 if ( "*" == $char ) { $result .= "<ul><li>"; }
918 else if ( "#" == $char ) { $result .= "<ol><li>"; }
919 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
920 else if ( ";" == $char ) {
921 $result .= "<dl><dt>";
922 $this->mDTopen = true;
923 }
924 else { $result = "<!-- ERR 1 -->"; }
925
926 return $result;
927 }
928
929 /* private */ function nextItem( $char )
930 {
931 if ( "*" == $char || "#" == $char ) { return "</li><li>"; }
932 else if ( ":" == $char || ";" == $char ) {
933 $close = "</dd>";
934 if ( $this->mDTopen ) { $close = "</dt>"; }
935 if ( ";" == $char ) {
936 $this->mDTopen = true;
937 return $close . "<dt>";
938 } else {
939 $this->mDTopen = false;
940 return $close . "<dd>";
941 }
942 }
943 return "<!-- ERR 2 -->";
944 }
945
946 /* private */function closeList( $char )
947 {
948 if ( "*" == $char ) { $text = "</li></ul>"; }
949 else if ( "#" == $char ) { $text = "</li></ol>"; }
950 else if ( ":" == $char ) {
951 if ( $this->mDTopen ) {
952 $this->mDTopen = false;
953 $text = "</dt></dl>";
954 } else {
955 $text = "</dd></dl>";
956 }
957 }
958 else { return "<!-- ERR 3 -->"; }
959 return $text."\n";
960 }
961
962 /* private */ function doBlockLevels( $text, $linestart )
963 {
964 $fname = "Parser::doBlockLevels";
965 wfProfileIn( $fname );
966 # Parsing through the text line by line. The main thing
967 # happening here is handling of block-level elements p, pre,
968 # and making lists from lines starting with * # : etc.
969 #
970 $a = explode( "\n", $text );
971 $a[0] = "\n".$a[0];
972 $lastPref = $text = '';
973 $this->mDTopen = $inBlockElem = false;
974
975 if ( ! $linestart ) { $text .= array_shift( $a ); }
976 foreach ( $a as $t ) {
977 if ( "" != $text ) { $text .= "\n"; }
978
979 $oLine = $t;
980 $opl = strlen( $lastPref );
981 $npl = strspn( $t, "*#:;" );
982 $pref = substr( $t, 0, $npl );
983 $pref2 = str_replace( ";", ":", $pref );
984 $t = substr( $t, $npl );
985
986 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
987 $text .= $this->nextItem( substr( $pref, -1 ) );
988
989 if ( ";" == substr( $pref, -1 ) ) {
990 $cpos = strpos( $t, ":" );
991 if ( ! ( false === $cpos ) ) {
992 $term = substr( $t, 0, $cpos );
993 $text .= $term . $this->nextItem( ":" );
994 $t = substr( $t, $cpos + 1 );
995 }
996 }
997 } else if (0 != $npl || 0 != $opl) {
998 $cpl = $this->getCommon( $pref, $lastPref );
999
1000 while ( $cpl < $opl ) {
1001 $text .= $this->closeList( $lastPref{$opl-1} );
1002 --$opl;
1003 }
1004 if ( $npl <= $cpl && $cpl > 0 ) {
1005 $text .= $this->nextItem( $pref{$cpl-1} );
1006 }
1007 while ( $npl > $cpl ) {
1008 $char = substr( $pref, $cpl, 1 );
1009 $text .= $this->openList( $char );
1010
1011 if ( ";" == $char ) {
1012 $cpos = strpos( $t, ":" );
1013 if ( ! ( false === $cpos ) ) {
1014 $term = substr( $t, 0, $cpos );
1015 $text .= $term . $this->nextItem( ":" );
1016 $t = substr( $t, $cpos + 1 );
1017 }
1018 }
1019 ++$cpl;
1020 }
1021 $lastPref = $pref2;
1022 }
1023 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1024 if ( preg_match(
1025 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<div|<pre)/i", $t ) ) {
1026 $text .= $this->closeParagraph();
1027 $inBlockElem = true;
1028 } else if ( preg_match("/(<hr)/i", $t ) ) {
1029 $text .= $this->closeParagraph();
1030 $inBlockElem = false;
1031 }
1032 if ( ! $inBlockElem ) {
1033 if ( " " == $t{0} ) {
1034 $newSection = "pre";
1035 # $t = wfEscapeHTML( $t );
1036 }
1037 else { $newSection = "p"; }
1038
1039 if ( '' == trim( $oLine ) ) {
1040 if ( $this->mLastSection != 'p') {
1041 $text .= $this->closeParagraph();
1042 $text .= "<" . $newSection . ">";
1043 $this->mLastSection = $newSection;
1044 } else if ( $this->mLastSection == 'p') {
1045 $text .= '<br />';
1046 }
1047 } else if ( $this->mLastSection == $newSection and $newSection != 'p' ) {
1048 $text .= $this->closeParagraph();
1049 $text .= "<" . $newSection . ">";
1050 $this->mLastSection = $newSection;
1051 }
1052 }
1053 if ( $inBlockElem &&
1054 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|<\\/p<\\/div|<\\/pre)/i", $t ) ) {
1055 $inBlockElem = false;
1056 }
1057 }
1058 $text .= $t;
1059 }
1060 while ( $npl ) {
1061 $text .= $this->closeList( $pref2{$npl-1} );
1062 --$npl;
1063 }
1064 if ( "" != $this->mLastSection ) {
1065 $text .= "</" . $this->mLastSection . ">";
1066 $this->mLastSection = "";
1067 }
1068 wfProfileOut( $fname );
1069 return $text;
1070 }
1071
1072 function getVariableValue( $index ) {
1073 global $wgLang;
1074
1075 switch ( $index ) {
1076 case MAG_CURRENTMONTH:
1077 return date( "m" );
1078 case MAG_CURRENTMONTHNAME:
1079 return $wgLang->getMonthName( date("n") );
1080 case MAG_CURRENTMONTHNAMEGEN:
1081 return $wgLang->getMonthNameGen( date("n") );
1082 case MAG_CURRENTDAY:
1083 return date("j");
1084 case MAG_CURRENTDAYNAME:
1085 return $wgLang->getWeekdayName( date("w")+1 );
1086 case MAG_CURRENTYEAR:
1087 return date( "Y" );
1088 case MAG_CURRENTTIME:
1089 return $wgLang->time( wfTimestampNow(), false );
1090 case MAG_NUMBEROFARTICLES:
1091 return wfNumberOfArticles();
1092 default:
1093 return NULL;
1094 }
1095 }
1096
1097 function initialiseVariables()
1098 {
1099 global $wgVariableIDs;
1100 $this->mVariables = array();
1101 foreach ( $wgVariableIDs as $id ) {
1102 $mw =& MagicWord::get( $id );
1103 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1104 }
1105 }
1106
1107 /* private */ function replaceVariables( $text )
1108 {
1109 global $wgLang, $wgCurParser;
1110 global $wgScript, $wgArticlePath;
1111
1112 $fname = "Parser::replaceVariables";
1113 wfProfileIn( $fname );
1114
1115 $bail = false;
1116 if ( !$this->mVariables ) {
1117 $this->initialiseVariables();
1118 }
1119 $titleChars = Title::legalChars();
1120 $regex = "/{{([$titleChars]*?)}}/s";
1121
1122 # "Recursive" variable expansion: run it through a couple of passes
1123 for ( $i=0; $i<MAX_INCLUDE_REPEAT && !$bail; $i++ ) {
1124 $oldText = $text;
1125
1126 # It's impossible to rebind a global in PHP
1127 # Instead, we run the substitution on a copy, then merge the changed fields back in
1128 $wgCurParser = $this->fork();
1129
1130 $text = preg_replace_callback( $regex, "wfBraceSubstitution", $text );
1131 if ( $oldText == $text ) {
1132 $bail = true;
1133 }
1134 $this->merge( $wgCurParser );
1135 }
1136
1137 return $text;
1138 }
1139
1140 # Returns a copy of this object except with various variables cleared
1141 # This copy can be re-merged with the parent after operations on the copy
1142 function fork()
1143 {
1144 $copy = $this;
1145 $copy->mOutput = new ParserOutput;
1146 return $copy;
1147 }
1148
1149 # Merges a copy split off with fork()
1150 function merge( &$copy )
1151 {
1152 $this->mOutput->merge( $copy->mOutput );
1153
1154 # Merge include throttling arrays
1155 foreach( $copy->mIncludeCount as $dbk => $count ) {
1156 if ( array_key_exists( $dbk, $this->mIncludeCount ) ) {
1157 $this->mIncludeCount[$dbk] += $count;
1158 } else {
1159 $this->mIncludeCount[$dbk] = $count;
1160 }
1161 }
1162 }
1163
1164 function braceSubstitution( $matches )
1165 {
1166 global $wgLinkCache;
1167 $fname = "Parser::braceSubstitution";
1168 $found = false;
1169 $nowiki = false;
1170
1171 $text = $matches[1];
1172
1173 # SUBST
1174 $mwSubst =& MagicWord::get( MAG_SUBST );
1175 if ( $mwSubst->matchStartAndRemove( $text ) ) {
1176 if ( $this->mOutputType == OT_HTML ) {
1177 # Invalid SUBST not replaced at PST time
1178 # Return without further processing
1179 $text = $matches[0];
1180 $found = true;
1181 }
1182 } elseif ( $this->mOutputType == OT_WIKI ) {
1183 # SUBST not found in PST pass, do nothing
1184 $text = $matches[0];
1185 $found = true;
1186 }
1187
1188 # Various prefixes
1189 if ( !$found ) {
1190 # Check for MSGNW:
1191 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1192 if ( $mwMsgnw->matchStartAndRemove( $text ) ) {
1193 $nowiki = true;
1194 } else {
1195 # Remove obsolete MSG:
1196 $mwMsg =& MagicWord::get( MAG_MSG );
1197 $mwMsg->matchStartAndRemove( $text );
1198 }
1199
1200 # Check if it is an internal message
1201 $mwInt =& MagicWord::get( MAG_INT );
1202 if ( $mwInt->matchStartAndRemove( $text ) ) {
1203 $text = wfMsg( $text );
1204 $found = true;
1205 }
1206 }
1207
1208 # Check for a match against internal variables
1209 if ( !$found && array_key_exists( $text, $this->mVariables ) ) {
1210 $text = $this->mVariables[$text];
1211 $found = true;
1212 $this->mOutput->mContainsOldMagic = true;
1213 }
1214
1215 # Load from database
1216 if ( !$found ) {
1217 $title = Title::newFromText( $text, NS_TEMPLATE );
1218 if ( !is_null( $text ) && !$title->isExternal() ) {
1219 # Check for excessive inclusion
1220 $dbk = $title->getPrefixedDBkey();
1221 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1222 $this->mIncludeCount[$dbk] = 0;
1223 }
1224 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1225 $article = new Article( $title );
1226 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1227 if ( $articleContent !== false ) {
1228 $found = true;
1229 $text = $articleContent;
1230
1231 # Escaping and link table handling
1232 # Not required for preSaveTransform()
1233 if ( $this->mOutputType == OT_HTML ) {
1234 if ( $nowiki ) {
1235 $text = wfEscapeWikiText( $text );
1236 } else {
1237 $text = $this->removeHTMLtags( $text );
1238 }
1239 $wgLinkCache->suspend();
1240 $text = $this->doTokenizedParser( $text );
1241 $wgLinkCache->resume();
1242 $wgLinkCache->addLinkObj( $title );
1243
1244 }
1245 }
1246 }
1247
1248 # If the title is valid but undisplayable, make a link to it
1249 if ( $this->mOutputType == OT_HTML && !$found ) {
1250 $text = "[[" . $title->getPrefixedText() . "]]";
1251 $found = true;
1252 }
1253 }
1254 }
1255
1256 if ( !$found ) {
1257 return $matches[0];
1258 } else {
1259 return $text;
1260 }
1261 }
1262
1263 # Cleans up HTML, removes dangerous tags and attributes
1264 /* private */ function removeHTMLtags( $text )
1265 {
1266 $fname = "Parser::removeHTMLtags";
1267 wfProfileIn( $fname );
1268 $htmlpairs = array( # Tags that must be closed
1269 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1270 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1271 "strike", "strong", "tt", "var", "div", "center",
1272 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1273 "ruby", "rt" , "rb" , "rp", "p"
1274 );
1275 $htmlsingle = array(
1276 "br", "hr", "li", "dt", "dd"
1277 );
1278 $htmlnest = array( # Tags that can be nested--??
1279 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1280 "dl", "font", "big", "small", "sub", "sup"
1281 );
1282 $tabletags = array( # Can only appear inside table
1283 "td", "th", "tr"
1284 );
1285
1286 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1287 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1288
1289 $htmlattrs = $this->getHTMLattrs () ;
1290
1291 # Remove HTML comments
1292 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1293
1294 $bits = explode( "<", $text );
1295 $text = array_shift( $bits );
1296 $tagstack = array(); $tablestack = array();
1297
1298 foreach ( $bits as $x ) {
1299 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1300 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1301 $x, $regs );
1302 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1303 error_reporting( $prev );
1304
1305 $badtag = 0 ;
1306 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1307 # Check our stack
1308 if ( $slash ) {
1309 # Closing a tag...
1310 if ( ! in_array( $t, $htmlsingle ) &&
1311 ( $ot = array_pop( $tagstack ) ) != $t ) {
1312 array_push( $tagstack, $ot );
1313 $badtag = 1;
1314 } else {
1315 if ( $t == "table" ) {
1316 $tagstack = array_pop( $tablestack );
1317 }
1318 $newparams = "";
1319 }
1320 } else {
1321 # Keep track for later
1322 if ( in_array( $t, $tabletags ) &&
1323 ! in_array( "table", $tagstack ) ) {
1324 $badtag = 1;
1325 } else if ( in_array( $t, $tagstack ) &&
1326 ! in_array ( $t , $htmlnest ) ) {
1327 $badtag = 1 ;
1328 } else if ( ! in_array( $t, $htmlsingle ) ) {
1329 if ( $t == "table" ) {
1330 array_push( $tablestack, $tagstack );
1331 $tagstack = array();
1332 }
1333 array_push( $tagstack, $t );
1334 }
1335 # Strip non-approved attributes from the tag
1336 $newparams = $this->fixTagAttributes($params);
1337
1338 }
1339 if ( ! $badtag ) {
1340 $rest = str_replace( ">", "&gt;", $rest );
1341 $text .= "<$slash$t $newparams$brace$rest";
1342 continue;
1343 }
1344 }
1345 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1346 }
1347 # Close off any remaining tags
1348 while ( $t = array_pop( $tagstack ) ) {
1349 $text .= "</$t>\n";
1350 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1351 }
1352 wfProfileOut( $fname );
1353 return $text;
1354 }
1355
1356 /*
1357 *
1358 * This function accomplishes several tasks:
1359 * 1) Auto-number headings if that option is enabled
1360 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1361 * 3) Add a Table of contents on the top for users who have enabled the option
1362 * 4) Auto-anchor headings
1363 *
1364 * It loops through all headlines, collects the necessary data, then splits up the
1365 * string and re-inserts the newly formatted headlines.
1366 *
1367 */
1368
1369 /* private */ function formatHeadings( $text )
1370 {
1371 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1372 $doShowToc = $this->mOptions->getShowToc();
1373 if( !$this->mTitle->userCanEdit() ) {
1374 $showEditLink = 0;
1375 $rightClickHack = 0;
1376 } else {
1377 $showEditLink = $this->mOptions->getEditSection();
1378 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1379 }
1380
1381 # Inhibit editsection links if requested in the page
1382 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1383 if( $esw->matchAndRemove( $text ) ) {
1384 $showEditLink = 0;
1385 }
1386 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1387 # do not add TOC
1388 $mw =& MagicWord::get( MAG_NOTOC );
1389 if( $mw->matchAndRemove( $text ) ) {
1390 $doShowToc = 0;
1391 }
1392
1393 # never add the TOC to the Main Page. This is an entry page that should not
1394 # be more than 1-2 screens large anyway
1395 if( $this->mTitle->getPrefixedText() == wfMsg("mainpage") ) {
1396 $doShowToc = 0;
1397 }
1398
1399 # Get all headlines for numbering them and adding funky stuff like [edit]
1400 # links - this is for later, but we need the number of headlines right now
1401 $numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
1402
1403 # if there are fewer than 4 headlines in the article, do not show TOC
1404 if( $numMatches < 4 ) {
1405 $doShowToc = 0;
1406 }
1407
1408 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1409 # override above conditions and always show TOC
1410 $mw =& MagicWord::get( MAG_FORCETOC );
1411 if ($mw->matchAndRemove( $text ) ) {
1412 $doShowToc = 1;
1413 }
1414
1415
1416 # We need this to perform operations on the HTML
1417 $sk =& $this->mOptions->getSkin();
1418
1419 # headline counter
1420 $headlineCount = 0;
1421
1422 # Ugh .. the TOC should have neat indentation levels which can be
1423 # passed to the skin functions. These are determined here
1424 $toclevel = 0;
1425 $toc = "";
1426 $full = "";
1427 $head = array();
1428 $sublevelCount = array();
1429 $level = 0;
1430 $prevlevel = 0;
1431 foreach( $matches[3] as $headline ) {
1432 $numbering = "";
1433 if( $level ) {
1434 $prevlevel = $level;
1435 }
1436 $level = $matches[1][$headlineCount];
1437 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1438 # reset when we enter a new level
1439 $sublevelCount[$level] = 0;
1440 $toc .= $sk->tocIndent( $level - $prevlevel );
1441 $toclevel += $level - $prevlevel;
1442 }
1443 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1444 # reset when we step back a level
1445 $sublevelCount[$level+1]=0;
1446 $toc .= $sk->tocUnindent( $prevlevel - $level );
1447 $toclevel -= $prevlevel - $level;
1448 }
1449 # count number of headlines for each level
1450 @$sublevelCount[$level]++;
1451 if( $doNumberHeadings || $doShowToc ) {
1452 $dot = 0;
1453 for( $i = 1; $i <= $level; $i++ ) {
1454 if( !empty( $sublevelCount[$i] ) ) {
1455 if( $dot ) {
1456 $numbering .= ".";
1457 }
1458 $numbering .= $sublevelCount[$i];
1459 $dot = 1;
1460 }
1461 }
1462 }
1463
1464 # The canonized header is a version of the header text safe to use for links
1465 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1466 $canonized_headline = Parser::unstrip( $headline, $this->mStripState );
1467
1468 # strip out HTML
1469 $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
1470
1471 $tocline = trim( $canonized_headline );
1472 $canonized_headline = preg_replace( "/[^a-z0-9]/i", "_", trim( $canonized_headline ) );
1473 $refer[$headlineCount] = $canonized_headline;
1474
1475 # count how many in assoc. array so we can track dupes in anchors
1476 @$refers[$canonized_headline]++;
1477 $refcount[$headlineCount]=$refers[$canonized_headline];
1478
1479 # Prepend the number to the heading text
1480
1481 if( $doNumberHeadings || $doShowToc ) {
1482 $tocline = $numbering . " " . $tocline;
1483
1484 # Don't number the heading if it is the only one (looks silly)
1485 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1486 # the two are different if the line contains a link
1487 $headline=$numbering . " " . $headline;
1488 }
1489 }
1490
1491 # Create the anchor for linking from the TOC to the section
1492 $anchor = $canonized_headline;
1493 if($refcount[$headlineCount] > 1 ) {
1494 $anchor .= "_" . $refcount[$headlineCount];
1495 }
1496 if( $doShowToc ) {
1497 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1498 }
1499 if( $showEditLink ) {
1500 if ( empty( $head[$headlineCount] ) ) {
1501 $head[$headlineCount] = "";
1502 }
1503 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
1504 }
1505
1506 # Add the edit section span
1507 if( $rightClickHack ) {
1508 $headline = $sk->editSectionScript($headlineCount+1,$headline);
1509 }
1510
1511 # give headline the correct <h#> tag
1512 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1513
1514 $headlineCount++;
1515 }
1516
1517 if( $doShowToc ) {
1518 $toclines = $headlineCount;
1519 $toc .= $sk->tocUnindent( $toclevel );
1520 $toc = $sk->tocTable( $toc );
1521 }
1522
1523 # split up and insert constructed headlines
1524
1525 $blocks = preg_split( "/<H[1-6].*?" . ">.*?<\/H[1-6]>/i", $text );
1526 $i = 0;
1527
1528 foreach( $blocks as $block ) {
1529 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1530 # This is the [edit] link that appears for the top block of text when
1531 # section editing is enabled
1532 $full .= $sk->editSectionLink(0);
1533 }
1534 $full .= $block;
1535 if( $doShowToc && !$i) {
1536 # Let's add a top anchor just in case we want to link to the top of the page
1537 $full = "<a name=\"top\"></a>".$full.$toc;
1538 }
1539
1540 if( !empty( $head[$i] ) ) {
1541 $full .= $head[$i];
1542 }
1543 $i++;
1544 }
1545
1546 return $full;
1547 }
1548
1549 /* private */ function doMagicISBN( &$tokenizer )
1550 {
1551 global $wgLang;
1552
1553 # Check whether next token is a text token
1554 # If yes, fetch it and convert the text into a
1555 # Special::BookSources link
1556 $token = $tokenizer->previewToken();
1557 while ( $token["type"] == "" )
1558 {
1559 $tokenizer->nextToken();
1560 $token = $tokenizer->previewToken();
1561 }
1562 if ( $token["type"] == "text" )
1563 {
1564 $token = $tokenizer->nextToken();
1565 $x = $token["text"];
1566 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1567
1568 $isbn = $blank = "" ;
1569 while ( " " == $x{0} ) {
1570 $blank .= " ";
1571 $x = substr( $x, 1 );
1572 }
1573 while ( strstr( $valid, $x{0} ) != false ) {
1574 $isbn .= $x{0};
1575 $x = substr( $x, 1 );
1576 }
1577 $num = str_replace( "-", "", $isbn );
1578 $num = str_replace( " ", "", $num );
1579
1580 if ( "" == $num ) {
1581 $text = "ISBN $blank$x";
1582 } else {
1583 $titleObj = Title::makeTitle( NS_SPECIAL, "Booksources" );
1584 $text = "<a href=\"" .
1585 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
1586 "\" class=\"internal\">ISBN $isbn</a>";
1587 $text .= $x;
1588 }
1589 } else {
1590 $text = "ISBN ";
1591 }
1592 return $text;
1593 }
1594 /* private */ function doMagicRFC( &$tokenizer )
1595 {
1596 global $wgLang;
1597
1598 # Check whether next token is a text token
1599 # If yes, fetch it and convert the text into a
1600 # link to an RFC source
1601 $token = $tokenizer->previewToken();
1602 while ( $token["type"] == "" )
1603 {
1604 $tokenizer->nextToken();
1605 $token = $tokenizer->previewToken();
1606 }
1607 if ( $token["type"] == "text" )
1608 {
1609 $token = $tokenizer->nextToken();
1610 $x = $token["text"];
1611 $valid = "0123456789";
1612
1613 $rfc = $blank = "" ;
1614 while ( " " == $x{0} ) {
1615 $blank .= " ";
1616 $x = substr( $x, 1 );
1617 }
1618 while ( strstr( $valid, $x{0} ) != false ) {
1619 $rfc .= $x{0};
1620 $x = substr( $x, 1 );
1621 }
1622
1623 if ( "" == $rfc ) {
1624 $text .= "RFC $blank$x";
1625 } else {
1626 $url = wfmsg( "rfcurl" );
1627 $url = str_replace( "$1", $rfc, $url);
1628 $sk =& $this->mOptions->getSkin();
1629 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
1630 $text = "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
1631 }
1632 } else {
1633 $text = "RFC ";
1634 }
1635 return $text;
1636 }
1637
1638 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
1639 {
1640 $this->mOptions = $options;
1641 $this->mTitle =& $title;
1642 $this->mOutputType = OT_WIKI;
1643
1644 if ( $clearState ) {
1645 $this->clearState();
1646 }
1647
1648 $stripState = false;
1649 $text = str_replace("\r\n", "\n", $text);
1650 $text = $this->strip( $text, $stripState, false );
1651 $text = $this->pstPass2( $text, $user );
1652 $text = $this->unstrip( $text, $stripState );
1653 return $text;
1654 }
1655
1656 /* private */ function pstPass2( $text, &$user )
1657 {
1658 global $wgLang, $wgLocaltimezone, $wgCurParser;
1659
1660 # Variable replacement
1661 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
1662 $text = $this->replaceVariables( $text );
1663
1664 # Signatures
1665 #
1666 $n = $user->getName();
1667 $k = $user->getOption( "nickname" );
1668 if ( "" == $k ) { $k = $n; }
1669 if(isset($wgLocaltimezone)) {
1670 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1671 }
1672 /* Note: this is an ugly timezone hack for the European wikis */
1673 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1674 " (" . date( "T" ) . ")";
1675 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1676
1677 $text = preg_replace( "/~~~~~/", $d, $text );
1678 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1679 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1680 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1681 Namespace::getUser() ) . ":$n|$k]]", $text );
1682
1683 # Context links: [[|name]] and [[name (context)|]]
1684 #
1685 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1686 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1687 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1688 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1689
1690 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1691 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1692 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1693 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1694 # [[ns:page (cont)|]]
1695 $context = "";
1696 $t = $this->mTitle->getText();
1697 if ( preg_match( $conpat, $t, $m ) ) {
1698 $context = $m[2];
1699 }
1700 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1701 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1702 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1703
1704 if ( "" == $context ) {
1705 $text = preg_replace( $p2, "[[\\1]]", $text );
1706 } else {
1707 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1708 }
1709
1710 /*
1711 $mw =& MagicWord::get( MAG_SUBST );
1712 $wgCurParser = $this->fork();
1713 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
1714 $this->merge( $wgCurParser );
1715 */
1716
1717 # Trim trailing whitespace
1718 # MAG_END (__END__) tag allows for trailing
1719 # whitespace to be deliberately included
1720 $text = rtrim( $text );
1721 $mw =& MagicWord::get( MAG_END );
1722 $mw->matchAndRemove( $text );
1723
1724 return $text;
1725 }
1726
1727 # Set up some variables which are usually set up in parse()
1728 # so that an external function can call some class members with confidence
1729 function startExternalParse( &$title, $options, $outputType, $clearState = true )
1730 {
1731 $this->mTitle =& $title;
1732 $this->mOptions = $options;
1733 $this->mOutputType = $outputType;
1734 if ( $clearState ) {
1735 $this->clearState();
1736 }
1737 }
1738 }
1739
1740 class ParserOutput
1741 {
1742 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
1743
1744 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
1745 $containsOldMagic = false )
1746 {
1747 $this->mText = $text;
1748 $this->mLanguageLinks = $languageLinks;
1749 $this->mCategoryLinks = $categoryLinks;
1750 $this->mContainsOldMagic = $containsOldMagic;
1751 }
1752
1753 function getText() { return $this->mText; }
1754 function getLanguageLinks() { return $this->mLanguageLinks; }
1755 function getCategoryLinks() { return $this->mCategoryLinks; }
1756 function containsOldMagic() { return $this->mContainsOldMagic; }
1757 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
1758 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
1759 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
1760 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
1761
1762 function merge( $other ) {
1763 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
1764 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
1765 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
1766 }
1767
1768 }
1769
1770 class ParserOptions
1771 {
1772 # All variables are private
1773 var $mUseTeX; # Use texvc to expand <math> tags
1774 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
1775 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
1776 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
1777 var $mAllowExternalImages; # Allow external images inline
1778 var $mSkin; # Reference to the preferred skin
1779 var $mDateFormat; # Date format index
1780 var $mEditSection; # Create "edit section" links
1781 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
1782 var $mNumberHeadings; # Automatically number headings
1783 var $mShowToc; # Show table of contents
1784
1785 function getUseTeX() { return $this->mUseTeX; }
1786 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
1787 function getUseDynamicDates() { return $this->mUseDynamicDates; }
1788 function getInterwikiMagic() { return $this->mInterwikiMagic; }
1789 function getAllowExternalImages() { return $this->mAllowExternalImages; }
1790 function getSkin() { return $this->mSkin; }
1791 function getDateFormat() { return $this->mDateFormat; }
1792 function getEditSection() { return $this->mEditSection; }
1793 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
1794 function getNumberHeadings() { return $this->mNumberHeadings; }
1795 function getShowToc() { return $this->mShowToc; }
1796
1797 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
1798 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
1799 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
1800 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
1801 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
1802 function setSkin( $x ) { return wfSetRef( $this->mSkin, $x ); }
1803 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
1804 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
1805 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
1806 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
1807 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
1808
1809 /* static */ function newFromUser( &$user )
1810 {
1811 $popts = new ParserOptions;
1812 $popts->initialiseFromUser( &$user );
1813 return $popts;
1814 }
1815
1816 function initialiseFromUser( &$userInput )
1817 {
1818 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
1819
1820 if ( !$userInput ) {
1821 $user = new User;
1822 } else {
1823 $user =& $userInput;
1824 }
1825
1826 $this->mUseTeX = $wgUseTeX;
1827 $this->mUseCategoryMagic = $wgUseCategoryMagic;
1828 $this->mUseDynamicDates = $wgUseDynamicDates;
1829 $this->mInterwikiMagic = $wgInterwikiMagic;
1830 $this->mAllowExternalImages = $wgAllowExternalImages;
1831 $this->mSkin =& $user->getSkin();
1832 $this->mDateFormat = $user->getOption( "date" );
1833 $this->mEditSection = $user->getOption( "editsection" );
1834 $this->mEditSectionOnRightClick = $user->getOption( "editsectiononrightclick" );
1835 $this->mNumberHeadings = $user->getOption( "numberheadings" );
1836 $this->mShowToc = $user->getOption( "showtoc" );
1837 }
1838
1839
1840 }
1841
1842 # Regex callbacks, used in Parser::replaceVariables
1843 function wfBraceSubstitution( $matches )
1844 {
1845 global $wgCurParser;
1846 return $wgCurParser->braceSubstitution( $matches );
1847 }
1848
1849 ?>