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