Revert r88502 since this can be done with a gadget
[lhc/web/wiklou.git] / maintenance / rebuildImages.php
1 <?php
2 /**
3 * Script to update image metadata records
4 *
5 * Usage: php rebuildImages.php [--missing] [--dry-run]
6 * Options:
7 * --missing Crawl the uploads dir for images without records, and
8 * add them only.
9 *
10 * Copyright © 2005 Brion Vibber <brion@pobox.com>
11 * http://www.mediawiki.org/
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
27 *
28 * @file
29 * @author Brion Vibber <brion at pobox.com>
30 * @ingroup maintenance
31 */
32
33 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
34
35 class ImageBuilder extends Maintenance {
36
37 /**
38 * @var DatabaseBase
39 */
40 protected $dbw;
41
42 function __construct() {
43 parent::__construct();
44
45 global $wgUpdateCompatibleMetadata;
46 //make sure to update old, but compatible img_metadata fields.
47 $wgUpdateCompatibleMetadata = true;
48
49 $this->mDescription = 'Script to update image metadata records';
50
51 $this->addOption( 'missing', 'Check for files without associated database record' );
52 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
53 }
54
55 public function execute() {
56 $this->dbw = wfGetDB( DB_MASTER );
57 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
58 $this->dryrun = $this->hasOption( 'dry-run' );
59 if ( $this->dryrun ) {
60 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
61 }
62
63 if ( $this->hasOption( 'missing' ) ) {
64 $this->crawlMissing();
65 } else {
66 $this->build();
67 }
68 }
69
70 function getRepo() {
71 if ( !isset( $this->repo ) ) {
72 $this->repo = RepoGroup::singleton()->getLocalRepo();
73 }
74 return $this->repo;
75 }
76
77 function build() {
78 $this->buildImage();
79 $this->buildOldImage();
80 }
81
82 function init( $count, $table ) {
83 $this->processed = 0;
84 $this->updated = 0;
85 $this->count = $count;
86 $this->startTime = wfTime();
87 $this->table = $table;
88 }
89
90 function progress( $updated ) {
91 $this->updated += $updated;
92 $this->processed++;
93 if ( $this->processed % 100 != 0 ) {
94 return;
95 }
96 $portion = $this->processed / $this->count;
97 $updateRate = $this->updated / $this->processed;
98
99 $now = wfTime();
100 $delta = $now - $this->startTime;
101 $estimatedTotalTime = $delta / $portion;
102 $eta = $this->startTime + $estimatedTotalTime;
103 $rate = $this->processed / $delta;
104
105 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
106 wfTimestamp( TS_DB, intval( $now ) ),
107 $portion * 100.0,
108 $this->table,
109 wfTimestamp( TS_DB, intval( $eta ) ),
110 $this->processed,
111 $this->count,
112 $rate,
113 $updateRate * 100.0 ) );
114 flush();
115 }
116
117 function buildTable( $table, $key, $callback ) {
118 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
119 $this->init( $count, $table );
120 $this->output( "Processing $table...\n" );
121
122 $result = wfGetDB( DB_SLAVE )->select( $table, '*', array(), __METHOD__ );
123
124 foreach ( $result as $row ) {
125 $update = call_user_func( $callback, $row, null );
126 if ( $update ) {
127 $this->progress( 1 );
128 } else {
129 $this->progress( 0 );
130 }
131 }
132 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
133 }
134
135 function buildImage() {
136 $callback = array( $this, 'imageCallback' );
137 $this->buildTable( 'image', 'img_name', $callback );
138 }
139
140 function imageCallback( $row, $copy ) {
141 // Create a File object from the row
142 // This will also upgrade it
143 $file = $this->getRepo()->newFileFromRow( $row );
144 return $file->getUpgraded();
145 }
146
147 function buildOldImage() {
148 $this->buildTable( 'oldimage', 'oi_archive_name',
149 array( $this, 'oldimageCallback' ) );
150 }
151
152 function oldimageCallback( $row, $copy ) {
153 // Create a File object from the row
154 // This will also upgrade it
155 if ( $row->oi_archive_name == '' ) {
156 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
157 return false;
158 }
159 $file = $this->getRepo()->newFileFromRow( $row );
160 return $file->getUpgraded();
161 }
162
163 function crawlMissing() {
164 $repo = RepoGroup::singleton()->getLocalRepo();
165 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
166 }
167
168 function checkMissingImage( $fullpath ) {
169 $filename = wfBaseName( $fullpath );
170 if ( is_dir( $fullpath ) ) {
171 return;
172 }
173 if ( is_link( $fullpath ) ) {
174 $this->output( "skipping symlink at $fullpath\n" );
175 return;
176 }
177 $row = $this->dbw->selectRow( 'image',
178 array( 'img_name' ),
179 array( 'img_name' => $filename ),
180 __METHOD__ );
181
182 if ( $row ) {
183 // already known, move on
184 return;
185 } else {
186 $this->addMissingImage( $filename, $fullpath );
187 }
188 }
189
190 function addMissingImage( $filename, $fullpath ) {
191 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
192
193 global $wgContLang;
194 $altname = $wgContLang->checkTitleEncoding( $filename );
195 if ( $altname != $filename ) {
196 if ( $this->dryrun ) {
197 $filename = $altname;
198 $this->output( "Estimating transcoding... $altname\n" );
199 } else {
200 $filename = $this->renameFile( $filename );
201 }
202 }
203
204 if ( $filename == '' ) {
205 $this->output( "Empty filename for $fullpath\n" );
206 return;
207 }
208 if ( !$this->dryrun ) {
209 $file = wfLocalFile( $filename );
210 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
211 false, $timestamp ) )
212 {
213 $this->output( "Error uploading file $fullpath\n" );
214 return;
215 }
216 }
217 $this->output( $fullpath . "\n" );
218 }
219 }
220
221 $maintClass = 'ImageBuilder';
222 require( RUN_MAINTENANCE_IF_MAIN );