Index: trunk/phase3/includes/api/ApiDelete.php |
— | — | @@ -64,29 +64,28 @@ |
65 | 65 | } |
66 | 66 | |
67 | 67 | $reason = ( isset( $params['reason'] ) ? $params['reason'] : null ); |
| 68 | + $pageObj = WikiPage::factory( $titleObj ); |
| 69 | + $user = $this->getUser(); |
| 70 | + |
68 | 71 | 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 ); |
73 | 73 | } else { |
74 | | - $articleObj = new Article( $titleObj ); |
75 | | - $retval = self::delete( $articleObj, $params['token'], $reason ); |
| 74 | + $retval = self::delete( $pageObj, $user, $params['token'], $reason ); |
| 75 | + } |
76 | 76 | |
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 | + } |
80 | 80 | |
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']; |
90 | 88 | } |
| 89 | + $this->setWatch( $watch, $titleObj, 'watchdeletion' ); |
91 | 90 | |
92 | 91 | $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason ); |
93 | 92 | $this->getResult()->addValue( null, $this->getModuleName(), $r ); |
— | — | @@ -94,32 +93,32 @@ |
95 | 94 | |
96 | 95 | /** |
97 | 96 | * @param $title Title |
| 97 | + * @param $user User doing the action |
98 | 98 | * @param $token String |
99 | 99 | * @return array |
100 | 100 | */ |
101 | | - private static function getPermissionsError( &$title, $token ) { |
102 | | - global $wgUser; |
103 | | - |
| 101 | + private static function getPermissionsError( $title, $user, $token ) { |
104 | 102 | // Check permissions |
105 | | - return $title->getUserPermissionsErrors( 'delete', $wgUser ); |
| 103 | + return $title->getUserPermissionsErrors( 'delete', $user ); |
106 | 104 | } |
107 | 105 | |
108 | 106 | /** |
109 | 107 | * We have our own delete() function, since Article.php's implementation is split in two phases |
110 | 108 | * |
111 | | - * @param $article Article object to work on |
| 109 | + * @param $page WikiPage object to work on |
| 110 | + * @param $user User doing the action |
112 | 111 | * @param $token String: delete token (same as edit token) |
113 | 112 | * @param $reason String: reason for the deletion. Autogenerated if NULL |
114 | 113 | * @return Title::getUserPermissionsErrors()-like array |
115 | 114 | */ |
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' ) ) { |
119 | 117 | global $wgDeleteRevisionsLimit; |
120 | 118 | return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) ); |
121 | 119 | } |
122 | | - $title = $article->getTitle(); |
123 | | - $errors = self::getPermissionsError( $title, $token ); |
| 120 | + |
| 121 | + $title = $page->getTitle(); |
| 122 | + $errors = self::getPermissionsError( $title, $user, $token ); |
124 | 123 | if ( count( $errors ) ) { |
125 | 124 | return $errors; |
126 | 125 | } |
— | — | @@ -129,54 +128,58 @@ |
130 | 129 | // Need to pass a throwaway variable because generateReason expects |
131 | 130 | // a reference |
132 | 131 | $hasHistory = false; |
133 | | - $reason = $article->generateReason( $hasHistory ); |
| 132 | + $reason = $page->getAutoDeleteReason( $hasHistory ); |
134 | 133 | if ( $reason === false ) { |
135 | | - return array( array( 'cannotdelete' ) ); |
| 134 | + return array( array( 'cannotdelete', $title->getPrefixedText() ) ); |
136 | 135 | } |
137 | 136 | } |
138 | 137 | |
139 | 138 | $error = ''; |
140 | 139 | // 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 ) ) { |
142 | 141 | return array(); |
143 | 142 | } else { |
144 | | - return array( array( 'cannotdelete', $article->getTitle()->getPrefixedText() ) ); |
| 143 | + return array( array( 'cannotdelete', $title->getPrefixedText() ) ); |
145 | 144 | } |
146 | 145 | } |
147 | 146 | |
148 | 147 | /** |
| 148 | + * @param $page WikiPage object to work on |
| 149 | + * @param $user User doing the action |
149 | 150 | * @param $token |
150 | | - * @param $title Title |
151 | 151 | * @param $oldimage |
152 | 152 | * @param $reason |
153 | 153 | * @param $suppress bool |
154 | 154 | * @return \type|array|Title |
155 | 155 | */ |
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 ); |
158 | 159 | if ( count( $errors ) ) { |
159 | 160 | return $errors; |
160 | 161 | } |
161 | 162 | |
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 ); |
164 | 166 | } |
165 | 167 | |
166 | | - $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) ); |
167 | | - $oldfile = false; |
168 | | - |
169 | 168 | if ( $oldimage ) { |
| 169 | + if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) { |
| 170 | + return array( array( 'invalidoldimage' ) ); |
| 171 | + } |
170 | 172 | $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; |
171 | 178 | } |
172 | 179 | |
173 | | - if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) { |
174 | | - return self::delete( new Article( $title ), $token, $reason ); |
175 | | - } |
176 | 180 | if ( is_null( $reason ) ) { // Log and RC don't like null reasons |
177 | 181 | $reason = ''; |
178 | 182 | } |
179 | 183 | $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress ); |
180 | | - |
181 | 184 | if ( !$status->isGood() ) { |
182 | 185 | return array( array( 'cannotdelete', $title->getPrefixedText() ) ); |
183 | 186 | } |
— | — | @@ -247,6 +250,10 @@ |
248 | 251 | array( 'nosuchpageid', 'pageid' ), |
249 | 252 | array( 'notanarticle' ), |
250 | 253 | array( 'hookaborted', 'error' ), |
| 254 | + array( 'delete-toobig', 'limit' ), |
| 255 | + array( 'cannotdelete', 'title' ), |
| 256 | + array( 'invalidoldimage' ), |
| 257 | + array( 'nodeleteablefile' ), |
251 | 258 | ) |
252 | 259 | ); |
253 | 260 | } |
Index: trunk/phase3/includes/api/ApiBase.php |
— | — | @@ -1148,6 +1148,8 @@ |
1149 | 1149 | 'mustbeposted' => array( 'code' => 'mustbeposted', 'info' => "The \$1 module requires a POST request" ), |
1150 | 1150 | 'show' => array( 'code' => 'show', 'info' => 'Incorrect parameter - mutually exclusive values may not be supplied' ), |
1151 | 1151 | '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' ), |
1152 | 1154 | |
1153 | 1155 | // ApiEditPage messages |
1154 | 1156 | 'noimageredirect-anon' => array( 'code' => 'noimageredirect-anon', 'info' => "Anonymous users can't create image redirects" ), |