export_handler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Export handler.
  4. *
  5. * @package Elgg.Core
  6. * @subpackage Export
  7. */
  8. require_once(dirname(dirname(__FILE__)) . "/start.php");
  9. // Get input values, these will be mapped via modrewrite
  10. $guid = get_input("guid"); // guid of the entity
  11. // For attributes eg http://example.com/odd/73/attr/owner_uuid/
  12. // or http://example.com/odd/73/metadata/86/
  13. $type = get_input("type"); // attr, metadata, annotation, relationship
  14. $id_or_name = get_input("idname"); // Either a number or the key name (if attribute)
  15. $body = "";
  16. $title = "";
  17. // Only export the GUID
  18. if (($guid != "") && ($type == "") && ($id_or_name == "")) {
  19. $entity = get_entity($guid);
  20. if (!$entity) {
  21. $query = "GUID:" . $guid . " could not be found, or you can not access it.";
  22. throw new \InvalidParameterException($query);
  23. }
  24. $title = "GUID:$guid";
  25. $body = elgg_view("export/entity", array("entity" => $entity, "uuid" => guid_to_uuid($guid)));
  26. // Export an individual attribute
  27. } else if (($guid != "") && ($type != "") && ($id_or_name != "")) {
  28. // Get a uuid
  29. $entity = get_entity($guid);
  30. if (!$entity) {
  31. $msg = "GUID:" . $guid . " could not be found, or you can not access it.";
  32. throw new \InvalidParameterException($msg);
  33. }
  34. $uuid = guid_to_uuid($entity->getGUID()) . "$type/$id_or_name/";
  35. switch ($type) {
  36. case 'attr' : // @todo: Do this better? - This is a bit of a hack...
  37. $v = $entity->get($id_or_name);
  38. if (!$v) {
  39. $msg = "Sorry, '" . $id_or_name . "' does not exist for guid:" . $guid;
  40. throw new \InvalidParameterException($msg);
  41. }
  42. $m = new \ElggMetadata();
  43. $m->value = $v;
  44. $m->name = $id_or_name;
  45. $m->entity_guid = $guid;
  46. $m->time_created = $entity->time_created;
  47. $m->time_updated = $entity->time_updated;
  48. $m->owner_guid = $entity->owner_guid;
  49. $m->id = $id_or_name;
  50. $m->type = "attr";
  51. break;
  52. case 'metadata' :
  53. $m = elgg_get_metadata_from_id($id_or_name);
  54. break;
  55. case 'annotation' :
  56. $m = elgg_get_annotation_from_id($id_or_name);
  57. break;
  58. case 'relationship' :
  59. $r = get_relationship($id_or_name);
  60. break;
  61. case 'volatile' :
  62. $m = elgg_trigger_plugin_hook('volatile', 'metadata', array(
  63. 'guid' => $guid,
  64. 'varname' => $id_or_name,
  65. ));
  66. break;
  67. default :
  68. $msg = "Sorry, I don't know how to export '" . $type . "'";
  69. throw new \InvalidParameterException($msg);
  70. }
  71. // Render metadata or relationship
  72. if ((!$m) && (!$r)) {
  73. throw new \InvalidParameterException("Could not find any data.");
  74. }
  75. // Exporting metadata?
  76. if ($m) {
  77. if ($m->entity_guid != $entity->guid) {
  78. throw new \InvalidParameterException("Does not belong to entity.");
  79. }
  80. $title = "$type:$id_or_name";
  81. $body = elgg_view("export/metadata", array("metadata" => $m, "uuid" => $uuid));
  82. }
  83. // Exporting relationship
  84. if ($r) {
  85. if (($r->guid_one != $entity->guid) && ($r->guid_two != $entity->guid)) {
  86. throw new \InvalidParameterException("Does not belong to entity or refer to entity.");
  87. }
  88. $title = "$type:$id_or_name";
  89. $body = elgg_view("export/relationship", array("relationship" => $r, "uuid" => $uuid));
  90. }
  91. // Something went wrong
  92. } else {
  93. throw new \InvalidParameterException("Missing parameter, you need to provide a GUID.");
  94. }
  95. $body = elgg_view_layout('one_sidebar', array(
  96. 'title' => $title,
  97. 'content' => $body
  98. ));
  99. echo elgg_view_page($title, $body);