Skip to content

Commit 7b50ba4

Browse files
committed
added: database seeder
1 parent bd44197 commit 7b50ba4

File tree

2 files changed

+224
-5
lines changed

2 files changed

+224
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
3+
namespace ColdTrick\EntityViewCounter;
4+
5+
use Elgg\Database\Clauses\OrderByClause;
6+
use Elgg\Database\QueryBuilder;
7+
use Elgg\Database\Seeds\Seed;
8+
use Elgg\Database\Update;
9+
10+
/**
11+
* Seed views with the seeded entities
12+
*/
13+
class Seeder extends Seed {
14+
15+
/**
16+
* @var array supported types for seeding
17+
*/
18+
protected $supported_types;
19+
20+
/**
21+
* {@inheritDoc}
22+
*/
23+
public function seed() {
24+
$this->advance($this->getCount());
25+
26+
$exclude = [];
27+
while ($this->getCount() < $this->limit) {
28+
$entity = $this->getRandomEntity($exclude);
29+
if (!$entity instanceof \ElggEntity) {
30+
// no more to fetch
31+
break;
32+
}
33+
34+
$user_guids = [];
35+
for ($i = 0; $i < $this->faker()->numberBetween(5, 25); $i++) {
36+
$user = $this->getRandomUser($user_guids);
37+
$user_guids[] = $user->guid;
38+
39+
$annotation_id = $entity->annotate(ENTITY_VIEW_COUNTER_ANNOTATION_NAME, '__faker', ACCESS_PUBLIC, $user->guid);
40+
if (empty($annotation_id)) {
41+
continue;
42+
}
43+
44+
$annotation = elgg_get_annotation_from_id($annotation_id);
45+
$this->backdateView($annotation, $entity);
46+
}
47+
48+
// cache the count for faster response
49+
$entity->entity_view_count = $entity->countAnnotations(ENTITY_VIEW_COUNTER_ANNOTATION_NAME);
50+
51+
$this->advance();
52+
}
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
public function unseed() {
59+
$options = [
60+
'annotation_name_value_pairs' => [
61+
[
62+
'name' => ENTITY_VIEW_COUNTER_ANNOTATION_NAME,
63+
'value' => '__faker',
64+
'case_sensitive' => false,
65+
'type' => ELGG_VALUE_STRING,
66+
],
67+
],
68+
'limit' => false,
69+
'batch' => true,
70+
'batch_inc_offset' => false,
71+
'count' => true,
72+
];
73+
74+
// set the correct count on the progressbar as it's wrong by default
75+
$count = elgg_get_annotations($options);
76+
unset($options['count']);
77+
78+
$this->progress->setMaxSteps($count);
79+
80+
/* @var $annotations \ElggBatch */
81+
$annotations = elgg_get_annotations($options);
82+
83+
/* @var $annotation \ElggAnnotation */
84+
foreach ($annotations as $annotation) {
85+
if ($annotation->delete()) {
86+
$this->log("Deleted entity view {$annotation->id}");
87+
} else {
88+
$this->log("Failed to delete entity view {$annotation->id}");
89+
$annotations->reportFailure();
90+
continue;
91+
}
92+
93+
$this->advance();
94+
}
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public static function getType(): string {
101+
return 'entity_view_counter';
102+
}
103+
104+
/**
105+
* {@inheritDoc}
106+
*/
107+
protected function getCountOptions(): array {
108+
return [
109+
'wheres' => [
110+
function (QueryBuilder $qb, $main_alias) {
111+
// can't use annotation_name_value_pairs because of join bug
112+
// @see https://github.com/Elgg/Elgg/issues/14405
113+
$ann = $qb->joinAnnotationTable($main_alias);
114+
115+
return $qb->merge([
116+
$qb->compare("{$ann}.name", '=', ENTITY_VIEW_COUNTER_ANNOTATION_NAME, ELGG_VALUE_STRING),
117+
$qb->compare("{$ann}.value", '=', '__faker', ELGG_VALUE_STRING),
118+
]);
119+
},
120+
],
121+
];
122+
}
123+
124+
/**
125+
* Get the supported types to seed view for
126+
*
127+
* @return array
128+
*/
129+
protected function getSupportedTypes(): array {
130+
if (isset($this->supported_types)) {
131+
return $this->supported_types;
132+
}
133+
134+
$setting = elgg_get_plugin_setting('entity_types', 'entity_view_counter');
135+
if (!empty($setting)) {
136+
$filtered = [];
137+
$setting = json_decode($setting, true);
138+
foreach ($setting as $type => $subtypes) {
139+
if (empty($subtypes) || !is_array($subtypes)) {
140+
continue;
141+
}
142+
143+
$filtered[$type] = [];
144+
foreach ($subtypes as $subtype => $enabled) {
145+
if (empty($enabled)) {
146+
continue;
147+
}
148+
149+
$filtered[$type][] = $subtype;
150+
}
151+
}
152+
153+
$this->supported_types = $filtered;
154+
} else {
155+
$this->supported_types = elgg_entity_types_with_capability('searchable');
156+
}
157+
158+
return $this->supported_types;
159+
}
160+
161+
/**
162+
* Get a random entity to seed views on
163+
*
164+
* @param array $excluded_guids excluded GUIDs (previously seeded)
165+
*
166+
* @return null|\ElggEntity
167+
*/
168+
protected function getRandomEntity(array $excluded_guids = []): ?\ElggEntity {
169+
$excluded_guids[] = 0;
170+
171+
$entities = elgg_get_entities([
172+
'type_subtype_pairs' => $this->getSupportedTypes(),
173+
'metadata_names' => ['__faker'],
174+
'wheres' => [
175+
function (QueryBuilder $qb, $main_alias) use ($excluded_guids) {
176+
return $qb->compare("{$main_alias}.guid", 'NOT IN', $excluded_guids, ELGG_VALUE_GUID);
177+
},
178+
function (QueryBuilder $qb, $main_alias) {
179+
$ann = $qb->subquery('annotations');
180+
$ann->select('entity_guid')
181+
->where($qb->compare('name', '=', ENTITY_VIEW_COUNTER_ANNOTATION_NAME, ELGG_VALUE_STRING))
182+
->andWhere($qb->compare('value', '=', '__faker', ELGG_VALUE_STRING));
183+
184+
return $qb->compare("{$main_alias}.guid", 'NOT IN', $ann->getSQL());
185+
}
186+
],
187+
'limit' => 1,
188+
'order_by' => new OrderByClause('RAND()', null),
189+
]);
190+
191+
return empty($entities) ? null : $entities[0];
192+
}
193+
194+
/**
195+
* Backdate the view
196+
*
197+
* @param \ElggAnnotation $annotation view annotation
198+
* @param \ElggEntity $entity viewed entity
199+
*
200+
* @return void
201+
*/
202+
protected function backdateView(\ElggAnnotation $annotation, \ElggEntity $entity): void {
203+
$since = $this->create_since;
204+
$this->setCreateSince($entity->time_created);
205+
206+
$update = Update::table('annotations');
207+
$update->set('time_created', $update->param($this->getRandomCreationTimestamp(), ELGG_VALUE_TIMESTAMP))
208+
->where($update->compare('id', '=', $annotation->id, ELGG_VALUE_ID));
209+
210+
elgg()->db->updateData($update);
211+
212+
$this->setCreateSince($since);
213+
}
214+
}

elgg-plugin.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@
2929
'\ColdTrick\EntityViewCounter\Permissions::canAnnotate' => [],
3030
],
3131
],
32-
'view_vars' => [
33-
'object/elements/imprint/contents' => [
34-
'\ColdTrick\EntityViewCounter\Views::addImprint' => [
35-
'priority' => 600,
36-
],
32+
'seeds' => [
33+
'database' => [
34+
'\ColdTrick\EntityViewCounter\Seeder::register' => ['priority' => 600],
3735
],
3836
],
3937
'setting' => [
4038
'plugin' => [
4139
'\ColdTrick\EntityViewCounter\Settings::saveSettingEntityTypes' => [],
4240
],
4341
],
42+
'view_vars' => [
43+
'object/elements/imprint/contents' => [
44+
'\ColdTrick\EntityViewCounter\Views::addImprint' => [
45+
'priority' => 600,
46+
],
47+
],
48+
],
4449
],
4550
'view_extensions' => [
4651
'core/settings/statistics' => [

0 commit comments

Comments
 (0)