View source
<?php
function biblio_get_contributor($aid) {
static $contributor = array();
if (!isset($contributor[$aid])) {
$contributor[$aid] = db_fetch_object(db_query('SELECT * FROM {biblio_contributor_data} WHERE cid = %d', $aid));
}
return $contributor[$aid];
}
function biblio_get_contributor_by_name($name) {
return db_fetch_object(db_query("SELECT * FROM {biblio_contributor_data} bcd WHERE bcd.name = '%s'", array(
$name,
)));
}
function biblio_get_first_contributor($vid) {
static $contributor = array();
if (!isset($contributor[$vid])) {
$contributor[$vid] = db_fetch_object(db_query("SELECT * FROM {biblio_contributor} bc\n INNER JOIN {biblio_contributor_data} bcd ON bc.cid=bcd.cid\n WHERE bc.vid=%d AND bc.rank=0", $vid));
}
return $contributor[$vid];
}
function biblio_load_contributors($vid) {
$contributors = array();
$query = "SELECT * FROM {biblio_contributor} bc\n INNER JOIN {biblio_contributor_data} bcd ON bc.cid=bcd.cid\n WHERE bc.vid=%d\n ORDER BY bc.rank ASC";
$result = db_query($query, $vid);
while ($creator = db_fetch_array($result)) {
$contributors[$creator['auth_category']][] = $creator;
}
return $contributors;
}
function biblio_authors_add_etal(&$authors, $type) {
$etal = "et al";
$max_rank = 0;
foreach ($authors as $author) {
if ($author['auth_type'] != $type) {
continue;
}
if ($author['name'] == $etal) {
return false;
}
$max_rank = max($max_rank, $author['rank']);
}
$authors[] = biblio_parse_author(array(
'name' => $etal,
'auth_type' => $type,
'lastname' => $etal,
'rank' => $max_rank + 1,
));
return true;
}
function biblio_parse_contributors($contributors) {
$result = array();
if (!count($contributors)) {
return;
}
foreach ($contributors as $cat => $authors) {
$etal = array();
foreach ($authors as $author) {
$author_cleaned = preg_replace("/et\\.?\\s+al\\.?/", '', $author['name']);
if ($author_cleaned != $author['name']) {
$author['name'] = $author_cleaned;
$etal[$author['auth_type']] = true;
}
$author['name'] = trim($author['name']);
if (strlen($author['name'])) {
$result[$cat][] = biblio_parse_author($author, $cat);
}
}
foreach ($etal as $type => $dummy) {
if (isset($result[$cat])) {
biblio_authors_add_etal($result[$cat], $type);
}
}
}
return $result;
}
function biblio_delete_contributors($node) {
db_query('DELETE FROM {biblio_contributor} WHERE nid = %d', array(
':nid' => $node->nid,
));
return;
}
function biblio_delete_contributors_revision($node) {
db_query('DELETE FROM {biblio_contributor} WHERE vid = %d', array(
':vid' => $node->vid,
));
$count = db_affected_rows();
return $count;
}
function biblio_delete_contributor($cid) {
db_query('DELETE FROM {biblio_contributor}
WHERE cid = %d', array(
':cid' => $cid,
));
db_query('DELETE FROM {biblio_contributor_data}
WHERE cid = %d', array(
':cid' => $cid,
));
$count = db_affected_rows();
return $count;
}
function biblio_delete_contributor_revision($cid, $vid) {
db_query('DELETE FROM {biblio_contributor}
WHERE cid = %d and vid = %d', array(
':cid' => $cid,
':vid' => $vid,
));
$count = db_affected_rows();
return $count;
}
function biblio_count_orphan_authors() {
return db_result(db_query('SELECT COUNT(*) FROM {biblio_contributor_data} bcd WHERE bcd.cid NOT IN (SELECT DISTINCT(bc.cid) FROM {biblio_contributor} bc )'));
}
function biblio_get_orphan_authors() {
$authors = array();
$result = db_query('SELECT distinct d.cid cid, name, affiliation
FROM {biblio_contributor_data} d
LEFT JOIN {biblio_contributor} c ON (d.cid=c.cid)
WHERE c.cid IS NULL');
while ($author = db_fetch_array($result)) {
$authors[] = $author;
}
return $authors;
}
function biblio_delete_orphan_authors($force = FALSE) {
if (variable_get('biblio_auto_orphaned_author_delete', 0) || $force) {
$active_cids = array();
$all_cids = array();
$result = db_query('SELECT cid FROM {biblio_contributor} GROUP BY cid');
while ($author = db_fetch_object($result)) {
$active_cids[] = $author->cid;
}
$result = db_query('SELECT cid FROM {biblio_contributor_data} GROUP BY cid');
while ($author = db_fetch_object($result)) {
$all_cids[] = $author->cid;
}
$orphans = array_diff($all_cids, $active_cids);
if (!empty($orphans)) {
db_query('DELETE FROM {biblio_contributor_data} WHERE cid IN (' . implode(',', $orphans) . ')');
$count = db_affected_rows();
$message = t('%count orphaned authors were deleted from the biblio_contributor_data table.', array(
'%count' => $count,
));
watchdog('biblio_cron', $message);
}
}
}
function biblio_insert_contributors($node) {
if (!empty($node->biblio_contributors)) {
return _save_contributors($node->biblio_contributors, $node->nid, $node->vid);
}
}
function biblio_update_contributors($node) {
if (!empty($node->biblio_contributors)) {
_save_contributors($node->biblio_contributors, $node->nid, $node->vid, TRUE);
}
return;
}
function biblio_save_contributor(&$author) {
return drupal_write_record('biblio_contributor_data', $author);
}
function biblio_update_contributor(&$author) {
if (!isset($author['cid'])) {
return false;
}
return drupal_write_record('biblio_contributor_data', $author, 'cid');
}
function _save_contributors(&$contributors, $nid, $vid, $update = FALSE) {
$md5 = _loadMD5();
$rank = 0;
db_query('DELETE FROM {biblio_contributor} WHERE nid = %d AND vid = %d', array(
$nid,
$vid,
));
foreach ($contributors as $cat => $authors) {
foreach ($authors as $key => $author) {
if (!empty($author['name'])) {
if (empty($author['lastname'])) {
$contributors[$cat][$key] = $author = biblio_parse_author($author, $cat);
}
if ($update && !empty($author['cid'])) {
$author['cid'] = null;
}
if (empty($author['cid']) && isset($author['md5']) && !empty($md5)) {
$author['cid'] = array_search($author['md5'], $md5);
}
if (empty($author['cid'])) {
biblio_save_contributor($author);
if (empty($author['cid'])) {
return false;
}
}
$link_array = array(
'nid' => $nid,
'vid' => $vid,
'cid' => $author['cid'],
'rank' => $rank++,
'auth_type' => $author['auth_type'],
'auth_category' => $cat,
);
if (!drupal_write_record('biblio_contributor', $link_array)) {
return false;
}
}
}
}
db_query("UPDATE {biblio_contributor_data} SET aka = cid WHERE aka = 0 OR aka IS NULL");
return true;
}
function biblio_parse_author($author_array, $cat = 0) {
if ($cat == 5) {
$author_array['firstname'] = '';
$author_array['initials'] = '';
$author_array['lastname'] = trim($author_array['name']);
$author_array['prefix'] = '';
}
else {
$value = trim($author_array['name']);
$appellation = $prefix = $surname = $firstname = $initials = '';
$prefix = "";
$value = preg_replace("/\\s{2,}/", ' ', $value);
$author = explode(",", $value);
$size = sizeof($author);
if ($size == 1) {
if (preg_match("/(.*){([^\\\\].*)}/", $value, $matches) && !preg_match("/(.*){\\\\.{.*}.*}/", $value, $matches2)) {
$author = explode(" ", $matches[1]);
$surname = $matches[2];
}
else {
$author = explode(" ", $value);
$surname = array_pop($author);
}
}
else {
if ($size == 2) {
list($surname, $prefix) = _grabSurname(array_shift($author));
}
else {
$appellation = implode(' ', array_splice($author, 1, 1));
list($surname, $prefix) = _grabSurname(array_shift($author));
}
}
$remainder = implode(" ", $author);
list($firstname, $initials, $prefix2) = _grabFirstnameInitials($remainder);
if (!empty($prefix2)) {
$prefix .= $prefix2;
}
$author_array['firstname'] = trim($firstname);
$author_array['initials'] = trim($initials);
$author_array['lastname'] = trim($surname);
$author_array['prefix'] = trim($prefix);
$author_array['suffix'] = trim($appellation);
}
$author_array['md5'] = _md5sum($author_array);
return $author_array;
}
function _md5sum($creator) {
$string = $creator['firstname'] . $creator['initials'] . $creator['prefix'] . $creator['lastname'];
$string = str_replace(' ', '', drupal_strtolower($string));
return md5($string);
}
function _grabFirstnameInitials($remainder) {
$prefix = array();
$firstname = $initials = '';
$array = explode(" ", $remainder);
foreach ($array as $value) {
$firstChar = drupal_substr($value, 0, 1);
if (ord($firstChar) >= 97 && ord($firstChar) <= 122) {
$prefix[] = $value;
}
else {
if (preg_match("/[a-zA-Z]{2,}/", trim($value))) {
$firstnameArray[] = trim($value);
}
else {
$initialsArray[] = trim(str_replace(".", " ", trim($value)));
}
}
}
if (isset($initialsArray)) {
$initials = implode(" ", $initialsArray);
}
if (isset($firstnameArray)) {
$firstname = implode(" ", $firstnameArray);
}
if (!empty($prefix)) {
$prefix = implode(" ", $prefix);
}
return array(
$firstname,
$initials,
$prefix,
);
}
function _grabSurname($input) {
$noPrefix = FALSE;
$surname = FALSE;
$prefix = FALSE;
$surnameArray = explode(" ", $input);
foreach ($surnameArray as $value) {
$firstChar = substr($value, 0, 1);
if (!$noPrefix && ord($firstChar) >= 97 && ord($firstChar) <= 122) {
$prefix[] = $value;
}
else {
$surname[] = $value;
$noPrefix = TRUE;
}
}
if (!empty($surname)) {
$surname = implode(" ", $surname);
}
if (!empty($prefix)) {
$prefix = implode(" ", $prefix);
}
return array(
$surname,
$prefix,
);
}
function _loadMD5() {
static $md5 = array();
static $count = 0;
$db_count = db_result(db_query("SELECT COUNT(*) FROM {biblio_contributor_data}"));
if ($db_count != $count) {
$count = $db_count;
$md5 = array();
$result = db_query('SELECT md5,cid FROM {biblio_contributor_data} ');
while ($row = db_fetch_array($result)) {
$md5[$row['cid']] = $row['md5'];
}
}
return count($md5) ? $md5 : NULL;
}
function biblio_contributor_user_access($user, $node) {
if (isset($node->biblio_contributors) && is_array($node->biblio_contributors)) {
foreach ($node->biblio_contributors as $cat => $authors) {
foreach ($authors as $key => $author) {
if ($author['drupal_uid'] == $user->uid) {
return TRUE;
}
}
}
}
return;
}