ApiQueryImages.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on May 13, 2007
  6. *
  7. * Copyright © 2006 Yuri Astrakhan "<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. * This query adds an "<images>" subelement to all pages with the list of
  28. * images embedded into those pages.
  29. *
  30. * @ingroup API
  31. */
  32. class ApiQueryImages extends ApiQueryGeneratorBase {
  33. public function __construct( ApiQuery $query, $moduleName ) {
  34. parent::__construct( $query, $moduleName, 'im' );
  35. }
  36. public function execute() {
  37. $this->run();
  38. }
  39. public function executeGenerator( $resultPageSet ) {
  40. $this->run( $resultPageSet );
  41. }
  42. /**
  43. * @param ApiPageSet $resultPageSet
  44. */
  45. private function run( $resultPageSet = null ) {
  46. if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
  47. return; // nothing to do
  48. }
  49. $params = $this->extractRequestParams();
  50. $this->addFields( [
  51. 'il_from',
  52. 'il_to'
  53. ] );
  54. $this->addTables( 'imagelinks' );
  55. $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
  56. if ( !is_null( $params['continue'] ) ) {
  57. $cont = explode( '|', $params['continue'] );
  58. $this->dieContinueUsageIf( count( $cont ) != 2 );
  59. $op = $params['dir'] == 'descending' ? '<' : '>';
  60. $ilfrom = intval( $cont[0] );
  61. $ilto = $this->getDB()->addQuotes( $cont[1] );
  62. $this->addWhere(
  63. "il_from $op $ilfrom OR " .
  64. "(il_from = $ilfrom AND " .
  65. "il_to $op= $ilto)"
  66. );
  67. }
  68. $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  69. // Don't order by il_from if it's constant in the WHERE clause
  70. if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
  71. $this->addOption( 'ORDER BY', 'il_to' . $sort );
  72. } else {
  73. $this->addOption( 'ORDER BY', [
  74. 'il_from' . $sort,
  75. 'il_to' . $sort
  76. ] );
  77. }
  78. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  79. if ( !is_null( $params['images'] ) ) {
  80. $images = [];
  81. foreach ( $params['images'] as $img ) {
  82. $title = Title::newFromText( $img );
  83. if ( !$title || $title->getNamespace() != NS_FILE ) {
  84. $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
  85. } else {
  86. $images[] = $title->getDBkey();
  87. }
  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. }