/[drupal]/contributions/modules/import_manager/import_manager.module
ViewVC logotype

Contents of /contributions/modules/import_manager/import_manager.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.18 - (show annotations) (download) (as text)
Thu Sep 10 19:38:13 2009 UTC (2 months, 2 weeks ago) by drumm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +3 -2 lines
File MIME type: text/x-php
Logging cleanup
1 <?php
2 // $Id: import_manager.module,v 1.17 2009/09/10 19:12:17 drumm Exp $
3
4 function import_manager_menu() {
5 $items = array(
6 'admin/content/import_manager' => array(
7 'title' => 'Imports',
8 'description' => t('See latest imports and run imports.'),
9 'page callback' => 'import_manager_latest',
10 'access arguments' => array('access import manager'),
11 ),
12 'admin/content/import_manager/latest' => array(
13 'title' => 'Latest',
14 'type' => MENU_DEFAULT_LOCAL_TASK,
15 ),
16 'import_manager/run' => array(
17 'page callback' => 'import_manager_run',
18 'access callback' => 'import_manager_is_cli',
19 'type' => MENU_CALLBACK,
20 ),
21 );
22
23
24 foreach (module_implements('imports') as $module) {
25 $info = drupal_parse_info_file(drupal_get_path('module', $module) .'/'. $module .'.info');
26 $groups = module_invoke($module, 'imports');
27 if (count($groups) > 1) {
28 $items['admin/content/import_manager/'. $module] = array(
29 'title' => $info['name'],
30 'page callback' => 'import_manager_module',
31 'access arguments' => array('access import manager'),
32 'type' => MENU_LOCAL_TASK,
33 );
34 foreach (array_keys($groups) as $group) {
35 $items['admin/content/import_manager/'. $module .'/'. drupal_urlencode($group)] = array(
36 'title' => $group,
37 'page callback' => 'import_manager_page',
38 'page arguments' => array($module, $group),
39 'access arguments' => array('access import manager'),
40 'type' => MENU_LOCAL_TASK,
41 );
42 }
43 }
44 else {
45 $group = reset(array_keys($groups));
46 $items['admin/content/import_manager/'. $module] = array(
47 'title' => $info['name'],
48 'page callback' => 'import_manager_page',
49 'page arguments' => array($module, $group),
50 'access arguments' => array('access import manager'),
51 'type' => MENU_LOCAL_TASK,
52 );
53 }
54 }
55
56 return $items;
57 }
58
59 function import_manager_is_cli() {
60 return $_SERVER['SERVER_SOFTWARE'] === 'PHP CLI';
61 }
62
63 function import_manager_help($page = NULL) {
64 switch ($page) {
65 case 'admin/help#import_manager':
66 return t('<h3>Importing from the command line</h3>
67 <p>Running imports from the command line removes web server resource and memory restrictions and can be called from the system\'s cron.</p>
68 <ol>
69 <li>Get <code>drupal.sh</code>, this is included in the <code>scripts</code> directory of Drupal 6 and later. It works well on Drupal 5.</li>
70 <li><code>drupal.sh [--root path/to/drupal] http://{sites.directory}/import_manager/run/{import_function_name}</code></li>
71 </ol>
72 <p>This path can only be accessed from command line PHP scripts.</p>');
73 }
74 }
75
76 function import_manager_perm() {
77 return array('access import manager');
78 }
79
80 function import_manager_latest() {
81 $header = array(
82 t('Import'),
83 t('Last run'),
84 );
85 $rows = array();
86 $result = db_query('SELECT callback, last_run FROM {import_manager} WHERE last_run > %d ORDER BY last_run DESC', time() - 30 * 24 * 60 * 60);
87 while ($row = db_fetch_object($result)) {
88 $import = import_manager_get_import($row->callback);
89 $rows[] = array(
90 l($import['title'], 'admin/content/import_manager/'. $import['module'] .'/'. drupal_urlencode($import['group']), array(), NULL, form_clean_id($row->callback)),
91 format_date($row->last_run),
92 );
93 }
94 if (count($rows) > 0) {
95 return theme('table', $header, $rows);
96 }
97 else {
98 return '<p><em>'. t('No imports have been run in the past 30 days.') .'</em></p>';
99 }
100 }
101
102 function import_manager_module() {
103 return '<p>'. t('Click a group above.') .'</p>';
104 }
105
106 function import_manager_forms() {
107 $forms = array();
108
109 foreach (module_implements('imports') as $module) {
110 foreach (module_invoke($module, 'imports') as $group => $imports) {
111 foreach ($imports as $callback => $import) {
112 $forms[$callback] = array(
113 'callback' => 'import_manager_form',
114 'callback arguments' => array($callback, $import),
115 );
116 }
117 }
118 }
119
120 return $forms;
121 }
122
123 function import_manager_last_run($callback) {
124 static $last_run;
125
126 if ($last_run == NULL) {
127 $result = db_query('SELECT callback, last_run FROM {import_manager}');
128 while ($row = db_fetch_object($result)) {
129 $last_run[$row->callback] = $row->last_run;
130 }
131 }
132
133 return $last_run[$callback];
134 }
135
136 function _import_manager_set_defaults($import) {
137 $defaults = array(
138 'message' => 'Ran %name import.',
139 'button' => t('Import now'),
140 );
141 return array_merge($defaults, $import);
142 }
143
144 function import_manager_form(&$form_state, $form_id, $import) {
145 $import = _import_manager_set_defaults($import);
146
147 drupal_add_css(drupal_get_path('module', 'import_manager') .'/import_manager.css');
148
149 $form = array(
150 '#base' => 'import_manager_form',
151 '#attributes' => array('class' => 'import-manager-form'),
152 );
153
154 $form['import'] = array(
155 '#type' => 'value',
156 '#value' => $import,
157 );
158
159 $last_run = import_manager_last_run($form_id);
160 if ($last_run == NULL) {
161 $form[] = array(
162 '#value' => t('<p><strong>@name</strong> never run</p>', array('@name' => $import['title'])),
163 );
164 }
165 else {
166 $form[] = array(
167 '#value' => t('<p><strong>@name</strong> last run @date</p>', array('@name' => $import['title'], '@date' => format_date($last_run))),
168 );
169 }
170
171 $form['data'] = array(
172 '#tree' => TRUE,
173 );
174
175 $form['submit'] = array(
176 '#type' => 'submit',
177 '#value' => $import['button'],
178 '#weight' => 10,
179 );
180 if (!$import['access']) {
181 $form['submit']['#attributes'] = array(
182 'disabled' => 'disabled',
183 );
184 $form[] = array(
185 '#value' => t('You do not have permission to run this import.'),
186 '#weight' => 11,
187 );
188 }
189
190 $form['#validate'] = array('import_manager_form_validate');
191 $form['#submit'] = array('import_manager_form_submit');
192
193 return $form;
194 }
195
196 function import_manager_form_validate($form, &$form_state) {
197 if (!$form_state['values']['import']['access']) {
198 form_set_error('submit', t('You do not have permission to run this import.'));
199 }
200 }
201
202 function import_manager_form_submit($form, &$form_state, $quiet = FALSE) {
203 set_time_limit(0);
204
205 if (isset($form_state['values']['import']['file path'])) {
206 include_once $form_state['values']['import']['file path'];
207 }
208
209 db_query("INSERT INTO {import_manager_log} (callback, time) VALUES ('%s', %d)", $form_state['values']['form_id'], time());
210 $import_id = db_last_insert_id('import_manager_log', 'import_id');
211 import_manager_watchdog(NULL, $import_id);
212
213 timer_start('import manager');
214 call_user_func_array($form_state['values']['form_id'], $form_state['values']['data']);
215 db_query("UPDATE {import_manager_log} SET duration = %d WHERE import_id = %d", timer_read('import manager'), $import_id);
216 import_manager_watchdog(NULL, NULL);
217
218 db_query("DELETE FROM {import_manager} WHERE callback = '%s'", $form_state['values']['form_id']);
219 db_query("INSERT INTO {import_manager} (callback, last_run) VALUES ('%s', %d)", $form_state['values']['form_id'], time());
220
221 if (!$quiet) {
222 drupal_set_message(t($form_state['values']['import']['message'], array('%name' => $form_state['values']['import']['title'])));
223 }
224 watchdog('import manager', $form_state['values']['import']['message'], array('%name' => $form_state['values']['import']['title']));
225 }
226
227 /**
228 * Run an import.
229 *
230 * @param $callback
231 * A string containing the import callback function name.
232 * @param $data
233 * Optional array of data which would usually be submitted with the import.
234 */
235 function import_manager_run($callback, $data = array()) {
236 $form_state = array(
237 'values' => array(
238 'import' => import_manager_get_import($callback),
239 'data' => $data,
240 'form_id' => $callback,
241 ),
242 );
243 import_manager_form_submit(array(), $form_state, TRUE);
244 }
245
246 /**
247 * Get the import definition for a callback.
248 *
249 * @param $callback
250 * A string containing the import callback function name.
251 * @return
252 * The import's definition array.
253 */
254 function import_manager_get_import($callback) {
255 foreach (module_implements('imports') as $module) {
256 foreach (module_invoke($module, 'imports') as $group => $imports) {
257 if (isset($imports[$callback])) {
258 $imports[$callback]['module'] = $module;
259 $imports[$callback]['group'] = $group;
260 return _import_manager_set_defaults($imports[$callback]);
261 }
262 }
263 }
264 }
265
266 function import_manager_page($module, $group) {
267 $output = '';
268
269 $imports = module_invoke($module, 'imports');
270 $list = array();
271 foreach ($imports[$group] as $callback => $import) {
272 $list[$import['title']] = $callback;
273 }
274 ksort($list);
275 foreach ($list as $title => $callback) {
276 $output .= drupal_get_form($callback);
277 }
278
279 return $output;
280 }
281
282 function import_manager_watchdog($log_entry, $set_import_id = FALSE) {
283 static $import_id;
284
285 if ($set_import_id !== FALSE) {
286 $import_id = $set_import_id;
287 return;
288 }
289
290 if (!is_null($import_id)) {
291 $row = new stdClass();
292 $row->import_id = $import_id;
293 $row->message = $log_entry['message'];
294 $row->timestamp = $log_entry['timestamp'];
295 $row->variables = serialize($log_entry['variables']);
296 drupal_write_record('import_manager_log_detail', $row);
297 }
298 }

  ViewVC Help
Powered by ViewVC 1.1.2