Tripal v1.0 (6.x-1.0)
tripal_bulk_loader.drush.inc
Go to the documentation of this file.
00001 <?php
00002 
00011 function tripal_bulk_loader_drush_command() {
00012   $items = array();
00013   $items['tripal-loader-progress'] = array(
00014     'description' => dt('Display the progress of any running tripal bulk loading job.'),
00015     'aliases' => array('trpload-%'),
00016   );
00017   $items['tripal-loader-view'] = array(
00018     // used by drush help
00019     'description' => dt('Returns the status/details of the specified bulk loading job.'),
00020     'arguments' => array(
00021       'nid' => dt('The Node ID of the bulk Loading Job')
00022     ),
00023     'examples' => array(
00024       'Standard Example' => 'drush tripal-loader-view 5433',
00025     ),
00026     'aliases' => array('trpload-view')
00027   );
00028   $items['tripal-loader-cancel'] = array(
00029     // used by drush help
00030     'description' => dt('Cancels the specified bulk loading job.'),
00031     'arguments' => array(
00032       'nid' => dt('The Node ID of the bulk Loading Job')
00033     ),
00034     'examples' => array(
00035       'Standard Example' => 'drush tripal-loader-cancel 5433',
00036     ),
00037     'aliases' => array('trpload-cncl')
00038   );
00039   $items['tripal-loader-submit'] = array(
00040     // used by drush help
00041     'description' => dt('Submit or Re-submit the given bulk loading job.'),
00042     'arguments' => array(
00043       'nid' => dt('The Node ID of the bulk Loading Job')
00044     ),
00045     'examples' => array(
00046       'Standard Example' => 'drush tripal-loader-submit 5433',
00047     ),
00048     'aliases' => array('trpload-sbmt')
00049   );
00050   $items['tripal-loader-revert'] = array(
00051     // used by drush help
00052     'description' => dt('Revert the records loaded by the last run of the specified loading job. This is only available if the specified loading job is keeping track of inserted IDs.'),
00053     'arguments' => array(
00054       'nid' => dt('The Node ID of the bulk Loading Job')
00055     ),
00056     'examples' => array(
00057       'Standard Example' => 'drush tripal-loader-revert 5433',
00058     ),
00059     'aliases' => array('trpload-revert')
00060   );
00061 
00062   return $items;
00063 }
00064 
00069 function drush_tripal_bulk_loader_tripal_loader_progress() {
00070 
00071   // determine the progress of any loading jobs
00072   $sql = "SELECT t.loader_name, t.file, t.job_id FROM {tripal_bulk_loader} t WHERE job_status='Loading...'";
00073   $resource = db_query($sql);
00074 
00075   while ($r = db_fetch_object($resource)) {
00076     if ($r->job_id) {
00077       $progress = tripal_bulk_loader_progess_file_get_progress($r->job_id);
00078       if ($progress->num_records > 0 AND $progress->total_percent < 100) {
00079         drush_print(
00080           $r->loader_name . "\n"
00081           . str_repeat("-", 40) . "\n"
00082           . "File:" . $r->file . "\n"
00083           . "Current Constant Set:\n"
00084           . "\tLines processed: " . $progress->num_lines . "\n"
00085           . "\tRecord Inserted: " . $progress->num_records . "\n"
00086           . "\tPercent Complete: " . $progress->percent_file . "\n"
00087           . "Number of Constant Sets fully loaded: " . $progress->num_constant_sets_loaded . "\n"
00088           . "Job Percent Complete: " . $progress->total_percent . "\n"
00089         );
00090       }
00091     }
00092   }
00093 }
00094 
00101 function drush_tripal_bulk_loader_tripal_loader_view ($nid) {
00102   $node = node_load($nid);
00103   $author = user_load($node->uid);
00104 
00105   drush_print("Job Name: ".$node->loader_name);
00106   drush_print("Submitted By: ".$author->name);
00107   drush_print("Job Creation Date: ".format_date($node->created));
00108   drush_print("Last Updated: ".format_date($node->changed));
00109   drush_print("Template Name: ".$node->template->name);
00110   drush_print("Data File: ".$node->file);
00111   drush_print("Job Status: ".$node->job_status);
00112 }
00113 
00120 function drush_tripal_bulk_loader_tripal_loader_cancel ($nid) {
00121   $node = node_load($nid);
00122   db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Job Cancelled', $node->nid);
00123   tripal_jobs_cancel($node->job_id,FALSE);
00124 }
00125 
00132 function drush_tripal_bulk_loader_tripal_loader_submit ($nid) {
00133   global $user;
00134 
00135   if ($node->job_id) {
00136     //Re-submit Tripal Job
00137     tripal_jobs_rerun($node->job_id);
00138     db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
00139   }
00140   else {
00141     //Submit Tripal Job
00142     $node= node_load($nid);
00143     $job_args[1] = $nid;
00144     if (is_readable($node->file)) {
00145       $fname = basename($node->file);
00146       $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
00147 
00148       // add job_id to bulk_loader node
00149       $success = db_query("UPDATE {tripal_bulk_loader} SET job_id=%d WHERE nid=%d", $job_id, $nid);
00150 
00151       // change status
00152       db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
00153     }
00154     else {
00155       drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
00156     }
00157   }
00158 
00159 }
00160 
00168 function drush_tripal_bulk_loader_tripal_loader_revert ($nid) {
00169 
00170   // Remove the records from the database that were already inserted
00171   $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d ORDER BY tripal_bulk_loader_inserted_id DESC', $nid);
00172   while ($r = db_fetch_object($resource)) {
00173     $ids = preg_split('/,/', $r->ids_inserted);
00174     db_query('DELETE FROM %s WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted);
00175     $result = db_fetch_object(db_query('SELECT true as present FROM %s WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted));
00176     if (!$result->present) {
00177       drush_print('Successfully Removed data Inserted into the '.$r->table_inserted_into.' table.');
00178       db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=%d', $r->tripal_bulk_loader_inserted_id);
00179     }
00180     else {
00181       drush_print('Unable to remove data Inserted into the '.$r->table_inserted_into.' table!');
00182     }
00183   }
00184 
00185   // reset status
00186   db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Reverted -Data Deleted', $nid);
00187 
00188 }
 All Classes Files Functions Variables