BUG#925 Fix canonized_headline for e.g. <math>
[lhc/web/wiklou.git] / includes / Parser.php
1 <?php
2
3 /**
4 * File for Parser and related classes
5 *
6 * @package MediaWiki
7 */
8
9 /**
10 * Update this version number when the ParserOutput format
11 * changes in an incompatible way, so the parser cache
12 * can automatically discard old data.
13 */
14 define( 'MW_PARSER_VERSION', '1.4.0' );
15
16 /**
17 * Variable substitution O(N^2) attack
18 *
19 * Without countermeasures, it would be possible to attack the parser by saving
20 * a page filled with a large number of inclusions of large pages. The size of
21 * the generated page would be proportional to the square of the input size.
22 * Hence, we limit the number of inclusions of any given page, thus bringing any
23 * attack back to O(N).
24 */
25
26 define( 'MAX_INCLUDE_REPEAT', 100 );
27 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
28
29 define( 'RLH_FOR_UPDATE', 1 );
30
31 # Allowed values for $mOutputType
32 define( 'OT_HTML', 1 );
33 define( 'OT_WIKI', 2 );
34 define( 'OT_MSG' , 3 );
35
36 # string parameter for extractTags which will cause it
37 # to strip HTML comments in addition to regular
38 # <XML>-style tags. This should not be anything we
39 # may want to use in wikisyntax
40 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
41
42 # prefix for escaping, used in two functions at least
43 define( 'UNIQ_PREFIX', 'NaodW29');
44
45 # Constants needed for external link processing
46 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
47 define( 'HTTP_PROTOCOLS', 'http|https' );
48 # Everything except bracket, space, or control characters
49 define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
50 # Including space
51 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
52 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
53 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
54 define( 'EXT_LINK_BRACKETED', '/\[(\b('.URL_PROTOCOLS.'):'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
55 define( 'EXT_IMAGE_REGEX',
56 '/^('.HTTP_PROTOCOLS.':)'. # Protocol
57 '('.EXT_LINK_URL_CLASS.'+)\\/'. # Hostname and path
58 '('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
59 );
60
61 /**
62 * PHP Parser
63 *
64 * Processes wiki markup
65 *
66 * <pre>
67 * There are three main entry points into the Parser class:
68 * parse()
69 * produces HTML output
70 * preSaveTransform().
71 * produces altered wiki markup.
72 * transformMsg()
73 * performs brace substitution on MediaWiki messages
74 *
75 * Globals used:
76 * objects: $wgLang, $wgDateFormatter, $wgLinkCache
77 *
78 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
79 *
80 * settings:
81 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
82 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
83 * $wgLocaltimezone
84 *
85 * * only within ParserOptions
86 * </pre>
87 *
88 * @package MediaWiki
89 */
90 class Parser
91 {
92 /**#@+
93 * @access private
94 */
95 # Persistent:
96 var $mTagHooks;
97
98 # Cleared with clearState():
99 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
100 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
101
102 # Temporary:
103 var $mOptions, $mTitle, $mOutputType,
104 $mTemplates, // cache of already loaded templates, avoids
105 // multiple SQL queries for the same string
106 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
107 // in this path. Used for loop detection.
108
109 /**#@-*/
110
111 /**
112 * Constructor
113 *
114 * @access public
115 */
116 function Parser() {
117 $this->mTemplates = array();
118 $this->mTemplatePath = array();
119 $this->mTagHooks = array();
120 $this->clearState();
121 }
122
123 /**
124 * Clear Parser state
125 *
126 * @access private
127 */
128 function clearState() {
129 $this->mOutput = new ParserOutput;
130 $this->mAutonumber = 0;
131 $this->mLastSection = "";
132 $this->mDTopen = false;
133 $this->mVariables = false;
134 $this->mIncludeCount = array();
135 $this->mStripState = array();
136 $this->mArgStack = array();
137 $this->mInPre = false;
138 }
139
140 /**
141 * First pass--just handle <nowiki> sections, pass the rest off
142 * to internalParse() which does all the real work.
143 *
144 * @access private
145 * @return ParserOutput a ParserOutput
146 */
147 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
148 global $wgUseTidy, $wgContLang;
149 $fname = 'Parser::parse';
150 wfProfileIn( $fname );
151
152 if ( $clearState ) {
153 $this->clearState();
154 }
155
156 $this->mOptions = $options;
157 $this->mTitle =& $title;
158 $this->mOutputType = OT_HTML;
159
160 $stripState = NULL;
161 global $fnord; $fnord = 1;
162 //$text = $this->strip( $text, $this->mStripState );
163 // VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
164 $x =& $this->mStripState;
165 $text = $this->strip( $text, $x );
166
167 $text = $this->internalParse( $text, $linestart );
168 $text = $this->unstrip( $text, $this->mStripState );
169 # Clean up special characters, only run once, next-to-last before doBlockLevels
170 if(!$wgUseTidy) {
171 $fixtags = array(
172 # french spaces, last one Guillemet-left
173 # only if there is something before the space
174 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1&nbsp;\\2',
175 # french spaces, Guillemet-right
176 "/(\\302\\253) /i"=>"\\1&nbsp;",
177 '/<hr *>/i' => '<hr />',
178 '/<br *>/i' => '<br />',
179 '/<center *>/i' => '<div class="center">',
180 '/<\\/center *>/i' => '</div>',
181 # Clean up spare ampersands; note that we probably ought to be
182 # more careful about named entities.
183 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
184 );
185 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
186 } else {
187 $fixtags = array(
188 # french spaces, last one Guillemet-left
189 '/ (\\?|:|;|!|\\302\\273)/i' => '&nbsp;\\1',
190 # french spaces, Guillemet-right
191 '/(\\302\\253) /i' => '\\1&nbsp;',
192 '/<center *>/i' => '<div class="center">',
193 '/<\\/center *>/i' => '</div>'
194 );
195 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
196 }
197 # only once and last
198 $text = $this->doBlockLevels( $text, $linestart );
199
200 $this->replaceLinkHolders( $text );
201 $text = $wgContLang->convert($text);
202
203 $text = $this->unstripNoWiki( $text, $this->mStripState );
204 global $wgUseTidy;
205 if ($wgUseTidy) {
206 $text = Parser::tidy($text);
207 }
208
209 $this->mOutput->setText( $text );
210 wfProfileOut( $fname );
211 return $this->mOutput;
212 }
213
214 /**
215 * Get a random string
216 *
217 * @access private
218 * @static
219 */
220 function getRandomString() {
221 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
222 }
223
224 /**
225 * Replaces all occurrences of <$tag>content</$tag> in the text
226 * with a random marker and returns the new text. the output parameter
227 * $content will be an associative array filled with data on the form
228 * $unique_marker => content.
229 *
230 * If $content is already set, the additional entries will be appended
231 * If $tag is set to STRIP_COMMENTS, the function will extract
232 * <!-- HTML comments -->
233 *
234 * @access private
235 * @static
236 */
237 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
238 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
239 if ( !$content ) {
240 $content = array( );
241 }
242 $n = 1;
243 $stripped = '';
244
245 while ( '' != $text ) {
246 if($tag==STRIP_COMMENTS) {
247 $p = preg_split( '/<!--/i', $text, 2 );
248 } else {
249 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
250 }
251 $stripped .= $p[0];
252 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
253 $text = '';
254 } else {
255 if($tag==STRIP_COMMENTS) {
256 $q = preg_split( '/-->/i', $p[1], 2 );
257 } else {
258 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
259 }
260 $marker = $rnd . sprintf('%08X', $n++);
261 $content[$marker] = $q[0];
262 $stripped .= $marker;
263 $text = $q[1];
264 }
265 }
266 return $stripped;
267 }
268
269 /**
270 * Strips and renders nowiki, pre, math, hiero
271 * If $render is set, performs necessary rendering operations on plugins
272 * Returns the text, and fills an array with data needed in unstrip()
273 * If the $state is already a valid strip state, it adds to the state
274 *
275 * @param bool $stripcomments when set, HTML comments <!-- like this -->
276 * will be stripped in addition to other tags. This is important
277 * for section editing, where these comments cause confusion when
278 * counting the sections in the wikisource
279 *
280 * @access private
281 */
282 function strip( $text, &$state, $stripcomments = false ) {
283 $render = ($this->mOutputType == OT_HTML);
284 $html_content = array();
285 $nowiki_content = array();
286 $math_content = array();
287 $pre_content = array();
288 $comment_content = array();
289 $ext_content = array();
290 $gallery_content = array();
291
292 # Replace any instances of the placeholders
293 $uniq_prefix = UNIQ_PREFIX;
294 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
295
296 # html
297 global $wgRawHtml, $wgWhitelistEdit;
298 if( $wgRawHtml && $wgWhitelistEdit ) {
299 $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
300 foreach( $html_content as $marker => $content ) {
301 if ($render ) {
302 # Raw and unchecked for validity.
303 $html_content[$marker] = $content;
304 } else {
305 $html_content[$marker] = '<html>'.$content.'</html>';
306 }
307 }
308 }
309
310 # nowiki
311 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
312 foreach( $nowiki_content as $marker => $content ) {
313 if( $render ){
314 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
315 } else {
316 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
317 }
318 }
319
320 # math
321 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
322 foreach( $math_content as $marker => $content ){
323 if( $render ) {
324 if( $this->mOptions->getUseTeX() ) {
325 $math_content[$marker] = renderMath( $content );
326 } else {
327 $math_content[$marker] = '&lt;math&gt;'.$content.'&lt;math&gt;';
328 }
329 } else {
330 $math_content[$marker] = '<math>'.$content.'</math>';
331 }
332 }
333
334 # pre
335 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
336 foreach( $pre_content as $marker => $content ){
337 if( $render ){
338 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
339 } else {
340 $pre_content[$marker] = '<pre>'.$content.'</pre>';
341 }
342 }
343
344 # gallery
345 $text = Parser::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
346 foreach( $gallery_content as $marker => $content ) {
347 require_once( 'ImageGallery.php' );
348 if ( $render ) {
349 $gallery_content[$marker] = Parser::renderImageGallery( $content );
350 } else {
351 $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
352 }
353 }
354
355 # Comments
356 if($stripcomments) {
357 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
358 foreach( $comment_content as $marker => $content ){
359 $comment_content[$marker] = '<!--'.$content.'-->';
360 }
361 }
362
363 # Extensions
364 foreach ( $this->mTagHooks as $tag => $callback ) {
365 $ext_contents[$tag] = array();
366 $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
367 foreach( $ext_content[$tag] as $marker => $content ) {
368 if ( $render ) {
369 $ext_content[$tag][$marker] = $callback( $content );
370 } else {
371 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
372 }
373 }
374 }
375
376 # Merge state with the pre-existing state, if there is one
377 if ( $state ) {
378 $state['html'] = $state['html'] + $html_content;
379 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
380 $state['math'] = $state['math'] + $math_content;
381 $state['pre'] = $state['pre'] + $pre_content;
382 $state['comment'] = $state['comment'] + $comment_content;
383 $state['gallery'] = $state['gallery'] + $gallery_content;
384
385 foreach( $ext_content as $tag => $array ) {
386 if ( array_key_exists( $tag, $state ) ) {
387 $state[$tag] = $state[$tag] + $array;
388 }
389 }
390 } else {
391 $state = array(
392 'html' => $html_content,
393 'nowiki' => $nowiki_content,
394 'math' => $math_content,
395 'pre' => $pre_content,
396 'comment' => $comment_content,
397 'gallery' => $gallery_content,
398 ) + $ext_content;
399 }
400 return $text;
401 }
402
403 /**
404 * restores pre, math, and hiero removed by strip()
405 *
406 * always call unstripNoWiki() after this one
407 * @access private
408 */
409 function unstrip( $text, &$state ) {
410 # Must expand in reverse order, otherwise nested tags will be corrupted
411 $contentDict = end( $state );
412 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
413 if( key($state) != 'nowiki' && key($state) != 'html') {
414 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
415 $text = str_replace( key( $contentDict ), $content, $text );
416 }
417 }
418 }
419
420 return $text;
421 }
422
423 /**
424 * always call this after unstrip() to preserve the order
425 *
426 * @access private
427 */
428 function unstripNoWiki( $text, &$state ) {
429 # Must expand in reverse order, otherwise nested tags will be corrupted
430 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
431 $text = str_replace( key( $state['nowiki'] ), $content, $text );
432 }
433
434 global $wgRawHtml;
435 if ($wgRawHtml) {
436 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
437 $text = str_replace( key( $state['html'] ), $content, $text );
438 }
439 }
440
441 return $text;
442 }
443
444 /**
445 * Add an item to the strip state
446 * Returns the unique tag which must be inserted into the stripped text
447 * The tag will be replaced with the original text in unstrip()
448 *
449 * @access private
450 */
451 function insertStripItem( $text, &$state ) {
452 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
453 if ( !$state ) {
454 $state = array(
455 'html' => array(),
456 'nowiki' => array(),
457 'math' => array(),
458 'pre' => array()
459 );
460 }
461 $state['item'][$rnd] = $text;
462 return $rnd;
463 }
464
465 /**
466 * Return allowed HTML attributes
467 *
468 * @access private
469 */
470 function getHTMLattrs () {
471 $htmlattrs = array( # Allowed attributes--no scripting, etc.
472 'title', 'align', 'lang', 'dir', 'width', 'height',
473 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
474 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
475 /* FONT */ 'type', 'start', 'value', 'compact',
476 /* For various lists, mostly deprecated but safe */
477 'summary', 'width', 'border', 'frame', 'rules',
478 'cellspacing', 'cellpadding', 'valign', 'char',
479 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
480 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
481 'id', 'class', 'name', 'style' /* For CSS */
482 );
483 return $htmlattrs ;
484 }
485
486 /**
487 * Remove non approved attributes and javascript in css
488 *
489 * @access private
490 */
491 function fixTagAttributes ( $t ) {
492 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
493 $htmlattrs = $this->getHTMLattrs() ;
494
495 # Strip non-approved attributes from the tag
496 $t = preg_replace(
497 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
498 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
499 $t);
500
501 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
502
503 # Strip javascript "expression" from stylesheets. Brute force approach:
504 # If anythin offensive is found, all attributes of the HTML tag are dropped
505
506 if( preg_match(
507 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
508 wfMungeToUtf8( $t ) ) )
509 {
510 $t='';
511 }
512
513 return trim ( $t ) ;
514 }
515
516 /**
517 * interface with html tidy, used if $wgUseTidy = true
518 *
519 * @access public
520 * @static
521 */
522 function tidy ( $text ) {
523 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
524 global $wgInputEncoding, $wgOutputEncoding;
525 $fname = 'Parser::tidy';
526 wfProfileIn( $fname );
527
528 $cleansource = '';
529 $opts = '';
530 switch(strtoupper($wgOutputEncoding)) {
531 case 'ISO-8859-1':
532 $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
533 break;
534 case 'UTF-8':
535 $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
536 break;
537 default:
538 $opts .= ' -raw';
539 }
540
541 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
542 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
543 '<head><title>test</title></head><body>'.$text.'</body></html>';
544 $descriptorspec = array(
545 0 => array('pipe', 'r'),
546 1 => array('pipe', 'w'),
547 2 => array('file', '/dev/null', 'a')
548 );
549 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
550 if (is_resource($process)) {
551 fwrite($pipes[0], $wrappedtext);
552 fclose($pipes[0]);
553 while (!feof($pipes[1])) {
554 $cleansource .= fgets($pipes[1], 1024);
555 }
556 fclose($pipes[1]);
557 $return_value = proc_close($process);
558 }
559
560 wfProfileOut( $fname );
561
562 if( $cleansource == '' && $text != '') {
563 wfDebug( "Tidy error detected!\n" );
564 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
565 } else {
566 return $cleansource;
567 }
568 }
569
570 /**
571 * parse the wiki syntax used to render tables
572 *
573 * @access private
574 */
575 function doTableStuff ( $t ) {
576 $fname = 'Parser::doTableStuff';
577 wfProfileIn( $fname );
578
579 $t = explode ( "\n" , $t ) ;
580 $td = array () ; # Is currently a td tag open?
581 $ltd = array () ; # Was it TD or TH?
582 $tr = array () ; # Is currently a tr tag open?
583 $ltr = array () ; # tr attributes
584 $indent_level = 0; # indent level of the table
585 foreach ( $t AS $k => $x )
586 {
587 $x = trim ( $x ) ;
588 $fc = substr ( $x , 0 , 1 ) ;
589 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
590 $indent_level = strlen( $matches[1] );
591 $t[$k] = "\n" .
592 str_repeat( '<dl><dd>', $indent_level ) .
593 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
594 array_push ( $td , false ) ;
595 array_push ( $ltd , '' ) ;
596 array_push ( $tr , false ) ;
597 array_push ( $ltr , '' ) ;
598 }
599 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
600 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
601 $z = "</table>\n" ;
602 $l = array_pop ( $ltd ) ;
603 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
604 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
605 array_pop ( $ltr ) ;
606 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
607 }
608 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
609 $x = substr ( $x , 1 ) ;
610 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
611 $z = '' ;
612 $l = array_pop ( $ltd ) ;
613 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
614 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
615 array_pop ( $ltr ) ;
616 $t[$k] = $z ;
617 array_push ( $tr , false ) ;
618 array_push ( $td , false ) ;
619 array_push ( $ltd , '' ) ;
620 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
621 }
622 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
623 # $x is a table row
624 if ( '|+' == substr ( $x , 0 , 2 ) ) {
625 $fc = '+' ;
626 $x = substr ( $x , 1 ) ;
627 }
628 $after = substr ( $x , 1 ) ;
629 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
630 $after = explode ( '||' , $after ) ;
631 $t[$k] = '' ;
632
633 # Loop through each table cell
634 foreach ( $after AS $theline )
635 {
636 $z = '' ;
637 if ( $fc != '+' )
638 {
639 $tra = array_pop ( $ltr ) ;
640 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
641 array_push ( $tr , true ) ;
642 array_push ( $ltr , '' ) ;
643 }
644
645 $l = array_pop ( $ltd ) ;
646 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
647 if ( $fc == '|' ) $l = 'td' ;
648 else if ( $fc == '!' ) $l = 'th' ;
649 else if ( $fc == '+' ) $l = 'caption' ;
650 else $l = '' ;
651 array_push ( $ltd , $l ) ;
652
653 # Cell parameters
654 $y = explode ( '|' , $theline , 2 ) ;
655 # Note that a '|' inside an invalid link should not
656 # be mistaken as delimiting cell parameters
657 if ( strpos( $y[0], '[[' ) !== false ) {
658 $y = array ($theline);
659 }
660 if ( count ( $y ) == 1 )
661 $y = "{$z}<{$l}>{$y[0]}" ;
662 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
663 $t[$k] .= $y ;
664 array_push ( $td , true ) ;
665 }
666 }
667 }
668
669 # Closing open td, tr && table
670 while ( count ( $td ) > 0 )
671 {
672 if ( array_pop ( $td ) ) $t[] = '</td>' ;
673 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
674 $t[] = '</table>' ;
675 }
676
677 $t = implode ( "\n" , $t ) ;
678 # $t = $this->removeHTMLtags( $t );
679 wfProfileOut( $fname );
680 return $t ;
681 }
682
683 /**
684 * Helper function for parse() that transforms wiki markup into
685 * HTML. Only called for $mOutputType == OT_HTML.
686 *
687 * @access private
688 */
689 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
690 global $wgContLang;
691
692 $fname = 'Parser::internalParse';
693 wfProfileIn( $fname );
694
695 $text = $this->removeHTMLtags( $text );
696 $text = $this->replaceVariables( $text, $args );
697
698 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
699
700 $text = $this->doHeadings( $text );
701 if($this->mOptions->getUseDynamicDates()) {
702 global $wgDateFormatter;
703 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
704 }
705 $text = $this->doAllQuotes( $text );
706 $text = $this->replaceInternalLinks( $text );
707 $text = $this->replaceExternalLinks( $text );
708
709 # replaceInternalLinks may sometimes leave behind
710 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
711 $text = str_replace("http-noparse://","http://",$text);
712
713 $text = $this->doMagicLinks( $text );
714 $text = $this->doTableStuff( $text );
715 $text = $this->formatHeadings( $text, $isMain );
716
717 wfProfileOut( $fname );
718 return $text;
719 }
720
721 /**
722 * Replace special strings like "ISBN xxx" and "RFC xxx" with
723 * magic external links.
724 *
725 * @access private
726 */
727 function &doMagicLinks( &$text ) {
728 global $wgUseGeoMode;
729 $text = $this->magicISBN( $text );
730 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
731 $text = $this->magicGEO( $text );
732 }
733 $text = $this->magicRFC( $text, 'RFC ', 'rfcurl' );
734 $text = $this->magicRFC( $text, 'PMID ', 'pubmedurl' );
735 return $text;
736 }
737
738 /**
739 * Parse ^^ tokens and return html
740 *
741 * @access private
742 */
743 function doExponent( $text ) {
744 $fname = 'Parser::doExponent';
745 wfProfileIn( $fname );
746 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
747 wfProfileOut( $fname );
748 return $text;
749 }
750
751 /**
752 * Parse headers and return html
753 *
754 * @access private
755 */
756 function doHeadings( $text ) {
757 $fname = 'Parser::doHeadings';
758 wfProfileIn( $fname );
759 for ( $i = 6; $i >= 1; --$i ) {
760 $h = substr( '======', 0, $i );
761 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
762 "<h{$i}>\\1</h{$i}>\\2", $text );
763 }
764 wfProfileOut( $fname );
765 return $text;
766 }
767
768 /**
769 * Replace single quotes with HTML markup
770 * @access private
771 * @return string the altered text
772 */
773 function doAllQuotes( $text ) {
774 $fname = 'Parser::doAllQuotes';
775 wfProfileIn( $fname );
776 $outtext = '';
777 $lines = explode( "\n", $text );
778 foreach ( $lines as $line ) {
779 $outtext .= $this->doQuotes ( $line ) . "\n";
780 }
781 $outtext = substr($outtext, 0,-1);
782 wfProfileOut( $fname );
783 return $outtext;
784 }
785
786 /**
787 * Helper function for doAllQuotes()
788 * @access private
789 */
790 function doQuotes( $text ) {
791 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
792 if ( count( $arr ) == 1 )
793 return $text;
794 else
795 {
796 # First, do some preliminary work. This may shift some apostrophes from
797 # being mark-up to being text. It also counts the number of occurrences
798 # of bold and italics mark-ups.
799 $i = 0;
800 $numbold = 0;
801 $numitalics = 0;
802 foreach ( $arr as $r )
803 {
804 if ( ( $i % 2 ) == 1 )
805 {
806 # If there are ever four apostrophes, assume the first is supposed to
807 # be text, and the remaining three constitute mark-up for bold text.
808 if ( strlen( $arr[$i] ) == 4 )
809 {
810 $arr[$i-1] .= "'";
811 $arr[$i] = "'''";
812 }
813 # If there are more than 5 apostrophes in a row, assume they're all
814 # text except for the last 5.
815 else if ( strlen( $arr[$i] ) > 5 )
816 {
817 $arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
818 $arr[$i] = "'''''";
819 }
820 # Count the number of occurrences of bold and italics mark-ups.
821 # We are not counting sequences of five apostrophes.
822 if ( strlen( $arr[$i] ) == 2 ) $numitalics++; else
823 if ( strlen( $arr[$i] ) == 3 ) $numbold++; else
824 if ( strlen( $arr[$i] ) == 5 ) { $numitalics++; $numbold++; }
825 }
826 $i++;
827 }
828
829 # If there is an odd number of both bold and italics, it is likely
830 # that one of the bold ones was meant to be an apostrophe followed
831 # by italics. Which one we cannot know for certain, but it is more
832 # likely to be one that has a single-letter word before it.
833 if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) )
834 {
835 $i = 0;
836 $firstsingleletterword = -1;
837 $firstmultiletterword = -1;
838 $firstspace = -1;
839 foreach ( $arr as $r )
840 {
841 if ( ( $i % 2 == 1 ) and ( strlen( $r ) == 3 ) )
842 {
843 $x1 = substr ($arr[$i-1], -1);
844 $x2 = substr ($arr[$i-1], -2, 1);
845 if ($x1 == ' ') {
846 if ($firstspace == -1) $firstspace = $i;
847 } else if ($x2 == ' ') {
848 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
849 } else {
850 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
851 }
852 }
853 $i++;
854 }
855
856 # If there is a single-letter word, use it!
857 if ($firstsingleletterword > -1)
858 {
859 $arr [ $firstsingleletterword ] = "''";
860 $arr [ $firstsingleletterword-1 ] .= "'";
861 }
862 # If not, but there's a multi-letter word, use that one.
863 else if ($firstmultiletterword > -1)
864 {
865 $arr [ $firstmultiletterword ] = "''";
866 $arr [ $firstmultiletterword-1 ] .= "'";
867 }
868 # ... otherwise use the first one that has neither.
869 # (notice that it is possible for all three to be -1 if, for example,
870 # there is only one pentuple-apostrophe in the line)
871 else if ($firstspace > -1)
872 {
873 $arr [ $firstspace ] = "''";
874 $arr [ $firstspace-1 ] .= "'";
875 }
876 }
877
878 # Now let's actually convert our apostrophic mush to HTML!
879 $output = '';
880 $buffer = '';
881 $state = '';
882 $i = 0;
883 foreach ($arr as $r)
884 {
885 if (($i % 2) == 0)
886 {
887 if ($state == 'both')
888 $buffer .= $r;
889 else
890 $output .= $r;
891 }
892 else
893 {
894 if (strlen ($r) == 2)
895 {
896 if ($state == 'i')
897 { $output .= '</i>'; $state = ''; }
898 else if ($state == 'bi')
899 { $output .= '</i>'; $state = 'b'; }
900 else if ($state == 'ib')
901 { $output .= '</b></i><b>'; $state = 'b'; }
902 else if ($state == 'both')
903 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
904 else # $state can be 'b' or ''
905 { $output .= '<i>'; $state .= 'i'; }
906 }
907 else if (strlen ($r) == 3)
908 {
909 if ($state == 'b')
910 { $output .= '</b>'; $state = ''; }
911 else if ($state == 'bi')
912 { $output .= '</i></b><i>'; $state = 'i'; }
913 else if ($state == 'ib')
914 { $output .= '</b>'; $state = 'i'; }
915 else if ($state == 'both')
916 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
917 else # $state can be 'i' or ''
918 { $output .= '<b>'; $state .= 'b'; }
919 }
920 else if (strlen ($r) == 5)
921 {
922 if ($state == 'b')
923 { $output .= '</b><i>'; $state = 'i'; }
924 else if ($state == 'i')
925 { $output .= '</i><b>'; $state = 'b'; }
926 else if ($state == 'bi')
927 { $output .= '</i></b>'; $state = ''; }
928 else if ($state == 'ib')
929 { $output .= '</b></i>'; $state = ''; }
930 else if ($state == 'both')
931 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
932 else # ($state == '')
933 { $buffer = ''; $state = 'both'; }
934 }
935 }
936 $i++;
937 }
938 # Now close all remaining tags. Notice that the order is important.
939 if ($state == 'b' || $state == 'ib')
940 $output .= '</b>';
941 if ($state == 'i' || $state == 'bi' || $state == 'ib')
942 $output .= '</i>';
943 if ($state == 'bi')
944 $output .= '</b>';
945 if ($state == 'both')
946 $output .= '<b><i>'.$buffer.'</i></b>';
947 return $output;
948 }
949 }
950
951 /**
952 * Replace external links
953 *
954 * Note: this is all very hackish and the order of execution matters a lot.
955 * Make sure to run maintenance/parserTests.php if you change this code.
956 *
957 * @access private
958 */
959 function replaceExternalLinks( $text ) {
960 $fname = 'Parser::replaceExternalLinks';
961 wfProfileIn( $fname );
962
963 $sk =& $this->mOptions->getSkin();
964 global $wgContLang;
965 $linktrail = $wgContLang->linkTrail();
966
967 $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
968
969 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
970
971 $i = 0;
972 while ( $i<count( $bits ) ) {
973 $url = $bits[$i++];
974 $protocol = $bits[$i++];
975 $text = $bits[$i++];
976 $trail = $bits[$i++];
977
978 # The characters '<' and '>' (which were escaped by
979 # removeHTMLtags()) should not be included in
980 # URLs, per RFC 2396.
981 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
982 $text = substr($url, $m2[0][1]) . ' ' . $text;
983 $url = substr($url, 0, $m2[0][1]);
984 }
985
986 # If the link text is an image URL, replace it with an <img> tag
987 # This happened by accident in the original parser, but some people used it extensively
988 $img = $this->maybeMakeImageLink( $text );
989 if ( $img !== false ) {
990 $text = $img;
991 }
992
993 $dtrail = '';
994
995 # Set linktype for CSS - if URL==text, link is essentially free
996 $linktype = ($text == $url) ? 'free' : 'text';
997
998 # No link text, e.g. [http://domain.tld/some.link]
999 if ( $text == '' ) {
1000 # Autonumber if allowed
1001 if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
1002 $text = '[' . ++$this->mAutonumber . ']';
1003 $linktype = 'autonumber';
1004 } else {
1005 # Otherwise just use the URL
1006 $text = htmlspecialchars( $url );
1007 $linktype = 'free';
1008 }
1009 } else {
1010 # Have link text, e.g. [http://domain.tld/some.link text]s
1011 # Check for trail
1012 if ( preg_match( $linktrail, $trail, $m2 ) ) {
1013 $dtrail = $m2[1];
1014 $trail = $m2[2];
1015 }
1016 }
1017
1018 # Process the trail (i.e. everything after this link up until start of the next link),
1019 # replacing any non-bracketed links
1020 $trail = $this->replaceFreeExternalLinks( $trail );
1021
1022 # Use the encoded URL
1023 # This means that users can paste URLs directly into the text
1024 # Funny characters like &ouml; aren't valid in URLs anyway
1025 # This was changed in August 2004
1026 $s .= $sk->makeExternalLink( $url, $text, false, $linktype ) . $dtrail . $trail;
1027 }
1028
1029 wfProfileOut( $fname );
1030 return $s;
1031 }
1032
1033 /**
1034 * Replace anything that looks like a URL with a link
1035 * @access private
1036 */
1037 function replaceFreeExternalLinks( $text ) {
1038 $fname = 'Parser::replaceFreeExternalLinks';
1039 wfProfileIn( $fname );
1040
1041 $bits = preg_split( '/(\b(?:'.URL_PROTOCOLS.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1042 $s = array_shift( $bits );
1043 $i = 0;
1044
1045 $sk =& $this->mOptions->getSkin();
1046
1047 while ( $i < count( $bits ) ){
1048 $protocol = $bits[$i++];
1049 $remainder = $bits[$i++];
1050
1051 if ( preg_match( '/^('.EXT_LINK_URL_CLASS.'+)(.*)$/s', $remainder, $m ) ) {
1052 # Found some characters after the protocol that look promising
1053 $url = $protocol . $m[1];
1054 $trail = $m[2];
1055
1056 # The characters '<' and '>' (which were escaped by
1057 # removeHTMLtags()) should not be included in
1058 # URLs, per RFC 2396.
1059 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
1060 $trail = substr($url, $m2[0][1]) . $trail;
1061 $url = substr($url, 0, $m2[0][1]);
1062 }
1063
1064 # Move trailing punctuation to $trail
1065 $sep = ',;\.:!?';
1066 # If there is no left bracket, then consider right brackets fair game too
1067 if ( strpos( $url, '(' ) === false ) {
1068 $sep .= ')';
1069 }
1070
1071 $numSepChars = strspn( strrev( $url ), $sep );
1072 if ( $numSepChars ) {
1073 $trail = substr( $url, -$numSepChars ) . $trail;
1074 $url = substr( $url, 0, -$numSepChars );
1075 }
1076
1077 # Replace &amp; from obsolete syntax with &.
1078 # All HTML entities will be escaped by makeExternalLink()
1079 # or maybeMakeImageLink()
1080 $url = str_replace( '&amp;', '&', $url );
1081
1082 # Is this an external image?
1083 $text = $this->maybeMakeImageLink( $url );
1084 if ( $text === false ) {
1085 # Not an image, make a link
1086 $text = $sk->makeExternalLink( $url, $url, true, 'free' );
1087 }
1088 $s .= $text . $trail;
1089 } else {
1090 $s .= $protocol . $remainder;
1091 }
1092 }
1093 wfProfileOut();
1094 return $s;
1095 }
1096
1097 /**
1098 * make an image if it's allowed
1099 * @access private
1100 */
1101 function maybeMakeImageLink( $url ) {
1102 $sk =& $this->mOptions->getSkin();
1103 $text = false;
1104 if ( $this->mOptions->getAllowExternalImages() ) {
1105 if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
1106 # Image found
1107 $text = $sk->makeImage( htmlspecialchars( $url ) );
1108 }
1109 }
1110 return $text;
1111 }
1112
1113 /**
1114 * Process [[ ]] wikilinks
1115 *
1116 * @access private
1117 */
1118
1119 function replaceInternalLinks( $s ) {
1120 global $wgLang, $wgContLang, $wgLinkCache;
1121 global $wgDisableLangConversion;
1122 static $fname = 'Parser::replaceInternalLinks' ;
1123
1124 wfProfileIn( $fname );
1125
1126 wfProfileIn( $fname.'-setup' );
1127 static $tc = FALSE;
1128 # the % is needed to support urlencoded titles as well
1129 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1130
1131 $sk =& $this->mOptions->getSkin();
1132 global $wgUseOldExistenceCheck;
1133 # "Post-parse link colour check" works only on wiki text since it's now
1134 # in Parser. Enable it, then disable it when we're done.
1135 $saveParseColour = $sk->postParseLinkColour( !$wgUseOldExistenceCheck );
1136
1137 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1138
1139 #split the entire text string on occurences of [[
1140 $a = explode( '[[', ' ' . $s );
1141 #get the first element (all text up to first [[), and remove the space we added
1142 $s = array_shift( $a );
1143 $s = substr( $s, 1 );
1144
1145 # Match a link having the form [[namespace:link|alternate]]trail
1146 static $e1 = FALSE;
1147 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD"; }
1148 # Match cases where there is no "]]", which might still be images
1149 static $e1_img = FALSE;
1150 if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
1151 # Match the end of a line for a word that's not followed by whitespace,
1152 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1153 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1154
1155 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
1156
1157 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1158
1159 if ( $useLinkPrefixExtension ) {
1160 if ( preg_match( $e2, $s, $m ) ) {
1161 $first_prefix = $m[2];
1162 $s = $m[1];
1163 } else {
1164 $first_prefix = false;
1165 }
1166 } else {
1167 $prefix = '';
1168 }
1169
1170 $selflink = $this->mTitle->getPrefixedText();
1171 wfProfileOut( $fname.'-setup' );
1172
1173 $checkVariantLink = sizeof($wgContLang->getVariants())>1;
1174 $useSubpages = $this->areSubpagesAllowed();
1175
1176 # Loop for each link
1177 for ($k = 0; isset( $a[$k] ); $k++) {
1178 $line = $a[$k];
1179 if ( $useLinkPrefixExtension ) {
1180 wfProfileIn( $fname.'-prefixhandling' );
1181 if ( preg_match( $e2, $s, $m ) ) {
1182 $prefix = $m[2];
1183 $s = $m[1];
1184 } else {
1185 $prefix='';
1186 }
1187 # first link
1188 if($first_prefix) {
1189 $prefix = $first_prefix;
1190 $first_prefix = false;
1191 }
1192 wfProfileOut( $fname.'-prefixhandling' );
1193 }
1194
1195 $might_be_img = false;
1196
1197 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1198 $text = $m[2];
1199 # fix up urlencoded title texts
1200 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1201 $trail = $m[3];
1202 } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
1203 $might_be_img = true;
1204 $text = $m[2];
1205 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1206 $trail = "";
1207 } else { # Invalid form; output directly
1208 $s .= $prefix . '[[' . $line ;
1209 continue;
1210 }
1211
1212 # Don't allow internal links to pages containing
1213 # PROTO: where PROTO is a valid URL protocol; these
1214 # should be external links.
1215 if (preg_match('/^(\b(?:'.URL_PROTOCOLS.'):)/', $m[1])) {
1216 $s .= $prefix . '[[' . $line ;
1217 continue;
1218 }
1219
1220 # Make subpage if necessary
1221 if( $useSubpages ) {
1222 $link = $this->maybeDoSubpageLink( $m[1], $text );
1223 } else {
1224 $link = $m[1];
1225 }
1226
1227 $noforce = (substr($m[1], 0, 1) != ':');
1228 if (!$noforce) {
1229 # Strip off leading ':'
1230 $link = substr($link, 1);
1231 }
1232
1233 $nt =& Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
1234 if( !$nt ) {
1235 $s .= $prefix . '[[' . $line;
1236 continue;
1237 }
1238
1239 #check other language variants of the link
1240 #if the article does not exist
1241 if( $checkVariantLink
1242 && $nt->getArticleID() == 0 ) {
1243 $wgContLang->findVariantLink($link, $nt);
1244 }
1245
1246 $ns = $nt->getNamespace();
1247 $iw = $nt->getInterWiki();
1248
1249 if ($might_be_img) { # if this is actually an invalid link
1250 if ($ns == NS_IMAGE && $noforce) { #but might be an image
1251 $found = false;
1252 while (isset ($a[$k+1]) ) {
1253 #look at the next 'line' to see if we can close it there
1254 $next_line = array_shift(array_splice( $a, $k + 1, 1) );
1255 if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
1256 # the first ]] closes the inner link, the second the image
1257 $found = true;
1258 $text .= '[[' . $m[1];
1259 $trail = $m[2];
1260 break;
1261 } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
1262 #if there's exactly one ]] that's fine, we'll keep looking
1263 $text .= '[[' . $m[0];
1264 } else {
1265 #if $next_line is invalid too, we need look no further
1266 $text .= '[[' . $next_line;
1267 break;
1268 }
1269 }
1270 if ( !$found ) {
1271 # we couldn't find the end of this imageLink, so output it raw
1272 #but don't ignore what might be perfectly normal links in the text we've examined
1273 $text = $this->replaceInternalLinks($text);
1274 $s .= $prefix . '[[' . $link . '|' . $text;
1275 # note: no $trail, because without an end, there *is* no trail
1276 continue;
1277 }
1278 } else { #it's not an image, so output it raw
1279 $s .= $prefix . '[[' . $link . '|' . $text;
1280 # note: no $trail, because without an end, there *is* no trail
1281 continue;
1282 }
1283 }
1284
1285 $wasblank = ( '' == $text );
1286 if( $wasblank ) $text = $link;
1287
1288
1289 # Link not escaped by : , create the various objects
1290 if( $noforce ) {
1291
1292 # Interwikis
1293 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1294 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1295 $tmp = $prefix . $trail ;
1296 $s .= (trim($tmp) == '')? '': $tmp;
1297 continue;
1298 }
1299
1300 if ( $ns == NS_IMAGE ) {
1301 wfProfileIn( "$fname-image" );
1302
1303 # recursively parse links inside the image caption
1304 # actually, this will parse them in any other parameters, too,
1305 # but it might be hard to fix that, and it doesn't matter ATM
1306 $text = $this->replaceExternalLinks($text);
1307 $text = $this->replaceInternalLinks($text);
1308
1309 # replace the image with a link-holder so that replaceExternalLinks() can't mess with it
1310 $s .= $prefix . $this->insertStripItem( $sk->makeImageLinkObj( $nt, $text ), $this->mStripState ) . $trail;
1311 $wgLinkCache->addImageLinkObj( $nt );
1312
1313 wfProfileOut( "$fname-image" );
1314 continue;
1315 }
1316
1317 if ( $ns == NS_CATEGORY ) {
1318 wfProfileIn( "$fname-category" );
1319 $t = $nt->getText();
1320
1321 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1322 $pPLC=$sk->postParseLinkColour();
1323 $sk->postParseLinkColour( false );
1324 $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
1325 $sk->postParseLinkColour( $pPLC );
1326 $wgLinkCache->resume();
1327
1328 if ( $wasblank ) {
1329 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
1330 $sortkey = $this->mTitle->getText();
1331 } else {
1332 $sortkey = $this->mTitle->getPrefixedText();
1333 }
1334 } else {
1335 $sortkey = $text;
1336 }
1337 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1338 $this->mOutput->addCategoryLink( $t );
1339 $s .= $prefix . $trail ;
1340
1341 wfProfileOut( "$fname-category" );
1342 continue;
1343 }
1344 }
1345
1346 if( ( $nt->getPrefixedText() === $selflink ) &&
1347 ( $nt->getFragment() === '' ) ) {
1348 # Self-links are handled specially; generally de-link and change to bold.
1349 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1350 continue;
1351 }
1352
1353 # Special and Media are pseudo-namespaces; no pages actually exist in them
1354 if( $ns == NS_MEDIA ) {
1355 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text, true ) . $trail;
1356 $wgLinkCache->addImageLinkObj( $nt );
1357 continue;
1358 } elseif( $ns == NS_SPECIAL ) {
1359 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1360 continue;
1361 }
1362 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1363 }
1364 $sk->postParseLinkColour( $saveParseColour );
1365 wfProfileOut( $fname );
1366 return $s;
1367 }
1368
1369 /**
1370 * Return true if subpage links should be expanded on this page.
1371 * @return bool
1372 */
1373 function areSubpagesAllowed() {
1374 # Some namespaces don't allow subpages
1375 global $wgNamespacesWithSubpages;
1376 return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
1377 }
1378
1379 /**
1380 * Handle link to subpage if necessary
1381 * @param string $target the source of the link
1382 * @param string &$text the link text, modified as necessary
1383 * @return string the full name of the link
1384 * @access private
1385 */
1386 function maybeDoSubpageLink($target, &$text) {
1387 # Valid link forms:
1388 # Foobar -- normal
1389 # :Foobar -- override special treatment of prefix (images, language links)
1390 # /Foobar -- convert to CurrentPage/Foobar
1391 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1392 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1393 # ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
1394
1395 $fname = 'Parser::maybeDoSubpageLink';
1396 wfProfileIn( $fname );
1397 $ret = $target; # default return value is no change
1398
1399 # Some namespaces don't allow subpages,
1400 # so only perform processing if subpages are allowed
1401 if( $this->areSubpagesAllowed() ) {
1402 # Look at the first character
1403 if( $target != '' && $target{0} == '/' ) {
1404 # / at end means we don't want the slash to be shown
1405 if( substr( $target, -1, 1 ) == '/' ) {
1406 $target = substr( $target, 1, -1 );
1407 $noslash = $target;
1408 } else {
1409 $noslash = substr( $target, 1 );
1410 }
1411
1412 $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1413 if( '' === $text ) {
1414 $text = $target;
1415 } # this might be changed for ugliness reasons
1416 } else {
1417 # check for .. subpage backlinks
1418 $dotdotcount = 0;
1419 $nodotdot = $target;
1420 while( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1421 ++$dotdotcount;
1422 $nodotdot = substr( $nodotdot, 3 );
1423 }
1424 if($dotdotcount > 0) {
1425 $exploded = explode( '/', $this->mTitle->GetPrefixedText() );
1426 if( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1427 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1428 # / at the end means don't show full path
1429 if( substr( $nodotdot, -1, 1 ) == '/' ) {
1430 $nodotdot = substr( $nodotdot, 0, -1 );
1431 if( '' === $text ) {
1432 $text = $nodotdot;
1433 }
1434 }
1435 $nodotdot = trim( $nodotdot );
1436 if( $nodotdot != '' ) {
1437 $ret .= '/' . $nodotdot;
1438 }
1439 }
1440 }
1441 }
1442 }
1443
1444 wfProfileOut( $fname );
1445 return $ret;
1446 }
1447
1448 /**#@+
1449 * Used by doBlockLevels()
1450 * @access private
1451 */
1452 /* private */ function closeParagraph() {
1453 $result = '';
1454 if ( '' != $this->mLastSection ) {
1455 $result = '</' . $this->mLastSection . ">\n";
1456 }
1457 $this->mInPre = false;
1458 $this->mLastSection = '';
1459 return $result;
1460 }
1461 # getCommon() returns the length of the longest common substring
1462 # of both arguments, starting at the beginning of both.
1463 #
1464 /* private */ function getCommon( $st1, $st2 ) {
1465 $fl = strlen( $st1 );
1466 $shorter = strlen( $st2 );
1467 if ( $fl < $shorter ) { $shorter = $fl; }
1468
1469 for ( $i = 0; $i < $shorter; ++$i ) {
1470 if ( $st1{$i} != $st2{$i} ) { break; }
1471 }
1472 return $i;
1473 }
1474 # These next three functions open, continue, and close the list
1475 # element appropriate to the prefix character passed into them.
1476 #
1477 /* private */ function openList( $char ) {
1478 $result = $this->closeParagraph();
1479
1480 if ( '*' == $char ) { $result .= '<ul><li>'; }
1481 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1482 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1483 else if ( ';' == $char ) {
1484 $result .= '<dl><dt>';
1485 $this->mDTopen = true;
1486 }
1487 else { $result = '<!-- ERR 1 -->'; }
1488
1489 return $result;
1490 }
1491
1492 /* private */ function nextItem( $char ) {
1493 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1494 else if ( ':' == $char || ';' == $char ) {
1495 $close = '</dd>';
1496 if ( $this->mDTopen ) { $close = '</dt>'; }
1497 if ( ';' == $char ) {
1498 $this->mDTopen = true;
1499 return $close . '<dt>';
1500 } else {
1501 $this->mDTopen = false;
1502 return $close . '<dd>';
1503 }
1504 }
1505 return '<!-- ERR 2 -->';
1506 }
1507
1508 /* private */ function closeList( $char ) {
1509 if ( '*' == $char ) { $text = '</li></ul>'; }
1510 else if ( '#' == $char ) { $text = '</li></ol>'; }
1511 else if ( ':' == $char ) {
1512 if ( $this->mDTopen ) {
1513 $this->mDTopen = false;
1514 $text = '</dt></dl>';
1515 } else {
1516 $text = '</dd></dl>';
1517 }
1518 }
1519 else { return '<!-- ERR 3 -->'; }
1520 return $text."\n";
1521 }
1522 /**#@-*/
1523
1524 /**
1525 * Make lists from lines starting with ':', '*', '#', etc.
1526 *
1527 * @access private
1528 * @return string the lists rendered as HTML
1529 */
1530 function doBlockLevels( $text, $linestart ) {
1531 $fname = 'Parser::doBlockLevels';
1532 wfProfileIn( $fname );
1533
1534 # Parsing through the text line by line. The main thing
1535 # happening here is handling of block-level elements p, pre,
1536 # and making lists from lines starting with * # : etc.
1537 #
1538 $textLines = explode( "\n", $text );
1539
1540 $lastPrefix = $output = $lastLine = '';
1541 $this->mDTopen = $inBlockElem = false;
1542 $prefixLength = 0;
1543 $paragraphStack = false;
1544
1545 if ( !$linestart ) {
1546 $output .= array_shift( $textLines );
1547 }
1548 foreach ( $textLines as $oLine ) {
1549 $lastPrefixLength = strlen( $lastPrefix );
1550 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1551 $preOpenMatch = preg_match('/<pre/i', $oLine );
1552 if ( !$this->mInPre ) {
1553 # Multiple prefixes may abut each other for nested lists.
1554 $prefixLength = strspn( $oLine, '*#:;' );
1555 $pref = substr( $oLine, 0, $prefixLength );
1556
1557 # eh?
1558 $pref2 = str_replace( ';', ':', $pref );
1559 $t = substr( $oLine, $prefixLength );
1560 $this->mInPre = !empty($preOpenMatch);
1561 } else {
1562 # Don't interpret any other prefixes in preformatted text
1563 $prefixLength = 0;
1564 $pref = $pref2 = '';
1565 $t = $oLine;
1566 }
1567
1568 # List generation
1569 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1570 # Same as the last item, so no need to deal with nesting or opening stuff
1571 $output .= $this->nextItem( substr( $pref, -1 ) );
1572 $paragraphStack = false;
1573
1574 if ( substr( $pref, -1 ) == ';') {
1575 # The one nasty exception: definition lists work like this:
1576 # ; title : definition text
1577 # So we check for : in the remainder text to split up the
1578 # title and definition, without b0rking links.
1579 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1580 $t = $t2;
1581 $output .= $term . $this->nextItem( ':' );
1582 }
1583 }
1584 } elseif( $prefixLength || $lastPrefixLength ) {
1585 # Either open or close a level...
1586 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1587 $paragraphStack = false;
1588
1589 while( $commonPrefixLength < $lastPrefixLength ) {
1590 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1591 --$lastPrefixLength;
1592 }
1593 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1594 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1595 }
1596 while ( $prefixLength > $commonPrefixLength ) {
1597 $char = substr( $pref, $commonPrefixLength, 1 );
1598 $output .= $this->openList( $char );
1599
1600 if ( ';' == $char ) {
1601 # FIXME: This is dupe of code above
1602 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1603 $t = $t2;
1604 $output .= $term . $this->nextItem( ':' );
1605 }
1606 }
1607 ++$commonPrefixLength;
1608 }
1609 $lastPrefix = $pref2;
1610 }
1611 if( 0 == $prefixLength ) {
1612 wfProfileIn( "$fname-paragraph" );
1613 # No prefix (not in list)--go to paragraph mode
1614 $uniq_prefix = UNIQ_PREFIX;
1615 // XXX: use a stack for nestable elements like span, table and div
1616 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
1617 $closematch = preg_match(
1618 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1619 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/iS', $t );
1620 if ( $openmatch or $closematch ) {
1621 $paragraphStack = false;
1622 $output .= $this->closeParagraph();
1623 if($preOpenMatch and !$preCloseMatch) {
1624 $this->mInPre = true;
1625 }
1626 if ( $closematch ) {
1627 $inBlockElem = false;
1628 } else {
1629 $inBlockElem = true;
1630 }
1631 } else if ( !$inBlockElem && !$this->mInPre ) {
1632 if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1633 // pre
1634 if ($this->mLastSection != 'pre') {
1635 $paragraphStack = false;
1636 $output .= $this->closeParagraph().'<pre>';
1637 $this->mLastSection = 'pre';
1638 }
1639 $t = substr( $t, 1 );
1640 } else {
1641 // paragraph
1642 if ( '' == trim($t) ) {
1643 if ( $paragraphStack ) {
1644 $output .= $paragraphStack.'<br />';
1645 $paragraphStack = false;
1646 $this->mLastSection = 'p';
1647 } else {
1648 if ($this->mLastSection != 'p' ) {
1649 $output .= $this->closeParagraph();
1650 $this->mLastSection = '';
1651 $paragraphStack = '<p>';
1652 } else {
1653 $paragraphStack = '</p><p>';
1654 }
1655 }
1656 } else {
1657 if ( $paragraphStack ) {
1658 $output .= $paragraphStack;
1659 $paragraphStack = false;
1660 $this->mLastSection = 'p';
1661 } else if ($this->mLastSection != 'p') {
1662 $output .= $this->closeParagraph().'<p>';
1663 $this->mLastSection = 'p';
1664 }
1665 }
1666 }
1667 }
1668 wfProfileOut( "$fname-paragraph" );
1669 }
1670 if ($paragraphStack === false) {
1671 $output .= $t."\n";
1672 }
1673 }
1674 while ( $prefixLength ) {
1675 $output .= $this->closeList( $pref2{$prefixLength-1} );
1676 --$prefixLength;
1677 }
1678 if ( '' != $this->mLastSection ) {
1679 $output .= '</' . $this->mLastSection . '>';
1680 $this->mLastSection = '';
1681 }
1682
1683 wfProfileOut( $fname );
1684 return $output;
1685 }
1686
1687 /**
1688 * Split up a string on ':', ignoring any occurences inside
1689 * <a>..</a> or <span>...</span>
1690 * @param string $str the string to split
1691 * @param string &$before set to everything before the ':'
1692 * @param string &$after set to everything after the ':'
1693 * return string the position of the ':', or false if none found
1694 */
1695 function findColonNoLinks($str, &$before, &$after) {
1696 # I wonder if we should make this count all tags, not just <a>
1697 # and <span>. That would prevent us from matching a ':' that
1698 # comes in the middle of italics other such formatting....
1699 # -- Wil
1700 $fname = 'Parser::findColonNoLinks';
1701 wfProfileIn( $fname );
1702 $pos = 0;
1703 do {
1704 $colon = strpos($str, ':', $pos);
1705
1706 if ($colon !== false) {
1707 $before = substr($str, 0, $colon);
1708 $after = substr($str, $colon + 1);
1709
1710 # Skip any ':' within <a> or <span> pairs
1711 $a = substr_count($before, '<a');
1712 $s = substr_count($before, '<span');
1713 $ca = substr_count($before, '</a>');
1714 $cs = substr_count($before, '</span>');
1715
1716 if ($a <= $ca and $s <= $cs) {
1717 # Tags are balanced before ':'; ok
1718 break;
1719 }
1720 $pos = $colon + 1;
1721 }
1722 } while ($colon !== false);
1723 wfProfileOut( $fname );
1724 return $colon;
1725 }
1726
1727 /**
1728 * Return value of a magic variable (like PAGENAME)
1729 *
1730 * @access private
1731 */
1732 function getVariableValue( $index ) {
1733 global $wgContLang, $wgSitename, $wgServer;
1734
1735 /**
1736 * Some of these require message or data lookups and can be
1737 * expensive to check many times.
1738 */
1739 static $varCache = array();
1740 if( isset( $varCache[$index] ) ) return $varCache[$index];
1741
1742 switch ( $index ) {
1743 case MAG_CURRENTMONTH:
1744 return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
1745 case MAG_CURRENTMONTHNAME:
1746 return $varCache[$index] = $wgContLang->getMonthName( date('n') );
1747 case MAG_CURRENTMONTHNAMEGEN:
1748 return $varCache[$index] = $wgContLang->getMonthNameGen( date('n') );
1749 case MAG_CURRENTDAY:
1750 return $varCache[$index] = $wgContLang->formatNum( date('j') );
1751 case MAG_PAGENAME:
1752 return $this->mTitle->getText();
1753 case MAG_PAGENAMEE:
1754 return $this->mTitle->getPartialURL();
1755 case MAG_NAMESPACE:
1756 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1757 return $wgContLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
1758 case MAG_CURRENTDAYNAME:
1759 return $varCache[$index] = $wgContLang->getWeekdayName( date('w')+1 );
1760 case MAG_CURRENTYEAR:
1761 return $varCache[$index] = $wgContLang->formatNum( date( 'Y' ) );
1762 case MAG_CURRENTTIME:
1763 return $varCache[$index] = $wgContLang->time( wfTimestampNow(), false );
1764 case MAG_CURRENTWEEK:
1765 return $varCache[$index] = $wgContLang->formatNum( date('W') );
1766 case MAG_CURRENTDOW:
1767 return $varCache[$index] = $wgContLang->formatNum( date('w') );
1768 case MAG_NUMBEROFARTICLES:
1769 return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
1770 case MAG_SITENAME:
1771 return $wgSitename;
1772 case MAG_SERVER:
1773 return $wgServer;
1774 default:
1775 return NULL;
1776 }
1777 }
1778
1779 /**
1780 * initialise the magic variables (like CURRENTMONTHNAME)
1781 *
1782 * @access private
1783 */
1784 function initialiseVariables() {
1785 $fname = 'Parser::initialiseVariables';
1786 wfProfileIn( $fname );
1787 global $wgVariableIDs;
1788 $this->mVariables = array();
1789 foreach ( $wgVariableIDs as $id ) {
1790 $mw =& MagicWord::get( $id );
1791 $mw->addToArray( $this->mVariables, $id );
1792 }
1793 wfProfileOut( $fname );
1794 }
1795
1796 /**
1797 * Replace magic variables, templates, and template arguments
1798 * with the appropriate text. Templates are substituted recursively,
1799 * taking care to avoid infinite loops.
1800 *
1801 * Note that the substitution depends on value of $mOutputType:
1802 * OT_WIKI: only {{subst:}} templates
1803 * OT_MSG: only magic variables
1804 * OT_HTML: all templates and magic variables
1805 *
1806 * @param string $tex The text to transform
1807 * @param array $args Key-value pairs representing template parameters to substitute
1808 * @access private
1809 */
1810 function replaceVariables( $text, $args = array() ) {
1811 global $wgLang, $wgScript, $wgArticlePath;
1812
1813 # Prevent too big inclusions
1814 if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
1815 return $text;
1816 }
1817
1818 $fname = 'Parser::replaceVariables';
1819 wfProfileIn( $fname );
1820
1821 $titleChars = Title::legalChars();
1822
1823 # This function is called recursively. To keep track of arguments we need a stack:
1824 array_push( $this->mArgStack, $args );
1825
1826 # Variable substitution
1827 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
1828
1829 if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
1830 # Argument substitution
1831 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
1832 }
1833 # Template substitution
1834 $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1835 $text = preg_replace_callback( $regex, array( &$this, 'braceSubstitution' ), $text );
1836
1837 array_pop( $this->mArgStack );
1838
1839 wfProfileOut( $fname );
1840 return $text;
1841 }
1842
1843 /**
1844 * Replace magic variables
1845 * @access private
1846 */
1847 function variableSubstitution( $matches ) {
1848 $fname = 'parser::variableSubstitution';
1849 $varname = $matches[1];
1850 wfProfileIn( $fname );
1851 if ( !$this->mVariables ) {
1852 $this->initialiseVariables();
1853 }
1854 $skip = false;
1855 if ( $this->mOutputType == OT_WIKI ) {
1856 # Do only magic variables prefixed by SUBST
1857 $mwSubst =& MagicWord::get( MAG_SUBST );
1858 if (!$mwSubst->matchStartAndRemove( $varname ))
1859 $skip = true;
1860 # Note that if we don't substitute the variable below,
1861 # we don't remove the {{subst:}} magic word, in case
1862 # it is a template rather than a magic variable.
1863 }
1864 if ( !$skip && array_key_exists( $varname, $this->mVariables ) ) {
1865 $id = $this->mVariables[$varname];
1866 $text = $this->getVariableValue( $id );
1867 $this->mOutput->mContainsOldMagic = true;
1868 } else {
1869 $text = $matches[0];
1870 }
1871 wfProfileOut( $fname );
1872 return $text;
1873 }
1874
1875 # Split template arguments
1876 function getTemplateArgs( $argsString ) {
1877 if ( $argsString === '' ) {
1878 return array();
1879 }
1880
1881 $args = explode( '|', substr( $argsString, 1 ) );
1882
1883 # If any of the arguments contains a '[[' but no ']]', it needs to be
1884 # merged with the next arg because the '|' character between belongs
1885 # to the link syntax and not the template parameter syntax.
1886 $argc = count($args);
1887 $i = 0;
1888 for ( $i = 0; $i < $argc-1; $i++ ) {
1889 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1890 $args[$i] .= '|'.$args[$i+1];
1891 array_splice($args, $i+1, 1);
1892 $i--;
1893 $argc--;
1894 }
1895 }
1896
1897 return $args;
1898 }
1899
1900 /**
1901 * Return the text of a template, after recursively
1902 * replacing any variables or templates within the template.
1903 *
1904 * @param array $matches The parts of the template
1905 * $matches[1]: the title, i.e. the part before the |
1906 * $matches[2]: the parameters (including a leading |), if any
1907 * @return string the text of the template
1908 * @access private
1909 */
1910 function braceSubstitution( $matches ) {
1911 global $wgLinkCache, $wgContLang;
1912 $fname = 'Parser::braceSubstitution';
1913 wfProfileIn( $fname );
1914
1915 $found = false;
1916 $nowiki = false;
1917 $noparse = false;
1918
1919 $title = NULL;
1920
1921 # Need to know if the template comes at the start of a line,
1922 # to treat the beginning of the template like the beginning
1923 # of a line for tables and block-level elements.
1924 $linestart = $matches[1];
1925
1926 # $part1 is the bit before the first |, and must contain only title characters
1927 # $args is a list of arguments, starting from index 0, not including $part1
1928
1929 $part1 = $matches[2];
1930 # If the third subpattern matched anything, it will start with |
1931
1932 $args = $this->getTemplateArgs($matches[3]);
1933 $argc = count( $args );
1934
1935 # Don't parse {{{}}} because that's only for template arguments
1936 if ( $linestart === '{' ) {
1937 $text = $matches[0];
1938 $found = true;
1939 $noparse = true;
1940 }
1941
1942 # SUBST
1943 if ( !$found ) {
1944 $mwSubst =& MagicWord::get( MAG_SUBST );
1945 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
1946 # One of two possibilities is true:
1947 # 1) Found SUBST but not in the PST phase
1948 # 2) Didn't find SUBST and in the PST phase
1949 # In either case, return without further processing
1950 $text = $matches[0];
1951 $found = true;
1952 $noparse = true;
1953 }
1954 }
1955
1956 # MSG, MSGNW and INT
1957 if ( !$found ) {
1958 # Check for MSGNW:
1959 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1960 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1961 $nowiki = true;
1962 } else {
1963 # Remove obsolete MSG:
1964 $mwMsg =& MagicWord::get( MAG_MSG );
1965 $mwMsg->matchStartAndRemove( $part1 );
1966 }
1967
1968 # Check if it is an internal message
1969 $mwInt =& MagicWord::get( MAG_INT );
1970 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1971 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1972 $text = $linestart . wfMsgReal( $part1, $args, true );
1973 $found = true;
1974 }
1975 }
1976 }
1977
1978 # NS
1979 if ( !$found ) {
1980 # Check for NS: (namespace expansion)
1981 $mwNs = MagicWord::get( MAG_NS );
1982 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1983 if ( intval( $part1 ) ) {
1984 $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
1985 $found = true;
1986 } else {
1987 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1988 if ( !is_null( $index ) ) {
1989 $text = $linestart . $wgContLang->getNsText( $index );
1990 $found = true;
1991 }
1992 }
1993 }
1994 }
1995
1996 # LOCALURL and LOCALURLE
1997 if ( !$found ) {
1998 $mwLocal = MagicWord::get( MAG_LOCALURL );
1999 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
2000
2001 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
2002 $func = 'getLocalURL';
2003 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
2004 $func = 'escapeLocalURL';
2005 } else {
2006 $func = '';
2007 }
2008
2009 if ( $func !== '' ) {
2010 $title = Title::newFromText( $part1 );
2011 if ( !is_null( $title ) ) {
2012 if ( $argc > 0 ) {
2013 $text = $linestart . $title->$func( $args[0] );
2014 } else {
2015 $text = $linestart . $title->$func();
2016 }
2017 $found = true;
2018 }
2019 }
2020 }
2021
2022 # GRAMMAR
2023 if ( !$found && $argc == 1 ) {
2024 $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
2025 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
2026 $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
2027 $found = true;
2028 }
2029 }
2030
2031 # Template table test
2032
2033 # Did we encounter this template already? If yes, it is in the cache
2034 # and we need to check for loops.
2035 if ( !$found && isset( $this->mTemplates[$part1] ) ) {
2036 # set $text to cached message.
2037 $text = $linestart . $this->mTemplates[$part1];
2038 $found = true;
2039
2040 # Infinite loop test
2041 if ( isset( $this->mTemplatePath[$part1] ) ) {
2042 $noparse = true;
2043 $found = true;
2044 $text .= '<!-- WARNING: template loop detected -->';
2045 }
2046 }
2047
2048 # Load from database
2049 $itcamefromthedatabase = false;
2050 if ( !$found ) {
2051 $ns = NS_TEMPLATE;
2052 $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
2053 if ($subpage !== '') {
2054 $ns = $this->mTitle->getNamespace();
2055 }
2056 $title = Title::newFromText( $part1, $ns );
2057 if ( !is_null( $title ) && !$title->isExternal() ) {
2058 # Check for excessive inclusion
2059 $dbk = $title->getPrefixedDBkey();
2060 if ( $this->incrementIncludeCount( $dbk ) ) {
2061 # This should never be reached.
2062 $article = new Article( $title );
2063 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
2064 if ( $articleContent !== false ) {
2065 $found = true;
2066 $text = $linestart . $articleContent;
2067 $itcamefromthedatabase = true;
2068 }
2069 }
2070
2071 # If the title is valid but undisplayable, make a link to it
2072 if ( $this->mOutputType == OT_HTML && !$found ) {
2073 $text = $linestart . '[['.$title->getPrefixedText().']]';
2074 $found = true;
2075 }
2076
2077 # Template cache array insertion
2078 $this->mTemplates[$part1] = $text;
2079 }
2080 }
2081
2082 # Recursive parsing, escaping and link table handling
2083 # Only for HTML output
2084 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
2085 $text = wfEscapeWikiText( $text );
2086 } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
2087 # Clean up argument array
2088 $assocArgs = array();
2089 $index = 1;
2090 foreach( $args as $arg ) {
2091 $eqpos = strpos( $arg, '=' );
2092 if ( $eqpos === false ) {
2093 $assocArgs[$index++] = $arg;
2094 } else {
2095 $name = trim( substr( $arg, 0, $eqpos ) );
2096 $value = trim( substr( $arg, $eqpos+1 ) );
2097 if ( $value === false ) {
2098 $value = '';
2099 }
2100 if ( $name !== false ) {
2101 $assocArgs[$name] = $value;
2102 }
2103 }
2104 }
2105
2106 # Add a new element to the templace recursion path
2107 $this->mTemplatePath[$part1] = 1;
2108
2109 $text = $this->strip( $text, $this->mStripState );
2110 $text = $this->removeHTMLtags( $text );
2111 $text = $this->replaceVariables( $text, $assocArgs );
2112
2113 # Resume the link cache and register the inclusion as a link
2114 if ( $this->mOutputType == OT_HTML && !is_null( $title ) ) {
2115 $wgLinkCache->addLinkObj( $title );
2116 }
2117
2118 # If the template begins with a table or block-level
2119 # element, it should be treated as beginning a new line.
2120 if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
2121 $text = "\n" . $text;
2122 }
2123 }
2124
2125 # Empties the template path
2126 $this->mTemplatePath = array();
2127 if ( !$found ) {
2128 wfProfileOut( $fname );
2129 return $matches[0];
2130 } else {
2131 # replace ==section headers==
2132 # XXX this needs to go away once we have a better parser.
2133 if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
2134 if( !is_null( $title ) )
2135 $encodedname = base64_encode($title->getPrefixedDBkey());
2136 else
2137 $encodedname = base64_encode("");
2138 $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
2139 PREG_SPLIT_DELIM_CAPTURE);
2140 $text = '';
2141 $nsec = 0;
2142 for( $i = 0; $i < count($m); $i += 2 ) {
2143 $text .= $m[$i];
2144 if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
2145 $hl = $m[$i + 1];
2146 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
2147 $text .= $hl;
2148 continue;
2149 }
2150 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
2151 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
2152 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
2153
2154 $nsec++;
2155 }
2156 }
2157 }
2158
2159 # Empties the template path
2160 $this->mTemplatePath = array();
2161
2162 if ( !$found ) {
2163 wfProfileOut( $fname );
2164 return $matches[0];
2165 } else {
2166 wfProfileOut( $fname );
2167 return $text;
2168 }
2169 }
2170
2171 /**
2172 * Triple brace replacement -- used for template arguments
2173 * @access private
2174 */
2175 function argSubstitution( $matches ) {
2176 $arg = trim( $matches[1] );
2177 $text = $matches[0];
2178 $inputArgs = end( $this->mArgStack );
2179
2180 if ( array_key_exists( $arg, $inputArgs ) ) {
2181 $text = $inputArgs[$arg];
2182 }
2183
2184 return $text;
2185 }
2186
2187 /**
2188 * Returns true if the function is allowed to include this entity
2189 * @access private
2190 */
2191 function incrementIncludeCount( $dbk ) {
2192 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
2193 $this->mIncludeCount[$dbk] = 0;
2194 }
2195 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
2196 return true;
2197 } else {
2198 return false;
2199 }
2200 }
2201
2202
2203 /**
2204 * Cleans up HTML, removes dangerous tags and attributes, and
2205 * removes HTML comments
2206 * @access private
2207 */
2208 function removeHTMLtags( $text ) {
2209 global $wgUseTidy, $wgUserHtml;
2210 $fname = 'Parser::removeHTMLtags';
2211 wfProfileIn( $fname );
2212
2213 if( $wgUserHtml ) {
2214 $htmlpairs = array( # Tags that must be closed
2215 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
2216 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
2217 'strike', 'strong', 'tt', 'var', 'div', 'center',
2218 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
2219 'ruby', 'rt' , 'rb' , 'rp', 'p', 'span'
2220 );
2221 $htmlsingle = array(
2222 'br', 'hr', 'li', 'dt', 'dd'
2223 );
2224 $htmlnest = array( # Tags that can be nested--??
2225 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
2226 'dl', 'font', 'big', 'small', 'sub', 'sup', 'span'
2227 );
2228 $tabletags = array( # Can only appear inside table
2229 'td', 'th', 'tr'
2230 );
2231 } else {
2232 $htmlpairs = array();
2233 $htmlsingle = array();
2234 $htmlnest = array();
2235 $tabletags = array();
2236 }
2237
2238 $htmlsingle = array_merge( $tabletags, $htmlsingle );
2239 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
2240
2241 $htmlattrs = $this->getHTMLattrs () ;
2242
2243 # Remove HTML comments
2244 $text = $this->removeHTMLcomments( $text );
2245
2246 $bits = explode( '<', $text );
2247 $text = array_shift( $bits );
2248 if(!$wgUseTidy) {
2249 $tagstack = array(); $tablestack = array();
2250 foreach ( $bits as $x ) {
2251 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
2252 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2253 $x, $regs );
2254 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2255 error_reporting( $prev );
2256
2257 $badtag = 0 ;
2258 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2259 # Check our stack
2260 if ( $slash ) {
2261 # Closing a tag...
2262 if ( ! in_array( $t, $htmlsingle ) &&
2263 ( $ot = @array_pop( $tagstack ) ) != $t ) {
2264 @array_push( $tagstack, $ot );
2265 $badtag = 1;
2266 } else {
2267 if ( $t == 'table' ) {
2268 $tagstack = array_pop( $tablestack );
2269 }
2270 $newparams = '';
2271 }
2272 } else {
2273 # Keep track for later
2274 if ( in_array( $t, $tabletags ) &&
2275 ! in_array( 'table', $tagstack ) ) {
2276 $badtag = 1;
2277 } else if ( in_array( $t, $tagstack ) &&
2278 ! in_array ( $t , $htmlnest ) ) {
2279 $badtag = 1 ;
2280 } else if ( ! in_array( $t, $htmlsingle ) ) {
2281 if ( $t == 'table' ) {
2282 array_push( $tablestack, $tagstack );
2283 $tagstack = array();
2284 }
2285 array_push( $tagstack, $t );
2286 }
2287 # Strip non-approved attributes from the tag
2288 $newparams = $this->fixTagAttributes($params);
2289
2290 }
2291 if ( ! $badtag ) {
2292 $rest = str_replace( '>', '&gt;', $rest );
2293 $text .= "<$slash$t $newparams$brace$rest";
2294 continue;
2295 }
2296 }
2297 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2298 }
2299 # Close off any remaining tags
2300 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2301 $text .= "</$t>\n";
2302 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2303 }
2304 } else {
2305 # this might be possible using tidy itself
2306 foreach ( $bits as $x ) {
2307 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2308 $x, $regs );
2309 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2310 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2311 $newparams = $this->fixTagAttributes($params);
2312 $rest = str_replace( '>', '&gt;', $rest );
2313 $text .= "<$slash$t $newparams$brace$rest";
2314 } else {
2315 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2316 }
2317 }
2318 }
2319 wfProfileOut( $fname );
2320 return $text;
2321 }
2322
2323 /**
2324 * Remove '<!--', '-->', and everything between.
2325 * To avoid leaving blank lines, when a comment is both preceded
2326 * and followed by a newline (ignoring spaces), trim leading and
2327 * trailing spaces and one of the newlines.
2328 *
2329 * @access private
2330 */
2331 function removeHTMLcomments( $text ) {
2332 $fname='Parser::removeHTMLcomments';
2333 wfProfileIn( $fname );
2334 while (($start = strpos($text, '<!--')) !== false) {
2335 $end = strpos($text, '-->', $start + 4);
2336 if ($end === false) {
2337 # Unterminated comment; bail out
2338 break;
2339 }
2340
2341 $end += 3;
2342
2343 # Trim space and newline if the comment is both
2344 # preceded and followed by a newline
2345 $spaceStart = max($start - 1, 0);
2346 $spaceLen = $end - $spaceStart;
2347 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2348 $spaceStart--;
2349 $spaceLen++;
2350 }
2351 while (substr($text, $spaceStart + $spaceLen, 1) === ' ')
2352 $spaceLen++;
2353 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") {
2354 # Remove the comment, leading and trailing
2355 # spaces, and leave only one newline.
2356 $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1);
2357 }
2358 else {
2359 # Remove just the comment.
2360 $text = substr_replace($text, '', $start, $end - $start);
2361 }
2362 }
2363 wfProfileOut( $fname );
2364 return $text;
2365 }
2366
2367 /**
2368 * This function accomplishes several tasks:
2369 * 1) Auto-number headings if that option is enabled
2370 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2371 * 3) Add a Table of contents on the top for users who have enabled the option
2372 * 4) Auto-anchor headings
2373 *
2374 * It loops through all headlines, collects the necessary data, then splits up the
2375 * string and re-inserts the newly formatted headlines.
2376 * @access private
2377 */
2378 /* private */ function formatHeadings( $text, $isMain=true ) {
2379 global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders;
2380
2381 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2382 $doShowToc = $this->mOptions->getShowToc();
2383 $forceTocHere = false;
2384 if( !$this->mTitle->userCanEdit() ) {
2385 $showEditLink = 0;
2386 $rightClickHack = 0;
2387 } else {
2388 $showEditLink = $this->mOptions->getEditSection();
2389 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2390 }
2391
2392 # Inhibit editsection links if requested in the page
2393 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2394 if( $esw->matchAndRemove( $text ) ) {
2395 $showEditLink = 0;
2396 }
2397 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2398 # do not add TOC
2399 $mw =& MagicWord::get( MAG_NOTOC );
2400 if( $mw->matchAndRemove( $text ) ) {
2401 $doShowToc = 0;
2402 }
2403
2404 # never add the TOC to the Main Page. This is an entry page that should not
2405 # be more than 1-2 screens large anyway
2406 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2407 $doShowToc = 0;
2408 }
2409
2410 # Get all headlines for numbering them and adding funky stuff like [edit]
2411 # links - this is for later, but we need the number of headlines right now
2412 $numMatches = preg_match_all( '/<H([1-6])(.*?'.'>)(.*?)<\/H[1-6] *>/i', $text, $matches );
2413
2414 # if there are fewer than 4 headlines in the article, do not show TOC
2415 if( $numMatches < 4 ) {
2416 $doShowToc = 0;
2417 }
2418
2419 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2420 # override above conditions and always show TOC at that place
2421
2422 $mw =& MagicWord::get( MAG_TOC );
2423 if($mw->match( $text ) ) {
2424 $doShowToc = 1;
2425 $forceTocHere = true;
2426 } else {
2427 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2428 # override above conditions and always show TOC above first header
2429 $mw =& MagicWord::get( MAG_FORCETOC );
2430 if ($mw->matchAndRemove( $text ) ) {
2431 $doShowToc = 1;
2432 }
2433 }
2434
2435 # Never ever show TOC if no headers
2436 if( $numMatches < 1 ) {
2437 $doShowToc = 0;
2438 }
2439
2440 # We need this to perform operations on the HTML
2441 $sk =& $this->mOptions->getSkin();
2442
2443 # headline counter
2444 $headlineCount = 0;
2445 $sectionCount = 0; # headlineCount excluding template sections
2446
2447 # Ugh .. the TOC should have neat indentation levels which can be
2448 # passed to the skin functions. These are determined here
2449 $toc = '';
2450 $full = '';
2451 $head = array();
2452 $sublevelCount = array();
2453 $levelCount = array();
2454 $toclevel = 0;
2455 $level = 0;
2456 $prevlevel = 0;
2457 $toclevel = 0;
2458 $prevtoclevel = 0;
2459
2460 foreach( $matches[3] as $headline ) {
2461 $istemplate = 0;
2462 $templatetitle = '';
2463 $templatesection = 0;
2464 $numbering = '';
2465
2466 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2467 $istemplate = 1;
2468 $templatetitle = base64_decode($mat[1]);
2469 $templatesection = 1 + (int)base64_decode($mat[2]);
2470 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2471 }
2472
2473 if( $toclevel ) {
2474 $prevlevel = $level;
2475 $prevtoclevel = $toclevel;
2476 }
2477 $level = $matches[1][$headlineCount];
2478
2479 if( $doNumberHeadings || $doShowToc ) {
2480
2481 if ( $level > $prevlevel ) {
2482 # Increase TOC level
2483 $toclevel++;
2484 $sublevelCount[$toclevel] = 0;
2485 $toc .= $sk->tocIndent();
2486 }
2487 elseif ( $level < $prevlevel && $toclevel > 1 ) {
2488 # Decrease TOC level, find level to jump to
2489
2490 if ( $toclevel == 2 && $level <= $levelCount[1] ) {
2491 # Can only go down to level 1
2492 $toclevel = 1;
2493 } else {
2494 for ($i = $toclevel; $i > 0; $i--) {
2495 if ( $levelCount[$i] == $level ) {
2496 # Found last matching level
2497 $toclevel = $i;
2498 break;
2499 }
2500 elseif ( $levelCount[$i] < $level ) {
2501 # Found first matching level below current level
2502 $toclevel = $i + 1;
2503 break;
2504 }
2505 }
2506 }
2507
2508 $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
2509 }
2510 else {
2511 # No change in level, end TOC line
2512 $toc .= $sk->tocLineEnd();
2513 }
2514
2515 $levelCount[$toclevel] = $level;
2516
2517 # count number of headlines for each level
2518 @$sublevelCount[$toclevel]++;
2519 $dot = 0;
2520 for( $i = 1; $i <= $toclevel; $i++ ) {
2521 if( !empty( $sublevelCount[$i] ) ) {
2522 if( $dot ) {
2523 $numbering .= '.';
2524 }
2525 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
2526 $dot = 1;
2527 }
2528 }
2529 }
2530
2531 # The canonized header is a version of the header text safe to use for links
2532 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2533 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2534 $canonized_headline = $this->unstripNoWiki( $canonized_headline, $this->mStripState );
2535
2536 # Remove link placeholders by the link text.
2537 # <!--LINK number-->
2538 # turns into
2539 # link text with suffix
2540 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2541 "\$wgLinkHolders['texts'][\$1]",
2542 $canonized_headline );
2543
2544 # strip out HTML
2545 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2546 $tocline = trim( $canonized_headline );
2547 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2548 $replacearray = array(
2549 '%3A' => ':',
2550 '%' => '.'
2551 );
2552 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2553 $refer[$headlineCount] = $canonized_headline;
2554
2555 # count how many in assoc. array so we can track dupes in anchors
2556 @$refers[$canonized_headline]++;
2557 $refcount[$headlineCount]=$refers[$canonized_headline];
2558
2559 # Don't number the heading if it is the only one (looks silly)
2560 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2561 # the two are different if the line contains a link
2562 $headline=$numbering . ' ' . $headline;
2563 }
2564
2565 # Create the anchor for linking from the TOC to the section
2566 $anchor = $canonized_headline;
2567 if($refcount[$headlineCount] > 1 ) {
2568 $anchor .= '_' . $refcount[$headlineCount];
2569 }
2570 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2571 $toc .= $sk->tocLine($anchor, $tocline, $numbering, $toclevel);
2572 }
2573 if( $showEditLink && ( !$istemplate || $templatetitle !== "" ) ) {
2574 if ( empty( $head[$headlineCount] ) ) {
2575 $head[$headlineCount] = '';
2576 }
2577 if( $istemplate )
2578 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2579 else
2580 $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1);
2581 }
2582
2583 # Add the edit section span
2584 if( $rightClickHack ) {
2585 if( $istemplate )
2586 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2587 else
2588 $headline = $sk->editSectionScript($this->mTitle, $sectionCount+1,$headline);
2589 }
2590
2591 # give headline the correct <h#> tag
2592 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2593
2594 $headlineCount++;
2595 if( !$istemplate )
2596 $sectionCount++;
2597 }
2598
2599 if( $doShowToc ) {
2600 $toclines = $headlineCount;
2601 $toc .= $sk->tocUnindent( $toclevel - 1 );
2602 $toc = $sk->tocList( $toc );
2603 }
2604
2605 # split up and insert constructed headlines
2606
2607 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2608 $i = 0;
2609
2610 foreach( $blocks as $block ) {
2611 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2612 # This is the [edit] link that appears for the top block of text when
2613 # section editing is enabled
2614
2615 # Disabled because it broke block formatting
2616 # For example, a bullet point in the top line
2617 # $full .= $sk->editSectionLink(0);
2618 }
2619 $full .= $block;
2620 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2621 # Top anchor now in skin
2622 $full = $full.$toc;
2623 }
2624
2625 if( !empty( $head[$i] ) ) {
2626 $full .= $head[$i];
2627 }
2628 $i++;
2629 }
2630 if($forceTocHere) {
2631 $mw =& MagicWord::get( MAG_TOC );
2632 return $mw->replace( $toc, $full );
2633 } else {
2634 return $full;
2635 }
2636 }
2637
2638 /**
2639 * Return an HTML link for the "ISBN 123456" text
2640 * @access private
2641 */
2642 function magicISBN( $text ) {
2643 global $wgLang;
2644 $fname = 'Parser::magicISBN';
2645 wfProfileIn( $fname );
2646
2647 $a = split( 'ISBN ', ' '.$text );
2648 if ( count ( $a ) < 2 ) {
2649 wfProfileOut( $fname );
2650 return $text;
2651 }
2652 $text = substr( array_shift( $a ), 1);
2653 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2654
2655 foreach ( $a as $x ) {
2656 $isbn = $blank = '' ;
2657 while ( ' ' == $x{0} ) {
2658 $blank .= ' ';
2659 $x = substr( $x, 1 );
2660 }
2661 if ( $x == '' ) { # blank isbn
2662 $text .= "ISBN $blank";
2663 continue;
2664 }
2665 while ( strstr( $valid, $x{0} ) != false ) {
2666 $isbn .= $x{0};
2667 $x = substr( $x, 1 );
2668 }
2669 $num = str_replace( '-', '', $isbn );
2670 $num = str_replace( ' ', '', $num );
2671
2672 if ( '' == $num ) {
2673 $text .= "ISBN $blank$x";
2674 } else {
2675 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2676 $text .= '<a href="' .
2677 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2678 "\" class=\"internal\">ISBN $isbn</a>";
2679 $text .= $x;
2680 }
2681 }
2682 wfProfileOut( $fname );
2683 return $text;
2684 }
2685
2686 /**
2687 * Return an HTML link for the "GEO ..." text
2688 * @access private
2689 */
2690 function magicGEO( $text ) {
2691 global $wgLang, $wgUseGeoMode;
2692 $fname = 'Parser::magicGEO';
2693 wfProfileIn( $fname );
2694
2695 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2696 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2697 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2698 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2699 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2700 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2701
2702 $a = split( 'GEO ', ' '.$text );
2703 if ( count ( $a ) < 2 ) {
2704 wfProfileOut( $fname );
2705 return $text;
2706 }
2707 $text = substr( array_shift( $a ), 1);
2708 $valid = '0123456789.+-:';
2709
2710 foreach ( $a as $x ) {
2711 $geo = $blank = '' ;
2712 while ( ' ' == $x{0} ) {
2713 $blank .= ' ';
2714 $x = substr( $x, 1 );
2715 }
2716 while ( strstr( $valid, $x{0} ) != false ) {
2717 $geo .= $x{0};
2718 $x = substr( $x, 1 );
2719 }
2720 $num = str_replace( '+', '', $geo );
2721 $num = str_replace( ' ', '', $num );
2722
2723 if ( '' == $num || count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2724 $text .= "GEO $blank$x";
2725 } else {
2726 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2727 $text .= '<a href="' .
2728 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2729 "\" class=\"internal\">GEO $geo</a>";
2730 $text .= $x;
2731 }
2732 }
2733 wfProfileOut( $fname );
2734 return $text;
2735 }
2736
2737 /**
2738 * Return an HTML link for the "RFC 1234" text
2739 * @access private
2740 * @param string $text text to be processed
2741 */
2742 function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl' ) {
2743 global $wgLang;
2744
2745 $valid = '0123456789';
2746 $internal = false;
2747
2748 $a = split( $keyword, ' '.$text );
2749 if ( count ( $a ) < 2 ) {
2750 return $text;
2751 }
2752 $text = substr( array_shift( $a ), 1);
2753
2754 /* Check if keyword is preceed by [[.
2755 * This test is made here cause of the array_shift above
2756 * that prevent the test to be done in the foreach.
2757 */
2758 if ( substr( $text, -2 ) == '[[' ) {
2759 $internal = true;
2760 }
2761
2762 foreach ( $a as $x ) {
2763 /* token might be empty if we have RFC RFC 1234 */
2764 if ( $x=='' ) {
2765 $text.=$keyword;
2766 continue;
2767 }
2768
2769 $id = $blank = '' ;
2770
2771 /** remove and save whitespaces in $blank */
2772 while ( $x{0} == ' ' ) {
2773 $blank .= ' ';
2774 $x = substr( $x, 1 );
2775 }
2776
2777 /** remove and save the rfc number in $id */
2778 while ( strstr( $valid, $x{0} ) != false ) {
2779 $id .= $x{0};
2780 $x = substr( $x, 1 );
2781 }
2782
2783 if ( $id == '' ) {
2784 /* call back stripped spaces*/
2785 $text .= $keyword.$blank.$x;
2786 } elseif( $internal ) {
2787 /* normal link */
2788 $text .= $keyword.$id.$x;
2789 } else {
2790 /* build the external link*/
2791 $url = wfmsg( $urlmsg );
2792 $url = str_replace( '$1', $id, $url);
2793 $sk =& $this->mOptions->getSkin();
2794 $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
2795 $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
2796 }
2797
2798 /* Check if the next RFC keyword is preceed by [[ */
2799 $internal = ( substr($x,-2) == '[[' );
2800 }
2801 return $text;
2802 }
2803
2804 /**
2805 * Transform wiki markup when saving a page by doing \r\n -> \n
2806 * conversion, substitting signatures, {{subst:}} templates, etc.
2807 *
2808 * @param string $text the text to transform
2809 * @param Title &$title the Title object for the current article
2810 * @param User &$user the User object describing the current user
2811 * @param ParserOptions $options parsing options
2812 * @param bool $clearState whether to clear the parser state first
2813 * @return string the altered wiki markup
2814 * @access public
2815 */
2816 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2817 $this->mOptions = $options;
2818 $this->mTitle =& $title;
2819 $this->mOutputType = OT_WIKI;
2820
2821 if ( $clearState ) {
2822 $this->clearState();
2823 }
2824
2825 $stripState = false;
2826 $pairs = array(
2827 "\r\n" => "\n",
2828 );
2829 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
2830 $text = $this->strip( $text, $stripState, false );
2831 $text = $this->pstPass2( $text, $user );
2832 $text = $this->unstrip( $text, $stripState );
2833 $text = $this->unstripNoWiki( $text, $stripState );
2834 return $text;
2835 }
2836
2837 /**
2838 * Pre-save transform helper function
2839 * @access private
2840 */
2841 function pstPass2( $text, &$user ) {
2842 global $wgLang, $wgContLang, $wgLocaltimezone;
2843
2844 # Variable replacement
2845 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2846 $text = $this->replaceVariables( $text );
2847
2848 # Signatures
2849 #
2850 $n = $user->getName();
2851 $k = $user->getOption( 'nickname' );
2852 if ( '' == $k ) { $k = $n; }
2853 if ( isset( $wgLocaltimezone ) ) {
2854 $oldtz = getenv( 'TZ' );
2855 putenv( 'TZ='.$wgLocaltimezone );
2856 }
2857 /* Note: this is an ugly timezone hack for the European wikis */
2858 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
2859 ' (' . date( 'T' ) . ')';
2860 if ( isset( $wgLocaltimezone ) ) {
2861 putenv( 'TZ='.$oldtzs );
2862 }
2863
2864 if( $user->getOption( 'fancysig' ) ) {
2865 $sigText = $k;
2866 } else {
2867 $sigText = '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]]";
2868 }
2869 $text = preg_replace( '/~~~~~/', $d, $text );
2870 $text = preg_replace( '/~~~~/', "$sigText $d", $text );
2871 $text = preg_replace( '/~~~/', $sigText, $text );
2872
2873 # Context links: [[|name]] and [[name (context)|]]
2874 #
2875 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2876 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2877 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2878 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2879
2880 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2881 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2882 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2883 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2884 $context = '';
2885 $t = $this->mTitle->getText();
2886 if ( preg_match( $conpat, $t, $m ) ) {
2887 $context = $m[2];
2888 }
2889 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2890 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2891 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2892
2893 if ( '' == $context ) {
2894 $text = preg_replace( $p2, '[[\\1]]', $text );
2895 } else {
2896 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2897 }
2898
2899 # Trim trailing whitespace
2900 # MAG_END (__END__) tag allows for trailing
2901 # whitespace to be deliberately included
2902 $text = rtrim( $text );
2903 $mw =& MagicWord::get( MAG_END );
2904 $mw->matchAndRemove( $text );
2905
2906 return $text;
2907 }
2908
2909 /**
2910 * Set up some variables which are usually set up in parse()
2911 * so that an external function can call some class members with confidence
2912 * @access public
2913 */
2914 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2915 $this->mTitle =& $title;
2916 $this->mOptions = $options;
2917 $this->mOutputType = $outputType;
2918 if ( $clearState ) {
2919 $this->clearState();
2920 }
2921 }
2922
2923 /**
2924 * Transform a MediaWiki message by replacing magic variables.
2925 *
2926 * @param string $text the text to transform
2927 * @param ParserOptions $options options
2928 * @return string the text with variables substituted
2929 * @access public
2930 */
2931 function transformMsg( $text, $options ) {
2932 global $wgTitle;
2933 static $executing = false;
2934
2935 # Guard against infinite recursion
2936 if ( $executing ) {
2937 return $text;
2938 }
2939 $executing = true;
2940
2941 $this->mTitle = $wgTitle;
2942 $this->mOptions = $options;
2943 $this->mOutputType = OT_MSG;
2944 $this->clearState();
2945 $text = $this->replaceVariables( $text );
2946
2947 $executing = false;
2948 return $text;
2949 }
2950
2951 /**
2952 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2953 * Callback will be called with the text within
2954 * Transform and return the text within
2955 * @access public
2956 */
2957 function setHook( $tag, $callback ) {
2958 $oldVal = @$this->mTagHooks[$tag];
2959 $this->mTagHooks[$tag] = $callback;
2960 return $oldVal;
2961 }
2962
2963 /**
2964 * Replace <!--LINK--> link placeholders with actual links, in the buffer
2965 * Placeholders created in Skin::makeLinkObj()
2966 * Returns an array of links found, indexed by PDBK:
2967 * 0 - broken
2968 * 1 - normal link
2969 * 2 - stub
2970 * $options is a bit field, RLH_FOR_UPDATE to select for update
2971 */
2972 function replaceLinkHolders( &$text, $options = 0 ) {
2973 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck, $wgLinkHolders;
2974 global $wgInterwikiLinkHolders;
2975 global $outputReplace;
2976
2977 if ( $wgUseOldExistenceCheck ) {
2978 return array();
2979 }
2980
2981 $fname = 'Parser::replaceLinkHolders';
2982 wfProfileIn( $fname );
2983
2984 $pdbks = array();
2985 $colours = array();
2986
2987 #if ( !empty( $tmpLinks[0] ) ) { #TODO
2988 if ( !empty( $wgLinkHolders['namespaces'] ) ) {
2989 wfProfileIn( $fname.'-check' );
2990 $dbr =& wfGetDB( DB_SLAVE );
2991 $page = $dbr->tableName( 'page' );
2992 $sk = $wgUser->getSkin();
2993 $threshold = $wgUser->getOption('stubthreshold');
2994
2995 # Sort by namespace
2996 asort( $wgLinkHolders['namespaces'] );
2997
2998 # Generate query
2999 $query = false;
3000 foreach ( $wgLinkHolders['namespaces'] as $key => $val ) {
3001 # Make title object
3002 $title = $wgLinkHolders['titles'][$key];
3003
3004 # Skip invalid entries.
3005 # Result will be ugly, but prevents crash.
3006 if ( is_null( $title ) ) {
3007 continue;
3008 }
3009 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
3010
3011 # Check if it's in the link cache already
3012 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
3013 $colours[$pdbk] = 1;
3014 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
3015 $colours[$pdbk] = 0;
3016 } else {
3017 # Not in the link cache, add it to the query
3018 if ( !isset( $current ) ) {
3019 $current = $val;
3020 $tables = $page;
3021 $join = '';
3022 $query = "SELECT page_id, page_namespace, page_title";
3023 if ( $threshold > 0 ) {
3024 $textTable = $dbr->tableName( 'text' );
3025 $query .= ', LENGTH(old_text) AS page_len, page_is_redirect';
3026 $tables .= ", $textTable";
3027 $join = 'page_latest=old_id AND';
3028 }
3029 $query .= " FROM $tables WHERE $join (page_namespace=$val AND page_title IN(";
3030 } elseif ( $current != $val ) {
3031 $current = $val;
3032 $query .= ")) OR (page_namespace=$val AND page_title IN(";
3033 } else {
3034 $query .= ', ';
3035 }
3036
3037 $query .= $dbr->addQuotes( $wgLinkHolders['dbkeys'][$key] );
3038 }
3039 }
3040 if ( $query ) {
3041 $query .= '))';
3042 if ( $options & RLH_FOR_UPDATE ) {
3043 $query .= ' FOR UPDATE';
3044 }
3045
3046 $res = $dbr->query( $query, $fname );
3047
3048 # Fetch data and form into an associative array
3049 # non-existent = broken
3050 # 1 = known
3051 # 2 = stub
3052 while ( $s = $dbr->fetchObject($res) ) {
3053 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
3054 $pdbk = $title->getPrefixedDBkey();
3055 $wgLinkCache->addGoodLink( $s->page_id, $pdbk );
3056
3057 if ( $threshold > 0 ) {
3058 $size = $s->page_len;
3059 if ( $s->page_is_redirect || $s->page_namespace != 0 || $length < $threshold ) {
3060 $colours[$pdbk] = 1;
3061 } else {
3062 $colours[$pdbk] = 2;
3063 }
3064 } else {
3065 $colours[$pdbk] = 1;
3066 }
3067 }
3068 }
3069 wfProfileOut( $fname.'-check' );
3070
3071 # Construct search and replace arrays
3072 wfProfileIn( $fname.'-construct' );
3073 $outputReplace = array();
3074 foreach ( $wgLinkHolders['namespaces'] as $key => $ns ) {
3075 $pdbk = $pdbks[$key];
3076 $searchkey = '<!--LINK '.$key.'-->';
3077 $title = $wgLinkHolders['titles'][$key];
3078 if ( empty( $colours[$pdbk] ) ) {
3079 $wgLinkCache->addBadLink( $pdbk );
3080 $colours[$pdbk] = 0;
3081 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
3082 $wgLinkHolders['texts'][$key],
3083 $wgLinkHolders['queries'][$key] );
3084 } elseif ( $colours[$pdbk] == 1 ) {
3085 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
3086 $wgLinkHolders['texts'][$key],
3087 $wgLinkHolders['queries'][$key] );
3088 } elseif ( $colours[$pdbk] == 2 ) {
3089 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
3090 $wgLinkHolders['texts'][$key],
3091 $wgLinkHolders['queries'][$key] );
3092 }
3093 }
3094 wfProfileOut( $fname.'-construct' );
3095
3096 # Do the thing
3097 wfProfileIn( $fname.'-replace' );
3098
3099 $text = preg_replace_callback(
3100 '/(<!--LINK .*?-->)/',
3101 "outputReplaceMatches",
3102 $text);
3103 wfProfileOut( $fname.'-replace' );
3104 }
3105
3106 if ( !empty( $wgInterwikiLinkHolders ) ) {
3107 wfProfileIn( $fname.'-interwiki' );
3108 $outputReplace = $wgInterwikiLinkHolders;
3109 $text = preg_replace_callback(
3110 '/<!--IWLINK (.*?)-->/',
3111 "outputReplaceMatches",
3112 $text );
3113 wfProfileOut( $fname.'-interwiki' );
3114 }
3115
3116 wfProfileOut( $fname );
3117 return $colours;
3118 }
3119
3120 /**
3121 * Renders an image gallery from a text with one line per image.
3122 * text labels may be given by using |-style alternative text. E.g.
3123 * Image:one.jpg|The number "1"
3124 * Image:tree.jpg|A tree
3125 * given as text will return the HTML of a gallery with two images,
3126 * labeled 'The number "1"' and
3127 * 'A tree'.
3128 */
3129 function renderImageGallery( $text ) {
3130 global $wgLinkCache;
3131 $ig = new ImageGallery();
3132 $ig->setShowBytes( false );
3133 $ig->setShowFilename( false );
3134 $lines = explode( "\n", $text );
3135
3136 foreach ( $lines as $line ) {
3137 # match lines like these:
3138 # Image:someimage.jpg|This is some image
3139 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
3140 # Skip empty lines
3141 if ( count( $matches ) == 0 ) {
3142 continue;
3143 }
3144 $nt = Title::newFromURL( $matches[1] );
3145 if( is_null( $nt ) ) {
3146 # Bogus title. Ignore these so we don't bomb out later.
3147 continue;
3148 }
3149 if ( isset( $matches[3] ) ) {
3150 $label = $matches[3];
3151 } else {
3152 $label = '';
3153 }
3154
3155 # FIXME: Use the full wiki parser and add its links
3156 # to the page's links.
3157 $html = $this->mOptions->mSkin->formatComment( $label );
3158
3159 $ig->add( Image::newFromTitle( $nt ), $html );
3160 $wgLinkCache->addImageLinkObj( $nt );
3161 }
3162 return $ig->toHTML();
3163 }
3164 }
3165
3166 /**
3167 * @todo document
3168 * @package MediaWiki
3169 */
3170 class ParserOutput
3171 {
3172 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
3173 var $mCacheTime; # Used in ParserCache
3174 var $mVersion; # Compatibility check
3175
3176 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
3177 $containsOldMagic = false )
3178 {
3179 $this->mText = $text;
3180 $this->mLanguageLinks = $languageLinks;
3181 $this->mCategoryLinks = $categoryLinks;
3182 $this->mContainsOldMagic = $containsOldMagic;
3183 $this->mCacheTime = '';
3184 $this->mVersion = MW_PARSER_VERSION;
3185 }
3186
3187 function getText() { return $this->mText; }
3188 function getLanguageLinks() { return $this->mLanguageLinks; }
3189 function getCategoryLinks() { return array_keys( $this->mCategoryLinks ); }
3190 function getCacheTime() { return $this->mCacheTime; }
3191 function containsOldMagic() { return $this->mContainsOldMagic; }
3192 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
3193 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
3194 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
3195 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
3196 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
3197 function addCategoryLink( $c ) { $this->mCategoryLinks[$c] = 1; }
3198
3199 function merge( $other ) {
3200 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
3201 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
3202 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
3203 }
3204
3205 /**
3206 * Return true if this cached output object predates the global or
3207 * per-article cache invalidation timestamps, or if it comes from
3208 * an incompatible older version.
3209 *
3210 * @param string $touched the affected article's last touched timestamp
3211 * @return bool
3212 * @access public
3213 */
3214 function expired( $touched ) {
3215 global $wgCacheEpoch;
3216 return $this->getCacheTime() <= $touched ||
3217 $this->getCacheTime() <= $wgCacheEpoch ||
3218 !isset( $this->mVersion ) ||
3219 version_compare( $this->mVersion, MW_PARSER_VERSION, "lt" );
3220 }
3221 }
3222
3223 /**
3224 * Set options of the Parser
3225 * @todo document
3226 * @package MediaWiki
3227 */
3228 class ParserOptions
3229 {
3230 # All variables are private
3231 var $mUseTeX; # Use texvc to expand <math> tags
3232 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
3233 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
3234 var $mAllowExternalImages; # Allow external images inline
3235 var $mSkin; # Reference to the preferred skin
3236 var $mDateFormat; # Date format index
3237 var $mEditSection; # Create "edit section" links
3238 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
3239 var $mNumberHeadings; # Automatically number headings
3240 var $mShowToc; # Show table of contents
3241
3242 function getUseTeX() { return $this->mUseTeX; }
3243 function getUseDynamicDates() { return $this->mUseDynamicDates; }
3244 function getInterwikiMagic() { return $this->mInterwikiMagic; }
3245 function getAllowExternalImages() { return $this->mAllowExternalImages; }
3246 function getSkin() { return $this->mSkin; }
3247 function getDateFormat() { return $this->mDateFormat; }
3248 function getEditSection() { return $this->mEditSection; }
3249 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
3250 function getNumberHeadings() { return $this->mNumberHeadings; }
3251 function getShowToc() { return $this->mShowToc; }
3252
3253 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
3254 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
3255 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
3256 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
3257 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
3258 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
3259 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
3260 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
3261 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
3262
3263 function setSkin( &$x ) { $this->mSkin =& $x; }
3264
3265 # Get parser options
3266 /* static */ function newFromUser( &$user ) {
3267 $popts = new ParserOptions;
3268 $popts->initialiseFromUser( $user );
3269 return $popts;
3270 }
3271
3272 # Get user options
3273 function initialiseFromUser( &$userInput ) {
3274 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
3275 $fname = 'ParserOptions::initialiseFromUser';
3276 wfProfileIn( $fname );
3277 if ( !$userInput ) {
3278 $user = new User;
3279 $user->setLoaded( true );
3280 } else {
3281 $user =& $userInput;
3282 }
3283
3284 $this->mUseTeX = $wgUseTeX;
3285 $this->mUseDynamicDates = $wgUseDynamicDates;
3286 $this->mInterwikiMagic = $wgInterwikiMagic;
3287 $this->mAllowExternalImages = $wgAllowExternalImages;
3288 wfProfileIn( $fname.'-skin' );
3289 $this->mSkin =& $user->getSkin();
3290 wfProfileOut( $fname.'-skin' );
3291 $this->mDateFormat = $user->getOption( 'date' );
3292 $this->mEditSection = $user->getOption( 'editsection' );
3293 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
3294 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
3295 $this->mShowToc = $user->getOption( 'showtoc' );
3296 wfProfileOut( $fname );
3297 }
3298
3299
3300 }
3301
3302 /**
3303 * Callback function used by Parser::replaceLinkHolders()
3304 * to substitute link placeholders.
3305 */
3306 function &outputReplaceMatches( $matches ) {
3307 global $outputReplace;
3308 return $outputReplace[$matches[1]];
3309 }
3310
3311 /**
3312 * Return the total number of articles
3313 */
3314 function wfNumberOfArticles() {
3315 global $wgNumberOfArticles;
3316
3317 wfLoadSiteStats();
3318 return $wgNumberOfArticles;
3319 }
3320
3321 /**
3322 * Get various statistics from the database
3323 * @private
3324 */
3325 function wfLoadSiteStats() {
3326 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
3327 $fname = 'wfLoadSiteStats';
3328
3329 if ( -1 != $wgNumberOfArticles ) return;
3330 $dbr =& wfGetDB( DB_SLAVE );
3331 $s = $dbr->selectRow( 'site_stats',
3332 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
3333 array( 'ss_row_id' => 1 ), $fname
3334 );
3335
3336 if ( $s === false ) {
3337 return;
3338 } else {
3339 $wgTotalViews = $s->ss_total_views;
3340 $wgTotalEdits = $s->ss_total_edits;
3341 $wgNumberOfArticles = $s->ss_good_articles;
3342 }
3343 }
3344
3345 function wfEscapeHTMLTagsOnly( $in ) {
3346 return str_replace(
3347 array( '"', '>', '<' ),
3348 array( '&quot;', '&gt;', '&lt;' ),
3349 $in );
3350 }
3351
3352 ?>