Extension:DeletePagePermanently

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
DeletePagePermanently

Release status: beta

Implementation User interface
Description Delete a page permanently from the database
Author(s) Wolfgang Stöttinger, Ludovic MOUTON (LeLorrainTalk)
Last Version 2.0
MediaWiki 1.13+
License GPL
Download This page

This extension can delete pages in every namespace configured (also images) without leaving any trace in the database.

I improved the original Version of Ludovic MOUTON. The original Version can be found at Extension:DeletePagePermanently/1.0

The extension DeletePagePermanently adds a new tab that allows you to delete a page permanently from the database. Although version 2.0 is safer than version 1.0 it can still harm your wiki, especially when used wrong. I advise you to test the extension carefully and backup your database as the extension is still in beta status.


Contents


[edit] Installation

The extension was designed for MediaWiki 1.13 but it should be compatible with later version as long as the database layout is the same as MediaWiki 1.13.0 for the tables.

[edit] Download

Please cut and paste the code found below and place it in $IP/extensions/DeletePagePermanently/DeletePagePermanently.php and DeletePagePermanently.i18n.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Install

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/DeletePagePermanently/DeletePagePermanently.php");

[edit] Configuration

[edit] User rights

The extension introduces a new user rights deleteperm. You can configure the rights in LocalSettings.php:

Defaults from DeletePagePermanently.php:

$wgGroupPermissions['*']         ['deleteperm'] = false;
$wgGroupPermissions['user']      ['deleteperm'] = false;
$wgGroupPermissions['bureaucrat']['deleteperm'] = false;
$wgGroupPermissions['sysop']     ['deleteperm'] = true;

[edit] Namespaces

New in version 2.0 is the possibility to configure the namespaces in which pages can be deleted. To configure this, add some lines to your LocalSettings.php:

$wgDeletePagePermanentlyNamespaces = array(
	NS_MAIN => true,
	NS_IMAGE => true,
	NS_CATEGORY => true,
	NS_TEMPLATE => true,
	NS_TALK => true,
);

[edit] Code

<?php
/**
 * DeletePagePermanently.php
 * based on Version 1.0 BETA from Ludovic MOUTON (2008)
 * Wolfgang Stöttinger 2008
 * v. 2.0 BETA
 * GPL
 * 
 * BE CAREFUL WHEN USING THIS EXTENSION. ONCE A PAGE IS DELETED, IT CAN NOT BE RESTORED ANY MORE.
 * 
 * Features of Version 2.0:
 *		- defined $wgDeletePagePermanentlyNamespaces as an array of namespaces in which pages can be deleted.
 * 			$wgDeletePagePermanentlyNamespaces = array(
 * 				NS_MAIN => true,
 *				NS_IMAGE => true,
 * 				NS_CATEGORY => true,
 * 				NS_TEMPLATE => true,
 * 				NS_TALK => true,
 * 			);
 *		
 *		- The tab "Delete permanently" is only shown on pages that can be deleted.
 * 		- An aditional page to approve the deletion
 *		- Also log entries are deleted.
 *		- Also redirects, external links, language links, searchindex entries, page_restrictions, pagelinks, categorylinks, templatelinks, archive entries and image links are deleted.
 * 		- The watchlist entries are also removed.
 *		- If the page is an image page, all versions of the image are deleted from the database, the archive and the filesystem. (Thumbnails won't be removed)
*/
 
if ( !defined( 'MEDIAWIKI' ) ) {
	exit( 1 );
}
 
$wgExtensionCredits['other'][] = array(
	'name' => 'DeletePagePermanently',
	'version' => '2.0',
	'author' => array('Ludovic MOUTON', 'Wolfgang ST&OUML;TTINGER'),
	'desciprion' => 'Adds a new delete tab to each page. Pages are deleted permanently from the database.',
	'url' => 'http://www.mediawiki.org/wiki/Extension:DeletePagePermanently',
);
 
# Default settings
$wgGroupPermissions['*']         ['deleteperm'] = false;
$wgGroupPermissions['user']      ['deleteperm'] = false;
$wgGroupPermissions['bureaucrat']['deleteperm'] = false;
$wgGroupPermissions['sysop']     ['deleteperm'] = true;
 
$wgExtensionMessagesFiles['DeletePagePermanently'] = dirname( __FILE__ ) . '/DeletePagePermanently.i18n.php';
 
$wgExtensionFunctions[] = 'wfDeletePagePermanently';
 
function wfDeletePagePermanently(){
	new wfDeletePagePermanently;
	return true;
}
 
class wfDeletePagePermanently {
	function wfDeletePagePermanently(){
		global $wgHooks, $wgMessageCache, $wgUser;
 
		wfLoadExtensionMessages( 'DeletePagePermanently' );
 
		$wgMessageCache->addMessage( 'delete_permanently', wfMsg('tab_label') );
		$wgHooks['SkinTemplateContentActions'][] = array(&$this, 'addContentHook');
		$wgHooks['UnknownAction'][] = array(&$this, 'addActionHook');
	}
 
	function addContentHook( &$content_actions ){
		global $wgRequest, $wgRequest, $wgTitle, $wgUser, $wgDeletePagePermanentlyNamespaces;
 
		if( !$wgUser->isAllowed( 'deleteperm' ) )
			return false;
 
		$action = $wgRequest->getText( 'action' );
 
		# Special pages can not be deleted (special pages have no article id anyway).
		if ( $wgTitle->getArticleID() != 0 & $wgDeletePagePermanentlyNamespaces[$wgTitle->getNamespace()] == true & $wgTitle->getNamespace() != NS_SPECIAL ){
			wfLoadExtensionMessages( 'DeletePagePermanently' );
 
			$content_actions['ask_delete_permanently'] = array(
				'class' => $action == 'ask_delete_permanently' ? 'selected' : false,
				'text' => wfMsg( 'delete_permanently' ),
				'href' => $wgTitle->getLocalUrl( 'action=ask_delete_permanently' )
			);
		}
 
		return true;
	}
 
	function addActionHook( $action, &$wgArticle ) {
		global $wgOut, $wgUser, $wgDeletePagePermanentlyNamespaces;
 
		wfLoadExtensionMessages( 'DeletePagePermanently' );
 
		if( !$wgUser->isAllowed( 'deleteperm' ) ){
			$wgOut->permissionRequired( 'deleteperm' );
			return false;
		}
 
		# Print a form to approve deletion
		if( $action == 'ask_delete_permanently' ){
 
			$action = $wgArticle->getTitle()->escapeLocalUrl()."?action=delete_permanently";		
			$wgOut->addHTML("<form id='ask_delete_permanently' method='post' action=\"$action\">
<table>
        <tr>
                <td>" . wfMsg('ask_deletion') . "</td>
        </tr>
        <tr>
                <td><input type='submit' name='submit' value=\"" . wfMsg('yes') . "\" /></td>
        </tr>
</table></form>");
			return false;
		} elseif( $action == 'delete_permanently' ){
			# Perform actual deletion
			$dbw = wfGetDB( DB_MASTER );
			$ns  = $wgArticle->mTitle->getNamespace();
			$t   = $wgArticle->mTitle->getDBkey();
			$id  = $wgArticle->mTitle->getArticleID();
 
			if ( $t == '' || $id == 0 || $wgDeletePagePermanentlyNamespaces[$ns] != true || $ns == NS_SPECIAL ){
				$wgOut->addHTML( wfMsgHtml('del_impossible') );
				return false;
			}
 
			####
			## First delete entries, which are in direct relation with the page:
			####
 
			# delete redirect...
			$dbw->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
 
			# delete external link...
			$dbw->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ );			
 
			# delete language link...
			$dbw->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ );
 
			# delete search index...
			$dbw->delete( 'searchindex', array( 'si_page' => $id ), __METHOD__);	
 
			# Delete restrictions for the page
			$dbw->delete( 'page_restrictions', array ( 'pr_page' => $id ), __METHOD__ );
 
			# Delete page Links
			$dbw->delete( 'pagelinks', array ( 'pl_from' => $id ), __METHOD__ );
 
			# delete category links
			$dbw->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ );
 
			# delete template links
			$dbw->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ );				
 
			# read text entries for all revisions and delete them.
			$res = $dbw->select( 'revision', 'rev_text_id', "rev_page=$id" );
			while( $row = $dbw->fetchObject($res) ){
				$value = $row->rev_text_id;
				$dbw->delete( 'text', array( 'old_id' => $value ), __METHOD__ );
			}
 
			# In the table 'revision' : Delete all the revision of the page where 'rev_page' = $id
			$dbw->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ );
 
			# delete image links
			$dbw->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ );
 
			####
			## then delete entries which are not in direct relation with the page: 
			####
 
			# Clean up recentchanges entries...
			$dbw->delete( 'recentchanges', array( 'rc_namespace' => $ns, 'rc_title' => $t ), __METHOD__ );
 
			# read text entries for all archived pages and delete them.
			$res = $dbw->select( 'archive', 'ar_text_id', array( 'ar_namespace' => $ns, 'ar_title' => $t ) );
			while( $row = $dbw->fetchObject($res) ){
				$value = $row->ar_text_id;
				$dbw->delete( 'text', array( 'old_id' => $value ), __METHOD__ );
			}
 
			# Clean archive entries...
			$dbw->delete( 'archive', array( 'ar_namespace' => $ns, 'ar_title' => $t ), __METHOD__ );	
 
			# Clean up log entries...
			$dbw->delete( 'logging', array( 'log_namespace' => $ns, 'log_title' => $t ), __METHOD__ );
 
			# Clean up watchlist...
			$dbw->delete( 'watchlist', array( 'wl_namespace' => $ns, 'wl_title' => $t ), __METHOD__ );	
 
			# In the table 'page' : Delete the page entry
			$dbw->delete( 'page', array( 'page_id' => $id ), __METHOD__ );			
 
			####
			## If an image is beeing deleted, some extra work needs to be done
			####
			if( $ns == NS_IMAGE ){
 
				$file = wfFindFile($t);
 
				if( $file ){
					# Get all filenames of old versions:
					$fields = OldLocalFile::selectFields();
					$res = $dbw->select( 'oldimage', $fields, array( 'oi_name' => $t ) );
					while( $row = $dbw->fetchObject($res) ){
						$oldLocalFile = OldLocalFile::newFromRow($row, $file->repo);
						$path = $oldLocalFile->getArchivePath().'/'.$oldLocalFile->getArchiveName();
 
						# Using the FileStore to delete the file
						$transaction = FileStore::deleteFile( $path );
						$transaction->commit();
					}
 
					$path = $file->getPath();
					# Using the FileStore to delete the file itself
					$transaction = FileStore::deleteFile( $path );
					$transaction->commit();
				}
 
				# clean the filearchive for the given filename:
				$fa_archive_name = array();
				$res = $dbw->select( 'filearchive', 'fa_storage_key', array( 'fa_name' => $t ) );
 
				while( $row = $dbw->fetchObject($res) ){
					$key = $row->fa_storage_key;
 
					# Using the FileStore to delete the file
					$store = FileStore::get( 'deleted' );
					$transaction = $store->delete($key);
					$transaction->commit();
				}
 
				# Delete old db entries of the image: 
				$dbw->delete( 'oldimage', array( 'oi_name' => $t ), __METHOD__ );
 
				# Delete archive entries of the image:
				$dbw->delete( 'filearchive', array( 'fa_name' => $t ), __METHOD__ );
 
				# Delete image entry:
				$dbw->delete( 'image', array( 'img_name' => $t ), __METHOD__ );
 
			}
 
			$wgOut->addHTML( wfMsgHtml('del_done') );
			return false;
		}
		//$wgOut->addHTML( wfMsgHtml('del_not_done') );
		return true;
	}
}
<?php
/**
 * DeletePagePermanently.i18n.php
 * Internationalisation file
 * based on Version 1.0 BETA from Ludovic MOUTON (2008)
 * Wolfgang Stöttinger 2008
 * v. 2.0 BETA
 * GPL
 */
 
$messages = array();
 
/** English */
$messages['en'] = array(
	'tab_label' => 'Delete permanently',
	'del_impossible' => 'This page can not be deleted permanently',
	'ask_deletion' => 'Are you sure, you want to delete this page permanently? All versions and log entries will be deleted. There is no chance to undo this process after completion.',
	'yes' => 'Yes',
	'del_done' => 'The page has been permanently deleted.',
	'del_not_done' => 'The page has NOT been permanently deleted.'
);
 
/** German (Deutsch) */
$messages['de'] = array(
	'tab_label' => 'Dauerhaft löschen',
	'del_impossible' => 'Diese Seite kann nicht dauerhaft gelöscht werden.',
	'ask_deletion' => 'Sind Sie sicher, dass Sie diese Seite dauerhaft löschen wollen? Es werden alle Versionen und Logeinträge gelöscht. Danach besteht keine Möglichkeit, diesen Vorgang rückgängig zu machen.',
	'yes' => 'Ja',
	'del_done' => 'Diese Seite wurde dauerhaft gelöscht.',
	'del_not_done' => 'Die Seite wurde nicht dauerhaft gelöscht.'
);
 
/** French (Français) */
$messages['fr'] = array(
	'tab_label' => 'Supprimer définitivement',
	'del_impossible' => 'Cette page ne peut pas être supprimée définitivement.',
	'ask_deletion' => 'Êtes-vous sûr de vouloir supprimer cette page définitivement ? Toutes les révisions et les entrées du log seront supprimées. Il sera impossible d’annuler la suppression.',
	'yes' => 'Oui',
	'del_done' => 'La page a été supprimée définitivement.',
	'del_not_done' => 'La page n’a pas été supprimée définitivement.'
);

[edit] See also

Personal tools