BUG#1317 - External links in image caption not working
[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 # No link text, e.g. [http://domain.tld/some.link]
996 if ( $text == '' ) {
997 # Autonumber if allowed
998 if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
999 $text = '[' . ++$this->mAutonumber . ']';
1000 } else {
1001 # Otherwise just use the URL
1002 $text = htmlspecialchars( $url );
1003 }
1004 } else {
1005 # Have link text, e.g. [http://domain.tld/some.link text]s
1006 # Check for trail
1007 if ( preg_match( $linktrail, $trail, $m2 ) ) {
1008 $dtrail = $m2[1];
1009 $trail = $m2[2];
1010 }
1011 }
1012
1013 $encUrl = htmlspecialchars( $url );
1014 # Bit in parentheses showing the URL for the printable version
1015 if( $url == $text || preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
1016 $paren = '';
1017 } else {
1018 # Expand the URL for printable version
1019 if ( ! $sk->suppressUrlExpansion() ) {
1020 $paren = "<span class='urlexpansion'>&nbsp;(<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
1021 } else {
1022 $paren = '';
1023 }
1024 }
1025
1026 # Process the trail (i.e. everything after this link up until start of the next link),
1027 # replacing any non-bracketed links
1028 $trail = $this->replaceFreeExternalLinks( $trail );
1029
1030 # Use the encoded URL
1031 # This means that users can paste URLs directly into the text
1032 # Funny characters like &ouml; aren't valid in URLs anyway
1033 # This was changed in August 2004
1034 $s .= $sk->makeExternalLink( $url, $text, false ) . $dtrail. $paren . $trail;
1035 }
1036
1037 wfProfileOut( $fname );
1038 return $s;
1039 }
1040
1041 /**
1042 * Replace anything that looks like a URL with a link
1043 * @access private
1044 */
1045 function replaceFreeExternalLinks( $text ) {
1046 $fname = 'Parser::replaceFreeExternalLinks';
1047 wfProfileIn( $fname );
1048
1049 $bits = preg_split( '/(\b(?:'.URL_PROTOCOLS.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1050 $s = array_shift( $bits );
1051 $i = 0;
1052
1053 $sk =& $this->mOptions->getSkin();
1054
1055 while ( $i < count( $bits ) ){
1056 $protocol = $bits[$i++];
1057 $remainder = $bits[$i++];
1058
1059 if ( preg_match( '/^('.EXT_LINK_URL_CLASS.'+)(.*)$/s', $remainder, $m ) ) {
1060 # Found some characters after the protocol that look promising
1061 $url = $protocol . $m[1];
1062 $trail = $m[2];
1063
1064 # The characters '<' and '>' (which were escaped by
1065 # removeHTMLtags()) should not be included in
1066 # URLs, per RFC 2396.
1067 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
1068 $trail = substr($url, $m2[0][1]) . $trail;
1069 $url = substr($url, 0, $m2[0][1]);
1070 }
1071
1072 # Move trailing punctuation to $trail
1073 $sep = ',;\.:!?';
1074 # If there is no left bracket, then consider right brackets fair game too
1075 if ( strpos( $url, '(' ) === false ) {
1076 $sep .= ')';
1077 }
1078
1079 $numSepChars = strspn( strrev( $url ), $sep );
1080 if ( $numSepChars ) {
1081 $trail = substr( $url, -$numSepChars ) . $trail;
1082 $url = substr( $url, 0, -$numSepChars );
1083 }
1084
1085 # Replace &amp; from obsolete syntax with &.
1086 # All HTML entities will be escaped by makeExternalLink()
1087 # or maybeMakeImageLink()
1088 $url = str_replace( '&amp;', '&', $url );
1089
1090 # Is this an external image?
1091 $text = $this->maybeMakeImageLink( $url );
1092 if ( $text === false ) {
1093 # Not an image, make a link
1094 $text = $sk->makeExternalLink( $url, $url );
1095 }
1096 $s .= $text . $trail;
1097 } else {
1098 $s .= $protocol . $remainder;
1099 }
1100 }
1101 wfProfileOut();
1102 return $s;
1103 }
1104
1105 /**
1106 * make an image if it's allowed
1107 * @access private
1108 */
1109 function maybeMakeImageLink( $url ) {
1110 $sk =& $this->mOptions->getSkin();
1111 $text = false;
1112 if ( $this->mOptions->getAllowExternalImages() ) {
1113 if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
1114 # Image found
1115 $text = $sk->makeImage( htmlspecialchars( $url ) );
1116 }
1117 }
1118 return $text;
1119 }
1120
1121 /**
1122 * Process [[ ]] wikilinks
1123 *
1124 * @access private
1125 */
1126
1127 function replaceInternalLinks( $s ) {
1128 global $wgLang, $wgContLang, $wgLinkCache;
1129 global $wgDisableLangConversion;
1130 static $fname = 'Parser::replaceInternalLinks' ;
1131
1132 wfProfileIn( $fname );
1133
1134 wfProfileIn( $fname.'-setup' );
1135 static $tc = FALSE;
1136 # the % is needed to support urlencoded titles as well
1137 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1138
1139 $sk =& $this->mOptions->getSkin();
1140 global $wgUseOldExistenceCheck;
1141 # "Post-parse link colour check" works only on wiki text since it's now
1142 # in Parser. Enable it, then disable it when we're done.
1143 $saveParseColour = $sk->postParseLinkColour( !$wgUseOldExistenceCheck );
1144
1145 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1146
1147 #split the entire text string on occurences of [[
1148 $a = explode( '[[', ' ' . $s );
1149 #get the first element (all text up to first [[), and remove the space we added
1150 $s = array_shift( $a );
1151 $s = substr( $s, 1 );
1152
1153 # Match a link having the form [[namespace:link|alternate]]trail
1154 static $e1 = FALSE;
1155 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD"; }
1156 # Match cases where there is no "]]", which might still be images
1157 static $e1_img = FALSE;
1158 if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
1159 # Match the end of a line for a word that's not followed by whitespace,
1160 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1161 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1162
1163 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
1164
1165 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1166
1167 if ( $useLinkPrefixExtension ) {
1168 if ( preg_match( $e2, $s, $m ) ) {
1169 $first_prefix = $m[2];
1170 $s = $m[1];
1171 } else {
1172 $first_prefix = false;
1173 }
1174 } else {
1175 $prefix = '';
1176 }
1177
1178 $selflink = $this->mTitle->getPrefixedText();
1179 wfProfileOut( $fname.'-setup' );
1180
1181 $checkVariantLink = sizeof($wgContLang->getVariants())>1;
1182 $useSubpages = $this->areSubpagesAllowed();
1183
1184 # Loop for each link
1185 for ($k = 0; isset( $a[$k] ); $k++) {
1186 $line = $a[$k];
1187 if ( $useLinkPrefixExtension ) {
1188 wfProfileIn( $fname.'-prefixhandling' );
1189 if ( preg_match( $e2, $s, $m ) ) {
1190 $prefix = $m[2];
1191 $s = $m[1];
1192 } else {
1193 $prefix='';
1194 }
1195 # first link
1196 if($first_prefix) {
1197 $prefix = $first_prefix;
1198 $first_prefix = false;
1199 }
1200 wfProfileOut( $fname.'-prefixhandling' );
1201 }
1202
1203 $might_be_img = false;
1204
1205 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1206 $text = $m[2];
1207 # fix up urlencoded title texts
1208 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1209 $trail = $m[3];
1210 } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
1211 $might_be_img = true;
1212 $text = $m[2];
1213 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1214 $trail = "";
1215 } else { # Invalid form; output directly
1216 $s .= $prefix . '[[' . $line ;
1217 continue;
1218 }
1219
1220 # Don't allow internal links to pages containing
1221 # PROTO: where PROTO is a valid URL protocol; these
1222 # should be external links.
1223 if (preg_match('/^(\b(?:'.URL_PROTOCOLS.'):)/', $m[1])) {
1224 $s .= $prefix . '[[' . $line ;
1225 continue;
1226 }
1227
1228 # Make subpage if necessary
1229 if( $useSubpages ) {
1230 $link = $this->maybeDoSubpageLink( $m[1], $text );
1231 } else {
1232 $link = $m[1];
1233 }
1234
1235 $noforce = (substr($m[1], 0, 1) != ':');
1236 if (!$noforce) {
1237 # Strip off leading ':'
1238 $link = substr($link, 1);
1239 }
1240
1241 $nt =& Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
1242 if( !$nt ) {
1243 $s .= $prefix . '[[' . $line;
1244 continue;
1245 }
1246
1247 #check other language variants of the link
1248 #if the article does not exist
1249 if( $checkVariantLink
1250 && $nt->getArticleID() == 0 ) {
1251 $wgContLang->findVariantLink($link, $nt);
1252 }
1253
1254 $ns = $nt->getNamespace();
1255 $iw = $nt->getInterWiki();
1256
1257 if ($might_be_img) { # if this is actually an invalid link
1258 if ($ns == NS_IMAGE && $noforce) { #but might be an image
1259 $found = false;
1260 while (isset ($a[$k+1]) ) {
1261 #look at the next 'line' to see if we can close it there
1262 $next_line = array_shift(array_splice( $a, $k + 1, 1) );
1263 if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
1264 # the first ]] closes the inner link, the second the image
1265 $found = true;
1266 $text .= '[[' . $m[1];
1267 $trail = $m[2];
1268 break;
1269 } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
1270 #if there's exactly one ]] that's fine, we'll keep looking
1271 $text .= '[[' . $m[0];
1272 } else {
1273 #if $next_line is invalid too, we need look no further
1274 $text .= '[[' . $next_line;
1275 break;
1276 }
1277 }
1278 if ( !$found ) {
1279 # we couldn't find the end of this imageLink, so output it raw
1280 #but don't ignore what might be perfectly normal links in the text we've examined
1281 $text = $this->replaceInternalLinks($text);
1282 $s .= $prefix . '[[' . $link . '|' . $text;
1283 # note: no $trail, because without an end, there *is* no trail
1284 continue;
1285 }
1286 } else { #it's not an image, so output it raw
1287 $s .= $prefix . '[[' . $link . '|' . $text;
1288 # note: no $trail, because without an end, there *is* no trail
1289 continue;
1290 }
1291 }
1292
1293 $wasblank = ( '' == $text );
1294 if( $wasblank ) $text = $link;
1295
1296
1297 # Link not escaped by : , create the various objects
1298 if( $noforce ) {
1299
1300 # Interwikis
1301 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1302 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1303 $tmp = $prefix . $trail ;
1304 $s .= (trim($tmp) == '')? '': $tmp;
1305 continue;
1306 }
1307
1308 if ( $ns == NS_IMAGE ) {
1309 wfProfileIn( "$fname-image" );
1310
1311 # recursively parse links inside the image caption
1312 # actually, this will parse them in any other parameters, too,
1313 # but it might be hard to fix that, and it doesn't matter ATM
1314 $text = $this->replaceExternalLinks($text);
1315 $text = $this->replaceInternalLinks($text);
1316
1317 # replace the image with a link-holder so that replaceExternalLinks() can't mess with it
1318 $s .= $prefix . $this->insertStripItem( $sk->makeImageLinkObj( $nt, $text ), $this->mStripState ) . $trail;
1319 $wgLinkCache->addImageLinkObj( $nt );
1320
1321 wfProfileOut( "$fname-image" );
1322 continue;
1323 }
1324
1325 if ( $ns == NS_CATEGORY ) {
1326 wfProfileIn( "$fname-category" );
1327 $t = $nt->getText();
1328
1329 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1330 $pPLC=$sk->postParseLinkColour();
1331 $sk->postParseLinkColour( false );
1332 $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
1333 $sk->postParseLinkColour( $pPLC );
1334 $wgLinkCache->resume();
1335
1336 if ( $wasblank ) {
1337 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
1338 $sortkey = $this->mTitle->getText();
1339 } else {
1340 $sortkey = $this->mTitle->getPrefixedText();
1341 }
1342 } else {
1343 $sortkey = $text;
1344 }
1345 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1346 $this->mOutput->addCategoryLink( $t );
1347 $s .= $prefix . $trail ;
1348
1349 wfProfileOut( "$fname-category" );
1350 continue;
1351 }
1352 }
1353
1354 if( ( $nt->getPrefixedText() === $selflink ) &&
1355 ( $nt->getFragment() === '' ) ) {
1356 # Self-links are handled specially; generally de-link and change to bold.
1357 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1358 continue;
1359 }
1360
1361 # Special and Media are pseudo-namespaces; no pages actually exist in them
1362 if( $ns == NS_MEDIA ) {
1363 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text, true ) . $trail;
1364 $wgLinkCache->addImageLinkObj( $nt );
1365 continue;
1366 } elseif( $ns == NS_SPECIAL ) {
1367 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1368 continue;
1369 }
1370 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1371 }
1372 $sk->postParseLinkColour( $saveParseColour );
1373 wfProfileOut( $fname );
1374 return $s;
1375 }
1376
1377 /**
1378 * Return true if subpage links should be expanded on this page.
1379 * @return bool
1380 */
1381 function areSubpagesAllowed() {
1382 # Some namespaces don't allow subpages
1383 global $wgNamespacesWithSubpages;
1384 return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
1385 }
1386
1387 /**
1388 * Handle link to subpage if necessary
1389 * @param string $target the source of the link
1390 * @param string &$text the link text, modified as necessary
1391 * @return string the full name of the link
1392 * @access private
1393 */
1394 function maybeDoSubpageLink($target, &$text) {
1395 # Valid link forms:
1396 # Foobar -- normal
1397 # :Foobar -- override special treatment of prefix (images, language links)
1398 # /Foobar -- convert to CurrentPage/Foobar
1399 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1400 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1401 # ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
1402
1403 $fname = 'Parser::maybeDoSubpageLink';
1404 wfProfileIn( $fname );
1405 $ret = $target; # default return value is no change
1406
1407 # Some namespaces don't allow subpages,
1408 # so only perform processing if subpages are allowed
1409 if( $this->areSubpagesAllowed() ) {
1410 # Look at the first character
1411 if( $target != '' && $target{0} == '/' ) {
1412 # / at end means we don't want the slash to be shown
1413 if( substr( $target, -1, 1 ) == '/' ) {
1414 $target = substr( $target, 1, -1 );
1415 $noslash = $target;
1416 } else {
1417 $noslash = substr( $target, 1 );
1418 }
1419
1420 $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1421 if( '' === $text ) {
1422 $text = $target;
1423 } # this might be changed for ugliness reasons
1424 } else {
1425 # check for .. subpage backlinks
1426 $dotdotcount = 0;
1427 $nodotdot = $target;
1428 while( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1429 ++$dotdotcount;
1430 $nodotdot = substr( $nodotdot, 3 );
1431 }
1432 if($dotdotcount > 0) {
1433 $exploded = explode( '/', $this->mTitle->GetPrefixedText() );
1434 if( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1435 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1436 # / at the end means don't show full path
1437 if( substr( $nodotdot, -1, 1 ) == '/' ) {
1438 $nodotdot = substr( $nodotdot, 0, -1 );
1439 if( '' === $text ) {
1440 $text = $nodotdot;
1441 }
1442 }
1443 $nodotdot = trim( $nodotdot );
1444 if( $nodotdot != '' ) {
1445 $ret .= '/' . $nodotdot;
1446 }
1447 }
1448 }
1449 }
1450 }
1451
1452 wfProfileOut( $fname );
1453 return $ret;
1454 }
1455
1456 /**#@+
1457 * Used by doBlockLevels()
1458 * @access private
1459 */
1460 /* private */ function closeParagraph() {
1461 $result = '';
1462 if ( '' != $this->mLastSection ) {
1463 $result = '</' . $this->mLastSection . ">\n";
1464 }
1465 $this->mInPre = false;
1466 $this->mLastSection = '';
1467 return $result;
1468 }
1469 # getCommon() returns the length of the longest common substring
1470 # of both arguments, starting at the beginning of both.
1471 #
1472 /* private */ function getCommon( $st1, $st2 ) {
1473 $fl = strlen( $st1 );
1474 $shorter = strlen( $st2 );
1475 if ( $fl < $shorter ) { $shorter = $fl; }
1476
1477 for ( $i = 0; $i < $shorter; ++$i ) {
1478 if ( $st1{$i} != $st2{$i} ) { break; }
1479 }
1480 return $i;
1481 }
1482 # These next three functions open, continue, and close the list
1483 # element appropriate to the prefix character passed into them.
1484 #
1485 /* private */ function openList( $char ) {
1486 $result = $this->closeParagraph();
1487
1488 if ( '*' == $char ) { $result .= '<ul><li>'; }
1489 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1490 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1491 else if ( ';' == $char ) {
1492 $result .= '<dl><dt>';
1493 $this->mDTopen = true;
1494 }
1495 else { $result = '<!-- ERR 1 -->'; }
1496
1497 return $result;
1498 }
1499
1500 /* private */ function nextItem( $char ) {
1501 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1502 else if ( ':' == $char || ';' == $char ) {
1503 $close = '</dd>';
1504 if ( $this->mDTopen ) { $close = '</dt>'; }
1505 if ( ';' == $char ) {
1506 $this->mDTopen = true;
1507 return $close . '<dt>';
1508 } else {
1509 $this->mDTopen = false;
1510 return $close . '<dd>';
1511 }
1512 }
1513 return '<!-- ERR 2 -->';
1514 }
1515
1516 /* private */ function closeList( $char ) {
1517 if ( '*' == $char ) { $text = '</li></ul>'; }
1518 else if ( '#' == $char ) { $text = '</li></ol>'; }
1519 else if ( ':' == $char ) {
1520 if ( $this->mDTopen ) {
1521 $this->mDTopen = false;
1522 $text = '</dt></dl>';
1523 } else {
1524 $text = '</dd></dl>';
1525 }
1526 }
1527 else { return '<!-- ERR 3 -->'; }
1528 return $text."\n";
1529 }
1530 /**#@-*/
1531
1532 /**
1533 * Make lists from lines starting with ':', '*', '#', etc.
1534 *
1535 * @access private
1536 * @return string the lists rendered as HTML
1537 */
1538 function doBlockLevels( $text, $linestart ) {
1539 $fname = 'Parser::doBlockLevels';
1540 wfProfileIn( $fname );
1541
1542 # Parsing through the text line by line. The main thing
1543 # happening here is handling of block-level elements p, pre,
1544 # and making lists from lines starting with * # : etc.
1545 #
1546 $textLines = explode( "\n", $text );
1547
1548 $lastPrefix = $output = $lastLine = '';
1549 $this->mDTopen = $inBlockElem = false;
1550 $prefixLength = 0;
1551 $paragraphStack = false;
1552
1553 if ( !$linestart ) {
1554 $output .= array_shift( $textLines );
1555 }
1556 foreach ( $textLines as $oLine ) {
1557 $lastPrefixLength = strlen( $lastPrefix );
1558 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1559 $preOpenMatch = preg_match('/<pre/i', $oLine );
1560 if ( !$this->mInPre ) {
1561 # Multiple prefixes may abut each other for nested lists.
1562 $prefixLength = strspn( $oLine, '*#:;' );
1563 $pref = substr( $oLine, 0, $prefixLength );
1564
1565 # eh?
1566 $pref2 = str_replace( ';', ':', $pref );
1567 $t = substr( $oLine, $prefixLength );
1568 $this->mInPre = !empty($preOpenMatch);
1569 } else {
1570 # Don't interpret any other prefixes in preformatted text
1571 $prefixLength = 0;
1572 $pref = $pref2 = '';
1573 $t = $oLine;
1574 }
1575
1576 # List generation
1577 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1578 # Same as the last item, so no need to deal with nesting or opening stuff
1579 $output .= $this->nextItem( substr( $pref, -1 ) );
1580 $paragraphStack = false;
1581
1582 if ( substr( $pref, -1 ) == ';') {
1583 # The one nasty exception: definition lists work like this:
1584 # ; title : definition text
1585 # So we check for : in the remainder text to split up the
1586 # title and definition, without b0rking links.
1587 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1588 $t = $t2;
1589 $output .= $term . $this->nextItem( ':' );
1590 }
1591 }
1592 } elseif( $prefixLength || $lastPrefixLength ) {
1593 # Either open or close a level...
1594 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1595 $paragraphStack = false;
1596
1597 while( $commonPrefixLength < $lastPrefixLength ) {
1598 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1599 --$lastPrefixLength;
1600 }
1601 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1602 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1603 }
1604 while ( $prefixLength > $commonPrefixLength ) {
1605 $char = substr( $pref, $commonPrefixLength, 1 );
1606 $output .= $this->openList( $char );
1607
1608 if ( ';' == $char ) {
1609 # FIXME: This is dupe of code above
1610 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1611 $t = $t2;
1612 $output .= $term . $this->nextItem( ':' );
1613 }
1614 }
1615 ++$commonPrefixLength;
1616 }
1617 $lastPrefix = $pref2;
1618 }
1619 if( 0 == $prefixLength ) {
1620 wfProfileIn( "$fname-paragraph" );
1621 # No prefix (not in list)--go to paragraph mode
1622 $uniq_prefix = UNIQ_PREFIX;
1623 // XXX: use a stack for nestable elements like span, table and div
1624 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
1625 $closematch = preg_match(
1626 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1627 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/iS', $t );
1628 if ( $openmatch or $closematch ) {
1629 $paragraphStack = false;
1630 $output .= $this->closeParagraph();
1631 if($preOpenMatch and !$preCloseMatch) {
1632 $this->mInPre = true;
1633 }
1634 if ( $closematch ) {
1635 $inBlockElem = false;
1636 } else {
1637 $inBlockElem = true;
1638 }
1639 } else if ( !$inBlockElem && !$this->mInPre ) {
1640 if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1641 // pre
1642 if ($this->mLastSection != 'pre') {
1643 $paragraphStack = false;
1644 $output .= $this->closeParagraph().'<pre>';
1645 $this->mLastSection = 'pre';
1646 }
1647 $t = substr( $t, 1 );
1648 } else {
1649 // paragraph
1650 if ( '' == trim($t) ) {
1651 if ( $paragraphStack ) {
1652 $output .= $paragraphStack.'<br />';
1653 $paragraphStack = false;
1654 $this->mLastSection = 'p';
1655 } else {
1656 if ($this->mLastSection != 'p' ) {
1657 $output .= $this->closeParagraph();
1658 $this->mLastSection = '';
1659 $paragraphStack = '<p>';
1660 } else {
1661 $paragraphStack = '</p><p>';
1662 }
1663 }
1664 } else {
1665 if ( $paragraphStack ) {
1666 $output .= $paragraphStack;
1667 $paragraphStack = false;
1668 $this->mLastSection = 'p';
1669 } else if ($this->mLastSection != 'p') {
1670 $output .= $this->closeParagraph().'<p>';
1671 $this->mLastSection = 'p';
1672 }
1673 }
1674 }
1675 }
1676 wfProfileOut( "$fname-paragraph" );
1677 }
1678 if ($paragraphStack === false) {
1679 $output .= $t."\n";
1680 }
1681 }
1682 while ( $prefixLength ) {
1683 $output .= $this->closeList( $pref2{$prefixLength-1} );
1684 --$prefixLength;
1685 }
1686 if ( '' != $this->mLastSection ) {
1687 $output .= '</' . $this->mLastSection . '>';
1688 $this->mLastSection = '';
1689 }
1690
1691 wfProfileOut( $fname );
1692 return $output;
1693 }
1694
1695 /**
1696 * Split up a string on ':', ignoring any occurences inside
1697 * <a>..</a> or <span>...</span>
1698 * @param string $str the string to split
1699 * @param string &$before set to everything before the ':'
1700 * @param string &$after set to everything after the ':'
1701 * return string the position of the ':', or false if none found
1702 */
1703 function findColonNoLinks($str, &$before, &$after) {
1704 # I wonder if we should make this count all tags, not just <a>
1705 # and <span>. That would prevent us from matching a ':' that
1706 # comes in the middle of italics other such formatting....
1707 # -- Wil
1708 $fname = 'Parser::findColonNoLinks';
1709 wfProfileIn( $fname );
1710 $pos = 0;
1711 do {
1712 $colon = strpos($str, ':', $pos);
1713
1714 if ($colon !== false) {
1715 $before = substr($str, 0, $colon);
1716 $after = substr($str, $colon + 1);
1717
1718 # Skip any ':' within <a> or <span> pairs
1719 $a = substr_count($before, '<a');
1720 $s = substr_count($before, '<span');
1721 $ca = substr_count($before, '</a>');
1722 $cs = substr_count($before, '</span>');
1723
1724 if ($a <= $ca and $s <= $cs) {
1725 # Tags are balanced before ':'; ok
1726 break;
1727 }
1728 $pos = $colon + 1;
1729 }
1730 } while ($colon !== false);
1731 wfProfileOut( $fname );
1732 return $colon;
1733 }
1734
1735 /**
1736 * Return value of a magic variable (like PAGENAME)
1737 *
1738 * @access private
1739 */
1740 function getVariableValue( $index ) {
1741 global $wgContLang, $wgSitename, $wgServer;
1742
1743 /**
1744 * Some of these require message or data lookups and can be
1745 * expensive to check many times.
1746 */
1747 static $varCache = array();
1748 if( isset( $varCache[$index] ) ) return $varCache[$index];
1749
1750 switch ( $index ) {
1751 case MAG_CURRENTMONTH:
1752 return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
1753 case MAG_CURRENTMONTHNAME:
1754 return $varCache[$index] = $wgContLang->getMonthName( date('n') );
1755 case MAG_CURRENTMONTHNAMEGEN:
1756 return $varCache[$index] = $wgContLang->getMonthNameGen( date('n') );
1757 case MAG_CURRENTDAY:
1758 return $varCache[$index] = $wgContLang->formatNum( date('j') );
1759 case MAG_PAGENAME:
1760 return $this->mTitle->getText();
1761 case MAG_PAGENAMEE:
1762 return $this->mTitle->getPartialURL();
1763 case MAG_NAMESPACE:
1764 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1765 return $wgContLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
1766 case MAG_CURRENTDAYNAME:
1767 return $varCache[$index] = $wgContLang->getWeekdayName( date('w')+1 );
1768 case MAG_CURRENTYEAR:
1769 return $varCache[$index] = $wgContLang->formatNum( date( 'Y' ) );
1770 case MAG_CURRENTTIME:
1771 return $varCache[$index] = $wgContLang->time( wfTimestampNow(), false );
1772 case MAG_CURRENTWEEK:
1773 return $varCache[$index] = $wgContLang->formatNum( date('W') );
1774 case MAG_CURRENTDOW:
1775 return $varCache[$index] = $wgContLang->formatNum( date('w') );
1776 case MAG_NUMBEROFARTICLES:
1777 return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
1778 case MAG_SITENAME:
1779 return $wgSitename;
1780 case MAG_SERVER:
1781 return $wgServer;
1782 default:
1783 return NULL;
1784 }
1785 }
1786
1787 /**
1788 * initialise the magic variables (like CURRENTMONTHNAME)
1789 *
1790 * @access private
1791 */
1792 function initialiseVariables() {
1793 $fname = 'Parser::initialiseVariables';
1794 wfProfileIn( $fname );
1795 global $wgVariableIDs;
1796 $this->mVariables = array();
1797 foreach ( $wgVariableIDs as $id ) {
1798 $mw =& MagicWord::get( $id );
1799 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1800 }
1801 wfProfileOut( $fname );
1802 }
1803
1804 /**
1805 * Replace magic variables, templates, and template arguments
1806 * with the appropriate text. Templates are substituted recursively,
1807 * taking care to avoid infinite loops.
1808 *
1809 * Note that the substitution depends on value of $mOutputType:
1810 * OT_WIKI: only {{subst:}} templates
1811 * OT_MSG: only magic variables
1812 * OT_HTML: all templates and magic variables
1813 *
1814 * @param string $tex The text to transform
1815 * @param array $args Key-value pairs representing template parameters to substitute
1816 * @access private
1817 */
1818 function replaceVariables( $text, $args = array() ) {
1819 global $wgLang, $wgScript, $wgArticlePath;
1820
1821 # Prevent too big inclusions
1822 if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
1823 return $text;
1824 }
1825
1826 $fname = 'Parser::replaceVariables';
1827 wfProfileIn( $fname );
1828
1829 $titleChars = Title::legalChars();
1830
1831 # This function is called recursively. To keep track of arguments we need a stack:
1832 array_push( $this->mArgStack, $args );
1833
1834 # Variable substitution
1835 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
1836
1837 if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
1838 # Argument substitution
1839 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
1840 }
1841 # Template substitution
1842 $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1843 $text = preg_replace_callback( $regex, array( &$this, 'braceSubstitution' ), $text );
1844
1845 array_pop( $this->mArgStack );
1846
1847 wfProfileOut( $fname );
1848 return $text;
1849 }
1850
1851 /**
1852 * Replace magic variables
1853 * @access private
1854 */
1855 function variableSubstitution( $matches ) {
1856 $fname = 'parser::variableSubstitution';
1857 wfProfileIn( $fname );
1858 if ( !$this->mVariables ) {
1859 $this->initialiseVariables();
1860 }
1861 $skip = false;
1862 if ( $this->mOutputType == OT_WIKI ) {
1863 # Do only magic variables prefixed by SUBST
1864 $mwSubst =& MagicWord::get( MAG_SUBST );
1865 if (!$mwSubst->matchStartAndRemove( $matches[1] ))
1866 $skip = true;
1867 # Note that if we don't substitute the variable below,
1868 # we don't remove the {{subst:}} magic word, in case
1869 # it is a template rather than a magic variable.
1870 }
1871 if ( !$skip && array_key_exists( $matches[1], $this->mVariables ) ) {
1872 $text = $this->mVariables[$matches[1]];
1873 $this->mOutput->mContainsOldMagic = true;
1874 } else {
1875 $text = $matches[0];
1876 }
1877 wfProfileOut( $fname );
1878 return $text;
1879 }
1880
1881 # Split template arguments
1882 function getTemplateArgs( $argsString ) {
1883 if ( $argsString === '' ) {
1884 return array();
1885 }
1886
1887 $args = explode( '|', substr( $argsString, 1 ) );
1888
1889 # If any of the arguments contains a '[[' but no ']]', it needs to be
1890 # merged with the next arg because the '|' character between belongs
1891 # to the link syntax and not the template parameter syntax.
1892 $argc = count($args);
1893 $i = 0;
1894 for ( $i = 0; $i < $argc-1; $i++ ) {
1895 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1896 $args[$i] .= '|'.$args[$i+1];
1897 array_splice($args, $i+1, 1);
1898 $i--;
1899 $argc--;
1900 }
1901 }
1902
1903 return $args;
1904 }
1905
1906 /**
1907 * Return the text of a template, after recursively
1908 * replacing any variables or templates within the template.
1909 *
1910 * @param array $matches The parts of the template
1911 * $matches[1]: the title, i.e. the part before the |
1912 * $matches[2]: the parameters (including a leading |), if any
1913 * @return string the text of the template
1914 * @access private
1915 */
1916 function braceSubstitution( $matches ) {
1917 global $wgLinkCache, $wgContLang;
1918 $fname = 'Parser::braceSubstitution';
1919 wfProfileIn( $fname );
1920
1921 $found = false;
1922 $nowiki = false;
1923 $noparse = false;
1924
1925 $title = NULL;
1926
1927 # Need to know if the template comes at the start of a line,
1928 # to treat the beginning of the template like the beginning
1929 # of a line for tables and block-level elements.
1930 $linestart = $matches[1];
1931
1932 # $part1 is the bit before the first |, and must contain only title characters
1933 # $args is a list of arguments, starting from index 0, not including $part1
1934
1935 $part1 = $matches[2];
1936 # If the third subpattern matched anything, it will start with |
1937
1938 $args = $this->getTemplateArgs($matches[3]);
1939 $argc = count( $args );
1940
1941 # Don't parse {{{}}} because that's only for template arguments
1942 if ( $linestart === '{' ) {
1943 $text = $matches[0];
1944 $found = true;
1945 $noparse = true;
1946 }
1947
1948 # SUBST
1949 if ( !$found ) {
1950 $mwSubst =& MagicWord::get( MAG_SUBST );
1951 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
1952 # One of two possibilities is true:
1953 # 1) Found SUBST but not in the PST phase
1954 # 2) Didn't find SUBST and in the PST phase
1955 # In either case, return without further processing
1956 $text = $matches[0];
1957 $found = true;
1958 $noparse = true;
1959 }
1960 }
1961
1962 # MSG, MSGNW and INT
1963 if ( !$found ) {
1964 # Check for MSGNW:
1965 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1966 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1967 $nowiki = true;
1968 } else {
1969 # Remove obsolete MSG:
1970 $mwMsg =& MagicWord::get( MAG_MSG );
1971 $mwMsg->matchStartAndRemove( $part1 );
1972 }
1973
1974 # Check if it is an internal message
1975 $mwInt =& MagicWord::get( MAG_INT );
1976 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1977 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1978 $text = $linestart . wfMsgReal( $part1, $args, true );
1979 $found = true;
1980 }
1981 }
1982 }
1983
1984 # NS
1985 if ( !$found ) {
1986 # Check for NS: (namespace expansion)
1987 $mwNs = MagicWord::get( MAG_NS );
1988 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1989 if ( intval( $part1 ) ) {
1990 $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
1991 $found = true;
1992 } else {
1993 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1994 if ( !is_null( $index ) ) {
1995 $text = $linestart . $wgContLang->getNsText( $index );
1996 $found = true;
1997 }
1998 }
1999 }
2000 }
2001
2002 # LOCALURL and LOCALURLE
2003 if ( !$found ) {
2004 $mwLocal = MagicWord::get( MAG_LOCALURL );
2005 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
2006
2007 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
2008 $func = 'getLocalURL';
2009 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
2010 $func = 'escapeLocalURL';
2011 } else {
2012 $func = '';
2013 }
2014
2015 if ( $func !== '' ) {
2016 $title = Title::newFromText( $part1 );
2017 if ( !is_null( $title ) ) {
2018 if ( $argc > 0 ) {
2019 $text = $linestart . $title->$func( $args[0] );
2020 } else {
2021 $text = $linestart . $title->$func();
2022 }
2023 $found = true;
2024 }
2025 }
2026 }
2027
2028 # GRAMMAR
2029 if ( !$found && $argc == 1 ) {
2030 $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
2031 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
2032 $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
2033 $found = true;
2034 }
2035 }
2036
2037 # Template table test
2038
2039 # Did we encounter this template already? If yes, it is in the cache
2040 # and we need to check for loops.
2041 if ( !$found && isset( $this->mTemplates[$part1] ) ) {
2042 # set $text to cached message.
2043 $text = $linestart . $this->mTemplates[$part1];
2044 $found = true;
2045
2046 # Infinite loop test
2047 if ( isset( $this->mTemplatePath[$part1] ) ) {
2048 $noparse = true;
2049 $found = true;
2050 $text .= '<!-- WARNING: template loop detected -->';
2051 }
2052 }
2053
2054 # Load from database
2055 $itcamefromthedatabase = false;
2056 if ( !$found ) {
2057 $ns = NS_TEMPLATE;
2058 $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
2059 if ($subpage !== '') {
2060 $ns = $this->mTitle->getNamespace();
2061 }
2062 $title = Title::newFromText( $part1, $ns );
2063 if ( !is_null( $title ) && !$title->isExternal() ) {
2064 # Check for excessive inclusion
2065 $dbk = $title->getPrefixedDBkey();
2066 if ( $this->incrementIncludeCount( $dbk ) ) {
2067 # This should never be reached.
2068 $article = new Article( $title );
2069 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
2070 if ( $articleContent !== false ) {
2071 $found = true;
2072 $text = $linestart . $articleContent;
2073 $itcamefromthedatabase = true;
2074 }
2075 }
2076
2077 # If the title is valid but undisplayable, make a link to it
2078 if ( $this->mOutputType == OT_HTML && !$found ) {
2079 $text = $linestart . '[['.$title->getPrefixedText().']]';
2080 $found = true;
2081 }
2082
2083 # Template cache array insertion
2084 $this->mTemplates[$part1] = $text;
2085 }
2086 }
2087
2088 # Recursive parsing, escaping and link table handling
2089 # Only for HTML output
2090 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
2091 $text = wfEscapeWikiText( $text );
2092 } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
2093 # Clean up argument array
2094 $assocArgs = array();
2095 $index = 1;
2096 foreach( $args as $arg ) {
2097 $eqpos = strpos( $arg, '=' );
2098 if ( $eqpos === false ) {
2099 $assocArgs[$index++] = $arg;
2100 } else {
2101 $name = trim( substr( $arg, 0, $eqpos ) );
2102 $value = trim( substr( $arg, $eqpos+1 ) );
2103 if ( $value === false ) {
2104 $value = '';
2105 }
2106 if ( $name !== false ) {
2107 $assocArgs[$name] = $value;
2108 }
2109 }
2110 }
2111
2112 # Add a new element to the templace recursion path
2113 $this->mTemplatePath[$part1] = 1;
2114
2115 $text = $this->strip( $text, $this->mStripState );
2116 $text = $this->removeHTMLtags( $text );
2117 $text = $this->replaceVariables( $text, $assocArgs );
2118
2119 # Resume the link cache and register the inclusion as a link
2120 if ( $this->mOutputType == OT_HTML && !is_null( $title ) ) {
2121 $wgLinkCache->addLinkObj( $title );
2122 }
2123
2124 # If the template begins with a table or block-level
2125 # element, it should be treated as beginning a new line.
2126 if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
2127 $text = "\n" . $text;
2128 }
2129 }
2130
2131 # Empties the template path
2132 $this->mTemplatePath = array();
2133 if ( !$found ) {
2134 wfProfileOut( $fname );
2135 return $matches[0];
2136 } else {
2137 # replace ==section headers==
2138 # XXX this needs to go away once we have a better parser.
2139 if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
2140 if( !is_null( $title ) )
2141 $encodedname = base64_encode($title->getPrefixedDBkey());
2142 else
2143 $encodedname = base64_encode("");
2144 $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
2145 PREG_SPLIT_DELIM_CAPTURE);
2146 $text = '';
2147 $nsec = 0;
2148 for( $i = 0; $i < count($m); $i += 2 ) {
2149 $text .= $m[$i];
2150 if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
2151 $hl = $m[$i + 1];
2152 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
2153 $text .= $hl;
2154 continue;
2155 }
2156 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
2157 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
2158 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
2159
2160 $nsec++;
2161 }
2162 }
2163 }
2164
2165 # Empties the template path
2166 $this->mTemplatePath = array();
2167
2168 if ( !$found ) {
2169 wfProfileOut( $fname );
2170 return $matches[0];
2171 } else {
2172 wfProfileOut( $fname );
2173 return $text;
2174 }
2175 }
2176
2177 /**
2178 * Triple brace replacement -- used for template arguments
2179 * @access private
2180 */
2181 function argSubstitution( $matches ) {
2182 $arg = trim( $matches[1] );
2183 $text = $matches[0];
2184 $inputArgs = end( $this->mArgStack );
2185
2186 if ( array_key_exists( $arg, $inputArgs ) ) {
2187 $text = $inputArgs[$arg];
2188 }
2189
2190 return $text;
2191 }
2192
2193 /**
2194 * Returns true if the function is allowed to include this entity
2195 * @access private
2196 */
2197 function incrementIncludeCount( $dbk ) {
2198 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
2199 $this->mIncludeCount[$dbk] = 0;
2200 }
2201 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
2202 return true;
2203 } else {
2204 return false;
2205 }
2206 }
2207
2208
2209 /**
2210 * Cleans up HTML, removes dangerous tags and attributes, and
2211 * removes HTML comments
2212 * @access private
2213 */
2214 function removeHTMLtags( $text ) {
2215 global $wgUseTidy, $wgUserHtml;
2216 $fname = 'Parser::removeHTMLtags';
2217 wfProfileIn( $fname );
2218
2219 if( $wgUserHtml ) {
2220 $htmlpairs = array( # Tags that must be closed
2221 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
2222 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
2223 'strike', 'strong', 'tt', 'var', 'div', 'center',
2224 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
2225 'ruby', 'rt' , 'rb' , 'rp', 'p', 'span'
2226 );
2227 $htmlsingle = array(
2228 'br', 'hr', 'li', 'dt', 'dd'
2229 );
2230 $htmlnest = array( # Tags that can be nested--??
2231 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
2232 'dl', 'font', 'big', 'small', 'sub', 'sup', 'span'
2233 );
2234 $tabletags = array( # Can only appear inside table
2235 'td', 'th', 'tr'
2236 );
2237 } else {
2238 $htmlpairs = array();
2239 $htmlsingle = array();
2240 $htmlnest = array();
2241 $tabletags = array();
2242 }
2243
2244 $htmlsingle = array_merge( $tabletags, $htmlsingle );
2245 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
2246
2247 $htmlattrs = $this->getHTMLattrs () ;
2248
2249 # Remove HTML comments
2250 $text = $this->removeHTMLcomments( $text );
2251
2252 $bits = explode( '<', $text );
2253 $text = array_shift( $bits );
2254 if(!$wgUseTidy) {
2255 $tagstack = array(); $tablestack = array();
2256 foreach ( $bits as $x ) {
2257 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
2258 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2259 $x, $regs );
2260 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2261 error_reporting( $prev );
2262
2263 $badtag = 0 ;
2264 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2265 # Check our stack
2266 if ( $slash ) {
2267 # Closing a tag...
2268 if ( ! in_array( $t, $htmlsingle ) &&
2269 ( $ot = @array_pop( $tagstack ) ) != $t ) {
2270 @array_push( $tagstack, $ot );
2271 $badtag = 1;
2272 } else {
2273 if ( $t == 'table' ) {
2274 $tagstack = array_pop( $tablestack );
2275 }
2276 $newparams = '';
2277 }
2278 } else {
2279 # Keep track for later
2280 if ( in_array( $t, $tabletags ) &&
2281 ! in_array( 'table', $tagstack ) ) {
2282 $badtag = 1;
2283 } else if ( in_array( $t, $tagstack ) &&
2284 ! in_array ( $t , $htmlnest ) ) {
2285 $badtag = 1 ;
2286 } else if ( ! in_array( $t, $htmlsingle ) ) {
2287 if ( $t == 'table' ) {
2288 array_push( $tablestack, $tagstack );
2289 $tagstack = array();
2290 }
2291 array_push( $tagstack, $t );
2292 }
2293 # Strip non-approved attributes from the tag
2294 $newparams = $this->fixTagAttributes($params);
2295
2296 }
2297 if ( ! $badtag ) {
2298 $rest = str_replace( '>', '&gt;', $rest );
2299 $text .= "<$slash$t $newparams$brace$rest";
2300 continue;
2301 }
2302 }
2303 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2304 }
2305 # Close off any remaining tags
2306 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2307 $text .= "</$t>\n";
2308 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2309 }
2310 } else {
2311 # this might be possible using tidy itself
2312 foreach ( $bits as $x ) {
2313 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2314 $x, $regs );
2315 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2316 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2317 $newparams = $this->fixTagAttributes($params);
2318 $rest = str_replace( '>', '&gt;', $rest );
2319 $text .= "<$slash$t $newparams$brace$rest";
2320 } else {
2321 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2322 }
2323 }
2324 }
2325 wfProfileOut( $fname );
2326 return $text;
2327 }
2328
2329 /**
2330 * Remove '<!--', '-->', and everything between.
2331 * To avoid leaving blank lines, when a comment is both preceded
2332 * and followed by a newline (ignoring spaces), trim leading and
2333 * trailing spaces and one of the newlines.
2334 *
2335 * @access private
2336 */
2337 function removeHTMLcomments( $text ) {
2338 $fname='Parser::removeHTMLcomments';
2339 wfProfileIn( $fname );
2340 while (($start = strpos($text, '<!--')) !== false) {
2341 $end = strpos($text, '-->', $start + 4);
2342 if ($end === false) {
2343 # Unterminated comment; bail out
2344 break;
2345 }
2346
2347 $end += 3;
2348
2349 # Trim space and newline if the comment is both
2350 # preceded and followed by a newline
2351 $spaceStart = max($start - 1, 0);
2352 $spaceLen = $end - $spaceStart;
2353 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2354 $spaceStart--;
2355 $spaceLen++;
2356 }
2357 while (substr($text, $spaceStart + $spaceLen, 1) === ' ')
2358 $spaceLen++;
2359 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") {
2360 # Remove the comment, leading and trailing
2361 # spaces, and leave only one newline.
2362 $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1);
2363 }
2364 else {
2365 # Remove just the comment.
2366 $text = substr_replace($text, '', $start, $end - $start);
2367 }
2368 }
2369 wfProfileOut( $fname );
2370 return $text;
2371 }
2372
2373 /**
2374 * This function accomplishes several tasks:
2375 * 1) Auto-number headings if that option is enabled
2376 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2377 * 3) Add a Table of contents on the top for users who have enabled the option
2378 * 4) Auto-anchor headings
2379 *
2380 * It loops through all headlines, collects the necessary data, then splits up the
2381 * string and re-inserts the newly formatted headlines.
2382 * @access private
2383 */
2384 /* private */ function formatHeadings( $text, $isMain=true ) {
2385 global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders;
2386
2387 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2388 $doShowToc = $this->mOptions->getShowToc();
2389 $forceTocHere = false;
2390 if( !$this->mTitle->userCanEdit() ) {
2391 $showEditLink = 0;
2392 $rightClickHack = 0;
2393 } else {
2394 $showEditLink = $this->mOptions->getEditSection();
2395 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2396 }
2397
2398 # Inhibit editsection links if requested in the page
2399 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2400 if( $esw->matchAndRemove( $text ) ) {
2401 $showEditLink = 0;
2402 }
2403 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2404 # do not add TOC
2405 $mw =& MagicWord::get( MAG_NOTOC );
2406 if( $mw->matchAndRemove( $text ) ) {
2407 $doShowToc = 0;
2408 }
2409
2410 # never add the TOC to the Main Page. This is an entry page that should not
2411 # be more than 1-2 screens large anyway
2412 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2413 $doShowToc = 0;
2414 }
2415
2416 # Get all headlines for numbering them and adding funky stuff like [edit]
2417 # links - this is for later, but we need the number of headlines right now
2418 $numMatches = preg_match_all( '/<H([1-6])(.*?'.'>)(.*?)<\/H[1-6] *>/i', $text, $matches );
2419
2420 # if there are fewer than 4 headlines in the article, do not show TOC
2421 if( $numMatches < 4 ) {
2422 $doShowToc = 0;
2423 }
2424
2425 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2426 # override above conditions and always show TOC at that place
2427 $mw =& MagicWord::get( MAG_TOC );
2428 if ($mw->match( $text ) ) {
2429 $doShowToc = 1;
2430 $forceTocHere = true;
2431 } else {
2432 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2433 # override above conditions and always show TOC above first header
2434 $mw =& MagicWord::get( MAG_FORCETOC );
2435 if ($mw->matchAndRemove( $text ) ) {
2436 $doShowToc = 1;
2437 }
2438 }
2439
2440
2441
2442 # We need this to perform operations on the HTML
2443 $sk =& $this->mOptions->getSkin();
2444
2445 # headline counter
2446 $headlineCount = 0;
2447 $sectionCount = 0; # headlineCount excluding template sections
2448
2449 # Ugh .. the TOC should have neat indentation levels which can be
2450 # passed to the skin functions. These are determined here
2451 $toclevel = 0;
2452 $toc = '';
2453 $full = '';
2454 $head = array();
2455 $sublevelCount = array();
2456 $level = 0;
2457 $prevlevel = 0;
2458 foreach( $matches[3] as $headline ) {
2459 $istemplate = 0;
2460 $templatetitle = "";
2461 $templatesection = 0;
2462
2463 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2464 $istemplate = 1;
2465 $templatetitle = base64_decode($mat[1]);
2466 $templatesection = 1 + (int)base64_decode($mat[2]);
2467 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2468 }
2469
2470 $numbering = '';
2471 if( $level ) {
2472 $prevlevel = $level;
2473 }
2474 $level = $matches[1][$headlineCount];
2475 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2476 # reset when we enter a new level
2477 $sublevelCount[$level] = 0;
2478 $toc .= $sk->tocIndent( $level - $prevlevel );
2479 $toclevel += $level - $prevlevel;
2480 }
2481 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2482 # reset when we step back a level
2483 $sublevelCount[$level+1]=0;
2484 $toc .= $sk->tocUnindent( $prevlevel - $level );
2485 $toclevel -= $prevlevel - $level;
2486 }
2487 # count number of headlines for each level
2488 @$sublevelCount[$level]++;
2489 if( $doNumberHeadings || $doShowToc ) {
2490 $dot = 0;
2491 for( $i = 1; $i <= $level; $i++ ) {
2492 if( !empty( $sublevelCount[$i] ) ) {
2493 if( $dot ) {
2494 $numbering .= '.';
2495 }
2496 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
2497 $dot = 1;
2498 }
2499 }
2500 }
2501
2502 # The canonized header is a version of the header text safe to use for links
2503 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2504 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2505 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2506
2507 # Remove link placeholders by the link text.
2508 # <!--LINK number-->
2509 # turns into
2510 # link text with suffix
2511 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2512 "\$wgLinkHolders['texts'][\$1]",
2513 $canonized_headline );
2514
2515 # strip out HTML
2516 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2517 $tocline = trim( $canonized_headline );
2518 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2519 $replacearray = array(
2520 '%3A' => ':',
2521 '%' => '.'
2522 );
2523 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2524 $refer[$headlineCount] = $canonized_headline;
2525
2526 # count how many in assoc. array so we can track dupes in anchors
2527 @$refers[$canonized_headline]++;
2528 $refcount[$headlineCount]=$refers[$canonized_headline];
2529
2530 # Prepend the number to the heading text
2531
2532 if( $doNumberHeadings || $doShowToc ) {
2533 $tocline = $numbering . ' ' . $tocline;
2534
2535 # Don't number the heading if it is the only one (looks silly)
2536 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2537 # the two are different if the line contains a link
2538 $headline=$numbering . ' ' . $headline;
2539 }
2540 }
2541
2542 # Create the anchor for linking from the TOC to the section
2543 $anchor = $canonized_headline;
2544 if($refcount[$headlineCount] > 1 ) {
2545 $anchor .= '_' . $refcount[$headlineCount];
2546 }
2547 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2548 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2549 }
2550 if( $showEditLink && ( !$istemplate || $templatetitle !== "" ) ) {
2551 if ( empty( $head[$headlineCount] ) ) {
2552 $head[$headlineCount] = '';
2553 }
2554 if( $istemplate )
2555 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2556 else
2557 $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1);
2558 }
2559
2560 # Add the edit section span
2561 if( $rightClickHack ) {
2562 if( $istemplate )
2563 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2564 else
2565 $headline = $sk->editSectionScript($this->mTitle, $sectionCount+1,$headline);
2566 }
2567
2568 # give headline the correct <h#> tag
2569 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2570
2571 $headlineCount++;
2572 if( !$istemplate )
2573 $sectionCount++;
2574 }
2575
2576 if( $doShowToc ) {
2577 $toclines = $headlineCount;
2578 $toc .= $sk->tocUnindent( $toclevel );
2579 $toc = $sk->tocTable( $toc );
2580 }
2581
2582 # split up and insert constructed headlines
2583
2584 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2585 $i = 0;
2586
2587 foreach( $blocks as $block ) {
2588 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2589 # This is the [edit] link that appears for the top block of text when
2590 # section editing is enabled
2591
2592 # Disabled because it broke block formatting
2593 # For example, a bullet point in the top line
2594 # $full .= $sk->editSectionLink(0);
2595 }
2596 $full .= $block;
2597 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2598 # Top anchor now in skin
2599 $full = $full.$toc;
2600 }
2601
2602 if( !empty( $head[$i] ) ) {
2603 $full .= $head[$i];
2604 }
2605 $i++;
2606 }
2607 if($forceTocHere) {
2608 $mw =& MagicWord::get( MAG_TOC );
2609 return $mw->replace( $toc, $full );
2610 } else {
2611 return $full;
2612 }
2613 }
2614
2615 /**
2616 * Return an HTML link for the "ISBN 123456" text
2617 * @access private
2618 */
2619 function magicISBN( $text ) {
2620 global $wgLang;
2621 $fname = 'Parser::magicISBN';
2622 wfProfileIn( $fname );
2623
2624 $a = split( 'ISBN ', ' '.$text );
2625 if ( count ( $a ) < 2 ) {
2626 wfProfileOut( $fname );
2627 return $text;
2628 }
2629 $text = substr( array_shift( $a ), 1);
2630 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2631
2632 foreach ( $a as $x ) {
2633 $isbn = $blank = '' ;
2634 while ( ' ' == $x{0} ) {
2635 $blank .= ' ';
2636 $x = substr( $x, 1 );
2637 }
2638 if ( $x == '' ) { # blank isbn
2639 $text .= "ISBN $blank";
2640 continue;
2641 }
2642 while ( strstr( $valid, $x{0} ) != false ) {
2643 $isbn .= $x{0};
2644 $x = substr( $x, 1 );
2645 }
2646 $num = str_replace( '-', '', $isbn );
2647 $num = str_replace( ' ', '', $num );
2648
2649 if ( '' == $num ) {
2650 $text .= "ISBN $blank$x";
2651 } else {
2652 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2653 $text .= '<a href="' .
2654 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2655 "\" class=\"internal\">ISBN $isbn</a>";
2656 $text .= $x;
2657 }
2658 }
2659 wfProfileOut( $fname );
2660 return $text;
2661 }
2662
2663 /**
2664 * Return an HTML link for the "GEO ..." text
2665 * @access private
2666 */
2667 function magicGEO( $text ) {
2668 global $wgLang, $wgUseGeoMode;
2669 $fname = 'Parser::magicGEO';
2670 wfProfileIn( $fname );
2671
2672 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2673 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2674 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2675 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2676 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2677 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2678
2679 $a = split( 'GEO ', ' '.$text );
2680 if ( count ( $a ) < 2 ) {
2681 wfProfileOut( $fname );
2682 return $text;
2683 }
2684 $text = substr( array_shift( $a ), 1);
2685 $valid = '0123456789.+-:';
2686
2687 foreach ( $a as $x ) {
2688 $geo = $blank = '' ;
2689 while ( ' ' == $x{0} ) {
2690 $blank .= ' ';
2691 $x = substr( $x, 1 );
2692 }
2693 while ( strstr( $valid, $x{0} ) != false ) {
2694 $geo .= $x{0};
2695 $x = substr( $x, 1 );
2696 }
2697 $num = str_replace( '+', '', $geo );
2698 $num = str_replace( ' ', '', $num );
2699
2700 if ( '' == $num || count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2701 $text .= "GEO $blank$x";
2702 } else {
2703 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2704 $text .= '<a href="' .
2705 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2706 "\" class=\"internal\">GEO $geo</a>";
2707 $text .= $x;
2708 }
2709 }
2710 wfProfileOut( $fname );
2711 return $text;
2712 }
2713
2714 /**
2715 * Return an HTML link for the "RFC 1234" text
2716 * @access private
2717 * @param string $text text to be processed
2718 */
2719 function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl' ) {
2720 global $wgLang;
2721
2722 $valid = '0123456789';
2723 $internal = false;
2724
2725 $a = split( $keyword, ' '.$text );
2726 if ( count ( $a ) < 2 ) {
2727 return $text;
2728 }
2729 $text = substr( array_shift( $a ), 1);
2730
2731 /* Check if keyword is preceed by [[.
2732 * This test is made here cause of the array_shift above
2733 * that prevent the test to be done in the foreach.
2734 */
2735 if ( substr( $text, -2 ) == '[[' ) {
2736 $internal = true;
2737 }
2738
2739 foreach ( $a as $x ) {
2740 /* token might be empty if we have RFC RFC 1234 */
2741 if ( $x=='' ) {
2742 $text.=$keyword;
2743 continue;
2744 }
2745
2746 $id = $blank = '' ;
2747
2748 /** remove and save whitespaces in $blank */
2749 while ( $x{0} == ' ' ) {
2750 $blank .= ' ';
2751 $x = substr( $x, 1 );
2752 }
2753
2754 /** remove and save the rfc number in $id */
2755 while ( strstr( $valid, $x{0} ) != false ) {
2756 $id .= $x{0};
2757 $x = substr( $x, 1 );
2758 }
2759
2760 if ( $id == '' ) {
2761 /* call back stripped spaces*/
2762 $text .= $keyword.$blank.$x;
2763 } elseif( $internal ) {
2764 /* normal link */
2765 $text .= $keyword.$id.$x;
2766 } else {
2767 /* build the external link*/
2768 $url = wfmsg( $urlmsg );
2769 $url = str_replace( '$1', $id, $url);
2770 $sk =& $this->mOptions->getSkin();
2771 $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
2772 $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
2773 }
2774
2775 /* Check if the next RFC keyword is preceed by [[ */
2776 $internal = ( substr($x,-2) == '[[' );
2777 }
2778 return $text;
2779 }
2780
2781 /**
2782 * Transform wiki markup when saving a page by doing \r\n -> \n
2783 * conversion, substitting signatures, {{subst:}} templates, etc.
2784 *
2785 * @param string $text the text to transform
2786 * @param Title &$title the Title object for the current article
2787 * @param User &$user the User object describing the current user
2788 * @param ParserOptions $options parsing options
2789 * @param bool $clearState whether to clear the parser state first
2790 * @return string the altered wiki markup
2791 * @access public
2792 */
2793 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2794 $this->mOptions = $options;
2795 $this->mTitle =& $title;
2796 $this->mOutputType = OT_WIKI;
2797
2798 if ( $clearState ) {
2799 $this->clearState();
2800 }
2801
2802 $stripState = false;
2803 $pairs = array(
2804 "\r\n" => "\n",
2805 );
2806 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
2807 $text = $this->strip( $text, $stripState, false );
2808 $text = $this->pstPass2( $text, $user );
2809 $text = $this->unstrip( $text, $stripState );
2810 $text = $this->unstripNoWiki( $text, $stripState );
2811 return $text;
2812 }
2813
2814 /**
2815 * Pre-save transform helper function
2816 * @access private
2817 */
2818 function pstPass2( $text, &$user ) {
2819 global $wgLang, $wgContLang, $wgLocaltimezone;
2820
2821 # Variable replacement
2822 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2823 $text = $this->replaceVariables( $text );
2824
2825 # Signatures
2826 #
2827 $n = $user->getName();
2828 $k = $user->getOption( 'nickname' );
2829 if ( '' == $k ) { $k = $n; }
2830 if ( isset( $wgLocaltimezone ) ) {
2831 $oldtz = getenv( 'TZ' );
2832 putenv( 'TZ='.$wgLocaltimezone );
2833 }
2834 /* Note: this is an ugly timezone hack for the European wikis */
2835 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
2836 ' (' . date( 'T' ) . ')';
2837 if ( isset( $wgLocaltimezone ) ) {
2838 putenv( 'TZ='.$oldtzs );
2839 }
2840
2841 if( $user->getOption( 'fancysig' ) ) {
2842 $sigText = $k;
2843 } else {
2844 $sigText = '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]]";
2845 }
2846 $text = preg_replace( '/~~~~~/', $d, $text );
2847 $text = preg_replace( '/~~~~/', "$sigText $d", $text );
2848 $text = preg_replace( '/~~~/', $sigText, $text );
2849
2850 # Context links: [[|name]] and [[name (context)|]]
2851 #
2852 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2853 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2854 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2855 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2856
2857 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2858 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2859 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2860 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2861 $context = '';
2862 $t = $this->mTitle->getText();
2863 if ( preg_match( $conpat, $t, $m ) ) {
2864 $context = $m[2];
2865 }
2866 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2867 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2868 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2869
2870 if ( '' == $context ) {
2871 $text = preg_replace( $p2, '[[\\1]]', $text );
2872 } else {
2873 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2874 }
2875
2876 # Trim trailing whitespace
2877 # MAG_END (__END__) tag allows for trailing
2878 # whitespace to be deliberately included
2879 $text = rtrim( $text );
2880 $mw =& MagicWord::get( MAG_END );
2881 $mw->matchAndRemove( $text );
2882
2883 return $text;
2884 }
2885
2886 /**
2887 * Set up some variables which are usually set up in parse()
2888 * so that an external function can call some class members with confidence
2889 * @access public
2890 */
2891 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2892 $this->mTitle =& $title;
2893 $this->mOptions = $options;
2894 $this->mOutputType = $outputType;
2895 if ( $clearState ) {
2896 $this->clearState();
2897 }
2898 }
2899
2900 /**
2901 * Transform a MediaWiki message by replacing magic variables.
2902 *
2903 * @param string $text the text to transform
2904 * @param ParserOptions $options options
2905 * @return string the text with variables substituted
2906 * @access public
2907 */
2908 function transformMsg( $text, $options ) {
2909 global $wgTitle;
2910 static $executing = false;
2911
2912 # Guard against infinite recursion
2913 if ( $executing ) {
2914 return $text;
2915 }
2916 $executing = true;
2917
2918 $this->mTitle = $wgTitle;
2919 $this->mOptions = $options;
2920 $this->mOutputType = OT_MSG;
2921 $this->clearState();
2922 $text = $this->replaceVariables( $text );
2923
2924 $executing = false;
2925 return $text;
2926 }
2927
2928 /**
2929 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2930 * Callback will be called with the text within
2931 * Transform and return the text within
2932 * @access public
2933 */
2934 function setHook( $tag, $callback ) {
2935 $oldVal = @$this->mTagHooks[$tag];
2936 $this->mTagHooks[$tag] = $callback;
2937 return $oldVal;
2938 }
2939
2940 /**
2941 * Replace <!--LINK--> link placeholders with actual links, in the buffer
2942 * Placeholders created in Skin::makeLinkObj()
2943 * Returns an array of links found, indexed by PDBK:
2944 * 0 - broken
2945 * 1 - normal link
2946 * 2 - stub
2947 * $options is a bit field, RLH_FOR_UPDATE to select for update
2948 */
2949 function replaceLinkHolders( &$text, $options = 0 ) {
2950 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck, $wgLinkHolders;
2951 global $wgInterwikiLinkHolders;
2952 global $outputReplace;
2953
2954 if ( $wgUseOldExistenceCheck ) {
2955 return array();
2956 }
2957
2958 $fname = 'Parser::replaceLinkHolders';
2959 wfProfileIn( $fname );
2960
2961 $pdbks = array();
2962 $colours = array();
2963
2964 #if ( !empty( $tmpLinks[0] ) ) { #TODO
2965 if ( !empty( $wgLinkHolders['namespaces'] ) ) {
2966 wfProfileIn( $fname.'-check' );
2967 $dbr =& wfGetDB( DB_SLAVE );
2968 $page = $dbr->tableName( 'page' );
2969 $sk = $wgUser->getSkin();
2970 $threshold = $wgUser->getOption('stubthreshold');
2971
2972 # Sort by namespace
2973 asort( $wgLinkHolders['namespaces'] );
2974
2975 # Generate query
2976 $query = false;
2977 foreach ( $wgLinkHolders['namespaces'] as $key => $val ) {
2978 # Make title object
2979 $title = $wgLinkHolders['titles'][$key];
2980
2981 # Skip invalid entries.
2982 # Result will be ugly, but prevents crash.
2983 if ( is_null( $title ) ) {
2984 continue;
2985 }
2986 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
2987
2988 # Check if it's in the link cache already
2989 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
2990 $colours[$pdbk] = 1;
2991 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
2992 $colours[$pdbk] = 0;
2993 } else {
2994 # Not in the link cache, add it to the query
2995 if ( !isset( $current ) ) {
2996 $current = $val;
2997 $tables = $page;
2998 $join = '';
2999 $query = "SELECT page_id, page_namespace, page_title";
3000 if ( $threshold > 0 ) {
3001 $textTable = $dbr->tableName( 'text' );
3002 $query .= ', LENGTH(old_text) AS page_len, page_is_redirect';
3003 $tables .= ", $textTable";
3004 $join = 'page_latest=old_id AND';
3005 }
3006 $query .= " FROM $tables WHERE $join (page_namespace=$val AND page_title IN(";
3007 } elseif ( $current != $val ) {
3008 $current = $val;
3009 $query .= ")) OR (page_namespace=$val AND page_title IN(";
3010 } else {
3011 $query .= ', ';
3012 }
3013
3014 $query .= $dbr->addQuotes( $wgLinkHolders['dbkeys'][$key] );
3015 }
3016 }
3017 if ( $query ) {
3018 $query .= '))';
3019 if ( $options & RLH_FOR_UPDATE ) {
3020 $query .= ' FOR UPDATE';
3021 }
3022
3023 $res = $dbr->query( $query, $fname );
3024
3025 # Fetch data and form into an associative array
3026 # non-existent = broken
3027 # 1 = known
3028 # 2 = stub
3029 while ( $s = $dbr->fetchObject($res) ) {
3030 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
3031 $pdbk = $title->getPrefixedDBkey();
3032 $wgLinkCache->addGoodLink( $s->page_id, $pdbk );
3033
3034 if ( $threshold > 0 ) {
3035 $size = $s->page_len;
3036 if ( $s->page_is_redirect || $s->page_namespace != 0 || $length < $threshold ) {
3037 $colours[$pdbk] = 1;
3038 } else {
3039 $colours[$pdbk] = 2;
3040 }
3041 } else {
3042 $colours[$pdbk] = 1;
3043 }
3044 }
3045 }
3046 wfProfileOut( $fname.'-check' );
3047
3048 # Construct search and replace arrays
3049 wfProfileIn( $fname.'-construct' );
3050 $outputReplace = array();
3051 foreach ( $wgLinkHolders['namespaces'] as $key => $ns ) {
3052 $pdbk = $pdbks[$key];
3053 $searchkey = '<!--LINK '.$key.'-->';
3054 $title = $wgLinkHolders['titles'][$key];
3055 if ( empty( $colours[$pdbk] ) ) {
3056 $wgLinkCache->addBadLink( $pdbk );
3057 $colours[$pdbk] = 0;
3058 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
3059 $wgLinkHolders['texts'][$key],
3060 $wgLinkHolders['queries'][$key] );
3061 } elseif ( $colours[$pdbk] == 1 ) {
3062 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
3063 $wgLinkHolders['texts'][$key],
3064 $wgLinkHolders['queries'][$key] );
3065 } elseif ( $colours[$pdbk] == 2 ) {
3066 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
3067 $wgLinkHolders['texts'][$key],
3068 $wgLinkHolders['queries'][$key] );
3069 }
3070 }
3071 wfProfileOut( $fname.'-construct' );
3072
3073 # Do the thing
3074 wfProfileIn( $fname.'-replace' );
3075
3076 $text = preg_replace_callback(
3077 '/(<!--LINK .*?-->)/',
3078 "outputReplaceMatches",
3079 $text);
3080 wfProfileOut( $fname.'-replace' );
3081 }
3082
3083 if ( !empty( $wgInterwikiLinkHolders ) ) {
3084 wfProfileIn( $fname.'-interwiki' );
3085 $outputReplace = $wgInterwikiLinkHolders;
3086 $text = preg_replace_callback(
3087 '/<!--IWLINK (.*?)-->/',
3088 "outputReplaceMatches",
3089 $text );
3090 wfProfileOut( $fname.'-interwiki' );
3091 }
3092
3093 wfProfileOut( $fname );
3094 return $colours;
3095 }
3096
3097 /**
3098 * Renders an image gallery from a text with one line per image.
3099 * text labels may be given by using |-style alternative text. E.g.
3100 * Image:one.jpg|The number "1"
3101 * Image:tree.jpg|A tree
3102 * given as text will return the HTML of a gallery with two images,
3103 * labeled 'The number "1"' and
3104 * 'A tree'.
3105 */
3106 function renderImageGallery( $text ) {
3107 global $wgLinkCache;
3108 $ig = new ImageGallery();
3109 $ig->setShowBytes( false );
3110 $ig->setShowFilename( false );
3111 $lines = explode( "\n", $text );
3112
3113 foreach ( $lines as $line ) {
3114 # match lines like these:
3115 # Image:someimage.jpg|This is some image
3116 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
3117 # Skip empty lines
3118 if ( count( $matches ) == 0 ) {
3119 continue;
3120 }
3121 $nt = Title::newFromURL( $matches[1] );
3122 if( is_null( $nt ) ) {
3123 # Bogus title. Ignore these so we don't bomb out later.
3124 continue;
3125 }
3126 if ( isset( $matches[3] ) ) {
3127 $label = $matches[3];
3128 } else {
3129 $label = '';
3130 }
3131
3132 # FIXME: Use the full wiki parser and add its links
3133 # to the page's links.
3134 $html = $this->mOptions->mSkin->formatComment( $label );
3135
3136 $ig->add( Image::newFromTitle( $nt ), $html );
3137 $wgLinkCache->addImageLinkObj( $nt );
3138 }
3139 return $ig->toHTML();
3140 }
3141 }
3142
3143 /**
3144 * @todo document
3145 * @package MediaWiki
3146 */
3147 class ParserOutput
3148 {
3149 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
3150 var $mCacheTime; # Used in ParserCache
3151 var $mVersion; # Compatibility check
3152
3153 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
3154 $containsOldMagic = false )
3155 {
3156 $this->mText = $text;
3157 $this->mLanguageLinks = $languageLinks;
3158 $this->mCategoryLinks = $categoryLinks;
3159 $this->mContainsOldMagic = $containsOldMagic;
3160 $this->mCacheTime = '';
3161 $this->mVersion = MW_PARSER_VERSION;
3162 }
3163
3164 function getText() { return $this->mText; }
3165 function getLanguageLinks() { return $this->mLanguageLinks; }
3166 function getCategoryLinks() { return array_keys( $this->mCategoryLinks ); }
3167 function getCacheTime() { return $this->mCacheTime; }
3168 function containsOldMagic() { return $this->mContainsOldMagic; }
3169 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
3170 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
3171 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
3172 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
3173 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
3174 function addCategoryLink( $c ) { $this->mCategoryLinks[$c] = 1; }
3175
3176 function merge( $other ) {
3177 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
3178 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
3179 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
3180 }
3181
3182 /**
3183 * Return true if this cached output object predates the global or
3184 * per-article cache invalidation timestamps, or if it comes from
3185 * an incompatible older version.
3186 *
3187 * @param string $touched the affected article's last touched timestamp
3188 * @return bool
3189 * @access public
3190 */
3191 function expired( $touched ) {
3192 global $wgCacheEpoch;
3193 return $this->getCacheTime() <= $touched ||
3194 $this->getCacheTime() <= $wgCacheEpoch ||
3195 !isset( $this->mVersion ) ||
3196 version_compare( $this->mVersion, MW_PARSER_VERSION, "lt" );
3197 }
3198 }
3199
3200 /**
3201 * Set options of the Parser
3202 * @todo document
3203 * @package MediaWiki
3204 */
3205 class ParserOptions
3206 {
3207 # All variables are private
3208 var $mUseTeX; # Use texvc to expand <math> tags
3209 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
3210 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
3211 var $mAllowExternalImages; # Allow external images inline
3212 var $mSkin; # Reference to the preferred skin
3213 var $mDateFormat; # Date format index
3214 var $mEditSection; # Create "edit section" links
3215 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
3216 var $mNumberHeadings; # Automatically number headings
3217 var $mShowToc; # Show table of contents
3218
3219 function getUseTeX() { return $this->mUseTeX; }
3220 function getUseDynamicDates() { return $this->mUseDynamicDates; }
3221 function getInterwikiMagic() { return $this->mInterwikiMagic; }
3222 function getAllowExternalImages() { return $this->mAllowExternalImages; }
3223 function getSkin() { return $this->mSkin; }
3224 function getDateFormat() { return $this->mDateFormat; }
3225 function getEditSection() { return $this->mEditSection; }
3226 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
3227 function getNumberHeadings() { return $this->mNumberHeadings; }
3228 function getShowToc() { return $this->mShowToc; }
3229
3230 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
3231 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
3232 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
3233 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
3234 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
3235 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
3236 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
3237 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
3238 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
3239
3240 function setSkin( &$x ) { $this->mSkin =& $x; }
3241
3242 # Get parser options
3243 /* static */ function newFromUser( &$user ) {
3244 $popts = new ParserOptions;
3245 $popts->initialiseFromUser( $user );
3246 return $popts;
3247 }
3248
3249 # Get user options
3250 function initialiseFromUser( &$userInput ) {
3251 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
3252 $fname = 'ParserOptions::initialiseFromUser';
3253 wfProfileIn( $fname );
3254 if ( !$userInput ) {
3255 $user = new User;
3256 $user->setLoaded( true );
3257 } else {
3258 $user =& $userInput;
3259 }
3260
3261 $this->mUseTeX = $wgUseTeX;
3262 $this->mUseDynamicDates = $wgUseDynamicDates;
3263 $this->mInterwikiMagic = $wgInterwikiMagic;
3264 $this->mAllowExternalImages = $wgAllowExternalImages;
3265 wfProfileIn( $fname.'-skin' );
3266 $this->mSkin =& $user->getSkin();
3267 wfProfileOut( $fname.'-skin' );
3268 $this->mDateFormat = $user->getOption( 'date' );
3269 $this->mEditSection = $user->getOption( 'editsection' );
3270 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
3271 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
3272 $this->mShowToc = $user->getOption( 'showtoc' );
3273 wfProfileOut( $fname );
3274 }
3275
3276
3277 }
3278
3279 /**
3280 * Callback function used by Parser::replaceLinkHolders()
3281 * to substitute link placeholders.
3282 */
3283 function &outputReplaceMatches( $matches ) {
3284 global $outputReplace;
3285 return $outputReplace[$matches[1]];
3286 }
3287
3288 /**
3289 * Return the total number of articles
3290 */
3291 function wfNumberOfArticles() {
3292 global $wgNumberOfArticles;
3293
3294 wfLoadSiteStats();
3295 return $wgNumberOfArticles;
3296 }
3297
3298 /**
3299 * Get various statistics from the database
3300 * @private
3301 */
3302 function wfLoadSiteStats() {
3303 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
3304 $fname = 'wfLoadSiteStats';
3305
3306 if ( -1 != $wgNumberOfArticles ) return;
3307 $dbr =& wfGetDB( DB_SLAVE );
3308 $s = $dbr->selectRow( 'site_stats',
3309 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
3310 array( 'ss_row_id' => 1 ), $fname
3311 );
3312
3313 if ( $s === false ) {
3314 return;
3315 } else {
3316 $wgTotalViews = $s->ss_total_views;
3317 $wgTotalEdits = $s->ss_total_edits;
3318 $wgNumberOfArticles = $s->ss_good_articles;
3319 }
3320 }
3321
3322 function wfEscapeHTMLTagsOnly( $in ) {
3323 return str_replace(
3324 array( '"', '>', '<' ),
3325 array( '&quot;', '&gt;', '&lt;' ),
3326 $in );
3327 }
3328
3329 ?>