Add API action=revisiondelete
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 26 Jun 2013 20:59:40 +0000 (16:59 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Tue, 4 Feb 2014 20:03:48 +0000 (15:03 -0500)
Add action=revisiondelete to the API, and some functions to the
revisiondelete backend to support this.

Bug: 23005
Change-Id: Iee146fed648603b86a35927518f850771fc69bd2

RELEASE-NOTES-1.23
includes/AutoLoader.php
includes/api/ApiMain.php
includes/api/ApiRevisionDelete.php [new file with mode: 0644]
includes/revisiondelete/RevisionDelete.php
includes/revisiondelete/RevisionDeleteAbstracts.php

index 97f2982..59118bd 100644 (file)
@@ -157,6 +157,7 @@ production.
   rights when checking whether wlprop=patrol is allowed.
 * (bug 32151) ApiWatch now has pageset capabilities (titles/pageids/generators).
   Title parameter is now deprecated.
+* (bug 23005) Added action=revisiondelete.
 
 === Languages updated in 1.23 ===
 
index 0f1cded..61e0941 100644 (file)
@@ -365,6 +365,7 @@ $wgAutoloadLocalClasses = array(
        'ApiQueryWatchlist' => 'includes/api/ApiQueryWatchlist.php',
        'ApiQueryWatchlistRaw' => 'includes/api/ApiQueryWatchlistRaw.php',
        'ApiResult' => 'includes/api/ApiResult.php',
+       'ApiRevisionDelete' => 'includes/api/ApiRevisionDelete.php',
        'ApiRollback' => 'includes/api/ApiRollback.php',
        'ApiRsd' => 'includes/api/ApiRsd.php',
        'ApiSetNotificationTimestamp' => 'includes/api/ApiSetNotificationTimestamp.php',
index 1be0181..81b5e9f 100644 (file)
@@ -84,6 +84,7 @@ class ApiMain extends ApiBase {
                'userrights' => 'ApiUserrights',
                'options' => 'ApiOptions',
                'imagerotate' => 'ApiImageRotate',
+               'revisiondelete' => 'ApiRevisionDelete',
        );
 
        /**
diff --git a/includes/api/ApiRevisionDelete.php b/includes/api/ApiRevisionDelete.php
new file mode 100644 (file)
index 0000000..55d59f4
--- /dev/null
@@ -0,0 +1,245 @@
+<?php
+/**
+ * Created on Jun 25, 2013
+ *
+ * Copyright © 2013 Brad Jorsch <bjorsch@wikimedia.org>
+ *
+ * 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
+ * @since 1.23
+ */
+
+/**
+ * API interface to RevDel. The API equivalent of Special:RevisionDelete.
+ * Requires API write mode to be enabled.
+ *
+ * @ingroup API
+ */
+class ApiRevisionDelete extends ApiBase {
+
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $user = $this->getUser();
+
+               if ( !$user->isAllowed( RevisionDeleter::getRestriction( $params['type'] ) ) ) {
+                       $this->dieUsageMsg( 'badaccess-group0' );
+               }
+
+               if ( !$params['ids'] ) {
+                       $this->dieUsage( "At least one value is required for 'ids'", 'badparams' );
+               }
+
+               $hide = $params['hide'] ?: array();
+               $show = $params['show'] ?: array();
+               if ( array_intersect( $hide, $show ) ) {
+                       $this->dieUsage( "Mutually exclusive values for 'hide' and 'show'", 'badparams' );
+               } elseif ( !$hide && !$show ) {
+                       $this->dieUsage( "At least one value is required for 'hide' or 'show'", 'badparams' );
+               }
+               $bits = array(
+                       'content' => RevisionDeleter::getRevdelConstant( $params['type'] ),
+                       'comment' => Revision::DELETED_COMMENT,
+                       'user' => Revision::DELETED_USER,
+               );
+               $bitfield = array();
+               foreach ( $bits as $key => $bit ) {
+                       if ( in_array( $key, $hide ) ) {
+                               $bitfield[$bit] = 1;
+                       } elseif ( in_array( $key, $show ) ) {
+                               $bitfield[$bit] = 0;
+                       } else {
+                               $bitfield[$bit] = -1;
+                       }
+               }
+
+               if ( $params['suppress'] === 'yes' ) {
+                       if ( !$user->isAllowed( 'suppressrevision' ) ) {
+                               $this->dieUsageMsg( 'badaccess-group0' );
+                       }
+                       $bitfield[Revision::DELETED_RESTRICTED] = 1;
+               } elseif ( $params['suppress'] === 'no' ) {
+                       $bitfield[Revision::DELETED_RESTRICTED] = 0;
+               } else {
+                       $bitfield[Revision::DELETED_RESTRICTED] = -1;
+               }
+
+               $targetObj = null;
+               if ( $params['target'] ) {
+                       $targetObj = Title::newFromText( $params['target'] );
+               }
+               $targetObj = RevisionDeleter::suggestTarget( $params['type'], $targetObj, $params['ids'] );
+               if ( $targetObj === null ) {
+                       $this->dieUsage( 'A target title is required for this RevDel type', 'needtarget' );
+               }
+
+               $list = RevisionDeleter::createList(
+                       $params['type'], $this->getContext(), $targetObj, $params['ids']
+               );
+               $status = $list->setVisibility(
+                       array( 'value' => $bitfield, 'comment' => $params['reason'], 'perItemStatus' => true )
+               );
+
+               $result = $this->getResult();
+               $data = $this->extractStatusInfo( $status );
+               $data['target'] = $targetObj->getFullText();
+               $data['items'] = array();
+               foreach ( $status->itemStatuses as $id => $s ) {
+                       $data['items'][$id] = $this->extractStatusInfo( $s );
+                       $data['items'][$id]['id'] = $id;
+               }
+               $list->reloadFromMaster();
+               for ( $item = $list->reset(); $list->current(); $item = $list->next() ) {
+                       $data['items'][$item->getId()] += $item->getApiData( $this->getResult() );
+               }
+               $data['items'] = array_values( $data['items'] );
+               $result->setIndexedTagName( $data['items'], 'i' );
+               $result->addValue( null, $this->getModuleName(), $data );
+       }
+
+       private function extractStatusInfo( $status ) {
+               $ret = array(
+                       'status' => $status->isOK() ? 'Success' : 'Fail',
+               );
+               $errors = $this->formatStatusMessages( $status->getErrorsByType( 'error' ) );
+               if ( $errors ) {
+                       $this->getResult()->setIndexedTagName( $errors, 'e' );
+                       $ret['errors'] = $errors;
+               }
+               $warnings = $this->formatStatusMessages( $status->getErrorsByType( 'warning' ) );
+               if ( $warnings ) {
+                       $this->getResult()->setIndexedTagName( $warnings, 'w' );
+                       $ret['warnings'] = $warnings;
+               }
+               return $ret;
+       }
+
+       private function formatStatusMessages( $messages ) {
+               if ( !$messages ) {
+                       return array();
+               }
+               $result = $this->getResult();
+               $ret = array();
+               foreach ( $messages as $m ) {
+                       $message = array();
+                       if ( $m['message'] instanceof Message ) {
+                               $msg = $m['message'];
+                               $message = array( 'message' => $msg->getKey() );
+                               if ( $msg->getParams() ) {
+                                       $message['params'] = $msg->getParams();
+                                       $result->setIndexedTagName( $message['params'], 'p' );
+                               }
+                       } else {
+                               $message = array( 'message' => $m['message'] );
+                               $msg = wfMessage( $m['message'] );
+                               if ( isset( $m['params'] ) ) {
+                                       $message['params'] = $m['params'];
+                                       $result->setIndexedTagName( $message['params'], 'p' );
+                                       $msg->params( $m['params'] );
+                               }
+                       }
+                       $message['rendered'] = $msg->useDatabase( false )->inLanguage( 'en' )->plain();
+                       $ret[] = $message;
+               }
+               return $ret;
+       }
+
+       public function mustBePosted() {
+               return true;
+       }
+
+       public function isWriteMode() {
+               return true;
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'type' => array(
+                               ApiBase::PARAM_TYPE => RevisionDeleter::getTypes(),
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'target' => null,
+                       'ids' => array(
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'hide' => array(
+                               ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
+                               ApiBase::PARAM_ISMULTI => true,
+                       ),
+                       'show' => array(
+                               ApiBase::PARAM_TYPE => array( 'content', 'comment', 'user' ),
+                               ApiBase::PARAM_ISMULTI => true,
+                       ),
+                       'suppress' => array(
+                               ApiBase::PARAM_TYPE => array( 'yes', 'no', 'nochange' ),
+                               ApiBase::PARAM_DFLT => 'nochange',
+                       ),
+                       'token' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'reason' => null,
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'type' => 'Type of revision deletion being performed',
+                       'target' => 'Page title for the revision deletion, if required for the type',
+                       'ids' => 'Identifiers for the revisions to be deleted',
+                       'hide' => 'What to hide for each revision',
+                       'show' => 'What to unhide for each revision',
+                       'suppress' => 'Whether to suppress data from administrators as well as others',
+                       'token' => 'A delete token previously retrieved through action=tokens',
+                       'reason' => 'Reason for the deletion/undeletion',
+               );
+       }
+
+       public function getDescription() {
+               return 'Delete/undelete revisions';
+       }
+
+       public function getPossibleErrors() {
+               return array_merge( parent::getPossibleErrors(),
+                       array(
+                               'needtarget' => 'A target title is required for this RevDel type',
+                               'badparams' => 'Bad value for some parameter',
+                       )
+               );
+       }
+
+       public function needsToken() {
+               return true;
+       }
+
+       public function getTokenSalt() {
+               return '';
+       }
+
+       public function getExamples() {
+               return array(
+                       'api.php?action=revisiondelete&target=Main%20Page&type=revision&ids=12345&hide=content&token=123ABC'
+                       => 'Hide content for revision 12345 on the Main Page',
+                       'api.php?action=revisiondelete&type=logging&ids=67890&hide=content|comment|user&reason=BLP%20violation&token=123ABC'
+                       => 'Hide all data on log entry 67890 with the reason "BLP violation"',
+               );
+       }
+
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Revisiondelete';
+       }
+}
index 0c887e4..0509008 100644 (file)
@@ -279,6 +279,30 @@ class RevDel_RevisionItem extends RevDel_Item {
                }
                return "<li>$difflink $revlink $userlink $comment</li>";
        }
+
+       public function getApiData( ApiResult $result ) {
+               $rev = $this->revision;
+               $user = $this->list->getUser();
+               $ret = array(
+                       'id' => $rev->getId(),
+                       'timestamp' => wfTimestamp( TS_ISO_8601, $rev->getTimestamp() ),
+               );
+               $ret += $rev->isDeleted( Revision::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
+               $ret += $rev->isDeleted( Revision::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
+               $ret += $rev->isDeleted( Revision::DELETED_TEXT ) ? array( 'texthidden' => '' ) : array();
+               if ( $rev->userCan( Revision::DELETED_USER, $user ) ) {
+                       $ret += array(
+                               'userid' => $rev->getUser( Revision::FOR_THIS_USER ),
+                               'user' => $rev->getUserText( Revision::FOR_THIS_USER ),
+                       );
+               }
+               if ( $rev->userCan( Revision::DELETED_COMMENT, $user ) ) {
+                       $ret += array(
+                               'comment' => $rev->getComment( Revision::FOR_THIS_USER ),
+                       );
+               }
+               return $ret;
+       }
 }
 
 /**
@@ -708,6 +732,50 @@ class RevDel_FileItem extends RevDel_Item {
                return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
                        $data . ' ' . $this->getComment() . '</li>';
        }
+
+       public function getApiData( ApiResult $result ) {
+               $file = $this->file;
+               $user = $this->list->getUser();
+               $ret = array(
+                       'title' => $this->list->title->getPrefixedText(),
+                       'archivename' => $file->getArchiveName(),
+                       'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
+                       'width' => $file->getWidth(),
+                       'height' => $file->getHeight(),
+                       'size' => $file->getSize(),
+               );
+               $ret += $file->isDeleted( Revision::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
+               $ret += $file->isDeleted( Revision::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
+               $ret += $this->isDeleted() ? array( 'contenthidden' => '' ) : array();
+               if ( !$this->isDeleted() ) {
+                       $ret += array(
+                               'url' => $file->getUrl(),
+                       );
+               } elseif ( $this->canViewContent() ) {
+                       $ret += array(
+                               'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
+                                       array(
+                                               'target' => $this->list->title->getPrefixedText(),
+                                               'file' => $file->getArchiveName(),
+                                               'token' => $user->getEditToken( $file->getArchiveName() )
+                                       ),
+                                       false, PROTO_RELATIVE
+                               ),
+                       );
+               }
+               if ( $file->userCan( Revision::DELETED_USER, $user ) ) {
+                       $ret += array(
+                               'userid' => $file->user,
+                               'user' => $file->user_text,
+                       );
+               }
+               if ( $file->userCan( Revision::DELETED_COMMENT, $user ) ) {
+                       $ret += array(
+                               'comment' => $file->description,
+                       );
+               }
+               return $ret;
+       }
 }
 
 /**
@@ -962,4 +1030,41 @@ class RevDel_LogItem extends RevDel_Item {
 
                return "<li>$loglink $date $action $comment</li>";
        }
+
+       public function getApiData( ApiResult $result ) {
+               $logEntry = DatabaseLogEntry::newFromRow( $this->row );
+               $user = $this->list->getUser();
+               $ret = array(
+                       'id' => $logEntry->getId(),
+                       'type' => $logEntry->getType(),
+                       'action' => $logEntry->getSubtype(),
+               );
+               $ret += $logEntry->isDeleted( LogPage::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
+               $ret += $logEntry->isDeleted( LogPage::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
+               $ret += $logEntry->isDeleted( LogPage::DELETED_ACTION ) ? array( 'actionhidden' => '' ) : array();
+
+               if ( LogEventsList::userCan( $this->row, LogPage::DELETED_ACTION, $user ) ) {
+                       ApiQueryLogEvents::addLogParams(
+                               $result,
+                               $ret,
+                               $logEntry->getParameters(),
+                               $logEntry->getType(),
+                               $logEntry->getSubtype(),
+                               $logEntry->getTimestamp(),
+                               $logEntry->isLegacy()
+                       );
+               }
+               if ( LogEventsList::userCan( $this->row, LogPage::DELETED_USER, $user ) ) {
+                       $ret += array(
+                               'userid' => $this->row->log_user,
+                               'user' => $this->row->log_user_text,
+                       );
+               }
+               if ( LogEventsList::userCan( $this->row, LogPage::DELETED_COMMENT, $user ) ) {
+                       $ret += array(
+                               'comment' => $this->row->log_comment,
+                       );
+               }
+               return $ret;
+       }
 }
index 803467e..3874602 100644 (file)
@@ -80,13 +80,16 @@ abstract class RevDel_List extends RevisionListBase {
         * transactions are done here.
         *
         * @param array $params Associative array of parameters. Members are:
-        *     value:       The integer value to set the visibility to
-        *     comment:     The log comment.
+        *     value:         The integer value to set the visibility to
+        *     comment:       The log comment.
+        *     perItemStatus: Set if you want per-item status reports
         * @return Status
+        * @since 1.23 Added 'perItemStatus' param
         */
        public function setVisibility( $params ) {
                $bitPars = $params['value'];
                $comment = $params['comment'];
+               $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
 
                $this->res = false;
                $dbw = wfGetDB( DB_MASTER );
@@ -98,16 +101,27 @@ abstract class RevDel_List extends RevisionListBase {
                $idsForLog = array();
                $authorIds = $authorIPs = array();
 
+               if ( $perItemStatus ) {
+                       $status->itemStatuses = array();
+               }
+
                for ( $this->reset(); $this->current(); $this->next() ) {
                        $item = $this->current();
                        unset( $missing[$item->getId()] );
 
+                       if ( $perItemStatus ) {
+                               $itemStatus = Status::newGood();
+                               $status->itemStatuses[$item->getId()] = $itemStatus;
+                       } else {
+                               $itemStatus = $status;
+                       }
+
                        $oldBits = $item->getBits();
                        // Build the actual new rev_deleted bitfield
                        $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
 
                        if ( $oldBits == $newBits ) {
-                               $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
+                               $itemStatus->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
                                $status->failCount++;
                                continue;
                        } elseif ( $oldBits == 0 && $newBits != 0 ) {
@@ -120,7 +134,7 @@ abstract class RevDel_List extends RevisionListBase {
 
                        if ( $item->isHideCurrentOp( $newBits ) ) {
                                // Cannot hide current version text
-                               $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
+                               $itemStatus->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
                                $status->failCount++;
                                continue;
                        }
@@ -128,13 +142,13 @@ abstract class RevDel_List extends RevisionListBase {
                                // Cannot access this revision
                                $msg = ( $opType == 'show' ) ?
                                        'revdelete-show-no-access' : 'revdelete-modify-no-access';
-                               $status->error( $msg, $item->formatDate(), $item->formatTime() );
+                               $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
                                $status->failCount++;
                                continue;
                        }
                        // Cannot just "hide from Sysops" without hiding any fields
                        if ( $newBits == Revision::DELETED_RESTRICTED ) {
-                               $status->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
+                               $itemStatus->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
                                $status->failCount++;
                                continue;
                        }
@@ -151,19 +165,22 @@ abstract class RevDel_List extends RevisionListBase {
                                        $authorIPs[] = $item->getAuthorName();
                                }
                        } else {
-                               $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
+                               $itemStatus->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
                                $status->failCount++;
                        }
                }
 
                // Handle missing revisions
                foreach ( $missing as $id => $unused ) {
-                       $status->error( 'revdelete-modify-missing', $id );
+                       if ( $perItemStatus ) {
+                               $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
+                       } else {
+                               $status->error( 'revdelete-modify-missing', $id );
+                       }
                        $status->failCount++;
                }
 
                if ( $status->successCount == 0 ) {
-                       $status->ok = false;
                        $dbw->rollback( __METHOD__ );
                        return $status;
                }
@@ -325,4 +342,12 @@ abstract class RevDel_Item extends RevisionItemBase {
         * @return boolean success
         */
        abstract public function setBits( $newBits );
+
+       /**
+        * Get the return information about the revision for the API
+        * @since 1.23
+        * @param ApiResult $result API result object
+        * @return array Data for the API result
+        */
+       abstract public function getApiData( ApiResult $result );
 }