MediaWiki master
SpecialNewFiles.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
34use Wikimedia\Mime\MimeAnalyzer;
36
44 protected $opts;
45
47 protected $mediaTypes;
48
49 private GroupPermissionsLookup $groupPermissionsLookup;
50 private IConnectionProvider $dbProvider;
51 private LinkBatchFactory $linkBatchFactory;
52
53 public function __construct(
54 MimeAnalyzer $mimeAnalyzer,
55 GroupPermissionsLookup $groupPermissionsLookup,
56 IConnectionProvider $dbProvider,
57 LinkBatchFactory $linkBatchFactory
58 ) {
59 parent::__construct( 'Newimages' );
60 $this->groupPermissionsLookup = $groupPermissionsLookup;
61 $this->dbProvider = $dbProvider;
62 $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
63 $this->linkBatchFactory = $linkBatchFactory;
64 }
65
66 public function execute( $par ) {
67 $context = new DerivativeContext( $this->getContext() );
68
69 $this->setHeaders();
70 $this->outputHeader();
71
72 $out = $this->getOutput();
73 $this->addHelpLink( 'Help:New images' );
74
75 $opts = new FormOptions();
76
77 $opts->add( 'user', '' );
78 $opts->add( 'showbots', false );
79 $opts->add( 'hidepatrolled', false );
80 $opts->add( 'mediatype', $this->mediaTypes );
81 $opts->add( 'limit', 50 );
82 $opts->add( 'offset', '' );
83 $opts->add( 'start', '' );
84 $opts->add( 'end', '' );
85
87
88 if ( $par !== null ) {
89 $opts->setValue( 'limit', $par );
90 }
91
92 // If start date comes after end date chronologically, swap them.
93 // They are swapped in the interface by JS.
94 $start = $opts->getValue( 'start' );
95 $end = $opts->getValue( 'end' );
96 if ( $start !== '' && $end !== '' && $start > $end ) {
97 $temp = $end;
98 $end = $start;
99 $start = $temp;
100
101 $opts->setValue( 'start', $start, true );
102 $opts->setValue( 'end', $end, true );
103
104 // also swap values in request object, which is used by HTMLForm
105 // to pre-populate the fields with the previous input
106 $request = $context->getRequest();
107 $context->setRequest( new DerivativeRequest(
108 $request,
109 [ 'start' => $start, 'end' => $end ] + $request->getValues(),
110 $request->wasPosted()
111 ) );
112 }
113
114 // Avoid unexpected query or query errors to assoc array input, or nested arrays via
115 // URL query params. Keep only string values (T321133).
116 $mediaTypes = $opts->getValue( 'mediatype' );
117 $mediaTypes = array_filter( $mediaTypes, 'is_string' );
118 // Avoid unbounded query size with bogus values. Keep only known types.
119 $mediaTypes = array_values( array_intersect( $this->mediaTypes, $mediaTypes ) );
120 // Optimization: Remove redundant IN() query condition if all types are checked.
121 if ( !array_diff( $this->mediaTypes, $mediaTypes ) ) {
122 $mediaTypes = [];
123 }
124 $opts->setValue( 'mediatype', $mediaTypes );
125
126 $opts->validateIntBounds( 'limit', 0, 500 );
127
128 $this->opts = $opts;
129
130 if ( !$this->including() ) {
131 $this->setTopText();
132 $this->buildForm( $context );
133 }
134
135 $pager = new NewFilesPager(
136 $context,
137 $this->groupPermissionsLookup,
138 $this->linkBatchFactory,
139 $this->getLinkRenderer(),
140 $this->dbProvider,
141 $opts
142 );
143
144 $out->addHTML( $pager->getBody() );
145 if ( !$this->including() ) {
146 $out->addHTML( $pager->getNavigationBar() );
147 }
148 }
149
150 protected function buildForm( IContextSource $context ) {
151 $mediaTypesText = array_map( function ( $type ) {
152 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
153 // mediastatistics-header-drawing, mediastatistics-header-audio,
154 // mediastatistics-header-video, mediastatistics-header-multimedia,
155 // mediastatistics-header-office, mediastatistics-header-text,
156 // mediastatistics-header-executable, mediastatistics-header-archive,
157 // mediastatistics-header-3d,
158 return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->escaped();
160 $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
161 ksort( $mediaTypesOptions );
162
163 $formDescriptor = [
164 'user' => [
165 'class' => HTMLUserTextField::class,
166 'label-message' => 'newimages-user',
167 'name' => 'user',
168 ],
169
170 'showbots' => [
171 'type' => 'check',
172 'label-message' => 'newimages-showbots',
173 'name' => 'showbots',
174 ],
175
176 'hidepatrolled' => [
177 'type' => 'check',
178 'label-message' => 'newimages-hidepatrolled',
179 'name' => 'hidepatrolled',
180 ],
181
182 'mediatype' => [
183 'type' => 'multiselect',
184 'flatlist' => true,
185 'name' => 'mediatype',
186 'label-message' => 'newimages-mediatype',
187 'options' => $mediaTypesOptions,
188 'default' => $this->mediaTypes,
189 ],
190
191 'limit' => [
192 'type' => 'hidden',
193 'default' => $this->opts->getValue( 'limit' ),
194 'name' => 'limit',
195 ],
196
197 'offset' => [
198 'type' => 'hidden',
199 'default' => $this->opts->getValue( 'offset' ),
200 'name' => 'offset',
201 ],
202
203 'start' => [
204 'type' => 'date',
205 'label-message' => 'date-range-from',
206 'name' => 'start',
207 ],
208
209 'end' => [
210 'type' => 'date',
211 'label-message' => 'date-range-to',
212 'name' => 'end',
213 ],
214 ];
215
216 if ( !$this->getUser()->useFilePatrol() ) {
217 unset( $formDescriptor['hidepatrolled'] );
218 }
219
220 HTMLForm::factory( 'ooui', $formDescriptor, $context )
221 // For the 'multiselect' field values to be preserved on submit
222 ->setFormIdentifier( 'specialnewimages' )
223 ->setWrapperLegendMsg( 'newimages-legend' )
224 ->setSubmitTextMsg( 'ilsubmit' )
225 ->setMethod( 'get' )
226 ->prepareForm()
227 ->displayForm( false );
228 }
229
230 protected function getGroupName() {
231 return 'changes';
232 }
233
237 public function setTopText() {
238 $message = $this->msg( 'newimagestext' )->inContentLanguage();
239 if ( !$message->isDisabled() ) {
240 $contLang = $this->getContentLanguage();
241 $this->getOutput()->addWikiTextAsContent(
242 Html::rawElement( 'div',
243 [
244 'lang' => $contLang->getHtmlCode(),
245 'dir' => $contLang->getDir()
246 ],
247 "\n" . $message->plain() . "\n"
248 )
249 );
250 }
251 }
252}
253
258class_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:210
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:57
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.