/
SpecialNukeDPL.php
108 lines (91 loc) · 3.85 KB
/
SpecialNukeDPL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
use MediaWiki\MediaWikiServices;
class SpecialNukeDPL extends SpecialPage {
function __construct() {
parent::__construct( 'NukeDPL', 'nukedpl' );
}
public function doesWrites() {
return true;
}
function execute( $parser = null ) {
$user = $this->getUser();
if ( !$this->userCanExecute( $user ) ) {
$this->displayRestrictionError();
}
$this->setHeaders();
$this->outputHeader();
$request = $this->getRequest();
if ( $request->wasPosted() ) {
$ids = $request->getArray( 'ids' );
$query = $request->getText( 'query' );
$reason = $request->getText( 'reason', $this->msg( 'nukedpl-defaultreason', $query )->inContentLanguage()->text() );
if ( $query ) {
$this->deleteForm( $query, $reason );
} else {
$this->doDelete( $ids, $reason );
}
} else {
$this->queryForm();
}
}
function queryForm() {
$title = $this->getPageTitle();
$output = $this->getOutput();
$output->addWikiTextAsInterface( $this->msg( 'nukedpl-intro' )->text() );
$output->addHTML( Xml::element( 'form', [ 'action' => $title->getLocalURL( 'action=submit' ), 'method' => 'post' ], null ) );
$output->addHTML( Xml::element( 'textarea', [ 'name' => 'query', 'style' => 'width: 100%; height: 50em;' ], $this->msg( 'nukedpl-defaulttext' )->text() ) );
$output->addHTML( Xml::element( 'br' ) );
$output->addHTML( Xml::element( 'input', [ 'type' => 'submit', 'value' => $this->msg( 'nukedpl-candidatelist' )->text() ] ) );
$output->addHTML( '</form>' );
}
function deleteForm( $query, $reason ) {
$title = $this->getPageTitle();
$output = $this->getOutput();
$pages = $this->getPages( $query );
$count = count( $pages );
if ( $count == 0 ) {
$output->addWikiTextAsInterface( $this->msg( 'nukedpl-nopages', $query )->text() );
return $this->queryForm();
}
$output->addWikiTextAsInterface( $this->msg( 'nukedpl-list', $count, $query )->text() );
$output->addHTML( Xml::element( 'form', [ 'action' => $title->getLocalURL( 'action=delete' ), 'method' => 'post' ], null ) );
$output->addHTML( Xml::element( 'input', [ 'type' => 'submit', 'value' => $this->msg( 'nukedpl-nuke' )->text() ] ) );
$output->addHTML( Xml::element( 'div', [], $this->msg( 'deletecomment' )->text() ) );
$output->addHTML( Xml::element( 'input', [ 'name' => 'reason', 'value' => $reason, 'size' => 60 ] ) );
$output->addHTML( '<ol>' );
foreach ( $pages as $page ) {
$page = Title::newFromText( $page );
if ( $page && $page->isKnown() ) {
$output->addHTML( '<li>' );
$output->addHTML( Xml::element( 'input', [ 'type' => 'checkbox', 'name' => 'ids[]', 'value' => $page->getArticleID(), 'checked' => 'checked' ] ) );
$output->addHTML( Linker::linkKnown( $page ) );
$output->addHTML( '</li>' );
}
}
$output->addHTML( '</ol>' );
$output->addHTML( Xml::element( 'input', [ 'type' => 'submit', 'value' => $this->msg( 'nukedpl-nuke' )->text() ] ) );
$output->addHTML( '</form>' );
}
function getPages( $query ) {
$user = $this->getUser();
$title = $this->getPageTitle();
$query = trim( $query ) . PHP_EOL . "format=,$$$%PAGE%$$$,,"; // Enclose each title with $$$
$parser = MediaWikiServices::getInstance()->getParser();
$parserOptions = ParserOptions::newFromUser( $user );
$parserOutput = $parser->parse( '<dpl>' . $query . '</dpl>', $title, $parserOptions, false, true );
preg_match_all( '|\$\$\$(.+)\$\$\$|U', $parserOutput->getText(), $matches ); // Extract the titles from the output
return $matches[1]; // Return an array of titles
}
function doDelete( $ids, $reason ) {
$services = MediaWikiServices::getInstance();
foreach ( $ids as $id ) {
$wikiPage = $services->getWikiPageFactory()->newFromID( $id );
$deletePage = $services->getDeletePageFactory()
->newDeletePage( $wikiPage, $this->getUser() );
$deletePage->deleteIfAllowed( $reason );
}
}
protected function getGroupName() {
return 'pagetools';
}
}