Skip to content

Commit

Permalink
Fixes Elgg#2146 Migrate comment annotations to ElggComment objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juho Jaakkola authored and mrclay committed Jun 23, 2013
1 parent 94d2d7b commit a065de0
Show file tree
Hide file tree
Showing 23 changed files with 330 additions and 293 deletions.
7 changes: 7 additions & 0 deletions actions/admin/site/update_advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
$allow_registration = (bool) get_input('allow_registration', FALSE);
set_config('allow_registration', $allow_registration, $site->getGUID());

// allow commenting?
if (get_input('comments_enabled', FALSE)) {
set_config('comments_enabled', TRUE, $site->getGUID());
} else {
set_config('comments_enabled', FALSE, $site->getGUID());
}

// setup walled garden
$walled_garden = (bool) get_input('walled_garden', FALSE);
set_config('walled_garden', $walled_garden, $site->getGUID());
Expand Down
42 changes: 21 additions & 21 deletions actions/comments/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

$user = elgg_get_logged_in_user_entity();

$annotation = create_annotation($entity->guid,
'generic_comment',
$comment_text,
"",
$user->guid,
$entity->access_id);

// tell user annotation posted
if (!$annotation) {
$comment = new ElggComment();
$comment->description = $comment_text;
$comment->owner_guid = $user->getGUID();
$comment->container_guid = $entity->getGUID();
$comment->access_id = $entity->access_id;

// tell user that comment was posted
if (!$comment->save()) {
register_error(elgg_echo("generic_comment:failure"));
forward(REFERER);
}
Expand All @@ -40,23 +39,24 @@
if ($entity->owner_guid != $user->guid) {

notify_user($entity->owner_guid,
$user->guid,
elgg_echo('generic_comment:email:subject'),
elgg_echo('generic_comment:email:body', array(
$entity->title,
$user->name,
$comment_text,
$entity->getURL(),
$user->name,
$user->getURL()
))
);
$user->guid,
elgg_echo('generic_comment:email:subject'),
elgg_echo('generic_comment:email:body', array(
$entity->title,
$user->name,
$comment_text,
$entity->getURL(),
$user->name,
$user->getURL()
))
);
}

system_message(elgg_echo("generic_comment:posted"));

//add to river
add_to_river('river/annotation/generic_comment/create', 'comment', $user->guid, $entity->guid, "", 0, $annotation);
add_to_river('river/object/comment/create', 'comment', $user->guid,
$entity->getGUID(), '', 0, 0, $comment->getGUID());

// Forward to the page the action occurred on
forward(REFERER);
22 changes: 13 additions & 9 deletions actions/comments/delete.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?php
/**
* Elgg delete comment action
* Delete comment entity
*
* @package Elgg
* @package Elgg.Core
* @subpackage Comments
*/

// Make sure we can get the comment in question
$annotation_id = (int) get_input('annotation_id');
$comment = elgg_get_annotation_from_id($annotation_id);
if ($comment && $comment->canEdit()) {
$comment->delete();
system_message(elgg_echo("generic_comment:deleted"));
$comment_guid = get_input('guid');
$comment = get_entity($comment_guid);

if (elgg_instanceof($comment, 'object', 'comment') && $comment->canEdit()) {
if ($comment->delete()) {
system_message(elgg_echo('generic_comment:deleted'));
} else {
register_error(elgg_echo('generic_comment:notdeleted'));
}
} else {
register_error(elgg_echo("generic_comment:notdeleted"));
register_error(elgg_echo('generic_comment:notfound'));
}

forward(REFERER);
17 changes: 17 additions & 0 deletions engine/classes/ElggComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* ElggComment
*
* @package Elgg.Core
* @subpackage Comments
* @since 1.9.0
*/
class ElggComment extends ElggObject {

/** @override */
protected function initializeAttributes() {
parent::initializeAttributes();

$this->attributes['subtype'] = "comment";
}
}
11 changes: 10 additions & 1 deletion engine/classes/ElggEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,12 @@ public function countComments() {
if (is_int($num)) {
return $num;
} else {
return $this->getAnnotationCalculation('generic_comment', 'count');
return elgg_get_entities(array(
'type' => 'object',
'subtype' => 'comment',
'container_guid' => $this->getGUID(),
'count' => true,
));
}
}

Expand Down Expand Up @@ -1089,6 +1094,10 @@ public function canWriteToContainer($user_guid = 0, $type = 'all', $subtype = 'a
* @return bool
*/
public function canComment($user_guid = 0) {
if (!elgg_get_config('comments_enabled')) {
return false;
}

if ($user_guid == 0) {
$user_guid = elgg_get_logged_in_user_guid();
}
Expand Down
87 changes: 87 additions & 0 deletions engine/lib/comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Elgg comments
*
* @package Elgg.Core
* @subpackage Comments
* @since 1.9
*/

/**
* Comments initialisation function
*
* @return void
* @access private
*/
function comments_init() {
// Register entity type
elgg_register_plugin_hook_handler('register', 'menu:entity', 'elgg_comment_setup_entity_menu', 900);

elgg_register_entity_url_handler('object', 'comment', 'comments_url_handler');
}

/**
* Setup the menu shown with a comment
*
* @return array
*/
function elgg_comment_setup_entity_menu($hook, $type, $return, $params) {
if (elgg_in_context('widgets')) {
return $return;
}

$entity = $params['entity'];
if (!elgg_instanceof($entity, 'object', 'comment')) {
return $return;
}

// Remove edit link and access level from the menu
foreach($return as $key => $item) {
if (in_array($item->getName(), array('access', 'edit'))) {
unset($return[$key]);
}
}

return $return;
}

/**
* Allow users to comment entities not owned by them.
*
* Object being commented is used as the container of the comment so
* permission check must be overridden if user isn't the owner of the object.
*
* @todo Is this the correct way?
*/
function comments_container_permissions_override ($hook, $type, $return, $params) {
if ($params['subtype'] === 'comment') {
return true;
}

return $return;
}

/**
* Format and return the URL for comments.
*
* The url consists of container's url and comment's anchor.
*
* @param ElggObject $entity Comment object
* @return string URL of comment.
*/
function comments_url_handler($entity) {
if (!$entity->getOwnerEntity()) {
// default to a standard view if no owner.
return FALSE;
}

$container_url = $entity->getContainerEntity()->getURL();

$comment_anchor = "comment-{$entity->getGUID()}";

return "$container_url#$comment_anchor";
}

elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'comments_container_permissions_override');

elgg_register_event_handler('init', 'system', 'comments_init', 0);
32 changes: 3 additions & 29 deletions engine/lib/navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ function _elgg_river_menu_setup($hook, $type, $return, $params) {
$item = $params['item'];
/* @var ElggRiverItem $item */
$object = $item->getObjectEntity();
// comments and non-objects cannot be commented on or liked
if (!elgg_in_context('widgets') && $item->annotation_id == 0) {
// non-objects cannot be commented on or liked
// comments cannot be commented
if (!elgg_in_context('widgets') && $item->annotation_id == 0 && $item->action_type != 'comment') {
// comments
if ($object->canComment()) {
$options = array(
Expand Down Expand Up @@ -476,32 +477,6 @@ function _elgg_widget_menu_setup($hook, $type, $return, $params) {
return $return;
}

/**
* Adds a delete link to "generic_comment" annotations
* @access private
*/
function _elgg_annotation_menu_setup($hook, $type, $return, $params) {
$annotation = $params['annotation'];
/* @var ElggAnnotation $annotation */

if ($annotation->name == 'generic_comment' && $annotation->canEdit()) {
$url = elgg_http_add_url_query_elements('action/comments/delete', array(
'annotation_id' => $annotation->id,
));

$options = array(
'name' => 'delete',
'href' => $url,
'text' => "<span class=\"elgg-icon elgg-icon-delete\"></span>",
'confirm' => elgg_echo('deleteconfirm'),
'encode_text' => false
);
$return[] = ElggMenuItem::factory($options);
}

return $return;
}

/**
* Add the register and forgot password links to login menu
* @access private
Expand Down Expand Up @@ -537,7 +512,6 @@ function _elgg_nav_init() {
elgg_register_plugin_hook_handler('register', 'menu:river', '_elgg_river_menu_setup');
elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
elgg_register_plugin_hook_handler('register', 'menu:annotation', '_elgg_annotation_menu_setup');
elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');

elgg_register_menu_item('footer', ElggMenuItem::factory(array(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Elgg 1.9.0-dev upgrade 2013010400
* comments_to_entities
*
* Convert comment annotations to entities
*/

// Register subtype and class for comments
update_subtype('object', 'comment', 'ElggComment');

// Before 1.9 there was no option for disabling comments so enable them by default
set_config('comments_enabled', TRUE);

$ia = elgg_set_ignore_access(true);
$batch = new ElggBatch('elgg_get_annotations', array(
'annotation_names' => 'generic_comment',
'limit' => false,
));
$batch->setIncrementOffset(false);

$db_prefix = elgg_get_config('dbprefix');

// Create a new object for each annotation
foreach ($batch as $annotation) {
$object = new ElggComment();
$object->owner_guid = $annotation->owner_guid;
$object->container_guid = $annotation->entity_guid;
$object->description = $annotation->value;
$object->access_id = $annotation->access_id;
$object->save();

// We need to save once before setting time_created
$object->time_created = $annotation->time_created;
$object->save();

$guid = $object->getGUID();

/**
* Update the entry in river table for this comment
*
* - Update the view path
* - Remove annotation id
* - Save comment guid to the target_guid column
*/
$query = "UPDATE {$db_prefix}river
SET view='river/object/comment/create', annotation_id=0, target_guid=$guid
WHERE action_type='comment' AND annotation_id={$annotation->id}";
update_data($query);

// Delete the annotation
$annotation->delete();
}
elgg_set_ignore_access($ia);
4 changes: 4 additions & 0 deletions engine/lib/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,10 @@ function elgg_view_friendly_time($time) {
* @return string|false Rendered comments or false on failure
*/
function elgg_view_comments($entity, $add_comment = true, array $vars = array()) {
if (!elgg_get_config('comments_enabled')) {
return false;
}

if (!($entity instanceof ElggEntity)) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions engine/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
'admin.php',
'annotations.php',
'cache.php',
'comments.php',
'configuration.php',
'cron.php',
'database.php',
Expand Down
5 changes: 5 additions & 0 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,9 @@
'installation:systemcache:description' => "The system cache decreases the loading time of the Elgg engine by caching data to files.",
'installation:systemcache:label' => "Use system cache (recommended)",

'installation:comments:description' => "Allow users to comment on other users' content. (Some content types may allow content owner to disable commenting even if this setting is enabled.)",
'installation:comments:label' => "Enable comments",

'upgrading' => 'Upgrading...',
'upgrade:db' => 'Your database was upgraded.',
'upgrade:core' => 'Your Elgg installation was upgraded.',
Expand Down Expand Up @@ -1114,6 +1117,7 @@
*/

'comments:count' => "%s comments",
'item:object:comment' => 'Comments',

'river:comment:object:default' => '%s commented on %s',

Expand All @@ -1130,6 +1134,7 @@
'generic_comment:none' => 'No comments',
'generic_comment:title' => 'Comment by %s',
'generic_comment:on' => '%s on %s',
'generic_comments:latest:posted' => 'posted a',

'generic_comment:email:subject' => 'You have a new comment!',
'generic_comment:email:body' => "You have a new comment on your item \"%s\" from %s. It reads:
Expand Down
Loading

0 comments on commit a065de0

Please sign in to comment.