You are here

function _biblio_move_authors in Bibliography Module 6.2

Moves author data into biblio_contributor* tables.

This function ...

Return value

array An associative array.

1 call to _biblio_move_authors()
biblio_update_6000 in ./biblio.install
Update ...

File

./biblio.install, line 1993
Install, update, and uninstall functions for the biblio module.

Code

function _biblio_move_authors(&$result) {
  $disable = FALSE;

  // If the biblio module is disabled, enable it so drupal_get_schema will work.
  if (!module_exists('biblio')) {
    module_enable(array(
      'biblio',
    ));
    $disable = TRUE;
  }
  drupal_get_schema('biblio_contributor', TRUE);
  drupal_get_schema('biblio_contributor_data', TRUE);

  // Tthis update will move author information from existing {biblio} table to
  // the new {biblio_contributor_data} table and create the appropriate cross-
  // reference links in the {biblio_contributor) table.
  require_once drupal_get_path('module', 'biblio') . '/includes/biblio.contributors.inc';
  $res = db_query("SELECT nid,vid,biblio_authors, biblio_secondary_authors,biblio_tertiary_authors,biblio_corp_author FROM {biblio}");
  $count = 0;
  $count_success = 0;
  while ($biblio = db_fetch_array($res)) {
    $biblio_contributors = array();
    if (!empty($biblio['biblio_authors'])) {
      _biblio_parse_authors($biblio_contributors, $biblio['biblio_authors'], 1);
    }
    if (!empty($biblio['biblio_secondary_authors'])) {
      _biblio_parse_authors($biblio_contributors, $biblio['biblio_secondary_authors'], 2);
    }
    if (!empty($biblio['biblio_tertiary_authors'])) {
      _biblio_parse_authors($biblio_contributors, $biblio['biblio_tertiary_authors'], 3);
    }
    if (!empty($biblio['biblio_corp_author'])) {
      _biblio_parse_authors($biblio_contributors, $biblio['biblio_corp_author'], 5);
    }
    $biblio_contributors = biblio_parse_contributors($biblio_contributors);
    if (_biblio_save_contributors($biblio_contributors, $biblio['nid'], $biblio['vid'])) {
      $count_success++;
    }
    $count++;
  }

  // Change auth_type to match overrides set in old biblio_type_details.
  update_sql("UPDATE {biblio_contributor} c\n    /* augment by biblio_type from biblio */\n    INNER JOIN {biblio} b ON c.nid=b.nid AND c.vid=b.vid\n    /* augment by old config settings */\n    INNER JOIN\n      (SELECT tid,if(fid=4,5,fid) as auth_type,title FROM\n        /* select (tid,fid) specific titles from 5.x: biblio_fields_old contains the defaults, biblio_type_details the overrides */\n        (SELECT tid,fid,title FROM {biblio_type_details} WHERE fid<=4\n         UNION SELECT tid,fid,title FROM {biblio_fields_old}, {biblio_types} WHERE fid<=4 AND tid>=100) t\n       /* grouping by tid,fid removes the duplicate default title if an override is available */\n       GROUP BY tid,fid) otd\n    /* match old config (otd) and newly imported (with type 1,2,3,5 see above) on biblio_type and auth_type */\n    ON otd.tid=b.biblio_type AND otd.auth_type=c.auth_type\n    /* augment with new auth_type based on title */\n    INNER JOIN {biblio_contributor_type_data} ctd ON ctd.title=otd.title\n    /* update auth_type in biblio_contributor table */\n    SET c.auth_type=ctd.auth_type");
  if ($count_success == $count) {
    $message = 'Moved authors from ' . $count_success . ' / ' . $count . ' publications to the new database structure';
    $contributors = array(
      1 => 'biblio_authors',
      2 => 'biblio_secondary_authors',
      3 => 'biblio_tertiary_authors',
      4 => 'biblio_subsidiary_authors',
      5 => 'biblio_corp_author',
    );

    // If the authors were sucessfully moved, remove obsolete D5 columns from
    // {biblio} table (if they are still present).
    foreach ($contributors as $column) {
      if (db_column_exists('biblio', $column)) {
        db_drop_field($result, 'biblio', $column);
      }
    }
  }
  else {
    $count_fail = $count - $count_success;
    $message = 'There was a problem moving authors from ' . $count_fail . ' / ' . $count . ' publications to the new database structure. The existing author fields have been ' . 'retained in the database, go to the "admin/settings/biblio/author" page to try again.';
  }
  $result[] = array(
    'success' => $count_success == $count,
    'query' => $message,
  );

  // If the biblio module started as disabled, then set it back to that state.
  if ($disable) {
    module_disable(array(
      'biblio',
    ));
  }
  return;
}