MediaWiki REL1_30
SpecialRedirect.php
Go to the documentation of this file.
1<?php
32
40 protected $mType;
41
49 protected $mValue;
50
51 function __construct() {
52 parent::__construct( 'Redirect' );
53 $this->mType = null;
54 $this->mValue = null;
55 }
56
61 function setParameter( $subpage ) {
62 // parse $subpage to pull out the parts
63 $parts = explode( '/', $subpage, 2 );
64 $this->mType = count( $parts ) > 0 ? $parts[0] : null;
65 $this->mValue = count( $parts ) > 1 ? $parts[1] : null;
66 }
67
73 function dispatchUser() {
74 if ( !ctype_digit( $this->mValue ) ) {
75 return null;
76 }
77 $user = User::newFromId( (int)$this->mValue );
78 $username = $user->getName(); // load User as side-effect
79 if ( $user->isAnon() ) {
80 return null;
81 }
82 $userpage = Title::makeTitle( NS_USER, $username );
83
84 return $userpage->getFullURL( '', false, PROTO_CURRENT );
85 }
86
92 function dispatchFile() {
93 $title = Title::makeTitleSafe( NS_FILE, $this->mValue );
94
95 if ( !$title instanceof Title ) {
96 return null;
97 }
98 $file = wfFindFile( $title );
99
100 if ( !$file || !$file->exists() ) {
101 return null;
102 }
103 // Default behavior: Use the direct link to the file.
104 $url = $file->getUrl();
105 $request = $this->getRequest();
106 $width = $request->getInt( 'width', -1 );
107 $height = $request->getInt( 'height', -1 );
108
109 // If a width is requested...
110 if ( $width != -1 ) {
111 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
112 // ... and we can
113 if ( $mto && !$mto->isError() ) {
114 // ... change the URL to point to a thumbnail.
115 $url = $mto->getUrl();
116 }
117 }
118
119 return $url;
120 }
121
128 function dispatchRevision() {
129 $oldid = $this->mValue;
130 if ( !ctype_digit( $oldid ) ) {
131 return null;
132 }
133 $oldid = (int)$oldid;
134 if ( $oldid === 0 ) {
135 return null;
136 }
137
138 return wfAppendQuery( wfScript( 'index' ), [
139 'oldid' => $oldid
140 ] );
141 }
142
148 function dispatchPage() {
149 $curid = $this->mValue;
150 if ( !ctype_digit( $curid ) ) {
151 return null;
152 }
153 $curid = (int)$curid;
154 if ( $curid === 0 ) {
155 return null;
156 }
157
158 return wfAppendQuery( wfScript( 'index' ), [
159 'curid' => $curid
160 ] );
161 }
162
170 function dispatchLog() {
171 $logid = $this->mValue;
172 if ( !ctype_digit( $logid ) ) {
173 return null;
174 }
175 $logid = (int)$logid;
176 if ( $logid === 0 ) {
177 return null;
178 }
179
180 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
181 return wfAppendQuery( wfScript( 'index' ), $query );
182 }
183
192 function dispatch() {
193 // the various namespaces supported by Special:Redirect
194 switch ( $this->mType ) {
195 case 'user':
196 $url = $this->dispatchUser();
197 break;
198 case 'file':
199 $url = $this->dispatchFile();
200 break;
201 case 'revision':
202 $url = $this->dispatchRevision();
203 break;
204 case 'page':
205 $url = $this->dispatchPage();
206 break;
207 case 'logid':
208 $url = $this->dispatchLog();
209 break;
210 default:
211 $url = null;
212 break;
213 }
214 if ( $url ) {
215 $this->getOutput()->redirect( $url );
216
217 return true;
218 }
219 if ( !is_null( $this->mValue ) ) {
220 $this->getOutput()->setStatusCode( 404 );
221 // Message: redirect-not-exists
222 $msg = $this->getMessagePrefix() . '-not-exists';
223
224 return Status::newFatal( $msg );
225 }
226
227 return false;
228 }
229
230 protected function getFormFields() {
231 $mp = $this->getMessagePrefix();
232 $ns = [
233 // subpage => message
234 // Messages: redirect-user, redirect-page, redirect-revision,
235 // redirect-file, redirect-logid
236 'user' => $mp . '-user',
237 'page' => $mp . '-page',
238 'revision' => $mp . '-revision',
239 'file' => $mp . '-file',
240 'logid' => $mp . '-logid',
241 ];
242 $a = [];
243 $a['type'] = [
244 'type' => 'select',
245 'label-message' => $mp . '-lookup', // Message: redirect-lookup
246 'options' => [],
247 'default' => current( array_keys( $ns ) ),
248 ];
249 foreach ( $ns as $n => $m ) {
250 $m = $this->msg( $m )->text();
251 $a['type']['options'][$m] = $n;
252 }
253 $a['value'] = [
254 'type' => 'text',
255 'label-message' => $mp . '-value' // Message: redirect-value
256 ];
257 // set the defaults according to the parsed subpage path
258 if ( !empty( $this->mType ) ) {
259 $a['type']['default'] = $this->mType;
260 }
261 if ( !empty( $this->mValue ) ) {
262 $a['value']['default'] = $this->mValue;
263 }
264
265 return $a;
266 }
267
268 public function onSubmit( array $data ) {
269 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
270 $this->setParameter( $data['type'] . '/' . $data['value'] );
271 }
272
273 /* if this returns false, will show the form */
274 return $this->dispatch();
275 }
276
277 public function onSuccess() {
278 /* do nothing, we redirect in $this->dispatch if successful. */
279 }
280
281 protected function alterForm( HTMLForm $form ) {
282 /* display summary at top of page */
283 $this->outputHeader();
284 // tweak label on submit button
285 // Message: redirect-submit
286 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
287 /* submit form every time */
288 $form->setMethod( 'get' );
289 }
290
291 protected function getDisplayFormat() {
292 return 'ooui';
293 }
294
300 protected function getSubpagesForPrefixSearch() {
301 return [
302 'file',
303 'page',
304 'revision',
305 'user',
306 'logid',
307 ];
308 }
309
313 public function requiresWrite() {
314 return false;
315 }
316
320 public function requiresUnblock() {
321 return false;
322 }
323
324 protected function getGroupName() {
325 return 'redirects';
326 }
327}
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.
msg( $key)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
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:67
const NS_FILE
Definition Defines.php:71
const PROTO_CURRENT
Definition Defines.php:223
the array() calling protocol came about after MediaWiki 1.4rc1.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2775
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:962
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:783
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:1610
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:247
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