ApiWatch.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Jan 4, 2008
  6. *
  7. * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
  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. /**
  27. * API module to allow users to watch a page
  28. *
  29. * @ingroup API
  30. */
  31. class ApiWatch extends ApiBase {
  32. private $mPageSet = null;
  33. public function execute() {
  34. $user = $this->getUser();
  35. if ( !$user->isLoggedIn() ) {
  36. $this->dieWithError( 'watchlistanontext', 'notloggedin' );
  37. }
  38. $this->checkUserRightsAny( 'editmywatchlist' );
  39. $params = $this->extractRequestParams();
  40. $continuationManager = new ApiContinuationManager( $this, [], [] );
  41. $this->setContinuationManager( $continuationManager );
  42. $pageSet = $this->getPageSet();
  43. // by default we use pageset to extract the page to work on.
  44. // title is still supported for backward compatibility
  45. if ( !isset( $params['title'] ) ) {
  46. $pageSet->execute();
  47. $res = $pageSet->getInvalidTitlesAndRevisions( [
  48. 'invalidTitles',
  49. 'special',
  50. 'missingIds',
  51. 'missingRevIds',
  52. 'interwikiTitles'
  53. ] );
  54. foreach ( $pageSet->getMissingTitles() as $title ) {
  55. $r = $this->watchTitle( $title, $user, $params );
  56. $r['missing'] = true;
  57. $res[] = $r;
  58. }
  59. foreach ( $pageSet->getGoodTitles() as $title ) {
  60. $r = $this->watchTitle( $title, $user, $params );
  61. $res[] = $r;
  62. }
  63. ApiResult::setIndexedTagName( $res, 'w' );
  64. } else {
  65. // dont allow use of old title parameter with new pageset parameters.
  66. $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
  67. return $x !== null && $x !== false;
  68. } ) );
  69. if ( $extraParams ) {
  70. $this->dieWithError(
  71. [
  72. 'apierror-invalidparammix-cannotusewith',
  73. $this->encodeParamName( 'title' ),
  74. $pageSet->encodeParamName( $extraParams[0] )
  75. ],
  76. 'invalidparammix'
  77. );
  78. }
  79. $title = Title::newFromText( $params['title'] );
  80. if ( !$title || !$title->isWatchable() ) {
  81. $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
  82. }
  83. $res = $this->watchTitle( $title, $user, $params, true );
  84. }
  85. $this->getResult()->addValue( null, $this->getModuleName(), $res );
  86. $this->setContinuationManager( null );
  87. $continuationManager->setContinuationIntoResult( $this->getResult() );
  88. }
  89. private function watchTitle( Title $title, User $user, array $params,
  90. $compatibilityMode = false
  91. ) {
  92. if ( !$title->isWatchable() ) {
  93. return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
  94. }
  95. $res = [ 'title' => $title->getPrefixedText() ];
  96. if ( $params['unwatch'] ) {
  97. $status = UnwatchAction::doUnwatch( $title, $user );
  98. $res['unwatched'] = $status->isOK();
  99. } else {
  100. $status = WatchAction::doWatch( $title, $user );
  101. $res['watched'] = $status->isOK();
  102. }
  103. if ( !$status->isOK() ) {
  104. if ( $compatibilityMode ) {
  105. $this->dieStatus( $status );
  106. }
  107. $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
  108. $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
  109. if ( !$res['warnings'] ) {
  110. unset( $res['warnings'] );
  111. }
  112. }
  113. return $res;
  114. }
  115. /**
  116. * Get a cached instance of an ApiPageSet object
  117. * @return ApiPageSet
  118. */
  119. private function getPageSet() {
  120. if ( $this->mPageSet === null ) {
  121. $this->mPageSet = new ApiPageSet( $this );
  122. }
  123. return $this->mPageSet;
  124. }
  125. public function mustBePosted() {
  126. return true;
  127. }
  128. public function isWriteMode() {
  129. return true;
  130. }
  131. public function needsToken() {
  132. return 'watch';
  133. }
  134. public function getAllowedParams( $flags = 0 ) {
  135. $result = [
  136. 'title' => [
  137. ApiBase::PARAM_TYPE => 'string',
  138. ApiBase::PARAM_DEPRECATED => true
  139. ],
  140. 'unwatch' => false,
  141. 'continue' => [
  142. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  143. ],
  144. ];
  145. if ( $flags ) {
  146. $result += $this->getPageSet()->getFinalParams( $flags );
  147. }
  148. return $result;
  149. }
  150. protected function getExamplesMessages() {
  151. return [
  152. 'action=watch&titles=Main_Page&token=123ABC'
  153. => 'apihelp-watch-example-watch',
  154. 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
  155. => 'apihelp-watch-example-unwatch',
  156. 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
  157. => 'apihelp-watch-example-generator',
  158. ];
  159. }
  160. public function getHelpUrls() {
  161. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
  162. }
  163. }