From 28babe0e6507b3899eded500a78708933284c393 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Tue, 20 May 2008 14:32:52 +0000 Subject: [PATCH] Use ApiQueryImageInfo::getInfo for both ApiQueryAllimages and ApiQueryImageInfo. Should also give some more consistency between the two modules. --- includes/api/ApiQueryAllimages.php | 30 +++-------- includes/api/ApiQueryImageInfo.php | 83 +++++++++++++++--------------- 2 files changed, 47 insertions(+), 66 deletions(-) diff --git a/includes/api/ApiQueryAllimages.php b/includes/api/ApiQueryAllimages.php index a658bcd471..80fe31baca 100644 --- a/includes/api/ApiQueryAllimages.php +++ b/includes/api/ApiQueryAllimages.php @@ -98,6 +98,7 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase { $data = array (); $count = 0; + $result = $this->getResult(); while ($row = $db->fetchObject($res)) { if (++ $count > $limit) { // We've reached the one extra which shows that there are additional pages to be had. Stop here... @@ -108,29 +109,8 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase { if (is_null($resultPageSet)) { $file = $repo->newFileFromRow( $row ); - $item['name'] = $row->img_name; - if(isset($prop['size'])) - $item['size'] = $file->getSize(); - if(isset($prop['dimensions'])) - { - $item['width'] = $file->getWidth(); - $item['height'] = $file->getHeight(); - } - if(isset($prop['mime'])) - $item['mime'] = $file->getMimeType(); - if(isset($prop['sha1'])) - $item['sha1'] = wfBaseConvert($file->getSha1(), 36, 16, 31); - if(isset($prop['timestamp'])) - $item['timestamp'] = wfTimestamp(TS_ISO_8601, $file->getTimestamp()); - if(isset($prop['url'])) - $item['url'] = $file->getFullUrl(); - if(isset($prop['metadata'])) - { - $metadata = unserialize($file->getMetadata()); - $item['metadata'] = $metadata ? $metadata : null; - $this->getResult()->setIndexedTagName_recursive($item['metadata'], 'meta'); - } - $data[] = $item; + + $data[] = ApiQueryImageInfo::getInfo( $file, $prop, $result ); } else { $data[] = Title::makeTitle( NS_IMAGE, $row->img_name ); } @@ -175,9 +155,11 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase { 'prop' => array ( ApiBase :: PARAM_TYPE => array( 'timestamp', + 'user', + 'comment', 'url', 'size', - 'dimensions', + 'dimensions', // Obsolete 'mime', 'sha1', 'metadata' diff --git a/includes/api/ApiQueryImageInfo.php b/includes/api/ApiQueryImageInfo.php index 0a134139de..c14ea17289 100644 --- a/includes/api/ApiQueryImageInfo.php +++ b/includes/api/ApiQueryImageInfo.php @@ -43,24 +43,22 @@ class ApiQueryImageInfo extends ApiQueryBase { $params = $this->extractRequestParams(); $prop = array_flip($params['prop']); - $this->fld_timestamp = isset($prop['timestamp']); - $this->fld_user = isset($prop['user']); - $this->fld_comment = isset($prop['comment']); - $this->fld_url = isset($prop['url']); - $this->fld_size = isset($prop['size']); - $this->fld_sha1 = isset($prop['sha1']); - $this->fld_mime = isset($prop['mime']); - $this->fld_metadata = isset($prop['metadata']); - $this->fld_archivename = isset($prop['archivename']); if($params['urlheight'] != -1 && $params['urlwidth'] == -1) $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth'); - $this->scale = ($params['urlwidth'] != -1); - $this->urlwidth = $params['urlwidth']; - $this->urlheight = $params['urlheight']; + + if ( $params['urlwidth'] != -1 ) { + $scale = array(); + $scale['width'] = $params['urlwidth']; + $scale['height'] = $params['urlheight']; + } else { + $scale = null; + } $pageIds = $this->getPageSet()->getAllTitlesByNamespace(); if (!empty($pageIds[NS_IMAGE])) { + + $result = $this->getResult(); foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) { $title = Title :: makeTitle(NS_IMAGE, $dbKey); @@ -77,7 +75,7 @@ class ApiQueryImageInfo extends ApiQueryBase { // Check that the current version is within the start-end boundaries if((is_null($params['start']) || $img->getTimestamp() <= $params['start']) && (is_null($params['end']) || $img->getTimestamp() >= $params['end'])) { - $data[] = $this->getInfo($img); + $data[] = self::getInfo( $img, $prop, $result, $scale ); } // Now get the old revisions @@ -92,11 +90,11 @@ class ApiQueryImageInfo extends ApiQueryBase { $this->setContinueEnumParameter('start', $oldie->getTimestamp()); break; } - $data[] = $this->getInfo($oldie); + $data[] = $this->getInfo( $oldie, $prop, $result ); } } - $this->getResult()->addValue(array( + $result->addValue(array( 'query', 'pages', intval($pageId)), 'imagerepository', $repository ); @@ -111,45 +109,46 @@ class ApiQueryImageInfo extends ApiQueryBase { * @param File f The image * @return array Result array */ - protected function getInfo($f) { + static function getInfo($file, $prop, $result, $scale = null) { $vals = array(); - if($this->fld_timestamp) - $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $f->getTimestamp()); - if($this->fld_user) { - $vals['user'] = $f->getUser(); - if(!$f->getUser('id')) + if( isset( $prop['timestamp'] ) ) + $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $file->getTimestamp()); + if( isset( $prop['user'] ) ) { + $vals['user'] = $file->getUser(); + if( !$file->getUser( 'id' ) ) $vals['anon'] = ''; } - if($this->fld_size) { - $vals['size'] = intval($f->getSize()); - $vals['width'] = intval($f->getWidth()); - $vals['height'] = intval($f->getHeight()); + if( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) { + $vals['size'] = intval( $file->getSize() ); + $vals['width'] = intval( $file->getWidth() ); + $vals['height'] = intval( $file->getHeight() ); } - if($this->fld_url) { - if($this->scale && !$f->isOld()) { - $thumb = $f->getThumbnail($this->urlwidth, $this->urlheight); - if($thumb) + if( isset( $prop['url'] ) ) { + if( !is_null( $scale ) && !$file->isOld() ) { + $thumb = $file->getThumbnail( $scale['width'], $scale['height'] ); + if( $thumb ) { $vals['thumburl'] = wfExpandUrl( $thumb->getURL() ); $vals['thumbwidth'] = $thumb->getWidth(); $vals['thumbheight'] = $thumb->getHeight(); } } - $vals['url'] = $f->getFullURL(); + $vals['url'] = $file->getFullURL(); } - if($this->fld_comment) - $vals['comment'] = $f->getDescription(); - if($this->fld_sha1) - $vals['sha1'] = wfBaseConvert($f->getSha1(), 36, 16, 40); - if($this->fld_mime) - $vals['mime'] = $f->getMimeType(); - if($this->fld_metadata) { - $metadata = unserialize($f->getMetadata()); - $vals['metadata'] = $metadata ? $metadata : null; - $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta'); + if( isset( $prop['comment'] ) ) + $vals['comment'] = $file->getDescription(); + if( isset( $prop['sha1'] ) ) + $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 ); + if( isset( $prop['metadata'] ) ) { + $metadata = $file->getMetadata(); + $vals['metadata'] = $metadata ? unserialize( $metadata ) : null; + $result->setIndexedTagName_recursive( $vals['metadata'], 'meta' ); } - if($this->fld_archivename && $f->isOld()) - $vals['archivename'] = $f->getArchiveName(); + if( isset( $prop['mimetype'] ) ) + $vals['mime'] = $file->getMimeType(); + + if( isset( $prop['archivename'] ) && $file->isOld() ) + $vals['archivename'] = $file->getArchiveName(); return $vals; } -- 2.20.1