Slightly simplify QueryPage::outputResults
[lhc/web/wiklou.git] / includes / specialpage / QueryPage.php
index bfb29ae..27e645a 100644 (file)
@@ -275,11 +275,14 @@ abstract class QueryPage extends SpecialPage {
        }
 
        /**
-        * Some special pages (for example SpecialListusers) might not return the
+        * Some special pages (for example SpecialListusers used to) might not return the
         * current object formatted, but return the previous one instead.
         * 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.
+        *
+        * @deprecated since 1.27
+        *
         * @return bool
         */
        function tryLastResult() {
@@ -325,25 +328,39 @@ abstract class QueryPage extends SpecialPage {
                                                $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
+                                       );
                                }
 
-                               $dbw->startAtomic( __METHOD__ );
-                               # Clear out any old cached data
-                               $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
-                               # 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 );
-                               $dbw->endAtomic( __METHOD__ );
+                               $that = $this;
+                               $dbw->doAtomicSection(
+                                       __METHOD__,
+                                       function ( IDatabase $dbw, $fname ) use ( $that, $vals ) {
+                                               # Clear out any old cached data
+                                               $dbw->delete( 'querycache',
+                                                       array( 'qc_type' => $that->getName() ),
+                                                       $fname
+                                               );
+                                               # Save results into the querycache table on the master
+                                               if ( count( $vals ) ) {
+                                                       $dbw->insert( 'querycache', $vals, $fname );
+                                               }
+                                               # Update the querycache_info record for the page
+                                               $dbw->delete( 'querycache_info',
+                                                       array( 'qci_type' => $that->getName() ),
+                                                       $fname
+                                               );
+                                               $dbw->insert( 'querycache_info',
+                                                       array( 'qci_type' => $that->getName(),
+                                                               'qci_timestamp' => $dbw->timestamp() ),
+                                                       $fname
+                                               );
+                                       }
+                               );
                        }
                } catch ( DBError $e ) {
                        if ( !$ignoreErrors ) {
@@ -473,12 +490,37 @@ abstract class QueryPage extends SpecialPage {
         * Returns limit and offset, as returned by $this->getRequest()->getLimitOffset().
         * 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
         *
         * @return int[] list( $limit, $offset )
         */
        protected function getLimitOffset() {
-               return $this->getRequest()->getLimitOffset();
+               list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
+               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 );
+               }
+               return array( $limit, $offset );
+       }
+
+       /**
+        * Get max number of results we can return in miser mode.
+        *
+        * Most QueryPage subclasses use inefficient paging, so limit the max amount we return
+        * This matters for uncached query pages that might otherwise accept an offset of 3 million
+        *
+        * @since 1.27
+        * @return int
+        */
+       protected function getMaxResults() {
+               // Max of 10000, unless we store more than 5000 in query cache.
+               return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
        }
 
        /**
@@ -562,8 +604,10 @@ 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() );
                                $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset,
-                                       $this->limit, $this->linkParameters(), ( $this->numRows <= $this->limit ) );
+                                       $this->limit, $this->linkParameters(), $atEnd );
                                $out->addHTML( '<p>' . $paging . '</p>' );
                        } else {
                                # No results to show, so don't bother with "showing X of Y" etc.
@@ -619,12 +663,9 @@ abstract class QueryPage extends SpecialPage {
                                // @codingStandardsIgnoreEnd
                                $line = $this->formatResult( $skin, $row );
                                if ( $line ) {
-                                       $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
-                                               ? ' class="not-patrolled"'
-                                               : '';
                                        $html[] = $this->listoutput
                                                ? $line
-                                               : "<li{$attr}>{$line}</li>\n";
+                                               : "<li>{$line}</li>\n";
                                }
                        }
 
@@ -633,12 +674,9 @@ abstract class QueryPage extends SpecialPage {
                                $row = null;
                                $line = $this->formatResult( $skin, $row );
                                if ( $line ) {
-                                       $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
-                                               ? ' class="not-patrolled"'
-                                               : '';
                                        $html[] = $this->listoutput
                                                ? $line
-                                               : "<li{$attr}>{$line}</li>\n";
+                                               : "<li>{$line}</li>\n";
                                }
                        }