123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
- class TripalImporter {
-
-
-
-
-
-
-
-
- public static $name = 'Tripal Loader';
-
- public static $machine_name = 'tripal_loader';
-
- public static $description = 'A base loader for all Tripal loaders';
-
- public static $file_types = array();
-
- public static $upload_description = '';
-
- public static $upload_title = 'File Upload';
-
- public static $use_analysis = TRUE;
-
- public static $require_analysis = TRUE;
-
- public static $button_text = 'Import File';
-
- public static $methods = array(
-
- 'file_upload' => TRUE,
-
- 'file_local' => TRUE,
-
- 'file_remote' => TRUE,
- );
-
- public static $file_required = TRUE;
-
- public static $argument_list = array();
-
- public static $cardinality = 1;
-
- public static $menu_path = '';
-
-
-
-
- private $total_items;
-
- private $num_handled;
-
- private $interval;
-
- private $prev_update;
-
- protected $job;
-
- protected $arguments;
-
- protected $import_id;
-
- protected $is_prepared;
-
-
-
-
- public function __construct(TripalJob $job = NULL) {
-
- $this->is_prepared = FALSE;
- $this->import_id = NULL;
- $this->arguments = array();
- $this->job = NULL;
- $this->total_items = 0;
- $this->interval = 1;
- $this->num_handled = 0;
- $this->prev_update = 0;
- }
-
- static public function byID($import_id) {
-
- $import = db_select('tripal_import', 'ti')
- ->fields('ti')
- ->condition('ti.import_id', $import_id)
- ->execute()
- ->fetchObject();
- if (!$import) {
- throw new Exception('Cannot find an importer that matches the given import ID.');
- }
- $class = $import->class;
- tripal_load_include_importer_class($class);
- if (class_exists($class)) {
- $loader = new $class();
- $loader->load($import_id);
- return $loader;
- }
- else {
- throw new Exception('Cannot find the matching class for this import record.');
- }
- }
-
- public function setJob(TripalJob $job) {
- $this->job = $job;
- }
-
- public function create($run_args, $file_details = array()) {
- global $user;
- $class = get_called_class();
- try {
-
- $values = array(
- 'uid' => $user->uid,
- 'class' => $class,
- 'submit_date' => time(),
- );
-
-
- $arguments = array(
- 'run_args' => $run_args,
- 'files' => array(),
- );
-
- $has_file = 0;
- if (array_key_exists('file_local', $file_details)) {
- $arguments['files'][] = array(
- 'file_local' => $file_details['file_local'],
- 'file_path' => $file_details['file_local']
- );
- $has_file++;
- }
- if (array_key_exists('file_remote', $file_details)) {
- $arguments['files'][] = array(
- 'file_remote' => $file_details['file_remote']
- );
- $has_file++;
- }
- if (array_key_exists('fid', $file_details)) {
- $values['fid'] = $file_details['fid'];
-
- if (preg_match('/\|/', $file_details['fid'])) {
- $fids = explode('|', $file_details['fid']);
- foreach ($fids as $fid) {
- $file = file_load($fid);
- $arguments['files'][] = array(
- 'file_path' => drupal_realpath($file->uri),
- 'fid' => $fid
- );
- $has_file++;
- }
- }
-
- else {
- $fid = $file_details['fid'];
- $file = file_load($fid);
- $arguments['files'][] = array(
- 'file_path' => drupal_realpath($file->uri),
- 'fid' => $fid
- );
- $has_file++;
-
- $arguments['file'] = array(
- 'file_path' => drupal_realpath($file->uri),
- 'fid' => $fid
- );
- }
- }
-
- if ($has_file == 0 and $class::$file_required == TRUE) {
- throw new Exception("Must provide a proper file identifier for the \$file_details argument.");
- }
-
- $this->arguments = $arguments;
- $values['arguments'] = serialize($arguments);
-
- $import_id = db_insert('tripal_import')
- ->fields($values)
- ->execute();
- $this->import_id = $import_id;
- }
- catch (Exception $e){
- throw new Exception('Cannot create importer: ' . $e->getMessage());
- }
- }
-
- public function load($import_id) {
- $class = get_called_class();
-
- $import = db_select('tripal_import', 'ti')
- ->fields('ti')
- ->condition('ti.import_id', $import_id)
- ->execute()
- ->fetchObject();
- if (!$import) {
- throw new Exception('Cannot find an importer that matches the given import ID.');
- }
- if ($import->class != $class) {
- throw new Exception('The importer specified by the given ID does not match this importer class.');
- }
- $this->arguments = unserialize($import->arguments);
- $this->import_id = $import_id;
- }
-
- public function submitJob() {
- global $user;
- $class = get_called_class();
- if (!$this->import_id) {
- throw new Exception('Cannot submit an importer job without an import record. Please run create() first.');
- }
-
- try {
- $args = array($this->import_id);
- $includes = array(
- module_load_include('inc', 'tripal', 'api/tripal.importer.api'),
- );
- $job_id = tripal_add_job($class::$button_text, 'tripal',
- 'tripal_run_importer', $args, $user->uid, 10, $includes);
- return $job_id;
- }
- catch (Exception $e){
- throw new Exception('Cannot create importer job: ' . $e->getMessage());
- }
- }
-
- public function prepareFiles() {
- $class = get_called_class();
-
- if ($class::$file_required == FALSE) {
- $this->is_prepared = TRUE;
- return;
- }
- try {
- for($i = 0; $i < count($this->arguments['files']); $i++) {
- if (!empty($this->arguments['files'][$i]['file_remote'])) {
- $file_remote = $this->arguments['files'][$i]['file_remote'];
- $this->logMessage('Download file: !file_remote...', array('!file_remote' => $file_remote));
-
-
- $ext = '';
- if (preg_match('/^(.*?)\.gz$/', $file_remote)) {
- $ext = '.gz';
- }
-
- $temp = tempnam("temporary://", 'import_') . $ext;
- $this->logMessage("Saving as: !file", array('!file' => $temp));
- $url_fh = fopen($file_remote, "r");
- $tmp_fh = fopen($temp, "w");
- if (!$url_fh) {
- throw new Exception(t("Unable to download the remote file at %url. Could a firewall be blocking outgoing connections?",
- array('%url', $file_remote)));
- }
-
- while (!feof($url_fh)) {
- fwrite($tmp_fh, fread($url_fh, 255), 255);
- }
-
- $this->arguments['files'][$i]['file_path'] = $temp;
- $this->is_prepared = TRUE;
- }
-
- $matches = array();
- if (preg_match('/^(.*?)\.gz$/', $this->arguments['files'][$i]['file_path'], $matches)) {
- $this->logMessage("Uncompressing: !file", array('!file' => $this->arguments['files'][$i]['file_path']));
- $buffer_size = 4096;
- $new_file_path = $matches[1];
- $gzfile = gzopen($this->arguments['files'][$i]['file_path'], 'rb');
- $out_file = fopen($new_file_path, 'wb');
- if (!$out_file) {
- throw new Exception("Cannot uncompress file: new temporary file, '$new_file_path', cannot be created.");
- }
-
- while (!gzeof($gzfile)) {
-
-
- fwrite($out_file, gzread($gzfile, $buffer_size));
- }
-
- fclose($out_file);
- gzclose($gzfile);
-
-
- unlink($this->arguments['files'][$i]['file_path']);
- $this->arguments['files'][$i]['file_path'] = $new_file_path;
- }
- }
- }
- catch (Exception $e){
- throw new Exception('Cannot prepare the importer: ' . $e->getMessage());
- }
- }
-
- public function cleanFile() {
- try {
-
- for($i = 0; $i < count($this->arguments['files']); $i++) {
- if (!empty($this->arguments['files'][$i]['file_remote']) and
- file_exists($this->arguments['files'][$i]['file_path'])) {
- $this->logMessage('Removing downloaded file...');
- unlink($this->arguments['files'][$i]['file_path']);
- $this->is_prepared = FALSE;
- }
- }
- }
- catch (Exception $e){
- throw new Exception('Cannot prepare the importer: ' . $e->getMessage());
- }
- }
-
- public function logMessage($message, $variables = array(), $severity = TRIPAL_INFO) {
-
- $tmessage = t($message, $variables);
-
- if ($this->job) {
- $this->job->logMessage($message, $variables, $severity);
- }
-
- else {
-
- if ($severity == TRIPAL_CRITICAL or $severity == TRIPAL_ERROR) {
- drupal_set_message($tmessage, 'error');
- }
- if ($severity == TRIPAL_WARNING) {
- drupal_set_message($tmessage, 'warning');
- }
- }
- }
-
- protected function setTotalItems($total_items) {
- $this->total_items = $total_items;
- }
-
- protected function addItemsHandled($num_handled) {
- $items_handled = $this->num_handled = $this->num_handled + $num_handled;
- $this->setItemsHandled($items_handled);
- }
-
- protected function setItemsHandled($total_handled) {
-
- $this->num_handled = $total_handled;
- if ($total_handled == 0) {
- $memory = number_format(memory_get_usage());
- print "Percent complete: 0%. Memory: " . $memory . " bytes.\r";
- return;
- }
-
-
- $percent = sprintf("%.2f", ($this->num_handled / $this->total_items) * 100);
- $diff = $percent - $this->prev_update;
- if ($diff >= $this->interval) {
- $memory = number_format(memory_get_usage());
- print "Percent complete: " . $percent . "%. Memory: " . $memory . " bytes.\r";
-
- if ($this->job) {
- $this->job->setProgress($percent);
- }
- $this->prev_update = $diff;
- }
- }
-
- protected function setInterval($interval) {
- $this->interval = $interval;
- }
-
-
-
-
- public function form($form, &$form_state) {
- return $form;
- }
-
- public function formSubmit($form, &$form_state) {
- }
-
- public function formValidate($form, &$form_state) {
- }
-
- public function run() {
- }
-
- public function postRun() {
- }
- }
|