You are here

function twituser_block_update in Heartbeat 8

Parameters

$userArray:

$unixtime:

$uid:

$ip:

null $tweetId:

null $location:

Return value

DatabaseStatementInterface|int|null

Throws

Exception

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

File

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

Code

function twituser_block_update($userArray, $unixtime, $uid, $ip, $tweetId = NULL, $location = NULL) {
  $tumid = null;

  // watchdog('twituser', var_dump($userArray));
  if ($ip == '127.0.0.1' && $location == NULL) {
    $geo = 1;
  }
  else {
    $location = smart_ip_get_location($ip);
    $geo = 1;
  }

  //Handling of non-local IPs to be added later
  $count = count($userArray);

  //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++) {
    $userName = $userArray[$i];
    $transaction = db_transaction();
    try {
      $tID = db_query('insert into twituser_name (name, hits, start) values (:name, 1, :start) on DUPLICATE KEY UPDATE hits = hits + :hits', array(
        ':name' => $userName,
        ':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('twituser_name_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:
      $tumid = db_query('insert into twituser_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:
      $tumid = db_query('insert into twituser_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:
      $tumid = db_query('insert into twituser_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:
      $tumid = db_query('insert into twituser_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:
      $tumid = db_query('insert into twituser_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 && $tumid != NULL) {
    $tweetIdQuery = db_insert('twituser_tid')
      ->fields(array(
      'tweetId' => $tweetId,
      'tumid' => $tumid,
    ))
      ->execute();
  }
  return $tumid > 0 ? $tumid : -1;
}