Merge "Add passing ''italic'''s case to 'Unclosed and unmatched quotes' test"
[lhc/web/wiklou.git] / maintenance / refreshImageMetadata.php
1 <?php
2 /**
3 * Script to refresh image metadata fields. See also rebuildImages.php
4 *
5 * Usage: php refreshImageMetadata.php
6 *
7 * Copyright © 2011 Brian Wolff
8 * http://www.mediawiki.org/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @author Brian Wolff
27 * @ingroup Maintenance
28 */
29
30 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
31
32 class RefreshImageMetadata extends Maintenance {
33
34 /**
35 * @var DatabaseBase
36 */
37 protected $dbw;
38
39 function __construct() {
40 parent::__construct();
41
42 $this->mDescription = 'Script to update image metadata records';
43 $this->setBatchSize( 200 );
44
45 $this->addOption( 'force', 'Reload metadata from file even if the metadata looks ok', false, false, 'f' );
46 $this->addOption( 'broken-only', 'Only fix really broken records, leave old but still compatible records alone.' );
47 $this->addOption( 'verbose', 'Output extra information about each upgraded/non-upgraded file.', false, false, 'v' );
48 $this->addOption( 'start', 'Name of file to start with', false, true );
49 $this->addOption( 'end', 'Name of file to end with', false, true );
50
51 $this->addOption( 'mime', '(Inefficient!) Only refresh files with this mime type. Can accept wild-card image/*' , false, true );
52 $this->addOption( 'metadata-contains', '(Inefficient!) Only refresh files where the img_metadata field contains this string. Can be used if its known a specific property was being extracted incorrectly.', false, true );
53
54 }
55
56 public function execute() {
57 $force = $this->hasOption( 'force' );
58 $brokenOnly = $this->hasOption( 'broken-only' );
59 $verbose = $this->hasOption( 'verbose' );
60 $start = $this->getOption( 'start', false );
61 $this->setupParameters( $force, $brokenOnly );
62
63 $upgraded = 0;
64 $leftAlone = 0;
65 $error = 0;
66
67 $dbw = wfGetDB( DB_MASTER );
68 if ( $this->mBatchSize <= 0 ) {
69 $this->error( "Batch size is too low...", 12 );
70 }
71
72 $repo = RepoGroup::singleton()->getLocalRepo();
73 $conds = $this->getConditions( $dbw );
74
75 // For the WHERE img_name > 'foo' condition that comes after doing a batch
76 $conds2 = array();
77 if ( $start !== false ) {
78 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
79 }
80
81 $options = array(
82 'LIMIT' => $this->mBatchSize,
83 'ORDER BY' => 'img_name ASC',
84 );
85
86 do {
87 $res = $dbw->select(
88 'image',
89 '*',
90 array_merge( $conds, $conds2 ),
91 __METHOD__,
92 $options
93 );
94
95 if ( $res->numRows() > 0 ) {
96 $row1 = $res->current();
97 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n");
98 $res->rewind();
99 } else {
100 $this->error( "No images to process.", 4 );
101 }
102
103 foreach ( $res as $row ) {
104 $file = $repo->newFileFromRow( $row );
105 if ( $file->getUpgraded() ) {
106 // File was upgraded.
107 $upgraded++;
108 $newLength = strlen( $file->getMetadata() );
109 $oldLength = strlen( $row->img_metadata );
110 if ( $newLength < $oldLength - 5 ) {
111 // If after updating, the metadata is smaller then
112 // what it was before, that's probably not a good thing
113 // because we extract more data with time, not less.
114 // Thus this probably indicates an error of some sort,
115 // or at the very least is suspicious. Have the - 5 just
116 // to weed out any inconsequential changes.
117 $error++;
118 $this->output( "Warning: File:{$row->img_name} used to have " .
119 "$oldLength bytes of metadata but now has $newLength bytes.\n" );
120 } elseif ( $verbose ) {
121 $this->output("Refreshed File:{$row->img_name}.\n" );
122 }
123 } else {
124 $leftAlone++;
125 if ( $force ) {
126 $file->upgradeRow();
127 $newLength = strlen( $file->getMetadata() );
128 $oldLength = strlen( $row->img_metadata );
129 if ( $newLength < $oldLength - 5 ) {
130 $error++;
131 $this->output( "Warning: File:{$row->img_name} used to have " .
132 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
133
134 }
135 if ( $verbose ) {
136 $this->output("Forcibly refreshed File:{$row->img_name}.\n" );
137 }
138 }
139 else {
140 if ( $verbose ) {
141 $this->output( "Skipping File:{$row->img_name}.\n" );
142 }
143 }
144 }
145
146 }
147 $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) );
148 wfWaitForSlaves();
149 } while( $res->numRows() === $this->mBatchSize );
150
151 $total = $upgraded + $leftAlone;
152 if ( $force ) {
153 $this->output( "\nFinished refreshing file metadata for $total files. $upgraded needed to be refreshed, $leftAlone did not need to be but were refreshed anyways, and $error refreshes were suspicious.\n" );
154 } else {
155 $this->output( "\nFinished refreshing file metadata for $total files. $upgraded were refreshed, $leftAlone were already up to date, and $error refreshes were suspicious.\n" );
156 }
157 }
158
159 /**
160 * @param $dbw DatabaseBase
161 * @return array
162 */
163 function getConditions( $dbw ) {
164 $conds = array();
165
166 $end = $this->getOption( 'end', false );
167 $mime = $this->getOption( 'mime', false );
168 $like = $this->getOption( 'metadata-contains', false );
169
170 if ( $end !== false ) {
171 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end ) ;
172 }
173 if ( $mime !== false ) {
174 list( $major, $minor ) = File::splitMime( $mime );
175 $conds['img_major_mime'] = $major;
176 if ( $minor !== '*' ) {
177 $conds['img_minor_mime'] = $minor;
178 }
179 }
180 if ( $like ) {
181 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
182 }
183 return $conds;
184 }
185
186 /**
187 * @param $force bool
188 * @param $brokenOnly bool
189 */
190 function setupParameters( $force, $brokenOnly ) {
191 global $wgUpdateCompatibleMetadata;
192
193 if ( $brokenOnly ) {
194 $wgUpdateCompatibleMetadata = false;
195 } else {
196 $wgUpdateCompatibleMetadata = true;
197 }
198
199 if ( $brokenOnly && $force ) {
200 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
201 }
202 }
203 }
204
205
206 $maintClass = 'RefreshImageMetadata';
207 require_once( RUN_MAINTENANCE_IF_MAIN );