Add file renaming for images and archived images, leaving symlinks from old
[lhc/web/wiklou.git] / maintenance / convertUtf8.php
index 8e9c5cc..c8f865e 100644 (file)
@@ -59,9 +59,10 @@ class UtfUpdater {
         * @param string $key Primary key, to identify fields in the UPDATE. If NULL, all fields will be used to match.
         * @param array $fields List of all fields to grab and convert. If null, will assume you want the $key, and will ask for DISTINCT.
         * @param array $timestamp A field which should be updated to the current timestamp on changed records.
+        * @param callable $callback
         * @access private
         */
-       function convertTable( $table, $key, $fields = null, $timestamp = null ) {
+       function convertTable( $table, $key, $fields = null, $timestamp = null, $callback = null ) {
                $fname = 'UtfUpdater::convertTable';
                if( $fields ) {
                        $distinct = '';
@@ -106,11 +107,63 @@ class UtfUpdater {
                                $keyCond,
                                $fname );
                        if( ++$n % 100 == 0 ) echo "$n\n";
+                       
+                       if( is_callable( $callback ) ) {
+                               call_user_func( $callback, $s );
+                       }
                }
                echo "$n done.\n";
                $this->db->freeResult( $res );
        }
        
+       /**
+        * @param object $row
+        * @access private
+        */
+       function imageRenameCallback( $row ) {
+               $this->renameFile( $row->img_name, 'wfImageDir' );
+       }
+       
+       /**
+        * @param object $row
+        * @access private
+        */
+       function oldimageRenameCallback( $row ) {
+               $this->renameFile( $row->oi_archive_name, 'wfImageThumbDir' );
+       }
+       
+       /**
+        * Rename a given image or archived image file to the converted filename,
+        * leaving a symlink for URL compatibility.
+        *
+        * @param string $oldname pre-conversion filename
+        * @param callable $subdirCallback a function to generate hashed directories
+        * @access private
+        */
+       function renameFile( $oldname, $subdirCallback ) {
+               $newname = $this->toUtf8( $oldname );
+               if( $newname == $oldname ) {
+                       // No need to rename; another field triggered this row.
+                       return;
+               }
+               
+               $oldpath = call_user_func( $subdirCallback, $oldname ) . '/' . $oldname;
+               $newpath = call_user_func( $subdirCallback, $newname ) . '/' . $newname;
+               
+               echo "Renaming $oldpath to $newpath... ";
+               if( rename( $oldpath, $newpath ) ) {
+                       echo "ok\n";
+                       echo "Creating compatibility symlink from $newpath to $oldpath... ";
+                       if( symlink( $newpath, $oldpath ) ) {
+                               echo "ok\n";
+                       } else {
+                               echo " symlink failed!\n";
+                       }
+               } else {
+                       echo " rename failed!\n";
+               }
+       }
+       
        /**
         * Lock tables.
         * @param array $tables An array of table to be locked.
@@ -130,13 +183,12 @@ class UtfUpdater {
         */     
        function updateAll() {
                $this->lockTables( array(
-                       'linkscc', 'objectcache', 'searchindex', 'querycache',
+                       'objectcache', 'searchindex', 'querycache',
                        'ipblocks', 'user', 'page', 'revision', 'recentchanges',
                        'brokenlinks', 'categorylinks', 'imagelinks', 'watchlist',
                        'image', 'oldimage', 'archive' ) );
                
                # These are safe to clear out:
-               $this->clearTable( 'linkscc' );
                $this->clearTable( 'objectcache' );
 
                # These need to be rebuild if used:
@@ -156,16 +208,20 @@ class UtfUpdater {
                $this->convertTable( 'recentchanges', 'rc_id',
                        array( 'rc_user_text', 'rc_title', 'rc_comment' ) );
                
-               $this->convertTable( 'brokenlinks', 'bl_to' );
+               $this->convertTable( 'pagelinks', 'pl_title' );
                $this->convertTable( 'categorylinks', 'cl_to' );
                $this->convertTable( 'imagelinks', 'il_to' );
                $this->convertTable( 'watchlist', 'wl_title' );
                
                # FIXME: We'll also need to change the files.
                $this->convertTable( 'image', 'img_name',
-                       array( 'img_name', 'img_description', 'img_user_text' ) );
+                       array( 'img_name', 'img_description', 'img_user_text' ),
+                       null,
+                       array( &$this, 'imageRenameCallback' ) );
                $this->convertTable( 'oldimage', 'archive_name',
-                       array( 'oi_name', 'oi_archive_name', 'oi_description', 'oi_user_text' ) );
+                       array( 'oi_name', 'oi_archive_name', 'oi_description', 'oi_user_text' ),
+                       null,
+                       array( &$this, 'oldimageRenameCallback' ) );
                
                # Don't change the ar_text entries; use $wgLegacyEncoding to read them at runtime
                $this->convertTable( 'archive', null,