Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / api / ApiSetNotificationTimestamp.php
index fa6fabf..5769ff6 100644 (file)
@@ -24,6 +24,7 @@
  *
  * @file
  */
+use MediaWiki\MediaWikiServices;
 
 /**
  * API interface for setting the wl_notificationtimestamp field
@@ -37,22 +38,24 @@ class ApiSetNotificationTimestamp extends ApiBase {
                $user = $this->getUser();
 
                if ( $user->isAnon() ) {
-                       $this->dieUsage( 'Anonymous users cannot use watchlist change notifications', 'notloggedin' );
-               }
-               if ( !$user->isAllowed( 'editmywatchlist' ) ) {
-                       $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
+                       $this->dieWithError( 'watchlistanontext', 'notloggedin' );
                }
+               $this->checkUserRightsAny( 'editmywatchlist' );
 
                $params = $this->extractRequestParams();
                $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
 
-               $continuationManager = new ApiContinuationManager( $this, array(), array() );
+               $continuationManager = new ApiContinuationManager( $this, [], [] );
                $this->setContinuationManager( $continuationManager );
 
                $pageSet = $this->getPageSet();
                if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
-                       $this->dieUsage(
-                               "Cannot use 'entirewatchlist' at the same time as '{$pageSet->getDataSource()}'",
+                       $this->dieWithError(
+                               [
+                                       'apierror-invalidparammix-cannotusewith',
+                                       $this->encodeParamName( 'entirewatchlist' ),
+                                       $pageSet->encodeParamName( $pageSet->getDataSource() )
+                               ],
                                'multisource'
                        );
                }
@@ -70,7 +73,7 @@ class ApiSetNotificationTimestamp extends ApiBase {
 
                if ( isset( $params['torevid'] ) ) {
                        if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
-                               $this->dieUsage( 'torevid may only be used with a single page', 'multpages' );
+                               $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] );
                        }
                        $title = reset( $pageSet->getGoodTitles() );
                        if ( $title ) {
@@ -84,7 +87,7 @@ class ApiSetNotificationTimestamp extends ApiBase {
                        }
                } elseif ( isset( $params['newerthanrevid'] ) ) {
                        if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
-                               $this->dieUsage( 'newerthanrevid may only be used with a single page', 'multpages' );
+                               $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] );
                        }
                        $title = reset( $pageSet->getGoodTitles() );
                        if ( $title ) {
@@ -98,13 +101,14 @@ class ApiSetNotificationTimestamp extends ApiBase {
                        }
                }
 
+               $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
                $apiResult = $this->getResult();
-               $result = array();
+               $result = [];
                if ( $params['entirewatchlist'] ) {
                        // Entire watchlist mode: Just update the thing and return a success indicator
-                       $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
-                               array( 'wl_user' => $user->getID() ),
-                               __METHOD__
+                       $watchedItemStore->setNotificationTimestampsForUser(
+                               $user,
+                               $timestamp
                        );
 
                        $result['notificationtimestamp'] = is_null( $timestamp )
@@ -117,14 +121,14 @@ class ApiSetNotificationTimestamp extends ApiBase {
                                $result[] = $r;
                        }
                        foreach ( $pageSet->getMissingPageIDs() as $p ) {
-                               $page = array();
+                               $page = [];
                                $page['pageid'] = $p;
                                $page['missing'] = true;
                                $page['notwatched'] = true;
                                $result[] = $page;
                        }
                        foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
-                               $rev = array();
+                               $rev = [];
                                $rev['revid'] = $r;
                                $rev['missing'] = true;
                                $rev['notwatched'] = true;
@@ -133,35 +137,32 @@ class ApiSetNotificationTimestamp extends ApiBase {
 
                        if ( $pageSet->getTitles() ) {
                                // Now process the valid titles
-                               $lb = new LinkBatch( $pageSet->getTitles() );
-                               $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
-                                       array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
-                                       __METHOD__
+                               $watchedItemStore->setNotificationTimestampsForUser(
+                                       $user,
+                                       $timestamp,
+                                       $pageSet->getTitles()
                                );
 
                                // Query the results of our update
-                               $timestamps = array();
-                               $res = $dbw->select(
-                                       'watchlist',
-                                       array( 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ),
-                                       array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
-                                       __METHOD__
+                               $timestamps = $watchedItemStore->getNotificationTimestampsBatch(
+                                       $user,
+                                       $pageSet->getTitles()
                                );
-                               foreach ( $res as $row ) {
-                                       $timestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
-                               }
 
                                // Now, put the valid titles into the result
                                /** @var $title Title */
                                foreach ( $pageSet->getTitles() as $title ) {
                                        $ns = $title->getNamespace();
                                        $dbkey = $title->getDBkey();
-                                       $r = array(
+                                       $r = [
                                                'ns' => intval( $ns ),
                                                'title' => $title->getPrefixedText(),
-                                       );
+                                       ];
                                        if ( !$title->exists() ) {
                                                $r['missing'] = true;
+                                               if ( $title->isKnown() ) {
+                                                       $r['known'] = true;
+                                               }
                                        }
                                        if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
                                                $r['notificationtimestamp'] = '';
@@ -208,23 +209,23 @@ class ApiSetNotificationTimestamp extends ApiBase {
        }
 
        public function getAllowedParams( $flags = 0 ) {
-               $result = array(
-                       'entirewatchlist' => array(
+               $result = [
+                       'entirewatchlist' => [
                                ApiBase::PARAM_TYPE => 'boolean'
-                       ),
-                       'timestamp' => array(
+                       ],
+                       'timestamp' => [
                                ApiBase::PARAM_TYPE => 'timestamp'
-                       ),
-                       'torevid' => array(
+                       ],
+                       'torevid' => [
                                ApiBase::PARAM_TYPE => 'integer'
-                       ),
-                       'newerthanrevid' => array(
+                       ],
+                       'newerthanrevid' => [
                                ApiBase::PARAM_TYPE => 'integer'
-                       ),
-                       'continue' => array(
+                       ],
+                       'continue' => [
                                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
-                       ),
-               );
+                       ],
+               ];
                if ( $flags ) {
                        $result += $this->getPageSet()->getFinalParams( $flags );
                }
@@ -233,7 +234,7 @@ class ApiSetNotificationTimestamp extends ApiBase {
        }
 
        protected function getExamplesMessages() {
-               return array(
+               return [
                        'action=setnotificationtimestamp&entirewatchlist=&token=123ABC'
                                => 'apihelp-setnotificationtimestamp-example-all',
                        'action=setnotificationtimestamp&titles=Main_page&token=123ABC'
@@ -243,7 +244,7 @@ class ApiSetNotificationTimestamp extends ApiBase {
                                => 'apihelp-setnotificationtimestamp-example-pagetimestamp',
                        'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC'
                                => 'apihelp-setnotificationtimestamp-example-allpages',
-               );
+               ];
        }
 
        public function getHelpUrls() {