@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
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Consolidate Macro loading

Summary:
Fixes T2820

Grepped for `PhabricatorFileImageMacro`, a common approach to load image macros from storage. Cleaned up file loading too (in most cases where I could be sure that I won't break anything).

Did not touch the `add_macro.php` util script, since many users will assume the user `ubuntu` or `ec2-user` to run the script. Add no sessions and no CSRF protection measures...

Test Plan:
Browsed around all kinds of places. Created and looked at memes, created and edited macros. Used them in Remarkup (with flushed cache). Used `macro.query`, verified it did not crash (that's always a good sign).

Could not verify object handles, since I have no idea where they appear right now.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T2820

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

authored by

Anh Nhan Nguyen and committed by
epriestley
f95710e7 15c9287d

+72 -48
+4 -14
src/applications/macro/conduit/ConduitAPI_macro_query_Method.php
··· 24 24 } 25 25 26 26 protected function execute(ConduitAPIRequest $request) { 27 - 28 - $macros = id(new PhabricatorFileImageMacro())->loadAll(); 29 - 30 - $files = array(); 31 - if ($macros) { 32 - $files = id(new PhabricatorFile())->loadAllWhere( 33 - 'phid IN (%Ls)', 34 - mpull($macros, 'getFilePHID')); 35 - $files = mpull($files, null, 'getPHID'); 36 - } 27 + $macros = id(new PhabricatorMacroQuery()) 28 + ->setViewer($request->getUser()) 29 + ->execute(); 37 30 38 31 $results = array(); 39 32 foreach ($macros as $macro) { 40 - if (empty($files[$macro->getFilePHID()])) { 41 - continue; 42 - } 43 33 $results[$macro->getName()] = array( 44 - 'uri' => $files[$macro->getFilePHID()]->getBestURI(), 34 + 'uri' => $macro->getFile()->getBestURI(), 45 35 ); 46 36 } 47 37
+4 -1
src/applications/macro/controller/PhabricatorMacroCommentController.php
··· 17 17 return new Aphront400Response(); 18 18 } 19 19 20 - $macro = id(new PhabricatorFileImageMacro())->load($this->id); 20 + $macro = id(new PhabricatorMacroQuery()) 21 + ->setViewer($user) 22 + ->withIDs(array($this->id)) 23 + ->executeOne(); 21 24 if (!$macro) { 22 25 return new Aphront404Response(); 23 26 }
+9 -1
src/applications/macro/controller/PhabricatorMacroDisableController.php
··· 13 13 $request = $this->getRequest(); 14 14 $user = $request->getUser(); 15 15 16 - $macro = id(new PhabricatorFileImageMacro())->load($this->id); 16 + $macro = id(new PhabricatorMacroQuery()) 17 + ->setViewer($user) 18 + ->requireCapabilities( 19 + array( 20 + PhabricatorPolicyCapability::CAN_VIEW, 21 + PhabricatorPolicyCapability::CAN_EDIT, 22 + )) 23 + ->withIDs(array($this->id)) 24 + ->executeOne(); 17 25 if (!$macro) { 18 26 return new Aphront404Response(); 19 27 }
+14 -7
src/applications/macro/controller/PhabricatorMacroEditController.php
··· 11 11 12 12 public function processRequest() { 13 13 14 + $request = $this->getRequest(); 15 + $user = $request->getUser(); 16 + 14 17 if ($this->id) { 15 - $macro = id(new PhabricatorFileImageMacro())->load($this->id); 18 + $macro = id(new PhabricatorMacroQuery()) 19 + ->setViewer($user) 20 + ->requireCapabilities( 21 + array( 22 + PhabricatorPolicyCapability::CAN_VIEW, 23 + PhabricatorPolicyCapability::CAN_EDIT, 24 + )) 25 + ->withIDs(array($this->id)) 26 + ->executeOne(); 16 27 if (!$macro) { 17 28 return new Aphront404Response(); 18 29 } ··· 26 37 $file = null; 27 38 $can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http'); 28 39 29 - $request = $this->getRequest(); 30 - $user = $request->getUser(); 31 40 if ($request->isFormPost()) { 32 41 $original = clone $macro; 33 42 ··· 81 90 $e_file = pht('Invalid'); 82 91 } else { 83 92 $macro->setFilePHID($file->getPHID()); 93 + $macro->attachFile($file); 84 94 $e_file = null; 85 95 } 86 96 } ··· 136 146 $error_view = null; 137 147 } 138 148 139 - 140 149 $current_file = null; 141 150 if ($macro->getFilePHID()) { 142 - $current_file = id(new PhabricatorFile())->loadOneWhere( 143 - 'phid = %s', 144 - $macro->getFilePHID()); 151 + $current_file = $macro->getFile(); 145 152 } 146 153 147 154 $form = new AphrontFormView();
-4
src/applications/macro/controller/PhabricatorMacroListController.php
··· 14 14 $request = $this->getRequest(); 15 15 $viewer = $request->getUser(); 16 16 17 - $macro_table = new PhabricatorFileImageMacro(); 18 - $file_table = new PhabricatorFile(); 19 - $conn = $macro_table->establishConnection('r'); 20 - 21 17 $pager = id(new AphrontCursorPagerView()) 22 18 ->readFromRequest($request); 23 19
+5 -5
src/applications/macro/controller/PhabricatorMacroMemeController.php
··· 9 9 $upper_text = $request->getStr('uppertext'); 10 10 $lower_text = $request->getStr('lowertext'); 11 11 $user = $request->getUser(); 12 - $macro = id(new PhabricatorFileImageMacro()) 13 - ->loadOneWhere('name=%s', $macro_name); 12 + $macro = id(new PhabricatorMacroQuery()) 13 + ->setViewer($user) 14 + ->withNames(array($macro_name)) 15 + ->executeOne(); 14 16 if (!$macro) { 15 17 return new Aphront404Response(); 16 18 } 17 - $file = id(new PhabricatorFile())->loadOneWhere( 18 - 'phid = %s', 19 - $macro->getFilePHID()); 19 + $file = $macro->getFile(); 20 20 21 21 $upper_text = strtoupper($upper_text); 22 22 $lower_text = strtoupper($lower_text);
+4 -3
src/applications/macro/controller/PhabricatorMacroMemeDialogController.php
··· 18 18 $e_macro = pht('Required'); 19 19 $errors[] = pht('Macro name is required.'); 20 20 } else { 21 - $macro = id(new PhabricatorFileImageMacro())->loadOneWhere( 22 - 'name = %s', 23 - $name); 21 + $macro = id(new PhabricatorMacroQuery()) 22 + ->setViewer($user) 23 + ->withNames(array($name)) 24 + ->executeOne(); 24 25 if (!$macro) { 25 26 $e_macro = pht('Invalid'); 26 27 $errors[] = pht('No such macro.');
+5 -4
src/applications/macro/controller/PhabricatorMacroViewController.php
··· 13 13 $request = $this->getRequest(); 14 14 $user = $request->getUser(); 15 15 16 - $macro = id(new PhabricatorFileImageMacro())->load($this->id); 16 + $macro = id(new PhabricatorMacroQuery()) 17 + ->setViewer($user) 18 + ->withIDs(array($this->id)) 19 + ->executeOne(); 17 20 if (!$macro) { 18 21 return new Aphront404Response(); 19 22 } 20 23 21 - $file = id(new PhabricatorFile())->loadOneWhere( 22 - 'phid = %s', 23 - $macro->getFilePHID()); 24 + $file = $macro->getFile(); 24 25 25 26 $title_short = pht('Macro "%s"', $macro->getName()); 26 27 $title_long = pht('Image Macro "%s"', $macro->getName());
+17 -4
src/applications/macro/query/PhabricatorMacroQuery.php
··· 9 9 private $ids; 10 10 private $phids; 11 11 private $authors; 12 - private $name; 12 + private $names; 13 + private $nameLike; 13 14 14 15 private $status = 'status-any'; 15 16 const STATUS_ANY = 'status-any'; ··· 31 32 } 32 33 33 34 public function withNameLike($name) { 34 - $this->name = $name; 35 + $this->nameLike = $name; 36 + return $this; 37 + } 38 + 39 + public function withNames(array $names) { 40 + $this->names = $names; 35 41 return $this; 36 42 } 37 43 ··· 94 100 $this->authors); 95 101 } 96 102 97 - if ($this->name) { 103 + if ($this->nameLike) { 98 104 $where[] = qsprintf( 99 105 $conn, 100 106 'm.name LIKE %~', 101 - $this->name); 107 + $this->nameLike); 108 + } 109 + 110 + if ($this->names) { 111 + $where[] = qsprintf( 112 + $conn, 113 + 'm.name IN (%Ls)', 114 + $this->names); 102 115 } 103 116 104 117 if ($this->status == self::STATUS_ACTIVE) {
+6 -2
src/applications/macro/remarkup/PhabricatorRemarkupRuleImageMacro.php
··· 18 18 public function markupImageMacro($matches) { 19 19 if ($this->images === null) { 20 20 $this->images = array(); 21 - $rows = id(new PhabricatorFileImageMacro())->loadAllWhere( 22 - 'isDisabled = 0'); 21 + 22 + $viewer = $this->getEngine()->getConfig('viewer'); 23 + $rows = id(new PhabricatorMacroQuery()) 24 + ->setViewer($viewer) 25 + ->withStatus(PhabricatorMacroQuery::STATUS_ACTIVE) 26 + ->execute(); 23 27 foreach ($rows as $row) { 24 28 $this->images[$row->getName()] = $row->getFilePHID(); 25 29 }
+4 -3
src/applications/phid/handle/PhabricatorObjectHandleData.php
··· 158 158 return mpull($xactions, null, 'getPHID'); 159 159 160 160 case PhabricatorPHIDConstants::PHID_TYPE_MCRO: 161 - $macros = id(new PhabricatorFileImageMacro())->loadAllWhere( 162 - 'phid IN (%Ls)', 163 - $phids); 161 + $macros = id(new PhabricatorMacroQuery()) 162 + ->setViewer($this->viewer) 163 + ->withPHIDs($phids) 164 + ->execute(); 164 165 return mpull($macros, null, 'getPHID'); 165 166 166 167 case PhabricatorPHIDConstants::PHID_TYPE_PSTE: