Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 132
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialNewFiles
0.00% covered (danger)
0.00%
0 / 131
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
72
 buildForm
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 1
6
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTopText
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Implements Special:Newimages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24namespace MediaWiki\Specials;
25
26use HTMLUserTextField;
27use MediaWiki\Cache\LinkBatchFactory;
28use MediaWiki\Context\DerivativeContext;
29use MediaWiki\Context\IContextSource;
30use MediaWiki\Html\FormOptions;
31use MediaWiki\Html\Html;
32use MediaWiki\HTMLForm\HTMLForm;
33use MediaWiki\Pager\NewFilesPager;
34use MediaWiki\Permissions\GroupPermissionsLookup;
35use MediaWiki\Request\DerivativeRequest;
36use MediaWiki\SpecialPage\IncludableSpecialPage;
37use MimeAnalyzer;
38use Wikimedia\Rdbms\IConnectionProvider;
39
40class SpecialNewFiles extends IncludableSpecialPage {
41    /** @var FormOptions */
42    protected $opts;
43
44    /** @var string[] */
45    protected $mediaTypes;
46
47    private GroupPermissionsLookup $groupPermissionsLookup;
48    private IConnectionProvider $dbProvider;
49    private LinkBatchFactory $linkBatchFactory;
50
51    /**
52     * @param MimeAnalyzer $mimeAnalyzer
53     * @param GroupPermissionsLookup $groupPermissionsLookup
54     * @param IConnectionProvider $dbProvider
55     * @param LinkBatchFactory $linkBatchFactory
56     */
57    public function __construct(
58        MimeAnalyzer $mimeAnalyzer,
59        GroupPermissionsLookup $groupPermissionsLookup,
60        IConnectionProvider $dbProvider,
61        LinkBatchFactory $linkBatchFactory
62    ) {
63        parent::__construct( 'Newimages' );
64        $this->groupPermissionsLookup = $groupPermissionsLookup;
65        $this->dbProvider = $dbProvider;
66        $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
67        $this->linkBatchFactory = $linkBatchFactory;
68    }
69
70    public function execute( $par ) {
71        $context = new DerivativeContext( $this->getContext() );
72
73        $this->setHeaders();
74        $this->outputHeader();
75
76        $out = $this->getOutput();
77        $this->addHelpLink( 'Help:New images' );
78
79        $opts = new FormOptions();
80
81        $opts->add( 'user', '' );
82        $opts->add( 'showbots', false );
83        $opts->add( 'hidepatrolled', false );
84        $opts->add( 'mediatype', $this->mediaTypes );
85        $opts->add( 'limit', 50 );
86        $opts->add( 'offset', '' );
87        $opts->add( 'start', '' );
88        $opts->add( 'end', '' );
89
90        $opts->fetchValuesFromRequest( $this->getRequest() );
91
92        if ( $par !== null ) {
93            $opts->setValue( 'limit', $par );
94        }
95
96        // If start date comes after end date chronologically, swap them.
97        // They are swapped in the interface by JS.
98        $start = $opts->getValue( 'start' );
99        $end = $opts->getValue( 'end' );
100        if ( $start !== '' && $end !== '' && $start > $end ) {
101            $temp = $end;
102            $end = $start;
103            $start = $temp;
104
105            $opts->setValue( 'start', $start, true );
106            $opts->setValue( 'end', $end, true );
107
108            // also swap values in request object, which is used by HTMLForm
109            // to pre-populate the fields with the previous input
110            $request = $context->getRequest();
111            $context->setRequest( new DerivativeRequest(
112                $request,
113                [ 'start' => $start, 'end' => $end ] + $request->getValues(),
114                $request->wasPosted()
115            ) );
116        }
117
118        // Avoid unexpected query or query errors to assoc array input, or nested arrays via
119        // URL query params. Keep only string values (T321133).
120        $mediaTypes = $opts->getValue( 'mediatype' );
121        $mediaTypes = array_filter( $mediaTypes, 'is_string' );
122        // Avoid unbounded query size with bogus values. Keep only known types.
123        $mediaTypes = array_values( array_intersect( $this->mediaTypes, $mediaTypes ) );
124        // Optimization: Remove redundant IN() query condition if all types are checked.
125        if ( !array_diff( $this->mediaTypes, $mediaTypes ) ) {
126            $mediaTypes = [];
127        }
128        $opts->setValue( 'mediatype', $mediaTypes );
129
130        $opts->validateIntBounds( 'limit', 0, 500 );
131
132        $this->opts = $opts;
133
134        if ( !$this->including() ) {
135            $this->setTopText();
136            $this->buildForm( $context );
137        }
138
139        $pager = new NewFilesPager(
140            $context,
141            $this->groupPermissionsLookup,
142            $this->linkBatchFactory,
143            $this->getLinkRenderer(),
144            $this->dbProvider,
145            $opts
146        );
147
148        $out->addHTML( $pager->getBody() );
149        if ( !$this->including() ) {
150            $out->addHTML( $pager->getNavigationBar() );
151        }
152    }
153
154    protected function buildForm( IContextSource $context ) {
155        $mediaTypesText = array_map( function ( $type ) {
156            // mediastatistics-header-unknown, mediastatistics-header-bitmap,
157            // mediastatistics-header-drawing, mediastatistics-header-audio,
158            // mediastatistics-header-video, mediastatistics-header-multimedia,
159            // mediastatistics-header-office, mediastatistics-header-text,
160            // mediastatistics-header-executable, mediastatistics-header-archive,
161            // mediastatistics-header-3d,
162            return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->escaped();
163        }, $this->mediaTypes );
164        $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
165        ksort( $mediaTypesOptions );
166
167        $formDescriptor = [
168            'user' => [
169                'class' => HTMLUserTextField::class,
170                'label-message' => 'newimages-user',
171                'name' => 'user',
172            ],
173
174            'showbots' => [
175                'type' => 'check',
176                'label-message' => 'newimages-showbots',
177                'name' => 'showbots',
178            ],
179
180            'hidepatrolled' => [
181                'type' => 'check',
182                'label-message' => 'newimages-hidepatrolled',
183                'name' => 'hidepatrolled',
184            ],
185
186            'mediatype' => [
187                'type' => 'multiselect',
188                'flatlist' => true,
189                'name' => 'mediatype',
190                'label-message' => 'newimages-mediatype',
191                'options' => $mediaTypesOptions,
192                'default' => $this->mediaTypes,
193            ],
194
195            'limit' => [
196                'type' => 'hidden',
197                'default' => $this->opts->getValue( 'limit' ),
198                'name' => 'limit',
199            ],
200
201            'offset' => [
202                'type' => 'hidden',
203                'default' => $this->opts->getValue( 'offset' ),
204                'name' => 'offset',
205            ],
206
207            'start' => [
208                'type' => 'date',
209                'label-message' => 'date-range-from',
210                'name' => 'start',
211            ],
212
213            'end' => [
214                'type' => 'date',
215                'label-message' => 'date-range-to',
216                'name' => 'end',
217            ],
218        ];
219
220        if ( !$this->getUser()->useFilePatrol() ) {
221            unset( $formDescriptor['hidepatrolled'] );
222        }
223
224        HTMLForm::factory( 'ooui', $formDescriptor, $context )
225            // For the 'multiselect' field values to be preserved on submit
226            ->setFormIdentifier( 'specialnewimages' )
227            ->setWrapperLegendMsg( 'newimages-legend' )
228            ->setSubmitTextMsg( 'ilsubmit' )
229            ->setMethod( 'get' )
230            ->prepareForm()
231            ->displayForm( false );
232    }
233
234    protected function getGroupName() {
235        return 'changes';
236    }
237
238    /**
239     * Send the text to be displayed above the options
240     */
241    public function setTopText() {
242        $message = $this->msg( 'newimagestext' )->inContentLanguage();
243        if ( !$message->isDisabled() ) {
244            $contLang = $this->getContentLanguage();
245            $this->getOutput()->addWikiTextAsContent(
246                Html::rawElement( 'div',
247                    [
248                        'lang' => $contLang->getHtmlCode(),
249                        'dir' => $contLang->getDir()
250                    ],
251                    "\n" . $message->plain() . "\n"
252                )
253            );
254        }
255    }
256}
257
258/**
259 * Retain the old class name for backwards compatibility.
260 * @deprecated since 1.41
261 */
262class_alias( SpecialNewFiles::class, 'SpecialNewFiles' );