ApiWatch.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API module to allow users to watch a page
  24. *
  25. * @ingroup API
  26. */
  27. class ApiWatch extends ApiBase {
  28. private $mPageSet = null;
  29. public function execute() {
  30. $user = $this->getUser();
  31. if ( !$user->isLoggedIn() ) {
  32. $this->dieWithError( 'watchlistanontext', 'notloggedin' );
  33. }
  34. $this->checkUserRightsAny( 'editmywatchlist' );
  35. $params = $this->extractRequestParams();
  36. $continuationManager = new ApiContinuationManager( $this, [], [] );
  37. $this->setContinuationManager( $continuationManager );
  38. $pageSet = $this->getPageSet();
  39. // by default we use pageset to extract the page to work on.
  40. // title is still supported for backward compatibility
  41. if ( !isset( $params['title'] ) ) {
  42. $pageSet->execute();
  43. $res = $pageSet->getInvalidTitlesAndRevisions( [
  44. 'invalidTitles',
  45. 'special',
  46. 'missingIds',
  47. 'missingRevIds',
  48. 'interwikiTitles'
  49. ] );
  50. foreach ( $pageSet->getMissingTitles() as $title ) {
  51. $r = $this->watchTitle( $title, $user, $params );
  52. $r['missing'] = true;
  53. $res[] = $r;
  54. }
  55. foreach ( $pageSet->getGoodTitles() as $title ) {
  56. $r = $this->watchTitle( $title, $user, $params );
  57. $res[] = $r;
  58. }
  59. ApiResult::setIndexedTagName( $res, 'w' );
  60. } else {
  61. // dont allow use of old title parameter with new pageset parameters.
  62. $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
  63. return $x !== null && $x !== false;
  64. } ) );
  65. if ( $extraParams ) {
  66. $this->dieWithError(
  67. [
  68. 'apierror-invalidparammix-cannotusewith',
  69. $this->encodeParamName( 'title' ),
  70. $pageSet->encodeParamName( $extraParams[0] )
  71. ],
  72. 'invalidparammix'
  73. );
  74. }
  75. $title = Title::newFromText( $params['title'] );
  76. if ( !$title || !$title->isWatchable() ) {
  77. $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
  78. }
  79. $res = $this->watchTitle( $title, $user, $params, true );
  80. }
  81. $this->getResult()->addValue( null, $this->getModuleName(), $res );
  82. $this->setContinuationManager( null );
  83. $continuationManager->setContinuationIntoResult( $this->getResult() );
  84. }
  85. private function watchTitle( Title $title, User $user, array $params,
  86. $compatibilityMode = false
  87. ) {
  88. if ( !$title->isWatchable() ) {
  89. return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
  90. }
  91. $res = [ 'title' => $title->getPrefixedText() ];
  92. if ( $params['unwatch'] ) {
  93. $status = UnwatchAction::doUnwatch( $title, $user );
  94. $res['unwatched'] = $status->isOK();
  95. } else {
  96. $status = WatchAction::doWatch( $title, $user );
  97. $res['watched'] = $status->isOK();
  98. }
  99. if ( !$status->isOK() ) {
  100. if ( $compatibilityMode ) {
  101. $this->dieStatus( $status );
  102. }
  103. $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
  104. $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
  105. if ( !$res['warnings'] ) {
  106. unset( $res['warnings'] );
  107. }
  108. }
  109. return $res;
  110. }
  111. /**
  112. * Get a cached instance of an ApiPageSet object
  113. * @return ApiPageSet
  114. */
  115. private function getPageSet() {
  116. if ( $this->mPageSet === null ) {
  117. $this->mPageSet = new ApiPageSet( $this );
  118. }
  119. return $this->mPageSet;
  120. }
  121. public function mustBePosted() {
  122. return true;
  123. }
  124. public function isWriteMode() {
  125. return true;
  126. }
  127. public function needsToken() {
  128. return 'watch';
  129. }
  130. public function getAllowedParams( $flags = 0 ) {
  131. $result = [
  132. 'title' => [
  133. ApiBase::PARAM_TYPE => 'string',
  134. ApiBase::PARAM_DEPRECATED => true
  135. ],
  136. 'unwatch' => false,
  137. 'continue' => [
  138. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  139. ],
  140. ];
  141. if ( $flags ) {
  142. $result += $this->getPageSet()->getFinalParams( $flags );
  143. }
  144. return $result;
  145. }
  146. protected function getExamplesMessages() {
  147. return [
  148. 'action=watch&titles=Main_Page&token=123ABC'
  149. => 'apihelp-watch-example-watch',
  150. 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
  151. => 'apihelp-watch-example-unwatch',
  152. 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
  153. => 'apihelp-watch-example-generator',
  154. ];
  155. }
  156. public function getHelpUrls() {
  157. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
  158. }
  159. }