function get_entity_relationships_info($entity, $right) { $dbq = new DBSelector(); $dbq->add_table('r', 'relationship'); $dbq->add_field('r', 'entity_a'); $dbq->add_field('r', 'entity_b'); $dbq->add_field('r', 'type'); $dbq->add_table('ar', 'allowable_relationship'); $dbq->add_field('ar', 'id'); $dbq->add_field('ar', 'type'); $dbq->add_field('ar', 'relationship_a'); $dbq->add_field('ar', 'relationship_b'); $dbq->add_field('ar', 'description'); if ($right) { $dbq->add_relation('r.entity_b = ' . $entity->id()); } else { $dbq->add_relation('r.entity_a = ' . $entity->id()); } $dbq->add_relation('ar.id = r.type'); if (reason_relationship_names_are_unique()) { $dbq->add_relation('ar.type = "association"'); } else { $dbq->add_relation('ar.name != "owns"'); $dbq->add_relation('ar.name != "borrows"'); } $rels = $dbq->run(); return $rels; }
/** * As well as removing the borrowing relationship, this removes relationships on the site where the * entity being unborrowed is on the "b" side of the relationship. If the site also owns the entity * being unborrowed (not a good state) but it can happen, we skip this relationship deletion phase. */ function delete_borrowed_relationship( $site_id , $id , $rel_id ) // {{{ //{{{ { // if we don't also own the entity, delete associations from the site where our entity is on the b side. if (!site_owns_entity( $site_id, $id )) { $dbq = new DBSelector; //actual relationships that we are selecting $dbq->add_table( 'r' , 'relationship' ); $dbq->add_table( 'ar' , 'allowable_relationship' ); $dbq->add_field( 'r' , '*' ); $dbq->add_relation( 'r.type = ar.id' ); $dbq->add_relation( 'r.entity_b = ' . $id ); //owns relationship table $dbq->add_table( 'r2' , 'relationship' ); $dbq->add_table( 'ar2' , 'allowable_relationship' ); if (!reason_relationship_names_are_unique()) { $dbq->add_relation( 'ar2.name = "owns"' ); } else { $dbq->add_relation( 'ar2.type = "owns"' ); } $dbq->add_relation( 'r2.type = ar2.id' ); $dbq->add_relation( 'r2.entity_a = ' . $site_id ); $dbq->add_relation( 'r2.entity_b = r.entity_a' ); //current borrowship $dbq->add_table( 'r3' , 'relationship' ); $dbq->add_table( 'ar3' , 'allowable_relationship' ); $dbq->add_relation( 'ar3.id = ' . $rel_id ); $dbq->add_relation( 'r3.type = ar3.id' ); $dbq->add_relation( 'r3.entity_a = ' . $site_id ); $dbq->add_relation( 'r3.entity_b = ' . $id ); $x = $dbq->run(); if( $x ) { $first = true; $in = ''; foreach( $x AS $rel ) { if (!$first) $in .= ','; else $first = false; $in .= $rel[ 'id' ]; } $q = 'DELETE FROM relationship WHERE id IN(' . $in . ')'; db_query( $q , 'Error removing associations of borrowed item before deleting' ); } } $q = 'DELETE FROM relationship WHERE entity_a = ' . $site_id . ' AND entity_b = ' . $id . ' AND type = ' . $rel_id; db_query( $q , 'Error removing borrowship' ); }
$q->add_table( $t ); if( $t != 'entity' ) $q->add_relation( 'entity.id = '.$t.'.id' ); } $q->add_relation( 'entity.id != '.$id ); reset( $fields ); while( list( ,$f ) = each( $fields ) ) { if( $d[$f] ) $q->add_relation( $f.' = "'.addslashes($d[$f]).'"' ); else $q->add_relation( $f.' IS NULL' ); } $q->add_field( 'entity','id' ); //echo $q->get_query().'<br /><br />'; $ids_to_delete = $q->run(); //pray( $ids_to_delete ); reset( $ids_to_delete ); while( list( ,$id_to_delete ) = each ( $ids_to_delete ) ) { $itd = $id_to_delete[ 'id' ]; // delete id from each table reset( $tables ); while( list( $key,$table ) = each ( $tables ) ) { $q = "DELETE FROM $table WHERE id = '$itd'"; $output .= $q.";\n"; //$r = mysql_query( $q ) OR die( 'unable to delete from table '.$table.': '.mysql_error() ); } $q = "DELETE FROM relationship WHERE entity_a = $itd OR entity_b = $itd"; $output .= $q.";\n";
/** * Grabs a list of fields associated with the current type in case we need them later. * Used in add_enum_element( $field ). * @param Viewer $viewer */ function get_db_fields($viewer) { $n = count($viewer); $i = 0; $in = '( '; foreach ($viewer as $key => $t) { $i++; $in .= '"' . $key . '"'; if ($i != $n) { $in .= ', '; } } $in .= ')'; $d = new DBSelector(); $d->add_table('entity'); $d->add_table('field'); $d->add_table('r1', 'relationship'); $d->add_table('ar1', 'allowable_relationship'); $d->add_table('r2', 'relationship'); $d->add_table('ar2', 'allowable_relationship'); $d->add_field('field', '*'); $d->add_field('entity', '*'); $d->add_relation('ar1.name = "type_to_table"'); $d->add_relation('ar2.name = "field_to_entity_table"'); $d->add_relation('r1.type = ar1.id'); $d->add_relation('r2.type = ar2.id'); $d->add_relation('entity.id = field.id'); $d->add_relation('r1.entity_a = ' . $this->page->type_id); $d->add_relation('r1.entity_b = r2.entity_b'); $d->add_relation('r2.entity_a = field.id'); if ($n > 0) { $d->add_relation('entity.name IN ' . $in); } $fields = $d->run(); $this->fields = array(); foreach ($fields as $field) { $this->fields[$field['name']] = $field; } }
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); }
function site_to_type() { if ($this->site_id && $this->type_id) { $d = new DBSelector(); $d->add_table('ar', 'allowable_relationship'); $d->add_table('r', 'relationship'); $d->add_relation('ar.id = r.type'); $d->add_relation('ar.name = "site_to_type"'); $d->add_relation('r.entity_a = ' . $this->site_id); $d->add_relation('r.entity_b = ' . $this->type_id); if ($d->run()) { return true; } else { return false; } } return true; }
protected function created_restricted_group_relationship() { $dbq = new DBSelector(); $dbq->add_table('ar', 'allowable_relationship'); $dbq->add_relation('ar.name = "av_restricted_to_group"'); $q = $dbq->run(); if (empty($q)) { return false; } else { return true; } }
function set($value) { if (!$this->can_run()) { return; } if (empty($value)) { $this->value = '<' . '?xml version="1.0" ?' . '><form submit="Submit" reset="Clear" />'; } else { $this->value = $value; } // If there's no xml declaration, assume that value contains the tmp_id if (strpos($this->value, '<' . '?xml') === false) { $this->tmp_id = $this->value; connectDB($this->thor_db_conn_name); $dbs = new DBSelector(); $dbs->add_table('thor'); $dbs->add_field('thor', 'content'); $dbs->add_relation('thor.id = ' . addslashes($this->tmp_id)); $results = $dbs->run(); if (count($results) > 0) { $this->value = $results[0]['content']; } else { $this->value = ''; } connectDB($this->original_db_conn_name); } else { connectDB($this->thor_db_conn_name); include_once CARL_UTIL_INC . 'db/sqler.php'; $sqler = new SQLER(); $sqler->insert('thor', array('content' => $this->value)); $this->tmp_id = mysql_insert_id(); connectDB($this->original_db_conn_name); } }
function grab_rels($rel_type_id) { $dbs = new DBSelector(); $dbs->add_table('rel', $table = 'relationship'); $dbs->add_relation('`type` = "' . $rel_type_id . '"'); return $dbs->run(); }
/** * Initialize the _right_relationships and _right_relationships_info arrays * @access private * @todo cache db structure info so it doesn't need to rediscover it every time */ function _init_right_relationships() { //first, get relationship types $dbq = new DBSelector(); $dbq->add_field('allow', '*'); $dbq->add_table('allow', 'allowable_relationship'); $dbq->add_table('entity', 'entity'); $dbq->add_relation('entity.id = ' . $this->_id); $dbq->add_relation('entity.type = allow.relationship_b'); // we think this should be commented out. it makes an error not appear. you probably want more than that. i don't have it. //$dbq->add_relation( 'entity.state = "Live"' ); $r1 = db_query($dbq->get_query(), 'Entity Error: Could not get relationships'); $rel_name = array(); while ($row = mysql_fetch_array($r1, MYSQL_ASSOC)) { $this->_right_relationships[$row['id']] = array(); $this->_right_relationships[$row['name']] = array(); $this->_right_relationships_info[$row['id']] = array(); $this->_right_relationships_info[$row['name']] = array(); $rel_name[$row['id']] = $row['name']; } $dbq = new DBSelector(); $dbq->add_table('r', 'relationship'); $dbq->add_field('r', '*'); $dbq->add_table('entity', 'entity'); $dbq->add_relation('entity.state = "Live"'); $dbq->add_relation('entity.id = r.entity_a'); $dbq->add_relation('r.entity_b = ' . $this->id()); if ($this->_env['restrict_site'] and !empty($this->_env['site'])) { $dbq->add_relation('(r.site=0 OR r.site=' . $this->_env['site'] . ')'); } $dbq->set_order('rel_sort_order'); $rels = $dbq->run(); foreach ($rels as $r) { $e = new entity($r['entity_a']); $this->_right_relationships[$r['type']][] = $e; $this->_right_relationships_info[$r['type']][] = $r; if (!empty($rel_name[$r['type']])) { $this->_right_relationships[$rel_name[$r['type']]][] = $e; $this->_right_relationships_info[$rel_name[$r['type']]][] = $r; } } }
/** * Refresh the in-memory cache of lock information * * @return void */ protected function _refresh_locks_cache() { $dbs = new DBSelector(); $dbs->add_table('entity_lock'); $dbs->add_relation('`entity_id` = "' . reason_sql_string_escape($this->_entity->id()) . '"'); self::$_raw_locks[$this->_entity->id()] = $dbs->run('Error getting locks for entity ' . $this->_entity->id() . '.', false); }
function clean_expunged() { $es = new entity_selector(); $es->add_type(id_of('minisite_page')); $es->limit_tables(); $es->limit_fields(); $result = $es->run_one('', 'All'); $page_ids = array_keys($result); $dbs = new DBSelector(); $dbs->add_table('URL_history', 'URL_history'); $dbs->add_field('URL_history', 'id'); $dbs->add_field('URL_history', 'page_id'); $dbs->add_field('URL_history', 'timestamp'); $dbs->add_field('URL_history', 'url'); $dbs->add_relation('URL_history.page_id NOT IN ("' . implode('","', $page_ids) . '")'); $rows = $dbs->run(); foreach ($rows as $row) { $e = new entity($row['page_id']); if (!reason_is_entity($e, 'minisite_page')) { $needs_deletion[] = $row['id']; } } if (isset($needs_deletion)) { $deleter_sql = 'DELETE FROM URL_history WHERE id IN ("' . implode('","', $needs_deletion) . '")'; if ($this->mode == 'test') { echo '<p>Would delete ' . count($needs_deletion) . ' rows that reference expunged entities with this query:</p>'; echo $deleter_sql; } if ($this->mode == 'run') { db_query($deleter_sql, 'Could not delete rows from URL_history'); echo '<p>Deleted ' . count($needs_deletion) . ' rows that reference expunged entities with this query:</p>'; echo $deleter_sql; } } else { echo '<p>There are no rows that reference expunged entities in the URL_history table - you may have already run this script</p>'; return true; } }
<?php /** * Provides a web service for the Thor WYSIWYG editor to get the current value of the temporary XML file in the DB * @package thor */ include_once 'paths.php'; include_once SETTINGS_INC . 'thor_settings.php'; if (!empty($_REQUEST['tmp_id'])) { include_once CARL_UTIL_INC . 'db/db.php'; include_once CARL_UTIL_INC . 'db/db_selector.php'; connectDB(THOR_FORM_DB_CONN); $dbs = new DBSelector(); $dbs->add_table('thor'); $dbs->add_field('thor', 'content'); $dbs->add_relation('thor.id = ' . carl_util_sql_string_escape($_REQUEST['tmp_id'])); $results = $dbs->run(); if (count($results) > 0) { header('Content-type: text/xml; charset=utf-8'); echo $results[0]['content']; } else { $results[0]['content'] = ''; } // if ( empty($results[0]['content']) ) // die('<' . '?xml version="1.0" ?' . '><form submit="Submit" reset="Clear" />'); } else { die('Please provide a tmp_id.'); }
else { $old_page_id = (integer) $_GET['old_page']; $new_page_id = (integer) $_GET['new_page']; if(empty($old_page_id) || empty($new_page_id)) { echo 'Invalid page id'; die(); } $dbs = new DBSelector(); $dbs->add_table('URL_history'); $dbs->add_relation('page_id = "'.addslashes($old_page_id).'"'); $dbs->set_order('timestamp ASC'); $rows = $dbs->run(); //pray($rows); echo '<h2>Replicating Page URL History</h2>'."\n"; echo '<ul>'."\n"; foreach($rows as $row) { if(!empty($row['url'])) { $query = 'INSERT INTO URL_history SET ' . 'url = "' . addslashes($row['url']) . '", ' . 'page_id = "' . addslashes($new_page_id) . '", ' . 'timestamp = "' . addslashes(time()) . '"'; $results = mysql_query( $query ); if( empty( $results ) )
</ol> <p>This script finds any unique names that do not fit those criteria, and reports on them. Fixing bad unique names is currently a manual process.</p> <input type="submit" name="do_it" value="Run the script" /> </form> <?php } else { reason_include_once('classes/entity_selector.php'); $dbs = new DBSelector(); $dbs->add_table('entity'); $dbs->add_field('entity', 'id', 'id'); $dbs->add_field('entity', 'unique_name', 'unique_name'); $dbs->add_field('entity', 'name', 'name'); $dbs->add_relation('`unique_name` != ""'); $dbs->add_relation('unique_name IS NOT NULL'); $dbs->add_relation('`state` IN ("Live","pending")'); $results = $dbs->run('Error getting unique names'); $uniques = array(); $names = array(); foreach ($results as $result) { if (empty($uniques[$result['unique_name']])) { $uniques[$result['unique_name']] = array(); } $uniques[$result['unique_name']][$result['id']] = $result['name']; } foreach ($uniques as $uname => $items) { $bad_string = false; $multiples = false; if (!preg_match("|^[0-9a-z_]*\$|i", $uname)) { $bad_string = true; } if (count($items) > 1) {
/** * We find all backwards rels in Reason. */ protected function find_backwards_rels() { $dbs = new DBSelector(); $dbs->add_table('r', 'relationship'); $dbs->add_table('ar', 'allowable_relationship'); $dbs->add_table('e1', 'entity'); $dbs->add_table('e2', 'entity'); $dbs->add_field('r', '*'); $dbs->add_relation('r.type = ar.id'); $dbs->add_relation('e1.id = r.entity_a'); $dbs->add_relation('e2.id = r.entity_b'); $dbs->add_relation('e1.type = ar.relationship_b'); $dbs->add_relation('e2.type = ar.relationship_a'); $dbs->add_relation('e1.type != e2.type'); // we cannot know if type to type rels are backwards! $result = $dbs->run(); if ($result) { foreach ($result as $row) { $backwards_rels[$row['id']] = $row; } } return !empty($backwards_rels) ? $backwards_rels : false; }