ApiQueryProtectedTitles.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. * Created on Feb 13, 2009
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2009 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. * Query module to enumerate all create-protected pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'pt');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. $this->run($resultPageSet);
  42. }
  43. private function run($resultPageSet = null) {
  44. $db = $this->getDB();
  45. $params = $this->extractRequestParams();
  46. $this->addTables('protected_titles');
  47. $this->addFields(array('pt_namespace', 'pt_title', 'pt_timestamp'));
  48. $prop = array_flip($params['prop']);
  49. $this->addFieldsIf('pt_user', isset($prop['user']));
  50. $this->addFieldsIf('pt_reason', isset($prop['comment']));
  51. $this->addFieldsIf('pt_expiry', isset($prop['expiry']));
  52. $this->addFieldsIf('pt_create_perm', isset($prop['level']));
  53. $this->addWhereRange('pt_timestamp', $params['dir'], $params['start'], $params['end']);
  54. $this->addWhereFld('pt_namespace', $params['namespace']);
  55. $this->addWhereFld('pt_create_perm', $params['level']);
  56. if(isset($prop['user']))
  57. {
  58. $this->addTables('user');
  59. $this->addFields('user_name');
  60. $this->addJoinConds(array('user' => array('LEFT JOIN',
  61. 'user_id=pt_user'
  62. )));
  63. }
  64. $this->addOption('LIMIT', $params['limit'] + 1);
  65. $res = $this->select(__METHOD__);
  66. $count = 0;
  67. $result = $this->getResult();
  68. while ($row = $db->fetchObject($res)) {
  69. if (++ $count > $params['limit']) {
  70. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  71. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->pt_timestamp));
  72. break;
  73. }
  74. $title = Title::makeTitle($row->pt_namespace, $row->pt_title);
  75. if (is_null($resultPageSet)) {
  76. $vals = array();
  77. ApiQueryBase::addTitleInfo($vals, $title);
  78. if(isset($prop['timestamp']))
  79. $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->pt_timestamp);
  80. if(isset($prop['user']) && !is_null($row->user_name))
  81. $vals['user'] = $row->user_name;
  82. if(isset($prop['comment']))
  83. $vals['comment'] = $row->pt_reason;
  84. if(isset($prop['expiry']))
  85. $vals['expiry'] = Block::decodeExpiry($row->pt_expiry, TS_ISO_8601);
  86. if(isset($prop['level']))
  87. $vals['level'] = $row->pt_create_perm;
  88. $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
  89. if(!$fit)
  90. {
  91. $this->setContinueEnumParameter('start',
  92. wfTimestamp(TS_ISO_8601, $row->pt_timestamp));
  93. break;
  94. }
  95. } else {
  96. $titles[] = $title;
  97. }
  98. }
  99. $db->freeResult($res);
  100. if(is_null($resultPageSet))
  101. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), $this->getModulePrefix());
  102. else
  103. $resultPageSet->populateFromTitles($titles);
  104. }
  105. public function getAllowedParams() {
  106. global $wgRestrictionLevels;
  107. return array (
  108. 'namespace' => array (
  109. ApiBase :: PARAM_ISMULTI => true,
  110. ApiBase :: PARAM_TYPE => 'namespace',
  111. ),
  112. 'level' => array(
  113. ApiBase :: PARAM_ISMULTI => true,
  114. ApiBase :: PARAM_TYPE => array_diff($wgRestrictionLevels, array(''))
  115. ),
  116. 'limit' => array (
  117. ApiBase :: PARAM_DFLT => 10,
  118. ApiBase :: PARAM_TYPE => 'limit',
  119. ApiBase :: PARAM_MIN => 1,
  120. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  121. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  122. ),
  123. 'dir' => array (
  124. ApiBase :: PARAM_DFLT => 'older',
  125. ApiBase :: PARAM_TYPE => array (
  126. 'older',
  127. 'newer'
  128. )
  129. ),
  130. 'start' => array(
  131. ApiBase :: PARAM_TYPE => 'timestamp'
  132. ),
  133. 'end' => array(
  134. ApiBase :: PARAM_TYPE => 'timestamp'
  135. ),
  136. 'prop' => array(
  137. ApiBase :: PARAM_ISMULTI => true,
  138. ApiBase :: PARAM_DFLT => 'timestamp|level',
  139. ApiBase :: PARAM_TYPE => array(
  140. 'timestamp',
  141. 'user',
  142. 'comment',
  143. 'expiry',
  144. 'level'
  145. )
  146. ),
  147. );
  148. }
  149. public function getParamDescription() {
  150. return array (
  151. 'namespace' => 'Only list titles in these namespaces',
  152. 'start' => 'Start listing at this protection timestamp',
  153. 'end' => 'Stop listing at this protection timestamp',
  154. 'dir' => 'The direction in which to list',
  155. 'limit' => 'How many total pages to return.',
  156. 'prop' => 'Which properties to get',
  157. 'level' => 'Only list titles with these protection levels',
  158. );
  159. }
  160. public function getDescription() {
  161. return 'List all titles protected from creation';
  162. }
  163. protected function getExamples() {
  164. return array (
  165. 'api.php?action=query&list=protectedtitles',
  166. );
  167. }
  168. public function getVersion() {
  169. return __CLASS__ . ': $Id: ApiQueryProtectedTitles.php 47235 2009-02-13 21:53:08Z catrope $';
  170. }
  171. }