From 2107f3f3de4898e64fa16538186031c5fda4d6d7 Mon Sep 17 00:00:00 2001 From: icewing Date: Fri, 28 Mar 2008 18:21:59 +0000 Subject: Marcus Povey * Export functionality for ElggEntity and children git-svn-id: https://code.elgg.org/elgg/trunk@287 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/export.php | 119 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) (limited to 'engine/lib/export.php') 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 .= "\n"; + } + + if (($n==0) || ( is_object($data) && !($data instanceof stdClass))) $output .= "\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 = "\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 .= "\n"; + } + + if ($n==0) $output = "\n"; + + return $output; + } + + class ExportException extends Exception {} + class ImportException extends Exception {} ?> \ No newline at end of file -- cgit v1.2.3