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