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