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
This commit is contained in:
Anh Nhan Nguyen
2013-03-22 13:07:20 -07:00
committed by epriestley
parent 15c9287d43
commit f95710e799
11 changed files with 72 additions and 48 deletions

View File

@@ -24,24 +24,14 @@ final class ConduitAPI_macro_query_Method extends ConduitAPI_macro_Method {
} }
protected function execute(ConduitAPIRequest $request) { protected function execute(ConduitAPIRequest $request) {
$macros = id(new PhabricatorMacroQuery())
$macros = id(new PhabricatorFileImageMacro())->loadAll(); ->setViewer($request->getUser())
->execute();
$files = array();
if ($macros) {
$files = id(new PhabricatorFile())->loadAllWhere(
'phid IN (%Ls)',
mpull($macros, 'getFilePHID'));
$files = mpull($files, null, 'getPHID');
}
$results = array(); $results = array();
foreach ($macros as $macro) { foreach ($macros as $macro) {
if (empty($files[$macro->getFilePHID()])) {
continue;
}
$results[$macro->getName()] = array( $results[$macro->getName()] = array(
'uri' => $files[$macro->getFilePHID()]->getBestURI(), 'uri' => $macro->getFile()->getBestURI(),
); );
} }

View File

@@ -17,7 +17,10 @@ final class PhabricatorMacroCommentController
return new Aphront400Response(); return new Aphront400Response();
} }
$macro = id(new PhabricatorFileImageMacro())->load($this->id); $macro = id(new PhabricatorMacroQuery())
->setViewer($user)
->withIDs(array($this->id))
->executeOne();
if (!$macro) { if (!$macro) {
return new Aphront404Response(); return new Aphront404Response();
} }

View File

@@ -13,7 +13,15 @@ final class PhabricatorMacroDisableController
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$macro = id(new PhabricatorFileImageMacro())->load($this->id); $macro = id(new PhabricatorMacroQuery())
->setViewer($user)
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->withIDs(array($this->id))
->executeOne();
if (!$macro) { if (!$macro) {
return new Aphront404Response(); return new Aphront404Response();
} }

View File

@@ -11,8 +11,19 @@ final class PhabricatorMacroEditController
public function processRequest() { public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
if ($this->id) { if ($this->id) {
$macro = id(new PhabricatorFileImageMacro())->load($this->id); $macro = id(new PhabricatorMacroQuery())
->setViewer($user)
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->withIDs(array($this->id))
->executeOne();
if (!$macro) { if (!$macro) {
return new Aphront404Response(); return new Aphront404Response();
} }
@@ -26,8 +37,6 @@ final class PhabricatorMacroEditController
$file = null; $file = null;
$can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http'); $can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http');
$request = $this->getRequest();
$user = $request->getUser();
if ($request->isFormPost()) { if ($request->isFormPost()) {
$original = clone $macro; $original = clone $macro;
@@ -81,6 +90,7 @@ final class PhabricatorMacroEditController
$e_file = pht('Invalid'); $e_file = pht('Invalid');
} else { } else {
$macro->setFilePHID($file->getPHID()); $macro->setFilePHID($file->getPHID());
$macro->attachFile($file);
$e_file = null; $e_file = null;
} }
} }
@@ -136,12 +146,9 @@ final class PhabricatorMacroEditController
$error_view = null; $error_view = null;
} }
$current_file = null; $current_file = null;
if ($macro->getFilePHID()) { if ($macro->getFilePHID()) {
$current_file = id(new PhabricatorFile())->loadOneWhere( $current_file = $macro->getFile();
'phid = %s',
$macro->getFilePHID());
} }
$form = new AphrontFormView(); $form = new AphrontFormView();

View File

@@ -14,10 +14,6 @@ final class PhabricatorMacroListController
$request = $this->getRequest(); $request = $this->getRequest();
$viewer = $request->getUser(); $viewer = $request->getUser();
$macro_table = new PhabricatorFileImageMacro();
$file_table = new PhabricatorFile();
$conn = $macro_table->establishConnection('r');
$pager = id(new AphrontCursorPagerView()) $pager = id(new AphrontCursorPagerView())
->readFromRequest($request); ->readFromRequest($request);

View File

@@ -9,14 +9,14 @@ final class PhabricatorMacroMemeController
$upper_text = $request->getStr('uppertext'); $upper_text = $request->getStr('uppertext');
$lower_text = $request->getStr('lowertext'); $lower_text = $request->getStr('lowertext');
$user = $request->getUser(); $user = $request->getUser();
$macro = id(new PhabricatorFileImageMacro()) $macro = id(new PhabricatorMacroQuery())
->loadOneWhere('name=%s', $macro_name); ->setViewer($user)
->withNames(array($macro_name))
->executeOne();
if (!$macro) { if (!$macro) {
return new Aphront404Response(); return new Aphront404Response();
} }
$file = id(new PhabricatorFile())->loadOneWhere( $file = $macro->getFile();
'phid = %s',
$macro->getFilePHID());
$upper_text = strtoupper($upper_text); $upper_text = strtoupper($upper_text);
$lower_text = strtoupper($lower_text); $lower_text = strtoupper($lower_text);

View File

@@ -18,9 +18,10 @@ final class PhabricatorMacroMemeDialogController
$e_macro = pht('Required'); $e_macro = pht('Required');
$errors[] = pht('Macro name is required.'); $errors[] = pht('Macro name is required.');
} else { } else {
$macro = id(new PhabricatorFileImageMacro())->loadOneWhere( $macro = id(new PhabricatorMacroQuery())
'name = %s', ->setViewer($user)
$name); ->withNames(array($name))
->executeOne();
if (!$macro) { if (!$macro) {
$e_macro = pht('Invalid'); $e_macro = pht('Invalid');
$errors[] = pht('No such macro.'); $errors[] = pht('No such macro.');

View File

@@ -13,14 +13,15 @@ final class PhabricatorMacroViewController
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$macro = id(new PhabricatorFileImageMacro())->load($this->id); $macro = id(new PhabricatorMacroQuery())
->setViewer($user)
->withIDs(array($this->id))
->executeOne();
if (!$macro) { if (!$macro) {
return new Aphront404Response(); return new Aphront404Response();
} }
$file = id(new PhabricatorFile())->loadOneWhere( $file = $macro->getFile();
'phid = %s',
$macro->getFilePHID());
$title_short = pht('Macro "%s"', $macro->getName()); $title_short = pht('Macro "%s"', $macro->getName());
$title_long = pht('Image Macro "%s"', $macro->getName()); $title_long = pht('Image Macro "%s"', $macro->getName());

View File

@@ -9,7 +9,8 @@ final class PhabricatorMacroQuery
private $ids; private $ids;
private $phids; private $phids;
private $authors; private $authors;
private $name; private $names;
private $nameLike;
private $status = 'status-any'; private $status = 'status-any';
const STATUS_ANY = 'status-any'; const STATUS_ANY = 'status-any';
@@ -31,7 +32,12 @@ final class PhabricatorMacroQuery
} }
public function withNameLike($name) { public function withNameLike($name) {
$this->name = $name; $this->nameLike = $name;
return $this;
}
public function withNames(array $names) {
$this->names = $names;
return $this; return $this;
} }
@@ -94,11 +100,18 @@ final class PhabricatorMacroQuery
$this->authors); $this->authors);
} }
if ($this->name) { if ($this->nameLike) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'm.name LIKE %~', 'm.name LIKE %~',
$this->name); $this->nameLike);
}
if ($this->names) {
$where[] = qsprintf(
$conn,
'm.name IN (%Ls)',
$this->names);
} }
if ($this->status == self::STATUS_ACTIVE) { if ($this->status == self::STATUS_ACTIVE) {

View File

@@ -18,8 +18,12 @@ final class PhabricatorRemarkupRuleImageMacro
public function markupImageMacro($matches) { public function markupImageMacro($matches) {
if ($this->images === null) { if ($this->images === null) {
$this->images = array(); $this->images = array();
$rows = id(new PhabricatorFileImageMacro())->loadAllWhere(
'isDisabled = 0'); $viewer = $this->getEngine()->getConfig('viewer');
$rows = id(new PhabricatorMacroQuery())
->setViewer($viewer)
->withStatus(PhabricatorMacroQuery::STATUS_ACTIVE)
->execute();
foreach ($rows as $row) { foreach ($rows as $row) {
$this->images[$row->getName()] = $row->getFilePHID(); $this->images[$row->getName()] = $row->getFilePHID();
} }

View File

@@ -158,9 +158,10 @@ final class PhabricatorObjectHandleData {
return mpull($xactions, null, 'getPHID'); return mpull($xactions, null, 'getPHID');
case PhabricatorPHIDConstants::PHID_TYPE_MCRO: case PhabricatorPHIDConstants::PHID_TYPE_MCRO:
$macros = id(new PhabricatorFileImageMacro())->loadAllWhere( $macros = id(new PhabricatorMacroQuery())
'phid IN (%Ls)', ->setViewer($this->viewer)
$phids); ->withPHIDs($phids)
->execute();
return mpull($macros, null, 'getPHID'); return mpull($macros, null, 'getPHID');
case PhabricatorPHIDConstants::PHID_TYPE_PSTE: case PhabricatorPHIDConstants::PHID_TYPE_PSTE: