/[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 74382 - (show annotations)
Fri Aug 8 19:41:54 2008 UTC (11 years, 5 months ago) by jfiat
File size: 11818 byte(s)
change the way we import...
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);
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_node_from($info) {
204 global $user;
205
206 // if (!user_access('create isedoc')) {
207 // drupal_access_denied();
208 // }
209
210 // Ensure it's a valid image.
211 if (!$info) {
212 return FALSE;
213 }
214
215 // Make sure we can copy the file into our temp directory.
216 $original_path = $filepath;
217
218 // Build the node.
219 $nid = 0;
220 if ($nid > 0) {
221 drupal_set_message("isedoc_create_node_from using nid=$nid");
222 $node = node_load ($nid, NULL, TRUE);
223 }
224 if (!$node) {
225 $node = new stdClass();
226 $node->type = 'doc';
227 }
228 $node->uid = $user->uid;
229 $node->name = $user->name;
230 $node->title = $info['title'];
231 $node->body = $info['body'];
232
233 // Set the node's defaults... (copied this from node and comment.module)
234 $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
235 $node->status = in_array('status', $node_options);
236 $node->promote = in_array('promote', $node_options);
237 if (module_exists('comment')) {
238 $node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
239 }
240 if (module_exists('taxonomy')) {
241 $node->taxonomy = $taxonomy;
242 }
243
244 if ($nid > 0) {
245 $node->revision = TRUE;
246 $node->new_file = FALSE;
247 } else {
248 $node->revision = FALSE;
249 $node->new_file = TRUE;
250 }
251 // $node->images[IMAGE_ORIGINAL] = $filepath;
252
253 // Save the node.
254 $node = node_submit($node);
255 node_save($node);
256
257 return $node;
258 }
259
260
261 /**
262 * Helpers
263 */
264
265 function isedoc_import_with_index($dn,$fn) {
266 drupal_set_message(t("Importing using index $fn ..."),'status');
267 $logfp = fopen($fn.".log", 'w');
268 fclose ($logfp);
269
270 $fp = fopen($fn, 'r');
271 $node_ids = array();
272 $dep = 0;
273
274 while (!feof($fp)) {
275 $line = fgets($fp);
276 $line = substr($line, 0, -1);
277 $i = strpos($line, "[");
278 if ($i > 0) {
279 $j = strpos($line, ":", $i);
280 $l_depth = substr($line, $i+1, 1 + ($j-1) -($i+1) );
281 $i = strpos($line, "]", $j+1);
282 $l_base_id = substr($line, $j+1, 1 + ($i-1) - ($j+1));
283 $l_title = substr($line, $i+2);
284
285 $line = fgets($fp);
286 $line = substr($line, 0, -1);
287
288 $i = strpos($line, "!src=");
289 if ($i > 0) {
290 $l_src = $dn . "/". substr($line, $i+5) . ".wiki";
291
292 $info = isedoc_file_info($l_src, TRUE);
293 if ($info) {
294 //drupal_set_message(t(" -> [%dep:%bas] \"%tit\" -> %src", array( "%dep" => $l_depth, "%tit" => $l_title, "%bas" => $l_base_id, "%src" => $l_src)),'status');
295 $info['title'] = $l_title;
296 $info['src'] = $l_src;
297 $info['depth'] = $l_depth;
298 $info['baseid'] = $l_baseid;
299 $node = isedoc_create_doc_node_from ($info);
300
301 if ($dep > 0) {
302 $p_nid = $node_ids[$dep];
303 } else {
304 $p_nid = 0;
305 }
306 $node_ids[$l_depth] = $node->nid;
307 $dep = $l_depth;
308
309 drupal_set_message(t(" -> [%dep] %src -> %nid (parent=$p_nid)", array( "%dep" => $l_depth, "%src" => $l_src, "%nid" => $node->nid)),'status');
310 } else {
311 drupal_set_message(t(" -> [%dep:%bas] \"%tit\" -> %src", array( "%dep" => $l_depth, "%tit" => $l_title, "%bas" => $l_base_id, "%src" => $l_src)),'error');
312 }
313
314 }
315 }
316 }
317
318 /*
319 if (substr($line, 0, 7) == 'title=') {
320 $title = substr($line, 7);
321 } else {
322 $title = '__'.basename($fn);
323 }
324 if (substr($line, 0, 7) == 'src=') {
325 $src = substr($line, 5);
326 }
327 if ($is_full) {
328 $l_body = fread($fp, filesize($fn));
329 }
330 */
331 fclose ($fp);
332
333 }
334
335 function isedoc_file_info($fn, $is_full=FALSE) {
336 $l_body = '';
337 $fp = fopen($fn, 'r');
338 $info = array();
339 $done = 0;
340 while(!$done) {
341 $line = fgets($fp);
342 $line = substr($line, 0, -1);
343 $i = strpos ($line, "=");
344 if ($i > 0) {
345 $k = substr($line, 0, $i);
346 $v = substr($line, $i+1);
347 $info[$k] = $v;
348 } else {
349 $done = 1;
350 }
351 }
352 if ($is_full) {
353 $l_body = fread($fp, filesize($fn));
354 }
355 fclose ($fp);
356 $info['body'] = $l_body;
357 return $info;
358 }
359
360 function var_dump_r(&$ref, $pre=TRUE) {
361 ob_start(); var_dump($ref); $a=htmlspecialchars(ob_get_contents(),ENT_QUOTES); ob_end_clean();
362 if ($pre) {
363 return "<pre>DUMP=".$a . '</pre>';
364 } else {
365 return 'DUMP='.$a;
366 }
367 }
368 function var_dump_global($refname) {
369 global $$refname;
370 ob_start(); var_dump($$refname); $a=htmlspecialchars(ob_get_contents(),ENT_QUOTES); ob_end_clean();
371 return "DUMP[".$refname."]=".$a;
372 }
373
374 /**
375 * Returns an array of all books.
376 *
377 * This list may be used for generating a list of all the books, or for building
378 * the options for a form select.
379 */
380
381 function book_doc_page_exists($a_title) {
382 // $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(title) = LOWER('%s') AND type IN ()", $a_title));
383 $nid = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(title) = LOWER('%s')", $a_title));
384 return $nid;
385 }
386
387 function isedoc_book_get_books() {
388 static $all_books;
389
390 if (!isset($all_books)) {
391 $all_books = array();
392 $result = db_query("SELECT DISTINCT(bid) FROM {book}");
393 $nids = array();
394 while ($book = db_fetch_array($result)) {
395 $nids[] = $book['bid'];
396 }
397 if ($nids) {
398 $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"));
399 while ($link = db_fetch_array($result2)) {
400 $link['href'] = $link['link_path'];
401 $link['options'] = unserialize($link['options']);
402 $all_books[$link['bid']] = $link;
403 }
404 }
405 }
406 return $all_books;
407 }

  ViewVC Help
Powered by ViewVC 1.1.23