(bug 37183) Removed hard coded parentheses in SpecialListfiles.php
[lhc/web/wiklou.git] / includes / QueryPage.php
index 17a641b..4440aac 100644 (file)
@@ -1,8 +1,24 @@
 <?php
 /**
- * Contain a class for special pages
+ * Base code for "query" special pages.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
- * @ingroup SpecialPages
+ * @ingroup SpecialPage
  */
 
 /**
@@ -259,6 +275,7 @@ abstract class QueryPage extends SpecialPage {
         * Setting this to return true will ensure formatResult() is called
         * one more time to make sure that the very last result is formatted
         * as well.
+        * @return bool
         */
        function tryLastResult() {
                return false;
@@ -269,6 +286,7 @@ abstract class QueryPage extends SpecialPage {
         *
         * @param $limit Integer: limit for SQL statement
         * @param $ignoreErrors Boolean: whether to ignore database errors
+        * @return bool|int
         */
        function recache( $limit, $ignoreErrors = true ) {
                if ( !$this->isCacheable() ) {
@@ -293,7 +311,7 @@ abstract class QueryPage extends SpecialPage {
                $res = $this->reallyDoQuery( $limit, false );
                $num = false;
                if ( $res ) {
-                       $num = $dbr->numRows( $res );
+                       $num = $res->numRows();
                        # Fetch results
                        $vals = array();
                        while ( $res && $row = $dbr->fetchObject( $res ) ) {
@@ -358,7 +376,7 @@ abstract class QueryPage extends SpecialPage {
                        $options = isset( $query['options'] ) ? (array)$query['options'] : array();
                        $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : array();
                        if ( count( $order ) ) {
-                               $options['ORDER BY'] = implode( ', ', $order );
+                               $options['ORDER BY'] = $order;
                        }
                        if ( $limit !== false ) {
                                $options['LIMIT'] = intval( $limit );
@@ -382,6 +400,7 @@ abstract class QueryPage extends SpecialPage {
 
        /**
         * Somewhat deprecated, you probably want to be using execute()
+        * @return ResultWrapper
         */
        function doQuery( $offset = false, $limit = false ) {
                if ( $this->isCached() && $this->isCacheable() ) {
@@ -423,7 +442,7 @@ abstract class QueryPage extends SpecialPage {
        }
 
        public function getCachedTimestamp() {
-               if ( !is_null( $this->cachedTimestamp ) ) {
+               if ( is_null( $this->cachedTimestamp ) ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        $fname = get_class( $this ) . '::getCachedTimestamp';
                        $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
@@ -435,30 +454,34 @@ abstract class QueryPage extends SpecialPage {
        /**
         * This is the actual workhorse. It does everything needed to make a
         * real, honest-to-gosh query page.
+        * @return int
         */
        function execute( $par ) {
-               global $wgUser, $wgOut, $wgLang;
+               global $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
 
-               if ( !$this->userCanExecute( $wgUser ) ) {
+               $user = $this->getUser();
+               if ( !$this->userCanExecute( $user ) ) {
                        $this->displayRestrictionError();
                        return;
                }
 
-               if ( $this->limit == 0 && $this->offset == 0 )
-                       list( $this->limit, $this->offset ) = wfCheckLimits();
-               $dbr = wfGetDB( DB_SLAVE );
-
                $this->setHeaders();
-               $wgOut->setSyndicated( $this->isSyndicated() );
+               $this->outputHeader();
+
+               $out = $this->getOutput();
 
                if ( $this->isCached() && !$this->isCacheable() ) {
-                       $wgOut->setSyndicated( false );
-                       $wgOut->addWikiMsg( 'querypage-disabled' );
+                       $out->addWikiMsg( 'querypage-disabled' );
                        return 0;
                }
 
+               $out->setSyndicated( $this->isSyndicated() );
+
+               if ( $this->limit == 0 && $this->offset == 0 ) {
+                       list( $this->limit, $this->offset ) = $this->getRequest()->getLimitOffset();
+               }
+
                // TODO: Use doQuery()
-               // $res = null;
                if ( !$this->isCached() ) {
                        $res = $this->reallyDoQuery( $this->limit, $this->offset );
                } else {
@@ -468,50 +491,50 @@ abstract class QueryPage extends SpecialPage {
 
                                # Fetch the timestamp of this update
                                $ts = $this->getCachedTimestamp();
+                               $lang = $this->getLanguage();
+                               $maxResults = $lang->formatNum( $wgQueryCacheLimit );
 
                                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 );
+                                       $updated = $lang->userTimeAndDate( $ts, $user );
+                                       $updateddate = $lang->userDate( $ts, $user );
+                                       $updatedtime = $lang->userTime( $ts, $user );
+                                       $out->addMeta( 'Data-Cache-Time', $ts );
+                                       $out->addJsConfigVars( 'dataCacheTime', $ts );
+                                       $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
                                } else {
-                                       $wgOut->addWikiMsg( 'perfcached' );
+                                       $out->addWikiMsg( 'perfcached', $maxResults );
                                }
 
                                # If updates on this page have been disabled, let the user know
                                # that the data set won't be refreshed for now
-                               global $wgDisableQueryPageUpdate;
                                if ( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
-                                       $wgOut->addWikiMsg( 'querypage-no-updates' );
+                                       $out->wrapWikiMsg( "<div class=\"mw-querypage-no-updates\">\n$1\n</div>", 'querypage-no-updates' );
                                }
-
                        }
-
                }
 
-               $this->numRows = $dbr->numRows( $res );
+               $this->numRows = $res->numRows();
 
+               $dbr = wfGetDB( DB_SLAVE );
                $this->preprocessResults( $dbr, $res );
 
-               $wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
+               $out->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
 
                # Top header and navigation
                if ( $this->shownavigation ) {
-                       $wgOut->addHTML( $this->getPageHeader() );
+                       $out->addHTML( $this->getPageHeader() );
                        if ( $this->numRows > 0 ) {
-                               $wgOut->addHTML( '<p>' . wfShowingResults( $this->offset, $this->numRows ) . '</p>' );
+                               $out->addHTML( $this->msg( 'showingresults' )->numParams(
+                                       $this->numRows, $this->offset + 1 )->parseAsBlock() );
                                # Disable the "next" link when we reach the end
-                               $paging = wfViewPrevNext( $this->offset, $this->limit,
-                                       $this->getTitle( $par ),
-                                       wfArrayToCGI( $this->linkParameters() ), ( $this->numRows < $this->limit ) );
-                               $wgOut->addHTML( '<p>' . $paging . '</p>' );
+                               $paging = $this->getLanguage()->viewPrevNext( $this->getTitle( $par ), $this->offset,
+                                       $this->limit, $this->linkParameters(), ( $this->numRows < $this->limit ) );
+                               $out->addHTML( '<p>' . $paging . '</p>' );
                        } else {
                                # No results to show, so don't bother with "showing X of Y" etc.
                                # -- just let the user know and give up now
-                               $wgOut->addHTML( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' );
-                               $wgOut->addHTML( Xml::closeElement( 'div' ) );
+                               $out->addWikiMsg( 'specialpage-empty' );
+                               $out->addHTML( Xml::closeElement( 'div' ) );
                                return;
                        }
                }
@@ -519,8 +542,8 @@ abstract class QueryPage extends SpecialPage {
                # The actual results; specialist subclasses will want to handle this
                # 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->outputResults( $out,
+                       $this->getSkin(),
                        $dbr, # Should use a ResultWrapper for this
                        $res,
                        $this->numRows,
@@ -528,10 +551,10 @@ abstract class QueryPage extends SpecialPage {
 
                # Repeat the paging links at the bottom
                if ( $this->shownavigation ) {
-                       $wgOut->addHTML( '<p>' . $paging . '</p>' );
+                       $out->addHTML( '<p>' . $paging . '</p>' );
                }
 
-               $wgOut->addHTML( Xml::closeElement( 'div' ) );
+               $out->addHTML( Xml::closeElement( 'div' ) );
 
                return $this->numRows;
        }
@@ -596,10 +619,17 @@ abstract class QueryPage extends SpecialPage {
                }
        }
 
+       /**
+        * @param $offset
+        * @return string
+        */
        function openList( $offset ) {
                return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
        }
 
+       /**
+        * @return string
+        */
        function closeList() {
                return "</ol>\n";
        }
@@ -611,13 +641,13 @@ abstract class QueryPage extends SpecialPage {
 
        /**
         * Similar to above, but packaging in a syndicated feed instead of a web page
+        * @return bool
         */
        function doFeed( $class = '', $limit = 50 ) {
                global $wgFeed, $wgFeedClasses;
 
                if ( !$wgFeed ) {
-                       global $wgOut;
-                       $wgOut->addWikiMsg( 'feed-unavailable' );
+                       $this->getOutput()->addWikiMsg( 'feed-unavailable' );
                        return;
                }
 
@@ -651,12 +681,13 @@ abstract class QueryPage extends SpecialPage {
        /**
         * Override for custom handling. If the titles/links are ok, just do
         * feedItemDesc()
+        * @return FeedItem|null
         */
        function feedResult( $row ) {
                if ( !isset( $row->title ) ) {
                        return null;
                }
-               $title = Title::MakeTitle( intval( $row->namespace ), $row->title );
+               $title = Title::makeTitle( intval( $row->namespace ), $row->title );
                if ( $title ) {
                        $date = isset( $row->timestamp ) ? $row->timestamp : '';
                        $comments = '';
@@ -692,7 +723,7 @@ abstract class QueryPage extends SpecialPage {
        }
 
        function feedDesc() {
-               return wfMsgExt( 'tagline', 'parsemag' );
+               return $this->msg( 'tagline' )->text();
        }
 
        function feedUrl() {
@@ -718,6 +749,10 @@ abstract class WantedQueryPage extends QueryPage {
         * Cache page existence for performance
         */
        function preprocessResults( $db, $res ) {
+               if ( !$res->numRows() ) {
+                       return;
+               }
+
                $batch = new LinkBatch;
                foreach ( $res as $row ) {
                        $batch->add( $row->namespace, $row->title );
@@ -725,9 +760,7 @@ abstract class WantedQueryPage extends QueryPage {
                $batch->execute();
 
                // Back to start for display
-               if ( $db->numRows( $res ) > 0 )
-                       // If there are no rows we get an error seeking.
-                       $db->dataSeek( $res, 0 );
+               $res->seek( 0 );
        }
 
        /**
@@ -736,6 +769,7 @@ abstract class WantedQueryPage extends QueryPage {
         * kluge for Special:WantedFiles, which may contain false
         * positives for files that exist e.g. in a shared repo (bug
         * 6220).
+        * @return bool
         */
        function forceExistenceCheck() {
                return false;
@@ -753,8 +787,8 @@ abstract class WantedQueryPage extends QueryPage {
                if ( $title instanceof Title ) {
                        if ( $this->isCached() || $this->forceExistenceCheck() ) {
                                $pageLink = $title->isKnown()
-                                       ? '<del>' . $skin->link( $title ) . '</del>'
-                                       : $skin->link(
+                                       ? '<del>' . Linker::link( $title ) . '</del>'
+                                       : Linker::link(
                                                $title,
                                                null,
                                                array(),
@@ -762,7 +796,7 @@ abstract class WantedQueryPage extends QueryPage {
                                                array( 'broken' )
                                        );
                        } else {
-                               $pageLink = $skin->link(
+                               $pageLink = Linker::link(
                                        $title,
                                        null,
                                        array(),
@@ -770,10 +804,9 @@ abstract class WantedQueryPage extends QueryPage {
                                        array( 'broken' )
                                );
                        }
-                       return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
+                       return $this->getLanguage()->specialList( $pageLink, $this->makeWlhLink( $title, $result ) );
                } else {
-                       $tsafe = htmlspecialchars( $result->title );
-                       return wfMsgHtml( 'wantedpages-badtitle', $tsafe );
+                       return $this->msg( 'wantedpages-badtitle', $result->title )->escaped();
                }
        }
 
@@ -781,15 +814,12 @@ abstract class WantedQueryPage extends QueryPage {
         * Make a "what links here" link for a given title
         *
         * @param $title Title to make the link for
-        * @param $skin Skin object to use
         * @param $result Object: result row
         * @return string
         */
-       private function makeWlhLink( $title, $skin, $result ) {
-               global $wgLang;
-               $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
-               $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
-               $wgLang->formatNum( $result->value ) );
-               return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
+       private function makeWlhLink( $title, $result ) {
+               $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
+               $label = $this->msg( 'nlinks' )->numParams( $result->value )->escaped();
+               return Linker::link( $wlh, $label );
        }
 }