From 49e6358fce2706206ce59b8455214e018ff3404d Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 14 Mar 2018 11:20:33 -0700 Subject: [PATCH] Bulk load builtin project default profile images Summary: Depends on D19221. Ref T13106. When we fall back to default profile images for projects, bulk load them instead of doing individual queries. Test Plan: Saw local task drop from 199 queries to 151 queries with the same actual outcome. Saw custom and default profile images on the project list page. Maniphest Tasks: T13106 Differential Revision: https://secure.phabricator.com/D19222 --- .../project/query/PhabricatorProjectQuery.php | 66 +++++++++++++++---- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/src/applications/project/query/PhabricatorProjectQuery.php b/src/applications/project/query/PhabricatorProjectQuery.php index 955eba9972..98713078fb 100644 --- a/src/applications/project/query/PhabricatorProjectQuery.php +++ b/src/applications/project/query/PhabricatorProjectQuery.php @@ -357,29 +357,69 @@ final class PhabricatorProjectQuery } protected function didFilterPage(array $projects) { + $viewer = $this->getViewer(); + if ($this->needImages) { - $file_phids = mpull($projects, 'getProfileImagePHID'); - $file_phids = array_filter($file_phids); + $need_images = $projects; + + // First, try to load custom profile images for any projects with custom + // images. + $file_phids = array(); + foreach ($need_images as $key => $project) { + $image_phid = $project->getProfileImagePHID(); + if ($image_phid) { + $file_phids[$key] = $image_phid; + } + } + if ($file_phids) { $files = id(new PhabricatorFileQuery()) ->setParentQuery($this) - ->setViewer($this->getViewer()) + ->setViewer($viewer) ->withPHIDs($file_phids) ->execute(); $files = mpull($files, null, 'getPHID'); - } else { - $files = array(); + + foreach ($file_phids as $key => $image_phid) { + $file = idx($files, $image_phid); + if (!$file) { + continue; + } + + $need_images[$key]->attachProfileImageFile($file); + unset($need_images[$key]); + } } - foreach ($projects as $project) { - $file = idx($files, $project->getProfileImagePHID()); - if (!$file) { - $builtin = PhabricatorProjectIconSet::getIconImage( - $project->getIcon()); - $file = PhabricatorFile::loadBuiltin($this->getViewer(), - 'projects/'.$builtin); + // For projects with default images, or projects where the custom image + // failed to load, load a builtin image. + if ($need_images) { + $builtin_map = array(); + $builtins = array(); + foreach ($need_images as $key => $project) { + $icon = $project->getIcon(); + + $builtin_name = PhabricatorProjectIconSet::getIconImage($icon); + $builtin_name = 'projects/'.$builtin_name; + + $builtin = id(new PhabricatorFilesOnDiskBuiltinFile()) + ->setName($builtin_name); + + $builtin_key = $builtin->getBuiltinFileKey(); + + $builtins[] = $builtin; + $builtin_map[$key] = $builtin_key; + } + + $builtin_files = PhabricatorFile::loadBuiltins( + $viewer, + $builtins); + + foreach ($need_images as $key => $project) { + $builtin_key = $builtin_map[$key]; + $builtin_file = $builtin_files[$builtin_key]; + $project->attachProfileImageFile($builtin_file); } - $project->attachProfileImageFile($file); } }