4 * File for Parser and related classes
10 * Update this version number when the ParserOutput format
11 * changes in an incompatible way, so the parser cache
12 * can automatically discard old data.
14 define( 'MW_PARSER_VERSION', '1.4.0' );
17 * Variable substitution O(N^2) attack
19 * Without countermeasures, it would be possible to attack the parser by saving
20 * a page filled with a large number of inclusions of large pages. The size of
21 * the generated page would be proportional to the square of the input size.
22 * Hence, we limit the number of inclusions of any given page, thus bringing any
23 * attack back to O(N).
26 define( 'MAX_INCLUDE_REPEAT', 100 );
27 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
29 define( 'RLH_FOR_UPDATE', 1 );
31 # Allowed values for $mOutputType
32 define( 'OT_HTML', 1 );
33 define( 'OT_WIKI', 2 );
34 define( 'OT_MSG' , 3 );
36 # string parameter for extractTags which will cause it
37 # to strip HTML comments in addition to regular
38 # <XML>-style tags. This should not be anything we
39 # may want to use in wikisyntax
40 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
42 # prefix for escaping, used in two functions at least
43 define( 'UNIQ_PREFIX', 'NaodW29');
45 # Constants needed for external link processing
46 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
47 define( 'HTTP_PROTOCOLS', 'http|https' );
48 # Everything except bracket, space, or control characters
49 define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
51 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
52 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
53 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
54 define( 'EXT_LINK_BRACKETED', '/\[(\b('.URL_PROTOCOLS
.'):'.EXT_LINK_URL_CLASS
.'+) *('.EXT_LINK_TEXT_CLASS
.'*?)\]/S' );
55 define( 'EXT_IMAGE_REGEX',
56 '/^('.HTTP_PROTOCOLS
.':)'. # Protocol
57 '('.EXT_LINK_URL_CLASS
.'+)\\/'. # Hostname and path
58 '('.EXT_IMAGE_FNAME_CLASS
.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS
.')$/S' # Filename
64 * Processes wiki markup
67 * There are three main entry points into the Parser class:
69 * produces HTML output
71 * produces altered wiki markup.
73 * performs brace substitution on MediaWiki messages
76 * objects: $wgLang, $wgDateFormatter, $wgLinkCache
78 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
81 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
82 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
85 * * only within ParserOptions
98 # Cleared with clearState():
99 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
100 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
103 var $mOptions, $mTitle, $mOutputType,
104 $mTemplates, // cache of already loaded templates, avoids
105 // multiple SQL queries for the same string
106 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
107 // in this path. Used for loop detection.
117 $this->mTemplates
= array();
118 $this->mTemplatePath
= array();
119 $this->mTagHooks
= array();
128 function clearState() {
129 $this->mOutput
= new ParserOutput
;
130 $this->mAutonumber
= 0;
131 $this->mLastSection
= "";
132 $this->mDTopen
= false;
133 $this->mVariables
= false;
134 $this->mIncludeCount
= array();
135 $this->mStripState
= array();
136 $this->mArgStack
= array();
137 $this->mInPre
= false;
141 * First pass--just handle <nowiki> sections, pass the rest off
142 * to internalParse() which does all the real work.
145 * @return ParserOutput a ParserOutput
147 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
148 global $wgUseTidy, $wgContLang;
149 $fname = 'Parser::parse';
150 wfProfileIn( $fname );
156 $this->mOptions
= $options;
157 $this->mTitle
=& $title;
158 $this->mOutputType
= OT_HTML
;
161 global $fnord; $fnord = 1;
162 //$text = $this->strip( $text, $this->mStripState );
163 // VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
164 $x =& $this->mStripState
;
165 $text = $this->strip( $text, $x );
167 $text = $this->internalParse( $text, $linestart );
168 $text = $this->unstrip( $text, $this->mStripState
);
169 # Clean up special characters, only run once, next-to-last before doBlockLevels
172 # french spaces, last one Guillemet-left
173 # only if there is something before the space
174 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
175 # french spaces, Guillemet-right
176 "/(\\302\\253) /i"=>"\\1 ",
177 '/<hr *>/i' => '<hr />',
178 '/<br *>/i' => '<br />',
179 '/<center *>/i' => '<div class="center">',
180 '/<\\/center *>/i' => '</div>',
181 # Clean up spare ampersands; note that we probably ought to be
182 # more careful about named entities.
183 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
185 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
188 # french spaces, last one Guillemet-left
189 '/ (\\?|:|;|!|\\302\\273)/i' => ' \\1',
190 # french spaces, Guillemet-right
191 '/(\\302\\253) /i' => '\\1 ',
192 '/<center *>/i' => '<div class="center">',
193 '/<\\/center *>/i' => '</div>'
195 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
198 $text = $this->doBlockLevels( $text, $linestart );
200 $this->replaceLinkHolders( $text );
201 $text = $wgContLang->convert($text);
203 $text = $this->unstripNoWiki( $text, $this->mStripState
);
206 $text = Parser
::tidy($text);
209 $this->mOutput
->setText( $text );
210 wfProfileOut( $fname );
211 return $this->mOutput
;
215 * Get a random string
220 function getRandomString() {
221 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
225 * Replaces all occurrences of <$tag>content</$tag> in the text
226 * with a random marker and returns the new text. the output parameter
227 * $content will be an associative array filled with data on the form
228 * $unique_marker => content.
230 * If $content is already set, the additional entries will be appended
231 * If $tag is set to STRIP_COMMENTS, the function will extract
232 * <!-- HTML comments -->
237 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
238 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
245 while ( '' != $text ) {
246 if($tag==STRIP_COMMENTS
) {
247 $p = preg_split( '/<!--/i', $text, 2 );
249 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
252 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
255 if($tag==STRIP_COMMENTS
) {
256 $q = preg_split( '/-->/i', $p[1], 2 );
258 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
260 $marker = $rnd . sprintf('%08X', $n++
);
261 $content[$marker] = $q[0];
262 $stripped .= $marker;
270 * Strips and renders nowiki, pre, math, hiero
271 * If $render is set, performs necessary rendering operations on plugins
272 * Returns the text, and fills an array with data needed in unstrip()
273 * If the $state is already a valid strip state, it adds to the state
275 * @param bool $stripcomments when set, HTML comments <!-- like this -->
276 * will be stripped in addition to other tags. This is important
277 * for section editing, where these comments cause confusion when
278 * counting the sections in the wikisource
282 function strip( $text, &$state, $stripcomments = false ) {
283 $render = ($this->mOutputType
== OT_HTML
);
284 $html_content = array();
285 $nowiki_content = array();
286 $math_content = array();
287 $pre_content = array();
288 $comment_content = array();
289 $ext_content = array();
290 $gallery_content = array();
292 # Replace any instances of the placeholders
293 $uniq_prefix = UNIQ_PREFIX
;
294 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
297 global $wgRawHtml, $wgWhitelistEdit;
298 if( $wgRawHtml && $wgWhitelistEdit ) {
299 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
300 foreach( $html_content as $marker => $content ) {
302 # Raw and unchecked for validity.
303 $html_content[$marker] = $content;
305 $html_content[$marker] = '<html>'.$content.'</html>';
311 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
312 foreach( $nowiki_content as $marker => $content ) {
314 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
316 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
321 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
322 foreach( $math_content as $marker => $content ){
324 if( $this->mOptions
->getUseTeX() ) {
325 $math_content[$marker] = renderMath( $content );
327 $math_content[$marker] = '<math>'.$content.'<math>';
330 $math_content[$marker] = '<math>'.$content.'</math>';
335 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
336 foreach( $pre_content as $marker => $content ){
338 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
340 $pre_content[$marker] = '<pre>'.$content.'</pre>';
345 $text = Parser
::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
346 foreach( $gallery_content as $marker => $content ) {
347 require_once( 'ImageGallery.php' );
349 $gallery_content[$marker] = Parser
::renderImageGallery( $content );
351 $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
357 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
358 foreach( $comment_content as $marker => $content ){
359 $comment_content[$marker] = '<!--'.$content.'-->';
364 foreach ( $this->mTagHooks
as $tag => $callback ) {
365 $ext_contents[$tag] = array();
366 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
367 foreach( $ext_content[$tag] as $marker => $content ) {
369 $ext_content[$tag][$marker] = $callback( $content );
371 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
376 # Merge state with the pre-existing state, if there is one
378 $state['html'] = $state['html'] +
$html_content;
379 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
380 $state['math'] = $state['math'] +
$math_content;
381 $state['pre'] = $state['pre'] +
$pre_content;
382 $state['comment'] = $state['comment'] +
$comment_content;
383 $state['gallery'] = $state['gallery'] +
$gallery_content;
385 foreach( $ext_content as $tag => $array ) {
386 if ( array_key_exists( $tag, $state ) ) {
387 $state[$tag] = $state[$tag] +
$array;
392 'html' => $html_content,
393 'nowiki' => $nowiki_content,
394 'math' => $math_content,
395 'pre' => $pre_content,
396 'comment' => $comment_content,
397 'gallery' => $gallery_content,
404 * restores pre, math, and hiero removed by strip()
406 * always call unstripNoWiki() after this one
409 function unstrip( $text, &$state ) {
410 # Must expand in reverse order, otherwise nested tags will be corrupted
411 $contentDict = end( $state );
412 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
413 if( key($state) != 'nowiki' && key($state) != 'html') {
414 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
415 $text = str_replace( key( $contentDict ), $content, $text );
424 * always call this after unstrip() to preserve the order
428 function unstripNoWiki( $text, &$state ) {
429 # Must expand in reverse order, otherwise nested tags will be corrupted
430 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
431 $text = str_replace( key( $state['nowiki'] ), $content, $text );
436 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
437 $text = str_replace( key( $state['html'] ), $content, $text );
445 * Add an item to the strip state
446 * Returns the unique tag which must be inserted into the stripped text
447 * The tag will be replaced with the original text in unstrip()
451 function insertStripItem( $text, &$state ) {
452 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
461 $state['item'][$rnd] = $text;
466 * Return allowed HTML attributes
470 function getHTMLattrs () {
471 $htmlattrs = array( # Allowed attributes--no scripting, etc.
472 'title', 'align', 'lang', 'dir', 'width', 'height',
473 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
474 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
475 /* FONT */ 'type', 'start', 'value', 'compact',
476 /* For various lists, mostly deprecated but safe */
477 'summary', 'width', 'border', 'frame', 'rules',
478 'cellspacing', 'cellpadding', 'valign', 'char',
479 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
480 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
481 'id', 'class', 'name', 'style' /* For CSS */
487 * Remove non approved attributes and javascript in css
491 function fixTagAttributes ( $t ) {
492 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
493 $htmlattrs = $this->getHTMLattrs() ;
495 # Strip non-approved attributes from the tag
497 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
498 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
501 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
503 # Strip javascript "expression" from stylesheets. Brute force approach:
504 # If anythin offensive is found, all attributes of the HTML tag are dropped
507 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
508 wfMungeToUtf8( $t ) ) )
517 * interface with html tidy, used if $wgUseTidy = true
522 function tidy ( $text ) {
523 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
524 global $wgInputEncoding, $wgOutputEncoding;
525 $fname = 'Parser::tidy';
526 wfProfileIn( $fname );
530 switch(strtoupper($wgOutputEncoding)) {
532 $opts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
535 $opts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
541 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
542 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
543 '<head><title>test</title></head><body>'.$text.'</body></html>';
544 $descriptorspec = array(
545 0 => array('pipe', 'r'),
546 1 => array('pipe', 'w'),
547 2 => array('file', '/dev/null', 'a')
549 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
550 if (is_resource($process)) {
551 fwrite($pipes[0], $wrappedtext);
553 while (!feof($pipes[1])) {
554 $cleansource .= fgets($pipes[1], 1024);
557 $return_value = proc_close($process);
560 wfProfileOut( $fname );
562 if( $cleansource == '' && $text != '') {
563 wfDebug( "Tidy error detected!\n" );
564 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
571 * parse the wiki syntax used to render tables
575 function doTableStuff ( $t ) {
576 $fname = 'Parser::doTableStuff';
577 wfProfileIn( $fname );
579 $t = explode ( "\n" , $t ) ;
580 $td = array () ; # Is currently a td tag open?
581 $ltd = array () ; # Was it TD or TH?
582 $tr = array () ; # Is currently a tr tag open?
583 $ltr = array () ; # tr attributes
584 $indent_level = 0; # indent level of the table
585 foreach ( $t AS $k => $x )
588 $fc = substr ( $x , 0 , 1 ) ;
589 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
590 $indent_level = strlen( $matches[1] );
592 str_repeat( '<dl><dd>', $indent_level ) .
593 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
594 array_push ( $td , false ) ;
595 array_push ( $ltd , '' ) ;
596 array_push ( $tr , false ) ;
597 array_push ( $ltr , '' ) ;
599 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
600 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
602 $l = array_pop ( $ltd ) ;
603 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
604 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
606 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
608 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
609 $x = substr ( $x , 1 ) ;
610 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
612 $l = array_pop ( $ltd ) ;
613 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
614 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
617 array_push ( $tr , false ) ;
618 array_push ( $td , false ) ;
619 array_push ( $ltd , '' ) ;
620 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
622 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) { # Caption
624 if ( '|+' == substr ( $x , 0 , 2 ) ) {
626 $x = substr ( $x , 1 ) ;
628 $after = substr ( $x , 1 ) ;
629 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
630 $after = explode ( '||' , $after ) ;
633 # Loop through each table cell
634 foreach ( $after AS $theline )
639 $tra = array_pop ( $ltr ) ;
640 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
641 array_push ( $tr , true ) ;
642 array_push ( $ltr , '' ) ;
645 $l = array_pop ( $ltd ) ;
646 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
647 if ( $fc == '|' ) $l = 'td' ;
648 else if ( $fc == '!' ) $l = 'th' ;
649 else if ( $fc == '+' ) $l = 'caption' ;
651 array_push ( $ltd , $l ) ;
654 $y = explode ( '|' , $theline , 2 ) ;
655 # Note that a '|' inside an invalid link should not
656 # be mistaken as delimiting cell parameters
657 if ( strpos( $y[0], '[[' ) !== false ) {
658 $y = array ($theline);
660 if ( count ( $y ) == 1 )
661 $y = "{$z}<{$l}>{$y[0]}" ;
662 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
664 array_push ( $td , true ) ;
669 # Closing open td, tr && table
670 while ( count ( $td ) > 0 )
672 if ( array_pop ( $td ) ) $t[] = '</td>' ;
673 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
677 $t = implode ( "\n" , $t ) ;
678 # $t = $this->removeHTMLtags( $t );
679 wfProfileOut( $fname );
684 * Helper function for parse() that transforms wiki markup into
685 * HTML. Only called for $mOutputType == OT_HTML.
689 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
692 $fname = 'Parser::internalParse';
693 wfProfileIn( $fname );
695 $text = $this->removeHTMLtags( $text );
696 $text = $this->replaceVariables( $text, $args );
698 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
700 $text = $this->doHeadings( $text );
701 if($this->mOptions
->getUseDynamicDates()) {
702 global $wgDateFormatter;
703 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
705 $text = $this->doAllQuotes( $text );
706 $text = $this->replaceInternalLinks( $text );
707 $text = $this->replaceExternalLinks( $text );
709 # replaceInternalLinks may sometimes leave behind
710 # absolute URLs, which have to be masked to hide them from replaceExternalLinks
711 $text = str_replace("http-noparse://","http://",$text);
713 $text = $this->doMagicLinks( $text );
714 $text = $this->doTableStuff( $text );
715 $text = $this->formatHeadings( $text, $isMain );
717 wfProfileOut( $fname );
722 * Replace special strings like "ISBN xxx" and "RFC xxx" with
723 * magic external links.
727 function &doMagicLinks( &$text ) {
728 global $wgUseGeoMode;
729 $text = $this->magicISBN( $text );
730 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
731 $text = $this->magicGEO( $text );
733 $text = $this->magicRFC( $text, 'RFC ', 'rfcurl' );
734 $text = $this->magicRFC( $text, 'PMID ', 'pubmedurl' );
739 * Parse ^^ tokens and return html
743 function doExponent( $text ) {
744 $fname = 'Parser::doExponent';
745 wfProfileIn( $fname );
746 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
747 wfProfileOut( $fname );
752 * Parse headers and return html
756 function doHeadings( $text ) {
757 $fname = 'Parser::doHeadings';
758 wfProfileIn( $fname );
759 for ( $i = 6; $i >= 1; --$i ) {
760 $h = substr( '======', 0, $i );
761 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
762 "<h{$i}>\\1</h{$i}>\\2", $text );
764 wfProfileOut( $fname );
769 * Replace single quotes with HTML markup
771 * @return string the altered text
773 function doAllQuotes( $text ) {
774 $fname = 'Parser::doAllQuotes';
775 wfProfileIn( $fname );
777 $lines = explode( "\n", $text );
778 foreach ( $lines as $line ) {
779 $outtext .= $this->doQuotes ( $line ) . "\n";
781 $outtext = substr($outtext, 0,-1);
782 wfProfileOut( $fname );
787 * Helper function for doAllQuotes()
790 function doQuotes( $text ) {
791 $arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
792 if ( count( $arr ) == 1 )
796 # First, do some preliminary work. This may shift some apostrophes from
797 # being mark-up to being text. It also counts the number of occurrences
798 # of bold and italics mark-ups.
802 foreach ( $arr as $r )
804 if ( ( $i %
2 ) == 1 )
806 # If there are ever four apostrophes, assume the first is supposed to
807 # be text, and the remaining three constitute mark-up for bold text.
808 if ( strlen( $arr[$i] ) == 4 )
813 # If there are more than 5 apostrophes in a row, assume they're all
814 # text except for the last 5.
815 else if ( strlen( $arr[$i] ) > 5 )
817 $arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
820 # Count the number of occurrences of bold and italics mark-ups.
821 # We are not counting sequences of five apostrophes.
822 if ( strlen( $arr[$i] ) == 2 ) $numitalics++
; else
823 if ( strlen( $arr[$i] ) == 3 ) $numbold++
; else
824 if ( strlen( $arr[$i] ) == 5 ) { $numitalics++
; $numbold++
; }
829 # If there is an odd number of both bold and italics, it is likely
830 # that one of the bold ones was meant to be an apostrophe followed
831 # by italics. Which one we cannot know for certain, but it is more
832 # likely to be one that has a single-letter word before it.
833 if ( ( $numbold %
2 == 1 ) && ( $numitalics %
2 == 1 ) )
836 $firstsingleletterword = -1;
837 $firstmultiletterword = -1;
839 foreach ( $arr as $r )
841 if ( ( $i %
2 == 1 ) and ( strlen( $r ) == 3 ) )
843 $x1 = substr ($arr[$i-1], -1);
844 $x2 = substr ($arr[$i-1], -2, 1);
846 if ($firstspace == -1) $firstspace = $i;
847 } else if ($x2 == ' ') {
848 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
850 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
856 # If there is a single-letter word, use it!
857 if ($firstsingleletterword > -1)
859 $arr [ $firstsingleletterword ] = "''";
860 $arr [ $firstsingleletterword-1 ] .= "'";
862 # If not, but there's a multi-letter word, use that one.
863 else if ($firstmultiletterword > -1)
865 $arr [ $firstmultiletterword ] = "''";
866 $arr [ $firstmultiletterword-1 ] .= "'";
868 # ... otherwise use the first one that has neither.
869 # (notice that it is possible for all three to be -1 if, for example,
870 # there is only one pentuple-apostrophe in the line)
871 else if ($firstspace > -1)
873 $arr [ $firstspace ] = "''";
874 $arr [ $firstspace-1 ] .= "'";
878 # Now let's actually convert our apostrophic mush to HTML!
887 if ($state == 'both')
894 if (strlen ($r) == 2)
897 { $output .= '</i>'; $state = ''; }
898 else if ($state == 'bi')
899 { $output .= '</i>'; $state = 'b'; }
900 else if ($state == 'ib')
901 { $output .= '</b></i><b>'; $state = 'b'; }
902 else if ($state == 'both')
903 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
904 else # $state can be 'b' or ''
905 { $output .= '<i>'; $state .= 'i'; }
907 else if (strlen ($r) == 3)
910 { $output .= '</b>'; $state = ''; }
911 else if ($state == 'bi')
912 { $output .= '</i></b><i>'; $state = 'i'; }
913 else if ($state == 'ib')
914 { $output .= '</b>'; $state = 'i'; }
915 else if ($state == 'both')
916 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
917 else # $state can be 'i' or ''
918 { $output .= '<b>'; $state .= 'b'; }
920 else if (strlen ($r) == 5)
923 { $output .= '</b><i>'; $state = 'i'; }
924 else if ($state == 'i')
925 { $output .= '</i><b>'; $state = 'b'; }
926 else if ($state == 'bi')
927 { $output .= '</i></b>'; $state = ''; }
928 else if ($state == 'ib')
929 { $output .= '</b></i>'; $state = ''; }
930 else if ($state == 'both')
931 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
932 else # ($state == '')
933 { $buffer = ''; $state = 'both'; }
938 # Now close all remaining tags. Notice that the order is important.
939 if ($state == 'b' ||
$state == 'ib')
941 if ($state == 'i' ||
$state == 'bi' ||
$state == 'ib')
945 if ($state == 'both')
946 $output .= '<b><i>'.$buffer.'</i></b>';
952 * Replace external links
954 * Note: this is all very hackish and the order of execution matters a lot.
955 * Make sure to run maintenance/parserTests.php if you change this code.
959 function replaceExternalLinks( $text ) {
960 $fname = 'Parser::replaceExternalLinks';
961 wfProfileIn( $fname );
963 $sk =& $this->mOptions
->getSkin();
965 $linktrail = $wgContLang->linkTrail();
967 $bits = preg_split( EXT_LINK_BRACKETED
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
969 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
972 while ( $i<count( $bits ) ) {
974 $protocol = $bits[$i++
];
976 $trail = $bits[$i++
];
978 # The characters '<' and '>' (which were escaped by
979 # removeHTMLtags()) should not be included in
980 # URLs, per RFC 2396.
981 if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE
)) {
982 $text = substr($url, $m2[0][1]) . ' ' . $text;
983 $url = substr($url, 0, $m2[0][1]);
986 # If the link text is an image URL, replace it with an <img> tag
987 # This happened by accident in the original parser, but some people used it extensively
988 $img = $this->maybeMakeImageLink( $text );
989 if ( $img !== false ) {
995 # Set linktype for CSS - if URL==text, link is essentially free
996 $linktype = ($text == $url) ?
'free' : 'text';
998 # No link text, e.g. [http://domain.tld/some.link]
1000 # Autonumber if allowed
1001 if ( strpos( HTTP_PROTOCOLS
, $protocol ) !== false ) {
1002 $text = '[' . ++
$this->mAutonumber
. ']';
1003 $linktype = 'autonumber';
1005 # Otherwise just use the URL
1006 $text = htmlspecialchars( $url );
1010 # Have link text, e.g. [http://domain.tld/some.link text]s
1012 if ( preg_match( $linktrail, $trail, $m2 ) ) {
1018 # Replace & from obsolete syntax with &.
1019 # All HTML entities will be escaped by makeExternalLink()
1020 # or maybeMakeImageLink()
1021 $url = str_replace( '&', '&', $url );
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 ö aren't valid in URLs anyway
1030 # This was changed in August 2004
1031 $s .= $sk->makeExternalLink( $url, $text, false, $linktype ) . $dtrail . $trail;
1034 wfProfileOut( $fname );
1039 * Replace anything that looks like a URL with a link
1042 function replaceFreeExternalLinks( $text ) {
1043 $fname = 'Parser::replaceFreeExternalLinks';
1044 wfProfileIn( $fname );
1046 $bits = preg_split( '/(\b(?:'.URL_PROTOCOLS
.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
1047 $s = array_shift( $bits );
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];
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
1071 # If there is no left bracket, then consider right brackets fair game too
1072 if ( strpos( $url, '(' ) === false ) {
1076 $numSepChars = strspn( strrev( $url ), $sep );
1077 if ( $numSepChars ) {
1078 $trail = substr( $url, -$numSepChars ) . $trail;
1079 $url = substr( $url, 0, -$numSepChars );
1082 # Replace & from obsolete syntax with &.
1083 # All HTML entities will be escaped by makeExternalLink()
1084 # or maybeMakeImageLink()
1085 $url = str_replace( '&', '&', $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, true, 'free' );
1093 $s .= $text . $trail;
1095 $s .= $protocol . $remainder;
1103 * make an image if it's allowed
1106 function maybeMakeImageLink( $url ) {
1107 $sk =& $this->mOptions
->getSkin();
1109 if ( $this->mOptions
->getAllowExternalImages() ) {
1110 if ( preg_match( EXT_IMAGE_REGEX
, $url ) ) {
1112 $text = $sk->makeImage( htmlspecialchars( $url ) );
1119 * Process [[ ]] wikilinks
1124 function replaceInternalLinks( $s ) {
1125 global $wgLang, $wgContLang, $wgLinkCache;
1126 global $wgDisableLangConversion;
1127 static $fname = 'Parser::replaceInternalLinks' ;
1129 wfProfileIn( $fname );
1131 wfProfileIn( $fname.'-setup' );
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
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];
1169 $first_prefix = false;
1175 $selflink = $this->mTitle
->getPrefixedText();
1176 wfProfileOut( $fname.'-setup' );
1178 $checkVariantLink = sizeof($wgContLang->getVariants())>1;
1179 $useSubpages = $this->areSubpagesAllowed();
1181 # Loop for each link
1182 for ($k = 0; isset( $a[$k] ); $k++
) {
1184 if ( $useLinkPrefixExtension ) {
1185 wfProfileIn( $fname.'-prefixhandling' );
1186 if ( preg_match( $e2, $s, $m ) ) {
1194 $prefix = $first_prefix;
1195 $first_prefix = false;
1197 wfProfileOut( $fname.'-prefixhandling' );
1200 $might_be_img = false;
1202 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1204 # fix up urlencoded title texts
1205 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1207 } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
1208 $might_be_img = true;
1210 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1212 } else { # Invalid form; output directly
1213 $s .= $prefix . '[[' . $line ;
1217 # Don't allow internal links to pages containing
1218 # PROTO: where PROTO is a valid URL protocol; these
1219 # should be external links.
1220 if (preg_match('/^(\b(?:'.URL_PROTOCOLS
.'):)/', $m[1])) {
1221 $s .= $prefix . '[[' . $line ;
1225 # Make subpage if necessary
1226 if( $useSubpages ) {
1227 $link = $this->maybeDoSubpageLink( $m[1], $text );
1232 $noforce = (substr($m[1], 0, 1) != ':');
1234 # Strip off leading ':'
1235 $link = substr($link, 1);
1238 $nt =& Title
::newFromText( $this->unstripNoWiki($link, $this->mStripState
) );
1240 $s .= $prefix . '[[' . $line;
1244 #check other language variants of the link
1245 #if the article does not exist
1246 if( $checkVariantLink
1247 && $nt->getArticleID() == 0 ) {
1248 $wgContLang->findVariantLink($link, $nt);
1251 $ns = $nt->getNamespace();
1252 $iw = $nt->getInterWiki();
1254 if ($might_be_img) { # if this is actually an invalid link
1255 if ($ns == NS_IMAGE
&& $noforce) { #but might be an image
1257 while (isset ($a[$k+
1]) ) {
1258 #look at the next 'line' to see if we can close it there
1259 $next_line = array_shift(array_splice( $a, $k +
1, 1) );
1260 if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
1261 # the first ]] closes the inner link, the second the image
1263 $text .= '[[' . $m[1];
1266 } elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
1267 #if there's exactly one ]] that's fine, we'll keep looking
1268 $text .= '[[' . $m[0];
1270 #if $next_line is invalid too, we need look no further
1271 $text .= '[[' . $next_line;
1276 # we couldn't find the end of this imageLink, so output it raw
1277 #but don't ignore what might be perfectly normal links in the text we've examined
1278 $text = $this->replaceInternalLinks($text);
1279 $s .= $prefix . '[[' . $link . '|' . $text;
1280 # note: no $trail, because without an end, there *is* no trail
1283 } else { #it's not an image, so output it raw
1284 $s .= $prefix . '[[' . $link . '|' . $text;
1285 # note: no $trail, because without an end, there *is* no trail
1290 $wasblank = ( '' == $text );
1291 if( $wasblank ) $text = $link;
1294 # Link not escaped by : , create the various objects
1298 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
1299 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1300 $tmp = $prefix . $trail ;
1301 $s .= (trim($tmp) == '')?
'': $tmp;
1305 if ( $ns == NS_IMAGE
) {
1306 wfProfileIn( "$fname-image" );
1308 # recursively parse links inside the image caption
1309 # actually, this will parse them in any other parameters, too,
1310 # but it might be hard to fix that, and it doesn't matter ATM
1311 $text = $this->replaceExternalLinks($text);
1312 $text = $this->replaceInternalLinks($text);
1314 # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
1315 $s .= $prefix . str_replace('http://', 'http-noparse://', $sk->makeImageLinkObj( $nt, $text ) ) . $trail;
1316 $wgLinkCache->addImageLinkObj( $nt );
1318 wfProfileOut( "$fname-image" );
1322 if ( $ns == NS_CATEGORY
) {
1323 wfProfileIn( "$fname-category" );
1324 $t = $nt->getText();
1326 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1327 $pPLC=$sk->postParseLinkColour();
1328 $sk->postParseLinkColour( false );
1329 $t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
1330 $sk->postParseLinkColour( $pPLC );
1331 $wgLinkCache->resume();
1334 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
1335 $sortkey = $this->mTitle
->getText();
1337 $sortkey = $this->mTitle
->getPrefixedText();
1342 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1343 $this->mOutput
->addCategoryLink( $t );
1344 $s .= $prefix . $trail ;
1346 wfProfileOut( "$fname-category" );
1351 if( ( $nt->getPrefixedText() === $selflink ) &&
1352 ( $nt->getFragment() === '' ) ) {
1353 # Self-links are handled specially; generally de-link and change to bold.
1354 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1358 # Special and Media are pseudo-namespaces; no pages actually exist in them
1359 if( $ns == NS_MEDIA
) {
1360 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text, true ) . $trail;
1361 $wgLinkCache->addImageLinkObj( $nt );
1363 } elseif( $ns == NS_SPECIAL
) {
1364 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1367 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1369 $sk->postParseLinkColour( $saveParseColour );
1370 wfProfileOut( $fname );
1375 * Return true if subpage links should be expanded on this page.
1378 function areSubpagesAllowed() {
1379 # Some namespaces don't allow subpages
1380 global $wgNamespacesWithSubpages;
1381 return !empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()]);
1385 * Handle link to subpage if necessary
1386 * @param string $target the source of the link
1387 * @param string &$text the link text, modified as necessary
1388 * @return string the full name of the link
1391 function maybeDoSubpageLink($target, &$text) {
1394 # :Foobar -- override special treatment of prefix (images, language links)
1395 # /Foobar -- convert to CurrentPage/Foobar
1396 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1397 # ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
1398 # ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
1400 $fname = 'Parser::maybeDoSubpageLink';
1401 wfProfileIn( $fname );
1402 $ret = $target; # default return value is no change
1404 # Some namespaces don't allow subpages,
1405 # so only perform processing if subpages are allowed
1406 if( $this->areSubpagesAllowed() ) {
1407 # Look at the first character
1408 if( $target != '' && $target{0} == '/' ) {
1409 # / at end means we don't want the slash to be shown
1410 if( substr( $target, -1, 1 ) == '/' ) {
1411 $target = substr( $target, 1, -1 );
1414 $noslash = substr( $target, 1 );
1417 $ret = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1418 if( '' === $text ) {
1420 } # this might be changed for ugliness reasons
1422 # check for .. subpage backlinks
1424 $nodotdot = $target;
1425 while( strncmp( $nodotdot, "../", 3 ) == 0 ) {
1427 $nodotdot = substr( $nodotdot, 3 );
1429 if($dotdotcount > 0) {
1430 $exploded = explode( '/', $this->mTitle
->GetPrefixedText() );
1431 if( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
1432 $ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
1433 # / at the end means don't show full path
1434 if( substr( $nodotdot, -1, 1 ) == '/' ) {
1435 $nodotdot = substr( $nodotdot, 0, -1 );
1436 if( '' === $text ) {
1440 $nodotdot = trim( $nodotdot );
1441 if( $nodotdot != '' ) {
1442 $ret .= '/' . $nodotdot;
1449 wfProfileOut( $fname );
1454 * Used by doBlockLevels()
1457 /* private */ function closeParagraph() {
1459 if ( '' != $this->mLastSection
) {
1460 $result = '</' . $this->mLastSection
. ">\n";
1462 $this->mInPre
= false;
1463 $this->mLastSection
= '';
1466 # getCommon() returns the length of the longest common substring
1467 # of both arguments, starting at the beginning of both.
1469 /* private */ function getCommon( $st1, $st2 ) {
1470 $fl = strlen( $st1 );
1471 $shorter = strlen( $st2 );
1472 if ( $fl < $shorter ) { $shorter = $fl; }
1474 for ( $i = 0; $i < $shorter; ++
$i ) {
1475 if ( $st1{$i} != $st2{$i} ) { break; }
1479 # These next three functions open, continue, and close the list
1480 # element appropriate to the prefix character passed into them.
1482 /* private */ function openList( $char ) {
1483 $result = $this->closeParagraph();
1485 if ( '*' == $char ) { $result .= '<ul><li>'; }
1486 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1487 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1488 else if ( ';' == $char ) {
1489 $result .= '<dl><dt>';
1490 $this->mDTopen
= true;
1492 else { $result = '<!-- ERR 1 -->'; }
1497 /* private */ function nextItem( $char ) {
1498 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1499 else if ( ':' == $char ||
';' == $char ) {
1501 if ( $this->mDTopen
) { $close = '</dt>'; }
1502 if ( ';' == $char ) {
1503 $this->mDTopen
= true;
1504 return $close . '<dt>';
1506 $this->mDTopen
= false;
1507 return $close . '<dd>';
1510 return '<!-- ERR 2 -->';
1513 /* private */ function closeList( $char ) {
1514 if ( '*' == $char ) { $text = '</li></ul>'; }
1515 else if ( '#' == $char ) { $text = '</li></ol>'; }
1516 else if ( ':' == $char ) {
1517 if ( $this->mDTopen
) {
1518 $this->mDTopen
= false;
1519 $text = '</dt></dl>';
1521 $text = '</dd></dl>';
1524 else { return '<!-- ERR 3 -->'; }
1530 * Make lists from lines starting with ':', '*', '#', etc.
1533 * @return string the lists rendered as HTML
1535 function doBlockLevels( $text, $linestart ) {
1536 $fname = 'Parser::doBlockLevels';
1537 wfProfileIn( $fname );
1539 # Parsing through the text line by line. The main thing
1540 # happening here is handling of block-level elements p, pre,
1541 # and making lists from lines starting with * # : etc.
1543 $textLines = explode( "\n", $text );
1545 $lastPrefix = $output = $lastLine = '';
1546 $this->mDTopen
= $inBlockElem = false;
1548 $paragraphStack = false;
1550 if ( !$linestart ) {
1551 $output .= array_shift( $textLines );
1553 foreach ( $textLines as $oLine ) {
1554 $lastPrefixLength = strlen( $lastPrefix );
1555 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1556 $preOpenMatch = preg_match('/<pre/i', $oLine );
1557 if ( !$this->mInPre
) {
1558 # Multiple prefixes may abut each other for nested lists.
1559 $prefixLength = strspn( $oLine, '*#:;' );
1560 $pref = substr( $oLine, 0, $prefixLength );
1563 $pref2 = str_replace( ';', ':', $pref );
1564 $t = substr( $oLine, $prefixLength );
1565 $this->mInPre
= !empty($preOpenMatch);
1567 # Don't interpret any other prefixes in preformatted text
1569 $pref = $pref2 = '';
1574 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1575 # Same as the last item, so no need to deal with nesting or opening stuff
1576 $output .= $this->nextItem( substr( $pref, -1 ) );
1577 $paragraphStack = false;
1579 if ( substr( $pref, -1 ) == ';') {
1580 # The one nasty exception: definition lists work like this:
1581 # ; title : definition text
1582 # So we check for : in the remainder text to split up the
1583 # title and definition, without b0rking links.
1584 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1586 $output .= $term . $this->nextItem( ':' );
1589 } elseif( $prefixLength ||
$lastPrefixLength ) {
1590 # Either open or close a level...
1591 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1592 $paragraphStack = false;
1594 while( $commonPrefixLength < $lastPrefixLength ) {
1595 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1596 --$lastPrefixLength;
1598 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1599 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1601 while ( $prefixLength > $commonPrefixLength ) {
1602 $char = substr( $pref, $commonPrefixLength, 1 );
1603 $output .= $this->openList( $char );
1605 if ( ';' == $char ) {
1606 # FIXME: This is dupe of code above
1607 if ($this->findColonNoLinks($t, $term, $t2) !== false) {
1609 $output .= $term . $this->nextItem( ':' );
1612 ++
$commonPrefixLength;
1614 $lastPrefix = $pref2;
1616 if( 0 == $prefixLength ) {
1617 wfProfileIn( "$fname-paragraph" );
1618 # No prefix (not in list)--go to paragraph mode
1619 $uniq_prefix = UNIQ_PREFIX
;
1620 // XXX: use a stack for nestable elements like span, table and div
1621 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
1622 $closematch = preg_match(
1623 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1624 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/iS', $t );
1625 if ( $openmatch or $closematch ) {
1626 $paragraphStack = false;
1627 $output .= $this->closeParagraph();
1628 if($preOpenMatch and !$preCloseMatch) {
1629 $this->mInPre
= true;
1631 if ( $closematch ) {
1632 $inBlockElem = false;
1634 $inBlockElem = true;
1636 } else if ( !$inBlockElem && !$this->mInPre
) {
1637 if ( ' ' == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1639 if ($this->mLastSection
!= 'pre') {
1640 $paragraphStack = false;
1641 $output .= $this->closeParagraph().'<pre>';
1642 $this->mLastSection
= 'pre';
1644 $t = substr( $t, 1 );
1647 if ( '' == trim($t) ) {
1648 if ( $paragraphStack ) {
1649 $output .= $paragraphStack.'<br />';
1650 $paragraphStack = false;
1651 $this->mLastSection
= 'p';
1653 if ($this->mLastSection
!= 'p' ) {
1654 $output .= $this->closeParagraph();
1655 $this->mLastSection
= '';
1656 $paragraphStack = '<p>';
1658 $paragraphStack = '</p><p>';
1662 if ( $paragraphStack ) {
1663 $output .= $paragraphStack;
1664 $paragraphStack = false;
1665 $this->mLastSection
= 'p';
1666 } else if ($this->mLastSection
!= 'p') {
1667 $output .= $this->closeParagraph().'<p>';
1668 $this->mLastSection
= 'p';
1673 wfProfileOut( "$fname-paragraph" );
1675 if ($paragraphStack === false) {
1679 while ( $prefixLength ) {
1680 $output .= $this->closeList( $pref2{$prefixLength-1} );
1683 if ( '' != $this->mLastSection
) {
1684 $output .= '</' . $this->mLastSection
. '>';
1685 $this->mLastSection
= '';
1688 wfProfileOut( $fname );
1693 * Split up a string on ':', ignoring any occurences inside
1694 * <a>..</a> or <span>...</span>
1695 * @param string $str the string to split
1696 * @param string &$before set to everything before the ':'
1697 * @param string &$after set to everything after the ':'
1698 * return string the position of the ':', or false if none found
1700 function findColonNoLinks($str, &$before, &$after) {
1701 # I wonder if we should make this count all tags, not just <a>
1702 # and <span>. That would prevent us from matching a ':' that
1703 # comes in the middle of italics other such formatting....
1705 $fname = 'Parser::findColonNoLinks';
1706 wfProfileIn( $fname );
1709 $colon = strpos($str, ':', $pos);
1711 if ($colon !== false) {
1712 $before = substr($str, 0, $colon);
1713 $after = substr($str, $colon +
1);
1715 # Skip any ':' within <a> or <span> pairs
1716 $a = substr_count($before, '<a');
1717 $s = substr_count($before, '<span');
1718 $ca = substr_count($before, '</a>');
1719 $cs = substr_count($before, '</span>');
1721 if ($a <= $ca and $s <= $cs) {
1722 # Tags are balanced before ':'; ok
1727 } while ($colon !== false);
1728 wfProfileOut( $fname );
1733 * Return value of a magic variable (like PAGENAME)
1737 function getVariableValue( $index ) {
1738 global $wgContLang, $wgSitename, $wgServer;
1741 * Some of these require message or data lookups and can be
1742 * expensive to check many times.
1744 static $varCache = array();
1745 if( isset( $varCache[$index] ) ) return $varCache[$index];
1748 case MAG_CURRENTMONTH
:
1749 return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
1750 case MAG_CURRENTMONTHNAME
:
1751 return $varCache[$index] = $wgContLang->getMonthName( date('n') );
1752 case MAG_CURRENTMONTHNAMEGEN
:
1753 return $varCache[$index] = $wgContLang->getMonthNameGen( date('n') );
1754 case MAG_CURRENTDAY
:
1755 return $varCache[$index] = $wgContLang->formatNum( date('j') );
1757 return $this->mTitle
->getText();
1759 return $this->mTitle
->getPartialURL();
1761 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1762 return $wgContLang->getNsText($this->mTitle
->getNamespace()); # Patch by Dori
1763 case MAG_CURRENTDAYNAME
:
1764 return $varCache[$index] = $wgContLang->getWeekdayName( date('w')+
1 );
1765 case MAG_CURRENTYEAR
:
1766 return $varCache[$index] = $wgContLang->formatNum( date( 'Y' ) );
1767 case MAG_CURRENTTIME
:
1768 return $varCache[$index] = $wgContLang->time( wfTimestampNow(), false );
1769 case MAG_CURRENTWEEK
:
1770 return $varCache[$index] = $wgContLang->formatNum( date('W') );
1771 case MAG_CURRENTDOW
:
1772 return $varCache[$index] = $wgContLang->formatNum( date('w') );
1773 case MAG_NUMBEROFARTICLES
:
1774 return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
1785 * initialise the magic variables (like CURRENTMONTHNAME)
1789 function initialiseVariables() {
1790 $fname = 'Parser::initialiseVariables';
1791 wfProfileIn( $fname );
1792 global $wgVariableIDs;
1793 $this->mVariables
= array();
1794 foreach ( $wgVariableIDs as $id ) {
1795 $mw =& MagicWord
::get( $id );
1796 $mw->addToArray( $this->mVariables
, $id );
1798 wfProfileOut( $fname );
1802 * Replace magic variables, templates, and template arguments
1803 * with the appropriate text. Templates are substituted recursively,
1804 * taking care to avoid infinite loops.
1806 * Note that the substitution depends on value of $mOutputType:
1807 * OT_WIKI: only {{subst:}} templates
1808 * OT_MSG: only magic variables
1809 * OT_HTML: all templates and magic variables
1811 * @param string $tex The text to transform
1812 * @param array $args Key-value pairs representing template parameters to substitute
1815 function replaceVariables( $text, $args = array() ) {
1816 global $wgLang, $wgScript, $wgArticlePath;
1818 # Prevent too big inclusions
1819 if( strlen( $text ) > MAX_INCLUDE_SIZE
) {
1823 $fname = 'Parser::replaceVariables';
1824 wfProfileIn( $fname );
1826 $titleChars = Title
::legalChars();
1828 # This function is called recursively. To keep track of arguments we need a stack:
1829 array_push( $this->mArgStack
, $args );
1831 # Variable substitution
1832 $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
1834 if ( $this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) {
1835 # Argument substitution
1836 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
1838 # Template substitution
1839 $regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1840 $text = preg_replace_callback( $regex, array( &$this, 'braceSubstitution' ), $text );
1842 array_pop( $this->mArgStack
);
1844 wfProfileOut( $fname );
1849 * Replace magic variables
1852 function variableSubstitution( $matches ) {
1853 $fname = 'parser::variableSubstitution';
1854 $varname = $matches[1];
1855 wfProfileIn( $fname );
1856 if ( !$this->mVariables
) {
1857 $this->initialiseVariables();
1860 if ( $this->mOutputType
== OT_WIKI
) {
1861 # Do only magic variables prefixed by SUBST
1862 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1863 if (!$mwSubst->matchStartAndRemove( $varname ))
1865 # Note that if we don't substitute the variable below,
1866 # we don't remove the {{subst:}} magic word, in case
1867 # it is a template rather than a magic variable.
1869 if ( !$skip && array_key_exists( $varname, $this->mVariables
) ) {
1870 $id = $this->mVariables
[$varname];
1871 $text = $this->getVariableValue( $id );
1872 $this->mOutput
->mContainsOldMagic
= true;
1874 $text = $matches[0];
1876 wfProfileOut( $fname );
1880 # Split template arguments
1881 function getTemplateArgs( $argsString ) {
1882 if ( $argsString === '' ) {
1886 $args = explode( '|', substr( $argsString, 1 ) );
1888 # If any of the arguments contains a '[[' but no ']]', it needs to be
1889 # merged with the next arg because the '|' character between belongs
1890 # to the link syntax and not the template parameter syntax.
1891 $argc = count($args);
1893 for ( $i = 0; $i < $argc-1; $i++
) {
1894 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1895 $args[$i] .= '|'.$args[$i+
1];
1896 array_splice($args, $i+
1, 1);
1906 * Return the text of a template, after recursively
1907 * replacing any variables or templates within the template.
1909 * @param array $matches The parts of the template
1910 * $matches[1]: the title, i.e. the part before the |
1911 * $matches[2]: the parameters (including a leading |), if any
1912 * @return string the text of the template
1915 function braceSubstitution( $matches ) {
1916 global $wgLinkCache, $wgContLang;
1917 $fname = 'Parser::braceSubstitution';
1918 wfProfileIn( $fname );
1926 # Need to know if the template comes at the start of a line,
1927 # to treat the beginning of the template like the beginning
1928 # of a line for tables and block-level elements.
1929 $linestart = $matches[1];
1931 # $part1 is the bit before the first |, and must contain only title characters
1932 # $args is a list of arguments, starting from index 0, not including $part1
1934 $part1 = $matches[2];
1935 # If the third subpattern matched anything, it will start with |
1937 $args = $this->getTemplateArgs($matches[3]);
1938 $argc = count( $args );
1940 # Don't parse {{{}}} because that's only for template arguments
1941 if ( $linestart === '{' ) {
1942 $text = $matches[0];
1949 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1950 if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType
== OT_WIKI
) ) {
1951 # One of two possibilities is true:
1952 # 1) Found SUBST but not in the PST phase
1953 # 2) Didn't find SUBST and in the PST phase
1954 # In either case, return without further processing
1955 $text = $matches[0];
1961 # MSG, MSGNW and INT
1964 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1965 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1968 # Remove obsolete MSG:
1969 $mwMsg =& MagicWord
::get( MAG_MSG
);
1970 $mwMsg->matchStartAndRemove( $part1 );
1973 # Check if it is an internal message
1974 $mwInt =& MagicWord
::get( MAG_INT
);
1975 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1976 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1977 $text = $linestart . wfMsgReal( $part1, $args, true );
1985 # Check for NS: (namespace expansion)
1986 $mwNs = MagicWord
::get( MAG_NS
);
1987 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1988 if ( intval( $part1 ) ) {
1989 $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
1992 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1993 if ( !is_null( $index ) ) {
1994 $text = $linestart . $wgContLang->getNsText( $index );
2001 # LOCALURL and LOCALURLE
2003 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
2004 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
2006 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
2007 $func = 'getLocalURL';
2008 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
2009 $func = 'escapeLocalURL';
2014 if ( $func !== '' ) {
2015 $title = Title
::newFromText( $part1 );
2016 if ( !is_null( $title ) ) {
2018 $text = $linestart . $title->$func( $args[0] );
2020 $text = $linestart . $title->$func();
2028 if ( !$found && $argc == 1 ) {
2029 $mwGrammar =& MagicWord
::get( MAG_GRAMMAR
);
2030 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
2031 $text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
2036 # Template table test
2038 # Did we encounter this template already? If yes, it is in the cache
2039 # and we need to check for loops.
2040 if ( !$found && isset( $this->mTemplates
[$part1] ) ) {
2041 # set $text to cached message.
2042 $text = $linestart . $this->mTemplates
[$part1];
2045 # Infinite loop test
2046 if ( isset( $this->mTemplatePath
[$part1] ) ) {
2049 $text .= '<!-- WARNING: template loop detected -->';
2053 # Load from database
2054 $itcamefromthedatabase = false;
2057 $part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
2058 if ($subpage !== '') {
2059 $ns = $this->mTitle
->getNamespace();
2061 $title = Title
::newFromText( $part1, $ns );
2062 if ( !is_null( $title ) && !$title->isExternal() ) {
2063 # Check for excessive inclusion
2064 $dbk = $title->getPrefixedDBkey();
2065 if ( $this->incrementIncludeCount( $dbk ) ) {
2066 # This should never be reached.
2067 $article = new Article( $title );
2068 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
2069 if ( $articleContent !== false ) {
2071 $text = $linestart . $articleContent;
2072 $itcamefromthedatabase = true;
2076 # If the title is valid but undisplayable, make a link to it
2077 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
2078 $text = $linestart . '[['.$title->getPrefixedText().']]';
2082 # Template cache array insertion
2083 $this->mTemplates
[$part1] = $text;
2087 # Recursive parsing, escaping and link table handling
2088 # Only for HTML output
2089 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
2090 $text = wfEscapeWikiText( $text );
2091 } elseif ( ($this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_WIKI
) && $found && !$noparse) {
2092 # Clean up argument array
2093 $assocArgs = array();
2095 foreach( $args as $arg ) {
2096 $eqpos = strpos( $arg, '=' );
2097 if ( $eqpos === false ) {
2098 $assocArgs[$index++
] = $arg;
2100 $name = trim( substr( $arg, 0, $eqpos ) );
2101 $value = trim( substr( $arg, $eqpos+
1 ) );
2102 if ( $value === false ) {
2105 if ( $name !== false ) {
2106 $assocArgs[$name] = $value;
2111 # Add a new element to the templace recursion path
2112 $this->mTemplatePath
[$part1] = 1;
2114 $text = $this->strip( $text, $this->mStripState
);
2115 $text = $this->removeHTMLtags( $text );
2116 $text = $this->replaceVariables( $text, $assocArgs );
2118 # Resume the link cache and register the inclusion as a link
2119 if ( $this->mOutputType
== OT_HTML
&& !is_null( $title ) ) {
2120 $wgLinkCache->addLinkObj( $title );
2123 # If the template begins with a table or block-level
2124 # element, it should be treated as beginning a new line.
2125 if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
2126 $text = "\n" . $text;
2130 # Empties the template path
2131 $this->mTemplatePath
= array();
2133 wfProfileOut( $fname );
2136 # replace ==section headers==
2137 # XXX this needs to go away once we have a better parser.
2138 if ( $this->mOutputType
!= OT_WIKI
&& $itcamefromthedatabase ) {
2139 if( !is_null( $title ) )
2140 $encodedname = base64_encode($title->getPrefixedDBkey());
2142 $encodedname = base64_encode("");
2143 $m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
2144 PREG_SPLIT_DELIM_CAPTURE
);
2147 for( $i = 0; $i < count($m); $i +
= 2 ) {
2149 if (!isset($m[$i +
1]) ||
$m[$i +
1] == "") continue;
2151 if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
2155 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
2156 $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
2157 . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
2164 # Empties the template path
2165 $this->mTemplatePath
= array();
2168 wfProfileOut( $fname );
2171 wfProfileOut( $fname );
2177 * Triple brace replacement -- used for template arguments
2180 function argSubstitution( $matches ) {
2181 $arg = trim( $matches[1] );
2182 $text = $matches[0];
2183 $inputArgs = end( $this->mArgStack
);
2185 if ( array_key_exists( $arg, $inputArgs ) ) {
2186 $text = $inputArgs[$arg];
2193 * Returns true if the function is allowed to include this entity
2196 function incrementIncludeCount( $dbk ) {
2197 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
2198 $this->mIncludeCount
[$dbk] = 0;
2200 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
2209 * Cleans up HTML, removes dangerous tags and attributes, and
2210 * removes HTML comments
2213 function removeHTMLtags( $text ) {
2214 global $wgUseTidy, $wgUserHtml;
2215 $fname = 'Parser::removeHTMLtags';
2216 wfProfileIn( $fname );
2219 $htmlpairs = array( # Tags that must be closed
2220 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
2221 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
2222 'strike', 'strong', 'tt', 'var', 'div', 'center',
2223 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
2224 'ruby', 'rt' , 'rb' , 'rp', 'p', 'span'
2226 $htmlsingle = array(
2227 'br', 'hr', 'li', 'dt', 'dd'
2229 $htmlnest = array( # Tags that can be nested--??
2230 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
2231 'dl', 'font', 'big', 'small', 'sub', 'sup', 'span'
2233 $tabletags = array( # Can only appear inside table
2237 $htmlpairs = array();
2238 $htmlsingle = array();
2239 $htmlnest = array();
2240 $tabletags = array();
2243 $htmlsingle = array_merge( $tabletags, $htmlsingle );
2244 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
2246 $htmlattrs = $this->getHTMLattrs () ;
2248 # Remove HTML comments
2249 $text = $this->removeHTMLcomments( $text );
2251 $bits = explode( '<', $text );
2252 $text = array_shift( $bits );
2254 $tagstack = array(); $tablestack = array();
2255 foreach ( $bits as $x ) {
2256 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
2257 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2259 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2260 error_reporting( $prev );
2263 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2267 if ( ! in_array( $t, $htmlsingle ) &&
2268 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
2269 @array_push
( $tagstack, $ot );
2272 if ( $t == 'table' ) {
2273 $tagstack = array_pop( $tablestack );
2278 # Keep track for later
2279 if ( in_array( $t, $tabletags ) &&
2280 ! in_array( 'table', $tagstack ) ) {
2282 } else if ( in_array( $t, $tagstack ) &&
2283 ! in_array ( $t , $htmlnest ) ) {
2285 } else if ( ! in_array( $t, $htmlsingle ) ) {
2286 if ( $t == 'table' ) {
2287 array_push( $tablestack, $tagstack );
2288 $tagstack = array();
2290 array_push( $tagstack, $t );
2292 # Strip non-approved attributes from the tag
2293 $newparams = $this->fixTagAttributes($params);
2297 $rest = str_replace( '>', '>', $rest );
2298 $text .= "<$slash$t $newparams$brace$rest";
2302 $text .= '<' . str_replace( '>', '>', $x);
2304 # Close off any remaining tags
2305 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2307 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2310 # this might be possible using tidy itself
2311 foreach ( $bits as $x ) {
2312 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2314 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2315 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2316 $newparams = $this->fixTagAttributes($params);
2317 $rest = str_replace( '>', '>', $rest );
2318 $text .= "<$slash$t $newparams$brace$rest";
2320 $text .= '<' . str_replace( '>', '>', $x);
2324 wfProfileOut( $fname );
2329 * Remove '<!--', '-->', and everything between.
2330 * To avoid leaving blank lines, when a comment is both preceded
2331 * and followed by a newline (ignoring spaces), trim leading and
2332 * trailing spaces and one of the newlines.
2336 function removeHTMLcomments( $text ) {
2337 $fname='Parser::removeHTMLcomments';
2338 wfProfileIn( $fname );
2339 while (($start = strpos($text, '<!--')) !== false) {
2340 $end = strpos($text, '-->', $start +
4);
2341 if ($end === false) {
2342 # Unterminated comment; bail out
2348 # Trim space and newline if the comment is both
2349 # preceded and followed by a newline
2350 $spaceStart = max($start - 1, 0);
2351 $spaceLen = $end - $spaceStart;
2352 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2356 while (substr($text, $spaceStart +
$spaceLen, 1) === ' ')
2358 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart +
$spaceLen, 1) === "\n") {
2359 # Remove the comment, leading and trailing
2360 # spaces, and leave only one newline.
2361 $text = substr_replace($text, "\n", $spaceStart, $spaceLen +
1);
2364 # Remove just the comment.
2365 $text = substr_replace($text, '', $start, $end - $start);
2368 wfProfileOut( $fname );
2373 * This function accomplishes several tasks:
2374 * 1) Auto-number headings if that option is enabled
2375 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2376 * 3) Add a Table of contents on the top for users who have enabled the option
2377 * 4) Auto-anchor headings
2379 * It loops through all headlines, collects the necessary data, then splits up the
2380 * string and re-inserts the newly formatted headlines.
2383 /* private */ function formatHeadings( $text, $isMain=true ) {
2384 global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders, $wgInterwikiLinkHolders;
2386 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
2387 $doShowToc = $this->mOptions
->getShowToc();
2388 $forceTocHere = false;
2389 if( !$this->mTitle
->userCanEdit() ) {
2391 $rightClickHack = 0;
2393 $showEditLink = $this->mOptions
->getEditSection();
2394 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
2397 # Inhibit editsection links if requested in the page
2398 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
2399 if( $esw->matchAndRemove( $text ) ) {
2402 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2404 $mw =& MagicWord
::get( MAG_NOTOC
);
2405 if( $mw->matchAndRemove( $text ) ) {
2409 # never add the TOC to the Main Page. This is an entry page that should not
2410 # be more than 1-2 screens large anyway
2411 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
2415 # Get all headlines for numbering them and adding funky stuff like [edit]
2416 # links - this is for later, but we need the number of headlines right now
2417 $numMatches = preg_match_all( '/<H([1-6])(.*?'.'>)(.*?)<\/H[1-6] *>/i', $text, $matches );
2419 # if there are fewer than 4 headlines in the article, do not show TOC
2420 if( $numMatches < 4 ) {
2424 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2425 # override above conditions and always show TOC at that place
2427 $mw =& MagicWord
::get( MAG_TOC
);
2428 if($mw->match( $text ) ) {
2430 $forceTocHere = true;
2432 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2433 # override above conditions and always show TOC above first header
2434 $mw =& MagicWord
::get( MAG_FORCETOC
);
2435 if ($mw->matchAndRemove( $text ) ) {
2440 # Never ever show TOC if no headers
2441 if( $numMatches < 1 ) {
2445 # We need this to perform operations on the HTML
2446 $sk =& $this->mOptions
->getSkin();
2450 $sectionCount = 0; # headlineCount excluding template sections
2452 # Ugh .. the TOC should have neat indentation levels which can be
2453 # passed to the skin functions. These are determined here
2457 $sublevelCount = array();
2458 $levelCount = array();
2465 foreach( $matches[3] as $headline ) {
2467 $templatetitle = '';
2468 $templatesection = 0;
2471 if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
2473 $templatetitle = base64_decode($mat[1]);
2474 $templatesection = 1 +
(int)base64_decode($mat[2]);
2475 $headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
2479 $prevlevel = $level;
2480 $prevtoclevel = $toclevel;
2482 $level = $matches[1][$headlineCount];
2484 if( $doNumberHeadings ||
$doShowToc ) {
2486 if ( $level > $prevlevel ) {
2487 # Increase TOC level
2489 $sublevelCount[$toclevel] = 0;
2490 $toc .= $sk->tocIndent();
2492 elseif ( $level < $prevlevel && $toclevel > 1 ) {
2493 # Decrease TOC level, find level to jump to
2495 if ( $toclevel == 2 && $level <= $levelCount[1] ) {
2496 # Can only go down to level 1
2499 for ($i = $toclevel; $i > 0; $i--) {
2500 if ( $levelCount[$i] == $level ) {
2501 # Found last matching level
2505 elseif ( $levelCount[$i] < $level ) {
2506 # Found first matching level below current level
2513 $toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
2516 # No change in level, end TOC line
2517 $toc .= $sk->tocLineEnd();
2520 $levelCount[$toclevel] = $level;
2522 # count number of headlines for each level
2523 @$sublevelCount[$toclevel]++
;
2525 for( $i = 1; $i <= $toclevel; $i++
) {
2526 if( !empty( $sublevelCount[$i] ) ) {
2530 $numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
2536 # The canonized header is a version of the header text safe to use for links
2537 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2538 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
2539 $canonized_headline = $this->unstripNoWiki( $canonized_headline, $this->mStripState
);
2541 # Remove link placeholders by the link text.
2542 # <!--LINK number-->
2544 # link text with suffix
2545 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2546 "\$wgLinkHolders['texts'][\$1]",
2547 $canonized_headline );
2548 $canonized_headline = preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
2549 "\$wgInterwikiLinkHolders[\$1]",
2550 $canonized_headline );
2553 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2554 $tocline = trim( $canonized_headline );
2555 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
2556 $replacearray = array(
2560 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2561 $refer[$headlineCount] = $canonized_headline;
2563 # count how many in assoc. array so we can track dupes in anchors
2564 @$refers[$canonized_headline]++
;
2565 $refcount[$headlineCount]=$refers[$canonized_headline];
2567 # Don't number the heading if it is the only one (looks silly)
2568 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2569 # the two are different if the line contains a link
2570 $headline=$numbering . ' ' . $headline;
2573 # Create the anchor for linking from the TOC to the section
2574 $anchor = $canonized_headline;
2575 if($refcount[$headlineCount] > 1 ) {
2576 $anchor .= '_' . $refcount[$headlineCount];
2578 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2579 $toc .= $sk->tocLine($anchor, $tocline, $numbering, $toclevel);
2581 if( $showEditLink && ( !$istemplate ||
$templatetitle !== "" ) ) {
2582 if ( empty( $head[$headlineCount] ) ) {
2583 $head[$headlineCount] = '';
2586 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2588 $head[$headlineCount] .= $sk->editSectionLink($this->mTitle
, $sectionCount+
1);
2591 # Add the edit section span
2592 if( $rightClickHack ) {
2594 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2596 $headline = $sk->editSectionScript($this->mTitle
, $sectionCount+
1,$headline);
2599 # give headline the correct <h#> tag
2600 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2608 $toclines = $headlineCount;
2609 $toc .= $sk->tocUnindent( $toclevel - 1 );
2610 $toc = $sk->tocList( $toc );
2613 # split up and insert constructed headlines
2615 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2618 foreach( $blocks as $block ) {
2619 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2620 # This is the [edit] link that appears for the top block of text when
2621 # section editing is enabled
2623 # Disabled because it broke block formatting
2624 # For example, a bullet point in the top line
2625 # $full .= $sk->editSectionLink(0);
2628 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2629 # Top anchor now in skin
2633 if( !empty( $head[$i] ) ) {
2639 $mw =& MagicWord
::get( MAG_TOC
);
2640 return $mw->replace( $toc, $full );
2647 * Return an HTML link for the "ISBN 123456" text
2650 function magicISBN( $text ) {
2652 $fname = 'Parser::magicISBN';
2653 wfProfileIn( $fname );
2655 $a = split( 'ISBN ', ' '.$text );
2656 if ( count ( $a ) < 2 ) {
2657 wfProfileOut( $fname );
2660 $text = substr( array_shift( $a ), 1);
2661 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2663 foreach ( $a as $x ) {
2664 $isbn = $blank = '' ;
2665 while ( ' ' == $x{0} ) {
2667 $x = substr( $x, 1 );
2669 if ( $x == '' ) { # blank isbn
2670 $text .= "ISBN $blank";
2673 while ( strstr( $valid, $x{0} ) != false ) {
2675 $x = substr( $x, 1 );
2677 $num = str_replace( '-', '', $isbn );
2678 $num = str_replace( ' ', '', $num );
2681 $text .= "ISBN $blank$x";
2683 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2684 $text .= '<a href="' .
2685 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2686 "\" class=\"internal\">ISBN $isbn</a>";
2690 wfProfileOut( $fname );
2695 * Return an HTML link for the "GEO ..." text
2698 function magicGEO( $text ) {
2699 global $wgLang, $wgUseGeoMode;
2700 $fname = 'Parser::magicGEO';
2701 wfProfileIn( $fname );
2703 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2704 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2705 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2706 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2707 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2708 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2710 $a = split( 'GEO ', ' '.$text );
2711 if ( count ( $a ) < 2 ) {
2712 wfProfileOut( $fname );
2715 $text = substr( array_shift( $a ), 1);
2716 $valid = '0123456789.+-:';
2718 foreach ( $a as $x ) {
2719 $geo = $blank = '' ;
2720 while ( ' ' == $x{0} ) {
2722 $x = substr( $x, 1 );
2724 while ( strstr( $valid, $x{0} ) != false ) {
2726 $x = substr( $x, 1 );
2728 $num = str_replace( '+', '', $geo );
2729 $num = str_replace( ' ', '', $num );
2731 if ( '' == $num ||
count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2732 $text .= "GEO $blank$x";
2734 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2735 $text .= '<a href="' .
2736 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2737 "\" class=\"internal\">GEO $geo</a>";
2741 wfProfileOut( $fname );
2746 * Return an HTML link for the "RFC 1234" text
2748 * @param string $text text to be processed
2750 function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl' ) {
2753 $valid = '0123456789';
2756 $a = split( $keyword, ' '.$text );
2757 if ( count ( $a ) < 2 ) {
2760 $text = substr( array_shift( $a ), 1);
2762 /* Check if keyword is preceed by [[.
2763 * This test is made here cause of the array_shift above
2764 * that prevent the test to be done in the foreach.
2766 if ( substr( $text, -2 ) == '[[' ) {
2770 foreach ( $a as $x ) {
2771 /* token might be empty if we have RFC RFC 1234 */
2779 /** remove and save whitespaces in $blank */
2780 while ( $x{0} == ' ' ) {
2782 $x = substr( $x, 1 );
2785 /** remove and save the rfc number in $id */
2786 while ( strstr( $valid, $x{0} ) != false ) {
2788 $x = substr( $x, 1 );
2792 /* call back stripped spaces*/
2793 $text .= $keyword.$blank.$x;
2794 } elseif( $internal ) {
2796 $text .= $keyword.$id.$x;
2798 /* build the external link*/
2799 $url = wfmsg( $urlmsg );
2800 $url = str_replace( '$1', $id, $url);
2801 $sk =& $this->mOptions
->getSkin();
2802 $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
2803 $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
2806 /* Check if the next RFC keyword is preceed by [[ */
2807 $internal = ( substr($x,-2) == '[[' );
2813 * Transform wiki markup when saving a page by doing \r\n -> \n
2814 * conversion, substitting signatures, {{subst:}} templates, etc.
2816 * @param string $text the text to transform
2817 * @param Title &$title the Title object for the current article
2818 * @param User &$user the User object describing the current user
2819 * @param ParserOptions $options parsing options
2820 * @param bool $clearState whether to clear the parser state first
2821 * @return string the altered wiki markup
2824 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2825 $this->mOptions
= $options;
2826 $this->mTitle
=& $title;
2827 $this->mOutputType
= OT_WIKI
;
2829 if ( $clearState ) {
2830 $this->clearState();
2833 $stripState = false;
2837 $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
2838 $text = $this->strip( $text, $stripState, false );
2839 $text = $this->pstPass2( $text, $user );
2840 $text = $this->unstrip( $text, $stripState );
2841 $text = $this->unstripNoWiki( $text, $stripState );
2846 * Pre-save transform helper function
2849 function pstPass2( $text, &$user ) {
2850 global $wgLang, $wgContLang, $wgLocaltimezone;
2852 # Variable replacement
2853 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2854 $text = $this->replaceVariables( $text );
2858 $n = $user->getName();
2859 $k = $user->getOption( 'nickname' );
2860 if ( '' == $k ) { $k = $n; }
2861 if ( isset( $wgLocaltimezone ) ) {
2862 $oldtz = getenv( 'TZ' );
2863 putenv( 'TZ='.$wgLocaltimezone );
2865 /* Note: this is an ugly timezone hack for the European wikis */
2866 $d = $wgContLang->timeanddate( date( 'YmdHis' ), false ) .
2867 ' (' . date( 'T' ) . ')';
2868 if ( isset( $wgLocaltimezone ) ) {
2869 putenv( 'TZ='.$oldtzs );
2872 if( $user->getOption( 'fancysig' ) ) {
2875 $sigText = '[[' . $wgContLang->getNsText( NS_USER
) . ":$n|$k]]";
2877 $text = preg_replace( '/~~~~~/', $d, $text );
2878 $text = preg_replace( '/~~~~/', "$sigText $d", $text );
2879 $text = preg_replace( '/~~~/', $sigText, $text );
2881 # Context links: [[|name]] and [[name (context)|]]
2883 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2884 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2885 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2886 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2888 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2889 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2890 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2891 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2893 $t = $this->mTitle
->getText();
2894 if ( preg_match( $conpat, $t, $m ) ) {
2897 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2898 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2899 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2901 if ( '' == $context ) {
2902 $text = preg_replace( $p2, '[[\\1]]', $text );
2904 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2907 # Trim trailing whitespace
2908 # MAG_END (__END__) tag allows for trailing
2909 # whitespace to be deliberately included
2910 $text = rtrim( $text );
2911 $mw =& MagicWord
::get( MAG_END
);
2912 $mw->matchAndRemove( $text );
2918 * Set up some variables which are usually set up in parse()
2919 * so that an external function can call some class members with confidence
2922 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2923 $this->mTitle
=& $title;
2924 $this->mOptions
= $options;
2925 $this->mOutputType
= $outputType;
2926 if ( $clearState ) {
2927 $this->clearState();
2932 * Transform a MediaWiki message by replacing magic variables.
2934 * @param string $text the text to transform
2935 * @param ParserOptions $options options
2936 * @return string the text with variables substituted
2939 function transformMsg( $text, $options ) {
2941 static $executing = false;
2943 # Guard against infinite recursion
2949 $this->mTitle
= $wgTitle;
2950 $this->mOptions
= $options;
2951 $this->mOutputType
= OT_MSG
;
2952 $this->clearState();
2953 $text = $this->replaceVariables( $text );
2960 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2961 * Callback will be called with the text within
2962 * Transform and return the text within
2965 function setHook( $tag, $callback ) {
2966 $oldVal = @$this->mTagHooks
[$tag];
2967 $this->mTagHooks
[$tag] = $callback;
2972 * Replace <!--LINK--> link placeholders with actual links, in the buffer
2973 * Placeholders created in Skin::makeLinkObj()
2974 * Returns an array of links found, indexed by PDBK:
2978 * $options is a bit field, RLH_FOR_UPDATE to select for update
2980 function replaceLinkHolders( &$text, $options = 0 ) {
2981 global $wgUser, $wgLinkCache, $wgUseOldExistenceCheck, $wgLinkHolders;
2982 global $wgInterwikiLinkHolders;
2983 global $outputReplace;
2985 if ( $wgUseOldExistenceCheck ) {
2989 $fname = 'Parser::replaceLinkHolders';
2990 wfProfileIn( $fname );
2995 #if ( !empty( $tmpLinks[0] ) ) { #TODO
2996 if ( !empty( $wgLinkHolders['namespaces'] ) ) {
2997 wfProfileIn( $fname.'-check' );
2998 $dbr =& wfGetDB( DB_SLAVE
);
2999 $page = $dbr->tableName( 'page' );
3000 $sk = $wgUser->getSkin();
3001 $threshold = $wgUser->getOption('stubthreshold');
3004 asort( $wgLinkHolders['namespaces'] );
3008 foreach ( $wgLinkHolders['namespaces'] as $key => $val ) {
3010 $title = $wgLinkHolders['titles'][$key];
3012 # Skip invalid entries.
3013 # Result will be ugly, but prevents crash.
3014 if ( is_null( $title ) ) {
3017 $pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
3019 # Check if it's in the link cache already
3020 if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
3021 $colours[$pdbk] = 1;
3022 } elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
3023 $colours[$pdbk] = 0;
3025 # Not in the link cache, add it to the query
3026 if ( !isset( $current ) ) {
3030 $query = "SELECT page_id, page_namespace, page_title";
3031 if ( $threshold > 0 ) {
3032 $textTable = $dbr->tableName( 'text' );
3033 $query .= ', LENGTH(old_text) AS page_len, page_is_redirect';
3034 $tables .= ", $textTable";
3035 $join = 'page_latest=old_id AND';
3037 $query .= " FROM $tables WHERE $join (page_namespace=$val AND page_title IN(";
3038 } elseif ( $current != $val ) {
3040 $query .= ")) OR (page_namespace=$val AND page_title IN(";
3045 $query .= $dbr->addQuotes( $wgLinkHolders['dbkeys'][$key] );
3050 if ( $options & RLH_FOR_UPDATE
) {
3051 $query .= ' FOR UPDATE';
3054 $res = $dbr->query( $query, $fname );
3056 # Fetch data and form into an associative array
3057 # non-existent = broken
3060 while ( $s = $dbr->fetchObject($res) ) {
3061 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
3062 $pdbk = $title->getPrefixedDBkey();
3063 $wgLinkCache->addGoodLink( $s->page_id
, $pdbk );
3065 if ( $threshold > 0 ) {
3066 $size = $s->page_len
;
3067 if ( $s->page_is_redirect ||
$s->page_namespace
!= 0 ||
$length < $threshold ) {
3068 $colours[$pdbk] = 1;
3070 $colours[$pdbk] = 2;
3073 $colours[$pdbk] = 1;
3077 wfProfileOut( $fname.'-check' );
3079 # Construct search and replace arrays
3080 wfProfileIn( $fname.'-construct' );
3081 $outputReplace = array();
3082 foreach ( $wgLinkHolders['namespaces'] as $key => $ns ) {
3083 $pdbk = $pdbks[$key];
3084 $searchkey = '<!--LINK '.$key.'-->';
3085 $title = $wgLinkHolders['titles'][$key];
3086 if ( empty( $colours[$pdbk] ) ) {
3087 $wgLinkCache->addBadLink( $pdbk );
3088 $colours[$pdbk] = 0;
3089 $outputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
3090 $wgLinkHolders['texts'][$key],
3091 $wgLinkHolders['queries'][$key] );
3092 } elseif ( $colours[$pdbk] == 1 ) {
3093 $outputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
3094 $wgLinkHolders['texts'][$key],
3095 $wgLinkHolders['queries'][$key] );
3096 } elseif ( $colours[$pdbk] == 2 ) {
3097 $outputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
3098 $wgLinkHolders['texts'][$key],
3099 $wgLinkHolders['queries'][$key] );
3102 wfProfileOut( $fname.'-construct' );
3105 wfProfileIn( $fname.'-replace' );
3107 $text = preg_replace_callback(
3108 '/(<!--LINK .*?-->)/',
3109 "outputReplaceMatches",
3111 wfProfileOut( $fname.'-replace' );
3114 if ( !empty( $wgInterwikiLinkHolders ) ) {
3115 wfProfileIn( $fname.'-interwiki' );
3116 $outputReplace = $wgInterwikiLinkHolders;
3117 $text = preg_replace_callback(
3118 '/<!--IWLINK (.*?)-->/',
3119 "outputReplaceMatches",
3121 wfProfileOut( $fname.'-interwiki' );
3124 wfProfileOut( $fname );
3129 * Renders an image gallery from a text with one line per image.
3130 * text labels may be given by using |-style alternative text. E.g.
3131 * Image:one.jpg|The number "1"
3132 * Image:tree.jpg|A tree
3133 * given as text will return the HTML of a gallery with two images,
3134 * labeled 'The number "1"' and
3137 function renderImageGallery( $text ) {
3138 global $wgLinkCache;
3139 $ig = new ImageGallery();
3140 $ig->setShowBytes( false );
3141 $ig->setShowFilename( false );
3142 $lines = explode( "\n", $text );
3144 foreach ( $lines as $line ) {
3145 # match lines like these:
3146 # Image:someimage.jpg|This is some image
3147 preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
3149 if ( count( $matches ) == 0 ) {
3152 $nt = Title
::newFromURL( $matches[1] );
3153 if( is_null( $nt ) ) {
3154 # Bogus title. Ignore these so we don't bomb out later.
3157 if ( isset( $matches[3] ) ) {
3158 $label = $matches[3];
3163 # FIXME: Use the full wiki parser and add its links
3164 # to the page's links.
3165 $html = $this->mOptions
->mSkin
->formatComment( $label );
3167 $ig->add( Image
::newFromTitle( $nt ), $html );
3168 $wgLinkCache->addImageLinkObj( $nt );
3170 return $ig->toHTML();
3176 * @package MediaWiki
3180 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
3181 var $mCacheTime; # Used in ParserCache
3182 var $mVersion; # Compatibility check
3184 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
3185 $containsOldMagic = false )
3187 $this->mText
= $text;
3188 $this->mLanguageLinks
= $languageLinks;
3189 $this->mCategoryLinks
= $categoryLinks;
3190 $this->mContainsOldMagic
= $containsOldMagic;
3191 $this->mCacheTime
= '';
3192 $this->mVersion
= MW_PARSER_VERSION
;
3195 function getText() { return $this->mText
; }
3196 function getLanguageLinks() { return $this->mLanguageLinks
; }
3197 function getCategoryLinks() { return array_keys( $this->mCategoryLinks
); }
3198 function getCacheTime() { return $this->mCacheTime
; }
3199 function containsOldMagic() { return $this->mContainsOldMagic
; }
3200 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
3201 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
3202 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
3203 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
3204 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
3205 function addCategoryLink( $c ) { $this->mCategoryLinks
[$c] = 1; }
3207 function merge( $other ) {
3208 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
3209 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
3210 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
3214 * Return true if this cached output object predates the global or
3215 * per-article cache invalidation timestamps, or if it comes from
3216 * an incompatible older version.
3218 * @param string $touched the affected article's last touched timestamp
3222 function expired( $touched ) {
3223 global $wgCacheEpoch;
3224 return $this->getCacheTime() <= $touched ||
3225 $this->getCacheTime() <= $wgCacheEpoch ||
3226 !isset( $this->mVersion
) ||
3227 version_compare( $this->mVersion
, MW_PARSER_VERSION
, "lt" );
3232 * Set options of the Parser
3234 * @package MediaWiki
3238 # All variables are private
3239 var $mUseTeX; # Use texvc to expand <math> tags
3240 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
3241 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
3242 var $mAllowExternalImages; # Allow external images inline
3243 var $mSkin; # Reference to the preferred skin
3244 var $mDateFormat; # Date format index
3245 var $mEditSection; # Create "edit section" links
3246 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
3247 var $mNumberHeadings; # Automatically number headings
3248 var $mShowToc; # Show table of contents
3250 function getUseTeX() { return $this->mUseTeX
; }
3251 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
3252 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
3253 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
3254 function getSkin() { return $this->mSkin
; }
3255 function getDateFormat() { return $this->mDateFormat
; }
3256 function getEditSection() { return $this->mEditSection
; }
3257 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
3258 function getNumberHeadings() { return $this->mNumberHeadings
; }
3259 function getShowToc() { return $this->mShowToc
; }
3261 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
3262 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
3263 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
3264 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
3265 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
3266 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
3267 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
3268 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
3269 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
3271 function setSkin( &$x ) { $this->mSkin
=& $x; }
3273 # Get parser options
3274 /* static */ function newFromUser( &$user ) {
3275 $popts = new ParserOptions
;
3276 $popts->initialiseFromUser( $user );
3281 function initialiseFromUser( &$userInput ) {
3282 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
3283 $fname = 'ParserOptions::initialiseFromUser';
3284 wfProfileIn( $fname );
3285 if ( !$userInput ) {
3287 $user->setLoaded( true );
3289 $user =& $userInput;
3292 $this->mUseTeX
= $wgUseTeX;
3293 $this->mUseDynamicDates
= $wgUseDynamicDates;
3294 $this->mInterwikiMagic
= $wgInterwikiMagic;
3295 $this->mAllowExternalImages
= $wgAllowExternalImages;
3296 wfProfileIn( $fname.'-skin' );
3297 $this->mSkin
=& $user->getSkin();
3298 wfProfileOut( $fname.'-skin' );
3299 $this->mDateFormat
= $user->getOption( 'date' );
3300 $this->mEditSection
= $user->getOption( 'editsection' );
3301 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
3302 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
3303 $this->mShowToc
= $user->getOption( 'showtoc' );
3304 wfProfileOut( $fname );
3311 * Callback function used by Parser::replaceLinkHolders()
3312 * to substitute link placeholders.
3314 function &outputReplaceMatches( $matches ) {
3315 global $outputReplace;
3316 return $outputReplace[$matches[1]];
3320 * Return the total number of articles
3322 function wfNumberOfArticles() {
3323 global $wgNumberOfArticles;
3326 return $wgNumberOfArticles;
3330 * Get various statistics from the database
3333 function wfLoadSiteStats() {
3334 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
3335 $fname = 'wfLoadSiteStats';
3337 if ( -1 != $wgNumberOfArticles ) return;
3338 $dbr =& wfGetDB( DB_SLAVE
);
3339 $s = $dbr->selectRow( 'site_stats',
3340 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
3341 array( 'ss_row_id' => 1 ), $fname
3344 if ( $s === false ) {
3347 $wgTotalViews = $s->ss_total_views
;
3348 $wgTotalEdits = $s->ss_total_edits
;
3349 $wgNumberOfArticles = $s->ss_good_articles
;
3353 function wfEscapeHTMLTagsOnly( $in ) {
3355 array( '"', '>', '<' ),
3356 array( '"', '>', '<' ),