Step 3: Balance the quotes directly on $text
[lhc/web/wiklou.git] / includes / BacklinkCache.php
index b48a34a..e445145 100644 (file)
@@ -47,19 +47,21 @@ class BacklinkCache {
 
        /**
         * Get the backlinks for a given table. Cached in process memory only.
-        * @param string $table
+        * @param $table String
+        * @param $startId Integer or false
+        * @param $endId Integer or false
         * @return TitleArray
         */
        public function getLinks( $table, $startId = false, $endId = false ) {
                wfProfileIn( __METHOD__ );
 
+               $fromField = $this->getPrefix( $table ) . '_from';
                if ( $startId || $endId ) {
                        // Partial range, not cached
                        wfDebug( __METHOD__.": from DB (uncacheable range)\n" );
                        $conds = $this->getConditions( $table );
                        // Use the from field in the condition rather than the joined page_id,
                        // because databases are stupid and don't necessarily propagate indexes.
-                       $fromField = $this->getPrefix( $table ) . '_from';
                        if ( $startId ) {
                                $conds[] = "$fromField >= " . intval( $startId );
                        }
@@ -67,10 +69,14 @@ class BacklinkCache {
                                $conds[] = "$fromField <= " . intval( $endId );
                        }
                        $res = $this->getDB()->select( 
-                               array( 'page', $table ),
-                               array( 'page_namespace', 'page_title', 'page_id' ),
+                               array( $table, 'page' ),
+                               array( 'page_namespace', 'page_title', 'page_id'),
                                $conds,
-                               __METHOD__ );
+                               __METHOD__,
+                               array(
+                                       'STRAIGHT_JOIN',
+                                       'ORDER BY' => $fromField
+                               ) );
                        $ta = TitleArray::newFromResult( $res );
                        wfProfileOut( __METHOD__ );
                        return $ta;
@@ -79,10 +85,14 @@ class BacklinkCache {
                if ( !isset( $this->fullResultCache[$table] ) ) {
                        wfDebug( __METHOD__.": from DB\n" );
                        $res = $this->getDB()->select( 
-                               array( 'page', $table ),
+                               array( $table, 'page' ),
                                array( 'page_namespace', 'page_title', 'page_id' ),
                                $this->getConditions( $table ),
-                               __METHOD__ );
+                               __METHOD__,
+                               array(
+                                       'STRAIGHT_JOIN',
+                                       'ORDER BY' => $fromField,
+                               ) );
                        $this->fullResultCache[$table] = $res;
                }
                $ta = TitleArray::newFromResult( $this->fullResultCache[$table] );
@@ -161,9 +171,9 @@ class BacklinkCache {
         * Returns an array giving the start and end of each range. The first batch has
         * a start of false, and the last batch has an end of false.
         *
-        * @param string $table The links table name
-        * @param integer $batchSize
-        * @return array
+        * @param $table String: the links table name
+        * @param $batchSize Integer
+        * @return Array
         */
        public function partition( $table, $batchSize ) {
                // Try cache
@@ -223,6 +233,12 @@ class BacklinkCache {
                                $row = $res->fetchObject();
                                $end = $row->page_id - 1;
                        }
+
+                       # Sanity check order
+                       if ( $start && $end && $start > $end ) {
+                               throw new MWException( __METHOD__.': Internal error: query result out of order' );
+                       }
+
                        $batches[] = array( $start, $end );
                }
                return array( 'numRows' => $numRows, 'batches' => $batches );