Extension:LockTitle

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
LockTitle

Release status: unknown

Implementation  Special page, Database, User rights
Description Enables locking of certain titles to prohibit unprivileged users to create said page
Author(s)  Carl Fürstenberg
Last Version  0.99.0 (2006-12-09)
MediaWiki  1.8+
License No license specified
Download N/A

check usage (experimental)

Enables locking of certain titles to prohibit unprivileged users to create said page. The locking is per page basis and utilizing a table in the database to store the pages. A simple special page is available to create or remove locks.

Contents

[edit] License

Copyright © 2006 Carl Fürstenberg

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

[edit] Author

Carl Fürstenberg (AzaToth) <azatoth@gmail.com>

[edit] Pre-requisites

This extension has only yet been tested with 1.9.2 SVN trunk

[edit] Installation

[edit] Files to install

Place these files in extension/LockTitle

[edit] install.php

<?php
 
/**
 * Installation script for the bad image list extension
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author Rob Church <robchur@gmail.com> modified to fit by Carl Fürstenberg (AzaToth) <azatoth@gmail.com>
 * @copyright © 2006 Rob Church
 * @licence Copyright holder allows use of the code for any purpose
 */
 
require_once( '../../maintenance/commandLine.inc' );
 
$sql = dirname( __FILE__ ) . '/LockTitle.sql';
$check_for_table = 'locktitle';
 
$dba = & wfGetDB( DB_MASTER );
 
# Check we're connected
if( !$dba->isOpen() ) {
	echo( "A connection to the database could not be established.\n\n" );
	die( 1 );
}
 
# Do nothing if the table exists
if( !$dba->tableExists( $dba->tableName( $check_for_table ) ) ) {
	$res = $dba->sourceFile( $sql );
	if( gettype($res) === boolean && $res === true ) {
		echo( "The table has been set up correctly.\n" );
	} else {
		echo( "$res\n" );
 
	}
 
} else {
	echo( "The table already exists. No action was taken.\n" );
}
 
# Close the connection
$dba->close();
echo( "\n" );
 
?>

[edit] LockTitle.i18n.php

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	echo "This file is part of MediaWiki, it is not a valid entry point.\n";
	exit( 1 );
}
 
$wgLockTitleMessages = array();
 
$wgLockTitleMessages['en'] = array(
	'locktitle_no_table' => "Couldn't find the locktitle table, please ensure it's installed",
);
?>

[edit] LockTitle.php

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	echo "This file is part of MediaWiki, it is not a valid entry point.\n";
	exit( 1 );
}
 
/**
 * A system for locking down titles from creation.
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @link http://www.mediawiki.org/wiki/Extension:LockTitle Documentation
 *
 * @author Carl Fürstenberg (AzaToth) <azatoth@gmail.com>
 * @copyright Copyright © 2006 Carl Fürstenberg
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */
 
// Some new permissions
$wgGroupPermissions['sysop']['locktitle']		= true;
$wgGroupPermissions['bureaucrat']['locktitle']	= true;
$wgGroupPermissions['steward']['locktitle']		= true;
$wgGroupPermissions['developer']['locktitle']	= true;
$wgAvailableRights[] = 'locktitle';
 
$wgExtensionFunctions[] = 'wfLockTitleSetup';
$wgExtensionCredits['other'][] = array(
	'name' => 'LockTitle',
	'author' => 'Carl Fürstenberg (AzaToth)',
	'description' => 'Locks titles from creation by unpriviledged users',
	'url' => 'http://www.mediawiki.org/wiki/Extension:LockTitle',
	'version' => '0.99.0',
);
$wgHooks['userCan'][] = 'wfLockTitle';
 
require_once( 'LockTitle.i18n.php');
 
function wfLockTitleSetup() {
	global $wgLockTitleLoaded, $wgLockTitleMessages, $wgMessageCache;
	foreach( $wgLockTitleMessages as $key => $value ) {
		$wgMessageCache->addMessages( $value, $key );
	}
	$dbw =& wfGetDB( DB_MASTER );
 
	// We might not have the table in the database
 
	if(!$dbw->tableExists('locktitle')){
		throw new FatalError(wfMsg('locktitle_no_table'));
	}
}
 
function wfLockTitle( $title , $user , $action , &$result ) {
	if( $action != 'create' ) {
		// We only are checking for creation
		return;
	}
 
	$key = $title->getPrefixedDBkey();
	$dbw =& wfGetDB( DB_MASTER );
 
	$field = $dbw->selectField( 
		'locktitle' , 
		'`key`' , 
		array( 
			'`key`' => $key
		)
	);
 
	if( $field === false ){
		// no entry found
		return;
	} else {
		// "title" is locked down
 
		if( $user->isAllowed( 'locktitle' ) ) {
			// some users may bypass the locktitle
			return;
		}
 
		$result = false;
		return;
	}
 
}
?>

[edit] LockTitle.sql

CREATE TABLE /*$wgDBprefix*/locktitle (
  `key` varchar(255) BINARY NOT NULL,
  PRIMARY KEY  (`key`)
) ENGINE=InnoDB;

[edit] SpecialLockTitle_body.php

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	echo "This file is part of MediaWiki, it is not a valid entry point.\n";
	exit( 1 );
}
 
class SpecialLockTitle_body extends SpecialPage {
 
	public function __construct() {
		global $wgUser;
		parent :: __construct( 'LockTitle', 'locktitle' );
	}
 
	function execute() {
		$this->setHeaders();
		global $wgUser, $wgRequest, $wgOut;
 
		if( ! $wgUser->isAllowed( 'locktitle' ) ) {
			$wgOut->permissionRequired('locktitle');
		} else {
			$wgOut->addWikiText(wfMsg('locktitle_init_text'));
 
			if( $wgRequest->getCheck( 'locktitle' ) && $wgRequest->wasPosted() ) {
				$dbw =& wfGetDB( DB_MASTER );
 
				$act = $wgRequest->getVal( 'act' );
				$target_title = $wgRequest->getVal( 'target_title' );
 
				if( $target_title ) {
 
					$target_title = Title::newFromText( $target_title );
					$key = $target_title->getPrefixedDBkey();
 
 
					if( $act == 'add' ) {
						$dbw->insert(
							'locktitle',
							array(
								'`key`' => $key,
							),
							'SpecialLockTitle::execute',
							array( 'IGNORE' )
						);
 
						$wgOut->addHTML( wfElement( 'div' , null , $target_title->getPrefixedText() . ' is now locked' ) );
 
					} else if ( $act == 'del' ) {
						$dbw->delete(
							'locktitle',
							array(
								'`key`' => $key,
							),
							'SpecialLockTitle::execute',
							array( 'IGNORE' )
						);
 
						$wgOut->addHTML( wfElement( 'div' , null , $target_title->getPrefixedText() . ' is now unlocked' ) );
					}
 
				}
			} 
			$self = $this->getTitle();
			$form = wfOpenElement( 'form', array( 'method' => 'post' , 'action' => $self->getLocalUrl() ) ) .
				wfOpenElement( 'table' ).
				wfOpenElement( 'tr' ).
				wfOpenElement( 'td' ).
				wfLabel( wfMsg('locktitle_add') , 'add' ).
				wfCloseElement( 'td' ).
				wfOpenElement( 'td' ).
				wfRadio( 'act' , 'add' , true , array ( 'id' => 'add' ) ).
				wfCloseElement( 'td' ).
				wfCloseElement( 'tr' ).
				wfOpenElement( 'tr' ).
				wfOpenElement( 'td' ).
				wfLabel( wfMsg('locktitle_del') , 'del' ).
				wfCloseElement( 'td' ).
				wfOpenElement( 'td' ).
				wfRadio( 'act' , 'del' , false , array ( 'id' => 'del' ) ).
				wfCloseElement( 'td' ).
				wfCloseElement( 'tr' ).
				wfOpenElement( 'tr' ).
				wfOpenElement( 'td' ).
				wfLabel( wfMsg('locktitle_title') , 'title' ).
				wfCloseElement( 'td' ).
				wfOpenElement( 'td' ).
				wfInput( 'target_title' , 40, '' , array( 'type' => 'text' ) ).
				wfCloseElement( 'td' ).
				wfCloseElement( 'tr' ).
				wfOpenElement( 'tr' ).
				wfOpenElement( 'td' , array( 'colspan' => 2 ) ).
				wfSubmitButton( 'locktitle' , array( 'name' => 'locktitle' ) ).
				wfCloseElement( 'td' ).
				wfCloseElement( 'tr' ).
				wfCloseElement( 'table' ).
				wfCloseElement( 'form' );
 
 
			$wgOut->addHTML( $form );
		}
	}
}
 
?>

[edit] SpecialLockTitle.i18n.php

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	echo "This file is part of MediaWiki, it is not a valid entry point.\n";
	exit( 1 );
}
 
$wgSpecialLockTitleMessages = array();
 
$wgSpecialLockTitleMessages['en'] = array(
	'locktitle_init_text' => 'Locks down a title from the ability for creation by non-priviledged users.',
	'locktitle' => 'Lock titles',
	'locktitle_add' => 'Add lock',
	'locktitle_del' => 'Remove lock',
	'locktitle_title' => 'Title',
 
);
?>

[edit] SpecialLockTitle.php

<?php
 
if ( !defined( 'MEDIAWIKI' ) ) {
	echo "This file is part of MediaWiki, it is not a valid entry point.\n";
	exit( 1 );
}
 
/**
 * A system for locking down titles from creation. A special page to handle lock downs
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @link http://www.mediawiki.org/wiki/Extension:LockTitle Documentation
 *
 * @author Carl Fürstenberg (AzaToth) <azatoth@gmail.com>
 * @copyright Copyright © 2006 Carl Fürstenberg
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */
 
$wgExtensionFunctions[] = 'wfSpecialLockTitle';
$wgExtensionCredits['specialpage'][] = array(
	'name' => 'LockTitle',
	'author' => 'Carl Fürstenberg (AzaToth)',
	'description' => 'Interface to add and remove locks',
	'url' => 'http://www.mediawiki.org/wiki/Extension:LockTitle',
	'version' => '0.99.0',	
);
 
require_once( 'SpecialLockTitle.i18n.php');
 
$wgAutoloadClasses['SpecialLockTitle_body'] = dirname( __FILE__ ) . '/SpecialLockTitle_body.php';
$wgSpecialPages['LockTitle'] = 'SpecialLockTitle_body';
 
function wfSpecialLockTitle() {
	global $wgSpecialLockTitleMessages, $wgMessageCache, $wgAvailableRights;
 
	foreach( $wgSpecialLockTitleMessages as $key => $value ) {
		$wgMessageCache->addMessages( $value, $key );
	}
}
?>

[edit] Instructions

Run this command in the top directory (to create the table needed):

extensions/LockTitles/install.php

In your MediaWiki LocalSettings.php, add the following line some place towards the bottom of the file:

require_once("$IP/extensions/LockTitles/LockTitles.php");
require_once("$IP/extensions/LockTitles/SpecialLockTitles.php");

[edit] Configuration

Priviledged users have acess to the page Special:LockTitle where adding and removing of locks can be done.