maintenance/version: Namespace and rename
[mediawiki.git] / maintenance / refreshFileHeaders.php
blob97aa3815699519c12c6d5e8d242e5506d4f06c39
1 <?php
2 /**
3 * Refresh file headers from metadata.
5 * Usage: php refreshFileHeaders.php
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.
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.
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
22 * @file
23 * @ingroup Maintenance
26 use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
27 use Wikimedia\Rdbms\SelectQueryBuilder;
29 require_once __DIR__ . '/Maintenance.php';
31 /**
32 * Maintenance script to refresh file headers from metadata
34 * @ingroup Maintenance
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
52 $this->setBatchSize( 200 );
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', '' ) );
66 $count = 0;
67 $dbr = $this->getDB( DB_REPLICA );
69 do {
70 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
72 $queryBuilder->where( [ 'img_name > ' . $dbr->addQuotes( $start ) ] );
73 if ( strlen( $end ) ) {
74 $queryBuilder->andWhere( 'img_name <= ' . $dbr->addQuotes( $end ) );
77 if ( strlen( $media_type ) ) {
78 $queryBuilder->andWhere( [ 'img_media_type' => $media_type ] );
81 if ( strlen( $major_mime ) ) {
82 $queryBuilder->andWhere( [ 'img_major_mime' => $major_mime ] );
85 if ( strlen( $minor_mime ) ) {
86 $queryBuilder->andWhere( [ 'img_minor_mime' => $minor_mime ] );
89 $res = $queryBuilder
90 ->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC )
91 ->limit( $this->getBatchSize() )
92 ->caller( __METHOD__ )->fetchResultSet();
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();
100 $backendOperations = [];
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;
109 if ( count( $headers ) ) {
110 $backendOperations[] = [
111 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
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
125 if ( $this->hasOption( 'verbose' ) ) {
126 $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
129 $start = $row->img_name; // advance
132 $backendOperationsCount = count( $backendOperations );
133 $count += $backendOperationsCount;
135 $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
136 $this->updateFileHeaders( $repo, $backendOperations );
137 } while ( $res->numRows() === $this->getBatchSize() );
139 $this->output( "Done. Updated headers for $count file(s).\n" );
143 * @param LocalRepo $repo
144 * @param array $backendOperations
146 protected function updateFileHeaders( $repo, $backendOperations ) {
147 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
149 if ( !$status->isGood() ) {
150 $this->error( "Encountered error: " . print_r( $status, true ) );
155 $maintClass = RefreshFileHeaders::class;
156 require_once RUN_MAINTENANCE_IF_MAIN;