/** * Get the most recent entry for the page_id - if its URL does not match the URL of the page now, create a new entry and update * the timestamp of the last location (if it exists). * * @todo consider whether or not there is any utility in checking children if we are not creating an entry for the current * location the page * * @param integer $page_id * @param boolean $check_children * @return boolean */ function update_URL_history( $page_id, $check_children = true ) { $page = new entity($page_id); if (reason_is_entity($page, 'minisite_page') && !$page->get_value('url')) { $builder = new reasonPageURL(); $builder->disable_static_cache(); // force a refresh for this instance of the builder in case the URL just changed $builder->set_id($page->id()); $url = $builder->get_relative_url(); if (!empty($url)) // we only bother if we can get a url { // lets grab the most recent entry for this page_id $d = new DBselector(); $d->add_table( 'history', 'URL_history' ); $d->add_field( 'history', 'id', 'id' ); $d->add_field( 'history', 'url', 'url' ); $d->add_field( 'history', 'page_id', 'page_id' ); $d->add_field( 'history', 'timestamp', 'timestamp' ); $d->add_relation( 'history.page_id = ' . $page_id ); $d->set_num(1); $d->set_order( 'history.timestamp DESC, history.id DESC' ); // get highest id of highest timestamp $result = db_query( $d->get_query() , 'Error getting most recent URL_history entry for page '.$page_id ); if( $row = mysql_fetch_assoc($result)) { $create_new = ($row['url'] == $url) ? false : true; $update_old = $create_new; // if we create new in this case we want to update the old as well } else { $create_new = true; $update_old = false; } if ($create_new) // lets use the SQLer to do this { $sqler = new SQLER(); $cur_time = time(); $values = array('url' => $url, 'page_id' => $page_id, 'timestamp' => $cur_time); $sqler->insert('URL_history', $values); if ($update_old) // the old row is the one we already grabbed - lets update its timestamp to be just before what we just created { $sqler->update_one('URL_history', array('timestamp' => ($cur_time - 1)), $row['id']); } } if( $check_children ) update_children( $page_id ); return true; } } trigger_error('update_URL_history called on entity id ' . $page_id . ' which does not appear to be a valid minisite_page entity with a url (is it an external URL?)'); return false; }
/** * Find pages that are actually external URLs - if the external url is the most recent timestamp for an entry, keep track of its details. * * Delete all the external URL page_id entries from the table, and then insert corrected ones for the external urls that were being used * as the most recent timestamp. The corrected entries use the parent page_id instead, which is what the external url entries would typically * resolve to. This approach keeps the history working as intended, while removing the external url entries that are no longer put into the * table in Reason 4 Beta 8 */ function repair_external_url_problem() { // first we grab all the minisite page_ids that are actually URLs $es = new entity_selector(); $es->add_type(id_of('minisite_page')); $es->limit_tables(array('page_node', 'url')); $es->limit_fields(); // we only want pages that have the url field populated $es->add_relation('((url.url != "") AND (url.url IS NOT NULL))'); $result = $es->run_one(); if ($result) { $dbs = new DBSelector(); $dbs->add_table('URL_history'); $dbs->add_relation('page_id IN ("' . implode('","', array_keys($result)) . '")'); $rows = $dbs->run(); foreach ($rows as $row) { $url = $row['url']; $id = $row['id']; $urls[$id] = $url; $rows_by_id[$id] = $row; } } if (isset($urls)) { $unique_urls = array_unique($urls); foreach ($unique_urls as $url) { $query = 'SELECT * FROM URL_history WHERE url ="' . reason_sql_string_escape($url) . '" ORDER BY timestamp DESC limit 1'; $result = db_query($query, 'error in query'); $latest_row = $result ? mysql_fetch_assoc($result) : false; if ($latest_row) { $latest_row_id = $latest_row['id']; if (isset($urls[$latest_row_id])) { $needs_fixin[] = $latest_row_id; } } } if (isset($needs_fixin)) { foreach ($needs_fixin as $url_history_id_to_update) { $d = new DBselector(); $d->add_table('r', 'relationship'); $d->add_field('r', 'entity_b', 'parent_id'); $d->add_relation('r.type = ' . relationship_id_of('minisite_page_parent')); $d->add_relation('r.entity_a = ' . $rows_by_id[$url_history_id_to_update]['page_id']); $result = db_query($d->get_query(), 'Error getting parent ID.'); if ($myrow = mysql_fetch_assoc($result)) { $fixer[$url_history_id_to_update]['deleted'] = $rows_by_id[$url_history_id_to_update]['deleted']; $fixer[$url_history_id_to_update]['timestamp'] = $rows_by_id[$url_history_id_to_update]['timestamp']; $fixer[$url_history_id_to_update]['url'] = $rows_by_id[$url_history_id_to_update]['url']; $fixer[$url_history_id_to_update]['page_id'] = $myrow['parent_id']; $fixer[$url_history_id_to_update]['old_page_id'] = $rows_by_id[$url_history_id_to_update]['page_id']; $fixer[$url_history_id_to_update]['new_page_id'] = $myrow['parent_id']; } } } if (isset($fixer)) { $fixer_sql = 'INSERT INTO `URL_history` (`id`, `url`, `page_id`, `timestamp`, `deleted`) VALUES '; $fixcount = 0; foreach ($fixer as $row_id => $fields) { $notfirst = !isset($notfirst) ? false : true; if ($notfirst) { $fixer_sql .= ", "; } $fixer_sql .= "(" . $row_id . ", '" . $fields['url'] . "', " . $fields['page_id'] . ", " . $fields['timestamp'] . ", '" . $fields['deleted'] . "')"; $fixcount++; } } $deleter_sql = 'DELETE FROM URL_history WHERE id IN ("' . implode('","', array_keys($rows_by_id)) . '")'; if ($this->mode == 'test') { echo '<p>Would delete ' . count($rows_by_id) . ' rows with this query:</p>'; echo $deleter_sql; } elseif ($this->mode == 'run') { db_query($deleter_sql, 'Could not delete rows from URL_history'); echo '<p>Deleted ' . count($rows_by_id) . ' rows with this query:</p>'; echo $deleter_sql; } if ($this->mode == 'test' && isset($fixer_sql)) { echo '<p>Would restore ' . $fixcount . ' rows with updated page_ids this query:</p>'; echo $fixer_sql; } elseif ($this->mode == 'run' && isset($fixer_sql)) { db_query($fixer_sql, 'Could not restore rows to URL_history'); echo '<p>Restored ' . $fixcount . ' rows with this query:</p>'; echo $fixer_sql; } } else { echo '<p>There are no rows in the URL_history table that resolve to external urls - you may have already run this script</p>'; return true; } return false; }
/** * We use a DBSelector to construct a super basic query to quickly get the owner id. * * This method probably ought to be a method directly on minisite page entities ... but alas we aren't there yet. * For efficiency sake, we could eliminate this if the owner was stored on the page entity and not across a relationship * * @access private * @param object minisite_page entity * @return int owner id for a minisite page */ function _get_owner_id(&$page) { if (!$page->has_value('owner_id')) { $d = new DBselector(); $d->add_table( 'r', 'relationship' ); $d->add_field( 'r', 'entity_a', 'owner_id' ); $d->add_relation( 'r.type = ' . get_owns_relationship_id(id_of('minisite_page'))); $d->add_relation( 'r.entity_b = ' . $page->id() ); $d->set_num(1); $result = db_query( $d->get_query() , 'Error getting owner ID.' ); if( $row = mysql_fetch_assoc($result)) { $page->set_value('owner_id', $row['owner_id']); } else { $page->set_value('owner_id', false); } } return $page->get_value('owner_id'); }
/** * 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; }