aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/lib/entities.php40
-rw-r--r--engine/lib/export.php119
2 files changed, 154 insertions, 5 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php
index 5dee5a4bb..8044b1df9 100644
--- a/engine/lib/entities.php
+++ b/engine/lib/entities.php
@@ -295,7 +295,19 @@
{
return delete_entity($this->get('guid'));
}
-
+
+ /**
+ * Export this class into a stdClass containing all necessary fields.
+ * Override if you wish to return more information than can be found in $this->attributes (shouldn't happen)
+ *
+ * @return stdClass
+ */
+ public function export()
+ {
+ $tmp = new stdClass;
+ $tmp->attributes = $this->attributes;
+ return $tmp;
+ }
}
/**
@@ -677,4 +689,30 @@
return delete_data("DELETE from {$CONFIG->dbprefix}entity_relationships where guid_one=$guid_one and relationship='$relationship' and guid_two=$guid_two");
}
+ /**
+ * Handler called by trigger_plugin_hook on the "export" event.
+ */
+ function export_entity_plugin_hook($hook, $entity_type, $returnvalue, $params)
+ {
+ // Sanity check values
+ if ((!is_array($params)) && (!isset($params['guid'])))
+ throw new InvalidParameterException("GUID has not been specified during export, this should never happen.");
+
+ if (!is_array($returnvalue))
+ throw new InvalidParameterException("Entity serialisation function passed a non-array returnvalue parameter");
+
+ $guid = (int)$params['guid'];
+
+ // Get the entity
+ $entity = get_entity($guid);
+
+ if ($entity instanceof ElggEntity)
+ $returnvalue[] = $entity; // Add object to list of things to serialise - actual serialisation done later
+
+ return $returnvalue;
+ }
+
+ /** Register the hook, ensuring entities are serialised first */
+ register_plugin_hook("export", "all", "export_entity_plugin_hook", 0);
+
?> \ No newline at end of file
diff --git a/engine/lib/export.php b/engine/lib/export.php
index 4a39d295f..89b5a1ef5 100644
--- a/engine/lib/export.php
+++ b/engine/lib/export.php
@@ -15,14 +15,45 @@
*
* This function exports a GUID and all information related to it in an XML format.
*
+ * This function makes use of the "serialise" plugin hook, which is passed an array to which plugins
+ * should add data to be serialised to.
+ *
+ * @see ElggEntity for an example of its usage.
* @param int $guid The GUID.
* @return xml
*/
function export($guid)
{
- // trigger serialise event
-
- // serialise resultant object
+ global $CONFIG;
+
+ $guid = (int)$guid;
+
+ // Initialise the array
+ $to_be_serialised = array();
+
+ // Trigger a hook to
+ $to_be_serialised = trigger_plugin_hook("export", "all", array("guid" => $guid), $to_be_serialised);
+
+ // Sanity check
+ if (!is_array($to_be_serialised)) throw new ExportException("There was a problem during the serialisation of GUID:$guid");
+
+ // Now serialise the result to XML
+ $wrapper = new stdClass;
+
+ // Construct header
+ $wrapper->header = new stdClass;
+ $wrapper->header->date = date("r");
+ $wrapper->header->timestamp = time();
+ $wrapper->header->domain = $CONFIG->wwwroot;
+ $wrapper->header->guid = $guid;
+ $wrapper->header->uuid = guid_to_uuid($guid);
+ $wrapper->header->exported_by = guid_to_uuid($_SESSION['id']);
+
+ // Construct data
+ $wrapper->data = $to_be_serialised;
+
+ return serialise_object_to_xml($wrapper, "elggexport");
+
/*
XML will look something like this:
@@ -92,6 +123,86 @@
{
global $CONFIG;
- return md5($CONFIG->wwwroot . ":$guid");
+ return "UUID:".md5($CONFIG->wwwroot) . ":$guid";
}
+
+ /**
+ * This function serialises an object recursively into an XML representation.
+ * The function attempts to call $data->export() which expects a stdClass in return, otherwise it will attempt to
+ * get the object variables using get_object_vars (which will only return public variables!)
+ * @param $data object The object to serialise.
+ * @param $n int Level, only used for recursion.
+ * @return string The serialised XML output.
+ */
+ function serialise_object_to_xml($data, $name = "", $n = 0)
+ {
+ $classname = ($name=="" ? get_class($data) : $name);
+
+ $vars = method_exists($data, "export") ? get_object_vars($data->export()) : get_object_vars($data);
+
+ $output = "";
+
+ if (($n==0) || ( is_object($data) && !($data instanceof stdClass))) $output = "<$classname>";
+
+ foreach ($vars as $key => $value)
+ {
+ $output .= "<$key type=\"".gettype($value)."\">";
+
+ if (is_object($value))
+ $output .= serialise_object_to_xml($value, $key, $n+1);
+ else if (is_array($value))
+ $output .= serialise_array_to_xml($value, $n+1);
+ else
+ $output .= htmlentities($value);
+
+ $output .= "</$key>\n";
+ }
+
+ if (($n==0) || ( is_object($data) && !($data instanceof stdClass))) $output .= "</$classname>\n";
+
+ return $output;
+ }
+
+ /**
+ * Serialise an array.
+ *
+ * @param array $data
+ * @param int $n Used for recursion
+ * @return string
+ */
+ function serialise_array_to_xml(array $data, $n = 0)
+ {
+ $output = "";
+
+ if ($n==0) $output = "<array>\n";
+
+ foreach ($data as $key => $value)
+ {
+ $item = "array_item";
+
+ if (is_numeric($key))
+ $output .= "<$item name=\"$key\" type=\"".gettype($value)."\">";
+ else
+ {
+ $item = $key;
+ $output .= "<$item type=\"".gettype($value)."\">";
+ }
+
+ if (is_object($value))
+ $output .= serialise_object_to_xml($value, "", $n+1);
+ else if (is_array($value))
+ $output .= serialise_array_to_xml($value, $n+1);
+ else
+ $output .= htmlentities($value);
+
+ $output .= "</$item>\n";
+ }
+
+ if ($n==0) $output = "</array>\n";
+
+ return $output;
+ }
+
+ class ExportException extends Exception {}
+ class ImportException extends Exception {}
?> \ No newline at end of file