MediaWiki  master
SpecialMIMESearch.php
Go to the documentation of this file.
1 <?php
28 
35  protected $major, $minor, $mime;
36 
39 
45  public function __construct(
48  LanguageConverterFactory $languageConverterFactory
49  ) {
50  parent::__construct( 'MIMEsearch' );
51  $this->setDBLoadBalancer( $loadBalancer );
52  $this->setLinkBatchFactory( $linkBatchFactory );
53  $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
54  }
55 
56  public function isExpensive() {
57  return false;
58  }
59 
60  public function isSyndicated() {
61  return false;
62  }
63 
64  public function isCacheable() {
65  return false;
66  }
67 
68  protected function linkParameters() {
69  return [ 'mime' => "{$this->major}/{$this->minor}" ];
70  }
71 
72  public function getQueryInfo() {
73  $minorType = [];
74  if ( $this->minor !== '*' ) {
75  // Allow wildcard searching
76  $minorType['img_minor_mime'] = $this->minor;
77  }
78  $imgQuery = LocalFile::getQueryInfo();
79  $qi = [
80  'tables' => $imgQuery['tables'],
81  'fields' => [
82  'namespace' => NS_FILE,
83  'title' => 'img_name',
84  // Still have a value field just in case,
85  // but it isn't actually used for sorting.
86  'value' => 'img_name',
87  'img_size',
88  'img_width',
89  'img_height',
90  'img_user_text' => $imgQuery['fields']['img_user_text'],
91  'img_timestamp'
92  ],
93  'conds' => [
94  'img_major_mime' => $this->major,
95  // This is in order to trigger using
96  // the img_media_mime index in "range" mode.
97  // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
98  'img_media_type' => [
109  MEDIATYPE_3D,
110  ],
111  ] + $minorType,
112  'join_conds' => $imgQuery['joins'],
113  ];
114 
115  return $qi;
116  }
117 
127  protected function getOrderFields() {
128  return [];
129  }
130 
135  protected function getPageHeader() {
136  $formDescriptor = [
137  'mime' => [
138  'type' => 'combobox',
139  'options' => $this->getSuggestionsForTypes(),
140  'name' => 'mime',
141  'label-message' => 'mimetype',
142  'required' => true,
143  'default' => $this->mime,
144  ],
145  ];
146 
147  HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
148  ->setSubmitTextMsg( 'ilsubmit' )
149  ->setAction( $this->getPageTitle()->getLocalURL() )
150  ->setMethod( 'get' )
151  ->prepareForm()
152  ->displayForm( false );
153  return '';
154  }
155 
156  protected function getSuggestionsForTypes() {
157  $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
158  $lastMajor = null;
159  $suggestions = [];
160  $result = $dbr->select(
161  [ 'image' ],
162  // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
163  // sensible execution plan
164  [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
165  [],
166  __METHOD__,
167  [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
168  );
169  foreach ( $result as $row ) {
170  $major = $row->img_major_mime;
171  $minor = $row->img_minor_mime;
172  $suggestions[ "$major/$minor" ] = "$major/$minor";
173  if ( $lastMajor === $major ) {
174  // If there are at least two with the same major mime type, also include the wildcard
175  $suggestions[ "$major/*" ] = "$major/*";
176  }
177  $lastMajor = $major;
178  }
179  ksort( $suggestions );
180  return $suggestions;
181  }
182 
183  public function execute( $par ) {
184  $this->addHelpLink( 'Help:Managing_files' );
185  $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
186  $this->mime = trim( $this->mime );
187  list( $this->major, $this->minor ) = File::splitMime( $this->mime );
188 
189  if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
190  !self::isValidType( $this->major )
191  ) {
192  $this->setHeaders();
193  $this->outputHeader();
194  $this->getPageHeader();
195  return;
196  }
197 
198  parent::execute( $par );
199  }
200 
206  public function formatResult( $skin, $result ) {
207  $linkRenderer = $this->getLinkRenderer();
208  $nt = Title::makeTitle( $result->namespace, $result->title );
209 
210  $text = $this->languageConverter->convertHtml( $nt->getText() );
211  $plink = $linkRenderer->makeLink(
212  Title::newFromText( $nt->getPrefixedText() ),
213  new HtmlArmor( $text )
214  );
215 
216  $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
217  $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
218  $lang = $this->getLanguage();
219  $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
220  $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
221  $result->img_height )->escaped();
222  $user = $linkRenderer->makeLink(
223  Title::makeTitle( NS_USER, $result->img_user_text ),
224  $result->img_user_text
225  );
226 
227  $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
228  $time = htmlspecialchars( $time );
229 
230  return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
231  }
232 
237  protected static function isValidType( $type ) {
238  // From maintenance/tables.sql => img_major_mime
239  $types = [
240  'unknown',
241  'application',
242  'audio',
243  'image',
244  'text',
245  'video',
246  'message',
247  'model',
248  'multipart',
249  'chemical'
250  ];
251 
252  return in_array( $type, $types );
253  }
254 
255  public function preprocessResults( $db, $res ) {
257  }
258 
259  protected function getGroupName() {
260  return 'media';
261  }
262 }
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:80
SpecialMIMESearch\$mime
$mime
Definition: SpecialMIMESearch.php:35
SpecialMIMESearch\getOrderFields
getOrderFields()
The index is on (img_media_type, img_major_mime, img_minor_mime) which unfortunately doesn't have img...
Definition: SpecialMIMESearch.php:127
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
QueryPage\getDBLoadBalancer
getDBLoadBalancer()
Definition: QueryPage.php:900
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
MEDIATYPE_AUDIO
const MEDIATYPE_AUDIO
Definition: defines.php:32
SpecialMIMESearch\getPageHeader
getPageHeader()
Generate and output the form.
Definition: SpecialMIMESearch.php:135
SpecialMIMESearch\__construct
__construct(ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory, LanguageConverterFactory $languageConverterFactory)
Definition: SpecialMIMESearch.php:45
SpecialMIMESearch\linkParameters
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
Definition: SpecialMIMESearch.php:68
SpecialMIMESearch
Searches the database for files of the requested MIME type, comparing this with the 'img_major_mime' ...
Definition: SpecialMIMESearch.php:34
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
MEDIATYPE_OFFICE
const MEDIATYPE_OFFICE
Definition: defines.php:39
MEDIATYPE_DRAWING
const MEDIATYPE_DRAWING
Definition: defines.php:30
MEDIATYPE_UNKNOWN
const MEDIATYPE_UNKNOWN
Definition: defines.php:26
File\splitMime
static splitMime( $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition: File.php:306
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
$res
$res
Definition: testCompression.php:57
SpecialMIMESearch\isExpensive
isExpensive()
Is this query expensive (for some definition of expensive)? Then we don't let it run in miser mode.
Definition: SpecialMIMESearch.php:56
SpecialMIMESearch\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialMIMESearch.php:72
QueryPage
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:41
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:46
QueryPage\executeLBFromResultWrapper
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
Definition: QueryPage.php:874
$dbr
$dbr
Definition: testCompression.php:54
QueryPage\setLinkBatchFactory
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:135
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:948
SpecialMIMESearch\getSuggestionsForTypes
getSuggestionsForTypes()
Definition: SpecialMIMESearch.php:156
Linker\makeMediaLinkObj
static makeMediaLinkObj( $title, $html='', $time=false)
Create a direct link to a given uploaded file.
Definition: Linker.php:924
MediaWiki\Languages\LanguageConverterFactory\getLanguageConverter
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Definition: LanguageConverterFactory.php:125
SpecialMIMESearch\$languageConverter
ILanguageConverter $languageConverter
Definition: SpecialMIMESearch.php:38
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
MEDIATYPE_3D
const MEDIATYPE_3D
Definition: defines.php:47
SpecialMIMESearch\preprocessResults
preprocessResults( $db, $res)
Do any necessary preprocessing of the result object.
Definition: SpecialMIMESearch.php:255
SpecialMIMESearch\$major
$major
Definition: SpecialMIMESearch.php:35
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
MEDIATYPE_BITMAP
const MEDIATYPE_BITMAP
Definition: defines.php:28
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:663
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
SpecialMIMESearch\isCacheable
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Definition: SpecialMIMESearch.php:64
MEDIATYPE_EXECUTABLE
const MEDIATYPE_EXECUTABLE
Definition: defines.php:43
MEDIATYPE_MULTIMEDIA
const MEDIATYPE_MULTIMEDIA
Definition: defines.php:37
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:29
SpecialMIMESearch\isSyndicated
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
Definition: SpecialMIMESearch.php:60
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:780
QueryPage\setDBLoadBalancer
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:892
MEDIATYPE_ARCHIVE
const MEDIATYPE_ARCHIVE
Definition: defines.php:45
NS_USER
const NS_USER
Definition: Defines.php:66
QueryPage\$linkBatchFactory
LinkBatchFactory null $linkBatchFactory
Definition: QueryPage.php:74
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1028
LocalFile\getQueryInfo
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:266
MEDIATYPE_TEXT
const MEDIATYPE_TEXT
Definition: defines.php:41
SpecialMIMESearch\formatResult
formatResult( $skin, $result)
Definition: SpecialMIMESearch.php:206
MEDIATYPE_VIDEO
const MEDIATYPE_VIDEO
Definition: defines.php:35
SpecialMIMESearch\$minor
$minor
Definition: SpecialMIMESearch.php:35
SpecialMIMESearch\isValidType
static isValidType( $type)
Definition: SpecialMIMESearch.php:237
SpecialMIMESearch\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialMIMESearch.php:183
SpecialMIMESearch\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialMIMESearch.php:259
QueryPage\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:71
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:840
NS_FILE
const NS_FILE
Definition: Defines.php:70
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:709
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
$type
$type
Definition: testCompression.php:52