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  ->setTitle( $this->getPageTitle() )
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 }
const NS_USER
Definition: Defines.php:66
const NS_FILE
Definition: Defines.php:70
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:307
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:338
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:929
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:270
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
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...
Definition: QueryPage.php:877
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:895
LinkBatchFactory null $linkBatchFactory
Definition: QueryPage.php:75
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:136
getDBLoadBalancer()
Definition: QueryPage.php:903
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:72
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()
Is this query expensive (for some definition of expensive)? Then we don't let it run in miser mode.
static isValidType( $type)
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...
ILanguageConverter $languageConverter
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.
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:81
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.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
Database cluster connection, tracking, load balancing, and transaction manager interface.
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
const DB_REPLICA
Definition: defines.php:26
if(!isset( $args[0])) $lang