MediaWiki  1.28.0
SpecialRedirect.php
Go to the documentation of this file.
1 <?php
32 
39  protected $mType;
40 
47  protected $mValue;
48 
49  function __construct() {
50  parent::__construct( 'Redirect' );
51  $this->mType = null;
52  $this->mValue = null;
53  }
54 
59  function setParameter( $subpage ) {
60  // parse $subpage to pull out the parts
61  $parts = explode( '/', $subpage, 2 );
62  $this->mType = count( $parts ) > 0 ? $parts[0] : null;
63  $this->mValue = count( $parts ) > 1 ? $parts[1] : null;
64  }
65 
71  function dispatchUser() {
72  if ( !ctype_digit( $this->mValue ) ) {
73  return null;
74  }
75  $user = User::newFromId( (int)$this->mValue );
76  $username = $user->getName(); // load User as side-effect
77  if ( $user->isAnon() ) {
78  return null;
79  }
80  $userpage = Title::makeTitle( NS_USER, $username );
81 
82  return $userpage->getFullURL( '', false, PROTO_CURRENT );
83  }
84 
90  function dispatchFile() {
91  $title = Title::makeTitleSafe( NS_FILE, $this->mValue );
92 
93  if ( !$title instanceof Title ) {
94  return null;
95  }
96  $file = wfFindFile( $title );
97 
98  if ( !$file || !$file->exists() ) {
99  return null;
100  }
101  // Default behavior: Use the direct link to the file.
102  $url = $file->getUrl();
103  $request = $this->getRequest();
104  $width = $request->getInt( 'width', -1 );
105  $height = $request->getInt( 'height', -1 );
106 
107  // If a width is requested...
108  if ( $width != -1 ) {
109  $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
110  // ... and we can
111  if ( $mto && !$mto->isError() ) {
112  // ... change the URL to point to a thumbnail.
113  $url = $mto->getUrl();
114  }
115  }
116 
117  return $url;
118  }
119 
126  function dispatchRevision() {
127  $oldid = $this->mValue;
128  if ( !ctype_digit( $oldid ) ) {
129  return null;
130  }
131  $oldid = (int)$oldid;
132  if ( $oldid === 0 ) {
133  return null;
134  }
135 
136  return wfAppendQuery( wfScript( 'index' ), [
137  'oldid' => $oldid
138  ] );
139  }
140 
146  function dispatchPage() {
147  $curid = $this->mValue;
148  if ( !ctype_digit( $curid ) ) {
149  return null;
150  }
151  $curid = (int)$curid;
152  if ( $curid === 0 ) {
153  return null;
154  }
155 
156  return wfAppendQuery( wfScript( 'index' ), [
157  'curid' => $curid
158  ] );
159  }
160 
168  function dispatchLog() {
169  $logid = $this->mValue;
170  if ( !ctype_digit( $logid ) ) {
171  return null;
172  }
173  $logid = (int)$logid;
174  if ( $logid === 0 ) {
175  return null;
176  }
177 
178  $logparams = [
179  'log_id',
180  'log_timestamp',
181  'log_type',
182  'log_user_text',
183  ];
184 
185  $dbr = wfGetDB( DB_REPLICA );
186 
187  // Gets the nested SQL statement which
188  // returns timestamp of the log with the given log ID
189  $inner = $dbr->selectSQLText(
190  'logging',
191  [ 'log_timestamp' ],
192  [ 'log_id' => $logid ]
193  );
194 
195  // Returns all fields mentioned in $logparams of the logs
196  // with the same timestamp as the one returned by the statement above
197  $logsSameTimestamps = $dbr->select(
198  'logging',
199  $logparams,
200  [ "log_timestamp = ($inner)" ]
201  );
202  if ( $logsSameTimestamps->numRows() === 0 ) {
203  return null;
204  }
205 
206  // Stores the row with the same log ID as the one given
207  $rowMain = [];
208  foreach ( $logsSameTimestamps as $row ) {
209  if ( (int)$row->log_id === $logid ) {
210  $rowMain = $row;
211  }
212  }
213 
214  array_shift( $logparams );
215 
216  // Stores all the rows with the same values in each column
217  // as $rowMain
218  foreach ( $logparams as $cond ) {
219  $matchedRows = [];
220  foreach ( $logsSameTimestamps as $row ) {
221  if ( $row->$cond === $rowMain->$cond ) {
222  $matchedRows[] = $row;
223  }
224  }
225  if ( count( $matchedRows ) === 1 ) {
226  break;
227  }
228  $logsSameTimestamps = $matchedRows;
229  }
230  $query = [ 'title' => 'Special:Log', 'limit' => count( $matchedRows ) ];
231 
232  // A map of database field names from table 'logging' to the values of $logparams
233  $keys = [
234  'log_timestamp' => 'offset',
235  'log_type' => 'type',
236  'log_user_text' => 'user'
237  ];
238 
239  foreach ( $logparams as $logKey ) {
240  $query[$keys[$logKey]] = $matchedRows[0]->$logKey;
241  }
242  $query['offset'] = $query['offset'] + 1;
243  $url = $query;
244 
245  return wfAppendQuery( wfScript( 'index' ), $url );
246  }
247 
256  function dispatch() {
257  // the various namespaces supported by Special:Redirect
258  switch ( $this->mType ) {
259  case 'user':
260  $url = $this->dispatchUser();
261  break;
262  case 'file':
263  $url = $this->dispatchFile();
264  break;
265  case 'revision':
266  $url = $this->dispatchRevision();
267  break;
268  case 'page':
269  $url = $this->dispatchPage();
270  break;
271  case 'logid':
272  $url = $this->dispatchLog();
273  break;
274  default:
275  $url = null;
276  break;
277  }
278  if ( $url ) {
279  $this->getOutput()->redirect( $url );
280 
281  return true;
282  }
283  if ( !is_null( $this->mValue ) ) {
284  $this->getOutput()->setStatusCode( 404 );
285  // Message: redirect-not-exists
286  $msg = $this->getMessagePrefix() . '-not-exists';
287 
288  return Status::newFatal( $msg );
289  }
290 
291  return false;
292  }
293 
294  protected function getFormFields() {
295  $mp = $this->getMessagePrefix();
296  $ns = [
297  // subpage => message
298  // Messages: redirect-user, redirect-page, redirect-revision,
299  // redirect-file, redirect-logid
300  'user' => $mp . '-user',
301  'page' => $mp . '-page',
302  'revision' => $mp . '-revision',
303  'file' => $mp . '-file',
304  'logid' => $mp . '-logid',
305  ];
306  $a = [];
307  $a['type'] = [
308  'type' => 'select',
309  'label-message' => $mp . '-lookup', // Message: redirect-lookup
310  'options' => [],
311  'default' => current( array_keys( $ns ) ),
312  ];
313  foreach ( $ns as $n => $m ) {
314  $m = $this->msg( $m )->text();
315  $a['type']['options'][$m] = $n;
316  }
317  $a['value'] = [
318  'type' => 'text',
319  'label-message' => $mp . '-value' // Message: redirect-value
320  ];
321  // set the defaults according to the parsed subpage path
322  if ( !empty( $this->mType ) ) {
323  $a['type']['default'] = $this->mType;
324  }
325  if ( !empty( $this->mValue ) ) {
326  $a['value']['default'] = $this->mValue;
327  }
328 
329  return $a;
330  }
331 
332  public function onSubmit( array $data ) {
333  if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
334  $this->setParameter( $data['type'] . '/' . $data['value'] );
335  }
336 
337  /* if this returns false, will show the form */
338  return $this->dispatch();
339  }
340 
341  public function onSuccess() {
342  /* do nothing, we redirect in $this->dispatch if successful. */
343  }
344 
345  protected function alterForm( HTMLForm $form ) {
346  /* display summary at top of page */
347  $this->outputHeader();
348  // tweak label on submit button
349  // Message: redirect-submit
350  $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
351  /* submit form every time */
352  $form->setMethod( 'get' );
353  }
354 
355  protected function getDisplayFormat() {
356  return 'ooui';
357  }
358 
364  protected function getSubpagesForPrefixSearch() {
365  return [
366  'file',
367  'page',
368  'revision',
369  'user',
370  'logid',
371  ];
372  }
373 
377  public function requiresWrite() {
378  return false;
379  }
380 
384  public function requiresUnblock() {
385  return false;
386  }
387 
388  protected function getGroupName() {
389  return 'redirects';
390  }
391 }
dispatchRevision()
Handle Special:Redirect/revision/xxx (by redirecting to index.php?oldid=xxx)
dispatchPage()
Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
the array() calling protocol came about after MediaWiki 1.4rc1.
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1555
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept.
wfScript($script= 'index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
onSubmit(array $data)
setMethod($method= 'post')
Set the method used to submit the form.
Definition: HTMLForm.php:1576
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
dispatch()
Use appropriate dispatch* method to obtain a redirection URL, and either: redirect, set a 404 error code and error message, or do nothing (if $mValue wasn't set) allowing the form to be displayed.
static newFromId($id)
Static factory method for creation from a given user ID.
Definition: User.php:548
$mValue
The identifier/value for the redirect (which id, which file)
const PROTO_CURRENT
Definition: Defines.php:226
msg()
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Special page which uses an HTMLForm to handle processing.
outputHeader($summaryMessageKey= '')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
wfAppendQuery($url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
alterForm(HTMLForm $form)
setSubmitTextMsg($msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1348
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:128
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
const NS_FILE
Definition: Defines.php:62
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
dispatchUser()
Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
getMessagePrefix()
Get message prefix for HTMLForm.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:802
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
A special page that redirects to: the user for a numeric user id, the file for a given filename...
dispatchLog()
Handle Special:Redirect/logid/xxx (by redirecting to index.php?title=Special:Log) ...
const DB_REPLICA
Definition: defines.php:22
getRequest()
Get the WebRequest being used for this instance.
setParameter($subpage)
Set $mType and $mValue based on parsed value of $subpage.
wfFindFile($title, $options=[])
Find a file.
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
dispatchFile()
Handle Special:Redirect/file/xxxx.
$mType
The type of the redirect (user/file/revision)