From 6c267f14349af583f994f1d1ca764000f5f69e62 Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Sat, 10 Apr 2010 06:11:02 +0000 Subject: [PATCH] =?utf8?q?*=20Clean=20up=20some=20duplicated=20code=20in?= =?utf8?q?=20r64291=20Would've=20like=20to=20refactor=20the=20$wgUser->*Wa?= =?utf8?q?tch=20=E2=80=94=20but=20I'm=20not=20sure=20if=20the=20hooks=20th?= =?utf8?q?at=20come=20along=20with=20$articleObj->*Watch=20are=20ok.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- includes/api/ApiBase.php | 29 +++- includes/api/ApiDelete.php | 20 ++- includes/api/ApiProtect.php | 17 +-- includes/api/ApiRollback.php | 14 +- maintenance/tests/ApiWatchTest.php | 205 +++++++++++++++++++++++++++++ 5 files changed, 246 insertions(+), 39 deletions(-) create mode 100644 maintenance/tests/ApiWatchTest.php diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 7abfb9e162..e90fd8e8b2 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -536,14 +536,19 @@ abstract class ApiBase { } /** - * Handle watchlist settings - */ + * Return true if we're to watch the page, false if not, null if no change. + * @param $watch String Valid values: 'watch', 'unwatch', 'preferences', 'nochange' + * @param $titleObj Title (optional) the page under consideration + * @returns mixed + */ protected function getWatchlistValue ( $watchlist, $titleObj = null ) { switch ( $watchlist ) { case 'watch': return true; + case 'unwatch': return false; + case 'preferences': global $wgUser; if ( isset($titleObj) @@ -553,12 +558,32 @@ abstract class ApiBase { return true; } return null; + case 'nochange': + return null; + default: return null; } } + /** + * Set a watch (or unwatch) based the based on a watchlist parameter. + * @param $watch String Valid values: 'watch', 'unwatch', 'preferences', 'nochange' + * @param $titleObj Title the article's title to change + */ + protected function setWatch ( $watch, $titleObj ) { + $value = $this->getWatchlistValue( $watch, $titleObj ); + if( $value === null ) return; + + $articleObj = new Article( $titleObj ); + if ( $value ) { + $articleObj->doWatch(); + } else { + $articleObj->doUnwatch(); + } + } + /** * Using the settings determine the value for the given parameter * diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php index df16682484..8c6c10e87a 100644 --- a/includes/api/ApiDelete.php +++ b/includes/api/ApiDelete.php @@ -81,23 +81,19 @@ class ApiDelete extends ApiBase { if ( count( $retval ) ) { $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them } - - $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) || $wgUser->getOption( 'watchdeletion' ); + $watch = 'nochange'; // Deprecated parameters if ( $params['watch'] ) { - $watch = true; + $watch = 'watch'; } elseif ( $params['unwatch'] ) { - $watch = false; - } - - if ( $watch !== null ) { - if ( $watch ) { - $articleObj->doWatch(); - } else { - $articleObj->doUnwatch(); - } + $watch = 'unwatch'; + } elseif ( $wgUser->getOption( 'watchdeletion' ) ) { + $watch = 'watch'; + } else { + $watch = $params['watchlist']; } + $this->setWatch( $watch, $titleObj ); } $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason ); diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php index 4e7f1fc3dc..856e4ef07f 100644 --- a/includes/api/ApiProtect.php +++ b/includes/api/ApiProtect.php @@ -113,20 +113,9 @@ class ApiProtect extends ApiBase { $cascade = $params['cascade']; $articleObj = new Article( $titleObj ); - - $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ); - - if ( $params['watch'] ) { - $watch = true; - } - - if ( $watch !== null ) { - if ( $watch ) { - $articleObj->doWatch(); - } else { - $articleObj->doUnwatch(); - } - } + + $watch = $params['watch'] ? 'watch' : $params['watchlist']; + $this->setWatch( $watch, $titleObj ); if ( $titleObj->exists() ) { $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray ); diff --git a/includes/api/ApiRollback.php b/includes/api/ApiRollback.php index b65ecae650..7de4390e9a 100644 --- a/includes/api/ApiRollback.php +++ b/includes/api/ApiRollback.php @@ -35,7 +35,7 @@ class ApiRollback extends ApiBase { public function __construct( $main, $action ) { parent::__construct( $main, $action ); } - + private $mTitleObj = null; public function execute() { @@ -52,16 +52,8 @@ class ApiRollback extends ApiBase { // We don't care about multiple errors, just report one of them $this->dieUsageMsg( reset( $retval ) ); } - - $watch = $this->getWatchlistValue( $params['watchlist'], $this->mTitleObj ); - - if ( $watch !== null) { - if ( $watch ) { - $articleObj->doWatch(); - } else { - $articleObj->doUnwatch(); - } - } + + $this->setWatch( $params['watchlist'], $this->mTitleObj ); $info = array( 'title' => $this->mTitleObj->getPrefixedText(), diff --git a/maintenance/tests/ApiWatchTest.php b/maintenance/tests/ApiWatchTest.php new file mode 100644 index 0000000000..03209fd6ec --- /dev/null +++ b/maintenance/tests/ApiWatchTest.php @@ -0,0 +1,205 @@ +execute(); + + $data[0] = $module->getResultData(); + $data[1] = $req; + $data[2] = $_SESSION; + + return $data; + } + + function testLogin() { + $data = $this->doApiRequest( array( + 'action' => 'login', + 'lgname' => 'WikiSysop', + 'lgpassword' => 'none' ), $data ); + + $this->assertArrayHasKey( "login", $data[0] ); + $this->assertArrayHasKey( "result", $data[0]['login'] ); + $this->assertEquals( "NeedToken", $data[0]['login']['result'] ); + $token = $data[0]['login']['token']; + + $data = $this->doApiRequest( array( + 'action' => 'login', + "lgtoken" => $token, + "lgname" => 'WikiSysop', + "lgpassword" => 'none' ), $data ); + + $this->assertArrayHasKey( "login", $data[0] ); + $this->assertArrayHasKey( "result", $data[0]['login'] ); + $this->assertEquals( "Success", $data[0]['login']['result'] ); + $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] ); + + return $data; + } + + function testGetToken() { + + $data = $this->doApiRequest( array( + 'action' => 'query', + 'titles' => 'Main Page', + 'intoken' => 'edit|delete|protect|move|block|unblock', + 'prop' => 'info'), $data); + + $this->assertArrayHasKey( 'query', $data[0] ); + $this->assertArrayHasKey( 'pages', $data[0]['query'] ); + $key = array_pop( array_keys( $data[0]['query']['pages'] ) ); + + $this->assertArrayHasKey( $key, $data[0]['query']['pages'] ); + $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key]); + + return $data; + } + + /** + * @depends testGetToken + */ + function testWatchEdit( $data ) { + $key = array_pop( array_keys( $data[0]['query']['pages'] ) ); + $pageinfo = $data[0]['query']['pages'][$key]; + + $data = $this->doApiRequest( array( + 'action' => 'edit', + 'title' => 'Main Page', + 'text' => 'new text', + 'token' => $pageinfo['edittoken'], + 'watchlist' => 'watch'), $data); + $this->assertArrayHasKey( 'edit', $data[0] ); + $this->assertArrayHasKey( 'result', $data[0]['edit'] ); + $this->assertEquals( 'Success', $data[0]['edit']['result'] ); + + return $data; + } + + + /** + * @depends testWatchEdit + */ + function testWatchClear( $data ) { + global $wgUser; + $data = $this->doApiRequest( array( + 'action' => 'query', + 'list' => 'watchlist'), $data); + + if(isset($data[0]['query']['watchlist'])) { + $wl = $data[0]['query']['watchlist']; + + foreach($wl as $page) { + $data = $this->doApiRequest( array( + 'action' => 'watch', + 'title' => $page['title'], + 'unwatch' => true), $data); + } + } + $data = $this->doApiRequest( array( + 'action' => 'query', + 'list' => 'watchlist'), $data); + $this->assertArrayHasKey( 'query', $data[0] ); + $this->assertArrayHasKey( 'watchlist', $data[0]['query'] ); + $this->assertEquals( 0, count($data[0]['query']['watchlist']) ); + + return $data; + } + + /** + * @depends testGetToken + */ + function testWatchProtect( $data ) { + $key = array_pop( array_keys( $data[0]['query']['pages'] ) ); + $pageinfo = $data[0]['query']['pages'][$key]; + + $data = $this->doApiRequest( array( + 'action' => 'protect', + 'token' => $pageinfo['protecttoken'], + 'title' => 'Main Page', + 'protections' => 'edit=sysop', + 'watchlist' => 'unwatch'), $data); + + $this->assertArrayHasKey( 'protect', $data[0] ); + $this->assertArrayHasKey( 'protections', $data[0]['protect'] ); + $this->assertEquals( 1, count($data[0]['protect']['protections']) ); + $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] ); + } + + /** + * @depends testGetToken + */ + function testGetRollbackToken( $data ) { + $data = $this->doApiRequest( array( + 'action' => 'query', + 'prop' => 'revisions', + 'titles' => 'Main Page', + 'rvtoken' => 'rollback'), $data); + + $this->assertArrayHasKey( 'query', $data[0] ); + $this->assertArrayHasKey( 'pages', $data[0]['query'] ); + $key = array_pop( array_keys( $data[0]['query']['pages'] ) ); + + $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 'revisions', $data[0]['query']['pages'][$key]); + $this->assertArrayHasKey( 0, $data[0]['query']['pages'][$key]['revisions'] ); + $this->assertArrayHasKey( 'rollbacktoken', $data[0]['query']['pages'][$key]['revisions'][0] ); + + return $data; + } + + /** + * @depends testGetRollbackToken + */ + function testWatchRollback( $data ) { + $key = array_pop( array_keys( $data[0]['query']['pages'] ) ); + $pageinfo = $data[0]['query']['pages'][$key]['revisions'][0]; + + $data = $this->doApiRequest( array( + 'action' => 'rollback', + 'title' => 'Main Page', + 'user' => 'WikiSysop', + 'token' => $pageinfo['rollbacktoken'], + 'watchlist' => 'watch'), $data); + + $this->assertArrayHasKey( 'rollback', $data[0] ); + $this->assertArrayHasKey( 'title', $data[0]['rollback'] ); + } + + /** + * @depends testGetToken + */ + function testWatchDelete( $data ) { + $key = array_pop( array_keys( $data[0]['query']['pages'] ) ); + $pageinfo = $data[0]['query']['pages'][$key]; + + $data = $this->doApiRequest( array( + 'action' => 'delete', + 'token' => $pageinfo['deletetoken'], + 'title' => 'Main Page'), $data); + $this->assertArrayHasKey( 'delete', $data[0] ); + $this->assertArrayHasKey( 'title', $data[0]['delete'] ); + + $data = $this->doApiRequest( array( + 'action' => 'query', + 'list' => 'watchlist'), $data); + } + +} -- 2.20.1