| 1 |
<?php |
<?php |
| 2 |
// $Id: import.module,v 1.17 2008/10/31 17:51:22 cyberswat Exp $ |
// $Id: import.module,v 1.18 2008/10/31 18:14:27 cyberswat Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* creates hook_import |
* invokes hook_import() |
| 6 |
*/ |
*/ |
| 7 |
function import_stage_import() { |
function import_stage_import() { |
| 8 |
module_invoke_all('import_stage'); |
module_invoke_all('import_stage'); |
| 9 |
return; |
return; |
| 10 |
} |
} |
| 11 |
/** |
/** |
| 12 |
* creates hook_import |
* invokes hook_import_process() and contains a context variable for use with |
| 13 |
|
* the batch api |
| 14 |
|
* |
| 15 |
|
* @param $data |
| 16 |
|
* An array containing the following keys that correspond to data staged in |
| 17 |
|
* the {import} table: |
| 18 |
|
* - impid |
| 19 |
|
* - type |
| 20 |
|
* |
| 21 |
|
* @param $context |
| 22 |
|
* The context array used for batch processing |
| 23 |
|
* |
| 24 |
*/ |
*/ |
| 25 |
function import_process_import($data) { |
function import_process_import($data, &$context) { |
| 26 |
module_invoke_all('import_process',$data); |
module_invoke_all('import_process',$data); |
| 27 |
|
$context['message'] = t('Processing import') .' '. $data->type .' '. $data->impid; |
| 28 |
return; |
return; |
| 29 |
} |
} |
| 30 |
/* |
/* |
| 32 |
*/ |
*/ |
| 33 |
function import_menu() { |
function import_menu() { |
| 34 |
$items = array(); |
$items = array(); |
| 35 |
$items['admin/settings/import'] = array( |
$items['admin/build/import'] = array( |
| 36 |
'title' => 'Import', |
'title' => 'Import', |
|
'description' => 'Settings for Import.', |
|
| 37 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 38 |
'page arguments' => array('import_settings'), |
'page arguments' => array('import_admin_form'), |
| 39 |
'type' => MENU_NORMAL_ITEM, |
'type' => MENU_NORMAL_ITEM, |
| 40 |
'access arguments' => array('administer nodes'), |
'access arguments' => array('administer nodes'), |
| 41 |
); |
); |
| 45 |
/** |
/** |
| 46 |
* Stages content into {import} |
* Stages content into {import} |
| 47 |
* |
* |
| 48 |
* @param array $args accepts $args['impid'] and $args['type'] |
* @param $args |
| 49 |
|
* An array that accepts $args['impid'] and $args['type'] |
| 50 |
*/ |
*/ |
| 51 |
function import_stage($args) { |
function import_stage($args) { |
| 52 |
if(array_key_exists('impid',$args) && array_key_exists('type',$args)) { |
if(array_key_exists('impid',$args) && array_key_exists('type',$args)) { |
| 58 |
} |
} |
| 59 |
|
|
| 60 |
/** |
/** |
| 61 |
* Implementation of hook_cron(). |
* A function menat to be called by modules implementing the Import API to |
| 62 |
*/ |
* indicate success of an imported item. |
|
function import_cron() { |
|
|
global $total_user_save, $total_profile_save, $total_import_save, $total_user_import, $total_get_old_user; |
|
|
$total_user_save = 0; |
|
|
$total_profile_save = 0; |
|
|
$total_import_save = 0; |
|
|
$total_user_import = 0; |
|
|
$total_get_old_user = 0; |
|
|
$start_timer = time(); |
|
|
// first stage all data |
|
|
import_stage_import(); |
|
|
//dpm('Staging took '. (time() - $start_timer) .' seconds.'); |
|
|
$result = db_query_range("Select impid, type from {import}", 0, variable_get("import_per_cron",50)); |
|
|
while ($row = db_fetch_object($result)) { |
|
|
import_process_import($row); |
|
|
} |
|
|
$timer_duration = time() - $start_timer; |
|
|
//dpm('Importing took '. $timer_duration .' seconds.'); |
|
|
//dpm('Getting old user from old_users took '. $total_get_old_user .' seconds.'); |
|
|
//dpm('Import of just users was '. $total_user_save .' seconds'); |
|
|
//dpm('Import of profile_value rows was '. $total_profile_save .' seconds.'); |
|
|
//dpm('Talking to import_pass/fail was '. $total_import_save .' seconds.'); |
|
|
//dpm(t('Total of calls to import function: @time seconds', array('@time' => $total_user_import))); |
|
|
} |
|
|
|
|
|
/** |
|
|
* a modified version of |
|
| 63 |
* |
* |
| 64 |
* @param object $node |
* @param $data |
| 65 |
* @param string $msg |
* An array containing impid and type so that we know what we are working with. |
| 66 |
|
* @param $msg |
| 67 |
|
* A string that serves as an optional message to associate with succesful import. |
| 68 |
* @todo implement message |
* @todo implement message |
| 69 |
*/ |
*/ |
| 70 |
function import_pass($data, $msg='') { |
function import_pass($data, $msg='') { |
| 73 |
} |
} |
| 74 |
|
|
| 75 |
/** |
/** |
| 76 |
* a modified version of |
* A function menat to be called by modules implementing the Import API to |
| 77 |
|
* indicate failure of an imported item. |
| 78 |
* |
* |
| 79 |
* @param object $node |
* @param $data |
| 80 |
* @param string $msg |
* An array containing impid and type so that we know what we are working with. |
| 81 |
* @todo implement message |
* @param $msg |
| 82 |
|
* A string that serves as an optional message to associate with succesful import. |
| 83 |
*/ |
*/ |
| 84 |
function import_fail($data, $msg='') { |
function import_fail($data, $msg='') { |
| 85 |
db_query("INSERT INTO {import_fail} VALUES('%s','%s','%s')", $data->impid, $data->type, $msg); |
db_query("INSERT INTO {import_fail} VALUES('%s','%s','%s')", $data->impid, $data->type, $msg); |
| 87 |
} |
} |
| 88 |
|
|
| 89 |
/** |
/** |
| 90 |
* Implementation of hook_settings |
* Generates a form containing the stage and process buttons for admins to use. |
| 91 |
*/ |
*/ |
| 92 |
function import_settings() { |
function import_admin_form() { |
| 93 |
$form['import_per_cron'] = array( |
$form['stage'] = array( |
| 94 |
'#type' => 'textfield', |
'#type' => 'button', |
| 95 |
'#title' => t('Number of items to process per cron'), |
'#value' => t('Stage Data for Import'), |
| 96 |
'#size' => 10, |
'#executes_submit_callback' => TRUE |
| 97 |
'#maxlength' => 20, |
); |
| 98 |
'#required' => true, |
$form['process'] = array( |
| 99 |
'#default_value' => variable_get('import_per_cron', 50), |
'#type' => 'button', |
| 100 |
|
'#value' => t('Process Import'), |
| 101 |
|
'#executes_submit_callback' => TRUE |
| 102 |
); |
); |
| 103 |
return system_settings_form($form); |
return $form; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
/** |
| 107 |
|
* Submit handler for the import_admin_form |
| 108 |
|
*/ |
| 109 |
|
function import_admin_form_submit($form, &$form_state) { |
| 110 |
|
$import = db_fetch_object(db_query("SELECT count(impid) as total FROM {import}")); |
| 111 |
|
if ($form_state['clicked_button']['#value'] == t('Stage Data for Import')) { |
| 112 |
|
import_stage_import(); |
| 113 |
|
$import = db_fetch_object(db_query("SELECT count(impid) as total FROM {import}")); |
| 114 |
|
drupal_set_message(t('%total items are staged for Import', array('%total' => $import->total))); |
| 115 |
|
} |
| 116 |
|
elseif ($form_state['clicked_button']['#value'] == t('Process Import')) { |
| 117 |
|
if($import->total > 0) { |
| 118 |
|
$operations = array(); |
| 119 |
|
$result = db_query("Select impid, type from {import}"); |
| 120 |
|
while ($row = db_fetch_object($result)) { |
| 121 |
|
$operations[] = array('import_process_import', array($row)); |
| 122 |
|
} |
| 123 |
|
$batch = array( |
| 124 |
|
'operations' => $operations, |
| 125 |
|
'title' => t('Importing'), |
| 126 |
|
'init_message' => t('Import is starting to process %total items', array('%total' => $import->total)), |
| 127 |
|
'progress_message' => t('Processed @current out of @total.'), |
| 128 |
|
'error_message' => t('Import has encountered an error.'), |
| 129 |
|
); |
| 130 |
|
|
| 131 |
|
batch_set($batch); |
| 132 |
|
} |
| 133 |
|
else { |
| 134 |
|
drupal_set_message(t('%total items are staged for Import', array('%total' => $import->total))); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
} |
} |