Modification to LinkBatch
[lhc/web/wiklou.git] / includes / LinkCache.php
index 9da6807..4a57909 100644 (file)
@@ -147,7 +147,7 @@ class LinkCache {
                                $options = array();
                        }
 
-                       $id = $db->getField( 'cur', 'cur_id', array( 'cur_namespace' => $ns, 'cur_title' => $t ), $fname, $options );
+                       $id = $db->selectField( 'page', 'page_id', array( 'page_namespace' => $ns, 'page_title' => $t ), $fname, $options );
                        if ( !$id ) {
                                $id = 0;
                        }
@@ -192,16 +192,16 @@ class LinkCache {
                        $options = '';
                }
 
-               $cur = $db->tableName( 'cur' );
+               $page = $db->tableName( 'page' );
                $links = $db->tableName( 'links' );
 
-               $sql = "SELECT cur_id,cur_namespace,cur_title
-                       FROM $cur,$links
-                       WHERE cur_id=l_to AND l_from=$id $options";
+               $sql = "SELECT page_id,page_namespace,page_title
+                       FROM $page,$links
+                       WHERE page_id=l_to AND l_from=$id $options";
                $res = $db->query( $sql, $fname );
                while( $s = $db->fetchObject( $res ) ) {
-                       $this->addGoodLink( $s->cur_id,
-                               Title::makeName( $s->cur_namespace, $s->cur_title )
+                       $this->addGoodLink( $s->page_id,
+                               Title::makeName( $s->page_namespace, $s->page_title )
                                );
                }
                
@@ -304,7 +304,7 @@ class LinkCache {
                        $db =& wfGetDB( DB_SLAVE );
                        $options = '';
                }
-               $raw = $db->getField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
+               $raw = $db->selectField( 'linkscc', 'lcc_cacheobj', array( 'lcc_pageid' => $id ), $fname, $options );
                if ( $raw === false ) {
                        return false;
                }
@@ -389,4 +389,98 @@ class LinkCache {
                }
        }
 }
+
+/**
+ * Class representing a list of titles
+ * The execute() method checks them all for existence and adds them to a LinkCache object
+ */
+class LinkBatch {
+       /** 
+        * 2-d array, first index namespace, second index dbkey, value arbitrary
+        */
+       var $data = array();
+
+       function addObj( $title ) {
+               $this->add( $title->getNamespace(), $title->getDBkey() );
+       }
+
+       function add( $ns, $dbkey ) {
+               if ( $ns < 0 ) {
+                       return;
+               }
+               if ( !array_key_exists( $ns, $this->data ) ) {
+                       $this->data[$ns] = array();
+               }
+
+               $this->data[$ns][$dbkey] = 1;
+       }
+
+       function execute( &$cache ) {
+               $fname = 'LinkBatch::execute';
+               $namespaces = array();
+
+               if ( !count( $this->data ) ) {
+                       return;
+               }
+
+               wfProfileIn( $fname );
+
+               // Construct query
+               // This is very similar to Parser::replaceLinkHolders
+               $dbr = wfGetDB( DB_SLAVE );
+               $page = $dbr->tableName( 'page' );
+               $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE ";
+               $first = true;
+               
+               foreach ( $this->data as $ns => $dbkeys ) {
+                       if ( !count( $dbkeys ) ) {
+                               continue;
+                       }
+
+                       if ( $first ) {
+                               $first = false;
+                       } else {
+                               $sql .= ' OR ';
+                       }
+                       $sql .= "(page_namespace=$ns AND page_title IN (";
+
+                       $firstTitle = true;
+                       foreach( $dbkeys as $dbkey => $nothing ) {
+                               if ( $firstTitle ) {
+                                       $firstTitle = false;
+                               } else {
+                                       $sql .= ',';
+                               }
+                               $sql .= $dbr->addQuotes( $dbkey );
+                       }
+
+                       $sql .= '))';
+               }
+               
+               // Do query
+               $res = $dbr->query( $sql, $fname );
+
+               // Process results
+               // For each returned entry, add it to the list of good links, and remove it from $remaining
+
+               $remaining = $this->data;
+               while ( $row = $dbr->fetchObject( $res ) ) {
+                       $title = Title::makeTitle( $row->page_namespace, $row->page_title );
+                       $cache->addGoodLink( $row->page_id, $title->getPrefixedDBkey() );
+                       unset( $remaining[$row->page_namespace][$row->page_title] );
+               }
+               $dbr->freeResult( $res );
+
+               // The remaining links in $data are bad links, register them as such
+               foreach ( $remaining as $ns => $dbkeys ) {
+                       foreach ( $dbkeys as $dbkey => $nothing ) {
+                               $title = Title::makeTitle( $ns, $dbkey );
+                               $cache->addBadLink( $title->getPrefixedText() );
+                       }
+               }
+
+               wfProfileOut( $fname );
+       }
+}
+
 ?>