Add media type based filtering to Special:NewFiles
[lhc/web/wiklou.git] / includes / specials / SpecialNewimages.php
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
24 class SpecialNewFiles extends IncludableSpecialPage {
25 /** @var FormOptions */
26 protected $opts;
27
28 /** @var string[] */
29 protected $mediaTypes;
30
31 public function __construct() {
32 parent::__construct( 'Newimages' );
33 }
34
35 public function execute( $par ) {
36 $this->setHeaders();
37 $this->outputHeader();
38 $mimeAnalyzer = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
39 $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
40
41 $out = $this->getOutput();
42 $out->addModules( 'mediawiki.special.newFiles' );
43 $this->addHelpLink( 'Help:New images' );
44
45 $opts = new FormOptions();
46
47 $opts->add( 'like', '' );
48 $opts->add( 'user', '' );
49 $opts->add( 'showbots', false );
50 $opts->add( 'hidepatrolled', false );
51 $opts->add( 'mediatype', $this->mediaTypes );
52 $opts->add( 'limit', 50 );
53 $opts->add( 'offset', '' );
54 $opts->add( 'start', '' );
55 $opts->add( 'end', '' );
56
57 $opts->fetchValuesFromRequest( $this->getRequest() );
58
59 if ( $par !== null ) {
60 $opts->setValue( is_numeric( $par ) ? 'limit' : 'like', $par );
61 }
62
63 // If start date comes after end date chronologically, swap them.
64 // They are swapped in the interface by JS.
65 $start = $opts->getValue( 'start' );
66 $end = $opts->getValue( 'end' );
67 if ( $start !== '' && $end !== '' && $start > $end ) {
68 $temp = $end;
69 $end = $start;
70 $start = $temp;
71
72 $opts->setValue( 'start', $start, true );
73 $opts->setValue( 'end', $end, true );
74 }
75
76 // if all media types have been selected, wipe out the array to prevent
77 // the pointless IN(...) query condition (which would have no effect
78 // because every possible type has been selected)
79 $missingMediaTypes = array_diff( $this->mediaTypes, $opts->getValue( 'mediatype' ) );
80 if ( empty( $missingMediaTypes ) ) {
81 $opts->setValue( 'mediatype', [] );
82 }
83
84 $opts->validateIntBounds( 'limit', 0, 500 );
85
86 $this->opts = $opts;
87
88 if ( !$this->including() ) {
89 $this->setTopText();
90 $this->buildForm();
91 }
92
93 $pager = new NewFilesPager( $this->getContext(), $opts );
94
95 $out->addHTML( $pager->getBody() );
96 if ( !$this->including() ) {
97 $out->addHTML( $pager->getNavigationBar() );
98 }
99 }
100
101 protected function buildForm() {
102 $mediaTypesText = array_map( function ( $type ) {
103 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
104 // mediastatistics-header-drawing, mediastatistics-header-audio,
105 // mediastatistics-header-video, mediastatistics-header-multimedia,
106 // mediastatistics-header-office, mediastatistics-header-text,
107 // mediastatistics-header-executable, mediastatistics-header-archive,
108 return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->text();
109 }, $this->mediaTypes );
110 $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
111 ksort( $mediaTypesOptions );
112
113 $formDescriptor = [
114 'like' => [
115 'type' => 'text',
116 'label-message' => 'newimages-label',
117 'name' => 'like',
118 ],
119
120 'user' => [
121 'type' => 'text',
122 'label-message' => 'newimages-user',
123 'name' => 'user',
124 ],
125
126 'showbots' => [
127 'type' => 'check',
128 'label-message' => 'newimages-showbots',
129 'name' => 'showbots',
130 ],
131
132 'hidepatrolled' => [
133 'type' => 'check',
134 'label-message' => 'newimages-hidepatrolled',
135 'name' => 'hidepatrolled',
136 ],
137
138 'mediatype' => [
139 'type' => 'multiselect',
140 'dropdown' => true,
141 'flatlist' => true,
142 'name' => 'mediatype',
143 'label-message' => 'newimages-mediatype',
144 'options' => $mediaTypesOptions,
145 'default' => $this->mediaTypes,
146 ],
147
148 'limit' => [
149 'type' => 'hidden',
150 'default' => $this->opts->getValue( 'limit' ),
151 'name' => 'limit',
152 ],
153
154 'offset' => [
155 'type' => 'hidden',
156 'default' => $this->opts->getValue( 'offset' ),
157 'name' => 'offset',
158 ],
159
160 'start' => [
161 'type' => 'date',
162 'label-message' => 'date-range-from',
163 'name' => 'start',
164 ],
165
166 'end' => [
167 'type' => 'date',
168 'label-message' => 'date-range-to',
169 'name' => 'end',
170 ],
171 ];
172
173 if ( $this->getConfig()->get( 'MiserMode' ) ) {
174 unset( $formDescriptor['like'] );
175 }
176
177 if ( !$this->getUser()->useFilePatrol() ) {
178 unset( $formDescriptor['hidepatrolled'] );
179 }
180
181 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
182 // For the 'multiselect' field values to be preserved on submit
183 ->setFormIdentifier( 'specialnewimages' )
184 ->setWrapperLegendMsg( 'newimages-legend' )
185 ->setSubmitTextMsg( 'ilsubmit' )
186 ->setMethod( 'get' )
187 ->prepareForm()
188 ->displayForm( false );
189 }
190
191 protected function getGroupName() {
192 return 'changes';
193 }
194
195 /**
196 * Send the text to be displayed above the options
197 */
198 function setTopText() {
199 global $wgContLang;
200
201 $message = $this->msg( 'newimagestext' )->inContentLanguage();
202 if ( !$message->isDisabled() ) {
203 $this->getOutput()->addWikiText(
204 Html::rawElement( 'p',
205 [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
206 "\n" . $message->plain() . "\n"
207 ),
208 /* $lineStart */ false,
209 /* $interface */ false
210 );
211 }
212 }
213 }