Brion VIBBER has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/132378

Change subject: Add API action=watchlistclear to bulk-clear watchlist.
......................................................................

Add API action=watchlistclear to bulk-clear watchlist.

Requested by bot runners, who often accumulate huge watchlists
and have trouble resetting them.

Requires POST and a token to prevent accidents or abuse.
Same token as individual watching.

https://www.mediawiki.org/wiki/API:WatchlistClear

Bug: 65058
Change-Id: Id8476a1ba8b2661cf6451cacf3121d5c2d0675cf
---
M includes/AutoLoader.php
M includes/api/ApiMain.php
A includes/api/ApiWatchlistClear.php
3 files changed, 107 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/78/132378/1

diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php
index bfee420..d6e1766 100644
--- a/includes/AutoLoader.php
+++ b/includes/AutoLoader.php
@@ -355,6 +355,7 @@
        'ApiUpload' => 'includes/api/ApiUpload.php',
        'ApiUserrights' => 'includes/api/ApiUserrights.php',
        'ApiWatch' => 'includes/api/ApiWatch.php',
+       'APiWatchlistClear' => 'includes/api/ApiWatchlistClear.php',
        'UsageException' => 'includes/api/ApiMain.php',
 
        # includes/cache
diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php
index 554bfa6..4b8febd 100644
--- a/includes/api/ApiMain.php
+++ b/includes/api/ApiMain.php
@@ -79,6 +79,7 @@
                'filerevert' => 'ApiFileRevert',
                'emailuser' => 'ApiEmailUser',
                'watch' => 'ApiWatch',
+               'watchlistclear' => 'ApiWatchlistClear',
                'patrol' => 'ApiPatrol',
                'import' => 'ApiImport',
                'userrights' => 'ApiUserrights',
diff --git a/includes/api/ApiWatchlistClear.php 
b/includes/api/ApiWatchlistClear.php
new file mode 100644
index 0000000..6f699af
--- /dev/null
+++ b/includes/api/ApiWatchlistClear.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Created on May 8, 2014
+ *
+ * Copyright © 2014 Brion Vibber <br...@pobox.com>
+ *
+ * Based on ApiWatch.php, copyright © 2008 Yuri Astrakhan 
"<Firstname><Lastname>@gmail.com"
+ *
+ * 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.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * API module to allow users to watch a page
+ *
+ * @ingroup API
+ */
+class ApiWatchlistClear extends ApiBase {
+       public function execute() {
+               $user = $this->getUser();
+               if ( !$user->isLoggedIn() ) {
+                       $this->dieUsage( 'You must be logged-in to have a 
watchlist', 'notloggedin' );
+               }
+
+               if ( !$user->isAllowed( 'editmywatchlist' ) ) {
+                       $this->dieUsage( 'You don\'t have permission to edit 
your watchlist', 'permissiondenied' );
+               }
+
+               $params = $this->extractRequestParams();
+               $res = $this->bulkUnwatch();
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$res );
+       }
+       
+       private function bulkUnwatch() {
+               // We're going to do this in the database as a bulk operation
+               // instead of one at a time, so it doesn't time out on largeish 
lists.          
+               
+               $this->getDB()->delete( 'watchlist', array( 'wl_user' => 
$this->getUser()->getId() ), __METHOD__ );
+               
+               return array( 'unwatch' => true );
+       }
+
+       public function mustBePosted() {
+               return true;
+       }
+
+       public function isWriteMode() {
+               return true;
+       }
+
+       public function needsToken() {
+               return true;
+       }
+
+       public function getTokenSalt() {
+               return 'watch';
+       }
+
+       public function getAllowedParams( $flags = 0 ) {
+               $result = array(
+                       'token' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+               );
+
+               return $result;
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'token' => 'A token previously acquired via prop=info',
+               );
+       }
+
+       public function getDescription() {
+               return 'Bulk-clear all items from the user\'s watchlist. This 
can be useful for large bot accounts.';
+       }
+
+       public function getPossibleErrors() {
+               return array_merge( parent::getPossibleErrors(), array(
+                       array( 'code' => 'notloggedin', 'info' => 'You must be 
logged-in to have a watchlist' ),
+                       array( 'invalidtitle', 'title' ),
+                       array( 'hookaborted' ),
+               ) );
+       }
+
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:WatchlistClear';
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/132378
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id8476a1ba8b2661cf6451cacf3121d5c2d0675cf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Brion VIBBER <br...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to