Difference between revisions of "Extension:NukeDPL.php"

From Organic Design wiki
(function getPages - Bug 1: first idem of the dpl list is not included - Solution: removed the begin-of-line token (^) from RegEx. See [http://www.mediawiki.org/wiki/Extension_talk:NukeDPL here])
m (Thanks for restoring the fixes, Nad, missed only one: the begin-of-line (^) in the RegExp leaves out of the list the first result of the DPL query.)
 
(4 intermediate revisions by 2 users not shown)
Line 12: Line 12:
 
  */
 
  */
  
if(!defined('MEDIAWIKI')) die('Not a valid entry point.');
+
if( !defined( 'MEDIAWIKI' ) ) die( 'Not a valid entry point.' );
  
define('NUKEDPL_VERSION', '1.1.3, 2008-07-13');
+
define( 'NUKEDPL_VERSION', '1.2.1, 2009-03-20' );
  
 
$wgGroupPermissions['sysop']['nuke'] = true;
 
$wgGroupPermissions['sysop']['nuke'] = true;
 
$wgAvailableRights[]                = 'nuke';
 
$wgAvailableRights[]                = 'nuke';
 
$wgExtensionFunctions[]              = 'wfSetupNukeDPL';
 
$wgExtensionFunctions[]              = 'wfSetupNukeDPL';
 +
$wgSpecialPages['NukeDPL']          = 'SpecialNukeDPL';
 +
$wgSpecialPageGroups['NukeDPL']      = 'pagetools';
  
 
# Text to be added into textbox by default
 
# Text to be added into textbox by default
Line 46: Line 48:
 
uses              = Template1 | Template2
 
uses              = Template1 | Template2
 
notuses          = Template1 | Template2
 
notuses          = Template1 | Template2
redirects        = exclude | include| only
+
redirects        = exclude | include | only
 
createdby        = User
 
createdby        = User
 
notcreatedby      = User
 
notcreatedby      = User
Line 65: Line 67:
 
function wfSetupNukeDPL() {
 
function wfSetupNukeDPL() {
 
global $wgMessageCache;
 
global $wgMessageCache;
 
+
$wgMessageCache->addMessages( array(
$wgMessageCache->addMessages(array(
 
 
'nukedpl'            => 'Mass delete by DPL query',
 
'nukedpl'            => 'Mass delete by DPL query',
 
'nuke-nopages'      => "No pages to delete using DPL-query: <tt>$1</tt>",
 
'nuke-nopages'      => "No pages to delete using DPL-query: <tt>$1</tt>",
 
'nuke-list'          => "The following pages were selected by DPL-query: <tt>$1</tt> hit the button to delete them.",
 
'nuke-list'          => "The following pages were selected by DPL-query: <tt>$1</tt> hit the button to delete them.",
 
'nuke-defaultreason' => "Mass removal of pages selected by DPL-query: ($1)",
 
'nuke-defaultreason' => "Mass removal of pages selected by DPL-query: ($1)",
));
+
) );
 
 
# Add the specialpage to the environment
 
SpecialPage::addPage(new SpecialNukeDPL());
 
 
}
 
}
  
Line 80: Line 78:
 
  * Define a new class based on the SpecialPage class
 
  * Define a new class based on the SpecialPage class
 
  */
 
  */
require_once("$IP/includes/SpecialPage.php");
 
 
class SpecialNukeDPL extends SpecialPage {
 
class SpecialNukeDPL extends SpecialPage {
+
 
# Constructor
+
function __construct() {
function SpecialNukeDPL() {
+
parent::__construct( 'NukeDPL', 'nuke' );
SpecialPage::SpecialPage('NukeDPL','nuke');
 
 
}
 
}
 
   
 
   
# Override SpecialPage::execute($param = '')
+
function execute( $par ) {
function execute($param) {
+
global $wgUser, $wgRequest;
global $wgRequest;
+
 
$target = $wgRequest->getText( 'target', $param );
+
if ( !$this->userCanExecute( $wgUser ) ) {
 +
$this->displayRestrictionError();
 +
return;
 +
}
 +
 
 
$this->setHeaders();
 
$this->setHeaders();
$form = new NukeDPLForm( $target, $wgRequest );
+
$this->outputHeader();
$form->run();
 
}
 
}
 
  
class NukeDPLForm {
+
$target = $wgRequest->getText( 'target' );
 +
$reason = $wgRequest->getText( 'wpReason', wfMsgForContent( 'nuke-defaultreason', $target ) );
 +
$posted = $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );
  
function NukeDPLForm($target,$request) {
+
if ( $posted ) {
global $wgUser;
+
if ( $pages = $wgRequest->getArray( 'pages' ) ) return $this->doDelete( $pages, $reason );
$this->mTarget = $target;
+
}
$this->mReason = $request->getText('wpReason',wfMsgForContent('nuke-defaultreason',$target));
 
$this->mPosted = $request->wasPosted() && $wgUser->matchEditToken($request->getVal('wpEditToken'));
 
if ($this->mPosted) $this->mPages = $request->getArray('pages');
 
}
 
  
function run() {
+
if ( $target ) $this->listForm( $target, $reason ); else $this->promptForm();
if ($this->mPosted && $this->mPages) return $this->doDelete($this->mPages,$this->mReason);
 
if ($this->mTarget != '') $this->listForm($this->mTarget,$this->mReason);
 
else $this->promptForm();
 
 
}
 
}
  
Line 118: Line 110:
  
 
$sk =& $wgUser->getSkin();
 
$sk =& $wgUser->getSkin();
$nuke = Title::makeTitle(NS_SPECIAL,'NukeDPL');
+
$nuke = Title::makeTitle( NS_SPECIAL, 'NukeDPL' );
$submit = wfElement('input',array('type' => 'submit', 'value' => 'View candidate list'));
+
$submit = wfElement( 'input', array( 'type' => 'submit', 'value' => 'View candidate list' ) );
  
$wgOut->addWikiText("This tool allows for mass deletions of pages selected by a DPL query.<br>");
+
$wgOut->addWikiText( "This tool allows for mass deletions of pages selected by a DPL query.<br>" );
$wgOut->addWikiText("Enter a query below to generate a list of titles to delete.");
+
$wgOut->addWikiText( "Enter a query below to generate a list of titles to delete." );
$wgOut->addWikiText("*Titles can be individually removed before deleting.");
+
$wgOut->addWikiText( "*Titles can be individually removed before deleting." );
$wgOut->addWikiText("*Remember, article titles are case-sensitive.");
+
$wgOut->addWikiText( "*Remember, article titles are case-sensitive." );
$wgOut->addWikiText("*Queries shouldn't be surrounded by any DPL tags or braces.");
+
$wgOut->addWikiText( "*Queries shouldn't be surrounded by any DPL tags or braces." );
$wgOut->addWikiText("*For information about the parameter meanings, see the [http://semeb.com/dpldemo/index.php?title=DPL:Manual DPL Manual].");
+
$wgOut->addWikiText( "*For information about the parameter meanings, see the [http://semeb.com/dpldemo/index.php?title=DPL:Manual DPL Manual]." );
$wgOut->addHTML(wfElement('form', array('action' => $nuke->getLocalURL('action=submit'), 'method' => 'post'),null)
+
$wgOut->addHTML( wfElement( 'form', array( 'action' => $nuke->getLocalURL( 'action=submit' ), 'method' => 'post' ), null )
."<textarea name=\"target\" cols=\"25\" rows=\"30\">$wgNukeDPLDefaultText</textarea>"
+
. "<textarea name=\"target\" cols=\"25\" rows=\"30\">$wgNukeDPLDefaultText</textarea>"
."\n$submit\n");
+
. "\n$submit\n" );
$wgOut->addHTML("</form>");
+
$wgOut->addHTML( "</form>" );
 
}
 
}
  
function listForm($query,$reason) {
+
function listForm( $query, $reason ) {
global $wgUser,$wgOut,$wgLang;
+
global $wgUser, $wgOut, $wgLang;
  
$pages = $this->getPages($query);
+
$pages = $this->getPages( $query );
if (count($pages) == 0) {
+
if ( count( $pages ) == 0 ) {
$wgOut->addWikiText(wfMsg('nuke-nopages',$query));
+
$wgOut->addWikiText( wfMsg( 'nuke-nopages', $query ) );
 
return $this->promptForm();
 
return $this->promptForm();
 
}
 
}
  
$wgOut->addWikiText(wfMsg('nuke-list',$query));
+
$wgOut->addWikiText( wfMsg( 'nuke-list',$query ) );
  
$nuke = Title::makeTitle(NS_SPECIAL,'NukeDPL');
+
$nuke = Title::makeTitle( NS_SPECIAL, 'NukeDPL' );
$submit = wfElement('input',array('type' => 'submit', 'value' => 'Nuke!'));
+
$submit = wfElement( 'input', array( 'type' => 'submit', 'value' => 'Nuke!' ) );
 
$wgOut->addHTML(
 
$wgOut->addHTML(
wfElement('form',array('action' => $nuke->getLocalURL('action=delete'),'method' => 'post'),null)
+
wfElement( 'form', array( 'action' => $nuke->getLocalURL( 'action=delete' ), 'method' => 'post' ), null )
."\n<div>".wfMsgHtml('deletecomment').': '
+
."\n<div>".wfMsgHtml( 'deletecomment' ) . ': '
.wfElement('input',array('name' => 'wpReason','value' => $reason,'size' => 60))."</div>\n$submit"
+
.wfElement( 'input', array( 'name' => 'wpReason', 'value' => $reason, 'size' => 60 ) ) . "</div>\n$submit"
.wfElement('input',array('type' => 'hidden','name' => 'wpEditToken','value' => $wgUser->editToken()))."\n<ul>\n"
+
.wfElement( 'input', array( 'type' => 'hidden', 'name' => 'wpEditToken', 'value' => $wgUser->editToken() ) ) . "\n<ul>\n"
 
);
 
);
  
 
$sk =& $wgUser->getSkin();
 
$sk =& $wgUser->getSkin();
foreach ($pages as $title) {
+
foreach ( $pages as $title ) {
$wgOut->addHTML('<li>'
+
$wgOut->addHTML( '<li>'
.wfElement('input',array('type' => 'checkbox','name' => "pages[]",'value' => $title,'checked' => 'checked'))
+
.wfElement( 'input', array( 'type' => 'checkbox', 'name' => "pages[]", 'value' => $title, 'checked' => 'checked' ) )
.'&nbsp;'.$sk->makeKnownLinkObj(Title::newFromText($title))."</li>\n"
+
.'&nbsp;' . $sk->makeKnownLinkObj( Title::newFromText( $title ) ) . "</li>\n"
 
);
 
);
 
}
 
}
$wgOut->addHTML("</ul>\n$submit</form>");
+
$wgOut->addHTML( "</ul>\n$submit</form>" );
 
}
 
}
  
function getPages($query) {
+
function getPages( $query ) {
global $wgTitle,$wgParser,$wgUser;
+
global $wgTitle, $wgParser, $wgUser;
 
$fname = 'NukeDPLForm::getNewPages';
 
$fname = 'NukeDPLForm::getNewPages';
$query = trim($query)."\nmode=userformat\nlistseparators=,\\n$$$%PAGE%$$$,,\n";
+
$query = trim( $query ) . "\nmode=userformat\nlistseparators=,\\n$$$%PAGE%$$$,,\n";
$opt = ParserOptions::newFromUser($wgUser);
+
$opt = ParserOptions::newFromUser( $wgUser );
$out = $wgParser->parse("<dpl>$query</dpl>",$wgTitle,$opt,false,true);
+
$out = $wgParser->parse( "<dpl>$query</dpl>", $wgTitle, $opt, false, true );
preg_match_all('|\\${3}(.+?)\\${3}|m',$out->getText(),$matches);
+
preg_match_all( '|\\${3}(.+?)\\${3}|m', $out->getText(), $matches );
return $matches[1];
+
return str_replace( array( '&nbsp;', '&amp;' ), array( ' ', '&' ), $matches[1] );
 
}
 
}
  
function doDelete($pages,$reason) {
+
function doDelete( $pages, $reason ) {
foreach($pages as $page) {
+
foreach ( $pages as $page ) {
$title = Title::newFromUrl($page);
+
$title = Title::newFromUrl( $page );
$article = new Article($title);
+
$article = new Article( $title );
$article->doDelete($reason);
+
$article->doDelete( $reason );
 
}
 
}
 
}
 
}
 
}
 
}

Latest revision as of 01:50, 22 March 2009

<?php /**

* NukeDPL extension - Mass delete by DPL query
* 
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

* See http://www.mediawiki.org/wiki/Extension:NukeDPL for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author Aran Dunkley User:Nad
* @copyright © 2007 Aran Dunkley
* @licence GNU General Public Licence 2.0 or later
*/

if( !defined( 'MEDIAWIKI' ) ) die( 'Not a valid entry point.' );

define( 'NUKEDPL_VERSION', '1.2.1, 2009-03-20' );

$wgGroupPermissions['sysop']['nuke'] = true; $wgAvailableRights[] = 'nuke'; $wgExtensionFunctions[] = 'wfSetupNukeDPL'; $wgSpecialPages['NukeDPL'] = 'SpecialNukeDPL'; $wgSpecialPageGroups['NukeDPL'] = 'pagetools';

  1. Text to be added into textbox by default

$wgNukeDPLDefaultText = ' distinct = true | false ignorecase = true | false title = Article nottitle = Article titlematch = %fragment% nottitlematch = %fragment% titleregexp = ^.+$ nottitleregexp = ^.+$ category = Category1 | Category2 notcategory = Category1 | Category2 categorymatch = %fragment% notcategorymatch = %fragment% categoryregexp = ^.+$ notcategoryregexp = ^.+$ namespace = Namespace1 | Namespace2 notnamespace = Namespace1 | Namespace2 linksfrom = Foo | Bar notlinksfrom = Foo | Bar linksto = Foo|Bar notlinksto = Foo|Bar imageused = Foo.jpg imagecontainer = Article1 | Article2 uses = Template1 | Template2 notuses = Template1 | Template2 redirects = exclude | include | only createdby = User notcreatedby = User modifiedby = User notmodifiedby = User lastmodifiedby = User notlastmodifiedby = User ';

$wgExtensionCredits['specialpage'][] = array( 'name' => 'Special:NukeDPL', 'author' => 'User:Nad', 'description' => 'Mass delete by DPL query', 'url' => 'http://www.mediawiki.org/wiki/Extension:NukeDPL', 'version' => NUKEDPL_VERSION );

function wfSetupNukeDPL() { global $wgMessageCache; $wgMessageCache->addMessages( array( 'nukedpl' => 'Mass delete by DPL query', 'nuke-nopages' => "No pages to delete using DPL-query: $1", 'nuke-list' => "The following pages were selected by DPL-query: $1 hit the button to delete them.", 'nuke-defaultreason' => "Mass removal of pages selected by DPL-query: ($1)", ) ); }

/**

* Define a new class based on the SpecialPage class
*/

class SpecialNukeDPL extends SpecialPage {

function __construct() { parent::__construct( 'NukeDPL', 'nuke' ); }

function execute( $par ) { global $wgUser, $wgRequest;

if ( !$this->userCanExecute( $wgUser ) ) { $this->displayRestrictionError(); return; }

$this->setHeaders(); $this->outputHeader();

$target = $wgRequest->getText( 'target' ); $reason = $wgRequest->getText( 'wpReason', wfMsgForContent( 'nuke-defaultreason', $target ) ); $posted = $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) );

if ( $posted ) { if ( $pages = $wgRequest->getArray( 'pages' ) ) return $this->doDelete( $pages, $reason ); }

if ( $target ) $this->listForm( $target, $reason ); else $this->promptForm(); }

function promptForm() { global $wgUser, $wgOut, $wgNukeDPLDefaultText;

$sk =& $wgUser->getSkin(); $nuke = Title::makeTitle( NS_SPECIAL, 'NukeDPL' ); $submit = wfElement( 'input', array( 'type' => 'submit', 'value' => 'View candidate list' ) );

$wgOut->addWikiText( "This tool allows for mass deletions of pages selected by a DPL query.
" ); $wgOut->addWikiText( "Enter a query below to generate a list of titles to delete." ); $wgOut->addWikiText( "*Titles can be individually removed before deleting." ); $wgOut->addWikiText( "*Remember, article titles are case-sensitive." ); $wgOut->addWikiText( "*Queries shouldn't be surrounded by any DPL tags or braces." ); $wgOut->addWikiText( "*For information about the parameter meanings, see the DPL Manual." ); $wgOut->addHTML( wfElement( 'form', array( 'action' => $nuke->getLocalURL( 'action=submit' ), 'method' => 'post' ), null ) . "<textarea name=\"target\" cols=\"25\" rows=\"30\">$wgNukeDPLDefaultText</textarea>" . "\n$submit\n" ); $wgOut->addHTML( "</form>" ); }

function listForm( $query, $reason ) { global $wgUser, $wgOut, $wgLang;

$pages = $this->getPages( $query ); if ( count( $pages ) == 0 ) { $wgOut->addWikiText( wfMsg( 'nuke-nopages', $query ) ); return $this->promptForm(); }

$wgOut->addWikiText( wfMsg( 'nuke-list',$query ) );

$nuke = Title::makeTitle( NS_SPECIAL, 'NukeDPL' ); $submit = wfElement( 'input', array( 'type' => 'submit', 'value' => 'Nuke!' ) ); $wgOut->addHTML( wfElement( 'form', array( 'action' => $nuke->getLocalURL( 'action=delete' ), 'method' => 'post' ), null )

."\n

".wfMsgHtml( 'deletecomment' ) . ': ' .wfElement( 'input', array( 'name' => 'wpReason', 'value' => $reason, 'size' => 60 ) ) . "

\n$submit" .wfElement( 'input', array( 'type' => 'hidden', 'name' => 'wpEditToken', 'value' => $wgUser->editToken() ) ) . "\n

    \n" ); $sk =& $wgUser->getSkin(); foreach ( $pages as $title ) { $wgOut->addHTML( '
  • ' .wfElement( 'input', array( 'type' => 'checkbox', 'name' => "pages[]", 'value' => $title, 'checked' => 'checked' ) ) .' ' . $sk->makeKnownLinkObj( Title::newFromText( $title ) ) . "
  • \n"

    ); }

    $wgOut->addHTML( "

\n$submit</form>" );

}

function getPages( $query ) { global $wgTitle, $wgParser, $wgUser; $fname = 'NukeDPLForm::getNewPages'; $query = trim( $query ) . "\nmode=userformat\nlistseparators=,\\n$$$%PAGE%$$$,,\n"; $opt = ParserOptions::newFromUser( $wgUser );

$out = $wgParser->parse( "

Extension:DynamicPageList (DPL), version 3.3.3: Warning: No parameter option supplied for '$query'. (Missing '=')


Extension:DynamicPageList (DPL), version 3.3.3: Error: No selection criteria found! You must use at least one of the following parameters: category, namespace, titlematch, linksto, uses, createdby, modifiedby, lastmodifiedby, or their 'not' variants


Extension:DynamicPageList (DPL), version 3.3.3: Warning: No results.

", $wgTitle, $opt, false, true );

preg_match_all( '|\\${3}(.+?)\\${3}|m', $out->getText(), $matches ); return str_replace( array( ' ', '&' ), array( ' ', '&' ), $matches[1] ); }

function doDelete( $pages, $reason ) { foreach ( $pages as $page ) { $title = Title::newFromUrl( $page ); $article = new Article( $title ); $article->doDelete( $reason ); } } }