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