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