Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / specialpage / QueryPage.php
index 27e645a..2523810 100644 (file)
@@ -64,43 +64,43 @@ abstract class QueryPage extends SpecialPage {
 
                if ( $qp === null ) {
                        // QueryPage subclass, Special page name
-                       $qp = array(
-                               array( 'AncientPagesPage', 'Ancientpages' ),
-                               array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
-                               array( 'DeadendPagesPage', 'Deadendpages' ),
-                               array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
-                               array( 'FileDuplicateSearchPage', 'FileDuplicateSearch' ),
-                               array( 'ListDuplicatedFilesPage', 'ListDuplicatedFiles' ),
-                               array( 'LinkSearchPage', 'LinkSearch' ),
-                               array( 'ListredirectsPage', 'Listredirects' ),
-                               array( 'LonelyPagesPage', 'Lonelypages' ),
-                               array( 'LongPagesPage', 'Longpages' ),
-                               array( 'MediaStatisticsPage', 'MediaStatistics' ),
-                               array( 'MIMEsearchPage', 'MIMEsearch' ),
-                               array( 'MostcategoriesPage', 'Mostcategories' ),
-                               array( 'MostimagesPage', 'Mostimages' ),
-                               array( 'MostinterwikisPage', 'Mostinterwikis' ),
-                               array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
-                               array( 'MostlinkedTemplatesPage', 'Mostlinkedtemplates' ),
-                               array( 'MostlinkedPage', 'Mostlinked' ),
-                               array( 'MostrevisionsPage', 'Mostrevisions' ),
-                               array( 'FewestrevisionsPage', 'Fewestrevisions' ),
-                               array( 'ShortPagesPage', 'Shortpages' ),
-                               array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
-                               array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
-                               array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
-                               array( 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ),
-                               array( 'UnusedCategoriesPage', 'Unusedcategories' ),
-                               array( 'UnusedimagesPage', 'Unusedimages' ),
-                               array( 'WantedCategoriesPage', 'Wantedcategories' ),
-                               array( 'WantedFilesPage', 'Wantedfiles' ),
-                               array( 'WantedPagesPage', 'Wantedpages' ),
-                               array( 'WantedTemplatesPage', 'Wantedtemplates' ),
-                               array( 'UnwatchedpagesPage', 'Unwatchedpages' ),
-                               array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
-                               array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
-                       );
-                       Hooks::run( 'wgQueryPages', array( &$qp ) );
+                       $qp = [
+                               [ 'AncientPagesPage', 'Ancientpages' ],
+                               [ 'BrokenRedirectsPage', 'BrokenRedirects' ],
+                               [ 'DeadendPagesPage', 'Deadendpages' ],
+                               [ 'DoubleRedirectsPage', 'DoubleRedirects' ],
+                               [ 'FileDuplicateSearchPage', 'FileDuplicateSearch' ],
+                               [ 'ListDuplicatedFilesPage', 'ListDuplicatedFiles' ],
+                               [ 'LinkSearchPage', 'LinkSearch' ],
+                               [ 'ListredirectsPage', 'Listredirects' ],
+                               [ 'LonelyPagesPage', 'Lonelypages' ],
+                               [ 'LongPagesPage', 'Longpages' ],
+                               [ 'MediaStatisticsPage', 'MediaStatistics' ],
+                               [ 'MIMEsearchPage', 'MIMEsearch' ],
+                               [ 'MostcategoriesPage', 'Mostcategories' ],
+                               [ 'MostimagesPage', 'Mostimages' ],
+                               [ 'MostinterwikisPage', 'Mostinterwikis' ],
+                               [ 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ],
+                               [ 'MostlinkedTemplatesPage', 'Mostlinkedtemplates' ],
+                               [ 'MostlinkedPage', 'Mostlinked' ],
+                               [ 'MostrevisionsPage', 'Mostrevisions' ],
+                               [ 'FewestrevisionsPage', 'Fewestrevisions' ],
+                               [ 'ShortPagesPage', 'Shortpages' ],
+                               [ 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ],
+                               [ 'UncategorizedPagesPage', 'Uncategorizedpages' ],
+                               [ 'UncategorizedImagesPage', 'Uncategorizedimages' ],
+                               [ 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ],
+                               [ 'UnusedCategoriesPage', 'Unusedcategories' ],
+                               [ 'UnusedimagesPage', 'Unusedimages' ],
+                               [ 'WantedCategoriesPage', 'Wantedcategories' ],
+                               [ 'WantedFilesPage', 'Wantedfiles' ],
+                               [ 'WantedPagesPage', 'Wantedpages' ],
+                               [ 'WantedTemplatesPage', 'Wantedtemplates' ],
+                               [ 'UnwatchedpagesPage', 'Unwatchedpages' ],
+                               [ 'UnusedtemplatesPage', 'Unusedtemplates' ],
+                               [ 'WithoutInterwikiPage', 'Withoutinterwiki' ],
+                       ];
+                       Hooks::run( 'wgQueryPages', [ &$qp ] );
                }
 
                return $qp;
@@ -165,7 +165,7 @@ abstract class QueryPage extends SpecialPage {
         * @since 1.18
         */
        function getOrderFields() {
-               return array( 'value' );
+               return [ 'value' ];
        }
 
        /**
@@ -271,7 +271,7 @@ abstract class QueryPage extends SpecialPage {
         * @return array
         */
        function linkParameters() {
-               return array();
+               return [];
        }
 
        /**
@@ -315,7 +315,7 @@ abstract class QueryPage extends SpecialPage {
                        if ( $res ) {
                                $num = $res->numRows();
                                # Fetch results
-                               $vals = array();
+                               $vals = [];
                                foreach ( $res as $row ) {
                                        if ( isset( $row->value ) ) {
                                                if ( $this->usesTimestamps() ) {
@@ -328,21 +328,20 @@ abstract class QueryPage extends SpecialPage {
                                                $value = 0;
                                        }
 
-                                       $vals[] = array(
+                                       $vals[] = [
                                                'qc_type' => $this->getName(),
                                                'qc_namespace' => $row->namespace,
                                                'qc_title' => $row->title,
                                                'qc_value' => $value
-                                       );
+                                       ];
                                }
 
-                               $that = $this;
                                $dbw->doAtomicSection(
                                        __METHOD__,
-                                       function ( IDatabase $dbw, $fname ) use ( $that, $vals ) {
+                                       function ( IDatabase $dbw, $fname ) use ( $vals ) {
                                                # Clear out any old cached data
                                                $dbw->delete( 'querycache',
-                                                       array( 'qc_type' => $that->getName() ),
+                                                       [ 'qc_type' => $this->getName() ],
                                                        $fname
                                                );
                                                # Save results into the querycache table on the master
@@ -351,12 +350,12 @@ abstract class QueryPage extends SpecialPage {
                                                }
                                                # Update the querycache_info record for the page
                                                $dbw->delete( 'querycache_info',
-                                                       array( 'qci_type' => $that->getName() ),
+                                                       [ 'qci_type' => $this->getName() ],
                                                        $fname
                                                );
                                                $dbw->insert( 'querycache_info',
-                                                       array( 'qci_type' => $that->getName(),
-                                                               'qci_timestamp' => $dbw->timestamp() ),
+                                                       [ 'qci_type' => $this->getName(),
+                                                               'qci_timestamp' => $dbw->timestamp() ],
                                                        $fname
                                                );
                                        }
@@ -377,7 +376,7 @@ abstract class QueryPage extends SpecialPage {
         * @return IDatabase
         */
        function getRecacheDB() {
-               return wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
+               return wfGetDB( DB_SLAVE, [ $this->getName(), 'QueryPage::recache', 'vslow' ] );
        }
 
        /**
@@ -400,11 +399,11 @@ abstract class QueryPage extends SpecialPage {
                }
 
                if ( is_array( $query ) ) {
-                       $tables = isset( $query['tables'] ) ? (array)$query['tables'] : array();
-                       $fields = isset( $query['fields'] ) ? (array)$query['fields'] : array();
-                       $conds = isset( $query['conds'] ) ? (array)$query['conds'] : array();
-                       $options = isset( $query['options'] ) ? (array)$query['options'] : array();
-                       $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : array();
+                       $tables = isset( $query['tables'] ) ? (array)$query['tables'] : [];
+                       $fields = isset( $query['fields'] ) ? (array)$query['fields'] : [];
+                       $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
+                       $options = isset( $query['options'] ) ? (array)$query['options'] : [];
+                       $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [];
 
                        if ( count( $order ) ) {
                                $options['ORDER BY'] = $order;
@@ -455,7 +454,7 @@ abstract class QueryPage extends SpecialPage {
         */
        public function fetchFromCache( $limit, $offset = false ) {
                $dbr = wfGetDB( DB_SLAVE );
-               $options = array();
+               $options = [];
                if ( $limit !== false ) {
                        $options['LIMIT'] = intval( $limit );
                }
@@ -467,11 +466,11 @@ abstract class QueryPage extends SpecialPage {
                } else {
                        $options['ORDER BY'] = 'qc_value ASC';
                }
-               return $dbr->select( 'querycache', array( 'qc_type',
+               return $dbr->select( 'querycache', [ 'qc_type',
                                'namespace' => 'qc_namespace',
                                'title' => 'qc_title',
-                               'value' => 'qc_value' ),
-                               array( 'qc_type' => $this->getName() ),
+                               'value' => 'qc_value' ],
+                               [ 'qc_type' => $this->getName() ],
                                __METHOD__, $options
                );
        }
@@ -481,7 +480,7 @@ abstract class QueryPage extends SpecialPage {
                        $dbr = wfGetDB( DB_SLAVE );
                        $fname = get_class( $this ) . '::getCachedTimestamp';
                        $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
-                               array( 'qci_type' => $this->getName() ), $fname );
+                               [ 'qci_type' => $this->getName() ], $fname );
                }
                return $this->cachedTimestamp;
        }
@@ -491,22 +490,41 @@ abstract class QueryPage extends SpecialPage {
         * Subclasses may override this to further restrict or modify limit and offset.
         *
         * @note Restricts the offset parameter, as most query pages have inefficient paging
-        * @since 1.26
         *
+        * Its generally expected that the returned limit will not be 0, and the returned
+        * offset will be less than the max results.
+        *
+        * @since 1.26
         * @return int[] list( $limit, $offset )
         */
        protected function getLimitOffset() {
                list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
-               if ( !$this->getConfig()->get( 'MiserMode' ) ) {
+               if ( $this->getConfig()->get( 'MiserMode' ) ) {
                        $maxResults = $this->getMaxResults();
                        // Can't display more than max results on a page
                        $limit = min( $limit, $maxResults );
-                       // Can't skip over more than $maxResults
-                       $offset = min( $offset, $maxResults );
-                       // Can't let $offset + $limit > $maxResults
-                       $limit = min( $limit, $maxResults - $offset );
+                       // Can't skip over more than the end of $maxResults
+                       $offset = min( $offset, $maxResults + 1 );
+               }
+               return [ $limit, $offset ];
+       }
+
+       /**
+        * What is limit to fetch from DB
+        *
+        * Used to make it appear the DB stores less results then it actually does
+        * @param $uiLimit int Limit from UI
+        * @param $uiOffset int Offset from UI
+        * @return int Limit to use for DB (not including extra row to see if at end)
+        */
+       protected function getDBLimit( $uiLimit, $uiOffset ) {
+               $maxResults = $this->getMaxResults();
+               if ( $this->getConfig()->get( 'MiserMode' ) ) {
+                       $limit = min( $uiLimit + 1, $maxResults - $uiOffset );
+                       return max( $limit, 0 );
+               } else {
+                       return $uiLimit + 1;
                }
-               return array( $limit, $offset );
        }
 
        /**
@@ -519,7 +537,7 @@ abstract class QueryPage extends SpecialPage {
         * @return int
         */
        protected function getMaxResults() {
-               // Max of 10000, unless we store more than 5000 in query cache.
+               // Max of 10000, unless we store more than 10000 in query cache.
                return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
        }
 
@@ -550,14 +568,14 @@ abstract class QueryPage extends SpecialPage {
                if ( $this->limit == 0 && $this->offset == 0 ) {
                        list( $this->limit, $this->offset ) = $this->getLimitOffset();
                }
-
+               $dbLimit = $this->getDBLimit( $this->limit, $this->offset );
                // @todo Use doQuery()
                if ( !$this->isCached() ) {
                        # select one extra row for navigation
-                       $res = $this->reallyDoQuery( $this->limit + 1, $this->offset );
+                       $res = $this->reallyDoQuery( $dbLimit, $this->offset );
                } else {
                        # Get the cached result, select one extra row for navigation
-                       $res = $this->fetchFromCache( $this->limit + 1, $this->offset );
+                       $res = $this->fetchFromCache( $dbLimit, $this->offset );
                        if ( !$this->listoutput ) {
 
                                # Fetch the timestamp of this update
@@ -594,7 +612,7 @@ abstract class QueryPage extends SpecialPage {
                $dbr = $this->getRecacheDB();
                $this->preprocessResults( $dbr, $res );
 
-               $out->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
+               $out->addHTML( Xml::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) );
 
                # Top header and navigation
                if ( $this->shownavigation ) {
@@ -604,8 +622,9 @@ abstract class QueryPage extends SpecialPage {
                                        min( $this->numRows, $this->limit ), # do not show the one extra row, if exist
                                        $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
                                # Disable the "next" link when we reach the end
-                               $atEnd = ( $this->numRows <= $this->limit )
-                                       || ( $this->offset + $this-> limit  >= $this->getMaxResults() );
+                               $miserMaxResults = $this->getConfig()->get( 'MiserMode' )
+                                       && ( $this->offset + $this->limit >= $this->getMaxResults() );
+                               $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults;
                                $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset,
                                        $this->limit, $this->linkParameters(), $atEnd );
                                $out->addHTML( '<p>' . $paging . '</p>' );
@@ -651,7 +670,7 @@ abstract class QueryPage extends SpecialPage {
                global $wgContLang;
 
                if ( $num > 0 ) {
-                       $html = array();
+                       $html = [];
                        if ( !$this->listoutput ) {
                                $html[] = $this->openList( $offset );
                        }