MediaWiki  1.23.6
SpecialRedirect.php
Go to the documentation of this file.
1 <?php
33 
40  protected $mType;
41 
48  protected $mValue;
49 
50  function __construct() {
51  parent::__construct( 'Redirect' );
52  $this->mType = null;
53  $this->mValue = null;
54  }
55 
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( array( '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' ), array(
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' ), array(
157  'curid' => $curid
158  ) );
159  }
160 
169  function dispatch() {
170  // the various namespaces supported by Special:Redirect
171  switch ( $this->mType ) {
172  case 'user':
173  $url = $this->dispatchUser();
174  break;
175  case 'file':
176  $url = $this->dispatchFile();
177  break;
178  case 'revision':
179  $url = $this->dispatchRevision();
180  break;
181  case 'page':
182  $url = $this->dispatchPage();
183  break;
184  default:
185  $this->getOutput()->setStatusCode( 404 );
186  $url = null;
187  break;
188  }
189  if ( $url ) {
190  $this->getOutput()->redirect( $url );
191 
192  return true;
193  }
194  if ( !is_null( $this->mValue ) ) {
195  $this->getOutput()->setStatusCode( 404 );
196  // Message: redirect-not-exists
197  $msg = $this->getMessagePrefix() . '-not-exists';
198 
199  return Status::newFatal( $msg );
200  }
201 
202  return false;
203  }
204 
205  protected function getFormFields() {
206  $mp = $this->getMessagePrefix();
207  $ns = array(
208  // subpage => message
209  // Messages: redirect-user, redirect-page, redirect-revision,
210  // redirect-file
211  'user' => $mp . '-user',
212  'page' => $mp . '-page',
213  'revision' => $mp . '-revision',
214  'file' => $mp . '-file',
215  );
216  $a = array();
217  $a['type'] = array(
218  'type' => 'select',
219  'label-message' => $mp . '-lookup', // Message: redirect-lookup
220  'options' => array(),
221  'default' => current( array_keys( $ns ) ),
222  );
223  foreach ( $ns as $n => $m ) {
224  $m = $this->msg( $m )->text();
225  $a['type']['options'][$m] = $n;
226  }
227  $a['value'] = array(
228  'type' => 'text',
229  'label-message' => $mp . '-value' // Message: redirect-value
230  );
231  // set the defaults according to the parsed subpage path
232  if ( !empty( $this->mType ) ) {
233  $a['type']['default'] = $this->mType;
234  }
235  if ( !empty( $this->mValue ) ) {
236  $a['value']['default'] = $this->mValue;
237  }
238 
239  return $a;
240  }
241 
242  public function onSubmit( array $data ) {
243  if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
244  $this->setParameter( $data['type'] . '/' . $data['value'] );
245  }
246 
247  /* if this returns false, will show the form */
248  return $this->dispatch();
249  }
250 
251  public function onSuccess() {
252  /* do nothing, we redirect in $this->dispatch if successful. */
253  }
254 
255  protected function alterForm( HTMLForm $form ) {
256  /* display summary at top of page */
257  $this->outputHeader();
258  // tweak label on submit button
259  // Message: redirect-submit
260  $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
261  /* submit form every time */
262  $form->setMethod( 'get' );
263  }
264 
265  protected function getGroupName() {
266  return 'redirects';
267  }
268 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:411
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
SpecialRedirect\$mValue
string $mValue
Definition: SpecialRedirect.php:46
$form
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead $form
Definition: hooks.txt:2573
SpecialRedirect\onSubmit
onSubmit(array $data)
Process the form on POST submission.
Definition: SpecialRedirect.php:240
$n
$n
Definition: RandomTest.php:76
SpecialRedirect\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialRedirect.php:249
NS_FILE
const NS_FILE
Definition: Defines.php:85
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialRedirect\$mType
string $mType
Definition: SpecialRedirect.php:39
HTMLForm\setMethod
setMethod( $method='post')
Set the method used to submit the form.
Definition: HTMLForm.php:1146
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition: GlobalFunctions.php:459
FormSpecialPage\getMessagePrefix
getMessagePrefix()
Get message prefix for HTMLForm.
Definition: FormSpecialPage.php:72
SpecialRedirect
A special page that redirects to: the user for a numeric user id, the file for a given filename,...
Definition: SpecialRedirect.php:32
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:3739
SpecialRedirect\dispatch
dispatch()
Use appropriate dispatch* method to obtain a redirection URL, and either: redirect,...
Definition: SpecialRedirect.php:167
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:270
SpecialRedirect\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialRedirect.php:203
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialRedirect\dispatchRevision
dispatchRevision()
Handle Special:Redirect/revision/xxx (by redirecting to index.php?oldid=xxx)
Definition: SpecialRedirect.php:124
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
SpecialRedirect\__construct
__construct()
Definition: SpecialRedirect.php:48
SpecialRedirect\dispatchFile
dispatchFile()
Handle Special:Redirect/file/xxxx.
Definition: SpecialRedirect.php:88
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:525
SpecialRedirect\dispatchUser
dispatchUser()
Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
Definition: SpecialRedirect.php:69
$user
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 account $user
Definition: hooks.txt:237
SpecialRedirect\dispatchPage
dispatchPage()
Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
Definition: SpecialRedirect.php:144
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:976
SpecialRedirect\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialRedirect.php:253
Title
Represents a title within MediaWiki.
Definition: Title.php:35
SpecialRedirect\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialRedirect.php:263
as
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
wfFindFile
wfFindFile( $title, $options=array())
Find a file.
Definition: GlobalFunctions.php:3702
NS_USER
const NS_USER
Definition: Defines.php:81
SpecialRedirect\setParameter
setParameter( $subpage)
Set $mType and $mValue based on parsed value of $subpage.
Definition: SpecialRedirect.php:57
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:443
Status\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: Status.php:63
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:100