* Clean up some duplicated code in r64291
authorMark A. Hershberger <mah@users.mediawiki.org>
Sat, 10 Apr 2010 06:11:02 +0000 (06:11 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Sat, 10 Apr 2010 06:11:02 +0000 (06:11 +0000)
Would've like to refactor the $wgUser->*Watch — but I'm not sure if the hooks that come along with $articleObj->*Watch are ok.

includes/api/ApiBase.php
includes/api/ApiDelete.php
includes/api/ApiProtect.php
includes/api/ApiRollback.php
maintenance/tests/ApiWatchTest.php [new file with mode: 0644]

index 7abfb9e..e90fd8e 100644 (file)
@@ -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
         *
index df16682..8c6c10e 100644 (file)
@@ -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 );
index 4e7f1fc..856e4ef 100644 (file)
@@ -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 );
index b65ecae..7de4390 100644 (file)
@@ -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 (file)
index 0000000..03209fd
--- /dev/null
@@ -0,0 +1,205 @@
+<?php
+
+global $IP;
+require_once( "$IP/maintenance/tests/ApiSetup.php" );
+
+class ApiWatchTest extends ApiSetup {
+
+       function setUp() {
+               ini_set( 'log_errors', 1 );
+               ini_set( 'error_reporting', 1 );
+               ini_set( 'display_errors', 1 );
+       }
+
+       function doApiRequest( $params, $data = null ) {
+               $_SESSION = isset( $data[2] ) ? $data[2] : array();
+
+               $req = new FauxRequest( $params, true, $session );
+               $module = new ApiMain( $req, true );
+               $module->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);
+       }
+
+}