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