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