mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-19 05:12:41 +01:00
Add Phurl Remarkup
Summary: Ref T9722, Add Phurl Remarkup as `((id))` or `((alias))` Test Plan: Add a comment to any object as `((id))` or `((alias))`. Make sure comment renders as a link. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin Maniphest Tasks: T9722 Differential Revision: https://secure.phabricator.com/D14427
This commit is contained in:
parent
6fe2377cc2
commit
268fac25d5
4 changed files with 93 additions and 0 deletions
|
@ -2643,6 +2643,8 @@ phutil_register_library_map(array(
|
|||
'PhabricatorPhurlApplication' => 'applications/phurl/application/PhabricatorPhurlApplication.php',
|
||||
'PhabricatorPhurlController' => 'applications/phurl/controller/PhabricatorPhurlController.php',
|
||||
'PhabricatorPhurlDAO' => 'applications/phurl/storage/PhabricatorPhurlDAO.php',
|
||||
'PhabricatorPhurlLinkRemarkupRule' => 'applications/phurl/remarkup/PhabricatorPhurlLinkRemarkupRule.php',
|
||||
'PhabricatorPhurlRemarkupRule' => 'applications/phurl/remarkup/PhabricatorPhurlRemarkupRule.php',
|
||||
'PhabricatorPhurlSchemaSpec' => 'applications/phurl/storage/PhabricatorPhurlSchemaSpec.php',
|
||||
'PhabricatorPhurlURL' => 'applications/phurl/storage/PhabricatorPhurlURL.php',
|
||||
'PhabricatorPhurlURLAccessController' => 'applications/phurl/controller/PhabricatorPhurlURLAccessController.php',
|
||||
|
@ -6770,6 +6772,8 @@ phutil_register_library_map(array(
|
|||
'PhabricatorPhurlApplication' => 'PhabricatorApplication',
|
||||
'PhabricatorPhurlController' => 'PhabricatorController',
|
||||
'PhabricatorPhurlDAO' => 'PhabricatorLiskDAO',
|
||||
'PhabricatorPhurlLinkRemarkupRule' => 'PhutilRemarkupRule',
|
||||
'PhabricatorPhurlRemarkupRule' => 'PhabricatorObjectRemarkupRule',
|
||||
'PhabricatorPhurlSchemaSpec' => 'PhabricatorConfigSchemaSpec',
|
||||
'PhabricatorPhurlURL' => array(
|
||||
'PhabricatorPhurlDAO',
|
||||
|
|
|
@ -26,6 +26,13 @@ final class PhabricatorPhurlApplication extends PhabricatorApplication {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function getRemarkupRules() {
|
||||
return array(
|
||||
new PhabricatorPhurlRemarkupRule(),
|
||||
new PhabricatorPhurlLinkRemarkupRule(),
|
||||
);
|
||||
}
|
||||
|
||||
public function getRoutes() {
|
||||
return array(
|
||||
'/U(?P<id>[1-9]\d*)' => 'PhabricatorPhurlURLViewController',
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorPhurlLinkRemarkupRule extends PhutilRemarkupRule {
|
||||
|
||||
public function getPriority() {
|
||||
return 200.0;
|
||||
}
|
||||
|
||||
public function apply($text) {
|
||||
// `((U123))` remarkup link to `/u/123`
|
||||
// `((alias))` remarkup link to `/u/alias`
|
||||
return preg_replace_callback(
|
||||
'/\(\(([^ )]+)\)\)/',
|
||||
array($this, 'markupLink'),
|
||||
$text);
|
||||
}
|
||||
|
||||
public function markupLink(array $matches) {
|
||||
$engine = $this->getEngine();
|
||||
$viewer = $engine->getConfig('viewer');
|
||||
$text_mode = $engine->isTextMode();
|
||||
|
||||
if (!$this->isFlatText($matches[0])) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
$ref = $matches[1];
|
||||
|
||||
if (ctype_digit($ref)) {
|
||||
$phurls = id(new PhabricatorPhurlURLQuery())
|
||||
->setViewer($viewer)
|
||||
->withIDs(array($ref))
|
||||
->execute();
|
||||
} else {
|
||||
$phurls = id(new PhabricatorPhurlURLQuery())
|
||||
->setViewer($viewer)
|
||||
->withAliases(array($ref))
|
||||
->execute();
|
||||
}
|
||||
|
||||
$phurl = head($phurls);
|
||||
|
||||
if ($phurl) {
|
||||
if ($text_mode) {
|
||||
return $phurl->getName().' <'.$phurl->getLongURL().'>';
|
||||
}
|
||||
|
||||
$link = phutil_tag(
|
||||
'a',
|
||||
array(
|
||||
'href' => $phurl->getLongURL(),
|
||||
'target' => '_blank',
|
||||
),
|
||||
$phurl->getName());
|
||||
|
||||
return $this->getEngine()->storeText($link);
|
||||
} else {
|
||||
return $matches[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorPhurlRemarkupRule
|
||||
extends PhabricatorObjectRemarkupRule {
|
||||
|
||||
protected function getObjectNamePrefix() {
|
||||
return 'U';
|
||||
}
|
||||
|
||||
protected function loadObjects(array $ids) {
|
||||
$viewer = $this->getEngine()->getConfig('viewer');
|
||||
|
||||
return id(new PhabricatorPhurlURLQuery())
|
||||
->setViewer($viewer)
|
||||
->withIDs($ids)
|
||||
->execute();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue