Merge "Removed references to DB ignoreErrors() function."
[lhc/web/wiklou.git] / includes / QueryPage.php
index 55f56f7..a01ac68 100644 (file)
@@ -1,6 +1,22 @@
 <?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 SpecialPage
  */
@@ -29,6 +45,7 @@ $wgQueryPages = array(
        array( 'MIMEsearchPage',                'MIMEsearch'                    ),
        array( 'MostcategoriesPage',            'Mostcategories'                ),
        array( 'MostimagesPage',                'Mostimages'                    ),
+       array( 'MostinterwikisPage',            'Mostinterwikis'                ),
        array( 'MostlinkedCategoriesPage',      'Mostlinkedcategories'          ),
        array( 'MostlinkedtemplatesPage',       'Mostlinkedtemplates'           ),
        array( 'MostlinkedPage',                'Mostlinked'                    ),
@@ -134,6 +151,7 @@ abstract class QueryPage extends SpecialPage {
        /**
         * For back-compat, subclasses may return a raw SQL query here, as a string.
         * This is stronly deprecated; getQueryInfo() should be overridden instead.
+        * @throws MWException
         * @return string
         */
        function getSQL() {
@@ -284,55 +302,51 @@ abstract class QueryPage extends SpecialPage {
                        return false;
                }
 
-               if ( $ignoreErrors ) {
-                       $ignoreW = $dbw->ignoreErrors( true );
-                       $ignoreR = $dbr->ignoreErrors( true );
-               }
-
-               # Clear out any old cached data
-               $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
-               # Do query
-               $res = $this->reallyDoQuery( $limit, false );
-               $num = false;
-               if ( $res ) {
-                       $num = $res->numRows();
-                       # Fetch results
-                       $vals = array();
-                       while ( $res && $row = $dbr->fetchObject( $res ) ) {
-                               if ( isset( $row->value ) ) {
-                                       if ( $this->usesTimestamps() ) {
-                                               $value = wfTimestamp( TS_UNIX,
-                                                       $row->value );
+               try {
+                       # Clear out any old cached data
+                       $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
+                       # Do query
+                       $res = $this->reallyDoQuery( $limit, false );
+                       $num = false;
+                       if ( $res ) {
+                               $num = $res->numRows();
+                               # Fetch results
+                               $vals = array();
+                               while ( $res && $row = $dbr->fetchObject( $res ) ) {
+                                       if ( isset( $row->value ) ) {
+                                               if ( $this->usesTimestamps() ) {
+                                                       $value = wfTimestamp( TS_UNIX,
+                                                               $row->value );
+                                               } else {
+                                                       $value = intval( $row->value ); // @bug 14414
+                                               }
                                        } else {
-                                               $value = intval( $row->value ); // @bug 14414
+                                               $value = 0;
                                        }
-                               } else {
-                                       $value = 0;
-                               }
 
-                               $vals[] = array( 'qc_type' => $this->getName(),
-                                               'qc_namespace' => $row->namespace,
-                                               'qc_title' => $row->title,
-                                               'qc_value' => $value );
-                       }
+                                       $vals[] = array( 'qc_type' => $this->getName(),
+                                                       'qc_namespace' => $row->namespace,
+                                                       'qc_title' => $row->title,
+                                                       'qc_value' => $value );
+                               }
 
-                       # Save results into the querycache table on the master
-                       if ( count( $vals ) ) {
-                               if ( !$dbw->insert( 'querycache', $vals, __METHOD__ ) ) {
-                                       // Set result to false to indicate error
-                                       $num = false;
+                               # Save results into the querycache table on the master
+                               if ( count( $vals ) ) {
+                                       $dbw->insert( 'querycache', $vals, __METHOD__ );
                                }
+                               # Update the querycache_info record for the page
+                               $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
+                               $dbw->insert( 'querycache_info',
+                                       array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ),
+                                       $fname );
                        }
-                       if ( $ignoreErrors ) {
-                               $dbw->ignoreErrors( $ignoreW );
-                               $dbr->ignoreErrors( $ignoreR );
+               } catch ( DBError $e ) {
+                       if ( !$ignoreErrors ) {
+                               throw $e; // report query error
                        }
-
-                       # Update the querycache_info record for the page
-                       $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
-                       $dbw->insert( 'querycache_info', array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ), $fname );
-
+                       $num = false; // set result to false to indicate error
                }
+
                return $num;
        }
 
@@ -360,7 +374,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 );
@@ -416,9 +430,9 @@ abstract class QueryPage extends SpecialPage {
                        $options['ORDER BY'] = 'qc_value ASC';
                }
                $res = $dbr->select( 'querycache', array( 'qc_type',
-                               'qc_namespace AS namespace',
-                               'qc_title AS title',
-                               'qc_value AS value' ),
+                               'namespace' => 'qc_namespace',
+                               'title' => 'qc_title',
+                               'value' => 'qc_value' ),
                                array( 'qc_type' => $this->getName() ),
                                __METHOD__, $options
                );
@@ -467,10 +481,11 @@ abstract class QueryPage extends SpecialPage {
 
                // TODO: Use doQuery()
                if ( !$this->isCached() ) {
-                       $res = $this->reallyDoQuery( $this->limit, $this->offset );
+                       # select one extra row for navigation
+                       $res = $this->reallyDoQuery( $this->limit + 1, $this->offset );
                } else {
-                       # Get the cached result
-                       $res = $this->fetchFromCache( $this->limit, $this->offset );
+                       # Get the cached result, select one extra row for navigation
+                       $res = $this->fetchFromCache( $this->limit + 1, $this->offset );
                        if ( !$this->listoutput ) {
 
                                # Fetch the timestamp of this update
@@ -483,7 +498,7 @@ abstract class QueryPage extends SpecialPage {
                                        $updateddate = $lang->userDate( $ts, $user );
                                        $updatedtime = $lang->userTime( $ts, $user );
                                        $out->addMeta( 'Data-Cache-Time', $ts );
-                                       $out->addInlineScript( "var dataCacheTime = '$ts';" );
+                                       $out->addJsConfigVars( 'dataCacheTime', $ts );
                                        $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
                                } else {
                                        $out->addWikiMsg( 'perfcached', $maxResults );
@@ -492,7 +507,7 @@ abstract class QueryPage extends SpecialPage {
                                # If updates on this page have been disabled, let the user know
                                # that the data set won't be refreshed for now
                                if ( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
-                                       $out->addWikiMsg( 'querypage-no-updates' );
+                                       $out->wrapWikiMsg( "<div class=\"mw-querypage-no-updates\">\n$1\n</div>", 'querypage-no-updates' );
                                }
                        }
                }
@@ -509,10 +524,11 @@ abstract class QueryPage extends SpecialPage {
                        $out->addHTML( $this->getPageHeader() );
                        if ( $this->numRows > 0 ) {
                                $out->addHTML( $this->msg( 'showingresults' )->numParams(
-                                       $this->numRows, $this->offset + 1 )->parseAsBlock() );
+                                       min( $this->numRows, $this->limit ), # do not show the one extra row, if exist
+                                       $this->offset + 1 )->parseAsBlock() );
                                # Disable the "next" link when we reach the end
                                $paging = $this->getLanguage()->viewPrevNext( $this->getTitle( $par ), $this->offset,
-                                       $this->limit, $this->linkParameters(), ( $this->numRows < $this->limit ) );
+                                       $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.
@@ -530,7 +546,7 @@ abstract class QueryPage extends SpecialPage {
                        $this->getSkin(),
                        $dbr, # Should use a ResultWrapper for this
                        $res,
-                       $this->numRows,
+                       min( $this->numRows, $this->limit ), # do not format the one extra row, if exist
                        $this->offset );
 
                # Repeat the paging links at the bottom
@@ -540,7 +556,7 @@ abstract class QueryPage extends SpecialPage {
 
                $out->addHTML( Xml::closeElement( 'div' ) );
 
-               return $this->numRows;
+               return min( $this->numRows, $this->limit ); # do not return the one extra row, if exist
        }
 
        /**
@@ -632,7 +648,7 @@ abstract class QueryPage extends SpecialPage {
 
                if ( !$wgFeed ) {
                        $this->getOutput()->addWikiMsg( 'feed-unavailable' );
-                       return;
+                       return false;
                }
 
                global $wgFeedLimit;