diff --git a/resources/sql/patches/133.imagemacro.sql b/resources/sql/patches/133.imagemacro.sql new file mode 100644 index 0000000000..94a27ea13f --- /dev/null +++ b/resources/sql/patches/133.imagemacro.sql @@ -0,0 +1,2 @@ +ALTER IGNORE TABLE `phabricator_file`.`file_imagemacro` + ADD UNIQUE `name` (`name`); diff --git a/src/applications/files/controller/macrolist/PhabricatorFileMacroListController.php b/src/applications/files/controller/macrolist/PhabricatorFileMacroListController.php index b5128d4e8d..0f1a5a8c9a 100644 --- a/src/applications/files/controller/macrolist/PhabricatorFileMacroListController.php +++ b/src/applications/files/controller/macrolist/PhabricatorFileMacroListController.php @@ -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( diff --git a/src/applications/files/controller/macrolist/__init__.php b/src/applications/files/controller/macrolist/__init__.php index a14eac9d80..a550defe3c 100644 --- a/src/applications/files/controller/macrolist/__init__.php +++ b/src/applications/files/controller/macrolist/__init__.php @@ -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'); diff --git a/src/infrastructure/markup/remarkup/markuprule/imagemacro/PhabricatorRemarkupRuleImageMacro.php b/src/infrastructure/markup/remarkup/markuprule/imagemacro/PhabricatorRemarkupRuleImageMacro.php index 27a1e5a5f4..96248b38a1 100644 --- a/src/infrastructure/markup/remarkup/markuprule/imagemacro/PhabricatorRemarkupRuleImageMacro.php +++ b/src/infrastructure/markup/remarkup/markuprule/imagemacro/PhabricatorRemarkupRuleImageMacro.php @@ -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) {