r103332 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103331‎ | r103332 | r103333 >
Date:15:57, 16 November 2011
Author:ialex
Status:ok (Comments)
Tags:
Comment:
* Use WikiPage instead of Article
* Create the WikiPage object directly in execute() and pass it to both delete() and deleteFile()
* Reordered parameters of delete() and deleteFile() to be consistent and take both on the WikiPage and User objects (instead of relying on $wgUser); no callers outside of this class
* Fixed deleteFile() to match the behaviour of FileDeleteForm with oldimage parameter
* Pass the User object to getPermissionsError()
* Factorised duplicated code
* Added missing error code to ApiBase (and also a new one)
* Added missing possible error codes (with a new one too)
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiDelete.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiDelete.php
@@ -64,29 +64,28 @@
6565 }
6666
6767 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
 68+ $pageObj = WikiPage::factory( $titleObj );
 69+ $user = $this->getUser();
 70+
6871 if ( $titleObj->getNamespace() == NS_FILE ) {
69 - $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
70 - if ( count( $retval ) ) {
71 - $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
72 - }
 72+ $retval = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
7373 } else {
74 - $articleObj = new Article( $titleObj );
75 - $retval = self::delete( $articleObj, $params['token'], $reason );
 74+ $retval = self::delete( $pageObj, $user, $params['token'], $reason );
 75+ }
7676
77 - if ( count( $retval ) ) {
78 - $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
79 - }
 77+ if ( count( $retval ) ) {
 78+ $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
 79+ }
8080
81 - // Deprecated parameters
82 - if ( $params['watch'] ) {
83 - $watch = 'watch';
84 - } elseif ( $params['unwatch'] ) {
85 - $watch = 'unwatch';
86 - } else {
87 - $watch = $params['watchlist'];
88 - }
89 - $this->setWatch( $watch, $titleObj, 'watchdeletion' );
 81+ // Deprecated parameters
 82+ if ( $params['watch'] ) {
 83+ $watch = 'watch';
 84+ } elseif ( $params['unwatch'] ) {
 85+ $watch = 'unwatch';
 86+ } else {
 87+ $watch = $params['watchlist'];
9088 }
 89+ $this->setWatch( $watch, $titleObj, 'watchdeletion' );
9190
9291 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
9392 $this->getResult()->addValue( null, $this->getModuleName(), $r );
@@ -94,32 +93,32 @@
9594
9695 /**
9796 * @param $title Title
 97+ * @param $user User doing the action
9898 * @param $token String
9999 * @return array
100100 */
101 - private static function getPermissionsError( &$title, $token ) {
102 - global $wgUser;
103 -
 101+ private static function getPermissionsError( $title, $user, $token ) {
104102 // Check permissions
105 - return $title->getUserPermissionsErrors( 'delete', $wgUser );
 103+ return $title->getUserPermissionsErrors( 'delete', $user );
106104 }
107105
108106 /**
109107 * We have our own delete() function, since Article.php's implementation is split in two phases
110108 *
111 - * @param $article Article object to work on
 109+ * @param $page WikiPage object to work on
 110+ * @param $user User doing the action
112111 * @param $token String: delete token (same as edit token)
113112 * @param $reason String: reason for the deletion. Autogenerated if NULL
114113 * @return Title::getUserPermissionsErrors()-like array
115114 */
116 - public static function delete( &$article, $token, &$reason = null ) {
117 - global $wgUser;
118 - if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
 115+ public static function delete( Page $page, User $user, $token, &$reason = null ) {
 116+ if ( $page->isBigDeletion() && !$user->isAllowed( 'bigdelete' ) ) {
119117 global $wgDeleteRevisionsLimit;
120118 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
121119 }
122 - $title = $article->getTitle();
123 - $errors = self::getPermissionsError( $title, $token );
 120+
 121+ $title = $page->getTitle();
 122+ $errors = self::getPermissionsError( $title, $user, $token );
124123 if ( count( $errors ) ) {
125124 return $errors;
126125 }
@@ -129,54 +128,58 @@
130129 // Need to pass a throwaway variable because generateReason expects
131130 // a reference
132131 $hasHistory = false;
133 - $reason = $article->generateReason( $hasHistory );
 132+ $reason = $page->getAutoDeleteReason( $hasHistory );
134133 if ( $reason === false ) {
135 - return array( array( 'cannotdelete' ) );
 134+ return array( array( 'cannotdelete', $title->getPrefixedText() ) );
136135 }
137136 }
138137
139138 $error = '';
140139 // Luckily, Article.php provides a reusable delete function that does the hard work for us
141 - if ( $article->doDeleteArticle( $reason, false, 0, true, $error ) ) {
 140+ if ( $page->doDeleteArticle( $reason, false, 0, true, $error ) ) {
142141 return array();
143142 } else {
144 - return array( array( 'cannotdelete', $article->getTitle()->getPrefixedText() ) );
 143+ return array( array( 'cannotdelete', $title->getPrefixedText() ) );
145144 }
146145 }
147146
148147 /**
 148+ * @param $page WikiPage object to work on
 149+ * @param $user User doing the action
149150 * @param $token
150 - * @param $title Title
151151 * @param $oldimage
152152 * @param $reason
153153 * @param $suppress bool
154154 * @return \type|array|Title
155155 */
156 - public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
157 - $errors = self::getPermissionsError( $title, $token );
 156+ public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) {
 157+ $title = $page->getTitle();
 158+ $errors = self::getPermissionsError( $title, $user, $token );
158159 if ( count( $errors ) ) {
159160 return $errors;
160161 }
161162
162 - if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
163 - return array( array( 'invalidoldimage' ) );
 163+ $file = $page->getFile();
 164+ if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
 165+ return self::delete( $page, $user, $token, $reason );
164166 }
165167
166 - $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
167 - $oldfile = false;
168 -
169168 if ( $oldimage ) {
 169+ if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
 170+ return array( array( 'invalidoldimage' ) );
 171+ }
170172 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
 173+ if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
 174+ return array( array( 'nodeleteablefile' ) );
 175+ }
 176+ } else {
 177+ $oldfile = false;
171178 }
172179
173 - if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
174 - return self::delete( new Article( $title ), $token, $reason );
175 - }
176180 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
177181 $reason = '';
178182 }
179183 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
180 -
181184 if ( !$status->isGood() ) {
182185 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
183186 }
@@ -247,6 +250,10 @@
248251 array( 'nosuchpageid', 'pageid' ),
249252 array( 'notanarticle' ),
250253 array( 'hookaborted', 'error' ),
 254+ array( 'delete-toobig', 'limit' ),
 255+ array( 'cannotdelete', 'title' ),
 256+ array( 'invalidoldimage' ),
 257+ array( 'nodeleteablefile' ),
251258 )
252259 );
253260 }
Index: trunk/phase3/includes/api/ApiBase.php
@@ -1148,6 +1148,8 @@
11491149 'mustbeposted' => array( 'code' => 'mustbeposted', 'info' => "The \$1 module requires a POST request" ),
11501150 'show' => array( 'code' => 'show', 'info' => 'Incorrect parameter - mutually exclusive values may not be supplied' ),
11511151 'specialpage-cantexecute' => array( 'code' => 'specialpage-cantexecute', 'info' => "You don't have permission to view the results of this special page" ),
 1152+ 'invalidoldimage' => array( 'code' => 'invalidoldimage', 'info' => 'The oldid parameter has invalid format' ),
 1153+ 'nodeleteablefile' => array( 'code' => 'nodeleteablefile', 'info' => 'No such old version of the file' ),
11521154
11531155 // ApiEditPage messages
11541156 'noimageredirect-anon' => array( 'code' => 'noimageredirect-anon', 'info' => "Anonymous users can't create image redirects" ),

Follow-up revisions

RevisionCommit summaryAuthorDate
r103648Follow-up r103332: fix "invalidoldimage" error message to say "oldimage" inst...ialex08:29, 19 November 2011

Comments

#Comment by Duplicatebug (talk | contribs)   17:59, 17 November 2011

'The oldid parameter has invalid format' should be oldimage parameter

Status & tagging log