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