MediaWiki  master
SpecialNewFiles.php
Go to the documentation of this file.
1 <?php
27 
30  protected $opts;
31 
33  protected $mediaTypes;
34 
37 
39  private $loadBalancer;
40 
43 
50  public function __construct(
51  MimeAnalyzer $mimeAnalyzer,
55  ) {
56  parent::__construct( 'Newimages' );
57  $this->groupPermissionsLookup = $groupPermissionsLookup;
58  $this->loadBalancer = $loadBalancer;
59  $this->mediaTypes = $mimeAnalyzer->getMediaTypes();
60  $this->linkBatchFactory = $linkBatchFactory;
61  }
62 
63  public function execute( $par ) {
64  $context = new DerivativeContext( $this->getContext() );
65 
66  $this->setHeaders();
67  $this->outputHeader();
68 
69  $out = $this->getOutput();
70  $this->addHelpLink( 'Help:New images' );
71 
72  $opts = new FormOptions();
73 
74  $opts->add( 'user', '' );
75  $opts->add( 'showbots', false );
76  $opts->add( 'hidepatrolled', false );
77  $opts->add( 'mediatype', $this->mediaTypes );
78  $opts->add( 'limit', 50 );
79  $opts->add( 'offset', '' );
80  $opts->add( 'start', '' );
81  $opts->add( 'end', '' );
82 
84 
85  if ( $par !== null ) {
86  $opts->setValue( 'limit', $par );
87  }
88 
89  // If start date comes after end date chronologically, swap them.
90  // They are swapped in the interface by JS.
91  $start = $opts->getValue( 'start' );
92  $end = $opts->getValue( 'end' );
93  if ( $start !== '' && $end !== '' && $start > $end ) {
94  $temp = $end;
95  $end = $start;
96  $start = $temp;
97 
98  $opts->setValue( 'start', $start, true );
99  $opts->setValue( 'end', $end, true );
100 
101  // also swap values in request object, which is used by HTMLForm
102  // to pre-populate the fields with the previous input
103  $request = $context->getRequest();
104  $context->setRequest( new DerivativeRequest(
105  $request,
106  [ 'start' => $start, 'end' => $end ] + $request->getValues(),
107  $request->wasPosted()
108  ) );
109  }
110 
111  // if all media types have been selected, wipe out the array to prevent
112  // the pointless IN(...) query condition (which would have no effect
113  // because every possible type has been selected)
114  $missingMediaTypes = array_diff( $this->mediaTypes, $opts->getValue( 'mediatype' ) );
115  if ( empty( $missingMediaTypes ) ) {
116  $opts->setValue( 'mediatype', [] );
117  }
118 
119  $opts->validateIntBounds( 'limit', 0, 500 );
120 
121  $this->opts = $opts;
122 
123  if ( !$this->including() ) {
124  $this->setTopText();
125  $this->buildForm( $context );
126  }
127 
128  $pager = new NewFilesPager(
129  $context,
130  $this->groupPermissionsLookup,
131  $this->linkBatchFactory,
132  $this->getLinkRenderer(),
133  $this->loadBalancer,
134  $opts
135  );
136 
137  $out->addHTML( $pager->getBody() );
138  if ( !$this->including() ) {
139  $out->addHTML( $pager->getNavigationBar() );
140  }
141  }
142 
143  protected function buildForm( IContextSource $context ) {
144  $mediaTypesText = array_map( function ( $type ) {
145  // mediastatistics-header-unknown, mediastatistics-header-bitmap,
146  // mediastatistics-header-drawing, mediastatistics-header-audio,
147  // mediastatistics-header-video, mediastatistics-header-multimedia,
148  // mediastatistics-header-office, mediastatistics-header-text,
149  // mediastatistics-header-executable, mediastatistics-header-archive,
150  // mediastatistics-header-3d,
151  return $this->msg( 'mediastatistics-header-' . strtolower( $type ) )->escaped();
152  }, $this->mediaTypes );
153  $mediaTypesOptions = array_combine( $mediaTypesText, $this->mediaTypes );
154  ksort( $mediaTypesOptions );
155 
156  $formDescriptor = [
157  'user' => [
158  'class' => HTMLUserTextField::class,
159  'label-message' => 'newimages-user',
160  'name' => 'user',
161  ],
162 
163  'showbots' => [
164  'type' => 'check',
165  'label-message' => 'newimages-showbots',
166  'name' => 'showbots',
167  ],
168 
169  'hidepatrolled' => [
170  'type' => 'check',
171  'label-message' => 'newimages-hidepatrolled',
172  'name' => 'hidepatrolled',
173  ],
174 
175  'mediatype' => [
176  'type' => 'multiselect',
177  'flatlist' => true,
178  'name' => 'mediatype',
179  'label-message' => 'newimages-mediatype',
180  'options' => $mediaTypesOptions,
181  'default' => $this->mediaTypes,
182  ],
183 
184  'limit' => [
185  'type' => 'hidden',
186  'default' => $this->opts->getValue( 'limit' ),
187  'name' => 'limit',
188  ],
189 
190  'offset' => [
191  'type' => 'hidden',
192  'default' => $this->opts->getValue( 'offset' ),
193  'name' => 'offset',
194  ],
195 
196  'start' => [
197  'type' => 'date',
198  'label-message' => 'date-range-from',
199  'name' => 'start',
200  ],
201 
202  'end' => [
203  'type' => 'date',
204  'label-message' => 'date-range-to',
205  'name' => 'end',
206  ],
207  ];
208 
209  if ( !$this->getUser()->useFilePatrol() ) {
210  unset( $formDescriptor['hidepatrolled'] );
211  }
212 
213  HTMLForm::factory( 'ooui', $formDescriptor, $context )
214  // For the 'multiselect' field values to be preserved on submit
215  ->setFormIdentifier( 'specialnewimages' )
216  ->setWrapperLegendMsg( 'newimages-legend' )
217  ->setSubmitTextMsg( 'ilsubmit' )
218  ->setMethod( 'get' )
219  ->prepareForm()
220  ->displayForm( false );
221  }
222 
223  protected function getGroupName() {
224  return 'changes';
225  }
226 
230  public function setTopText() {
231  $message = $this->msg( 'newimagestext' )->inContentLanguage();
232  if ( !$message->isDisabled() ) {
233  $contLang = $this->getContentLanguage();
234  $this->getOutput()->addWikiTextAsContent(
235  Html::rawElement( 'div',
236  [
237  'lang' => $contLang->getHtmlCode(),
238  'dir' => $contLang->getDir()
239  ],
240  "\n" . $message->plain() . "\n"
241  )
242  );
243  }
244  }
245 }
An IContextSource implementation which will inherit context from another source but allow individual ...
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
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:338
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
Shortcut to construct an includable special page.
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.
ILoadBalancer $loadBalancer
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
GroupPermissionsLookup $groupPermissionsLookup
__construct(MimeAnalyzer $mimeAnalyzer, GroupPermissionsLookup $groupPermissionsLookup, ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
setTopText()
Send the text to be displayed above the options.
LinkBatchFactory $linkBatchFactory
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.
Database cluster connection, tracking, load balancing, and transaction manager interface.