ApiWatch.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * Created on Jan 4, 2008
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiBase.php');
  27. }
  28. /**
  29. * API module to allow users to watch a page
  30. *
  31. * @ingroup API
  32. */
  33. class ApiWatch extends ApiBase {
  34. public function __construct($main, $action) {
  35. parent :: __construct($main, $action);
  36. }
  37. public function execute() {
  38. global $wgUser;
  39. if(!$wgUser->isLoggedIn())
  40. $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
  41. $params = $this->extractRequestParams();
  42. $title = Title::newFromText($params['title']);
  43. if(!$title)
  44. $this->dieUsageMsg(array('invalidtitle', $params['title']));
  45. $article = new Article($title);
  46. $res = array('title' => $title->getPrefixedText());
  47. if($params['unwatch'])
  48. {
  49. $res['unwatched'] = '';
  50. $success = $article->doUnwatch();
  51. }
  52. else
  53. {
  54. $res['watched'] = '';
  55. $success = $article->doWatch();
  56. }
  57. if(!$success)
  58. $this->dieUsageMsg(array('hookaborted'));
  59. $this->getResult()->addValue(null, $this->getModuleName(), $res);
  60. }
  61. public function isWriteMode() {
  62. return true;
  63. }
  64. public function getAllowedParams() {
  65. return array (
  66. 'title' => null,
  67. 'unwatch' => false,
  68. );
  69. }
  70. public function getParamDescription() {
  71. return array (
  72. 'title' => 'The page to (un)watch',
  73. 'unwatch' => 'If set the page will be unwatched rather than watched',
  74. );
  75. }
  76. public function getDescription() {
  77. return array (
  78. 'Add or remove a page from/to the current user\'s watchlist'
  79. );
  80. }
  81. protected function getExamples() {
  82. return array(
  83. 'api.php?action=watch&title=Main_Page',
  84. 'api.php?action=watch&title=Main_Page&unwatch',
  85. );
  86. }
  87. public function getVersion() {
  88. return __CLASS__ . ': $Id: ApiWatch.php 48091 2009-03-06 13:49:44Z catrope $';
  89. }
  90. }