| 1 |
<?php |
<?php |
| 2 |
// $Id: bookimport.module,v 1.3 2006/01/27 06:45:17 puregin Exp $ |
// $Id: bookimport.module,v 1.4 2006/01/28 07:26:18 puregin Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 21 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 22 |
*/ |
*/ |
| 23 |
function bookimport_menu($may_cache) { |
function bookimport_menu($may_cache) { |
| 24 |
|
$items = array(); |
| 25 |
if ($may_cache) { |
if ($may_cache) { |
| 26 |
$items[] = array('path' => 'import', |
$items[] = array('path' => 'import', |
| 27 |
'title' => t('import'), |
'title' => t('import'), |
| 30 |
'type' => MENU_NORMAL_ITEM); |
'type' => MENU_NORMAL_ITEM); |
| 31 |
$items[] = array('path' => 'import/book', |
$items[] = array('path' => 'import/book', |
| 32 |
'title' => t('book'), |
'title' => t('book'), |
| 33 |
'callback' => 'bookimport_page_xml', |
'callback' => 'bookimport_import', |
| 34 |
'access' => user_access('import books'), |
'access' => user_access('import books'), |
| 35 |
'type' => MENU_NORMAL_ITEM); |
'type' => MENU_NORMAL_ITEM); |
|
$items[] = array('path' => 'import/book/xml', |
|
|
'title' => t('Drupal Book XML import'), |
|
|
'callback' => 'bookimport_page_xml', |
|
|
'access' => user_access('import books'), |
|
|
'type' => MENU_CALLBACK); |
|
| 36 |
} |
} |
| 37 |
|
return $items; |
|
return $items ? $items : array(); |
|
| 38 |
} |
} |
| 39 |
|
|
| 40 |
/** |
/** |
| 55 |
return t('Allows authorized users to import Drupal books from Drupal XML files'); |
return t('Allows authorized users to import Drupal books from Drupal XML files'); |
| 56 |
case 'admin/modules/bookimport': |
case 'admin/modules/bookimport': |
| 57 |
return t('Import Drupal book XML files'); |
return t('Import Drupal book XML files'); |
|
|
|
| 58 |
} |
} |
| 59 |
} |
} |
| 60 |
|
|
| 66 |
print theme('page', $output); |
print theme('page', $output); |
| 67 |
} |
} |
| 68 |
|
|
|
function _bookimport_form_radios_action() { |
|
|
return |
|
|
array( |
|
|
'#type' => 'radios', |
|
|
'#title' => t("Action"), |
|
|
'#default_value' => variable_get('import_action', 0), |
|
|
'#options' => array('0' => t("Update an existing book"), |
|
|
'1' => t("Create a new book")), |
|
|
'#description' => t("Choose whether to import into an existing book, or to create a new book.")); |
|
|
} |
|
|
|
|
| 69 |
/** |
/** |
| 70 |
* Converts code of an import action to a text string |
* Menu callback function. |
| 71 |
*/ |
*/ |
| 72 |
function _bookimport_form_radio2text() { |
function bookimport_import() { |
| 73 |
if (isset($_POST['edit']['import_action'])) { |
$op = $_POST['op']; |
| 74 |
switch ($_POST['edit']['import_action']) { |
switch ($op) { |
| 75 |
case 0: |
case 'Upload': |
| 76 |
return 'update'; |
bookimport_form_getfile(); |
| 77 |
case 1: |
break; |
| 78 |
return 'insert'; |
case 'Import data': |
| 79 |
} |
bookimport_form_getoptions(); |
| 80 |
} |
break; |
| 81 |
else { |
case 'Cancel': |
| 82 |
print "edit/import_action not set"; |
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 |
* XML import page. This form obtains the necessary information from |
| 92 |
* the user, then calls bookimport_importxml() to do the actual |
* the user. |
|
* import. |
|
| 93 |
*/ |
*/ |
| 94 |
function bookimport_page_xml() { |
function bookimport_form_getfile() { |
|
global $error; |
|
|
|
|
|
$file = file_check_upload('xml_upload'); |
|
|
watchdog('bookimport', t("Uploaded file: %file", array('%file' => theme('placeholder', $file->filename)))); |
|
|
|
|
|
if ($_POST['op'] == t('Upload')) { |
|
|
// save to temporary location |
|
|
$file = file_save_upload('xml_upload'); |
|
|
// get all nid from file |
|
|
} |
|
|
if ($_POST['op'] == t('Remove file')) { |
|
|
if ($file) { |
|
|
print "delete file"; |
|
|
file_delete($file->filepath); |
|
|
Unset($file); |
|
|
} |
|
|
} |
|
|
|
|
|
if ($file) { |
|
|
$form['file'] = |
|
|
array( |
|
|
'#type' => 'item', |
|
|
'#title' => t('Uploaded file'), |
|
|
'#value' => sprintf("%s (%d Bytes)", $file->filename, $file->filesize) |
|
|
); |
|
|
$form['op_file']['delete'] = |
|
|
array( |
|
|
'#type' => 'submit', |
|
|
'#value' => t('Remove file'), |
|
|
); |
|
|
} |
|
|
|
|
| 95 |
$form['group1'] = |
$form['group1'] = |
| 96 |
array( |
array( |
| 97 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 104 |
'#type' => 'file', |
'#type' => 'file', |
| 105 |
'#title' => t('File name'), |
'#title' => t('File name'), |
| 106 |
'#size' => 40, |
'#size' => 40, |
| 107 |
'#description' => t('A file containing XML data in UTF-8. Enter filename and press "Upload" to enable import options.'), |
'#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'] = |
$form['group1']['op_file']['upload'] = |
| 112 |
'#type' => 'submit', |
'#type' => 'submit', |
| 113 |
'#value' => t('Upload'), |
'#value' => t('Upload'), |
| 114 |
); |
); |
|
|
|
|
if ($file) { |
|
|
$form['group2'] = |
|
|
array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('Step 2: Update or Create the book'), |
|
|
'#collapsible' => TRUE, |
|
|
'#collapsed' => TRUE, |
|
|
); |
|
|
|
|
|
$form['group2']['import_action'] = |
|
|
array( |
|
|
'#type' => 'radios', |
|
|
'#title' => t("Action"), |
|
|
'#default_value' => variable_get('import_action', 0), |
|
|
'#options' => array('0' => t("Update an existing book"), |
|
|
'1' => t("Create a new book")), |
|
|
'#description' => t("Choose whether to import into an existing book, or to create a new book."), |
|
|
); |
|
|
|
|
|
$form['group2']['op_xml']['import'] = |
|
|
array( |
|
|
'#type' => 'submit', |
|
|
'#value' => t('Import data'), |
|
|
); |
|
| 115 |
|
|
|
} |
|
| 116 |
$form['#method'] = 'post'; |
$form['#method'] = 'post'; |
| 117 |
$form['#action'] = url('import/book'); |
$form['#action'] = url('import/book'); |
| 118 |
$form['#attributes'] = array("enctype" => "multipart/form-data"); |
$form['#attributes'] = array("enctype" => "multipart/form-data"); |
| 119 |
$output = drupal_get_form('bookimport_page_xml', $form); |
$output = drupal_get_form('bookimport_form_getfile', $form); |
| 120 |
|
print theme('page', $output); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
if ($_POST['op'] == t('Import data')) { |
function bookimport_form_getfile_validate($form_id, $form_values) { |
| 124 |
bookimport_importxml(); |
// 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); |
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 |
* 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. |
* as a new book (insert mode), or as an update of an existing book. |
| 228 |
* This is called from bookimport_page_xml() once all of the necessary |
* This is called from bookimport_form_getoptions_submit() once all of the necessary |
| 229 |
* parameters have been set up. |
* parameters have been set up. |
| 230 |
*/ |
*/ |
| 231 |
function bookimport_importxml() { |
function bookimport_importxml($file, $import_action, $phpimport_option) { |
| 232 |
define('DEBUG', 0); |
define('DEBUG', 3); |
| 233 |
|
|
| 234 |
$edit = $_POST['edit']; |
$xmlfile = $file->filepath; |
|
$op = _bookimport_form_radio2text(); |
|
| 235 |
|
|
| 236 |
if (DEBUG > 0) { |
if (DEBUG > 0) { |
| 237 |
print "<pre>"; |
print "<pre>"; |
| 238 |
print "Op = $op<br />"; |
print "Import action = $import_action<br />"; |
| 239 |
|
print "PHP Import option = $phpimport_option<br />"; |
| 240 |
|
print "File = $xmlfile<br />"; |
| 241 |
} |
} |
|
|
|
|
$fileh = file_check_upload('xml_upload'); |
|
|
$xmlfile = $fileh->filepath; |
|
| 242 |
|
|
| 243 |
// Create the XML parser |
// Create the XML parser |
| 244 |
if (!$xml = xml_parser_create()) { |
if (!$xml = xml_parser_create()) { |
| 245 |
drupal_set_message("Can't create XML parser\n"); |
drupal_set_message("Can't create XML parser\n"); |
| 246 |
drupal_goto('import/book/xml'); |
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 |
require_once('node.php'); // Drupal node defined as PHP object |
| 253 |
|
|
| 254 |
// Create the helper class (event handlers) |
// Create the helper class (event handlers) |
| 255 |
$parser = new drupal_export_parser('', 'save'); // register callback |
$parser = new drupal_export_parser('', 'save'); // register callback |
| 256 |
$parser->op = $op; |
$parser->action = $import_action; |
| 257 |
|
$parser->phpimport = $phpimport_option; |
| 258 |
|
|
| 259 |
xml_set_object($xml, $parser); |
xml_set_object($xml, $parser); |
| 260 |
xml_set_element_handler($xml, 'start_element', 'end_element'); |
xml_set_element_handler($xml, 'start_element', 'end_element'); |
| 264 |
// Open the XML file and parse it |
// Open the XML file and parse it |
| 265 |
if (!$fp = fopen($xmlfile, 'r')) { |
if (!$fp = fopen($xmlfile, 'r')) { |
| 266 |
drupal_set_message("Can't read XML file $xmlfile\n"); |
drupal_set_message("Can't read XML file $xmlfile\n"); |
| 267 |
drupal_goto('import/book/xml'); |
watchdog('bookimport', t("Can't read XML file $xmlfile")); |
| 268 |
|
drupal_goto('import/book'); |
| 269 |
} |
} |
| 270 |
|
|
| 271 |
while ($data = fread($fp, 4096)) { |
while ($data = fread($fp, 4096)) { |
| 272 |
if (!xml_parse($xml, $data, feof($fp))) { |
if (!xml_parse($xml, $data, feof($fp))) { |
| 273 |
drupal_set_message("Can't parse XML data\n"); |
drupal_set_message("Can't parse XML data\n"); |
| 274 |
|
watchdog('bookimport', t("Can't parse XML data.")); |
| 275 |
drupal_goto('import'); |
drupal_goto('import'); |
| 276 |
} |
} |
| 277 |
} |
} |
| 289 |
drupal_goto('import'); |
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 |
|
|