You are here

function _move_authors in Bibliography Module 6

1 call to _move_authors()
biblio_update_6000 in ./biblio.install

File

./biblio.install, line 1488
Install file for biblio module

Code

function _move_authors(&$result) {
  $disable = FALSE;
  if (!module_exists('biblio')) {

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

  // this update will move author information from existing biblio table to the new
  // biblio_contributor_data table and make the appropriate links in the biblio_contributor table
  require_once drupal_get_path('module', 'biblio') . '/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'])) {
      _parse_authors($biblio_contributors, $biblio['biblio_authors'], 1);
    }
    if (!empty($biblio['biblio_secondary_authors'])) {
      _parse_authors($biblio_contributors, $biblio['biblio_secondary_authors'], 2);
    }
    if (!empty($biblio['biblio_tertiary_authors'])) {
      _parse_authors($biblio_contributors, $biblio['biblio_tertiary_authors'], 3);
    }
    if (!empty($biblio['biblio_corp_author'])) {
      _parse_authors($biblio_contributors, $biblio['biblio_corp_author'], 5);
    }
    $biblio_contributors = biblio_parse_contributors($biblio_contributors);
    if (_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) {
    $mesg = '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 were sucessfully moved, remove obsolete D5 columns from biblio table (if they are present)
    foreach ($contributors as $column) {
      if (db_column_exists('biblio', $column)) {
        db_drop_field($result, 'biblio', $column);
      }
    }
  }
  else {
    $count_fail = $count - $count_success;
    $mesg = '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' => $mesg,
  );
  if ($disable) {

    // if the module was disabled, then set it back that way.
    module_disable(array(
      'biblio',
    ));
  }
  return;
}