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