Skip to content

Commit

Permalink
Merge pull request #11254 from jdalsem/drop-group-subtable
Browse files Browse the repository at this point in the history
Drop group subtable
  • Loading branch information
jdalsem committed Oct 9, 2017
2 parents 96b2fe1 + 67eaae2 commit fab0a49
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 71 deletions.
4 changes: 3 additions & 1 deletion docs/guides/upgrading.rst
Expand Up @@ -107,6 +107,7 @@ All the functions in ``engine/lib/deprecated-1.10.php`` were removed. See https:
* ``file_delete``: Use ``ElggFile->deleteIcon()``
* ``get_default_filestore``
* ``get_site_entity_as_row``
* ``get_group_entity_as_row``
* ``garbagecollector_orphaned_metastrings``
* ``groups_setup_sidebar_menus``
* ``set_default_filestore``
Expand Down Expand Up @@ -241,7 +242,8 @@ settings.php, or computed by Symphony Request.
Entity Subtable Changes
-----------------------

The subtable ``sites_entity`` for ``ElggSite`` no longer exists. All attributes have been moved to metadata.
The subtable ``sites_entity`` for ``ElggSite`` no longer exists. All attributes have been moved to metadata.
The subtable ``groups_entity`` for ``ElggGroup`` no longer exists. All attributes have been moved to metadata.
If you have custom queries referencing this table you need to update them.

Custom class loading
Expand Down
7 changes: 4 additions & 3 deletions engine/classes/Elgg/Database/EntityTable.php
Expand Up @@ -747,7 +747,6 @@ protected function autoJoinTables(array $options) {
// Each class must have a static getExternalAttributes() : array
'object' => 'ElggObject',
'user' => 'ElggUser',
'group' => 'ElggGroup',
];

// We use reset() because $options['types'] may not have a numeric key
Expand All @@ -757,6 +756,9 @@ protected function autoJoinTables(array $options) {
// clause may reference "guid", which MySQL will complain about being ambiguous
try {
$attributes = \ElggEntity::getExtraAttributeDefaults($type);
if (empty($attributes)) {
return $options;
}
} catch (\Exception $e) {
$this->logger->error("Unrecognized type: $type");
return $options;
Expand Down Expand Up @@ -791,7 +793,6 @@ public function fetchFromSql($sql, \ElggBatch $batch = null) {
$types_to_optimize = [
'object' => 'title',
'user' => 'password_hash',
'group' => 'name',
];

$rows = $this->db->getData($sql);
Expand Down Expand Up @@ -1216,7 +1217,7 @@ public function getEntityAttributeWhereSql(array $options = []) {
}

// @todo the types should be defined somewhere (as constant on \ElggEntity?)
if (!in_array($type, ['group', 'object', 'user'])) {
if (!in_array($type, ['object', 'user'])) {
throw new InvalidArgumentException("Invalid type '$type' passed to elgg_get_entities_from_attributes()");
}

Expand Down
8 changes: 4 additions & 4 deletions engine/classes/Elgg/Database/Seeds/Seeding.php
Expand Up @@ -206,12 +206,12 @@ public function createGroup(array $attributes = [], array $metadata = [], array
$metadata['membership'] = ACCESS_PUBLIC;
}

if (empty($attributes['name'])) {
$attributes['name'] = $this->faker()->sentence();
if (empty($metadata['name'])) {
$metadata['name'] = $this->faker()->sentence();
}

if (empty($attributes['description'])) {
$attributes['description'] = $this->faker()->text($this->faker()->numberBetween(500, 1000));
if (empty($metadata['description'])) {
$metadata['description'] = $this->faker()->text($this->faker()->numberBetween(500, 1000));
}

if (empty($attributes['owner_guid'])) {
Expand Down
9 changes: 3 additions & 6 deletions engine/classes/ElggEntity.php
Expand Up @@ -1592,7 +1592,7 @@ private function getSecondaryTableColumns() {
return ['name', 'username', 'password_hash', 'email', 'language'];
}
if ($this instanceof ElggGroup) {
return ['name', 'description'];
return [];
}
if ($this instanceof ElggSite) {
return [];
Expand Down Expand Up @@ -1629,10 +1629,7 @@ public static function getExtraAttributeDefaults($type) {
'prev_last_login' => null,
];
case 'group':
return [
'name' => null,
'description' => null,
];
return [];
case 'site':
return [];
}
Expand Down Expand Up @@ -1954,7 +1951,7 @@ public function delete($recursive = true) {

$deleted = $this->getDatabase()->deleteData($sql, $params);

if ($deleted && in_array($this->type, ['object', 'user', 'group'])) {
if ($deleted && in_array($this->type, ['object', 'user'])) {
// delete from type-specific subtable
$sql = "
DELETE FROM {$dbprefix}{$this->type}s_entity
Expand Down
17 changes: 0 additions & 17 deletions engine/lib/group.php
Expand Up @@ -8,23 +8,6 @@
* @subpackage DataModel.Group
*/

use Elgg\Project\Paths;

/**
* Get the group entity.
*
* @param int $guid GUID for a group
*
* @return array|false
* @access private
*/
function get_group_entity_as_row($guid) {
$guid = (int) $guid;

$prefix = _elgg_config()->dbprefix;
return get_data_row("SELECT * from {$prefix}groups_entity where guid=$guid");
}

/**
* Adds a group tool option
*
Expand Down
3 changes: 1 addition & 2 deletions engine/lib/river.php
Expand Up @@ -444,8 +444,7 @@ function _elgg_prefetch_river_entities(array $river_items) {
'limit' => 0,
'distinct' => false,

// Why specify? user containers are likely already loaded via the owners, and
// specifying groups allows ege() to auto-join the groups_entity table
// Why specify? user containers are likely already loaded via the owners
'type' => 'group',
]);
}
Expand Down
@@ -0,0 +1,54 @@
<?php

use Phinx\Migration\AbstractMigration;

class DropGroupsEntityTable extends AbstractMigration
{
/**
* Move groups_entity attributes to metadata
*/
public function up() {

if (!$this->hasTable('groups_entity') || !$this->hasTable('metadata')) {
return;
}

$prefix = $this->getAdapter()->getOption('table_prefix');
$cols = ['name', 'description'];

$groups_query = "SELECT * FROM {$prefix}groups_entity LIMIT 25";
while ($rows = $this->fetchAll($groups_query)) {
foreach ($rows as $row) {
foreach ($cols as $col) {

// remove existing metadata... attributes are more important
$this->execute("
DELETE FROM {$prefix}metadata
WHERE entity_guid = {$row['guid']} AND
name = '{$col}'
");

$this->insert('metadata', [
'entity_guid' => $row['guid'],
'name' => $col,
'value' => $row[$col],
'value_type' => 'text',
'owner_guid' => 0,
'access_id' => 2,
'time_created' => time(),
'enabled' => 'yes',
]);
}

// remove from groups so it does not get processed again in the next while loop
$this->execute("
DELETE FROM {$prefix}groups_entity
WHERE guid = {$row['guid']}
");
}
}

// all data migrated, so drop the table
$this->dropTable('groups_entity');
}
}
6 changes: 0 additions & 6 deletions engine/tests/classes/Elgg/Mocks/Database/EntityTable.php
Expand Up @@ -119,12 +119,6 @@ public function setup($guid, $type, $subtype, array $attributes = []) {
'prev_last_login' => null,
];
break;
case 'group' :
$external_attributes = [
'name' => null,
'description' => null,
];
break;
}

$map = array_merge($primary_attributes, $external_attributes, $attributes);
Expand Down
1 change: 0 additions & 1 deletion mod/garbagecollector/start.php
Expand Up @@ -102,7 +102,6 @@ function garbagecollector_entities() {

$tables = [
'object' => 'objects_entity',
'group' => 'groups_entity',
'user' => 'users_entity',
];

Expand Down
11 changes: 5 additions & 6 deletions mod/groups/views/default/groups/listing/alpha.php
@@ -1,14 +1,13 @@
<?php

/**
* Renders a list of groups ordered alphabetically
*/
$dbprefix = elgg_get_config('dbprefix');
echo elgg_list_entities([
echo elgg_list_entities_from_metadata([
'type' => 'group',
'joins' => ["JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid"],
'order_by' => 'ge.name',
'order_by_metadata' => [
'name' => 'name',
'direction' => 'ASC',
],
'full_view' => false,
'no_results' => elgg_echo('groups:none'),
'distinct' => false,
]);
1 change: 0 additions & 1 deletion mod/groups/views/default/groups/listing/newest.php
Expand Up @@ -7,5 +7,4 @@
'type' => 'group',
'full_view' => false,
'no_results' => elgg_echo('groups:none'),
'distinct' => false,
]);
8 changes: 4 additions & 4 deletions mod/groups/views/default/resources/groups/member.php
Expand Up @@ -22,16 +22,16 @@
elgg_register_title_button('groups', 'add', 'group');
}

$dbprefix = elgg_get_config('dbprefix');

$content = elgg_list_entities_from_relationship([
'type' => 'group',
'relationship' => 'member',
'relationship_guid' => elgg_get_page_owner_guid(),
'inverse_relationship' => false,
'full_view' => false,
'joins' => ["JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid"],
'order_by' => 'ge.name ASC',
'order_by_metadata' => [
'name' => 'name',
'direction' => 'ASC',
],
'no_results' => elgg_echo('groups:none'),
]);

Expand Down
10 changes: 5 additions & 5 deletions mod/groups/views/default/resources/groups/owner.php
Expand Up @@ -13,15 +13,15 @@
elgg_register_title_button('groups', 'add', 'group');
}

$dbprefix = elgg_get_config('dbprefix');
$content = elgg_list_entities([
$content = elgg_list_entities_from_metadata([
'type' => 'group',
'owner_guid' => elgg_get_page_owner_guid(),
'joins' => ["JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid"],
'order_by' => 'ge.name ASC',
'order_by_metadata' => [
'name' => 'name',
'direction' => 'ASC',
],
'full_view' => false,
'no_results' => elgg_echo('groups:none'),
'distinct' => false,
]);

$params = [
Expand Down
Expand Up @@ -12,26 +12,27 @@
// Returns a list of groups a user a member of, as well as any other groups
// the user is subscribed to
$dbprefix = elgg_get_config('dbprefix');
$options = [

echo elgg_list_entities_from_metadata([
'selects' => ['GROUP_CONCAT(ers.relationship) as relationships'],
'types' => 'group',
'joins' => [
"JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid",
"JOIN {$dbprefix}entity_relationships ers
ON e.guid = ers.guid_two AND ers.guid_one = $user->guid",
],
'wheres' => [
"ers.relationship = 'member' OR ers.relationship LIKE 'notify%'"
],
'group_by' => 'e.guid',
'order_by' => 'ge.name',
'order_by_metadata' => [
'name' => 'name',
'direction' => 'ASC',
],
'offset_key' => 'subscriptions_groups',
'item_view' => 'notifications/subscriptions/record',
'user' => $user,
'no_results' => elgg_echo('notifications:subscriptions:no_results'),
'limit' => max(20, elgg_get_config('default_limit')),
'list_class' => 'elgg-subscriptions',
'item_class' => 'elgg-subscription-record',
];

echo elgg_list_entities($options);
]);
39 changes: 30 additions & 9 deletions mod/search/search_hooks.php
Expand Up @@ -73,18 +73,39 @@ function search_groups_hook($hook, $type, $value, $params) {
$params['joins'] = (array) elgg_extract('joins', $params, []);
$params['wheres'] = (array) elgg_extract('wheres', $params, []);

$db_prefix = elgg_get_config('dbprefix');

$query = sanitise_string($params['query']);

$join = "JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid";
array_unshift($params['joins'], $join);
$query_parts = explode(' ', $query);
$db_prefix = elgg_get_config('dbprefix');

$params['joins'][] = "JOIN {$db_prefix}metadata md on e.guid = md.entity_guid";

$fields = ['name', 'description'];
$where = search_get_where_sql('ge', $fields, $params);
$params['wheres'][] = $where;

$wheres = [];
foreach ($fields as $field) {
$sublikes = [];
foreach ($query_parts as $query_part) {
$query_part = sanitise_string($query_part);
if (strlen($query_part) == 0) {
continue;
}
$sublikes[] = "(md.value LIKE '%{$query_part}%')";
}

if (empty($sublikes)) {
continue;
}

$wheres[] = "(md.name = '{$field}' AND (" . implode(' AND ', $sublikes) . "))";
}

if (!empty($wheres)) {
$params['wheres'][] = implode(' OR ', $wheres);
}


$params['count'] = true;

$count = elgg_get_entities($params);

// no need to continue if nothing here.
Expand All @@ -94,7 +115,7 @@ function search_groups_hook($hook, $type, $value, $params) {

$params['count'] = false;
if (isset($params['sort']) || !isset($params['order_by'])) {
$params['order_by'] = search_get_order_by_sql('e', 'ge', $params['sort'], $params['order']);
$params['order_by'] = search_get_order_by_sql('e', '', $params['sort'], $params['order']);
}
$entities = elgg_get_entities($params);

Expand Down Expand Up @@ -161,7 +182,7 @@ function search_users_hook($hook, $type, $value, $params) {
]);

$params['joins'] = array_merge($clauses['joins'], $params['joins']);
$md_where = "(({$clauses['wheres'][0]}) AND md.value LIKE '%$query%')";
$md_where = "(({$clauses['wheres'][0]}) AND an.value LIKE '%$query%')";

$params['wheres'][] = "(($where) OR ($md_where))";
} else {
Expand Down

0 comments on commit fab0a49

Please sign in to comment.