Merge "Fix enhanced RC data attribute sanitizing"
[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 $this->addHelpLink( 'Help:New images' );
43
44 $opts = new FormOptions();
45
46 $opts->add( 'like', '' );
47 $opts->add( 'user', '' );
48 $opts->add( 'showbots', false );
49 $opts->add( 'newbies', 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 'newbies' => [
127 'type' => 'check',
128 'label-message' => 'newimages-newbies',
129 'name' => 'newbies',
130 ],
131
132 'showbots' => [
133 'type' => 'check',
134 'label-message' => 'newimages-showbots',
135 'name' => 'showbots',
136 ],
137
138 'hidepatrolled' => [
139 'type' => 'check',
140 'label-message' => 'newimages-hidepatrolled',
141 'name' => 'hidepatrolled',
142 ],
143
144 'mediatype' => [
145 'type' => 'multiselect',
146 'dropdown' => true,
147 'flatlist' => true,
148 'name' => 'mediatype',
149 'label-message' => 'newimages-mediatype',
150 'options' => $mediaTypesOptions,
151 'default' => $this->mediaTypes,
152 ],
153
154 'limit' => [
155 'type' => 'hidden',
156 'default' => $this->opts->getValue( 'limit' ),
157 'name' => 'limit',
158 ],
159
160 'offset' => [
161 'type' => 'hidden',
162 'default' => $this->opts->getValue( 'offset' ),
163 'name' => 'offset',
164 ],
165
166 'start' => [
167 'type' => 'date',
168 'label-message' => 'date-range-from',
169 'name' => 'start',
170 ],
171
172 'end' => [
173 'type' => 'date',
174 'label-message' => 'date-range-to',
175 'name' => 'end',
176 ],
177 ];
178
179 if ( $this->getConfig()->get( 'MiserMode' ) ) {
180 unset( $formDescriptor['like'] );
181 }
182
183 if ( !$this->getUser()->useFilePatrol() ) {
184 unset( $formDescriptor['hidepatrolled'] );
185 }
186
187 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
188 // For the 'multiselect' field values to be preserved on submit
189 ->setFormIdentifier( 'specialnewimages' )
190 ->setWrapperLegendMsg( 'newimages-legend' )
191 ->setSubmitTextMsg( 'ilsubmit' )
192 ->setMethod( 'get' )
193 ->prepareForm()
194 ->displayForm( false );
195
196 $this->getOutput()->addModules( 'mediawiki.special.newFiles' );
197 }
198
199 protected function getGroupName() {
200 return 'changes';
201 }
202
203 /**
204 * Send the text to be displayed above the options
205 */
206 function setTopText() {
207 global $wgContLang;
208
209 $message = $this->msg( 'newimagestext' )->inContentLanguage();
210 if ( !$message->isDisabled() ) {
211 $this->getOutput()->addWikiText(
212 Html::rawElement( 'p',
213 [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
214 "\n" . $message->plain() . "\n"
215 ),
216 /* $lineStart */ false,
217 /* $interface */ false
218 );
219 }
220 }
221 }