MediaWiki REL1_32
SpecialInterwiki.php
Go to the documentation of this file.
1<?php
2
4
13 public function __construct() {
14 parent::__construct( 'Interwiki' );
15 }
16
17 public function doesWrites() {
18 return true;
19 }
20
26 function getDescription() {
27 return $this->msg( $this->canModify() ?
28 'interwiki' : 'interwiki-title-norights' )->plain();
29 }
30
31 public function getSubpagesForPrefixSearch() {
32 // delete, edit both require the prefix parameter.
33 return [ 'add' ];
34 }
35
41 public function execute( $par ) {
42 $this->setHeaders();
43 $this->outputHeader();
44
45 $out = $this->getOutput();
46 $request = $this->getRequest();
47
48 $out->addModuleStyles( 'ext.interwiki.specialpage' );
49
50 $action = $par ?: $request->getVal( 'action', $par );
51 $return = $this->getPageTitle();
52
53 switch ( $action ) {
54 case 'delete':
55 case 'edit':
56 case 'add':
57 if ( $this->canModify( $out ) ) {
58 $this->showForm( $action );
59 }
60 $out->returnToMain( false, $return );
61 break;
62 case 'submit':
63 if ( !$this->canModify( $out ) ) {
64 // Error msg added by canModify()
65 } elseif ( !$request->wasPosted() ||
66 !$this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) )
67 ) {
68 // Prevent cross-site request forgeries
69 $out->addWikiMsg( 'sessionfailure' );
70 } else {
71 $this->doSubmit();
72 }
73 $out->returnToMain( false, $return );
74 break;
75 default:
76 $this->showList();
77 break;
78 }
79 }
80
87 public function canModify( $out = false ) {
88 global $wgInterwikiCache;
89 if ( !$this->getUser()->isAllowed( 'interwiki' ) ) {
90 // Check permissions
91 if ( $out ) {
92 throw new PermissionsError( 'interwiki' );
93 }
94
95 return false;
96 } elseif ( $wgInterwikiCache ) {
97 // Editing the interwiki cache is not supported
98 if ( $out ) {
99 $out->addWikiMsg( 'interwiki-cached' );
100 }
101
102 return false;
103 } elseif ( wfReadOnly() ) {
104 throw new ReadOnlyError;
105 }
106
107 return true;
108 }
109
113 protected function showForm( $action ) {
114 $request = $this->getRequest();
115
116 $prefix = $request->getVal( 'prefix' );
117 $wpPrefix = '';
118 $label = [ 'class' => 'mw-label' ];
119 $input = [ 'class' => 'mw-input' ];
120
121 if ( $action === 'delete' ) {
122 $topmessage = $this->msg( 'interwiki_delquestion', $prefix )->text();
123 $intromessage = $this->msg( 'interwiki_deleting', $prefix )->escaped();
124 $wpPrefix = Html::hidden( 'wpInterwikiPrefix', $prefix );
125 $button = 'delete';
126 $formContent = '';
127 } elseif ( $action === 'edit' ) {
129 $row = $dbr->selectRow( 'interwiki', '*', [ 'iw_prefix' => $prefix ], __METHOD__ );
130
131 if ( !$row ) {
132 $this->error( 'interwiki_editerror', $prefix );
133 return;
134 }
135
136 $prefix = $prefixElement = $row->iw_prefix;
137 $defaulturl = $row->iw_url;
138 $trans = $row->iw_trans;
139 $local = $row->iw_local;
140 $wpPrefix = Html::hidden( 'wpInterwikiPrefix', $row->iw_prefix );
141 $topmessage = $this->msg( 'interwiki_edittext' )->text();
142 $intromessage = $this->msg( 'interwiki_editintro' )->escaped();
143 $button = 'edit';
144 } elseif ( $action === 'add' ) {
145 $prefix = $request->getVal( 'wpInterwikiPrefix', $request->getVal( 'prefix' ) );
146 $prefixElement = Xml::input( 'wpInterwikiPrefix', 20, $prefix,
147 [ 'tabindex' => 1, 'id' => 'mw-interwiki-prefix', 'maxlength' => 20 ] );
148 $local = $request->getCheck( 'wpInterwikiLocal' );
149 $trans = $request->getCheck( 'wpInterwikiTrans' );
150 $defaulturl = $request->getVal( 'wpInterwikiURL', $this->msg( 'interwiki-defaulturl' )->text() );
151 $topmessage = $this->msg( 'interwiki_addtext' )->text();
152 $intromessage = $this->msg( 'interwiki_addintro' )->escaped();
153 $button = 'interwiki_addbutton';
154 }
155
156 if ( $action === 'add' || $action === 'edit' ) {
157 $formContent = Html::rawElement( 'tr', null,
158 Html::element( 'td', $label, $this->msg( 'interwiki-prefix-label' )->text() ) .
159 Html::rawElement( 'td', null, '<code>' . $prefixElement . '</code>' )
160 ) . Html::rawElement(
161 'tr',
162 null,
163 Html::rawElement(
164 'td',
165 $label,
166 Xml::label( $this->msg( 'interwiki-local-label' )->text(), 'mw-interwiki-local' )
167 ) .
168 Html::rawElement(
169 'td',
170 $input,
171 Xml::check( 'wpInterwikiLocal', $local, [ 'id' => 'mw-interwiki-local' ] )
172 )
173 ) . Html::rawElement( 'tr', null,
174 Html::rawElement(
175 'td',
176 $label,
177 Xml::label( $this->msg( 'interwiki-trans-label' )->text(), 'mw-interwiki-trans' )
178 ) .
179 Html::rawElement(
180 'td',
181 $input, Xml::check( 'wpInterwikiTrans', $trans, [ 'id' => 'mw-interwiki-trans' ] ) )
182 ) . Html::rawElement( 'tr', null,
183 Html::rawElement(
184 'td',
185 $label,
186 Xml::label( $this->msg( 'interwiki-url-label' )->text(), 'mw-interwiki-url' )
187 ) .
188 Html::rawElement( 'td', $input, Xml::input( 'wpInterwikiURL', 60, $defaulturl,
189 [ 'tabindex' => 1, 'maxlength' => 200, 'id' => 'mw-interwiki-url' ] ) )
190 );
191 }
192
193 $form = Xml::fieldset( $topmessage, Html::rawElement(
194 'form',
195 [
196 'id' => "mw-interwiki-{$action}form",
197 'method' => 'post',
198 'action' => $this->getPageTitle()->getLocalURL( [
199 'action' => 'submit',
200 'prefix' => $prefix
201 ] )
202 ],
203 Html::rawElement( 'p', null, $intromessage ) .
204 Html::rawElement( 'table', [ 'id' => "mw-interwiki-{$action}" ],
205 $formContent . Html::rawElement( 'tr', null,
206 Html::rawElement( 'td', $label, Xml::label( $this->msg( 'interwiki_reasonfield' )->text(),
207 "mw-interwiki-{$action}reason" ) ) .
208 Html::rawElement( 'td', $input, Xml::input( 'wpInterwikiReason', 60, '',
209 [ 'tabindex' => 1, 'id' => "mw-interwiki-{$action}reason", 'maxlength' => 200 ] ) )
210 ) . Html::rawElement( 'tr', null,
211 Html::rawElement( 'td', null, '' ) .
212 Html::rawElement( 'td', [ 'class' => 'mw-submit' ],
213 Xml::submitButton( $this->msg( $button )->text(), [ 'id' => 'mw-interwiki-submit' ] ) )
214 ) . $wpPrefix .
215 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
216 Html::hidden( 'wpInterwikiAction', $action )
217 )
218 ) );
219 $this->getOutput()->addHTML( $form );
220 }
221
222 protected function doSubmit() {
223 global $wgContLang;
224
225 $request = $this->getRequest();
226 $prefix = $request->getVal( 'wpInterwikiPrefix' );
227 $do = $request->getVal( 'wpInterwikiAction' );
228 // Show an error if the prefix is invalid (only when adding one).
229 // Invalid characters for a title should also be invalid for a prefix.
230 // Whitespace, ':', '&' and '=' are invalid, too.
231 // (Bug 30599).
232 global $wgLegalTitleChars;
233 $validPrefixChars = preg_replace( '/[ :&=]/', '', $wgLegalTitleChars );
234 if ( $do === 'add' && preg_match( "/\s|[^$validPrefixChars]/", $prefix ) ) {
235 $this->error( 'interwiki-badprefix', htmlspecialchars( $prefix ) );
236 $this->showForm( $do );
237 return;
238 }
239 $reason = $request->getText( 'wpInterwikiReason' );
240 $selfTitle = $this->getPageTitle();
241 $lookup = MediaWikiServices::getInstance()->getInterwikiLookup();
242 $dbw = wfGetDB( DB_MASTER );
243 switch ( $do ) {
244 case 'delete':
245 $dbw->delete( 'interwiki', [ 'iw_prefix' => $prefix ], __METHOD__ );
246
247 if ( $dbw->affectedRows() === 0 ) {
248 $this->error( 'interwiki_delfailed', $prefix );
249 $this->showForm( $do );
250 } else {
251 $this->getOutput()->addWikiMsg( 'interwiki_deleted', $prefix );
252 $log = new LogPage( 'interwiki' );
253 $log->addEntry( 'iw_delete', $selfTitle, $reason, [ $prefix ] );
254 $lookup->invalidateCache( $prefix );
255 }
256 break;
258 case 'add':
259 $prefix = $wgContLang->lc( $prefix );
260 case 'edit':
261 $theurl = $request->getVal( 'wpInterwikiURL' );
262 $local = $request->getCheck( 'wpInterwikiLocal' ) ? 1 : 0;
263 $trans = $request->getCheck( 'wpInterwikiTrans' ) ? 1 : 0;
264 $data = [
265 'iw_prefix' => $prefix,
266 'iw_url' => $theurl,
267 'iw_local' => $local,
268 'iw_trans' => $trans
269 ];
270
271 if ( $prefix === '' || $theurl === '' ) {
272 $this->error( 'interwiki-submit-empty' );
273 $this->showForm( $do );
274 return;
275 }
276
277 // Simple URL validation: check that the protocol is one of
278 // the supported protocols for this wiki.
279 // (bug 30600)
280 if ( !wfParseUrl( $theurl ) ) {
281 $this->error( 'interwiki-submit-invalidurl' );
282 $this->showForm( $do );
283 return;
284 }
285
286 if ( $do === 'add' ) {
287 $dbw->insert( 'interwiki', $data, __METHOD__, 'IGNORE' );
288 } else { // $do === 'edit'
289 $dbw->update( 'interwiki', $data, [ 'iw_prefix' => $prefix ], __METHOD__, 'IGNORE' );
290 }
291
292 // used here: interwiki_addfailed, interwiki_added, interwiki_edited
293 if ( $dbw->affectedRows() === 0 ) {
294 $this->error( "interwiki_{$do}failed", $prefix );
295 $this->showForm( $do );
296 } else {
297 $this->getOutput()->addWikiMsg( "interwiki_{$do}ed", $prefix );
298 $log = new LogPage( 'interwiki' );
299 $log->addEntry( 'iw_' . $do, $selfTitle, $reason, [ $prefix, $theurl, $trans, $local ] );
300 $lookup->invalidateCache( $prefix );
301 }
302 break;
303 }
304 }
305
306 protected function showList() {
307 global $wgInterwikiCentralDB, $wgInterwikiViewOnly;
308 $canModify = $this->canModify();
309
310 // Build lists
311 $lookup = MediaWikiServices::getInstance()->getInterwikiLookup();
312 $iwPrefixes = $lookup->getAllPrefixes( null );
313 $iwGlobalPrefixes = [];
314 if ( $wgInterwikiCentralDB !== null && $wgInterwikiCentralDB !== wfWikiID() ) {
315 // Fetch list from global table
316 $dbrCentralDB = wfGetDB( DB_REPLICA, [], $wgInterwikiCentralDB );
317 $res = $dbrCentralDB->select( 'interwiki', '*', false, __METHOD__ );
318 $retval = [];
319 foreach ( $res as $row ) {
320 $row = (array)$row;
321 if ( !Language::fetchLanguageName( $row['iw_prefix'] ) ) {
322 $retval[] = $row;
323 }
324 }
325 $iwGlobalPrefixes = $retval;
326 }
327
328 // Split out language links
329 $iwLocalPrefixes = [];
330 $iwLanguagePrefixes = [];
331 foreach ( $iwPrefixes as $iwPrefix ) {
332 if ( Language::fetchLanguageName( $iwPrefix['iw_prefix'] ) ) {
333 $iwLanguagePrefixes[] = $iwPrefix;
334 } else {
335 $iwLocalPrefixes[] = $iwPrefix;
336 }
337 }
338
339 // Page intro content
340 $this->getOutput()->addWikiMsg( 'interwiki_intro' );
341
342 // Add 'view log' link when possible
343 if ( $wgInterwikiViewOnly === false ) {
344 $logLink = $this->getLinkRenderer()->makeLink(
345 SpecialPage::getTitleFor( 'Log', 'interwiki' ),
346 $this->msg( 'interwiki-logtext' )->text()
347 );
348 $this->getOutput()->addHTML( '<p class="mw-interwiki-log">' . $logLink . '</p>' );
349 }
350
351 // Add 'add' link
352 if ( $canModify ) {
353 if ( count( $iwGlobalPrefixes ) !== 0 ) {
354 $addtext = $this->msg( 'interwiki-addtext-local' )->text();
355 } else {
356 $addtext = $this->msg( 'interwiki_addtext' )->text();
357 }
358 $addlink = $this->getLinkRenderer()->makeKnownLink(
359 $this->getPageTitle( 'add' ), $addtext );
360 $this->getOutput()->addHTML(
361 '<p class="mw-interwiki-addlink">' . $addlink . '</p>' );
362 }
363
364 $this->getOutput()->addWikiMsg( 'interwiki-legend' );
365
366 if ( ( !is_array( $iwPrefixes ) || count( $iwPrefixes ) === 0 ) &&
367 ( !is_array( $iwGlobalPrefixes ) || count( $iwGlobalPrefixes ) === 0 )
368 ) {
369 // If the interwiki table(s) are empty, display an error message
370 $this->error( 'interwiki_error' );
371 return;
372 }
373
374 // Add the global table
375 if ( count( $iwGlobalPrefixes ) !== 0 ) {
376 $this->getOutput()->addHTML(
377 '<h2 id="interwikitable-global">' .
378 $this->msg( 'interwiki-global-links' )->parse() .
379 '</h2>'
380 );
381 $this->getOutput()->addWikiMsg( 'interwiki-global-description' );
382
383 // $canModify is false here because this is just a display of remote data
384 $this->makeTable( false, $iwGlobalPrefixes );
385 }
386
387 // Add the local table
388 if ( count( $iwLocalPrefixes ) !== 0 ) {
389 if ( count( $iwGlobalPrefixes ) !== 0 ) {
390 $this->getOutput()->addHTML(
391 '<h2 id="interwikitable-local">' .
392 $this->msg( 'interwiki-local-links' )->parse() .
393 '</h2>'
394 );
395 $this->getOutput()->addWikiMsg( 'interwiki-local-description' );
396 } else {
397 $this->getOutput()->addHTML(
398 '<h2 id="interwikitable-local">' .
399 $this->msg( 'interwiki-links' )->parse() .
400 '</h2>'
401 );
402 $this->getOutput()->addWikiMsg( 'interwiki-description' );
403 }
404 $this->makeTable( $canModify, $iwLocalPrefixes );
405 }
406
407 // Add the language table
408 if ( count( $iwLanguagePrefixes ) !== 0 ) {
409 $this->getOutput()->addHTML(
410 '<h2 id="interwikitable-language">' .
411 $this->msg( 'interwiki-language-links' )->parse() .
412 '</h2>'
413 );
414 $this->getOutput()->addWikiMsg( 'interwiki-language-description' );
415
416 $this->makeTable( $canModify, $iwLanguagePrefixes );
417 }
418 }
419
420 protected function makeTable( $canModify, $iwPrefixes ) {
421 // Output the existing Interwiki prefixes table header
422 $out = '';
423 $out .= Html::openElement(
424 'table',
425 [ 'class' => 'mw-interwikitable wikitable sortable body' ]
426 ) . "\n";
427 $out .= Html::openElement( 'tr', [ 'class' => 'interwikitable-header' ] ) .
428 Html::element( 'th', null, $this->msg( 'interwiki_prefix' )->text() ) .
429 Html::element( 'th', null, $this->msg( 'interwiki_url' )->text() ) .
430 Html::element( 'th', null, $this->msg( 'interwiki_local' )->text() ) .
431 Html::element( 'th', null, $this->msg( 'interwiki_trans' )->text() ) .
432 ( $canModify ?
433 Html::element(
434 'th',
435 [ 'class' => 'unsortable' ],
436 $this->msg( 'interwiki_edit' )->text()
437 ) :
438 ''
439 );
440 $out .= Html::closeElement( 'tr' ) . "\n";
441
442 $selfTitle = $this->getPageTitle();
443
444 // Output the existing Interwiki prefixes table rows
445 foreach ( $iwPrefixes as $iwPrefix ) {
446 $out .= Html::openElement( 'tr', [ 'class' => 'mw-interwikitable-row' ] );
447 $out .= Html::element( 'td', [ 'class' => 'mw-interwikitable-prefix' ],
448 $iwPrefix['iw_prefix'] );
449 $out .= Html::element(
450 'td',
451 [ 'class' => 'mw-interwikitable-url' ],
452 $iwPrefix['iw_url']
453 );
454 $attribs = [ 'class' => 'mw-interwikitable-local' ];
455 // Green background for cells with "yes".
456 if ( isset( $iwPrefix['iw_local'] ) && $iwPrefix['iw_local'] ) {
457 $attribs['class'] .= ' mw-interwikitable-local-yes';
458 }
459 // The messages interwiki_0 and interwiki_1 are used here.
460 $contents = isset( $iwPrefix['iw_local'] ) ?
461 $this->msg( 'interwiki_' . $iwPrefix['iw_local'] )->text() :
462 '-';
463 $out .= Html::element( 'td', $attribs, $contents );
464 $attribs = [ 'class' => 'mw-interwikitable-trans' ];
465 // Green background for cells with "yes".
466 if ( isset( $iwPrefix['iw_trans'] ) && $iwPrefix['iw_trans'] ) {
467 $attribs['class'] .= ' mw-interwikitable-trans-yes';
468 }
469 // The messages interwiki_0 and interwiki_1 are used here.
470 $contents = isset( $iwPrefix['iw_trans'] ) ?
471 $this->msg( 'interwiki_' . $iwPrefix['iw_trans'] )->text() :
472 '-';
473 $out .= Html::element( 'td', $attribs, $contents );
474
475 // Additional column when the interwiki table can be modified.
476 if ( $canModify ) {
477 $out .= Html::rawElement( 'td', [ 'class' => 'mw-interwikitable-modify' ],
478 $this->getLinkRenderer()->makeKnownLink(
479 $selfTitle,
480 $this->msg( 'edit' )->text(),
481 [],
482 [ 'action' => 'edit', 'prefix' => $iwPrefix['iw_prefix'] ]
483 ) .
484 $this->msg( 'comma-separator' ) .
485 $this->getLinkRenderer()->makeKnownLink(
486 $selfTitle,
487 $this->msg( 'delete' )->text(),
488 [],
489 [ 'action' => 'delete', 'prefix' => $iwPrefix['iw_prefix'] ]
490 )
491 );
492 }
493 $out .= Html::closeElement( 'tr' ) . "\n";
494 }
495 $out .= Html::closeElement( 'table' );
496
497 $this->getOutput()->addHTML( $out );
498 }
499
500 protected function error() {
501 $args = func_get_args();
502 $this->getOutput()->wrapWikiMsg( "<p class='error'>$1</p>", $args );
503 }
504
505 protected function getGroupName() {
506 return 'wiki';
507 }
508}
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
$wgLegalTitleChars
Allowed title characters – regex character class Don't change this unless you know what you're doing.
bool array string $wgInterwikiCache
Interwiki cache, either as an associative array or a path to a constant database (....
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfReadOnly()
Check whether the wiki is in read-only mode.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
$wgContLang
Definition Setup.php:809
if( $line===false) $args
Definition cdb.php:64
Class to simplify the use of log pages.
Definition LogPage.php:33
MediaWikiServices is the service locator for the application scope of MediaWiki.
Show an error when a user tries to do something they do not have the necessary permissions for.
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
Implements Special:Interwiki.
canModify( $out=false)
Returns boolean whether the user can modify the data.
getDescription()
Different description will be shown on Special:SpecialPage depending on whether the user can modify t...
__construct()
Constructor - sets up the new special page.
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept for prefix searches.
doesWrites()
Indicates whether this special page may perform database writes.
execute( $par)
Show the special page.
makeTable( $canModify, $iwPrefixes)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Parent class for all special pages.
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.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
msg( $key)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
$res
Definition database.txt:21
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:2880
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 incomplete not yet checked for validity & $retval
Definition hooks.txt:266
either a plain
Definition hooks.txt:2105
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:894
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2063
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
if(is_array($mode)) switch( $mode) $input
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26