Example #1
0
 /**
  * Get all of the network comments and import them into the local database 
  *
  * @return void
  */
 public static function getAllComments($time_interval = null)
 {
     //Go through the item table in blocks of 1000
     switch (strtolower($time_interval)) {
         case 'hour':
         case 'day':
         case 'week':
         case 'month':
         case 'year':
             $time_constraint = " AND DATE_SUB(CURDATE(), INTERVAL 1 {$time_interval}) <= created";
             break;
         default:
             $time_constraint = '';
     }
     global $dbh;
     $block_size = 1000;
     $items = array();
     $sql = 'SELECT max(itemID) AS maxID FROM ' . FEED_DB . '.item';
     $last_id_query = DB::getArray($sql, __LINE__, __FILE__);
     $max_id = $last_id_query[0]['maxID'];
     $last_item = $max_id;
     echo "starting backwards count from last id {$last_item} \n";
     while ($last_item > 0) {
         $xids = array();
         $i = 0;
         while ($i < $block_size) {
             if (!$items) {
                 if ($last_item < 0) {
                     $i = $block_size;
                 } else {
                     $first_item = $last_item - $block_size;
                     $sql = 'SELECT itemID FROM ' . FEED_DB . '.item WHERE itemID > ' . $first_item . ' AND itemID <= ' . $last_item . $time_constraint;
                     $items = DB::getArray($sql, __LINE__, __FILE__);
                     $last_item = $first_item;
                     if (count($items) == 0) {
                         $i = $block_size;
                     }
                 }
             }
             while ($row = array_pop($items)) {
                 $xids[] = $row['itemID'];
                 if (++$i >= $block_size) {
                     break;
                 }
             }
         }
         if (!$xids) {
             echo 'Empty result; we are done!' . "\n";
             break;
         }
         echo 'sending block of ' . count($xids) . ' feed ids to facebook' . "\n";
         echo 'feed item ids: ' . $xids[0] . '-' . end($xids) . "\n";
         $xid_sql = implode(',', DB::s($xids));
         // pull comments for xids
         try {
             $fql = "select xid , post_id, fromid, time , text , id , username , reply_xid from comment where xid in ({$xid_sql})";
             $results = self::getClient()->api_client->fql_query($fql);
         } catch (Exception $e) {
             $results = array('error_code' => $e->getCode(), 'error_msg' => $e->getMessage());
         }
         // if an error occured, log it
         if (isset($results['error_code'])) {
             self::log('facebook getComments-' . $uid . '-' . $results['error_msg']);
         } elseif ($results) {
             echo 'received ' . count($results) . ' comments from facebook' . "\n";
             $values = array();
             foreach ($results as $row) {
                 if ($feedItem = feed_item_from_id($row['xid'])) {
                     // if album is empty skip it
                     $netUser = new User();
                     if (!$netUser->getByNetworkID(self::getNetwork(), $row['fromid'])) {
                         $netUser = UserHelper::create(null, null, null, null, self::getNetwork(), $row['fromid']);
                     }
                     // format data how wib likes it
                     try {
                         $comments[] = $feedItem->addComment($netUser, $row['text'], $row['time'], self::getNetwork()->getName() . ':' . $row['id']);
                     } catch (Exception $ex) {
                         //If no id, it's likely a dupe; just continue
                     }
                 }
             }
         }
     }
     return;
 }