3 // require_once('Tokenizer.php');
7 # Processes wiki markup
9 # There are two main entry points into the Parser class:
11 # produces HTML output
13 # produces altered wiki markup.
16 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
18 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
21 # $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
22 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
25 # * only within ParserOptions
27 #----------------------------------------
28 # Variable substitution O(N^2) attack
29 #-----------------------------------------
30 # Without countermeasures, it would be possible to attack the parser by saving
31 # a page filled with a large number of inclusions of large pages. The size of
32 # the generated page would be proportional to the square of the input size.
33 # Hence, we limit the number of inclusions of any given page, thus bringing any
34 # attack back to O(N).
35 define( 'MAX_INCLUDE_REPEAT', 100 );
36 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
38 # Allowed values for $mOutputType
39 define( 'OT_HTML', 1 );
40 define( 'OT_WIKI', 2 );
41 define( 'OT_MSG' , 3 );
43 # string parameter for extractTags which will cause it
44 # to strip HTML comments in addition to regular
45 # <XML>-style tags. This should not be anything we
46 # may want to use in wikisyntax
47 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
49 # prefix for escaping, used in two functions at least
50 define( 'UNIQ_PREFIX', 'NaodW29');
52 # Constants needed for external link processing
53 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
54 define( 'HTTP_PROTOCOLS', 'http|https' );
55 # Everything except bracket, space, or control characters
56 define( 'EXT_LINK_URL_CLASS', '[^]\\x00-\\x20\\x7F]' );
57 define( 'INVERSE_EXT_LINK_URL_CLASS', '[\]\\x00-\\x20\\x7F]' );
59 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
60 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
61 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
62 define( 'EXT_LINK_BRACKETED', '/\[(('.URL_PROTOCOLS
.'):'.EXT_LINK_URL_CLASS
.'+) *('.EXT_LINK_TEXT_CLASS
.'*?)\]/S' );
63 define( 'EXT_IMAGE_REGEX',
64 '/^('.HTTP_PROTOCOLS
.':)'. # Protocol
65 '('.EXT_LINK_URL_CLASS
.'+)\\/'. # Hostname and path
66 '('.EXT_IMAGE_FNAME_CLASS
.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS
.')$/S' # Filename
74 # Cleared with clearState():
75 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
76 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
79 var $mOptions, $mTitle, $mOutputType,
80 $mTemplates, // cache of already loaded templates, avoids
81 // multiple SQL queries for the same string
82 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
83 // in this path. Used for loop detection.
86 $this->mTemplates
= array();
87 $this->mTemplatePath
= array();
88 $this->mTagHooks
= array();
92 function clearState() {
93 $this->mOutput
= new ParserOutput
;
94 $this->mAutonumber
= 0;
95 $this->mLastSection
= "";
96 $this->mDTopen
= false;
97 $this->mVariables
= false;
98 $this->mIncludeCount
= array();
99 $this->mStripState
= array();
100 $this->mArgStack
= array();
101 $this->mInPre
= false;
104 # First pass--just handle <nowiki> sections, pass the rest off
105 # to internalParse() which does all the real work.
107 # Returns a ParserOutput
109 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
111 $fname = 'Parser::parse';
112 wfProfileIn( $fname );
118 $this->mOptions
= $options;
119 $this->mTitle
=& $title;
120 $this->mOutputType
= OT_HTML
;
123 $text = $this->strip( $text, $this->mStripState
);
124 $text = $this->internalParse( $text, $linestart );
125 $text = $this->unstrip( $text, $this->mStripState
);
126 # Clean up special characters, only run once, next-to-last before doBlockLevels
129 # french spaces, last one Guillemet-left
130 # only if there is something before the space
131 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
132 # french spaces, Guillemet-right
133 "/(\\302\\253) /i"=>"\\1 ",
134 '/<hr *>/i' => '<hr />',
135 '/<br *>/i' => '<br />',
136 '/<center *>/i' => '<div class="center">',
137 '/<\\/center *>/i' => '</div>',
138 # Clean up spare ampersands; note that we probably ought to be
139 # more careful about named entities.
140 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
142 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
145 # french spaces, last one Guillemet-left
146 '/ (\\?|:|;|!|\\302\\273)/i' => ' \\1',
147 # french spaces, Guillemet-right
148 '/(\\302\\253) /i' => '\\1 ',
149 '/<center *>/i' => '<div class="center">',
150 '/<\\/center *>/i' => '</div>'
152 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
155 $text = $this->doBlockLevels( $text, $linestart );
156 $text = $this->unstripNoWiki( $text, $this->mStripState
);
158 $text = $this->tidy($text);
160 $this->mOutput
->setText( $text );
161 wfProfileOut( $fname );
162 return $this->mOutput
;
165 /* static */ function getRandomString() {
166 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
169 # Replaces all occurrences of <$tag>content</$tag> in the text
170 # with a random marker and returns the new text. the output parameter
171 # $content will be an associative array filled with data on the form
172 # $unique_marker => content.
174 # If $content is already set, the additional entries will be appended
176 # If $tag is set to STRIP_COMMENTS, the function will extract
177 # <!-- HTML comments -->
179 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ''){
180 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
187 while ( '' != $text ) {
188 if($tag==STRIP_COMMENTS
) {
189 $p = preg_split( '/<!--/i', $text, 2 );
191 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
194 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
197 if($tag==STRIP_COMMENTS
) {
198 $q = preg_split( '/-->/i', $p[1], 2 );
200 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
202 $marker = $rnd . sprintf('%08X', $n++
);
203 $content[$marker] = $q[0];
204 $stripped .= $marker;
211 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
212 # If $render is set, performs necessary rendering operations on plugins
213 # Returns the text, and fills an array with data needed in unstrip()
214 # If the $state is already a valid strip state, it adds to the state
216 # When $stripcomments is set, HTML comments <!-- like this -->
217 # will be stripped in addition to other tags. This is important
218 # for section editing, where these comments cause confusion when
219 # counting the sections in the wikisource
220 function strip( $text, &$state, $stripcomments = false ) {
221 $render = ($this->mOutputType
== OT_HTML
);
222 $html_content = array();
223 $nowiki_content = array();
224 $math_content = array();
225 $pre_content = array();
226 $comment_content = array();
227 $ext_content = array();
229 # Replace any instances of the placeholders
230 $uniq_prefix = UNIQ_PREFIX
;
231 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
236 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
237 foreach( $html_content as $marker => $content ) {
239 # Raw and unchecked for validity.
240 $html_content[$marker] = $content;
242 $html_content[$marker] = '<html>'.$content.'</html>';
248 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
249 foreach( $nowiki_content as $marker => $content ) {
251 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
253 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
258 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
259 foreach( $math_content as $marker => $content ){
261 if( $this->mOptions
->getUseTeX() ) {
262 $math_content[$marker] = renderMath( $content );
264 $math_content[$marker] = '<math>'.$content.'<math>';
267 $math_content[$marker] = '<math>'.$content.'</math>';
272 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
273 foreach( $pre_content as $marker => $content ){
275 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
277 $pre_content[$marker] = '<pre>'.$content.'</pre>';
283 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
284 foreach( $comment_content as $marker => $content ){
285 $comment_content[$marker] = '<!--'.$content.'-->';
290 foreach ( $this->mTagHooks
as $tag => $callback ) {
291 $ext_contents[$tag] = array();
292 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
293 foreach( $ext_content[$tag] as $marker => $content ) {
295 $ext_content[$tag][$marker] = $callback( $content );
297 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
302 # Merge state with the pre-existing state, if there is one
304 $state['html'] = $state['html'] +
$html_content;
305 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
306 $state['math'] = $state['math'] +
$math_content;
307 $state['pre'] = $state['pre'] +
$pre_content;
308 $state['comment'] = $state['comment'] +
$comment_content;
310 foreach( $ext_content as $tag => $array ) {
311 if ( array_key_exists( $tag, $state ) ) {
312 $state[$tag] = $state[$tag] +
$array;
317 'html' => $html_content,
318 'nowiki' => $nowiki_content,
319 'math' => $math_content,
320 'pre' => $pre_content,
321 'comment' => $comment_content,
327 # always call unstripNoWiki() after this one
328 function unstrip( $text, &$state ) {
329 # Must expand in reverse order, otherwise nested tags will be corrupted
330 $contentDict = end( $state );
331 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
332 if( key($state) != 'nowiki' && key($state) != 'html') {
333 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
334 $text = str_replace( key( $contentDict ), $content, $text );
341 # always call this after unstrip() to preserve the order
342 function unstripNoWiki( $text, &$state ) {
343 # Must expand in reverse order, otherwise nested tags will be corrupted
344 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
345 $text = str_replace( key( $state['nowiki'] ), $content, $text );
350 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
351 $text = str_replace( key( $state['html'] ), $content, $text );
358 # Add an item to the strip state
359 # Returns the unique tag which must be inserted into the stripped text
360 # The tag will be replaced with the original text in unstrip()
361 function insertStripItem( $text, &$state ) {
362 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
371 $state['item'][$rnd] = $text;
375 # Return allowed HTML attributes
376 function getHTMLattrs () {
377 $htmlattrs = array( # Allowed attributes--no scripting, etc.
378 'title', 'align', 'lang', 'dir', 'width', 'height',
379 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
380 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
381 /* FONT */ 'type', 'start', 'value', 'compact',
382 /* For various lists, mostly deprecated but safe */
383 'summary', 'width', 'border', 'frame', 'rules',
384 'cellspacing', 'cellpadding', 'valign', 'char',
385 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
386 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
387 'id', 'class', 'name', 'style' /* For CSS */
392 # Remove non approved attributes and javascript in css
393 function fixTagAttributes ( $t ) {
394 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
395 $htmlattrs = $this->getHTMLattrs() ;
397 # Strip non-approved attributes from the tag
399 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
400 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
403 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
405 # Strip javascript "expression" from stylesheets. Brute force approach:
406 # If anythin offensive is found, all attributes of the HTML tag are dropped
409 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
410 wfMungeToUtf8( $t ) ) )
418 # interface with html tidy, used if $wgUseTidy = true
419 function tidy ( $text ) {
420 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
421 global $wgInputEncoding, $wgOutputEncoding;
422 $fname = 'Parser::tidy';
423 wfProfileIn( $fname );
426 switch(strtoupper($wgOutputEncoding)) {
428 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
431 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
434 $wgTidyOpts .= ' -raw';
437 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
438 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
439 '<head><title>test</title></head><body>'.$text.'</body></html>';
440 $descriptorspec = array(
441 0 => array('pipe', 'r'),
442 1 => array('pipe', 'w'),
443 2 => array('file', '/dev/null', 'a')
445 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
446 if (is_resource($process)) {
447 fwrite($pipes[0], $wrappedtext);
449 while (!feof($pipes[1])) {
450 $cleansource .= fgets($pipes[1], 1024);
453 $return_value = proc_close($process);
456 wfProfileOut( $fname );
458 if( $cleansource == '' && $text != '') {
459 wfDebug( "Tidy error detected!\n" );
460 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
466 # parse the wiki syntax used to render tables
467 function doTableStuff ( $t ) {
468 $fname = 'Parser::doTableStuff';
469 wfProfileIn( $fname );
471 $t = explode ( "\n" , $t ) ;
472 $td = array () ; # Is currently a td tag open?
473 $ltd = array () ; # Was it TD or TH?
474 $tr = array () ; # Is currently a tr tag open?
475 $ltr = array () ; # tr attributes
476 $indent_level = 0; # indent level of the table
477 foreach ( $t AS $k => $x )
480 $fc = substr ( $x , 0 , 1 ) ;
481 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
482 $indent_level = strlen( $matches[1] );
484 str_repeat( '<dl><dd>', $indent_level ) .
485 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
486 array_push ( $td , false ) ;
487 array_push ( $ltd , '' ) ;
488 array_push ( $tr , false ) ;
489 array_push ( $ltr , '' ) ;
491 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
492 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
494 $l = array_pop ( $ltd ) ;
495 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
496 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
498 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
500 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
501 $x = substr ( $x , 1 ) ;
502 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
504 $l = array_pop ( $ltd ) ;
505 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
506 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
509 array_push ( $tr , false ) ;
510 array_push ( $td , false ) ;
511 array_push ( $ltd , '' ) ;
512 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
514 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) { # Caption
515 if ( '|+' == substr ( $x , 0 , 2 ) ) {
517 $x = substr ( $x , 1 ) ;
519 $after = substr ( $x , 1 ) ;
520 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
521 $after = explode ( '||' , $after ) ;
523 foreach ( $after AS $theline )
528 $tra = array_pop ( $ltr ) ;
529 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
530 array_push ( $tr , true ) ;
531 array_push ( $ltr , '' ) ;
534 $l = array_pop ( $ltd ) ;
535 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
536 if ( $fc == '|' ) $l = 'td' ;
537 else if ( $fc == '!' ) $l = 'th' ;
538 else if ( $fc == '+' ) $l = 'caption' ;
540 array_push ( $ltd , $l ) ;
541 $y = explode ( '|' , $theline , 2 ) ;
542 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
543 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
545 array_push ( $td , true ) ;
550 # Closing open td, tr && table
551 while ( count ( $td ) > 0 )
553 if ( array_pop ( $td ) ) $t[] = '</td>' ;
554 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
558 $t = implode ( "\n" , $t ) ;
559 # $t = $this->removeHTMLtags( $t );
560 wfProfileOut( $fname );
564 # Parses the text and adds the result to the strip state
565 # Returns the strip tag
566 function stripParse( $text, $newline, $args ) {
567 $text = $this->strip( $text, $this->mStripState
);
568 $text = $this->internalParse( $text, (bool)$newline, $args, false );
569 return $newline.$this->insertStripItem( $text, $this->mStripState
);
572 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
573 $fname = 'Parser::internalParse';
574 wfProfileIn( $fname );
576 $text = $this->removeHTMLtags( $text );
577 $text = $this->replaceVariables( $text, $args );
579 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
581 $text = $this->doHeadings( $text );
582 if($this->mOptions
->getUseDynamicDates()) {
583 global $wgDateFormatter;
584 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
586 $text = $this->doAllQuotes( $text );
587 $text = $this->replaceExternalLinks( $text );
588 $text = $this->doMagicLinks( $text );
589 $text = $this->replaceInternalLinks ( $text );
590 $text = $this->replaceInternalLinks ( $text );
592 $text = $this->unstrip( $text, $this->mStripState
);
593 $text = $this->unstripNoWiki( $text, $this->mStripState
);
595 $text = $this->doTableStuff( $text );
596 $text = $this->formatHeadings( $text, $isMain );
597 $sk =& $this->mOptions
->getSkin();
598 $text = $sk->transformContent( $text );
600 wfProfileOut( $fname );
604 /* private */ function &doMagicLinks( &$text ) {
605 global $wgUseGeoMode;
606 $text = $this->magicISBN( $text );
607 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
608 $text = $this->magicGEO( $text );
610 $text = $this->magicRFC( $text );
614 # Parse ^^ tokens and return html
615 /* private */ function doExponent ( $text ) {
616 $fname = 'Parser::doExponent';
617 wfProfileIn( $fname);
618 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
619 wfProfileOut( $fname);
623 # Parse headers and return html
624 /* private */ function doHeadings( $text ) {
625 $fname = 'Parser::doHeadings';
626 wfProfileIn( $fname );
627 for ( $i = 6; $i >= 1; --$i ) {
628 $h = substr( '======', 0, $i );
629 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
630 "<h{$i}>\\1</h{$i}>\\2", $text );
632 wfProfileOut( $fname );
636 /* private */ function doAllQuotes( $text ) {
637 $fname = 'Parser::doAllQuotes';
638 wfProfileIn( $fname );
640 $lines = explode( "\n", $text );
641 foreach ( $lines as $line ) {
642 $outtext .= $this->doQuotes ( $line ) . "\n";
644 $outtext = substr($outtext, 0,-1);
645 wfProfileOut( $fname );
649 /* private */ function doQuotes( $text ) {
650 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
651 if (count ($arr) == 1)
655 # First, do some preliminary work. This may shift some apostrophes from
656 # being mark-up to being text. It also counts the number of occurrences
657 # of bold and italics mark-ups.
665 # If there are ever four apostrophes, assume the first is supposed to
666 # be text, and the remaining three constitute mark-up for bold text.
667 if (strlen ($arr[$i]) == 4)
672 # If there are more than 5 apostrophes in a row, assume they're all
673 # text except for the last 5.
674 else if (strlen ($arr[$i]) > 5)
676 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
679 # Count the number of occurrences of bold and italics mark-ups.
680 # We are not counting sequences of five apostrophes.
681 if (strlen ($arr[$i]) == 2) $numitalics++
; else
682 if (strlen ($arr[$i]) == 3) $numbold++
; else
683 if (strlen ($arr[$i]) == 5) { $numitalics++
; $numbold++
; }
688 # If there is an odd number of both bold and italics, it is likely
689 # that one of the bold ones was meant to be an apostrophe followed
690 # by italics. Which one we cannot know for certain, but it is more
691 # likely to be one that has a single-letter word before it.
692 if (($numbold %
2 == 1) && ($numitalics %
2 == 1))
695 $firstsingleletterword = -1;
696 $firstmultiletterword = -1;
700 if (($i %
2 == 1) and (strlen ($r) == 3))
702 $x1 = substr ($arr[$i-1], -1);
703 $x2 = substr ($arr[$i-1], -2, 1);
705 if ($firstspace == -1) $firstspace = $i;
706 } else if ($x2 == ' ') {
707 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
709 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
715 # If there is a single-letter word, use it!
716 if ($firstsingleletterword > -1)
718 $arr [ $firstsingleletterword ] = "''";
719 $arr [ $firstsingleletterword-1 ] .= "'";
721 # If not, but there's a multi-letter word, use that one.
722 else if ($firstmultiletterword > -1)
724 $arr [ $firstmultiletterword ] = "''";
725 $arr [ $firstmultiletterword-1 ] .= "'";
727 # ... otherwise use the first one that has neither.
728 # (notice that it is possible for all three to be -1 if, for example,
729 # there is only one pentuple-apostrophe in the line)
730 else if ($firstspace > -1)
732 $arr [ $firstspace ] = "''";
733 $arr [ $firstspace-1 ] .= "'";
737 # Now let's actually convert our apostrophic mush to HTML!
746 if ($state == 'both')
753 if (strlen ($r) == 2)
756 { $output .= '</em>'; $state = ''; }
757 else if ($state == 'strongem')
758 { $output .= '</em>'; $state = 'strong'; }
759 else if ($state == 'emstrong')
760 { $output .= '</strong></em><strong>'; $state = 'strong'; }
761 else if ($state == 'both')
762 { $output .= '<strong><em>'.$buffer.'</em>'; $state = 'strong'; }
763 else # $state can be 'strong' or ''
764 { $output .= '<em>'; $state .= 'em'; }
766 else if (strlen ($r) == 3)
768 if ($state == 'strong')
769 { $output .= '</strong>'; $state = ''; }
770 else if ($state == 'strongem')
771 { $output .= '</em></strong><em>'; $state = 'em'; }
772 else if ($state == 'emstrong')
773 { $output .= '</strong>'; $state = 'em'; }
774 else if ($state == 'both')
775 { $output .= '<em><strong>'.$buffer.'</strong>'; $state = 'em'; }
776 else # $state can be 'em' or ''
777 { $output .= '<strong>'; $state .= 'strong'; }
779 else if (strlen ($r) == 5)
781 if ($state == 'strong')
782 { $output .= '</strong><em>'; $state = 'em'; }
783 else if ($state == 'em')
784 { $output .= '</em><strong>'; $state = 'strong'; }
785 else if ($state == 'strongem')
786 { $output .= '</em></strong>'; $state = ''; }
787 else if ($state == 'emstrong')
788 { $output .= '</strong></em>'; $state = ''; }
789 else if ($state == 'both')
790 { $output .= '<em><strong>'.$buffer.'</strong></em>'; $state = ''; }
791 else # ($state == '')
792 { $buffer = ''; $state = 'both'; }
797 # Now close all remaining tags. Notice that the order is important.
798 if ($state == 'strong' ||
$state == 'emstrong')
799 $output .= '</strong>';
800 if ($state == 'em' ||
$state == 'strongem' ||
$state == 'emstrong')
802 if ($state == 'strongem')
803 $output .= '</strong>';
804 if ($state == 'both')
805 $output .= '<strong><em>'.$buffer.'</em></strong>';
810 # Note: we have to do external links before the internal ones,
811 # and otherwise take great care in the order of things here, so
812 # that we don't end up interpreting some URLs twice.
814 /* private */ function replaceExternalLinks( $text ) {
815 $fname = 'Parser::replaceExternalLinks';
816 wfProfileIn( $fname );
818 $sk =& $this->mOptions
->getSkin();
819 $linktrail = wfMsg('linktrail');
820 $bits = preg_split( EXT_LINK_BRACKETED
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
822 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
825 while ( $i<count( $bits ) ) {
827 $protocol = $bits[$i++
];
829 $trail = $bits[$i++
];
831 # If the link text is an image URL, replace it with an <img> tag
832 # This happened by accident in the original parser, but some people used it extensively
833 $img = $this->maybeMakeImageLink( $text );
834 if ( $img !== false ) {
840 # No link text, e.g. [http://domain.tld/some.link]
842 # Autonumber if allowed
843 if ( strpos( HTTP_PROTOCOLS
, $protocol ) !== false ) {
844 $text = '[' . ++
$this->mAutonumber
. ']';
846 # Otherwise just use the URL
847 $text = htmlspecialchars( $url );
850 # Have link text, e.g. [http://domain.tld/some.link text]s
852 if ( preg_match( $linktrail, $trail, $m2 ) ) {
858 $encUrl = htmlspecialchars( $url );
859 # Bit in parentheses showing the URL for the printable version
860 if( $url == $text ||
preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
863 # Expand the URL for printable version
864 if ( ! $sk->suppressUrlExpansion() ) {
865 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
871 # Process the trail (i.e. everything after this link up until start of the next link),
872 # replacing any non-bracketed links
873 $trail = $this->replaceFreeExternalLinks( $trail );
875 $la = $sk->getExternalLinkAttributes( $url, $text );
877 # Use the encoded URL
878 # This means that users can paste URLs directly into the text
879 # Funny characters like ö aren't valid in URLs anyway
880 # This was changed in August 2004
881 $s .= "<a href=\"{$url}\" {$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
884 wfProfileOut( $fname );
888 # Replace anything that looks like a URL with a link
889 function replaceFreeExternalLinks( $text ) {
890 $bits = preg_split( '/((?:'.URL_PROTOCOLS
.'):)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
891 $s = array_shift( $bits );
894 $sk =& $this->mOptions
->getSkin();
896 while ( $i < count( $bits ) ){
897 $protocol = $bits[$i++
];
898 $remainder = $bits[$i++
];
900 if ( preg_match( '/^('.EXT_LINK_URL_CLASS
.'+)(.*)$/s', $remainder, $m ) ) {
901 # Found some characters after the protocol that look promising
902 $url = $protocol . $m[1];
905 # Move trailing punctuation to $trail
907 # If there is no left bracket, then consider right brackets fair game too
908 if ( strpos( $url, '(' ) === false ) {
912 $numSepChars = strspn( strrev( $url ), $sep );
913 if ( $numSepChars ) {
914 $trail = substr( $url, -$numSepChars ) . $trail;
915 $url = substr( $url, 0, -$numSepChars );
918 # Replace & from obsolete syntax with &
919 $url = str_replace( '&', '&', $url );
921 # Is this an external image?
922 $text = $this->maybeMakeImageLink( $url );
923 if ( $text === false ) {
924 # Not an image, make a link
925 $text = $sk->makeExternalLink( $url, $url );
927 $s .= $text . $trail;
929 $s .= $protocol . $remainder;
935 # make an image if it's allowed
936 function maybeMakeImageLink( $url ) {
937 $sk =& $this->mOptions
->getSkin();
939 if ( $this->mOptions
->getAllowExternalImages() ) {
940 if ( preg_match( EXT_IMAGE_REGEX
, $url ) ) {
942 $text = $sk->makeImage( htmlspecialchars( $url ) );
948 # The wikilinks [[ ]] are procedeed here.
949 /* private */ function replaceInternalLinks( $s ) {
950 global $wgLang, $wgLinkCache;
951 global $wgNamespacesWithSubpages, $wgLanguageCode;
952 static $fname = 'Parser::replaceInternalLinks' ;
953 wfProfileIn( $fname );
955 wfProfileIn( $fname.'-setup' );
957 # the % is needed to support urlencoded titles as well
958 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
959 $sk =& $this->mOptions
->getSkin();
961 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
963 $a = explode( '[[', ' ' . $s );
964 $s = array_shift( $a );
965 $s = substr( $s, 1 );
967 # Match a link having the form [[namespace:link|alternate]]trail
969 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
970 # Match the end of a line for a word that's not followed by whitespace,
971 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
972 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
974 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
975 # Special and Media are pseudo-namespaces; no pages actually exist in them
977 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
979 if ( $useLinkPrefixExtension ) {
980 if ( preg_match( $e2, $s, $m ) ) {
981 $first_prefix = $m[2];
984 $first_prefix = false;
990 wfProfileOut( $fname.'-setup' );
992 # start procedeeding each line
993 foreach ( $a as $line ) {
994 wfProfileIn( $fname.'-prefixhandling' );
995 if ( $useLinkPrefixExtension ) {
996 if ( preg_match( $e2, $s, $m ) ) {
1004 $prefix = $first_prefix;
1005 $first_prefix = false;
1008 wfProfileOut( $fname.'-prefixhandling' );
1010 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1012 # fix up urlencoded title texts
1013 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1015 } else { # Invalid form; output directly
1016 $s .= $prefix . '[[' . $line ;
1022 # :Foobar -- override special treatment of prefix (images, language links)
1023 # /Foobar -- convert to CurrentPage/Foobar
1024 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1026 # Look at the first character
1027 $c = substr($m[1],0,1);
1028 $noforce = ($c != ':');
1032 # / at end means we don't want the slash to be shown
1033 if(substr($m[1],-1,1)=='/') {
1034 $m[1]=substr($m[1],1,strlen($m[1])-2);
1037 $noslash=substr($m[1],1);
1040 # Some namespaces don't allow subpages
1041 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) {
1042 # subpages allowed here
1043 $link = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1046 } # this might be changed for ugliness reasons
1048 # no subpage allowed, use standard link
1052 } elseif( $noforce ) { # no subpage
1055 # We don't want to keep the first character
1056 $link = substr( $m[1], 1 );
1059 $wasblank = ( '' == $text );
1060 if( $wasblank ) $text = $link;
1062 $nt = Title
::newFromText( $link );
1064 $s .= $prefix . '[[' . $line;
1068 $ns = $nt->getNamespace();
1069 $iw = $nt->getInterWiki();
1071 # Link not escaped by : , create the various objects
1075 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1076 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1077 $tmp = $prefix . $trail ;
1078 $s .= (trim($tmp) == '')?
'': $tmp;
1082 if ( $ns == NS_IMAGE
) {
1083 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1084 $wgLinkCache->addImageLinkObj( $nt );
1088 if ( $ns == NS_CATEGORY
) {
1089 $t = $nt->getText() ;
1090 $nnt = Title
::newFromText ( Namespace::getCanonicalName(NS_CATEGORY
).':'.$t ) ;
1092 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1093 $pPLC=$sk->postParseLinkColour();
1094 $sk->postParseLinkColour( false );
1095 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1096 $sk->postParseLinkColour( $pPLC );
1097 $wgLinkCache->resume();
1099 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
1100 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1101 $this->mOutput
->mCategoryLinks
[] = $t ;
1102 $s .= $prefix . $trail ;
1107 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1108 ( strpos( $link, '#' ) == FALSE ) ) {
1109 # Self-links are handled specially; generally de-link and change to bold.
1110 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1114 if( $ns == NS_MEDIA
) {
1115 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1116 $wgLinkCache->addImageLinkObj( $nt );
1118 } elseif( $ns == NS_SPECIAL
) {
1119 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1122 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1124 wfProfileOut( $fname );
1128 # Some functions here used by doBlockLevels()
1130 /* private */ function closeParagraph() {
1132 if ( '' != $this->mLastSection
) {
1133 $result = '</' . $this->mLastSection
. ">\n";
1135 $this->mInPre
= false;
1136 $this->mLastSection
= '';
1139 # getCommon() returns the length of the longest common substring
1140 # of both arguments, starting at the beginning of both.
1142 /* private */ function getCommon( $st1, $st2 ) {
1143 $fl = strlen( $st1 );
1144 $shorter = strlen( $st2 );
1145 if ( $fl < $shorter ) { $shorter = $fl; }
1147 for ( $i = 0; $i < $shorter; ++
$i ) {
1148 if ( $st1{$i} != $st2{$i} ) { break; }
1152 # These next three functions open, continue, and close the list
1153 # element appropriate to the prefix character passed into them.
1155 /* private */ function openList( $char ) {
1156 $result = $this->closeParagraph();
1158 if ( '*' == $char ) { $result .= '<ul><li>'; }
1159 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1160 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1161 else if ( ';' == $char ) {
1162 $result .= '<dl><dt>';
1163 $this->mDTopen
= true;
1165 else { $result = '<!-- ERR 1 -->'; }
1170 /* private */ function nextItem( $char ) {
1171 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1172 else if ( ':' == $char ||
';' == $char ) {
1174 if ( $this->mDTopen
) { $close = '</dt>'; }
1175 if ( ';' == $char ) {
1176 $this->mDTopen
= true;
1177 return $close . '<dt>';
1179 $this->mDTopen
= false;
1180 return $close . '<dd>';
1183 return '<!-- ERR 2 -->';
1186 /* private */ function closeList( $char ) {
1187 if ( '*' == $char ) { $text = '</li></ul>'; }
1188 else if ( '#' == $char ) { $text = '</li></ol>'; }
1189 else if ( ':' == $char ) {
1190 if ( $this->mDTopen
) {
1191 $this->mDTopen
= false;
1192 $text = '</dt></dl>';
1194 $text = '</dd></dl>';
1197 else { return '<!-- ERR 3 -->'; }
1201 /* private */ function doBlockLevels( $text, $linestart ) {
1202 $fname = 'Parser::doBlockLevels';
1203 wfProfileIn( $fname );
1205 # Parsing through the text line by line. The main thing
1206 # happening here is handling of block-level elements p, pre,
1207 # and making lists from lines starting with * # : etc.
1209 $textLines = explode( "\n", $text );
1211 $lastPrefix = $output = $lastLine = '';
1212 $this->mDTopen
= $inBlockElem = false;
1214 $paragraphStack = false;
1216 if ( !$linestart ) {
1217 $output .= array_shift( $textLines );
1219 foreach ( $textLines as $oLine ) {
1220 $lastPrefixLength = strlen( $lastPrefix );
1221 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1222 $preOpenMatch = preg_match('/<pre/i', $oLine );
1223 if ( !$this->mInPre
) {
1224 # Multiple prefixes may abut each other for nested lists.
1225 $prefixLength = strspn( $oLine, '*#:;' );
1226 $pref = substr( $oLine, 0, $prefixLength );
1229 $pref2 = str_replace( ';', ':', $pref );
1230 $t = substr( $oLine, $prefixLength );
1231 $this->mInPre
= !empty($preOpenMatch);
1233 # Don't interpret any other prefixes in preformatted text
1235 $pref = $pref2 = '';
1240 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1241 # Same as the last item, so no need to deal with nesting or opening stuff
1242 $output .= $this->nextItem( substr( $pref, -1 ) );
1243 $paragraphStack = false;
1245 if ( substr( $pref, -1 ) == ';') {
1246 # The one nasty exception: definition lists work like this:
1247 # ; title : definition text
1248 # So we check for : in the remainder text to split up the
1249 # title and definition, without b0rking links.
1250 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1251 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1253 $output .= $term . $this->nextItem( ':' );
1257 } elseif( $prefixLength ||
$lastPrefixLength ) {
1258 # Either open or close a level...
1259 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1260 $paragraphStack = false;
1262 while( $commonPrefixLength < $lastPrefixLength ) {
1263 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1264 --$lastPrefixLength;
1266 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1267 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1269 while ( $prefixLength > $commonPrefixLength ) {
1270 $char = substr( $pref, $commonPrefixLength, 1 );
1271 $output .= $this->openList( $char );
1273 if ( ';' == $char ) {
1274 # FIXME: This is dupe of code above
1275 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1277 $output .= $term . $this->nextItem( ':' );
1281 ++
$commonPrefixLength;
1283 $lastPrefix = $pref2;
1285 if( 0 == $prefixLength ) {
1286 # No prefix (not in list)--go to paragraph mode
1287 $uniq_prefix = UNIQ_PREFIX
;
1288 // XXX: use a stack for nestable elements like span, table and div
1289 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1290 $closematch = preg_match(
1291 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1292 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1293 if ( $openmatch or $closematch ) {
1294 $paragraphStack = false;
1295 $output .= $this->closeParagraph();
1296 if($preOpenMatch and !$preCloseMatch) {
1297 $this->mInPre
= true;
1299 if ( $closematch ) {
1300 $inBlockElem = false;
1302 $inBlockElem = true;
1304 } else if ( !$inBlockElem && !$this->mInPre
) {
1305 if ( ' ' == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1307 if ($this->mLastSection
!= 'pre') {
1308 $paragraphStack = false;
1309 $output .= $this->closeParagraph().'<pre>';
1310 $this->mLastSection
= 'pre';
1314 if ( '' == trim($t) ) {
1315 if ( $paragraphStack ) {
1316 $output .= $paragraphStack.'<br />';
1317 $paragraphStack = false;
1318 $this->mLastSection
= 'p';
1320 if ($this->mLastSection
!= 'p' ) {
1321 $output .= $this->closeParagraph();
1322 $this->mLastSection
= '';
1323 $paragraphStack = '<p>';
1325 $paragraphStack = '</p><p>';
1329 if ( $paragraphStack ) {
1330 $output .= $paragraphStack;
1331 $paragraphStack = false;
1332 $this->mLastSection
= 'p';
1333 } else if ($this->mLastSection
!= 'p') {
1334 $output .= $this->closeParagraph().'<p>';
1335 $this->mLastSection
= 'p';
1341 if ($paragraphStack === false) {
1345 while ( $prefixLength ) {
1346 $output .= $this->closeList( $pref2{$prefixLength-1} );
1349 if ( '' != $this->mLastSection
) {
1350 $output .= '</' . $this->mLastSection
. '>';
1351 $this->mLastSection
= '';
1354 wfProfileOut( $fname );
1358 # Return value of a magic variable (like PAGENAME)
1359 function getVariableValue( $index ) {
1360 global $wgLang, $wgSitename, $wgServer;
1363 case MAG_CURRENTMONTH
:
1364 return $wgLang->formatNum( date( 'm' ) );
1365 case MAG_CURRENTMONTHNAME
:
1366 return $wgLang->getMonthName( date('n') );
1367 case MAG_CURRENTMONTHNAMEGEN
:
1368 return $wgLang->getMonthNameGen( date('n') );
1369 case MAG_CURRENTDAY
:
1370 return $wgLang->formatNum( date('j') );
1372 return $this->mTitle
->getText();
1374 return $this->mTitle
->getPartialURL();
1376 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1377 return $wgLang->getNsText($this->mTitle
->getNamespace()); # Patch by Dori
1378 case MAG_CURRENTDAYNAME
:
1379 return $wgLang->getWeekdayName( date('w')+
1 );
1380 case MAG_CURRENTYEAR
:
1381 return $wgLang->formatNum( date( 'Y' ) );
1382 case MAG_CURRENTTIME
:
1383 return $wgLang->time( wfTimestampNow(), false );
1384 case MAG_NUMBEROFARTICLES
:
1385 return $wgLang->formatNum( wfNumberOfArticles() );
1395 # initialise the magic variables (like CURRENTMONTHNAME)
1396 function initialiseVariables() {
1397 global $wgVariableIDs;
1398 $this->mVariables
= array();
1399 foreach ( $wgVariableIDs as $id ) {
1400 $mw =& MagicWord
::get( $id );
1401 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1405 /* private */ function replaceVariables( $text, $args = array() ) {
1406 global $wgLang, $wgScript, $wgArticlePath;
1408 # Prevent too big inclusions
1409 if(strlen($text)> MAX_INCLUDE_SIZE
)
1412 $fname = 'Parser::replaceVariables';
1413 wfProfileIn( $fname );
1416 $titleChars = Title
::legalChars();
1417 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1419 # This function is called recursively. To keep track of arguments we need a stack:
1420 array_push( $this->mArgStack
, $args );
1422 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1423 $GLOBALS['wgCurParser'] =& $this;
1425 if ( $this->mOutputType
== OT_HTML ||
$this->mOutputType
== OT_MSG
) {
1426 # Variable substitution
1427 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1430 if ( $this->mOutputType
== OT_HTML
) {
1431 # Argument substitution
1432 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1434 # Template substitution
1435 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1436 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1438 array_pop( $this->mArgStack
);
1440 wfProfileOut( $fname );
1444 function variableSubstitution( $matches ) {
1445 if ( !$this->mVariables
) {
1446 $this->initialiseVariables();
1448 if ( array_key_exists( $matches[1], $this->mVariables
) ) {
1449 $text = $this->mVariables
[$matches[1]];
1450 $this->mOutput
->mContainsOldMagic
= true;
1452 $text = $matches[0];
1457 # Split template arguments
1458 function getTemplateArgs( $argsString ) {
1459 if ( $argsString === '' ) {
1463 $args = explode( '|', substr( $argsString, 1 ) );
1465 # If any of the arguments contains a '[[' but no ']]', it needs to be
1466 # merged with the next arg because the '|' character between belongs
1467 # to the link syntax and not the template parameter syntax.
1468 $argc = count($args);
1470 for ( $i = 0; $i < $argc-1; $i++
) {
1471 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1472 $args[$i] .= '|'.$args[$i+
1];
1473 array_splice($args, $i+
1, 1);
1482 function braceSubstitution( $matches ) {
1483 global $wgLinkCache, $wgLang;
1484 $fname = 'Parser::braceSubstitution';
1491 # $newline is an optional newline character before the braces
1492 # $part1 is the bit before the first |, and must contain only title characters
1493 # $args is a list of arguments, starting from index 0, not including $part1
1495 $newline = $matches[1];
1496 $part1 = $matches[2];
1497 # If the third subpattern matched anything, it will start with |
1499 $args = $this->getTemplateArgs($matches[3]);
1500 $argc = count( $args );
1503 if ( strpos( $matches[0], '{{{' ) !== false ) {
1504 $text = $matches[0];
1511 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1512 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1513 if ( $this->mOutputType
!= OT_WIKI
) {
1514 # Invalid SUBST not replaced at PST time
1515 # Return without further processing
1516 $text = $matches[0];
1520 } elseif ( $this->mOutputType
== OT_WIKI
) {
1521 # SUBST not found in PST pass, do nothing
1522 $text = $matches[0];
1527 # MSG, MSGNW and INT
1530 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1531 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1534 # Remove obsolete MSG:
1535 $mwMsg =& MagicWord
::get( MAG_MSG
);
1536 $mwMsg->matchStartAndRemove( $part1 );
1539 # Check if it is an internal message
1540 $mwInt =& MagicWord
::get( MAG_INT
);
1541 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1542 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1543 $text = wfMsgReal( $part1, $args, true );
1551 # Check for NS: (namespace expansion)
1552 $mwNs = MagicWord
::get( MAG_NS
);
1553 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1554 if ( intval( $part1 ) ) {
1555 $text = $wgLang->getNsText( intval( $part1 ) );
1558 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1559 if ( !is_null( $index ) ) {
1560 $text = $wgLang->getNsText( $index );
1567 # LOCALURL and LOCALURLE
1569 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1570 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1572 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1573 $func = 'getLocalURL';
1574 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1575 $func = 'escapeLocalURL';
1580 if ( $func !== '' ) {
1581 $title = Title
::newFromText( $part1 );
1582 if ( !is_null( $title ) ) {
1584 $text = $title->$func( $args[0] );
1586 $text = $title->$func();
1593 # Internal variables
1594 if ( !$this->mVariables
) {
1595 $this->initialiseVariables();
1597 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1598 $text = $this->mVariables
[$part1];
1600 $this->mOutput
->mContainsOldMagic
= true;
1604 if ( !$found && $argc == 1 ) {
1605 $mwGrammar =& MagicWord
::get( MAG_GRAMMAR
);
1606 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1607 $text = $wgLang->convertGrammar( $args[0], $part1 );
1612 # Template table test
1614 # Did we encounter this template already? If yes, it is in the cache
1615 # and we need to check for loops.
1616 if ( isset( $this->mTemplates
[$part1] ) ) {
1617 # Infinite loop test
1618 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1622 # set $text to cached message.
1623 $text = $this->mTemplates
[$part1];
1627 # Load from database
1629 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1630 if ( !is_null( $title ) && !$title->isExternal() ) {
1631 # Check for excessive inclusion
1632 $dbk = $title->getPrefixedDBkey();
1633 if ( $this->incrementIncludeCount( $dbk ) ) {
1634 # This should never be reached.
1635 $article = new Article( $title );
1636 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1637 if ( $articleContent !== false ) {
1639 $text = $articleContent;
1643 # If the title is valid but undisplayable, make a link to it
1644 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1645 $text = '[['.$title->getPrefixedText().']]';
1649 # Template cache array insertion
1650 $this->mTemplates
[$part1] = $text;
1654 # Recursive parsing, escaping and link table handling
1655 # Only for HTML output
1656 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1657 $text = wfEscapeWikiText( $text );
1658 } elseif ( $this->mOutputType
== OT_HTML
&& $found && !$noparse) {
1659 # Clean up argument array
1660 $assocArgs = array();
1662 foreach( $args as $arg ) {
1663 $eqpos = strpos( $arg, '=' );
1664 if ( $eqpos === false ) {
1665 $assocArgs[$index++
] = $arg;
1667 $name = trim( substr( $arg, 0, $eqpos ) );
1668 $value = trim( substr( $arg, $eqpos+
1 ) );
1669 if ( $value === false ) {
1672 if ( $name !== false ) {
1673 $assocArgs[$name] = $value;
1678 # Do not enter included links in link table
1679 if ( !is_null( $title ) ) {
1680 $wgLinkCache->suspend();
1683 # Add a new element to the templace recursion path
1684 $this->mTemplatePath
[$part1] = 1;
1686 $text = $this->stripParse( $text, $newline, $assocArgs );
1688 # Resume the link cache and register the inclusion as a link
1689 if ( !is_null( $title ) ) {
1690 $wgLinkCache->resume();
1691 $wgLinkCache->addLinkObj( $title );
1695 # Empties the template path
1696 $this->mTemplatePath
= array();
1705 # Triple brace replacement -- used for template arguments
1706 function argSubstitution( $matches ) {
1707 $newline = $matches[1];
1708 $arg = trim( $matches[2] );
1709 $text = $matches[0];
1710 $inputArgs = end( $this->mArgStack
);
1712 if ( array_key_exists( $arg, $inputArgs ) ) {
1713 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1719 # Returns true if the function is allowed to include this entity
1720 function incrementIncludeCount( $dbk ) {
1721 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1722 $this->mIncludeCount
[$dbk] = 0;
1724 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1732 # Cleans up HTML, removes dangerous tags and attributes
1733 /* private */ function removeHTMLtags( $text ) {
1734 global $wgUseTidy, $wgUserHtml;
1735 $fname = 'Parser::removeHTMLtags';
1736 wfProfileIn( $fname );
1739 $htmlpairs = array( # Tags that must be closed
1740 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1741 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1742 'strike', 'strong', 'tt', 'var', 'div', 'center',
1743 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1744 'ruby', 'rt' , 'rb' , 'rp', 'p'
1746 $htmlsingle = array(
1747 'br', 'hr', 'li', 'dt', 'dd'
1749 $htmlnest = array( # Tags that can be nested--??
1750 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1751 'dl', 'font', 'big', 'small', 'sub', 'sup'
1753 $tabletags = array( # Can only appear inside table
1757 $htmlpairs = array();
1758 $htmlsingle = array();
1759 $htmlnest = array();
1760 $tabletags = array();
1763 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1764 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1766 $htmlattrs = $this->getHTMLattrs () ;
1768 # Remove HTML comments
1769 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1771 $bits = explode( '<', $text );
1772 $text = array_shift( $bits );
1774 $tagstack = array(); $tablestack = array();
1775 foreach ( $bits as $x ) {
1776 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1777 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1779 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1780 error_reporting( $prev );
1783 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1787 if ( ! in_array( $t, $htmlsingle ) &&
1788 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
1789 @array_push
( $tagstack, $ot );
1792 if ( $t == 'table' ) {
1793 $tagstack = array_pop( $tablestack );
1798 # Keep track for later
1799 if ( in_array( $t, $tabletags ) &&
1800 ! in_array( 'table', $tagstack ) ) {
1802 } else if ( in_array( $t, $tagstack ) &&
1803 ! in_array ( $t , $htmlnest ) ) {
1805 } else if ( ! in_array( $t, $htmlsingle ) ) {
1806 if ( $t == 'table' ) {
1807 array_push( $tablestack, $tagstack );
1808 $tagstack = array();
1810 array_push( $tagstack, $t );
1812 # Strip non-approved attributes from the tag
1813 $newparams = $this->fixTagAttributes($params);
1817 $rest = str_replace( '>', '>', $rest );
1818 $text .= "<$slash$t $newparams$brace$rest";
1822 $text .= '<' . str_replace( '>', '>', $x);
1824 # Close off any remaining tags
1825 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1827 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1830 # this might be possible using tidy itself
1831 foreach ( $bits as $x ) {
1832 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1834 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1835 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1836 $newparams = $this->fixTagAttributes($params);
1837 $rest = str_replace( '>', '>', $rest );
1838 $text .= "<$slash$t $newparams$brace$rest";
1840 $text .= '<' . str_replace( '>', '>', $x);
1844 wfProfileOut( $fname );
1849 # This function accomplishes several tasks:
1850 # 1) Auto-number headings if that option is enabled
1851 # 2) Add an [edit] link to sections for logged in users who have enabled the option
1852 # 3) Add a Table of contents on the top for users who have enabled the option
1853 # 4) Auto-anchor headings
1855 # It loops through all headlines, collects the necessary data, then splits up the
1856 # string and re-inserts the newly formatted headlines.
1857 /* private */ function formatHeadings( $text, $isMain=true ) {
1858 global $wgInputEncoding, $wgMaxTocLevel, $wgLang;
1860 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1861 $doShowToc = $this->mOptions
->getShowToc();
1862 $forceTocHere = false;
1863 if( !$this->mTitle
->userCanEdit() ) {
1865 $rightClickHack = 0;
1867 $showEditLink = $this->mOptions
->getEditSection();
1868 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1871 # Inhibit editsection links if requested in the page
1872 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1873 if( $esw->matchAndRemove( $text ) ) {
1876 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1878 $mw =& MagicWord
::get( MAG_NOTOC
);
1879 if( $mw->matchAndRemove( $text ) ) {
1883 # never add the TOC to the Main Page. This is an entry page that should not
1884 # be more than 1-2 screens large anyway
1885 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
1889 # Get all headlines for numbering them and adding funky stuff like [edit]
1890 # links - this is for later, but we need the number of headlines right now
1891 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1893 # if there are fewer than 4 headlines in the article, do not show TOC
1894 if( $numMatches < 4 ) {
1898 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1899 # override above conditions and always show TOC at that place
1900 $mw =& MagicWord
::get( MAG_TOC
);
1901 if ($mw->match( $text ) ) {
1903 $forceTocHere = true;
1905 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1906 # override above conditions and always show TOC above first header
1907 $mw =& MagicWord
::get( MAG_FORCETOC
);
1908 if ($mw->matchAndRemove( $text ) ) {
1915 # We need this to perform operations on the HTML
1916 $sk =& $this->mOptions
->getSkin();
1921 # Ugh .. the TOC should have neat indentation levels which can be
1922 # passed to the skin functions. These are determined here
1927 $sublevelCount = array();
1930 foreach( $matches[3] as $headline ) {
1933 $prevlevel = $level;
1935 $level = $matches[1][$headlineCount];
1936 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1937 # reset when we enter a new level
1938 $sublevelCount[$level] = 0;
1939 $toc .= $sk->tocIndent( $level - $prevlevel );
1940 $toclevel +
= $level - $prevlevel;
1942 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1943 # reset when we step back a level
1944 $sublevelCount[$level+
1]=0;
1945 $toc .= $sk->tocUnindent( $prevlevel - $level );
1946 $toclevel -= $prevlevel - $level;
1948 # count number of headlines for each level
1949 @$sublevelCount[$level]++
;
1950 if( $doNumberHeadings ||
$doShowToc ) {
1952 for( $i = 1; $i <= $level; $i++
) {
1953 if( !empty( $sublevelCount[$i] ) ) {
1957 $numbering .= $wgLang->formatNum( $sublevelCount[$i] );
1963 # The canonized header is a version of the header text safe to use for links
1964 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1965 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1966 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
1968 # Remove link placeholders by the link text.
1969 # <!--LINK namespace page_title link text with suffix-->
1971 # link text with suffix
1972 $canonized_headline = preg_replace( '/<!--LINK [0-9]* [^ ]* *(.*?)-->/','$1', $canonized_headline );
1974 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
1975 $tocline = trim( $canonized_headline );
1976 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
1977 $replacearray = array(
1981 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
1982 $refer[$headlineCount] = $canonized_headline;
1984 # count how many in assoc. array so we can track dupes in anchors
1985 @$refers[$canonized_headline]++
;
1986 $refcount[$headlineCount]=$refers[$canonized_headline];
1988 # Prepend the number to the heading text
1990 if( $doNumberHeadings ||
$doShowToc ) {
1991 $tocline = $numbering . ' ' . $tocline;
1993 # Don't number the heading if it is the only one (looks silly)
1994 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1995 # the two are different if the line contains a link
1996 $headline=$numbering . ' ' . $headline;
2000 # Create the anchor for linking from the TOC to the section
2001 $anchor = $canonized_headline;
2002 if($refcount[$headlineCount] > 1 ) {
2003 $anchor .= '_' . $refcount[$headlineCount];
2005 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2006 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2008 if( $showEditLink ) {
2009 if ( empty( $head[$headlineCount] ) ) {
2010 $head[$headlineCount] = '';
2012 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
2015 # Add the edit section span
2016 if( $rightClickHack ) {
2017 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
2020 # give headline the correct <h#> tag
2021 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2027 $toclines = $headlineCount;
2028 $toc .= $sk->tocUnindent( $toclevel );
2029 $toc = $sk->tocTable( $toc );
2032 # split up and insert constructed headlines
2034 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2037 foreach( $blocks as $block ) {
2038 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2039 # This is the [edit] link that appears for the top block of text when
2040 # section editing is enabled
2042 # Disabled because it broke block formatting
2043 # For example, a bullet point in the top line
2044 # $full .= $sk->editSectionLink(0);
2047 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2048 # Top anchor now in skin
2052 if( !empty( $head[$i] ) ) {
2058 $mw =& MagicWord
::get( MAG_TOC
);
2059 return $mw->replace( $toc, $full );
2065 # Return an HTML link for the "ISBN 123456" text
2066 /* private */ function magicISBN( $text ) {
2068 $fname = 'Parser::magicISBN';
2069 wfProfileIn( $fname );
2071 $a = split( 'ISBN ', ' '.$text );
2072 if ( count ( $a ) < 2 ) {
2073 wfProfileOut( $fname );
2076 $text = substr( array_shift( $a ), 1);
2077 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2079 foreach ( $a as $x ) {
2080 $isbn = $blank = '' ;
2081 while ( ' ' == $x{0} ) {
2083 $x = substr( $x, 1 );
2085 while ( strstr( $valid, $x{0} ) != false ) {
2087 $x = substr( $x, 1 );
2089 $num = str_replace( '-', '', $isbn );
2090 $num = str_replace( ' ', '', $num );
2093 $text .= "ISBN $blank$x";
2095 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2096 $text .= '<a href="' .
2097 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2098 "\" class=\"internal\">ISBN $isbn</a>";
2102 wfProfileOut( $fname );
2106 # Return an HTML link for the "GEO ..." text
2107 /* private */ function magicGEO( $text ) {
2108 global $wgLang, $wgUseGeoMode;
2109 $fname = 'Parser::magicGEO';
2110 wfProfileIn( $fname );
2112 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2113 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2114 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2115 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2116 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2117 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2119 $a = split( 'GEO ', ' '.$text );
2120 if ( count ( $a ) < 2 ) {
2121 wfProfileOut( $fname );
2124 $text = substr( array_shift( $a ), 1);
2125 $valid = '0123456789.+-:';
2127 foreach ( $a as $x ) {
2128 $geo = $blank = '' ;
2129 while ( ' ' == $x{0} ) {
2131 $x = substr( $x, 1 );
2133 while ( strstr( $valid, $x{0} ) != false ) {
2135 $x = substr( $x, 1 );
2137 $num = str_replace( '+', '', $geo );
2138 $num = str_replace( ' ', '', $num );
2140 if ( '' == $num ||
count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2141 $text .= "GEO $blank$x";
2143 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2144 $text .= '<a href="' .
2145 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2146 "\" class=\"internal\">GEO $geo</a>";
2150 wfProfileOut( $fname );
2154 # Return an HTML link for the "RFC 1234" text
2155 /* private */ function magicRFC( $text ) {
2158 $a = split( 'RFC ', ' '.$text );
2159 if ( count ( $a ) < 2 ) return $text;
2160 $text = substr( array_shift( $a ), 1);
2161 $valid = '0123456789';
2163 foreach ( $a as $x ) {
2164 $rfc = $blank = '' ;
2165 while ( ' ' == $x{0} ) {
2167 $x = substr( $x, 1 );
2169 while ( strstr( $valid, $x{0} ) != false ) {
2171 $x = substr( $x, 1 );
2175 $text .= "RFC $blank$x";
2177 $url = wfmsg( 'rfcurl' );
2178 $url = str_replace( '$1', $rfc, $url);
2179 $sk =& $this->mOptions
->getSkin();
2180 $la = $sk->getExternalLinkAttributes( $url, 'RFC '.$rfc );
2181 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2187 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2188 $this->mOptions
= $options;
2189 $this->mTitle
=& $title;
2190 $this->mOutputType
= OT_WIKI
;
2192 if ( $clearState ) {
2193 $this->clearState();
2196 $stripState = false;
2200 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2204 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2205 "/<br *?>/i" => "<br />",
2207 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2209 $text = $this->strip( $text, $stripState, false );
2210 $text = $this->pstPass2( $text, $user );
2211 $text = $this->unstrip( $text, $stripState );
2212 $text = $this->unstripNoWiki( $text, $stripState );
2216 /* private */ function pstPass2( $text, &$user ) {
2217 global $wgLang, $wgLocaltimezone, $wgCurParser;
2219 # Variable replacement
2220 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2221 $text = $this->replaceVariables( $text );
2225 $n = $user->getName();
2226 $k = $user->getOption( 'nickname' );
2227 if ( '' == $k ) { $k = $n; }
2228 if(isset($wgLocaltimezone)) {
2229 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2231 /* Note: this is an ugly timezone hack for the European wikis */
2232 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2233 ' (' . date( 'T' ) . ')';
2234 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2236 $text = preg_replace( '/~~~~~/', $d, $text );
2237 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2238 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2240 # Context links: [[|name]] and [[name (context)|]]
2242 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2243 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2244 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2245 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2247 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2248 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2249 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2250 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2252 $t = $this->mTitle
->getText();
2253 if ( preg_match( $conpat, $t, $m ) ) {
2256 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2257 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2258 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2260 if ( '' == $context ) {
2261 $text = preg_replace( $p2, '[[\\1]]', $text );
2263 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2267 $mw =& MagicWord::get( MAG_SUBST );
2268 $wgCurParser = $this->fork();
2269 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2270 $this->merge( $wgCurParser );
2273 # Trim trailing whitespace
2274 # MAG_END (__END__) tag allows for trailing
2275 # whitespace to be deliberately included
2276 $text = rtrim( $text );
2277 $mw =& MagicWord
::get( MAG_END
);
2278 $mw->matchAndRemove( $text );
2283 # Set up some variables which are usually set up in parse()
2284 # so that an external function can call some class members with confidence
2285 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2286 $this->mTitle
=& $title;
2287 $this->mOptions
= $options;
2288 $this->mOutputType
= $outputType;
2289 if ( $clearState ) {
2290 $this->clearState();
2294 function transformMsg( $text, $options ) {
2296 static $executing = false;
2298 # Guard against infinite recursion
2304 $this->mTitle
= $wgTitle;
2305 $this->mOptions
= $options;
2306 $this->mOutputType
= OT_MSG
;
2307 $this->clearState();
2308 $text = $this->replaceVariables( $text );
2314 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2315 # Callback will be called with the text within
2316 # Transform and return the text within
2317 function setHook( $tag, $callback ) {
2318 $oldVal = @$this->mTagHooks
[$tag];
2319 $this->mTagHooks
[$tag] = $callback;
2326 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2327 var $mCacheTime; # Used in ParserCache
2329 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
2330 $containsOldMagic = false )
2332 $this->mText
= $text;
2333 $this->mLanguageLinks
= $languageLinks;
2334 $this->mCategoryLinks
= $categoryLinks;
2335 $this->mContainsOldMagic
= $containsOldMagic;
2336 $this->mCacheTime
= '';
2339 function getText() { return $this->mText
; }
2340 function getLanguageLinks() { return $this->mLanguageLinks
; }
2341 function getCategoryLinks() { return $this->mCategoryLinks
; }
2342 function getCacheTime() { return $this->mCacheTime
; }
2343 function containsOldMagic() { return $this->mContainsOldMagic
; }
2344 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2345 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2346 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2347 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2348 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2350 function merge( $other ) {
2351 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2352 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2353 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2360 # All variables are private
2361 var $mUseTeX; # Use texvc to expand <math> tags
2362 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2363 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2364 var $mAllowExternalImages; # Allow external images inline
2365 var $mSkin; # Reference to the preferred skin
2366 var $mDateFormat; # Date format index
2367 var $mEditSection; # Create "edit section" links
2368 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2369 var $mNumberHeadings; # Automatically number headings
2370 var $mShowToc; # Show table of contents
2372 function getUseTeX() { return $this->mUseTeX
; }
2373 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2374 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2375 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2376 function getSkin() { return $this->mSkin
; }
2377 function getDateFormat() { return $this->mDateFormat
; }
2378 function getEditSection() { return $this->mEditSection
; }
2379 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2380 function getNumberHeadings() { return $this->mNumberHeadings
; }
2381 function getShowToc() { return $this->mShowToc
; }
2383 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2384 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2385 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2386 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2387 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2388 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2389 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2390 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2391 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2393 function setSkin( &$x ) { $this->mSkin
=& $x; }
2395 # Get parser options
2396 /* static */ function newFromUser( &$user ) {
2397 $popts = new ParserOptions
;
2398 $popts->initialiseFromUser( $user );
2403 function initialiseFromUser( &$userInput ) {
2404 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2406 $fname = 'ParserOptions::initialiseFromUser';
2407 wfProfileIn( $fname );
2408 if ( !$userInput ) {
2410 $user->setLoaded( true );
2412 $user =& $userInput;
2415 $this->mUseTeX
= $wgUseTeX;
2416 $this->mUseDynamicDates
= $wgUseDynamicDates;
2417 $this->mInterwikiMagic
= $wgInterwikiMagic;
2418 $this->mAllowExternalImages
= $wgAllowExternalImages;
2419 wfProfileIn( $fname.'-skin' );
2420 $this->mSkin
=& $user->getSkin();
2421 wfProfileOut( $fname.'-skin' );
2422 $this->mDateFormat
= $user->getOption( 'date' );
2423 $this->mEditSection
= $user->getOption( 'editsection' );
2424 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2425 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2426 $this->mShowToc
= $user->getOption( 'showtoc' );
2427 wfProfileOut( $fname );
2433 # Regex callbacks, used in Parser::replaceVariables
2434 function wfBraceSubstitution( $matches ) {
2435 global $wgCurParser;
2436 return $wgCurParser->braceSubstitution( $matches );
2439 function wfArgSubstitution( $matches ) {
2440 global $wgCurParser;
2441 return $wgCurParser->argSubstitution( $matches );
2444 function wfVariableSubstitution( $matches ) {
2445 global $wgCurParser;
2446 return $wgCurParser->variableSubstitution( $matches );
2449 # Return the total number of articles
2450 function wfNumberOfArticles() {
2451 global $wgNumberOfArticles;
2454 return $wgNumberOfArticles;
2457 # Get various statistics from the database
2458 /* private */ function wfLoadSiteStats() {
2459 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
2460 $fname = 'wfLoadSiteStats';
2462 if ( -1 != $wgNumberOfArticles ) return;
2463 $dbr =& wfGetDB( DB_SLAVE
);
2464 $s = $dbr->getArray( 'site_stats',
2465 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
2466 array( 'ss_row_id' => 1 ), $fname
2469 if ( $s === false ) {
2472 $wgTotalViews = $s->ss_total_views
;
2473 $wgTotalEdits = $s->ss_total_edits
;
2474 $wgNumberOfArticles = $s->ss_good_articles
;
2478 function wfEscapeHTMLTagsOnly( $in ) {
2480 array( '"', '>', '<' ),
2481 array( '"', '>', '<' ),