Skip to content

Commit 35949ed

Browse files
committed
WIP more cleanups
1 parent a1d21af commit 35949ed

9 files changed

+79
-18
lines changed

classes/check/tagging_status.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3030
*/
3131
class tagging_status extends check {
32-
// TODO action link.
32+
/**
33+
* Link to ObjectFS settings page.
34+
*
35+
* @return \action_link|null
36+
*/
37+
public function get_action_link(): ?\action_link {
38+
$url = new \moodle_url('/admin/category.php', ['category' => 'tool_objectfs']);
39+
return new \action_link($url, get_string('pluginname', 'tool_objectfs'));
40+
}
3341

3442
/**
3543
* Get result
@@ -46,11 +54,9 @@ public function get_result(): result {
4654
$result = $client->test_set_object_tag();
4755

4856
if ($result->success) {
49-
// TODO lang.
50-
return new result(result::OK, 'tagging success', $result->details);
57+
return new result(result::OK, get_string('check:tagging:ok', 'tool_objectfs'), $result->details);
5158
} else {
52-
// TODO lang.
53-
return new result(result::ERROR, 'tagging failed', $result->details);
59+
return new result(result::ERROR, get_string('check:tagging:error', 'tool_objectfs'), $result->details);
5460
}
5561
}
5662
}

classes/local/store/s3/client.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,8 @@ public function test_set_object_tag(): stdClass {
916916
* @param array $tags array of key=>value pairs to set as tags.
917917
*/
918918
public function set_object_tags(string $contenthash, array $tags) {
919-
// First convert PHP array tags to XML string.
919+
// Convert tags into format S3 client expects.
920920
$s3tags = [];
921-
922921
foreach($tags as $key => $value) {
923922
$s3tags[] = [
924923
'Key' => $key,

classes/local/tag/tag_manager.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class tag_manager {
4545
*/
4646
public const SYNC_STATUS_SYNC_NOT_REQUIRED = 1;
4747

48+
/**
49+
* @var array All possible tag sync statuses.
50+
*/
4851
public const SYNC_STATUSES = [
4952
self::SYNC_STATUS_NEEDS_SYNC,
5053
self::SYNC_STATUS_SYNC_NOT_REQUIRED
@@ -131,7 +134,7 @@ public static function update_tags_if_necessary(string $contenthash, bool $force
131134
}
132135

133136
/**
134-
* Updates the tags for a given file. Updates both local db and external.
137+
* Updates the local db tag records, and marks object as needing sync to sync local tags to external.
135138
* @param int $objectid
136139
* @param array $tags
137140
*/
@@ -202,7 +205,6 @@ public static function replicate_local_to_external_tags_for_object(string $conte
202205
}
203206

204207
try {
205-
206208
// TODO refactor so all func use contenthash, no objectid.
207209
// Then also probably remove from DB.
208210
$tags = self::query_local_tags($contenthash);
@@ -249,12 +251,15 @@ private static function mark_object_tag_sync_status(string $contenthash, int $st
249251
}
250252

251253
/**
252-
* Returns true if the tags given are different
253-
* @param array $a
254-
* @param array $b
254+
* Returns true if the tags given are different. They are different when their keys or values do not match, order does not matter.
255+
* @param array $a array of key=>value where keys and values are both strings
256+
* @param array $b array of key=>value where keys and values are both strings
255257
* @return bool
256258
*/
257259
private static function are_tags_different(array $a, array $b): bool {
260+
// Order the keys in both arrays, then do a simple equality check.
261+
ksort($a);
262+
ksort($b);
258263
return $a !== $b;
259264
}
260265
}

classes/task/queue_objects_needing_tags.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function execute() {
4646
return;
4747
}
4848

49-
// TODO configurable limit per run.
50-
$contenthashes = tag_manager::get_objects_needing_sync(1000);
49+
$limit = get_config('tool_objectfs', 'maxtaggingperrun');
50+
$contenthashes = tag_manager::get_objects_needing_sync($limit);
5151
mtrace("Found " . count($contenthashes) . " objects needing tag sync");
5252

5353
foreach ($contenthashes as $contenthash) {

classes/task/update_object_tags.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ class update_object_tags extends adhoc_task {
3333
*/
3434
public function execute() {
3535
if (!tag_manager::is_tagging_enabled_and_supported()) {
36-
mtrace("Tagging feature not enabled or supported by filesystem, exiting early.");
36+
mtrace("Tagging feature not enabled or supported by filesystem, exiting.");
3737
return;
3838
}
3939

4040
// Find the object from the customdata.
4141
$contenthash = $this->get_custom_data()->contenthash ?? null;
4242

4343
if (empty($contenthash)) {
44-
mtrace("No content hash given, invalid customdata, exiting");
44+
mtrace("No contenthash given, invalid customdata, exiting.");
4545
return;
4646
}
4747

48+
// Update local tags in db.
4849
tag_manager::update_tags_if_necessary($contenthash);
49-
// TODO if this fails because of 404, just ignore since file is clearly not replicated yet
50+
51+
// Sync these to external store.
5052
tag_manager::replicate_local_to_external_tags_for_object($contenthash);
5153
}
5254
}

db/upgrade.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,41 @@ function xmldb_tool_objectfs_upgrade($oldversion) {
171171
upgrade_plugin_savepoint(true, 2023013100, 'tool', 'objectfs');
172172
}
173173

174-
// TODO db update step for db.
174+
if ($oldversion < 2023051704) {
175+
176+
// Define table tool_objectfs_object_tags to be created.
177+
$table = new xmldb_table('tool_objectfs_object_tags');
178+
179+
// Adding fields to table tool_objectfs_object_tags.
180+
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
181+
$table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
182+
$table->add_field('tagkey', XMLDB_TYPE_CHAR, '15', null, XMLDB_NOTNULL, null, null);
183+
$table->add_field('tagvalue', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
184+
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
185+
186+
// Adding keys to table tool_objectfs_object_tags.
187+
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
188+
189+
// Adding indexes to table tool_objectfs_object_tags.
190+
$table->add_index('objecttagkey_idx', XMLDB_INDEX_UNIQUE, ['contenthash', 'tagkey']);
191+
192+
// Conditionally launch create table for tool_objectfs_object_tags.
193+
if (!$dbman->table_exists($table)) {
194+
$dbman->create_table($table);
195+
}
196+
197+
// Define field tagsyncstatus to be added to tool_objectfs_objects.
198+
$table = new xmldb_table('tool_objectfs_objects');
199+
$field = new xmldb_field('tagsyncstatus', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'filesize');
200+
201+
// Conditionally launch add field tagsyncstatus.
202+
if (!$dbman->field_exists($table, $field)) {
203+
$dbman->add_field($table, $field);
204+
}
205+
206+
// Objectfs savepoint reached.
207+
upgrade_plugin_savepoint(true, 2023051704, 'tool', 'objectfs');
208+
}
175209

176210
return true;
177211
}

lang/en/tool_objectfs.php

+5
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,8 @@
274274
$string['checkproxy_range_request'] = 'Pre-signed URL range request proxy';
275275

276276
$string['task:queueobjectsneedingtags'] = 'Queue objects needing tagging';
277+
$string['checktagging_status'] = 'Object tagging';
278+
$string['check:tagging:ok'] = 'Object tagging ok';
279+
$string['check:tagging:error'] = 'Error trying to tag object';
280+
$string['settings:maxtaggingperrun'] = 'Max objects tagged per run';
281+
$string['settings:maxtaggingperrun:desc'] = 'The maximum number of objects to be queued for tag updates per run of the queue_objects_needing_tags scheduled task.';

settings.php

+7
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,18 @@
250250
$settings->add(new admin_setting_configcheckbox('tool_objectfs/preferexternal',
251251
new lang_string('settings:preferexternal', 'tool_objectfs'), '', ''));
252252

253+
// Tagging settings.
253254
$settings->add(new admin_setting_heading('tool_objectfs/taggingsettings',
254255
new lang_string('settings:taggingheader', 'tool_objectfs'), ''));
255256

256257
// TODO some info here about tagging, maybe a link to doc in git.
257258

258259
$settings->add(new admin_setting_configcheckbox('tool_objectfs/taggingenabled',
259260
new lang_string('settings:taggingenabled', 'tool_objectfs'), '', ''));
261+
262+
$settings->add(new admin_setting_configtext('tool_objectfs/maxtaggingperrun',
263+
new lang_string('settings:maxtaggingperrun', 'tool_objectfs'),
264+
get_string('settings:maxtaggingperrun:desc', 'tool_objectfs'),
265+
10000,
266+
PARAM_INT));
260267
}

tests/local/tagging_test.php

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public function test_tag_sources_value(tag_source $source) {
8484
$this->assertGreaterThan(0, $count);
8585
}
8686

87+
// TODO ensure all sources have unique keys.
88+
// TODO ensure keys and values are all strings.
89+
8790
/**
8891
* Provides values to test_is_tagging_enabled_and_supported
8992
* @return array

0 commit comments

Comments
 (0)