]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/refreshFileHeaders.php
MediaWiki 1.30.2-scripts
[autoinstalls/mediawiki.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->setBatchSize( 200 );
41         }
42
43         public function execute() {
44                 $repo = RepoGroup::singleton()->getLocalRepo();
45                 $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
46                 $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
47
48                 $count = 0;
49                 $dbr = $this->getDB( DB_REPLICA );
50
51                 do {
52                         $conds = [ "img_name > {$dbr->addQuotes( $start )}" ];
53
54                         if ( strlen( $end ) ) {
55                                 $conds[] = "img_name <= {$dbr->addQuotes( $end )}";
56                         }
57
58                         $res = $dbr->select( 'image', '*', $conds,
59                                 __METHOD__, [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name ASC' ] );
60
61                         if ( $res->numRows() > 0 ) {
62                                 $row1 = $res->current();
63                                 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
64                                 $res->rewind();
65                         }
66
67                         $backendOperations = [];
68
69                         foreach ( $res as $row ) {
70                                 $file = $repo->newFileFromRow( $row );
71                                 $headers = $file->getContentHeaders();
72
73                                 if ( count( $headers ) ) {
74                                         $backendOperations[] = [
75                                                 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
76                                         ];
77                                 }
78
79                                 // Do all of the older file versions...
80                                 foreach ( $file->getHistory() as $oldFile ) {
81                                         $headers = $oldFile->getContentHeaders();
82                                         if ( count( $headers ) ) {
83                                                 $backendOperations[] = [
84                                                         'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers
85                                                 ];
86                                         }
87                                 }
88
89                                 if ( $this->hasOption( 'verbose' ) ) {
90                                         $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
91                                 }
92
93                                 $start = $row->img_name; // advance
94                         }
95
96                         $backendOperationsCount = count( $backendOperations );
97                         $count += $backendOperationsCount;
98
99                         $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
100                         $this->updateFileHeaders( $repo, $backendOperations );
101                 } while ( $res->numRows() === $this->mBatchSize );
102
103                 $this->output( "Done. Updated headers for $count file(s).\n" );
104         }
105
106         protected function updateFileHeaders( $repo, $backendOperations ) {
107                 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
108
109                 if ( !$status->isGood() ) {
110                         $this->error( "Encountered error: " . print_r( $status, true ) );
111                 }
112         }
113 }
114
115 $maintClass = 'RefreshFileHeaders';
116 require_once RUN_MAINTENANCE_IF_MAIN;