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