Skip to content

Commit e5b94bf

Browse files
committed
WIP intial tagging code structures
1 parent e90dc51 commit e5b94bf

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace tool_objectfs\local\tag;
4+
5+
use stored_file;
6+
7+
class file_type_source implements tag_source {
8+
public const TYPE_STANDARD = 'standard';
9+
10+
public const TYPE_BACKUP = 'backup';
11+
12+
public static function get_identifer(): string {
13+
return 'type';
14+
}
15+
16+
public function get_value_for_stored_file(stored_file $file): ?string {
17+
// TODO implement types e.g. is backup, etc..
18+
return self::TYPE_STANDARD;
19+
}
20+
21+
public function get_value_for_contenthash(string $contenthash): ?string {
22+
// TODO implement types e.g. is backup, etc..
23+
return self::TYPE_STANDARD;
24+
}
25+
}

classes/local/tag/tag_manager.php

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace tool_objectfs\local\tag;
4+
5+
use stored_file;
6+
7+
class tag_manager {
8+
9+
/**
10+
* Returns an array of tag_source instances that are currently defined.
11+
* @return array
12+
*/
13+
public static function get_defined_tag_sources(): array {
14+
// All possible tag sources should be defined here.
15+
// Note this should be a maximum of 10 sources, as this is an AWS limit.
16+
return [
17+
new file_type_source()
18+
];
19+
}
20+
21+
/**
22+
* Is the tagging feature enabled?
23+
* @return bool
24+
*/
25+
public static function is_tagging_enabled(): bool {
26+
return false; // TODO config.
27+
}
28+
29+
/**
30+
* Gathers the tags for a given content hash
31+
* @param string $contenthash
32+
* @return array array of key=>value pairs, the tags for the given file.
33+
*/
34+
private function gather_tags(string $contenthash): array {
35+
$tags = [];
36+
foreach(self::get_defined_tag_sources() as $source) {
37+
$tags[$source->get_identifer()] = $source->get_value_for_contenthash($contenthash);
38+
}
39+
return $tags;
40+
}
41+
42+
/**
43+
* Returns the tags stored locally in the DB for the given file contenthash
44+
* @param string $contenthash
45+
* @return array array of key=>value pairs, the tags for the given file that are stored in the database.
46+
*/
47+
private function query_local_tags(string $contenthash): array {
48+
// TODO.
49+
return [];
50+
}
51+
52+
/**
53+
* Returns the tags attached to an object in the external storage.
54+
* @param string $contenthash
55+
* @return array array of key=>value pairs, the tags for the given file that are attached to the external object.
56+
*/
57+
private function query_external_tags(string $contenthash): array {
58+
// TODO.
59+
return [];
60+
}
61+
62+
/**
63+
* Synchronises the tags for a given file.
64+
*
65+
* @param string $contenthash file to update tags for
66+
* @param bool $force By default, will not update stored tags unless the gathered tags differ from the locally stored tags.
67+
* Setting this to true will force it to always update external object tags.
68+
*/
69+
public function sync_tags(string $contenthash, bool $force = false) {
70+
// Gather local and compare against local db.
71+
$gatheredtags = $this->gather_tags($contenthash);
72+
$localtags = $this->query_local_tags($contenthash);
73+
74+
// Update if forced to, or if what we just gathered is not the same as what is stored in the database.
75+
$shouldupdate = $force || $this->are_tags_different($gatheredtags, $localtags);
76+
77+
if ($shouldupdate) {
78+
$this->store_tags($contenthash, $gatheredtags);
79+
}
80+
}
81+
82+
/**
83+
* Updates the tags for a given file. Updates both local db and external.
84+
* @param string $contenthash
85+
* @param array $tags
86+
* @param bool $updateexternal If the external tags should be updated.
87+
* Unless you are uploading the file (and uploading tags along with it), this should always be true to keep the local and external values in sync.
88+
*/
89+
private function store_tags(string $contenthash, array $tags, bool $updateexternal = true) {
90+
global $DB;
91+
// TODO store locally.
92+
// TODO store externally.
93+
}
94+
95+
private function are_tags_different(array $a, array $b) {
96+
return true; // TODO compare the keys and values to ensure they are exactly the same!
97+
}
98+
99+
// private function query_tags_for_contenthash(string $contenthash): array {
100+
// // TODO query from the db, the current db values.
101+
// }
102+
103+
// public function update_object_tags_by_contenthash(string $contenthash, bool $skipifsame = true) {
104+
// $shouldupdateexternal = true;
105+
// $tags = $this->build_tags_for_contenthash($contenthash);
106+
107+
108+
// foreach ($tags as $key => $value) {
109+
// $this->upsert_tag_value($contenthash, $key, $value);
110+
// }
111+
// $this->store_local_tags_for_contenthash($contenthash);
112+
// }
113+
114+
// private function store_local_tags_for_contenthash(string $contenthash) {
115+
// }
116+
117+
// private function upsert_tag_value(string $contenthash, string $tagkey, string $tagvalue) {
118+
// // TODO upsert this in the DB.
119+
// global $DB;
120+
// // TODO.
121+
// }
122+
123+
// public function sync_external_tags_by_contenthash(string $contenthash) {
124+
// // TODO call external api to sync.
125+
// global $DB;
126+
// $tags =
127+
// }
128+
}

classes/local/tag/tag_source.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace tool_objectfs\local\tag;
4+
5+
use stored_file;
6+
7+
interface tag_source {
8+
/**
9+
* Returns an unchanging identifier for this source.
10+
* Must never change, otherwise it will lose connection with the tags replicated to objects.
11+
* If it ever must change, a migration step must be completed to trigger all objects to recalculate the value under the new tag name.
12+
* Must not exceed 128 chars. TODO unit test this.
13+
* @return string
14+
*/
15+
public static function get_identifer(): string;
16+
17+
/**
18+
* Returns the value of this tag for the given stored file.
19+
* This must be deterministic, and should never exceed 256 chars. TODO unit test this.
20+
* @param stored_file $file
21+
* @return string
22+
*/
23+
public function get_value_for_stored_file(stored_file $file): ?string;
24+
25+
/**
26+
* Returns the value of this tag for the file with the given content hash.
27+
* This must be deterministic, and should never exceed 256 chars. TODO unit test this.
28+
* @param string $contenthash
29+
* @return string
30+
*/
31+
public function get_value_for_contenthash(string $contenthash): ?string;
32+
}

classes/task/update_object_tags.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace tool_objectfs\task;
4+
5+
use core\task\adhoc_task;
6+
7+
class update_object_tags extends adhoc_task {
8+
public function execute() {
9+
// Find the contenthashes from the customdata.
10+
$hashes = $this->get_custom_data()['contenthashes'] ?? [];
11+
// TODO sync these.
12+
}
13+
}

0 commit comments

Comments
 (0)