ApiQueryProtectedTitles.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Query module to enumerate all create-protected pages.
  24. *
  25. * @ingroup API
  26. */
  27. class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
  28. public function __construct( ApiQuery $query, $moduleName ) {
  29. parent::__construct( $query, $moduleName, 'pt' );
  30. }
  31. public function execute() {
  32. $this->run();
  33. }
  34. public function executeGenerator( $resultPageSet ) {
  35. $this->run( $resultPageSet );
  36. }
  37. /**
  38. * @param ApiPageSet $resultPageSet
  39. * @return void
  40. */
  41. private function run( $resultPageSet = null ) {
  42. $params = $this->extractRequestParams();
  43. $this->addTables( 'protected_titles' );
  44. $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
  45. $prop = array_flip( $params['prop'] );
  46. $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
  47. $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
  48. $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
  49. if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
  50. $commentStore = CommentStore::getStore();
  51. $commentQuery = $commentStore->getJoin( 'pt_reason' );
  52. $this->addTables( $commentQuery['tables'] );
  53. $this->addFields( $commentQuery['fields'] );
  54. $this->addJoinConds( $commentQuery['joins'] );
  55. }
  56. $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
  57. $this->addWhereFld( 'pt_namespace', $params['namespace'] );
  58. $this->addWhereFld( 'pt_create_perm', $params['level'] );
  59. // Include in ORDER BY for uniqueness
  60. $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
  61. $this->addWhereRange( 'pt_title', $params['dir'], null, null );
  62. if ( !is_null( $params['continue'] ) ) {
  63. $cont = explode( '|', $params['continue'] );
  64. $this->dieContinueUsageIf( count( $cont ) != 3 );
  65. $op = ( $params['dir'] === 'newer' ? '>' : '<' );
  66. $db = $this->getDB();
  67. $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
  68. $continueNs = (int)$cont[1];
  69. $this->dieContinueUsageIf( $continueNs != $cont[1] );
  70. $continueTitle = $db->addQuotes( $cont[2] );
  71. $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
  72. "(pt_timestamp = $continueTimestamp AND " .
  73. "(pt_namespace $op $continueNs OR " .
  74. "(pt_namespace = $continueNs AND " .
  75. "pt_title $op= $continueTitle)))"
  76. );
  77. }
  78. if ( isset( $prop['user'] ) ) {
  79. $this->addTables( 'user' );
  80. $this->addFields( 'user_name' );
  81. $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
  82. 'user_id=pt_user'
  83. ] ] );
  84. }
  85. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  86. $res = $this->select( __METHOD__ );
  87. $count = 0;
  88. $result = $this->getResult();
  89. $titles = [];
  90. foreach ( $res as $row ) {
  91. if ( ++$count > $params['limit'] ) {
  92. // We've reached the one extra which shows that there are
  93. // additional pages to be had. Stop here...
  94. $this->setContinueEnumParameter( 'continue',
  95. "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
  96. );
  97. break;
  98. }
  99. $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
  100. if ( is_null( $resultPageSet ) ) {
  101. $vals = [];
  102. ApiQueryBase::addTitleInfo( $vals, $title );
  103. if ( isset( $prop['timestamp'] ) ) {
  104. $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
  105. }
  106. if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
  107. $vals['user'] = $row->user_name;
  108. }
  109. if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
  110. $vals['userid'] = (int)$row->pt_user;
  111. }
  112. if ( isset( $prop['comment'] ) ) {
  113. $vals['comment'] = $commentStore->getComment( 'pt_reason', $row )->text;
  114. }
  115. if ( isset( $prop['parsedcomment'] ) ) {
  116. $vals['parsedcomment'] = Linker::formatComment(
  117. $commentStore->getComment( 'pt_reason', $row )->text
  118. );
  119. }
  120. if ( isset( $prop['expiry'] ) ) {
  121. $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
  122. }
  123. if ( isset( $prop['level'] ) ) {
  124. $vals['level'] = $row->pt_create_perm;
  125. }
  126. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  127. if ( !$fit ) {
  128. $this->setContinueEnumParameter( 'continue',
  129. "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
  130. );
  131. break;
  132. }
  133. } else {
  134. $titles[] = $title;
  135. }
  136. }
  137. if ( is_null( $resultPageSet ) ) {
  138. $result->addIndexedTagName(
  139. [ 'query', $this->getModuleName() ],
  140. $this->getModulePrefix()
  141. );
  142. } else {
  143. $resultPageSet->populateFromTitles( $titles );
  144. }
  145. }
  146. public function getCacheMode( $params ) {
  147. if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
  148. // formatComment() calls wfMessage() among other things
  149. return 'anon-public-user-private';
  150. } else {
  151. return 'public';
  152. }
  153. }
  154. public function getAllowedParams() {
  155. return [
  156. 'namespace' => [
  157. ApiBase::PARAM_ISMULTI => true,
  158. ApiBase::PARAM_TYPE => 'namespace',
  159. ],
  160. 'level' => [
  161. ApiBase::PARAM_ISMULTI => true,
  162. ApiBase::PARAM_TYPE => array_diff( $this->getConfig()->get( 'RestrictionLevels' ), [ '' ] )
  163. ],
  164. 'limit' => [
  165. ApiBase::PARAM_DFLT => 10,
  166. ApiBase::PARAM_TYPE => 'limit',
  167. ApiBase::PARAM_MIN => 1,
  168. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  169. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  170. ],
  171. 'dir' => [
  172. ApiBase::PARAM_DFLT => 'older',
  173. ApiBase::PARAM_TYPE => [
  174. 'newer',
  175. 'older'
  176. ],
  177. ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
  178. ],
  179. 'start' => [
  180. ApiBase::PARAM_TYPE => 'timestamp'
  181. ],
  182. 'end' => [
  183. ApiBase::PARAM_TYPE => 'timestamp'
  184. ],
  185. 'prop' => [
  186. ApiBase::PARAM_ISMULTI => true,
  187. ApiBase::PARAM_DFLT => 'timestamp|level',
  188. ApiBase::PARAM_TYPE => [
  189. 'timestamp',
  190. 'user',
  191. 'userid',
  192. 'comment',
  193. 'parsedcomment',
  194. 'expiry',
  195. 'level'
  196. ],
  197. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  198. ],
  199. 'continue' => [
  200. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  201. ],
  202. ];
  203. }
  204. protected function getExamplesMessages() {
  205. return [
  206. 'action=query&list=protectedtitles'
  207. => 'apihelp-query+protectedtitles-example-simple',
  208. 'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
  209. => 'apihelp-query+protectedtitles-example-generator',
  210. ];
  211. }
  212. public function getHelpUrls() {
  213. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
  214. }
  215. }