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