r23627 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23626‎ | r23627 | r23628 >
Date:06:02, 2 July 2007
Author:yurik
Status:old
Tags:
Comment:
API: minor cleanup to bring changes to maximum match with the trunk to simplify diffing.
Introduced class constants instead of the defines, and some renaming.
Modified paths:
  • /branches/apiedit/phase3/includes/Article.php (modified) (history)
  • /branches/apiedit/phase3/includes/Defines.php (modified) (history)
  • /branches/apiedit/phase3/includes/api/ApiDelete.php (modified) (history)
  • /branches/apiedit/phase3/includes/api/ApiRollback.php (modified) (history)

Diff [purge]

Index: branches/apiedit/phase3/includes/Article.php
@@ -37,6 +37,19 @@
3838 /**@}}*/
3939
4040 /**
 41+ * Constants used by internal components to get rollback results
 42+ */
 43+ const SUCCESS = 0;
 44+ const PERM_DENIED = 1; // Permission denied
 45+ const BLOCKED = 2; // User has been blocked
 46+ const READONLY = 3; // Wiki is in read-only mode
 47+ const BAD_TOKEN = 4; // Invalid token specified
 48+ const BAD_TITLE = 5; // $this is not a valid Article
 49+ const ALREADYROLLED = 6;// Someone else already rolled this back. $info['usertext'] and $info['comment'] will be set
 50+ const ONLY_AUTHOR = 7; // User is the only author of the page
 51+ const EDIT_FAILED = 8; // Article::doEdit() failed. This is a very weird error
 52+
 53+ /**
4154 * Constructor and clear the article
4255 * @param $title Reference to a Title object.
4356 * @param $oldId Integer revision ID, null to fetch from request, zero for current
@@ -2161,34 +2174,40 @@
21622175 * @param bool $bot - If true, mark all reverted edits as bot.
21632176 * @param string $summary - Custom summary. Set to default summary if empty.
21642177 * @param array $info - Reference to associative array that will be set to contain the revision ID, edit summary, etc.
2165 - * @return ROLLBACK_SUCCES on succes, ROLLBACK_* on failure
 2178+ * @return self::SUCCESS on succes, self::* on failure
21662179 */
2167 - public function doRollback($user, $token, $bot = false, $summary = "", &$info = NULL)
2168 - {
 2180+ public function doRollback($user, $token, $bot = false, $summary = "", &$info = NULL) {
21692181 global $wgUser, $wgUseRCPatrol;
2170 - if(!$wgUser->isAllowed('rollback'))
2171 - return ROLLBACK_PERM;
2172 - if($wgUser->isBlocked())
2173 - return ROLLBACK_BLOCKED;
2174 - if(wfReadOnly())
2175 - return ROLLBACK_READONLY;
 2182+
 2183+ if(!$wgUser->isAllowed('rollback')) {
 2184+ return self::PERM_DENIED;
 2185+ }
 2186+ if( $wgUser->isBlocked() ) {
 2187+ return self::BLOCKED;
 2188+ }
 2189+ if ( wfReadOnly() ) {
 2190+ return self::READONLY;
 2191+ }
21762192
21772193 // Check token first
2178 - if(!$wgUser->matchEditToken($token, array($this->mTitle->getPrefixedText(), $user)))
2179 - return ROLLBACK_BADTOKEN;
 2194+ if( !$wgUser->matchEditToken( $token,
 2195+ array( $this->mTitle->getPrefixedText(),
 2196+ $user ) ) ) {
 2197+ return self::BAD_TOKEN;
 2198+ }
 2199+ $dbw = wfGetDB( DB_MASTER );
21802200
2181 - $dbw = wfGetDB(DB_MASTER);
21822201 $current = Revision::newFromTitle($this->mTitle);
21832202 if(is_null($current))
2184 - return ROLLBACK_BADARTICLE;
 2203+ return self::BAD_TITLE;
21852204
21862205 // Check if someone else was there first
2187 - if($user != $current->getUserText())
2188 - {
 2206+ if( $user != $current->getUserText() ) {
21892207 $info['usertext'] = $current->getUserText();
21902208 $info['comment'] = $current->getComment();
2191 - return ROLLBACK_ALREADYROLLED;
 2209+ return self::ALREADYROLLED;
21922210 }
 2211+
21932212 // Get the last edit not by $user
21942213 $userid = intval($current->getUser());
21952214 $s = $dbw->selectRow('revision',
@@ -2199,26 +2218,35 @@
22002219 ), __METHOD__,
22012220 array(
22022221 'USE INDEX' => 'page_timestamp',
2203 - 'ORDER BY' => 'rev_timestamp DESC'
2204 - ));
2205 - if($s === false)
2206 - return ROLLBACK_ONLYAUTHOR;
 2222+ 'ORDER BY' => 'rev_timestamp DESC' )
 2223+ );
 2224+ if( $s === false ) {
 2225+ # Something wrong
 2226+ return self::ONLY_AUTHOR;
 2227+ }
 2228+
22072229 $target = Revision::newFromID($s->rev_id);
22082230
22092231 // If the reverted edits should be marked bot or patrolled, do so
22102232 $set = array();
2211 - if($bot)
 2233+ if ( $bot ) {
 2234+ # Mark all reverted edits as bot
22122235 $set['rc_bot'] = 1;
2213 - if($wgUseRCPatrol)
 2236+ }
 2237+ if ( $wgUseRCPatrol ) {
 2238+ # Mark all reverted edits as patrolled
22142239 $set['rc_patrolled'] = 1;
2215 - if($set)
2216 - $dbw->update('recentchanges', $set,
2217 - array(
 2240+ }
 2241+
 2242+ if ( $set ) {
 2243+ $dbw->update( 'recentchanges', $set,
 2244+ array( /* WHERE */
22182245 'rc_cur_id' => $current->getPage(),
22192246 'rc_user_text' => $user,
22202247 "rc_timestamp > '{$s->rev_timestamp}'"
22212248 ), __METHOD__
22222249 );
 2250+ }
22232251
22242252 // Generate an edit summary
22252253 if(empty($summary))
@@ -2229,11 +2257,11 @@
22302258 if($bot)
22312259 $flags |= EDIT_FORCE_BOT;
22322260 if(!$this->doEdit($target->getText(), $summary, $flags))
2233 - return ROLLBACK_EDITFAILED;
 2261+ return self::EDIT_FAILED;
22342262
22352263 if(is_null($info))
22362264 // Save time
2237 - return ROLLBACK_SUCCESS;
 2265+ return self::SUCCESS;
22382266
22392267 $info['title'] = $this->mTitle->getPrefixedText();
22402268 $info['pageid'] = $current->getPage();
@@ -2247,7 +2275,7 @@
22482276 $info['to'] = $target->getUserText(); // The user whose last version was reverted to
22492277 if($bot)
22502278 $info['bot'] = "";
2251 - return ROLLBACK_SUCCESS;
 2279+ return self::SUCCESS;
22522280 }
22532281
22542282 /** UI entry point for rollbacks. Relies on doRollback() to do the hard work */
@@ -2260,31 +2288,31 @@
22612289 $wgRequest->getText('summary'), &$info);
22622290 switch($retval)
22632291 {
2264 - case ROLLBACK_SUCCESS:
2265 - case ROLLBACK_EDITFAILED: // Is ignored
 2292+ case self::SUCCESS:
 2293+ case self::EDIT_FAILED: // Is ignored
22662294 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
22672295 $wgOut->setRobotpolicy( 'noindex,nofollow' );
22682296 $wgOut->addHTML( '<h2>' . htmlspecialchars( $info['summary'] ) . "</h2>\n<hr />\n" );
22692297 $this->doRedirect(true);
22702298 $wgOut->returnToMain(false);
22712299 return;
2272 - case ROLLBACK_PERM:
 2300+ case self::PERM_DENIED:
22732301 $wgOut->permissionRequired('rollback');
22742302 return;
2275 - case ROLLBACK_BLOCKED:
 2303+ case self::BLOCKED:
22762304 $wgOut->blockedPage();
22772305 return;
2278 - case ROLLBACK_READONLY:
 2306+ case self::READONLY:
22792307 $wgOut->readOnlyPage($this->getContent());
22802308 return;
2281 - case ROLLBACK_BADTOKEN:
 2309+ case self::BAD_TOKEN:
22822310 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
22832311 $wgOut->addWikiText(wfMsg('sessionfailure'));
22842312 return;
2285 - case ROLLBACK_BADARTICLE:
 2313+ case self::BAD_TITLE:
22862314 $wgOut->addHTML(wfMsg('notanarticle'));
22872315 return;
2288 - case ROLLBACK_ALREADYROLLED:
 2316+ case self::ALREADYROLLED:
22892317 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
22902318 $wgOut->addWikiText(wfMsg('alreadyrolled',
22912319 htmlspecialchars($this->mTitle->getPrefixedText()),
@@ -2294,7 +2322,7 @@
22952323 $wgOut->addHTML(wfMsg('editcomment',
22962324 $wgUser->getSkin()->formatComment($info['comment'])));
22972325 return;
2298 - case ROLLBACK_ONLYAUTHOR:
 2326+ case self::ONLY_AUTHOR:
22992327 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
23002328 $wgOut->addHTML(wfMsg('cantrollback'));
23012329 return;
Index: branches/apiedit/phase3/includes/api/ApiRollback.php
@@ -67,29 +67,29 @@
6868
6969 switch($retval)
7070 {
71 - case ROLLBACK_SUCCESS:
 71+ case Article::SUCCESS:
7272 break; // We'll deal with that later
73 - case ROLLBACK_PERM:
74 - $this->dieUsage('You don\'t have permission to rollback', 'permissiondenied');
75 - case ROLLBACK_BLOCKED: // If we get BLOCKED or PERM that's very weird, but it's possible
 73+ case Article::PERM_DENIED:
 74+ $this->dieUsage("You don't have permission to rollback", 'permissiondenied');
 75+ case Article::BLOCKED: // If we get BLOCKED or PERM_DENIED that's very weird, but it's possible
7676 $this->dieUsage('You have been blocked from editing', 'blocked');
77 - case ROLLBACK_READONLY:
 77+ case Article::READONLY:
7878 $this->dieUsage('The wiki is in read-only mode', 'readonly');
79 - case ROLLBACK_BADTOKEN:
 79+ case Article::BAD_TOKEN:
8080 $this->dieUsage('Invalid token', 'badtoken');
81 - case ROLLBACK_BADARTICLE:
 81+ case Article::BAD_TITLE:
8282 $this->dieUsage("The article ``{$params['title']}'' doesn't exist", 'missingtitle');
83 - case ROLLBACK_ALREADYROLLED:
 83+ case Article::ALREADYROLLED:
8484 $this->dieUsage('The edit(s) you tried to rollback is/are already rolled back', 'alreadyrolled');
85 - case ROLLBACK_ONLYAUTHOR:
 85+ case Article::ONLY_AUTHOR:
8686 $this->dieUsage("{$params['user']} is the only author of the page", 'onlyauthor');
87 - case ROLLBACK_EDITFAILED:
 87+ case Article::EDIT_FAILED:
8888 $this->dieDebug(__METHOD__, 'Article::doEdit() failed');
8989 default:
9090 // rollback() has apparently invented a new error, which is extremely weird
9191 $this->dieDebug(__METHOD__, "rollback() returned an unknown error ($retval)");
9292 }
93 - // $retval has to be ROLLBACK_SUCCESS if we get here
 93+ // $retval has to be Article::SUCCESS if we get here
9494 $this->getResult()->addValue(null, 'rollback', $info);
9595 }
9696
Index: branches/apiedit/phase3/includes/api/ApiDelete.php
@@ -125,7 +125,7 @@
126126 $this->dieUsage('The wiki is in read-only mode', 'readonly');
127127 case DELETE_BADTOKEN:
128128 $this->dieUsage('Invalid token', 'badtoken');
129 - case ROLLBACK_BADARTICLE:
 129+ case Article::RB_BADARTICLE:
130130 $this->dieUsage("The article ``{$params['title']}'' doesn't exist or has already been deleted", 'missingtitle');
131131 default:
132132 // delete() has apparently invented a new error, which is extremely weird
Index: branches/apiedit/phase3/includes/Defines.php
@@ -195,19 +195,6 @@
196196 define( 'EDIT_AUTOSUMMARY', 64 );
197197 /**#@-*/
198198
199 -/**#@+
200 - * Article::doRollback() return values
201 - */
202 -define('ROLLBACK_SUCCES', 0);
203 -define('ROLLBACK_PERM', 1); // Permission denied
204 -define('ROLLBACK_BLOCKED', 2); // User has been blocked
205 -define('ROLLBACK_READONLY', 3); // Wiki is in read-only mode
206 -define('ROLLBACK_BADTOKEN', 4); // Invalid token specified
207 -define('ROLLBACK_BADARTICLE', 5); // $this is not a valid Article
208 -define('ROLLBACK_ALREADYROLLED', 6); // Someone else already rolled this back. $info['usertext'] and $info['comment'] will be set
209 -define('ROLLBACK_ONLYAUTHOR', 7); // User is the only author of the page
210 -define('ROLLBACK_EDITFAILED', 8); // Article::doEdit() failed. This is a very weird error
211 -
212199 /**
213200 * Flags for Database::makeList()
214201 * These are also available as Database class constants

Status & tagging log