mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-18 19:40:55 +01:00
Improve image macros
Summary: Couple of small improvements: - Delete `randomon` macro. - Make name unique (deleting current conflicts randomly). - Image macro must be alone on the line. - Filter by name. Test Plan: Run SQL. /file/macro/ /file/macro/?name=imagemacro Try to create conflicting name. Write this comment: Test imagemacro. imagemacro Reviewers: aran, epriestley Reviewed By: epriestley CC: epriestley, Koolvin Differential Revision: https://secure.phabricator.com/D2230
This commit is contained in:
parent
1ff68376f5
commit
1f1c7a34b7
4 changed files with 58 additions and 48 deletions
2
resources/sql/patches/133.imagemacro.sql
Normal file
2
resources/sql/patches/133.imagemacro.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
ALTER IGNORE TABLE `phabricator_file`.`file_imagemacro`
|
||||
ADD UNIQUE `name` (`name`);
|
|
@ -23,25 +23,31 @@ final class PhabricatorFileMacroListController
|
|||
|
||||
$request = $this->getRequest();
|
||||
|
||||
$pager = new AphrontPagerView();
|
||||
$pager->setOffset($request->getInt('page'));
|
||||
|
||||
$macro_table = new PhabricatorFileImageMacro();
|
||||
$macros = $macro_table->loadAllWhere(
|
||||
'1 = 1 ORDER BY id DESC LIMIT %d, %d',
|
||||
$pager->getOffset(),
|
||||
$pager->getPageSize());
|
||||
if ($request->getStr('name') !== null) {
|
||||
$macros = $macro_table->loadAllWhere(
|
||||
'name LIKE %~',
|
||||
$request->getStr('name'));
|
||||
} else {
|
||||
$pager = new AphrontPagerView();
|
||||
$pager->setOffset($request->getInt('page'));
|
||||
|
||||
// Get an exact count since the size here is reasonably going to be a few
|
||||
// thousand at most in any reasonable case.
|
||||
$count = queryfx_one(
|
||||
$macro_table->establishConnection('r'),
|
||||
'SELECT COUNT(*) N FROM %T',
|
||||
$macro_table->getTableName());
|
||||
$count = $count['N'];
|
||||
$macros = $macro_table->loadAllWhere(
|
||||
'1 = 1 ORDER BY id DESC LIMIT %d, %d',
|
||||
$pager->getOffset(),
|
||||
$pager->getPageSize());
|
||||
|
||||
$pager->setCount($count);
|
||||
$pager->setURI($request->getRequestURI(), 'page');
|
||||
// Get an exact count since the size here is reasonably going to be a few
|
||||
// thousand at most in any reasonable case.
|
||||
$count = queryfx_one(
|
||||
$macro_table->establishConnection('r'),
|
||||
'SELECT COUNT(*) N FROM %T',
|
||||
$macro_table->getTableName());
|
||||
$count = $count['N'];
|
||||
|
||||
$pager->setCount($count);
|
||||
$pager->setURI($request->getRequestURI(), 'page');
|
||||
}
|
||||
|
||||
$file_phids = mpull($macros, 'getFilePHID');
|
||||
|
||||
|
@ -111,15 +117,40 @@ final class PhabricatorFileMacroListController
|
|||
'action',
|
||||
));
|
||||
|
||||
$filter_form = id(new AphrontFormView())
|
||||
->setMethod('GET')
|
||||
->setAction('/file/macro/')
|
||||
->setUser($request->getUser())
|
||||
->appendChild(
|
||||
id(new AphrontFormTextControl())
|
||||
->setName('name')
|
||||
->setLabel('Name')
|
||||
->setValue($request->getStr('name')))
|
||||
->appendChild(
|
||||
id(new AphrontFormSubmitControl())
|
||||
->setValue('Filter Image Macros'));
|
||||
|
||||
$filter_view = new AphrontListFilterView();
|
||||
$filter_view->appendChild($filter_form);
|
||||
$filter_view->addButton(
|
||||
phutil_render_tag(
|
||||
'a',
|
||||
array(
|
||||
'href' => '/file/macro/edit/',
|
||||
'class' => 'green button',
|
||||
),
|
||||
'New Image Macro'));
|
||||
|
||||
$panel = new AphrontPanelView();
|
||||
$panel->appendChild($table);
|
||||
|
||||
$panel->setHeader('Image Macros');
|
||||
$panel->setCreateButton('New Image Macro', '/file/macro/edit/');
|
||||
$panel->appendChild($pager);
|
||||
if ($request->getStr('name') === null) {
|
||||
$panel->appendChild($pager);
|
||||
}
|
||||
|
||||
$side_nav = new PhabricatorFileSideNavView();
|
||||
$side_nav->setSelectedFilter('all_macros');
|
||||
$side_nav->appendChild($filter_view);
|
||||
$side_nav->appendChild($panel);
|
||||
|
||||
return $this->buildStandardPageResponse(
|
||||
|
|
|
@ -15,6 +15,10 @@ phutil_require_module('phabricator', 'infrastructure/javelin/markup');
|
|||
phutil_require_module('phabricator', 'storage/queryfx');
|
||||
phutil_require_module('phabricator', 'view/control/pager');
|
||||
phutil_require_module('phabricator', 'view/control/table');
|
||||
phutil_require_module('phabricator', 'view/form/base');
|
||||
phutil_require_module('phabricator', 'view/form/control/submit');
|
||||
phutil_require_module('phabricator', 'view/form/control/text');
|
||||
phutil_require_module('phabricator', 'view/layout/listfilter');
|
||||
phutil_require_module('phabricator', 'view/layout/panel');
|
||||
|
||||
phutil_require_module('phutil', 'markup');
|
||||
|
|
|
@ -22,52 +22,25 @@
|
|||
final class PhabricatorRemarkupRuleImageMacro
|
||||
extends PhutilRemarkupRule {
|
||||
|
||||
const RANDOM_IMAGE_NAME = 'randomon';
|
||||
private $images = array();
|
||||
private $hash = 0;
|
||||
|
||||
public function __construct() {
|
||||
$rows = id(new PhabricatorFileImageMacro())->loadAll();
|
||||
foreach ($rows as $row) {
|
||||
$this->images[$row->getName()] = $row->getFilePHID();
|
||||
}
|
||||
$this->images[self::RANDOM_IMAGE_NAME] = '';
|
||||
$this->hash = 0;
|
||||
}
|
||||
|
||||
public function apply($text) {
|
||||
return preg_replace_callback(
|
||||
'@\b([a-zA-Z0-9_\-]+)\b@',
|
||||
'@^([a-zA-Z0-9_\-]+)$@m',
|
||||
array($this, 'markupImageMacro'),
|
||||
$text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Silly function for generating some 'randomness' based on the
|
||||
* words in the text
|
||||
*/
|
||||
private function updateHash($word) {
|
||||
// Simple Jenkins hash
|
||||
for ($ii = 0; $ii < strlen($word); $ii++) {
|
||||
$this->hash += ord($word[$ii]);
|
||||
$this->hash += ($this->hash << 10);
|
||||
$this->hash ^= ($this->hash >> 6);
|
||||
}
|
||||
}
|
||||
|
||||
public function markupImageMacro($matches) {
|
||||
// Update the hash that is used for defining each 'randomon' image. This way
|
||||
// each 'randomon' image will be different, but they won't change when the
|
||||
// text is updated.
|
||||
$this->updateHash($matches[1]);
|
||||
|
||||
if (array_key_exists($matches[1], $this->images)) {
|
||||
if ($matches[1] === self::RANDOM_IMAGE_NAME) {
|
||||
$keys = array_keys($this->images);
|
||||
$phid = $this->images[$keys[$this->hash % count($this->images)]];
|
||||
} else {
|
||||
$phid = $this->images[$matches[1]];
|
||||
}
|
||||
$phid = $this->images[$matches[1]];
|
||||
|
||||
$file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $phid);
|
||||
if ($file) {
|
||||
|
|
Loading…
Reference in a new issue