1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 22:10:55 +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:
lkassianik 2015-11-06 13:47:52 -08:00
parent 6fe2377cc2
commit 268fac25d5
4 changed files with 93 additions and 0 deletions

View file

@ -2643,6 +2643,8 @@ phutil_register_library_map(array(
'PhabricatorPhurlApplication' => 'applications/phurl/application/PhabricatorPhurlApplication.php', 'PhabricatorPhurlApplication' => 'applications/phurl/application/PhabricatorPhurlApplication.php',
'PhabricatorPhurlController' => 'applications/phurl/controller/PhabricatorPhurlController.php', 'PhabricatorPhurlController' => 'applications/phurl/controller/PhabricatorPhurlController.php',
'PhabricatorPhurlDAO' => 'applications/phurl/storage/PhabricatorPhurlDAO.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', 'PhabricatorPhurlSchemaSpec' => 'applications/phurl/storage/PhabricatorPhurlSchemaSpec.php',
'PhabricatorPhurlURL' => 'applications/phurl/storage/PhabricatorPhurlURL.php', 'PhabricatorPhurlURL' => 'applications/phurl/storage/PhabricatorPhurlURL.php',
'PhabricatorPhurlURLAccessController' => 'applications/phurl/controller/PhabricatorPhurlURLAccessController.php', 'PhabricatorPhurlURLAccessController' => 'applications/phurl/controller/PhabricatorPhurlURLAccessController.php',
@ -6770,6 +6772,8 @@ phutil_register_library_map(array(
'PhabricatorPhurlApplication' => 'PhabricatorApplication', 'PhabricatorPhurlApplication' => 'PhabricatorApplication',
'PhabricatorPhurlController' => 'PhabricatorController', 'PhabricatorPhurlController' => 'PhabricatorController',
'PhabricatorPhurlDAO' => 'PhabricatorLiskDAO', 'PhabricatorPhurlDAO' => 'PhabricatorLiskDAO',
'PhabricatorPhurlLinkRemarkupRule' => 'PhutilRemarkupRule',
'PhabricatorPhurlRemarkupRule' => 'PhabricatorObjectRemarkupRule',
'PhabricatorPhurlSchemaSpec' => 'PhabricatorConfigSchemaSpec', 'PhabricatorPhurlSchemaSpec' => 'PhabricatorConfigSchemaSpec',
'PhabricatorPhurlURL' => array( 'PhabricatorPhurlURL' => array(
'PhabricatorPhurlDAO', 'PhabricatorPhurlDAO',

View file

@ -26,6 +26,13 @@ final class PhabricatorPhurlApplication extends PhabricatorApplication {
return true; return true;
} }
public function getRemarkupRules() {
return array(
new PhabricatorPhurlRemarkupRule(),
new PhabricatorPhurlLinkRemarkupRule(),
);
}
public function getRoutes() { public function getRoutes() {
return array( return array(
'/U(?P<id>[1-9]\d*)' => 'PhabricatorPhurlURLViewController', '/U(?P<id>[1-9]\d*)' => 'PhabricatorPhurlURLViewController',

View file

@ -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];
}
}
}

View file

@ -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();
}
}