(bug 20275) Fixed LIKE queries on SQLite backend
[lhc/web/wiklou.git] / includes / api / ApiQueryAllimages.php
index 5f0c1bc..e108a8f 100644 (file)
@@ -32,12 +32,23 @@ if (!defined('MEDIAWIKI')) {
 /**
  * Query module to enumerate all available pages.
  *
- * @addtogroup API
+ * @ingroup API
  */
 class ApiQueryAllimages extends ApiQueryGeneratorBase {
 
        public function __construct($query, $moduleName) {
                parent :: __construct($query, $moduleName, 'ai');
+               $this->mRepo = RepoGroup::singleton()->getLocalRepo();
+       }
+       
+       /**
+        * Overide parent method to make sure to make sure the repo's DB is used
+        * which may not necesarilly be the same as the local DB.
+        * 
+        * TODO: allow querying non-local repos.
+        */
+       protected function getDB() {
+               return $this->mRepo->getSlaveDB();
        }
 
        public function execute() {
@@ -52,16 +63,20 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
        }
 
        private function run($resultPageSet = null) {
+               $repo = $this->mRepo;
+               if ( !$repo instanceof LocalRepo )
+                       $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo');
 
                $db = $this->getDB();
 
                $params = $this->extractRequestParams();
 
                // Image filters
-               if (!is_null($params['from']))
-                       $this->addWhere('img_name>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
+               $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
+               $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
+               $this->addWhereRange('img_name', $dir, $from, null);
                if (isset ($params['prefix']))
-                       $this->addWhere("img_name LIKE '" . $db->escapeLike(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
+                       $this->addWhere('img_name' . $db->buildLike( $this->titlePartToKey($params['prefix']), $db->anyString() ) );
 
                if (isset ($params['minsize'])) {
                        $this->addWhere('img_size>=' . intval($params['minsize']));
@@ -84,11 +99,7 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                $this->addTables('image');
 
                $prop = array_flip($params['prop']);
-               $this->addFields('img_name');
-               $this->addFieldsIf('img_size', isset($prop['size']));
-               $this->addFieldsIf(array('img_width', 'img_height'), isset($prop['dimensions']));
-               $this->addFieldsIf(array('img_major_mime', 'img_minor_mime'), isset($prop['mime']));
-               $this->addFieldsIf('img_timestamp', isset($prop['timestamp']));
+               $this->addFields( LocalFile::selectFields() );
 
                $limit = $params['limit'];
                $this->addOption('LIMIT', $limit+1);
@@ -97,47 +108,36 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
 
                $res = $this->select(__METHOD__);
 
-               $data = array ();
+               $titles = 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...
                                // TODO: Security issue - if the user has no right to view next title, it will still be shown
-                               $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->img_name));
+                               $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
                                break;
                        }
 
                        if (is_null($resultPageSet)) {
-                               $file = wfLocalFile( $row->img_name );
-                               $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();
+                               $file = $repo->newFileFromRow( $row );
+                               $info = array_merge(array('name' => $row->img_name),
+                                       ApiQueryImageInfo::getInfo($file, $prop, $result));
+                               $fit = $result->addValue(array('query', $this->getModuleName()), null, $info);
+                               if( !$fit ) {
+                                       $this->setContinueEnumParameter('from', $this->keyToTitle($row->img_name));
+                                       break;
                                }
-                               if(isset($prop['mime']))
-                                       $item['mime'] = $row->img_major_mime . '/' . $row->img_minor_mime;
-                               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->getUrl();
-                               $data[] = $item;
                        } else {
-                               $data[] = Title::makeTitle( NS_IMAGE, $row->img_name );
+                               $titles[] = Title::makeTitle(NS_IMAGE, $row->img_name);
                        }
                }
                $db->freeResult($res);
 
                if (is_null($resultPageSet)) {
-                       $result = $this->getResult();
-                       $result->setIndexedTagName($data, 'img');
-                       $result->addValue('query', $this->getModuleName(), $data);
+                       $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img');
                } else {
-                       $resultPageSet->populateFromTitles( $data );
+                       $resultPageSet->populateFromTitles($titles);
                }
        }
 
@@ -168,14 +168,7 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                        'sha1' => null,
                        'sha1base36' => null,
                        'prop' => array (
-                               ApiBase :: PARAM_TYPE => array(
-                                       'timestamp',
-                                       'url',
-                                       'size',
-                                       'dimensions',
-                                       'mime',
-                                       'sha1'
-                               ),
+                               ApiBase :: PARAM_TYPE => ApiQueryImageInfo::getPropertyNames(),
                                ApiBase :: PARAM_DFLT => 'timestamp|url',
                                ApiBase :: PARAM_ISMULTI => true
                        )
@@ -189,7 +182,7 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
                        'dir' => 'The direction in which to list',
                        'minsize' => 'Limit to images with at least this many bytes',
                        'maxsize' => 'Limit to images with at most this many bytes',
-                       'limit' => 'How many total pages to return.',
+                       'limit' => 'How many total images to return.',
                        'sha1' => 'SHA1 hash of image',
                        'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
                        'prop' => 'Which properties to get',