1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 00:42:41 +01:00

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) {
$macros = id(new PhabricatorFileImageMacro())->loadAll();
$files = array();
if ($macros) {
$files = id(new PhabricatorFile())->loadAllWhere(
'phid IN (%Ls)',
mpull($macros, 'getFilePHID'));
$files = mpull($files, null, 'getPHID');
}
$macros = id(new PhabricatorMacroQuery())
->setViewer($request->getUser())
->execute();
$results = array();
foreach ($macros as $macro) {
if (empty($files[$macro->getFilePHID()])) {
continue;
}
$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();
}
$macro = id(new PhabricatorFileImageMacro())->load($this->id);
$macro = id(new PhabricatorMacroQuery())
->setViewer($user)
->withIDs(array($this->id))
->executeOne();
if (!$macro) {
return new Aphront404Response();
}

View file

@ -13,7 +13,15 @@ final class PhabricatorMacroDisableController
$request = $this->getRequest();
$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) {
return new Aphront404Response();
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -9,7 +9,8 @@ final class PhabricatorMacroQuery
private $ids;
private $phids;
private $authors;
private $name;
private $names;
private $nameLike;
private $status = 'status-any';
const STATUS_ANY = 'status-any';
@ -31,7 +32,12 @@ final class PhabricatorMacroQuery
}
public function withNameLike($name) {
$this->name = $name;
$this->nameLike = $name;
return $this;
}
public function withNames(array $names) {
$this->names = $names;
return $this;
}
@ -94,11 +100,18 @@ final class PhabricatorMacroQuery
$this->authors);
}
if ($this->name) {
if ($this->nameLike) {
$where[] = qsprintf(
$conn,
'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) {

View file

@ -18,8 +18,12 @@ final class PhabricatorRemarkupRuleImageMacro
public function markupImageMacro($matches) {
if ($this->images === null) {
$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) {
$this->images[$row->getName()] = $row->getFilePHID();
}

View file

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