/**
  * Run the upgrader
  * @return string HTML report
  */
 public function run()
 {
     $backwards_rels = $this->find_backwards_rels();
     $count = !empty($backwards_rels) ? count($backwards_rels) : 0;
     if (empty($count)) {
         return '<p>No backwards relationships exist in this reason instance.</p>';
     } else {
         $backwards_rel_keys = array_keys($backwards_rels);
         foreach ($backwards_rel_keys as $id) {
             delete_relationship($id);
         }
         return '<p>Deleted ' . $count . ' backwards relationships.</p>';
     }
 }
/**
 * Define an arbitrary relationship between two entities.
 * This relationship could be a friendship, a group membership or a site membership.
 *
 * This function lets you make the statement "$guid_one is a $relationship of $guid_two".
 *
 * @param int    $guid_one     First GUID
 * @param string $relationship Relationship name
 * @param int    $guid_two     Second GUID
 *
 * @return bool
 */
function add_entity_relationship($guid_one, $relationship, $guid_two)
{
    global $CONFIG;
    $guid_one = (int) $guid_one;
    $relationship = sanitise_string($relationship);
    $guid_two = (int) $guid_two;
    $time = time();
    // Check for duplicates
    if (check_entity_relationship($guid_one, $relationship, $guid_two)) {
        return false;
    }
    $result = insert_data("INSERT into {$CONFIG->dbprefix}entity_relationships\n\t\t(guid_one, relationship, guid_two, time_created)\n\t\tvalues ({$guid_one}, '{$relationship}', {$guid_two}, {$time})");
    if ($result !== false) {
        $obj = get_relationship($result);
        if (elgg_trigger_event('create', $relationship, $obj)) {
            return true;
        } else {
            delete_relationship($result);
        }
    }
    return false;
}
Ejemplo n.º 3
0
/**
 * Create a relationship between two entities. E.g. friendship, group membership, site membership.
 *
 * This function lets you make the statement "$guid_one is a $relationship of $guid_two". In the statement,
 * $guid_one is the subject of the relationship, $guid_two is the target, and $relationship is the type.
 *
 * @param int    $guid_one     GUID of the subject entity of the relationship
 * @param string $relationship Type of the relationship
 * @param int    $guid_two     GUID of the target entity of the relationship
 *
 * @return bool
 * @throws InvalidArgumentException
 */
function add_entity_relationship($guid_one, $relationship, $guid_two)
{
    global $CONFIG;
    if (strlen($relationship) > ElggRelationship::RELATIONSHIP_LIMIT) {
        $msg = "relationship name cannot be longer than " . ElggRelationship::RELATIONSHIP_LIMIT;
        throw InvalidArgumentException($msg);
    }
    $guid_one = (int) $guid_one;
    $relationship = sanitise_string($relationship);
    $guid_two = (int) $guid_two;
    $time = time();
    // Check for duplicates
    if (check_entity_relationship($guid_one, $relationship, $guid_two)) {
        return false;
    }
    $id = insert_data("INSERT INTO {$CONFIG->dbprefix}entity_relationships\n\t\t(guid_one, relationship, guid_two, time_created)\n\t\tVALUES ({$guid_one}, '{$relationship}', {$guid_two}, {$time})");
    if ($id !== false) {
        $obj = get_relationship($id);
        // this event has been deprecated in 1.9. Use 'create', 'relationship'
        $result_old = elgg_trigger_event('create', $relationship, $obj);
        $result = elgg_trigger_event('create', 'relationship', $obj);
        if ($result && $result_old) {
            return true;
        } else {
            delete_relationship($result);
        }
    }
    return false;
}
Ejemplo n.º 4
0
 /**
  * Delete a given relationship.
  *
  * @return bool
  */
 public function delete()
 {
     return delete_relationship($this->id);
 }
Ejemplo n.º 5
0
 function delete_orphan_page_parent($orphaned_page_id)
 {
     $d = new DBSelector();
     $d->add_table('relationship');
     $d->add_field('relationship', 'entity_a');
     $d->add_field('relationship', 'entity_b');
     $d->add_field('relationship', 'id');
     $d->add_relation('relationship.entity_a =' . $orphaned_page_id);
     $d->add_relation('relationship.entity_b =' . $orphaned_page_id);
     $results = current($d->run());
     $id = $results['id'];
     delete_relationship($id);
 }
Ejemplo n.º 6
0
 function do_course_toggle($id)
 {
     $this->build_course_list();
     if (isset($this->courses[$id])) {
         if ($this->courses[$id]->include_source == 'page_courses') {
             $course = new entity($id);
             $rels = $course->get_left_relationships_info();
             foreach ($rels['course_template_to_page'] as $rel) {
                 if ($rel['entity_b'] == $this->get_source_page_id()) {
                     delete_relationship($rel['id']);
                     return true;
                     break;
                 }
             }
         } else {
             if ($this->courses[$id]->include_source == 'categories') {
                 $course = new entity($id);
                 $rels = $course->get_left_relationships_info();
                 $cats = $this->get_page_categories();
                 foreach ($rels['course_template_to_category'] as $rel) {
                     if (isset($cats[$rel['entity_b']])) {
                         delete_relationship($rel['id']);
                         return true;
                         break;
                     }
                 }
             }
         }
     } else {
         if ($this->params['get_page_courses']) {
             return create_relationship($id, $this->get_source_page_id(), relationship_id_of('course_template_to_page'), false, false);
         } else {
             if ($this->params['get_courses_by_page_categories']) {
                 if ($cats = $this->get_page_categories()) {
                     // For now we're just attaching the first page category we find to the course;
                     // a better, future interface would allow the user to pick the category.
                     $cat_id = key($cats);
                     return create_relationship($id, $cat_id, relationship_id_of('course_template_to_category'), false, false);
                 }
             }
         }
     }
     return false;
 }
Ejemplo n.º 7
0
 /**
  * Create a relationship between two entities. E.g. friendship, group membership, site membership.
  *
  * This function lets you make the statement "$guid_one is a $relationship of $guid_two". In the statement,
  * $guid_one is the subject of the relationship, $guid_two is the target, and $relationship is the type.
  *
  * @param int    $guid_one     GUID of the subject entity of the relationship
  * @param string $relationship Type of the relationship
  * @param int    $guid_two     GUID of the target entity of the relationship
  *
  * @return bool
  * @throws InvalidArgumentException
  */
 function add($guid_one, $relationship, $guid_two)
 {
     if (strlen($relationship) > \ElggRelationship::RELATIONSHIP_LIMIT) {
         $msg = "relationship name cannot be longer than " . \ElggRelationship::RELATIONSHIP_LIMIT;
         throw InvalidArgumentException($msg);
     }
     $guid_one = (int) $guid_one;
     $relationship = sanitise_string($relationship);
     $guid_two = (int) $guid_two;
     $time = time();
     // Check for duplicates
     if (check_entity_relationship($guid_one, $relationship, $guid_two)) {
         return false;
     }
     $id = _elgg_services()->db->insertData("INSERT INTO {$this->CONFIG->dbprefix}entity_relationships\n\t\t\t(guid_one, relationship, guid_two, time_created)\n\t\t\tVALUES ({$guid_one}, '{$relationship}', {$guid_two}, {$time})");
     if ($id !== false) {
         $obj = get_relationship($id);
         $result = _elgg_services()->events->trigger('create', 'relationship', $obj);
         if ($result) {
             return true;
         } else {
             delete_relationship($result);
         }
     }
     return false;
 }
Ejemplo n.º 8
0
    }
    // update the code to add the audience stuff as disco elements in the content managers
    // update the code to pay attention to the rels on the front end rather than to the fields
} elseif (!empty($_REQUEST['step_2'])) {
    $out = array();
    // remove the audience table from those types
    $q = 'SELECT * FROM `relationship` WHERE `entity_b` = ' . get_id_of_audience_table() . ' AND `type` = ' . relationship_id_of('type_to_table');
    $r = db_query($q);
    $rels = array();
    while ($row = mysql_fetch_array($r)) {
        $rels[$row['id']] = $row;
    }
    $deleted_rels = false;
    foreach ($rels as $id => $rel) {
        $type = new entity($rel['entity_a']);
        delete_relationship($id);
        $out[] = 'Removed audience table from ' . $type->get_value('name');
        $deleted_rels = true;
    }
    if (!$deleted_rels) {
        $out[] = 'Audience table is not in any types';
    }
    pray($out);
}
function get_types_with_audience_table()
{
    $es = new entity_selector();
    $es->add_type(id_of('type'));
    $es->add_left_relationship(get_id_of_audience_table(), relationship_id_of('type_to_entity_table'));
    return $es->run_one();
}
 /**
  * We always return true - we check if the entity id is a root node (and then if the destination has a root node) and delete the page parent rel if so.
  */
 function run_job()
 {
     $d = new DBselector();
     $d->add_table('r', 'relationship');
     $d->add_field('r', 'entity_b', 'parent_id');
     $d->add_field('r', 'id', 'rel_id');
     $d->add_relation('r.type = ' . relationship_id_of('minisite_page_parent'));
     $d->add_relation('r.entity_a = ' . $this->config('page_id'));
     $d->set_num(1);
     $result = db_query($d->get_query(), 'Error getting parent ID.');
     if ($row = mysql_fetch_assoc($result)) {
         if ($row['parent_id'] == $this->config('page_id')) {
             if ($this->new_site_has_root_node()) {
                 delete_relationship($row['rel_id']);
                 $this->set_report("Page ID " . $this->config('page_id') . " is a root node - deleted rel " . $row['rel_id'] . '.');
             } else {
                 $this->set_report("Page ID " . $this->config('page_id') . " remains a root node - not deleting because destination site has no root node.");
             }
         }
     }
     return true;
 }
 /**
  * handles the extended garbage collection
  *
  * @param string $hook        hookname
  * @param string $type        hooktype
  * @param mixed  $returnvalue current return value
  * @param mixed  $params      original parameters
  *
  * @return void
  */
 public static function collect($hook, $type, $returnvalue, $params)
 {
     if (elgg_get_plugin_setting('enable_gc', 'garbagecollector_extended') !== 'yes') {
         return;
     }
     elgg_register_plugin_hook_handler('permissions_check', 'all', '\\Elgg\\Values::getTrue');
     $dbprefix = elgg_get_config('dbprefix');
     // overrule access settigns
     $ia = elgg_get_ignore_access();
     elgg_set_ignore_access(true);
     // cleanup entities
     if ($entity_guids = garbagecollector_extended_get_orphaned_entities()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['entities']);
         foreach ($entity_guids as $guid) {
             $entity = get_entity($guid);
             if ($entity) {
                 $entity->delete();
             }
         }
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // cleanup access collections
     if ($acl_ids = garbagecollector_extended_get_orphaned_access_collections()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['access collections']);
         foreach ($acl_ids as $id) {
             delete_access_collection($id);
         }
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // cleanup annotations
     if ($anno_ids = garbagecollector_extended_get_orphaned_annotations()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['annotations']);
         foreach ($anno_ids as $id) {
             elgg_delete_annotation_by_id($id);
         }
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // cleanup metadata
     if ($meta_ids = garbagecollector_extended_get_orphaned_metadata()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['metadata']);
         foreach ($meta_ids as $id) {
             // We need to manualy delete metadata as the Elgg functions don't work because this is orphaned metadata
             // also we need to delete this one by one because of potential long query strings
             $sql = 'DELETE FROM ' . $dbprefix . 'metadata';
             $sql .= ' WHERE id = ' . $id;
             delete_data($sql);
         }
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // cleanup private settings
     if ($private_ids = garbagecollector_extended_get_orphaned_private_settings()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['private settings']);
         foreach ($private_ids as $id) {
             // We need to manualy delete private settings as Elgg doesn't have a function fot this
             // also we need to delete this one by one because of potential long query strings
             $sql = 'DELETE FROM ' . $dbprefix . 'private_settings';
             $sql .= ' WHERE id = ' . $id;
             delete_data($sql);
         }
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // cleanup relationships
     if ($rel_ids = garbagecollector_extended_get_orphaned_relationships()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['relationships']);
         foreach ($rel_ids as $id) {
             delete_relationship($id);
         }
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // cleanup river
     if ($river_ids = garbagecollector_extended_get_orphaned_river()) {
         echo elgg_echo('garbagecollector_extended:cleanup', ['river items']);
         elgg_delete_river(['ids' => $river_ids]);
         echo elgg_echo('garbagecollector_extended:done') . '\\n';
     }
     // because we cleaned up a lot, do metastrings again
     garbagecollector_orphaned_metastrings();
     // restore access settings
     elgg_set_ignore_access($ia);
     elgg_unregister_plugin_hook_handler('permissions_check', 'all', '\\Elgg\\Values::getTrue');
 }
	<p>If one or both of the entities of a relationship have been expunged from the Reason database -- but the relationship still exists -- then the relationship is "widowed."</p>
	<p>This script deletes any widowed relationships in Reason. It should probably should be run regularly. In fact, it should probably be made into a cron job at some point.</p>
	<input type="submit" name="do_it" value="Run the script" />
	</form>
	<?php 
} else {
    connectDB(REASON_DB);
    $results = array();
    $sides = array('a', 'b');
    foreach ($sides as $side) {
        $q = 'SELECT r.id FROM relationship AS r LEFT JOIN entity AS e ON r.entity_' . $side . ' = e.id WHERE e.id IS NULL';
        $r = db_query($q, 'Unable to grab widowed relationships.');
        while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
            if (empty($results[$side])) {
                $results[$side] = array();
            }
            $results[$side][] = $row['id'];
            delete_relationship($row['id']);
        }
    }
    if (!empty($results)) {
        echo '<h2>Widowed Relationships Deleted</h2>';
        foreach ($results as $side => $ids) {
            echo '<h3>Side ' . $side . ': ' . count($ids) . ' relationships deleted</h3>';
            echo '<ul><li>' . implode('</li><li>', $ids) . '</li></ul>';
        }
    } else {
        echo '<h2>Congratulations</h2>';
        echo '<p>There are no widowed relationships in Reason</p>';
    }
}
Ejemplo n.º 12
0
<?php

// this could take a while
set_time_limit(0);
// get the plugin name
$plugin = get_input("plugin");
// only on main site
if (!subsite_manager_on_subsite()) {
    // is this a valid plugin
    if (!empty($plugin) && elgg_get_plugin_from_id($plugin)) {
        // are there any active plugins
        if ($relationships = subsite_manager_get_active_plugin_relationships($plugin)) {
            $error_count = 0;
            // remove relationships
            foreach ($relationships as $rel) {
                if (!delete_relationship($rel->getSystemLogID())) {
                    $error_count++;
                }
            }
            if ($error_count == 0) {
                system_message(elgg_echo("subsite_manager:actions:plugins:disable_all:success"));
            } else {
                register_error(elgg_echo("subsite_manager:actions:plugins:disable_all:error:some_errors"));
            }
        } else {
            register_error(elgg_echo("subsite_manager:actions:plugins:disable_all:error:no_active"));
        }
    } else {
        register_error(elgg_echo("PluginException:InvalidID"), array($plugin));
    }
} else {