ApiQueryWatchlistRaw.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * Created on Oct 4, 2008
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
  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 ('ApiQueryBase.php');
  27. }
  28. /**
  29. * This query action allows clients to retrieve a list of pages
  30. * on the logged-in user's watchlist.
  31. *
  32. * @ingroup API
  33. */
  34. class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
  35. public function __construct($query, $moduleName) {
  36. parent :: __construct($query, $moduleName, 'wr');
  37. }
  38. public function execute() {
  39. $this->run();
  40. }
  41. public function executeGenerator($resultPageSet) {
  42. $this->run($resultPageSet);
  43. }
  44. private function run($resultPageSet = null) {
  45. global $wgUser;
  46. $this->selectNamedDB('watchlist', DB_SLAVE, 'watchlist');
  47. if (!$wgUser->isLoggedIn())
  48. $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
  49. $params = $this->extractRequestParams();
  50. $prop = array_flip((array)$params['prop']);
  51. $show = array_flip((array)$params['show']);
  52. if(isset($show['changed']) && isset($show['!changed']))
  53. $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
  54. $this->addTables('watchlist');
  55. $this->addFields(array('wl_namespace', 'wl_title'));
  56. $this->addFieldsIf('wl_notificationtimestamp', isset($prop['changed']));
  57. $this->addWhereFld('wl_user', $wgUser->getId());
  58. $this->addWhereFld('wl_namespace', $params['namespace']);
  59. $this->addWhereIf('wl_notificationtimestamp IS NOT NULL', isset($show['changed']));
  60. $this->addWhereIf('wl_notificationtimestamp IS NULL', isset($show['!changed']));
  61. if(isset($params['continue']))
  62. {
  63. $cont = explode('|', $params['continue']);
  64. if(count($cont) != 2)
  65. $this->dieUsage("Invalid continue param. You should pass the " .
  66. "original value returned by the previous query", "_badcontinue");
  67. $ns = intval($cont[0]);
  68. $title = $this->getDB()->strencode($this->titleToKey($cont[1]));
  69. $this->addWhere("wl_namespace > '$ns' OR ".
  70. "(wl_namespace = '$ns' AND ".
  71. "wl_title >= '$title')");
  72. }
  73. // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
  74. if(count($params['namespace']) == 1)
  75. $this->addOption('ORDER BY', 'wl_title');
  76. else
  77. $this->addOption('ORDER BY', 'wl_namespace, wl_title');
  78. $this->addOption('LIMIT', $params['limit'] + 1);
  79. $res = $this->select(__METHOD__);
  80. $db = $this->getDB();
  81. $titles = array();
  82. $count = 0;
  83. while($row = $db->fetchObject($res))
  84. {
  85. if(++$count > $params['limit'])
  86. {
  87. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  88. $this->setContinueEnumParameter('continue', $row->wl_namespace . '|' .
  89. $this->keyToTitle($row->wl_title));
  90. break;
  91. }
  92. $t = Title::makeTitle($row->wl_namespace, $row->wl_title);
  93. if(is_null($resultPageSet))
  94. {
  95. $vals = array();
  96. ApiQueryBase::addTitleInfo($vals, $t);
  97. if(isset($prop['changed']) && !is_null($row->wl_notificationtimestamp))
  98. $vals['changed'] = wfTimestamp(TS_ISO_8601, $row->wl_notificationtimestamp);
  99. $fit = $this->getResult()->addValue($this->getModuleName(), null, $vals);
  100. if(!$fit)
  101. {
  102. $this->setContinueEnumParameter('continue', $row->wl_namespace . '|' .
  103. $this->keyToTitle($row->wl_title));
  104. break;
  105. }
  106. }
  107. else
  108. $titles[] = $t;
  109. }
  110. if(is_null($resultPageSet))
  111. $this->getResult()->setIndexedTagName_internal($this->getModuleName(), 'wr');
  112. else
  113. $resultPageSet->populateFromTitles($titles);
  114. }
  115. public function getAllowedParams() {
  116. return array (
  117. 'continue' => null,
  118. 'namespace' => array (
  119. ApiBase :: PARAM_ISMULTI => true,
  120. ApiBase :: PARAM_TYPE => 'namespace'
  121. ),
  122. 'limit' => array (
  123. ApiBase :: PARAM_DFLT => 10,
  124. ApiBase :: PARAM_TYPE => 'limit',
  125. ApiBase :: PARAM_MIN => 1,
  126. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  127. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  128. ),
  129. 'prop' => array (
  130. ApiBase :: PARAM_ISMULTI => true,
  131. ApiBase :: PARAM_TYPE => array (
  132. 'changed',
  133. )
  134. ),
  135. 'show' => array (
  136. ApiBase :: PARAM_ISMULTI => true,
  137. ApiBase :: PARAM_TYPE => array (
  138. 'changed',
  139. '!changed',
  140. )
  141. )
  142. );
  143. }
  144. public function getParamDescription() {
  145. return array (
  146. 'continue' => 'When more results are available, use this to continue',
  147. 'namespace' => 'Only list pages in the given namespace(s).',
  148. 'limit' => 'How many total results to return per request.',
  149. 'prop' => 'Which additional properties to get (non-generator mode only).',
  150. 'show' => 'Only list items that meet these criteria.',
  151. );
  152. }
  153. public function getDescription() {
  154. return "Get all pages on the logged in user's watchlist";
  155. }
  156. protected function getExamples() {
  157. return array (
  158. 'api.php?action=query&list=watchlistraw',
  159. 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
  160. );
  161. }
  162. public function getVersion() {
  163. return __CLASS__ . ': $Id: ApiQueryWatchlistRaw.php 46845 2009-02-05 14:30:59Z catrope $';
  164. }
  165. }