aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoricewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-03-14 13:52:13 +0000
committericewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-03-14 13:52:13 +0000
commit641dc210aaebb8db131db9100397fc4c386963da (patch)
tree1a2f3abded3a9d895c2f92563cf0a4346d4b213f
parent31feeca2e8bfe62bc47f11e24bbaea213936d0c5 (diff)
downloadelgg-641dc210aaebb8db131db9100397fc4c386963da.tar.gz
elgg-641dc210aaebb8db131db9100397fc4c386963da.tar.bz2
Marcus Povey <marcus@dushka.co.uk>
* Metadata code git-svn-id: https://code.elgg.org/elgg/trunk@220 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--engine/lib/entities.php9
-rw-r--r--engine/lib/metadata.php244
-rw-r--r--index.php29
3 files changed, 126 insertions, 156 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php
index 2a90ea3e1..f2eed1658 100644
--- a/engine/lib/entities.php
+++ b/engine/lib/entities.php
@@ -112,7 +112,7 @@
*/
public function getMetaData($name)
{
- //TODO: Writeme
+ return get_metadata_byname($name);
}
/**
@@ -120,16 +120,17 @@
*
* @param string $name
* @param string $value
+ * @param string $value_type
* @return bool
*/
- public function setMetaData($name, $value)
+ public function setMetaData($name, $value, $value_type = "")
{
- // TODO: WRITEME
+ return create_metadata($this->getGUID(), $name, $value, $value_type, $this->getOwner());
}
public function clearMetaData()
{
- // TODO: WRITEME
+ return clear_metadata($this->getGUID());
}
/**
diff --git a/engine/lib/metadata.php b/engine/lib/metadata.php
index 202ef60ad..e91842302 100644
--- a/engine/lib/metadata.php
+++ b/engine/lib/metadata.php
@@ -13,6 +13,7 @@
/**
* @class ElggMetadata
+ * This class describes metadata that can be attached to ElggEntities.
* @author Marcus Povey <marcus@dushka.co.uk>
*/
class ElggMetadata
@@ -80,15 +81,18 @@
*
* @return mixed
*/
- function getOwner() { return get_user($this->owner_id); }
+ function getOwner()
+ {
+ return get_user($this->owner_guid);
+ }
function save()
{
if ($this->id > 0)
- return update_metadata($this->id, $this->name, $this->value, $this->value_type, $this->owner_id, $this->access_id);
+ return update_metadata($this->id, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
else
{
- $this->id = create_metadata($this->entity_id, $this->entity_type, $this->name, $this->value, $this->value_type, $this->owner_id, $this->access_id);
+ $this->id = create_metadata($this->entity_guid, $this->name, $this->value, $this->value_type, $this->owner_guid, $this->access_id);
if (!$this->id) throw new IOException("Unable to save new ElggAnnotation");
return $this->id;
}
@@ -98,10 +102,13 @@
/**
* Delete a given site.
*/
- function delete() { return delete_metadata($this->id); }
-
+ function delete()
+ {
+ return delete_metadata($this->id);
+ }
}
+
/**
* Convert a database row to a new ElggMetadata
*
@@ -136,41 +143,54 @@
return 'tag';
}
-
+
+ /**
+ * Get a specific item of metadata.
+ *
+ * @param $id int The item of metadata being retrieved.
+ */
+ function get_metadata($id)
+ {
+ global $CONFIG;
+
+ $id = (int)$id;
+ $access = get_access_list();
+
+ return row_to_elggmetadata(get_data_row("SELECT * from {$CONFIG->dbprefix}metadata where id=$id and (access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']}))"));
+ }
+
/**
* Create a new metadata object, or update an existing one.
*
- * @param int $entity_id
- * @param string $entity_type
+ * @param int $entity_guid
* @param string $name
* @param string $value
* @param string $value_type
- * @param int $owner_id
+ * @param int $owner_guid
* @param int $access_id
*/
- function create_metadata($entity_id, $entity_type, $name, $value, $value_type, $owner_id, $access_id = 0)
+ function create_metadata($entity_guid, $name, $value, $value_type, $owner_guid, $access_id = 0)
{
global $CONFIG;
- $entity_id = (int)$entity_id;
- $entity_type = sanitise_string(trim($entity_type));
+ $entity_guid = (int)$entity_guid;
$name = sanitise_string(trim($name));
$value = sanitise_string(trim($value));
$value_type = detect_metadata_valuetype(sanitise_string(trim($value_type)));
$time = time();
- $owner_id = (int)$owner_id;
- if ($owner_id==0) $owner_id = $_SESSION['id'];
+ $owner_guid = (int)$owner_guid;
+ if ($owner_guid==0) $owner_guid = $_SESSION['id'];
$access_id = (int)$access_id;
$id = false;
- $existing = get_data_row("SELECT * from {$CONFIG->dbprefix}metadata WHERE entity_id = $entity_id and entity_type='$entity_type' and name='$name' limit 1");
+ $existing = get_data_row("SELECT * from {$CONFIG->dbprefix}metadata WHERE entity_guid = $entity_guid and name='$name' limit 1");
if ($existing)
{
$id = $existing->id;
- $result = update_metadata($id,$name, $value, $value_type, $owner_id, $access_id);
+ $result = update_metadata($id, $name, $value, $value_type, $owner_guid, $access_id);
if (!$result) return false;
}
@@ -181,7 +201,7 @@
if (!$value) return false;
// If ok then add it
- $id = insert_data("INSERT into {$CONFIG->dbprefix}metadata (entity_id, entity_type, name, value, value_type, owner_id, created, access_id) VALUES ($entity_id,'$entity_type','$name','$value','$value_type', $owner_id, $time, $access_id)");
+ $id = insert_data("INSERT into {$CONFIG->dbprefix}metadata (entity_guid, name, value, value_type, owner_guid, time_created, access_id) VALUES ($entity_guid, '$name','$value','$value_type', $owner_guid, $time, $access_id)");
}
return $id;
@@ -194,10 +214,10 @@
* @param string $name
* @param string $value
* @param string $value_type
- * @param int $owner_id
+ * @param int $owner_guid
* @param int $access_id
*/
- function update_metadata($id, $name, $value, $value_type, $owner_id, $access_id)
+ function update_metadata($id, $name, $value, $value_type, $owner_guid, $access_id)
{
global $CONFIG;
@@ -206,8 +226,8 @@
$value = sanitise_string(trim($value));
$value_type = detect_metadata_valuetype(sanitise_string(trim($value_type)));
- $owner_id = (int)$owner_id;
- if ($owner_id==0) $owner_id = $_SESSION['id'];
+ $owner_guid = (int)$owner_guid;
+ if ($owner_guid==0) $owner_guid = $_SESSION['id'];
$access_id = (int)$access_id;
@@ -219,165 +239,99 @@
if (!$value) return false;
// If ok then add it
- return update_data("UPDATE {$CONFIG->dbprefix}metadata set value='$value', value_type='$value_type', access_id=$access_id, owner_id=$owner_id where id=$id and name='$name' and (access_id in {$access} or (access_id = 0 and owner_id = {$_SESSION['id']}))");
+ return update_data("UPDATE {$CONFIG->dbprefix}metadata set value='$value', value_type='$value_type', access_id=$access_id, owner_guid=$owner_guid where id=$id and name='$name' and (access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']}))");
}
-
+
+
/**
- * Get a specific item of metadata.
+ * Delete an item of metadata, where the current user has access.
*
- * @param $id int The item of metadata being retrieved.
+ * @param $id int The item of metadata to delete.
*/
- function get_metadata($id)
+ function delete_metadata($id)
{
global $CONFIG;
$id = (int)$id;
$access = get_access_list();
- return row_to_elggmetadata(get_data_row("SELECT * from {$CONFIG->dbprefix}metadata where id=$id and (access_id in {$access} or (access_id = 0 and owner_id = {$_SESSION['id']}))"));
+ return delete_data("DELETE from {$CONFIG->dbprefix}metadata where id=$id and (access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']}))");
+
}
-
+
/**
- * Get a list of metadatas for a given object/user/metadata type.
- *
- * @param int $entity_id
- * @param string $entity_type
- * @param string $entity_subtype
- * @param mixed $name Either a string or an array of terms.
- * @param mixed $value Either a string or an array of terms.
- * @param int $owner_id
- * @param string $order_by
- * @param int $limit
- * @param int $offset
- * @return array of ElggMetadata
+ * Return the metadata values that match your query.
+ *
+ * @param string $meta_name
*/
- function get_metadatas($entity_id = 0, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_id = 0, $order_by = "created desc", $limit = 10, $offset = 0)
+ function get_metadata_byname($meta_name)
{
global $CONFIG;
- $entity_id = (int)$entity_id;
- $entity_type = sanitise_string(trim($entity_type));
- $entity_subtype = sanitise_string($entity_subtype);
-
- if (!is_array($name))
- $name = sanitise_string($name);
- if (!is_array($value))
- $value = get_metastring_id($value);
-
- if ((is_array($name)) && (is_array($value)) && (count($name)!=count($value)))
- throw new InvalidParameterException("Name and value arrays not equal.");
-
- $value_type = sanitise_string(trim($value_type));
- $owner_id = (int)$owner_id;
- $order_by = sanitise_string($order_by);
-
- $limit = (int)$limit;
- $offset = (int)$offset;
-
- $join = "";
-
- // Construct query
- $where = array();
-
- if ($entity_id != 0)
- $where[] = "o.entity_id=$entity_id";
-
- if ($entity_type != "")
- $where[] = "o.entity_type='$entity_type'";
-
- if ($owner_id != 0)
- $where[] = "o.owner_id=$owner_id";
-
- if (is_array($name)) {
- foreach ($name as $n)
- $where[]= "o.name='$n'";
- } else if ($name != "")
- $where[] = "o.name='$name'";
-
- if (is_array($value)) {
- foreach ($value as $v)
- $where[]= "o.value='$v'";
- } else if ($value != "")
- $where[] = "o.value='$value'";
-
- if ($value_type != "")
- $where[] = "o.value_type='$value_type'";
-
- if ($entity_subtype != "")
- $where[] = "s.id=" . get_entity_subtype($entity_id, $entity_type);
-
- // add access controls
+ $meta_name = sanitise_string($meta_name);
$access = get_access_list();
- $where[] = "(o.access_id in {$access} or (o.access_id = 0 and o.owner_id = {$_SESSION['id']}))";
-
- if ($entity_subtype!="")
- $where[] = "";
-
- // construct query.
- $query = "SELECT o.* from {$CONFIG->dbprefix}metadata o LEFT JOIN {$CONFIG->dbprefix}entity_subtypes s on o.entity_id=s.entity_id and o.entity_type=s.entity_type where ";
- for ($n = 0; $n < count($where); $n++)
- {
- if ($n > 0) $query .= " and ";
- $query .= $where[$n];
- }
- $query .= " order by $order_by limit $offset,$limit";
- return get_data($query, "row_to_elggmetadata");
+ return row_to_elggmetadata(get_data_row("SELECT * from {$CONFIG->dbprefix}metadata where name='$meta_name' and (access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']}))"));
}
/**
- * Similar to get_metadatas, but instead returns the objects associated with a given meta search.
+ * Return a list of entities based on the given search criteria.
*
- * @param int $entity_id
- * @param string $entity_type
- * @param string $entity_subtype
- * @param mixed $name Either a string or an array of terms.
- * @param mixed $value Either a string or an array of terms.
- * @param int $owner_id
- * @param string $order_by
- * @param int $limit
+ * @param mixed $meta_name
+ * @param mixed $meta_value
+ * @param string $entity_type The type of entity to look for, eg 'site' or 'object'
+ * @param string $entity_subtype The subtype of the entity.
+ * @param int $limit
* @param int $offset
- * @return mixed Array of objects or false.
+ * @param string $order_by Optional ordering.
*/
- function get_objects_from_metadatas($entity_id = 0, $entity_type = "", $entity_subtype = "", $name = "", $value = "", $value_type = "", $owner_id = 0, $order_by = "created desc", $limit = 10, $offset = 0)
+ function get_entities_from_metadata($meta_name, $meta_value = "", $entity_type = "", $entity_subtype = "", $limit = 10, $offset = 0, $order_by = "e.time_created desc")
{
- $results = get_metadatas($entity_id, $entity_type, $entity_subtype, $name, $value, $value_type, $owner_id, $order_by, $limit, $offset);
- $objects = false;
+ global $CONFIG;
- if ($results)
- {
- $objects = array();
+ $meta_name = sanitise_string($meta_name);
+ $meta_value = get_metastring_id($meta_value);
- foreach ($results as $r)
- {
-
- switch ($r->entity_type)
- {
- case 'object' : $objects[] = new ElggObject((int)$r->entity_id); break;
- case 'user' : $objects[] = new ElggUser((int)$r->entity_id); break;
- case 'collection' : $objects[] = new ElggCollection((int)$r->entity_id); break;
- case 'site' : $objects[] = new ElggSite((int)$r->entity_id); break;
- default: default : throw new InstallationException("Type {$r->entity_type} is not supported. This indicates an error in your installation, most likely caused by an incomplete upgrade.");
- }
- }
- }
+ $entity_type = sanitise_string($entity_type);
+ $entity_subtype = get_subtype_id($entity_subtype);
+ $limit = (int)$limit;
+ $offset = (int)$offset;
+ $order_by = sanitise_string($order_by);
+
+ $access = get_access_list();
+
+ $where = array();
+
+ if ($entity_type!="")
+ $where[] = "e.type='$entity_type'";
+ if ($entity_subtype)
+ $where[] = "e.subtype=$entity_subtype";
+ if ($meta_name!="")
+ $where[] = "m.name='$meta_name'";
+ if ($meta_value!="")
+ $where[] = "m.value='$meta_value'";
- return $objects;
+ $query = "SELECT * from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}metadata m on e.guid = m.entity_guid where";
+ foreach ($where as $w)
+ $query .= " $w and ";
+ $query .= " (e.access_id in {$access} or (e.access_id = 0 and e.owner_guid = {$_SESSION['id']}))"; // Add access controls
+ $query .= " order by $order_by limit $offset,$limit"; // Add order and limit
+
+ return get_data($query, "entity_row_to_elggstar");
}
/**
- * Delete an item of metadata, where the current user has access.
+ * Clear all the metadata for a given entity, assuming you have access to that metadata.
*
- * @param $id int The item of metadata to delete.
+ * @param int $guid
*/
- function delete_metadata($id)
+ function clear_metadata($guid)
{
global $CONFIG;
-
- $id = (int)$id;
- $access = get_access_list();
-
- return delete_data("DELETE from {$CONFIG->dbprefix}metadata where id=$id and (access_id in {$access} or (access_id = 0 and owner_id = {$_SESSION['id']}))");
+ $guid = (int)$guid;
+
+ return delete_data("DELETE from {$CONFIG->dbprefix}metadata where access_id in {$access} or (access_id = 0 and owner_guid = {$_SESSION['id']})");
}
+
?> \ No newline at end of file
diff --git a/index.php b/index.php
index 0bfdade03..a96f35df3 100644
--- a/index.php
+++ b/index.php
@@ -91,18 +91,18 @@ error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true));
*/
// get site annotations
-$site = get_site(1);
+$site = get_site_by_url("http://localhost/");
error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true));
if (empty($site))
{
$site = new ElggSite();
- $site->title = "Test title";
+ $site->name = "Test title";
$site->description = "Test description";
- $site->url = "http://dushka/~icewing/Workingdirectory/elggnew/";
- $site->owner_id = 1;
- $site->access_id = 0;
+ $site->url = "http://localhost/";
+ $site->owner_guid = 1;
+ $site->access_guid = 0;
error_log("LOADED NEW SITE: Saving" . print_r($site,true));
@@ -112,12 +112,27 @@ error_log("GETTIGN SITE ".$_SESSION['id']. " " . print_r($site, true));
{
$site = new ElggSite($site);
- $site->title = "Test title " . time();
+ $site->name = "Test title " . time();
$site->save();
error_log("UPDATING SITE " . print_r($site, true));
}
- error_log("SITE Annotations : " . print_r($site->getAnnotations("Test"), true));
+ // add meta data
+ $site->setMetadata("Metaname", "Value");
+
+
+
+ // change site metadata
+ $site->setMetadata("Metaname", "Value2");
+
+ error_log("SITE Metadata : " . print_r(get_entities_from_metadata("Metaname","","site"), true));
+
+
+ // Get objects belonging to a site
+
+ // get objects of type
+
+
?> \ No newline at end of file