MediaWiki master
refreshFileHeaders.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
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
146 protected function updateFileHeaders( $repo, $backendOperations ) {
147 $status = $repo->getBackend()->doQuickOperations( $backendOperations );
148
149 if ( !$status->isGood() ) {
150 $this->error( "Encountered error: " . print_r( $status, true ) );
151 }
152 }
153}
154
155$maintClass = RefreshFileHeaders::class;
156require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
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.