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