Merge "Whitelist the <wbr> element."
[mediawiki.git] / maintenance / refreshImageMetadata.php
blob7fe5c4c17723d718c396b44432ea9ead0885d54b
1 <?php
2 /**
3 * Refresh image metadata fields. See also rebuildImages.php
5 * Usage: php refreshImageMetadata.php
7 * Copyright © 2011 Brian Wolff
8 * http://www.mediawiki.org/
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.
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.
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
25 * @file
26 * @author Brian Wolff
27 * @ingroup Maintenance
30 require_once __DIR__ . '/Maintenance.php';
32 /**
33 * Maintenance script to refresh image metadata fields.
35 * @ingroup Maintenance
37 class RefreshImageMetadata extends Maintenance {
39 /**
40 * @var DatabaseBase
42 protected $dbw;
44 function __construct() {
45 parent::__construct();
47 $this->mDescription = 'Script to update image metadata records';
48 $this->setBatchSize( 200 );
50 $this->addOption( 'force', 'Reload metadata from file even if the metadata looks ok', false, false, 'f' );
51 $this->addOption( 'broken-only', 'Only fix really broken records, leave old but still compatible records alone.' );
52 $this->addOption( 'verbose', 'Output extra information about each upgraded/non-upgraded file.', false, false, 'v' );
53 $this->addOption( 'start', 'Name of file to start with', false, true );
54 $this->addOption( 'end', 'Name of file to end with', false, true );
56 $this->addOption( 'mime', '(Inefficient!) Only refresh files with this mime type. Can accept wild-card image/*', false, true );
57 $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 );
61 public function execute() {
62 $force = $this->hasOption( 'force' );
63 $brokenOnly = $this->hasOption( 'broken-only' );
64 $verbose = $this->hasOption( 'verbose' );
65 $start = $this->getOption( 'start', false );
66 $this->setupParameters( $force, $brokenOnly );
68 $upgraded = 0;
69 $leftAlone = 0;
70 $error = 0;
72 $dbw = wfGetDB( DB_MASTER );
73 if ( $this->mBatchSize <= 0 ) {
74 $this->error( "Batch size is too low...", 12 );
77 $repo = RepoGroup::singleton()->getLocalRepo();
78 $conds = $this->getConditions( $dbw );
80 // For the WHERE img_name > 'foo' condition that comes after doing a batch
81 $conds2 = array();
82 if ( $start !== false ) {
83 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
86 $options = array(
87 'LIMIT' => $this->mBatchSize,
88 'ORDER BY' => 'img_name ASC',
91 do {
92 $res = $dbw->select(
93 'image',
94 '*',
95 array_merge( $conds, $conds2 ),
96 __METHOD__,
97 $options
100 if ( $res->numRows() > 0 ) {
101 $row1 = $res->current();
102 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
103 $res->rewind();
104 } else {
105 $this->error( "No images to process.", 4 );
108 foreach ( $res as $row ) {
109 $file = $repo->newFileFromRow( $row );
110 if ( $file->getUpgraded() ) {
111 // File was upgraded.
112 $upgraded++;
113 $newLength = strlen( $file->getMetadata() );
114 $oldLength = strlen( $row->img_metadata );
115 if ( $newLength < $oldLength - 5 ) {
116 // If after updating, the metadata is smaller then
117 // what it was before, that's probably not a good thing
118 // because we extract more data with time, not less.
119 // Thus this probably indicates an error of some sort,
120 // or at the very least is suspicious. Have the - 5 just
121 // to weed out any inconsequential changes.
122 $error++;
123 $this->output( "Warning: File:{$row->img_name} used to have " .
124 "$oldLength bytes of metadata but now has $newLength bytes.\n" );
125 } elseif ( $verbose ) {
126 $this->output( "Refreshed File:{$row->img_name}.\n" );
128 } else {
129 $leftAlone++;
130 if ( $force ) {
131 $file->upgradeRow();
132 $newLength = strlen( $file->getMetadata() );
133 $oldLength = strlen( $row->img_metadata );
134 if ( $newLength < $oldLength - 5 ) {
135 $error++;
136 $this->output( "Warning: File:{$row->img_name} used to have " .
137 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
140 if ( $verbose ) {
141 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
144 else {
145 if ( $verbose ) {
146 $this->output( "Skipping File:{$row->img_name}.\n" );
152 $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) );
153 wfWaitForSlaves();
154 } while ( $res->numRows() === $this->mBatchSize );
156 $total = $upgraded + $leftAlone;
157 if ( $force ) {
158 $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" );
159 } else {
160 $this->output( "\nFinished refreshing file metadata for $total files. $upgraded were refreshed, $leftAlone were already up to date, and $error refreshes were suspicious.\n" );
165 * @param $dbw DatabaseBase
166 * @return array
168 function getConditions( $dbw ) {
169 $conds = array();
171 $end = $this->getOption( 'end', false );
172 $mime = $this->getOption( 'mime', false );
173 $like = $this->getOption( 'metadata-contains', false );
175 if ( $end !== false ) {
176 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
178 if ( $mime !== false ) {
179 list( $major, $minor ) = File::splitMime( $mime );
180 $conds['img_major_mime'] = $major;
181 if ( $minor !== '*' ) {
182 $conds['img_minor_mime'] = $minor;
185 if ( $like ) {
186 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
188 return $conds;
192 * @param $force bool
193 * @param $brokenOnly bool
195 function setupParameters( $force, $brokenOnly ) {
196 global $wgUpdateCompatibleMetadata;
198 if ( $brokenOnly ) {
199 $wgUpdateCompatibleMetadata = false;
200 } else {
201 $wgUpdateCompatibleMetadata = true;
204 if ( $brokenOnly && $force ) {
205 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
211 $maintClass = 'RefreshImageMetadata';
212 require_once RUN_MAINTENANCE_IF_MAIN;