MediaWiki  master
SpecialNewFiles.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Specials;
25 
27 use HTMLForm;
29 use IContextSource;
37 use MimeAnalyzer;
39 
42  protected $opts;
43 
45  protected $mediaTypes;
46 
47  private GroupPermissionsLookup $groupPermissionsLookup;
48  private IConnectionProvider $dbProvider;
49  private LinkBatchFactory $linkBatchFactory;
50 
57  public function __construct(
58  MimeAnalyzer $mimeAnalyzer,
59  GroupPermissionsLookup $groupPermissionsLookup,
60  IConnectionProvider $dbProvider,
61  LinkBatchFactory $linkBatchFactory
62  ) {
63  parent::__construct( 'Newimages' );
64  $this->groupPermissionsLookup = $groupPermissionsLookup;
65  $this->dbProvider = $dbProvider;
66  $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
67  $this->linkBatchFactory = $linkBatchFactory;
68  }
69 
70  public function execute( $par ) {
71  $context = new DerivativeContext( $this->getContext() );
72 
73  $this->setHeaders();
74  $this->outputHeader();
75 
76  $out = $this->getOutput();
77  $this->addHelpLink( 'Help:New images' );
78 
79  $opts = new FormOptions();
80 
81  $opts->add( 'user', '' );
82  $opts->add( 'showbots', false );
83  $opts->add( 'hidepatrolled', false );
84  $opts->add( 'mediatype', $this->mediaTypes );
85  $opts->add( 'limit', 50 );
86  $opts->add( 'offset', '' );
87  $opts->add( 'start', '' );
88  $opts->add( 'end', '' );
89 
91 
92  if ( $par !== null ) {
93  $opts->setValue( 'limit', $par );
94  }
95 
96  // If start date comes after end date chronologically, swap them.
97  // They are swapped in the interface by JS.
98  $start = $opts->getValue( 'start' );
99  $end = $opts->getValue( 'end' );
100  if ( $start !== '' && $end !== '' && $start > $end ) {
101  $temp = $end;
102  $end = $start;
103  $start = $temp;
104 
105  $opts->setValue( 'start', $start, true );
106  $opts->setValue( 'end', $end, true );
107 
108  // also swap values in request object, which is used by HTMLForm
109  // to pre-populate the fields with the previous input
110  $request = $context->getRequest();
111  $context->setRequest( new DerivativeRequest(
112  $request,
113  [ 'start' => $start, 'end' => $end ] + $request->getValues(),
114  $request->wasPosted()
115  ) );
116  }
117 
118  // Avoid unexpected query or query errors to assoc array input, or nested arrays via
119  // URL query params. Keep only string values (T321133).
120  $mediaTypes = $opts->getValue( 'mediatype' );
121  $mediaTypes = array_filter( $mediaTypes, 'is_string' );
122  // Avoid unbounded query size with bogus values. Keep only known types.
123  $mediaTypes = array_values( array_intersect( $this->mediaTypes, $mediaTypes ) );
124  // Optimization: Remove redundant IN() query condition if all types are checked.
125  if ( !array_diff( $this->mediaTypes, $mediaTypes ) ) {
126  $mediaTypes = [];
127  }
128  $opts->setValue( 'mediatype', $mediaTypes );
129 
130  $opts->validateIntBounds( 'limit', 0, 500 );
131 
132  $this->opts = $opts;
133 
134  if ( !$this->including() ) {
135  $this->setTopText();
136  $this->buildForm( $context );
137  }
138 
139  $pager = new NewFilesPager(
140  $context,
141  $this->groupPermissionsLookup,
142  $this->linkBatchFactory,
143  $this->getLinkRenderer(),
144  $this->dbProvider,
145  $opts
146  );
147 
148  $out->addHTML( $pager->getBody() );
149  if ( !$this->including() ) {
150  $out->addHTML( $pager->getNavigationBar() );
151  }
152  }
153 
154  protected function buildForm( IContextSource $context ) {
155  $mediaTypesText = array_map( function ( $type ) {
156  // mediastatistics-header-unknown, mediastatistics-header-bitmap,
157  // mediastatistics-header-drawing, mediastatistics-header-audio,
158  // mediastatistics-header-video, mediastatistics-header-multimedia,
159  // mediastatistics-header-office, mediastatistics-header-text,
160  // mediastatistics-header-executable, mediastatistics-header-archive,
161  // mediastatistics-header-3d,
162  return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->escaped();
163  }, $this->mediaTypes );
164  $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
165  ksort( $mediaTypesOptions );
166 
167  $formDescriptor = [
168  'user' => [
169  'class' => HTMLUserTextField::class,
170  'label-message' => 'newimages-user',
171  'name' => 'user',
172  ],
173 
174  'showbots' => [
175  'type' => 'check',
176  'label-message' => 'newimages-showbots',
177  'name' => 'showbots',
178  ],
179 
180  'hidepatrolled' => [
181  'type' => 'check',
182  'label-message' => 'newimages-hidepatrolled',
183  'name' => 'hidepatrolled',
184  ],
185 
186  'mediatype' => [
187  'type' => 'multiselect',
188  'flatlist' => true,
189  'name' => 'mediatype',
190  'label-message' => 'newimages-mediatype',
191  'options' => $mediaTypesOptions,
192  'default' => $this->mediaTypes,
193  ],
194 
195  'limit' => [
196  'type' => 'hidden',
197  'default' => $this->opts->getValue( 'limit' ),
198  'name' => 'limit',
199  ],
200 
201  'offset' => [
202  'type' => 'hidden',
203  'default' => $this->opts->getValue( 'offset' ),
204  'name' => 'offset',
205  ],
206 
207  'start' => [
208  'type' => 'date',
209  'label-message' => 'date-range-from',
210  'name' => 'start',
211  ],
212 
213  'end' => [
214  'type' => 'date',
215  'label-message' => 'date-range-to',
216  'name' => 'end',
217  ],
218  ];
219 
220  if ( !$this->getUser()->useFilePatrol() ) {
221  unset( $formDescriptor['hidepatrolled'] );
222  }
223 
224  HTMLForm::factory( 'ooui', $formDescriptor, $context )
225  // For the 'multiselect' field values to be preserved on submit
226  ->setFormIdentifier( 'specialnewimages' )
227  ->setWrapperLegendMsg( 'newimages-legend' )
228  ->setSubmitTextMsg( 'ilsubmit' )
229  ->setMethod( 'get' )
230  ->prepareForm()
231  ->displayForm( false );
232  }
233 
234  protected function getGroupName() {
235  return 'changes';
236  }
237 
241  public function setTopText() {
242  $message = $this->msg( 'newimagestext' )->inContentLanguage();
243  if ( !$message->isDisabled() ) {
244  $contLang = $this->getContentLanguage();
245  $this->getOutput()->addWikiTextAsContent(
246  Html::rawElement( 'div',
247  [
248  'lang' => $contLang->getHtmlCode(),
249  'dir' => $contLang->getDir()
250  ],
251  "\n" . $message->plain() . "\n"
252  )
253  );
254  }
255  }
256 }
257 
262 class_alias( SpecialNewFiles::class, 'SpecialNewFiles' );
An IContextSource implementation which will inherit context from another source but allow individual ...
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:158
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:360
Implements a text input field for user names.
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:41
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.
Definition: FormOptions.php:89
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
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:235
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 Per default the message key is the canonical name o...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
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...
Implements functions related to MIME types such as detection and mapping to file extension.
getMediaTypes()
Returns an array of media types (MEDIATYPE_xxx constants)
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.