MediaWiki REL1_29
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 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
179 return wfAppendQuery( wfScript( 'index' ), $query );
180 }
181
190 function dispatch() {
191 // the various namespaces supported by Special:Redirect
192 switch ( $this->mType ) {
193 case 'user':
194 $url = $this->dispatchUser();
195 break;
196 case 'file':
197 $url = $this->dispatchFile();
198 break;
199 case 'revision':
200 $url = $this->dispatchRevision();
201 break;
202 case 'page':
203 $url = $this->dispatchPage();
204 break;
205 case 'logid':
206 $url = $this->dispatchLog();
207 break;
208 default:
209 $url = null;
210 break;
211 }
212 if ( $url ) {
213 $this->getOutput()->redirect( $url );
214
215 return true;
216 }
217 if ( !is_null( $this->mValue ) ) {
218 $this->getOutput()->setStatusCode( 404 );
219 // Message: redirect-not-exists
220 $msg = $this->getMessagePrefix() . '-not-exists';
221
222 return Status::newFatal( $msg );
223 }
224
225 return false;
226 }
227
228 protected function getFormFields() {
229 $mp = $this->getMessagePrefix();
230 $ns = [
231 // subpage => message
232 // Messages: redirect-user, redirect-page, redirect-revision,
233 // redirect-file, redirect-logid
234 'user' => $mp . '-user',
235 'page' => $mp . '-page',
236 'revision' => $mp . '-revision',
237 'file' => $mp . '-file',
238 'logid' => $mp . '-logid',
239 ];
240 $a = [];
241 $a['type'] = [
242 'type' => 'select',
243 'label-message' => $mp . '-lookup', // Message: redirect-lookup
244 'options' => [],
245 'default' => current( array_keys( $ns ) ),
246 ];
247 foreach ( $ns as $n => $m ) {
248 $m = $this->msg( $m )->text();
249 $a['type']['options'][$m] = $n;
250 }
251 $a['value'] = [
252 'type' => 'text',
253 'label-message' => $mp . '-value' // Message: redirect-value
254 ];
255 // set the defaults according to the parsed subpage path
256 if ( !empty( $this->mType ) ) {
257 $a['type']['default'] = $this->mType;
258 }
259 if ( !empty( $this->mValue ) ) {
260 $a['value']['default'] = $this->mValue;
261 }
262
263 return $a;
264 }
265
266 public function onSubmit( array $data ) {
267 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
268 $this->setParameter( $data['type'] . '/' . $data['value'] );
269 }
270
271 /* if this returns false, will show the form */
272 return $this->dispatch();
273 }
274
275 public function onSuccess() {
276 /* do nothing, we redirect in $this->dispatch if successful. */
277 }
278
279 protected function alterForm( HTMLForm $form ) {
280 /* display summary at top of page */
281 $this->outputHeader();
282 // tweak label on submit button
283 // Message: redirect-submit
284 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
285 /* submit form every time */
286 $form->setMethod( 'get' );
287 }
288
289 protected function getDisplayFormat() {
290 return 'ooui';
291 }
292
298 protected function getSubpagesForPrefixSearch() {
299 return [
300 'file',
301 'page',
302 'revision',
303 'user',
304 'logid',
305 ];
306 }
307
311 public function requiresWrite() {
312 return false;
313 }
314
318 public function requiresUnblock() {
319 return false;
320 }
321
322 protected function getGroupName() {
323 return 'redirects';
324 }
325}
wfFindFile( $title, $options=[])
Find a file.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Special page which uses an HTMLForm to handle processing.
getMessagePrefix()
Get message prefix for HTMLForm.
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:128
setMethod( $method='post')
Set the method used to submit the form.
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
getOutput()
Get the OutputPage being used for this instance.
getRequest()
Get the WebRequest being used for this instance.
msg()
Wrapper around wfMessage that sets the current context.
A special page that redirects to: the user for a numeric user id, the file for a given filename,...
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept.
setParameter( $subpage)
Set $mType and $mValue based on parsed value of $subpage.
onSubmit(array $data)
Process the form on POST submission.
getDisplayFormat()
Get display format for the form.
dispatchLog()
Handle Special:Redirect/logid/xxx (by redirecting to index.php?title=Special:Log&logid=xxx)
getFormFields()
Get an HTMLForm descriptor array.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
dispatchUser()
Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
dispatch()
Use appropriate dispatch* method to obtain a redirection URL, and either: redirect,...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
dispatchRevision()
Handle Special:Redirect/revision/xxx (by redirecting to index.php?oldid=xxx)
$mValue
The identifier/value for the redirect (which id, which file)
dispatchPage()
Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
dispatchFile()
Handle Special:Redirect/file/xxxx.
$mType
The type of the redirect (user/file/revision)
Represents a title within MediaWiki.
Definition Title.php:39
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
const NS_USER
Definition Defines.php:64
const NS_FILE
Definition Defines.php:68
const PROTO_CURRENT
Definition Defines.php:220
the array() calling protocol came about after MediaWiki 1.4rc1.
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:249
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2723
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:785
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:1601
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:37