MediaWiki REL1_39
SpecialMIMESearch.php
Go to the documentation of this file.
1<?php
29
36 protected $major, $minor, $mime;
37
39 private $languageConverter;
40
46 public function __construct(
47 ILoadBalancer $loadBalancer,
48 LinkBatchFactory $linkBatchFactory,
49 LanguageConverterFactory $languageConverterFactory
50 ) {
51 parent::__construct( 'MIMEsearch' );
52 $this->setDBLoadBalancer( $loadBalancer );
53 $this->setLinkBatchFactory( $linkBatchFactory );
54 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
55 }
56
57 public function isExpensive() {
58 return false;
59 }
60
61 public function isSyndicated() {
62 return false;
63 }
64
65 public function isCacheable() {
66 return false;
67 }
68
69 protected function linkParameters() {
70 return [ 'mime' => "{$this->major}/{$this->minor}" ];
71 }
72
73 public function getQueryInfo() {
74 $minorType = [];
75 if ( $this->minor !== '*' ) {
76 // Allow wildcard searching
77 $minorType['img_minor_mime'] = $this->minor;
78 }
79 $imgQuery = LocalFile::getQueryInfo();
80 $qi = [
81 'tables' => $imgQuery['tables'],
82 'fields' => [
83 'namespace' => NS_FILE,
84 'title' => 'img_name',
85 // Still have a value field just in case,
86 // but it isn't actually used for sorting.
87 'value' => 'img_name',
88 'img_size',
89 'img_width',
90 'img_height',
91 'img_user_text' => $imgQuery['fields']['img_user_text'],
92 'img_timestamp'
93 ],
94 'conds' => [
95 'img_major_mime' => $this->major,
96 // This is in order to trigger using
97 // the img_media_mime index in "range" mode.
98 // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
99 'img_media_type' => [
111 ],
112 ] + $minorType,
113 'join_conds' => $imgQuery['joins'],
114 ];
115
116 return $qi;
117 }
118
128 protected function getOrderFields() {
129 return [];
130 }
131
136 protected function getPageHeader() {
137 $formDescriptor = [
138 'mime' => [
139 'type' => 'combobox',
140 'options' => $this->getSuggestionsForTypes(),
141 'name' => 'mime',
142 'label-message' => 'mimetype',
143 'required' => true,
144 'default' => $this->mime,
145 ],
146 ];
147
148 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
149 ->setSubmitTextMsg( 'ilsubmit' )
150 ->setTitle( $this->getPageTitle() )
151 ->setMethod( 'get' )
152 ->prepareForm()
153 ->displayForm( false );
154 return '';
155 }
156
157 protected function getSuggestionsForTypes() {
158 $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
159 $lastMajor = null;
160 $suggestions = [];
161 $result = $dbr->select(
162 [ 'image' ],
163 // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
164 // sensible execution plan
165 [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
166 [],
167 __METHOD__,
168 [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
169 );
170 foreach ( $result as $row ) {
171 $major = $row->img_major_mime;
172 $minor = $row->img_minor_mime;
173 $suggestions[ "$major/$minor" ] = "$major/$minor";
174 if ( $lastMajor === $major ) {
175 // If there are at least two with the same major mime type, also include the wildcard
176 $suggestions[ "$major/*" ] = "$major/*";
177 }
178 $lastMajor = $major;
179 }
180 ksort( $suggestions );
181 return $suggestions;
182 }
183
184 public function execute( $par ) {
185 $this->addHelpLink( 'Help:Managing_files' );
186 $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
187 $this->mime = trim( $this->mime );
188 list( $this->major, $this->minor ) = File::splitMime( $this->mime );
189 $mimeAnalyzer = MediaWikiServices::getInstance()->getMimeAnalyzer();
190
191 if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
192 !$mimeAnalyzer->isValidMajorMimeType( $this->major )
193 ) {
194 $this->setHeaders();
195 $this->outputHeader();
196 $this->getPageHeader();
197 return;
198 }
199
200 parent::execute( $par );
201 }
202
208 public function formatResult( $skin, $result ) {
209 $linkRenderer = $this->getLinkRenderer();
210 $nt = Title::makeTitle( $result->namespace, $result->title );
211
212 $text = $this->languageConverter->convertHtml( $nt->getText() );
213 $plink = $linkRenderer->makeLink(
214 Title::newFromText( $nt->getPrefixedText() ),
215 new HtmlArmor( $text )
216 );
217
218 $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
219 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
220 $lang = $this->getLanguage();
221 $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
222 $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
223 $result->img_height )->escaped();
224 $user = $linkRenderer->makeLink(
225 Title::makeTitle( NS_USER, $result->img_user_text ),
226 $result->img_user_text
227 );
228
229 $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
230 $time = htmlspecialchars( $time );
231
232 return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
233 }
234
235 public function preprocessResults( $db, $res ) {
237 }
238
239 protected function getGroupName() {
240 return 'media';
241 }
242}
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
static makeMediaLinkObj( $title, $html='', $time=false)
Create a direct link to a given uploaded file.
Definition Linker.php:974
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Service locator for MediaWiki core services.
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:42
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.
Create and track the database connections and transactions 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