Some cleanup to EditPage:
[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 /**
71 * @return FileRepo
72 */
73 function getRepo() {
74 if ( !isset( $this->repo ) ) {
75 $this->repo = RepoGroup::singleton()->getLocalRepo();
76 }
77 return $this->repo;
78 }
79
80 function build() {
81 $this->buildImage();
82 $this->buildOldImage();
83 }
84
85 function init( $count, $table ) {
86 $this->processed = 0;
87 $this->updated = 0;
88 $this->count = $count;
89 $this->startTime = wfTime();
90 $this->table = $table;
91 }
92
93 function progress( $updated ) {
94 $this->updated += $updated;
95 $this->processed++;
96 if ( $this->processed % 100 != 0 ) {
97 return;
98 }
99 $portion = $this->processed / $this->count;
100 $updateRate = $this->updated / $this->processed;
101
102 $now = wfTime();
103 $delta = $now - $this->startTime;
104 $estimatedTotalTime = $delta / $portion;
105 $eta = $this->startTime + $estimatedTotalTime;
106 $rate = $this->processed / $delta;
107
108 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
109 wfTimestamp( TS_DB, intval( $now ) ),
110 $portion * 100.0,
111 $this->table,
112 wfTimestamp( TS_DB, intval( $eta ) ),
113 $this->processed,
114 $this->count,
115 $rate,
116 $updateRate * 100.0 ) );
117 flush();
118 }
119
120 function buildTable( $table, $key, $callback ) {
121 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
122 $this->init( $count, $table );
123 $this->output( "Processing $table...\n" );
124
125 $result = wfGetDB( DB_SLAVE )->select( $table, '*', array(), __METHOD__ );
126
127 foreach ( $result as $row ) {
128 $update = call_user_func( $callback, $row, null );
129 if ( $update ) {
130 $this->progress( 1 );
131 } else {
132 $this->progress( 0 );
133 }
134 }
135 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
136 }
137
138 function buildImage() {
139 $callback = array( $this, 'imageCallback' );
140 $this->buildTable( 'image', 'img_name', $callback );
141 }
142
143 function imageCallback( $row, $copy ) {
144 // Create a File object from the row
145 // This will also upgrade it
146 $file = $this->getRepo()->newFileFromRow( $row );
147 return $file->getUpgraded();
148 }
149
150 function buildOldImage() {
151 $this->buildTable( 'oldimage', 'oi_archive_name',
152 array( $this, 'oldimageCallback' ) );
153 }
154
155 function oldimageCallback( $row, $copy ) {
156 // Create a File object from the row
157 // This will also upgrade it
158 if ( $row->oi_archive_name == '' ) {
159 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
160 return false;
161 }
162 $file = $this->getRepo()->newFileFromRow( $row );
163 return $file->getUpgraded();
164 }
165
166 function crawlMissing() {
167 $repo = RepoGroup::singleton()->getLocalRepo();
168 $repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
169 }
170
171 function checkMissingImage( $fullpath ) {
172 $filename = wfBaseName( $fullpath );
173 if ( is_dir( $fullpath ) ) {
174 return;
175 }
176 if ( is_link( $fullpath ) ) {
177 $this->output( "skipping symlink at $fullpath\n" );
178 return;
179 }
180 $row = $this->dbw->selectRow( 'image',
181 array( 'img_name' ),
182 array( 'img_name' => $filename ),
183 __METHOD__ );
184
185 if ( $row ) {
186 // already known, move on
187 return;
188 } else {
189 $this->addMissingImage( $filename, $fullpath );
190 }
191 }
192
193 function addMissingImage( $filename, $fullpath ) {
194 $timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
195
196 global $wgContLang;
197 $altname = $wgContLang->checkTitleEncoding( $filename );
198 if ( $altname != $filename ) {
199 if ( $this->dryrun ) {
200 $filename = $altname;
201 $this->output( "Estimating transcoding... $altname\n" );
202 } else {
203 $filename = $this->renameFile( $filename );
204 }
205 }
206
207 if ( $filename == '' ) {
208 $this->output( "Empty filename for $fullpath\n" );
209 return;
210 }
211 if ( !$this->dryrun ) {
212 $file = wfLocalFile( $filename );
213 if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
214 false, $timestamp ) )
215 {
216 $this->output( "Error uploading file $fullpath\n" );
217 return;
218 }
219 }
220 $this->output( $fullpath . "\n" );
221 }
222 }
223
224 $maintClass = 'ImageBuilder';
225 require_once( RUN_MAINTENANCE_IF_MAIN );