diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 85400a5ebc..9f499bc955 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2454,6 +2454,7 @@ phutil_register_library_map(array( 'PhabricatorHandlePool' => 'applications/phid/handle/pool/PhabricatorHandlePool.php', 'PhabricatorHandlePoolTestCase' => 'applications/phid/handle/pool/__tests__/PhabricatorHandlePoolTestCase.php', 'PhabricatorHandleQuery' => 'applications/phid/query/PhabricatorHandleQuery.php', + 'PhabricatorHandleRemarkupRule' => 'applications/phid/remarkup/PhabricatorHandleRemarkupRule.php', 'PhabricatorHandlesEditField' => 'applications/transactions/editfield/PhabricatorHandlesEditField.php', 'PhabricatorHarbormasterApplication' => 'applications/harbormaster/application/PhabricatorHarbormasterApplication.php', 'PhabricatorHarbormasterConfigOptions' => 'applications/harbormaster/config/PhabricatorHarbormasterConfigOptions.php', @@ -6897,6 +6898,7 @@ phutil_register_library_map(array( 'PhabricatorHandlePool' => 'Phobject', 'PhabricatorHandlePoolTestCase' => 'PhabricatorTestCase', 'PhabricatorHandleQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', + 'PhabricatorHandleRemarkupRule' => 'PhutilRemarkupRule', 'PhabricatorHandlesEditField' => 'PhabricatorPHIDListEditField', 'PhabricatorHarbormasterApplication' => 'PhabricatorApplication', 'PhabricatorHarbormasterConfigOptions' => 'PhabricatorApplicationConfigOptions', diff --git a/src/applications/phid/remarkup/PhabricatorHandleRemarkupRule.php b/src/applications/phid/remarkup/PhabricatorHandleRemarkupRule.php new file mode 100644 index 0000000000..35920e16e0 --- /dev/null +++ b/src/applications/phid/remarkup/PhabricatorHandleRemarkupRule.php @@ -0,0 +1,109 @@ +getEngine(); + $viewer = $engine->getConfig('viewer'); + + if (!$this->isFlatText($matches[0])) { + return $matches[0]; + } + + $phid_type = phid_get_type($matches[1]); + if ($phid_type == PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) { + return $matches[0]; + } + + $token = $engine->storeText($matches[0]); + if ($engine->isTextMode()) { + return $token; + } + + $original_key = self::KEY_RULE_HANDLE_ORIGINAL; + $original = $engine->getTextMetadata($original_key, array()); + $original[$token] = $matches[0]; + $engine->setTextMetadata($original_key, $original); + + $metadata_key = self::KEY_RULE_HANDLE; + $metadata = $engine->getTextMetadata($metadata_key, array()); + $phid = $matches[1]; + if (empty($metadata[$phid])) { + $metadata[$phid] = array(); + } + $metadata[$phid][] = $token; + $engine->setTextMetadata($metadata_key, $metadata); + + return $token; + } + + public function didMarkupText() { + $engine = $this->getEngine(); + + $metadata_key = self::KEY_RULE_HANDLE; + + $metadata = $engine->getTextMetadata($metadata_key, array()); + if (empty($metadata)) { + // No mentions, or we already processed them. + return; + } + + $original_key = self::KEY_RULE_HANDLE_ORIGINAL; + $original = $engine->getTextMetadata($original_key, array()); + + $phids = array_keys($metadata); + + $handles = id(new PhabricatorHandleQuery()) + ->setViewer($this->getEngine()->getConfig('viewer')) + ->withPHIDs($phids) + ->execute(); + + foreach ($metadata as $phid => $tokens) { + $handle = idx($handles, $phid); + + if ($handle->isComplete()) { + if ($engine->isHTMLMailMode()) { + $href = $handle->getURI(); + $href = PhabricatorEnv::getProductionURI($href); + + $link = phutil_tag( + 'a', + array( + 'href' => $href, + 'style' => ' + border-color: #f1f7ff; + color: #19558d; + background-color: #f1f7ff; + border: 1px solid transparent; + border-radius: 3px; + font-weight: bold; + padding: 0 4px;', + ), + $handle->getLinkName()); + } else { + $link = $handle->renderTag(); + $link->setPHID($phid); + } + foreach ($tokens as $token) { + $engine->overwriteStoredText($token, $link); + } + } else { + foreach ($tokens as $token) { + $engine->overwriteStoredText($token, idx($original, $token)); + } + } + } + + $engine->setTextMetadata($metadata_key, array()); + } +} diff --git a/src/infrastructure/markup/PhabricatorMarkupEngine.php b/src/infrastructure/markup/PhabricatorMarkupEngine.php index c3d9dab02a..f32971646a 100644 --- a/src/infrastructure/markup/PhabricatorMarkupEngine.php +++ b/src/infrastructure/markup/PhabricatorMarkupEngine.php @@ -494,6 +494,7 @@ final class PhabricatorMarkupEngine extends Phobject { $rules[] = new PhabricatorIconRemarkupRule(); $rules[] = new PhabricatorEmojiRemarkupRule(); + $rules[] = new PhabricatorHandleRemarkupRule(); $applications = PhabricatorApplication::getAllInstalledApplications(); foreach ($applications as $application) {