MediaWiki  master
SpecialMIMESearch.php
Go to the documentation of this file.
1 <?php
25 namespace MediaWiki\Specials;
26 
27 use File;
28 use HtmlArmor;
29 use HTMLForm;
31 use LocalFile;
36 use QueryPage;
37 use Skin;
38 use stdClass;
40 
47  protected $major, $minor, $mime;
48 
50  private $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' => [
121  MEDIATYPE_3D,
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  $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
170  $lastMajor = null;
171  $suggestions = [];
172  $result = $dbr->select(
173  [ 'image' ],
174  // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
175  // sensible execution plan
176  [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
177  [],
178  __METHOD__,
179  [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
180  );
181  foreach ( $result as $row ) {
182  $major = $row->img_major_mime;
183  $minor = $row->img_minor_mime;
184  $suggestions[ "$major/$minor" ] = "$major/$minor";
185  if ( $lastMajor === $major ) {
186  // If there are at least two with the same major mime type, also include the wildcard
187  $suggestions[ "$major/*" ] = "$major/*";
188  }
189  $lastMajor = $major;
190  }
191  ksort( $suggestions );
192  return $suggestions;
193  }
194 
195  public function execute( $par ) {
196  $this->addHelpLink( 'Help:Managing_files' );
197  $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
198  $this->mime = trim( $this->mime );
199  [ $this->major, $this->minor ] = File::splitMime( $this->mime );
200 
201  if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
202  !self::isValidType( $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 
249  protected static function isValidType( $type ) {
250  // From maintenance/tables.sql => img_major_mime
251  $types = [
252  'unknown',
253  'application',
254  'audio',
255  'image',
256  'text',
257  'video',
258  'message',
259  'model',
260  'multipart',
261  'chemical'
262  ];
263 
264  return in_array( $type, $types );
265  }
266 
267  public function preprocessResults( $db, $res ) {
269  }
270 
271  protected function getGroupName() {
272  return 'media';
273  }
274 }
275 
280 class_alias( SpecialMIMESearch::class, 'SpecialMIMESearch' );
const NS_USER
Definition: Defines.php:66
const NS_FILE
Definition: Defines.php:70
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:68
static splitMime(?string $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
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:155
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:354
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
Local file in the wiki's own database.
Definition: LocalFile.php:61
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Definition: LocalFile.php:272
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
static makeMediaLinkObj( $title, $html='', $time=false)
Create a direct link to a given uploaded file.
Definition: Linker.php:1003
Searches the database for files of the requested MIME type, comparing this with the 'img_major_mime' ...
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:82
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:425
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:693
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:65
setDatabaseProvider(IConnectionProvider $databaseProvider)
Definition: QueryPage.php:964
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:925
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:162
getDatabaseProvider()
Definition: QueryPage.php:972
The main skin class which provides methods and properties for all other skins.
Definition: Skin.php:57
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.
Provide primary and replica IDatabase connections.
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