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