MediaWiki master
SpecialNewFiles.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
34use MimeAnalyzer;
36
44 protected $opts;
45
47 protected $mediaTypes;
48
49 private GroupPermissionsLookup $groupPermissionsLookup;
50 private IConnectionProvider $dbProvider;
51 private LinkBatchFactory $linkBatchFactory;
52
59 public function __construct(
60 MimeAnalyzer $mimeAnalyzer,
61 GroupPermissionsLookup $groupPermissionsLookup,
62 IConnectionProvider $dbProvider,
63 LinkBatchFactory $linkBatchFactory
64 ) {
65 parent::__construct( 'Newimages' );
66 $this->groupPermissionsLookup = $groupPermissionsLookup;
67 $this->dbProvider = $dbProvider;
68 $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
69 $this->linkBatchFactory = $linkBatchFactory;
70 }
71
72 public function execute( $par ) {
73 $context = new DerivativeContext( $this->getContext() );
74
75 $this->setHeaders();
76 $this->outputHeader();
77
78 $out = $this->getOutput();
79 $this->addHelpLink( 'Help:New images' );
80
81 $opts = new FormOptions();
82
83 $opts->add( 'user', '' );
84 $opts->add( 'showbots', false );
85 $opts->add( 'hidepatrolled', false );
86 $opts->add( 'mediatype', $this->mediaTypes );
87 $opts->add( 'limit', 50 );
88 $opts->add( 'offset', '' );
89 $opts->add( 'start', '' );
90 $opts->add( 'end', '' );
91
93
94 if ( $par !== null ) {
95 $opts->setValue( 'limit', $par );
96 }
97
98 // If start date comes after end date chronologically, swap them.
99 // They are swapped in the interface by JS.
100 $start = $opts->getValue( 'start' );
101 $end = $opts->getValue( 'end' );
102 if ( $start !== '' && $end !== '' && $start > $end ) {
103 $temp = $end;
104 $end = $start;
105 $start = $temp;
106
107 $opts->setValue( 'start', $start, true );
108 $opts->setValue( 'end', $end, true );
109
110 // also swap values in request object, which is used by HTMLForm
111 // to pre-populate the fields with the previous input
112 $request = $context->getRequest();
113 $context->setRequest( new DerivativeRequest(
114 $request,
115 [ 'start' => $start, 'end' => $end ] + $request->getValues(),
116 $request->wasPosted()
117 ) );
118 }
119
120 // Avoid unexpected query or query errors to assoc array input, or nested arrays via
121 // URL query params. Keep only string values (T321133).
122 $mediaTypes = $opts->getValue( 'mediatype' );
123 $mediaTypes = array_filter( $mediaTypes, 'is_string' );
124 // Avoid unbounded query size with bogus values. Keep only known types.
125 $mediaTypes = array_values( array_intersect( $this->mediaTypes, $mediaTypes ) );
126 // Optimization: Remove redundant IN() query condition if all types are checked.
127 if ( !array_diff( $this->mediaTypes, $mediaTypes ) ) {
128 $mediaTypes = [];
129 }
130 $opts->setValue( 'mediatype', $mediaTypes );
131
132 $opts->validateIntBounds( 'limit', 0, 500 );
133
134 $this->opts = $opts;
135
136 if ( !$this->including() ) {
137 $this->setTopText();
138 $this->buildForm( $context );
139 }
140
141 $pager = new NewFilesPager(
142 $context,
143 $this->groupPermissionsLookup,
144 $this->linkBatchFactory,
145 $this->getLinkRenderer(),
146 $this->dbProvider,
147 $opts
148 );
149
150 $out->addHTML( $pager->getBody() );
151 if ( !$this->including() ) {
152 $out->addHTML( $pager->getNavigationBar() );
153 }
154 }
155
156 protected function buildForm( IContextSource $context ) {
157 $mediaTypesText = array_map( function ( $type ) {
158 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
159 // mediastatistics-header-drawing, mediastatistics-header-audio,
160 // mediastatistics-header-video, mediastatistics-header-multimedia,
161 // mediastatistics-header-office, mediastatistics-header-text,
162 // mediastatistics-header-executable, mediastatistics-header-archive,
163 // mediastatistics-header-3d,
164 return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->escaped();
166 $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
167 ksort( $mediaTypesOptions );
168
169 $formDescriptor = [
170 'user' => [
171 'class' => HTMLUserTextField::class,
172 'label-message' => 'newimages-user',
173 'name' => 'user',
174 ],
175
176 'showbots' => [
177 'type' => 'check',
178 'label-message' => 'newimages-showbots',
179 'name' => 'showbots',
180 ],
181
182 'hidepatrolled' => [
183 'type' => 'check',
184 'label-message' => 'newimages-hidepatrolled',
185 'name' => 'hidepatrolled',
186 ],
187
188 'mediatype' => [
189 'type' => 'multiselect',
190 'flatlist' => true,
191 'name' => 'mediatype',
192 'label-message' => 'newimages-mediatype',
193 'options' => $mediaTypesOptions,
194 'default' => $this->mediaTypes,
195 ],
196
197 'limit' => [
198 'type' => 'hidden',
199 'default' => $this->opts->getValue( 'limit' ),
200 'name' => 'limit',
201 ],
202
203 'offset' => [
204 'type' => 'hidden',
205 'default' => $this->opts->getValue( 'offset' ),
206 'name' => 'offset',
207 ],
208
209 'start' => [
210 'type' => 'date',
211 'label-message' => 'date-range-from',
212 'name' => 'start',
213 ],
214
215 'end' => [
216 'type' => 'date',
217 'label-message' => 'date-range-to',
218 'name' => 'end',
219 ],
220 ];
221
222 if ( !$this->getUser()->useFilePatrol() ) {
223 unset( $formDescriptor['hidepatrolled'] );
224 }
225
226 HTMLForm::factory( 'ooui', $formDescriptor, $context )
227 // For the 'multiselect' field values to be preserved on submit
228 ->setFormIdentifier( 'specialnewimages' )
229 ->setWrapperLegendMsg( 'newimages-legend' )
230 ->setSubmitTextMsg( 'ilsubmit' )
231 ->setMethod( 'get' )
232 ->prepareForm()
233 ->displayForm( false );
234 }
235
236 protected function getGroupName() {
237 return 'changes';
238 }
239
243 public function setTopText() {
244 $message = $this->msg( 'newimagestext' )->inContentLanguage();
245 if ( !$message->isDisabled() ) {
246 $contLang = $this->getContentLanguage();
247 $this->getOutput()->addWikiTextAsContent(
248 Html::rawElement( 'div',
249 [
250 'lang' => $contLang->getHtmlCode(),
251 'dir' => $contLang->getDir()
252 ],
253 "\n" . $message->plain() . "\n"
254 )
255 );
256 }
257 }
258}
259
264class_alias( SpecialNewFiles::class, 'SpecialNewFiles' );
An IContextSource implementation which will inherit context from another source but allow individual ...
Implements a text input field for user names.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
Helper class to keep track of options when mixing links and form elements.
fetchValuesFromRequest(WebRequest $r, $optionKeys=null)
Fetch values for all options (or selected options) from the given WebRequest, making them available f...
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
getValue( $name)
Get the value for the given option name.
setValue( $name, $value, $force=false)
Use to set the value of an option.
validateIntBounds( $name, $min, $max)
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Similar to MediaWiki\Request\FauxRequest, but only fakes URL parameters and method (POST or GET) and ...
Shortcut to construct an includable special page.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getUser()
Shortcut to get the User executing this instance.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
including( $x=null)
Whether the special page is being evaluated via transclusion.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Implements Special:Newimages.
buildForm(IContextSource $context)
execute( $par)
Default execute method Checks user permissions.
setTopText()
Send the text to be displayed above the options.
__construct(MimeAnalyzer $mimeAnalyzer, GroupPermissionsLookup $groupPermissionsLookup, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.