PageRenderTime 147ms CodeModel.GetById 42ms RepoModel.GetById 8ms app.codeStats 0ms

/phase3/includes/api/ApiQueryWatchlistRaw.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 210 lines | 156 code | 17 blank | 37 comment | 17 complexity | cc5eb99f0c19f7f4a59c952774a3ffb4 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Oct 4, 2008
  6. *
  7. * Copyright © 2008 Roan Kattouw <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. if ( !defined( 'MEDIAWIKI' ) ) {
  27. // Eclipse helper - will be ignored in production
  28. require_once( 'ApiQueryBase.php' );
  29. }
  30. /**
  31. * This query action allows clients to retrieve a list of pages
  32. * on the logged-in user's watchlist.
  33. *
  34. * @ingroup API
  35. */
  36. class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
  37. public function __construct( $query, $moduleName ) {
  38. parent::__construct( $query, $moduleName, 'wr' );
  39. }
  40. public function execute() {
  41. $this->run();
  42. }
  43. public function executeGenerator( $resultPageSet ) {
  44. $this->run( $resultPageSet );
  45. }
  46. /**
  47. * @param $resultPageSet ApiPageSet
  48. * @return void
  49. */
  50. private function run( $resultPageSet = null ) {
  51. $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
  52. $params = $this->extractRequestParams();
  53. $user = $this->getWatchlistUser( $params );
  54. $prop = array_flip( (array)$params['prop'] );
  55. $show = array_flip( (array)$params['show'] );
  56. if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) {
  57. $this->dieUsageMsg( 'show' );
  58. }
  59. $this->addTables( 'watchlist' );
  60. $this->addFields( array( 'wl_namespace', 'wl_title' ) );
  61. $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) );
  62. $this->addWhereFld( 'wl_user', $user->getId() );
  63. $this->addWhereFld( 'wl_namespace', $params['namespace'] );
  64. $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) );
  65. $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) );
  66. if ( isset( $params['continue'] ) ) {
  67. $cont = explode( '|', $params['continue'] );
  68. if ( count( $cont ) != 2 ) {
  69. $this->dieUsage( "Invalid continue param. You should pass the " .
  70. "original value returned by the previous query", "_badcontinue" );
  71. }
  72. $ns = intval( $cont[0] );
  73. $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
  74. $this->addWhere(
  75. "wl_namespace > '$ns' OR " .
  76. "(wl_namespace = '$ns' AND " .
  77. "wl_title >= '$title')"
  78. );
  79. }
  80. // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
  81. if ( count( $params['namespace'] ) == 1 ) {
  82. $this->addOption( 'ORDER BY', 'wl_title' );
  83. } else {
  84. $this->addOption( 'ORDER BY', 'wl_namespace, wl_title' );
  85. }
  86. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  87. $res = $this->select( __METHOD__ );
  88. $titles = array();
  89. $count = 0;
  90. foreach ( $res as $row ) {
  91. if ( ++$count > $params['limit'] ) {
  92. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  93. $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
  94. $this->keyToTitle( $row->wl_title ) );
  95. break;
  96. }
  97. $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
  98. if ( is_null( $resultPageSet ) ) {
  99. $vals = array();
  100. ApiQueryBase::addTitleInfo( $vals, $t );
  101. if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
  102. {
  103. $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
  104. }
  105. $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
  106. if ( !$fit ) {
  107. $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
  108. $this->keyToTitle( $row->wl_title ) );
  109. break;
  110. }
  111. } else {
  112. $titles[] = $t;
  113. }
  114. }
  115. if ( is_null( $resultPageSet ) ) {
  116. $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
  117. } else {
  118. $resultPageSet->populateFromTitles( $titles );
  119. }
  120. }
  121. public function getAllowedParams() {
  122. return array(
  123. 'continue' => null,
  124. 'namespace' => array(
  125. ApiBase::PARAM_ISMULTI => true,
  126. ApiBase::PARAM_TYPE => 'namespace'
  127. ),
  128. 'limit' => array(
  129. ApiBase::PARAM_DFLT => 10,
  130. ApiBase::PARAM_TYPE => 'limit',
  131. ApiBase::PARAM_MIN => 1,
  132. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  133. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  134. ),
  135. 'prop' => array(
  136. ApiBase::PARAM_ISMULTI => true,
  137. ApiBase::PARAM_TYPE => array(
  138. 'changed',
  139. )
  140. ),
  141. 'show' => array(
  142. ApiBase::PARAM_ISMULTI => true,
  143. ApiBase::PARAM_TYPE => array(
  144. 'changed',
  145. '!changed',
  146. )
  147. ),
  148. 'owner' => array(
  149. ApiBase::PARAM_TYPE => 'user'
  150. ),
  151. 'token' => array(
  152. ApiBase::PARAM_TYPE => 'string'
  153. )
  154. );
  155. }
  156. public function getParamDescription() {
  157. return array(
  158. 'continue' => 'When more results are available, use this to continue',
  159. 'namespace' => 'Only list pages in the given namespace(s)',
  160. 'limit' => 'How many total results to return per request',
  161. 'prop' => array(
  162. 'Which additional properties to get (non-generator mode only)',
  163. ' changed - Adds timestamp of when the user was last notified about the edit',
  164. ),
  165. 'show' => 'Only list items that meet these criteria',
  166. 'owner' => 'The name of the user whose watchlist you\'d like to access',
  167. 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
  168. );
  169. }
  170. public function getDescription() {
  171. return "Get all pages on the logged in user's watchlist";
  172. }
  173. public function getPossibleErrors() {
  174. return array_merge( parent::getPossibleErrors(), array(
  175. array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
  176. array( 'show' ),
  177. array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
  178. array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
  179. ) );
  180. }
  181. public function getExamples() {
  182. return array(
  183. 'api.php?action=query&list=watchlistraw',
  184. 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
  185. );
  186. }
  187. public function getVersion() {
  188. return __CLASS__ . ': $Id$';
  189. }
  190. }