use Database::timestamp() for cutoff
[lhc/web/wiklou.git] / includes / SpecialMaintenance.php
index 37b5875..e294dea 100644 (file)
@@ -1,4 +1,4 @@
-<?
+<?php
 
 function sns()
 {
@@ -9,35 +9,38 @@ function sns()
 
 function wfSpecialMaintenance( $par=NULL )
 {
-       global $wgUser, $wgOut, $wgLang, $wgTitle, $subfunction, $wgLanguageCode, $submitmll;
+       global $wgUser, $wgOut, $wgLang, $wgTitle, $wgRequest, $wgLanguageCode;
        global $wgMiserMode;
+
        if ( $wgMiserMode ) {
                $wgOut->addWikiText( wfMsg( "perfdisabled" ) );
                return;
        }
        
+       $submitmll = $wgRequest->getVal( 'submitmll' );
+
        if( $par )
                $subfunction = $par;
        else
-               $subfunction = $_REQUEST['subfunction'];
+               $subfunction = $wgRequest->getText( 'subfunction' );
 
        if ( $subfunction == "disambiguations" ) return wfSpecialDisambiguations() ;
        if ( $subfunction == "doubleredirects" ) return wfSpecialDoubleRedirects() ;
        if ( $subfunction == "brokenredirects" ) return wfSpecialBrokenRedirects() ;
        if ( $subfunction == "selflinks" ) return wfSpecialSelfLinks() ;
-        if ( $subfunction == "mispeelings" ) return wfSpecialMispeelings() ;
+       if ( $subfunction == "mispeelings" ) return wfSpecialMispeelings() ;
        if ( $subfunction == "missinglanguagelinks" ) return wfSpecialMissingLanguageLinks() ;
-       if ( isset ( $submitmll ) ) return wfSpecialMissingLanguageLinks() ;
+       if ( !is_null( $submitmll ) ) return wfSpecialMissingLanguageLinks() ;
 
        $sk = $wgUser->getSkin();
        $ns = $wgLang->getNamespaces() ;
        $r = wfMsg("maintnancepagetext") ;
        $r .= "<UL>\n" ;
-       $r .= "<li>".getMPL("disambiguations")."</li>\n" ;
+       #$r .= "<li>".getMPL("disambiguations")."</li>\n" ; # Doesn't work
        $r .= "<li>".getMPL("doubleredirects")."</li>\n" ;
        $r .= "<li>".getMPL("brokenredirects")."</li>\n" ;
-       $r .= "<li>".getMPL("selflinks")."</li>\n" ;
-        $r .= "<li>".getMPL("mispeelings")."</li>\n" ;
+       #$r .= "<li>".getMPL("selflinks")."</li>\n" ; # Doesn't work
+       $r .= "<li>".getMPL("mispeelings")."</li>\n" ;
 
        $r .= "<li>";
        $l = getMPL("missinglanguagelinks");
@@ -47,7 +50,7 @@ function wfSpecialMaintenance( $par=NULL )
        $l = $l[0] ;
        $r .= $l.">\n" ;
        $r .= "<input type=submit name='submitmll' value='" ;
-       $r .= wfMsg("missinglanguagelinksbutton");
+       $r .= htmlspecialchars(wfMsg("missinglanguagelinksbutton"), ENT_QUOTES);
        $r .= "'>\n" ;
        $r .= "<select name=thelang>\n" ;
        $a = $wgLang->getLanguageNames();
@@ -55,7 +58,7 @@ function wfSpecialMaintenance( $par=NULL )
        foreach ( $ak AS $k ) {
                if ( $k != $wgLanguageCode )
                        $r .= "<option value='{$k}'>{$a[$k]}</option>\n" ;
-               }
+       }
        $r .= "</select>\n" ;
        $r .= "</FORM>\n</li>" ;
 
@@ -70,9 +73,9 @@ function getMPL ( $x )
        return $sk->makeKnownLink(sns().":Maintenance",wfMsg($x),"subfunction={$x}") ;
 }
 
-function getMaintenancePageBacklink()
+function getMaintenancePageBacklink( $subfunction )
 {
-       global $wgUser , $wgLang , $subfunction ;
+       global $wgUser , $wgLang;
        $sk = $wgUser->getSkin() ;
        $ns = $wgLang->getNamespaces() ;
        $r = $sk->makeKnownLink (
@@ -86,20 +89,25 @@ function getMaintenancePageBacklink()
        return $s ;
 }
 
-
+# Broken function
+# Suggest deprecating this in favour of a Special:Whatlinkshere with prev/next links [TS]
 function wfSpecialDisambiguations()
 {
        global $wgUser, $wgOut, $wgLang, $wgTitle;
        $fname = "wfSpecialDisambiguations";
 
        list( $limit, $offset ) = wfCheckLimits();
+       $dbr =& wfGetDB( DB_SLAVE );
+       extract( $dbr->tableNames( 'links', 'cur' ) );
 
-       $dp = wfStrencode( wfMsg("disambiguationspage") );
-
+       $dp = $dbr->strencode( wfMsg("disambiguationspage") );
+       
+       die( "wfSpecialDisambiguation is broken. Link tables have changed...\n" );
+       
        $sql = "SELECT la.l_from,la.l_to,"
                . " lb.l_from AS source,lb.l_to AS dest,"
                . " c.cur_id, c.cur_title AS dt"
-               . " FROM links AS la, links AS lb, cur AS c, cur AS d"
+               . " FROM $links AS la, $links AS lb, $cur AS c, $cur AS d"
                . " WHERE la.l_from='{$dp}'"
                . " AND la.l_to=lb.l_to"
                . " AND la.l_from<>lb.l_from"
@@ -109,13 +117,12 @@ function wfSpecialDisambiguations()
                . " AND d.cur_namespace=0"
                . " LIMIT {$offset}, {$limit}";
 
-       $res = wfQuery( $sql, DB_READ, $fname );
+       $res = $dbr->query( $sql, $fname );
 
        $sk = $wgUser->getSkin();
 
-       $top = "<p>".wfMsg("disambiguationstext")."</p><br>\n";
-       $top = str_replace ( "$1" , $sk->makeKnownLink ( $dp ) , $top ) ;
-       $top = getMaintenancePageBacklink().$top ;
+       $top = "<p>".wfMsg( "disambiguationstext", $sk->makeKnownLink( $dp ) )."</p><br>\n";
+       $top = getMaintenancePageBacklink( "disambiguations" ) . $top;
        $top .= wfShowingResults( $offset, $limit );
        $wgOut->addHTML( "<p>{$top}\n" );
 
@@ -124,13 +131,13 @@ function wfSpecialDisambiguations()
        $wgOut->addHTML( "<br>{$sl}\n" );
 
        $s = "<ol start=" . ( $offset + 1 ) . ">";
-       while ( $obj = wfFetchObject( $res ) ) {
+       while ( $obj = $dbr->fetchObject( $res ) ) {
                $l1 = $sk->makeKnownLink ( $obj->source , "" , "redirect=no" ) ;
                $l2 = $sk->makeKnownLink ( $obj->dt ) ;
                $l3 = $sk->makeBrokenLink ( $obj->source , "(".wfMsg("qbedit").")" , "redirect=no" ) ;
                $s .= "<li>{$l1} {$l3} => {$l2}</li>\n" ;
        }
-       wfFreeResult( $res );
+       $dbr->freeResult( $res );
        $s .= "</ol>";
        $wgOut->addHTML( $s );
        $wgOut->addHTML( "<p>{$sl}\n" );
@@ -142,12 +149,19 @@ function wfSpecialDoubleRedirects()
        $fname = "wfSpecialDoubleRedirects";
 
        list( $limit, $offset ) = wfCheckLimits();
+       $dbr =& wfGetDB( DB_SLAVE );
+       extract( $dbr->tableNames( 'cur', 'links' ) );
 
-       $sql = "SELECT l_from,l_to,cb.cur_text AS rt,cb.cur_title AS ti FROM links,cur AS ca, cur AS cb WHERE ca.cur_is_redirect=1 AND cb.cur_is_redirect=1 AND l_to=cb.cur_id AND l_from=ca.cur_title AND ca.cur_namespace=0 LIMIT {$offset}, {$limit}" ;
+       $sql = "SELECT ca.cur_namespace as ns_a, ca.cur_title as title_a," . 
+              "  cb.cur_namespace as ns_b, cb.cur_title as title_b," .
+                  "  cb.cur_text AS rt " . 
+              "FROM $links,$cur AS ca,$cur AS cb ". 
+              "WHERE ca.cur_is_redirect=1 AND cb.cur_is_redirect=1 AND l_to=cb.cur_id " .
+              "  AND l_from=ca.cur_id LIMIT {$offset}, {$limit}" ;
 
-       $res = wfQuery( $sql, DB_READ, $fname );
+       $res = $dbr->query( $sql, $fname );
 
-       $top = getMaintenancePageBacklink();
+       $top = getMaintenancePageBacklink( "doubleredirects" );
        $top .= "<p>".wfMsg("doubleredirectstext")."</p><br>\n";
        $top .= wfShowingResults( $offset, $limit );
        $wgOut->addHTML( "<p>{$top}\n" );
@@ -158,15 +172,18 @@ function wfSpecialDoubleRedirects()
 
        $sk = $wgUser->getSkin();
        $s = "<ol start=" . ( $offset + 1 ) . ">";
-       while ( $obj = wfFetchObject( $res ) ) {
+       while ( $obj = $dbr->fetchObject( $res ) ) {
                $n = explode ( "\n" , $obj->rt ) ;
                $n = $n[0] ;
-               $l1 = $sk->makeKnownLink ( $obj->l_from , "" , "redirect=no" ) ;
-               $l2 = $sk->makeKnownLink ( $obj->ti , "" , "redirect=no" ) ;
-               $l3 = $sk->makeBrokenLink ( $obj->l_from , "(".wfMsg("qbedit").")" , "redirect=no" ) ;
+               $sourceTitle = Title::makeTitle( $obj->ns_a, $obj->title_a );
+               $destTitle = Title::makeTitle( $obj->ns_b, $obj->title_b );
+
+               $l1 = $sk->makeKnownLinkObj( $sourceTitle , "" , "redirect=no" ) ; 
+               $l2 = $sk->makeKnownLinkObj( $destTitle , "" , "redirect=no" ) ;
+               $l3 = $sk->makeBrokenLinkObj( $sourceTitle , "(".wfMsg("qbedit").")" , "redirect=no" ) ;
                $s .= "<li>{$l1} {$l3} => {$l2} (\"{$n}\")</li>\n" ;
        }
-       wfFreeResult( $res );
+       $dbr->freeResult( $res );
        $s .= "</ol>";
        $wgOut->addHTML( $s );
        $wgOut->addHTML( "<p>{$sl}\n" );
@@ -178,12 +195,17 @@ function wfSpecialBrokenRedirects()
        $fname = "wfSpecialBrokenRedirects";
 
        list( $limit, $offset ) = wfCheckLimits();
+       $dbr =& wfGetDB( DB_SLAVE );
+       extract( $dbr->tableNames( 'cur', 'brokenlinks' ) );
 
-       $sql = "SELECT bl_to,cur_title FROM brokenlinks,cur WHERE cur_is_redirect=1 AND cur_namespace=0 AND bl_from=cur_id LIMIT {$offset}, {$limit}" ;
 
-       $res = wfQuery( $sql, DB_READ, $fname );
+       $sql = "SELECT bl_to,cur_title FROM $brokenlinks,$cur " .
+         "WHERE cur_is_redirect=1 AND cur_namespace=0 AND bl_from=cur_id " . 
+         "LIMIT {$offset}, {$limit}" ;
 
-       $top = getMaintenancePageBacklink();
+       $res = $dbr->query( $sql, $fname );
+
+       $top = getMaintenancePageBacklink( "brokenredirects" );
        $top .= "<p>".wfMsg("brokenredirectstext")."</p><br>\n";
        $top .= wfShowingResults( $offset, $limit );
        $wgOut->addHTML( "<p>{$top}\n" );
@@ -194,18 +216,20 @@ function wfSpecialBrokenRedirects()
 
        $sk = $wgUser->getSkin();
        $s = "<ol start=" . ( $offset + 1 ) . ">";
-       while ( $obj = wfFetchObject( $res ) ) {
+       while ( $obj = $dbr->fetchObject( $res ) ) {
                $l1 = $sk->makeKnownLink ( $obj->cur_title , "" , "redirect=no" ) ;
                $l2 = $sk->makeBrokenLink ( $obj->cur_title , "(".wfMsg("qbedit").")" , "redirect=no" ) ;
                $l3 = $sk->makeBrokenLink ( $obj->bl_to , "" , "redirect=no" ) ;
                $s .= "<li>{$l1} {$l2} => {$l3}</li>\n" ;
        }
-       wfFreeResult( $res );
+       $dbr->freeResult( $res );
        $s .= "</ol>";
        $wgOut->addHTML( $s );
        $wgOut->addHTML( "<p>{$sl}\n" );
 }
 
+# This doesn't really work anymore, because self-links are now displayed as
+# unlinked bold text, and are not entered into the link table.
 function wfSpecialSelfLinks()
 {
        global $wgUser, $wgOut, $wgLang, $wgTitle;
@@ -213,11 +237,13 @@ function wfSpecialSelfLinks()
 
        list( $limit, $offset ) = wfCheckLimits();
 
-       $sql = "SELECT cur_title FROM cur,links WHERE cur_is_redirect=0 AND cur_namespace=0 AND l_from=cur_title AND l_to=cur_id LIMIT {$offset}, {$limit}";
+       $sql = "SELECT cur_namespace,cur_title FROM cur,links " . 
+         "WHERE l_from=l_to AND l_to=cur_id " . 
+         "LIMIT {$offset}, {$limit}";
 
-       $res = wfQuery( $sql, DB_READ, $fname );
+       $res = wfQuery( $sql, DB_SLAVE, $fname );
 
-       $top = getMaintenancePageBacklink();
+       $top = getMaintenancePageBacklink( "selflinks" );
        $top .= "<p>".wfMsg("selflinkstext")."</p><br>\n";
        $top .= wfShowingResults( $offset, $limit );
        $wgOut->addHTML( "<p>{$top}\n" );
@@ -228,8 +254,10 @@ function wfSpecialSelfLinks()
 
        $sk = $wgUser->getSkin();
        $s = "<ol start=" . ( $offset + 1 ) . ">";
-       while ( $obj = wfFetchObject( $res ) )
-               $s .= "<li>".$sk->makeKnownLink ( $obj->cur_title )."</li>\n" ;
+       while ( $obj = wfFetchObject( $res ) ) {
+               $title = Title::makeTitle( $obj->cur_namespace, $obj->cur_title );
+               $s .= "<li>".$sk->makeKnownLinkObj( $title )."</li>\n" ;
+       }
        wfFreeResult( $res );
        $s .= "</ol>";
        $wgOut->addHTML( $s );
@@ -238,90 +266,95 @@ function wfSpecialSelfLinks()
 
 function wfSpecialMispeelings ()
 {
-        global $wgUser, $wgOut, $wgLang, $wgTitle;
-        $sk = $wgUser->getSkin();
-        $fname = "wfSpecialMispeelings";
-
-               list( $limit, $offset ) = wfCheckLimits();
-
-        # Determine page name
-        $ms = wfMsg ( "mispeelingspage" ) ;
-        $mss = wfStrencode( str_replace ( " " , "_" , $ms ) );
-        $msp = $wgLang->getNsText(4).":".$ms ;
-        $msl = $sk->makeKnownLink ( $msp ) ;
-
-        # Load list from database
-        $sql = "SELECT cur_text FROM cur WHERE cur_title='{$mss}' AND cur_namespace=4" ;
-        $res = wfQuery( $sql, DB_READ, $fname );
-        $obj = wfFetchObject ( $res ) ;
-        $l = $obj->cur_text ;
-        $l = explode ( "\n" , $l ) ;
-        $a = array () ;
-        foreach ( $l as $x )
-                if ( substr ( trim ( $x ) , 0 , 1 ) == "*" )
-                        $a[] = strtolower ( trim ( substr ( trim ( $x ) , 1 ) ) );
-        asort ( $a ) ;
-
-        $cnt = 0 ;
-        $b = array () ;
-        foreach ( $a AS $x ) {
-                if ( $cnt < $offset+$limit && $x != "" ) {
-                        $y = $x ;
-                        $x = preg_replace( '/^(\S+).*$/', '$1', $x );
-                       #$sql = "SELECT DISTINCT cur_title FROM cur WHERE cur_namespace=0 AND cur_is_redirect=0 AND (MATCH(cur_ind_text) AGAINST ('" . wfStrencode( $wgLang->stripForSearch( $x ) ) . "'))" ;
-                       $sql = "SELECT DISTINCT cur_title FROM cur,searchindex WHERE cur_id=si_page AND cur_namespace=0 AND cur_is_redirect=0 AND (MATCH(si_text) AGAINST ('" . wfStrencode( $wgLang->stripForSearch( $x ) ) . "'))" ;
-                        $res = wfQuery( $sql, DB_READ, $fname );
-                        while ( $obj = wfFetchObject ( $res ) ) {
-                                if ( $cnt >= $offset AND $cnt < $offset+$limit ) {
-                                        if ( $y != "" ) {
-                                                if ( count ( $b ) > 0 ) $b[] = "</OL>\n" ;
-                                                $b[] = "<H3>{$y}</H3>\n<OL start=".($cnt+1).">\n" ;
-                                                $y = "" ;
-                                                }
-                                        $b[] = "<li>".
-                                                $sk->makeKnownLink ( $obj->cur_title ).
-                                                " (".
-                                                $sk->makeBrokenLink ( $obj->cur_title , wfMsg ( "qbedit" ) ).
-                                                ")</li>\n" ;
-                                        }
-                                $cnt++ ;
-                                }
-                        }
-                }
-        $top = getMaintenancePageBacklink();
-        $top .= "<p>".str_replace("$1",$msl,wfMsg("mispeelingstext"))."</p><br>\n";
-        $top .= wfShowingResults( $offset, $limit );
-        $wgOut->addHTML( "<p>{$top}\n" );
-
-        $sl = wfViewPrevNext( $offset, $limit, "REPLACETHIS" ) ;
-        $sl = str_replace ( "REPLACETHIS" , sns().":Maintenance&subfunction=mispeelings" , $sl ) ;
-        $wgOut->addHTML( "<br>{$sl}\n" );
-
-        $s = implode ( "" , $b ) ;
-        if ( count ( $b ) > 0 ) $s .= "</ol>";
-        $wgOut->addHTML( $s );
-        $wgOut->addHTML( "<p>{$sl}\n" );
+       global $wgUser, $wgOut, $wgLang, $wgTitle;
+       $sk = $wgUser->getSkin();
+       $fname = "wfSpecialMispeelings";
+
+       list( $limit, $offset ) = wfCheckLimits();
+       $dbr =& wfGetDB( DB_SLAVE );
+       extract( $dbr->tableNames( 'cur', 'searchindex' ) );
+
+       # Determine page name
+       $ms = wfMsg ( "mispeelingspage" ) ;
+       $mss = str_replace ( " " , "_" , $ms );
+       $msp = $wgLang->getNsText(4).":".$ms ;
+       $msl = $sk->makeKnownLink ( $msp ) ;
+
+       # Load list from database
+       $l = $dbr->selectField( 'cur', 'cur_text', array( 'cur_title' => $mss, 'cur_namespace' => 4 ), $fname );
+       $l = explode ( "\n" , $l ) ;
+       $a = array () ;
+       foreach ( $l as $x )
+               if ( substr ( trim ( $x ) , 0 , 1 ) == "*" )
+                       $a[] = strtolower ( trim ( substr ( trim ( $x ) , 1 ) ) );
+       asort ( $a ) ;
+
+       $cnt = 0 ;
+       $b = array () ;
+       foreach ( $a AS $x ) {
+               if ( $cnt < $offset+$limit && $x != "" ) {
+                       $y = $x ;
+                       $x = preg_replace( '/^(\S+).*$/', '$1', $x );
+                       $sql = "SELECT DISTINCT cur_title FROM $cur,$searchindex WHERE cur_id=si_page AND ".
+                               "cur_namespace=0 AND cur_is_redirect=0 AND " .
+                               "(MATCH(si_text) AGAINST ('" . $dbr->strencode( $wgLang->stripForSearch( $x ) ) . "'))" ;
+                       $res = $dbr->query( $sql, $fname );
+                       while ( $obj = $dbr->fetchObject ( $res ) ) {
+                               if ( $cnt >= $offset AND $cnt < $offset+$limit ) {
+                                       if ( $y != "" ) {
+                                               if ( count ( $b ) > 0 ) $b[] = "</OL>\n" ;
+                                               $b[] = "<H3>{$y}</H3>\n<OL start=".($cnt+1).">\n" ;
+                                               $y = "" ;
+                                       }
+                                       $b[] = "<li>".
+                                               $sk->makeKnownLink ( $obj->cur_title ).
+                                               " (".
+                                               $sk->makeBrokenLink ( $obj->cur_title , wfMsg ( "qbedit" ) ).
+                                               ")</li>\n" ;
+                               }
+                               $cnt++ ;
+                       }
+               }
+       }
+       $top = getMaintenancePageBacklink( "mispeelings" );
+       $top .= "<p>".wfMsg( "mispeelingstext", $msl )."</p><br>\n";
+       $top .= wfShowingResults( $offset, $limit );
+       $wgOut->addHTML( "<p>{$top}\n" );
+
+       $sl = wfViewPrevNext( $offset, $limit, "REPLACETHIS" ) ;
+       $sl = str_replace ( "REPLACETHIS" , sns().":Maintenance&subfunction=mispeelings" , $sl ) ;
+       $wgOut->addHTML( "<br>{$sl}\n" );
+
+       $s = implode ( "" , $b ) ;
+       if ( count ( $b ) > 0 ) $s .= "</ol>";
+       $wgOut->addHTML( $s );
+       $wgOut->addHTML( "<p>{$sl}\n" );
 }
 
 
 function wfSpecialMissingLanguageLinks()
 {
-       global $wgUser, $wgOut, $wgLang, $wgTitle, $thelang, $subfunction;
+       global $wgUser, $wgOut, $wgLang, $wgTitle, $wgRequest;
+       
        $fname = "wfSpecialMissingLanguageLinks";
-       $subfunction = "missinglanguagelinks" ;
+       $thelang = $wgRequest->getText( 'thelang' );
        if ( $thelang == "w" ) $thelang = "en" ; # Fix for international wikis
 
        list( $limit, $offset ) = wfCheckLimits();
+       $dbr =& wfGetDB( DB_SLAVE );
+       $cur = $dbr->tableName( 'cur' );
 
-       $sql = "SELECT cur_title FROM cur WHERE cur_namespace=0 AND cur_is_redirect=0 AND cur_title NOT LIKE '%/%' AND cur_text NOT LIKE '%[[{$thelang}:%' LIMIT {$offset}, {$limit}";
+       $sql = "SELECT cur_title FROM $cur " .
+         "WHERE cur_namespace=0 AND cur_is_redirect=0 " .
+         "AND cur_title NOT LIKE '%/%' AND cur_text NOT LIKE '%[[{$thelang}:%' " .
+         "LIMIT {$offset}, {$limit}";
 
-       $res = wfQuery( $sql, DB_READ, $fname );
+       $res = $dbr->query( $sql, $fname );
 
 
-       $mll = wfMsg("missinglanguagelinkstext");
-       $mll = str_replace ( "$1" , $wgLang->getLanguageName($thelang) , $mll ) ;
+       $mll = wfMsg( "missinglanguagelinkstext", $wgLang->getLanguageName($thelang) );
 
-       $top = getMaintenancePageBacklink();
+       $top = getMaintenancePageBacklink( "missinglanguagelinks" );
        $top .= "<p>$mll</p><br>";
        $top .= wfShowingResults( $offset, $limit );
        $wgOut->addHTML( "<p>{$top}\n" );
@@ -332,9 +365,9 @@ function wfSpecialMissingLanguageLinks()
 
        $sk = $wgUser->getSkin();
        $s = "<ol start=" . ( $offset + 1 ) . ">";
-       while ( $obj = wfFetchObject( $res ) )
+       while ( $obj = $dbr->fetchObject( $res ) )
                $s .= "<li>".$sk->makeKnownLink ( $obj->cur_title )."</li>\n" ;
-       wfFreeResult( $res );
+       $dbr->freeResult( $res );
        $s .= "</ol>";
        $wgOut->addHTML( $s );
        $wgOut->addHTML( "<p>{$sl}\n" );