function tripal_stock_set_urls
1.x tripal_stock.sync_stocks.inc | tripal_stock_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_stock_set_urls'
- tripal_stock_admin_validate in tripal_stock/
includes/ tripal_stock.admin.inc - Implements hook_form_validate(): Validates user input
File
- tripal_stock/
includes/ tripal_stock.sync_stocks.inc, line 124 - @todo Add file header description
Code
function tripal_stock_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_stock}";
$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_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
if (!$url_alias) {
$url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
}
$url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
// get the list of stocks that have been synced
$sql = "SELECT * FROM {chado_stock}";
$nodes = db_query($sql);
while ($node = db_fetch_object($nodes)) {
// get the URL alias
$src = "node/$node->nid";
$dst = tripal_stock_get_stock_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% stocks
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");
}