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