MediaWiki/archive/extensions/Special/Interwiki

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search

Overview

This is a modified version of Extension:SpecialInterwiki by Stephanie Amanda Stevens. It adds the following features:

  • Interwiki URLs are shown as links, so you can quickly check validity (and answer the inevitable "what the heck is that wiki?" question more easily)
  • Compatible with MediaWiki 1.14 (there was a class name conflict)
  • Version data has link to this page (original extension had no URL)
  • Description changed to shorter "Interwiki alias management", so it will be where you expect it in the list (under "I" instead of under "V")

Code

<php><?php /*

* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* @author Stephanie Amanda Stevens <phroziac@gmail.com>
* @copyright Copyright (C) 2005 Stephanie Amanda Stevens
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*/

/*

HISTORY:
 2009-02-23 - v1.2 - Patched to work with MW 1.14; added version, url
 2009-03-10 - v1.2a - minor tweak to function execute() definition so it doesn't generate a warning
  • /

if (!defined('MEDIAWIKI')) die(); $wgExtensionFunctions[] = "Interwiki";

$wgExtensionCredits['other'][] = array( 'name' => 'Special:Interwiki', 'description' => 'adds a special page to view and manipulate the interwiki table.', 'version' => '1.2a', 'url' => 'http://htyp.org/MediaWiki/Special/Interwiki', 'author' => 'Stephanie Amanda Stevens' );

function Interwiki() {

       global $IP, $wgMessageCache, $wgHooks;
       require_once( "$IP/includes/SpecialPage.php" );
       $wgMessageCache->addMessage('interwiki', 'Interwiki alias management');
       $wgMessageCache->addMessage('interwiki_delquestion', 'Deleting "$1"');
       $wgMessageCache->addMessage('interwiki_deleted', '$1 was successfully removed from the interwiki table.');
       $wgMessageCache->addMessage('interwiki_prefix', 'Prefix');
       $wgMessageCache->addMessage('interwiki_prefix2', 'Prefix:');
       $wgMessageCache->addMessage('interwiki_url', 'URL');
       $wgMessageCache->addMessage('interwiki_local', 'Local');
       $wgMessageCache->addMessage('interwiki_trans', 'Trans');
       $wgMessageCache->addMessage('interwiki_delete', 'Delete');
       $wgMessageCache->addMessage('interwiki_yes', 'Yes');
       $wgMessageCache->addMessage('interwiki_delfailed', '$1 could not be removed from the interwiki table.');
       $wgMessageCache->addMessage('interwiki_added', '$1 was successfully added to the interwiki table.');
       $wgMessageCache->addMessage('interwiki_addfailed', '$1 could not be added to the interwiki table.');
       $wgMessageCache->addMessage('interwiki_log_added', 'Added "$1" ($2) (local: $3) (trans: $4) to the interwiki table: $5');
       $wgMessageCache->addMessage('interwiki_alreadyexists', '$1 already exists in the interwiki table!');
       $wgMessageCache->addMessage('interwiki_log_deleted', 'Removed prefix "$1" from the interwiki table: $2');
       $wgMessageCache->addMessage('interwiki_add', '*Add an interwiki prefix');
       $wgMessageCache->addMessage('interwiki_show', '*View all interwiki prefixes');
       $wgMessageCache->addMessage('interwiki_logpagename', 'Interwiki table log');
       $wgMessageCache->addMessage('interwiki_logpagetext', 'This is a log of changes to the interwiki table.');
       $wgMessageCache->addMessage('interwiki_logentry', );
       $wgMessageCache->addMessage('interwiki_reasonfield', 'Reason');
       $wgMessageCache->addMessage('interwiki_addtext', 'Add an interwiki prefix');
       $wgMessageCache->addMessage('interwiki_delbutton', 'Delete');
       $wgMessageCache->addMessage('interwiki_addbutton', 'Add');
       # Add a new log type
       $wgHooks['LogPageValidTypes'][] = 'wfInterwikiAddLogType';
       $wgHooks['LogPageLogName'][] = 'wfInterwikiAddLogName';
       $wgHooks['LogPageLogHeader'][] = 'wfInterwikiAddLogHeader';
       $wgHooks['LogPageActionText'][] = 'wfInterwikiAddActionText';
       class SpecialInterwiki extends SpecialPage {

function __construct() {

                       SpecialPage::SpecialPage( 'Interwiki' );
                       $this->includable( true );
               }

function execute( $par ) {

                       $fname = 'SpecialInterwiki::execute';
                       global $wgOut, $wgRequest, $wgUser;
                       $this->setHeaders();
                       $wgOut->setPagetitle( wfMsg( 'interwiki' ) );
                       $do = $wgRequest->getVal( 'do' );
                       // Some common checks
                       $admin = $wgUser->isAllowed( 'interwiki' );
                       $selfTitle = Title::makeTitle( NS_SPECIAL, 'Interwiki' );
                       // Protect administrative actions against malicious requests
                       $safePost = $wgRequest->wasPosted() &&
                               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
                       if ($do == "delete") {
                               if (!$admin) { $wgOut->permissionRequired('interwiki'); return; }
                               $prefix = $wgRequest->getVal( 'prefix' );
                               $encPrefix = htmlspecialchars( $prefix );
                               $action = $selfTitle->escapeLocalURL( "do=delete2" );
                               $button = wfMsgHtml("interwiki_delbutton");
                               $question = htmlspecialchars( wfMsg('interwiki_delquestion', $prefix) );
                               $reasonmessage = wfMsgHtml('interwiki_reasonfield');
                               $token = htmlspecialchars( $wgUser->editToken() );
                               $out = $question;
                               $out .= "<form id=\"delete\" method=\"post\" action=\"{$action}\">
                                       <input type=\"hidden\" name=\"prefix\" value=\"{$encPrefix}\" />
                                       $reasonmessage <input tabindex='1' type='text' name=\"reason\" maxlength='200' size='60' />
                                       <input type=\"submit\" name=\"delete\" value=\"{$button}\" />
                                       <input type=\"hidden\" name=\"wpEditToken\" value=\"{$token}\"/>
                                       </form>\n";
                       } elseif ($do == "delete2" && $safePost) {
                               if (!$admin) { $wgOut->permissionRequired('interwiki'); return; }
                               $prefix = $wgRequest->getVal('prefix');
                               $reason = $wgRequest->getText('reason');
                               $deletedmessage = htmlspecialchars( wfMsg('interwiki_deleted', $prefix) );
                               $delfailedmessage = htmlspecialchars( wfMsg('interwiki_delfailed', $prefix) );
                               $dbw =& wfGetDB( DB_MASTER );
                               $dbw->delete( 'interwiki', array( 'iw_prefix' => $prefix ), $fname );
                               if ($dbw->affectedRows() == 0) {
                                       $out = $delfailedmessage;
                               } else {
                                       $out = $deletedmessage;
                                       $log = new LogPage( 'interwiki' );
                                       $log->addEntry( 'interwiki', $selfTitle, wfMsgForContent( 'interwiki_log_deleted', $prefix, $reason ) );
                               }
                       } elseif ($do == "add") {
                               if (!$admin) { $wgOut->permissionRequired('interwiki'); return; };
                               $action = $selfTitle->escapeLocalURL( "do=add2");
                               $prefixmessage = wfMsgHtml('interwiki_prefix');
                               $transmessage = wfMsgHtml('interwiki_trans');
                               $localmessage = wfMsgHtml('interwiki_local');
                               $reasonmessage = wfMsgHtml('interwiki_reasonfield');
                               $urlmessage = wfMsgHtml('interwiki_url');
                               $button = wfMsgHtml('interwiki_addbutton');
                               $token = htmlspecialchars( $wgUser->editToken() );
                               $out = "<form id=\"add\" method=\"post\" action=\"{$action}\">
                                       $prefixmessage <input tabindex='1' type='text' name=\"prefix\" maxlength='20' size='20' />
$localmessage <input type=\"checkbox\" id=\"local\" name=\"local\" />
$transmessage <input type=\"checkbox\" id=\"trans\" name=\"trans\" />
                                       $urlmessage <input tabindex='1' type='text' name=\"theurl\" maxlength='200' size='60' />
                                       $reasonmessage <input tabindex='1' type='text' name=\"reason\" maxlength='200' size='60' />
                                       <input type=\"submit\" name=\"add\" value=\"{$button}\" />
                                       <input type=\"hidden\" name=\"wpEditToken\" value=\"{$token}\" />
                                       </form>\n";
                       } elseif ($do == "add2" && $safePost) {
                               if (!$admin) { $wgOut->permissionRequired('interwiki'); return; }
                               $prefix = $wgRequest->getVal('prefix');
                               $reason = $wgRequest->getText('reason');
                               $theurl = $wgRequest->getVal('theurl');
                               $local = $wgRequest->getCheck('local') ? 1 : 0;
                               $trans = $wgRequest->getCheck('trans') ? 1 : 0;
                               $addedmessage = htmlspecialchars( wfMsg('interwiki_added', $prefix) );
                               $addfailedmessage = htmlspecialchars( wfMsg('interwiki_addfailed', $prefix) );
                               $alreadyexists = htmlspecialchars( wfMsg('interwiki_alreadyexists', $prefix) );
                               $dbw =& wfGetDB( DB_MASTER );
                               $dbw->insert( 'interwiki',
                                       array(
                                               'iw_prefix' => $prefix,
                                               'iw_url'    => $theurl,
                                               'iw_local'  => $local,
                                               'iw_trans'  => $trans ),
                                       $fname,
                                       'IGNORE' );
                               if( $dbw->affectedRows() == 0 ) {
                                       $out = $alreadyexists;
                               } else {
                                       $out = $addedmessage;
                                       $log = new LogPage( 'interwiki' );
                                       $log->addEntry( 'interwiki', $selfTitle, wfMsgForContent( 'interwiki_log_added', $prefix, $theurl, $trans, $local, $reason ) );
                               }
                       } else {
                               $dbr =& wfGetDB( DB_SLAVE );
                               $res = $dbr->select( 'interwiki', '*' );
                               $prefixmessage = wfMsgHtml('interwiki_prefix');
                               $urlmessage = wfMsgHtml('interwiki_url');
                               $localmessage = wfMsgHtml('interwiki_local');
                               $transmessage = wfMsgHtml('interwiki_trans');
                               $deletemessage = wfMsgHtml('interwiki_delete');
                               $addtext = wfMsgHtml('interwiki_addtext');
                               if ($admin) {
                                       $skin = $wgUser->getSkin();
                                       $out = $skin->makeLinkObj( $selfTitle, $addtext, 'do=add' );
                               } else {
                                       $out = ;
                               }
                               $out .= "
                               
";
                               if( $admin ) {
$out .= "";
                               }
$out .= "\n"; $numrows = $dbr->numRows( $res ); if ($numrows == 0) { $out .= "";
                               }
                               while( $s = $dbr->fetchObject( $res ) ) {
                                       $prefix = htmlspecialchars( $s->iw_prefix );
                                       $url = htmlspecialchars( $s->iw_url );

$url_generic = str_replace("$1","",$url);

                                       $trans = htmlspecialchars( $s->iw_trans );
                                       $local = htmlspecialchars( $s->iw_local );
$out .= "";
                                       if( $admin ) {
                                               $skin = $wgUser->getSkin();
$out .= ''; } $out .= "\n"; } $dbr->freeResult( $res ); $out .= "
$prefixmessage $urlmessage $localmessage $transmessage$deletemessage
ERROR: The interwiki table is empty, or something else went wrong.
$prefix <a href=".$url_generic.">$url</a> $local $trans';
                                               $out .= $skin->makeLinkObj( $selfTitle, $deletemessage,
                                                       'do=delete&prefix=' . urlencode( $s->iw_prefix ) );
$out .= '


";

                       }
                       $wgOut->addHTML($out);
               }
       }
       SpecialPage::addPage( new SpecialInterwiki );

}

function wfInterwikiAddLogType( &$types ) {

       if ( !in_array( 'interwiki', $types ) )
               $types[] = 'interwiki';
       return true;

}

function wfInterwikiAddLogName( &$names ) {

       $names['interwiki'] = 'interwiki_logpagename';
       return true;

}

function wfInterwikiAddLogHeader( &$headers ) {

       $headers['interwiki'] = 'interwiki_logpagetext';
       return true;

}

function wfInterwikiAddActionText( &$actions ) {

       $actions['interwiki/interwiki'] = 'interwiki_logentry';
       return true;

}</php>