/** * Returns an array of field names that are OK to query against * @return array */ function get_ok_fieldnames() { static $ok = array(); static $query_made = false; if (!$query_made) { $this->open_conn(); foreach (get_entity_tables_by_type(id_of('user')) as $table) { foreach (get_fields_by_content_table($table) as $field) { if (!in_array($field, $this->taboo_fields) && !in_array($field, $ok)) { $ok[] = $field; } } } $this->close_conn(); foreach ($this->_gen_attr_depend as $fields_to_be_queried) { foreach ($fields_to_be_queried as $field) { if (!in_array($field, $this->taboo_fields) && !in_array($field, $ok)) { $ok[] = $field; } } } $query_made = true; } return $ok; }
function fix_amputees($type_id = 0) { set_time_limit(240); $types = get_entities_by_type_name('type'); foreach ($types as $type) { if ($type_id == 0 || $type_id == $type['id']) { $tables = get_entity_tables_by_type($type['id'], false); // lets not cache - this could be used right after a type is setup foreach ($tables as $table) { // changed from e.* to e.id ... test for speed improvements. $q = "SELECT e.id,type.name as type_name FROM entity AS e LEFT JOIN {$table} AS t ON e.id = t.id, entity AS type WHERE e.type = " . $type['id'] . " AND t.id IS NULL AND e.type = type.id"; $r = db_query($q, 'Unable to grab amputees.'); while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { $q = 'INSERT INTO ' . $table . ' (id) VALUES (' . $row['id'] . ')'; $this->queries[] = $q; if (empty($this->stats[$type['name']])) { $this->stats[$type['name']] = array(); } if (empty($this->stats[$type['name']][$table])) { $this->stats[$type['name']][$table] = 0; } $this->stats[$type['name']][$table]++; db_query($q, 'Unable to add prosthetic record.'); } mysql_free_result($r); } } } }
/** * Run the upgrader * * @return string HTML report */ public function run() { $ret = ''; $e = new entity(id_of('css')); if (!$e->get_value('custom_content_handler')) { reason_update_entity($e->id(), $this->user_id(), array('custom_content_handler' => 'css.php')); $ret .= '<p>The CSS type has been assigned a content manager.</p>' . "\n"; } $tables = get_entity_tables_by_type(id_of('css'), false); $has_meta = in_array('meta', $tables); $has_url = in_array('url', $tables); if ($has_meta || $has_url) { set_time_limit(3600); if ($has_meta) { $ret .= $this->_move_table_fields('meta'); } if ($has_url) { $ret .= $this->_move_table_fields('url'); } } if (empty($ret)) { $ret .= '<p>This upgrade has already been run. There is nothing to do.</p>'; } return $ret; }
function add_new_entity_table_to_type($table, $type_unique_name) { $type_id = reason_unique_name_exists($type_unique_name) ? id_of($type_unique_name) : false; if ($type_id) { $tables = get_entity_tables_by_type($type_id, false); if (!in_array($table, $tables)) { if ($this->mode == 'test') { echo '<p>Would create table ' . $table . ' for type ' . $type_unique_name . '</p>'; } else { create_reason_table($table, $type_unique_name, $this->reason_id); echo '<p>Created table ' . $table . ' for type ' . $type_unique_name . '</p>'; } } else { echo '<p>The table ' . $table . ' for type ' . $type_unique_name . ' already exists.</p>'; } } }
/** * Run the upgrader * * @return string HTML report */ public function run() { $tables = get_entity_tables_by_type(id_of('faculty_staff'), false); if (!in_array('chunk', $tables)) { return '<p>This upgrade has already been run. There is nothing to do.</p>'; } else { // This should take considerably less time than one hour! // This should make sure things aren't killed partway through set_time_limit(3600); $success = reason_move_table_fields(id_of('faculty_staff'), 'chunk', 'faculty_staff', $this->user_id()); if ($success) { return '<p>Successfully moved faculty/staff content in the chunk table to the faculty_staff table.</p>'; } else { $ret = '<p>Unable to move the content from the chunk to the faculty/staff table. Please look in your PHP error logs for information about the cause of this error. This error may cause problems with Reason modules or queries; we recommend restoring your latest backup, identifying and fixing the cause on a testing server, and re-running this upgrade.</p>'; $err = error_get_last(); if (!empty($err)) { $ret .= '<p>Last error: "' . htmlspecialchars($err['message']) . '"</p>'; } return $ret; } } }
mysql_free_result( $r ); // run through all types reset( $types ); while( list( $type_id, $type ) = each( $types ) ) { $duplicates = array(); $tables = array(); $fields = array(); $ids_to_delete = array(); //echo '<hr />'; //echo 'type '.$type_id.': '.$type['name'].'<br />'; // get tables of a type $tables = get_entity_tables_by_type( $type_id ); // get fields of those tables reset( $tables ); while( list( ,$table ) = each( $tables ) ) { $q = "DESC $table"; $r = db_query( $q, 'could not query table '.$table ); while( $row = mysql_fetch_array( $r, MYSQL_ASSOC ) ) { if( !in_array($row['Field'], $ignore_fields ) ) $fields[ $row['Field'] ] = $row['Field']; } mysql_free_result( $r ); }
function process() { $site_id = $this->site_id; $counts = array(); for ($i = 1; $i <= $this->max_upload_number; $i++) { $element = $this->get_element('upload_' . $i); if (!empty($element->tmp_full_path) and file_exists($element->tmp_full_path)) { $filename = $this->get_value('upload_' . $i . '_filename'); if ($this->verify_image($element->tmp_full_path)) { if (empty($counts[$filename])) { $this->files[$filename] = $element->tmp_full_path; $counts[$filename] = 1; } else { $counts[$filename]++; $this->files[$filename . '.' . $counts[$filename]] = $element->tmp_full_path; } } else { $this->invalid_files[$filename] = $element->tmp_full_path; } } } if (count($this->files)) { $page_id = (int) $this->get_value('attach_to_page'); $max_sort_order_value = 0; if ($page_id) { $max_sort_order_value = $this->_get_max_sort_order_value($page_id); } $sort_order_value = $max_sort_order_value; $tables = get_entity_tables_by_type(id_of('image')); $valid_file_html = '<ul>' . "\n"; foreach ($this->files as $entry => $cur_name) { $sort_order_value++; $valid_file_html .= '<li><strong>' . $entry . ':</strong> processing '; $date = ''; // get suffix $type = strtolower(substr($cur_name, strrpos($cur_name, '.') + 1)); $ok_types = array('jpg'); // get exif data if ($this->get_value('exif_override') && in_array($type, $ok_types) && function_exists('read_exif_data')) { // read_exif_data() does not obey error supression $exif_data = @read_exif_data($cur_name); if ($exif_data) { // some photos may have different fields filled in for dates - look through these until one is found $valid_dt_fields = array('DateTimeOriginal', 'DateTime', 'DateTimeDigitized'); foreach ($valid_dt_fields as $field) { // once we've found a valid date field, store that and break out of the loop if (!empty($exif_data[$field])) { $date = $exif_data[$field]; break; } } } } else { $date = $this->get_value('datetime'); } $keywords = $entry; if ($this->get_value('keywords')) { $keywords .= ', ' . $this->get_value('keywords'); } // insert entry into DB with proper info $values = array('datetime' => $date, 'image_type' => $type, 'author' => $this->get_value('author'), 'state' => 'Pending', 'keywords' => $keywords, 'description' => $this->get_value('description'), 'name' => $this->get_value('name') ? $this->get_value('name') : $entry, 'content' => $this->get_value('content'), 'original_image_format' => $this->get_value('original_image_format'), 'new' => 0, 'no_share' => $this->get_value('no_share')); //tidy values $no_tidy = array('state', 'new'); foreach ($values as $key => $val) { if (!in_array($key, $no_tidy) && !empty($val)) { $values[$key] = trim(get_safer_html(tidy($val))); } } $id = reason_create_entity($site_id, id_of('image'), $this->user_id, $entry, $values); if ($id) { //assign to categories $categories = $this->get_value('assign_to_categories'); if (!empty($categories)) { foreach ($categories as $category_id) { create_relationship($id, $category_id, relationship_id_of('image_to_category')); } } //assign to gallery page if ($page_id) { create_relationship($page_id, $id, relationship_id_of('minisite_page_to_image'), array('rel_sort_order' => $sort_order_value)); } // resize and move photos $new_name = PHOTOSTOCK . $id . '.' . $type; $orig_name = PHOTOSTOCK . $id . '_orig.' . $type; $tn_name = PHOTOSTOCK . $id . '_tn.' . $type; // Support for new fields; they should be set null by default, but will be // changed below if a thumbnail/original image is created. This is very messy... $thumbnail_image_type = null; $original_image_type = null; // atomic move the file if possible, copy if necessary if (is_writable($cur_name)) { rename($cur_name, $new_name); } else { copy($cur_name, $new_name); } // create a thumbnail if need be list($width, $height, $type, $attr) = getimagesize($new_name); if ($width > REASON_STANDARD_MAX_IMAGE_WIDTH || $height > REASON_STANDARD_MAX_IMAGE_HEIGHT) { copy($new_name, $orig_name); resize_image($new_name, REASON_STANDARD_MAX_IMAGE_WIDTH, REASON_STANDARD_MAX_IMAGE_HEIGHT); $original_image_type = $this->image_types[$type]; } $thumb_dimensions = get_reason_thumbnail_dimensions($site_id); if ($width > $thumb_dimensions['width'] || $height > $thumb_dimensions['height']) { copy($new_name, $tn_name); resize_image($tn_name, $thumb_dimensions['width'], $thumb_dimensions['height']); $thumbnail_image_type = $this->image_types[$type]; } // real original $my_orig_name = $this->add_name_suffix($cur_name, '-unscaled'); if (file_exists($my_orig_name)) { // move the original image into the photostock directory if (is_writable($my_orig_name)) { rename($my_orig_name, $orig_name); } else { copy($my_orig_name, $orig_name); } $original_image_type = $this->image_types[$type]; } $info = getimagesize($new_name); $size = round(filesize($new_name) / 1024); // now we have the size of the resized image. $values = array('width' => $info[0], 'height' => $info[1], 'size' => $size, 'thumbnail_image_type' => $thumbnail_image_type, 'original_image_type' => $original_image_type); // update with new info - don't archive since this is really just an extension of the creation of the item // we needed that ID to do something reason_update_entity($id, $this->user_id, $values, false); $valid_file_html .= 'completed</li>'; } else { trigger_error('Unable to create image entity'); $valid_file_html .= '<li>Unable to import ' . $entry . '</li>'; } sleep(1); } $valid_file_html .= '</ul>' . "\n"; $num_image_string = count($this->files) == 1 ? '1 image has ' : count($this->files) . ' images have '; $valid_file_html .= '<p>' . $num_image_string . 'been successfully imported into Reason.</p>'; $valid_file_html .= '<p>They are now pending.</p>'; $next_steps[] = '<a href="?site_id=' . $site_id . '&type_id=' . id_of('image') . '&user_id=' . $this->user_id . '&cur_module=Lister&state=pending">review & approve imported images</a>'; } if (isset($this->invalid_files)) { $invalid_files = array_keys($this->invalid_files); $invalid_file_html = '<p>The following could not be validated as image files and were not successfully imported.</p>'; $invalid_file_html .= '<ul>'; foreach ($invalid_files as $file) { $invalid_file_html .= '<li><strong>' . reason_htmlspecialchars($file) . '</strong></li>'; } $invalid_file_html .= '</ul>'; } $next_steps[] = '<a href="' . get_current_url() . '">Import another set of images</a>'; if (!isset($this->invalid_files) && !isset($this->files)) { echo '<p>You did not select any files for upload</p>'; } if (isset($valid_file_html)) { echo $valid_file_html; } if (isset($invalid_file_html)) { echo $invalid_file_html; } echo '<p>Next Steps:</p><ul><li>' . implode('</li><li>', $next_steps) . '</li></ul>'; $this->show_form = false; }
function get_fields_by_type($type, $full_field = false) { $tables = get_entity_tables_by_type($type); $fields = array(); foreach ($tables as $value) { $r = db_query('desc ' . $value, 'Unable to select fields'); while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) { if ($full_field) { $fields[$row['Field']] = $row; } else { $fields[$row['Field']] = $row['Field']; } } mysql_free_result($r); } return $fields; }
/** * Run the upgrader * * @return string HTML report */ public function run() { $ret = ''; $tables = get_entity_tables_by_type($this->_type_id(), false); if (!in_array($this->_destination_table(), $tables)) { $ret .= $this->_add_table($this->_destination_table()); } $to_move = array_intersect($this->_source_tables(), $tables); if (!empty($to_move)) { foreach ($to_move as $table) { $ret .= $this->_move_table_fields($table); } } if (empty($ret)) { $ret .= '<p>This upgrade has already been run. There is nothing to do.</p>'; } return $ret; }
/** * Move all the fields of one table into another table for a specific type * * This method is for denormalizing Reason tables. For example, a type may use a common table * like meta, datetime, or chunk. For performance reasons, it can be desirable to collapse * these tables into a single table just for that type. This method will do that. * * @param integer $type The ID of the type whose fields we are moving * @param string $source_table The name of the table we are moving fields FROM * @param string $destination_table The name of the table we are moving fields TO * @param integer $user_id The Reason ID of the user who is doing this move * @return boolean Success * * @todo Add limit to ensure fields are only created that don't already exist */ function reason_move_table_fields($type, $source_table, $destination_table, $user_id) { // Sanity checks if(empty($type)) { trigger_error('No type provided in reason_move_table_fields()'); return false; } if(empty($source_table)) { trigger_error('No source table provided in reason_move_table_fields()'); return false; } if(!is_string($source_table)) { trigger_error('Source table provided not a string in reason_move_table_fields()'); return false; } if(empty($destination_table)) { trigger_error('No destination table provided in reason_move_table_fields()'); return false; } if(!is_string($destination_table)) { trigger_error('Destination table provided not a string in reason_move_table_fields()'); return false; } if('entity' == $source_table || 'entity' == $destination_table) { trigger_error('reason_move_table_fields() cannot move fields into or out of the entity table.'); return false; } if(is_object($type)) { $type_id = $type->id(); } elseif(is_numeric($type)) { $type_id = (integer) $type; } else { $type_id = id_of($type); } if(empty($type_id)) { trigger_error('Invalid type specified in reason_move_table_fields().'); return false; } if(is_object($type)) $type_entity = $type; else $type_entity = new entity($type_id); $type_vals = $type_entity->get_values(); if(empty($type_vals)) { trigger_error('Type specified (id '.$type_id.') is not a Reason entity in reason_move_table_fields().'); return false; } if($type_entity->get_value('type') != id_of('type')) { trigger_error('Type specified (id '.$type_id.') is not a Type entity in reason_move_table_fields().'); return false; } if($type_entity->get_value('state') != 'Live') { trigger_error('Type specified (id '.$type_id.') is not a live entity in reason_move_table_fields().'); return false; } if(empty($user_id)) { trigger_error('No user id specified in reason_move_table_fields().'); return false; } $user = new entity($user_id); if(!$user->get_values() || $user->get_value('type') != id_of('user')) { trigger_error('Invalid user ID specified in reason_move_table_fields().'); return false; } // check for table existence $es = new entity_selector(); $es->add_type(id_of('content_table')); $es->add_relation('`name` = "'.addslashes($source_table).'"'); $source_table_result = $es->run_one(); if(empty($source_table_result)) { trigger_error('Source table "'.$source_table.'" does not exist in reason_move_table_fields()'); return false; } $es = new entity_selector(); $es->add_type(id_of('content_table')); $es->add_relation('`name` = "'.addslashes($destination_table).'"'); $destination_table_result = $es->run_one(); if(empty($destination_table_result)) { trigger_error('Destination table "'.$destination_table.'" does not exist in reason_move_table_fields()'); return false; } $source_table_entity = current($source_table_result); $destination_table_entity = current($destination_table_result); // ensure type uses both tables $type_tables = get_entity_tables_by_type( $type_id ); if(!in_array($source_table, $type_tables)) { trigger_error('Source table "'.$source_table.'" not part of the type in reason_move_table_fields()'); return false; } if(!in_array($destination_table, $type_tables)) { trigger_error('Destination table "'.$destination_table.'" not part of the type in reason_move_table_fields()'); return false; } $es = new entity_selector(); $es->add_type(id_of('type')); $es->add_left_relationship($destination_table_entity->id(),relationship_id_of('type_to_table')); $es->add_relation('`entity`.`id` != "'.addslashes($type_id).'"'); $other_types = $es->run_one(); if(!empty($other_types)) { trigger_error(count($other_types).' other type(s) share the destination table with the type specified in reason_move_table_fields(). reason_move_table_fields() can only move fields into single-type tables.'); return false; } // get the fields in the old table $es = new entity_selector(); $es->add_type(id_of('field')); $es->add_left_relationship($source_table_entity->id(), relationship_id_of('field_to_entity_table')); $source_table_fields = $es->run_one(); if(empty($source_table_fields)) { trigger_error('Source table '.$source_table.' does not appear to have any fields associated with it in Reason. Unable to move its content in reason_move_table_fields()'); } $q = 'DESCRIBE `'.addslashes($destination_table).'`'; $handle = db_query( $q, 'Unable to describe destination table in reason_move_table_fields()' ); $raw_dest_cols = array(); while($row = mysql_fetch_assoc($handle)) { $raw_dest_cols[] = $row['Field']; } foreach($source_table_fields as $k=>$field) { if(in_array($field->get_value('name'),$raw_dest_cols)) { trigger_error($field->get_value('name').' field is already in destination table. Unable to accomplish reason_move_table_fields().'); return false; } $tmp_field_name = $field->get_value('name').'_move_tmp'; if(in_array($tmp_field_name,$raw_dest_cols)) { trigger_error($tmp_field_name.' field already in destination table. There appears to have been an error in a previous attempt to run reason_move_table_fields(). Please drop this column in MySQL and try again.'); return false; } $source_table_fields[$k]->set_value('_field_move_temp_name',$field->get_value('name').'_move_tmp'); } // Done with sanity checks // map old to temp field names & create new fields $query_parts = array(); foreach($source_table_fields as $k=>$field) { $source_table_fields[$k]->set_value('_field_move_temp_name',$field->get_value('name').'_move_tmp'); $q = 'ALTER TABLE `'.addslashes($destination_table).'` ADD '.addslashes( $field->get_value('_field_move_temp_name') ).' '. $field->get_value('db_type'); db_query( $q, 'Unable to create new field '.$field->get_value('_field_move_temp_name').' in reason_move_table_fields()' ); $values = array(); foreach($field->get_values() as $f=>$v) { if($f != 'name' && $f != 'id' && strpos($f,'_') !== 0) { $values[$f] = $v; } } $id = reason_create_entity( id_of('master_admin'), id_of('field'), $user_id, $field->get_value('_field_move_temp_name'), $values); $source_table_fields[$k]->set_value('_new_field_id',$id); $query_parts[] = '`'.addslashes($destination_table).'`.`'.addslashes($field->get_value('_field_move_temp_name')).'` = `'.addslashes($source_table).'`.`'.addslashes($field->get_value('name')).'`'; } // copy content of old fields to new fields $q = 'UPDATE `'.addslashes($destination_table).'`, `'.addslashes($source_table).'`, `entity` SET '.implode(' , ',$query_parts).' WHERE `'.addslashes($destination_table).'`.`id` = `'.addslashes($source_table).'`.`id` AND `'.addslashes($destination_table).'`.`id` = `entity`.`id` AND `entity`.`type` = "'.addslashes($type_id).'";'; db_query($q,'Attempt to move data between fields'); // zap source table's type-to-table relationship for this type $conditions = array( 'entity_a' => $type_id, 'entity_b' => $source_table_entity->id(), 'type' => relationship_id_of('type_to_table'), ); delete_relationships( $conditions ); // create new field-to-table relationship for new fields and update field names in new table -- remove temp flag foreach($source_table_fields as $field) { create_relationship( $field->get_value('_new_field_id'), $destination_table_entity->id(), relationship_id_of( 'field_to_entity_table' ) ); $q = 'ALTER TABLE `'.addslashes($destination_table).'` CHANGE '.addslashes($field->get_value('_field_move_temp_name')).' '.addslashes( $field->get_value('name') ).' '.$field->get_value('db_type') ; db_query( $q, 'Unable to change field name of '.$field->get_value('_field_move_temp_name').' in reason_move_table_fields()' ); reason_update_entity( $field->get_value('_new_field_id'), $user_id, array('name' => $field->get_value('name') ), false ); } // delete the rows from the source table $q = 'DELETE `'.addslashes($source_table).'` FROM `'.addslashes($source_table).'`, `entity` WHERE `'.addslashes($source_table).'`.`id` = `entity`.`id` AND `entity`.`type` = "'.addslashes($type_id).'"'; db_query($q,'Attempt to delete rows from '.$source_table.' in reason_move_table_fields()'); get_entity_tables_by_id( $type_id, false ); return true; }
/** * @todo ensure that the collation works if you have your columns set up to use utf-8 character sets */ function &get_matches() { if (!isset($this->_matches)) { $limit = $this->get_limit(); $type_id = $this->get_type_id(); $tables = get_entity_tables_by_type($type_id); $table_array[] = 'entity'; if ($site_id = $this->get_site_id()) { $es = new entity_selector($site_id); } else { $es = new entity_selector(); } $es->add_type($type_id); if (!empty($limit)) { $es->set_num($limit); } $relation_pieces = array(); foreach ($tables as $table) { $fields = array_intersect(get_fields_by_content_table($table), $this->get_type_fields()); if ($fields) { $table_array[] = $table; foreach ($fields as $field) { $relation_pieces[] = $table . '.' . $field . ' LIKE "%' . $this->get_search_term_for_query() . '%" COLLATE latin1_bin'; } } } $es->limit_tables(array_unique($table_array)); $es->add_relation('( ' . implode(' OR ', $relation_pieces) . ' )'); if ($excluded_array = $this->get_excluded()) { $es->add_relation('entity.id NOT IN (' . implode(",", $excluded_array) . ')'); } $this->_matches = $es->run_one(); } return $this->_matches; }
$txt = ''; if (!empty($_REQUEST['type'])) { if (isset($types[$_REQUEST['type']])) { $only_type = $types[$_REQUEST['type']]; $types = array($_REQUEST['type'] => $only_type); } else { $types = array(); echo 'Not a type'; } } foreach ($types as $type) { //echo $type->get_value('name').'<br />'; $tables = get_entity_tables_by_type($type->id()); $es = new entity_selector(); $es->add_type($type->id()); $tables = get_entity_tables_by_type($type->id()); //pray($tables); $relation_pieces = array(); foreach ($tables as $table) { $fields = get_fields_by_content_table($table); //pray($fields); foreach ($fields as $field) { $relation_pieces[] = $table . '.' . $field . ' LIKE "%' . $sql_search_string . '%"'; } } $relation = '( ' . implode(' OR ', $relation_pieces) . ' )'; //echo '<p>'.$relation.'</p>'; $es->add_relation($relation); //$es->add_relation('* LIKE "%'.$_REQUEST['search_string'].'%"'); $entities = $es->run_one(); if (!empty($entities)) {
/** * Gets one of the queries. Works by creating a new entity selector, adding the owns/borrows * relationship, swallowing a db_selector object, then swallowing the current object. * sort of a funny want to do things, but it assures us that we'll get all the right ones * @param int $type the type id to be selected * @param string $status either Live, Archived, Pending, or All * @return string MYSQL query string to select the objects from the DB. * @todo $status should really be the first variable here, not the second. I don't think this is called outside of the class anywhere on the site, but it would be disasterous if it did and we switched it. */ function get_one_query($type = '', $status = 'Live') { if (!$type) { if ($this->type[0]) { $type = $this->type[0]; } else { return ''; } } if (count($this->_rel_sort_field_queue) > 0) { $this->_process_rel_sort_fields(); } $new_e = new entity_selector($this->site_id); $sharing = ''; if ($this->owns) { $sharing .= 'owns'; } if ($this->borrows) { $sharing .= 'borrows'; } if ($status != 'All') { $new_e->add_relation('entity.state = "' . $status . '"'); } else { $new_e->add_relation('entity.state != "Archived"'); } if (is_array($this->limit_fields) && empty($this->table_mod) && $this->_exclude_tables_dynamically) { $to_exclude = false; $tables = get_entity_tables_by_type($type); foreach ($tables as $table) { $exclude = true; if ($table == 'entity') { $exclude = false; } if ($exclude == true && strpos($this->orderby, $table . '.') !== FALSE) { $exclude = false; } if ($exclude == true) { $limit_fields = $this->limit_fields; while ($cur_element = array_pop($limit_fields)) { if (strpos($cur_element, $table . '.') !== FALSE) { $exclude = false; continue; } } } if ($exclude == true) { $relation_array = $this->relations; while ($cur_element = array_pop($relation_array)) { if (strpos($cur_element, $table . '.') !== FALSE) { $exclude = false; continue; } } } if ($exclude) { $to_exclude[] = $table; } } if ($to_exclude) { $this->exclude_tables($to_exclude); } } $new_e->swallow(get_entities_by_type_object($type, $this->site_id, $sharing, $this->table_mod, $this->table_mod_action)); if (is_array($this->limit_fields)) { $new_e->fields = array_unique(array_merge(array('entity.id'), $this->limit_fields)); } $new_e->swallow($this); if ($this->union) { $union_clause = ''; $union_es = carl_clone($new_e); foreach ($this->diff['fields'] as $k => $union) { $multi_union_es[$k] = carl_clone($new_e); $multi_union_es[$k]->fields = array_diff($union_es->fields, $this->diff['fields'][$k]); $multi_union_es[$k]->tables = array_diff_assoc($union_es->tables, $this->diff['tables'][$k]); $multi_union_es[$k]->relations = array_diff($union_es->relations, $this->diff['relations'][$k]); $alter_array = array_diff($union_es->fields, $multi_union_es[$k]->fields); foreach ($alter_array as $k2 => $v) { if (isset($this->union_fields[$v])) { $multi_union_es[$k]->fields[$k2] = $this->union_fields[$v]; } } ksort($multi_union_es[$k]->fields); } foreach ($multi_union_es as $mu_es) { $merged_es[] = carl_clone($mu_es); } if (count($multi_union_es) > 1) { // prep final selector - needs to select all while padding left and right relationship fields with 0 foreach ($this->diff['fields'] as $k => $union) { $union_es->fields = array_diff($union_es->fields, $this->diff['fields'][$k]); $union_es->tables = array_diff_assoc($union_es->tables, $this->diff['tables'][$k]); $union_es->relations = array_diff($union_es->relations, $this->diff['relations'][$k]); } foreach ($new_e->fields as $k => $v) { if (!isset($union_es->fields[$k])) { $union_es->fields[$k] = $this->union_fields[$v]; } } ksort($union_es->fields); //$union_es now holds a base es foreach ($merged_es as $k => $alter_es) { // fix fields - invert what is being selected as 0 and those that are not foreach ($alter_es->fields as $k2 => $v) { if (isset($this->union_fields[$v])) { $merged_es[$k]->fields[$k2] = $this->union_fields[$v]; } elseif (in_array($v, $this->union_fields)) { $merged_es[$k]->fields[$k2] = current($this->diff['fields'][$k]); } } $merged_es[$k]->tables = array_merge($this->diff['tables'][$k], $union_es->tables); $merged_es[$k]->relations = array_merge($this->diff['relations'][$k], $union_es->relations); } $merged_es[] = carl_clone($union_es); } else { $merged_es[] = carl_clone($new_e); } return $this->get_merged_query($merged_es); } else { return $new_e->get_query(); } }
public function run() { if (!$this->fully_set_up()) { return '<p>There is a programming error that prevents this denormalization attempt from working. Please check with a friendly geek and try again once fixes have been made.</p>'; } $ret = ''; $tables = get_entity_tables_by_type($this->type_id(), false); if (!in_array($this->destination_table(), $tables)) { $ret .= $this->_add_table($this->destination_table()); } $to_move = array_intersect($this->source_tables(), $tables); if (!empty($to_move)) { foreach ($to_move as $table) { $ret .= $this->_move_table_fields($table); } } if (empty($ret)) { $ret .= '<p>This upgrade has already been run. There is nothing to do.</p>'; } return $ret; }
function remove_sortable_table_from_blurbs() { echo '<hr/>'; $tables = get_entity_tables_by_type(id_of('text_blurb'), false); // no cache if (!in_array("sortable", $tables)) { echo "<p>The text blurb type does not use the entity table sortable - this script has probably been run</p>"; } else { $es = new entity_selector(); $es->add_type(id_of('content_table')); $es->add_relation('entity.name = "sortable"'); $es->add_right_relationship(id_of('text_blurb'), relationship_id_of('type_to_table')); $es->set_num(1); $es->limit_tables(); $es->limit_fields(); $result = $es->run_one(); if (!empty($result)) { $table = current($result); // grab all text blurb entities $es2 = new entity_selector(); $es2->add_type(id_of('text_blurb')); $es2->limit_tables(); $es2->limit_fields(); $result2 = $es2->run_one(); $ids = implode(",", array_keys($result2)); $q = "DELETE from sortable WHERE id IN(" . $ids . ")"; if ($this->mode == "test") { echo "<p>Would delete all relationships between the text blurb type and the sortable entity table across type_to_table<p>"; echo "<p>Would also run this query to zap all the entities in the sortable table that correspond to text blurbs:</p>"; echo "<p>" . $q . "</p>"; } else { delete_relationships(array('entity_a' => id_of('text_blurb'), 'entity_b' => $table->id(), 'type' => relationship_id_of('type_to_table'))); db_query($q); echo "<p>Deleted type_to_table relationship bewteen text blurbs and sortable, and the corresponding sortable entities</p>"; } } else { echo '<p>Could not find the entity table even though get_entity_tables_by_type says that it exists. Doing nothing.</p>'; } } }