MediaWiki master
refreshFileHeaders.php
Go to the documentation of this file.
1<?php
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'Script to update file HTTP headers' );
44 $this->addOption( 'verbose', 'Output information about each file.', false, false, 'v' );
45 $this->addOption( 'start', 'Name of file to start with', false, true );
46 $this->addOption( 'end', 'Name of file to end with', false, true );
47 $this->addOption( 'media_type', 'Media type to filter for', false, true );
48 $this->addOption( 'major_mime', 'Major mime type to filter for', false, true );
49 $this->addOption( 'minor_mime', 'Minor mime type to filter for', false, true );
50 $this->addOption(
51 'refreshContentType',
52 'Set true to refresh file content type from mime data in db',
53 false,
54 false
55 );
56 $this->setBatchSize( 200 );
57 }
58
59 public function execute() {
60 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
61 $start = str_replace( ' ', '_', $this->getOption( 'start', '' ) ); // page on img_name
62 $end = str_replace( ' ', '_', $this->getOption( 'end', '' ) ); // page on img_name
63 // filter by img_media_type
64 $media_type = str_replace( ' ', '_', $this->getOption( 'media_type', '' ) );
65 // filter by img_major_mime
66 $major_mime = str_replace( ' ', '_', $this->getOption( 'major_mime', '' ) );
67 // filter by img_minor_mime
68 $minor_mime = str_replace( ' ', '_', $this->getOption( 'minor_mime', '' ) );
69
70 $count = 0;
71 $dbr = $this->getReplicaDB();
72
73 do {
74 $queryBuilder = FileSelectQueryBuilder::newForFile( $dbr );
75
76 $queryBuilder->where( $dbr->expr( 'img_name', '>', $start ) );
77 if ( $end !== '' ) {
78 $queryBuilder->andWhere( $dbr->expr( 'img_name', '<=', $end ) );
79 }
80
81 if ( $media_type !== '' ) {
82 $queryBuilder->andWhere( [ 'img_media_type' => $media_type ] );
83 }
84
85 if ( $major_mime !== '' ) {
86 $queryBuilder->andWhere( [ 'img_major_mime' => $major_mime ] );
87 }
88
89 if ( $minor_mime !== '' ) {
90 $queryBuilder->andWhere( [ 'img_minor_mime' => $minor_mime ] );
91 }
92
93 $res = $queryBuilder
94 ->orderBy( 'img_name', SelectQueryBuilder::SORT_ASC )
95 ->limit( $this->getBatchSize() )
96 ->caller( __METHOD__ )->fetchResultSet();
97
98 if ( $res->numRows() > 0 ) {
99 $row1 = $res->current();
100 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
101 $res->rewind();
102 }
103
104 $backendOperations = [];
105
106 foreach ( $res as $row ) {
107 $file = $repo->newFileFromRow( $row );
108 $headers = $file->getContentHeaders();
109 if ( $this->getOption( 'refreshContentType', false ) ) {
110 $headers['Content-Type'] = $row->img_major_mime . '/' . $row->img_minor_mime;
111 }
112
113 if ( count( $headers ) ) {
114 $backendOperations[] = [
115 'op' => 'describe', 'src' => $file->getPath(), 'headers' => $headers
116 ];
117 }
118
119 // Do all of the older file versions...
120 foreach ( $file->getHistory() as $oldFile ) {
121 $headers = $oldFile->getContentHeaders();
122 if ( count( $headers ) ) {
123 $backendOperations[] = [
124 'op' => 'describe', 'src' => $oldFile->getPath(), 'headers' => $headers
125 ];
126 }
127 }
128
129 if ( $this->hasOption( 'verbose' ) ) {
130 $this->output( "Queued headers update for file '{$row->img_name}'.\n" );
131 }
132
133 $start = $row->img_name; // advance
134 }
135
136 $backendOperationsCount = count( $backendOperations );
137 $count += $backendOperationsCount;
138
139 $this->output( "Updating headers for {$backendOperationsCount} file(s).\n" );
140 $this->updateFileHeaders( $repo, $backendOperations );
141 } while ( $res->numRows() === $this->getBatchSize() );
142
143 $this->output( "Done. Updated headers for $count file(s).\n" );
144 }
145
150 protected function updateFileHeaders( $repo, $backendOperations ) {
151 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
152
153 if ( !$status->isGood() ) {
154 $this->error( $status );
155 }
156 }
157}
158
159// @codeCoverageIgnoreStart
160$maintClass = RefreshFileHeaders::class;
161require_once RUN_MAINTENANCE_IF_MAIN;
162// @codeCoverageIgnoreEnd
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:57
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script to refresh file headers from metadata.
updateFileHeaders( $repo, $backendOperations)
execute()
Do the actual work.
__construct()
Default constructor.
Build SELECT queries with a fluent interface.