follow up to r85847 - tokipona projects are long dead and gone
[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 $this->mDescription = 'Script to update image metadata records';
46
47 $this->addOption( 'missing', 'Check for files without associated database record' );
48 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
49 }
50
51 public function execute() {
52 $this->dbw = wfGetDB( DB_MASTER );
53 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
54 $this->dryrun = $this->hasOption( 'dry-run' );
55 if ( $this->dryrun ) {
56 $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
57 }
58
59 if ( $this->hasOption( 'missing' ) ) {
60 $this->crawlMissing();
61 } else {
62 $this->build();
63 }
64 }
65
66 function getRepo() {
67 if ( !isset( $this->repo ) ) {
68 $this->repo = RepoGroup::singleton()->getLocalRepo();
69 }
70 return $this->repo;
71 }
72
73 function build() {
74 $this->buildImage();
75 $this->buildOldImage();
76 }
77
78 function init( $count, $table ) {
79 $this->processed = 0;
80 $this->updated = 0;
81 $this->count = $count;
82 $this->startTime = wfTime();
83 $this->table = $table;
84 }
85
86 function progress( $updated ) {
87 $this->updated += $updated;
88 $this->processed++;
89 if ( $this->processed % 100 != 0 ) {
90 return;
91 }
92 $portion = $this->processed / $this->count;
93 $updateRate = $this->updated / $this->processed;
94
95 $now = wfTime();
96 $delta = $now - $this->startTime;
97 $estimatedTotalTime = $delta / $portion;
98 $eta = $this->startTime + $estimatedTotalTime;
99 $rate = $this->processed / $delta;
100
101 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
102 wfTimestamp( TS_DB, intval( $now ) ),
103 $portion * 100.0,
104 $this->table,
105 wfTimestamp( TS_DB, intval( $eta ) ),
106 $this->processed,
107 $this->count,
108 $rate,
109 $updateRate * 100.0 ) );
110 flush();
111 }
112
113 function buildTable( $table, $key, $callback ) {
114 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
115 $this->init( $count, $table );
116 $this->output( "Processing $table...\n" );
117
118 $result = wfGetDB( DB_SLAVE )->select( $table, '*', array(), __METHOD__ );
119
120 foreach ( $result as $row ) {
121 $update = call_user_func( $callback, $row, null );
122 if ( $update ) {
123 $this->progress( 1 );
124 } else {
125 $this->progress( 0 );
126 }
127 }
128 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
129 }
130
131 function buildImage() {
132 $callback = array( $this, 'imageCallback' );
133 $this->buildTable( 'image', 'img_name', $callback );
134 }
135
136 function imageCallback( $row, $copy ) {
137 // Create a File object from the row
138 // This will also upgrade it
139 $file = $this->getRepo()->newFileFromRow( $row );
140 return $file->getUpgraded();
141 }
142
143 function buildOldImage() {
144 $this->buildTable( 'oldimage', 'oi_archive_name',
145 array( $this, 'oldimageCallback' ) );
146 }
147
148 function oldimageCallback( $row, $copy ) {
149 // Create a File object from the row
150 // This will also upgrade it
151 if ( $row->oi_archive_name == '' ) {
152 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
153 return false;
154 }
155 $file = $this->getRepo()->newFileFromRow( $row );
156 return $file->getUpgraded();
157 }
158
159 function crawlMissing() {
160 $repo = RepoGroup::singleton()->getLocalRepo();
161 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
162 }
163
164 function checkMissingImage( $fullpath ) {
165 $filename = wfBaseName( $fullpath );
166 if ( is_dir( $fullpath ) ) {
167 return;
168 }
169 if ( is_link( $fullpath ) ) {
170 $this->output( "skipping symlink at $fullpath\n" );
171 return;
172 }
173 $row = $this->dbw->selectRow( 'image',
174 array( 'img_name' ),
175 array( 'img_name' => $filename ),
176 __METHOD__ );
177
178 if ( $row ) {
179 // already known, move on
180 return;
181 } else {
182 $this->addMissingImage( $filename, $fullpath );
183 }
184 }
185
186 function addMissingImage( $filename, $fullpath ) {
187 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
188
189 global $wgContLang;
190 $altname = $wgContLang->checkTitleEncoding( $filename );
191 if ( $altname != $filename ) {
192 if ( $this->dryrun ) {
193 $filename = $altname;
194 $this->output( "Estimating transcoding... $altname\n" );
195 } else {
196 $filename = $this->renameFile( $filename );
197 }
198 }
199
200 if ( $filename == '' ) {
201 $this->output( "Empty filename for $fullpath\n" );
202 return;
203 }
204 if ( !$this->dryrun ) {
205 $file = wfLocalFile( $filename );
206 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
207 false, $timestamp ) )
208 {
209 $this->output( "Error uploading file $fullpath\n" );
210 return;
211 }
212 }
213 $this->output( $fullpath . "\n" );
214 }
215 }
216
217 $maintClass = 'ImageBuilder';
218 require( RUN_MAINTENANCE_IF_MAIN );