2014040201.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * Update view path of river entries for comments made on Tidypics images, albums and tidypics_batches (image uploads)
  4. *
  5. * This is a follow-up upgrade to be executed AFTER the Elgg core upgrade from Elgg 1.8 to Elgg 1.9.
  6. * The Elgg core upgrade script changes comments from annotations to entities and updates the river entries accordingly.
  7. * This Tidypics-specific script then updates the views referred in river entries for comments made on Tidypics entities
  8. * to allow for using the Tidypics-specific river comment views (which add optionally a thumbnail image of the image/album
  9. * commented on and takes the specifics of commenting on tidypics_batches into account)
  10. */
  11. // prevent timeout when script is running (thanks to Matt Beckett for suggesting)
  12. set_time_limit(0);
  13. // Ignore access to make sure all items get updated
  14. $ia = elgg_set_ignore_access(true);
  15. elgg_register_plugin_hook_handler('permissions_check', 'all', 'elgg_override_permissions');
  16. elgg_register_plugin_hook_handler('container_permissions_check', 'all', 'elgg_override_permissions');
  17. // Make sure that entries for disabled entities also get upgraded
  18. $access_status = access_get_show_hidden_status();
  19. access_show_hidden_entities(true);
  20. $db_prefix = elgg_get_config('dbprefix');
  21. $image_subtype_id = get_subtype_id('object', 'image');
  22. $album_subtype_id = get_subtype_id('object', 'album');
  23. $tidypics_batch_subtype_id = get_subtype_id('object', 'tidypics_batch');
  24. // Begin of Update PART 1/5:
  25. // After Elgg core updated the comment annotations to entities there's some additional maintenance necessary to get the Tidypics specific river views back
  26. //
  27. // Upgrade view paths in river table rows for comments added on Tidypics images after Elgg core converted comment annotations to entities
  28. //
  29. // Get river entries for comments added to Tidypics images
  30. $batch = new ElggBatch('elgg_get_river', array(
  31. 'type' => 'object',
  32. 'subtype' => 'comment',
  33. 'action_type' => 'comment',
  34. 'joins' => array("JOIN {$db_prefix}entities im ON im.guid = rv.target_guid"),
  35. 'wheres' => array("im.subtype = $image_subtype_id"),
  36. 'limit' => false
  37. ));
  38. // now collect the ids of the river items that need to be upgraded
  39. $river_entry_ids = array();
  40. foreach ($batch as $river_entry) {
  41. $river_entry_ids[] = $river_entry->id;
  42. }
  43. // and finally update the rows in the river table if there are any rows to update
  44. if ($river_entry_ids) {
  45. $river_entry_ids = implode(', ', $river_entry_ids);
  46. $query = "UPDATE {$db_prefix}river
  47. SET view = 'river/object/comment/image'
  48. WHERE id IN ($river_entry_ids)";
  49. update_data($query);
  50. }
  51. //
  52. // Upgrade view paths in river table rows for comments added on Tidypics albums after Elgg core converted comment annotations to entities
  53. //
  54. // Get river entries for comments added to Tidypics albums
  55. $batch = new ElggBatch('elgg_get_river', array(
  56. 'type' => 'object',
  57. 'subtype' => 'comment',
  58. 'action_type' => 'comment',
  59. 'joins' => array("JOIN {$db_prefix}entities al ON al.guid = rv.target_guid"),
  60. 'wheres' => array("al.subtype = $album_subtype_id"),
  61. 'limit' => false
  62. ));
  63. // now collect the ids of the river items that need to be upgraded
  64. $river_entry_ids = array();
  65. foreach ($batch as $river_entry) {
  66. $river_entry_ids[] = $river_entry->id;
  67. }
  68. // and finally update the rows in the river table if there are any rows to update
  69. if ($river_entry_ids) {
  70. $river_entry_ids = implode(', ', $river_entry_ids);
  71. $query = "UPDATE {$db_prefix}river
  72. SET view = 'river/object/comment/album'
  73. WHERE id IN ($river_entry_ids)";
  74. update_data($query);
  75. }
  76. // End of Update PART 1/5
  77. // Begin of Update PART 2/5:
  78. // Get river entries for comments added to Tidypics batches
  79. // and update these entries together with the comment entities
  80. // to point to the image (if only 1 image was uploaded) or album
  81. $batch = new ElggBatch('elgg_get_river', array(
  82. 'type' => 'object',
  83. 'subtype' => 'comment',
  84. 'action_type' => 'comment',
  85. 'joins' => array("JOIN {$db_prefix}entities ba ON ba.guid = rv.target_guid"),
  86. 'wheres' => array("ba.subtype = $tidypics_batch_subtype_id"),
  87. 'limit' => false
  88. ));
  89. foreach ($batch as $river_entry) {
  90. // Get the batch entity
  91. $tidypics_batch = get_entity($river_entry->target_guid);
  92. // Get images related to this batch
  93. $images = elgg_get_entities_from_relationship(array(
  94. 'relationship' => 'belongs_to_batch',
  95. 'relationship_guid' => $tidypics_batch->getGUID(),
  96. 'inverse_relationship' => true,
  97. 'type' => 'object',
  98. 'subtype' => 'image',
  99. 'limit' => false
  100. ));
  101. // for more than a single image uploaded in the batch move the comment to the album
  102. if (count($images) > 1) {
  103. $album = get_entity($tidypics_batch->container_guid);
  104. // fix river entry
  105. $query = "
  106. UPDATE {$db_prefix}river
  107. SET access_id = {$album->access_id},
  108. view = 'river/object/comment/album',
  109. target_guid = {$album->guid}
  110. WHERE id = {$river_entry->id}
  111. ";
  112. update_data($query);
  113. // and fix comment entity
  114. $comment_entity = get_entity($river_entry->object_guid);
  115. $query = "
  116. UPDATE {$db_prefix}entities
  117. SET container_guid = {$album->guid},
  118. access_id = {$album->access_id}
  119. WHERE guid = {$comment_entity->guid}
  120. ";
  121. update_data($query);
  122. // for a batch with only a single image uploaded move the comment to this image
  123. } else {
  124. // fix river entry
  125. $query = "
  126. UPDATE {$db_prefix}river
  127. SET access_id = {$images[0]->access_id},
  128. view = 'river/object/comment/image',
  129. access_id = {$images[0]->access_id},
  130. target_guid = {$images[0]->guid}
  131. WHERE id = {$river_entry->id}
  132. ";
  133. update_data($query);
  134. // and fix comment entity
  135. $comment_entity = get_entity($river_entry->object_guid);
  136. $query = "
  137. UPDATE {$db_prefix}entities
  138. SET container_guid = {$images[0]->guid},
  139. access_id = {$images[0]->access_id}
  140. WHERE guid = {$comment_entity->guid}
  141. ";
  142. update_data($query);
  143. }
  144. }
  145. // End of Update Part 2/5
  146. // Begin of Update PART 3/5:
  147. // Get comment entities (former annotations) added to Tidypics batches with a Tidypics 1.8.1betaXX version
  148. // and update these entries together with their river entries to be assigned
  149. // to the image (if only 1 image was uploaded) or album
  150. $batch = new ElggBatch('elgg_get_entities', array(
  151. 'type' => 'object',
  152. 'subtype' => 'comment',
  153. 'joins' => array("JOIN {$db_prefix}entities ba ON ba.guid = e.container_guid"),
  154. 'wheres' => array("ba.subtype = $tidypics_batch_subtype_id"),
  155. 'limit' => false
  156. ));
  157. foreach ($batch as $comment_entity_entry) {
  158. // Get the batch entity
  159. $tidypics_batch = get_entity($comment_entity_entry->container_guid);
  160. // Get images related to this batch
  161. $images = elgg_get_entities_from_relationship(array(
  162. 'relationship' => 'belongs_to_batch',
  163. 'relationship_guid' => $tidypics_batch->getGUID(),
  164. 'inverse_relationship' => true,
  165. 'type' => 'object',
  166. 'subtype' => 'image',
  167. 'limit' => false
  168. ));
  169. // for more than a single image uploaded in the batch move the comment to the album
  170. if (count($images) > 1) {
  171. $album = get_entity($tidypics_batch->container_guid);
  172. // fix river entry
  173. $query = "
  174. UPDATE {$db_prefix}river
  175. SET access_id = {$album->access_id},
  176. view = 'river/object/comment/album',
  177. target_guid = {$album->guid}
  178. WHERE object_guid = {$comment_entity_entry->guid}
  179. ";
  180. update_data($query);
  181. // and fix comment entity
  182. $query = "
  183. UPDATE {$db_prefix}entities
  184. SET container_guid = {$album->guid},
  185. access_id = {$album->access_id}
  186. WHERE guid = {$comment_entity_entry->guid}
  187. ";
  188. update_data($query);
  189. // for a batch with only a single image uploaded move the comment to this image
  190. } else {
  191. // fix river entry
  192. $query = "
  193. UPDATE {$db_prefix}river
  194. SET access_id = {$images[0]->access_id},
  195. view = 'river/object/comment/image',
  196. target_guid = {$images[0]->guid}
  197. WHERE object_guid = {$comment_entity_entry->guid}
  198. ";
  199. update_data($query);
  200. // and fix comment entity
  201. $query = "
  202. UPDATE {$db_prefix}entities
  203. SET container_guid = {$images[0]->guid},
  204. access_id = {$images[0]->access_id}
  205. WHERE guid = {$comment_entity_entry->guid}
  206. ";
  207. update_data($query);
  208. }
  209. }
  210. // End of Update PART 3/5
  211. // Begin of Update PART 4/5:
  212. // Check for album comments without a river entry and delete them (for each comment made on the activity page
  213. // on an image upload a second comment annotation (on Elgg 1.9 it has been converted to an entity) was created
  214. // with Tidypics 1.8.1betaXX that showed up on the corresponding album page while the first comment annotation
  215. // was only visible below the river entry on the activity page. This second comment annotation is now no longer
  216. // necessary and should be removed to avoid comments appearing twice on album pages)
  217. // ATTENTION: this part of the upgrade script will remove ALL album comment entities (formerly annotations on Elgg 1.9)
  218. // that don't a corresponding river entry. If you removed the river entries by any means or prevented them from getting
  219. // created in the first place you might not want this part of the upgrade to be executed. Then you should comment out part 4
  220. // of the upgrade. But you will most likely end with double comments on album pages then.
  221. $batch = new ElggBatch('elgg_get_entities', array(
  222. 'type' => 'object',
  223. 'subtype' => 'comment',
  224. 'joins' => array("JOIN {$db_prefix}entities al ON al.guid = e.container_guid"),
  225. 'wheres' => array("al.subtype = $album_subtype_id"),
  226. 'limit' => false
  227. ));
  228. foreach ($batch as $album_comment) {
  229. $river_entry_count = elgg_get_river(array(
  230. 'type' => 'object',
  231. 'subtype' => 'comment',
  232. 'action_type' => 'comment',
  233. 'object_guid' => $album_comment->guid,
  234. 'target_guid' => $album_comment->container_guid,
  235. 'count' => true
  236. ));
  237. if($river_entry_count < 1) {
  238. $album_comment->delete();
  239. }
  240. }
  241. // End of Update Part 4/5
  242. // Begin of Update Part 5/5
  243. // Update likes made to Tidypics batches and assign them either to the image uploaded (if only one) or the album
  244. $batch = new ElggBatch('elgg_get_annotations', array(
  245. 'annotation_name' => 'likes',
  246. 'joins' => array("JOIN {$db_prefix}entities li ON li.guid = n_table.entity_guid"),
  247. 'wheres' => array("li.subtype = $tidypics_batch_subtype_id"),
  248. 'limit' => false
  249. ));
  250. foreach ($batch as $like_entry) {
  251. // Get the batch entity
  252. $tidypics_batch = get_entity($like_entry->entity_guid);
  253. // Get images related to this batch
  254. $images = elgg_get_entities_from_relationship(array(
  255. 'relationship' => 'belongs_to_batch',
  256. 'relationship_guid' => $tidypics_batch->getGUID(),
  257. 'inverse_relationship' => true,
  258. 'type' => 'object',
  259. 'subtype' => 'image',
  260. 'limit' => false
  261. ));
  262. // move the like to the album if more than a single image was uploaded in this batch
  263. if (count($images) > 1) {
  264. $album = get_entity($tidypics_batch->container_guid);
  265. // in case the same user who liked the Tidypics batch entry on the activity already
  266. // liked the album delete the annotation to prevent double likes
  267. if (elgg_annotation_exists($album->guid, 'likes', $like_entry->owner_guid)) {
  268. elgg_delete_annotation_by_id($like_entry->id);
  269. } else {
  270. // fix annotation
  271. $query = "
  272. UPDATE {$db_prefix}annotations
  273. SET entity_guid = {$album->guid},
  274. access_id = {$album->access_id}
  275. WHERE id = {$like_entry->id}
  276. ";
  277. update_data($query);
  278. }
  279. // move the like to the image if only a single image was uploaded in the batch
  280. } else {
  281. // in case the same user who liked the Tidypics batch entry on the activity already
  282. // liked the image delete the annotation to prevent double likes
  283. if (elgg_annotation_exists($images[0]->guid, 'likes', $like_entry->owner_guid)) {
  284. elgg_delete_annotation_by_id($like_entry->id);
  285. } else {
  286. // fix annotation
  287. $query = "
  288. UPDATE {$db_prefix}annotations
  289. SET entity_guid = {$images[0]->guid},
  290. access_id = {$images[0]->access_id}
  291. WHERE id = {$like_entry->id}
  292. ";
  293. update_data($query);
  294. }
  295. }
  296. }
  297. // End of Update Part 5/5
  298. elgg_set_ignore_access($ia);
  299. access_show_hidden_entities($access_status);