1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
<?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
*/
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\Permissions\GroupPermissionsLookup;
use MediaWiki\Request\DerivativeRequest;
use Wikimedia\Rdbms\ILoadBalancer;
class SpecialNewFiles extends IncludableSpecialPage {
/** @var FormOptions */
protected $opts;
/** @var string[] */
protected $mediaTypes;
/** @var GroupPermissionsLookup */
private $groupPermissionsLookup;
/** @var ILoadBalancer */
private $loadBalancer;
/** @var LinkBatchFactory */
private $linkBatchFactory;
/**
* @param MimeAnalyzer $mimeAnalyzer
* @param GroupPermissionsLookup $groupPermissionsLookup
* @param ILoadBalancer $loadBalancer
* @param LinkBatchFactory $linkBatchFactory
*/
public function __construct(
MimeAnalyzer $mimeAnalyzer,
GroupPermissionsLookup $groupPermissionsLookup,
ILoadBalancer $loadBalancer,
LinkBatchFactory $linkBatchFactory
) {
parent::__construct( 'Newimages' );
$this->groupPermissionsLookup = $groupPermissionsLookup;
$this->loadBalancer = $loadBalancer;
$this->mediaTypes = $mimeAnalyzer->getMediaTypes();
$this->linkBatchFactory = $linkBatchFactory;
}
public function execute( $par ) {
$context = new DerivativeContext( $this->getContext() );
$this->setHeaders();
$this->outputHeader();
$out = $this->getOutput();
$this->addHelpLink( 'Help:New images' );
$opts = new FormOptions();
$opts->add( 'user', '' );
$opts->add( 'showbots', 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( 'limit', $par );
}
// 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()
) );
}
// Avoid unexpected query or query errors to assoc array input, or nested arrays via
// URL query params. Keep only string values (T321133).
$mediaTypes = $opts->getValue( 'mediatype' );
$mediaTypes = array_filter( $mediaTypes, 'is_string' );
// Avoid unbounded query size with bogus values. Keep only known types.
$mediaTypes = array_values( array_intersect( $this->mediaTypes, $mediaTypes ) );
// Optimization: Remove redundant IN() query condition if all types are checked.
if ( !array_diff( $this->mediaTypes, $mediaTypes ) ) {
$mediaTypes = [];
}
$opts->setValue( 'mediatype', $mediaTypes );
$opts->validateIntBounds( 'limit', 0, 500 );
$this->opts = $opts;
if ( !$this->including() ) {
$this->setTopText();
$this->buildForm( $context );
}
$pager = new NewFilesPager(
$context,
$this->groupPermissionsLookup,
$this->linkBatchFactory,
$this->getLinkRenderer(),
$this->loadBalancer,
$opts
);
$out->addHTML( $pager->getBody() );
if ( !$this->including() ) {
$out->addHTML( $pager->getNavigationBar() );
}
}
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 ) )->escaped();
}, $this->mediaTypes );
$mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
ksort( $mediaTypesOptions );
$formDescriptor = [
'user' => [
'class' => HTMLUserTextField::class,
'label-message' => 'newimages-user',
'name' => 'user',
],
'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->getUser()->useFilePatrol() ) {
unset( $formDescriptor['hidepatrolled'] );
}
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 );
}
protected function getGroupName() {
return 'changes';
}
/**
* Send the text to be displayed above the options
*/
public function setTopText() {
$message = $this->msg( 'newimagestext' )->inContentLanguage();
if ( !$message->isDisabled() ) {
$contLang = $this->getContentLanguage();
$this->getOutput()->addWikiTextAsContent(
Html::rawElement( 'div',
[
'lang' => $contLang->getHtmlCode(),
'dir' => $contLang->getDir()
],
"\n" . $message->plain() . "\n"
)
);
}
}
}
|