@@ -88,7 +88,7 @@ public static function can_override_existing_tags(): bool {
88
88
* @param string $contenthash
89
89
* @return array array of key=>value pairs, the tags for the given file.
90
90
*/
91
- private static function gather_tags (string $ contenthash ): array {
91
+ public static function gather_object_tags_for_upload (string $ contenthash ): array {
92
92
$ tags = [];
93
93
foreach (self ::get_defined_tag_sources () as $ source ) {
94
94
$ val = $ source ->get_value_for_contenthash ($ contenthash );
@@ -108,7 +108,7 @@ private static function gather_tags(string $contenthash): array {
108
108
* @param int $objectid
109
109
* @param array $tags
110
110
*/
111
- private static function store_tags (string $ contenthash , array $ tags ) {
111
+ public static function store_tags_locally (string $ contenthash , array $ tags ) {
112
112
global $ DB ;
113
113
114
114
// Purge any existing tags for this object.
@@ -148,86 +148,45 @@ public static function get_objects_needing_sync(int $limit) {
148
148
}
149
149
150
150
/**
151
- * Gathers the tag values for a given object contenthash, stores the values in the DB and then replicates these to the external store.
152
- * Will mark the object sync status to sync status not required.
151
+ * Marks a given object as the given status.
153
152
* @param string $contenthash
153
+ * @param int $status one of SYNC_STATUS_* constants
154
154
*/
155
- public static function calculate_tags_store_locally_and_replicate_to_external (string $ contenthash ) {
156
- // TODO someway to genericise this, so we can call it with and without replicating ,etc...
157
-
158
- // Take lock, to ensure tags are not updated while we sync them.
159
- $ lock = self ::get_object_tag_lock ($ contenthash );
160
-
161
- if (!$ lock ) {
162
- throw new coding_exception ("Unable to obtain lock for object " . $ contenthash ); // TODO different ex type ?
155
+ public static function mark_object_tag_sync_status (string $ contenthash , int $ status ) {
156
+ global $ DB ;
157
+ if (!in_array ($ status , self ::SYNC_STATUSES )) {
158
+ throw new coding_exception ("Invalid object tag sync status " . $ status );
163
159
}
160
+ $ DB ->set_field ('tool_objectfs_objects ' , 'tagsyncstatus ' , $ status , ['contenthash ' => $ contenthash ]);
161
+ }
164
162
165
- try {
166
- // Get current client, ensure it is configured correctly.
167
- $ client = manager::get_client (manager::get_objectfs_config ());
168
-
169
- if (empty ($ client )) {
170
- throw new coding_exception ("No client set, cannot replicate tags " ); // TODO moodle ex ?
171
- }
163
+ public static function sync_object_tags_post_upload (string $ contenthash ) {
164
+ // First get object lock
165
+ // TODO
172
166
173
- // Sanity check, ensure supports object tagging.
174
- if (!$ client ->supports_object_tagging ()) {
175
- throw new coding_exception ("Client does not support object tagging " ); // TODO different ex ?
176
- }
167
+ $ client = manager::get_client (manager::get_objectfs_config ());
177
168
178
- // Gather tags from sources & store locally.
179
- $ gatheredtags = self ::gather_tags ($ contenthash );
180
- self ::store_tags ($ contenthash , $ gatheredtags );
181
-
182
- // Replicate to external.
183
- $ client ->set_object_tags ($ contenthash , $ gatheredtags );
184
-
185
- // Mark as synced, this stops it from re-syncing in the future.
186
- self ::mark_object_tag_sync_status ($ contenthash , self ::SYNC_STATUS_SYNC_NOT_REQUIRED );
187
- } catch (Exception $ e ) {
188
- $ lock ->release ();
189
- throw $ e ;
169
+ // If object does not exist, cannot sync tags, abort.
170
+ if (!$ client ->object_exists ($ contenthash )) {
171
+ return ;
190
172
}
191
- }
192
-
193
- /**
194
- * This should only ever be called as part of uploading an object for the first time.
195
- */
196
- public static function calculate_tags_and_store_locally_and_return (string $ contenthash ): array {
197
- // TODO.
198
- }
199
173
200
- private static function should_set_object_tags (string $ contenthash ) {
201
- // If can override, always return true.
202
- if (self ::can_override_existing_tags ()) {
203
- return true ;
174
+ // Cannot override and object exists.
175
+ if (!$ client ->can_override_objects ()) {
176
+ // Query existing tags and store them.
177
+ $ existingtags = $ client ->get_object_tags ($ contenthash );
178
+ self ::store_tags_locally ($ contenthash , $ existingtags );
179
+
180
+ // Else can override, upload new.
181
+ } else {
182
+ $ tags = self ::gather_object_tags_for_upload ($ contenthash );
183
+ $ client ->set_object_tags ($ contenthash , $ tags );
184
+ self ::store_tags_locally ($ contenthash , $ tags );
204
185
}
205
186
206
- // Else lookup if object already has tags, or if it even exists.
207
- // TODO.
208
- }
187
+ // Either way, it has synced.
188
+ self ::mark_object_tag_sync_status ($ contenthash , self ::SYNC_STATUS_SYNC_NOT_REQUIRED );
209
189
210
- /**
211
- * Gets a tag writing lock on the given object, ensuring tags are not written/updated while being synced, for example.
212
- * @param string $contenthash
213
- * @param int $timeout lock wait timeout in seconds
214
- * @return lock
215
- */
216
- private static function get_object_tag_lock (string $ contenthash , int $ timeout = 5 ): lock {
217
- $ factory = lock_config::get_lock_factory ('tool_objectfs_object_tags ' );
218
- return $ factory ->get_lock ($ contenthash , $ timeout );
219
- }
220
-
221
- /**
222
- * Marks a given object as the given status.
223
- * @param string $contenthash
224
- * @param int $status one of SYNC_STATUS_* constants
225
- */
226
- private static function mark_object_tag_sync_status (string $ contenthash , int $ status ) {
227
- global $ DB ;
228
- if (!in_array ($ status , self ::SYNC_STATUSES )) {
229
- throw new coding_exception ("Invalid object tag sync status " . $ status );
230
- }
231
- $ DB ->set_field ('tool_objectfs_objects ' , 'tagsyncstatus ' , $ status , ['contenthash ' => $ contenthash ]);
190
+ // TODO release object lock.
232
191
}
233
192
}
0 commit comments