ApiQueryProtectedTitles.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Feb 13, 2009
  6. *
  7. * Copyright © 2009 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. /**
  27. * Query module to enumerate all create-protected pages.
  28. *
  29. * @ingroup API
  30. */
  31. class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
  32. public function __construct( ApiQuery $query, $moduleName ) {
  33. parent::__construct( $query, $moduleName, 'pt' );
  34. }
  35. public function execute() {
  36. $this->run();
  37. }
  38. public function executeGenerator( $resultPageSet ) {
  39. $this->run( $resultPageSet );
  40. }
  41. /**
  42. * @param ApiPageSet $resultPageSet
  43. * @return void
  44. */
  45. private function run( $resultPageSet = null ) {
  46. $params = $this->extractRequestParams();
  47. $this->addTables( 'protected_titles' );
  48. $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
  49. $prop = array_flip( $params['prop'] );
  50. $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
  51. $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
  52. $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
  53. if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
  54. $commentStore = new CommentStore( 'pt_reason' );
  55. $commentQuery = $commentStore->getJoin();
  56. $this->addTables( $commentQuery['tables'] );
  57. $this->addFields( $commentQuery['fields'] );
  58. $this->addJoinConds( $commentQuery['joins'] );
  59. }
  60. $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
  61. $this->addWhereFld( 'pt_namespace', $params['namespace'] );
  62. $this->addWhereFld( 'pt_create_perm', $params['level'] );
  63. // Include in ORDER BY for uniqueness
  64. $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
  65. $this->addWhereRange( 'pt_title', $params['dir'], null, null );
  66. if ( !is_null( $params['continue'] ) ) {
  67. $cont = explode( '|', $params['continue'] );
  68. $this->dieContinueUsageIf( count( $cont ) != 3 );
  69. $op = ( $params['dir'] === 'newer' ? '>' : '<' );
  70. $db = $this->getDB();
  71. $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
  72. $continueNs = (int)$cont[1];
  73. $this->dieContinueUsageIf( $continueNs != $cont[1] );
  74. $continueTitle = $db->addQuotes( $cont[2] );
  75. $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
  76. "(pt_timestamp = $continueTimestamp AND " .
  77. "(pt_namespace $op $continueNs OR " .
  78. "(pt_namespace = $continueNs AND " .
  79. "pt_title $op= $continueTitle)))"
  80. );
  81. }
  82. if ( isset( $prop['user'] ) ) {
  83. $this->addTables( 'user' );
  84. $this->addFields( 'user_name' );
  85. $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
  86. 'user_id=pt_user'
  87. ] ] );
  88. }
  89. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  90. $res = $this->select( __METHOD__ );
  91. $count = 0;
  92. $result = $this->getResult();
  93. $titles = [];
  94. foreach ( $res as $row ) {
  95. if ( ++$count > $params['limit'] ) {
  96. // We've reached the one extra which shows that there are
  97. // additional pages to be had. Stop here...
  98. $this->setContinueEnumParameter( 'continue',
  99. "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
  100. );
  101. break;
  102. }
  103. $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
  104. if ( is_null( $resultPageSet ) ) {
  105. $vals = [];
  106. ApiQueryBase::addTitleInfo( $vals, $title );
  107. if ( isset( $prop['timestamp'] ) ) {
  108. $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
  109. }
  110. if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
  111. $vals['user'] = $row->user_name;
  112. }
  113. if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
  114. $vals['userid'] = (int)$row->pt_user;
  115. }
  116. if ( isset( $prop['comment'] ) ) {
  117. $vals['comment'] = $commentStore->getComment( $row )->text;
  118. }
  119. if ( isset( $prop['parsedcomment'] ) ) {
  120. $vals['parsedcomment'] = Linker::formatComment(
  121. $commentStore->getComment( $row )->text, $titles
  122. );
  123. }
  124. if ( isset( $prop['expiry'] ) ) {
  125. $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
  126. }
  127. if ( isset( $prop['level'] ) ) {
  128. $vals['level'] = $row->pt_create_perm;
  129. }
  130. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
  131. if ( !$fit ) {
  132. $this->setContinueEnumParameter( 'continue',
  133. "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
  134. );
  135. break;
  136. }
  137. } else {
  138. $titles[] = $title;
  139. }
  140. }
  141. if ( is_null( $resultPageSet ) ) {
  142. $result->addIndexedTagName(
  143. [ 'query', $this->getModuleName() ],
  144. $this->getModulePrefix()
  145. );
  146. } else {
  147. $resultPageSet->populateFromTitles( $titles );
  148. }
  149. }
  150. public function getCacheMode( $params ) {
  151. if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
  152. // formatComment() calls wfMessage() among other things
  153. return 'anon-public-user-private';
  154. } else {
  155. return 'public';
  156. }
  157. }
  158. public function getAllowedParams() {
  159. return [
  160. 'namespace' => [
  161. ApiBase::PARAM_ISMULTI => true,
  162. ApiBase::PARAM_TYPE => 'namespace',
  163. ],
  164. 'level' => [
  165. ApiBase::PARAM_ISMULTI => true,
  166. ApiBase::PARAM_TYPE => array_diff( $this->getConfig()->get( 'RestrictionLevels' ), [ '' ] )
  167. ],
  168. 'limit' => [
  169. ApiBase::PARAM_DFLT => 10,
  170. ApiBase::PARAM_TYPE => 'limit',
  171. ApiBase::PARAM_MIN => 1,
  172. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  173. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  174. ],
  175. 'dir' => [
  176. ApiBase::PARAM_DFLT => 'older',
  177. ApiBase::PARAM_TYPE => [
  178. 'newer',
  179. 'older'
  180. ],
  181. ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
  182. ],
  183. 'start' => [
  184. ApiBase::PARAM_TYPE => 'timestamp'
  185. ],
  186. 'end' => [
  187. ApiBase::PARAM_TYPE => 'timestamp'
  188. ],
  189. 'prop' => [
  190. ApiBase::PARAM_ISMULTI => true,
  191. ApiBase::PARAM_DFLT => 'timestamp|level',
  192. ApiBase::PARAM_TYPE => [
  193. 'timestamp',
  194. 'user',
  195. 'userid',
  196. 'comment',
  197. 'parsedcomment',
  198. 'expiry',
  199. 'level'
  200. ],
  201. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  202. ],
  203. 'continue' => [
  204. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  205. ],
  206. ];
  207. }
  208. protected function getExamplesMessages() {
  209. return [
  210. 'action=query&list=protectedtitles'
  211. => 'apihelp-query+protectedtitles-example-simple',
  212. 'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
  213. => 'apihelp-query+protectedtitles-example-generator',
  214. ];
  215. }
  216. public function getHelpUrls() {
  217. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
  218. }
  219. }