* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index 360680f..30b1b50 100644 (file)
-<?
-# See deferred.doc
-
+<?php
+/**
+ * See deferred.txt
+ * @package MediaWiki
+ */
+
+/**
+ * @todo document
+ * @package MediaWiki
+ */
 class LinksUpdate {
 
-       /* private */ var $mId, $mTitle;
-
-       function LinksUpdate( $id, $title )
-       {
+       /**#@+
+        * @access private
+        */
+       var $mId, $mTitle;
+       /**#@-*/
+
+       /**
+        * Constructor
+        * Initialize private variables
+        * @param integer $id
+        * @param string $title
+        */
+       function LinksUpdate( $id, $title ) {
                $this->mId = $id;
                $this->mTitle = $title;
-               $this->mTitleEnc = wfStrencode( $title );
        }
 
+       /**
+        * Update link tables with outgoing links from an updated article
+        * Relies on the 'link cache' to be filled out.
+        */
        
-       function doUpdate()
-       {
-               global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
-               global $wgEnablePersistentLC;
+       function doUpdate() {
+               global $wgUseDumbLinkUpdate, $wgLinkCache, $wgDBtransactions;
+               global $wgUseCategoryMagic;
 
-               /* Update link tables with outgoing links from an updated article */
-               /* Relies on the 'link cache' to be filled out */
-
-               if ( !$wgUseBetterLinksUpdate ) {
+               if ( $wgUseDumbLinkUpdate ) {
                        $this->doDumbUpdate();
                        return;
                }
 
-               $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 );
+               $pagelinks = $dbw->tableName( 'pagelinks' );
+               $imagelinks = $dbw->tableName( 'imagelinks' );
+               $categorylinks = $dbw->tableName( 'categorylinks' );
                
                #------------------------------------------------------------------------------
                # Good links
 
-               if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
+               if ( $wgLinkCache->incrementalSetup( LINKCACHE_PAGE, $del, $add ) ) {
                        # Delete where necessary
                        if ( count( $del ) ) {
-                               $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}' AND l_to IN(".
-                                       implode( ",", $del ) . ")";
-                               wfQuery( $sql, DB_WRITE, $fname );
+                               $batch = new LinkBatch( $del );
+                               $set = $batch->constructSet( 'pl', $dbw );
+                               if ( $set ) {
+                                       $sql = "DELETE FROM $pagelinks WHERE pl_from={$this->mId} AND ($set)";
+                                       $dbw->query( $sql, $fname );
+                               }
                        }
                } else {
                        # Delete everything
-                       $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
-                       wfQuery( $sql, DB_WRITE, $fname );
-                       
+                       $dbw->delete( 'pagelinks', array( 'pl_from' => $this->mId ), $fname );
+                                               
                        # Get the addition list
-                       $add = $wgLinkCache->getGoodLinks();
+                       $add = $wgLinkCache->getPageLinks();
                }
 
                # 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->mTitleEnc}',{$lid})";
-                       }
-               }
-               if ( "" != $sql ) { 
-                       wfQuery( $sql, DB_WRITE, $fname ); 
-               }
-
-               #------------------------------------------------------------------------------
-               # Bad links
-
-               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( "','", $del ) . "')";
-                               wfQuery( $sql, DB_WRITE, $fname );
+               if( 0 != count( $add ) ) {
+                       $arr = array();
+                       foreach( $add as $lt => $target ) {
+                               array_push( $arr, array(
+                                                       'pl_from' => $this->mId,
+                                                       'pl_namespace' => $target->getNamespace(),
+                                                       'pl_title'     => $target->getDbKey() ) );
                        }
-               } else {
-                       # Delete all
-                       $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
-                       wfQuery( $sql, DB_WRITE, $fname );
                        
-                       # Get addition list
-                       $add = $wgLinkCache->getBadLinks();
-               }
-
-               # Do additions
-               $sql = "";
-               if ( 0 != count ( $add ) ) {
-                       $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
-                       $first = true;
-                       foreach( $add as $blt ) {
-                               $blt = wfStrencode( $blt );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$blt}')";
-                       }
-               }
-               if ( "" != $sql ) { 
-                       wfQuery( $sql, DB_WRITE, $fname );
+                       # 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->insert( 'pagelinks', $arr, $fname, array( 'IGNORE' ) );
                }
 
                #------------------------------------------------------------------------------
                # Image links
-               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
-               wfQuery( $sql, DB_WRITE, $fname );
+               $dbw->delete('imagelinks',array('il_from'=>$this->mId),$fname);
                
                # Get addition list
                $add = $wgLinkCache->getImageLinks();
                
                # Do the insertion
-               $sql = "";
-               $image = Namespace::getImage();
+               $sql = '';
+               $image = NS_IMAGE;
                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->mTitleEnc}','{$iname}')";
+                               array_push( $arr, array(
+                                       'il_from' => $this->mId,
+                                       'il_to'   => $iname ) );
                        }
+                       $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
                }
-               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
-
-               $this->fixBrokenLinks();
 
-               if( $wgDBtransactions ) {
-                       $sql = "COMMIT";
-                       wfQuery( $sql, DB_WRITE, $fname );
+               #------------------------------------------------------------------------------
+               # Category links
+               if( $wgUseCategoryMagic ) {
+                       global $messageMemc, $wgDBname;
+                       
+                       # Get addition list
+                       $add = $wgLinkCache->getCategoryLinks();
+                       
+                       # select existing catlinks for this page
+                       $res = $dbw->select( 'categorylinks',
+                               array( 'cl_to', 'cl_sortkey' ),
+                               array( 'cl_from' => $this->mId ), 
+                               $fname,
+                               'FOR UPDATE' );
+
+                       $del = array();
+                       if( 0 != $dbw->numRows( $res ) ) {
+                               while( $row = $dbw->fetchObject( $res ) ) {
+                                       if( !isset( $add[$row->cl_to] ) || $add[$row->cl_to] != $row->cl_sortkey ) {
+                                               // in the db, but no longer in the page
+                                               // or sortkey has changed -> delete
+                                               $del[] = $row->cl_to;
+                                       } else {
+                                               // remove already existing category memberships
+                                               // from the add array
+                                               unset( $add[$row->cl_to] );
+                                       }
+                               }
+                       }
+                       
+                       // delete any removed categorylinks
+                       if( count( $del ) > 0) {
+                               // delete old ones
+                               $dbw->delete( 'categorylinks',
+                                       array(
+                                               'cl_from' => $this->mId,
+                                               'cl_to'   => $del ),
+                                       $fname );
+                               foreach( $del as $cname ){
+                                       $nt = Title::makeTitle( NS_CATEGORY, $cname );
+                                       $nt->invalidateCache();
+                                       // update the timestamp which indicates when the last article
+                                       // was added or removed to/from this article
+                                       $key = $wgDBname . ':Category:' . md5( $nt->getDBkey() ) . ':adddeltimestamp';
+                                       $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
+                               }
+                       }
+                       
+                       // add any new category memberships
+                       if( count( $add ) > 0 ) {
+                               $arr = array();
+                               foreach( $add as $cname => $sortkey ) {
+                                       $nt = Title::makeTitle( NS_CATEGORY, $cname );
+                                       $nt->invalidateCache();
+                                       // update the timestamp which indicates when the last article
+                                       // was added or removed to/from this article
+                                       $key = $wgDBname . ':Category:' . md5( $nt->getDBkey() ) . ':adddeltimestamp';
+                                       $messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
+                                       array_push( $arr, array(
+                                               'cl_from'    => $this->mId,
+                                               'cl_to'      => $cname,
+                                               'cl_sortkey' => $sortkey ) );
+                               }
+                               // do the actual sql insertion
+                               $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
+                       }
                }
+               
                wfProfileOut( $fname );
        }
 
-       function doDumbUpdate()
-       {
-               # Old update function. This can probably be removed eventually, if the new one
-               # proves to be stable
-               global $wgLinkCache, $wgDBtransactions;
-               $fname = "LinksUpdate::doDumbUpdate";
+       /**
+         * 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 );
-
-               if( $wgDBtransactions ) {
-                       $sql = "BEGIN";
-                       wfQuery( $sql, DB_WRITE, $fname );
-               }
                
-               $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
-               wfQuery( $sql, DB_WRITE, $fname );
+               
+               $dbw =& wfGetDB( DB_MASTER );
+               $pagelinks = $dbw->tableName( 'pagelinks' );
+               $imagelinks = $dbw->tableName( 'imagelinks' );
+               $categorylinks = $dbw->tableName( 'categorylinks' );
+               
+               $dbw->delete('pagelinks', array('pl_from'=>$this->mId),$fname);
 
-               $a = $wgLinkCache->getGoodLinks();
-               $sql = "";
+               $a = $wgLinkCache->getPageLinks();
                if ( 0 != count( $a ) ) {
-                       $sql = "INSERT INTO links (l_from,l_to) VALUES ";
-                       $first = true;
-                       foreach( $a as $lt => $lid ) {
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "('{$this->mTitleEnc}',{$lid})";
+                       $arr = array();
+                       foreach( $a as $lt => $target ) {
+                               array_push( $arr, array(
+                                       'pl_from'      => $this->mId,
+                                       'pl_namespace' => $target->getNamespace(),
+                                       'pl_title'     => $target->getDBkey() ) );
                        }
+                       $dbw->insert( 'pagelinks', $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 );
 
-               $a = $wgLinkCache->getBadLinks();
-               $sql = "";
-               if ( 0 != count ( $a ) ) {
-                       $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
-                       $first = true;
-                       foreach( $a as $blt ) {
-                               $blt = wfStrencode( $blt );
-                               if ( ! $first ) { $sql .= ","; }
-                               $first = false;
-
-                               $sql .= "({$this->mId},'{$blt}')";
-                       }
-               }
-               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
-               
-               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
-               wfQuery( $sql, DB_WRITE, $fname );
+               $dbw->delete('imagelinks', array('il_from'=>$this->mId),$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->mTitleEnc}','{$iname}')";
-                       }
+                       $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 ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
 
-               $this->fixBrokenLinks();
-
-               if( $wgDBtransactions ) {
-                       $sql = "COMMIT";
-                       wfQuery( $sql, DB_WRITE, $fname );
+               if( $wgUseCategoryMagic ) {
+                       $dbw->delete('categorylinks', array('cl_from'=>$this->mId),$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'      => $cname,
+                                               'cl_sortkey' => $sortkey ) );
+                               }
+                               $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
+                       }
                }
                wfProfileOut( $fname );
        }
-       
-       function fixBrokenLinks() {
-               /* Update any brokenlinks *to* this page */
-               /* Call for a newly created page, or just to make sure state is consistent */
-               
-               $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
-               $res = wfQuery( $sql, DB_READ, $fname );
-               if ( 0 == wfNumRows( $res ) ) { return; }
-
-               $sql = "INSERT INTO links (l_from,l_to) VALUES ";
-               $now = wfTimestampNow();
-               $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
-               $first = true;
-               while ( $row = wfFetchObject( $res ) ) {
-                       if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
-                       $first = false;
-                       $nl = wfStrencode( Title::nameOf( $row->bl_from ) );
-
-                       $sql .= "('{$nl}',{$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 );
-       }
-       
 }
-
 ?>