MediaWiki REL1_31
SpecialNuke.php
Go to the documentation of this file.
1<?php
2
3class SpecialNuke extends SpecialPage {
4
5 public function __construct() {
6 parent::__construct( 'Nuke', 'nuke' );
7 }
8
9 public function doesWrites() {
10 return true;
11 }
12
16 public function execute( $par ) {
17 $this->setHeaders();
18 $this->checkPermissions();
19 $this->checkReadOnly();
20 $this->outputHeader();
21
22 $currentUser = $this->getUser();
23 if ( $currentUser->isBlocked() ) {
24 $block = $currentUser->getBlock();
25 throw new UserBlockedError( $block );
26 }
27
28 $req = $this->getRequest();
29 $target = trim( $req->getText( 'target', $par ) );
30
31 // Normalise name
32 if ( $target !== '' ) {
33 $user = User::newFromName( $target );
34 if ( $user ) {
35 $target = $user->getName();
36 }
37 }
38
39 $msg = $target === '' ?
40 $this->msg( 'nuke-multiplepeople' )->inContentLanguage()->text() :
41 $this->msg( 'nuke-defaultreason', $target )->
42 inContentLanguage()->text();
43 $reason = $req->getText( 'wpReason', $msg );
44
45 $limit = $req->getInt( 'limit', 500 );
46 $namespace = $req->getVal( 'namespace' );
47 $namespace = ctype_digit( $namespace ) ? (int)$namespace : null;
48
49 if ( $req->wasPosted()
50 && $currentUser->matchEditToken( $req->getVal( 'wpEditToken' ) )
51 ) {
52 if ( $req->getVal( 'action' ) === 'delete' ) {
53 $pages = $req->getArray( 'pages' );
54
55 if ( $pages ) {
56 $this->doDelete( $pages, $reason );
57
58 return;
59 }
60 } elseif ( $req->getVal( 'action' ) === 'submit' ) {
61 $this->listForm( $target, $reason, $limit, $namespace );
62 } else {
63 $this->promptForm();
64 }
65 } elseif ( $target === '' ) {
66 $this->promptForm();
67 } else {
68 $this->listForm( $target, $reason, $limit, $namespace );
69 }
70 }
71
77 protected function promptForm( $userName = '' ) {
78 $out = $this->getOutput();
79
80 $out->addWikiMsg( 'nuke-tools' );
81
82 $formDescriptor = [
83 'nuke-target' => [
84 'id' => 'nuke-target',
85 'default' => $userName,
86 'label' => $this->msg( 'nuke-userorip' )->text(),
87 'type' => 'user',
88 'name' => 'target'
89 ],
90 'nuke-pattern' => [
91 'id' => 'nuke-pattern',
92 'label' => $this->msg( 'nuke-pattern' )->text(),
93 'maxLength' => 40,
94 'type' => 'text',
95 'name' => 'pattern'
96 ],
97 'namespace' => [
98 'id' => 'nuke-namespace',
99 'type' => 'namespaceselect',
100 'label' => $this->msg( 'nuke-namespace' )->text(),
101 'all' => 'all',
102 'name' => 'namespace'
103 ],
104 'limit' => [
105 'id' => 'nuke-limit',
106 'maxLength' => 7,
107 'default' => 500,
108 'label' => $this->msg( 'nuke-maxpages' )->text(),
109 'type' => 'int',
110 'name' => 'limit'
111 ]
112 ];
113
114 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
115 ->setName( 'massdelete' )
116 ->setFormIdentifier( 'massdelete' )
117 ->setWrapperLegendMsg( 'nuke' )
118 ->setSubmitTextMsg( 'nuke-submit-user' )
119 ->setSubmitName( 'nuke-submit-user' )
120 ->setAction( $this->getPageTitle()->getLocalURL( 'action=submit' ) )
121 ->setMethod( 'post' )
122 ->addHiddenField( 'wpEditToken', $this->getUser()->getEditToken() )
123 ->prepareForm()
124 ->displayForm( false );
125 }
126
135 protected function listForm( $username, $reason, $limit, $namespace = null ) {
136 $out = $this->getOutput();
137
138 $pages = $this->getNewPages( $username, $limit, $namespace );
139
140 if ( count( $pages ) === 0 ) {
141 if ( $username === '' ) {
142 $out->addWikiMsg( 'nuke-nopages-global' );
143 } else {
144 $out->addWikiMsg( 'nuke-nopages', $username );
145 }
146
147 $this->promptForm( $username );
148
149 return;
150 }
151
152 $out->addModules( 'ext.nuke.confirm' );
153
154 if ( $username === '' ) {
155 $out->addWikiMsg( 'nuke-list-multiple' );
156 } else {
157 $out->addWikiMsg( 'nuke-list', $username );
158 }
159
160 $nuke = $this->getPageTitle();
161
162 $out->addHTML(
163 Xml::openElement( 'form', [
164 'action' => $nuke->getLocalURL( 'action=delete' ),
165 'method' => 'post',
166 'name' => 'nukelist' ]
167 ) .
168 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
169 Xml::tags( 'p',
170 null,
171 Xml::inputLabel(
172 $this->msg( 'deletecomment' )->text(), 'wpReason', 'wpReason', 70, $reason
173 )
174 )
175 );
176
177 // Select: All, None, Invert
178 // ListToggle was introduced in 1.27, old code kept for B/C
179 if ( class_exists( 'ListToggle' ) ) {
180 $listToggle = new ListToggle( $this->getOutput() );
181 $selectLinks = $listToggle->getHTML();
182 } else {
183 $out->addModules( 'ext.nuke' );
184
185 $links = [];
186 $links[] = '<a href="#" id="toggleall">' .
187 $this->msg( 'powersearch-toggleall' )->escaped() . '</a>';
188 $links[] = '<a href="#" id="togglenone">' .
189 $this->msg( 'powersearch-togglenone' )->escaped() . '</a>';
190 $links[] = '<a href="#" id="toggleinvert">' .
191 $this->msg( 'nuke-toggleinvert' )->escaped() . '</a>';
192
193 $selectLinks = Xml::tags( 'p',
194 null,
195 $this->msg( 'nuke-select' )
196 ->rawParams( $this->getLanguage()->commaList( $links ) )->escaped()
197 );
198 }
199
200 $out->addHTML(
201 $selectLinks .
202 '<ul>'
203 );
204
205 $wordSeparator = $this->msg( 'word-separator' )->escaped();
206 $commaSeparator = $this->msg( 'comma-separator' )->escaped();
207
209 foreach ( $pages as $info ) {
213 list( $title, $userName ) = $info;
214
215 $image = $title->inNamespace( NS_FILE ) ? wfLocalFile( $title ) : false;
216 $thumb = $image && $image->exists() ?
217 $image->transform( [ 'width' => 120, 'height' => 120 ], 0 ) :
218 false;
219
220 $userNameText = $userName ?
221 $this->msg( 'nuke-editby', $userName )->parse() . $commaSeparator :
222 '';
223 $changesLink = $linkRenderer->makeKnownLink(
224 $title,
225 $this->msg( 'nuke-viewchanges' )->text(),
226 [],
227 [ 'action' => 'history' ]
228 );
229 $out->addHTML( '<li>' .
230 Xml::check(
231 'pages[]',
232 true,
233 [ 'value' => $title->getPrefixedDBkey() ]
234 ) . '&#160;' .
235 ( $thumb ? $thumb->toHtml( [ 'desc-link' => true ] ) : '' ) .
236 $linkRenderer->makeKnownLink( $title ) . $wordSeparator .
237 $this->msg( 'parentheses' )->rawParams( $userNameText . $changesLink )->escaped() .
238 "</li>\n" );
239 }
240
241 $out->addHTML(
242 "</ul>\n" .
243 Xml::submitButton( $this->msg( 'nuke-submit-delete' )->text() ) .
244 '</form>'
245 );
246 }
247
257 protected function getNewPages( $username, $limit, $namespace = null ) {
259
260 $what = [
261 'rc_namespace',
262 'rc_title',
263 'rc_timestamp',
264 ];
265
266 $where = [ "(rc_new = 1) OR (rc_log_type = 'upload' AND rc_log_action = 'upload')" ];
267
268 if ( class_exists( 'ActorMigration' ) ) {
269 if ( $username === '' ) {
270 $actorQuery = ActorMigration::newMigration()->getJoin( 'rc_user' );
271 $what['rc_user_text'] = $actorQuery['fields']['rc_user_text'];
272 } else {
273 $actorQuery = ActorMigration::newMigration()
274 ->getWhere( $dbr, 'rc_user', User::newFromName( $username, false ) );
275 $where[] = $actorQuery['conds'];
276 }
277 } else {
278 $actorQuery = [ 'tables' => [], 'joins' => [] ];
279 if ( $username === '' ) {
280 $what[] = 'rc_user_text';
281 } else {
282 $where['rc_user_text'] = $username;
283 }
284 }
285
286 if ( $namespace !== null ) {
287 $where['rc_namespace'] = $namespace;
288 }
289
290 $pattern = $this->getRequest()->getText( 'pattern' );
291 if ( !is_null( $pattern ) && trim( $pattern ) !== '' ) {
292 // $pattern is a SQL pattern supporting wildcards, so buildLike
293 // will not work.
294 $where[] = 'rc_title LIKE ' . $dbr->addQuotes( $pattern );
295 }
296 $group = implode( ', ', $what );
297
298 $result = $dbr->select(
299 [ 'recentchanges' ] + $actorQuery['tables'],
300 $what,
301 $where,
302 __METHOD__,
303 [
304 'ORDER BY' => 'rc_timestamp DESC',
305 'GROUP BY' => $group,
306 'LIMIT' => $limit
307 ],
308 $actorQuery['joins']
309 );
310
311 $pages = [];
312
313 foreach ( $result as $row ) {
314 $pages[] = [
315 Title::makeTitle( $row->rc_namespace, $row->rc_title ),
316 $username === '' ? $row->rc_user_text : false
317 ];
318 }
319
320 // Allows other extensions to provide pages to be nuked that don't use
321 // the recentchanges table the way mediawiki-core does
322 Hooks::run( 'NukeGetNewPages', [ $username, $pattern, $namespace, $limit, &$pages ] );
323
324 // Re-enforcing the limit *after* the hook because other extensions
325 // may add and/or remove pages. We need to make sure we don't end up
326 // with more pages than $limit.
327 if ( count( $pages ) > $limit ) {
328 $pages = array_slice( $pages, 0, $limit );
329 }
330
331 return $pages;
332 }
333
341 protected function doDelete( array $pages, $reason ) {
342 $res = [];
343
344 foreach ( $pages as $page ) {
345 $title = Title::newFromText( $page );
346
347 $deletionResult = false;
348 if ( !Hooks::run( 'NukeDeletePage', [ $title, $reason, &$deletionResult ] ) ) {
349 if ( $deletionResult ) {
350 $res[] = $this->msg( 'nuke-deleted', $title->getPrefixedText() )->parse();
351 } else {
352 $res[] = $this->msg( 'nuke-not-deleted', $title->getPrefixedText() )->parse();
353 }
354 continue;
355 }
356
357 $file = $title->getNamespace() === NS_FILE ? wfLocalFile( $title ) : false;
358 $permission_errors = $title->getUserPermissionsErrors( 'delete', $this->getUser() );
359
360 if ( $permission_errors !== [] ) {
361 throw new PermissionsError( 'delete', $permission_errors );
362 }
363
364 if ( $file ) {
365 $oldimage = null; // Must be passed by reference
366 $ok = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, false )->isOK();
367 } else {
368 $article = new Article( $title, 0 );
369 $ok = $article->doDeleteArticle( $reason );
370 }
371
372 if ( $ok ) {
373 $res[] = $this->msg( 'nuke-deleted', $title->getPrefixedText() )->parse();
374 } else {
375 $res[] = $this->msg( 'nuke-not-deleted', $title->getPrefixedText() )->parse();
376 }
377 }
378
379 $this->getOutput()->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $res ) . "</li>\n</ul>\n" );
380 $this->getOutput()->addWikiMsg( 'nuke-delete-more' );
381 }
382
391 public function prefixSearchSubpages( $search, $limit, $offset ) {
392 if ( !class_exists( 'UserNamePrefixSearch' ) ) { // check for version 1.27
393 return [];
394 }
395 $user = User::newFromName( $search );
396 if ( !$user ) {
397 // No prefix suggestion for invalid user
398 return [];
399 }
400 // Autocomplete subpage as user list - public to allow caching
401 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
402 }
403
404 protected function getGroupName() {
405 return 'pagetools';
406 }
407}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfLocalFile( $title)
Get an object referring to a locally registered file.
Class for viewing MediaWiki article and history.
Definition Article.php:35
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
Class for generating clickable toggle links for a list of checkboxes.
Show an error when a user tries to do something they do not have the necessary permissions for.
execute( $par)
doDelete(array $pages, $reason)
Does the actual deletion of the pages.
promptForm( $userName='')
Prompt for a username or IP address.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
getNewPages( $username, $limit, $namespace=null)
Gets a list of new pages by the specified user or everyone when none is specified.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
listForm( $username, $reason, $limit, $namespace=null)
Display list of pages to delete.
doesWrites()
Indicates whether this special page may perform database writes.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
MediaWiki Linker LinkRenderer null $linkRenderer
Show an error when the user tries to do something whilst blocked.
static search( $audience, $search, $limit, $offset=0)
Do a prefix search of user names and return a list of matching user names.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
this hook is for auditing only $req
Definition hooks.txt:990
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition hooks.txt:895
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:785
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const NS_FILE
Definition Defines.php:80
const DB_REPLICA
Definition defines.php:25