From 3295dd5062cfe4d78ba6242f9a00ea55fa258c23 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Fri, 18 Aug 2023 14:42:21 +0200 Subject: [PATCH] Don't make query if not needed (#2863) * Don't make query if not needed See https://github.com/silverstripe/silverstripe-assets/issues/557 for background * add comment --- code/Model/SiteTreeLinkTracking.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/code/Model/SiteTreeLinkTracking.php b/code/Model/SiteTreeLinkTracking.php index c9b53826..a779f912 100644 --- a/code/Model/SiteTreeLinkTracking.php +++ b/code/Model/SiteTreeLinkTracking.php @@ -116,15 +116,22 @@ class SiteTreeLinkTracking extends DataExtension $allFields = DataObject::getSchema()->fieldSpecs($this->owner); $linkedPages = []; $anyBroken = false; + $hasTrackedFields = false; foreach ($allFields as $field => $fieldSpec) { $fieldObj = $this->owner->dbObject($field); if ($fieldObj instanceof DBHTMLText) { + $hasTrackedFields = true; // Merge links in this field with global list. $linksInField = $this->trackLinksInField($field, $anyBroken); $linkedPages = array_merge($linkedPages, $linksInField); } } + // We need a boolean flag instead of checking linkedPages because it can be empty when pages are removed + if (!$hasTrackedFields) { + return; + } + // Soft support for HasBrokenLink db field (e.g. SiteTree) if ($this->owner->hasField('HasBrokenLink')) { $this->owner->HasBrokenLink = $anyBroken;