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