first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
<?php
/**
* @file
* The Node export Drupal format handler.
*
* Adds Drupal var export format to Node export.
*/
/**
* Export callback.
*/
function node_export_drupal_export($nodes, $format) {
require_once DRUPAL_ROOT . '/includes/utility.inc';
return drupal_var_export($nodes);
}
/**
* Import callback.
*/
function node_export_drupal_import($code_string) {
if (substr(ltrim($code_string), 0, 6) == "array(") {
$nodes = eval('return ' . $code_string . ';');
if (is_array($nodes)) {
return node_export_drupal_decode_objects($nodes);
}
}
}
/**
* Recursively convert arrays back to objects.
*
* This is only for backwards compatibility with the deprecated node_code format.
*/
function node_export_drupal_decode_objects($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = node_export_drupal_decode_objects($v);
}
}
if (isset($array['#_export_node_encode_object'])) {
unset($array['#_export_node_encode_object']);
$array = (object)$array;
}
return $array;
}
/**
* Callback for actions.
*/
function node_export_drupal_action_form($context, &$form_state) {
return node_export_action_form($context, $form_state, 'drupal');
}

View File

@@ -0,0 +1,345 @@
<?php
/**
* @file
* The Node export DSV format handler.
*
* Adds configurable DSV format to Node export.
*/
/**
* Settings callback.
*/
function node_export_dsv_settings($form, $form_state) {
$settings['dsv'] = array(
'#type' => 'fieldset',
'#title' => t('DSV format settings'),
'#description' => t(
'Select how your DSV output will be formatted - this must be configured the
same on both sites. By default this is configured to RFC4180 CSV format
where the delimiter is a comma (,), the enclosure is a double-quote ("),
and the seperator is CRLF (\r\n). Not all configurations may be possible,
use wisely. Enclosure will only be used to escape values that contain any
of the configured strings. Additionally single-quotes will be used to
escape values that are equivalent to reserved words (NULL, TRUE, FALSE).'
),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$settings['dsv']['node_export_dsv_delimiter'] = array(
'#type' => 'textfield',
'#title' => t('Value delimiter'),
'#size' => 5,
'#maxlength' => 255,
'#default_value' => variable_get('node_export_dsv_delimiter', ','),
'#required' => TRUE,
);
$settings['dsv']['node_export_dsv_enclosure'] = array(
'#type' => 'textfield',
'#title' => t('Escape enclosure'),
'#size' => 5,
'#maxlength' => 255,
'#default_value' => variable_get('node_export_dsv_enclosure', '"'),
'#required' => TRUE,
);
$settings['dsv']['node_export_dsv_seperator'] = array(
'#type' => 'textfield',
'#title' => t('Record seperator'),
'#size' => 5,
'#maxlength' => 255,
'#default_value' => variable_get('node_export_dsv_seperator', '\r\n'),
'#required' => TRUE,
);
$settings['dsv']['node_export_dsv_escape_eol'] = array(
'#type' => 'checkbox',
'#title' => t('Always escape values containing line breaks'),
'#default_value' => variable_get('node_export_dsv_escape_eol', 1),
'#description' => t('This is to overcome problems where Windows injects CRLF line breaks.'),
);
return $settings;
}
function node_export_dsv_string($string) {
$replace = array(
'\n' => "\n",
'\r' => "\r",
'\t' => "\t",
'\v' => "\v",
'\e' => "\e",
'\f' => "\f",
);
return str_replace(array_keys($replace), array_values($replace), $string);
}
/**
* Export callback.
*/
function node_export_dsv_export($nodes, $format) {
$delimiter = node_export_dsv_string(variable_get('node_export_dsv_delimiter', ','));
$enclosure = node_export_dsv_string(variable_get('node_export_dsv_enclosure', '"'));
$seperator = node_export_dsv_string(variable_get('node_export_dsv_seperator', '\r\n'));
$escape_eol = variable_get('node_export_dsv_escape_eol', 1);
return node_export_dsv_encode($nodes, $delimiter, $enclosure, $seperator, $escape_eol);
}
/**
* Build DSV string.
*/
function node_export_dsv_encode($nodes, $delimiter, $enclosure, $seperator, $escape_eol) {
$encoded_nodes = array();
$dsv_lines = array();
$node_keys = array();
foreach (array_keys($nodes) as $node_key) {
$new_node_key = 'node_' . $node_key;
$node_keys[] = $new_node_key;
node_export_dsv_encode_node($encoded_nodes, $new_node_key, $nodes[$node_key]);
}
$dsv_lines['node_export_dsv_header'] = array_keys($encoded_nodes);
foreach (array_keys($encoded_nodes) as $header_value) {
$encoded_nodes[$header_value] = array_merge(array_fill_keys($node_keys, NULL), $encoded_nodes[$header_value]);
foreach (array_keys($encoded_nodes[$header_value]) as $encoded_node_key) {
$dsv_lines[$encoded_node_key][$header_value] = $encoded_nodes[$header_value][$encoded_node_key];
}
}
return node_export_dsv_array_to_dsv($dsv_lines, $delimiter, $enclosure, $seperator, $escape_eol);
}
/**
* Process a node and update $header and $encoded_nodes accordingly.
*/
function node_export_dsv_encode_node(&$encoded_nodes, $node_key, $var, $parent = NULL) {
foreach ($var as $k => &$v) {
// Get the new header value.
$header_value = node_export_dsv_encode_header_value($parent, $var, $k);
if (is_object($v) || is_array($v)) {
// Recurse through the structure.
node_export_dsv_encode_node($encoded_nodes, $node_key, $v, $header_value);
}
else {
// Create a safe text version of this value and store it against the header using a safe key.
$encoded_nodes[$header_value][$node_key] = node_export_dsv_encode_sanitize_value($v);
}
}
}
/**
* Encode a value.
*/
function node_export_dsv_encode_sanitize_value($var) {
if (is_numeric($var)) {
return $var;
}
elseif (is_bool($var)) {
return ($var ? 'TRUE' : 'FALSE');
}
elseif (is_null($var)) {
return 'NULL';
}
elseif (is_string($var) && !empty($var)) {
// Single-quote strings that could be confused for null or boolean.
if (in_array(strtoupper($var), array('TRUE', 'FALSE', 'NULL'))) {
$var = "'" . $var . "'";
}
return $var;
}
else {
return '';
}
}
/**
* Decode a value.
*/
function node_export_dsv_decode_sanitize_value($var) {
// Allow numeric, bool, and null values to pass right back as is.
if (is_numeric($var) || is_bool($var) || is_null($var)) {
return $var;
}
// Allow the special case strings back as is.
elseif (in_array(strtoupper($var), array("'TRUE'", "'FALSE'", "'NULL'"))) {
return $var;
}
// Assume this is a string.
return "'" . str_replace("'", "\'", $var) . "'";
}
/**
* Create header value from $parents, $var, and $k.
*/
function node_export_dsv_encode_header_value($parents, $var, $k) {
if (is_null($parents)) {
// Special case; on the first level do not prefix the key.
$header_value = $k;
}
elseif (is_object($var)) {
$header_value = $parents . "->" . $k;
}
elseif (is_array($var)) {
$header_value = $parents . "['" . $k . "']";
}
return $header_value;
}
/**
* Import callback.
*/
function node_export_dsv_import($code_string) {
$delimiter = node_export_dsv_string(variable_get('node_export_dsv_delimiter', ','));
$enclosure = node_export_dsv_string(variable_get('node_export_dsv_enclosure', '"'));
$seperator = node_export_dsv_string(variable_get('node_export_dsv_seperator', '\r\n'));
return node_export_dsv_decode($code_string, $delimiter, $enclosure, $seperator);
}
/**
* Interpret a DSV string.
*/
function node_export_dsv_decode($code_string, $delimiter, $enclosure, $seperator) {
// Get array data from DSV.
$array = @node_export_dsv_dsv_to_array($code_string, $delimiter, $enclosure, $seperator);
// If the first two rows are of equal length, we can assume this is a DSV.
// Also checks there are a decent number of fields.
if (!empty($array[0]) && !empty($array[1]) && count($array[0]) > 10 && count($array[0]) == count($array[1])) {
$nodes = array();
// Assume row 0 is the header, and the rest of the rows are the nodes.
$header = array_shift($array);
// Build the nodes.
foreach ($array as &$row) {
$node = (object)array();
foreach ($row as $key => $item) {
$item = node_export_dsv_decode_sanitize_value($item);
eval('$node->' . $header[$key] . ' = ' . $item . ';');
}
$nodes[] = $node;
}
return $nodes;
}
}
/**
* Encode DSV.
*/
function node_export_dsv_array_to_dsv($array, $delimiter, $enclosure, $seperator, $escape_eol) {
$lines = array();
foreach ($array as $line) {
$out_item = array();
foreach ($line as $item) {
if (stripos($item, $enclosure) !== FALSE) {
$item = str_replace($enclosure, $enclosure . $enclosure, $item);
}
if (
(stripos($item, $delimiter) !== FALSE)
|| (stripos($item, $enclosure) !== FALSE)
|| (stripos($item, $seperator) !== FALSE)
|| ($escape_eol && stripos($item, "\n") !== FALSE)
) {
$item = $enclosure . $item . $enclosure;
}
$out_item[] = $item;
}
$lines[] = implode($delimiter, $out_item);
}
return implode($seperator, $lines);
}
/**
* Decode DSV.
*/
function node_export_dsv_dsv_to_array($string, $delimiter, $enclosure, $seperator) {
$lines = array();
$out_item = array();
$count = strlen($string);
$escape = FALSE;
$double_escape = FALSE;
$position = 0;
$i = 0;
$seperators = str_split($seperator);
while ($i < $count) {
$c = $string[$i];
// Determine whether this is an EOL.
$is_eol = TRUE;
for ($j = 0; $j < count($seperators); $j++) {
if (!isset($string[$i + $j]) || $string[$i + $j] != $seperators[$j]) {
$is_eol = FALSE;
break;
}
}
if ($is_eol) {
if ($escape) {
$out_item[$position] .= $c;
}
else {
$i += count($seperators);
$lines[] = $out_item;
$out_item = array();
$position = 0;
continue;
}
}
elseif ($c == $delimiter) {
if ($escape) {
$out_item[$position] .= $c;
}
else {
if ($string[$i - 1] == $delimiter) {
$out_item[$position] .= '';
}
$position++;
$escape = FALSE;
$double_escape = FALSE;
}
}
elseif ($c == $enclosure) {
if ($double_escape) {
$out_item[$position] .= $enclosure;
$double_escape = FALSE;
}
if ($escape) {
$escape = FALSE;
$double_escape = TRUE;
}
else {
$escape = TRUE;
$double_escape = FALSE;
}
}
else {
if ($double_escape) {
$out_item[$position] .= $enclosure;
$double_escape = FALSE;
}
$out_item[$position] .= $c;
}
$i++;
}
if (!empty($out_item)) {
$lines[] = $out_item;
}
return $lines;
}
/**
* Callback for actions.
*/
function node_export_dsv_action_form($context, &$form_state) {
return node_export_action_form($context, $form_state, 'dsv');
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* @file
* The Node export JSON format handler.
*
* Adds JSON format to Node export.
*/
/**
* Export callback.
*/
function node_export_json_export($nodes, $format) {
return drupal_json_encode(node_export_json_encode_objects($nodes));
}
/**
* Import callback.
*/
function node_export_json_import($code_string) {
return node_export_json_decode_objects(drupal_json_decode($code_string));
}
/**
* Mark objects as being objects.
*/
function node_export_json_encode_objects($var) {
if (is_object($var)) {
$var = (array)$var;
$var['#node_export_object'] = '1';
}
if (is_array($var)) {
foreach ($var as $key => $value) {
$var[$key] = node_export_json_encode_objects($value);
}
}
return $var;
}
/**
* Recursively convert arrays back to objects.
*/
function node_export_json_decode_objects($array) {
if (is_array($array)) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = node_export_json_decode_objects($v);
}
}
if (isset($array['#node_export_object'])) {
unset($array['#node_export_object']);
$array = (object)$array;
}
return $array;
}
}
/**
* Callback for actions.
*/
function node_export_json_action_form($context, &$form_state) {
return node_export_action_form($context, $form_state, 'json');
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* @file
* The Node export serialize format handler.
*
* Adds serialize format to Node export.
*/
/**
* Export callback.
*/
function node_export_serialize_export($nodes, $format) {
return 'node_export_serialize::' . htmlspecialchars(serialize($nodes));
}
/**
* Import callback.
*/
function node_export_serialize_import($code_string) {
// Check for 'node_export_serialize::' at the start.
if (substr(ltrim($code_string), 0, 23) == 'node_export_serialize::') {
return unserialize(htmlspecialchars_decode(str_replace('node_export_serialize::', '', $code_string)));
}
}
/**
* Callback for actions.
*/
function node_export_serialize_action_form($context, &$form_state) {
return node_export_action_form($context, $form_state, 'serialize');
}

View File

@@ -0,0 +1,232 @@
<?php
/**
* @file
* The Node export XML format handler.
*
* Adds XML format to Node export.
*/
/**
* Export callback.
*/
function node_export_xml_export($nodes, $format) {
$xml_code = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
$xml_code .= "<node_export created=\"" . date('r') . "\">\n";
$xml_code .= node_export_xml_encode($nodes);
$xml_code .= "</node_export>";
return $xml_code;
}
/**
* Import callback.
*/
function node_export_xml_import($code_string) {
// Check for "<?xml" at the start.
if (substr(ltrim($code_string), 0, 5) == "<?xml") {
// Decode the XML.
$xml_class = new NodeExportXmlDecoder();
$result = $xml_class->decode($code_string);
// Convert the nodes into objects.
if (!isset($result['success'])) {
foreach($result as $k => $v) {
$result[$k] = (object)$v;
}
}
return $result;
}
}
/**
* Build XML string recursively.
*/
function node_export_xml_encode($var, $iteration = 0) {
$xml_code = "";
$tab = '';
for ($i = 0; $i <= $iteration; $i++) {
$tab = $tab . " ";
}
$iteration++;
foreach ($var as $key => $value) {
$attributes = array();
if (is_bool($value)) {
$attributes['type'] = 'boolean';
}
elseif (is_null($value)) {
$attributes['type'] = 'NULL';
}
elseif (is_object($value)) {
if ($iteration == 1 && isset($value->nid) && isset($value->type)) {
// Assume first-level object with a nid and type is a stdClass node.
$key = "node";
}
else {
$attributes['class'] = get_class($value);
}
$value = (array)$value;
}
if (is_array($value) && array_values($value) === $value) {
$attributes['_numeric_keys'] = "1";
}
$attr_string = "";
foreach ($attributes as $attr_name => $attr_value) {
$attr_string .= ' ' . $attr_name . '="' . $attr_value . '"';
}
if (is_numeric($key)) {
$key = "n" . $key;
}
$xml_code .= $tab . "<" . $key . $attr_string . ">";
if (is_array($value)) {
if (!empty($value)) {
$xml_code .= "\n";
$xml_code .= node_export_xml_encode($value, $iteration);
if (!is_numeric($key)) {
$xml_code .= $tab;
}
}
}
elseif (is_numeric($value)) {
$xml_code .= $value;
}
elseif (is_bool($value)) {
$xml_code .= ($value ? 'TRUE' : 'FALSE');
}
elseif (is_string($value)) {
$xml_code .= htmlspecialchars($value);
}
$xml_code .= "</" . $key . ">\n";
}
return $xml_code;
}
/**
* Class for parsing Node export XML.
*/
class NodeExportXmlDecoder {
var $stack;
var $output;
function decode($code_string) {
$parser = xml_parser_create();
xml_set_element_handler($parser, array(&$this, 'start_handler'), array(&$this, 'end_handler'));
xml_set_character_data_handler($parser, array(&$this, 'data_handler'));
$this->stack = array(
array(
'name' => 'node_export',
'attributes' => array(),
'children' => array(),
'data' => '',
)
);
if (!xml_parse($parser, $code_string)) {
$errors[] = "Node export XML import was unsuccessful, error details follow. No nodes imported.";
$line = xml_get_current_line_number($parser);
$column = xml_get_current_column_number($parser);
$error = xml_error_string(xml_get_error_code($parser));
$errors[] = "Line " . $line . ", Column " . $column .": ". $error;
$lines = explode("\n", $code_string, $line + 1);
$errors[] = "<pre>". htmlspecialchars($lines[$line - 1]) ."</pre>";
xml_parser_free($parser);
return array(
'success' => FALSE,
'output' => $errors,
);
}
xml_parser_free($parser);
$tmp = $this->build($this->stack[0]);
if (count($tmp) == 1) {
$this->output = array_pop($tmp);
}
else {
$this->output = array();
}
unset($this->stack);
return $this->output;
}
function build($stack) {
$result = array();
if (count($stack['children']) > 0) {
$keycount = array();
foreach ($stack['children'] as $child) {
$keycount[] = $child['name'];
}
if (count(array_unique($keycount)) != count($keycount)) {
// Enumerated array.
$children = array();
foreach ($stack['children'] as $child) {
$children[] = $this->build($child);
}
}
else {
// Associative array.
$children = array();
foreach ($stack['children'] as $child) {
if (!empty($stack['attributes']['_NUMERIC_KEYS'])) {
$child['name'] = intval(substr($child['name'], 1));
}
$children[$child['name']] = $this->build($child);
}
}
$result = array_merge($result, $children);
}
if (count($result) == 0) {
// An atomic value.
$return = trim($stack['data']);
if (isset($stack['attributes']['TYPE'])) {
if ($stack['attributes']['TYPE'] == 'boolean') {
return (trim($stack['data']) == 'TRUE' ? TRUE : FALSE);
}
elseif ($stack['attributes']['TYPE'] == 'NULL') {
return NULL;
}
}
return htmlspecialchars_decode(trim($stack['data']));
}
else {
// An array or object.
if (isset($stack['attributes']['CLASS'])) {
$object = new $stack['attributes']['CLASS']();
foreach ($result as $k => $v) {
$object->$k = $v;
}
$result = $object;
}
return $result;
}
}
function start_handler($parser, $name, $attributes = array()) {
$token = array();
$token['name'] = strtolower($name);
$token['attributes'] = $attributes;
$token['data'] = '';
$token['children'] = array();
$this->stack[] = $token;
}
function end_handler($parser, $name, $attributes = array()) {
$token = array_pop($this->stack);
$this->stack[count($this->stack) - 1]['children'][] = $token;
}
function data_handler($parser, $data) {
$this->stack[count($this->stack) - 1]['data'] .= $data;
}
}
/**
* Callback for actions.
*/
function node_export_xml_action_form($context, &$form_state) {
return node_export_action_form($context, $form_state, 'xml');
}