Removed hardcodes for Zh-conversion
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index fe39d36..aaf0495 100644 (file)
@@ -1,6 +1,13 @@
 <?php
-# See deferred.doc
-
+/**
+ * See deferred.doc
+ * @package MediaWiki
+ */
+
+/**
+ * @todo document
+ * @package MediaWiki
+ */
 class LinksUpdate {
 
        /* private */ var $mId, $mTitle;
@@ -9,28 +16,28 @@ class LinksUpdate {
        {
                $this->mId = $id;
                $this->mTitle = $title;
-               $this->mTitleEnc = wfStrencode( $title );
        }
 
        
        function doUpdate()
        {
                global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
-               global $wgEnablePersistentLC;
+               global $wgEnablePersistentLC, $wgUseCategoryMagic;
 
                /* Update link tables with outgoing links from an updated article */
                /* Relies on the 'link cache' to be filled out */
 
-               $fname = "LinksUpdate::doUpdate";
+               $fname = 'LinksUpdate::doUpdate';
                wfProfileIn( $fname );
 
                $del = array();
                $add = array();
 
-               if( $wgDBtransactions ) {
-                       $sql = "BEGIN";
-                       wfQuery( $sql, DB_WRITE, $fname );
-               }
+               $dbw =& wfGetDB( DB_MASTER );
+               $links = $dbw->tableName( 'links' );
+               $brokenlinks = $dbw->tableName( 'brokenlinks' );
+               $imagelinks = $dbw->tableName( 'imagelinks' );
+               $categorylinks = $dbw->tableName( 'categorylinks' );
                
                #------------------------------------------------------------------------------
                # Good links
@@ -38,34 +45,29 @@ class LinksUpdate {
                if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
                        # Delete where necessary
                        if ( count( $del ) ) {
-                               $sql = "DELETE FROM links WHERE l_from={$this->mId} AND l_to IN(".
-                                       implode( ",", $del ) . ")";
-                               wfQuery( $sql, DB_WRITE, $fname );
+                               $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
+                                       implode( ',', $del ) . ')';
+                               $dbw->query( $sql, $fname );
                        }
                } else {
                        # Delete everything
-                       $sql = "DELETE FROM links WHERE l_from={$this->mId}";
-                       wfQuery( $sql, DB_WRITE, $fname );
-                       
+                       $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
+                                               
                        # Get the addition list
                        $add = $wgLinkCache->getGoodLinks();
                }
 
                # Do the insertion
-               $sql = "";
                if ( 0 != count( $add ) ) {
-                       $sql = "INSERT INTO links (l_from,l_to) VALUES ";
-                       $first = true;
-                       foreach( $add as $lt => $lid ) {
-                               
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},{$lid})";
-                       }
-               }
-               if ( "" != $sql ) { 
-                       wfQuery( $sql, DB_WRITE, $fname ); 
+                       $arr=array();
+                       foreach($add as $lt=>$lid) 
+                               array_push($arr,array(
+                                                       'l_from'=>$this->mId,
+                                                       'l_to'=>$lid));
+                       # The link cache was constructed without FOR UPDATE, so there may be collisions
+                       # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
+                       # sure it's better than without IGNORE
+                       $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
                }
 
                #------------------------------------------------------------------------------
@@ -74,71 +76,93 @@ class LinksUpdate {
                if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
                        # Delete where necessary
                        if ( count( $del ) ) {
-                               $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .    
-                                       implode( "','", array_map( "wfStrencode", $del ) ) . "')";
-                               wfQuery( $sql, DB_WRITE, $fname );
+                               $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
+                               $first = true;
+                               foreach( $del as $badTitle ) {
+                                       if ( $first ) {
+                                               $first = false;
+                                       } else {
+                                               $sql .= ',';
+                                       }
+                                       $sql .= $dbw->addQuotes( $badTitle );
+                               }
+                               $sql .= ')';
+                               $dbw->query( $sql, $fname );
                        }
                } else {
                        # Delete all
-                       $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
-                       wfQuery( $sql, DB_WRITE, $fname );
+                       $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
                        
                        # Get addition list
                        $add = $wgLinkCache->getBadLinks();
                }
 
                # Do additions
-               $sql = "";
+               $sql = '';
                if ( 0 != count ( $add ) ) {
-                       $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
-                       $first = true;
+                       $arr=array();
                        foreach( $add as $blt ) {
-                               $blt = wfStrencode( $blt );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$blt}')";
+                               $blt = $dbw->strencode( $blt );
+                               array_push($arr,array(
+                                       'bl_from'=>$this->mId,
+                                       'bl_to'=>$blt));
                        }
-               }
-               if ( "" != $sql ) { 
-                       wfQuery( $sql, DB_WRITE, $fname );
+                       $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
                }
 
                #------------------------------------------------------------------------------
                # Image links
-               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
-               wfQuery( $sql, DB_WRITE, $fname );
+               $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
+               $dbw->query( $sql, $fname );
                
                # Get addition list
                $add = $wgLinkCache->getImageLinks();
                
                # Do the insertion
-               $sql = "";
+               $sql = '';
                $image = Namespace::getImage();
                if ( 0 != count ( $add ) ) {
-                       $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
-                       $first = true;
-                       foreach( $add as $iname => $val ) {
-                               # FIXME: Change all this to avoid unnecessary duplication
+                       $arr = array();
+                       foreach ($add as $iname => $val ) {
                                $nt = Title::makeTitle( $image, $iname );
                                if( !$nt ) continue;
                                $nt->invalidateCache();
-
-                               $iname = wfStrencode( $iname );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$iname}')";
+                               $iname = $dbw->strencode( $iname );
+                               array_push($arr,array(
+                                       'il_from'=>$this->mId,
+                                       'il_to'=>$iname));
                        }
+                       $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
                }
-               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
 
+               #------------------------------------------------------------------------------
+               # Category links
+               if( $wgUseCategoryMagic ) {
+                       $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
+                       $dbw->query( $sql, $fname );
+                       
+                       # Get addition list
+                       $add = $wgLinkCache->getCategoryLinks();
+                       
+                       # Do the insertion
+                       $sql = '';
+                       if ( 0 != count ( $add ) ) {
+                               $arr=array();
+                               foreach( $add as $cname => $sortkey ) {
+                                       $nt = Title::makeTitle( NS_CATEGORY, $cname );
+                                        if( !$nt ) continue;
+                                        $nt->invalidateCache();
+                                       array_push($arr,array(
+                                               'cl_from'=>$this->mId,
+                                               'cl_to'=>$dbw->strencode( $cname ),
+                                               'cl_sortkey'=>$dbw->strencode( $sortkey )));
+                               }
+                               $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
+                       }
+               }
+               
                $this->fixBrokenLinks();
 
-               if( $wgDBtransactions ) {
-                       $sql = "COMMIT";
-                       wfQuery( $sql, DB_WRITE, $fname );
-               }
                wfProfileOut( $fname );
        }
        
@@ -146,106 +170,118 @@ class LinksUpdate {
        {
                # Old inefficient update function
                # Used for rebuilding the link table
-               
-               global $wgLinkCache, $wgDBtransactions;
-               $fname = "LinksUpdate::doDumbUpdate";
+               global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
+               $fname = 'LinksUpdate::doDumbUpdate';
                wfProfileIn( $fname );
-
-               if( $wgDBtransactions ) {
-                       $sql = "BEGIN";
-                       wfQuery( $sql, DB_WRITE, $fname );
-               }
                
-               $sql = "DELETE FROM links WHERE l_from={$this->mId}";
-               wfQuery( $sql, DB_WRITE, $fname );
+               
+               $dbw =& wfGetDB( DB_MASTER );
+               $links = $dbw->tableName( 'links' );
+               $brokenlinks = $dbw->tableName( 'brokenlinks' );
+               $imagelinks = $dbw->tableName( 'imagelinks' );
+               $categorylinks = $dbw->tableName( 'categorylinks' );
+               
+               $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
+               $dbw->query( $sql, $fname );
 
                $a = $wgLinkCache->getGoodLinks();
-               $sql = "";
                if ( 0 != count( $a ) ) {
-                       $sql = "INSERT INTO links (l_from,l_to) VALUES ";
-                       $first = true;
+                       $arr=array();
                        foreach( $a as $lt => $lid ) {
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},{$lid})";
+                               array_push($arr,array(
+                                       'l_from'=>$this->mId,
+                                       'l_to'=>$lid));
                        }
+                       $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
                }
-               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
 
-               $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
-               wfQuery( $sql, DB_WRITE, $fname );
+               $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
+               $dbw->query( $sql, $fname );
 
                $a = $wgLinkCache->getBadLinks();
-               $sql = "";
                if ( 0 != count ( $a ) ) {
-                       $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
-                       $first = true;
+                       $arr=array();
                        foreach( $a as $blt ) {
-                               $blt = wfStrencode( $blt );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$blt}')";
+                               $blt = $dbw->strencode( $blt );
+                               array_push($arr,array(
+                                       'bl_from'=>$this->mId,
+                                       'bl_to'=>$blt));
                        }
+                       $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
                }
-               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
                
-               $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
-               wfQuery( $sql, DB_WRITE, $fname );
+               $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
+               $dbw->query( $sql, $fname );
 
                $a = $wgLinkCache->getImageLinks();
-               $sql = "";
+               $sql = '';
                if ( 0 != count ( $a ) ) {
-                       $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
-                       $first = true;
-                       foreach( $a as $iname => $val ) {
-                               $iname = wfStrencode( $iname );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$iname}')";
-                       }
+                       $arr=array();
+                       foreach( $a as $iname => $val )
+                               array_push($arr,array(
+                                       'il_from'=>$this->mId,
+                                       'il_to'=>$dbw->strencode( $iname )));
+                       $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
                }
-               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
-
-               $this->fixBrokenLinks();
 
-               if( $wgDBtransactions ) {
-                       $sql = "COMMIT";
-                       wfQuery( $sql, DB_WRITE, $fname );
+               if( $wgUseCategoryMagic ) {
+                       $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
+                       $dbw->query( $sql, $fname );
+                       
+                       # Get addition list
+                       $add = $wgLinkCache->getCategoryLinks();
+                       
+                       # Do the insertion
+                       $sql = '';
+                       if ( 0 != count ( $add ) ) {
+                               $arr=array();
+                               foreach( $add as $cname => $sortkey ) {
+                                       # FIXME: Change all this to avoid unnecessary duplication
+                                       $nt = Title::makeTitle( NS_CATEGORY, $cname );
+                                        if( !$nt ) continue;
+                                        $nt->invalidateCache();
+                                       array_push($arr,array(
+                                               'cl_from'=>$this->mId,
+                                               'cl_to'=>$dbw->strencode( $cname ),
+                                               'cl_sortkey'=>$dbw->strencode( $sortkey )));
+                               }
+                               $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
+                       }
                }
+               $this->fixBrokenLinks();
                wfProfileOut( $fname );
        }
        
        function fixBrokenLinks() {
                /* Update any brokenlinks *to* this page */
                /* Call for a newly created page, or just to make sure state is consistent */
-               $fname = "LinksUpdate::fixBrokenLinks";
-               
-               $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
-               $res = wfQuery( $sql, DB_READ, $fname );
-               if ( 0 == wfNumRows( $res ) ) { return; }
+               $fname = 'LinksUpdate::fixBrokenLinks';
 
-               $sql = "INSERT INTO links (l_from,l_to) VALUES ";
-               $now = wfTimestampNow();
-               $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
+               $dbw =& wfGetDB( DB_MASTER );
+               $cur = $dbw->tableName( 'cur' );
+               $links = $dbw->tableName( 'links' );
+               
+               $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ), 
+                 $fname, 'FOR UPDATE' );
+               if ( 0 == $dbw->numRows( $res ) ) { return; }
+
+               # Ignore errors. If a link existed in both the brokenlinks table and the links 
+               # table, that's an error which can be fixed at this stage by simply ignoring collisions
+               $arr=array();
+               $now = $dbw->timestamp();
+               $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
                $first = true;
-               while ( $row = wfFetchObject( $res ) ) {
-                       if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
+               while ( $row = $dbw->fetchObject( $res ) ) {
+                       if ( ! $first ) { $sql2 .= ","; }
                        $first = false;
-
-                       $sql .= "({$row->bl_from},{$this->mId})";
+                       array_push($arr,array('l_from'=>$row->bl_from,'l_to'=>$this->mId));
                        $sql2 .= $row->bl_from;
                }
-               $sql2 .= ")";
-               wfQuery( $sql, DB_WRITE, $fname );
-               wfQuery( $sql2, DB_WRITE, $fname );
-
-               $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
-               wfQuery( $sql, DB_WRITE, $fname );
+               $sql2 .= ')';
+               $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
+               $dbw->query( $sql2, $fname );
+               $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
        }
-       
 }
 
 ?>