ApiSetNotificationTimestamp.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * API for MediaWiki 1.14+
  4. *
  5. * Created on Jun 18, 2012
  6. *
  7. * Copyright © 2012 Wikimedia Foundation and contributors
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. use MediaWiki\MediaWikiServices;
  27. /**
  28. * API interface for setting the wl_notificationtimestamp field
  29. * @ingroup API
  30. */
  31. class ApiSetNotificationTimestamp extends ApiBase {
  32. private $mPageSet;
  33. public function execute() {
  34. $user = $this->getUser();
  35. if ( $user->isAnon() ) {
  36. $this->dieWithError( 'watchlistanontext', 'notloggedin' );
  37. }
  38. $this->checkUserRightsAny( 'editmywatchlist' );
  39. $params = $this->extractRequestParams();
  40. $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
  41. $continuationManager = new ApiContinuationManager( $this, [], [] );
  42. $this->setContinuationManager( $continuationManager );
  43. $pageSet = $this->getPageSet();
  44. if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
  45. $this->dieWithError(
  46. [
  47. 'apierror-invalidparammix-cannotusewith',
  48. $this->encodeParamName( 'entirewatchlist' ),
  49. $pageSet->encodeParamName( $pageSet->getDataSource() )
  50. ],
  51. 'multisource'
  52. );
  53. }
  54. $dbw = wfGetDB( DB_MASTER, 'api' );
  55. $timestamp = null;
  56. if ( isset( $params['timestamp'] ) ) {
  57. $timestamp = $dbw->timestamp( $params['timestamp'] );
  58. }
  59. if ( !$params['entirewatchlist'] ) {
  60. $pageSet->execute();
  61. }
  62. if ( isset( $params['torevid'] ) ) {
  63. if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
  64. $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] );
  65. }
  66. $title = reset( $pageSet->getGoodTitles() );
  67. if ( $title ) {
  68. $timestamp = Revision::getTimestampFromId(
  69. $title, $params['torevid'], Revision::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. $title = reset( $pageSet->getGoodTitles() );
  81. if ( $title ) {
  82. $revid = $title->getNextRevisionID(
  83. $params['newerthanrevid'], Title::GAID_FOR_UPDATE );
  84. if ( $revid ) {
  85. $timestamp = $dbw->timestamp( Revision::getTimestampFromId( $title, $revid ) );
  86. } else {
  87. $timestamp = null;
  88. }
  89. }
  90. }
  91. $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
  92. $apiResult = $this->getResult();
  93. $result = [];
  94. if ( $params['entirewatchlist'] ) {
  95. // Entire watchlist mode: Just update the thing and return a success indicator
  96. $watchedItemStore->setNotificationTimestampsForUser(
  97. $user,
  98. $timestamp
  99. );
  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' => intval( $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 ( !isset( $this->mPageSet ) ) {
  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. }