Trim whitespace on TOC links
[mediawiki.git] / includes / OutputPage.php
blob86038f56bfb67c780375b06b78e0a2bde0cac958
1 <?
2 # See design.doc
4 if($wgUseTeX) include_once( "Math.php" );
6 class OutputPage {
7 var $mHeaders, $mCookies, $mMetatags, $mKeywords;
8 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
9 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
10 var $mSubtitle, $mRedirect, $mAutonumber, $mHeadtext;
11 var $mLastModified, $mCategoryLinks;
13 var $mDTopen, $mLastSection; # Used for processing DL, PRE
14 var $mLanguageLinks, $mSupressQuickbar;
15 var $mOnloadHandler;
16 var $mDoNothing;
18 function OutputPage()
20 $this->mHeaders = $this->mCookies = $this->mMetatags =
21 $this->mKeywords = $this->mLinktags = array();
22 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
23 $this->mLastSection = $this->mRedirect = $this->mLastModified =
24 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
25 $this->mOnloadHandler = "";
26 $this->mIsarticle = $this->mPrintable = true;
27 $this->mSupressQuickbar = $this->mDTopen = $this->mPrintable = false;
28 $this->mLanguageLinks = array();
29 $this->mCategoryLinks = array() ;
30 $this->mAutonumber = 0;
31 $this->mDoNothing = false;
34 function addHeader( $name, $val ) { array_push( $this->mHeaders, "$name: $val" ) ; }
35 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
36 function redirect( $url ) { $this->mRedirect = $url; }
38 # To add an http-equiv meta tag, precede the name with "http:"
39 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
40 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
41 function addLink( $rel, $rev, $target ) { array_push( $this->mLinktags, array( $rel, $rev, $target ) ); }
43 # checkLastModified tells the client to use the client-cached page if
44 # possible. If sucessful, the OutputPage is disabled so that
45 # any future call to OutputPage->output() have no effect. The method
46 # returns true iff cache-ok headers was sent.
47 function checkLastModified ( $timestamp )
49 global $wgLang, $wgCachePages, $wgUser;
50 if( !$wgCachePages ) {
51 wfDebug( "CACHE DISABLED\n", false );
52 return;
54 if( preg_match( '/MSIE ([1-4]|5\.0)/', $_SERVER["HTTP_USER_AGENT"] ) ) {
55 # IE 5.0 has probs with our caching
56 wfDebug( "-- bad client, not caching\n", false );
57 return;
59 if( $wgUser->getOption( "nocache" ) ) {
60 wfDebug( "USER DISABLED CACHE\n", false );
61 return;
64 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
65 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
67 if( !empty( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
68 # IE sends sizes after the date like this:
69 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
70 # this breaks strtotime().
71 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
72 $ismodsince = wfUnix2Timestamp( strtotime( $modsince ) );
73 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
74 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
76 if( ($ismodsince >= $timestamp ) and $wgUser->validateCache( $ismodsince ) ) {
77 # Make sure you're in a place you can leave when you call us!
78 header( "HTTP/1.0 304 Not Modified" );
79 header( "Expires: Mon, 15 Jan 2001 00:00:00 GMT" ); # Cachers always validate the page!
80 header( "Cache-Control: private, must-revalidate, max-age=0" );
81 header( "Last-Modified: {$lastmod}" );
82 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
83 $this->disable();
84 return true;
85 } else {
86 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
87 $this->mLastModified = $lastmod;
89 } else {
90 wfDebug( "We're confused.\n", false );
91 $this->mLastModified = $lastmod;
95 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
96 function setHTMLtitle( $name ) { $this->mHTMLtitle = $name; }
97 function setPageTitle( $name ) { $this->mPagetitle = $name; }
98 function getPageTitle() { return $this->mPagetitle; }
99 function setSubtitle( $str ) { $this->mSubtitle = $str; }
100 function getSubtitle() { return $this->mSubtitle; }
101 function setArticleFlag( $v ) { $this->mIsarticle = $v; }
102 function isArticle() { return $this->mIsarticle; }
103 function setPrintable() { $this->mPrintable = true; }
104 function isPrintable() { return $this->mPrintable; }
105 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
106 function getOnloadHandler() { return $this->mOnloadHandler; }
107 function disable() { $this->mDoNothing = true; }
109 function getLanguageLinks() {
110 global $wgTitle, $wgLanguageCode;
111 global $wgDBconnection, $wgDBname;
112 return $this->mLanguageLinks;
114 function supressQuickbar() { $this->mSupressQuickbar = true; }
115 function isQuickbarSupressed() { return $this->mSupressQuickbar; }
117 function addHTML( $text ) { $this->mBodytext .= $text; }
118 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
119 function debug( $text ) { $this->mDebugtext .= $text; }
121 # First pass--just handle <nowiki> sections, pass the rest off
122 # to doWikiPass2() which does all the real work.
125 function addWikiText( $text, $linestart = true )
127 global $wgUseTeX;
128 $fname = "OutputPage::addWikiText";
129 wfProfileIn( $fname );
130 $unique = "3iyZiyA7iMwg5rhxP0Dcc9oTnj8qD1jm1Sfv4";
131 $unique2 = "4LIQ9nXtiYFPCSfitVwDw7EYwQlL4GeeQ7qSO";
132 $unique3 = "fPaA8gDfdLBqzj68Yjg9Hil3qEF8JGO0uszIp";
133 $nwlist = array();
134 $nwsecs = 0;
135 $mathlist = array();
136 $mathsecs = 0;
137 $prelist = array ();
138 $presecs = 0;
139 $stripped = "";
140 $stripped2 = "";
141 $stripped3 = "";
143 while ( "" != $text ) {
144 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
145 $stripped .= $p[0];
146 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
147 else {
148 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
149 ++$nwsecs;
150 $nwlist[$nwsecs] = wfEscapeHTMLTagsOnly($q[0]);
151 $stripped .= $unique . $nwsecs . "s";
152 $text = $q[1];
156 if( $wgUseTeX ) {
157 while ( "" != $stripped ) {
158 $p = preg_split( "/<\\s*math\\s*>/i", $stripped, 2 );
159 $stripped2 .= $p[0];
160 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $stripped = ""; }
161 else {
162 $q = preg_split( "/<\\/\\s*math\\s*>/i", $p[1], 2 );
163 ++$mathsecs;
164 $mathlist[$mathsecs] = renderMath($q[0]);
165 $stripped2 .= $unique2 . $mathsecs . "s";
166 $stripped = $q[1];
169 } else {
170 $stripped2 = $stripped;
173 while ( "" != $stripped2 ) {
174 $p = preg_split( "/<\\s*pre\\s*>/i", $stripped2, 2 );
175 $stripped3 .= $p[0];
176 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $stripped2 = ""; }
177 else {
178 $q = preg_split( "/<\\/\\s*pre\\s*>/i", $p[1], 2 );
179 ++$presecs;
180 $prelist[$presecs] = "<pre>". wfEscapeHTMLTagsOnly($q[0]). "</pre>";
181 $stripped3 .= $unique3 . $presecs . "s";
182 $stripped2 = $q[1];
186 $text = $this->doWikiPass2( $stripped3, $linestart );
188 $specialChars = array("\\", "$");
189 $escapedChars = array("\\\\", "\\$");
190 for ( $i = 1; $i <= $presecs; ++$i ) {
191 $text = preg_replace( "/{$unique3}{$i}s/", str_replace( $specialChars,
192 $escapedChars, $prelist[$i] ), $text );
195 for ( $i = 1; $i <= $mathsecs; ++$i ) {
196 $text = preg_replace( "/{$unique2}{$i}s/", str_replace( $specialChars,
197 $escapedChars, $mathlist[$i] ), $text );
200 for ( $i = 1; $i <= $nwsecs; ++$i ) {
201 $text = preg_replace( "/{$unique}{$i}s/", str_replace( $specialChars,
202 $escapedChars, $nwlist[$i] ), $text );
204 $this->addHTML( $text );
205 wfProfileOut( $fname );
208 function sendCacheControl() {
209 global $wgUseGzip;
210 if( $this->mLastModified != "" ) {
211 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
212 header( "Cache-Control: private, must-revalidate, max-age=0" );
213 header( "Last-modified: {$this->mLastModified}" );
214 if( $wgUseGzip ) {
215 # We should put in Accept-Encoding, but IE chokes on anything but
216 # User-Agent in a Vary: header (at least through 6.0)
217 header( "Vary: User-Agent" );
219 } else {
220 wfDebug( "** no caching **\n", false );
221 header( "Cache-Control: no-cache" ); # Experimental - see below
222 header( "Pragma: no-cache" );
223 header( "Last-modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
225 header( "Expires: Mon, 15 Jan 2001 00:00:00 GMT" ); # Cachers always validate the page!
228 # Finally, all the text has been munged and accumulated into
229 # the object, let's actually output it:
231 function output()
233 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
234 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
235 if( $this->mDoNothing ){
236 return;
238 $fname = "OutputPage::output";
239 wfProfileIn( $fname );
241 $sk = $wgUser->getSkin();
243 $this->sendCacheControl();
245 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
246 header( "Content-language: {$wgLanguageCode}" );
248 if ( "" != $this->mRedirect ) {
249 header( "Location: {$this->mRedirect}" );
250 return;
253 $exp = time() + $wgCookieExpiration;
254 foreach( $this->mCookies as $name => $val ) {
255 setcookie( $name, $val, $exp, "/" );
258 $sk->outputPage( $this );
259 flush();
262 function out( $ins )
264 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
265 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
266 $outs = $ins;
267 } else {
268 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
269 if ( false === $outs ) { $outs = $ins; }
271 print $outs;
274 function setEncodings()
276 global $wgInputEncoding, $wgOutputEncoding;
277 global $wgUser, $wgLang;
279 $wgInputEncoding = strtolower( $wgInputEncoding );
281 if( $wgUser->getOption( 'altencoding' ) ) {
282 $wgLang->setAltEncoding();
283 return;
286 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
287 $wgOutputEncoding = strtolower( $wgOutputEncoding );
288 return;
292 # This code is unused anyway!
293 # Commenting out. --bv 2003-11-15
295 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
296 $best = 0.0;
297 $bestset = "*";
299 foreach ( $a as $s ) {
300 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
301 $set = $m[1];
302 $q = (float)($m[2]);
303 } else {
304 $set = $s;
305 $q = 1.0;
307 if ( $q > $best ) {
308 $bestset = $set;
309 $best = $q;
312 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
313 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
314 $wgOutputEncoding = strtolower( $bestset );
316 # Disable for now
319 $wgOutputEncoding = $wgInputEncoding;
322 # Returns a HTML comment with the elapsed time since request.
323 # This method has no side effects.
324 function reportTime()
326 global $wgRequestTime;
328 list( $usec, $sec ) = explode( " ", microtime() );
329 $now = (float)$sec + (float)$usec;
331 list( $usec, $sec ) = explode( " ", $wgRequestTime );
332 $start = (float)$sec + (float)$usec;
333 $elapsed = $now - $start;
334 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
335 $elapsed );
336 return $com;
339 # Note: these arguments are keys into wfMsg(), not text!
341 function errorpage( $title, $msg )
343 global $wgTitle;
345 $this->mDebugtext .= "Original title: " .
346 $wgTitle->getPrefixedText() . "\n";
347 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
348 $this->setPageTitle( wfMsg( $title ) );
349 $this->setRobotpolicy( "noindex,nofollow" );
350 $this->setArticleFlag( false );
352 $this->mBodytext = "";
353 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
354 $this->returnToMain( false );
356 $this->output();
357 wfAbruptExit();
360 function sysopRequired()
362 global $wgUser;
364 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
365 $this->setPageTitle( wfMsg( "sysoptitle" ) );
366 $this->setRobotpolicy( "noindex,nofollow" );
367 $this->setArticleFlag( false );
368 $this->mBodytext = "";
370 $sk = $wgUser->getSkin();
371 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
372 $this->addHTML( wfMsg( "sysoptext", $ap ) );
373 $this->returnToMain();
376 function developerRequired()
378 global $wgUser;
380 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
381 $this->setPageTitle( wfMsg( "developertitle" ) );
382 $this->setRobotpolicy( "noindex,nofollow" );
383 $this->setArticleFlag( false );
384 $this->mBodytext = "";
386 $sk = $wgUser->getSkin();
387 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
388 $this->addHTML( wfMsg( "developertext", $ap ) );
389 $this->returnToMain();
392 function databaseError( $fname )
394 global $wgUser, $wgCommandLineMode;
396 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
397 $this->setRobotpolicy( "noindex,nofollow" );
398 $this->setArticleFlag( false );
400 if ( $wgCommandLineMode ) {
401 $msg = wfMsgNoDB( "dberrortextcl" );
402 } else {
403 $msg = wfMsgNoDB( "dberrortext" );
406 $msg = str_replace( "$1", htmlspecialchars( wfLastDBquery() ), $msg );
407 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
408 $msg = str_replace( "$3", wfLastErrno(), $msg );
409 $msg = str_replace( "$4", htmlspecialchars( wfLastError() ), $msg );
411 if ( $wgCommandLineMode ) {
412 print "$msg\n";
413 wfAbruptExit();
415 $sk = $wgUser->getSkin();
416 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
417 wfMsgNoDB( "searchingwikipedia" ) );
418 $msg = str_replace( "$5", $shlink, $msg );
420 $this->mBodytext = $msg;
421 $this->output();
422 wfAbruptExit();
425 function readOnlyPage( $source = "", $protected = false )
427 global $wgUser, $wgReadOnlyFile;
429 $this->setRobotpolicy( "noindex,nofollow" );
430 $this->setArticleFlag( false );
432 if( $protected ) {
433 $this->setPageTitle( wfMsg( "viewsource" ) );
434 $this->addWikiText( wfMsg( "protectedtext" ) );
435 } else {
436 $this->setPageTitle( wfMsg( "readonly" ) );
437 $reason = file_get_contents( $wgReadOnlyFile );
438 $this->addHTML( wfMsg( "readonlytext", $reason ) );
441 if($source) {
442 $rows = $wgUser->getOption( "rows" );
443 $cols = $wgUser->getOption( "cols" );
444 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
445 htmlspecialchars( $source ) . "\n</textarea>";
446 $this->addHTML( $text );
449 $this->returnToMain( false );
452 function fatalError( $message )
454 $this->setPageTitle( wfMsg( "internalerror" ) );
455 $this->setRobotpolicy( "noindex,nofollow" );
456 $this->setArticleFlag( false );
458 $this->mBodytext = $message;
459 $this->output();
460 wfAbruptExit();
463 function unexpectedValueError( $name, $val )
465 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
468 function fileCopyError( $old, $new )
470 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
473 function fileRenameError( $old, $new )
475 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
478 function fileDeleteError( $name )
480 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
483 function fileNotFoundError( $name )
485 $this->fatalError( wfMsg( "filenotfound", $name ) );
488 function returnToMain( $auto = true )
490 global $wgUser, $wgOut, $returnto;
492 $sk = $wgUser->getSkin();
493 if ( "" == $returnto ) {
494 $returnto = wfMsg( "mainpage" );
496 $link = $sk->makeKnownLink( $returnto, "" );
498 $r = wfMsg( "returnto", $link );
499 if ( $auto ) {
500 $wgOut->addMeta( "http:Refresh", "10;url=" .
501 wfLocalUrlE( wfUrlencode( $returnto ) ) );
503 $wgOut->addHTML( "\n<p>$r\n" );
507 function categoryMagic ()
509 global $wgTitle , $wgUseCategoryMagic ;
510 if ( !isset ( $wgUseCategoryMagic ) || !$wgUseCategoryMagic ) return ;
511 $id = $wgTitle->getArticleID() ;
512 $cat = ucfirst ( wfMsg ( "category" ) ) ;
513 $ti = $wgTitle->getText() ;
514 $ti = explode ( ":" , $ti , 2 ) ;
515 if ( $cat != $ti[0] ) return "" ;
516 $r = "<br break=all>\n" ;
518 $articles = array() ;
519 $parents = array () ;
520 $children = array() ;
523 global $wgUser ;
524 $sk = $wgUser->getSkin() ;
525 $sql = "SELECT l_from FROM links WHERE l_to={$id}" ;
526 $res = wfQuery ( $sql, DB_READ ) ;
527 while ( $x = wfFetchObject ( $res ) )
529 # $t = new Title ;
530 # $t->newFromDBkey ( $x->l_from ) ;
531 # $t = $t->getText() ;
532 $t = $x->l_from ;
533 $y = explode ( ":" , $t , 2 ) ;
534 if ( count ( $y ) == 2 && $y[0] == $cat ) {
535 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
536 } else {
537 array_push ( $articles , $sk->makeLink ( $t ) ) ;
540 wfFreeResult ( $res ) ;
542 # Children
543 if ( count ( $children ) > 0 )
545 asort ( $children ) ;
546 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
547 $r .= implode ( ", " , $children ) ;
550 # Articles
551 if ( count ( $articles ) > 0 )
553 asort ( $articles ) ;
554 $h = wfMsg( "category_header", $ti[1] );
555 $r .= "<h2>{$h}</h2>\n" ;
556 $r .= implode ( ", " , $articles ) ;
560 return $r ;
563 function getHTMLattrs ()
565 $htmlattrs = array( # Allowed attributes--no scripting, etc.
566 "title", "align", "lang", "dir", "width", "height",
567 "bgcolor", "clear", /* BR */ "noshade", /* HR */
568 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
569 /* FONT */ "type", "start", "value", "compact",
570 /* For various lists, mostly deprecated but safe */
571 "summary", "width", "border", "frame", "rules",
572 "cellspacing", "cellpadding", "valign", "char",
573 "charoff", "colgroup", "col", "span", "abbr", "axis",
574 "headers", "scope", "rowspan", "colspan", /* Tables */
575 "id", "class", "name", "style" /* For CSS */
577 return $htmlattrs ;
580 function fixTableTags ( $t )
582 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
583 $htmlattrs = $this->getHTMLattrs() ;
585 # Strip non-approved attributes from the tag
586 $t = preg_replace(
587 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
588 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
589 $t);
591 return trim ( $t ) ;
594 function doTableStuff ( $t )
596 $t = explode ( "\n" , $t ) ;
597 $td = array () ; # Is currently a td tag open?
598 $ltd = array () ; # Was it TD or TH?
599 $tr = array () ; # Is currently a tr tag open?
600 $ltr = array () ; # tr attributes
601 foreach ( $t AS $k => $x )
603 $x = rtrim ( $x ) ;
604 $fc = substr ( $x , 0 , 1 ) ;
605 if ( "{|" == substr ( $x , 0 , 2 ) )
607 $t[$k] = "<table " . $this->fixTableTags ( substr ( $x , 3 ) ) . ">" ;
608 array_push ( $td , false ) ;
609 array_push ( $ltd , "" ) ;
610 array_push ( $tr , false ) ;
611 array_push ( $ltr , "" ) ;
613 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
614 else if ( "|}" == substr ( $x , 0 , 2 ) )
616 $z = "</table>\n" ;
617 $l = array_pop ( $ltd ) ;
618 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
619 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
620 array_pop ( $ltr ) ;
621 $t[$k] = $z ;
623 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
625 $z = trim ( substr ( $x , 2 ) ) ;
626 $t[$k] = "<caption>{$z}</caption>\n" ;
628 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
630 $x = substr ( $x , 1 ) ;
631 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
632 $z = "" ;
633 $l = array_pop ( $ltd ) ;
634 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
635 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
636 array_pop ( $ltr ) ;
637 $t[$k] = $z ;
638 array_push ( $tr , false ) ;
639 array_push ( $td , false ) ;
640 array_push ( $ltd , "" ) ;
641 array_push ( $ltr , $this->fixTableTags ( $x ) ) ;
643 else if ( "|" == $fc || "!" == $fc || "|+" == substr ( $x , 0 , 2 ) ) # Caption
645 if ( "|+" == substr ( $x , 0 , 2 ) )
647 $fc = "+" ;
648 $x = substr ( $x , 1 ) ;
650 $after = substr ( $x , 1 ) ;
651 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
652 $after = explode ( "||" , $after ) ;
653 $t[$k] = "" ;
654 foreach ( $after AS $theline )
656 $z = "" ;
657 $tra = array_pop ( $ltr ) ;
658 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
659 array_push ( $tr , true ) ;
660 array_push ( $ltr , "" ) ;
662 $l = array_pop ( $ltd ) ;
663 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
664 if ( $fc == "|" ) $l = "TD" ;
665 else if ( $fc == "!" ) $l = "TH" ;
666 else if ( $fc == "+" ) $l = "CAPTION" ;
667 else $l = "" ;
668 array_push ( $ltd , $l ) ;
669 $y = explode ( "|" , $theline , 2 ) ;
670 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
671 else $y = $y = "{$z}<{$l} ".$this->fixTableTags($y[0]).">{$y[1]}" ;
672 $t[$k] .= $y ;
673 array_push ( $td , true ) ;
678 # Closing open td, tr && table
679 while ( count ( $td ) > 0 )
681 if ( array_pop ( $td ) ) $t[] = "</td>" ;
682 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
683 $t[] = "</table>" ;
686 $t = implode ( "\n" , $t ) ;
687 # $t = $this->removeHTMLtags( $t );
688 return $t ;
691 # Well, OK, it's actually about 14 passes. But since all the
692 # hard lifting is done inside PHP's regex code, it probably
693 # wouldn't speed things up much to add a real parser.
695 function doWikiPass2( $text, $linestart )
697 global $wgUser, $wgLang, $wgUseDynamicDates;
698 $fname = "OutputPage::doWikiPass2";
699 wfProfileIn( $fname );
701 $text = $this->removeHTMLtags( $text );
702 $text = $this->replaceVariables( $text );
704 $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
705 $text = str_replace ( "<HR>", "<hr>", $text );
707 $text = $this->doAllQuotes( $text );
708 $text = $this->doHeadings( $text );
709 $text = $this->doBlockLevels( $text, $linestart );
711 if($wgUseDynamicDates) {
712 global $wgDateFormatter;
713 $text = $wgDateFormatter->reformat( $wgUser->getOption("date"), $text );
716 $text = $this->replaceExternalLinks( $text );
717 $text = $this->replaceInternalLinks ( $text );
718 $text = $this->doTableStuff ( $text ) ;
720 $text = $this->magicISBN( $text );
721 $text = $this->magicRFC( $text );
722 $text = $this->formatHeadings( $text );
724 $sk = $wgUser->getSkin();
725 $text = $sk->transformContent( $text );
726 $text .= $this->categoryMagic () ;
728 wfProfileOut( $fname );
729 return $text;
732 /* private */ function doAllQuotes( $text )
734 $outtext = "";
735 $lines = explode( "\r\n", $text );
736 foreach ( $lines as $line ) {
737 $outtext .= $this->doQuotes ( "", $line, "" ) . "\r\n";
739 return $outtext;
742 /* private */ function doQuotes( $pre, $text, $mode )
744 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
745 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
746 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
747 if ( substr ($m[2], 0, 1) == "'" ) {
748 $m[2] = substr ($m[2], 1);
749 if ($mode == "em") {
750 return $this->doQuotes ( $m[1], $m[2], ($m[1] == "") ? "both" : "emstrong" );
751 } else if ($mode == "strong") {
752 return $m1_strong . $this->doQuotes ( "", $m[2], "" );
753 } else if (($mode == "emstrong") || ($mode == "both")) {
754 return $this->doQuotes ( "", $pre.$m1_strong.$m[2], "em" );
755 } else if ($mode == "strongem") {
756 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( "", $m[2], "em" );
757 } else {
758 return $m[1] . $this->doQuotes ( "", $m[2], "strong" );
760 } else {
761 if ($mode == "strong") {
762 return $this->doQuotes ( $m[1], $m[2], ($m[1] == "") ? "both" : "strongem" );
763 } else if ($mode == "em") {
764 return $m1_em . $this->doQuotes ( "", $m[2], "" );
765 } else if ($mode == "emstrong") {
766 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( "", $m[2], "strong" );
767 } else if (($mode == "strongem") || ($mode == "both")) {
768 return $this->doQuotes ( "", $pre.$m1_em.$m[2], "strong" );
769 } else {
770 return $m[1] . $this->doQuotes ( "", $m[2], "em" );
773 } else {
774 $text_strong = ($text == "") ? "" : "<strong>{$text}</strong>";
775 $text_em = ($text == "") ? "" : "<em>{$text}</em>";
776 if ($mode == "") {
777 return $pre . $text;
778 } else if ($mode == "em") {
779 return $pre . $text_em;
780 } else if ($mode == "strong") {
781 return $pre . $text_strong;
782 } else if ($mode == "strongem") {
783 return (($pre == "") && ($text == "")) ? "" : "<strong>{$pre}{$text_em}</strong>";
784 } else {
785 return (($pre == "") && ($text == "")) ? "" : "<em>{$pre}{$text_strong}</em>";
790 /* private */ function doHeadings( $text )
792 for ( $i = 6; $i >= 1; --$i ) {
793 $h = substr( "======", 0, $i );
794 $text = preg_replace( "/^{$h}([^=]+){$h}(\\s|$)/m",
795 "<h{$i}>\\1</h{$i}>\\2", $text );
797 return $text;
800 # Note: we have to do external links before the internal ones,
801 # and otherwise take great care in the order of things here, so
802 # that we don't end up interpreting some URLs twice.
804 /* private */ function replaceExternalLinks( $text )
806 $fname = "OutputPage::replaceExternalLinks";
807 wfProfileIn( $fname );
808 $text = $this->subReplaceExternalLinks( $text, "http", true );
809 $text = $this->subReplaceExternalLinks( $text, "https", true );
810 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
811 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
812 $text = $this->subReplaceExternalLinks( $text, "news", false );
813 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
814 wfProfileOut( $fname );
815 return $text;
818 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
820 global $wgUser, $printable;
821 global $wgAllowExternalImages;
824 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
825 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
827 # this is the list of separators that should be ignored if they
828 # are the last character of an URL but that should be included
829 # if they occur within the URL, e.g. "go to www.foo.com, where .."
830 # in this case, the last comma should not become part of the URL,
831 # but in "www.foo.com/123,2342,32.htm" it should.
832 $sep = ",;\.:";
833 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
834 $images = "gif|png|jpg|jpeg";
836 # PLEASE NOTE: The curly braces { } are not part of the regex,
837 # they are interpreted as part of the string (used to tell PHP
838 # that the content of the string should be inserted there).
839 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
840 "((?i){$images})([^{$uc}]|$)/";
842 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
843 $sk = $wgUser->getSkin();
845 if ( $autonumber and $wgAllowExternalImages) { # Use img tags only for HTTP urls
846 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
847 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
849 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
850 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
851 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
852 "</a>\\5", $s );
853 $s = str_replace( $unique, $protocol, $s );
855 $a = explode( "[{$protocol}:", " " . $s );
856 $s = array_shift( $a );
857 $s = substr( $s, 1 );
859 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
860 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
862 foreach ( $a as $line ) {
863 if ( preg_match( $e1, $line, $m ) ) {
864 $link = "{$protocol}:{$m[1]}";
865 $trail = $m[2];
866 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
867 else { $text = wfEscapeHTML( $link ); }
868 } else if ( preg_match( $e2, $line, $m ) ) {
869 $link = "{$protocol}:{$m[1]}";
870 $text = $m[2];
871 $trail = $m[3];
872 } else {
873 $s .= "[{$protocol}:" . $line;
874 continue;
876 if ( $printable == "yes") $paren = " (<i>" . htmlspecialchars ( $link ) . "</i>)";
877 else $paren = "";
878 $la = $sk->getExternalLinkAttributes( $link, $text );
879 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
882 return $s;
885 /* private */ function replaceInternalLinks( $s )
887 global $wgTitle, $wgUser, $wgLang;
888 global $wgLinkCache, $wgInterwikiMagic, $wgUseCategoryMagic;
889 global $wgNamespacesWithSubpages, $wgLanguageCode;
890 wfProfileIn( $fname = "OutputPage::replaceInternalLinks" );
892 wfProfileIn( "$fname-setup" );
893 $tc = Title::legalChars() . "#";
894 $sk = $wgUser->getSkin();
896 $a = explode( "[[", " " . $s );
897 $s = array_shift( $a );
898 $s = substr( $s, 1 );
900 $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD";
902 # Special and Media are pseudo-namespaces; no pages actually exist in them
903 $image = Namespace::getImage();
904 $special = Namespace::getSpecial();
905 $media = Namespace::getMedia();
906 $nottalk = !Namespace::isTalk( $wgTitle->getNamespace() );
907 wfProfileOut( "$fname-setup" );
909 foreach ( $a as $line ) {
910 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
911 $text = $m[2];
912 $trail = $m[3];
913 } else { # Invalid form; output directly
914 $s .= "[[" . $line ;
915 continue;
918 /* Valid link forms:
919 Foobar -- normal
920 :Foobar -- override special treatment of prefix (images, language links)
921 /Foobar -- convert to CurrentPage/Foobar
922 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
924 $c = substr($m[1],0,1);
925 $noforce = ($c != ":");
926 if( $c == "/" ) { # subpage
927 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
928 $m[1]=substr($m[1],1,strlen($m[1])-2);
929 $noslash=$m[1];
930 } else {
931 $noslash=substr($m[1],1);
933 if($wgNamespacesWithSubpages[$wgTitle->getNamespace()]) { # subpages allowed here
934 $link = $wgTitle->getPrefixedText(). "/" . trim($noslash);
935 if( "" == $text ) {
936 $text= $m[1];
937 } # this might be changed for ugliness reasons
938 } else {
939 $link = $noslash; # no subpage allowed, use standard link
941 } elseif( $noforce ) { # no subpage
942 $link = $m[1];
943 } else {
944 $link = substr( $m[1], 1 );
946 if( "" == $text )
947 $text = $link;
949 $nt = Title::newFromText( $link );
950 if( !$nt ) {
951 $s .= "[[" . $line;
952 continue;
954 $ns = $nt->getNamespace();
955 $iw = $nt->getInterWiki();
956 if( $noforce ) {
957 if( $iw && $wgInterwikiMagic && $nottalk && $wgLang->getLanguageName( $iw ) ) {
958 array_push( $this->mLanguageLinks, $nt->getPrefixedText() );
959 $s .= $trail;
960 /* CHECK MERGE @@@
961 } else if ( "media" == $pre ) {
962 $nt = Title::newFromText( $suf );
963 $name = $nt->getDBkey();
964 if ( "" == $text ) { $text = $nt->GetText(); }
966 $wgLinkCache->addImageLink( $name );
967 $s .= $sk->makeMediaLink( $name,
968 wfImageUrl( $name ), $text );
969 $s .= $trail;
970 } else if ( isset($wgUseCategoryMagic) && $wgUseCategoryMagic && $pre == wfMsg ( "category" ) ) {
971 $l = $sk->makeLink ( $pre.":".ucfirst( $m[2] ), ucfirst ( $m[2] ) ) ;
972 array_push ( $this->mCategoryLinks , $l ) ;
973 $s .= $trail ;
974 } else {
975 $l = $wgLang->getLanguageName( $pre );
976 if ( "" == $l or !$wgInterwikiMagic or Namespace::isTalk( $wgTitle->getNamespace() ) ) {
977 if ( "" == $text ) {
978 $text = $link;
980 $s .= $sk->makeLink( $link, $text, "", $trail );
981 } else if ( $pre != $wgLanguageCode ) {
982 array_push( $this->mLanguageLinks, "$pre:$suf" );
983 $s .= $trail;
986 continue;
988 if( $ns == $image ) {
989 $s .= $sk->makeImageLinkObj( $nt, $text ) . $trail;
990 $wgLinkCache->addImageLinkObj( $nt );
991 continue;
993 /* CHECK MERGE @@@
994 # } else if ( 0 == strcmp( "##", substr( $link, 0, 2 ) ) ) {
995 # $link = substr( $link, 2 );
996 # $s .= "<a name=\"{$link}\">{$text}</a>{$trail}";
997 } else {
998 if ( "" == $text ) { $text = $link; }
999 # Hotspot:
1000 $s .= $sk->makeLink( $link, $text, "", $trail );
1003 if( $ns == $media ) {
1004 $s .= $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1005 $wgLinkCache->addImageLinkObj( $nt );
1006 continue;
1007 } elseif( $ns == $special ) {
1008 $s .= $sk->makeKnownLinkObj( $nt, $text, "", $trail );
1009 continue;
1011 $s .= $sk->makeLinkObj( $nt, $text, "", $trail );
1013 wfProfileOut( $fname );
1014 return $s;
1017 # Some functions here used by doBlockLevels()
1019 /* private */ function closeParagraph()
1021 $result = "";
1022 if ( 0 != strcmp( "p", $this->mLastSection ) &&
1023 0 != strcmp( "", $this->mLastSection ) ) {
1024 $result = "</" . $this->mLastSection . ">";
1026 $this->mLastSection = "";
1027 return $result;
1029 # getCommon() returns the length of the longest common substring
1030 # of both arguments, starting at the beginning of both.
1032 /* private */ function getCommon( $st1, $st2 )
1034 $fl = strlen( $st1 );
1035 $shorter = strlen( $st2 );
1036 if ( $fl < $shorter ) { $shorter = $fl; }
1038 for ( $i = 0; $i < $shorter; ++$i ) {
1039 if ( $st1{$i} != $st2{$i} ) { break; }
1041 return $i;
1043 # These next three functions open, continue, and close the list
1044 # element appropriate to the prefix character passed into them.
1046 /* private */ function openList( $char )
1048 $result = $this->closeParagraph();
1050 if ( "*" == $char ) { $result .= "<ul><li>"; }
1051 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1052 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1053 else if ( ";" == $char ) {
1054 $result .= "<dl><dt>";
1055 $this->mDTopen = true;
1057 else { $result = "<!-- ERR 1 -->"; }
1059 return $result;
1062 /* private */ function nextItem( $char )
1064 if ( "*" == $char || "#" == $char ) { return "</li><li>"; }
1065 else if ( ":" == $char || ";" == $char ) {
1066 $close = "</dd>";
1067 if ( $this->mDTopen ) { $close = "</dt>"; }
1068 if ( ";" == $char ) {
1069 $this->mDTopen = true;
1070 return $close . "<dt>";
1071 } else {
1072 $this->mDTopen = false;
1073 return $close . "<dd>";
1076 return "<!-- ERR 2 -->";
1079 /* private */function closeList( $char )
1081 if ( "*" == $char ) { return "</li></ul>"; }
1082 else if ( "#" == $char ) { return "</li></ol>"; }
1083 else if ( ":" == $char ) {
1084 if ( $this->mDTopen ) {
1085 $this->mDTopen = false;
1086 return "</dt></dl>";
1087 } else {
1088 return "</dd></dl>";
1091 return "<!-- ERR 3 -->";
1094 /* private */ function doBlockLevels( $text, $linestart )
1096 $fname = "OutputPage::doBlockLevels";
1097 wfProfileIn( $fname );
1098 # Parsing through the text line by line. The main thing
1099 # happening here is handling of block-level elements p, pre,
1100 # and making lists from lines starting with * # : etc.
1102 $a = explode( "\n", $text );
1103 $text = $lastPref = "";
1104 $this->mDTopen = $inBlockElem = false;
1106 if ( ! $linestart ) { $text .= array_shift( $a ); }
1107 foreach ( $a as $t ) {
1108 if ( "" != $text ) { $text .= "\n"; }
1110 $oLine = $t;
1111 $opl = strlen( $lastPref );
1112 $npl = strspn( $t, "*#:;" );
1113 $pref = substr( $t, 0, $npl );
1114 $pref2 = str_replace( ";", ":", $pref );
1115 $t = substr( $t, $npl );
1117 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
1118 $text .= $this->nextItem( substr( $pref, -1 ) );
1120 if ( ";" == substr( $pref, -1 ) ) {
1121 $cpos = strpos( $t, ":" );
1122 if ( ! ( false === $cpos ) ) {
1123 $term = substr( $t, 0, $cpos );
1124 $text .= $term . $this->nextItem( ":" );
1125 $t = substr( $t, $cpos + 1 );
1128 } else if (0 != $npl || 0 != $opl) {
1129 $cpl = $this->getCommon( $pref, $lastPref );
1131 while ( $cpl < $opl ) {
1132 $text .= $this->closeList( $lastPref{$opl-1} );
1133 --$opl;
1135 if ( $npl <= $cpl && $cpl > 0 ) {
1136 $text .= $this->nextItem( $pref{$cpl-1} );
1138 while ( $npl > $cpl ) {
1139 $char = substr( $pref, $cpl, 1 );
1140 $text .= $this->openList( $char );
1142 if ( ";" == $char ) {
1143 $cpos = strpos( $t, ":" );
1144 if ( ! ( false === $cpos ) ) {
1145 $term = substr( $t, 0, $cpos );
1146 $text .= $term . $this->nextItem( ":" );
1147 $t = substr( $t, $cpos + 1 );
1150 ++$cpl;
1152 $lastPref = $pref2;
1154 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1155 if ( preg_match(
1156 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6)/i", $t ) ) {
1157 $text .= $this->closeParagraph();
1158 $inBlockElem = true;
1160 if ( ! $inBlockElem ) {
1161 if ( " " == $t{0} ) {
1162 $newSection = "pre";
1163 # $t = wfEscapeHTML( $t );
1165 else { $newSection = "p"; }
1167 if ( 0 == strcmp( "", trim( $oLine ) ) ) {
1168 $text .= $this->closeParagraph();
1169 $text .= "<" . $newSection . ">";
1170 } else if ( 0 != strcmp( $this->mLastSection,
1171 $newSection ) ) {
1172 $text .= $this->closeParagraph();
1173 if ( 0 != strcmp( "p", $newSection ) ) {
1174 $text .= "<" . $newSection . ">";
1177 $this->mLastSection = $newSection;
1179 if ( $inBlockElem &&
1180 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6)/i", $t ) ) {
1181 $inBlockElem = false;
1184 $text .= $t;
1186 while ( $npl ) {
1187 $text .= $this->closeList( $pref2{$npl-1} );
1188 --$npl;
1190 if ( "" != $this->mLastSection ) {
1191 if ( "p" != $this->mLastSection ) {
1192 $text .= "</" . $this->mLastSection . ">";
1194 $this->mLastSection = "";
1196 wfProfileOut( $fname );
1197 return $text;
1200 /* private */ function replaceVariables( $text )
1202 global $wgLang;
1203 $fname = "OutputPage::replaceVariables";
1204 wfProfileIn( $fname );
1207 # Basic variables
1208 # See Language.php for the definition of each magic word
1210 # As with sigs, this uses the server's local time -- ensure
1211 # this is appropriate for your audience!
1212 $v = date( "m" );
1213 $mw =& MagicWord::get( MAG_CURRENTMONTH );
1214 $text = $mw->replace( $v, $text );
1216 $v = $wgLang->getMonthName( date( "n" ) );
1217 $mw =& MagicWord::get( MAG_CURRENTMONTHNAME );
1218 $text = $mw->replace( $v, $text );
1220 $v = $wgLang->getMonthNameGen( date( "n" ) );
1221 $mw =& MagicWord::get( MAG_CURRENTMONTHNAMEGEN );
1222 $text = $mw->replace( $v, $text );
1224 $v = date( "j" );
1225 $mw = MagicWord::get( MAG_CURRENTDAY );
1226 $text = $mw->replace( $v, $text );
1228 $v = $wgLang->getWeekdayName( date( "w" )+1 );
1229 $mw =& MagicWord::get( MAG_CURRENTDAYNAME );
1230 $text = $mw->replace( $v, $text );
1232 $v = date( "Y" );
1233 $mw =& MagicWord::get( MAG_CURRENTYEAR );
1234 $text = $mw->replace( $v, $text );
1236 $v = $wgLang->time( wfTimestampNow(), false );
1237 $mw =& MagicWord::get( MAG_CURRENTTIME );
1238 $text = $mw->replace( $v, $text );
1240 $mw =& MagicWord::get( MAG_NUMBEROFARTICLES );
1241 if ( $mw->match( $text ) ) {
1242 $v = wfNumberOfArticles();
1243 $text = $mw->replace( $v, $text );
1246 # "Variables" with an additional parameter e.g. {{MSG:wikipedia}}
1247 # The callbacks are at the bottom of this file
1248 $mw =& MagicWord::get( MAG_MSG );
1249 $text = $mw->substituteCallback( $text, "wfReplaceMsgVar" );
1251 $mw =& MagicWord::get( MAG_MSGNW );
1252 $text = $mw->substituteCallback( $text, "wfReplaceMsgnwVar" );
1254 wfProfileOut( $fname );
1255 return $text;
1258 # Cleans up HTML, removes dangerous tags and attributes
1259 /* private */ function removeHTMLtags( $text )
1261 $fname = "OutputPage::removeHTMLtags";
1262 wfProfileIn( $fname );
1263 $htmlpairs = array( # Tags that must be closed
1264 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1265 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1266 "strike", "strong", "tt", "var", "div", "center",
1267 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1268 "ruby", "rt" , "rb" , "rp"
1270 $htmlsingle = array(
1271 "br", "p", "hr", "li", "dt", "dd"
1273 $htmlnest = array( # Tags that can be nested--??
1274 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1275 "dl", "font", "big", "small", "sub", "sup"
1277 $tabletags = array( # Can only appear inside table
1278 "td", "th", "tr"
1281 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1282 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1284 $htmlattrs = $this->getHTMLattrs () ;
1286 # Remove HTML comments
1287 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1289 $bits = explode( "<", $text );
1290 $text = array_shift( $bits );
1291 $tagstack = array(); $tablestack = array();
1293 foreach ( $bits as $x ) {
1294 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1295 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1296 $x, $regs );
1297 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1298 error_reporting( $prev );
1300 $badtag = 0 ;
1301 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1302 # Check our stack
1303 if ( $slash ) {
1304 # Closing a tag...
1305 if ( ! in_array( $t, $htmlsingle ) &&
1306 ( $ot = array_pop( $tagstack ) ) != $t ) {
1307 array_push( $tagstack, $ot );
1308 $badtag = 1;
1309 } else {
1310 if ( $t == "table" ) {
1311 $tagstack = array_pop( $tablestack );
1313 $newparams = "";
1315 } else {
1316 # Keep track for later
1317 if ( in_array( $t, $tabletags ) &&
1318 ! in_array( "table", $tagstack ) ) {
1319 $badtag = 1;
1320 } else if ( in_array( $t, $tagstack ) &&
1321 ! in_array ( $t , $htmlnest ) ) {
1322 $badtag = 1 ;
1323 } else if ( ! in_array( $t, $htmlsingle ) ) {
1324 if ( $t == "table" ) {
1325 array_push( $tablestack, $tagstack );
1326 $tagstack = array();
1328 array_push( $tagstack, $t );
1330 # Strip non-approved attributes from the tag
1331 $newparams = preg_replace(
1332 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
1333 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
1334 $params);
1336 if ( ! $badtag ) {
1337 $rest = str_replace( ">", "&gt;", $rest );
1338 $text .= "<$slash$t$newparams$brace$rest";
1339 continue;
1342 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1344 # Close off any remaining tags
1345 while ( $t = array_pop( $tagstack ) ) {
1346 $text .= "</$t>\n";
1347 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1349 wfProfileOut( $fname );
1350 return $text;
1356 * This function accomplishes several tasks:
1357 * 1) Auto-number headings if that option is enabled
1358 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1359 * 3) Add a Table of contents on the top for users who have enabled the option
1360 * 4) Auto-anchor headings
1362 * It loops through all headlines, collects the necessary data, then splits up the
1363 * string and re-inserts the newly formatted headlines.
1365 * */
1366 /* private */ function formatHeadings( $text )
1368 global $wgUser,$wgArticle,$wgTitle,$wpPreview;
1369 $nh=$wgUser->getOption( "numberheadings" );
1370 $st=$wgUser->getOption( "showtoc" );
1371 if(!$wgTitle->userCanEdit()) {
1372 $es=0;
1373 $esr=0;
1374 } else {
1375 $es=$wgUser->getID() && $wgUser->getOption( "editsection" );
1376 $esr=$wgUser->getID() && $wgUser->getOption( "editsectiononrightclick" );
1379 # Inhibit editsection links if requested in the page
1380 if ($es) {
1381 $esw=& MagicWord::get(MAG_NOEDITSECTION);
1382 if ($esw->matchAndRemove( $text )) {
1383 $es=0;
1386 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1387 # do not add TOC
1388 $mw =& MagicWord::get( MAG_NOTOC );
1389 if ($mw->matchAndRemove( $text ))
1391 $st = 0;
1394 # never add the TOC to the Main Page. This is an entry page that should not
1395 # be more than 1-2 screens large anyway
1396 if($wgTitle->getPrefixedText()==wfMsg("mainpage")) {$st=0;}
1398 # We need this to perform operations on the HTML
1399 $sk=$wgUser->getSkin();
1401 # Get all headlines for numbering them and adding funky stuff like [edit]
1402 # links
1403 preg_match_all("/<H([1-6])(.*?>)(.*?)<\/H[1-6]>/i",$text,$matches);
1405 # headline counter
1406 $c=0;
1408 # Ugh .. the TOC should have neat indentation levels which can be
1409 # passed to the skin functions. These are determined here
1410 foreach($matches[3] as $headline) {
1411 if($level) { $prevlevel=$level;}
1412 $level=$matches[1][$c];
1413 if(($nh||$st) && $prevlevel && $level>$prevlevel) {
1415 $h[$level]=0; // reset when we enter a new level
1416 $toc.=$sk->tocIndent($level-$prevlevel);
1417 $toclevel+=$level-$prevlevel;
1420 if(($nh||$st) && $level<$prevlevel) {
1421 $h[$level+1]=0; // reset when we step back a level
1422 $toc.=$sk->tocUnindent($prevlevel-$level);
1423 $toclevel-=$prevlevel-$level;
1426 $h[$level]++; // count number of headlines for each level
1428 if($nh||$st) {
1429 for($i=1;$i<=$level;$i++) {
1430 if($h[$i]) {
1431 if($dot) {$numbering.=".";}
1432 $numbering.=$h[$i];
1433 $dot=1;
1438 // The canonized header is a version of the header text safe to use for links
1440 $canonized_headline=preg_replace("/<.*?>/","",$headline); // strip out HTML
1441 $tocline = trim( $canonized_headline );
1442 $canonized_headline=str_replace('"',"",$canonized_headline);
1443 $canonized_headline=str_replace(" ","_",trim($canonized_headline));
1444 $refer[$c]=$canonized_headline;
1445 $refers[$canonized_headline]++; // count how many in assoc. array so we can track dupes in anchors
1446 $refcount[$c]=$refers[$canonized_headline];
1448 // Prepend the number to the heading text
1450 if($nh||$st) {
1451 $tocline=$numbering ." ". $tocline;
1453 // Don't number the heading if it is the only one (looks silly)
1454 if($nh && count($matches[3]) > 1) {
1455 $headline=$numbering . " " . $headline; // the two are different if the line contains a link
1459 // Create the anchor for linking from the TOC to the section
1461 $anchor=$canonized_headline;
1462 if($refcount[$c]>1) {$anchor.="_".$refcount[$c];}
1463 if($st) {
1464 $toc.=$sk->tocLine($anchor,$tocline,$toclevel);
1466 if($es && !isset($wpPreview)) {
1467 $head[$c].=$sk->editSectionLink($c+1);
1470 // Put it all together
1472 $head[$c].="<h".$level.$matches[2][$c]
1473 ."<a name=\"".$anchor."\">"
1474 .$headline
1475 ."</a>"
1476 ."</h".$level.">";
1478 // Add the edit section link
1480 if($esr && !isset($wpPreview)) {
1481 $head[$c]=$sk->editSectionScript($c+1,$head[$c]);
1484 $numbering="";
1485 $c++;
1486 $dot=0;
1489 if($st) {
1490 $toclines=$c;
1491 $toc.=$sk->tocUnindent($toclevel);
1492 $toc=$sk->tocTable($toc);
1495 // split up and insert constructed headlines
1497 $blocks=preg_split("/<H[1-6].*?>.*?<\/H[1-6]>/i",$text);
1498 $i=0;
1500 foreach($blocks as $block) {
1501 if(($es) && !isset($wpPreview) && $c>0 && $i==0) {
1502 # This is the [edit] link that appears for the top block of text when
1503 # section editing is enabled
1504 $full.=$sk->editSectionLink(0);
1506 $full.=$block;
1507 if($st && $toclines>3 && !$i) {
1508 # Let's add a top anchor just in case we want to link to the top of the page
1509 $full="<a name=\"top\"></a>".$full.$toc;
1512 $full.=$head[$i];
1513 $i++;
1516 return $full;
1519 /* private */ function magicISBN( $text )
1521 global $wgLang;
1523 $a = split( "ISBN ", " $text" );
1524 if ( count ( $a ) < 2 ) return $text;
1525 $text = substr( array_shift( $a ), 1);
1526 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1528 foreach ( $a as $x ) {
1529 $isbn = $blank = "" ;
1530 while ( " " == $x{0} ) {
1531 $blank .= " ";
1532 $x = substr( $x, 1 );
1534 while ( strstr( $valid, $x{0} ) != false ) {
1535 $isbn .= $x{0};
1536 $x = substr( $x, 1 );
1538 $num = str_replace( "-", "", $isbn );
1539 $num = str_replace( " ", "", $num );
1541 if ( "" == $num ) {
1542 $text .= "ISBN $blank$x";
1543 } else {
1544 $text .= "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
1545 "Booksources"), "isbn={$num}" ) . "\" class=\"internal\">ISBN $isbn</a>";
1546 $text .= $x;
1549 return $text;
1552 /* private */ function magicRFC( $text )
1554 return $text;
1557 /* private */ function headElement()
1559 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
1561 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
1563 if ( "" == $this->mHTMLtitle ) {
1564 $this->mHTMLtitle = $this->mPagetitle;
1566 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
1567 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
1568 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
1569 foreach ( $this->mMetatags as $tag ) {
1570 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
1571 $a = "http-equiv";
1572 $tag[0] = substr( $tag[0], 5 );
1573 } else {
1574 $a = "name";
1576 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
1578 $p = $this->mRobotpolicy;
1579 if ( "" == $p ) { $p = "index,follow"; }
1580 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
1582 if ( count( $this->mKeywords ) > 0 ) {
1583 $ret .= "<meta name=\"keywords\" content=\"" .
1584 implode( ",", $this->mKeywords ) . "\">\n";
1586 foreach ( $this->mLinktags as $tag ) {
1587 $ret .= "<link ";
1588 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
1589 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
1590 $ret .= "href=\"{$tag[2]}\">\n";
1592 $sk = $wgUser->getSkin();
1593 $ret .= $sk->getHeadScripts();
1594 $ret .= $sk->getUserStyles();
1596 $ret .= "</head>\n";
1597 return $ret;
1601 # Regex callbacks, used in OutputPage::replaceVariables
1603 # Just get rid of the dangerous stuff
1604 # Necessary because replaceVariables is called after removeHTMLtags,
1605 # and message text can come from any user
1606 function wfReplaceMsgVar( $matches ) {
1607 global $wgOut;
1608 $text = $wgOut->removeHTMLtags( wfMsg( $matches[1] ) );
1609 return $text;
1612 # Effective <nowiki></nowiki>
1613 # Not real <nowiki> because this is called after nowiki sections are processed
1614 function wfReplaceMsgnwVar( $matches ) {
1615 $text = wfEscapeWikiText( wfMsg( $matches[1] ) );
1616 return $text;