ApiQueryImages.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<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. * This query adds an "<images>" subelement to all pages with the list of
  24. * images embedded into those pages.
  25. *
  26. * @ingroup API
  27. */
  28. class ApiQueryImages extends ApiQueryGeneratorBase {
  29. public function __construct( ApiQuery $query, $moduleName ) {
  30. parent::__construct( $query, $moduleName, 'im' );
  31. }
  32. public function execute() {
  33. $this->run();
  34. }
  35. public function executeGenerator( $resultPageSet ) {
  36. $this->run( $resultPageSet );
  37. }
  38. /**
  39. * @param ApiPageSet $resultPageSet
  40. */
  41. private function run( $resultPageSet = null ) {
  42. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  43. return; // nothing to do
  44. }
  45. $params = $this->extractRequestParams();
  46. $this->addFields( [
  47. 'il_from',
  48. 'il_to'
  49. ] );
  50. $this->addTables( 'imagelinks' );
  51. $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  52. if ( !is_null( $params['continue'] ) ) {
  53. $cont = explode( '|', $params['continue'] );
  54. $this->dieContinueUsageIf( count( $cont ) != 2 );
  55. $op = $params['dir'] == 'descending' ? '<' : '>';
  56. $ilfrom = (int)$cont[0];
  57. $ilto = $this->getDB()->addQuotes( $cont[1] );
  58. $this->addWhere(
  59. "il_from $op $ilfrom OR " .
  60. "(il_from = $ilfrom AND " .
  61. "il_to $op= $ilto)"
  62. );
  63. }
  64. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  65. // Don't order by il_from if it's constant in the WHERE clause
  66. if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
  67. $this->addOption( 'ORDER BY', 'il_to' . $sort );
  68. } else {
  69. $this->addOption( 'ORDER BY', [
  70. 'il_from' . $sort,
  71. 'il_to' . $sort
  72. ] );
  73. }
  74. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  75. if ( $params['images'] ) {
  76. $images = [];
  77. foreach ( $params['images'] as $img ) {
  78. $title = Title::newFromText( $img );
  79. if ( !$title || $title->getNamespace() != NS_FILE ) {
  80. $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
  81. } else {
  82. $images[] = $title->getDBkey();
  83. }
  84. }
  85. if ( !$images ) {
  86. // No titles so no results
  87. return;
  88. }
  89. $this->addWhereFld( 'il_to', $images );
  90. }
  91. $res = $this->select( __METHOD__ );
  92. if ( is_null( $resultPageSet ) ) {
  93. $count = 0;
  94. foreach ( $res as $row ) {
  95. if ( ++$count > $params['limit'] ) {
  96. // We've reached the one extra which shows that
  97. // there are additional pages to be had. Stop here...
  98. $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
  99. break;
  100. }
  101. $vals = [];
  102. ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
  103. $fit = $this->addPageSubItem( $row->il_from, $vals );
  104. if ( !$fit ) {
  105. $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
  106. break;
  107. }
  108. }
  109. } else {
  110. $titles = [];
  111. $count = 0;
  112. foreach ( $res as $row ) {
  113. if ( ++$count > $params['limit'] ) {
  114. // We've reached the one extra which shows that
  115. // there are additional pages to be had. Stop here...
  116. $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
  117. break;
  118. }
  119. $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
  120. }
  121. $resultPageSet->populateFromTitles( $titles );
  122. }
  123. }
  124. public function getCacheMode( $params ) {
  125. return 'public';
  126. }
  127. public function getAllowedParams() {
  128. return [
  129. 'limit' => [
  130. ApiBase::PARAM_DFLT => 10,
  131. ApiBase::PARAM_TYPE => 'limit',
  132. ApiBase::PARAM_MIN => 1,
  133. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  134. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  135. ],
  136. 'continue' => [
  137. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  138. ],
  139. 'images' => [
  140. ApiBase::PARAM_ISMULTI => true,
  141. ],
  142. 'dir' => [
  143. ApiBase::PARAM_DFLT => 'ascending',
  144. ApiBase::PARAM_TYPE => [
  145. 'ascending',
  146. 'descending'
  147. ]
  148. ],
  149. ];
  150. }
  151. protected function getExamplesMessages() {
  152. return [
  153. 'action=query&prop=images&titles=Main%20Page'
  154. => 'apihelp-query+images-example-simple',
  155. 'action=query&generator=images&titles=Main%20Page&prop=info'
  156. => 'apihelp-query+images-example-generator',
  157. ];
  158. }
  159. public function getHelpUrls() {
  160. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
  161. }
  162. }