From c84cb13cd4be421de4bbba019e677d6261a5d998 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Tue, 5 Jun 2018 10:39:04 +0800 Subject: [PATCH] MDL-62574 core_privacy: Check the contextlist for real contexts. There are situations when contexts will be deleted in the provider. The context list is not updated and so we have introduced a try catch. --- .../local/request/contextlist_base.php | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/privacy/classes/local/request/contextlist_base.php b/privacy/classes/local/request/contextlist_base.php index 71e601a6eff8a..41dc87e1e0dc1 100644 --- a/privacy/classes/local/request/contextlist_base.php +++ b/privacy/classes/local/request/contextlist_base.php @@ -79,12 +79,20 @@ public function get_contextids() : array { * Get the complete list of context objects that relate to this * request. * - * @return \contect[] + * @return \context[] */ public function get_contexts() : array { $contexts = []; foreach ($this->contextids as $contextid) { - $contexts[] = \context::instance_by_id($contextid); + // It is possible that this context has been deleted and we now have subsequent calls being made with this + // contextlist. Exceptions here will stop the further processing of this component and that is why we are + // doing a try catch. + try { + $contexts[] = \context::instance_by_id($contextid); + } catch (\Exception $e) { + // Remove this context. + unset($this->contextids[$this->iteratorposition]); + } } return $contexts; @@ -114,7 +122,25 @@ public function get_component() : string { * @return \context */ public function current() { - return \context::instance_by_id($this->contextids[$this->iteratorposition]); + // It is possible that this context has been deleted and we now have subsequent calls being made with this + // contextlist. Exceptions here will stop the further processing of this component and that is why we are + // doing a try catch. + try { + $context = \context::instance_by_id($this->contextids[$this->iteratorposition]); + } catch (\Exception $e) { + // Remove this context. + unset($this->contextids[$this->iteratorposition]); + // Check to see if there are any more contexts left. + if ($this->count()) { + // Move the pointer to the next record and try again. + $this->next(); + $context = $this->current(); + } else { + // There are no more context ids left. + return; + } + } + return $context; } /**