fix some spacing
[lhc/web/wiklou.git] / includes / cache / BacklinkCache.php
index c4f8762..11e1e7b 100644 (file)
@@ -222,11 +222,11 @@ class BacklinkCache {
         */
        protected function getPrefix( $table ) {
                static $prefixes = array(
-                       'pagelinks'     => 'pl',
-                       'imagelinks'    => 'il',
+                       'pagelinks' => 'pl',
+                       'imagelinks' => 'il',
                        'categorylinks' => 'cl',
                        'templatelinks' => 'tl',
-                       'redirect'      => 'rd',
+                       'redirect' => 'rd',
                );
 
                if ( isset( $prefixes[$table] ) ) {
@@ -259,17 +259,17 @@ class BacklinkCache {
                        case 'templatelinks':
                                $conds = array(
                                        "{$prefix}_namespace" => $this->title->getNamespace(),
-                                       "{$prefix}_title"     => $this->title->getDBkey(),
+                                       "{$prefix}_title" => $this->title->getDBkey(),
                                        "page_id={$prefix}_from"
                                );
                                break;
                        case 'redirect':
                                $conds = array(
                                        "{$prefix}_namespace" => $this->title->getNamespace(),
-                                       "{$prefix}_title"     => $this->title->getDBkey(),
+                                       "{$prefix}_title" => $this->title->getDBkey(),
                                        $this->getDb()->makeList( array(
-                                               "{$prefix}_interwiki = ''",
-                                               "{$prefix}_interwiki is null",
+                                               "{$prefix}_interwiki" => '',
+                                               "{$prefix}_interwiki IS NULL",
                                        ), LIST_OR ),
                                        "page_id={$prefix}_from"
                                );
@@ -297,23 +297,33 @@ class BacklinkCache {
                return $conds;
        }
 
+       /**
+        * Check if there are any backlinks
+        * @param $table String
+        * @return bool
+        */
+       public function hasLinks( $table ) {
+               return ( $this->getNumLinks( $table, 1 ) > 0 );
+       }
+
        /**
         * Get the approximate number of backlinks
         * @param $table String
+        * @param $max integer Only count up to this many backlinks
         * @return integer
         */
-       public function getNumLinks( $table ) {
+       public function getNumLinks( $table, $max = INF ) {
                global $wgMemc;
 
                // 1) try partition cache ...
                if ( isset( $this->partitionCache[$table] ) ) {
                        $entry = reset( $this->partitionCache[$table] );
-                       return $entry['numRows'];
+                       return min( $max, $entry['numRows'] );
                }
 
                // 2) ... then try full result cache ...
                if ( isset( $this->fullResultCache[$table] ) ) {
-                       return $this->fullResultCache[$table]->numRows();
+                       return min( $max, $this->fullResultCache[$table]->numRows() );
                }
 
                $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table );
@@ -321,12 +331,22 @@ class BacklinkCache {
                // 3) ... fallback to memcached ...
                $count = $wgMemc->get( $memcKey );
                if ( $count ) {
-                       return $count;
+                       return min( $max, $count );
                }
 
                // 4) fetch from the database ...
-               $count = $this->getLinks( $table )->count();
-               $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY );
+               if ( is_infinite( $max ) ) { // full count
+                       $count = $this->getLinks( $table )->count();
+                       $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY );
+               } else { // with limit
+                       $count = $this->getDB()->select(
+                               array( $table, 'page' ),
+                               '1',
+                               $this->getConditions( $table ),
+                               __METHOD__,
+                               array( 'LIMIT' => $max )
+                       )->numRows();
+               }
 
                return $count;
        }
@@ -374,7 +394,6 @@ class BacklinkCache {
                        return $cacheEntry['batches'];
                }
 
-
                // 4) ... finally fetch from the slow database :(
                $this->getLinks( $table );
                $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );