Index: trunk/phase3/maintenance/convertUtf8.php |
— | — | @@ -59,9 +59,10 @@ |
60 | 60 | * @param string $key Primary key, to identify fields in the UPDATE. If NULL, all fields will be used to match. |
61 | 61 | * @param array $fields List of all fields to grab and convert. If null, will assume you want the $key, and will ask for DISTINCT. |
62 | 62 | * @param array $timestamp A field which should be updated to the current timestamp on changed records. |
| 63 | + * @param callable $callback |
63 | 64 | * @access private |
64 | 65 | */ |
65 | | - function convertTable( $table, $key, $fields = null, $timestamp = null ) { |
| 66 | + function convertTable( $table, $key, $fields = null, $timestamp = null, $callback = null ) { |
66 | 67 | $fname = 'UtfUpdater::convertTable'; |
67 | 68 | if( $fields ) { |
68 | 69 | $distinct = ''; |
— | — | @@ -106,12 +107,64 @@ |
107 | 108 | $keyCond, |
108 | 109 | $fname ); |
109 | 110 | if( ++$n % 100 == 0 ) echo "$n\n"; |
| 111 | + |
| 112 | + if( is_callable( $callback ) ) { |
| 113 | + call_user_func( $callback, $s ); |
| 114 | + } |
110 | 115 | } |
111 | 116 | echo "$n done.\n"; |
112 | 117 | $this->db->freeResult( $res ); |
113 | 118 | } |
114 | 119 | |
115 | 120 | /** |
| 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 | + /** |
116 | 169 | * Lock tables. |
117 | 170 | * @param array $tables An array of table to be locked. |
118 | 171 | */ |
— | — | @@ -162,9 +215,13 @@ |
163 | 216 | |
164 | 217 | # FIXME: We'll also need to change the files. |
165 | 218 | $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' ) ); |
167 | 222 | $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' ) ); |
169 | 226 | |
170 | 227 | # Don't change the ar_text entries; use $wgLegacyEncoding to read them at runtime |
171 | 228 | $this->convertTable( 'archive', null, |