Followup r91672: remove commented-out reference to removed file (rtl.css)
[lhc/web/wiklou.git] / includes / QueryPage.php
index 7dfe3fd..da8faf5 100644 (file)
@@ -85,10 +85,12 @@ abstract class QueryPage extends SpecialPage {
         */
        protected $numRows;
 
+       protected $cachedTimestamp = null;
+
        /**
         * Wheter to show prev/next links
         */
-       var $shownavigation = true;
+       protected $shownavigation = true;
 
        /**
         * A mutator for $this->listoutput;
@@ -99,15 +101,6 @@ abstract class QueryPage extends SpecialPage {
                $this->listoutput = $bool;
        }
 
-       /**
-        * Return title object representing this page
-        *
-        * @return Title
-        */
-       function getTitle() {
-               return SpecialPage::getTitleFor( $this->getName() );
-       }
-
        /**
         * Subclasses return an SQL query here, formatted as an array with the
         * following keys:
@@ -118,8 +111,7 @@ abstract class QueryPage extends SpecialPage {
         *    join_conds => JOIN conditions
         *
         * Note that the query itself should return the following three columns:
-        * 'namespace', 'title', and 'value'
-        * *in that order*. 'value' is used for sorting.
+        * 'namespace', 'title', and 'value'. 'value' is used for sorting.
         *
         * These may be stored in the querycache table for expensive queries,
         * and that cached data will be returned sometimes, so the presence of
@@ -138,14 +130,14 @@ abstract class QueryPage extends SpecialPage {
        function getQueryInfo() {
                return null;
        }
-       
+
        /**
         * For back-compat, subclasses may return a raw SQL query here, as a string.
         * This is stronly deprecated; getQueryInfo() should be overridden instead.
         * @return string
-        * @deprecated since 1.18
         */
        function getSQL() {
+               /* Implement getQueryInfo() instead */
                throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor getQuery() properly" );
        }
 
@@ -168,6 +160,7 @@ abstract class QueryPage extends SpecialPage {
         *       or TS_UNIX (querycache) format, so be sure to always run them
         *       through wfTimestamp()
         * @return bool
+        * @since 1.18
         */
        function usesTimestamps() {
                return false;
@@ -199,6 +192,7 @@ abstract class QueryPage extends SpecialPage {
         * will be disabled in miser mode and will not have their results written
         * to the querycache table.
         * @return Boolean
+        * @since 1.18
         */
        public function isCacheable() {
                return true;
@@ -277,10 +271,14 @@ abstract class QueryPage extends SpecialPage {
         * @param $ignoreErrors Boolean: whether to ignore database errors
         */
        function recache( $limit, $ignoreErrors = true ) {
+               if ( !$this->isCacheable() ) {
+                       return 0;
+               }
+
                $fname = get_class( $this ) . '::recache';
                $dbw = wfGetDB( DB_MASTER );
                $dbr = wfGetDB( DB_SLAVE, array( $this->getName(), __METHOD__, 'vslow' ) );
-               if ( !$dbw || !$dbr || !$this->isCacheable() ) {
+               if ( !$dbw || !$dbr ) {
                        return false;
                }
 
@@ -341,9 +339,11 @@ abstract class QueryPage extends SpecialPage {
         * @param $limit mixed Numerical limit or false for no limit
         * @param $offset mixed Numerical offset or false for no offset
         * @return ResultWrapper
+        * @since 1.18
         */
        function reallyDoQuery( $limit, $offset = false ) {
                $fname = get_class( $this ) . "::reallyDoQuery";
+               $dbr = wfGetDB( DB_SLAVE );
                $query = $this->getQueryInfo();
                $order = $this->getOrderFields();
                if ( $this->sortDescending() ) {
@@ -367,7 +367,6 @@ abstract class QueryPage extends SpecialPage {
                                $options['OFFSET'] = intval( $offset );
                        }
 
-                       $dbr = wfGetDB( DB_SLAVE );
                        $res = $dbr->select( $tables, $fields, $conds, $fname,
                                        $options, $join_conds
                        );
@@ -376,12 +375,15 @@ abstract class QueryPage extends SpecialPage {
                        $sql = $this->getSQL();
                        $sql .= ' ORDER BY ' . implode( ', ', $order );
                        $sql = $dbr->limitResult( $sql, $limit, $offset );
-                       $res = $dbr->query( $sql );
+                       $res = $dbr->query( $sql, $fname );
                }
                return $dbr->resultObject( $res );
        }
 
-       function doQuery( $limit, $offset = false ) {
+       /**
+        * Somewhat deprecated, you probably want to be using execute()
+        */
+       function doQuery( $offset = false, $limit = false ) {
                if ( $this->isCached() && $this->isCacheable() ) {
                        return $this->fetchFromCache( $limit, $offset );
                } else {
@@ -394,6 +396,7 @@ abstract class QueryPage extends SpecialPage {
         * @param $limit mixed Numerical limit or false for no limit
         * @param $offset mixed Numerical offset or false for no offset
         * @return ResultWrapper
+        * @since 1.18
         */
        function fetchFromCache( $limit, $offset = false ) {
                $dbr = wfGetDB( DB_SLAVE );
@@ -404,6 +407,11 @@ abstract class QueryPage extends SpecialPage {
                if ( $offset !== false ) {
                        $options['OFFSET'] = intval( $offset );
                }
+               if ( $this->sortDescending() ) {
+                       $options['ORDER BY'] = 'qc_value DESC';
+               } else {
+                       $options['ORDER BY'] = 'qc_value ASC';
+               }
                $res = $dbr->select( 'querycache', array( 'qc_type',
                                'qc_namespace AS namespace',
                                'qc_title AS title',
@@ -414,22 +422,31 @@ abstract class QueryPage extends SpecialPage {
                return $dbr->resultObject( $res );
        }
 
+       public function getCachedTimestamp() {
+               if ( !is_null( $this->cachedTimestamp ) ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $fname = get_class( $this ) . '::getCachedTimestamp';
+                       $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
+                               array( 'qci_type' => $this->getName() ), $fname );
+               }
+               return $this->cachedTimestamp;
+       }
+
        /**
         * This is the actual workhorse. It does everything needed to make a
         * real, honest-to-gosh query page.
         */
        function execute( $par ) {
-               global $wgUser, $wgOut, $wgLang;
+               global $wgUser, $wgOut, $wgLang, $wgRequest;
 
                if ( !$this->userCanExecute( $wgUser ) ) {
                        $this->displayRestrictionError();
                        return;
                }
 
-               if ( $this->limit == 0 && $this->offset == 0 )
-                       list( $this->limit, $this->offset ) = wfCheckLimits();
-               $sname = $this->getName();
-               $fname = get_class( $this ) . '::doQuery';
+               if ( $this->limit == 0 && $this->offset == 0 ) {
+                       list( $this->limit, $this->offset ) = $wgRequest->getLimitOffset();
+               }
                $dbr = wfGetDB( DB_SLAVE );
 
                $this->setHeaders();
@@ -451,15 +468,14 @@ abstract class QueryPage extends SpecialPage {
                        if ( !$this->listoutput ) {
 
                                # Fetch the timestamp of this update
-                               $tRes = $dbr->select( 'querycache_info', array( 'qci_timestamp' ), array( 'qci_type' => $sname ), $fname );
-                               $tRow = $dbr->fetchObject( $tRes );
-
-                               if ( $tRow ) {
-                                       $updated = $wgLang->timeanddate( $tRow->qci_timestamp, true, true );
-                                       $updateddate = $wgLang->date( $tRow->qci_timestamp, true, true );
-                                       $updatedtime = $wgLang->time( $tRow->qci_timestamp, true, true );
-                                       $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp );
-                                       $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" );
+                               $ts = $this->getCachedTimestamp();
+
+                               if ( $ts ) {
+                                       $updated = $wgLang->timeanddate( $ts, true, true );
+                                       $updateddate = $wgLang->date( $ts, true, true );
+                                       $updatedtime = $wgLang->time( $ts, true, true );
+                                       $wgOut->addMeta( 'Data-Cache-Time', $ts );
+                                       $wgOut->addInlineScript( "var dataCacheTime = '$ts';" );
                                        $wgOut->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime );
                                } else {
                                        $wgOut->addWikiMsg( 'perfcached' );
@@ -505,7 +521,7 @@ abstract class QueryPage extends SpecialPage {
                # with more than a straight list, so we hand them the info, plus
                # an OutputPage, and let them get on with it
                $this->outputResults( $wgOut,
-                       $wgUser->getSkin(),
+                       $this->getSkin(),
                        $dbr, # Should use a ResultWrapper for this
                        $res,
                        $this->numRows,
@@ -537,8 +553,9 @@ abstract class QueryPage extends SpecialPage {
 
                if ( $num > 0 ) {
                        $html = array();
-                       if ( !$this->listoutput )
+                       if ( !$this->listoutput ) {
                                $html[] = $this->openList( $offset );
+                       }
 
                        # $res might contain the whole 1,000 rows, so we read up to
                        # $num [should update this to use a Pager]
@@ -568,8 +585,9 @@ abstract class QueryPage extends SpecialPage {
                                }
                        }
 
-                       if ( !$this->listoutput )
+                       if ( !$this->listoutput ) {
                                $html[] = $this->closeList();
+                       }
 
                        $html = $this->listoutput
                                ? $wgContLang->listToText( $html )
@@ -616,7 +634,6 @@ abstract class QueryPage extends SpecialPage {
                                $this->feedUrl() );
                        $feed->outHeader();
 
-                       $dbr = wfGetDB( DB_SLAVE );
                        $res = $this->reallyDoQuery( $limit, 0 );
                        foreach ( $res as $obj ) {
                                $item = $this->feedResult( $obj );
@@ -671,8 +688,7 @@ abstract class QueryPage extends SpecialPage {
 
        function feedTitle() {
                global $wgLanguageCode, $wgSitename;
-               $page = SpecialPage::getPage( $this->getName() );
-               $desc = $page->getDescription();
+               $desc = $this->getDescription();
                return "$wgSitename - $desc [$wgLanguageCode]";
        }
 
@@ -681,8 +697,7 @@ abstract class QueryPage extends SpecialPage {
        }
 
        function feedUrl() {
-               $title = SpecialPage::getTitleFor( $this->getName() );
-               return $title->getFullURL();
+               return $this->getTitle()->getFullURL();
        }
 }