silverstripe / silverstripe-assets

Silverstripe Assets component
BSD 3-Clause "New" or "Revised" License
9 stars 65 forks source link

FileLinkTracking::augmentSyncLinkTracking should not make make queries if there are no tracked fields #557

Closed lekoala closed 10 months ago

lekoala commented 1 year ago

Recently found this by chance

It seems that since FileLinkTracking is attached to the dataobject, it runs for classes that are not relevant (eg: LoginSession)

image

By checking if there are actually any DBHTMLText, the query is not happening anymore

        // Build a list of HTMLText fields, merging all linked pages together.
        $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);
            }
        }

        if (!$hasTrackedFields) {
            return;
        }

PRs

lekoala commented 1 year ago

same thing for SiteTreeLinkTracking

maybe i'm missing something, but these changes seem to save two queries :-)

GuySartorelli commented 10 months ago

Both PRs are merged. Thanks for that.