@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator

Provide "builtin" files and use them to fix Pholio when files are deleted

Summary:
Fixes T3132. Currently, if a user deletes a file which is present in a mock, that mock throws an exception when loading. If the file is also the cover photo, the mock list throws an exception as well.

In other applications, we can sometimes deal with this (a sub-object vanishing) by implicitly hiding the parent object (for example, we can just vanish feed stories about objects which no longer exist). We can also sometimes deal with it by preventing sub-objects from being directly deleted.

However, neither approach is reasonable in this case.

If we vanish the whole mock, we'll lose all the comments and it will generally be weird. Vanishing a mock is a big deal compared to vanishing a feed story. We'll also need to load more data on the list view to prevent showing a mock on the list view and then realizing we need to vanish it on the detail view (because all of its images have been deleted).

We permit total deletion of files to allow users to recover from accidentally uploading sensitive files (which has happened a few times), and I'm hesitant to remove this capability because I think it serves a real need, so we can't prevent sub-objects from being deleted.

So we're left in a relatively unique situation. To solve this, I've added a "builtin" mechanism, which allows us to expose some resource we ship with as a PhabricatorFile. Then we just swap it out in place of the original file and proceed forward normally, as though nothing happened. The user sees a placeholder image instead of the original, but everything else works reasonably and this seems like a fairly acceptable outcome.

I believe we can use this mechanism to simplify some other code too, like default profile pictures.

Test Plan: Deleted a Pholio mock cover image's file. Implemented change, saw functional Pholio again with beautiful life-affirming "?" art replacing soul-shattering exception.

Reviewers: btrahan, chad

Reviewed By: chad

CC: aran

Maniphest Tasks: T3132

Differential Revision: https://secure.phabricator.com/D5870

+147 -7
resources/builtin/missing.png

This is a binary file and will not be displayed.

+51 -5
src/applications/files/query/PhabricatorFileQuery.php
··· 7 7 private $phids; 8 8 private $authorPHIDs; 9 9 private $explicitUploads; 10 + private $transforms; 10 11 11 12 public function withIDs(array $ids) { 12 13 $this->ids = $ids; ··· 23 24 return $this; 24 25 } 25 26 27 + public function withTransforms(array $specs) { 28 + foreach ($specs as $spec) { 29 + if (!is_array($spec) || 30 + empty($spec['originalPHID']) || 31 + empty($spec['transform'])) { 32 + throw new Exception( 33 + "Transform specification must be a dictionary with keys ". 34 + "'originalPHID' and 'transform'!"); 35 + } 36 + } 37 + 38 + $this->transforms = $specs; 39 + return $this; 40 + } 41 + 26 42 public function showOnlyExplicitUploads($explicit_uploads) { 27 43 $this->explicitUploads = $explicit_uploads; 28 44 return $this; ··· 34 50 35 51 $data = queryfx_all( 36 52 $conn_r, 37 - 'SELECT * FROM %T f %Q %Q %Q', 53 + 'SELECT * FROM %T f %Q %Q %Q %Q', 38 54 $table->getTableName(), 55 + $this->buildJoinClause($conn_r), 39 56 $this->buildWhereClause($conn_r), 40 57 $this->buildOrderClause($conn_r), 41 58 $this->buildLimitClause($conn_r)); ··· 43 60 return $table->loadAllFromArray($data); 44 61 } 45 62 63 + private function buildJoinClause(AphrontDatabaseConnection $conn_r) { 64 + $joins = array(); 65 + 66 + if ($this->transforms) { 67 + $joins[] = qsprintf( 68 + $conn_r, 69 + 'JOIN %T t ON t.transformedPHID = f.phid', 70 + id(new PhabricatorTransformedFile())->getTableName()); 71 + } 72 + 73 + return implode(' ', $joins); 74 + } 75 + 46 76 private function buildWhereClause(AphrontDatabaseConnection $conn_r) { 47 77 $where = array(); 48 78 ··· 51 81 if ($this->ids) { 52 82 $where[] = qsprintf( 53 83 $conn_r, 54 - 'id IN (%Ld)', 84 + 'f.id IN (%Ld)', 55 85 $this->ids); 56 86 } 57 87 58 88 if ($this->phids) { 59 89 $where[] = qsprintf( 60 90 $conn_r, 61 - 'phid IN (%Ls)', 91 + 'f.phid IN (%Ls)', 62 92 $this->phids); 63 93 } 64 94 65 95 if ($this->authorPHIDs) { 66 96 $where[] = qsprintf( 67 97 $conn_r, 68 - 'authorPHID IN (%Ls)', 98 + 'f.authorPHID IN (%Ls)', 69 99 $this->authorPHIDs); 70 100 } 71 101 72 102 if ($this->explicitUploads) { 73 103 $where[] = qsprintf( 74 104 $conn_r, 75 - 'isExplicitUpload = true'); 105 + 'f.isExplicitUpload = true'); 106 + } 107 + 108 + if ($this->transforms) { 109 + $clauses = array(); 110 + foreach ($this->transforms as $transform) { 111 + $clauses[] = qsprintf( 112 + $conn_r, 113 + '(t.originalPHID = %s AND t.transform = %s)', 114 + $transform['originalPHID'], 115 + $transform['transform']); 116 + } 117 + $where[] = qsprintf($conn_r, '(%Q)', implode(') OR (', $clauses)); 76 118 } 77 119 78 120 return $this->formatWhereClause($where); 121 + } 122 + 123 + protected function getPagingColumn() { 124 + return 'f.id'; 79 125 } 80 126 81 127 }
+83
src/applications/files/storage/PhabricatorFile.php
··· 682 682 } 683 683 684 684 685 + /** 686 + * Load (or build) the {@class:PhabricatorFile} objects for builtin file 687 + * resources. The builtin mechanism allows files shipped with Phabricator 688 + * to be treated like normal files so that APIs do not need to special case 689 + * things like default images or deleted files. 690 + * 691 + * Builtins are located in `resources/builtin/` and identified by their 692 + * name. 693 + * 694 + * @param PhabricatorUser Viewing user. 695 + * @param list<string> List of builtin file names. 696 + * @return dict<string, PhabricatorFile> Dictionary of named builtins. 697 + */ 698 + public static function loadBuiltins(PhabricatorUser $user, array $names) { 699 + $specs = array(); 700 + foreach ($names as $name) { 701 + $specs[] = array( 702 + 'originalPHID' => PhabricatorPHIDConstants::PHID_VOID, 703 + 'transform' => 'builtin:'.$name, 704 + ); 705 + } 706 + 707 + $files = id(new PhabricatorFileQuery()) 708 + ->setViewer($user) 709 + ->withTransforms($specs) 710 + ->execute(); 711 + 712 + $files = mpull($files, null, 'getName'); 713 + 714 + $root = dirname(phutil_get_library_root('phabricator')); 715 + $root = $root.'/resources/builtin/'; 716 + 717 + $build = array(); 718 + foreach ($names as $name) { 719 + if (isset($files[$name])) { 720 + continue; 721 + } 722 + 723 + // This is just a sanity check to prevent loading arbitrary files. 724 + if (basename($name) != $name) { 725 + throw new Exception("Invalid builtin name '{$name}'!"); 726 + } 727 + 728 + $path = $root.$name; 729 + 730 + if (!Filesystem::pathExists($path)) { 731 + throw new Exception("Builtin '{$path}' does not exist!"); 732 + } 733 + 734 + $data = Filesystem::readFile($path); 735 + $params = array( 736 + 'name' => $name, 737 + 'ttl' => time() + (60 * 60 * 24 * 7), 738 + ); 739 + 740 + $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 741 + $file = PhabricatorFile::newFromFileData($data, $params); 742 + $xform = id(new PhabricatorTransformedFile()) 743 + ->setOriginalPHID(PhabricatorPHIDConstants::PHID_VOID) 744 + ->setTransform('builtin:'.$name) 745 + ->setTransformedPHID($file->getPHID()) 746 + ->save(); 747 + unset($unguarded); 748 + 749 + $files[$name] = $file; 750 + } 751 + 752 + return $files; 753 + } 754 + 755 + 756 + /** 757 + * Convenience wrapper for @{method:loadBuiltins}. 758 + * 759 + * @param PhabricatorUser Viewing user. 760 + * @param string Single builtin name to load. 761 + * @return PhabricatorFile Corresponding builtin file. 762 + */ 763 + public static function loadBuiltin(PhabricatorUser $user, $name) { 764 + return idx(self::loadBuiltins($user, array($name)), $name); 765 + } 766 + 767 + 685 768 /* -( PhabricatorPolicyInterface Implementation )-------------------------- */ 686 769 687 770
+3
src/applications/phid/PhabricatorPHIDConstants.php
··· 44 44 const PHID_TYPE_XCMT = 'XCMT'; 45 45 const PHID_TYPE_XUSR = 'XUSR'; 46 46 47 + const PHID_TYPE_VOID = 'VOID'; 48 + const PHID_VOID = 'PHID-VOID-00000000000000000000'; 49 + 47 50 }
+10 -2
src/applications/pholio/query/PholioMockQuery.php
··· 129 129 } 130 130 131 131 foreach ($all_images as $image) { 132 - $image->attachFile($all_files[$image->getFilePHID()]); 132 + $file = idx($all_files, $image->getFilePHID()); 133 + if (!$file) { 134 + $file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png'); 135 + } 136 + $image->attachFile($file); 133 137 if ($this->needInlineComments) { 134 138 $inlines = idx($all_images, $image->getID(), array()); 135 139 $image->attachInlineComments($inlines); ··· 151 155 $cover_file_phids), null, 'getPHID'); 152 156 153 157 foreach ($mocks as $mock) { 154 - $mock->attachCoverFile($cover_files[$mock->getCoverPHID()]); 158 + $file = idx($cover_files, $mock->getCoverPHID()); 159 + if (!$file) { 160 + $file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png'); 161 + } 162 + $mock->attachCoverFile($file); 155 163 } 156 164 } 157 165