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:
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,10 +23,15 @@ final class PhabricatorFileMacroListController
|
|||||||
|
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
|
|
||||||
|
$macro_table = new PhabricatorFileImageMacro();
|
||||||
|
if ($request->getStr('name') !== null) {
|
||||||
|
$macros = $macro_table->loadAllWhere(
|
||||||
|
'name LIKE %~',
|
||||||
|
$request->getStr('name'));
|
||||||
|
} else {
|
||||||
$pager = new AphrontPagerView();
|
$pager = new AphrontPagerView();
|
||||||
$pager->setOffset($request->getInt('page'));
|
$pager->setOffset($request->getInt('page'));
|
||||||
|
|
||||||
$macro_table = new PhabricatorFileImageMacro();
|
|
||||||
$macros = $macro_table->loadAllWhere(
|
$macros = $macro_table->loadAllWhere(
|
||||||
'1 = 1 ORDER BY id DESC LIMIT %d, %d',
|
'1 = 1 ORDER BY id DESC LIMIT %d, %d',
|
||||||
$pager->getOffset(),
|
$pager->getOffset(),
|
||||||
@@ -42,6 +47,7 @@ final class PhabricatorFileMacroListController
|
|||||||
|
|
||||||
$pager->setCount($count);
|
$pager->setCount($count);
|
||||||
$pager->setURI($request->getRequestURI(), 'page');
|
$pager->setURI($request->getRequestURI(), 'page');
|
||||||
|
}
|
||||||
|
|
||||||
$file_phids = mpull($macros, 'getFilePHID');
|
$file_phids = mpull($macros, 'getFilePHID');
|
||||||
|
|
||||||
@@ -111,15 +117,40 @@ final class PhabricatorFileMacroListController
|
|||||||
'action',
|
'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 = new AphrontPanelView();
|
||||||
$panel->appendChild($table);
|
$panel->appendChild($table);
|
||||||
|
|
||||||
$panel->setHeader('Image Macros');
|
$panel->setHeader('Image Macros');
|
||||||
$panel->setCreateButton('New Image Macro', '/file/macro/edit/');
|
if ($request->getStr('name') === null) {
|
||||||
$panel->appendChild($pager);
|
$panel->appendChild($pager);
|
||||||
|
}
|
||||||
|
|
||||||
$side_nav = new PhabricatorFileSideNavView();
|
$side_nav = new PhabricatorFileSideNavView();
|
||||||
$side_nav->setSelectedFilter('all_macros');
|
$side_nav->setSelectedFilter('all_macros');
|
||||||
|
$side_nav->appendChild($filter_view);
|
||||||
$side_nav->appendChild($panel);
|
$side_nav->appendChild($panel);
|
||||||
|
|
||||||
return $this->buildStandardPageResponse(
|
return $this->buildStandardPageResponse(
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ phutil_require_module('phabricator', 'infrastructure/javelin/markup');
|
|||||||
phutil_require_module('phabricator', 'storage/queryfx');
|
phutil_require_module('phabricator', 'storage/queryfx');
|
||||||
phutil_require_module('phabricator', 'view/control/pager');
|
phutil_require_module('phabricator', 'view/control/pager');
|
||||||
phutil_require_module('phabricator', 'view/control/table');
|
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('phabricator', 'view/layout/panel');
|
||||||
|
|
||||||
phutil_require_module('phutil', 'markup');
|
phutil_require_module('phutil', 'markup');
|
||||||
|
|||||||
@@ -22,52 +22,25 @@
|
|||||||
final class PhabricatorRemarkupRuleImageMacro
|
final class PhabricatorRemarkupRuleImageMacro
|
||||||
extends PhutilRemarkupRule {
|
extends PhutilRemarkupRule {
|
||||||
|
|
||||||
const RANDOM_IMAGE_NAME = 'randomon';
|
|
||||||
private $images = array();
|
private $images = array();
|
||||||
private $hash = 0;
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$rows = id(new PhabricatorFileImageMacro())->loadAll();
|
$rows = id(new PhabricatorFileImageMacro())->loadAll();
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$this->images[$row->getName()] = $row->getFilePHID();
|
$this->images[$row->getName()] = $row->getFilePHID();
|
||||||
}
|
}
|
||||||
$this->images[self::RANDOM_IMAGE_NAME] = '';
|
|
||||||
$this->hash = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function apply($text) {
|
public function apply($text) {
|
||||||
return preg_replace_callback(
|
return preg_replace_callback(
|
||||||
'@\b([a-zA-Z0-9_\-]+)\b@',
|
'@^([a-zA-Z0-9_\-]+)$@m',
|
||||||
array($this, 'markupImageMacro'),
|
array($this, 'markupImageMacro'),
|
||||||
$text);
|
$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) {
|
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 (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);
|
$file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $phid);
|
||||||
if ($file) {
|
if ($file) {
|
||||||
|
|||||||
Reference in New Issue
Block a user