Add media type based filtering to Special:NewFiles
authorMatthias Mullie <git@mullie.eu>
Thu, 16 Mar 2017 14:27:49 +0000 (15:27 +0100)
committerMatthias Mullie <git@mullie.eu>
Thu, 8 Jun 2017 14:07:06 +0000 (16:07 +0200)
Change-Id: Iff6e25055489f2181d6f9868e9250e03a54b91b7

includes/libs/mime/MimeAnalyzer.php
includes/specials/SpecialMIMEsearch.php
includes/specials/SpecialNewimages.php
includes/specials/pagers/NewFilesPager.php
languages/i18n/en.json
languages/i18n/qqq.json

index 565c157..c361fdf 100644 (file)
@@ -1144,6 +1144,15 @@ EOT;
                return MEDIATYPE_UNKNOWN;
        }
 
+       /**
+        * Returns an array of media types (MEDIATYPE_xxx constants)
+        *
+        * @return array
+        */
+       public function getMediaTypes() {
+               return array_keys( $this->mediaTypes );
+       }
+
        /**
         * Get the MIME types that various versions of Internet Explorer would
         * detect from a chunk of the content.
index 52cb30a..7087cff 100644 (file)
@@ -74,6 +74,7 @@ class MIMEsearchPage extends QueryPage {
                                '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,
index 583d4f9..20f507f 100644 (file)
@@ -25,6 +25,9 @@ class SpecialNewFiles extends IncludableSpecialPage {
        /** @var FormOptions */
        protected $opts;
 
+       /** @var string[] */
+       protected $mediaTypes;
+
        public function __construct() {
                parent::__construct( 'Newimages' );
        }
@@ -32,6 +35,8 @@ class SpecialNewFiles extends IncludableSpecialPage {
        public function execute( $par ) {
                $this->setHeaders();
                $this->outputHeader();
+               $mimeAnalyzer = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
+               $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
 
                $out = $this->getOutput();
                $out->addModules( 'mediawiki.special.newFiles' );
@@ -43,6 +48,7 @@ class SpecialNewFiles extends IncludableSpecialPage {
                $opts->add( 'user', '' );
                $opts->add( 'showbots', false );
                $opts->add( 'hidepatrolled', false );
+               $opts->add( 'mediatype', $this->mediaTypes );
                $opts->add( 'limit', 50 );
                $opts->add( 'offset', '' );
                $opts->add( 'start', '' );
@@ -67,6 +73,14 @@ class SpecialNewFiles extends IncludableSpecialPage {
                        $opts->setValue( 'end', $end, true );
                }
 
+               // if all media types have been selected, wipe out the array to prevent
+               // the pointless IN(...) query condition (which would have no effect
+               // because every possible type has been selected)
+               $missingMediaTypes = array_diff( $this->mediaTypes, $opts->getValue( 'mediatype' ) );
+               if ( empty( $missingMediaTypes ) ) {
+                       $opts->setValue( 'mediatype', [] );
+               }
+
                $opts->validateIntBounds( 'limit', 0, 500 );
 
                $this->opts = $opts;
@@ -85,6 +99,17 @@ class SpecialNewFiles extends IncludableSpecialPage {
        }
 
        protected function buildForm() {
+               $mediaTypesText = array_map( function ( $type ) {
+                       // mediastatistics-header-unknown, mediastatistics-header-bitmap,
+                       // mediastatistics-header-drawing, mediastatistics-header-audio,
+                       // mediastatistics-header-video, mediastatistics-header-multimedia,
+                       // mediastatistics-header-office, mediastatistics-header-text,
+                       // mediastatistics-header-executable, mediastatistics-header-archive,
+                       return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->text();
+               }, $this->mediaTypes );
+               $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
+               ksort( $mediaTypesOptions );
+
                $formDescriptor = [
                        'like' => [
                                'type' => 'text',
@@ -110,6 +135,16 @@ class SpecialNewFiles extends IncludableSpecialPage {
                                'name' => 'hidepatrolled',
                        ],
 
+                       'mediatype' => [
+                               'type' => 'multiselect',
+                               'dropdown' => true,
+                               'flatlist' => true,
+                               'name' => 'mediatype',
+                               'label-message' => 'newimages-mediatype',
+                               'options' => $mediaTypesOptions,
+                               'default' => $this->mediaTypes,
+                       ],
+
                        'limit' => [
                                'type' => 'hidden',
                                'default' => $this->opts->getValue( 'limit' ),
@@ -144,6 +179,8 @@ class SpecialNewFiles extends IncludableSpecialPage {
                }
 
                HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
+                       // For the 'multiselect' field values to be preserved on submit
+                       ->setFormIdentifier( 'specialnewimages' )
                        ->setWrapperLegendMsg( 'newimages-legend' )
                        ->setSubmitTextMsg( 'ilsubmit' )
                        ->setMethod( 'get' )
index cce0323..3aeebea 100644 (file)
@@ -112,6 +112,10 @@ class NewFilesPager extends RangeChronologicalPager {
                        $options[] = 'STRAIGHT_JOIN';
                }
 
+               if ( $opts->getValue( 'mediatype' ) ) {
+                       $conds['img_media_type'] = $opts->getValue( 'mediatype' );
+               }
+
                $likeVal = $opts->getValue( 'like' );
                if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
                        $dbr = wfGetDB( DB_REPLICA );
index 9fe0e3c..a8d90d1 100644 (file)
        "newimages-user": "IP address or username",
        "newimages-showbots": "Show uploads by bots",
        "newimages-hidepatrolled": "Hide patrolled uploads",
+       "newimages-mediatype": "Media type:",
        "noimages": "Nothing to see.",
        "gallery-slideshow-toggle": "Toggle thumbnails",
        "ilsubmit": "Search",
index 4d46e6f..1498f99 100644 (file)
        "newimages-user": "Caption of the username/IP address editbox on [[Special:NewImages]]",
        "newimages-showbots": "Used as label for a checkbox. When checked, [[Special:NewImages]] will also display uploads by users in the bots group.",
        "newimages-hidepatrolled": "Used as label for a checkbox. When checked, [[Special:NewImages]] will not display patrolled uploads.\n\nCf. {{msg-mw|tog-hidepatrolled}} and {{msg-mw|apihelp-feedrecentchanges-param-hidepatrolled}}.",
+       "newimages-mediatype": "Used as label for a multiselect where users can select the media types to display.",
        "noimages": "This is shown on the special page [[Special:NewImages]], when there aren't any recently uploaded files.",
        "gallery-slideshow-toggle": "Tooltip for the icon that toggles thumbnails on a slideshow  gallery.",
        "ilsubmit": "Search button in [[Special:MIMESearch]].\n\nStrings on the page:\n* {{msg-mw|Mimesearch|page title, legend of input form, link in special pages}}\n* {{msg-mw|Mimesearch-summary|page summary}}\n* {{msg-mw|Mimetype|label for input box}}\n* {{msg-mw|Ilsubmit|search button}}\n\nCheck [[mw:Manual:MIME_type_detection]] for MIME types.\n\n{{Identical|Search}}",