You are here

function twithash_block_update in Heartbeat 8

Update tracking of hashtag trends

1 call to twithash_block_update()
twit_submit_block_submit_callback in modules/statusmessage/includes/twit.php

File

modules/statusmessage/includes/twit.php, line 53

Code

function twithash_block_update($hashArray, $unixtime, $uid, $ip, $tweetId = NULL, $location = NULL) {
  $tmid = null;
  if ($location == NULL) {
    if ($ip == '127.0.0.1') {
      $geo = 1;
    }
    else {
      $geo = 1;
    }

    //Handling of non-local IPs to be added later
  }
  else {
    $checkLocQuery = db_query(' SELECT id FROM twithash_geo
        WHERE country = :country
        AND city = :city
        AND region = :region', array(
      ':country' => 'Canada',
      ':city' => $location->city,
      ':region' => $location->province,
    ));
    $locResult = $checkLocQuery
      ->fetchAll();
    if ($checkLocQuery
      ->rowCount() > 0) {
      $geo = $locResult[0]->id;
    }
    else {
      $locInsert = db_insert('twithash_geo')
        ->fields(array(
        'country' => 'Canada',
        'city' => $location->city,
        'region' => $location->province,
      ));
      $locId = $locInsert
        ->execute();
      if ($locId != NULL && $locId > 0) {
        $geo = $locId;
      }
    }
  }
  $count = count($hashArray);

  //Populate twithash_term table with new terms or update number of hits for recurring terms. Simultaneously update twithash_term_update table which

  //tracks specific dates for each time a given term is searched.
  for ($i = 0; $i < $count; $i++) {
    $keyword = $hashArray[$i];
    $transaction = db_transaction();
    try {
      $tID = db_query('insert into twithash_term (term, hits, start) values (:term, 1, :start) on DUPLICATE KEY UPDATE hits = hits + :hits', array(
        ':term' => $keyword,
        ':start' => $unixtime,
        ':hits' => 1,
      ), array(
        'return' => Database::RETURN_INSERT_ID,
      ));

      //Insert IDs are collected for use in the query_master table, which tracks which different terms were compared up to a maximum

      //of 5 terms (the maximum allowed by Google Trends)
      if ($tID != 0) {
        db_insert('twithash_term_update')
          ->fields(array(
          't_id' => $tID,
          'hit_time' => $unixtime,
        ))
          ->execute();
      }
      $tIDs[] = $tID;
    } catch (Exception $e) {
      $transaction
        ->rollback();
      throw $e;
    }
  }
  $numTerms = isset($tIDs) ? count($tIDs) : 0;

  //Get number of terms in query
  switch ($numTerms) {

    //add overall query to twithash_master
    case 0:
      break;
    case 1:
      $tmid = db_query('
        INSERT INTO twithash_master (uid, query_date, geo, source, tid_1) VALUES (:uid, :query_date, :geo, :source, :tid_1)', array(
        ':uid' => $uid,
        ':query_date' => $unixtime,
        ':geo' => $geo,
        ':source' => 1,
        ':tid_1' => $tIDs[0],
      ), array(
        'return' => Database::RETURN_INSERT_ID,
      ));
      break;
    case 2:
      $tmid = db_query('insert into twithash_master (uid, query_date, geo, source, tid_1, tid_2) values (:uid, :query_date, :geo, :source, :tid_1, :tid_2)', array(
        ':uid' => $uid,
        ':query_date' => $unixtime,
        ':geo' => $geo,
        ':source' => 1,
        ':tid_1' => $tIDs[0],
        ':tid_2' => $tIDs[1],
      ), array(
        'return' => Database::RETURN_INSERT_ID,
      ));
      break;
    case 3:
      $tmid = db_query('insert into twithash_master (uid, query_date, geo, source, tid_1, tid_2, tid_3) values (:uid, :query_date, :geo, :source, :tid_1, :tid_2, :tid_3)', array(
        ':uid' => $uid,
        ':query_date' => $unixtime,
        ':geo' => $geo,
        ':source' => 1,
        ':tid_1' => $tIDs[0],
        ':tid_2' => $tIDs[1],
        ':tid_3' => $tIDs[2],
      ), array(
        'return' => Database::RETURN_INSERT_ID,
      ));
      break;
    case 4:
      $tmid = db_query('insert into twithash_master (uid, query_date, geo, source, tid_1, tid_2, tid_3, tid_4) values (:uid, :query_date, :geo, :source, :tid_1, :tid_2, :tid_3, :tid_4)', array(
        ':uid' => $uid,
        ':query_date' => $unixtime,
        ':geo' => $geo,
        ':source' => 1,
        ':tid_1' => $tIDs[0],
        ':tid_2' => $tIDs[1],
        ':tid_3' => $tIDs[2],
        ':tid_4' => $tIDs[3],
      ), array(
        'return' => Database::RETURN_INSERT_ID,
      ));
      break;
    case 5:
      $tmid = db_query('insert into twithash_master (uid, query_date, geo, source, tid_1, tid_2, tid_3, tid_4, tid_5) values (:uid, :query_date, :geo, :source, :tid_1, :tid_2, :tid_3, :tid_4, :tid_5)', array(
        ':uid' => $uid,
        ':query_date' => $unixtime,
        ':geo' => $geo,
        ':source' => 1,
        ':tid_1' => $tIDs[0],
        ':tid_2' => $tIDs[1],
        ':tid_3' => $tIDs[2],
        ':tid_4' => $tIDs[3],
        ':tid_5' => $tIDs[4],
      ), array(
        'return' => Database::RETURN_INSERT_ID,
      ));
      break;
  }
  if ($tweetId != NULL && $tmid != NULL) {
    $tweetIdQuery = db_insert('twithash_tid')
      ->fields(array(
      'tweetId' => $tweetId,
      'thmid' => $tmid,
    ))
      ->execute();
  }
  return $tmid;
}