Skip to content

Instantly share code, notes, and snippets.

@bartjkdp
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartjkdp/9519074c52de0ef64bd4 to your computer and use it in GitHub Desktop.
Save bartjkdp/9519074c52de0ef64bd4 to your computer and use it in GitHub Desktop.
ELGG Entities to SQL
<?php
require_once(dirname(__FILE__) . "/engine/start.php");
$guids = array(28386102,28380552,28382572,28383902,29136752,29124682,29139712,29139502,29138962);
$fetch_guids = array();
function getChildrenEntities($guid) {
global $fetch_guids;
if (!in_array($guid, $fetch_guids)) {
$fetch_guids[] = $guid;
}
$entities = get_data("SELECT * FROM `elgg_entities` WHERE `container_guid` = " . (int) $guid);
foreach ($entities as $entity) {
if (!in_array($entity->guid, $fetch_guids)) {
$fetch_guids[] = $entity->guid;
}
getChildrenEntities($entity->guid);
}
}
function parseSQL($table, $entity, $ignore = false) {
$vars = get_object_vars($entity);
if ($ignore == true) {
$query = "INSERT IGNORE INTO " . $table . " (";
} else {
$query = "INSERT INTO " . $table . " (";
}
$last_value = end(array_keys($vars));
foreach ($vars as $key => $value) {
$query .= $key;
if ($key != $last_value) {
$query .= ",";
}
}
$query .= ") VALUES (";
$last_value = end(array_keys($vars));
foreach ($vars as $key => $value) {
$query .= "\"" . mysql_real_escape_string($value) . "\"";
if ($key != $last_value) {
$query .= ",";
}
}
$query .= ");";
return $query . PHP_EOL;
}
foreach ($guids as $guid) {
getChildrenEntities($guid);
}
$metastring_guids = array();
foreach ($fetch_guids as $guid) {
// elgg_annotations
$rows = get_data("SELECT * FROM `elgg_annotations` WHERE `entity_guid` = " . (int) $guid);
foreach ($rows as $row) {
$queries .= parseSQL('elgg_annotations', $row);
if (!in_array($row->name_id, $metastring_guids)) {
$metastring_guids[] = $row->name_id;
}
if (!in_array($row->value_id, $metastring_guids)) {
$metastring_guids[] = $row->value_id;
}
}
// elgg_entities
$row = get_data_row("SELECT * FROM `elgg_entities` WHERE `guid` = " . (int) $guid);
$queries .= parseSQL('elgg_entities', $row);
$entity = $row;
// elgg_entity_relationships
$rows = get_data("SELECT * FROM `elgg_entity_relationships` WHERE `guid_one` = " . (int) $guid . " OR `guid_two` = " . (int) $guid);
foreach ($rows as $row) {
$queries .= parseSQL('elgg_entity_relationships', $row, true);
}
// elgg_{type}s_entity
$row = get_data_row("SELECT * FROM `elgg_" . $entity->type . "s_entity` WHERE `guid` = " . (int) $guid);
$queries .= parseSQL('elgg_' . $entity->type . 's_entity', $row);
// elgg_metadata
$rows = get_data("SELECT * FROM `elgg_metadata` WHERE `entity_guid` = " . (int) $guid);
foreach ($rows as $row) {
$queries .= parseSQL('elgg_metadata', $row);
if (!in_array($row->name_id, $metastring_guids)) {
$metastring_guids[] = $row->name_id;
}
if (!in_array($row->value_id, $metastring_guids)) {
$metastring_guids[] = $row->value_id;
}
}
// elgg_private_settings
$rows = get_data("SELECT * FROM `elgg_private_settings` WHERE `entity_guid` = " . (int) $guid);
foreach ($rows as $row) {
$queries .= parseSQL('elgg_private_settings', $row);
}
// elgg_river
$rows = get_data("SELECT * FROM `elgg_river` WHERE `subject_guid` = " . (int) $guid . " OR `object_guid` = " . (int) $guid);
foreach ($rows as $row) {
$queries .= parseSQL('elgg_river', $row);
}
}
foreach ($metastring_guids as $metastring_guid) {
// elgg_metastrings
$row = get_data_row("SELECT * FROM `elgg_metastrings` WHERE `id` = " . (int) $metastring_guid);
$queries .= parseSQL('elgg_metastrings', $row, true);
}
file_put_contents('export.sql', $queries);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment