From 6441a960edc875a0c33fa8bbd735fd1faebaa552 Mon Sep 17 00:00:00 2001 From: icewing Date: Fri, 28 Mar 2008 19:18:55 +0000 Subject: Marcus Povey * Metadata and Annotations now exported. git-svn-id: https://code.elgg.org/elgg/trunk@291 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/annotations.php | 49 +++++++++++++++++++++++++++++++++++++++++- engine/lib/api.php | 2 +- engine/lib/entities.php | 2 +- engine/lib/export.php | 11 ++++++++++ engine/lib/metadata.php | 53 +++++++++++++++++++++++++++++++++++++++++++++- index.php | 16 ++++++++------ 6 files changed, 122 insertions(+), 11 deletions(-) diff --git a/engine/lib/annotations.php b/engine/lib/annotations.php index e1570ac0d..cceb8fd05 100644 --- a/engine/lib/annotations.php +++ b/engine/lib/annotations.php @@ -16,7 +16,7 @@ * An annotation is similar to metadata each entity can contain more than one of each annotation. * @author Marcus Povey */ - class ElggAnnotation + class ElggAnnotation implements Exportable { /** * This contains the site's main properties (id, etc) @@ -105,6 +105,12 @@ return delete_annotation($this->id); } + public function export() + { + $tmp = new stdClass; + $tmp->attributes = $this->attributes; + return $tmp; + } } /** @@ -442,4 +448,45 @@ return delete_data(); } + /** + * Handler called by trigger_plugin_hook on the "export" event. + */ + function export_annotations_plugin_hook($hook, $entity_type, $returnvalue, $params) + { + global $CONFIG; + + // 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']; + $access = get_access_list(); + + $where = array(); + + if ($guid != 0) + $where[] = "a.entity_guid=$guid"; + + $query = "SELECT a.*, n.string as name, v.string as value from {$CONFIG->dbprefix}annotations a JOIN {$CONFIG->dbprefix}entities e on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings v on a.value_id=v.id JOIN {$CONFIG->dbprefix}metastrings n on a.name_id = n.id where "; + foreach ($where as $w) + $query .= " $w and "; + $query .= " (a.access_id in {$access} or (a.access_id = 0 and a.owner_guid = {$_SESSION['id']}))"; // Add access controls + $query .= " order by a.time_created"; // Add order and limit + error_log($query); + $annotations = get_data($query, "row_to_elggannotation"); + + if ($annotations) + { + foreach ($annotations as $a) + $returnvalue[] = $a; + } + + return $returnvalue; + } + + /** Register the hook, ensuring entities are serialised first */ + register_plugin_hook("export", "all", "export_annotations_plugin_hook", 2); ?> \ No newline at end of file diff --git a/engine/lib/api.php b/engine/lib/api.php index e7d6cc4a1..95bbede01 100644 --- a/engine/lib/api.php +++ b/engine/lib/api.php @@ -17,7 +17,7 @@ * @class GenericResult Result superclass. * @author Marcus Povey */ - abstract class GenericResult + abstract class GenericResult implements Exportable { /** * The status of the result. diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 8044b1df9..017e86a74 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -16,7 +16,7 @@ * This class holds methods for accessing the main entities table. * @author Marcus Povey */ - abstract class ElggEntity + abstract class ElggEntity implements Exportable { /** * The main attributes of an entity. diff --git a/engine/lib/export.php b/engine/lib/export.php index 89b5a1ef5..5937a71bc 100644 --- a/engine/lib/export.php +++ b/engine/lib/export.php @@ -10,6 +10,17 @@ * @link http://elgg.org/ */ + /** + * Define an interface for all exportable objects. + */ + interface Exportable + { + /** + * This must take the contents of the object and return it as a stdClass. + */ + public function export(); + } + /** * Export a GUID. * diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php index 4744f6f6f..2c738ebda 100644 --- a/engine/lib/metadata.php +++ b/engine/lib/metadata.php @@ -16,7 +16,7 @@ * This class describes metadata that can be attached to ElggEntities. * @author Marcus Povey */ - class ElggMetadata + class ElggMetadata implements Exportable { /** * This contains the site's main properties (id, etc) @@ -106,6 +106,13 @@ { return delete_metadata($this->id); } + + public function export() + { + $tmp = new stdClass; + $tmp->attributes = $this->attributes; + return $tmp; + } } @@ -299,6 +306,21 @@ return get_data("SELECT m.*, n.string as name, v.string as value from {$CONFIG->dbprefix}metadata m JOIN {$CONFIG->dbprefix}metastrings v on m.value_id = v.id JOIN {$CONFIG->dbprefix}metastrings n on m.name_id = n.id where m.entity_guid=$entity_guid and m.name_id='$meta_name' and (m.access_id in {$access} or (m.access_id = 0 and m.owner_guid = {$_SESSION['id']}))", "row_to_elggmetadata"); } + + /** + * Return all the metadata for a given GUID. + * + * @param int $entity_guid + */ + function get_metadata_for_entity($entity_guid) + { + global $CONFIG; + + $entity_guid = (int)$entity_guid; + $access = get_access_list(); + + return get_data("SELECT m.*, n.string as name, v.string as value from {$CONFIG->dbprefix}metadata m JOIN {$CONFIG->dbprefix}metastrings v on m.value_id = v.id JOIN {$CONFIG->dbprefix}metastrings n on m.name_id = n.id where m.entity_guid=$entity_guid and (m.access_id in {$access} or (m.access_id = 0 and m.owner_guid = {$_SESSION['id']}))", "row_to_elggmetadata"); + } /** * Return a list of entities based on the given search criteria. @@ -360,4 +382,33 @@ return delete_data("DELETE from {$CONFIG->dbprefix}metadata where entity_guid=$entity_guid and access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']})"); } + /** + * Handler called by trigger_plugin_hook on the "export" event. + */ + function export_metadata_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 metadata for the entity + $metadata = get_metadata_for_entity($guid); + + if ($metadata) + { + foreach ($metadata as $m) + $returnvalue[] = $m; + } + + return $returnvalue; + } + + /** Register the hook, ensuring entities are serialised first */ + register_plugin_hook("export", "all", "export_metadata_plugin_hook", 2); + ?> \ No newline at end of file diff --git a/index.php b/index.php index 2c7d2810f..bf55b4c43 100644 --- a/index.php +++ b/index.php @@ -19,7 +19,7 @@ // Testing /////// - if ($_SESSION['id']==-1) $_SESSION['id'] = 1; + if ($_SESSION['id']==-1) $_SESSION['id'] = 2; @@ -43,7 +43,7 @@ // Testing /////// - if ($_SESSION['id']==-1) $_SESSION['id'] = 1; + if ($_SESSION['id']==-1) $_SESSION['id'] = 2; // Create / load a site /* $site = get_site(1); @@ -96,7 +96,7 @@ error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true)); // get site metadata error_log("SITE Metadata : " . print_r($site->getMetadata("Metaname"), true)); */ -/* +$CONFIG->debug = true; // get site annotations $site = get_site_by_url("http://localhost/"); error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true)); @@ -108,7 +108,7 @@ error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true)); $site->name = "Test title"; $site->description = "Test description"; $site->url = "http://localhost/"; - $site->owner_guid = 1; + $site->owner_guid = 2; $site->access_guid = 0; error_log("LOADED NEW SITE: Saving" . print_r($site,true)); @@ -127,8 +127,10 @@ error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true)); // add meta data $site->setMetadata("Metaname", "Value"); + + print_r(get_metadata(9)); - +/* // change site metadata $site->setMetadata("Metaname", "Value2"); @@ -144,8 +146,8 @@ error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true)); // get site annotations error_log("SITE Annotations : " . print_r($site->getAnnotations("Test"), true)); - */ - + + */ -- cgit v1.2.3