ApiQueryImages.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * Created on May 13, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 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. * 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. * This query adds an <images> subelement to all pages with the list of images embedded into those pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryImages extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'im');
  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. if ($this->getPageSet()->getGoodTitleCount() == 0)
  45. return; // nothing to do
  46. $params = $this->extractRequestParams();
  47. $this->addFields(array (
  48. 'il_from',
  49. 'il_to'
  50. ));
  51. $this->addTables('imagelinks');
  52. $this->addWhereFld('il_from', array_keys($this->getPageSet()->getGoodTitles()));
  53. if(!is_null($params['continue'])) {
  54. $cont = explode('|', $params['continue']);
  55. if(count($cont) != 2)
  56. $this->dieUsage("Invalid continue param. You should pass the " .
  57. "original value returned by the previous query", "_badcontinue");
  58. $ilfrom = intval($cont[0]);
  59. $ilto = $this->getDB()->strencode($this->titleToKey($cont[1]));
  60. $this->addWhere("il_from > $ilfrom OR ".
  61. "(il_from = $ilfrom AND ".
  62. "il_to >= '$ilto')");
  63. }
  64. # Don't order by il_from if it's constant in the WHERE clause
  65. if(count($this->getPageSet()->getGoodTitles()) == 1)
  66. $this->addOption('ORDER BY', 'il_to');
  67. else
  68. $this->addOption('ORDER BY', 'il_from, il_to');
  69. $this->addOption('LIMIT', $params['limit'] + 1);
  70. $db = $this->getDB();
  71. $res = $this->select(__METHOD__);
  72. if (is_null($resultPageSet)) {
  73. $count = 0;
  74. while ($row = $db->fetchObject($res)) {
  75. if (++$count > $params['limit']) {
  76. // We've reached the one extra which shows that
  77. // there are additional pages to be had. Stop here...
  78. $this->setContinueEnumParameter('continue', $row->il_from .
  79. '|' . $this->keyToTitle($row->il_to));
  80. break;
  81. }
  82. $vals = array();
  83. ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle(NS_FILE, $row->il_to));
  84. $fit = $this->addPageSubItem($row->il_from, $vals);
  85. if(!$fit)
  86. {
  87. $this->setContinueEnumParameter('continue', $row->il_from .
  88. '|' . $this->keyToTitle($row->il_to));
  89. break;
  90. }
  91. }
  92. } else {
  93. $titles = array();
  94. $count = 0;
  95. while ($row = $db->fetchObject($res)) {
  96. if (++$count > $params['limit']) {
  97. // We've reached the one extra which shows that
  98. // there are additional pages to be had. Stop here...
  99. $this->setContinueEnumParameter('continue', $row->il_from .
  100. '|' . $this->keyToTitle($row->il_to));
  101. break;
  102. }
  103. $titles[] = Title :: makeTitle(NS_FILE, $row->il_to);
  104. }
  105. $resultPageSet->populateFromTitles($titles);
  106. }
  107. $db->freeResult($res);
  108. }
  109. public function getAllowedParams() {
  110. return array(
  111. 'limit' => array(
  112. ApiBase :: PARAM_DFLT => 10,
  113. ApiBase :: PARAM_TYPE => 'limit',
  114. ApiBase :: PARAM_MIN => 1,
  115. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  116. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  117. ),
  118. 'continue' => null,
  119. );
  120. }
  121. public function getParamDescription () {
  122. return array(
  123. 'limit' => 'How many images to return',
  124. 'continue' => 'When more results are available, use this to continue',
  125. );
  126. }
  127. public function getDescription() {
  128. return 'Returns all images contained on the given page(s)';
  129. }
  130. protected function getExamples() {
  131. return array (
  132. "Get a list of images used in the [[Main Page]]:",
  133. " api.php?action=query&prop=images&titles=Main%20Page",
  134. "Get information about all images used in the [[Main Page]]:",
  135. " api.php?action=query&generator=images&titles=Main%20Page&prop=info"
  136. );
  137. }
  138. public function getVersion() {
  139. return __CLASS__ . ': $Id: ApiQueryImages.php 46845 2009-02-05 14:30:59Z catrope $';
  140. }
  141. }