Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized the upgrade script #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 80 additions & 53 deletions tidypics/upgrades/2014010101.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,103 @@
// prevent timeout when script is running (thanks to Matt Beckett for suggesting)
set_time_limit(0);

// Ignore access to make sure all items get updated
$ia = elgg_set_ignore_access(true);

// don't want any event or plugin hook handlers from plugins to run
$original_events = _elgg_services()->events;
$original_hooks = _elgg_services()->hooks;
_elgg_services()->events = new Elgg_EventsService();
_elgg_services()->hooks = new Elgg_PluginHooksService();
elgg_register_plugin_hook_handler('permissions_check', 'all', 'elgg_override_permissions');
elgg_register_plugin_hook_handler('container_permissions_check', 'all', 'elgg_override_permissions');

// Make sure that entries for disabled entities also get upgraded
$access_status = access_get_show_hidden_status();
access_show_hidden_entities(true);

$db_prefix = elgg_get_config('dbprefix');

// now updating river entries for comments on images
$image_subtype_id = get_subtype_id('image');
$album_subtype_id = get_subtype_id('album');
$tidypics_batch_subtype_id = get_subtype_id('tidypics_batch');

/**
* Upgrade comments added on Tidypics images
*/

// Get river entries for comments added to Tidypics images
$batch = new ElggBatch('elgg_get_river', array(
'type' => 'object',
'subtype' => 'comment',
'action_type' => 'comment',
'joins' => array("JOIN {$db_prefix}entities te ON te.guid = rv.target_guid",
"JOIN {$db_prefix}entity_subtypes ts ON te.subtype = ts.id AND ts.subtype = 'image'"),
'limit' => false));
'type' => 'object',
'subtype' => 'comment',
'action_type' => 'comment',
'joins' => array("JOIN {$db_prefix}entities te ON te.guid = rv.target_guid"),
'wheres' => array("te.subtype = $image_subtype_id"),
'limit' => false
));

// Collect the ids of the river items that need to be upgraded
$river_entry_ids = array();
foreach ($batch as $river_entry) {
$query = "
UPDATE {$db_prefix}river
SET view = 'river/object/comment/image'
WHERE id = {$river_entry->id}
";
update_data($query);
$river_entry_ids[] = $river_entry->id;
}

// now updating river entries for comments on albums
$river_entry_ids = implode(', ', $river_entry_ids);
$query = "UPDATE {$db_prefix}river
SET view = 'river/object/comment/image'
WHERE id IN ($river_entry_ids)";
update_data($query);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first looped through all the river entries and collected their ID's. Thanks to this it's possible to do only one DB query instead of one query per each found river entry.


/**
* Upgrade comments added on Tidypics albums
*/

// Get river entries for comments added to Tidypics albums
$batch = new ElggBatch('elgg_get_river', array(
'type' => 'object',
'subtype' => 'comment',
'action_type' => 'comment',
'joins' => array("JOIN {$db_prefix}entities te ON te.guid = rv.target_guid",
"JOIN {$db_prefix}entity_subtypes ts ON te.subtype = ts.id AND ts.subtype = 'album'"),
'limit' => false));
'type' => 'object',
'subtype' => 'comment',
'action_type' => 'comment',
'joins' => array("JOIN {$db_prefix}entities te ON te.guid = rv.target_guid"),
'wheres' => array("te.subtype = $album_subtype_id"),
'limit' => false
));

// Collect the ids of the river items that need to be upgraded
$river_entry_ids = array();
foreach ($batch as $river_entry) {
$query = "
UPDATE {$db_prefix}river
SET view = 'river/object/comment/album'
WHERE id = {$river_entry->id}
";
update_data($query);
$river_entry_ids[] = $river_entry->id;
}

// now updating river entries for comments on tidypics_batches
// fix target_guid and access_id for river entries that do not yet point to the album
$river_entry_ids = implode(', ', $river_entry_ids);
$query = "UPDATE {$db_prefix}river
SET view = 'river/object/comment/album'
WHERE id IN {$river_entry_ids}";
update_data($query);

/**
* Upgrade comments added on Tidypics image batches
*/

// Get river entries for comments added to Tidypics batches
$batch = new ElggBatch('elgg_get_river', array(
'type' => 'object',
'subtype' => 'comment',
'action_type' => 'comment',
'joins' => array("JOIN {$db_prefix}entities te ON te.guid = rv.target_guid",
"JOIN {$db_prefix}entity_subtypes ts ON te.subtype = ts.id AND ts.subtype = 'tidypics_batch'"),
'limit' => false));
'type' => 'object',
'subtype' => 'comment',
'action_type' => 'comment',
'joins' => array("JOIN {$db_prefix}entities te ON te.guid = rv.target_guid"),
//"JOIN {$db_prefix}entity_subtypes ts ON te.subtype = ts.id AND ts.subtype = 'tidypics_batch'"),
'wheres' => array("te.subtype" => $tidypics_batch_subtype_id),
'limit' => false
));

// TODO Is this doing the correct kind of upgrade?
foreach ($batch as $river_entry) {
$target_entity = get_entity($river_entry->target_guid);
$album = get_entity($target_entity->container_guid);
$query = "
UPDATE {$db_prefix}river
SET view = 'river/object/comment/album',
access_id = {$album->access_id},
target_guid = {$album->guid}
WHERE id = {$river_entry->id}
";
update_data($query);
// fix target_guid and access_id for river entries that do not yet point to the album
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This last part is a bit weird. Was it possible to comment image batches instead of albums in the 1.8 version? Or why do we need to change the river target from image_batch into album?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Tidypics 1.8.0RC (last "official" release) the river entry created on uploading new images had the tidypics_batch entity as object_entity. I kept this unchanged. The problem was when commenting directly on the activity page such an entry the comments did only show up in the activity river attached to the upload entry but not on the album page or any image within this album.

So, I changed this in the following way already in my forked 1.8 version of Tidypics:

  • the original comment (annotation) to the tidypics_batch entity is still created (and will still show up only on the activity page). But there is no new separate river comment entry created.
  • in addition there's a second comment annotation created (with the same content) that is attached to the album the tidypics_batch belongs to. This comment will then also show up on the album page. For this comment annotation a new river entry was created (with album as object entity).

I think this way the commenting on Tidypics entities (with the three subtypes album, image and tidypics_batch) the behaviour is more on the line of other plugins with only one subtype (where comments made in the river are also displayed on the full view page of these entities).

What I did not in the Elgg 1.8 tree of my Tidypics fork is updating the already existing tidypic_batch river comments. But I thought the upgrade of an existing site to Elgg 1.9 (which made it necessary to update the river entries for images and albums anyway after the Elgg core upgrade script that converts the comment annotations to entities) would be a good opportunity for upgrading the old river entries of tidypics_batches to match the newly created entries.

$target_entity = get_entity($river_entry->target_guid);
$album = get_entity($target_entity->container_guid);
$query = "
UPDATE {$db_prefix}river
SET view = 'river/object/comment/album',
access_id = {$album->access_id},
target_guid = {$album->guid}
WHERE id = {$river_entry->id}
";
update_data($query);
}

// replace events and hooks
_elgg_services()->events = $original_events;
_elgg_services()->hooks = $original_hooks;

elgg_set_ignore_access($ia);
access_show_hidden_entities($access_status);