Corrected spelling error
[lhc/web/wiklou.git] / includes / LinksUpdate.php
index de4b861..104e442 100644 (file)
@@ -1,4 +1,4 @@
-<?
+<?php
 # See deferred.doc
 
 class LinksUpdate {
@@ -12,18 +12,15 @@ class LinksUpdate {
                $this->mTitleEnc = wfStrencode( $title );
        }
 
+       
        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 */
 
-               if ( !$wgUseBetterLinksUpdate ) {
-                       $this->doDumbUpdate();
-                       return;
-               }
-
                $fname = "LinksUpdate::doUpdate";
                wfProfileIn( $fname );
 
@@ -41,13 +38,13 @@ class LinksUpdate {
                if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
                        # Delete where necessary
                        if ( count( $del ) ) {
-                               $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}' AND l_to IN(".
+                               $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->mTitleEnc}'";
+                       $sql = "DELETE FROM links WHERE l_from={$this->mId}";
                        wfQuery( $sql, DB_WRITE, $fname );
                        
                        # Get the addition list
@@ -64,7 +61,7 @@ class LinksUpdate {
                                if ( ! $first ) { $sql .= ","; }
                                $first = false;
 
-                               $sql .= "('{$this->mTitleEnc}',{$lid})";
+                               $sql .= "({$this->mId},{$lid})";
                        }
                }
                if ( "" != $sql ) { 
@@ -78,7 +75,7 @@ class LinksUpdate {
                        # Delete where necessary
                        if ( count( $del ) ) {
                                $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .    
-                                       implode( "','", $del ) . "')";
+                                       implode( "','", array_map( "wfStrencode", $del ) ) . "')";
                                wfQuery( $sql, DB_WRITE, $fname );
                        }
                } else {
@@ -109,7 +106,7 @@ class LinksUpdate {
 
                #------------------------------------------------------------------------------
                # Image links
-               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
+               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
                wfQuery( $sql, DB_WRITE, $fname );
                
                # Get addition list
@@ -117,33 +114,70 @@ class LinksUpdate {
                
                # 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->mTitleEnc}','{$iname}')";
+                               $sql .= "({$this->mId},'{$iname}')";
                        }
                }
                if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
 
+               #------------------------------------------------------------------------------
+               # Category links
+               if( $wgUseCategoryMagic ) {
+                       $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
+                       wfQuery( $sql, DB_WRITE, $fname );
+                       
+                       # Get addition list
+                       $add = $wgLinkCache->getCategoryLinks();
+                       
+                       # Do the insertion
+                       $sql = "";
+                       if ( 0 != count ( $add ) ) {
+                               $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
+                               $first = true;
+                               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},'" . wfStrencode( $cname ) .
+                                               "','" . wfStrencode( $sortkey ) . "')";
+                               }
+                       }
+                       if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
+               }
+               
                $this->fixBrokenLinks();
 
                if( $wgDBtransactions ) {
                        $sql = "COMMIT";
                        wfQuery( $sql, DB_WRITE, $fname );
                }
-               wfProfileOut();
+               wfProfileOut( $fname );
        }
-
+       
        function doDumbUpdate()
        {
-               # Old update function. This can probably be removed eventually, if the new one
-               # proves to be stable
-               global $wgLinkCache, $wgDBtransactions;
+               # Old inefficient update function
+               # Used for rebuilding the link table
+               
+               global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
                $fname = "LinksUpdate::doDumbUpdate";
                wfProfileIn( $fname );
 
@@ -152,7 +186,7 @@ class LinksUpdate {
                        wfQuery( $sql, DB_WRITE, $fname );
                }
                
-               $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
+               $sql = "DELETE FROM links WHERE l_from={$this->mId}";
                wfQuery( $sql, DB_WRITE, $fname );
 
                $a = $wgLinkCache->getGoodLinks();
@@ -164,7 +198,7 @@ class LinksUpdate {
                                if ( ! $first ) { $sql .= ","; }
                                $first = false;
 
-                               $sql .= "('{$this->mTitleEnc}',{$lid})";
+                               $sql .= "({$this->mId},{$lid})";
                        }
                }
                if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
@@ -187,7 +221,7 @@ class LinksUpdate {
                }
                if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
                
-               $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
+               $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
                wfQuery( $sql, DB_WRITE, $fname );
 
                $a = $wgLinkCache->getImageLinks();
@@ -200,23 +234,51 @@ class LinksUpdate {
                                if ( ! $first ) { $sql .= ","; }
                                $first = false;
 
-                               $sql .= "('{$this->mTitleEnc}','{$iname}')";
+                               $sql .= "({$this->mId},'{$iname}')";
                        }
                }
                if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
 
+               if( $wgUseCategoryMagic ) {
+                       $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
+                       wfQuery( $sql, DB_WRITE, $fname );
+                       
+                       # Get addition list
+                       $add = $wgLinkCache->getCategoryLinks();
+                       
+                       # Do the insertion
+                       $sql = "";
+                       if ( 0 != count ( $add ) ) {
+                               $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
+                               $first = true;
+                               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},'" . wfStrencode( $cname ) .
+                                               "','" . wfStrencode( $sortkey ) . "')";
+                               }
+                       }
+                       if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
+               }
                $this->fixBrokenLinks();
 
                if( $wgDBtransactions ) {
                        $sql = "COMMIT";
                        wfQuery( $sql, DB_WRITE, $fname );
                }
-               wfProfileOut();
+               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 );
@@ -229,9 +291,8 @@ class LinksUpdate {
                while ( $row = wfFetchObject( $res ) ) {
                        if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
                        $first = false;
-                       $nl = wfStrencode( Title::nameOf( $row->bl_from ) );
 
-                       $sql .= "('{$nl}',{$this->mId})";
+                       $sql .= "({$row->bl_from},{$this->mId})";
                        $sql2 .= $row->bl_from;
                }
                $sql2 .= ")";