]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/specials/SpecialNewimages.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialNewimages.php
index a39b56eef0359fa036924dd033287b784364c273..693b8aa90e7b05341653b4269de63aef13470375 100644 (file)
 <?php
 /**
+ * Implements Special:Newimages
+ *
+ * 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
- * FIXME: this code is crap, should use Pager and Database::select().
  */
 
-function wfSpecialNewimages( $par, $specialPage ) {
-       global $wgUser, $wgOut, $wgLang, $wgRequest, $wgMiserMode;
-
-       $wpIlMatch = $wgRequest->getText( 'wpIlMatch' );
-       $dbr = wfGetDB( DB_SLAVE );
-       $sk = $wgUser->getSkin();
-       $shownav = !$specialPage->including();
-       $hidebots = $wgRequest->getBool( 'hidebots' , 1 );
-
-       $hidebotsql = '';
-       if ( $hidebots ) {
-               # Make a list of group names which have the 'bot' flag set.
-               $botconds = array();
-               foreach ( User::getGroupsWithPermission('bot') as $groupname ) {
-                       $botconds[] = 'ug_group = ' . $dbr->addQuotes( $groupname );
-               }
+class SpecialNewFiles extends IncludableSpecialPage {
+       /** @var FormOptions */
+       protected $opts;
 
-               # If not bot groups, do not set $hidebotsql
-               if ( $botconds ) {
-                       $isbotmember = $dbr->makeList( $botconds, LIST_OR );
+       /** @var string[] */
+       protected $mediaTypes;
 
-                       # This join, in conjunction with WHERE ug_group IS NULL, returns
-                       # only those rows from IMAGE where the uploading user is not a mem-
-                       # ber of a group which has the 'bot' permission set.
-                       $ug = $dbr->tableName( 'user_groups' );
-                       $hidebotsql = " LEFT JOIN $ug ON img_user=ug_user AND ($isbotmember)";
-               }
+       public function __construct() {
+               parent::__construct( 'Newimages' );
        }
 
-       $image = $dbr->tableName( 'image' );
+       public function execute( $par ) {
+               $context = new DerivativeContext( $this->getContext() );
 
-       $sql = "SELECT img_timestamp from $image";
-       if ($hidebotsql) {
-               $sql .= "$hidebotsql WHERE ug_group IS NULL";
-       }
-       $sql .= ' ORDER BY img_timestamp DESC';
-       $sql = $dbr->limitResult($sql, 1, false);
-       $res = $dbr->query( $sql, __FUNCTION__ );
-       $row = $dbr->fetchRow( $res );
-       if( $row !== false ) {
-               $ts = $row[0];
-       } else {
-               $ts = false;
-       }
-       $dbr->freeResult( $res );
-       $sql = '';
+               $this->setHeaders();
+               $this->outputHeader();
+               $mimeAnalyzer = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
+               $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
 
-       # If we were clever, we'd use this to cache.
-       $latestTimestamp = wfTimestamp( TS_MW, $ts );
+               $out = $this->getOutput();
+               $this->addHelpLink( 'Help:New images' );
 
-       # Hardcode this for now.
-       $limit = 48;
+               $opts = new FormOptions();
 
-       if ( $parval = intval( $par ) ) {
-               if ( $parval <= $limit && $parval > 0 ) {
-                       $limit = $parval;
+               $opts->add( 'like', '' );
+               $opts->add( 'user', '' );
+               $opts->add( 'showbots', false );
+               $opts->add( 'newbies', false );
+               $opts->add( 'hidepatrolled', false );
+               $opts->add( 'mediatype', $this->mediaTypes );
+               $opts->add( 'limit', 50 );
+               $opts->add( 'offset', '' );
+               $opts->add( 'start', '' );
+               $opts->add( 'end', '' );
+
+               $opts->fetchValuesFromRequest( $this->getRequest() );
+
+               if ( $par !== null ) {
+                       $opts->setValue( is_numeric( $par ) ? 'limit' : 'like', $par );
                }
-       }
 
-       $where = array();
-       $searchpar = array();
-       if ( $wpIlMatch != '' && !$wgMiserMode) {
-               $nt = Title::newFromURL( $wpIlMatch );
-               if( $nt ) {
-                       $where[] = 'LOWER(img_name) ' .  $dbr->buildLike( $dbr->anyString(), strtolower( $nt->getDBkey() ), $dbr->anyString() );
-                       $searchpar['wpIlMatch'] = $wpIlMatch;
+               // If start date comes after end date chronologically, swap them.
+               // They are swapped in the interface by JS.
+               $start = $opts->getValue( 'start' );
+               $end = $opts->getValue( 'end' );
+               if ( $start !== '' && $end !== '' && $start > $end ) {
+                       $temp = $end;
+                       $end = $start;
+                       $start = $temp;
+
+                       $opts->setValue( 'start', $start, true );
+                       $opts->setValue( 'end', $end, true );
+
+                       // also swap values in request object, which is used by HTMLForm
+                       // to pre-populate the fields with the previous input
+                       $request = $context->getRequest();
+                       $context->setRequest( new DerivativeRequest(
+                               $request,
+                               [ 'start' => $start, 'end' => $end ] + $request->getValues(),
+                               $request->wasPosted()
+                       ) );
                }
-       }
 
-       $invertSort = false;
-       if( $until = $wgRequest->getVal( 'until' ) ) {
-               $where[] = "img_timestamp < '" . $dbr->timestamp( $until ) . "'";
-       }
-       if( $from = $wgRequest->getVal( 'from' ) ) {
-               $where[] = "img_timestamp >= '" . $dbr->timestamp( $from ) . "'";
-               $invertSort = true;
-       }
-       $sql='SELECT img_size, img_name, img_user, img_user_text,'.
-            "img_description,img_timestamp FROM $image";
+               // 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', [] );
+               }
 
-       if( $hidebotsql ) {
-               $sql .= $hidebotsql;
-               $where[] = 'ug_group IS NULL';
-       }
-       if( count( $where ) ) {
-               $sql .= ' WHERE ' . $dbr->makeList( $where, LIST_AND );
-       }
-       $sql.=' ORDER BY img_timestamp '. ( $invertSort ? '' : ' DESC' );
-       $sql = $dbr->limitResult($sql, ( $limit + 1 ), false);
-       $res = $dbr->query( $sql, __FUNCTION__ );
+               $opts->validateIntBounds( 'limit', 0, 500 );
 
-       /**
-        * We have to flip things around to get the last N after a certain date
-        */
-       $images = array();
-       while ( $s = $dbr->fetchObject( $res ) ) {
-               if( $invertSort ) {
-                       array_unshift( $images, $s );
-               } else {
-                       array_push( $images, $s );
-               }
-       }
-       $dbr->freeResult( $res );
-
-       $gallery = new ImageGallery();
-       $firstTimestamp = null;
-       $lastTimestamp = null;
-       $shownImages = 0;
-       foreach( $images as $s ) {
-               $shownImages++;
-               if( $shownImages > $limit ) {
-                       # One extra just to test for whether to show a page link;
-                       # don't actually show it.
-                       break;
+               $this->opts = $opts;
+
+               if ( !$this->including() ) {
+                       $this->setTopText();
+                       $this->buildForm( $context );
                }
 
-               $name = $s->img_name;
-               $ut = $s->img_user_text;
+               $pager = new NewFilesPager( $context, $opts );
 
-               $nt = Title::newFromText( $name, NS_FILE );
-               $ul = $sk->link( Title::makeTitle( NS_USER, $ut ), $ut );
+               $out->addHTML( $pager->getBody() );
+               if ( !$this->including() ) {
+                       $out->addHTML( $pager->getNavigationBar() );
+               }
+       }
 
-               $gallery->add( $nt, "$ul<br />\n<i>".htmlspecialchars($wgLang->timeanddate( $s->img_timestamp, true ))."</i><br />\n" );
+       protected function buildForm( IContextSource $context ) {
+               $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,
+                       // mediastatistics-header-3d,
+                       return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->text();
+               }, $this->mediaTypes );
+               $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
+               ksort( $mediaTypesOptions );
+
+               $formDescriptor = [
+                       'like' => [
+                               'type' => 'text',
+                               'label-message' => 'newimages-label',
+                               'name' => 'like',
+                       ],
+
+                       'user' => [
+                               'type' => 'text',
+                               'label-message' => 'newimages-user',
+                               'name' => 'user',
+                       ],
+
+                       'newbies' => [
+                               'type' => 'check',
+                               'label-message' => 'newimages-newbies',
+                               'name' => 'newbies',
+                       ],
+
+                       'showbots' => [
+                               'type' => 'check',
+                               'label-message' => 'newimages-showbots',
+                               'name' => 'showbots',
+                       ],
+
+                       'hidepatrolled' => [
+                               'type' => 'check',
+                               'label-message' => 'newimages-hidepatrolled',
+                               'name' => 'hidepatrolled',
+                       ],
+
+                       'mediatype' => [
+                               'type' => 'multiselect',
+                               'flatlist' => true,
+                               'name' => 'mediatype',
+                               'label-message' => 'newimages-mediatype',
+                               'options' => $mediaTypesOptions,
+                               'default' => $this->mediaTypes,
+                       ],
+
+                       'limit' => [
+                               'type' => 'hidden',
+                               'default' => $this->opts->getValue( 'limit' ),
+                               'name' => 'limit',
+                       ],
+
+                       'offset' => [
+                               'type' => 'hidden',
+                               'default' => $this->opts->getValue( 'offset' ),
+                               'name' => 'offset',
+                       ],
+
+                       'start' => [
+                               'type' => 'date',
+                               'label-message' => 'date-range-from',
+                               'name' => 'start',
+                       ],
+
+                       'end' => [
+                               'type' => 'date',
+                               'label-message' => 'date-range-to',
+                               'name' => 'end',
+                       ],
+               ];
+
+               if ( $this->getConfig()->get( 'MiserMode' ) ) {
+                       unset( $formDescriptor['like'] );
+               }
 
-               $timestamp = wfTimestamp( TS_MW, $s->img_timestamp );
-               if( empty( $firstTimestamp ) ) {
-                       $firstTimestamp = $timestamp;
+               if ( !$this->getUser()->useFilePatrol() ) {
+                       unset( $formDescriptor['hidepatrolled'] );
                }
-               $lastTimestamp = $timestamp;
-       }
 
-       $titleObj = SpecialPage::getTitleFor( 'Newimages' );
-       $action = $titleObj->getLocalURL( $hidebots ? '' : 'hidebots=0' );
-       if ( $shownav && !$wgMiserMode ) {
-               $wgOut->addHTML(
-                       Xml::openElement( 'form', array( 'action' => $action, 'method' => 'post', 'id' => 'imagesearch' ) ) .
-                       Xml::fieldset( wfMsg( 'newimages-legend' ) ) .
-                       Xml::inputLabel( wfMsg( 'newimages-label' ), 'wpIlMatch', 'wpIlMatch', 20, $wpIlMatch ) . ' ' .
-                       Xml::submitButton( wfMsg( 'ilsubmit' ), array( 'name' => 'wpIlSubmit' ) ) .
-                       Xml::closeElement( 'fieldset' ) .
-                       Xml::closeElement( 'form' )
-                );
+               HTMLForm::factory( 'ooui', $formDescriptor, $context )
+                       // For the 'multiselect' field values to be preserved on submit
+                       ->setFormIdentifier( 'specialnewimages' )
+                       ->setWrapperLegendMsg( 'newimages-legend' )
+                       ->setSubmitTextMsg( 'ilsubmit' )
+                       ->setMethod( 'get' )
+                       ->prepareForm()
+                       ->displayForm( false );
        }
 
-       $bydate = wfMsg( 'bydate' );
-       $lt = $wgLang->formatNum( min( $shownImages, $limit ) );
-       if ( $shownav ) {
-               $text = wfMsgExt( 'imagelisttext', array('parse'), $lt, $bydate );
-               $wgOut->addHTML( $text . "\n" );
+       protected function getGroupName() {
+               return 'changes';
        }
 
        /**
-        * Paging controls...
+        * Send the text to be displayed above the options
         */
-
-       # If we change bot visibility, this needs to be carried along.
-       if( !$hidebots ) {
-               $botpar = array( 'hidebots' => 0 );
-       } else {
-               $botpar = array();
-       }
-       $now = wfTimestampNow();
-       $d = $wgLang->date( $now, true );
-       $t = $wgLang->time( $now, true );
-       $query = array_merge(
-               array( 'from' => $now ),
-               $botpar,
-               $searchpar
-       );
-
-       $dateLink = $sk->linkKnown(
-               $titleObj,
-               htmlspecialchars( wfMsg( 'sp-newimages-showfrom', $d, $t ) ),
-               array(),
-               $query
-       );
-
-       $query = array_merge(
-               array( 'hidebots' => ( $hidebots ? 0 : 1 ) ),
-               $searchpar
-       );
-
-       $showhide = $hidebots ? wfMsg( 'show' ) : wfMsg( 'hide' );
-
-       $botLink = $sk->linkKnown(
-               $titleObj,
-               htmlspecialchars( wfMsg( 'showhidebots', $showhide ) ),
-               array(),
-               $query
-       );
-
-       $opts = array( 'parsemag', 'escapenoentities' );
-       $prevLink = wfMsgExt( 'pager-newer-n', $opts, $wgLang->formatNum( $limit ) );
-       if( $firstTimestamp && $firstTimestamp != $latestTimestamp ) {
-               $query = array_merge(
-                       array( 'from' => $firstTimestamp ),
-                       $botpar,
-                       $searchpar
-               );
-
-               $prevLink = $sk->linkKnown(
-                       $titleObj,
-                       $prevLink,
-                       array(),
-                       $query
-               );
-       }
-
-       $nextLink = wfMsgExt( 'pager-older-n', $opts, $wgLang->formatNum( $limit ) );
-       if( $shownImages > $limit && $lastTimestamp ) {
-               $query = array_merge(
-                       array( 'until' => $lastTimestamp ),
-                       $botpar,
-                       $searchpar
-               );
-
-               $nextLink = $sk->linkKnown(
-                       $titleObj,
-                       $nextLink,
-                       array(),
-                       $query
-               );
-
-       }
-
-       $prevnext = '<p>' . $botLink . ' '. wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $dateLink ) .'</p>';
-
-       if ($shownav)
-               $wgOut->addHTML( $prevnext );
-
-       if( count( $images ) ) {
-               $wgOut->addHTML( $gallery->toHTML() );
-               if ($shownav)
-                       $wgOut->addHTML( $prevnext );
-       } else {
-               $wgOut->addWikiMsg( 'noimages' );
+       function setTopText() {
+               global $wgContLang;
+
+               $message = $this->msg( 'newimagestext' )->inContentLanguage();
+               if ( !$message->isDisabled() ) {
+                       $this->getOutput()->addWikiText(
+                               Html::rawElement( 'p',
+                                       [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
+                                       "\n" . $message->plain() . "\n"
+                               ),
+                               /* $lineStart */ false,
+                               /* $interface */ false
+                       );
+               }
        }
 }