Extension:SpecialNamespaces/sourcecode (ver 0.1)

<?php /* * This is an extension to edit the list of namespaces, and is based on the * Special:Interwiki code originally by Stephanie Amanda Stevens. This is * not an officially-supported version of this code. * * 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 Based on Special:Interwiki, by Stephanie Amanda Stevens  * @copyright Copyright (C) 2005 Stephanie Amanda Stevens * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */

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

$wgExtensionCredits['other'][] = array(       'name' => 'Special:Namespaces',        'description' => 'adds a special page to view and manipulate the namespaces table.' );

function Namespaces { global $IP, $wgMessageCache, $wgHooks; require_once( "$IP/includes/SpecialPage.php" );

$wgMessageCache->addMessage('namespaces', 'View and manipulate namespaces data'); $wgMessageCache->addMessage('ns_delquestion', 'Deleting "$1"'); $wgMessageCache->addMessage('ns_deleted', '$1 was successfully removed from the namespaces table.'); $wgMessageCache->addMessage('ns_id', 'ID'); $wgMessageCache->addMessage('ns_id2', 'ID:'); $wgMessageCache->addMessage('ns_name', 'Name'); $wgMessageCache->addMessage('ns_default', 'Default'); $wgMessageCache->addMessage('ns_canonical', 'Canonical'); $wgMessageCache->addMessage('ns_delete', 'Delete'); $wgMessageCache->addMessage('ns_yes', 'Yes'); $wgMessageCache->addMessage('ns_delfailed', '$1 could not be removed from the namespaces table.'); $wgMessageCache->addMessage('ns_added', '$1 was successfully added to the namespaces table.'); $wgMessageCache->addMessage('ns_addfailed', '$1 could not be added to the namespaces table.'); $wgMessageCache->addMessage('ns_log_added', 'Added "$1" ($2) (local: $3) (trans: $4) to the namespaces table: $5'); $wgMessageCache->addMessage('ns_alreadyexists', '$1 already exists in the namespace table!'); $wgMessageCache->addMessage('ns_log_deleted', 'Removed prefix "$1" from the namespace table: $2'); $wgMessageCache->addMessage('ns_add', '*Add a namespace prefix'); $wgMessageCache->addMessage('ns_show', '*View all namespace prefixes'); $wgMessageCache->addMessage('ns_logpagename', 'Namespace table log'); $wgMessageCache->addMessage('ns_logpagetext', 'This is a log of changes to the namespace table.'); $wgMessageCache->addMessage('ns_logentry', ''); $wgMessageCache->addMessage('ns_reasonfield', 'Reason'); $wgMessageCache->addMessage('ns_addtext', 'Add a namespace prefix'); $wgMessageCache->addMessage('ns_delbutton', 'Delete'); $wgMessageCache->addMessage('ns_addbutton', 'Add');

# Add a new log type $wgHooks['LogPageValidTypes'][] = 'wfNamespacesAddLogType'; $wgHooks['LogPageLogName'][] = 'wfNamespacesAddLogName'; $wgHooks['LogPageLogHeader'][] = 'wfNamespacesAddLogHeader'; $wgHooks['LogPageActionText'][] = 'wfNamespacesAddActionText'; $wgHooks['LanguageNamespaces'][] = 'wfNamespaces';

class Namespaces extends SpecialPage { function Namespaces { SpecialPage::SpecialPage( 'Namespaces' ); $this->includable( true ); }

function discard {                       global $wgDBname, $wgMemc; $wgMemc->delete ("$wgDBname:SpecialNamespaces:names"); $wgMemc->delete ("$wgDBname:SpecialNamespaces:alias"); }

function execute( $par = null ) { $fname = 'Namespaces::execute'; global $wgOut, $wgRequest, $wgUser;

$this->setHeaders; $wgOut->setPagetitle( wfMsg( 'namespaces' ) ); $do = $wgRequest->getVal( 'do' );

// Some common checks $admin = $wgUser->isAllowed( 'delete' ); $selfTitle = Title::makeTitle( NS_SPECIAL, 'Namespaces' );

// Protect administrative actions against malicious requests $safePost = $wgRequest->wasPosted && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );

if ($do == "delete") { if (!$admin) { $wgOut->permissionRequired('namespaces'); return; }

$prefix = $wgRequest->getVal( 'prefix' ); $encPrefix = htmlspecialchars( $prefix );

$action = $selfTitle->escapeLocalURL( "do=delete2" ); $button = wfMsgHtml("ns_delbutton"); $question = htmlspecialchars( wfMsg('ns_delquestion', $prefix) ); $reasonmessage = wfMsgHtml('ns_reasonfield'); $token = htmlspecialchars( $wgUser->editToken );

$out = $question; $out .= "                                                                               $reasonmessage                                                                                                                         \n"; } elseif ($do == "delete2" && $safePost) { if (!$admin) { $wgOut->permissionRequired('namespaces'); return; } $prefix = $wgRequest->getVal('prefix'); $reason = $wgRequest->getText('reason'); $deletedmessage = htmlspecialchars( wfMsg('ns_deleted', $prefix) ); $delfailedmessage = htmlspecialchars( wfMsg('ns_delfailed', $prefix) );

$dbw =& wfGetDB( DB_MASTER ); $dbw->delete( 'namespace_names', array( 'ns_id' => $prefix ), $fname );

if ($dbw->affectedRows == 0) { $out = $delfailedmessage; } else { $out = $deletedmessage; $log = new LogPage( 'namespaces' ); $log->addEntry( 'namespaces', $selfTitle, wfMsgForContent( 'ns_log_deleted', $prefix, $reason ) ); }                               discard; } elseif ($do == "add") { if (!$admin) { $wgOut->permissionRequired('namespaces'); return; }; $action = $selfTitle->escapeLocalURL( "do=add2"); $prefixmessage = wfMsgHtml('ns_id'); $transmessage = wfMsgHtml('ns_canonical'); $localmessage = wfMsgHtml('ns_default'); $reasonmessage = wfMsgHtml('ns_reasonfield'); $urlmessage = wfMsgHtml('ns_name'); $button = wfMsgHtml('ns_addbutton'); $token = htmlspecialchars( $wgUser->editToken );

$out = "                                       $prefixmessage 

$localmessage 

$transmessage 

$urlmessage 

$reasonmessage 

  \n";                       } elseif ($do == "add2" && $safePost) {                                if (!$admin) { $wgOut->permissionRequired('namespaces'); 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('ns_added', $prefix) ); $addfailedmessage = htmlspecialchars( wfMsg('ns_addfailed', $prefix) ); $alreadyexists = htmlspecialchars( wfMsg('ns_alreadyexists', $prefix) );

$dbw =& wfGetDB( DB_MASTER ); $dbw->insert( 'namespace_names',                                       array( 'ns_id' => $prefix, 'ns_name'   => $theurl, 'ns_default' => $local, 'ns_canonical' => $trans ),                                        $fname,                                        'IGNORE' );

if( $dbw->affectedRows == 0 ) { $out = $alreadyexists; } else { $out = $addedmessage; $log = new LogPage( 'namespaces' ); $log->addEntry( 'namespaces', $selfTitle, wfMsgForContent( 'ns_log_added', $prefix, $theurl, $trans, $local, $reason ) ); }                               discard; } else { $dbr =& wfGetDB( DB_SLAVE ); $res = $dbr->select( 'namespace_names', '*' );

$prefixmessage = wfMsgHtml('ns_id'); $urlmessage = wfMsgHtml('ns_name'); $localmessage = wfMsgHtml('ns_default'); $transmessage = wfMsgHtml('ns_canonical'); $deletemessage = wfMsgHtml('ns_delete'); $addtext = wfMsgHtml('ns_addtext');

if ($admin) { $skin = $wgUser->getSkin; $out = $skin->makeLinkObj( $selfTitle, $addtext, 'do=add' ); } else { $out = ''; }                               $out .= "

";                       }                        $wgOut->addHTML($out);                }        }

SpecialPage::addPage( new Namespaces ); }

function wfNamespaces(&$namespaceNames,&$namespaceAliases) {       global $wgDBname,$wgMemc; $key = "$wgDBname:SpecialNamespaces:names"; $cached = $wgMemc->get ($key);
 * 1) Insert translated custom namespace name/aliases from namespace_names database
 * 2) destination format is:
 * 3)     $this->namespaceNames[100]="ns_100";
 * 4)     $this->namespaceAliases =  array("Centurion" => 100);

if (!is_array($cached)) {               $dbr =& wfGetDB( DB_SLAVE ); $res = $dbr->select( 'namespace_names', '*' ); $numrows = $dbr->numRows( $res ); if ($numrows > 0) while( $s = $dbr->fetchObject( $res ) ) { $nsindex = htmlspecialchars( $s->ns_id ); $nsname = htmlspecialchars( $s->ns_name); $nscanonical = htmlspecialchars( $s->ns_canonical ); $nsdefault = htmlspecialchars( $s->ns_default );

if ($nsdefault>0) $namespaceNames[$nsindex] = $nsname; $namespaceAliases = $namespaceAliases + array($nsname => $nsindex); }               $dbr->freeResult( $res ); $wgMemc->set ($key, $namespaceNames); $key = "$wgDBname:SpecialNamespaces:alias"; $wgMemc->set ($key, $namespaceAliases); } else { $namespaceNames = $cached; $key = "$wgDBname:SpecialNamespaces:alias"; $namespaceAliases = $wgMemc->get ($key); } }

function wfNamespacesAddLogType( &$types ) { if ( !in_array( 'namespaces', $types ) ) $types[] = 'namespaces'; return true; }

function wfNamespacesAddLogName( &$names ) { $names['namespaces'] = 'ns_logpagename'; return true; }

function wfNamespacesAddLogHeader( &$headers ) { $headers['namespaces'] = 'ns_logpagetext'; return true; }

function wfNamespacesAddActionText( &$actions ) { $actions['namespaces/namespaces'] = 'ns_logentry'; return true; }

?>