PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/mediawiki-integration/source/php/mediawiki/includes/api/ApiQueryWatchlist.php

https://code.google.com/
PHP | 234 lines | 181 code | 28 blank | 25 comment | 20 complexity | f356242576ca9f39ccf6172b3f695c55 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /*
  3. * Created on Sep 25, 2006
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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 ('ApiQueryBase.php');
  27. }
  28. class ApiQueryWatchlist extends ApiQueryGeneratorBase {
  29. public function __construct($query, $moduleName) {
  30. parent :: __construct($query, $moduleName, 'wl');
  31. }
  32. public function execute() {
  33. $this->run();
  34. }
  35. public function executeGenerator($resultPageSet) {
  36. $this->run($resultPageSet);
  37. }
  38. private function run($resultPageSet = null) {
  39. global $wgUser;
  40. if (!$wgUser->isLoggedIn())
  41. $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
  42. $allrev = $start = $end = $namespace = $dir = $limit = $prop = null;
  43. extract($this->extractRequestParams());
  44. $patrol = $timestamp = $user = $comment = false;
  45. if (!is_null($prop)) {
  46. if (!is_null($resultPageSet))
  47. $this->dieUsage('prop parameter may not be used in a generator', 'params');
  48. $user = (false !== array_search('user', $prop));
  49. $comment = (false !== array_search('comment', $prop));
  50. $timestamp = (false !== array_search('timestamp', $prop)); // TODO: $timestamp not currently being used.
  51. $patrol = (false !== array_search('patrol', $prop));
  52. if ($patrol) {
  53. global $wgUseRCPatrol, $wgUser;
  54. if (!$wgUseRCPatrol || !$wgUser->isAllowed('patrol'))
  55. $this->dieUsage('patrol property is not available', 'patrol');
  56. }
  57. }
  58. if (is_null($resultPageSet)) {
  59. $this->addFields(array (
  60. 'rc_cur_id',
  61. 'rc_this_oldid',
  62. 'rc_namespace',
  63. 'rc_title',
  64. 'rc_new',
  65. 'rc_minor',
  66. 'rc_timestamp'
  67. ));
  68. $this->addFieldsIf('rc_user', $user);
  69. $this->addFieldsIf('rc_user_text', $user);
  70. $this->addFieldsIf('rc_comment', $comment);
  71. $this->addFieldsIf('rc_patrolled', $patrol);
  72. }
  73. elseif ($allrev) {
  74. $this->addFields(array (
  75. 'rc_this_oldid',
  76. 'rc_namespace',
  77. 'rc_title',
  78. 'rc_timestamp'
  79. ));
  80. } else {
  81. $this->addFields(array (
  82. 'rc_cur_id',
  83. 'rc_namespace',
  84. 'rc_title',
  85. 'rc_timestamp'
  86. ));
  87. }
  88. $this->addTables(array (
  89. 'watchlist',
  90. 'page',
  91. 'recentchanges'
  92. ));
  93. $userId = $wgUser->getID();
  94. $this->addWhere(array (
  95. 'wl_namespace = rc_namespace',
  96. 'wl_title = rc_title',
  97. 'rc_cur_id = page_id',
  98. 'wl_user' => $userId
  99. ));
  100. $this->addWhereRange('rc_timestamp', $dir, $start, $end);
  101. $this->addWhereFld('wl_namespace', $namespace);
  102. $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
  103. $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end));
  104. $this->addOption('LIMIT', $limit +1);
  105. $data = array ();
  106. $count = 0;
  107. $res = $this->select(__METHOD__);
  108. $db = $this->getDB();
  109. while ($row = $db->fetchObject($res)) {
  110. if (++ $count > $limit) {
  111. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  112. $this->setContinueEnumParameter('start', $row->rc_timestamp);
  113. break;
  114. }
  115. if (is_null($resultPageSet)) {
  116. $vals = $this->addRowInfo('rc', $row);
  117. if($vals)
  118. $data[] = $vals;
  119. } else {
  120. $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
  121. // skip any pages that user has no rights to read
  122. if ($title->userCanRead()) {
  123. if ($allrev) {
  124. $data[] = intval($row->rc_this_oldid);
  125. } else {
  126. $data[] = intval($row->rc_cur_id);
  127. }
  128. }
  129. }
  130. }
  131. $db->freeResult($res);
  132. if (is_null($resultPageSet)) {
  133. $this->getResult()->setIndexedTagName($data, 'item');
  134. $this->getResult()->addValue('query', $this->getModuleName(), $data);
  135. }
  136. elseif ($allrev) {
  137. $resultPageSet->populateFromRevisionIDs($data);
  138. } else {
  139. $resultPageSet->populateFromPageIDs($data);
  140. }
  141. }
  142. protected function getAllowedParams() {
  143. return array (
  144. 'allrev' => false,
  145. 'start' => array (
  146. ApiBase :: PARAM_TYPE => 'timestamp'
  147. ),
  148. 'end' => array (
  149. ApiBase :: PARAM_TYPE => 'timestamp'
  150. ),
  151. 'namespace' => array (
  152. ApiBase :: PARAM_ISMULTI => true,
  153. ApiBase :: PARAM_TYPE => 'namespace'
  154. ),
  155. 'dir' => array (
  156. ApiBase :: PARAM_DFLT => 'older',
  157. ApiBase :: PARAM_TYPE => array (
  158. 'newer',
  159. 'older'
  160. )
  161. ),
  162. 'limit' => array (
  163. ApiBase :: PARAM_DFLT => 10,
  164. ApiBase :: PARAM_TYPE => 'limit',
  165. ApiBase :: PARAM_MIN => 1,
  166. ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
  167. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  168. ),
  169. 'prop' => array (
  170. APIBase :: PARAM_ISMULTI => true,
  171. APIBase :: PARAM_TYPE => array (
  172. 'user',
  173. 'comment',
  174. 'timestamp',
  175. 'patrol'
  176. )
  177. )
  178. );
  179. }
  180. protected function getParamDescription() {
  181. return array (
  182. 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
  183. 'start' => 'The timestamp to start enumerating from.',
  184. 'end' => 'The timestamp to end enumerating.',
  185. 'namespace' => 'Filter changes to only the given namespace(s).',
  186. 'dir' => 'In which direction to enumerate pages.',
  187. 'limit' => 'How many total pages to return per request.',
  188. 'prop' => 'Which additional items to get (non-generator mode only).'
  189. );
  190. }
  191. protected function getDescription() {
  192. return '';
  193. }
  194. protected function getExamples() {
  195. return array (
  196. 'api.php?action=query&list=watchlist',
  197. 'api.php?action=query&list=watchlist&wlallrev',
  198. 'api.php?action=query&generator=watchlist&prop=info',
  199. 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
  200. );
  201. }
  202. public function getVersion() {
  203. return __CLASS__ . ': $Id: ApiQueryWatchlist.php 17987 2006-11-29 05:45:03Z nickj $';
  204. }
  205. }
  206. ?>