MediaWiki master
SpecialMIMESearch.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
23use File;
24use HtmlArmor;
26use LocalFile;
34use Skin;
35use stdClass;
37
46 protected $major;
47 protected $minor;
48 protected $mime;
49
50 private ILanguageConverter $languageConverter;
51
57 public function __construct(
58 IConnectionProvider $dbProvider,
59 LinkBatchFactory $linkBatchFactory,
60 LanguageConverterFactory $languageConverterFactory
61 ) {
62 parent::__construct( 'MIMEsearch' );
63 $this->setDatabaseProvider( $dbProvider );
64 $this->setLinkBatchFactory( $linkBatchFactory );
65 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
66 }
67
68 public function isExpensive() {
69 return false;
70 }
71
72 public function isSyndicated() {
73 return false;
74 }
75
76 public function isCacheable() {
77 return false;
78 }
79
80 protected function linkParameters() {
81 return [ 'mime' => "{$this->major}/{$this->minor}" ];
82 }
83
84 public function getQueryInfo() {
85 $minorType = [];
86 if ( $this->minor !== '*' ) {
87 // Allow wildcard searching
88 $minorType['img_minor_mime'] = $this->minor;
89 }
90 $imgQuery = LocalFile::getQueryInfo();
91 $qi = [
92 'tables' => $imgQuery['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_user_text' => $imgQuery['fields']['img_user_text'],
103 'img_timestamp'
104 ],
105 'conds' => [
106 'img_major_mime' => $this->major,
107 // This is in order to trigger using
108 // the img_media_mime index in "range" mode.
109 // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
110 'img_media_type' => [
122 ],
123 ] + $minorType,
124 'join_conds' => $imgQuery['joins'],
125 ];
126
127 return $qi;
128 }
129
139 protected function getOrderFields() {
140 return [];
141 }
142
147 protected function getPageHeader() {
148 $formDescriptor = [
149 'mime' => [
150 'type' => 'combobox',
151 'options' => $this->getSuggestionsForTypes(),
152 'name' => 'mime',
153 'label-message' => 'mimetype',
154 'required' => true,
155 'default' => $this->mime,
156 ],
157 ];
158
159 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
160 ->setSubmitTextMsg( 'ilsubmit' )
161 ->setTitle( $this->getPageTitle() )
162 ->setMethod( 'get' )
163 ->prepareForm()
164 ->displayForm( false );
165 return '';
166 }
167
168 protected function getSuggestionsForTypes() {
169 $queryBuilder = $this->getDatabaseProvider()->getReplicaDatabase()->newSelectQueryBuilder();
170 $queryBuilder
171 // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
172 // sensible execution plan
173 ->select( [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] )
174 ->from( 'image' )
175 ->groupBy( [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] );
176 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
177
178 $lastMajor = null;
179 $suggestions = [];
180 foreach ( $result as $row ) {
181 $major = $row->img_major_mime;
182 $minor = $row->img_minor_mime;
183 $suggestions[ "$major/$minor" ] = "$major/$minor";
184 if ( $lastMajor === $major ) {
185 // If there are at least two with the same major mime type, also include the wildcard
186 $suggestions[ "$major/*" ] = "$major/*";
187 }
188 $lastMajor = $major;
189 }
190 ksort( $suggestions );
191 return $suggestions;
192 }
193
194 public function execute( $par ) {
195 $this->addHelpLink( 'Help:Managing_files' );
196 $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
197 $this->mime = trim( $this->mime );
198 [ $this->major, $this->minor ] = File::splitMime( $this->mime );
199 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
200
201 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
202 !$mimeAnalyzer->isValidMajorMimeType( $this->major )
203 ) {
204 $this->setHeaders();
205 $this->outputHeader();
206 $this->getPageHeader();
207 return;
208 }
209
210 parent::execute( $par );
211 }
212
218 public function formatResult( $skin, $result ) {
219 $linkRenderer = $this->getLinkRenderer();
220 $nt = Title::makeTitle( $result->namespace, $result->title );
221
222 $text = $this->languageConverter->convertHtml( $nt->getText() );
223 $plink = $linkRenderer->makeLink(
224 Title::newFromText( $nt->getPrefixedText() ),
225 new HtmlArmor( $text )
226 );
227
228 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
229 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
230 $lang = $this->getLanguage();
231 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
232 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
233 $result->img_height )->escaped();
234 $user = $linkRenderer->makeLink(
235 Title::makeTitle( NS_USER, $result->img_user_text ),
236 $result->img_user_text
237 );
238
239 $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
240 $time = htmlspecialchars( $time );
241
242 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
243 }
244
245 public function preprocessResults( $db, $res ) {
246 $this->executeLBFromResultWrapper( $res );
247 }
248
249 protected function getGroupName() {
250 return 'media';
251 }
252}
253
258class_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: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: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: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 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:59
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