Use localized numerals for CURRENTMONTH, CURRENTDAY, CURRENTYEAR, NUMBEROFARTICLES
[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 # Strip javascript "expression" from stylesheets. Brute force approach:
646 # If anythin offensive is found, all attributes of the HTML tag are dropped
647
648 if( preg_match(
649 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
650 wfMungeToUtf8( $t ) ) )
651 {
652 $t='';
653 }
654
655 return trim ( $t ) ;
656 }
657
658 # interface with html tidy, used if $wgUseTidy = true
659 function tidy ( $text ) {
660 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
661 global $wgInputEncoding, $wgOutputEncoding;
662 $fname = 'Parser::tidy';
663 wfProfileIn( $fname );
664
665 $cleansource = '';
666 switch(strtoupper($wgOutputEncoding)) {
667 case 'ISO-8859-1':
668 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
669 break;
670 case 'UTF-8':
671 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
672 break;
673 default:
674 $wgTidyOpts .= ' -raw';
675 }
676
677 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
678 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
679 '<head><title>test</title></head><body>'.$text.'</body></html>';
680 $descriptorspec = array(
681 0 => array('pipe', 'r'),
682 1 => array('pipe', 'w'),
683 2 => array('file', '/dev/null', 'a')
684 );
685 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
686 if (is_resource($process)) {
687 fwrite($pipes[0], $wrappedtext);
688 fclose($pipes[0]);
689 while (!feof($pipes[1])) {
690 $cleansource .= fgets($pipes[1], 1024);
691 }
692 fclose($pipes[1]);
693 $return_value = proc_close($process);
694 }
695
696 wfProfileOut( $fname );
697
698 if( $cleansource == '' && $text != '') {
699 wfDebug( "Tidy error detected!\n" );
700 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
701 } else {
702 return $cleansource;
703 }
704 }
705
706 # parse the wiki syntax used to render tables
707 function doTableStuff ( $t ) {
708 $fname = 'Parser::doTableStuff';
709 wfProfileIn( $fname );
710
711 $t = explode ( "\n" , $t ) ;
712 $td = array () ; # Is currently a td tag open?
713 $ltd = array () ; # Was it TD or TH?
714 $tr = array () ; # Is currently a tr tag open?
715 $ltr = array () ; # tr attributes
716 foreach ( $t AS $k => $x )
717 {
718 $x = trim ( $x ) ;
719 $fc = substr ( $x , 0 , 1 ) ;
720 if ( '{|' == substr ( $x , 0 , 2 ) )
721 {
722 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 2 ) ) . '>' ;
723 array_push ( $td , false ) ;
724 array_push ( $ltd , '' ) ;
725 array_push ( $tr , false ) ;
726 array_push ( $ltr , '' ) ;
727 }
728 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
729 else if ( '|}' == substr ( $x , 0 , 2 ) )
730 {
731 $z = "</table>\n" ;
732 $l = array_pop ( $ltd ) ;
733 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
734 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
735 array_pop ( $ltr ) ;
736 $t[$k] = $z ;
737 }
738 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
739 {
740 $x = substr ( $x , 1 ) ;
741 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
742 $z = '' ;
743 $l = array_pop ( $ltd ) ;
744 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
745 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
746 array_pop ( $ltr ) ;
747 $t[$k] = $z ;
748 array_push ( $tr , false ) ;
749 array_push ( $td , false ) ;
750 array_push ( $ltd , '' ) ;
751 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
752 }
753 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
754 {
755 if ( '|+' == substr ( $x , 0 , 2 ) )
756 {
757 $fc = '+' ;
758 $x = substr ( $x , 1 ) ;
759 }
760 $after = substr ( $x , 1 ) ;
761 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
762 $after = explode ( '||' , $after ) ;
763 $t[$k] = '' ;
764 foreach ( $after AS $theline )
765 {
766 $z = '' ;
767 if ( $fc != '+' )
768 {
769 $tra = array_pop ( $ltr ) ;
770 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
771 array_push ( $tr , true ) ;
772 array_push ( $ltr , '' ) ;
773 }
774
775 $l = array_pop ( $ltd ) ;
776 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
777 if ( $fc == '|' ) $l = 'td' ;
778 else if ( $fc == '!' ) $l = 'th' ;
779 else if ( $fc == '+' ) $l = 'caption' ;
780 else $l = '' ;
781 array_push ( $ltd , $l ) ;
782 $y = explode ( '|' , $theline , 2 ) ;
783 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
784 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
785 $t[$k] .= $y ;
786 array_push ( $td , true ) ;
787 }
788 }
789 }
790
791 # Closing open td, tr && table
792 while ( count ( $td ) > 0 )
793 {
794 if ( array_pop ( $td ) ) $t[] = '</td>' ;
795 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
796 $t[] = '</table>' ;
797 }
798
799 $t = implode ( "\n" , $t ) ;
800 # $t = $this->removeHTMLtags( $t );
801 wfProfileOut( $fname );
802 return $t ;
803 }
804
805 # Parses the text and adds the result to the strip state
806 # Returns the strip tag
807 function stripParse( $text, $newline, $args )
808 {
809 $text = $this->strip( $text, $this->mStripState );
810 $text = $this->internalParse( $text, (bool)$newline, $args, false );
811 return $newline.$this->insertStripItem( $text, $this->mStripState );
812 }
813
814 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
815 $fname = 'Parser::internalParse';
816 wfProfileIn( $fname );
817
818 $text = $this->removeHTMLtags( $text );
819 $text = $this->replaceVariables( $text, $args );
820
821 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
822
823 $text = $this->doHeadings( $text );
824 if($this->mOptions->getUseDynamicDates()) {
825 global $wgDateFormatter;
826 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
827 }
828 $text = $this->doAllQuotes( $text );
829 // $text = $this->doExponent( $text );
830 $text = $this->replaceExternalLinks( $text );
831 $text = $this->replaceInternalLinks ( $text );
832 $text = $this->replaceInternalLinks ( $text );
833 //$text = $this->doTokenizedParser ( $text );
834 $text = $this->doTableStuff ( $text ) ;
835 $text = $this->magicISBN( $text );
836 $text = $this->magicGEO( $text );
837 $text = $this->magicRFC( $text );
838 $text = $this->formatHeadings( $text, $isMain );
839 $sk =& $this->mOptions->getSkin();
840 $text = $sk->transformContent( $text );
841
842 if ( $isMain && !isset ( $this->categoryMagicDone ) ) {
843 $text .= $this->categoryMagic () ;
844 $this->categoryMagicDone = true ;
845 }
846
847 wfProfileOut( $fname );
848 return $text;
849 }
850
851 # Parse ^^ tokens and return html
852 /* private */ function doExponent ( $text )
853 {
854 $fname = 'Parser::doExponent';
855 wfProfileIn( $fname);
856 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
857 wfProfileOut( $fname);
858 return $text;
859 }
860
861 # Parse headers and return html
862 /* private */ function doHeadings( $text ) {
863 $fname = 'Parser::doHeadings';
864 wfProfileIn( $fname );
865 for ( $i = 6; $i >= 1; --$i ) {
866 $h = substr( '======', 0, $i );
867 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
868 "<h{$i}>\\1</h{$i}>\\2", $text );
869 }
870 wfProfileOut( $fname );
871 return $text;
872 }
873
874 /* private */ function doAllQuotes( $text ) {
875 $fname = 'Parser::doAllQuotes';
876 wfProfileIn( $fname );
877 $outtext = '';
878 $lines = explode( "\n", $text );
879 foreach ( $lines as $line ) {
880 $outtext .= $this->doQuotes ( '', $line, '' ) . "\n";
881 }
882 $outtext = substr($outtext, 0,-1);
883 wfProfileOut( $fname );
884 return $outtext;
885 }
886
887 /* private */ function doQuotes( $pre, $text, $mode ) {
888 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
889 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
890 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
891 if ( substr ($m[2], 0, 1) == '\'' ) {
892 $m[2] = substr ($m[2], 1);
893 if ($mode == 'em') {
894 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'emstrong' );
895 } else if ($mode == 'strong') {
896 return $m1_strong . $this->doQuotes ( '', $m[2], '' );
897 } else if (($mode == 'emstrong') || ($mode == 'both')) {
898 return $this->doQuotes ( '', $pre.$m1_strong.$m[2], 'em' );
899 } else if ($mode == 'strongem') {
900 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( '', $m[2], 'em' );
901 } else {
902 return $m[1] . $this->doQuotes ( '', $m[2], 'strong' );
903 }
904 } else {
905 if ($mode == 'strong') {
906 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'strongem' );
907 } else if ($mode == 'em') {
908 return $m1_em . $this->doQuotes ( '', $m[2], '' );
909 } else if ($mode == 'emstrong') {
910 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( '', $m[2], 'strong' );
911 } else if (($mode == 'strongem') || ($mode == 'both')) {
912 return $this->doQuotes ( '', $pre.$m1_em.$m[2], 'strong' );
913 } else {
914 return $m[1] . $this->doQuotes ( '', $m[2], 'em' );
915 }
916 }
917 } else {
918 $text_strong = ($text == '') ? '' : "<strong>{$text}</strong>";
919 $text_em = ($text == '') ? '' : "<em>{$text}</em>";
920 if ($mode == '') {
921 return $pre . $text;
922 } else if ($mode == 'em') {
923 return $pre . $text_em;
924 } else if ($mode == 'strong') {
925 return $pre . $text_strong;
926 } else if ($mode == 'strongem') {
927 return (($pre == '') && ($text == '')) ? '' : "<strong>{$pre}{$text_em}</strong>";
928 } else {
929 return (($pre == '') && ($text == '')) ? '' : "<em>{$pre}{$text_strong}</em>";
930 }
931 }
932 }
933
934 # Note: we have to do external links before the internal ones,
935 # and otherwise take great care in the order of things here, so
936 # that we don't end up interpreting some URLs twice.
937
938 /* private */ function replaceExternalLinks( $text ) {
939 $fname = 'Parser::replaceExternalLinks';
940 wfProfileIn( $fname );
941 $text = $this->subReplaceExternalLinks( $text, 'http', true );
942 $text = $this->subReplaceExternalLinks( $text, 'https', true );
943 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
944 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
945 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
946 $text = $this->subReplaceExternalLinks( $text, 'news', false );
947 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
948 wfProfileOut( $fname );
949 return $text;
950 }
951
952 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
953 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
954 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
955
956 # this is the list of separators that should be ignored if they
957 # are the last character of an URL but that should be included
958 # if they occur within the URL, e.g. "go to www.foo.com, where .."
959 # in this case, the last comma should not become part of the URL,
960 # but in "www.foo.com/123,2342,32.htm" it should.
961 $sep = ",;\.:";
962 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
963 $images = 'gif|png|jpg|jpeg';
964
965 # PLEASE NOTE: The curly braces { } are not part of the regex,
966 # they are interpreted as part of the string (used to tell PHP
967 # that the content of the string should be inserted there).
968 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
969 "((?i){$images})([^{$uc}]|$)/";
970
971 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
972 $sk =& $this->mOptions->getSkin();
973
974 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
975 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
976 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
977 }
978 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
979 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
980 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
981 '</a>\\5', $s );
982 $s = str_replace( $unique, $protocol, $s );
983
984 $a = explode( "[{$protocol}:", " " . $s );
985 $s = array_shift( $a );
986 $s = substr( $s, 1 );
987
988 # Regexp for URL in square brackets
989 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
990 # Regexp for URL with link text in square brackets
991 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
992
993 foreach ( $a as $line ) {
994
995 # CASE 1: Link in square brackets, e.g.
996 # some text [http://domain.tld/some.link] more text
997 if ( preg_match( $e1, $line, $m ) ) {
998 $link = "{$protocol}:{$m[1]}";
999 $trail = $m[2];
1000 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
1001 else { $text = wfEscapeHTML( $link ); }
1002 }
1003
1004 # CASE 2: Link with link text and text directly following it, e.g.
1005 # This is a collection of [http://domain.tld/some.link link]s
1006 else if ( preg_match( $e2, $line, $m ) ) {
1007 $link = "{$protocol}:{$m[1]}";
1008 $text = $m[2];
1009 $dtrail = '';
1010 $trail = $m[3];
1011 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1012 $dtrail = $m2[1];
1013 $trail = $m2[2];
1014 }
1015 }
1016
1017 # CASE 3: Nothing matches, just output the source text
1018 else {
1019 $s .= "[{$protocol}:" . $line;
1020 continue;
1021 }
1022
1023 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1024 $paren = '';
1025 } else {
1026 # Expand the URL for printable version
1027 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1028 }
1029 $la = $sk->getExternalLinkAttributes( $link, $text );
1030 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1031
1032 }
1033 return $s;
1034 }
1035
1036
1037 /* private */ function replaceInternalLinks( $s ) {
1038 global $wgLang, $wgLinkCache;
1039 global $wgNamespacesWithSubpages, $wgLanguageCode;
1040 static $fname = 'Parser::replaceInternalLinks' ;
1041 wfProfileIn( $fname );
1042
1043 wfProfileIn( $fname.'-setup' );
1044 static $tc = FALSE;
1045 # the % is needed to support urlencoded titles as well
1046 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1047 $sk =& $this->mOptions->getSkin();
1048
1049 $a = explode( '[[', ' ' . $s );
1050 $s = array_shift( $a );
1051 $s = substr( $s, 1 );
1052
1053 # Match a link having the form [[namespace:link|alternate]]trail
1054 static $e1 = FALSE;
1055 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1056 # Match the end of a line for a word that's not followed by whitespace,
1057 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1058 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1059
1060 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1061 # Special and Media are pseudo-namespaces; no pages actually exist in them
1062 static $image = FALSE;
1063 static $special = FALSE;
1064 static $media = FALSE;
1065 static $category = FALSE;
1066 if ( !$image ) { $image = Namespace::getImage(); }
1067 if ( !$special ) { $special = Namespace::getSpecial(); }
1068 if ( !$media ) { $media = Namespace::getMedia(); }
1069 if ( !$category ) { $category = Namespace::getCategory(); }
1070
1071 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1072
1073 if ( $useLinkPrefixExtension ) {
1074 if ( preg_match( $e2, $s, $m ) ) {
1075 $first_prefix = $m[2];
1076 $s = $m[1];
1077 } else {
1078 $first_prefix = false;
1079 }
1080 } else {
1081 $prefix = '';
1082 }
1083
1084 wfProfileOut( $fname.'-setup' );
1085
1086 foreach ( $a as $line ) {
1087 wfProfileIn( $fname.'-prefixhandling' );
1088 if ( $useLinkPrefixExtension ) {
1089 if ( preg_match( $e2, $s, $m ) ) {
1090 $prefix = $m[2];
1091 $s = $m[1];
1092 } else {
1093 $prefix='';
1094 }
1095 # first link
1096 if($first_prefix) {
1097 $prefix = $first_prefix;
1098 $first_prefix = false;
1099 }
1100 }
1101 wfProfileOut( $fname.'-prefixhandling' );
1102
1103 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1104 $text = $m[2];
1105 # fix up urlencoded title texts
1106 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1107 $trail = $m[3];
1108 } else { # Invalid form; output directly
1109 $s .= $prefix . '[[' . $line ;
1110 continue;
1111 }
1112
1113 /* Valid link forms:
1114 Foobar -- normal
1115 :Foobar -- override special treatment of prefix (images, language links)
1116 /Foobar -- convert to CurrentPage/Foobar
1117 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1118 */
1119 $c = substr($m[1],0,1);
1120 $noforce = ($c != ':');
1121 if( $c == '/' ) { # subpage
1122 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1123 $m[1]=substr($m[1],1,strlen($m[1])-2);
1124 $noslash=$m[1];
1125 } else {
1126 $noslash=substr($m[1],1);
1127 }
1128 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1129 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1130 if( '' == $text ) {
1131 $text= $m[1];
1132 } # this might be changed for ugliness reasons
1133 } else {
1134 $link = $noslash; # no subpage allowed, use standard link
1135 }
1136 } elseif( $noforce ) { # no subpage
1137 $link = $m[1];
1138 } else {
1139 $link = substr( $m[1], 1 );
1140 }
1141 $wasblank = ( '' == $text );
1142 if( $wasblank )
1143 $text = $link;
1144
1145 $nt = Title::newFromText( $link );
1146 if( !$nt ) {
1147 $s .= $prefix . '[[' . $line;
1148 continue;
1149 }
1150 $ns = $nt->getNamespace();
1151 $iw = $nt->getInterWiki();
1152 if( $noforce ) {
1153 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1154 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
1155 $tmp = $prefix . $trail ;
1156 $s .= (trim($tmp) == '')? '': $tmp;
1157 continue;
1158 }
1159 if ( $ns == $image ) {
1160 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1161 $wgLinkCache->addImageLinkObj( $nt );
1162 continue;
1163 }
1164 if ( $ns == $category ) {
1165 $t = $nt->getText() ;
1166 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1167
1168 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1169 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1170 $wgLinkCache->resume();
1171
1172 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1173 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1174 $this->mOutput->mCategoryLinks[] = $t ;
1175 $s .= $prefix . $trail ;
1176 continue;
1177 }
1178 }
1179 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1180 ( strpos( $link, '#' ) == FALSE ) ) {
1181 # Self-links are handled specially; generally de-link and change to bold.
1182 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1183 continue;
1184 }
1185
1186 if( $ns == $media ) {
1187 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1188 $wgLinkCache->addImageLinkObj( $nt );
1189 continue;
1190 } elseif( $ns == $special ) {
1191 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1192 continue;
1193 }
1194 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1195 }
1196 wfProfileOut( $fname );
1197 return $s;
1198 }
1199
1200 # Some functions here used by doBlockLevels()
1201 #
1202 /* private */ function closeParagraph() {
1203 $result = '';
1204 if ( '' != $this->mLastSection ) {
1205 $result = '</' . $this->mLastSection . ">\n";
1206 }
1207 $this->mInPre = false;
1208 $this->mLastSection = '';
1209 return $result;
1210 }
1211 # getCommon() returns the length of the longest common substring
1212 # of both arguments, starting at the beginning of both.
1213 #
1214 /* private */ function getCommon( $st1, $st2 ) {
1215 $fl = strlen( $st1 );
1216 $shorter = strlen( $st2 );
1217 if ( $fl < $shorter ) { $shorter = $fl; }
1218
1219 for ( $i = 0; $i < $shorter; ++$i ) {
1220 if ( $st1{$i} != $st2{$i} ) { break; }
1221 }
1222 return $i;
1223 }
1224 # These next three functions open, continue, and close the list
1225 # element appropriate to the prefix character passed into them.
1226 #
1227 /* private */ function openList( $char )
1228 {
1229 $result = $this->closeParagraph();
1230
1231 if ( '*' == $char ) { $result .= '<ul><li>'; }
1232 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1233 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1234 else if ( ';' == $char ) {
1235 $result .= '<dl><dt>';
1236 $this->mDTopen = true;
1237 }
1238 else { $result = '<!-- ERR 1 -->'; }
1239
1240 return $result;
1241 }
1242
1243 /* private */ function nextItem( $char ) {
1244 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1245 else if ( ':' == $char || ';' == $char ) {
1246 $close = "</dd>";
1247 if ( $this->mDTopen ) { $close = '</dt>'; }
1248 if ( ';' == $char ) {
1249 $this->mDTopen = true;
1250 return $close . '<dt>';
1251 } else {
1252 $this->mDTopen = false;
1253 return $close . '<dd>';
1254 }
1255 }
1256 return '<!-- ERR 2 -->';
1257 }
1258
1259 /* private */function closeList( $char ) {
1260 if ( '*' == $char ) { $text = '</li></ul>'; }
1261 else if ( '#' == $char ) { $text = '</li></ol>'; }
1262 else if ( ':' == $char ) {
1263 if ( $this->mDTopen ) {
1264 $this->mDTopen = false;
1265 $text = '</dt></dl>';
1266 } else {
1267 $text = '</dd></dl>';
1268 }
1269 }
1270 else { return '<!-- ERR 3 -->'; }
1271 return $text."\n";
1272 }
1273
1274 /* private */ function doBlockLevels( $text, $linestart ) {
1275 $fname = 'Parser::doBlockLevels';
1276 wfProfileIn( $fname );
1277
1278 # Parsing through the text line by line. The main thing
1279 # happening here is handling of block-level elements p, pre,
1280 # and making lists from lines starting with * # : etc.
1281 #
1282 $textLines = explode( "\n", $text );
1283
1284 $lastPrefix = $output = $lastLine = '';
1285 $this->mDTopen = $inBlockElem = false;
1286 $prefixLength = 0;
1287 $paragraphStack = false;
1288
1289 if ( !$linestart ) {
1290 $output .= array_shift( $textLines );
1291 }
1292 foreach ( $textLines as $oLine ) {
1293 $lastPrefixLength = strlen( $lastPrefix );
1294 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1295 $preOpenMatch = preg_match("/<pre/i", $oLine );
1296 if ( !$this->mInPre ) {
1297 # Multiple prefixes may abut each other for nested lists.
1298 $prefixLength = strspn( $oLine, '*#:;' );
1299 $pref = substr( $oLine, 0, $prefixLength );
1300
1301 # eh?
1302 $pref2 = str_replace( ';', ':', $pref );
1303 $t = substr( $oLine, $prefixLength );
1304 $this->mInPre = !empty($preOpenMatch);
1305 } else {
1306 # Don't interpret any other prefixes in preformatted text
1307 $prefixLength = 0;
1308 $pref = $pref2 = '';
1309 $t = $oLine;
1310 }
1311
1312 # List generation
1313 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1314 # Same as the last item, so no need to deal with nesting or opening stuff
1315 $output .= $this->nextItem( substr( $pref, -1 ) );
1316 $paragraphStack = false;
1317
1318 if ( ";" == substr( $pref, -1 ) ) {
1319 # The one nasty exception: definition lists work like this:
1320 # ; title : definition text
1321 # So we check for : in the remainder text to split up the
1322 # title and definition, without b0rking links.
1323 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1324 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1325 $term = $match[1];
1326 $output .= $term . $this->nextItem( ':' );
1327 $t = $match[2];
1328 }
1329 }
1330 } elseif( $prefixLength || $lastPrefixLength ) {
1331 # Either open or close a level...
1332 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1333 $paragraphStack = false;
1334
1335 while( $commonPrefixLength < $lastPrefixLength ) {
1336 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1337 --$lastPrefixLength;
1338 }
1339 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1340 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1341 }
1342 while ( $prefixLength > $commonPrefixLength ) {
1343 $char = substr( $pref, $commonPrefixLength, 1 );
1344 $output .= $this->openList( $char );
1345
1346 if ( ';' == $char ) {
1347 # FIXME: This is dupe of code above
1348 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1349 $term = $match[1];
1350 $output .= $term . $this->nextItem( ":" );
1351 $t = $match[2];
1352 }
1353 }
1354 ++$commonPrefixLength;
1355 }
1356 $lastPrefix = $pref2;
1357 }
1358 if( 0 == $prefixLength ) {
1359 # No prefix (not in list)--go to paragraph mode
1360 $uniq_prefix = UNIQ_PREFIX;
1361 // XXX: use a stack for nestable elements like span, table and div
1362 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1363 $closematch = preg_match(
1364 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1365 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1366 if ( $openmatch or $closematch ) {
1367 $paragraphStack = false;
1368 $output .= $this->closeParagraph();
1369 if($preOpenMatch and !$preCloseMatch) {
1370 $this->mInPre = true;
1371 }
1372 if ( $closematch ) {
1373 $inBlockElem = false;
1374 } else {
1375 $inBlockElem = true;
1376 }
1377 } else if ( !$inBlockElem && !$this->mInPre ) {
1378 if ( " " == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1379 // pre
1380 if ($this->mLastSection != 'pre') {
1381 $paragraphStack = false;
1382 $output .= $this->closeParagraph().'<pre>';
1383 $this->mLastSection = 'pre';
1384 }
1385 } else {
1386 // paragraph
1387 if ( '' == trim($t) ) {
1388 if ( $paragraphStack ) {
1389 $output .= $paragraphStack.'<br />';
1390 $paragraphStack = false;
1391 $this->mLastSection = 'p';
1392 } else {
1393 if ($this->mLastSection != 'p' ) {
1394 $output .= $this->closeParagraph();
1395 $this->mLastSection = '';
1396 $paragraphStack = '<p>';
1397 } else {
1398 $paragraphStack = '</p><p>';
1399 }
1400 }
1401 } else {
1402 if ( $paragraphStack ) {
1403 $output .= $paragraphStack;
1404 $paragraphStack = false;
1405 $this->mLastSection = 'p';
1406 } else if ($this->mLastSection != 'p') {
1407 $output .= $this->closeParagraph().'<p>';
1408 $this->mLastSection = 'p';
1409 }
1410 }
1411 }
1412 }
1413 }
1414 if ($paragraphStack === false) {
1415 $output .= $t."\n";
1416 }
1417 }
1418 while ( $prefixLength ) {
1419 $output .= $this->closeList( $pref2{$prefixLength-1} );
1420 --$prefixLength;
1421 }
1422 if ( '' != $this->mLastSection ) {
1423 $output .= '</' . $this->mLastSection . '>';
1424 $this->mLastSection = '';
1425 }
1426
1427 wfProfileOut( $fname );
1428 return $output;
1429 }
1430
1431 # Return value of a magic variable (like PAGENAME)
1432 function getVariableValue( $index ) {
1433 global $wgLang, $wgSitename, $wgServer;
1434
1435 switch ( $index ) {
1436 case MAG_CURRENTMONTH:
1437 return $wgLang->formatNum( date( 'm' ) );
1438 case MAG_CURRENTMONTHNAME:
1439 return $wgLang->getMonthName( date('n') );
1440 case MAG_CURRENTMONTHNAMEGEN:
1441 return $wgLang->getMonthNameGen( date('n') );
1442 case MAG_CURRENTDAY:
1443 return $wgLang->formatNum( date('j') );
1444 case MAG_PAGENAME:
1445 return $this->mTitle->getText();
1446 case MAG_NAMESPACE:
1447 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1448 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1449 case MAG_CURRENTDAYNAME:
1450 return $wgLang->getWeekdayName( date('w')+1 );
1451 case MAG_CURRENTYEAR:
1452 return $wgLang->formatNum( date( 'Y' ) );
1453 case MAG_CURRENTTIME:
1454 return $wgLang->time( wfTimestampNow(), false );
1455 case MAG_NUMBEROFARTICLES:
1456 return $wgLang->formatNum( wfNumberOfArticles() );
1457 case MAG_SITENAME:
1458 return $wgSitename;
1459 case MAG_SERVER:
1460 return $wgServer;
1461 default:
1462 return NULL;
1463 }
1464 }
1465
1466 # initialise the magic variables (like CURRENTMONTHNAME)
1467 function initialiseVariables() {
1468 global $wgVariableIDs;
1469 $this->mVariables = array();
1470 foreach ( $wgVariableIDs as $id ) {
1471 $mw =& MagicWord::get( $id );
1472 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1473 }
1474 }
1475
1476 /* private */ function replaceVariables( $text, $args = array() ) {
1477 global $wgLang, $wgScript, $wgArticlePath;
1478
1479 # Prevent too big inclusions
1480 if(strlen($text)> MAX_INCLUDE_SIZE)
1481 return $text;
1482
1483 $fname = 'Parser::replaceVariables';
1484 wfProfileIn( $fname );
1485
1486 $bail = false;
1487 $titleChars = Title::legalChars();
1488 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1489
1490 # This function is called recursively. To keep track of arguments we need a stack:
1491 array_push( $this->mArgStack, $args );
1492
1493 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1494 $GLOBALS['wgCurParser'] =& $this;
1495
1496
1497 if ( $this->mOutputType == OT_HTML ) {
1498 # Variable substitution
1499 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1500
1501 # Argument substitution
1502 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1503 }
1504 # Template substitution
1505 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1506 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1507
1508 array_pop( $this->mArgStack );
1509
1510 wfProfileOut( $fname );
1511 return $text;
1512 }
1513
1514 function variableSubstitution( $matches ) {
1515 if ( !$this->mVariables ) {
1516 $this->initialiseVariables();
1517 }
1518 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1519 $text = $this->mVariables[$matches[1]];
1520 $this->mOutput->mContainsOldMagic = true;
1521 } else {
1522 $text = $matches[0];
1523 }
1524 return $text;
1525 }
1526
1527 function braceSubstitution( $matches ) {
1528 global $wgLinkCache, $wgLang;
1529 $fname = 'Parser::braceSubstitution';
1530 $found = false;
1531 $nowiki = false;
1532 $noparse = false;
1533
1534 $title = NULL;
1535
1536 # $newline is an optional newline character before the braces
1537 # $part1 is the bit before the first |, and must contain only title characters
1538 # $args is a list of arguments, starting from index 0, not including $part1
1539
1540 $newline = $matches[1];
1541 $part1 = $matches[2];
1542 # If the third subpattern matched anything, it will start with |
1543 if ( $matches[3] !== '' ) {
1544 $args = explode( '|', substr( $matches[3], 1 ) );
1545 } else {
1546 $args = array();
1547 }
1548 $argc = count( $args );
1549
1550 # {{{}}}
1551 if ( strpos( $matches[0], '{{{' ) !== false ) {
1552 $text = $matches[0];
1553 $found = true;
1554 $noparse = true;
1555 }
1556
1557 # SUBST
1558 if ( !$found ) {
1559 $mwSubst =& MagicWord::get( MAG_SUBST );
1560 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1561 if ( $this->mOutputType != OT_WIKI ) {
1562 # Invalid SUBST not replaced at PST time
1563 # Return without further processing
1564 $text = $matches[0];
1565 $found = true;
1566 $noparse= true;
1567 }
1568 } elseif ( $this->mOutputType == OT_WIKI ) {
1569 # SUBST not found in PST pass, do nothing
1570 $text = $matches[0];
1571 $found = true;
1572 }
1573 }
1574
1575 # MSG, MSGNW and INT
1576 if ( !$found ) {
1577 # Check for MSGNW:
1578 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1579 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1580 $nowiki = true;
1581 } else {
1582 # Remove obsolete MSG:
1583 $mwMsg =& MagicWord::get( MAG_MSG );
1584 $mwMsg->matchStartAndRemove( $part1 );
1585 }
1586
1587 # Check if it is an internal message
1588 $mwInt =& MagicWord::get( MAG_INT );
1589 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1590 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1591 $text = wfMsgReal( $part1, $args, true );
1592 $found = true;
1593 }
1594 }
1595 }
1596
1597 # NS
1598 if ( !$found ) {
1599 # Check for NS: (namespace expansion)
1600 $mwNs = MagicWord::get( MAG_NS );
1601 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1602 if ( intval( $part1 ) ) {
1603 $text = $wgLang->getNsText( intval( $part1 ) );
1604 $found = true;
1605 } else {
1606 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1607 if ( !is_null( $index ) ) {
1608 $text = $wgLang->getNsText( $index );
1609 $found = true;
1610 }
1611 }
1612 }
1613 }
1614
1615 # LOCALURL and LOCALURLE
1616 if ( !$found ) {
1617 $mwLocal = MagicWord::get( MAG_LOCALURL );
1618 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1619
1620 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1621 $func = 'getLocalURL';
1622 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1623 $func = 'escapeLocalURL';
1624 } else {
1625 $func = '';
1626 }
1627
1628 if ( $func !== '' ) {
1629 $title = Title::newFromText( $part1 );
1630 if ( !is_null( $title ) ) {
1631 if ( $argc > 0 ) {
1632 $text = $title->$func( $args[0] );
1633 } else {
1634 $text = $title->$func();
1635 }
1636 $found = true;
1637 }
1638 }
1639 }
1640
1641 # Internal variables
1642 if ( !$this->mVariables ) {
1643 $this->initialiseVariables();
1644 }
1645 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1646 $text = $this->mVariables[$part1];
1647 $found = true;
1648 $this->mOutput->mContainsOldMagic = true;
1649 }
1650
1651 # Template table test
1652
1653 # Did we encounter this template already? If yes, it is in the cache
1654 # and we need to check for loops.
1655 if ( isset( $this->mTemplates[$part1] ) ) {
1656 # Infinite loop test
1657 if ( isset( $this->mTemplatePath[$part1] ) ) {
1658 $noparse = true;
1659 $found = true;
1660 }
1661 # set $text to cached message.
1662 $text = $this->mTemplates[$part1];
1663 $found = true;
1664 }
1665
1666 # Load from database
1667 if ( !$found ) {
1668 $title = Title::newFromText( $part1, NS_TEMPLATE );
1669 if ( !is_null( $title ) && !$title->isExternal() ) {
1670 # Check for excessive inclusion
1671 $dbk = $title->getPrefixedDBkey();
1672 if ( $this->incrementIncludeCount( $dbk ) ) {
1673 # This should never be reached.
1674 $article = new Article( $title );
1675 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1676 if ( $articleContent !== false ) {
1677 $found = true;
1678 $text = $articleContent;
1679
1680 }
1681 }
1682
1683 # If the title is valid but undisplayable, make a link to it
1684 if ( $this->mOutputType == OT_HTML && !$found ) {
1685 $text = '[[' . $title->getPrefixedText() . ']]';
1686 $found = true;
1687 }
1688
1689 # Template cache array insertion
1690 $this->mTemplates[$part1] = $text;
1691 }
1692 }
1693
1694 # Recursive parsing, escaping and link table handling
1695 # Only for HTML output
1696 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1697 $text = wfEscapeWikiText( $text );
1698 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1699 # Clean up argument array
1700 $assocArgs = array();
1701 $index = 1;
1702 foreach( $args as $arg ) {
1703 $eqpos = strpos( $arg, '=' );
1704 if ( $eqpos === false ) {
1705 $assocArgs[$index++] = $arg;
1706 } else {
1707 $name = trim( substr( $arg, 0, $eqpos ) );
1708 $value = trim( substr( $arg, $eqpos+1 ) );
1709 if ( $value === false ) {
1710 $value = '';
1711 }
1712 if ( $name !== false ) {
1713 $assocArgs[$name] = $value;
1714 }
1715 }
1716 }
1717
1718 # Do not enter included links in link table
1719 if ( !is_null( $title ) ) {
1720 $wgLinkCache->suspend();
1721 }
1722
1723 # Add a new element to the templace recursion path
1724 $this->mTemplatePath[$part1] = 1;
1725
1726 # Run full parser on the included text
1727 $text = $this->stripParse( $text, $newline, $assocArgs );
1728
1729 # Resume the link cache and register the inclusion as a link
1730 if ( !is_null( $title ) ) {
1731 $wgLinkCache->resume();
1732 $wgLinkCache->addLinkObj( $title );
1733 }
1734 }
1735 # Empties the template path
1736 $this->mTemplatePath = array();
1737
1738 if ( !$found ) {
1739 return $matches[0];
1740 } else {
1741 return $text;
1742 }
1743 }
1744
1745 # Triple brace replacement -- used for template arguments
1746 function argSubstitution( $matches ) {
1747 $newline = $matches[1];
1748 $arg = trim( $matches[2] );
1749 $text = $matches[0];
1750 $inputArgs = end( $this->mArgStack );
1751
1752 if ( array_key_exists( $arg, $inputArgs ) ) {
1753 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1754 }
1755
1756 return $text;
1757 }
1758
1759 # Returns true if the function is allowed to include this entity
1760 function incrementIncludeCount( $dbk ) {
1761 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1762 $this->mIncludeCount[$dbk] = 0;
1763 }
1764 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1765 return true;
1766 } else {
1767 return false;
1768 }
1769 }
1770
1771
1772 # Cleans up HTML, removes dangerous tags and attributes
1773 /* private */ function removeHTMLtags( $text ) {
1774 global $wgUseTidy, $wgUserHtml;
1775 $fname = 'Parser::removeHTMLtags';
1776 wfProfileIn( $fname );
1777
1778 if( $wgUserHtml ) {
1779 $htmlpairs = array( # Tags that must be closed
1780 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1781 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1782 'strike', 'strong', 'tt', 'var', 'div', 'center',
1783 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1784 'ruby', 'rt' , 'rb' , 'rp', 'p'
1785 );
1786 $htmlsingle = array(
1787 'br', 'hr', 'li', 'dt', 'dd'
1788 );
1789 $htmlnest = array( # Tags that can be nested--??
1790 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1791 'dl', 'font', 'big', 'small', 'sub', 'sup'
1792 );
1793 $tabletags = array( # Can only appear inside table
1794 'td', 'th', 'tr'
1795 );
1796 } else {
1797 $htmlpairs = array();
1798 $htmlsingle = array();
1799 $htmlnest = array();
1800 $tabletags = array();
1801 }
1802
1803 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1804 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1805
1806 $htmlattrs = $this->getHTMLattrs () ;
1807
1808 # Remove HTML comments
1809 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1810
1811 $bits = explode( '<', $text );
1812 $text = array_shift( $bits );
1813 if(!$wgUseTidy) {
1814 $tagstack = array(); $tablestack = array();
1815 foreach ( $bits as $x ) {
1816 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1817 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1818 $x, $regs );
1819 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1820 error_reporting( $prev );
1821
1822 $badtag = 0 ;
1823 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1824 # Check our stack
1825 if ( $slash ) {
1826 # Closing a tag...
1827 if ( ! in_array( $t, $htmlsingle ) &&
1828 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1829 @array_push( $tagstack, $ot );
1830 $badtag = 1;
1831 } else {
1832 if ( $t == 'table' ) {
1833 $tagstack = array_pop( $tablestack );
1834 }
1835 $newparams = '';
1836 }
1837 } else {
1838 # Keep track for later
1839 if ( in_array( $t, $tabletags ) &&
1840 ! in_array( 'table', $tagstack ) ) {
1841 $badtag = 1;
1842 } else if ( in_array( $t, $tagstack ) &&
1843 ! in_array ( $t , $htmlnest ) ) {
1844 $badtag = 1 ;
1845 } else if ( ! in_array( $t, $htmlsingle ) ) {
1846 if ( $t == 'table' ) {
1847 array_push( $tablestack, $tagstack );
1848 $tagstack = array();
1849 }
1850 array_push( $tagstack, $t );
1851 }
1852 # Strip non-approved attributes from the tag
1853 $newparams = $this->fixTagAttributes($params);
1854
1855 }
1856 if ( ! $badtag ) {
1857 $rest = str_replace( '>', '&gt;', $rest );
1858 $text .= "<$slash$t $newparams$brace$rest";
1859 continue;
1860 }
1861 }
1862 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1863 }
1864 # Close off any remaining tags
1865 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1866 $text .= "</$t>\n";
1867 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1868 }
1869 } else {
1870 # this might be possible using tidy itself
1871 foreach ( $bits as $x ) {
1872 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1873 $x, $regs );
1874 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1875 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1876 $newparams = $this->fixTagAttributes($params);
1877 $rest = str_replace( '>', '&gt;', $rest );
1878 $text .= "<$slash$t $newparams$brace$rest";
1879 } else {
1880 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1881 }
1882 }
1883 }
1884 wfProfileOut( $fname );
1885 return $text;
1886 }
1887
1888
1889 /*
1890 *
1891 * This function accomplishes several tasks:
1892 * 1) Auto-number headings if that option is enabled
1893 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1894 * 3) Add a Table of contents on the top for users who have enabled the option
1895 * 4) Auto-anchor headings
1896 *
1897 * It loops through all headlines, collects the necessary data, then splits up the
1898 * string and re-inserts the newly formatted headlines.
1899 *
1900 */
1901
1902 /* private */ function formatHeadings( $text, $isMain=true ) {
1903 global $wgInputEncoding, $wgMaxTocLevel;
1904
1905 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1906 $doShowToc = $this->mOptions->getShowToc();
1907 $forceTocHere = false;
1908 if( !$this->mTitle->userCanEdit() ) {
1909 $showEditLink = 0;
1910 $rightClickHack = 0;
1911 } else {
1912 $showEditLink = $this->mOptions->getEditSection();
1913 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1914 }
1915
1916 # Inhibit editsection links if requested in the page
1917 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1918 if( $esw->matchAndRemove( $text ) ) {
1919 $showEditLink = 0;
1920 }
1921 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1922 # do not add TOC
1923 $mw =& MagicWord::get( MAG_NOTOC );
1924 if( $mw->matchAndRemove( $text ) ) {
1925 $doShowToc = 0;
1926 }
1927
1928 # never add the TOC to the Main Page. This is an entry page that should not
1929 # be more than 1-2 screens large anyway
1930 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
1931 $doShowToc = 0;
1932 }
1933
1934 # Get all headlines for numbering them and adding funky stuff like [edit]
1935 # links - this is for later, but we need the number of headlines right now
1936 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1937
1938 # if there are fewer than 4 headlines in the article, do not show TOC
1939 if( $numMatches < 4 ) {
1940 $doShowToc = 0;
1941 }
1942
1943 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1944 # override above conditions and always show TOC at that place
1945 $mw =& MagicWord::get( MAG_TOC );
1946 if ($mw->match( $text ) ) {
1947 $doShowToc = 1;
1948 $forceTocHere = true;
1949 } else {
1950 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1951 # override above conditions and always show TOC above first header
1952 $mw =& MagicWord::get( MAG_FORCETOC );
1953 if ($mw->matchAndRemove( $text ) ) {
1954 $doShowToc = 1;
1955 }
1956 }
1957
1958
1959
1960 # We need this to perform operations on the HTML
1961 $sk =& $this->mOptions->getSkin();
1962
1963 # headline counter
1964 $headlineCount = 0;
1965
1966 # Ugh .. the TOC should have neat indentation levels which can be
1967 # passed to the skin functions. These are determined here
1968 $toclevel = 0;
1969 $toc = '';
1970 $full = '';
1971 $head = array();
1972 $sublevelCount = array();
1973 $level = 0;
1974 $prevlevel = 0;
1975 foreach( $matches[3] as $headline ) {
1976 $numbering = '';
1977 if( $level ) {
1978 $prevlevel = $level;
1979 }
1980 $level = $matches[1][$headlineCount];
1981 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1982 # reset when we enter a new level
1983 $sublevelCount[$level] = 0;
1984 $toc .= $sk->tocIndent( $level - $prevlevel );
1985 $toclevel += $level - $prevlevel;
1986 }
1987 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1988 # reset when we step back a level
1989 $sublevelCount[$level+1]=0;
1990 $toc .= $sk->tocUnindent( $prevlevel - $level );
1991 $toclevel -= $prevlevel - $level;
1992 }
1993 # count number of headlines for each level
1994 @$sublevelCount[$level]++;
1995 if( $doNumberHeadings || $doShowToc ) {
1996 $dot = 0;
1997 for( $i = 1; $i <= $level; $i++ ) {
1998 if( !empty( $sublevelCount[$i] ) ) {
1999 if( $dot ) {
2000 $numbering .= '.';
2001 }
2002 $numbering .= $sublevelCount[$i];
2003 $dot = 1;
2004 }
2005 }
2006 }
2007
2008 # The canonized header is a version of the header text safe to use for links
2009 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2010 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2011 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2012
2013 # strip out HTML
2014 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2015 $tocline = trim( $canonized_headline );
2016 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2017 $replacearray = array(
2018 '%3A' => ':',
2019 '%' => '.'
2020 );
2021 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2022 $refer[$headlineCount] = $canonized_headline;
2023
2024 # count how many in assoc. array so we can track dupes in anchors
2025 @$refers[$canonized_headline]++;
2026 $refcount[$headlineCount]=$refers[$canonized_headline];
2027
2028 # Prepend the number to the heading text
2029
2030 if( $doNumberHeadings || $doShowToc ) {
2031 $tocline = $numbering . ' ' . $tocline;
2032
2033 # Don't number the heading if it is the only one (looks silly)
2034 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2035 # the two are different if the line contains a link
2036 $headline=$numbering . ' ' . $headline;
2037 }
2038 }
2039
2040 # Create the anchor for linking from the TOC to the section
2041 $anchor = $canonized_headline;
2042 if($refcount[$headlineCount] > 1 ) {
2043 $anchor .= '_' . $refcount[$headlineCount];
2044 }
2045 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2046 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2047 }
2048 if( $showEditLink ) {
2049 if ( empty( $head[$headlineCount] ) ) {
2050 $head[$headlineCount] = '';
2051 }
2052 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
2053 }
2054
2055 # Add the edit section span
2056 if( $rightClickHack ) {
2057 $headline = $sk->editSectionScript($headlineCount+1,$headline);
2058 }
2059
2060 # give headline the correct <h#> tag
2061 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2062
2063 $headlineCount++;
2064 }
2065
2066 if( $doShowToc ) {
2067 $toclines = $headlineCount;
2068 $toc .= $sk->tocUnindent( $toclevel );
2069 $toc = $sk->tocTable( $toc );
2070 }
2071
2072 # split up and insert constructed headlines
2073
2074 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2075 $i = 0;
2076
2077 foreach( $blocks as $block ) {
2078 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2079 # This is the [edit] link that appears for the top block of text when
2080 # section editing is enabled
2081
2082 # Disabled because it broke block formatting
2083 # For example, a bullet point in the top line
2084 # $full .= $sk->editSectionLink(0);
2085 }
2086 $full .= $block;
2087 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2088 # Top anchor now in skin
2089 $full = $full.$toc;
2090 }
2091
2092 if( !empty( $head[$i] ) ) {
2093 $full .= $head[$i];
2094 }
2095 $i++;
2096 }
2097 if($forceTocHere) {
2098 $mw =& MagicWord::get( MAG_TOC );
2099 return $mw->replace( $toc, $full );
2100 } else {
2101 return $full;
2102 }
2103 }
2104
2105 # Return an HTML link for the "ISBN 123456" text
2106 /* private */ function magicISBN( $text ) {
2107 global $wgLang;
2108 $fname = 'Parser::magicISBN';
2109 wfProfileIn( $fname );
2110
2111 $a = split( 'ISBN ', " $text" );
2112 if ( count ( $a ) < 2 ) {
2113 wfProfileOut( $fname );
2114 return $text;
2115 }
2116 $text = substr( array_shift( $a ), 1);
2117 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2118
2119 foreach ( $a as $x ) {
2120 $isbn = $blank = '' ;
2121 while ( ' ' == $x{0} ) {
2122 $blank .= ' ';
2123 $x = substr( $x, 1 );
2124 }
2125 while ( strstr( $valid, $x{0} ) != false ) {
2126 $isbn .= $x{0};
2127 $x = substr( $x, 1 );
2128 }
2129 $num = str_replace( '-', '', $isbn );
2130 $num = str_replace( ' ', '', $num );
2131
2132 if ( '' == $num ) {
2133 $text .= "ISBN $blank$x";
2134 } else {
2135 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2136 $text .= '<a href="' .
2137 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2138 "\" class=\"internal\">ISBN $isbn</a>";
2139 $text .= $x;
2140 }
2141 }
2142 wfProfileOut( $fname );
2143 return $text;
2144 }
2145
2146 # Return an HTML link for the "GEO ..." text
2147 /* private */ function magicGEO( $text ) {
2148 global $wgLang, $wgUseGeoMode;
2149 if ( !isset ( $wgUseGeoMode ) || !$wgUseGeoMode ) return $text ;
2150 $fname = 'Parser::magicGEO';
2151 wfProfileIn( $fname );
2152
2153 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2154 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2155 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2156 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2157 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2158 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2159
2160 $a = split( 'GEO ', " $text" );
2161 if ( count ( $a ) < 2 ) {
2162 wfProfileOut( $fname );
2163 return $text;
2164 }
2165 $text = substr( array_shift( $a ), 1);
2166 $valid = '0123456789.+-:';
2167
2168 foreach ( $a as $x ) {
2169 $geo = $blank = '' ;
2170 while ( ' ' == $x{0} ) {
2171 $blank .= ' ';
2172 $x = substr( $x, 1 );
2173 }
2174 while ( strstr( $valid, $x{0} ) != false ) {
2175 $geo .= $x{0};
2176 $x = substr( $x, 1 );
2177 }
2178 $num = str_replace( '+', '', $geo );
2179 $num = str_replace( ' ', '', $num );
2180
2181 if ( '' == $num || count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2182 $text .= "GEO $blank$x";
2183 } else {
2184 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2185 $text .= '<a href="' .
2186 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2187 "\" class=\"internal\">GEO $geo</a>";
2188 $text .= $x;
2189 }
2190 }
2191 wfProfileOut( $fname );
2192 return $text;
2193 }
2194
2195 # Return an HTML link for the "RFC 1234" text
2196 /* private */ function magicRFC( $text ) {
2197 global $wgLang;
2198
2199 $a = split( 'RFC ', ' '.$text );
2200 if ( count ( $a ) < 2 ) return $text;
2201 $text = substr( array_shift( $a ), 1);
2202 $valid = '0123456789';
2203
2204 foreach ( $a as $x ) {
2205 $rfc = $blank = '' ;
2206 while ( ' ' == $x{0} ) {
2207 $blank .= ' ';
2208 $x = substr( $x, 1 );
2209 }
2210 while ( strstr( $valid, $x{0} ) != false ) {
2211 $rfc .= $x{0};
2212 $x = substr( $x, 1 );
2213 }
2214
2215 if ( '' == $rfc ) {
2216 $text .= "RFC $blank$x";
2217 } else {
2218 $url = wfmsg( 'rfcurl' );
2219 $url = str_replace( '$1', $rfc, $url);
2220 $sk =& $this->mOptions->getSkin();
2221 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2222 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2223 }
2224 }
2225 return $text;
2226 }
2227
2228 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2229 $this->mOptions = $options;
2230 $this->mTitle =& $title;
2231 $this->mOutputType = OT_WIKI;
2232
2233 if ( $clearState ) {
2234 $this->clearState();
2235 }
2236
2237 $stripState = false;
2238 $pairs = array(
2239 "\r\n" => "\n",
2240 );
2241 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2242 // now with regexes
2243 /*
2244 $pairs = array(
2245 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2246 "/<br *?>/i" => "<br />",
2247 );
2248 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2249 */
2250 $text = $this->strip( $text, $stripState, false );
2251 $text = $this->pstPass2( $text, $user );
2252 $text = $this->unstrip( $text, $stripState );
2253 $text = $this->unstripNoWiki( $text, $stripState );
2254 return $text;
2255 }
2256
2257 /* private */ function pstPass2( $text, &$user ) {
2258 global $wgLang, $wgLocaltimezone, $wgCurParser;
2259
2260 # Variable replacement
2261 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2262 $text = $this->replaceVariables( $text );
2263
2264 # Signatures
2265 #
2266 $n = $user->getName();
2267 $k = $user->getOption( 'nickname' );
2268 if ( '' == $k ) { $k = $n; }
2269 if(isset($wgLocaltimezone)) {
2270 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2271 }
2272 /* Note: this is an ugly timezone hack for the European wikis */
2273 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2274 ' (' . date( 'T' ) . ')';
2275 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2276
2277 $text = preg_replace( '/~~~~~/', $d, $text );
2278 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2279 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2280 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2281 Namespace::getUser() ) . ":$n|$k]]", $text );
2282
2283 # Context links: [[|name]] and [[name (context)|]]
2284 #
2285 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2286 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2287 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2288 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2289
2290 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2291 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2292 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2293 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2294 # [[ns:page (cont)|]]
2295 $context = "";
2296 $t = $this->mTitle->getText();
2297 if ( preg_match( $conpat, $t, $m ) ) {
2298 $context = $m[2];
2299 }
2300 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2301 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2302 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2303
2304 if ( '' == $context ) {
2305 $text = preg_replace( $p2, '[[\\1]]', $text );
2306 } else {
2307 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2308 }
2309
2310 /*
2311 $mw =& MagicWord::get( MAG_SUBST );
2312 $wgCurParser = $this->fork();
2313 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2314 $this->merge( $wgCurParser );
2315 */
2316
2317 # Trim trailing whitespace
2318 # MAG_END (__END__) tag allows for trailing
2319 # whitespace to be deliberately included
2320 $text = rtrim( $text );
2321 $mw =& MagicWord::get( MAG_END );
2322 $mw->matchAndRemove( $text );
2323
2324 return $text;
2325 }
2326
2327 # Set up some variables which are usually set up in parse()
2328 # so that an external function can call some class members with confidence
2329 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2330 $this->mTitle =& $title;
2331 $this->mOptions = $options;
2332 $this->mOutputType = $outputType;
2333 if ( $clearState ) {
2334 $this->clearState();
2335 }
2336 }
2337
2338 function transformMsg( $text, $options ) {
2339 global $wgTitle;
2340 static $executing = false;
2341
2342 # Guard against infinite recursion
2343 if ( $executing ) {
2344 return $text;
2345 }
2346 $executing = true;
2347
2348 $this->mTitle = $wgTitle;
2349 $this->mOptions = $options;
2350 $this->mOutputType = OT_MSG;
2351 $this->clearState();
2352 $text = $this->replaceVariables( $text );
2353
2354 $executing = false;
2355 return $text;
2356 }
2357
2358 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2359 # Callback will be called with the text within
2360 # Transform and return the text within
2361 function setHook( $tag, $callback ) {
2362 $oldVal = @$this->mTagHooks[$tag];
2363 $this->mTagHooks[$tag] = $callback;
2364 return $oldVal;
2365 }
2366 }
2367
2368 class ParserOutput
2369 {
2370 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2371 var $mCacheTime; # Used in ParserCache
2372
2373 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2374 $containsOldMagic = false )
2375 {
2376 $this->mText = $text;
2377 $this->mLanguageLinks = $languageLinks;
2378 $this->mCategoryLinks = $categoryLinks;
2379 $this->mContainsOldMagic = $containsOldMagic;
2380 $this->mCacheTime = "";
2381 }
2382
2383 function getText() { return $this->mText; }
2384 function getLanguageLinks() { return $this->mLanguageLinks; }
2385 function getCategoryLinks() { return $this->mCategoryLinks; }
2386 function getCacheTime() { return $this->mCacheTime; }
2387 function containsOldMagic() { return $this->mContainsOldMagic; }
2388 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2389 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2390 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2391 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2392 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2393
2394 function merge( $other ) {
2395 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2396 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2397 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2398 }
2399
2400 }
2401
2402 class ParserOptions
2403 {
2404 # All variables are private
2405 var $mUseTeX; # Use texvc to expand <math> tags
2406 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2407 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2408 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2409 var $mAllowExternalImages; # Allow external images inline
2410 var $mSkin; # Reference to the preferred skin
2411 var $mDateFormat; # Date format index
2412 var $mEditSection; # Create "edit section" links
2413 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2414 var $mNumberHeadings; # Automatically number headings
2415 var $mShowToc; # Show table of contents
2416
2417 function getUseTeX() { return $this->mUseTeX; }
2418 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2419 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2420 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2421 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2422 function getSkin() { return $this->mSkin; }
2423 function getDateFormat() { return $this->mDateFormat; }
2424 function getEditSection() { return $this->mEditSection; }
2425 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2426 function getNumberHeadings() { return $this->mNumberHeadings; }
2427 function getShowToc() { return $this->mShowToc; }
2428
2429 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2430 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2431 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2432 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2433 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2434 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2435 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2436 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2437 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2438 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2439
2440 function setSkin( &$x ) { $this->mSkin =& $x; }
2441
2442 /* static */ function newFromUser( &$user ) {
2443 $popts = new ParserOptions;
2444 $popts->initialiseFromUser( $user );
2445 return $popts;
2446 }
2447
2448 function initialiseFromUser( &$userInput ) {
2449 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2450
2451 if ( !$userInput ) {
2452 $user = new User;
2453 $user->setLoaded( true );
2454 } else {
2455 $user =& $userInput;
2456 }
2457
2458 $this->mUseTeX = $wgUseTeX;
2459 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2460 $this->mUseDynamicDates = $wgUseDynamicDates;
2461 $this->mInterwikiMagic = $wgInterwikiMagic;
2462 $this->mAllowExternalImages = $wgAllowExternalImages;
2463 $this->mSkin =& $user->getSkin();
2464 $this->mDateFormat = $user->getOption( 'date' );
2465 $this->mEditSection = $user->getOption( 'editsection' );
2466 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2467 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2468 $this->mShowToc = $user->getOption( 'showtoc' );
2469 }
2470
2471
2472 }
2473
2474 # Regex callbacks, used in Parser::replaceVariables
2475 function wfBraceSubstitution( $matches )
2476 {
2477 global $wgCurParser;
2478 return $wgCurParser->braceSubstitution( $matches );
2479 }
2480
2481 function wfArgSubstitution( $matches )
2482 {
2483 global $wgCurParser;
2484 return $wgCurParser->argSubstitution( $matches );
2485 }
2486
2487 function wfVariableSubstitution( $matches )
2488 {
2489 global $wgCurParser;
2490 return $wgCurParser->variableSubstitution( $matches );
2491 }
2492
2493 ?>