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