Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
RefreshFileHeaders | |
0.00% |
0 / 69 |
|
0.00% |
0 / 3 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
156 | |||
updateFileHeaders | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
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 | use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
27 | use Wikimedia\Rdbms\SelectQueryBuilder; |
28 | |
29 | require_once __DIR__ . '/Maintenance.php'; |
30 | |
31 | /** |
32 | * Maintenance script to refresh file headers from metadata |
33 | * |
34 | * @ingroup Maintenance |
35 | */ |
36 | class RefreshFileHeaders extends Maintenance { |
37 | public function __construct() { |
38 | parent::__construct(); |
39 | $this->addDescription( 'Script to update file HTTP headers' ); |
40 | $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' ); |
41 | $this->addOption( 'start', 'Name of file to start with', false, true ); |
42 | $this->addOption( 'end', 'Name of file to end with', false, true ); |
43 | $this->addOption( 'media_type', 'Media type to filter for', false, true ); |
44 | $this->addOption( 'major_mime', 'Major mime type to filter for', false, true ); |
45 | $this->addOption( 'minor_mime', 'Minor mime type to filter for', false, true ); |
46 | $this->addOption( |
47 | 'refreshContentType', |
48 | 'Set true to refresh file content type from mime data in db', |
49 | false, |
50 | false |
51 | ); |
52 | $this->setBatchSize( 200 ); |
53 | } |
54 | |
55 | public function execute() { |
56 | $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo(); |
57 | $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name |
58 | $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name |
59 | // filter by img_media_type |
60 | $media_type = str_replace( ' ', '_', $this->getOption( 'media_type', '' ) ); |
61 | // filter by img_major_mime |
62 | $major_mime = str_replace( ' ', '_', $this->getOption( 'major_mime', '' ) ); |
63 | // filter by img_minor_mime |
64 | $minor_mime = str_replace( ' ', '_', $this->getOption( 'minor_mime', '' ) ); |
65 | |
66 | $count = 0; |
67 | $dbr = $this->getReplicaDB(); |
68 | |
69 | do { |
70 | $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr ); |
71 | |
72 | $queryBuilder->where( $dbr->expr( 'img_name', '>', $start ) ); |
73 | if ( strlen( $end ) ) { |
74 | $queryBuilder->andWhere( $dbr->expr( 'img_name', '<=', $end ) ); |
75 | } |
76 | |
77 | if ( strlen( $media_type ) ) { |
78 | $queryBuilder->andWhere( [ 'img_media_type' => $media_type ] ); |
79 | } |
80 | |
81 | if ( strlen( $major_mime ) ) { |
82 | $queryBuilder->andWhere( [ 'img_major_mime' => $major_mime ] ); |
83 | } |
84 | |
85 | if ( strlen( $minor_mime ) ) { |
86 | $queryBuilder->andWhere( [ 'img_minor_mime' => $minor_mime ] ); |
87 | } |
88 | |
89 | $res = $queryBuilder |
90 | ->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC ) |
91 | ->limit( $this->getBatchSize() ) |
92 | ->caller( __METHOD__ )->fetchResultSet(); |
93 | |
94 | if ( $res->numRows() > 0 ) { |
95 | $row1 = $res->current(); |
96 | $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" ); |
97 | $res->rewind(); |
98 | } |
99 | |
100 | $backendOperations = []; |
101 | |
102 | foreach ( $res as $row ) { |
103 | $file = $repo->newFileFromRow( $row ); |
104 | $headers = $file->getContentHeaders(); |
105 | if ( $this->getOption( 'refreshContentType', false ) ) { |
106 | $headers['Content-Type'] = $row->img_major_mime . '/' . $row->img_minor_mime; |
107 | } |
108 | |
109 | if ( count( $headers ) ) { |
110 | $backendOperations[] = [ |
111 | 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers |
112 | ]; |
113 | } |
114 | |
115 | // Do all of the older file versions... |
116 | foreach ( $file->getHistory() as $oldFile ) { |
117 | $headers = $oldFile->getContentHeaders(); |
118 | if ( count( $headers ) ) { |
119 | $backendOperations[] = [ |
120 | 'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers |
121 | ]; |
122 | } |
123 | } |
124 | |
125 | if ( $this->hasOption( 'verbose' ) ) { |
126 | $this->output( "Queued headers update for file '{$row->img_name}'.\n" ); |
127 | } |
128 | |
129 | $start = $row->img_name; // advance |
130 | } |
131 | |
132 | $backendOperationsCount = count( $backendOperations ); |
133 | $count += $backendOperationsCount; |
134 | |
135 | $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" ); |
136 | $this->updateFileHeaders( $repo, $backendOperations ); |
137 | } while ( $res->numRows() === $this->getBatchSize() ); |
138 | |
139 | $this->output( "Done. Updated headers for $count file(s).\n" ); |
140 | } |
141 | |
142 | /** |
143 | * @param LocalRepo $repo |
144 | * @param array $backendOperations |
145 | */ |
146 | protected function updateFileHeaders( $repo, $backendOperations ) { |
147 | $status = $repo->getBackend()->doQuickOperations( $backendOperations ); |
148 | |
149 | if ( !$status->isGood() ) { |
150 | $this->error( $status ); |
151 | } |
152 | } |
153 | } |
154 | |
155 | $maintClass = RefreshFileHeaders::class; |
156 | require_once RUN_MAINTENANCE_IF_MAIN; |