API: Allow deleting files without corresponding pages
authorBrad Jorsch <bjorsch@wikimedia.org>
Mon, 12 Jun 2017 17:09:26 +0000 (13:09 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Mon, 12 Jun 2017 17:11:23 +0000 (13:11 -0400)
Such a deletion doesn't currently produce a log entry, but that's a
different bug that also occurs via the web UI.

Bug: T167693
Change-Id: If6e751aa28960243db49ac9b81fe518edba11bd5

includes/api/ApiDelete.php

index 99065c4..72bbe00 100644 (file)
@@ -44,11 +44,13 @@ class ApiDelete extends ApiBase {
                $params = $this->extractRequestParams();
 
                $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
-               if ( !$pageObj->exists() ) {
+               $titleObj = $pageObj->getTitle();
+               if ( !$pageObj->exists() &&
+                       !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
+               ) {
                        $this->dieWithError( 'apierror-missingtitle' );
                }
 
-               $titleObj = $pageObj->getTitle();
                $reason = $params['reason'];
                $user = $this->getUser();
 
@@ -128,6 +130,14 @@ class ApiDelete extends ApiBase {
                return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
        }
 
+       /**
+        * @param File $file
+        * @return bool
+        */
+       protected static function canDeleteFile( File $file ) {
+               return $file->exists() && $file->isLocal() && !$file->getRedirected();
+       }
+
        /**
         * @param Page $page Object to work on
         * @param User $user User doing the action
@@ -143,7 +153,7 @@ class ApiDelete extends ApiBase {
                $title = $page->getTitle();
 
                $file = $page->getFile();
-               if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
+               if ( !self::canDeleteFile( $file ) ) {
                        return self::delete( $page, $user, $reason, $tags );
                }