MediaWiki 1.40.4
SpecialMIMESearch.php
Go to the documentation of this file.
1<?php
31
38 protected $major, $minor, $mime;
39
41 private $languageConverter;
42
48 public function __construct(
49 ILoadBalancer $loadBalancer,
50 LinkBatchFactory $linkBatchFactory,
51 LanguageConverterFactory $languageConverterFactory
52 ) {
53 parent::__construct( 'MIMEsearch' );
54 $this->setDBLoadBalancer( $loadBalancer );
55 $this->setLinkBatchFactory( $linkBatchFactory );
56 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
57 }
58
59 public function isExpensive() {
60 return false;
61 }
62
63 public function isSyndicated() {
64 return false;
65 }
66
67 public function isCacheable() {
68 return false;
69 }
70
71 protected function linkParameters() {
72 return [ 'mime' => "{$this->major}/{$this->minor}" ];
73 }
74
75 public function getQueryInfo() {
76 $minorType = [];
77 if ( $this->minor !== '*' ) {
78 // Allow wildcard searching
79 $minorType['img_minor_mime'] = $this->minor;
80 }
81 $imgQuery = LocalFile::getQueryInfo();
82 $qi = [
83 'tables' => $imgQuery['tables'],
84 'fields' => [
85 'namespace' => NS_FILE,
86 'title' => 'img_name',
87 // Still have a value field just in case,
88 // but it isn't actually used for sorting.
89 'value' => 'img_name',
90 'img_size',
91 'img_width',
92 'img_height',
93 'img_user_text' => $imgQuery['fields']['img_user_text'],
94 'img_timestamp'
95 ],
96 'conds' => [
97 'img_major_mime' => $this->major,
98 // This is in order to trigger using
99 // the img_media_mime index in "range" mode.
100 // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
101 'img_media_type' => [
113 ],
114 ] + $minorType,
115 'join_conds' => $imgQuery['joins'],
116 ];
117
118 return $qi;
119 }
120
130 protected function getOrderFields() {
131 return [];
132 }
133
138 protected function getPageHeader() {
139 $formDescriptor = [
140 'mime' => [
141 'type' => 'combobox',
142 'options' => $this->getSuggestionsForTypes(),
143 'name' => 'mime',
144 'label-message' => 'mimetype',
145 'required' => true,
146 'default' => $this->mime,
147 ],
148 ];
149
150 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
151 ->setSubmitTextMsg( 'ilsubmit' )
152 ->setTitle( $this->getPageTitle() )
153 ->setMethod( 'get' )
154 ->prepareForm()
155 ->displayForm( false );
156 return '';
157 }
158
159 protected function getSuggestionsForTypes() {
160 $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
161 $lastMajor = null;
162 $suggestions = [];
163 $result = $dbr->select(
164 [ 'image' ],
165 // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
166 // sensible execution plan
167 [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
168 [],
169 __METHOD__,
170 [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
171 );
172 foreach ( $result as $row ) {
173 $major = $row->img_major_mime;
174 $minor = $row->img_minor_mime;
175 $suggestions[ "$major/$minor" ] = "$major/$minor";
176 if ( $lastMajor === $major ) {
177 // If there are at least two with the same major mime type, also include the wildcard
178 $suggestions[ "$major/*" ] = "$major/*";
179 }
180 $lastMajor = $major;
181 }
182 ksort( $suggestions );
183 return $suggestions;
184 }
185
186 public function execute( $par ) {
187 $this->addHelpLink( 'Help:Managing_files' );
188 $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
189 $this->mime = trim( $this->mime );
190 [ $this->major, $this->minor ] = File::splitMime( $this->mime );
191 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
192
193 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
194 !$mimeAnalyzer->isValidMajorMimeType( $this->major )
195 ) {
196 $this->setHeaders();
197 $this->outputHeader();
198 $this->getPageHeader();
199 return;
200 }
201
202 parent::execute( $par );
203 }
204
210 public function formatResult( $skin, $result ) {
211 $linkRenderer = $this->getLinkRenderer();
212 $nt = Title::makeTitle( $result->namespace, $result->title );
213
214 $text = $this->languageConverter->convertHtml( $nt->getText() );
215 $plink = $linkRenderer->makeLink(
216 Title::newFromText( $nt->getPrefixedText() ),
217 new HtmlArmor( $text )
218 );
219
220 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
221 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
222 $lang = $this->getLanguage();
223 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
224 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
225 $result->img_height )->escaped();
226 $user = $linkRenderer->makeLink(
227 Title::makeTitle( NS_USER, $result->img_user_text ),
228 $result->img_user_text
229 );
230
231 $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
232 $time = htmlspecialchars( $time );
233
234 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
235 }
236
237 public function preprocessResults( $db, $res ) {
239 }
240
241 protected function getGroupName() {
242 return 'media';
243 }
244}
const NS_USER
Definition Defines.php:66
const NS_FILE
Definition Defines.php:70
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
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:67
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
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:45
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
setDBLoadBalancer(ILoadBalancer $loadBalancer)
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
getDBLoadBalancer()
Searches the database for files of the requested MIME type, comparing this with the 'img_major_mime' ...
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
formatResult( $skin, $result)
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
preprocessResults( $db, $res)
Do any necessary preprocessing of the result object.
getPageHeader()
Generate and output the form.
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
isExpensive()
Should this query page only be updated offline on large wikis?
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $par)
This is the actual workhorse.
__construct(ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory, LanguageConverterFactory $languageConverterFactory)
getOrderFields()
The index is on (img_media_type, img_major_mime, img_minor_mime) which unfortunately doesn't have img...
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
getContentLanguage()
Shortcut to get content language.
The shared interface for all language converters.
This class is a delegate to ILBFactory for a given database cluster.
const MEDIATYPE_DRAWING
Definition defines.php:30
const MEDIATYPE_VIDEO
Definition defines.php:35
const MEDIATYPE_OFFICE
Definition defines.php:39
const MEDIATYPE_UNKNOWN
Definition defines.php:26
const MEDIATYPE_AUDIO
Definition defines.php:32
const MEDIATYPE_TEXT
Definition defines.php:41
const MEDIATYPE_BITMAP
Definition defines.php:28
const MEDIATYPE_MULTIMEDIA
Definition defines.php:37
const MEDIATYPE_EXECUTABLE
Definition defines.php:43
const MEDIATYPE_3D
Definition defines.php:47
const MEDIATYPE_ARCHIVE
Definition defines.php:45
if(!isset( $args[0])) $lang