Author: Christer Edvartsen (cogo@starzinger.net)
Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function getImages($publicKey, Query $query, Images $model)
 {
     $images = array();
     $qb = $this->getConnection()->createQueryBuilder();
     $qb->select('*')->from($this->tableNames['imageinfo'], 'i')->where('i.publicKey = :publicKey')->setParameter(':publicKey', $publicKey);
     if ($sort = $query->sort()) {
         // Fields valid for sorting
         $validFields = array('size' => true, 'publicKey' => true, 'imageIdentifier' => true, 'extension' => true, 'mime' => true, 'added' => true, 'updated' => true, 'width' => true, 'height' => true, 'checksum' => true, 'originalChecksum' => true);
         foreach ($sort as $f) {
             if (!isset($validFields[$f['field']])) {
                 throw new InvalidArgumentException('Invalid sort field: ' . $f['field'], 400);
             }
             $qb->addOrderBy($f['field'], $f['sort']);
         }
     } else {
         $qb->orderBy('added', 'DESC');
     }
     $from = $query->from();
     $to = $query->to();
     if ($from || $to) {
         if ($from !== null) {
             $qb->andWhere('added >= :from')->setParameter(':from', $from);
         }
         if ($to !== null) {
             $qb->andWhere('added <= :to')->setParameter(':to', $to);
         }
     }
     if ($imageIdentifiers = $query->imageIdentifiers()) {
         $expr = $qb->expr();
         $composite = $expr->orX();
         foreach ($imageIdentifiers as $i => $id) {
             $composite->add($expr->eq('i.imageIdentifier', ':imageIdentifier' . $i));
             $qb->setParameter(':imageIdentifier' . $i, $id);
         }
         $qb->andWhere($composite);
     }
     if ($checksums = $query->checksums()) {
         $expr = $qb->expr();
         $composite = $expr->orX();
         foreach ($checksums as $i => $id) {
             $composite->add($expr->eq('i.checksum', ':checksum' . $i));
             $qb->setParameter(':checksum' . $i, $id);
         }
         $qb->andWhere($composite);
     }
     if ($originalChecksums = $query->originalChecksums()) {
         $expr = $qb->expr();
         $composite = $expr->orX();
         foreach ($originalChecksums as $i => $id) {
             $composite->add($expr->eq('i.originalChecksum', ':originalChecksum' . $i));
             $qb->setParameter(':originalChecksum' . $i, $id);
         }
         $qb->andWhere($composite);
     }
     // Create a querybuilder that will be used to fetch the hits number, and update the model
     $hitsQb = clone $qb;
     $hitsQb->select('COUNT(i.id)');
     $stmt = $hitsQb->execute();
     $model->setHits((int) $stmt->fetchColumn());
     if ($limit = $query->limit()) {
         $qb->setMaxResults($limit);
     }
     if ($page = $query->page()) {
         $offset = (int) $query->limit() * ($page - 1);
         $qb->setFirstResult($offset);
     }
     $stmt = $qb->execute();
     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
     $returnMetadata = $query->returnMetadata();
     foreach ($rows as $row) {
         $image = array('extension' => $row['extension'], 'added' => new DateTime('@' . $row['added'], new DateTimeZone('UTC')), 'updated' => new DateTime('@' . $row['updated'], new DateTimeZone('UTC')), 'checksum' => $row['checksum'], 'originalChecksum' => isset($row['originalChecksum']) ? $row['originalChecksum'] : null, 'publicKey' => $row['publicKey'], 'imageIdentifier' => $row['imageIdentifier'], 'mime' => $row['mime'], 'size' => (int) $row['size'], 'width' => (int) $row['width'], 'height' => (int) $row['height']);
         if ($returnMetadata) {
             $image['metadata'] = $this->getMetadata($publicKey, $row['imageIdentifier']);
         }
         $images[] = $image;
     }
     return $images;
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider getSortData
  */
 public function testCanSortImages(array $sort = null, $field, array $values)
 {
     $this->insertImages();
     $query = new Query();
     if ($sort !== null) {
         $query->sort($sort);
     }
     $images = $this->adapter->getImages('publickey', $query, $this->getMock('Imbo\\Model\\Images'));
     foreach ($images as $i => $image) {
         $this->assertSame($values[$i], $image[$field]);
     }
 }
Ejemplo n.º 3
0
 /**
  * @expectedException Imbo\Exception\RuntimeException
  * @expectedExceptionMessage Badly formatted sort
  * @expectedExceptionCode 400
  */
 public function testSortThrowsExceptionWhenTheStortStringIsBadlyFormatted()
 {
     $this->query->sort(array('field:asc', ''));
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function getImages($publicKey, Query $query, Images $model)
 {
     // Initialize return value
     $images = [];
     // Query data
     $queryData = ['publicKey' => $publicKey];
     $from = $query->from();
     $to = $query->to();
     if ($from || $to) {
         $tmp = [];
         if ($from !== null) {
             $tmp['$gte'] = $from;
         }
         if ($to !== null) {
             $tmp['$lte'] = $to;
         }
         $queryData['added'] = $tmp;
     }
     $imageIdentifiers = $query->imageIdentifiers();
     if (!empty($imageIdentifiers)) {
         $queryData['imageIdentifier']['$in'] = $imageIdentifiers;
     }
     $checksums = $query->checksums();
     if (!empty($checksums)) {
         $queryData['checksum']['$in'] = $checksums;
     }
     $originalChecksums = $query->originalChecksums();
     if (!empty($originalChecksums)) {
         $queryData['originalChecksum']['$in'] = $originalChecksums;
     }
     // Sorting
     $sort = ['added' => -1];
     if ($querySort = $query->sort()) {
         $sort = [];
         foreach ($querySort as $s) {
             $sort[$s['field']] = $s['sort'] === 'asc' ? 1 : -1;
         }
     }
     // Fields to fetch
     $fields = array_fill_keys(['extension', 'added', 'checksum', 'originalChecksum', 'updated', 'publicKey', 'imageIdentifier', 'mime', 'size', 'width', 'height'], true);
     if ($query->returnMetadata()) {
         $fields['metadata'] = true;
     }
     try {
         $cursor = $this->getImageCollection()->find($queryData, $fields)->limit($query->limit())->sort($sort);
         // Skip some images if a page has been set
         if (($page = $query->page()) > 1) {
             $skip = $query->limit() * ($page - 1);
             $cursor->skip($skip);
         }
         foreach ($cursor as $image) {
             unset($image['_id']);
             $image['added'] = new DateTime('@' . $image['added'], new DateTimeZone('UTC'));
             $image['updated'] = new DateTime('@' . $image['updated'], new DateTimeZone('UTC'));
             $images[] = $image;
         }
         // Update model
         $model->setHits($cursor->count());
     } catch (MongoException $e) {
         throw new DatabaseException('Unable to search for images', 500, $e);
     }
     return $images;
 }