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