ApiQueryAllimages.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * Created on Mar 16, 2008
  4. *
  5. * API for MediaWiki 1.12+
  6. *
  7. * Copyright (C) 2008 Vasiliev Victor vasilvv@gmail.com,
  8. * based on ApiQueryAllpages.php
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. * http://www.gnu.org/copyleft/gpl.html
  24. */
  25. if (!defined('MEDIAWIKI')) {
  26. // Eclipse helper - will be ignored in production
  27. require_once ('ApiQueryBase.php');
  28. }
  29. /**
  30. * Query module to enumerate all available pages.
  31. *
  32. * @ingroup API
  33. */
  34. class ApiQueryAllimages extends ApiQueryGeneratorBase {
  35. public function __construct($query, $moduleName) {
  36. parent :: __construct($query, $moduleName, 'ai');
  37. }
  38. public function execute() {
  39. $this->run();
  40. }
  41. public function executeGenerator($resultPageSet) {
  42. if ($resultPageSet->isResolvingRedirects())
  43. $this->dieUsage('Use "gaifilterredir=nonredirects" option instead of "redirects" when using allimages as a generator', 'params');
  44. $this->run($resultPageSet);
  45. }
  46. private function run($resultPageSet = null) {
  47. $repo = RepoGroup::singleton()->getLocalRepo();
  48. if ( !$repo instanceof LocalRepo )
  49. $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo');
  50. $db = $this->getDB();
  51. $params = $this->extractRequestParams();
  52. // Image filters
  53. $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
  54. $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
  55. $this->addWhereRange('img_name', $dir, $from, null);
  56. if (isset ($params['prefix']))
  57. $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
  58. if (isset ($params['minsize'])) {
  59. $this->addWhere('img_size>=' . intval($params['minsize']));
  60. }
  61. if (isset ($params['maxsize'])) {
  62. $this->addWhere('img_size<=' . intval($params['maxsize']));
  63. }
  64. $sha1 = false;
  65. if( isset( $params['sha1'] ) ) {
  66. $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
  67. } elseif( isset( $params['sha1base36'] ) ) {
  68. $sha1 = $params['sha1base36'];
  69. }
  70. if( $sha1 ) {
  71. $this->addWhere( 'img_sha1=' . $db->addQuotes( $sha1 ) );
  72. }
  73. $this->addTables('image');
  74. $prop = array_flip($params['prop']);
  75. $this->addFields( LocalFile::selectFields() );
  76. $limit = $params['limit'];
  77. $this->addOption('LIMIT', $limit+1);
  78. $this->addOption('ORDER BY', 'img_name' .
  79. ($params['dir'] == 'descending' ? ' DESC' : ''));
  80. $res = $this->select(__METHOD__);
  81. $titles = array();
  82. $count = 0;
  83. $result = $this->getResult();
  84. while ($row = $db->fetchObject($res)) {
  85. if (++ $count > $limit) {
  86. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  87. // TODO: Security issue - if the user has no right to view next title, it will still be shown
  88. $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
  89. break;
  90. }
  91. if (is_null($resultPageSet)) {
  92. $file = $repo->newFileFromRow( $row );
  93. $info = array_merge(array('name' => $row->img_name),
  94. ApiQueryImageInfo::getInfo($file, $prop, $result));
  95. $fit = $result->addValue(array('query', $this->getModuleName()), null, $info);
  96. if( !$fit ) {
  97. $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
  98. break;
  99. }
  100. } else {
  101. $titles[] = Title::makeTitle(NS_IMAGE, $row->img_name);
  102. }
  103. }
  104. $db->freeResult($res);
  105. if (is_null($resultPageSet)) {
  106. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img');
  107. } else {
  108. $resultPageSet->populateFromTitles($titles);
  109. }
  110. }
  111. public function getAllowedParams() {
  112. return array (
  113. 'from' => null,
  114. 'prefix' => null,
  115. 'minsize' => array (
  116. ApiBase :: PARAM_TYPE => 'integer',
  117. ),
  118. 'maxsize' => array (
  119. ApiBase :: PARAM_TYPE => 'integer',
  120. ),
  121. 'limit' => array (
  122. ApiBase :: PARAM_DFLT => 10,
  123. ApiBase :: PARAM_TYPE => 'limit',
  124. ApiBase :: PARAM_MIN => 1,
  125. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  126. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  127. ),
  128. 'dir' => array (
  129. ApiBase :: PARAM_DFLT => 'ascending',
  130. ApiBase :: PARAM_TYPE => array (
  131. 'ascending',
  132. 'descending'
  133. )
  134. ),
  135. 'sha1' => null,
  136. 'sha1base36' => null,
  137. 'prop' => array (
  138. ApiBase :: PARAM_TYPE => array(
  139. 'timestamp',
  140. 'user',
  141. 'comment',
  142. 'url',
  143. 'size',
  144. 'dimensions', // Obsolete
  145. 'mime',
  146. 'sha1',
  147. 'metadata',
  148. 'bitdepth',
  149. ),
  150. ApiBase :: PARAM_DFLT => 'timestamp|url',
  151. ApiBase :: PARAM_ISMULTI => true
  152. )
  153. );
  154. }
  155. public function getParamDescription() {
  156. return array (
  157. 'from' => 'The image title to start enumerating from.',
  158. 'prefix' => 'Search for all image titles that begin with this value.',
  159. 'dir' => 'The direction in which to list',
  160. 'minsize' => 'Limit to images with at least this many bytes',
  161. 'maxsize' => 'Limit to images with at most this many bytes',
  162. 'limit' => 'How many total images to return.',
  163. 'sha1' => 'SHA1 hash of image',
  164. 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
  165. 'prop' => 'Which properties to get',
  166. );
  167. }
  168. public function getDescription() {
  169. return 'Enumerate all images sequentially';
  170. }
  171. protected function getExamples() {
  172. return array (
  173. 'Simple Use',
  174. ' Show a list of images starting at the letter "B"',
  175. ' api.php?action=query&list=allimages&aifrom=B',
  176. 'Using as Generator',
  177. ' Show info about 4 images starting at the letter "T"',
  178. ' api.php?action=query&generator=allimages&gailimit=4&gaifrom=T&prop=imageinfo',
  179. );
  180. }
  181. public function getVersion() {
  182. return __CLASS__ . ': $Id: ApiQueryAllimages.php 46845 2009-02-05 14:30:59Z catrope $';
  183. }
  184. }