Merge "Add attributes parameter to ShowSearchHitTitle"
[lhc/web/wiklou.git] / maintenance / refreshFileHeaders.php
1 <?php
2 /**
3 * Refresh file headers from metadata.
4 *
5 * Usage: php refreshFileHeaders.php
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 */
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script to refresh file headers from metadata
30 *
31 * @ingroup Maintenance
32 */
33 class RefreshFileHeaders extends Maintenance {
34 function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Script to update file HTTP headers' );
37 $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
38 $this->addOption( 'start', 'Name of file to start with', false, true );
39 $this->addOption( 'end', 'Name of file to end with', false, true );
40 $this->addOption( 'media_type', 'Media type to filter for', false, true );
41 $this->addOption( 'major_mime', 'Major mime type to filter for', false, true );
42 $this->addOption( 'minor_mime', 'Minor mime type to filter for', false, true );
43 $this->setBatchSize( 200 );
44 }
45
46 public function execute() {
47 $repo = RepoGroup::singleton()->getLocalRepo();
48 $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
49 $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
50 // filter by img_media_type
51 $media_type = str_replace( ' ', '_', $this->getOption( 'media_type', '' ) );
52 // filter by img_major_mime
53 $major_mime = str_replace( ' ', '_', $this->getOption( 'major_mime', '' ) );
54 // filter by img_minor_mime
55 $minor_mime = str_replace( ' ', '_', $this->getOption( 'minor_mime', '' ) );
56
57 $count = 0;
58 $dbr = $this->getDB( DB_REPLICA );
59
60 $fileQuery = LocalFile::getQueryInfo();
61
62 do {
63 $conds = [ "img_name > {$dbr->addQuotes( $start )}" ];
64
65 if ( strlen( $end ) ) {
66 $conds[] = "img_name <= {$dbr->addQuotes( $end )}";
67 }
68
69 if ( strlen( $media_type ) ) {
70 $conds[] = "img_media_type = {$dbr->addQuotes( $media_type )}";
71 }
72
73 if ( strlen( $major_mime ) ) {
74 $conds[] = "img_major_mime = {$dbr->addQuotes( $major_mime )}";
75 }
76
77 if ( strlen( $minor_mime ) ) {
78 $conds[] = "img_minor_mime = {$dbr->addQuotes( $minor_mime )}";
79 }
80
81 $res = $dbr->select( $fileQuery['tables'], $fileQuery['fields'], $conds,
82 __METHOD__, [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name ASC' ], $fileQuery['joins']
83 );
84
85 if ( $res->numRows() > 0 ) {
86 $row1 = $res->current();
87 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
88 $res->rewind();
89 }
90
91 $backendOperations = [];
92
93 foreach ( $res as $row ) {
94 $file = $repo->newFileFromRow( $row );
95 $headers = $file->getContentHeaders();
96
97 if ( count( $headers ) ) {
98 $backendOperations[] = [
99 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
100 ];
101 }
102
103 // Do all of the older file versions...
104 foreach ( $file->getHistory() as $oldFile ) {
105 $headers = $oldFile->getContentHeaders();
106 if ( count( $headers ) ) {
107 $backendOperations[] = [
108 'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers
109 ];
110 }
111 }
112
113 if ( $this->hasOption( 'verbose' ) ) {
114 $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
115 }
116
117 $start = $row->img_name; // advance
118 }
119
120 $backendOperationsCount = count( $backendOperations );
121 $count += $backendOperationsCount;
122
123 $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
124 $this->updateFileHeaders( $repo, $backendOperations );
125 } while ( $res->numRows() === $this->mBatchSize );
126
127 $this->output( "Done. Updated headers for $count file(s).\n" );
128 }
129
130 protected function updateFileHeaders( $repo, $backendOperations ) {
131 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
132
133 if ( !$status->isGood() ) {
134 $this->error( "Encountered error: " . print_r( $status, true ) );
135 }
136 }
137 }
138
139 $maintClass = 'RefreshFileHeaders';
140 require_once RUN_MAINTENANCE_IF_MAIN;