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 ApiQueryImageInfo extends ApiQueryBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'ii' );
00040 }
00041
00042 public function execute() {
00043 $params = $this->extractRequestParams();
00044
00045 $prop = array_flip( $params['prop'] );
00046
00047 if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 )
00048 $this->dieUsage( "iiurlheight cannot be used without iiurlwidth", 'iiurlwidth' );
00049
00050 if ( $params['urlwidth'] != - 1 ) {
00051 $scale = array();
00052 $scale['width'] = $params['urlwidth'];
00053 $scale['height'] = $params['urlheight'];
00054 } else {
00055 $scale = null;
00056 }
00057
00058 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
00059 if ( !empty( $pageIds[NS_FILE] ) ) {
00060 $titles = array_keys( $pageIds[NS_FILE] );
00061 asort( $titles );
00062
00063 $skip = false;
00064 if ( !is_null( $params['continue'] ) )
00065 {
00066 $skip = true;
00067 $cont = explode( '|', $params['continue'] );
00068 if ( count( $cont ) != 2 )
00069 $this->dieUsage( "Invalid continue param. You should pass the original " .
00070 "value returned by the previous query", "_badcontinue" );
00071 $fromTitle = strval( $cont[0] );
00072 $fromTimestamp = $cont[1];
00073
00074 foreach ( $titles as $key => $title )
00075 if ( $title < $fromTitle )
00076 unset( $titles[$key] );
00077 else
00078 break;
00079 }
00080
00081 $result = $this->getResult();
00082 $images = RepoGroup::singleton()->findFiles( $titles );
00083 foreach ( $images as $img ) {
00084
00085 if ( $img->getOriginalTitle()->isRedirect() )
00086 continue;
00087
00088 $start = $skip ? $fromTimestamp : $params['start'];
00089 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
00090
00091 $fit = $result->addValue(
00092 array( 'query', 'pages', intval( $pageId ) ),
00093 'imagerepository', $img->getRepoName()
00094 );
00095 if ( !$fit )
00096 {
00097 if ( count( $pageIds[NS_IMAGE] ) == 1 )
00098
00099
00100
00101
00102
00103 $this->setContinueEnumParameter( 'start',
00104 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
00105 else
00106 $this->setContinueEnumParameter( 'continue',
00107 $this->getContinueStr( $img ) );
00108 break;
00109 }
00110
00111
00112
00113 $gotOne = false;
00114 if ( ( is_null( $start ) || $img->getTimestamp() <= $start ) &&
00115 ( is_null( $params['end'] ) || $img->getTimestamp() >= $params['end'] ) ) {
00116 $gotOne = true;
00117 $fit = $this->addPageSubItem( $pageId,
00118 self::getInfo( $img, $prop, $result, $scale ) );
00119 if ( !$fit )
00120 {
00121 if ( count( $pageIds[NS_IMAGE] ) == 1 )
00122
00123 $this->setContinueEnumParameter( 'start',
00124 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
00125 else
00126 $this->setContinueEnumParameter( 'continue',
00127 $this->getContinueStr( $img ) );
00128 break;
00129 }
00130 }
00131
00132
00133
00134 $count = ( $gotOne ? 1 : 0 );
00135 $oldies = $img->getHistory( $params['limit'] - $count + 1, $start, $params['end'] );
00136 foreach ( $oldies as $oldie ) {
00137 if ( ++$count > $params['limit'] ) {
00138
00139
00140 if ( count( $pageIds[NS_FILE] ) == 1 )
00141 {
00142 $this->setContinueEnumParameter( 'start',
00143 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
00144 }
00145 break;
00146 }
00147 $fit = $this->addPageSubItem( $pageId,
00148 self::getInfo( $oldie, $prop, $result ) );
00149 if ( !$fit )
00150 {
00151 if ( count( $pageIds[NS_IMAGE] ) == 1 )
00152 $this->setContinueEnumParameter( 'start',
00153 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
00154 else
00155 $this->setContinueEnumParameter( 'continue',
00156 $this->getContinueStr( $oldie ) );
00157 break;
00158 }
00159 }
00160 if ( !$fit )
00161 break;
00162 $skip = false;
00163 }
00164
00165 $data = $this->getResultData();
00166 foreach ( $data['query']['pages'] as $pageid => $arr ) {
00167 if ( !isset( $arr['imagerepository'] ) )
00168 $result->addValue(
00169 array( 'query', 'pages', $pageid ),
00170 'imagerepository', ''
00171 );
00172
00173 }
00174 }
00175 }
00176
00182 static function getInfo( $file, $prop, $result, $scale = null ) {
00183 $vals = array();
00184 if ( isset( $prop['timestamp'] ) )
00185 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
00186 if ( isset( $prop['user'] ) ) {
00187 $vals['user'] = $file->getUser();
00188 if ( !$file->getUser( 'id' ) )
00189 $vals['anon'] = '';
00190 }
00191 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
00192 $vals['size'] = intval( $file->getSize() );
00193 $vals['width'] = intval( $file->getWidth() );
00194 $vals['height'] = intval( $file->getHeight() );
00195 }
00196 if ( isset( $prop['url'] ) ) {
00197 if ( !is_null( $scale ) && !$file->isOld() ) {
00198 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
00199 if ( $mto && !$mto->isError() )
00200 {
00201 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
00202 $vals['thumbwidth'] = intval( $mto->getWidth() );
00203 $vals['thumbheight'] = intval( $mto->getHeight() );
00204 }
00205 }
00206 $vals['url'] = $file->getFullURL();
00207 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
00208 }
00209 if ( isset( $prop['comment'] ) )
00210 $vals['comment'] = $file->getDescription();
00211 if ( isset( $prop['sha1'] ) )
00212 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
00213 if ( isset( $prop['metadata'] ) ) {
00214 $metadata = $file->getMetadata();
00215 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
00216 }
00217 if ( isset( $prop['mime'] ) )
00218 $vals['mime'] = $file->getMimeType();
00219
00220 if ( isset( $prop['archivename'] ) && $file->isOld() )
00221 $vals['archivename'] = $file->getArchiveName();
00222
00223 if ( isset( $prop['bitdepth'] ) )
00224 $vals['bitdepth'] = $file->getBitDepth();
00225
00226 return $vals;
00227 }
00228
00229 public static function processMetaData( $metadata, $result )
00230 {
00231 $retval = array();
00232 if ( is_array( $metadata ) ) {
00233 foreach ( $metadata as $key => $value )
00234 {
00235 $r = array( 'name' => $key );
00236 if ( is_array( $value ) )
00237 $r['value'] = self::processMetaData( $value, $result );
00238 else
00239 $r['value'] = $value;
00240 $retval[] = $r;
00241 }
00242 }
00243 $result->setIndexedTagName( $retval, 'metadata' );
00244 return $retval;
00245 }
00246
00247 public function getCacheMode( $params ) {
00248 return 'public';
00249 }
00250
00251 private function getContinueStr( $img )
00252 {
00253 return $img->getOriginalTitle()->getText() .
00254 '|' . $img->getTimestamp();
00255 }
00256
00257 public function getAllowedParams() {
00258 return array (
00259 'prop' => array (
00260 ApiBase :: PARAM_ISMULTI => true,
00261 ApiBase :: PARAM_DFLT => 'timestamp|user',
00262 ApiBase :: PARAM_TYPE => self::getPropertyNames()
00263 ),
00264 'limit' => array(
00265 ApiBase :: PARAM_TYPE => 'limit',
00266 ApiBase :: PARAM_DFLT => 1,
00267 ApiBase :: PARAM_MIN => 1,
00268 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00269 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00270 ),
00271 'start' => array(
00272 ApiBase :: PARAM_TYPE => 'timestamp'
00273 ),
00274 'end' => array(
00275 ApiBase :: PARAM_TYPE => 'timestamp'
00276 ),
00277 'urlwidth' => array(
00278 ApiBase :: PARAM_TYPE => 'integer',
00279 ApiBase :: PARAM_DFLT => - 1
00280 ),
00281 'urlheight' => array(
00282 ApiBase :: PARAM_TYPE => 'integer',
00283 ApiBase :: PARAM_DFLT => - 1
00284 ),
00285 'continue' => null,
00286 );
00287 }
00288
00292 public static function getPropertyNames() {
00293 return array (
00294 'timestamp',
00295 'user',
00296 'comment',
00297 'url',
00298 'size',
00299 'dimensions',
00300 'sha1',
00301 'mime',
00302 'metadata',
00303 'archivename',
00304 'bitdepth',
00305 );
00306 }
00307
00308 public function getParamDescription() {
00309 return array (
00310 'prop' => 'What image information to get.',
00311 'limit' => 'How many image revisions to return',
00312 'start' => 'Timestamp to start listing from',
00313 'end' => 'Timestamp to stop listing at',
00314 'urlwidth' => array( 'If iiprop=url is set, a URL to an image scaled to this width will be returned.',
00315 'Only the current version of the image can be scaled.' ),
00316 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
00317 'continue' => 'When more results are available, use this to continue',
00318 );
00319 }
00320
00321 public function getDescription() {
00322 return array (
00323 'Returns image information and upload history'
00324 );
00325 }
00326
00327 public function getPossibleErrors() {
00328 return array_merge( parent::getPossibleErrors(), array(
00329 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
00330 ) );
00331 }
00332
00333 protected function getExamples() {
00334 return array (
00335 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
00336 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
00337 );
00338 }
00339
00340 public function getVersion() {
00341 return __CLASS__ . ': $Id: ApiQueryImageInfo.php 69932 2010-07-26 08:03:21Z tstarling $';
00342 }
00343 }