/[eiffelstudio]/trunk/Documentation/tools/web/trunk/drupal/sites/all/modules/isedoc/import/isedoc_import.admin.inc
ViewVC logotype

Contents of /trunk/Documentation/tools/web/trunk/drupal/sites/all/modules/isedoc/import/isedoc_import.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 74386 - (show annotations)
Sat Aug 9 09:19:13 2008 UTC (11 years, 5 months ago) by jfiat
File size: 13254 byte(s)
commit for safety, almost working
1 <?php
2 // $Id$
3
4 /**
5 * Admin settings callback.
6 */
7 function isedoc_import_admin_settings() {
8
9 $form['isedoc_import'] = array(
10 '#type' => 'fieldset',
11 '#title' => t('Import Documentation Settings')
12 );
13
14 $form['isedoc_import']['isedoc_import_subfolder'] = array(
15 '#type' => 'textfield',
16 '#title' => t('Import subFolder'),
17 '#default_value' => variable_get('isedoc_import_subfolder', '/isedoc/import'),
18 '#description' => t('Subdirectory in directory "%dir" where documentation will be loaded from. Do not include trailing slash.<br/>To import content, %link', array('%dir' => file_directory_path(), '%link' => 'admin/content/isedoc_import')),
19 '#required' => TRUE,
20 );
21 $form['isedoc_import']['isedoc_import_action'] = array(
22 '#type' => 'submit',
23 '#submit' => array('isedoc_import_admin_settings_goto_import'),
24 '#value' => t('Import Content'),
25 );
26
27 return system_settings_form($form);
28 }
29
30 function isedoc_import_settings_submit($form, &$form_state) {
31 drupal_set_message(t("Processed isedoc_import_settings_submit ..."),'status');
32 }
33 function isedoc_import_admin_settings_goto_import($form, &$form_state) {
34 drupal_goto('admin/content/isedoc_import');
35 }
36
37 /**
38 * Callback isedoc/book/import
39 */
40
41 function _isedoc_import_doc() {
42 drupal_set_message("_isedoc_import_doc...");
43 // $links = array();
44 // $links[] = l('import 1', 'іsedoc/import');
45 // $links[] = l('import 2', 'іsedoc/import');
46 // return theme('list', $links);
47 $form['isedoc_import'] = array(
48 '#type' => 'fieldset',
49 '#title' => t('Import Documentation')
50 );
51 $form['isedoc_import']['toto'] = array(
52 '#type' => 'textfield',
53 '#title' => t('toto'),
54 '#default_value' => 'totofofo',
55 '#description' => t('Subdir'),
56 );
57 return $form;
58 // return "totot";
59 }
60
61 /*
62 * Menu: admin/content/isedoc_import
63 */
64 function isedoc_import_form() {
65 $form = array();
66 //drupal_set_message("<pre>".var_dump_global('GLOBALS')."</pre>");
67
68 $import_path = variable_get('isedoc_import_subfolder', '/isedoc/import');
69 $dirpath = file_directory_path() . $import_path ;
70
71 if (!file_check_directory($dirpath)) {
72 drupal_set_message(t("You need to configure the isedoc directory on the isedoc import module's <a href='!admin-settings-isedoc_import'>settings page</a>.", array('!admin-settings-isedoc_import' => url('admin/settings/isedoc/isedoc_import'))), 'error');
73 // return $form;
74 }
75
76 /*
77 * Files
78 */
79
80 $book_import_log_path = $dirpath . "/" . "book.index.log";
81 $book_index_path = $dirpath . "/" . "book.index";
82 if (file_exists($book_index_path)) {
83 $form['isedoc_import_index'] = array(
84 '#type' => 'textfield',
85 '#title' => t('Book index'),
86 '#default_value' => variable_get('isedoc_import_bookindex', $book_index_path),
87 '#description' => t('BookIndex'),
88 );
89
90 /*
91 * Parameters
92 */
93 $all_books_titles = array();
94 $all_books_titles[''] = t('<None>');
95 $dft_book = '';
96
97 $all_books = book_get_books();
98 if (count($all_books) > 0) {
99 $dft_book = $all_books[1]['title'];
100 foreach ($all_books as $b) {
101 if ($dft_book == '') { $dft_book = $b['bid']; }
102 $all_books_titles[$b['bid']] = $b['title']; //. " #".$b['bid'];
103 }
104 }
105
106 $form['isedoc_import_booknames'] = array(
107 '#type' => 'select',
108 '#title' => t('Select a book'),
109 '#default_value' => variable_get('isedoc_import_bid', $dft_book),
110 '#options' => $all_books_titles,
111 '#description' => t('The book to receive the imported content.'),
112 );
113
114 $form['isedoc_import_bookname'] = array(
115 '#type' => 'textfield',
116 '#title' => t('Book name'),
117 '#default_value' => variable_get('isedoc_import_bookname', 'ISE Documentation'),
118 '#description' => t('Bookname'),
119 );
120 $form['isedoc_import_dirpath'] = array(
121 '#type' => 'hidden',
122 '#value' => $dirpath,
123 );
124
125 $form['buttons']['submit'] = array(
126 '#type' => 'submit',
127 '#submit' => array('isedoc_import_form_import'),
128 '#value' => t('Import'),
129 );
130 }
131 else {
132 $form['isedoc_import_index'] = array(
133 '#type' => 'item',
134 '#value' => t('No book.index file found'),
135 );
136
137 $form['isedoc_import_subfolder'] = array(
138 '#type' => 'textfield',
139 '#title' => t('Import path'),
140 '#default_value' => $import_path,
141 '#description' => t('Import path'),
142 );
143 $form['isedoc_import_update_action'] = array(
144 '#type' => 'submit',
145 '#submit' => array('isedoc_import_form_update'),
146 '#value' => t('Update'),
147 );
148 }
149
150 return $form;
151 }
152
153 function theme_isedoc_import_form($form) {
154 return drupal_render($form);
155 }
156
157
158 function isedoc_import_form_update($form, &$form_state) {
159 drupal_set_message(t('Update processing... "@bookname"', array('@bookname' => $form_state['values']['isedoc_import_bookname'])));
160 variable_set('isedoc_import_subfolder',$form_state['values']['isedoc_import_subfolder']);
161 }
162
163 function isedoc_import_form_import($form, &$form_state) {
164 $bookid = $form_state['values']['isedoc_import_booknames'];
165 drupal_set_message(t('Import processing... "bookid:@bookname"', array('@bookname' => $bookid)));
166 if (isset($form_state['values']['isedoc_import_subfolder'])) {
167 variable_set('isedoc_import_subfolder',$form_state['values']['isedoc_import_subfolder']);
168 }
169 variable_set('isedoc_import_bid',$form_state['values']['isedoc_import_booknames']);
170 variable_set('isedoc_import_bookname',$form_state['values']['isedoc_import_bookname']);
171 $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
172 if ($op == t('Import')) {
173 $dirpath = $form_state['values']['isedoc_import_dirpath'];
174 // $dirpath = variable_get('isedoc_import_subfolder', '');
175 drupal_set_message(t("Importing files from %dirpath into bookid=%bookid", array ('%dirpath' => $dirpath, '%bookid' => $bookid)));
176 if (file_check_directory($dirpath)) {
177 $nodes = array();
178 $files = array();
179 // drupal_set_message(var_dump_r($form_state['values']));
180 $book_index_path = $form_state['values']['isedoc_import_index'];
181 //drupal_set_message(t("Importing files using index %i into bookid=%bookid", array ('%i' => $book_index_path, '%bookid' => $bookid)));
182
183 // a slow machine
184 if (!ini_get('safe_mode')) {
185 set_time_limit(0);
186 }
187 isedoc_import_with_index($dirpath,$book_index_path,$bookid);
188 $filename = file_check_location($dirpath .'/'. $origname, $dirpath);
189 if ($filename) {
190 }
191
192 // report back on our progress
193 if (!empty($nodes)) {
194 drupal_set_message(t('Successfully imported: ') . theme('item_list', $nodes));
195 }
196 else {
197 drupal_set_message(t('No files were imported.'));
198 }
199 }
200 }
201 }
202
203 function isedoc_create_doc_outline_from($nid,$info) {
204
205 $node = node_load ($nid, NULL, TRUE);
206
207 book_nodeapi(&$node, "prepare", NULL, NULL);
208 $node->book['bid'] = $info['book_id'];
209 book_nodeapi(&$node, "insert", NULL, NULL);
210
211 $node->book['plid'] = $info['parent_mlid'];
212 book_nodeapi(&$node, "update", NULL, NULL);
213
214 node_save($node);
215 return $node->book['mlid'];
216
217 }
218
219 function isedoc_create_doc_node_from(&$info) {
220 global $user;
221
222 // if (!user_access('create isedoc')) {
223 // drupal_access_denied();
224 // }
225
226 // Ensure it's a valid image.
227 if (!$info) {
228 return FALSE;
229 }
230
231 // Make sure we can copy the file into our temp directory.
232 $original_path = $filepath;
233
234 // Build the node.
235 $nid = 0;
236 if ($nid > 0) {
237 //drupal_set_message("isedoc_create_node_from using nid=$nid");
238 $node = node_load ($nid, NULL, TRUE);
239 }
240 if (!$node) {
241 $node = new stdClass();
242 $node->type = 'doc';
243 }
244 $node->uid = $user->uid;
245 $node->name = $user->name;
246 $node->title = $info['title'];
247 $node->body = $info['body'];
248
249
250 // Set the node's defaults... (copied this from node and comment.module)
251 $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
252 $node->status = in_array('status', $node_options);
253 $node->promote = in_array('promote', $node_options);
254 if (module_exists('comment')) {
255 $node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
256 }
257 if (module_exists('taxonomy')) {
258 $node->taxonomy = $taxonomy;
259 }
260
261 if ($nid > 0) {
262 $node->revision = TRUE;
263 $node->new_file = FALSE;
264 } else {
265 $node->revision = FALSE;
266 $node->new_file = TRUE;
267 }
268 // $node->images[IMAGE_ORIGINAL] = $filepath;
269
270 // Save the node.
271 $node = node_submit($node);
272 node_save($node);
273
274 return $node;
275 }
276
277
278 /**
279 * Helpers
280 */
281
282 function isedoc_import_with_index($dn,$fn,$bid) {
283 drupal_set_message(t("Importing using index $fn ..."),'status');
284 $logfp = fopen($fn.".log", 'w+');
285
286 $fp = fopen($fn, 'r');
287 $node_ids = array();
288 $dep = 0;
289 $is_reuse = 0;
290
291 while (!feof($fp)) {
292 $node = NULL;
293 $line = fgets($fp);
294 $line = substr($line, 0, -1);
295 if (substr($line, 1, 1) == "@") {
296 $is_reuse = 1;
297 } else {
298 $is_reuse = 0;
299 }
300 $i = strpos($line, "[");
301 if ($i > 0) {
302 $j = strpos($line, ":", $i);
303 $l_depth = substr($line, $i+1, 1 + ($j-1) -($i+1) );
304 $i = strpos($line, "]", $j+1);
305 $l_base_id = substr($line, $j+1, 1 + ($i-1) - ($j+1));
306 $l_title = substr($line, $i+2);
307
308 $line = fgets($fp);
309 $line = substr($line, 0, -1);
310
311 $i = strpos($line, "!src=");
312 if ($i > 0) {
313 $l_src = $dn . "/". substr($line, $i+5) . ".wiki";
314
315 fputs($logfp, "#$dep: $l_title [$l_src]\n");
316
317 if ($reuse == 1) {
318 $a_nid = book_doc_page_exists($info['title']);
319 if ($a_nid > 0) {
320 $node = node_load ($nid);
321 } else {
322 drupal_set_message(t(" ERROR no REUSE dep=%dep \"%tit\" -> %src", array( "%dep" => $l_depth, "%tit" => $l_title, "%bas" => $l_base_id, "%src" => $l_src)),'error');
323 $is_reuse = 0;
324 }
325 if (!$node) {
326 $is_reuse = 0;
327 drupal_set_message(t(" ERROR no REUSE dep=%dep \"%tit\" -> %src", array( "%dep" => $l_depth, "%tit" => $l_title, "%bas" => $l_base_id, "%src" => $l_src)),'error');
328 }
329 }
330
331 if ($is_reuse) {
332 $info = isedoc_file_info($l_src, FALSE);
333 } else {
334 $info = isedoc_file_info($l_src, TRUE);
335 }
336 if ($info) {
337 //drupal_set_message(t(" -> [%dep:%bas] \"%tit\" -> %src", array( "%dep" => $l_depth, "%tit" => $l_title, "%bas" => $l_base_id, "%src" => $l_src)),'status');
338 $info['title'] = $l_title;
339 $info['src'] = $l_src;
340 $info['baseid'] = $l_baseid;
341 $info['depth'] = $l_depth;
342
343 if ($dep > 0) {
344 $p_mlid = $node_ids[$l_depth - 1];
345 } else {
346 $p_mlid = $book_id;
347 }
348
349 $dep = $l_depth;
350 $info['parent_mlid'] = $p_mlid;
351 $info['book_id'] = $bid;
352 if ($node) {
353 $l_mlid = $node->book['mlid'];
354 drupal_set_message(t(" REUSE -> [%dep] %src -> %nid (mlid=$l_mlid)(parent=$p_mlid)", array( "%dep" => $l_depth, "%src" => $l_src, "%nid" => $node->nid)),'status');
355 } else {
356 $node = isedoc_create_doc_node_from (&$info);
357 $l_mlid = isedoc_create_doc_outline_from ($node->nid, &$info);
358 drupal_set_message(t(" -> [%dep] %src -> %nid (mlid=$l_mlid)(parent=$p_mlid)", array( "%dep" => $l_depth, "%src" => $l_src, "%nid" => $node->nid)),'status');
359 }
360 $node_ids[$l_depth] = $l_mlid;
361
362 $node = NULL;
363 } else {
364 drupal_set_message(t(" -> [%dep:%bas] \"%tit\" -> %src", array( "%dep" => $l_depth, "%tit" => $l_title, "%bas" => $l_base_id, "%src" => $l_src)),'error');
365 }
366
367 }
368 }
369 }
370
371 fclose ($logfp);
372 fclose ($fp);
373
374 }
375
376 function isedoc_file_info($fn, $is_full=FALSE) {
377 $l_body = '';
378 $fp = fopen($fn, 'r');
379 $info = array();
380 $done = 0;
381 while(!$done) {
382 $line = fgets($fp);
383 $line = substr($line, 0, -1);
384 $i = strpos ($line, "=");
385 if ($i > 0) {
386 $k = substr($line, 0, $i);
387 $v = substr($line, $i+1);
388 $info[$k] = $v;
389 } else {
390 $done = 1;
391 }
392 }
393 if ($is_full) {
394 $l_body = fread($fp, filesize($fn));
395 }
396 fclose ($fp);
397 $info['body'] = $l_body;
398 return $info;
399 }
400
401 function var_dump_r(&$ref, $pre=TRUE) {
402 ob_start(); var_dump($ref); $a=htmlspecialchars(ob_get_contents(),ENT_QUOTES); ob_end_clean();
403 if ($pre) {
404 return "<pre>DUMP=".$a . '</pre>';
405 } else {
406 return 'DUMP='.$a;
407 }
408 }
409 function var_dump_global($refname) {
410 global $$refname;
411 ob_start(); var_dump($$refname); $a=htmlspecialchars(ob_get_contents(),ENT_QUOTES); ob_end_clean();
412 return "DUMP[".$refname."]=".$a;
413 }
414
415 /**
416 * Returns an array of all books.
417 *
418 * This list may be used for generating a list of all the books, or for building
419 * the options for a form select.
420 */
421
422 function book_doc_page_exists($a_title) {
423 // $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(title) = LOWER('%s') AND type IN ()", $a_title));
424 $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(title) = LOWER('%s')", $a_title));
425 return $nid;
426 }
427
428 function isedoc_book_get_books() {
429 static $all_books;
430
431 if (!isset($all_books)) {
432 $all_books = array();
433 $result = db_query("SELECT DISTINCT(bid) FROM {book}");
434 $nids = array();
435 while ($book = db_fetch_array($result)) {
436 $nids[] = $book['bid'];
437 }
438 if ($nids) {
439 $result2 = db_query(db_rewrite_sql("SELECT n.type, n.title, b.*, ml.* FROM {book} b INNER JOIN {node} n on b.nid = n.nid INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE n.nid IN (". implode(',', $nids) .") AND n.status = 1 ORDER BY ml.weight, ml.link_title"));
440 while ($link = db_fetch_array($result2)) {
441 $link['href'] = $link['link_path'];
442 $link['options'] = unserialize($link['options']);
443 $all_books[$link['bid']] = $link;
444 }
445 }
446 }
447 return $all_books;
448 }

  ViewVC Help
Powered by ViewVC 1.1.23