00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ("ApiQueryBase.php");
00029 }
00030
00036 class ApiQueryImages extends ApiQueryGeneratorBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'im');
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function executeGenerator($resultPageSet) {
00047 $this->run($resultPageSet);
00048 }
00049
00050 private function run($resultPageSet = null) {
00051
00052 if ($this->getPageSet()->getGoodTitleCount() == 0)
00053 return;
00054
00055 $params = $this->extractRequestParams();
00056 $this->addFields(array (
00057 'il_from',
00058 'il_to'
00059 ));
00060
00061 $this->addTables('imagelinks');
00062 $this->addWhereFld('il_from', array_keys($this->getPageSet()->getGoodTitles()));
00063 if(!is_null($params['continue'])) {
00064 $cont = explode('|', $params['continue']);
00065 if(count($cont) != 2)
00066 $this->dieUsage("Invalid continue param. You should pass the " .
00067 "original value returned by the previous query", "_badcontinue");
00068 $ilfrom = intval($cont[0]);
00069 $ilto = $this->getDB()->strencode($this->titleToKey($cont[1]));
00070 $this->addWhere("il_from > $ilfrom OR ".
00071 "(il_from = $ilfrom AND ".
00072 "il_to >= '$ilto')");
00073 }
00074 # Don't order by il_from if it's constant in the WHERE clause
00075 if(count($this->getPageSet()->getGoodTitles()) == 1)
00076 $this->addOption('ORDER BY', 'il_to');
00077 else
00078 $this->addOption('ORDER BY', 'il_from, il_to');
00079 $this->addOption('LIMIT', $params['limit'] + 1);
00080
00081 $db = $this->getDB();
00082 $res = $this->select(__METHOD__);
00083
00084 if (is_null($resultPageSet)) {
00085 $count = 0;
00086 while ($row = $db->fetchObject($res)) {
00087 if (++$count > $params['limit']) {
00088
00089
00090 $this->setContinueEnumParameter('continue', $row->il_from .
00091 '|' . $this->keyToTitle($row->il_to));
00092 break;
00093 }
00094 $vals = array();
00095 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle(NS_FILE, $row->il_to));
00096 $fit = $this->addPageSubItem($row->il_from, $vals);
00097 if(!$fit)
00098 {
00099 $this->setContinueEnumParameter('continue', $row->il_from .
00100 '|' . $this->keyToTitle($row->il_to));
00101 break;
00102 }
00103 }
00104 } else {
00105
00106 $titles = array();
00107 $count = 0;
00108 while ($row = $db->fetchObject($res)) {
00109 if (++$count > $params['limit']) {
00110
00111
00112 $this->setContinueEnumParameter('continue', $row->il_from .
00113 '|' . $this->keyToTitle($row->il_to));
00114 break;
00115 }
00116 $titles[] = Title :: makeTitle(NS_FILE, $row->il_to);
00117 }
00118 $resultPageSet->populateFromTitles($titles);
00119 }
00120
00121 $db->freeResult($res);
00122 }
00123
00124 public function getAllowedParams() {
00125 return array(
00126 'limit' => array(
00127 ApiBase :: PARAM_DFLT => 10,
00128 ApiBase :: PARAM_TYPE => 'limit',
00129 ApiBase :: PARAM_MIN => 1,
00130 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00131 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00132 ),
00133 'continue' => null,
00134 );
00135 }
00136
00137 public function getParamDescription () {
00138 return array(
00139 'limit' => 'How many images to return',
00140 'continue' => 'When more results are available, use this to continue',
00141 );
00142 }
00143
00144 public function getDescription() {
00145 return 'Returns all images contained on the given page(s)';
00146 }
00147
00148 protected function getExamples() {
00149 return array (
00150 "Get a list of images used in the [[Main Page]]:",
00151 " api.php?action=query&prop=images&titles=Main%20Page",
00152 "Get information about all images used in the [[Main Page]]:",
00153 " api.php?action=query&generator=images&titles=Main%20Page&prop=info"
00154 );
00155 }
00156
00157 public function getVersion() {
00158 return __CLASS__ . ': $Id: ApiQueryImages.php 46845 2009-02-05 14:30:59Z catrope $';
00159 }
00160 }