| 1 |
<?php
|
| 2 |
// $Id: bookimport.module,v 1.4 2006/01/28 07:26:18 puregin Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enables import of books from dxml (Drupal Book XML) format files
|
| 7 |
*
|
| 8 |
* Copyright (C) 2006 Djun M. Kim
|
| 9 |
*
|
| 10 |
* This program is free software; you can redistribute it and/or
|
| 11 |
* modify it under the terms of the GNU General Public License as
|
| 12 |
* published by the Free Software Foundation; either version 2 of the
|
| 13 |
* License, or (at your option) any later version.
|
| 14 |
*
|
| 15 |
* See the GNU General Public License version 2 LICENSE file for
|
| 16 |
* full terms and conditions of use.
|
| 17 |
*
|
| 18 |
*/
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_menu().
|
| 22 |
*/
|
| 23 |
function bookimport_menu($may_cache) {
|
| 24 |
$items = array();
|
| 25 |
if ($may_cache) {
|
| 26 |
$items[] = array('path' => 'import',
|
| 27 |
'title' => t('import'),
|
| 28 |
'callback' => 'bookimport_page',
|
| 29 |
'access' => user_access('import books'),
|
| 30 |
'type' => MENU_NORMAL_ITEM);
|
| 31 |
$items[] = array('path' => 'import/book',
|
| 32 |
'title' => t('book'),
|
| 33 |
'callback' => 'bookimport_import',
|
| 34 |
'access' => user_access('import books'),
|
| 35 |
'type' => MENU_NORMAL_ITEM);
|
| 36 |
}
|
| 37 |
return $items;
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Implementation of hook_perm()
|
| 42 |
*/
|
| 43 |
function bookimport_perm() {
|
| 44 |
return array('import books');
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Implementation of hook_help()
|
| 49 |
*/
|
| 50 |
function bookimport_help($section) {
|
| 51 |
switch ($section) {
|
| 52 |
case 'admin/help#bookimport':
|
| 53 |
return t('The Bookimport module enables import of Drupal book XML files');
|
| 54 |
case 'admin/modules#description':
|
| 55 |
return t('Allows authorized users to import Drupal books from Drupal XML files');
|
| 56 |
case 'admin/modules/bookimport':
|
| 57 |
return t('Import Drupal book XML files');
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Implementation of global import/export page
|
| 63 |
*/
|
| 64 |
function bookimport_page() {
|
| 65 |
$output = '';
|
| 66 |
print theme('page', $output);
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Menu callback function.
|
| 71 |
*/
|
| 72 |
function bookimport_import() {
|
| 73 |
$op = $_POST['op'];
|
| 74 |
switch ($op) {
|
| 75 |
case 'Upload':
|
| 76 |
bookimport_form_getfile();
|
| 77 |
break;
|
| 78 |
case 'Import data':
|
| 79 |
bookimport_form_getoptions();
|
| 80 |
break;
|
| 81 |
case 'Cancel':
|
| 82 |
bookimport_form_getoptions();
|
| 83 |
break;
|
| 84 |
default:
|
| 85 |
bookimport_form_getfile();
|
| 86 |
}
|
| 87 |
|
| 88 |
}
|
| 89 |
|
| 90 |
/**
|
| 91 |
* XML import page. This form obtains the necessary information from
|
| 92 |
* the user.
|
| 93 |
*/
|
| 94 |
function bookimport_form_getfile() {
|
| 95 |
$form['group1'] =
|
| 96 |
array(
|
| 97 |
'#type' => 'fieldset',
|
| 98 |
'#title' => t('Step 1: Upload a Drupal Book XML (dxml) file'),
|
| 99 |
'#collapsible' => TRUE,
|
| 100 |
'#collapsed' => TRUE,
|
| 101 |
);
|
| 102 |
$form['group1']['xml_upload'] =
|
| 103 |
array(
|
| 104 |
'#type' => 'file',
|
| 105 |
'#title' => t('File name'),
|
| 106 |
'#size' => 40,
|
| 107 |
'#description' => t('A file containing XML data in UTF-8. Enter filename and press "Upload" to enable import options. The file will not be imported until confirmed in the next step.'),
|
| 108 |
);
|
| 109 |
|
| 110 |
$form['group1']['op_file']['upload'] =
|
| 111 |
array(
|
| 112 |
'#type' => 'submit',
|
| 113 |
'#value' => t('Upload'),
|
| 114 |
);
|
| 115 |
|
| 116 |
$form['#method'] = 'post';
|
| 117 |
$form['#action'] = url('import/book');
|
| 118 |
$form['#attributes'] = array("enctype" => "multipart/form-data");
|
| 119 |
$output = drupal_get_form('bookimport_form_getfile', $form);
|
| 120 |
print theme('page', $output);
|
| 121 |
}
|
| 122 |
|
| 123 |
function bookimport_form_getfile_validate($form_id, $form_values) {
|
| 124 |
// No validation, yet.
|
| 125 |
}
|
| 126 |
|
| 127 |
function bookimport_form_getfile_submit($form_id, $form_values) {
|
| 128 |
$file = file_save_upload('xml_upload');
|
| 129 |
if ($_POST['op'] == t('Upload')) {
|
| 130 |
// already been saved to temporary location
|
| 131 |
watchdog('bookimport', t("Uploaded file: %file", array('%file' => theme('placeholder', $file->filename))));
|
| 132 |
bookimport_form_getoptions($file);
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
/** Generates the form used to set options for the import.
|
| 137 |
*/
|
| 138 |
function bookimport_form_getoptions($file = NULL) {
|
| 139 |
$form = array();
|
| 140 |
$form['file'] =
|
| 141 |
array(
|
| 142 |
'#type' => 'item',
|
| 143 |
'#title' => t('Uploaded file'),
|
| 144 |
'#value' => sprintf("%s (%d Bytes)", $file->filename, $file->filesize)
|
| 145 |
);
|
| 146 |
$form['group2'] =
|
| 147 |
array(
|
| 148 |
'#type' => 'fieldset',
|
| 149 |
'#title' => t('Step 2: Update or Create the book'),
|
| 150 |
'#collapsible' => TRUE,
|
| 151 |
'#collapsed' => TRUE,
|
| 152 |
);
|
| 153 |
|
| 154 |
$form['group2']['import_action'] =
|
| 155 |
array(
|
| 156 |
'#type' => 'radios',
|
| 157 |
'#title' => t("Action"),
|
| 158 |
'#default_value' => variable_get('import_action', 0),
|
| 159 |
'#options' => array('0' => t("Update an existing book"),
|
| 160 |
'1' => t("Create a new book")),
|
| 161 |
'#description' => t("Choose whether to import into an existing book, or to create a new book."),
|
| 162 |
);
|
| 163 |
|
| 164 |
$form['group2']['php_import'] =
|
| 165 |
array(
|
| 166 |
'#type' => 'radios',
|
| 167 |
'#title' => t("Allow PHP Import"),
|
| 168 |
'#default_value' => variable_get('allow_php_import', 1),
|
| 169 |
'#options' => array('0' => t("Allow PHP content to be created/updated"),
|
| 170 |
'1' => t("Allow PHP content to be created, but set type to Full HTML"),
|
| 171 |
'2' => t("Empty PHP nodes before importing")
|
| 172 |
),
|
| 173 |
'#description' => t("Choose how to handle import of PHP content."),
|
| 174 |
);
|
| 175 |
$form['group2']['import'] =
|
| 176 |
array(
|
| 177 |
'#type' => 'submit',
|
| 178 |
'#value' => t('Import data'),
|
| 179 |
);
|
| 180 |
$form['group2']['cancel'] =
|
| 181 |
array(
|
| 182 |
'#type' => 'submit',
|
| 183 |
'#value' => t('Cancel'),
|
| 184 |
);
|
| 185 |
$form['#method'] = 'post';
|
| 186 |
$form['#action'] = url('import/book');
|
| 187 |
$form['#attributes'] = array("enctype" => "multipart/form-data");
|
| 188 |
$output = drupal_get_form('bookimport_form_getoptions', $form);
|
| 189 |
print theme('page', $output);
|
| 190 |
}
|
| 191 |
|
| 192 |
function bookimport_form_getoptions_validate() {
|
| 193 |
// echo "bookimport_form_getoptions_validate()<br/>";
|
| 194 |
$op = $_POST['op'];
|
| 195 |
}
|
| 196 |
|
| 197 |
function bookimport_form_getoptions_submit() {
|
| 198 |
// echo "bookimport_form_getoptions_validate()<br/>";
|
| 199 |
$op = $_POST['op'];
|
| 200 |
$file = file_save_upload('xml_upload');
|
| 201 |
|
| 202 |
switch ($op) {
|
| 203 |
case 'Cancel':
|
| 204 |
bookimport_cancel($file);
|
| 205 |
break;
|
| 206 |
case 'Import data':
|
| 207 |
$import_action = _bookimport_form_action_radio2text();
|
| 208 |
$phpimport_option = _bookimport_form_phpimport_radio2text();
|
| 209 |
bookimport_importxml($file, $import_action, $phpimport_option);
|
| 210 |
break;
|
| 211 |
default:
|
| 212 |
bookimport_form_getfile();
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|
| 216 |
function bookimport_cancel($file) {
|
| 217 |
if ($file) {
|
| 218 |
file_delete($file->filepath);
|
| 219 |
unset($file);
|
| 220 |
drupal_set_message(t("Cancelled import of file."));
|
| 221 |
drupal_goto("import/book");
|
| 222 |
}
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
| 226 |
* Imports an uploaded Drupal book XML file into the database, either
|
| 227 |
* as a new book (insert mode), or as an update of an existing book.
|
| 228 |
* This is called from bookimport_form_getoptions_submit() once all of the necessary
|
| 229 |
* parameters have been set up.
|
| 230 |
*/
|
| 231 |
function bookimport_importxml($file, $import_action, $phpimport_option) {
|
| 232 |
define('DEBUG', 3);
|
| 233 |
|
| 234 |
$xmlfile = $file->filepath;
|
| 235 |
|
| 236 |
if (DEBUG > 0) {
|
| 237 |
print "<pre>";
|
| 238 |
print "Import action = $import_action<br />";
|
| 239 |
print "PHP Import option = $phpimport_option<br />";
|
| 240 |
print "File = $xmlfile<br />";
|
| 241 |
}
|
| 242 |
|
| 243 |
// Create the XML parser
|
| 244 |
if (!$xml = xml_parser_create()) {
|
| 245 |
drupal_set_message("Can't create XML parser\n");
|
| 246 |
watchdog('bookimport', t("Can't create XML parser"));
|
| 247 |
drupal_goto('import/book');
|
| 248 |
}
|
| 249 |
|
| 250 |
require_once('node.php'); // Drupal node defined as PHP object
|
| 251 |
require_once('save_node.php'); // callback that saves nodes to the database
|
| 252 |
require_once('drupal_export_parser.php');
|
| 253 |
|
| 254 |
// Create the helper class (event handlers)
|
| 255 |
$parser = new drupal_export_parser('', 'save'); // register callback
|
| 256 |
$parser->action = $import_action;
|
| 257 |
$parser->phpimport = $phpimport_option;
|
| 258 |
|
| 259 |
xml_set_object($xml, $parser);
|
| 260 |
xml_set_element_handler($xml, 'start_element', 'end_element');
|
| 261 |
xml_set_character_data_handler($xml, 'character_data');
|
| 262 |
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
|
| 263 |
|
| 264 |
// Open the XML file and parse it
|
| 265 |
if (!$fp = fopen($xmlfile, 'r')) {
|
| 266 |
drupal_set_message("Can't read XML file $xmlfile\n");
|
| 267 |
watchdog('bookimport', t("Can't read XML file $xmlfile"));
|
| 268 |
drupal_goto('import/book');
|
| 269 |
}
|
| 270 |
|
| 271 |
while ($data = fread($fp, 4096)) {
|
| 272 |
if (!xml_parse($xml, $data, feof($fp))) {
|
| 273 |
drupal_set_message("Can't parse XML data\n");
|
| 274 |
watchdog('bookimport', t("Can't parse XML data."));
|
| 275 |
drupal_goto('import');
|
| 276 |
}
|
| 277 |
}
|
| 278 |
fclose($fp);
|
| 279 |
|
| 280 |
// Clean up
|
| 281 |
xml_parser_free($xml);
|
| 282 |
|
| 283 |
make_links();
|
| 284 |
|
| 285 |
if (DEBUG > 1) {
|
| 286 |
print "</pre>";
|
| 287 |
};
|
| 288 |
drupal_set_message("Finished import of file: $xmlfile");
|
| 289 |
drupal_goto('import');
|
| 290 |
|
| 291 |
}
|
| 292 |
|
| 293 |
|
| 294 |
/**
|
| 295 |
* Converts code of an import action to a text string
|
| 296 |
*/
|
| 297 |
function _bookimport_form_action_radio2text() {
|
| 298 |
if (isset($_POST['edit']['import_action'])) {
|
| 299 |
switch ($_POST['edit']['import_action']) {
|
| 300 |
case 0:
|
| 301 |
return 'update';
|
| 302 |
case 1:
|
| 303 |
return 'insert';
|
| 304 |
}
|
| 305 |
}
|
| 306 |
else {
|
| 307 |
print "edit/import_action not set";
|
| 308 |
}
|
| 309 |
}
|
| 310 |
|
| 311 |
|
| 312 |
/**
|
| 313 |
* Converts code of an import action to a text string
|
| 314 |
*/
|
| 315 |
function _bookimport_form_phpimport_radio2text() {
|
| 316 |
if (isset($_POST['edit']['php_import'])) {
|
| 317 |
switch ($_POST['edit']['php_import']) {
|
| 318 |
case 0:
|
| 319 |
return 'allow_php';
|
| 320 |
case 1:
|
| 321 |
return 'php_as_html';
|
| 322 |
case 2:
|
| 323 |
return 'no_php';
|
| 324 |
}
|
| 325 |
}
|
| 326 |
}
|
| 327 |
|