r9546 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r9545‎ | r9546 | r9547 >
Date:05:29, 21 June 2005
Author:vibber
Status:old
Tags:
Comment:
Add file renaming for images and archived images, leaving symlinks from old
names. This code is untested so far.
Modified paths:
  • /trunk/phase3/maintenance/convertUtf8.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/convertUtf8.php
@@ -59,9 +59,10 @@
6060 * @param string $key Primary key, to identify fields in the UPDATE. If NULL, all fields will be used to match.
6161 * @param array $fields List of all fields to grab and convert. If null, will assume you want the $key, and will ask for DISTINCT.
6262 * @param array $timestamp A field which should be updated to the current timestamp on changed records.
 63+ * @param callable $callback
6364 * @access private
6465 */
65 - function convertTable( $table, $key, $fields = null, $timestamp = null ) {
 66+ function convertTable( $table, $key, $fields = null, $timestamp = null, $callback = null ) {
6667 $fname = 'UtfUpdater::convertTable';
6768 if( $fields ) {
6869 $distinct = '';
@@ -106,12 +107,64 @@
107108 $keyCond,
108109 $fname );
109110 if( ++$n % 100 == 0 ) echo "$n\n";
 111+
 112+ if( is_callable( $callback ) ) {
 113+ call_user_func( $callback, $s );
 114+ }
110115 }
111116 echo "$n done.\n";
112117 $this->db->freeResult( $res );
113118 }
114119
115120 /**
 121+ * @param object $row
 122+ * @access private
 123+ */
 124+ function imageRenameCallback( $row ) {
 125+ $this->renameFile( $row->img_name, 'wfImageDir' );
 126+ }
 127+
 128+ /**
 129+ * @param object $row
 130+ * @access private
 131+ */
 132+ function oldimageRenameCallback( $row ) {
 133+ $this->renameFile( $row->oi_archive_name, 'wfImageThumbDir' );
 134+ }
 135+
 136+ /**
 137+ * Rename a given image or archived image file to the converted filename,
 138+ * leaving a symlink for URL compatibility.
 139+ *
 140+ * @param string $oldname pre-conversion filename
 141+ * @param callable $subdirCallback a function to generate hashed directories
 142+ * @access private
 143+ */
 144+ function renameFile( $oldname, $subdirCallback ) {
 145+ $newname = $this->toUtf8( $oldname );
 146+ if( $newname == $oldname ) {
 147+ // No need to rename; another field triggered this row.
 148+ return;
 149+ }
 150+
 151+ $oldpath = call_user_func( $subdirCallback, $oldname ) . '/' . $oldname;
 152+ $newpath = call_user_func( $subdirCallback, $newname ) . '/' . $newname;
 153+
 154+ echo "Renaming $oldpath to $newpath... ";
 155+ if( rename( $oldpath, $newpath ) ) {
 156+ echo "ok\n";
 157+ echo "Creating compatibility symlink from $newpath to $oldpath... ";
 158+ if( symlink( $newpath, $oldpath ) ) {
 159+ echo "ok\n";
 160+ } else {
 161+ echo " symlink failed!\n";
 162+ }
 163+ } else {
 164+ echo " rename failed!\n";
 165+ }
 166+ }
 167+
 168+ /**
116169 * Lock tables.
117170 * @param array $tables An array of table to be locked.
118171 */
@@ -162,9 +215,13 @@
163216
164217 # FIXME: We'll also need to change the files.
165218 $this->convertTable( 'image', 'img_name',
166 - array( 'img_name', 'img_description', 'img_user_text' ) );
 219+ array( 'img_name', 'img_description', 'img_user_text' ),
 220+ null,
 221+ array( &$this, 'imageRenameCallback' ) );
167222 $this->convertTable( 'oldimage', 'archive_name',
168 - array( 'oi_name', 'oi_archive_name', 'oi_description', 'oi_user_text' ) );
 223+ array( 'oi_name', 'oi_archive_name', 'oi_description', 'oi_user_text' ),
 224+ null,
 225+ array( &$this, 'oldimageRenameCallback' ) );
169226
170227 # Don't change the ar_text entries; use $wgLegacyEncoding to read them at runtime
171228 $this->convertTable( 'archive', null,

Status & tagging log