Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
index de324e4..121cb48 100644 (file)
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiBase.php" );
-}
-
 /**
  * API module that facilitates deleting pages. The API equivalent of action=delete.
  * Requires API write mode to be enabled.
@@ -51,85 +46,62 @@ class ApiDelete extends ApiBase {
        public function execute() {
                $params = $this->extractRequestParams();
 
-               $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
-
-               if ( isset( $params['title'] ) ) {
-                       $titleObj = Title::newFromText( $params['title'] );
-                       if ( !$titleObj ) {
-                               $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
-                       }
-               } elseif ( isset( $params['pageid'] ) ) {
-                       $titleObj = Title::newFromID( $params['pageid'] );
-                       if ( !$titleObj ) {
-                               $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
-                       }
-               }
-               if ( !$titleObj->exists() ) {
+               $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
+               if ( !$pageObj->exists() ) {
                        $this->dieUsageMsg( 'notanarticle' );
                }
 
+               $titleObj = $pageObj->getTitle();
                $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
+               $user = $this->getUser();
+
                if ( $titleObj->getNamespace() == NS_FILE ) {
-                       $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
-                       if ( count( $retval ) ) {
-                               $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
-                       }
+                       $retval = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
                } else {
-                       $articleObj = new Article( $titleObj );
-                       $retval = self::delete( $articleObj, $params['token'], $reason );
+                       $retval = self::delete( $pageObj, $user, $params['token'], $reason );
+               }
 
-                       if ( count( $retval ) ) {
-                               $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
-                       }
+               if ( count( $retval ) ) {
+                       $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
+               }
 
-                       // Deprecated parameters
-                       if ( $params['watch'] ) {
-                               $watch = 'watch';
-                       } elseif ( $params['unwatch'] ) {
-                               $watch = 'unwatch';
-                       } else {
-                               $watch = $params['watchlist'];
-                       }
-                       $this->setWatch( $watch, $titleObj, 'watchdeletion' );
+               // Deprecated parameters
+               if ( $params['watch'] ) {
+                       $watch = 'watch';
+               } elseif ( $params['unwatch'] ) {
+                       $watch = 'unwatch';
+               } else {
+                       $watch = $params['watchlist'];
                }
+               $this->setWatch( $watch, $titleObj, 'watchdeletion' );
 
                $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
                $this->getResult()->addValue( null, $this->getModuleName(), $r );
        }
 
        /**
-        *
-        * @param &$title Title
+        * @param $title Title
+        * @param $user User doing the action
         * @param $token String
+        * @return array
         */
-       private static function getPermissionsError( &$title, $token ) {
-               global $wgUser;
-
+       private static function getPermissionsError( $title, $user, $token ) {
                // Check permissions
-               $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
-               if ( count( $errors ) > 0 ) {
-                       return $errors;
-               }
-
-               return array();
+               return $title->getUserPermissionsErrors( 'delete', $user );
        }
 
        /**
         * We have our own delete() function, since Article.php's implementation is split in two phases
         *
-        * @param $article Article object to work on
+        * @param $page WikiPage object to work on
+        * @param $user User doing the action
         * @param $token String: delete token (same as edit token)
         * @param $reason String: reason for the deletion. Autogenerated if NULL
         * @return Title::getUserPermissionsErrors()-like array
         */
-       public static function delete( &$article, $token, &$reason = null ) {
-               global $wgUser;
-               if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
-                       global $wgDeleteRevisionsLimit;
-                       return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
-               }
-               $title = $article->getTitle();
-               $errors = self::getPermissionsError( $title, $token );
+       public static function delete( Page $page, User $user, $token, &$reason = null ) {
+               $title = $page->getTitle();
+               $errors = self::getPermissionsError( $title, $user, $token );
                if ( count( $errors ) ) {
                        return $errors;
                }
@@ -139,54 +111,58 @@ class ApiDelete extends ApiBase {
                        // Need to pass a throwaway variable because generateReason expects
                        // a reference
                        $hasHistory = false;
-                       $reason = $article->generateReason( $hasHistory );
+                       $reason = $page->getAutoDeleteReason( $hasHistory ); #FIXME: use ContentHandler::getAutoDeleteReason()
                        if ( $reason === false ) {
-                               return array( array( 'cannotdelete' ) );
+                               return array( array( 'cannotdelete', $title->getPrefixedText() ) );
                        }
                }
 
                $error = '';
                // Luckily, Article.php provides a reusable delete function that does the hard work for us
-               if ( $article->doDeleteArticle( $reason, false, 0, true, $error ) ) {
+               if ( $page->doDeleteArticle( $reason, false, 0, true, $error ) ) {
                        return array();
                } else {
-                       return array( array( 'cannotdelete', $article->getTitle()->getPrefixedText() ) );
+                       return array( array( 'cannotdelete', $title->getPrefixedText() ) );
                }
        }
 
        /**
+        * @param $page WikiPage object to work on
+        * @param $user User doing the action
         * @param $token
-        * @param $title Title
         * @param $oldimage
         * @param $reason
         * @param $suppress bool
-        * @return \type|array|Title
+        * @return array|Title
         */
-       public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
-               $errors = self::getPermissionsError( $title, $token );
+       public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) {
+               $title = $page->getTitle();
+               $errors = self::getPermissionsError( $title, $user, $token );
                if ( count( $errors ) ) {
                        return $errors;
                }
 
-               if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
-                       return array( array( 'invalidoldimage' ) );
+               $file = $page->getFile();
+               if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
+                       return self::delete( $page, $user, $token, $reason );
                }
 
-               $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
-               $oldfile = false;
-
                if ( $oldimage ) {
+                       if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
+                               return array( array( 'invalidoldimage' ) );
+                       }
                        $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
+                       if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
+                               return array( array( 'nodeleteablefile' ) );
+                       }
+               } else {
+                       $oldfile = false;
                }
 
-               if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
-                       return self::delete( new Article( $title ), $token, $reason );
-               }
                if ( is_null( $reason ) ) { // Log and RC don't like null reasons
                        $reason = '';
                }
                $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
-
                if ( !$status->isGood() ) {
                        return array( array( 'cannotdelete', $title->getPrefixedText() ) );
                }
@@ -251,12 +227,14 @@ class ApiDelete extends ApiBase {
 
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(),
-                       $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
+                       $this->getTitleOrPageIdErrorMessage(),
                        array(
-                               array( 'invalidtitle', 'title' ),
-                               array( 'nosuchpageid', 'pageid' ),
                                array( 'notanarticle' ),
                                array( 'hookaborted', 'error' ),
+                               array( 'delete-toobig', 'limit' ),
+                               array( 'cannotdelete', 'title' ),
+                               array( 'invalidoldimage' ),
+                               array( 'nodeleteablefile' ),
                        )
                );
        }
@@ -271,13 +249,13 @@ class ApiDelete extends ApiBase {
 
        public function getExamples() {
                return array(
-                       'api.php?action=delete&title=Main%20Page&token=123ABC',
-                       'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
+                       'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
+                       'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"',
                );
        }
 
        public function getHelpUrls() {
-               return 'http://www.mediawiki.org/wiki/API:Delete';
+               return 'https://www.mediawiki.org/wiki/API:Delete';
        }
 
        public function getVersion() {