function tripal_feature_set_urls
1.x tripal_feature.sync_features.inc | tripal_feature_set_urls($na = NULL, $job = NULL) |
Parameters
$na : Tripal expects all jobs to have at least one argument. For this function we don't need any, so we have this dummy argument as a filler
$job_id:
1 string reference to 'tripal_feature_set_urls'
- tripal_feature_admin_validate in tripal_feature/
includes/ tripal_feature.admin.inc
File
- tripal_feature/
includes/ tripal_feature.sync_features.inc, line 126 - @todo Add file header description
Code
function tripal_feature_set_urls($na = NULL, $job = NULL) {
// begin the transaction
db_query("BEGIN");
print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
"If the load fails or is terminated prematurely then the entire set of \n" .
"new URLs will be rolled back and no changes will be made\n\n";
// get the number of records we need to set URLs for
$csql = "SELECT count(*) FROM {chado_feature}";
$num_nodes = db_result(db_query($csql));
// calculate the interval at which we will print an update on the screen
$num_set = 0;
$num_per_interval = 100;
// prepate the statements which will quickly add url alias. Because these
// are not Chado tables we must manually prepare them
$psql = "
PREPARE del_url_alias_by_src (text) AS
DELETE FROM {url_alias} WHERE src = \$1
";
db_query($psql);
$psql = "
PREPARE ins_url_alias_nisrds (text, text) AS
INSERT INTO url_alias (src, dst) VALUES (\$1, \$2)
";
db_query($psql);
// get the URL alias syntax string
$url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
if (!$url_alias) {
$url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
}
$url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
// get the list of features that have been synced
$sql = "SELECT * FROM {chado_feature}";
$nodes = db_query($sql);
while ($node = db_fetch_object($nodes)) {
// get the URL alias
$src = "node/$node->nid";
$dst = tripal_feature_get_feature_url($node, $url_alias);
if (!$dst) {
db_query('DEALLOCATE "del_url_alias_by_src"');
db_query('DEALLOCATE "ins_url_alias_nisrds"');
db_query("ROLLBACK");
return;
}
// if the src and dst is the same (the URL alias couldn't be set)
// then skip to the next one. There's nothing we can do about this one.
if ($src == $dst) {
continue;
}
// remove any previous alias and then add the new one
$success = db_query("EXECUTE del_url_alias_by_src('%s')", $src);
if (!$success) {
db_query('DEALLOCATE "del_url_alias_by_src"');
db_query('DEALLOCATE "ins_url_alias_nisrds"');
db_query("ROLLBACK");
watchdog('trp-seturl', "Failed Removing URL Alias: %src", array('%src' => $src), WATCHDOG_ERROR);
return;
}
$success = db_query("EXECUTE ins_url_alias_nisrds('%s', '%s')", $src, $dst);
if (!$success) {
db_query('DEALLOCATE "del_url_alias_by_src"');
db_query('DEALLOCATE "ins_url_alias_nisrds"');
db_query("ROLLBACK");
watchdog('trp-seturl', "Failed Adding URL Alias: %dst", array('%dst' => $dst), WATCHDOG_ERROR);
return;
}
// update the job status every 1% features
if ($job and $num_set % $num_per_interval == 0) {
$percent = ($num_set / $num_nodes) * 100;
tripal_job_set_progress($job, intval($percent));
$percent = sprintf("%.2f", $percent);
print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
}
$num_set++;
}
$percent = ($num_set / $num_nodes) * 100;
tripal_job_set_progress($job, intval($percent));
$percent = sprintf("%.2f", $percent);
print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
print "\nDone. Set " . number_format($num_set) . " URLs\n";
// unprepare the statements
db_query('DEALLOCATE "del_url_alias_by_src"');
db_query('DEALLOCATE "ins_url_alias_nisrds"');
db_query("COMMIT");
}