Summary: Currently, the author of an image macro is read from the attached file. This is messy and necessitates a join, and is not always correct. Instead, store the data when the macro is created. This lays the groundwork for generalizing ApplicationSearch here. Ref T2625. Test Plan: Migrated existing macros, created a new macro, checked web UI. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2625 Differential Revision: https://secure.phabricator.com/D6071
142 lines
2.8 KiB
PHP
142 lines
2.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group phriction
|
|
*/
|
|
final class PhabricatorMacroQuery
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
|
|
|
private $ids;
|
|
private $phids;
|
|
private $authors;
|
|
private $names;
|
|
private $nameLike;
|
|
|
|
private $status = 'status-any';
|
|
const STATUS_ANY = 'status-any';
|
|
const STATUS_ACTIVE = 'status-active';
|
|
|
|
public function withIDs(array $ids) {
|
|
$this->ids = $ids;
|
|
return $this;
|
|
}
|
|
|
|
public function withPHIDs(array $phids) {
|
|
$this->phids = $phids;
|
|
return $this;
|
|
}
|
|
|
|
public function withAuthorPHIDs(array $authors) {
|
|
$this->authors = $authors;
|
|
return $this;
|
|
}
|
|
|
|
public function withNameLike($name) {
|
|
$this->nameLike = $name;
|
|
return $this;
|
|
}
|
|
|
|
public function withNames(array $names) {
|
|
$this->names = $names;
|
|
return $this;
|
|
}
|
|
|
|
public function withStatus($status) {
|
|
$this->status = $status;
|
|
return $this;
|
|
}
|
|
|
|
protected function loadPage() {
|
|
$macro_table = new PhabricatorFileImageMacro();
|
|
$conn = $macro_table->establishConnection('r');
|
|
|
|
$rows = queryfx_all(
|
|
$conn,
|
|
'SELECT m.* FROM %T m %Q %Q %Q',
|
|
$macro_table->getTableName(),
|
|
$this->buildWhereClause($conn),
|
|
$this->buildOrderClause($conn),
|
|
$this->buildLimitClause($conn));
|
|
|
|
return $macro_table->loadAllFromArray($rows);
|
|
}
|
|
|
|
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
|
|
$where = array();
|
|
|
|
if ($this->ids) {
|
|
$where[] = qsprintf(
|
|
$conn,
|
|
'm.id IN (%Ld)',
|
|
$this->ids);
|
|
}
|
|
|
|
if ($this->phids) {
|
|
$where[] = qsprintf(
|
|
$conn,
|
|
'm.phid IN (%Ls)',
|
|
$this->phids);
|
|
}
|
|
|
|
if ($this->authors) {
|
|
$where[] = qsprintf(
|
|
$conn,
|
|
'm.authorPHID IN (%Ls)',
|
|
$this->authors);
|
|
}
|
|
|
|
if ($this->nameLike) {
|
|
$where[] = qsprintf(
|
|
$conn,
|
|
'm.name LIKE %~',
|
|
$this->nameLike);
|
|
}
|
|
|
|
if ($this->names) {
|
|
$where[] = qsprintf(
|
|
$conn,
|
|
'm.name IN (%Ls)',
|
|
$this->names);
|
|
}
|
|
|
|
if ($this->status == self::STATUS_ACTIVE) {
|
|
$where[] = qsprintf(
|
|
$conn,
|
|
'm.isDisabled = 0');
|
|
}
|
|
|
|
$where[] = $this->buildPagingClause($conn);
|
|
|
|
return $this->formatWhereClause($where);
|
|
}
|
|
|
|
protected function willFilterPage(array $macros) {
|
|
if (!$macros) {
|
|
return array();
|
|
}
|
|
|
|
$file_phids = mpull($macros, 'getFilePHID');
|
|
$files = id(new PhabricatorFileQuery())
|
|
->setViewer($this->getViewer())
|
|
->withPHIDs($file_phids)
|
|
->execute();
|
|
$files = mpull($files, null, 'getPHID');
|
|
|
|
foreach ($macros as $key => $macro) {
|
|
$file = idx($files, $macro->getFilePHID());
|
|
if (!$file) {
|
|
unset($macros[$key]);
|
|
continue;
|
|
}
|
|
$macro->attachFile($file);
|
|
}
|
|
|
|
return $macros;
|
|
}
|
|
|
|
protected function getPagingColumn() {
|
|
return 'm.id';
|
|
}
|
|
|
|
}
|