123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- /**
- * Implements Special:MIMESearch
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @ingroup SpecialPage
- * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
- */
- use MediaWiki\MediaWikiServices;
- /**
- * Searches the database for files of the requested MIME type, comparing this with the
- * 'img_major_mime' and 'img_minor_mime' fields in the image table.
- * @ingroup SpecialPage
- */
- class SpecialMIMESearch extends QueryPage {
- protected $major, $minor, $mime;
- function __construct( $name = 'MIMEsearch' ) {
- parent::__construct( $name );
- }
- public function isExpensive() {
- return false;
- }
- function isSyndicated() {
- return false;
- }
- function isCacheable() {
- return false;
- }
- function linkParameters() {
- return [ 'mime' => "{$this->major}/{$this->minor}" ];
- }
- public function getQueryInfo() {
- $minorType = [];
- if ( $this->minor !== '*' ) {
- // Allow wildcard searching
- $minorType['img_minor_mime'] = $this->minor;
- }
- $imgQuery = LocalFile::getQueryInfo();
- $qi = [
- 'tables' => $imgQuery['tables'],
- 'fields' => [
- 'namespace' => NS_FILE,
- 'title' => 'img_name',
- // Still have a value field just in case,
- // but it isn't actually used for sorting.
- 'value' => 'img_name',
- 'img_size',
- 'img_width',
- 'img_height',
- 'img_user_text' => $imgQuery['fields']['img_user_text'],
- 'img_timestamp'
- ],
- 'conds' => [
- 'img_major_mime' => $this->major,
- // This is in order to trigger using
- // the img_media_mime index in "range" mode.
- // @todo how is order defined? use MimeAnalyzer::getMediaTypes?
- 'img_media_type' => [
- MEDIATYPE_BITMAP,
- MEDIATYPE_DRAWING,
- MEDIATYPE_AUDIO,
- MEDIATYPE_VIDEO,
- MEDIATYPE_MULTIMEDIA,
- MEDIATYPE_UNKNOWN,
- MEDIATYPE_OFFICE,
- MEDIATYPE_TEXT,
- MEDIATYPE_EXECUTABLE,
- MEDIATYPE_ARCHIVE,
- MEDIATYPE_3D,
- ],
- ] + $minorType,
- 'join_conds' => $imgQuery['joins'],
- ];
- return $qi;
- }
- /**
- * The index is on (img_media_type, img_major_mime, img_minor_mime)
- * which unfortunately doesn't have img_name at the end for sorting.
- * So tell db to sort it however it wishes (Its not super important
- * that this report gives results in a logical order). As an aditional
- * note, mysql seems to by default order things by img_name ASC, which
- * is what we ideally want, so everything works out fine anyhow.
- * @return array
- */
- function getOrderFields() {
- return [];
- }
- /**
- * Generate and output the form
- */
- function getPageHeader() {
- $formDescriptor = [
- 'mime' => [
- 'type' => 'combobox',
- 'options' => $this->getSuggestionsForTypes(),
- 'name' => 'mime',
- 'label-message' => 'mimetype',
- 'required' => true,
- 'default' => $this->mime,
- ],
- ];
- HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
- ->setSubmitTextMsg( 'ilsubmit' )
- ->setAction( $this->getPageTitle()->getLocalURL() )
- ->setMethod( 'get' )
- ->prepareForm()
- ->displayForm( false );
- return '';
- }
- protected function getSuggestionsForTypes() {
- $dbr = wfGetDB( DB_REPLICA );
- $lastMajor = null;
- $suggestions = [];
- $result = $dbr->select(
- [ 'image' ],
- // We ignore img_media_type, but using it in the query is needed for MySQL to choose a
- // sensible execution plan
- [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ],
- [],
- __METHOD__,
- [ 'GROUP BY' => [ 'img_media_type', 'img_major_mime', 'img_minor_mime' ] ]
- );
- foreach ( $result as $row ) {
- $major = $row->img_major_mime;
- $minor = $row->img_minor_mime;
- $suggestions[ "$major/$minor" ] = "$major/$minor";
- if ( $lastMajor === $major ) {
- // If there are at least two with the same major mime type, also include the wildcard
- $suggestions[ "$major/*" ] = "$major/*";
- }
- $lastMajor = $major;
- }
- ksort( $suggestions );
- return $suggestions;
- }
- public function execute( $par ) {
- $this->addHelpLink( 'Help:Managing_files' );
- $this->mime = $par ?: $this->getRequest()->getText( 'mime' );
- $this->mime = trim( $this->mime );
- list( $this->major, $this->minor ) = File::splitMime( $this->mime );
- if ( $this->major == '' || $this->minor == '' || $this->minor == 'unknown' ||
- !self::isValidType( $this->major )
- ) {
- $this->setHeaders();
- $this->outputHeader();
- $this->getPageHeader();
- return;
- }
- parent::execute( $par );
- }
- /**
- * @param Skin $skin
- * @param object $result Result row
- * @return string
- */
- function formatResult( $skin, $result ) {
- $linkRenderer = $this->getLinkRenderer();
- $nt = Title::makeTitle( $result->namespace, $result->title );
- $text = MediaWikiServices::getInstance()->getContentLanguage()
- ->convert( htmlspecialchars( $nt->getText() ) );
- $plink = $linkRenderer->makeLink(
- Title::newFromText( $nt->getPrefixedText() ),
- new HtmlArmor( $text )
- );
- $download = Linker::makeMediaLinkObj( $nt, $this->msg( 'download' )->escaped() );
- $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
- $lang = $this->getLanguage();
- $bytes = htmlspecialchars( $lang->formatSize( $result->img_size ) );
- $dimensions = $this->msg( 'widthheight' )->numParams( $result->img_width,
- $result->img_height )->escaped();
- $user = $linkRenderer->makeLink(
- Title::makeTitle( NS_USER, $result->img_user_text ),
- $result->img_user_text
- );
- $time = $lang->userTimeAndDate( $result->img_timestamp, $this->getUser() );
- $time = htmlspecialchars( $time );
- return "$download $plink . . $dimensions . . $bytes . . $user . . $time";
- }
- /**
- * @param string $type
- * @return bool
- */
- protected static function isValidType( $type ) {
- // From maintenance/tables.sql => img_major_mime
- $types = [
- 'unknown',
- 'application',
- 'audio',
- 'image',
- 'text',
- 'video',
- 'message',
- 'model',
- 'multipart',
- 'chemical'
- ];
- return in_array( $type, $types );
- }
- public function preprocessResults( $db, $res ) {
- $this->executeLBFromResultWrapper( $res );
- }
- protected function getGroupName() {
- return 'media';
- }
- }
|