msg( $this->canModify() ? 'interwiki' : 'interwiki-title-norights' )->plain(); } public function getSubpagesForPrefixSearch() { // delete, edit both require the prefix parameter. return [ 'add' ]; } /** * Show the special page * * @param string|null $par parameter passed to the page or null */ public function execute( $par ) { $this->setHeaders(); $this->outputHeader(); $out = $this->getOutput(); $request = $this->getRequest(); $out->addModuleStyles( 'ext.interwiki.specialpage' ); $action = $par ?: $request->getVal( 'action', $par ); if ( !in_array( $action, [ 'add', 'edit', 'delete' ] ) || !$this->canModify( $out ) ) { $this->showList(); } else { $this->showForm( $action ); } } /** * Returns boolean whether the user can modify the data. * @param OutputPage|bool $out If $wgOut object given, it adds the respective error message. * @return bool * @throws PermissionsError|ReadOnlyError */ public function canModify( $out = false ) { global $wgInterwikiCache; if ( !$this->getUser()->isAllowed( 'interwiki' ) ) { // Check permissions if ( $out ) { throw new PermissionsError( 'interwiki' ); } return false; } elseif ( $wgInterwikiCache ) { // Editing the interwiki cache is not supported if ( $out ) { $out->addWikiMsg( 'interwiki-cached' ); } return false; } else { $this->checkReadOnly(); } return true; } /** * @param string $action The action of the form */ protected function showForm( $action ) { $formDescriptor = []; $hiddenFields = [ 'action' => $action, ]; $status = Status::newGood(); $request = $this->getRequest(); $prefix = $request->getVal( 'prefix', $request->getVal( 'hiddenPrefix' ) ); switch ( $action ) { case 'add': case 'edit': $formDescriptor = [ 'prefix' => [ 'type' => 'text', 'label-message' => 'interwiki-prefix-label', 'name' => 'prefix', ], 'local' => [ 'type' => 'check', 'id' => 'mw-interwiki-local', 'label-message' => 'interwiki-local-label', 'name' => 'local', ], 'trans' => [ 'type' => 'check', 'id' => 'mw-interwiki-trans', 'label-message' => 'interwiki-trans-label', 'name' => 'trans', ], 'url' => [ 'type' => 'url', 'id' => 'mw-interwiki-url', 'label-message' => 'interwiki-url-label', 'maxlength' => 200, 'name' => 'wpInterwikiURL', 'size' => 60, 'tabindex' => 1, ], 'reason' => [ 'type' => 'text', 'id' => "mw-interwiki-{$action}reason", 'label-message' => 'interwiki_reasonfield', 'maxlength' => 200, 'name' => 'wpInterwikiReason', 'size' => 60, 'tabindex' => 1, ], ]; break; case 'delete': $formDescriptor = [ 'prefix' => [ 'type' => 'hidden', 'name' => 'prefix', 'default' => $prefix, ], 'reason' => [ 'type' => 'text', 'name' => 'reason', 'label-message' => 'interwiki_reasonfield', ], ]; break; } $formDescriptor['hiddenPrefix'] = [ 'type' => 'hidden', 'name' => 'hiddenPrefix', 'default' => $prefix, ]; if ( $action === 'edit' ) { $dbr = wfGetDB( DB_REPLICA ); $row = $dbr->selectRow( 'interwiki', '*', [ 'iw_prefix' => $prefix ], __METHOD__ ); $formDescriptor['prefix']['disabled'] = true; $formDescriptor['prefix']['default'] = $prefix; $hiddenFields['prefix'] = $prefix; if ( !$row ) { $status->fatal( 'interwiki_editerror', $prefix ); } else { $formDescriptor['url']['default'] = $row->iw_url; $formDescriptor['url']['trans'] = $row->iw_trans; $formDescriptor['url']['local'] = $row->iw_local; } } if ( !$status->isOK() ) { $formDescriptor = []; } $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ); $htmlForm ->addHiddenFields( $hiddenFields ) ->setSubmitCallback( [ $this, 'onSubmit' ] ); if ( $status->isOK() ) { if ( $action === 'delete' ) { $htmlForm->setSubmitDestructive(); } $htmlForm->setSubmitTextMsg( $action !== 'add' ? $action : 'interwiki_addbutton' ) ->setIntro( $this->msg( $action !== 'delete' ? "interwiki_{$action}intro" : 'interwiki_deleting', $prefix ) ) ->show(); } else { $htmlForm->suppressDefaultSubmit() ->prepareForm() ->displayForm( $status ); } $this->getOutput()->addBacklinkSubtitle( $this->getPageTitle() ); } public function onSubmit( array $data ) { global $wgInterwikiCentralInterlanguageDB; $status = Status::newGood(); $request = $this->getRequest(); $prefix = $this->getRequest()->getVal( 'prefix', '' ); $do = $request->getVal( 'action' ); // Show an error if the prefix is invalid (only when adding one). // Invalid characters for a title should also be invalid for a prefix. // Whitespace, ':', '&' and '=' are invalid, too. // (Bug 30599). global $wgLegalTitleChars; $validPrefixChars = preg_replace( '/[ :&=]/', '', $wgLegalTitleChars ); if ( $do === 'add' && preg_match( "/\s|[^$validPrefixChars]/", $prefix ) ) { $status->fatal( 'interwiki-badprefix', htmlspecialchars( $prefix ) ); return $status; } // Disallow adding local interlanguage definitions if using global if ( $do === 'add' && Language::fetchLanguageName( $prefix ) && $wgInterwikiCentralInterlanguageDB !== wfWikiID() && $wgInterwikiCentralInterlanguageDB !== null ) { $status->fatal( 'interwiki-cannotaddlocallanguage', htmlspecialchars( $prefix ) ); return $status; } $reason = $data['reason']; $selfTitle = $this->getPageTitle(); $lookup = MediaWikiServices::getInstance()->getInterwikiLookup(); $dbw = wfGetDB( DB_MASTER ); switch ( $do ) { case 'delete': $dbw->delete( 'interwiki', [ 'iw_prefix' => $prefix ], __METHOD__ ); if ( $dbw->affectedRows() === 0 ) { $status->fatal( 'interwiki_delfailed', $prefix ); } else { $this->getOutput()->addWikiMsg( 'interwiki_deleted', $prefix ); $log = new LogPage( 'interwiki' ); $log->addEntry( 'iw_delete', $selfTitle, $reason, [ $prefix ], $this->getUser() ); $lookup->invalidateCache( $prefix ); } break; /** @noinspection PhpMissingBreakStatementInspection */ case 'add': $contLang = MediaWikiServices::getInstance()->getContentLanguage(); $prefix = $contLang->lc( $prefix ); case 'edit': $theurl = $data['url']; $local = $data['local'] ? 1 : 0; $trans = $data['trans'] ? 1 : 0; $rows = [ 'iw_prefix' => $prefix, 'iw_url' => $theurl, 'iw_local' => $local, 'iw_trans' => $trans ]; if ( $prefix === '' || $theurl === '' ) { $status->fatal( 'interwiki-submit-empty' ); break; } // Simple URL validation: check that the protocol is one of // the supported protocols for this wiki. // (bug 30600) if ( !wfParseUrl( $theurl ) ) { $status->fatal( 'interwiki-submit-invalidurl' ); break; } if ( $do === 'add' ) { $dbw->insert( 'interwiki', $rows, __METHOD__, [ 'IGNORE' ] ); } else { // $do === 'edit' $dbw->update( 'interwiki', $rows, [ 'iw_prefix' => $prefix ], __METHOD__, [ 'IGNORE' ] ); } // used here: interwiki_addfailed, interwiki_added, interwiki_edited if ( $dbw->affectedRows() === 0 ) { $status->fatal( "interwiki_{$do}failed", $prefix ); } else { $this->getOutput()->addWikiMsg( "interwiki_{$do}ed", $prefix ); $log = new LogPage( 'interwiki' ); $log->addEntry( 'iw_' . $do, $selfTitle, $reason, [ $prefix, $theurl, $trans, $local ], $this->getUser() ); // @phan-suppress-next-line PhanTypeMismatchArgumentNullable $lookup->invalidateCache( $prefix ); } break; } return $status; } protected function showList() { global $wgInterwikiCentralDB, $wgInterwikiCentralInterlanguageDB, $wgInterwikiViewOnly; $canModify = $this->canModify(); // Build lists $lookup = MediaWikiServices::getInstance()->getInterwikiLookup(); $iwPrefixes = $lookup->getAllPrefixes( null ); $iwGlobalPrefixes = []; $iwGlobalLanguagePrefixes = []; if ( $wgInterwikiCentralDB !== null && $wgInterwikiCentralDB !== wfWikiID() ) { // Fetch list from global table $dbrCentralDB = wfGetDB( DB_REPLICA, [], $wgInterwikiCentralDB ); $res = $dbrCentralDB->select( 'interwiki', '*', [], __METHOD__ ); $retval = []; foreach ( $res as $row ) { $row = (array)$row; if ( !Language::fetchLanguageName( $row['iw_prefix'] ) ) { $retval[] = $row; } } $iwGlobalPrefixes = $retval; } // Almost the same loop as above, but for global inter*language* links, whereas the above is for // global inter*wiki* links $usingGlobalInterlangLinks = ( $wgInterwikiCentralInterlanguageDB !== null ); $isGlobalInterlanguageDB = ( $wgInterwikiCentralInterlanguageDB === wfWikiID() ); $usingGlobalLanguages = $usingGlobalInterlangLinks && !$isGlobalInterlanguageDB; if ( $usingGlobalLanguages ) { // Fetch list from global table $dbrCentralLangDB = wfGetDB( DB_REPLICA, [], $wgInterwikiCentralInterlanguageDB ); $res = $dbrCentralLangDB->select( 'interwiki', '*', [], __METHOD__ ); $retval2 = []; foreach ( $res as $row ) { $row = (array)$row; // Note that the above DB query explicitly *excludes* interlang ones // (which makes sense), whereas here we _only_ care about interlang ones! if ( Language::fetchLanguageName( $row['iw_prefix'] ) ) { $retval2[] = $row; } } $iwGlobalLanguagePrefixes = $retval2; } // Split out language links $iwLocalPrefixes = []; $iwLanguagePrefixes = []; foreach ( $iwPrefixes as $iwPrefix ) { if ( Language::fetchLanguageName( $iwPrefix['iw_prefix'] ) ) { $iwLanguagePrefixes[] = $iwPrefix; } else { $iwLocalPrefixes[] = $iwPrefix; } } // If using global interlanguage links, just ditch the data coming from the // local table and overwrite it with the global data if ( $usingGlobalInterlangLinks ) { unset( $iwLanguagePrefixes ); $iwLanguagePrefixes = $iwGlobalLanguagePrefixes; } // Page intro content $this->getOutput()->addWikiMsg( 'interwiki_intro' ); // Add 'view log' link when possible if ( $wgInterwikiViewOnly === false ) { $logLink = $this->getLinkRenderer()->makeLink( SpecialPage::getTitleFor( 'Log', 'interwiki' ), $this->msg( 'interwiki-logtext' )->text() ); $this->getOutput()->addHTML( '
' . $logLink . '
' ); } // Add 'add' link if ( $canModify ) { if ( count( $iwGlobalPrefixes ) !== 0 ) { if ( $usingGlobalLanguages ) { $addtext = 'interwiki-addtext-local-nolang'; } else { $addtext = 'interwiki-addtext-local'; } } else { if ( $usingGlobalLanguages ) { $addtext = 'interwiki-addtext-nolang'; } else { $addtext = 'interwiki_addtext'; } } $addtext = $this->msg( $addtext )->text(); $addlink = $this->getLinkRenderer()->makeKnownLink( $this->getPageTitle( 'add' ), $addtext ); $this->getOutput()->addHTML( '' . $addlink . '
' ); } $this->getOutput()->addWikiMsg( 'interwiki-legend' ); if ( $iwPrefixes === [] && $iwGlobalPrefixes === [] ) { // If the interwiki table(s) are empty, display an error message $this->error( 'interwiki_error' ); return; } // Add the global table if ( count( $iwGlobalPrefixes ) !== 0 ) { $this->getOutput()->addHTML( '$1
", $args ); } protected function getGroupName() { return 'wiki'; } }