MediaWiki master
SpecialMIMESearch.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Specials;
26
27use File;
28use HtmlArmor;
30use LocalFile;
38use Skin;
39use stdClass;
41
48 protected $major;
49 protected $minor;
50 protected $mime;
51
52 private ILanguageConverter $languageConverter;
53
59 public function __construct(
60 IConnectionProvider $dbProvider,
61 LinkBatchFactory $linkBatchFactory,
62 LanguageConverterFactory $languageConverterFactory
63 ) {
64 parent::__construct( 'MIMEsearch' );
65 $this->setDatabaseProvider( $dbProvider );
66 $this->setLinkBatchFactory( $linkBatchFactory );
67 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
68 }
69
70 public function isExpensive() {
71 return false;
72 }
73
74 public function isSyndicated() {
75 return false;
76 }
77
78 public function isCacheable() {
79 return false;
80 }
81
82 protected function linkParameters() {
83 return [ 'mime' => "{$this->major}/{$this->minor}" ];
84 }
85
86 public function getQueryInfo() {
87 $minorType = [];
88 if ( $this->minor !== '*' ) {
89 // Allow wildcard searching
90 $minorType['img_minor_mime'] = $this->minor;
91 }
92 $imgQuery = LocalFile::getQueryInfo();
93 $qi = [
94 'tables' => $imgQuery['tables'],
95 'fields' => [
96 'namespace' => NS_FILE,
97 'title' => 'img_name',
98 // Still have a value field just in case,
99 // but it isn't actually used for sorting.
100 'value' => 'img_name',
101 'img_size',
102 'img_width',
103 'img_height',
104 'img_user_text' => $imgQuery['fields']['img_user_text'],
105 'img_timestamp'
106 ],
107 'conds' => [
108 'img_major_mime' => $this->major,
109 // This is in order to trigger using
110 // the img_media_mime index in "range" mode.
111 // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
112 'img_media_type' => [
124 ],
125 ] + $minorType,
126 'join_conds' => $imgQuery['joins'],
127 ];
128
129 return $qi;
130 }
131
141 protected function getOrderFields() {
142 return [];
143 }
144
149 protected function getPageHeader() {
150 $formDescriptor = [
151 'mime' => [
152 'type' => 'combobox',
153 'options' => $this->getSuggestionsForTypes(),
154 'name' => 'mime',
155 'label-message' => 'mimetype',
156 'required' => true,
157 'default' => $this->mime,
158 ],
159 ];
160
161 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
162 ->setSubmitTextMsg( 'ilsubmit' )
163 ->setTitle( $this->getPageTitle() )
164 ->setMethod( 'get' )
165 ->prepareForm()
166 ->displayForm( false );
167 return '';
168 }
169
170 protected function getSuggestionsForTypes() {
171 $queryBuilder = $this->getDatabaseProvider()->getReplicaDatabase()->newSelectQueryBuilder();
172 $queryBuilder
173 // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
174 // sensible execution plan
175 ->select( [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] )
176 ->from( 'image' )
177 ->groupBy( [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] );
178 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
179
180 $lastMajor = null;
181 $suggestions = [];
182 foreach ( $result as $row ) {
183 $major = $row->img_major_mime;
184 $minor = $row->img_minor_mime;
185 $suggestions[ "$major/$minor" ] = "$major/$minor";
186 if ( $lastMajor === $major ) {
187 // If there are at least two with the same major mime type, also include the wildcard
188 $suggestions[ "$major/*" ] = "$major/*";
189 }
190 $lastMajor = $major;
191 }
192 ksort( $suggestions );
193 return $suggestions;
194 }
195
196 public function execute( $par ) {
197 $this->addHelpLink( 'Help:Managing_files' );
198 $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
199 $this->mime = trim( $this->mime );
200 [ $this->major, $this->minor ] = File::splitMime( $this->mime );
201 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
202
203 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
204 !$mimeAnalyzer->isValidMajorMimeType( $this->major )
205 ) {
206 $this->setHeaders();
207 $this->outputHeader();
208 $this->getPageHeader();
209 return;
210 }
211
212 parent::execute( $par );
213 }
214
220 public function formatResult( $skin, $result ) {
221 $linkRenderer = $this->getLinkRenderer();
222 $nt = Title::makeTitle( $result->namespace, $result->title );
223
224 $text = $this->languageConverter->convertHtml( $nt->getText() );
225 $plink = $linkRenderer->makeLink(
226 Title::newFromText( $nt->getPrefixedText() ),
227 new HtmlArmor( $text )
228 );
229
230 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
231 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
232 $lang = $this->getLanguage();
233 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
234 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
235 $result->img_height )->escaped();
236 $user = $linkRenderer->makeLink(
237 Title::makeTitle( NS_USER, $result->img_user_text ),
238 $result->img_user_text
239 );
240
241 $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
242 $time = htmlspecialchars( $time );
243
244 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
245 }
246
247 public function preprocessResults( $db, $res ) {
248 $this->executeLBFromResultWrapper( $res );
249 }
250
251 protected function getGroupName() {
252 return 'media';
253 }
254}
255
260class_alias( SpecialMIMESearch::class, 'SpecialMIMESearch' );
const NS_USER
Definition Defines.php:66
const NS_FILE
Definition Defines.php:70
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:73
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
Local file in the wiki's own database.
Definition LocalFile.php:68
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
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:65
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
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:89
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 Per default the message key is the canonical name o...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Searches the database for files of the requested MIME type, comparing this with the 'img_major_mime' ...
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
The base class for all skins.
Definition Skin.php:58
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
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...