New date formatter
[lhc/web/wiklou.git] / includes / OutputPage.php
index 995187d..37379c8 100644 (file)
@@ -56,8 +56,8 @@ class OutputPage {
                $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
                        max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
                
-               if( $_SERVER["HTTP_IF_MODIFIED_SINCE"] != "" ) {
-                       # IE sends sizes after the date for compressed pages:
+               if( !empty( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
+                       # IE sends sizes after the date like this:
                        # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
                        # this breaks strtotime().
                        $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
@@ -72,6 +72,7 @@ class OutputPage {
                                header( "Cache-Control: private, must-revalidate, max-age=0" );
                                header( "Last-Modified: {$lastmod}" );                  
                                wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
+                               $this->reportTime(); # For profiling
                                exit;
                        } else {
                                wfDebug( "READY  client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
@@ -95,30 +96,10 @@ class OutputPage {
        function isPrintable() { return $this->mPrintable; }
 
        function getLanguageLinks() {
-               global $wgUseNewInterlanguage, $wgTitle, $wgLanguageCode;
-               global $wgDBconnection, $wgDBname, $wgDBintlname;
-
-               if ( ! $wgUseNewInterlanguage )
-                       return $this->mLanguageLinks; 
-               
-               mysql_select_db( $wgDBintlname, $wgDBconnection ) or die(
-                         htmlspecialchars(mysql_error()) );
-
-               $list = array();
-               $sql = "SELECT * FROM ilinks WHERE lang_from=\"" .
-                 "{$wgLanguageCode}\" AND title_from=\"" . $wgTitle->getDBkey() . "\"";
-               $res = mysql_query( $sql, $wgDBconnection );
-
-               while ( $q = mysql_fetch_object ( $res ) ) {
-                       $list[] = $q->lang_to . ":" . $q->title_to;
-               }
-               mysql_free_result( $res );
-               mysql_select_db( $wgDBname, $wgDBconnection ) or die(
-                 htmlspecialchars(mysql_error()) );
-
-               return $list;
+               global $wgTitle, $wgLanguageCode;
+               global $wgDBconnection, $wgDBname;
+               return $this->mLanguageLinks;
        }
-
        function supressQuickbar() { $this->mSupressQuickbar = true; }
        function isQuickbarSupressed() { return $this->mSupressQuickbar; }
 
@@ -279,22 +260,26 @@ class OutputPage {
 
        function setEncodings()
        {
-               global $HTTP_SERVER_VARS, $wgInputEncoding, $wgOutputEncoding;
+               global $wgInputEncoding, $wgOutputEncoding;
                global $wgUser, $wgLang;
 
                $wgInputEncoding = strtolower( $wgInputEncoding );
-               $s = $HTTP_SERVER_VARS['HTTP_ACCEPT_CHARSET'];
                
                if( $wgUser->getOption( 'altencoding' ) ) {
                        $wgLang->setAltEncoding();
                        return;
                }
 
-               if ( "" == $s ) {
+               if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
                        $wgOutputEncoding = strtolower( $wgOutputEncoding );
                        return;
                }
-               $a = explode( ",", $s );
+               
+               /*
+               # This code is unused anyway!
+               # Commenting out. --bv 2003-11-15
+               
+               $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
                $best = 0.0;
                $bestset = "*";
 
@@ -317,13 +302,14 @@ class OutputPage {
 
 # Disable for now
 #
+               */
                $wgOutputEncoding = $wgInputEncoding;
        }
 
        function reportTime()
        {
-               global $wgRequestTime, $wgDebugLogFile, $HTTP_SERVER_VARS;
-               global $wgProfiling, $wgProfileStack, $wgUser;
+               global $wgRequestTime, $wgDebugLogFile;
+               global $wgProfiling, $wgProfileStack, $wgProfileLimit, $wgUser;
 
                list( $usec, $sec ) = explode( " ", microtime() );
                $now = (float)$sec + (float)$usec;
@@ -334,20 +320,19 @@ class OutputPage {
 
                if ( "" != $wgDebugLogFile ) {
                        $prof = wfGetProfilingOutput( $start, $elapsed );
-               
-                       if( $forward = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'] )
-                               $forward = " forwarded for $forward";
-                       if( $client = $HTTP_SERVER_VARS['HTTP_CLIENT_IP'] )
-                               $forward .= " client IP $client";
-                       if( $from = $HTTP_SERVER_VARS['HTTP_FROM'] )
-                               $forward .= " from $from";
+                       if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
+                               $forward = " forwarded for " . $_SERVER['HTTP_X_FORWARDED_FOR'];
+                       if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
+                               $forward .= " client IP " . $_SERVER['HTTP_CLIENT_IP'];
+                       if( !empty( $_SERVER['HTTP_FROM'] ) )
+                               $forward .= " from " . $_SERVER['HTTP_FROM'];
                        if( $forward )
-                               $forward = "\t(proxied via {$HTTP_SERVER_VARS['REMOTE_ADDR']}{$forward})";
+                               $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
                        if($wgUser->getId() == 0)
                                $forward .= " anon";
                        $log = sprintf( "%s\t%04.3f\t%s\n",
                          gmdate( "YmdHis" ), $elapsed,
-                         urldecode( $HTTP_SERVER_VARS['REQUEST_URI'] . $forward ) );
+                         urldecode( $_SERVER['REQUEST_URI'] . $forward ) );
                        error_log( $log . $prof, 3, $wgDebugLogFile );
                }
                $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
@@ -388,8 +373,7 @@ class OutputPage {
 
                $sk = $wgUser->getSkin();
                $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );      
-               $text = str_replace( "$1", $ap, wfMsg( "sysoptext" ) );
-               $this->addHTML( $text );
+               $this->addHTML( wfMsg( "sysoptext", $ap ) );
                $this->returnToMain();
        }
 
@@ -405,8 +389,7 @@ class OutputPage {
 
                $sk = $wgUser->getSkin();
                $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );      
-               $text = str_replace( "$1", $ap, wfMsg( "developertext" ) );
-               $this->addHTML( $text );
+               $this->addHTML( wfMsg( "developertext", $ap ) );
                $this->returnToMain();
        }
 
@@ -421,7 +404,7 @@ class OutputPage {
                if ( $wgCommandLineMode ) {
                        $msg = wfMsgNoDB( "dberrortextcl" );
                } else {
-                       $msg = wfMsgNoDB( "dberrortextcl" );
+                       $msg = wfMsgNoDB( "dberrortext" );
                }
 
                $msg = str_replace( "$1", htmlspecialchars( wfLastDBquery() ), $msg );
@@ -443,25 +426,30 @@ class OutputPage {
                exit();
        }
 
-       function readOnlyPage( $source = "" )
+       function readOnlyPage( $source = "", $protected = false )
        {
                global $wgUser, $wgReadOnlyFile;
 
-               $this->setPageTitle( wfMsg( "readonly" ) );
                $this->setRobotpolicy( "noindex,nofollow" );
                $this->setArticleFlag( false );
 
-               $reason = implode( "", file( $wgReadOnlyFile ) );
-               $text = str_replace( "$1", $reason, wfMsg( "readonlytext" ) );
+               if( $protected ) {
+                       $this->setPageTitle( wfMsg( "viewsource" ) );
+                       $this->addWikiText( wfMsg( "protectedtext" ) );
+               } else {
+                       $this->setPageTitle( wfMsg( "readonly" ) );
+                       $reason = file_get_contents( $wgReadOnlyFile );
+                       $this->addHTML( wfMsg( "readonlytext", $reason ) );
+               }
                
                if($source) {
                        $rows = $wgUser->getOption( "rows" );
                        $cols = $wgUser->getOption( "cols" );
                        $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
                                htmlspecialchars( $source ) . "\n</textarea>";
+                       $this->addHTML( $text );
                }
                
-               $this->addHTML( $text );
                $this->returnToMain( false );
        }
 
@@ -478,35 +466,27 @@ class OutputPage {
 
        function unexpectedValueError( $name, $val )
        {
-               $msg = str_replace( "$1", $name, wfMsg( "unexpected" ) );
-               $msg = str_replace( "$2", $val, $msg );
-               $this->fatalError( $msg );
+               $this->fatalError( wfMsg( "unexpected", $name, $val ) );
        }
 
        function fileCopyError( $old, $new )
        {
-               $msg = str_replace( "$1", $old, wfMsg( "filecopyerror" ) );
-               $msg = str_replace( "$2", $new, $msg );
-               $this->fatalError( $msg );
+               $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
        }
 
        function fileRenameError( $old, $new )
        {
-               $msg = str_replace( "$1", $old, wfMsg( "filerenameerror" ) );
-               $msg = str_replace( "$2", $new, $msg );
-               $this->fatalError( $msg );
+               $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
        }
 
        function fileDeleteError( $name )
        {
-               $msg = str_replace( "$1", $name, wfMsg( "filedeleteerror" ) );
-               $this->fatalError( $msg );
+               $this->fatalError( wfMsg( "filedeleteerror", $name ) );
        }
 
        function fileNotFoundError( $name )
        {
-               $msg = str_replace( "$1", $name, wfMsg( "filenotfound" ) );
-               $this->fatalError( $msg );
+               $this->fatalError( wfMsg( "filenotfound", $name ) );
        }
 
        function returnToMain( $auto = true )
@@ -519,7 +499,7 @@ class OutputPage {
                }
                $link = $sk->makeKnownLink( $returnto, "" );
 
-               $r = str_replace( "$1", $link, wfMsg( "returnto" ) );
+               $r = wfMsg( "returnto", $link );
                if ( $auto ) {
                        $wgOut->addMeta( "http:Refresh", "10;url=" .
                          wfLocalUrlE( wfUrlencode( $returnto ) ) );
@@ -575,7 +555,7 @@ class OutputPage {
                if ( count ( $articles ) > 0 )
                {
                        asort ( $articles ) ;
-                       $h = str_replace ( "$1" , $ti[1] , wfMsg("category_header") ) ;
+                       $h =  wfMsg( "category_header", $ti[1] );
                        $r .= "<h2>{$h}</h2>\n" ;
                        $r .= implode ( ", " , $articles ) ;
                }
@@ -746,7 +726,7 @@ $t[] = "</table>" ;
 
                $sk = $wgUser->getSkin();
                $text = $sk->transformContent( $text );
-                $text .= $this->categoryMagic () ;
+               $text .= $this->categoryMagic () ;
 
                wfProfileOut( $fname );
                return $text;
@@ -920,61 +900,67 @@ $t[] = "</table>" ;
                $s = array_shift( $a );
                $s = substr( $s, 1 );
 
-               $e1 = "/^([{$tc}]+)\\|([^]]+)]](.*)\$/sD";
-               $e2 = "/^([{$tc}]+)]](.*)\$/sD";
+               $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD";
+
+               # Special and Media are pseudo-namespaces; no pages actually exist in them
+               $image = Namespace::getImage();
+               $special = Namespace::getSpecial();
+               $media = Namespace::getMedia();
+               $nottalk = !Namespace::isTalk( $wgTitle->getNamespace() );
                wfProfileOut( "$fname-setup" );
 
                foreach ( $a as $line ) {
-                       if ( preg_match( $e1, $line, $m ) ) { # page with alternate text
-                               
+                       if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
                                $text = $m[2];
                                $trail = $m[3];                         
-                       
-                       } else if ( preg_match( $e2, $line, $m ) ) { # page with normal text
-                       
-                               $text = "";
-                               $trail = $m[2];                 
-                       }
-                       
-                       else { # Invalid form; output directly
+                       } else { # Invalid form; output directly
                                $s .= "[[" . $line ;
                                continue;
                        }
-                       if(substr($m[1],0,1)=="/") { # subpage
+                       
+                       /* Valid link forms:
+                       Foobar -- normal
+                       :Foobar -- override special treatment of prefix (images, language links)
+                       /Foobar -- convert to CurrentPage/Foobar
+                       /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
+                       */
+                       $c = substr($m[1],0,1);
+                       $noforce = ($c != ":");
+                       if( $c == "/" ) { # subpage
                                if(substr($m[1],-1,1)=="/") {                 # / at end means we don't want the slash to be shown
                                        $m[1]=substr($m[1],1,strlen($m[1])-2); 
                                        $noslash=$m[1];
-                                       
                                } else {
                                        $noslash=substr($m[1],1);
                                }
                                if($wgNamespacesWithSubpages[$wgTitle->getNamespace()]) { # subpages allowed here
                                        $link = $wgTitle->getPrefixedText(). "/" . trim($noslash);
-                                       if(!$text) {                                            
+                                       if(!$text) {
                                                $text= $m[1]; 
                                        } # this might be changed for ugliness reasons
                                } else {
                                        $link = $noslash; # no subpage allowed, use standard link
                                }
-                       } else { # no subpage
-                               $link = $m[1]; 
+                       } elseif( $noforce ) { # no subpage
+                               $link = $m[1];
+                       } else {
+                               $link = substr( $m[1], 1 );
                        }
-                       
-                       if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z\\x80-\\xff]+):(.*)\$/", $link,  $m ) ) {
-                               $pre = strtolower( $m[1] );
-                               $suf = trim($m[2]);
-                               if( empty( $suf ) ) {
-                                       $s .= $trail;
-                               } else if ( $wgLang->getNsIndex( $pre ) ==
-                                 Namespace::getImage() ) {
-                                       $nt = Title::newFromText( $suf );
-                                       $name = $nt->getDBkey();
-                                       if ( "" == $text ) { $text = $nt->GetText(); }
+                       if( empty( $text ) )
+                               $text = $link;
 
-                                       $wgLinkCache->addImageLink( $name );
-                                       $s .= $sk->makeImageLink( $name,
-                                         wfImageUrl( $name ), $text );
+                       $nt = Title::newFromText( $link );
+                       if( !$nt ) {
+                               $s .= "[[" . $line;
+                               continue;
+                       }
+                       $ns = $nt->getNamespace();
+                       $iw = $nt->getInterWiki();
+                       if( $noforce ) {
+                               if( $iw && $wgInterwikiMagic && $nottalk && $wgLang->getLanguageName( $iw ) ) {
+                                       array_push( $this->mLanguageLinks, $nt->getPrefixedText() );
                                        $s .= $trail;
+/* CHECK MERGE @@@
                                } else if ( "media" == $pre ) {
                                        $nt = Title::newFromText( $suf );
                                        $name = $nt->getDBkey();
@@ -999,7 +985,15 @@ $t[] = "</table>" ;
                                                array_push( $this->mLanguageLinks, "$pre:$suf" );
                                                $s .= $trail;
                                        }
+*/
+                                       continue;
+                               }
+                               if( $ns == $image ) {
+                                       $s .= $sk->makeImageLinkObj( $nt, $text ) . $trail;
+                                       $wgLinkCache->addImageLinkObj( $nt );
+                                       continue;
                                }
+/* CHECK MERGE @@@
 #                      } else if ( 0 == strcmp( "##", substr( $link, 0, 2 ) ) ) {
 #                              $link = substr( $link, 2 );
 #                              $s .= "<a name=\"{$link}\">{$text}</a>{$trail}";
@@ -1007,7 +1001,17 @@ $t[] = "</table>" ;
                                if ( "" == $text ) { $text = $link; }
                                # Hotspot: 
                                $s .= $sk->makeLink( $link, $text, "", $trail );
+*/
+                       }
+                       if( $ns == $media ) {
+                               $s .= $sk->makeMediaLinkObj( $nt, $text ) . $trail;
+                               $wgLinkCache->addImageLinkObj( $nt );
+                               continue;
+                       } elseif( $ns == $special ) {
+                               $s .= $sk->makeKnownLinkObj( $nt, $text, "", $trail );
+                               continue;
                        }
+                       $s .= $sk->makeLinkObj( $nt, $text, "", $trail );
                }
                wfProfileOut( $fname );
                return $s;
@@ -1434,6 +1438,7 @@ $t[] = "</table>" ;
                                }
                        }
 
+                       // The canonized header is a version of the header text safe to use for links
                        
                        $canonized_headline=preg_replace("/<.*?>/","",$headline); // strip out HTML
                        $tocline=$canonized_headline;
@@ -1442,12 +1447,20 @@ $t[] = "</table>" ;
                        $refer[$c]=$canonized_headline;
                        $refers[$canonized_headline]++;  // count how many in assoc. array so we can track dupes in anchors
                        $refcount[$c]=$refers[$canonized_headline];
+
+            // Prepend the number to the heading text
+                       
                        if($nh||$st) {
                                $tocline=$numbering ." ". $tocline;
-                               if($nh) {
+                               
+                               // Don't number the heading if it is the only one (looks silly)
+                               if($nh && count($matches[3]) > 1) {
                                        $headline=$numbering . " " . $headline; // the two are different if the line contains a link
-                               }                               
+                               }
                        }
+                       
+                       // Create the anchor for linking from the TOC to the section
+                       
                        $anchor=$canonized_headline;
                        if($refcount[$c]>1) {$anchor.="_".$refcount[$c];}
                        if($st) {
@@ -1456,14 +1469,21 @@ $t[] = "</table>" ;
                        if($es && !isset($wpPreview)) {
                                $head[$c].=$sk->editSectionLink($c+1);
                        }
-                       $head[$c].="<H".$level.$matches[2][$c]
+                       
+                       // Put it all together
+                       
+                       $head[$c].="<h".$level.$matches[2][$c]
                         ."<a name=\"".$anchor."\">"
                         .$headline
                         ."</a>"
-                        ."</H".$level.">";
+                        ."</h".$level.">";
+                       
+                       // Add the edit section link
+                       
                        if($esr && !isset($wpPreview)) {
                                $head[$c]=$sk->editSectionScript($c+1,$head[$c]);       
                        }
+                       
                        $numbering="";
                        $c++;
                        $dot=0;
@@ -1480,12 +1500,11 @@ $t[] = "</table>" ;
                $blocks=preg_split("/<H[1-6].*?>.*?<\/H[1-6]>/i",$text);
                $i=0;
 
-
                foreach($blocks as $block) {
                        if(($es) && !isset($wpPreview) && $c>0 && $i==0) {
                            # This is the [edit] link that appears for the top block of text when 
                                # section editing is enabled
-                               $full.=$sk->editSectionLink(0);                         
+                               $full.=$sk->editSectionLink(0);
                        }
                        $full.=$block;
                        if($st && $toclines>3 && !$i) {
@@ -1496,6 +1515,7 @@ $t[] = "</table>" ;
                        $full.=$head[$i];
                        $i++;
                }
+               
                return $full;
        }
 
@@ -1506,30 +1526,30 @@ $t[] = "</table>" ;
                $a = split( "ISBN ", " $text" );
                if ( count ( $a ) < 2 ) return $text;
                $text = substr( array_shift( $a ), 1);
-        $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+               $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
                foreach ( $a as $x ) {
                        $isbn = $blank = "" ;
                        while ( " " == $x{0} ) {
-                $blank .= " ";
-                $x = substr( $x, 1 );
+                               $blank .= " ";
+                               $x = substr( $x, 1 );
                        }
-            while ( strstr( $valid, $x{0} ) != false ) {
+                       while ( strstr( $valid, $x{0} ) != false ) {
                                $isbn .= $x{0};
                                $x = substr( $x, 1 );
                        }
-            $num = str_replace( "-", "", $isbn );
-            $num = str_replace( " ", "", $num );
+                       $num = str_replace( "-", "", $isbn );
+                       $num = str_replace( " ", "", $num );
 
-            if ( "" == $num ) {
+                       if ( "" == $num ) {
                                $text .= "ISBN $blank$x";
-            } else {
+                       } else {
                                $text .= "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
-                                 "Booksources"), "isbn={$num}" ) . "\" CLASS=\"internal\">ISBN $isbn</a>";
+                                 "Booksources"), "isbn={$num}" ) . "\" class=\"internal\">ISBN $isbn</a>";
                                $text .= $x;
                        }
                }
-        return $text;
+               return $text;
        }
 
        /* private */ function magicRFC( $text )