You are here

function biblio_delete_orphan_keywords in Bibliography Module 6.2

Same name and namespace in other branches
  1. 6 biblio.keywords.inc \biblio_delete_orphan_keywords()
  2. 7 includes/biblio.keywords.inc \biblio_delete_orphan_keywords()
  3. 7.2 includes/biblio.keywords.inc \biblio_delete_orphan_keywords()

Parameters

bool $force: (optional)

2 calls to biblio_delete_orphan_keywords()
BiblioKeywordUnitTest::testBiblioDeleteOrphanKeywords in tests/keyword.test
biblio_cron in ./biblio.module
Implements hook_cron().

File

includes/biblio.keywords.inc, line 88
Keywords related functions for Drupal biblio module.

Code

function biblio_delete_orphan_keywords($force = FALSE) {
  if (variable_get('biblio_keyword_orphan_autoclean', 0) || $force) {
    $active_kids = array();
    $all_kids = array();
    $result = db_query('SELECT kid FROM {biblio_keyword} GROUP BY kid');
    while ($kw = db_fetch_object($result)) {
      $active_kids[] = $kw->kid;
    }
    $result = db_query('SELECT kid FROM {biblio_keyword_data} GROUP BY kid');
    while ($kw = db_fetch_object($result)) {
      $all_kids[] = $kw->kid;
    }
    $orphans = array_diff($all_kids, $active_kids);
    if (!empty($orphans)) {
      db_query('DELETE FROM {biblio_keyword_data} WHERE kid IN (' . implode(',', $orphans) . ')');
      $count = db_affected_rows();
      $message = t('%count orphaned keywords were deleted from the biblio_keyword_data table.', array(
        '%count' => $count,
      ));
      watchdog('biblio_cron', $message);
    }
  }
}