Tell regexp parser to use extra analysis on external link regexp;
[mediawiki.git] / includes / Parser.php
blob433487a5f6c12da4c8fd91d5373538f4a817a354
1 <?php
3 /**
4 * File for Parser and related classes
6 * @package MediaWiki
7 * @version $Id$
8 */
10 /**
11 * Update this version number when the ParserOutput format
12 * changes in an incompatible way, so the parser cache
13 * can automatically discard old data.
15 define( 'MW_PARSER_VERSION', '1.4.0' );
17 /**
18 * Variable substitution O(N^2) attack
20 * Without countermeasures, it would be possible to attack the parser by saving
21 * a page filled with a large number of inclusions of large pages. The size of
22 * the generated page would be proportional to the square of the input size.
23 * Hence, we limit the number of inclusions of any given page, thus bringing any
24 * attack back to O(N).
27 define( 'MAX_INCLUDE_REPEAT', 100 );
28 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
30 define( 'RLH_FOR_UPDATE', 1 );
32 # Allowed values for $mOutputType
33 define( 'OT_HTML', 1 );
34 define( 'OT_WIKI', 2 );
35 define( 'OT_MSG' , 3 );
37 # string parameter for extractTags which will cause it
38 # to strip HTML comments in addition to regular
39 # <XML>-style tags. This should not be anything we
40 # may want to use in wikisyntax
41 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
43 # prefix for escaping, used in two functions at least
44 define( 'UNIQ_PREFIX', 'NaodW29');
46 # Constants needed for external link processing
47 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
48 define( 'HTTP_PROTOCOLS', 'http|https' );
49 # Everything except bracket, space, or control characters
50 define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
51 # Including space
52 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
53 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
54 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
55 define( 'EXT_LINK_BRACKETED', '/\[(('.URL_PROTOCOLS.'):'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
56 define( 'EXT_IMAGE_REGEX',
57 '/^('.HTTP_PROTOCOLS.':)'. # Protocol
58 '('.EXT_LINK_URL_CLASS.'+)\\/'. # Hostname and path
59 '('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
62 /**
63 * PHP Parser
65 * Processes wiki markup
67 * <pre>
68 * There are three main entry points into the Parser class:
69 * parse()
70 * produces HTML output
71 * preSaveTransform().
72 * produces altered wiki markup.
73 * transformMsg()
74 * performs brace substitution on MediaWiki messages
76 * Globals used:
77 * objects: $wgLang, $wgDateFormatter, $wgLinkCache
79 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
81 * settings:
82 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
83 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
84 * $wgLocaltimezone
86 * * only within ParserOptions
87 * </pre>
89 * @package MediaWiki
91 class Parser
93 /**#@+
94 * @access private
96 # Persistent:
97 var $mTagHooks;
99 # Cleared with clearState():
100 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
101 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
103 # Temporary:
104 var $mOptions, $mTitle, $mOutputType,
105 $mTemplates, // cache of already loaded templates, avoids
106 // multiple SQL queries for the same string
107 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
108 // in this path. Used for loop detection.
110 /**#@-*/
113 * Constructor
115 * @access public
117 function Parser() {
118 $this->mTemplates = array();
119 $this->mTemplatePath = array();
120 $this->mTagHooks = array();
121 $this->clearState();
125 * Clear Parser state
127 * @access private
129 function clearState() {
130 $this->mOutput = new ParserOutput;
131 $this->mAutonumber = 0;
132 $this->mLastSection = "";
133 $this->mDTopen = false;
134 $this->mVariables = false;
135 $this->mIncludeCount = array();
136 $this->mStripState = array();
137 $this->mArgStack = array();
138 $this->mInPre = false;
142 * First pass--just handle <nowiki> sections, pass the rest off
143 * to internalParse() which does all the real work.
145 * @access private
146 * @return ParserOutput a ParserOutput
148 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
149 global $wgUseTidy, $wgContLang;
150 $fname = 'Parser::parse';
151 wfProfileIn( $fname );
153 if ( $clearState ) {
154 $this->clearState();
157 $this->mOptions = $options;
158 $this->mTitle =& $title;
159 $this->mOutputType = OT_HTML;
161 $stripState = NULL;
162 $text = $this->strip( $text, $this->mStripState );
164 $text = $this->internalParse( $text, $linestart );
165 $text = $this->unstrip( $text, $this->mStripState );
166 # Clean up special characters, only run once, next-to-last before doBlockLevels
167 if(!$wgUseTidy) {
168 $fixtags = array(
169 # french spaces, last one Guillemet-left
170 # only if there is something before the space
171 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1&nbsp;\\2',
172 # french spaces, Guillemet-right
173 "/(\\302\\253) /i"=>"\\1&nbsp;",
174 '/<hr *>/i' => '<hr />',
175 '/<br *>/i' => '<br />',
176 '/<center *>/i' => '<div class="center">',
177 '/<\\/center *>/i' => '</div>',
178 # Clean up spare ampersands; note that we probably ought to be
179 # more careful about named entities.
180 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
182 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
183 } else {
184 $fixtags = array(
185 # french spaces, last one Guillemet-left
186 '/ (\\?|:|;|!|\\302\\273)/i' => '&nbsp;\\1',
187 # french spaces, Guillemet-right
188 '/(\\302\\253) /i' => '\\1&nbsp;',
189 '/<center *>/i' => '<div class="center">',
190 '/<\\/center *>/i' => '</div>'
192 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
194 # only once and last
195 $text = $this->doBlockLevels( $text, $linestart );
197 $this->replaceLinkHolders( $text );
198 $text = $wgContLang->convert($text);
200 $text = $this->unstripNoWiki( $text, $this->mStripState );
201 global $wgUseTidy;
202 if ($wgUseTidy) {
203 $text = Parser::tidy($text);
206 $this->mOutput->setText( $text );
207 wfProfileOut( $fname );
208 return $this->mOutput;
212 * Get a random string
214 * @access private
215 * @static
217 function getRandomString() {
218 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
221 /**
222 * Replaces all occurrences of <$tag>content</$tag> in the text
223 * with a random marker and returns the new text. the output parameter
224 * $content will be an associative array filled with data on the form
225 * $unique_marker => content.
227 * If $content is already set, the additional entries will be appended
228 * If $tag is set to STRIP_COMMENTS, the function will extract
229 * <!-- HTML comments -->
231 * @access private
232 * @static
234 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
235 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
236 if ( !$content ) {
237 $content = array( );
239 $n = 1;
240 $stripped = '';
242 while ( '' != $text ) {
243 if($tag==STRIP_COMMENTS) {
244 $p = preg_split( '/<!--/i', $text, 2 );
245 } else {
246 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
248 $stripped .= $p[0];
249 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
250 $text = '';
251 } else {
252 if($tag==STRIP_COMMENTS) {
253 $q = preg_split( '/-->/i', $p[1], 2 );
254 } else {
255 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
257 $marker = $rnd . sprintf('%08X', $n++);
258 $content[$marker] = $q[0];
259 $stripped .= $marker;
260 $text = $q[1];
263 return $stripped;
267 * Strips and renders nowiki, pre, math, hiero
268 * If $render is set, performs necessary rendering operations on plugins
269 * Returns the text, and fills an array with data needed in unstrip()
270 * If the $state is already a valid strip state, it adds to the state
272 * @param bool $stripcomments when set, HTML comments <!-- like this -->
273 * will be stripped in addition to other tags. This is important
274 * for section editing, where these comments cause confusion when
275 * counting the sections in the wikisource
277 * @access private
279 function strip( $text, &$state, $stripcomments = false ) {
280 $render = ($this->mOutputType == OT_HTML);
281 $html_content = array();
282 $nowiki_content = array();
283 $math_content = array();
284 $pre_content = array();
285 $comment_content = array();
286 $ext_content = array();
287 $gallery_content = array();
289 # Replace any instances of the placeholders
290 $uniq_prefix = UNIQ_PREFIX;
291 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
293 # html
294 global $wgRawHtml, $wgWhitelistEdit;
295 if( $wgRawHtml && $wgWhitelistEdit ) {
296 $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
297 foreach( $html_content as $marker => $content ) {
298 if ($render ) {
299 # Raw and unchecked for validity.
300 $html_content[$marker] = $content;
301 } else {
302 $html_content[$marker] = '<html>'.$content.'</html>';
307 # nowiki
308 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
309 foreach( $nowiki_content as $marker => $content ) {
310 if( $render ){
311 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
312 } else {
313 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
317 # math
318 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
319 foreach( $math_content as $marker => $content ){
320 if( $render ) {
321 if( $this->mOptions->getUseTeX() ) {
322 $math_content[$marker] = renderMath( $content );
323 } else {
324 $math_content[$marker] = '&lt;math&gt;'.$content.'&lt;math&gt;';
326 } else {
327 $math_content[$marker] = '<math>'.$content.'</math>';
331 # pre
332 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
333 foreach( $pre_content as $marker => $content ){
334 if( $render ){
335 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
336 } else {
337 $pre_content[$marker] = '<pre>'.$content.'</pre>';
341 # gallery
342 $text = Parser::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
343 foreach( $gallery_content as $marker => $content ) {
344 require_once( 'ImageGallery.php' );
345 if ( $render ) {
346 $gallery_content[$marker] = Parser::renderImageGallery( $content );
347 } else {
348 $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
352 # Comments
353 if($stripcomments) {
354 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
355 foreach( $comment_content as $marker => $content ){
356 $comment_content[$marker] = '<!--'.$content.'-->';
360 # Extensions
361 foreach ( $this->mTagHooks as $tag => $callback ) {
362 $ext_contents[$tag] = array();
363 $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
364 foreach( $ext_content[$tag] as $marker => $content ) {
365 if ( $render ) {
366 $ext_content[$tag][$marker] = $callback( $content );
367 } else {
368 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
373 # Merge state with the pre-existing state, if there is one
374 if ( $state ) {
375 $state['html'] = $state['html'] + $html_content;
376 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
377 $state['math'] = $state['math'] + $math_content;
378 $state['pre'] = $state['pre'] + $pre_content;
379 $state['comment'] = $state['comment'] + $comment_content;
380 $state['gallery'] = $state['gallery'] + $gallery_content;
382 foreach( $ext_content as $tag => $array ) {
383 if ( array_key_exists( $tag, $state ) ) {
384 $state[$tag] = $state[$tag] + $array;
387 } else {
388 $state = array(
389 'html' => $html_content,
390 'nowiki' => $nowiki_content,
391 'math' => $math_content,
392 'pre' => $pre_content,
393 'comment' => $comment_content,
394 'gallery' => $gallery_content,
395 ) + $ext_content;
397 return $text;
401 * restores pre, math, and hiero removed by strip()
403 * always call unstripNoWiki() after this one
404 * @access private
406 function unstrip( $text, &$state ) {
407 # Must expand in reverse order, otherwise nested tags will be corrupted
408 $contentDict = end( $state );
409 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
410 if( key($state) != 'nowiki' && key($state) != 'html') {
411 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
412 $text = str_replace( key( $contentDict ), $content, $text );
417 return $text;
421 * always call this after unstrip() to preserve the order
423 * @access private
425 function unstripNoWiki( $text, &$state ) {
426 # Must expand in reverse order, otherwise nested tags will be corrupted
427 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
428 $text = str_replace( key( $state['nowiki'] ), $content, $text );
431 global $wgRawHtml;
432 if ($wgRawHtml) {
433 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
434 $text = str_replace( key( $state['html'] ), $content, $text );
438 return $text;
442 * Add an item to the strip state
443 * Returns the unique tag which must be inserted into the stripped text
444 * The tag will be replaced with the original text in unstrip()
446 * @access private
448 function insertStripItem( $text, &$state ) {
449 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
450 if ( !$state ) {
451 $state = array(
452 'html' => array(),
453 'nowiki' => array(),
454 'math' => array(),
455 'pre' => array()
458 $state['item'][$rnd] = $text;
459 return $rnd;
463 * Return allowed HTML attributes
465 * @access private
467 function getHTMLattrs () {
468 $htmlattrs = array( # Allowed attributes--no scripting, etc.
469 'title', 'align', 'lang', 'dir', 'width', 'height',
470 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
471 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
472 /* FONT */ 'type', 'start', 'value', 'compact',
473 /* For various lists, mostly deprecated but safe */
474 'summary', 'width', 'border', 'frame', 'rules',
475 'cellspacing', 'cellpadding', 'valign', 'char',
476 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
477 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
478 'id', 'class', 'name', 'style' /* For CSS */
480 return $htmlattrs ;
484 * Remove non approved attributes and javascript in css
486 * @access private
488 function fixTagAttributes ( $t ) {
489 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
490 $htmlattrs = $this->getHTMLattrs() ;
492 # Strip non-approved attributes from the tag
493 $t = preg_replace(
494 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
495 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
496 $t);
498 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
500 # Strip javascript "expression" from stylesheets. Brute force approach:
501 # If anythin offensive is found, all attributes of the HTML tag are dropped
503 if( preg_match(
504 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
505 wfMungeToUtf8( $t ) ) )
507 $t='';
510 return trim ( $t ) ;
514 * interface with html tidy, used if $wgUseTidy = true
516 * @access public
517 * @static
519 function tidy ( $text ) {
520 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
521 global $wgInputEncoding, $wgOutputEncoding;
522 $fname = 'Parser::tidy';
523 wfProfileIn( $fname );
525 $cleansource = '';
526 $opts = '';
527 switch(strtoupper($wgOutputEncoding)) {
528 case 'ISO-8859-1':
529 $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
530 break;
531 case 'UTF-8':
532 $opts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
533 break;
534 default:
535 $opts .= ' -raw';
538 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
539 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
540 '<head><title>test</title></head><body>'.$text.'</body></html>';
541 $descriptorspec = array(
542 0 => array('pipe', 'r'),
543 1 => array('pipe', 'w'),
544 2 => array('file', '/dev/null', 'a')
546 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
547 if (is_resource($process)) {
548 fwrite($pipes[0], $wrappedtext);
549 fclose($pipes[0]);
550 while (!feof($pipes[1])) {
551 $cleansource .= fgets($pipes[1], 1024);
553 fclose($pipes[1]);
554 $return_value = proc_close($process);
557 wfProfileOut( $fname );
559 if( $cleansource == '' && $text != '') {
560 wfDebug( "Tidy error detected!\n" );
561 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
562 } else {
563 return $cleansource;
568 * parse the wiki syntax used to render tables
570 * @access private
572 function doTableStuff ( $t ) {
573 $fname = 'Parser::doTableStuff';
574 wfProfileIn( $fname );
576 $t = explode ( "\n" , $t ) ;
577 $td = array () ; # Is currently a td tag open?
578 $ltd = array () ; # Was it TD or TH?
579 $tr = array () ; # Is currently a tr tag open?
580 $ltr = array () ; # tr attributes
581 $indent_level = 0; # indent level of the table
582 foreach ( $t AS $k => $x )
584 $x = trim ( $x ) ;
585 $fc = substr ( $x , 0 , 1 ) ;
586 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
587 $indent_level = strlen( $matches[1] );
588 $t[$k] = "\n" .
589 str_repeat( '<dl><dd>', $indent_level ) .
590 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
591 array_push ( $td , false ) ;
592 array_push ( $ltd , '' ) ;
593 array_push ( $tr , false ) ;
594 array_push ( $ltr , '' ) ;
596 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
597 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
598 $z = "</table>\n" ;
599 $l = array_pop ( $ltd ) ;
600 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
601 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
602 array_pop ( $ltr ) ;
603 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
605 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
606 $x = substr ( $x , 1 ) ;
607 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
608 $z = '' ;
609 $l = array_pop ( $ltd ) ;
610 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
611 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
612 array_pop ( $ltr ) ;
613 $t[$k] = $z ;
614 array_push ( $tr , false ) ;
615 array_push ( $td , false ) ;
616 array_push ( $ltd , '' ) ;
617 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
619 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
620 # $x is a table row
621 if ( '|+' == substr ( $x , 0 , 2 ) ) {
622 $fc = '+' ;
623 $x = substr ( $x , 1 ) ;
625 $after = substr ( $x , 1 ) ;
626 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
627 $after = explode ( '||' , $after ) ;
628 $t[$k] = '' ;
630 # Loop through each table cell
631 foreach ( $after AS $theline )
633 $z = '' ;
634 if ( $fc != '+' )
636 $tra = array_pop ( $ltr ) ;
637 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
638 array_push ( $tr , true ) ;
639 array_push ( $ltr , '' ) ;
642 $l = array_pop ( $ltd ) ;
643 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
644 if ( $fc == '|' ) $l = 'td' ;
645 else if ( $fc == '!' ) $l = 'th' ;
646 else if ( $fc == '+' ) $l = 'caption' ;
647 else $l = '' ;
648 array_push ( $ltd , $l ) ;
650 # Cell parameters
651 $y = explode ( '|' , $theline , 2 ) ;
652 # Note that a '|' inside an invalid link should not
653 # be mistaken as delimiting cell parameters
654 if ( strpos( $y[0], '[[' ) !== false ) {
655 $y = array ($theline);
657 if ( count ( $y ) == 1 )
658 $y = "{$z}<{$l}>{$y[0]}" ;
659 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
660 $t[$k] .= $y ;
661 array_push ( $td , true ) ;
666 # Closing open td, tr && table
667 while ( count ( $td ) > 0 )
669 if ( array_pop ( $td ) ) $t[] = '</td>' ;
670 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
671 $t[] = '</table>' ;
674 $t = implode ( "\n" , $t ) ;
675 # $t = $this->removeHTMLtags( $t );
676 wfProfileOut( $fname );
677 return $t ;
681 * Helper function for parse() that transforms wiki markup into
682 * HTML. Only called for $mOutputType == OT_HTML.
684 * @access private
686 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
687 global $wgContLang;
689 $fname = 'Parser::internalParse';
690 wfProfileIn( $fname );
692 $text = $this->removeHTMLtags( $text );
693 $text = $this->replaceVariables( $text, $args );
695 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
697 $text = $this->doHeadings( $text );
698 if($this->mOptions->getUseDynamicDates()) {
699 global $wgDateFormatter;
700 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
702 $text = $this->doAllQuotes( $text );
703 $text = $this->replaceInternalLinks ( $text );
704 $text = $this->replaceExternalLinks( $text );
706 # replaceInternalLinks may sometimes leave behind
707 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
708 $text = str_replace("http-noparse://","http://",$text);
710 $text = $this->doMagicLinks( $text );
711 $text = $this->doTableStuff( $text );
712 $text = $this->formatHeadings( $text, $isMain );
713 $sk =& $this->mOptions->getSkin();
714 $text = $sk->transformContent( $text );
716 wfProfileOut( $fname );
717 return $text;
721 * Replace special strings like "ISBN xxx" and "RFC xxx" with
722 * magic external links.
724 * @access private
726 function &doMagicLinks( &$text ) {
727 global $wgUseGeoMode;
728 $text = $this->magicISBN( $text );
729 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
730 $text = $this->magicGEO( $text );
732 $text = $this->magicRFC( $text, 'RFC ', 'rfcurl' );
733 $text = $this->magicRFC( $text, 'PMID ', 'pubmedurl' );
734 return $text;
738 * Parse ^^ tokens and return html
740 * @access private
742 function doExponent( $text ) {
743 $fname = 'Parser::doExponent';
744 wfProfileIn( $fname );
745 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
746 wfProfileOut( $fname );
747 return $text;
751 * Parse headers and return html
753 * @access private
755 function doHeadings( $text ) {
756 $fname = 'Parser::doHeadings';
757 wfProfileIn( $fname );
758 for ( $i = 6; $i >= 1; --$i ) {
759 $h = substr( '======', 0, $i );
760 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
761 "<h{$i}>\\1</h{$i}>\\2", $text );
763 wfProfileOut( $fname );
764 return $text;
768 * Replace single quotes with HTML markup
769 * @access private
770 * @return string the altered text
772 function doAllQuotes( $text ) {
773 $fname = 'Parser::doAllQuotes';
774 wfProfileIn( $fname );
775 $outtext = '';
776 $lines = explode( "\n", $text );
777 foreach ( $lines as $line ) {
778 $outtext .= $this->doQuotes ( $line ) . "\n";
780 $outtext = substr($outtext, 0,-1);
781 wfProfileOut( $fname );
782 return $outtext;
786 * Helper function for doAllQuotes()
787 * @access private
789 function doQuotes( $text ) {
790 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
791 if ( count( $arr ) == 1 )
792 return $text;
793 else
795 # First, do some preliminary work. This may shift some apostrophes from
796 # being mark-up to being text. It also counts the number of occurrences
797 # of bold and italics mark-ups.
798 $i = 0;
799 $numbold = 0;
800 $numitalics = 0;
801 foreach ( $arr as $r )
803 if ( ( $i % 2 ) == 1 )
805 # If there are ever four apostrophes, assume the first is supposed to
806 # be text, and the remaining three constitute mark-up for bold text.
807 if ( strlen( $arr[$i] ) == 4 )
809 $arr[$i-1] .= "'";
810 $arr[$i] = "'''";
812 # If there are more than 5 apostrophes in a row, assume they're all
813 # text except for the last 5.
814 else if ( strlen( $arr[$i] ) > 5 )
816 $arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
817 $arr[$i] = "'''''";
819 # Count the number of occurrences of bold and italics mark-ups.
820 # We are not counting sequences of five apostrophes.
821 if ( strlen( $arr[$i] ) == 2 ) $numitalics++; else
822 if ( strlen( $arr[$i] ) == 3 ) $numbold++; else
823 if ( strlen( $arr[$i] ) == 5 ) { $numitalics++; $numbold++; }
825 $i++;
828 # If there is an odd number of both bold and italics, it is likely
829 # that one of the bold ones was meant to be an apostrophe followed
830 # by italics. Which one we cannot know for certain, but it is more
831 # likely to be one that has a single-letter word before it.
832 if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) )
834 $i = 0;
835 $firstsingleletterword = -1;
836 $firstmultiletterword = -1;
837 $firstspace = -1;
838 foreach ( $arr as $r )
840 if ( ( $i % 2 == 1 ) and ( strlen( $r ) == 3 ) )
842 $x1 = substr ($arr[$i-1], -1);
843 $x2 = substr ($arr[$i-1], -2, 1);
844 if ($x1 == ' ') {
845 if ($firstspace == -1) $firstspace = $i;
846 } else if ($x2 == ' ') {
847 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
848 } else {
849 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
852 $i++;
855 # If there is a single-letter word, use it!
856 if ($firstsingleletterword > -1)
858 $arr [ $firstsingleletterword ] = "''";
859 $arr [ $firstsingleletterword-1 ] .= "'";
861 # If not, but there's a multi-letter word, use that one.
862 else if ($firstmultiletterword > -1)
864 $arr [ $firstmultiletterword ] = "''";
865 $arr [ $firstmultiletterword-1 ] .= "'";
867 # ... otherwise use the first one that has neither.
868 # (notice that it is possible for all three to be -1 if, for example,
869 # there is only one pentuple-apostrophe in the line)
870 else if ($firstspace > -1)
872 $arr [ $firstspace ] = "''";
873 $arr [ $firstspace-1 ] .= "'";
877 # Now let's actually convert our apostrophic mush to HTML!
878 $output = '';
879 $buffer = '';
880 $state = '';
881 $i = 0;
882 foreach ($arr as $r)
884 if (($i % 2) == 0)
886 if ($state == 'both')
887 $buffer .= $r;
888 else
889 $output .= $r;
891 else
893 if (strlen ($r) == 2)
895 if ($state == 'i')
896 { $output .= '</i>'; $state = ''; }
897 else if ($state == 'bi')
898 { $output .= '</i>'; $state = 'b'; }
899 else if ($state == 'ib')
900 { $output .= '</b></i><b>'; $state = 'b'; }
901 else if ($state == 'both')
902 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
903 else # $state can be 'b' or ''
904 { $output .= '<i>'; $state .= 'i'; }
906 else if (strlen ($r) == 3)
908 if ($state == 'b')
909 { $output .= '</b>'; $state = ''; }
910 else if ($state == 'bi')
911 { $output .= '</i></b><i>'; $state = 'i'; }
912 else if ($state == 'ib')
913 { $output .= '</b>'; $state = 'i'; }
914 else if ($state == 'both')
915 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
916 else # $state can be 'i' or ''
917 { $output .= '<b>'; $state .= 'b'; }
919 else if (strlen ($r) == 5)
921 if ($state == 'b')
922 { $output .= '</b><i>'; $state = 'i'; }
923 else if ($state == 'i')
924 { $output .= '</i><b>'; $state = 'b'; }
925 else if ($state == 'bi')
926 { $output .= '</i></b>'; $state = ''; }
927 else if ($state == 'ib')
928 { $output .= '</b></i>'; $state = ''; }
929 else if ($state == 'both')
930 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
931 else # ($state == '')
932 { $buffer = ''; $state = 'both'; }
935 $i++;
937 # Now close all remaining tags. Notice that the order is important.
938 if ($state == 'b' || $state == 'ib')
939 $output .= '</b>';
940 if ($state == 'i' || $state == 'bi' || $state == 'ib')
941 $output .= '</i>';
942 if ($state == 'bi')
943 $output .= '</b>';
944 if ($state == 'both')
945 $output .= '<b><i>'.$buffer.'</i></b>';
946 return $output;
951 * Replace external links
953 * Note: this is all very hackish and the order of execution matters a lot.
954 * Make sure to run maintenance/parserTests.php if you change this code.
956 * @access private
958 function replaceExternalLinks( $text ) {
959 $fname = 'Parser::replaceExternalLinks';
960 wfProfileIn( $fname );
962 $sk =& $this->mOptions->getSkin();
963 $linktrail = wfMsgForContent('linktrail');
964 $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
966 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
968 $i = 0;
969 while ( $i<count( $bits ) ) {
970 $url = $bits[$i++];
971 $protocol = $bits[$i++];
972 $text = $bits[$i++];
973 $trail = $bits[$i++];
975 # The characters '<' and '>' (which were escaped by
976 # removeHTMLtags()) should not be included in
977 # URLs, per RFC 2396.
978 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
979 $text = substr($url, $m2[0][1]) . ' ' . $text;
980 $url = substr($url, 0, $m2[0][1]);
983 # If the link text is an image URL, replace it with an <img> tag
984 # This happened by accident in the original parser, but some people used it extensively
985 $img = $this->maybeMakeImageLink( $text );
986 if ( $img !== false ) {
987 $text = $img;
990 $dtrail = '';
992 # No link text, e.g. [http://domain.tld/some.link]
993 if ( $text == '' ) {
994 # Autonumber if allowed
995 if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
996 $text = '[' . ++$this->mAutonumber . ']';
997 } else {
998 # Otherwise just use the URL
999 $text = htmlspecialchars( $url );
1001 } else {
1002 # Have link text, e.g. [http://domain.tld/some.link text]s
1003 # Check for trail
1004 if ( preg_match( $linktrail, $trail, $m2 ) ) {
1005 $dtrail = $m2[1];
1006 $trail = $m2[2];
1010 $encUrl = htmlspecialchars( $url );
1011 # Bit in parentheses showing the URL for the printable version
1012 if( $url == $text || preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
1013 $paren = '';
1014 } else {
1015 # Expand the URL for printable version
1016 if ( ! $sk->suppressUrlExpansion() ) {
1017 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
1018 } else {
1019 $paren = '';
1023 # Process the trail (i.e. everything after this link up until start of the next link),
1024 # replacing any non-bracketed links
1025 $trail = $this->replaceFreeExternalLinks( $trail );
1027 # Use the encoded URL
1028 # This means that users can paste URLs directly into the text
1029 # Funny characters like &ouml; aren't valid in URLs anyway
1030 # This was changed in August 2004
1031 $s .= $sk->makeExternalLink( $url, $text, false ) . $dtrail. $paren . $trail;
1034 wfProfileOut( $fname );
1035 return $s;
1039 * Replace anything that looks like a URL with a link
1040 * @access private
1042 function replaceFreeExternalLinks( $text ) {
1043 $fname = 'Parser::replaceFreeExternalLinks';
1044 wfProfileIn( $fname );
1046 $bits = preg_split( '/((?:'.URL_PROTOCOLS.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1047 $s = array_shift( $bits );
1048 $i = 0;
1050 $sk =& $this->mOptions->getSkin();
1052 while ( $i < count( $bits ) ){
1053 $protocol = $bits[$i++];
1054 $remainder = $bits[$i++];
1056 if ( preg_match( '/^('.EXT_LINK_URL_CLASS.'+)(.*)$/s', $remainder, $m ) ) {
1057 # Found some characters after the protocol that look promising
1058 $url = $protocol . $m[1];
1059 $trail = $m[2];
1061 # The characters '<' and '>' (which were escaped by
1062 # removeHTMLtags()) should not be included in
1063 # URLs, per RFC 2396.
1064 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
1065 $trail = substr($url, $m2[0][1]) . $trail;
1066 $url = substr($url, 0, $m2[0][1]);
1069 # Move trailing punctuation to $trail
1070 $sep = ',;\.:!?';
1071 # If there is no left bracket, then consider right brackets fair game too
1072 if ( strpos( $url, '(' ) === false ) {
1073 $sep .= ')';
1076 $numSepChars = strspn( strrev( $url ), $sep );
1077 if ( $numSepChars ) {
1078 $trail = substr( $url, -$numSepChars ) . $trail;
1079 $url = substr( $url, 0, -$numSepChars );
1082 # Replace &amp; from obsolete syntax with &.
1083 # All HTML entities will be escaped by makeExternalLink()
1084 # or maybeMakeImageLink()
1085 $url = str_replace( '&amp;', '&', $url );
1087 # Is this an external image?
1088 $text = $this->maybeMakeImageLink( $url );
1089 if ( $text === false ) {
1090 # Not an image, make a link
1091 $text = $sk->makeExternalLink( $url, $url );
1093 $s .= $text . $trail;
1094 } else {
1095 $s .= $protocol . $remainder;
1098 wfProfileOut();
1099 return $s;
1103 * make an image if it's allowed
1104 * @access private
1106 function maybeMakeImageLink( $url ) {
1107 $sk =& $this->mOptions->getSkin();
1108 $text = false;
1109 if ( $this->mOptions->getAllowExternalImages() ) {
1110 if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
1111 # Image found
1112 $text = $sk->makeImage( htmlspecialchars( $url ) );
1115 return $text;
1119 * Process [[ ]] wikilinks
1121 * @access private
1124 function replaceInternalLinks( $s ) {
1125 global $wgLang, $wgContLang, $wgLinkCache;
1126 global $wgDisableLangConversion;
1127 static $fname = 'Parser::replaceInternalLinks' ;
1129 wfProfileIn( $fname );
1131 wfProfileIn( $fname.'-setup' );
1132 static $tc = FALSE;
1133 # the % is needed to support urlencoded titles as well
1134 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1136 $sk =& $this->mOptions->getSkin();
1137 global $wgUseOldExistenceCheck;
1138 # "Post-parse link colour check" works only on wiki text since it's now
1139 # in Parser. Enable it, then disable it when we're done.
1140 $saveParseColour = $sk->postParseLinkColour( !$wgUseOldExistenceCheck );
1142 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1144 #split the entire text string on occurences of [[
1145 $a = explode( '[[', ' ' . $s );
1146 #get the first element (all text up to first [[), and remove the space we added
1147 $s = array_shift( $a );
1148 $s = substr( $s, 1 );
1150 # Match a link having the form [[namespace:link|alternate]]trail
1151 static $e1 = FALSE;
1152 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1153 # Match cases where there is no "]]", which might still be images
1154 static $e1_img = FALSE;
1155 if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
1156 # Match the end of a line for a word that's not followed by whitespace,
1157 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1158 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1160 $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
1162 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1164 if ( $useLinkPrefixExtension ) {
1165 if ( preg_match( $e2, $s, $m ) ) {
1166 $first_prefix = $m[2];
1167 $s = $m[1];
1168 } else {
1169 $first_prefix = false;
1171 } else {
1172 $prefix = '';
1175 $selflink = $this->mTitle->getPrefixedText();
1176 wfProfileOut( $fname.'-setup' );
1178 $checkVariantLink = sizeof($wgContLang->getVariants())>1;
1179 # Loop for each link
1180 for ($k = 0; isset( $a[$k] ); $k++) {
1181 $line = $a[$k];
1182 if ( $useLinkPrefixExtension ) {
1183 wfProfileIn( $fname.'-prefixhandling' );
1184 if ( preg_match( $e2, $s, $m ) ) {
1185 $prefix = $m[2];
1186 $s = $m[1];
1187 } else {
1188 $prefix='';
1190 # first link
1191 if($first_prefix) {
1192 $prefix = $first_prefix;
1193 $first_prefix = false;
1195 wfProfileOut( $fname.'-prefixhandling' );
1198 $might_be_img = false;
1200 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1201 $text = $m[2];
1202 # fix up urlencoded title texts
1203 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1204 $trail = $m[3];
1205 } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
1206 $might_be_img = true;
1207 $text = $m[2];
1208 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1209 $trail = "";
1210 } else { # Invalid form; output directly
1211 $s .= $prefix . '[[' . $line ;
1212 continue;
1215 # Don't allow internal links to pages containing
1216 # PROTO: where PROTO is a valid URL protocol; these
1217 # should be external links.
1218 if (preg_match('/^((?:'.URL_PROTOCOLS.'):)/', $m[1])) {
1219 $s .= $prefix . '[[' . $line ;
1220 continue;
1223 # Make subpage if necessary
1224 $link = $this->maybeDoSubpageLink( $m[1], $text );
1226 $noforce = (substr($m[1], 0, 1) != ':');
1227 if (!$noforce) {
1228 # Strip off leading ':'
1229 $link = substr($link, 1);
1232 $nt =& Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
1233 if( !$nt ) {
1234 $s .= $prefix . '[[' . $line;
1235 continue;
1238 #check other language variants of the link
1239 #if the article does not exist
1240 if( $checkVariantLink
1241 && $nt->getArticleID() == 0 ) {
1242 $wgContLang->findVariantLink($link, $nt);
1245 $ns = $nt->getNamespace();
1246 $iw = $nt->getInterWiki();
1248 if ($might_be_img) { # if this is actually an invalid link
1249 if ($ns == NS_IMAGE && $noforce) { #but might be an image
1250 $found = false;
1251 while (isset ($a[$k+1]) ) {
1252 #look at the next 'line' to see if we can close it there
1253 $next_line = array_shift(array_splice( $a, $k + 1, 1) );
1254 if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
1255 # the first ]] closes the inner link, the second the image
1256 $found = true;
1257 $text .= '[[' . $m[1];
1258 $trail = $m[2];
1259 break;
1260 } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
1261 #if there's exactly one ]] that's fine, we'll keep looking
1262 $text .= '[[' . $m[0];
1263 } else {
1264 #if $next_line is invalid too, we need look no further
1265 $text .= '[[' . $next_line;
1266 break;
1269 if ( !$found ) {
1270 # we couldn't find the end of this imageLink, so output it raw
1271 #but don't ignore what might be perfectly normal links in the text we've examined
1272 $text = $this->replaceInternalLinks($text);
1273 $s .= $prefix . '[[' . $link . '|' . $text;
1274 # note: no $trail, because without an end, there *is* no trail
1275 continue;
1277 } else { #it's not an image, so output it raw
1278 $s .= $prefix . '[[' . $link . '|' . $text;
1279 # note: no $trail, because without an end, there *is* no trail
1280 continue;
1284 $wasblank = ( '' == $text );
1285 if( $wasblank ) $text = $link;
1288 # Link not escaped by : , create the various objects
1289 if( $noforce ) {
1291 # Interwikis
1292 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1293 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1294 $tmp = $prefix . $trail ;
1295 $s .= (trim($tmp) == '')? '': $tmp;
1296 continue;
1299 if ( $ns == NS_IMAGE ) {
1300 wfProfileIn( "$fname-image" );
1302 # recursively parse links inside the image caption
1303 # actually, this will parse them in any other parameters, too,
1304 # but it might be hard to fix that, and it doesn't matter ATM
1305 $text = $this->replaceExternalLinks($text);
1306 $text = $this->replaceInternalLinks($text);
1308 # replace the image with a link-holder so that replaceExternalLinks() can't mess with it
1309 $s .= $prefix . $this->insertStripItem( $sk->makeImageLinkObj( $nt, $text ), $this->mStripState ) . $trail;
1310 $wgLinkCache->addImageLinkObj( $nt );
1312 wfProfileOut( "$fname-image" );
1313 continue;
1316 if ( $ns == NS_CATEGORY ) {
1317 wfProfileIn( "$fname-category" );
1318 $t = $nt->getText();
1320 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1321 $pPLC=$sk->postParseLinkColour();
1322 $sk->postParseLinkColour( false );
1323 $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
1324 $sk->postParseLinkColour( $pPLC );
1325 $wgLinkCache->resume();
1327 if ( $wasblank ) {
1328 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
1329 $sortkey = $this->mTitle->getText();
1330 } else {
1331 $sortkey = $this->mTitle->getPrefixedText();
1333 } else {
1334 $sortkey = $text;
1336 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1337 $this->mOutput->addCategoryLink( $t );
1338 $s .= $prefix . $trail ;
1340 wfProfileOut( "$fname-category" );
1341 continue;
1345 if( ( $nt->getPrefixedText() === $selflink ) &&
1346 ( $nt->getFragment() === '' ) ) {
1347 # Self-links are handled specially; generally de-link and change to bold.
1348 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1349 continue;
1352 # Special and Media are pseudo-namespaces; no pages actually exist in them
1353 if( $ns == NS_MEDIA ) {
1354 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text, true ) . $trail;
1355 $wgLinkCache->addImageLinkObj( $nt );
1356 continue;
1357 } elseif( $ns == NS_SPECIAL ) {
1358 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1359 continue;
1361 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1363 $sk->postParseLinkColour( $saveParseColour );
1364 wfProfileOut( $fname );
1365 return $s;
1369 * Handle link to subpage if necessary
1370 * @param string $target the source of the link
1371 * @param string &$text the link text, modified as necessary
1372 * @return string the full name of the link
1373 * @access private
1375 function maybeDoSubpageLink($target, &$text) {
1376 # Valid link forms:
1377 # Foobar -- normal
1378 # :Foobar -- override special treatment of prefix (images, language links)
1379 # /Foobar -- convert to CurrentPage/Foobar
1380 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1381 global $wgNamespacesWithSubpages;
1383 $fname = 'Parser::maybeDoSubpageLink';
1384 wfProfileIn( $fname );
1385 # Look at the first character
1386 if( $target{0} == '/' ) {
1387 # / at end means we don't want the slash to be shown
1388 if(substr($target,-1,1)=='/') {
1389 $target=substr($target,1,-1);
1390 $noslash=$target;
1391 } else {
1392 $noslash=substr($target,1);
1395 # Some namespaces don't allow subpages
1396 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) {
1397 # subpages allowed here
1398 $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1399 if( '' === $text ) {
1400 $text = $target;
1401 } # this might be changed for ugliness reasons
1402 } else {
1403 # no subpage allowed, use standard link
1404 $ret = $target;
1406 } else {
1407 # no subpage
1408 $ret = $target;
1411 wfProfileOut( $fname );
1412 return $ret;
1415 /**#@+
1416 * Used by doBlockLevels()
1417 * @access private
1419 /* private */ function closeParagraph() {
1420 $result = '';
1421 if ( '' != $this->mLastSection ) {
1422 $result = '</' . $this->mLastSection . ">\n";
1424 $this->mInPre = false;
1425 $this->mLastSection = '';
1426 return $result;
1428 # getCommon() returns the length of the longest common substring
1429 # of both arguments, starting at the beginning of both.
1431 /* private */ function getCommon( $st1, $st2 ) {
1432 $fl = strlen( $st1 );
1433 $shorter = strlen( $st2 );
1434 if ( $fl < $shorter ) { $shorter = $fl; }
1436 for ( $i = 0; $i < $shorter; ++$i ) {
1437 if ( $st1{$i} != $st2{$i} ) { break; }
1439 return $i;
1441 # These next three functions open, continue, and close the list
1442 # element appropriate to the prefix character passed into them.
1444 /* private */ function openList( $char ) {
1445 $result = $this->closeParagraph();
1447 if ( '*' == $char ) { $result .= '<ul><li>'; }
1448 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1449 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1450 else if ( ';' == $char ) {
1451 $result .= '<dl><dt>';
1452 $this->mDTopen = true;
1454 else { $result = '<!-- ERR 1 -->'; }
1456 return $result;
1459 /* private */ function nextItem( $char ) {
1460 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1461 else if ( ':' == $char || ';' == $char ) {
1462 $close = '</dd>';
1463 if ( $this->mDTopen ) { $close = '</dt>'; }
1464 if ( ';' == $char ) {
1465 $this->mDTopen = true;
1466 return $close . '<dt>';
1467 } else {
1468 $this->mDTopen = false;
1469 return $close . '<dd>';
1472 return '<!-- ERR 2 -->';
1475 /* private */ function closeList( $char ) {
1476 if ( '*' == $char ) { $text = '</li></ul>'; }
1477 else if ( '#' == $char ) { $text = '</li></ol>'; }
1478 else if ( ':' == $char ) {
1479 if ( $this->mDTopen ) {
1480 $this->mDTopen = false;
1481 $text = '</dt></dl>';
1482 } else {
1483 $text = '</dd></dl>';
1486 else { return '<!-- ERR 3 -->'; }
1487 return $text."\n";
1489 /**#@-*/
1492 * Make lists from lines starting with ':', '*', '#', etc.
1494 * @access private
1495 * @return string the lists rendered as HTML
1497 function doBlockLevels( $text, $linestart ) {
1498 $fname = 'Parser::doBlockLevels';
1499 wfProfileIn( $fname );
1501 # Parsing through the text line by line. The main thing
1502 # happening here is handling of block-level elements p, pre,
1503 # and making lists from lines starting with * # : etc.
1505 $textLines = explode( "\n", $text );
1507 $lastPrefix = $output = $lastLine = '';
1508 $this->mDTopen = $inBlockElem = false;
1509 $prefixLength = 0;
1510 $paragraphStack = false;
1512 if ( !$linestart ) {
1513 $output .= array_shift( $textLines );
1515 foreach ( $textLines as $oLine ) {
1516 $lastPrefixLength = strlen( $lastPrefix );
1517 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1518 $preOpenMatch = preg_match('/<pre/i', $oLine );
1519 if ( !$this->mInPre ) {
1520 # Multiple prefixes may abut each other for nested lists.
1521 $prefixLength = strspn( $oLine, '*#:;' );
1522 $pref = substr( $oLine, 0, $prefixLength );
1524 # eh?
1525 $pref2 = str_replace( ';', ':', $pref );
1526 $t = substr( $oLine, $prefixLength );
1527 $this->mInPre = !empty($preOpenMatch);
1528 } else {
1529 # Don't interpret any other prefixes in preformatted text
1530 $prefixLength = 0;
1531 $pref = $pref2 = '';
1532 $t = $oLine;
1535 # List generation
1536 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1537 # Same as the last item, so no need to deal with nesting or opening stuff
1538 $output .= $this->nextItem( substr( $pref, -1 ) );
1539 $paragraphStack = false;
1541 if ( substr( $pref, -1 ) == ';') {
1542 # The one nasty exception: definition lists work like this:
1543 # ; title : definition text
1544 # So we check for : in the remainder text to split up the
1545 # title and definition, without b0rking links.
1546 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1547 $t = $t2;
1548 $output .= $term . $this->nextItem( ':' );
1551 } elseif( $prefixLength || $lastPrefixLength ) {
1552 # Either open or close a level...
1553 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1554 $paragraphStack = false;
1556 while( $commonPrefixLength < $lastPrefixLength ) {
1557 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1558 --$lastPrefixLength;
1560 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1561 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1563 while ( $prefixLength > $commonPrefixLength ) {
1564 $char = substr( $pref, $commonPrefixLength, 1 );
1565 $output .= $this->openList( $char );
1567 if ( ';' == $char ) {
1568 # FIXME: This is dupe of code above
1569 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1570 $t = $t2;
1571 $output .= $term . $this->nextItem( ':' );
1574 ++$commonPrefixLength;
1576 $lastPrefix = $pref2;
1578 if( 0 == $prefixLength ) {
1579 wfProfileIn( "$fname-paragraph" );
1580 # No prefix (not in list)--go to paragraph mode
1581 $uniq_prefix = UNIQ_PREFIX;
1582 // XXX: use a stack for nestable elements like span, table and div
1583 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
1584 $closematch = preg_match(
1585 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1586 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/iS', $t );
1587 if ( $openmatch or $closematch ) {
1588 $paragraphStack = false;
1589 $output .= $this->closeParagraph();
1590 if($preOpenMatch and !$preCloseMatch) {
1591 $this->mInPre = true;
1593 if ( $closematch ) {
1594 $inBlockElem = false;
1595 } else {
1596 $inBlockElem = true;
1598 } else if ( !$inBlockElem && !$this->mInPre ) {
1599 if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1600 // pre
1601 if ($this->mLastSection != 'pre') {
1602 $paragraphStack = false;
1603 $output .= $this->closeParagraph().'<pre>';
1604 $this->mLastSection = 'pre';
1606 $t = substr( $t, 1 );
1607 } else {
1608 // paragraph
1609 if ( '' == trim($t) ) {
1610 if ( $paragraphStack ) {
1611 $output .= $paragraphStack.'<br />';
1612 $paragraphStack = false;
1613 $this->mLastSection = 'p';
1614 } else {
1615 if ($this->mLastSection != 'p' ) {
1616 $output .= $this->closeParagraph();
1617 $this->mLastSection = '';
1618 $paragraphStack = '<p>';
1619 } else {
1620 $paragraphStack = '</p><p>';
1623 } else {
1624 if ( $paragraphStack ) {
1625 $output .= $paragraphStack;
1626 $paragraphStack = false;
1627 $this->mLastSection = 'p';
1628 } else if ($this->mLastSection != 'p') {
1629 $output .= $this->closeParagraph().'<p>';
1630 $this->mLastSection = 'p';
1635 wfProfileOut( "$fname-paragraph" );
1637 if ($paragraphStack === false) {
1638 $output .= $t."\n";
1641 while ( $prefixLength ) {
1642 $output .= $this->closeList( $pref2{$prefixLength-1} );
1643 --$prefixLength;
1645 if ( '' != $this->mLastSection ) {
1646 $output .= '</' . $this->mLastSection . '>';
1647 $this->mLastSection = '';
1650 wfProfileOut( $fname );
1651 return $output;
1655 * Split up a string on ':', ignoring any occurences inside
1656 * <a>..</a> or <span>...</span>
1657 * @param string $str the string to split
1658 * @param string &$before set to everything before the ':'
1659 * @param string &$after set to everything after the ':'
1660 * return string the position of the ':', or false if none found
1662 function findColonNoLinks($str, &$before, &$after) {
1663 # I wonder if we should make this count all tags, not just <a>
1664 # and <span>. That would prevent us from matching a ':' that
1665 # comes in the middle of italics other such formatting....
1666 # -- Wil
1667 $fname = 'Parser::findColonNoLinks';
1668 wfProfileIn( $fname );
1669 $pos = 0;
1670 do {
1671 $colon = strpos($str, ':', $pos);
1673 if ($colon !== false) {
1674 $before = substr($str, 0, $colon);
1675 $after = substr($str, $colon + 1);
1677 # Skip any ':' within <a> or <span> pairs
1678 $a = substr_count($before, '<a');
1679 $s = substr_count($before, '<span');
1680 $ca = substr_count($before, '</a>');
1681 $cs = substr_count($before, '</span>');
1683 if ($a <= $ca and $s <= $cs) {
1684 # Tags are balanced before ':'; ok
1685 break;
1687 $pos = $colon + 1;
1689 } while ($colon !== false);
1690 wfProfileOut( $fname );
1691 return $colon;
1695 * Return value of a magic variable (like PAGENAME)
1697 * @access private
1699 function getVariableValue( $index ) {
1700 global $wgContLang, $wgSitename, $wgServer;
1703 * Some of these require message or data lookups and can be
1704 * expensive to check many times.
1706 static $varCache = array();
1707 if( isset( $varCache[$index] ) ) return $varCache[$index];
1709 switch ( $index ) {
1710 case MAG_CURRENTMONTH:
1711 return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
1712 case MAG_CURRENTMONTHNAME:
1713 return $varCache[$index] = $wgContLang->getMonthName( date('n') );
1714 case MAG_CURRENTMONTHNAMEGEN:
1715 return $varCache[$index] = $wgContLang->getMonthNameGen( date('n') );
1716 case MAG_CURRENTDAY:
1717 return $varCache[$index] = $wgContLang->formatNum( date('j') );
1718 case MAG_PAGENAME:
1719 return $this->mTitle->getText();
1720 case MAG_PAGENAMEE:
1721 return $this->mTitle->getPartialURL();
1722 case MAG_NAMESPACE:
1723 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1724 return $wgContLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
1725 case MAG_CURRENTDAYNAME:
1726 return $varCache[$index] = $wgContLang->getWeekdayName( date('w')+1 );
1727 case MAG_CURRENTYEAR:
1728 return $varCache[$index] = $wgContLang->formatNum( date( 'Y' ) );
1729 case MAG_CURRENTTIME:
1730 return $varCache[$index] = $wgContLang->time( wfTimestampNow(), false );
1731 case MAG_NUMBEROFARTICLES:
1732 return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
1733 case MAG_SITENAME:
1734 return $wgSitename;
1735 case MAG_SERVER:
1736 return $wgServer;
1737 default:
1738 return NULL;
1743 * initialise the magic variables (like CURRENTMONTHNAME)
1745 * @access private
1747 function initialiseVariables() {
1748 $fname = 'Parser::initialiseVariables';
1749 wfProfileIn( $fname );
1750 global $wgVariableIDs;
1751 $this->mVariables = array();
1752 foreach ( $wgVariableIDs as $id ) {
1753 $mw =& MagicWord::get( $id );
1754 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1756 wfProfileOut( $fname );
1760 * Replace magic variables, templates, and template arguments
1761 * with the appropriate text. Templates are substituted recursively,
1762 * taking care to avoid infinite loops.
1764 * Note that the substitution depends on value of $mOutputType:
1765 * OT_WIKI: only {{subst:}} templates
1766 * OT_MSG: only magic variables
1767 * OT_HTML: all templates and magic variables
1769 * @param string $tex The text to transform
1770 * @param array $args Key-value pairs representing template parameters to substitute
1771 * @access private
1773 function replaceVariables( $text, $args = array() ) {
1774 global $wgLang, $wgScript, $wgArticlePath;
1776 # Prevent too big inclusions
1777 if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
1778 return $text;
1781 $fname = 'Parser::replaceVariables';
1782 wfProfileIn( $fname );
1784 $titleChars = Title::legalChars();
1786 # This function is called recursively. To keep track of arguments we need a stack:
1787 array_push( $this->mArgStack, $args );
1789 # Variable substitution
1790 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
1792 if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
1793 # Argument substitution
1794 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
1796 # Template substitution
1797 $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1798 $text = preg_replace_callback( $regex, array( &$this, 'braceSubstitution' ), $text );
1800 array_pop( $this->mArgStack );
1802 wfProfileOut( $fname );
1803 return $text;
1807 * Replace magic variables
1808 * @access private
1810 function variableSubstitution( $matches ) {
1811 if ( !$this->mVariables ) {
1812 $this->initialiseVariables();
1814 $skip = false;
1815 if ( $this->mOutputType == OT_WIKI ) {
1816 # Do only magic variables prefixed by SUBST
1817 $mwSubst =& MagicWord::get( MAG_SUBST );
1818 if (!$mwSubst->matchStartAndRemove( $matches[1] ))
1819 $skip = true;
1820 # Note that if we don't substitute the variable below,
1821 # we don't remove the {{subst:}} magic word, in case
1822 # it is a template rather than a magic variable.
1824 if ( !$skip && array_key_exists( $matches[1], $this->mVariables ) ) {
1825 $text = $this->mVariables[$matches[1]];
1826 $this->mOutput->mContainsOldMagic = true;
1827 } else {
1828 $text = $matches[0];
1830 return $text;
1833 # Split template arguments
1834 function getTemplateArgs( $argsString ) {
1835 if ( $argsString === '' ) {
1836 return array();
1839 $args = explode( '|', substr( $argsString, 1 ) );
1841 # If any of the arguments contains a '[[' but no ']]', it needs to be
1842 # merged with the next arg because the '|' character between belongs
1843 # to the link syntax and not the template parameter syntax.
1844 $argc = count($args);
1845 $i = 0;
1846 for ( $i = 0; $i < $argc-1; $i++ ) {
1847 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1848 $args[$i] .= '|'.$args[$i+1];
1849 array_splice($args, $i+1, 1);
1850 $i--;
1851 $argc--;
1855 return $args;
1859 * Return the text of a template, after recursively
1860 * replacing any variables or templates within the template.
1862 * @param array $matches The parts of the template
1863 * $matches[1]: the title, i.e. the part before the |
1864 * $matches[2]: the parameters (including a leading |), if any
1865 * @return string the text of the template
1866 * @access private
1868 function braceSubstitution( $matches ) {
1869 global $wgLinkCache, $wgContLang;
1870 $fname = 'Parser::braceSubstitution';
1871 $found = false;
1872 $nowiki = false;
1873 $noparse = false;
1875 $title = NULL;
1877 # Need to know if the template comes at the start of a line,
1878 # to treat the beginning of the template like the beginning
1879 # of a line for tables and block-level elements.
1880 $linestart = $matches[1];
1882 # $part1 is the bit before the first |, and must contain only title characters
1883 # $args is a list of arguments, starting from index 0, not including $part1
1885 $part1 = $matches[2];
1886 # If the third subpattern matched anything, it will start with |
1888 $args = $this->getTemplateArgs($matches[3]);
1889 $argc = count( $args );
1891 # Don't parse {{{}}} because that's only for template arguments
1892 if ( $linestart === '{' ) {
1893 $text = $matches[0];
1894 $found = true;
1895 $noparse = true;
1898 # SUBST
1899 if ( !$found ) {
1900 $mwSubst =& MagicWord::get( MAG_SUBST );
1901 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
1902 # One of two possibilities is true:
1903 # 1) Found SUBST but not in the PST phase
1904 # 2) Didn't find SUBST and in the PST phase
1905 # In either case, return without further processing
1906 $text = $matches[0];
1907 $found = true;
1908 $noparse = true;
1912 # MSG, MSGNW and INT
1913 if ( !$found ) {
1914 # Check for MSGNW:
1915 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1916 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1917 $nowiki = true;
1918 } else {
1919 # Remove obsolete MSG:
1920 $mwMsg =& MagicWord::get( MAG_MSG );
1921 $mwMsg->matchStartAndRemove( $part1 );
1924 # Check if it is an internal message
1925 $mwInt =& MagicWord::get( MAG_INT );
1926 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1927 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1928 $text = $linestart . wfMsgReal( $part1, $args, true );
1929 $found = true;
1934 # NS
1935 if ( !$found ) {
1936 # Check for NS: (namespace expansion)
1937 $mwNs = MagicWord::get( MAG_NS );
1938 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1939 if ( intval( $part1 ) ) {
1940 $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
1941 $found = true;
1942 } else {
1943 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1944 if ( !is_null( $index ) ) {
1945 $text = $linestart . $wgContLang->getNsText( $index );
1946 $found = true;
1952 # LOCALURL and LOCALURLE
1953 if ( !$found ) {
1954 $mwLocal = MagicWord::get( MAG_LOCALURL );
1955 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1957 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1958 $func = 'getLocalURL';
1959 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1960 $func = 'escapeLocalURL';
1961 } else {
1962 $func = '';
1965 if ( $func !== '' ) {
1966 $title = Title::newFromText( $part1 );
1967 if ( !is_null( $title ) ) {
1968 if ( $argc > 0 ) {
1969 $text = $linestart . $title->$func( $args[0] );
1970 } else {
1971 $text = $linestart . $title->$func();
1973 $found = true;
1978 # GRAMMAR
1979 if ( !$found && $argc == 1 ) {
1980 $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
1981 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1982 $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
1983 $found = true;
1987 # Template table test
1989 # Did we encounter this template already? If yes, it is in the cache
1990 # and we need to check for loops.
1991 if ( !$found && isset( $this->mTemplates[$part1] ) ) {
1992 # set $text to cached message.
1993 $text = $linestart . $this->mTemplates[$part1];
1994 $found = true;
1996 # Infinite loop test
1997 if ( isset( $this->mTemplatePath[$part1] ) ) {
1998 $noparse = true;
1999 $found = true;
2000 $text .= '<!-- WARNING: template loop detected -->';
2004 # Load from database
2005 $itcamefromthedatabase = false;
2006 if ( !$found ) {
2007 $ns = NS_TEMPLATE;
2008 $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
2009 if ($subpage !== '') {
2010 $ns = $this->mTitle->getNamespace();
2012 $title = Title::newFromText( $part1, $ns );
2013 if ( !is_null( $title ) && !$title->isExternal() ) {
2014 # Check for excessive inclusion
2015 $dbk = $title->getPrefixedDBkey();
2016 if ( $this->incrementIncludeCount( $dbk ) ) {
2017 # This should never be reached.
2018 $article = new Article( $title );
2019 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
2020 if ( $articleContent !== false ) {
2021 $found = true;
2022 $text = $linestart . $articleContent;
2023 $itcamefromthedatabase = true;
2027 # If the title is valid but undisplayable, make a link to it
2028 if ( $this->mOutputType == OT_HTML && !$found ) {
2029 $text = $linestart . '[['.$title->getPrefixedText().']]';
2030 $found = true;
2033 # Template cache array insertion
2034 $this->mTemplates[$part1] = $text;
2038 # Recursive parsing, escaping and link table handling
2039 # Only for HTML output
2040 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
2041 $text = wfEscapeWikiText( $text );
2042 } elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
2043 # Clean up argument array
2044 $assocArgs = array();
2045 $index = 1;
2046 foreach( $args as $arg ) {
2047 $eqpos = strpos( $arg, '=' );
2048 if ( $eqpos === false ) {
2049 $assocArgs[$index++] = $arg;
2050 } else {
2051 $name = trim( substr( $arg, 0, $eqpos ) );
2052 $value = trim( substr( $arg, $eqpos+1 ) );
2053 if ( $value === false ) {
2054 $value = '';
2056 if ( $name !== false ) {
2057 $assocArgs[$name] = $value;
2062 # Add a new element to the templace recursion path
2063 $this->mTemplatePath[$part1] = 1;
2065 $text = $this->strip( $text, $this->mStripState );
2066 $text = $this->removeHTMLtags( $text );
2067 $text = $this->replaceVariables( $text, $assocArgs );
2069 # Resume the link cache and register the inclusion as a link
2070 if ( $this->mOutputType == OT_HTML && !is_null( $title ) ) {
2071 $wgLinkCache->addLinkObj( $title );
2074 # If the template begins with a table or block-level
2075 # element, it should be treated as beginning a new line.
2076 if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
2077 $text = "\n" . $text;
2081 # Empties the template path
2082 $this->mTemplatePath = array();
2083 if ( !$found ) {
2084 return $matches[0];
2085 } else {
2086 # replace ==section headers==
2087 # XXX this needs to go away once we have a better parser.
2088 if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
2089 if( !is_null( $title ) )
2090 $encodedname = base64_encode($title->getPrefixedDBkey());
2091 else
2092 $encodedname = base64_encode("");
2093 $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
2094 PREG_SPLIT_DELIM_CAPTURE);
2095 $text = '';
2096 $nsec = 0;
2097 for( $i = 0; $i < count($m); $i += 2 ) {
2098 $text .= $m[$i];
2099 if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
2100 $hl = $m[$i + 1];
2101 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
2102 $text .= $hl;
2103 continue;
2105 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
2106 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
2107 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
2109 $nsec++;
2114 # Empties the template path
2115 $this->mTemplatePath = array();
2117 if ( !$found ) {
2118 return $matches[0];
2119 } else {
2120 return $text;
2125 * Triple brace replacement -- used for template arguments
2126 * @access private
2128 function argSubstitution( $matches ) {
2129 $arg = trim( $matches[1] );
2130 $text = $matches[0];
2131 $inputArgs = end( $this->mArgStack );
2133 if ( array_key_exists( $arg, $inputArgs ) ) {
2134 $text = $inputArgs[$arg];
2137 return $text;
2141 * Returns true if the function is allowed to include this entity
2142 * @access private
2144 function incrementIncludeCount( $dbk ) {
2145 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
2146 $this->mIncludeCount[$dbk] = 0;
2148 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
2149 return true;
2150 } else {
2151 return false;
2157 * Cleans up HTML, removes dangerous tags and attributes, and
2158 * removes HTML comments
2159 * @access private
2161 function removeHTMLtags( $text ) {
2162 global $wgUseTidy, $wgUserHtml;
2163 $fname = 'Parser::removeHTMLtags';
2164 wfProfileIn( $fname );
2166 if( $wgUserHtml ) {
2167 $htmlpairs = array( # Tags that must be closed
2168 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
2169 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
2170 'strike', 'strong', 'tt', 'var', 'div', 'center',
2171 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
2172 'ruby', 'rt' , 'rb' , 'rp', 'p'
2174 $htmlsingle = array(
2175 'br', 'hr', 'li', 'dt', 'dd'
2177 $htmlnest = array( # Tags that can be nested--??
2178 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
2179 'dl', 'font', 'big', 'small', 'sub', 'sup'
2181 $tabletags = array( # Can only appear inside table
2182 'td', 'th', 'tr'
2184 } else {
2185 $htmlpairs = array();
2186 $htmlsingle = array();
2187 $htmlnest = array();
2188 $tabletags = array();
2191 $htmlsingle = array_merge( $tabletags, $htmlsingle );
2192 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
2194 $htmlattrs = $this->getHTMLattrs () ;
2196 # Remove HTML comments
2197 $text = $this->removeHTMLcomments( $text );
2199 $bits = explode( '<', $text );
2200 $text = array_shift( $bits );
2201 if(!$wgUseTidy) {
2202 $tagstack = array(); $tablestack = array();
2203 foreach ( $bits as $x ) {
2204 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
2205 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2206 $x, $regs );
2207 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2208 error_reporting( $prev );
2210 $badtag = 0 ;
2211 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2212 # Check our stack
2213 if ( $slash ) {
2214 # Closing a tag...
2215 if ( ! in_array( $t, $htmlsingle ) &&
2216 ( $ot = @array_pop( $tagstack ) ) != $t ) {
2217 @array_push( $tagstack, $ot );
2218 $badtag = 1;
2219 } else {
2220 if ( $t == 'table' ) {
2221 $tagstack = array_pop( $tablestack );
2223 $newparams = '';
2225 } else {
2226 # Keep track for later
2227 if ( in_array( $t, $tabletags ) &&
2228 ! in_array( 'table', $tagstack ) ) {
2229 $badtag = 1;
2230 } else if ( in_array( $t, $tagstack ) &&
2231 ! in_array ( $t , $htmlnest ) ) {
2232 $badtag = 1 ;
2233 } else if ( ! in_array( $t, $htmlsingle ) ) {
2234 if ( $t == 'table' ) {
2235 array_push( $tablestack, $tagstack );
2236 $tagstack = array();
2238 array_push( $tagstack, $t );
2240 # Strip non-approved attributes from the tag
2241 $newparams = $this->fixTagAttributes($params);
2244 if ( ! $badtag ) {
2245 $rest = str_replace( '>', '&gt;', $rest );
2246 $text .= "<$slash$t $newparams$brace$rest";
2247 continue;
2250 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2252 # Close off any remaining tags
2253 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2254 $text .= "</$t>\n";
2255 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2257 } else {
2258 # this might be possible using tidy itself
2259 foreach ( $bits as $x ) {
2260 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2261 $x, $regs );
2262 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2263 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2264 $newparams = $this->fixTagAttributes($params);
2265 $rest = str_replace( '>', '&gt;', $rest );
2266 $text .= "<$slash$t $newparams$brace$rest";
2267 } else {
2268 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2272 wfProfileOut( $fname );
2273 return $text;
2277 * Remove '<!--', '-->', and everything between.
2278 * To avoid leaving blank lines, when a comment is both preceded
2279 * and followed by a newline (ignoring spaces), trim leading and
2280 * trailing spaces and one of the newlines.
2282 * @access private
2284 function removeHTMLcomments( $text ) {
2285 $fname='Parser::removeHTMLcomments';
2286 wfProfileIn( $fname );
2287 while (($start = strpos($text, '<!--')) !== false) {
2288 $end = strpos($text, '-->', $start + 4);
2289 if ($end === false) {
2290 # Unterminated comment; bail out
2291 break;
2294 $end += 3;
2296 # Trim space and newline if the comment is both
2297 # preceded and followed by a newline
2298 $spaceStart = max($start - 1, 0);
2299 $spaceLen = $end - $spaceStart;
2300 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2301 $spaceStart--;
2302 $spaceLen++;
2304 while (substr($text, $spaceStart + $spaceLen, 1) === ' ')
2305 $spaceLen++;
2306 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") {
2307 # Remove the comment, leading and trailing
2308 # spaces, and leave only one newline.
2309 $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1);
2311 else {
2312 # Remove just the comment.
2313 $text = substr_replace($text, '', $start, $end - $start);
2316 wfProfileOut( $fname );
2317 return $text;
2321 * This function accomplishes several tasks:
2322 * 1) Auto-number headings if that option is enabled
2323 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2324 * 3) Add a Table of contents on the top for users who have enabled the option
2325 * 4) Auto-anchor headings
2327 * It loops through all headlines, collects the necessary data, then splits up the
2328 * string and re-inserts the newly formatted headlines.
2329 * @access private
2331 /* private */ function formatHeadings( $text, $isMain=true ) {
2332 global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders;
2334 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2335 $doShowToc = $this->mOptions->getShowToc();
2336 $forceTocHere = false;
2337 if( !$this->mTitle->userCanEdit() ) {
2338 $showEditLink = 0;
2339 $rightClickHack = 0;
2340 } else {
2341 $showEditLink = $this->mOptions->getEditSection();
2342 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2345 # Inhibit editsection links if requested in the page
2346 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2347 if( $esw->matchAndRemove( $text ) ) {
2348 $showEditLink = 0;
2350 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2351 # do not add TOC
2352 $mw =& MagicWord::get( MAG_NOTOC );
2353 if( $mw->matchAndRemove( $text ) ) {
2354 $doShowToc = 0;
2357 # never add the TOC to the Main Page. This is an entry page that should not
2358 # be more than 1-2 screens large anyway
2359 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2360 $doShowToc = 0;
2363 # Get all headlines for numbering them and adding funky stuff like [edit]
2364 # links - this is for later, but we need the number of headlines right now
2365 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2367 # if there are fewer than 4 headlines in the article, do not show TOC
2368 if( $numMatches < 4 ) {
2369 $doShowToc = 0;
2372 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2373 # override above conditions and always show TOC at that place
2374 $mw =& MagicWord::get( MAG_TOC );
2375 if ($mw->match( $text ) ) {
2376 $doShowToc = 1;
2377 $forceTocHere = true;
2378 } else {
2379 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2380 # override above conditions and always show TOC above first header
2381 $mw =& MagicWord::get( MAG_FORCETOC );
2382 if ($mw->matchAndRemove( $text ) ) {
2383 $doShowToc = 1;
2389 # We need this to perform operations on the HTML
2390 $sk =& $this->mOptions->getSkin();
2392 # headline counter
2393 $headlineCount = 0;
2394 $sectionCount = 0; # headlineCount excluding template sections
2396 # Ugh .. the TOC should have neat indentation levels which can be
2397 # passed to the skin functions. These are determined here
2398 $toclevel = 0;
2399 $toc = '';
2400 $full = '';
2401 $head = array();
2402 $sublevelCount = array();
2403 $level = 0;
2404 $prevlevel = 0;
2405 foreach( $matches[3] as $headline ) {
2406 $istemplate = 0;
2407 $templatetitle = "";
2408 $templatesection = 0;
2410 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2411 $istemplate = 1;
2412 $templatetitle = base64_decode($mat[1]);
2413 $templatesection = 1 + (int)base64_decode($mat[2]);
2414 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2417 $numbering = '';
2418 if( $level ) {
2419 $prevlevel = $level;
2421 $level = $matches[1][$headlineCount];
2422 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2423 # reset when we enter a new level
2424 $sublevelCount[$level] = 0;
2425 $toc .= $sk->tocIndent( $level - $prevlevel );
2426 $toclevel += $level - $prevlevel;
2428 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2429 # reset when we step back a level
2430 $sublevelCount[$level+1]=0;
2431 $toc .= $sk->tocUnindent( $prevlevel - $level );
2432 $toclevel -= $prevlevel - $level;
2434 # count number of headlines for each level
2435 @$sublevelCount[$level]++;
2436 if( $doNumberHeadings || $doShowToc ) {
2437 $dot = 0;
2438 for( $i = 1; $i <= $level; $i++ ) {
2439 if( !empty( $sublevelCount[$i] ) ) {
2440 if( $dot ) {
2441 $numbering .= '.';
2443 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
2444 $dot = 1;
2449 # The canonized header is a version of the header text safe to use for links
2450 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2451 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2452 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2454 # Remove link placeholders by the link text.
2455 # <!--LINK number-->
2456 # turns into
2457 # link text with suffix
2458 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2459 "\$wgLinkHolders['texts'][\$1]",
2460 $canonized_headline );
2462 # strip out HTML
2463 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2464 $tocline = trim( $canonized_headline );
2465 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2466 $replacearray = array(
2467 '%3A' => ':',
2468 '%' => '.'
2470 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2471 $refer[$headlineCount] = $canonized_headline;
2473 # count how many in assoc. array so we can track dupes in anchors
2474 @$refers[$canonized_headline]++;
2475 $refcount[$headlineCount]=$refers[$canonized_headline];
2477 # Prepend the number to the heading text
2479 if( $doNumberHeadings || $doShowToc ) {
2480 $tocline = $numbering . ' ' . $tocline;
2482 # Don't number the heading if it is the only one (looks silly)
2483 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2484 # the two are different if the line contains a link
2485 $headline=$numbering . ' ' . $headline;
2489 # Create the anchor for linking from the TOC to the section
2490 $anchor = $canonized_headline;
2491 if($refcount[$headlineCount] > 1 ) {
2492 $anchor .= '_' . $refcount[$headlineCount];
2494 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2495 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2497 if( $showEditLink && ( !$istemplate || $templatetitle !== "" ) ) {
2498 if ( empty( $head[$headlineCount] ) ) {
2499 $head[$headlineCount] = '';
2501 if( $istemplate )
2502 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2503 else
2504 $head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1);
2507 # Add the edit section span
2508 if( $rightClickHack ) {
2509 if( $istemplate )
2510 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2511 else
2512 $headline = $sk->editSectionScript($this->mTitle, $sectionCount+1,$headline);
2515 # give headline the correct <h#> tag
2516 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2518 $headlineCount++;
2519 if( !$istemplate )
2520 $sectionCount++;
2523 if( $doShowToc ) {
2524 $toclines = $headlineCount;
2525 $toc .= $sk->tocUnindent( $toclevel );
2526 $toc = $sk->tocTable( $toc );
2529 # split up and insert constructed headlines
2531 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2532 $i = 0;
2534 foreach( $blocks as $block ) {
2535 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2536 # This is the [edit] link that appears for the top block of text when
2537 # section editing is enabled
2539 # Disabled because it broke block formatting
2540 # For example, a bullet point in the top line
2541 # $full .= $sk->editSectionLink(0);
2543 $full .= $block;
2544 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2545 # Top anchor now in skin
2546 $full = $full.$toc;
2549 if( !empty( $head[$i] ) ) {
2550 $full .= $head[$i];
2552 $i++;
2554 if($forceTocHere) {
2555 $mw =& MagicWord::get( MAG_TOC );
2556 return $mw->replace( $toc, $full );
2557 } else {
2558 return $full;
2563 * Return an HTML link for the "ISBN 123456" text
2564 * @access private
2566 function magicISBN( $text ) {
2567 global $wgLang;
2568 $fname = 'Parser::magicISBN';
2569 wfProfileIn( $fname );
2571 $a = split( 'ISBN ', ' '.$text );
2572 if ( count ( $a ) < 2 ) {
2573 wfProfileOut( $fname );
2574 return $text;
2576 $text = substr( array_shift( $a ), 1);
2577 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2579 foreach ( $a as $x ) {
2580 $isbn = $blank = '' ;
2581 while ( ' ' == $x{0} ) {
2582 $blank .= ' ';
2583 $x = substr( $x, 1 );
2585 if ( $x == '' ) { # blank isbn
2586 $text .= "ISBN $blank";
2587 continue;
2589 while ( strstr( $valid, $x{0} ) != false ) {
2590 $isbn .= $x{0};
2591 $x = substr( $x, 1 );
2593 $num = str_replace( '-', '', $isbn );
2594 $num = str_replace( ' ', '', $num );
2596 if ( '' == $num ) {
2597 $text .= "ISBN $blank$x";
2598 } else {
2599 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2600 $text .= '<a href="' .
2601 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2602 "\" class=\"internal\">ISBN $isbn</a>";
2603 $text .= $x;
2606 wfProfileOut( $fname );
2607 return $text;
2611 * Return an HTML link for the "GEO ..." text
2612 * @access private
2614 function magicGEO( $text ) {
2615 global $wgLang, $wgUseGeoMode;
2616 $fname = 'Parser::magicGEO';
2617 wfProfileIn( $fname );
2619 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2620 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2621 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2622 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2623 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2624 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2626 $a = split( 'GEO ', ' '.$text );
2627 if ( count ( $a ) < 2 ) {
2628 wfProfileOut( $fname );
2629 return $text;
2631 $text = substr( array_shift( $a ), 1);
2632 $valid = '0123456789.+-:';
2634 foreach ( $a as $x ) {
2635 $geo = $blank = '' ;
2636 while ( ' ' == $x{0} ) {
2637 $blank .= ' ';
2638 $x = substr( $x, 1 );
2640 while ( strstr( $valid, $x{0} ) != false ) {
2641 $geo .= $x{0};
2642 $x = substr( $x, 1 );
2644 $num = str_replace( '+', '', $geo );
2645 $num = str_replace( ' ', '', $num );
2647 if ( '' == $num || count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2648 $text .= "GEO $blank$x";
2649 } else {
2650 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2651 $text .= '<a href="' .
2652 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2653 "\" class=\"internal\">GEO $geo</a>";
2654 $text .= $x;
2657 wfProfileOut( $fname );
2658 return $text;
2662 * Return an HTML link for the "RFC 1234" text
2663 * @access private
2664 * @param string $text text to be processed
2666 function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl' ) {
2667 global $wgLang;
2669 $valid = '0123456789';
2670 $internal = false;
2672 $a = split( $keyword, ' '.$text );
2673 if ( count ( $a ) < 2 ) {
2674 return $text;
2676 $text = substr( array_shift( $a ), 1);
2678 /* Check if keyword is preceed by [[.
2679 * This test is made here cause of the array_shift above
2680 * that prevent the test to be done in the foreach.
2682 if ( substr( $text, -2 ) == '[[' ) {
2683 $internal = true;
2686 foreach ( $a as $x ) {
2687 /* token might be empty if we have RFC RFC 1234 */
2688 if ( $x=='' ) {
2689 $text.=$keyword;
2690 continue;
2693 $id = $blank = '' ;
2695 /** remove and save whitespaces in $blank */
2696 while ( $x{0} == ' ' ) {
2697 $blank .= ' ';
2698 $x = substr( $x, 1 );
2701 /** remove and save the rfc number in $id */
2702 while ( strstr( $valid, $x{0} ) != false ) {
2703 $id .= $x{0};
2704 $x = substr( $x, 1 );
2707 if ( $id == '' ) {
2708 /* call back stripped spaces*/
2709 $text .= $keyword.$blank.$x;
2710 } elseif( $internal ) {
2711 /* normal link */
2712 $text .= $keyword.$id.$x;
2713 } else {
2714 /* build the external link*/
2715 $url = wfmsg( $urlmsg );
2716 $url = str_replace( '$1', $id, $url);
2717 $sk =& $this->mOptions->getSkin();
2718 $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
2719 $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
2722 /* Check if the next RFC keyword is preceed by [[ */
2723 $internal = ( substr($x,-2) == '[[' );
2725 return $text;
2729 * Transform wiki markup when saving a page by doing \r\n -> \n
2730 * conversion, substitting signatures, {{subst:}} templates, etc.
2732 * @param string $text the text to transform
2733 * @param Title &$title the Title object for the current article
2734 * @param User &$user the User object describing the current user
2735 * @param ParserOptions $options parsing options
2736 * @param bool $clearState whether to clear the parser state first
2737 * @return string the altered wiki markup
2738 * @access public
2740 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2741 $this->mOptions = $options;
2742 $this->mTitle =& $title;
2743 $this->mOutputType = OT_WIKI;
2745 if ( $clearState ) {
2746 $this->clearState();
2749 $stripState = false;
2750 $pairs = array(
2751 "\r\n" => "\n",
2753 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
2754 $text = $this->strip( $text, $stripState, false );
2755 $text = $this->pstPass2( $text, $user );
2756 $text = $this->unstrip( $text, $stripState );
2757 $text = $this->unstripNoWiki( $text, $stripState );
2758 return $text;
2762 * Pre-save transform helper function
2763 * @access private
2765 function pstPass2( $text, &$user ) {
2766 global $wgLang, $wgContLang, $wgLocaltimezone;
2768 # Variable replacement
2769 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2770 $text = $this->replaceVariables( $text );
2772 # Signatures
2774 $n = $user->getName();
2775 $k = $user->getOption( 'nickname' );
2776 if ( '' == $k ) { $k = $n; }
2777 if ( isset( $wgLocaltimezone ) ) {
2778 $oldtz = getenv( 'TZ' );
2779 putenv( 'TZ='.$wgLocaltimezone );
2781 /* Note: this is an ugly timezone hack for the European wikis */
2782 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
2783 ' (' . date( 'T' ) . ')';
2784 if ( isset( $wgLocaltimezone ) ) {
2785 putenv( 'TZ='.$oldtzs );
2788 $text = preg_replace( '/~~~~~~/', $d, $text );
2789 $text = preg_replace( '/~~~~/', '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]] $d", $text );
2790 $text = preg_replace( '/~~~/', '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]]", $text );
2792 # Context links: [[|name]] and [[name (context)|]]
2794 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2795 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2796 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2797 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2799 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2800 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2801 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2802 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2803 $context = '';
2804 $t = $this->mTitle->getText();
2805 if ( preg_match( $conpat, $t, $m ) ) {
2806 $context = $m[2];
2808 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2809 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2810 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2812 if ( '' == $context ) {
2813 $text = preg_replace( $p2, '[[\\1]]', $text );
2814 } else {
2815 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2818 # Trim trailing whitespace
2819 # MAG_END (__END__) tag allows for trailing
2820 # whitespace to be deliberately included
2821 $text = rtrim( $text );
2822 $mw =& MagicWord::get( MAG_END );
2823 $mw->matchAndRemove( $text );
2825 return $text;
2829 * Set up some variables which are usually set up in parse()
2830 * so that an external function can call some class members with confidence
2831 * @access public
2833 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2834 $this->mTitle =& $title;
2835 $this->mOptions = $options;
2836 $this->mOutputType = $outputType;
2837 if ( $clearState ) {
2838 $this->clearState();
2843 * Transform a MediaWiki message by replacing magic variables.
2845 * @param string $text the text to transform
2846 * @param ParserOptions $options options
2847 * @return string the text with variables substituted
2848 * @access public
2850 function transformMsg( $text, $options ) {
2851 global $wgTitle;
2852 static $executing = false;
2854 # Guard against infinite recursion
2855 if ( $executing ) {
2856 return $text;
2858 $executing = true;
2860 $this->mTitle = $wgTitle;
2861 $this->mOptions = $options;
2862 $this->mOutputType = OT_MSG;
2863 $this->clearState();
2864 $text = $this->replaceVariables( $text );
2866 $executing = false;
2867 return $text;
2871 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2872 * Callback will be called with the text within
2873 * Transform and return the text within
2874 * @access public
2876 function setHook( $tag, $callback ) {
2877 $oldVal = @$this->mTagHooks[$tag];
2878 $this->mTagHooks[$tag] = $callback;
2879 return $oldVal;
2883 * Replace <!--LINK--> link placeholders with actual links, in the buffer
2884 * Placeholders created in Skin::makeLinkObj()
2885 * Returns an array of links found, indexed by PDBK:
2886 * 0 - broken
2887 * 1 - normal link
2888 * 2 - stub
2889 * $options is a bit field, RLH_FOR_UPDATE to select for update
2891 function replaceLinkHolders( &$text, $options = 0 ) {
2892 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck, $wgLinkHolders;
2893 global $wgInterwikiLinkHolders;
2894 global $outputReplace;
2896 if ( $wgUseOldExistenceCheck ) {
2897 return array();
2900 $fname = 'Parser::replaceLinkHolders';
2901 wfProfileIn( $fname );
2903 $pdbks = array();
2904 $colours = array();
2906 #if ( !empty( $tmpLinks[0] ) ) { #TODO
2907 if ( !empty( $wgLinkHolders['namespaces'] ) ) {
2908 wfProfileIn( $fname.'-check' );
2909 $dbr =& wfGetDB( DB_SLAVE );
2910 $cur = $dbr->tableName( 'cur' );
2911 $sk = $wgUser->getSkin();
2912 $threshold = $wgUser->getOption('stubthreshold');
2914 # Sort by namespace
2915 asort( $wgLinkHolders['namespaces'] );
2917 # Generate query
2918 $query = false;
2919 foreach ( $wgLinkHolders['namespaces'] as $key => $val ) {
2920 # Make title object
2921 $title = $wgLinkHolders['titles'][$key];
2923 # Skip invalid entries.
2924 # Result will be ugly, but prevents crash.
2925 if ( is_null( $title ) ) {
2926 continue;
2928 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
2930 # Check if it's in the link cache already
2931 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
2932 $colours[$pdbk] = 1;
2933 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
2934 $colours[$pdbk] = 0;
2935 } else {
2936 # Not in the link cache, add it to the query
2937 if ( !isset( $current ) ) {
2938 $current = $val;
2939 $query = "SELECT cur_id, cur_namespace, cur_title";
2940 if ( $threshold > 0 ) {
2941 $query .= ", LENGTH(cur_text) AS cur_len, cur_is_redirect";
2943 $query .= " FROM $cur WHERE (cur_namespace=$val AND cur_title IN(";
2944 } elseif ( $current != $val ) {
2945 $current = $val;
2946 $query .= ")) OR (cur_namespace=$val AND cur_title IN(";
2947 } else {
2948 $query .= ', ';
2951 $query .= $dbr->addQuotes( $wgLinkHolders['dbkeys'][$key] );
2954 if ( $query ) {
2955 $query .= '))';
2956 if ( $options & RLH_FOR_UPDATE ) {
2957 $query .= ' FOR UPDATE';
2960 $res = $dbr->query( $query, $fname );
2962 # Fetch data and form into an associative array
2963 # non-existent = broken
2964 # 1 = known
2965 # 2 = stub
2966 while ( $s = $dbr->fetchObject($res) ) {
2967 $title = Title::makeTitle( $s->cur_namespace, $s->cur_title );
2968 $pdbk = $title->getPrefixedDBkey();
2969 $wgLinkCache->addGoodLink( $s->cur_id, $pdbk );
2971 if ( $threshold > 0 ) {
2972 $size = $s->cur_len;
2973 if ( $s->cur_is_redirect || $s->cur_namespace != 0 || $length < $threshold ) {
2974 $colours[$pdbk] = 1;
2975 } else {
2976 $colours[$pdbk] = 2;
2978 } else {
2979 $colours[$pdbk] = 1;
2983 wfProfileOut( $fname.'-check' );
2985 # Construct search and replace arrays
2986 wfProfileIn( $fname.'-construct' );
2987 $outputReplace = array();
2988 foreach ( $wgLinkHolders['namespaces'] as $key => $ns ) {
2989 $pdbk = $pdbks[$key];
2990 $searchkey = '<!--LINK '.$key.'-->';
2991 $title = $wgLinkHolders['titles'][$key];
2992 if ( empty( $colours[$pdbk] ) ) {
2993 $wgLinkCache->addBadLink( $pdbk );
2994 $colours[$pdbk] = 0;
2995 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
2996 $wgLinkHolders['texts'][$key],
2997 $wgLinkHolders['queries'][$key] );
2998 } elseif ( $colours[$pdbk] == 1 ) {
2999 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
3000 $wgLinkHolders['texts'][$key],
3001 $wgLinkHolders['queries'][$key] );
3002 } elseif ( $colours[$pdbk] == 2 ) {
3003 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
3004 $wgLinkHolders['texts'][$key],
3005 $wgLinkHolders['queries'][$key] );
3008 wfProfileOut( $fname.'-construct' );
3010 # Do the thing
3011 wfProfileIn( $fname.'-replace' );
3013 $text = preg_replace_callback(
3014 '/(<!--LINK .*?-->)/',
3015 "outputReplaceMatches",
3016 $text);
3017 wfProfileOut( $fname.'-replace' );
3020 if ( !empty( $wgInterwikiLinkHolders ) ) {
3021 wfProfileIn( $fname.'-interwiki' );
3022 $outputReplace = $wgInterwikiLinkHolders;
3023 $text = preg_replace_callback(
3024 '/<!--IWLINK (.*?)-->/',
3025 "outputReplaceMatches",
3026 $text );
3027 wfProfileOut( $fname.'-interwiki' );
3030 wfProfileOut( $fname );
3031 return $colours;
3035 * Renders an image gallery from a text with one line per image.
3036 * text labels may be given by using |-style alternative text. E.g.
3037 * Image:one.jpg|The number "1"
3038 * Image:tree.jpg|A tree
3039 * given as text will return the HTML of a gallery with two images,
3040 * labeled 'The number "1"' and
3041 * 'A tree'.
3043 function renderImageGallery( $text ) {
3044 global $wgLinkCache;
3045 $ig = new ImageGallery();
3046 $ig->setShowBytes( false );
3047 $ig->setShowFilename( false );
3048 $lines = explode( "\n", $text );
3050 foreach ( $lines as $line ) {
3051 # match lines like these:
3052 # Image:someimage.jpg|This is some image
3053 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
3054 # Skip empty lines
3055 if ( count( $matches ) == 0 ) {
3056 continue;
3058 $nt = Title::newFromURL( $matches[1] );
3059 if ( isset( $matches[3] ) ) {
3060 $label = $matches[3];
3061 } else {
3062 $label = '';
3064 $ig->add( Image::newFromTitle( $nt ), $label );
3065 $wgLinkCache->addImageLinkObj( $nt );
3067 return $ig->toHTML();
3072 * @todo document
3073 * @package MediaWiki
3075 class ParserOutput
3077 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
3078 var $mCacheTime; # Used in ParserCache
3079 var $mVersion; # Compatibility check
3081 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
3082 $containsOldMagic = false )
3084 $this->mText = $text;
3085 $this->mLanguageLinks = $languageLinks;
3086 $this->mCategoryLinks = $categoryLinks;
3087 $this->mContainsOldMagic = $containsOldMagic;
3088 $this->mCacheTime = '';
3089 $this->mVersion = MW_PARSER_VERSION;
3092 function getText() { return $this->mText; }
3093 function getLanguageLinks() { return $this->mLanguageLinks; }
3094 function getCategoryLinks() { return array_keys( $this->mCategoryLinks ); }
3095 function getCacheTime() { return $this->mCacheTime; }
3096 function containsOldMagic() { return $this->mContainsOldMagic; }
3097 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
3098 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
3099 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
3100 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
3101 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
3102 function addCategoryLink( $c ) { $this->mCategoryLinks[$c] = 1; }
3104 function merge( $other ) {
3105 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
3106 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
3107 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
3111 * Return true if this cached output object predates the global or
3112 * per-article cache invalidation timestamps, or if it comes from
3113 * an incompatible older version.
3115 * @param string $touched the affected article's last touched timestamp
3116 * @return bool
3117 * @access public
3119 function expired( $touched ) {
3120 global $wgCacheEpoch;
3121 return $this->getCacheTime() <= $touched ||
3122 $this->getCacheTime() <= $wgCacheEpoch ||
3123 !isset( $this->mVersion ) ||
3124 version_compare( $this->mVersion, MW_PARSER_VERSION, "lt" );
3129 * Set options of the Parser
3130 * @todo document
3131 * @package MediaWiki
3133 class ParserOptions
3135 # All variables are private
3136 var $mUseTeX; # Use texvc to expand <math> tags
3137 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
3138 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
3139 var $mAllowExternalImages; # Allow external images inline
3140 var $mSkin; # Reference to the preferred skin
3141 var $mDateFormat; # Date format index
3142 var $mEditSection; # Create "edit section" links
3143 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
3144 var $mNumberHeadings; # Automatically number headings
3145 var $mShowToc; # Show table of contents
3147 function getUseTeX() { return $this->mUseTeX; }
3148 function getUseDynamicDates() { return $this->mUseDynamicDates; }
3149 function getInterwikiMagic() { return $this->mInterwikiMagic; }
3150 function getAllowExternalImages() { return $this->mAllowExternalImages; }
3151 function getSkin() { return $this->mSkin; }
3152 function getDateFormat() { return $this->mDateFormat; }
3153 function getEditSection() { return $this->mEditSection; }
3154 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
3155 function getNumberHeadings() { return $this->mNumberHeadings; }
3156 function getShowToc() { return $this->mShowToc; }
3158 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
3159 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
3160 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
3161 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
3162 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
3163 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
3164 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
3165 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
3166 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
3168 function setSkin( &$x ) { $this->mSkin =& $x; }
3170 # Get parser options
3171 /* static */ function newFromUser( &$user ) {
3172 $popts = new ParserOptions;
3173 $popts->initialiseFromUser( $user );
3174 return $popts;
3177 # Get user options
3178 function initialiseFromUser( &$userInput ) {
3179 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
3180 $fname = 'ParserOptions::initialiseFromUser';
3181 wfProfileIn( $fname );
3182 if ( !$userInput ) {
3183 $user = new User;
3184 $user->setLoaded( true );
3185 } else {
3186 $user =& $userInput;
3189 $this->mUseTeX = $wgUseTeX;
3190 $this->mUseDynamicDates = $wgUseDynamicDates;
3191 $this->mInterwikiMagic = $wgInterwikiMagic;
3192 $this->mAllowExternalImages = $wgAllowExternalImages;
3193 wfProfileIn( $fname.'-skin' );
3194 $this->mSkin =& $user->getSkin();
3195 wfProfileOut( $fname.'-skin' );
3196 $this->mDateFormat = $user->getOption( 'date' );
3197 $this->mEditSection = $user->getOption( 'editsection' );
3198 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
3199 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
3200 $this->mShowToc = $user->getOption( 'showtoc' );
3201 wfProfileOut( $fname );
3208 * Callback function used by Parser::replaceLinkHolders()
3209 * to substitute link placeholders.
3211 function &outputReplaceMatches( $matches ) {
3212 global $outputReplace;
3213 return $outputReplace[$matches[1]];
3217 * Return the total number of articles
3219 function wfNumberOfArticles() {
3220 global $wgNumberOfArticles;
3222 wfLoadSiteStats();
3223 return $wgNumberOfArticles;
3227 * Get various statistics from the database
3228 * @private
3230 function wfLoadSiteStats() {
3231 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
3232 $fname = 'wfLoadSiteStats';
3234 if ( -1 != $wgNumberOfArticles ) return;
3235 $dbr =& wfGetDB( DB_SLAVE );
3236 $s = $dbr->selectRow( 'site_stats',
3237 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
3238 array( 'ss_row_id' => 1 ), $fname
3241 if ( $s === false ) {
3242 return;
3243 } else {
3244 $wgTotalViews = $s->ss_total_views;
3245 $wgTotalEdits = $s->ss_total_edits;
3246 $wgNumberOfArticles = $s->ss_good_articles;
3250 function wfEscapeHTMLTagsOnly( $in ) {
3251 return str_replace(
3252 array( '"', '>', '<' ),
3253 array( '&quot;', '&gt;', '&lt;' ),
3254 $in );