ApiSetNotificationTimestamp.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * API for MediaWiki 1.14+
  4. *
  5. * Copyright © 2012 Wikimedia Foundation and contributors
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * API interface for setting the wl_notificationtimestamp field
  27. * @ingroup API
  28. */
  29. class ApiSetNotificationTimestamp extends ApiBase {
  30. private $mPageSet = null;
  31. public function execute() {
  32. $user = $this->getUser();
  33. if ( $user->isAnon() ) {
  34. $this->dieWithError( 'watchlistanontext', 'notloggedin' );
  35. }
  36. $this->checkUserRightsAny( 'editmywatchlist' );
  37. $params = $this->extractRequestParams();
  38. $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
  39. $continuationManager = new ApiContinuationManager( $this, [], [] );
  40. $this->setContinuationManager( $continuationManager );
  41. $pageSet = $this->getPageSet();
  42. if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
  43. $this->dieWithError(
  44. [
  45. 'apierror-invalidparammix-cannotusewith',
  46. $this->encodeParamName( 'entirewatchlist' ),
  47. $pageSet->encodeParamName( $pageSet->getDataSource() )
  48. ],
  49. 'multisource'
  50. );
  51. }
  52. $dbw = wfGetDB( DB_MASTER, 'api' );
  53. $timestamp = null;
  54. if ( isset( $params['timestamp'] ) ) {
  55. $timestamp = $dbw->timestamp( $params['timestamp'] );
  56. }
  57. if ( !$params['entirewatchlist'] ) {
  58. $pageSet->execute();
  59. }
  60. if ( isset( $params['torevid'] ) ) {
  61. if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
  62. $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] );
  63. }
  64. $titles = $pageSet->getGoodTitles();
  65. $title = reset( $titles );
  66. if ( $title ) {
  67. // XXX $title isn't actually used, can we just get rid of the previous six lines?
  68. $timestamp = MediaWikiServices::getInstance()->getRevisionStore()
  69. ->getTimestampFromId( $params['torevid'], IDBAccessObject::READ_LATEST );
  70. if ( $timestamp ) {
  71. $timestamp = $dbw->timestamp( $timestamp );
  72. } else {
  73. $timestamp = null;
  74. }
  75. }
  76. } elseif ( isset( $params['newerthanrevid'] ) ) {
  77. if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
  78. $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] );
  79. }
  80. $titles = $pageSet->getGoodTitles();
  81. $title = reset( $titles );
  82. if ( $title ) {
  83. $timestamp = null;
  84. $rl = MediaWikiServices::getInstance()->getRevisionLookup();
  85. $currRev = $rl->getRevisionById( $params['newerthanrevid'], Title::READ_LATEST );
  86. if ( $currRev ) {
  87. $nextRev = $rl->getNextRevision( $currRev, Title::READ_LATEST );
  88. if ( $nextRev ) {
  89. $timestamp = $dbw->timestamp( $nextRev->getTimestamp() );
  90. }
  91. }
  92. }
  93. }
  94. $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
  95. $apiResult = $this->getResult();
  96. $result = [];
  97. if ( $params['entirewatchlist'] ) {
  98. // Entire watchlist mode: Just update the thing and return a success indicator
  99. $watchedItemStore->resetAllNotificationTimestampsForUser( $user, $timestamp );
  100. $result['notificationtimestamp'] = is_null( $timestamp )
  101. ? ''
  102. : wfTimestamp( TS_ISO_8601, $timestamp );
  103. } else {
  104. // First, log the invalid titles
  105. foreach ( $pageSet->getInvalidTitlesAndReasons() as $r ) {
  106. $r['invalid'] = true;
  107. $result[] = $r;
  108. }
  109. foreach ( $pageSet->getMissingPageIDs() as $p ) {
  110. $page = [];
  111. $page['pageid'] = $p;
  112. $page['missing'] = true;
  113. $page['notwatched'] = true;
  114. $result[] = $page;
  115. }
  116. foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
  117. $rev = [];
  118. $rev['revid'] = $r;
  119. $rev['missing'] = true;
  120. $rev['notwatched'] = true;
  121. $result[] = $rev;
  122. }
  123. if ( $pageSet->getTitles() ) {
  124. // Now process the valid titles
  125. $watchedItemStore->setNotificationTimestampsForUser(
  126. $user,
  127. $timestamp,
  128. $pageSet->getTitles()
  129. );
  130. // Query the results of our update
  131. $timestamps = $watchedItemStore->getNotificationTimestampsBatch(
  132. $user,
  133. $pageSet->getTitles()
  134. );
  135. // Now, put the valid titles into the result
  136. /** @var Title $title */
  137. foreach ( $pageSet->getTitles() as $title ) {
  138. $ns = $title->getNamespace();
  139. $dbkey = $title->getDBkey();
  140. $r = [
  141. 'ns' => (int)$ns,
  142. 'title' => $title->getPrefixedText(),
  143. ];
  144. if ( !$title->exists() ) {
  145. $r['missing'] = true;
  146. if ( $title->isKnown() ) {
  147. $r['known'] = true;
  148. }
  149. }
  150. if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
  151. $r['notificationtimestamp'] = '';
  152. if ( $timestamps[$ns][$dbkey] !== null ) {
  153. $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $timestamps[$ns][$dbkey] );
  154. }
  155. } else {
  156. $r['notwatched'] = true;
  157. }
  158. $result[] = $r;
  159. }
  160. }
  161. ApiResult::setIndexedTagName( $result, 'page' );
  162. }
  163. $apiResult->addValue( null, $this->getModuleName(), $result );
  164. $this->setContinuationManager( null );
  165. $continuationManager->setContinuationIntoResult( $apiResult );
  166. }
  167. /**
  168. * Get a cached instance of an ApiPageSet object
  169. * @return ApiPageSet
  170. */
  171. private function getPageSet() {
  172. if ( $this->mPageSet === null ) {
  173. $this->mPageSet = new ApiPageSet( $this );
  174. }
  175. return $this->mPageSet;
  176. }
  177. public function mustBePosted() {
  178. return true;
  179. }
  180. public function isWriteMode() {
  181. return true;
  182. }
  183. public function needsToken() {
  184. return 'csrf';
  185. }
  186. public function getAllowedParams( $flags = 0 ) {
  187. $result = [
  188. 'entirewatchlist' => [
  189. ApiBase::PARAM_TYPE => 'boolean'
  190. ],
  191. 'timestamp' => [
  192. ApiBase::PARAM_TYPE => 'timestamp'
  193. ],
  194. 'torevid' => [
  195. ApiBase::PARAM_TYPE => 'integer'
  196. ],
  197. 'newerthanrevid' => [
  198. ApiBase::PARAM_TYPE => 'integer'
  199. ],
  200. 'continue' => [
  201. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  202. ],
  203. ];
  204. if ( $flags ) {
  205. $result += $this->getPageSet()->getFinalParams( $flags );
  206. }
  207. return $result;
  208. }
  209. protected function getExamplesMessages() {
  210. return [
  211. 'action=setnotificationtimestamp&entirewatchlist=&token=123ABC'
  212. => 'apihelp-setnotificationtimestamp-example-all',
  213. 'action=setnotificationtimestamp&titles=Main_page&token=123ABC'
  214. => 'apihelp-setnotificationtimestamp-example-page',
  215. 'action=setnotificationtimestamp&titles=Main_page&' .
  216. 'timestamp=2012-01-01T00:00:00Z&token=123ABC'
  217. => 'apihelp-setnotificationtimestamp-example-pagetimestamp',
  218. 'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC'
  219. => 'apihelp-setnotificationtimestamp-example-allpages',
  220. ];
  221. }
  222. public function getHelpUrls() {
  223. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetNotificationTimestamp';
  224. }
  225. }