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