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
+ }
0 commit comments