Beispiel #1
0
 /**
  * Returns a singleton instance of the Zend_Db_Table object for this table.
  * 
  * @param string $table
  * @return Zend_Db_Table
  */
 public static function getInstance($table)
 {
     if (!isset(self::$_instances[$table])) {
         self::$_instances[$table] = new Zend_Db_Table(array('name' => $table, 'db' => Yadda_Db::getInstance()));
     }
     return self::$_instances[$table];
 }
Beispiel #2
0
 /**
  * Lazy loads the database connection.
  * 
  * @return Zend_Db_Adapter_Abstract
  */
 public static function getInstance()
 {
     if (self::$_instance === null) {
         $config = Zend_Registry::get('config');
         self::$_instance = Zend_Db::factory($config->db->driver, $config->db->config->toArray());
         self::$_instance->query("SET NAMES 'UTF8'");
     }
     return self::$_instance;
 }
Beispiel #3
0
 /**
  * Returns the ID of the user with the provided email address. If the user
  * account doesn't exist, it is created. If the user account exists but is
  * inactive, an exception is thrown.
  * 
  * @param string $email
  * @param string|null $name If this is a new user, $name will be used as the
  *                          user's name.
  * @return array
  * @throws Yadda_Model_Exception
  */
 public static function findByEmail($email, $name = null)
 {
     $userDb = Yadda_Db_Table::getInstance('user');
     $user = $userDb->fetchRow(array('email = ?' => $email));
     if ($user !== null) {
         if ($user->status != 'active') {
             throw new Yadda_Model_Exception('Your account is no longer active.');
         }
     } else {
         $userId = $userDb->insert(array('email' => $email, 'name' => $name, 'created' => Yadda_Db::now()));
         $user = $userDb->fetchRow(array('email = ?' => $email));
     }
     return self::toArray($user);
 }
Beispiel #4
0
 public static function pull($from, $to)
 {
     $db = Yadda_Db::getInstance();
     $days = array();
     $cur = $from;
     while ($cur <= $to) {
         $days[$cur] = array();
         // get the number of deals for this day
         $select = $db->select()->from('deal', array('cnt' => 'COUNT(1)'))->where('DATE_FORMAT(created, \'%Y-%m-%d\') = ?', $cur);
         $row = $db->fetchRow($select);
         $days[$cur]['deals'] = (int) $row['cnt'];
         // get the number of votes for the day
         $select = $db->select()->from('vote', array('cnt' => 'COUNT(1)'))->where('DATE_FORMAT(FROM_UNIXTIME(created), \'%Y-%m-%d\') = ?', $cur);
         $row = $db->fetchRow($select);
         $days[$cur]['votes'] = (int) $row['cnt'];
         // get the number of subscriptions for the day
         $select = $db->select()->from('subscription', array('cnt' => 'COUNT(1)'))->where('DATE_FORMAT(created, \'%Y-%m-%d\') = ?', $cur);
         $row = $db->fetchRow($select);
         $days[$cur]['subscriptions'] = (int) $row['cnt'];
         $cur = date('Y-m-d', strtotime('+1 day', strtotime($cur)));
     }
     return $days;
 }
Beispiel #5
0
 public static function unsubscribe($id, $hash)
 {
     // validate - id
     if (empty($id)) {
         throw new Yadda_Model_Exception('Please provide a subscription ID.');
     }
     $id = (int) $id;
     $subscriptionDb = Yadda_Db_Table::getInstance('subscription');
     $subscription = $subscriptionDb->fetchRow(array('id = ?' => $id, 'status = ?' => 'active'));
     if ($subscription === null) {
         throw new Yadda_Model_Exception('There is no subscription with the provided ID.');
     }
     // validate - hash
     if (empty($hash)) {
         throw new Yadda_Model_Exception('Please provide an authentication hash.');
     }
     $config = Zend_Registry::get('config');
     if ($hash != md5($id . $config->secret)) {
         throw new Yadda_Model_Exception('Please provide a valid authentication hash.');
     }
     // ok all good -- unsub
     $subscriptionDb->update(array('status' => 'deleted', 'modified' => Yadda_Db::now()), array('id = ?' => $id));
 }
Beispiel #6
0
<?php

include_once '../bootstrap.php';
ini_set('memory_limit', '1024M');
set_time_limit(0);
// create logger
$logger = Yadda_Log::getInstance();
// get all feeds that have last_fetch = null or last_fetch more than 30m ago
$feedDb = Yadda_Db_Table::getInstance('feed');
$select = $feedDb->select()->setIntegrityCheck(false)->from('feed')->joinLeft('site', 'feed.site_id = site.id', array())->joinLeft('region', 'feed.region_id = region.id', array('region_lat' => 'lat', 'region_long' => 'long'))->where('feed.engine = ?', 'vuvuplaza')->order('feed.created');
$feeds = $feedDb->fetchAll($select);
foreach ($feeds as $feed) {
    $logger->log('Processing feed for region "' . $feed->region_id . '" from site "' . $feed->site_id . '"', Zend_Log::DEBUG);
    $logger->log('URL: ' . $feed->url, Zend_Log::DEBUG);
    $logger->log('Last fetch: ' . $feed->last_fetch, Zend_Log::DEBUG);
    try {
        Yadda_Model_Feed::import($feed);
        // mark the feed as fetched
        $feedDb->update(array('last_fetch' => Yadda_Db::now(), 'modified' => Yadda_Db::now()), array('id = ?' => $feed->id));
    } catch (Exception $e) {
        $logger->log('Exception: ' . (string) $e, Zend_Log::ERR);
    }
    sleep(rand(0, 10));
}
Beispiel #7
0
 /**
  * Deletes the specified site.
  * 
  * @param string $id
  * @return void
  */
 public static function delete($id)
 {
     $site = self::find($id);
     $siteDb = Yadda_Db_Table::getInstance('site');
     $siteDb->update(array('status' => 'deleted', 'modified' => Yadda_Db::now()), array('id = ?' => $site['id']));
 }
Beispiel #8
0
<?php

include_once '../bootstrap.php';
set_time_limit(0);
$logger = Yadda_Log::getInstance();
// get all images that need to be fetched
$imageDb = Yadda_Db_Table::getInstance('image');
$select = $imageDb->select()->from('image')->where('status = ?', 'new')->where('source LIKE ?', 'http%')->where('created >= ?', date('Y-m-d H:i:s', strtotime('-1 day')));
$images = $imageDb->fetchAll($select);
foreach ($images as $image) {
    // mark the image as 'fetching' -- if the update doesn't return > 0 then
    // either the row has been deleted or it is already being fetched by
    // another process
    $rows = $imageDb->update(array('status' => 'fetching', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id));
    // we need to process this image
    if ($rows > 0) {
        $logger->log('Fetching image #' . $image->id, Zend_Log::INFO);
        $logger->log('Source: ' . $image->source, Zend_log::DEBUG);
        try {
            Yadda_Model_Image::fetch($image);
            // mark the image as 'active'
            $image->getTable()->update(array('status' => 'active', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id));
        } catch (Zend_Exception $e) {
            $logger->log('Exception: ' . (string) $e, Zend_Log::ERR);
            // reset the image to 'new' so that it can be fetched next time
            $image->getTable()->update(array('status' => 'new', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id));
        }
        sleep(rand(0, 10));
    }
}
Beispiel #9
0
 /**
  * Adds a deal to the featured deals roll.
  * 
  * @param int $id
  * @return void
  */
 public static function feature($id)
 {
     $deal = self::find($id);
     $db = Yadda_Db::getInstance();
     $db->insert('feature', array('deal_id' => $deal['id'], 'created' => time()));
 }
Beispiel #10
0
 /**
  * Imports the deals from a feed.
  * 
  * @param Zend_Db_Table_Row $feed
  * @throws Yadda_Model_Exception
  * @throws Yadda_Feed_Exception
  * @return void
  */
 public static function import($feed)
 {
     if (!isset(self::$engines[$feed->engine])) {
         throw new Yadda_Model_Exception('Unknown feed type "' . $feed->engine . '"');
     }
     $class = 'Yadda_Feed_Engine_' . ucfirst(strtolower($feed->engine));
     $engine = new $class();
     $stubs = $engine->import($feed);
     $logger = Yadda_Log::getInstance();
     $dealDb = Yadda_Db_Table::getInstance('deal');
     foreach ($stubs as $stub) {
         ob_start();
         print_r($stub);
         $logger->log('Stub:' . "\n" . ob_get_clean(), Zend_Log::DEBUG);
         // make sure we have the bare minimum
         if ($stub->getGuid() === null || $stub->getTitle() === null) {
             $logger->log('Rejected: Stub doesn\'t have GUID or title.', Zend_Log::DEBUG);
             continue;
         }
         // do we already have this guid for this site?
         $deal = $dealDb->fetchRow(array('site_id = ?' => $feed->site_id, 'guid = ?' => $stub->getGuid()));
         if ($deal !== null) {
             continue;
         }
         // do we already have an item with this title for this site today?
         $deal = $dealDb->fetchRow(array('site_id = ?' => $feed->site_id, 'title = ?' => $stub->getTitle(), 'DATE_FORMAT(display_date, \'%Y%m%d\') = ?' => date('Ymd', strtotime($stub->getDate()))));
         if ($deal !== null) {
             continue;
         }
         $logger->log('New entry: ' . $stub->getGuid(), Zend_Log::INFO);
         // determine geo coords
         $geo = $stub->getGeo();
         if ($geo === null && $feed->region_lat !== null && $feed->region_long !== null) {
             // get deal title
             $title = $stub->getTitle();
             $title = utf8_decode($title);
             $matches = array();
             preg_match_all('#(at|from)\\s+((([A-Z][\\w\']+)\\s*)+)#', $title, $matches);
             foreach ($matches[2] as $match) {
                 $logger->log('Testing "' . $match . '" for coords', Zend_Log::DEBUG);
                 $match = trim($match);
                 // remove prices/stopwords/etc.
                 if (preg_match('/^R\\d+$/', $match)) {
                     continue;
                 }
                 if ($match == 'On') {
                     continue;
                 }
                 // get coordinates
                 $test = Yadda_Model_Deal::getGeoFromPlaceName(array((double) $feed->region_lat, (double) $feed->region_long), $match);
                 if ($test !== null) {
                     $logger->log('Found geo coords: ' . join(' ', $test), Zend_Log::INFO);
                     $geo = $test;
                     break;
                 }
             }
         }
         // insert the new deal
         $dealDb->getAdapter()->beginTransaction();
         try {
             $insert = array('site_id' => $feed->site_id, 'region_id' => $feed->region_id, 'guid' => $stub->getGuid(), 'title' => $stub->getTitle(), 'description' => $stub->getDescription(), 'link' => $stub->getLink(), 'display_date' => date('Y-m-d H:i:s', strtotime($stub->getDate())), 'price' => $stub->getPrice(), 'value' => $stub->getValue(), 'discount' => $stub->getDiscount(), 'lat' => $geo !== null ? $geo[0] : null, 'long' => $geo !== null ? $geo[1] : null, 'status' => 'active', 'created' => Yadda_Db::now());
             $dealId = $dealDb->insert($insert);
             // check for images
             foreach ($stub->getImages() as $image) {
                 $logger->log('New image: ' . $image, Zend_Log::INFO);
                 $imageId = $dealDb->getAdapter()->insert('image', array('deal_id' => $dealId, 'source' => $image, 'created' => Yadda_Db::now()));
             }
             $dealDb->getAdapter()->commit();
         } catch (Zend_Exception $e) {
             $dealDb->getAdapter()->rollBack();
             throw new Yadda_Feed_Exception('Error importing deal: ' . $e->getMessage());
         }
     }
 }