MediaWiki master
SpecialMIMESearch.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
35use stdClass;
38
48 protected $major;
50 protected $minor;
52 protected $mime;
53
54 private ILanguageConverter $languageConverter;
55
56 public function __construct(
57 IConnectionProvider $dbProvider,
58 LinkBatchFactory $linkBatchFactory,
59 LanguageConverterFactory $languageConverterFactory
60 ) {
61 parent::__construct( 'MIMEsearch' );
62 $this->setDatabaseProvider( $dbProvider );
63 $this->setLinkBatchFactory( $linkBatchFactory );
64 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
65 }
66
67 public function isExpensive() {
68 return false;
69 }
70
71 public function isSyndicated() {
72 return false;
73 }
74
75 public function isCacheable() {
76 return false;
77 }
78
79 protected function linkParameters() {
80 return [ 'mime' => "{$this->major}/{$this->minor}" ];
81 }
82
83 public function getQueryInfo() {
84 $minorType = [];
85 if ( $this->minor !== '*' ) {
86 // Allow wildcard searching
87 $minorType['img_minor_mime'] = $this->minor;
88 }
89 $fileQuery = FileSelectQueryBuilder::newForFile( $this->getDatabaseProvider()->getReplicaDatabase() )
90 ->getQueryInfo();
91 $qi = [
92 'tables' => $fileQuery['tables'],
93 'fields' => [
94 'namespace' => NS_FILE,
95 'title' => 'img_name',
96 // Still have a value field just in case,
97 // but it isn't actually used for sorting.
98 'value' => 'img_name',
99 'img_size',
100 'img_width',
101 'img_height',
102 'img_timestamp'
103 ],
104 'conds' => [
105 'img_major_mime' => $this->major,
106 // This is in order to trigger using
107 // the img_media_mime index in "range" mode.
108 // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
109 'img_media_type' => [
121 ],
122 ] + $minorType,
123 'join_conds' => $fileQuery['join_conds'],
124 ];
125
126 if ( isset( $fileQuery['fields']['img_user_text'] ) ) {
127 $qi['fields']['img_user_text'] = $fileQuery['fields']['img_user_text'];
128 } else {
129 // file read new
130 $qi['fields'][] = 'img_user_text';
131 }
132
133 return $qi;
134 }
135
145 protected function getOrderFields() {
146 return [];
147 }
148
153 protected function getPageHeader() {
154 $formDescriptor = [
155 'mime' => [
156 'type' => 'combobox',
157 'options' => $this->getSuggestionsForTypes(),
158 'name' => 'mime',
159 'label-message' => 'mimetype',
160 'required' => true,
161 'default' => $this->mime,
162 ],
163 ];
164
165 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
166 ->setSubmitTextMsg( 'ilsubmit' )
167 ->setTitle( $this->getPageTitle() )
168 ->setMethod( 'get' )
169 ->prepareForm()
170 ->displayForm( false );
171 return '';
172 }
173
174 protected function getSuggestionsForTypes() {
175 $queryBuilder = $this->getDatabaseProvider()->getReplicaDatabase()->newSelectQueryBuilder();
176 $migrationStage = MediaWikiServices::getInstance()->getMainConfig()->get(
178 );
179 if ( $migrationStage & SCHEMA_COMPAT_READ_OLD ) {
180 $queryBuilder
181 // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
182 // sensible execution plan
183 ->select( [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] )
184 ->from( 'image' )
185 ->groupBy( [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] );
186 } else {
187 $queryBuilder->select(
188 [
189 'img_media_type' => 'ft_media_type',
190 'img_major_mime' => 'ft_major_mime',
191 'img_minor_mime' => 'ft_minor_mime',
192 ]
193 )
194 ->from( 'filetypes' );
195 }
196
197 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
198
199 $lastMajor = null;
200 $suggestions = [];
201 foreach ( $result as $row ) {
202 $major = $row->img_major_mime;
203 $minor = $row->img_minor_mime;
204 $suggestions[ "$major/$minor" ] = "$major/$minor";
205 if ( $lastMajor === $major ) {
206 // If there are at least two with the same major mime type, also include the wildcard
207 $suggestions[ "$major/*" ] = "$major/*";
208 }
209 $lastMajor = $major;
210 }
211 ksort( $suggestions );
212 return $suggestions;
213 }
214
215 public function execute( $par ) {
216 $this->addHelpLink( 'Help:Managing_files' );
217 $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
218 $this->mime = trim( $this->mime );
219 [ $this->major, $this->minor ] = File::splitMime( $this->mime );
220 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
221
222 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
223 !$mimeAnalyzer->isValidMajorMimeType( $this->major )
224 ) {
225 $this->setHeaders();
226 $this->outputHeader();
227 $this->getPageHeader();
228 return;
229 }
230
231 parent::execute( $par );
232 }
233
239 public function formatResult( $skin, $result ) {
240 $linkRenderer = $this->getLinkRenderer();
241 $nt = Title::makeTitle( $result->namespace, $result->title );
242
243 $text = $this->languageConverter->convertHtml( $nt->getText() );
244 $plink = $linkRenderer->makeLink(
245 Title::newFromText( $nt->getPrefixedText() ),
246 new HtmlArmor( $text )
247 );
248
249 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
250 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
251 $lang = $this->getLanguage();
252 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
253 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
254 $result->img_height )->escaped();
255 $user = $linkRenderer->makeLink(
256 Title::makeTitle( NS_USER, $result->img_user_text ),
257 $result->img_user_text
258 );
259
260 $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
261 $time = htmlspecialchars( $time );
262
263 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
264 }
265
266 public function preprocessResults( $db, $res ) {
267 $this->executeLBFromResultWrapper( $res );
268 }
269
270 protected function getGroupName() {
271 return 'media';
272 }
273}
274
279class_alias( SpecialMIMESearch::class, 'SpecialMIMESearch' );
const NS_USER
Definition Defines.php:67
const NS_FILE
Definition Defines.php:71
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:304
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Some internal bits split of from Skin.php.
Definition Linker.php:61
A class containing constants representing the names of configuration variables.
const FileSchemaMigrationStage
Name constant for the FileSchemaMigrationStage setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The base class for all skins.
Definition Skin.php:58
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:87
setDatabaseProvider(IConnectionProvider $databaseProvider)
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getContentLanguage()
Shortcut to get content language.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Search the database for files of the requested MIME type, comparing this with the 'img_major_mime' an...
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
__construct(IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory, LanguageConverterFactory $languageConverterFactory)
isExpensive()
Should this query page only be updated offline on large wikis?
preprocessResults( $db, $res)
Do any necessary preprocessing of the result object.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $par)
This is the actual workhorse.
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
getOrderFields()
The index is on (img_media_type, img_major_mime, img_minor_mime) which unfortunately doesn't have img...
getPageHeader()
Generate and output the form.
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Represents a title within MediaWiki.
Definition Title.php:78
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:32
The shared interface for all language converters.
Provide primary and replica IDatabase connections.
const MEDIATYPE_DRAWING
Definition defines.php:31
const MEDIATYPE_VIDEO
Definition defines.php:36
const MEDIATYPE_OFFICE
Definition defines.php:40
const MEDIATYPE_UNKNOWN
Definition defines.php:27
const MEDIATYPE_AUDIO
Definition defines.php:33
const MEDIATYPE_TEXT
Definition defines.php:42
const MEDIATYPE_BITMAP
Definition defines.php:29
const MEDIATYPE_MULTIMEDIA
Definition defines.php:38
const MEDIATYPE_EXECUTABLE
Definition defines.php:44
const MEDIATYPE_3D
Definition defines.php:48
const MEDIATYPE_ARCHIVE
Definition defines.php:46