You are here

radioactivity_arbitrary_target.module in Radioactivity 5

File

plugins/radioactivity_arbitrary_target.module
View source
<?php

function radioactivity_arbitrary_target_help($section = '') {
  $output = '';
  switch ($section) {
    case "admin/help#radioactivity_arbitrary_target":
      $output = '<p>' . t('Provides support for arbitrary targets with non-numeric ids, such as urls. ' . 'You could use this to include non-local objects into your local ' . 'radioactivity database. Note that this ' . 'module is not very useful alone, but consider combining with ' . '<code>radioactivity_arbitrary_source</code> ' . 'and <code>radioactivity_http</code> for integration purposes.') . '</p>';
      break;
  }
  return $output;
}
function radioactivity_arbitrary_target_radioactivity_info() {
  return array(
    'targets' => array(
      'arb' => array(
        'id_mapper' => '_radioactivity_arbitrary_target_id_mapper',
      ),
    ),
    'sources' => array(
      'arb' => array(),
    ),
  );
}
function _radioactivity_arbitrary_target_id_mapper($oid, $oclass) {

  // check if we already have $oid mapped
  $result = db_query("SELECT rad_id FROM {radioactivity_arbitrary_target} WHERE arb_id='%s'", $oid);
  $rad_id = db_result($result);
  if ($rad_id) {
    return $rad_id;
  }

  // no mapping yet, so create one
  $rad_id = db_next_id('{radioactivity_arbitrary_target}_rad_id');
  db_query("INSERT INTO {radioactivity_arbitrary_target} (rad_id, arb_id) VALUES (%d, '%s')", $rad_id, $oid);
  return $rad_id;
}
function radioactivity_arbitrary_target_cron() {

  // clean up radioactivity_arbitrary_target
  $result = db_query("SELECT at.rad_id\n                FROM {radioactivity_arbitrary_target} at\n                     LEFT JOIN {radioactivity} r ON (at.rad_id = r.id)\n               WHERE r.id IS NULL");
  $rad_ids = array();
  $count = 0;
  while ($rad_id = db_result($result)) {
    $rad_ids[] = $rad_id;
    $count++;
    if ($count == 500) {
      break;
    }

    // clean up at most 500 ids per cron run
  }
  if (count($rad_ids)) {
    db_query("DELETE FROM {radioactivity_arbitrary_target} WHERE rad_id IN (" . implode(',', $rad_ids) . ')');
  }
}