* (bug 1754) Patch by Anders Wegge Jakobsen: Both Atom and RSS were using the
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index 4e15ce8..c161a33 100644 (file)
@@ -1,25 +1,41 @@
 <?php
-# See deferred.doc
+/**
+ * See deferred.txt
+ * @package MediaWiki
+ */
 
+/**
+ * @todo document
+ * @package MediaWiki
+ */
 class LinksUpdate {
 
-       /* private */ var $mId, $mTitle;
+       /**#@+
+        * @access private
+        */
+       var $mId, $mTitle;
+       /**#@-*/
 
-       function LinksUpdate( $id, $title )
-       {
+       /**
+        * Constructor
+        * Initialize private variables
+        * @param integer $id
+        * @param string $title
+        */
+       function LinksUpdate( $id, $title ) {
                $this->mId = $id;
                $this->mTitle = $title;
        }
 
+       /**
+        * Update link tables with outgoing links from an updated article
+        * Relies on the 'link cache' to be filled out.
+        */
        
-       function doUpdate()
-       {
+       function doUpdate() {
                global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
                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';
                wfProfileIn( $fname );
 
@@ -51,17 +67,16 @@ class LinksUpdate {
                }
 
                # Do the insertion
-               $sql = '';
                if ( 0 != count( $add ) ) {
                        $arr=array();
                        foreach($add as $lt=>$lid) 
-                               array_push($arr,array(
-                                                       'l_from'=>$this->mId,
-                                                       'l_to'=>$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'));
+                       $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
                }
 
                #------------------------------------------------------------------------------
@@ -94,15 +109,13 @@ class LinksUpdate {
                # Do additions
                $sql = '';
                if ( 0 != count ( $add ) ) {
-                       $arr=array();
+                       $arr = array();
                        foreach( $add as $blt ) {
-                               $blt = $dbw->strencode( $blt );
-                               array_push($arr,array(
-                                       'bl_from'=>$this->mId,
-                                       'bl_to'=>$blt));
+                               array_push( $arr, array(
+                                       'bl_from' => $this->mId,
+                                       'bl_to'   => $blt ) );
                        }
-                       $dbw->insertArray( 'brokenlinks',$arr,array('IGNORE'));
-                       $dbw->query( $sql, $fname );
+                       $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
                }
 
                #------------------------------------------------------------------------------
@@ -115,25 +128,18 @@ class LinksUpdate {
                
                # Do the insertion
                $sql = '';
-               $image = Namespace::getImage();
+               $image = NS_IMAGE;
                if ( 0 != count ( $add ) ) {
-                       $sql = "INSERT IGNORE 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 = $dbw->strencode( $iname );
-                               if ( ! $first ) { $sql .= ','; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$iname}')";
+                               array_push( $arr, array(
+                                       'il_from' => $this->mId,
+                                       'il_to'   => $iname ) );
                        }
-               }
-               if ( '' != $sql ) { 
-                       $dbw->query( $sql, $fname ); 
+                       $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
                }
 
                #------------------------------------------------------------------------------
@@ -148,23 +154,17 @@ class LinksUpdate {
                        # Do the insertion
                        $sql = '';
                        if ( 0 != count ( $add ) ) {
-                               $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
-                               $first = true;
+                               $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();
-       
-                                       if ( ! $first ) { $sql .= ','; }
-                                       $first = false;
-       
-                                       $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
-                                               "','" . $dbw->strencode( $sortkey ) . "')";
+                                       array_push( $arr, array(
+                                               'cl_from'    => $this->mId,
+                                               'cl_to'      => $cname,
+                                               'cl_sortkey' => $sortkey ) );
                                }
-                       }
-                       if ( '' != $sql ) {
-                               $dbw->query( $sql, $fname ); 
+                               $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
                        }
                }
                
@@ -172,11 +172,13 @@ class LinksUpdate {
 
                wfProfileOut( $fname );
        }
-       
-       function doDumbUpdate()
-       {
-               # Old inefficient update function
-               # Used for rebuilding the link table
+
+       /**
+         * Link update which clears the previous entries and inserts new ones
+         * May be slower or faster depending on level of lock contention and write speed of DB
+         * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
+        */
+       function doDumbUpdate() {
                global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
                $fname = 'LinksUpdate::doDumbUpdate';
                wfProfileIn( $fname );
@@ -192,39 +194,28 @@ class LinksUpdate {
                $dbw->query( $sql, $fname );
 
                $a = $wgLinkCache->getGoodLinks();
-               $sql = '';
                if ( 0 != count( $a ) ) {
-                       $sql = "INSERT IGNORE 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 ) );
                        }
-               }
-               if ( '' != $sql ) { 
-                       $dbw->query( $sql, $fname ); 
+                       $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
                }
 
                $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
                $dbw->query( $sql, $fname );
 
                $a = $wgLinkCache->getBadLinks();
-               $sql = '';
                if ( 0 != count ( $a ) ) {
-                       $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
-                       $first = true;
+                       $arr = array();
                        foreach( $a as $blt ) {
-                               $blt = $dbw->strencode( $blt );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$blt}')";
+                               array_push($arr,array(
+                                       'bl_from' => $this->mId,
+                                       'bl_to'   => $blt));
                        }
-               }
-               if ( '' != $sql ) { 
-                       $dbw->query( $sql, $fname ); 
+                       $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
                }
                
                $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
@@ -233,18 +224,12 @@ class LinksUpdate {
                $a = $wgLinkCache->getImageLinks();
                $sql = '';
                if ( 0 != count ( $a ) ) {
-                       $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
-                       $first = true;
-                       foreach( $a as $iname => $val ) {
-                               $iname = $dbw->strencode( $iname );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$iname}')";
-                       }
-               }
-               if ( '' != $sql ) { 
-                       $dbw->query( $sql, $fname ); 
+                       $arr = array();
+                       foreach( $a as $iname => $val )
+                               array_push( $arr, array(
+                                       'il_from' => $this->mId,
+                                       'il_to'   => $iname ) );
+                       $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
                }
 
                if( $wgUseCategoryMagic ) {
@@ -257,61 +242,58 @@ class LinksUpdate {
                        # Do the insertion
                        $sql = '';
                        if ( 0 != count ( $add ) ) {
-                               $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
-                               $first = true;
+                               $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();
-       
-                                       if ( ! $first ) { $sql .= ","; }
-                                       $first = false;
-       
-                                       $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
-                                               "','" . $dbw->strencode( $sortkey ) . "')";
+                                        if( !$nt ) continue;
+                                        $nt->invalidateCache();
+                                       array_push( $arr, array(
+                                               'cl_from'    => $this->mId,
+                                               'cl_to'      => $cname,
+                                               'cl_sortkey' => $sortkey ) );
                                }
-                       }
-                       if ( '' != $sql ) { 
-                               $dbw->query( $sql, $fname ); 
+                               $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
                        }
                }
                $this->fixBrokenLinks();
                wfProfileOut( $fname );
        }
-       
+
+       /**
+        * Update any brokenlinks *to* this page
+        * Call for a newly created page, or just to make sure state is consistent
+        */
        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';
 
                $dbw =& wfGetDB( DB_MASTER );
-               $cur = $dbw->tableName( 'cur' );
+               $page = $dbw->tableName( 'page' );
                $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
-               $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
-               $now = wfTimestampNow();
-               $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
+               $arr=array();
+               $now = $dbw->timestamp();
+               $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
                $first = true;
                while ( $row = $dbw->fetchObject( $res ) ) {
-                       if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
+                       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 .= ')';
-               $dbw->query( $sql, $fname );
+               
+               # 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
+               $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
                $dbw->query( $sql2, $fname );
-
                $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
        }
 }
-
-?>
+?>
\ No newline at end of file