Closed rebuildGlobalPhraseCache() unnecessarily loads all phrase records to retrieve their titles

Painbaker

Well-known member
Licensed customer
Affected version
2.3.13
XF\Repository\LanguageRepository::rebuildGlobalPhraseCache() unnecessarily fetches all fields of global master phrases and creates Entity objects, only to extract title:


PHP:
    public function rebuildGlobalPhraseCache()
    {
        $titles = $this->finder(PhraseFinder::class)
            ->where('language_id', 0)
            ->where('global_cache', 1)
            ->fetch()
            ->pluckNamed('title');

        $languages = $this->findLanguages()->fetch();

        $globalPhrases = [];

        if (!$titles)
        {
            return;
        }

        $result = $this->db()->query('
            SELECT language_id, title, phrase_text
            FROM xf_phrase_compiled
            WHERE title IN (' . $this->db()->quote($titles) . ')
        ');
        while ($phrase = $result->fetch())
        {
            $globalPhrases[$phrase['language_id']][$phrase['title']] = $phrase['phrase_text'];
        }

        $this->db()->beginTransaction();

        /** @var $language \XF\Entity\Language */
        foreach ($languages AS $languageId => $language)
        {
            if (isset($globalPhrases[$languageId]))
            {
                $phrases = $globalPhrases[$languageId];
            }
            else
            {
                $phrases = [];
            }
            $language->fastUpdate('phrase_cache', $phrases);
        }

        $this->db()->commit();
    }
 
Good catch and ordinarily a reasonable fix.

However, although unannounced currently, this is resolved in XenForo 2.4 and so there's not a compelling reason to fix it as-is in XenForo 2.3.
 
Back
Top Bottom