only get the title string if action is not empty
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index bf181ad..fe39d36 100644 (file)
@@ -1,4 +1,4 @@
-<?
+<?php
 # See deferred.doc
 
 class LinksUpdate {
@@ -9,22 +9,155 @@ class LinksUpdate {
        {
                $this->mId = $id;
                $this->mTitle = $title;
+               $this->mTitleEnc = wfStrencode( $title );
        }
 
+       
        function doUpdate()
        {
-               global $wgLinkCache, $wgDBtransactions;
+               global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
+               global $wgEnablePersistentLC;
+
+               /* Update link tables with outgoing links from an updated article */
+               /* Relies on the 'link cache' to be filled out */
+
                $fname = "LinksUpdate::doUpdate";
                wfProfileIn( $fname );
-               $t = wfStrencode( $this->mTitle );
+
+               $del = array();
+               $add = array();
+
+               if( $wgDBtransactions ) {
+                       $sql = "BEGIN";
+                       wfQuery( $sql, DB_WRITE, $fname );
+               }
+               
+               #------------------------------------------------------------------------------
+               # Good links
+
+               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 );
+                       }
+               } else {
+                       # Delete everything
+                       $sql = "DELETE FROM links WHERE l_from={$this->mId}";
+                       wfQuery( $sql, DB_WRITE, $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 ); 
+               }
+
+               #------------------------------------------------------------------------------
+               # 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( "','", array_map( "wfStrencode", $del ) ) . "')";
+                               wfQuery( $sql, DB_WRITE, $fname );
+                       }
+               } 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 );
+               }
+
+               #------------------------------------------------------------------------------
+               # Image links
+               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
+               wfQuery( $sql, DB_WRITE, $fname );
+               
+               # Get addition list
+               $add = $wgLinkCache->getImageLinks();
+               
+               # Do the insertion
+               $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
+                               $nt = Title::makeTitle( $image, $iname );
+                               if( !$nt ) continue;
+                               $nt->invalidateCache();
+
+                               $iname = wfStrencode( $iname );
+                               if ( ! $first ) { $sql .= ","; }
+                               $first = false;
+
+                               $sql .= "({$this->mId},'{$iname}')";
+                       }
+               }
+               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
+
+               $this->fixBrokenLinks();
+
+               if( $wgDBtransactions ) {
+                       $sql = "COMMIT";
+                       wfQuery( $sql, DB_WRITE, $fname );
+               }
+               wfProfileOut( $fname );
+       }
+       
+       function doDumbUpdate()
+       {
+               # Old inefficient update function
+               # Used for rebuilding the link table
+               
+               global $wgLinkCache, $wgDBtransactions;
+               $fname = "LinksUpdate::doDumbUpdate";
+               wfProfileIn( $fname );
 
                if( $wgDBtransactions ) {
                        $sql = "BEGIN";
-                       wfQuery( $sql, $fname );
+                       wfQuery( $sql, DB_WRITE, $fname );
                }
                
-               $sql = "DELETE FROM links WHERE l_from='{$t}'";
-               wfQuery( $sql, $fname );
+               $sql = "DELETE FROM links WHERE l_from={$this->mId}";
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $a = $wgLinkCache->getGoodLinks();
                $sql = "";
@@ -35,13 +168,13 @@ class LinksUpdate {
                                if ( ! $first ) { $sql .= ","; }
                                $first = false;
 
-                               $sql .= "('{$t}',{$lid})";
+                               $sql .= "({$this->mId},{$lid})";
                        }
                }
-               if ( "" != $sql ) { wfQuery( $sql, $fname ); }
+               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
 
                $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $a = $wgLinkCache->getBadLinks();
                $sql = "";
@@ -56,10 +189,10 @@ class LinksUpdate {
                                $sql .= "({$this->mId},'{$blt}')";
                        }
                }
-               if ( "" != $sql ) { wfQuery( $sql, $fname ); }
-
-               $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
-               wfQuery( $sql, $fname );
+               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
+               
+               $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $a = $wgLinkCache->getImageLinks();
                $sql = "";
@@ -71,13 +204,27 @@ class LinksUpdate {
                                if ( ! $first ) { $sql .= ","; }
                                $first = false;
 
-                               $sql .= "('{$t}','{$iname}')";
+                               $sql .= "({$this->mId},'{$iname}')";
                        }
                }
-               if ( "" != $sql ) { wfQuery( $sql, $fname ); }
+               if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
+
+               $this->fixBrokenLinks();
 
-               $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$t}'";
-               $res = wfQuery( $sql, $fname );
+               if( $wgDBtransactions ) {
+                       $sql = "COMMIT";
+                       wfQuery( $sql, DB_WRITE, $fname );
+               }
+               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; }
 
                $sql = "INSERT INTO links (l_from,l_to) VALUES ";
@@ -87,24 +234,18 @@ class LinksUpdate {
                while ( $row = wfFetchObject( $res ) ) {
                        if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
                        $first = false;
-                       $nl = wfStrencode( Article::nameOf( $row->bl_from ) );
 
-                       $sql .= "('{$nl}',{$this->mId})";
+                       $sql .= "({$row->bl_from},{$this->mId})";
                        $sql2 .= $row->bl_from;
                }
                $sql2 .= ")";
-               wfQuery( $sql, $fname );
-               wfQuery( $sql2, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
+               wfQuery( $sql2, DB_WRITE, $fname );
 
-               $sql = "DELETE FROM brokenlinks WHERE bl_to='{$t}'";
-               wfQuery( $sql, $fname );
-
-               if( $wgDBtransactions ) {
-                       $sql = "COMMIT";
-                       wfQuery( $sql, $fname );
-               }
-               wfProfileOut();
+               $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
+               wfQuery( $sql, DB_WRITE, $fname );
        }
+       
 }
 
 ?>