2011-06-24 19:59:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-02-29 00:04:36 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-06-24 19:59:57 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group markup
|
|
|
|
*/
|
2012-03-14 00:21:04 +01:00
|
|
|
final class PhabricatorRemarkupRuleMention
|
2011-06-24 19:59:57 +02:00
|
|
|
extends PhutilRemarkupRule {
|
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
const KEY_RULE_MENTION = 'rule.mention';
|
|
|
|
const KEY_RULE_MENTION_ORIGINAL = 'rule.mention.original';
|
|
|
|
|
|
|
|
const KEY_MENTIONED = 'phabricator.mentioned-user-phids';
|
2011-06-24 19:59:57 +02:00
|
|
|
|
|
|
|
|
2012-03-28 07:23:16 +02:00
|
|
|
// NOTE: Negative lookahead for period prevents us from picking up email
|
|
|
|
// addresses, while allowing constructs like "@tomo, lol". The negative
|
|
|
|
// lookbehind for a word character prevents us from matching "mail@lists"
|
|
|
|
// while allowing "@tomo/@mroch". The negative lookahead prevents us from
|
|
|
|
// matching "@joe.com" while allowing us to match "hey, @joe.".
|
|
|
|
const REGEX = '/(?<!\w)@([a-zA-Z0-9]+)\b(?![.]\w)/';
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2012-03-28 07:23:16 +02:00
|
|
|
public function apply($text) {
|
2011-07-27 21:41:21 +02:00
|
|
|
return preg_replace_callback(
|
2012-03-28 07:23:16 +02:00
|
|
|
self::REGEX,
|
2011-07-27 21:41:21 +02:00
|
|
|
array($this, 'markupMention'),
|
|
|
|
$text);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function markupMention($matches) {
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
$token = $engine->storeText('');
|
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
// Store the original text exactly so we can preserve casing if it doesn't
|
|
|
|
// resolve into a username.
|
|
|
|
$original_key = self::KEY_RULE_MENTION_ORIGINAL;
|
|
|
|
$original = $engine->getTextMetadata($original_key, array());
|
|
|
|
$original[$token] = $matches[1];
|
|
|
|
$engine->setTextMetadata($original_key, $original);
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$metadata_key = self::KEY_RULE_MENTION;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
2011-11-30 17:45:31 +01:00
|
|
|
$username = strtolower($matches[1]);
|
2011-07-27 21:41:21 +02:00
|
|
|
if (empty($metadata[$username])) {
|
|
|
|
$metadata[$username] = array();
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
$metadata[$username][] = $token;
|
|
|
|
$engine->setTextMetadata($metadata_key, $metadata);
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
public function didMarkupText() {
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
|
|
|
$metadata_key = self::KEY_RULE_MENTION;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
|
|
|
if (empty($metadata)) {
|
|
|
|
// No mentions, or we already processed them.
|
|
|
|
return;
|
|
|
|
}
|
2011-06-24 19:59:57 +02:00
|
|
|
|
2011-11-30 17:45:31 +01:00
|
|
|
$original_key = self::KEY_RULE_MENTION_ORIGINAL;
|
|
|
|
$original = $engine->getTextMetadata($original_key, array());
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$usernames = array_keys($metadata);
|
2011-06-24 19:59:57 +02:00
|
|
|
$user_table = new PhabricatorUser();
|
|
|
|
$real_user_names = queryfx_all(
|
|
|
|
$user_table->establishConnection('r'),
|
2012-05-03 04:00:42 +02:00
|
|
|
'SELECT username, phid, realName, isDisabled
|
|
|
|
FROM %T
|
|
|
|
WHERE username IN (%Ls)',
|
2011-06-24 19:59:57 +02:00
|
|
|
$user_table->getTableName(),
|
|
|
|
$usernames);
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$actual_users = array();
|
2011-06-24 20:50:19 +02:00
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$mentioned_key = self::KEY_MENTIONED;
|
|
|
|
$mentioned = $engine->getTextMetadata($mentioned_key, array());
|
2011-06-24 19:59:57 +02:00
|
|
|
foreach ($real_user_names as $row) {
|
2011-07-27 21:41:21 +02:00
|
|
|
$actual_users[strtolower($row['username'])] = $row;
|
2011-06-24 20:50:19 +02:00
|
|
|
$mentioned[$row['phid']] = $row['phid'];
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
$engine->setTextMetadata($mentioned_key, $mentioned);
|
|
|
|
|
|
|
|
foreach ($metadata as $username => $tokens) {
|
|
|
|
$exists = isset($actual_users[$username]);
|
2012-05-03 04:00:42 +02:00
|
|
|
if (!$exists) {
|
|
|
|
$class = 'phabricator-remarkup-mention-unknown';
|
|
|
|
} else if ($actual_users[$username]['isDisabled']) {
|
|
|
|
$class = 'phabricator-remarkup-mention-disabled';
|
|
|
|
} else {
|
|
|
|
$class = 'phabricator-remarkup-mention-exists';
|
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
|
|
|
|
if ($exists) {
|
|
|
|
$tag = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'class' => $class,
|
2012-02-29 00:04:36 +01:00
|
|
|
'href' => '/p/'.$actual_users[$username]['username'].'/',
|
2011-07-27 21:41:21 +02:00
|
|
|
'target' => '_blank',
|
|
|
|
'title' => $actual_users[$username]['realName'],
|
|
|
|
),
|
2012-02-29 00:04:36 +01:00
|
|
|
phutil_escape_html('@'.$actual_users[$username]['username']));
|
2011-11-30 17:45:31 +01:00
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$engine->overwriteStoredText($token, $tag);
|
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
} else {
|
2011-11-30 17:45:31 +01:00
|
|
|
// NOTE: The structure here is different from the 'exists' branch,
|
|
|
|
// because we want to preserve the original text capitalization and it
|
|
|
|
// may differ for each token.
|
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$tag = phutil_render_tag(
|
|
|
|
'span',
|
|
|
|
array(
|
|
|
|
'class' => $class,
|
|
|
|
),
|
|
|
|
phutil_escape_html('@'.idx($original, $token, $username)));
|
|
|
|
$engine->overwriteStoredText($token, $tag);
|
|
|
|
}
|
2011-07-27 21:41:21 +02:00
|
|
|
}
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 21:41:21 +02:00
|
|
|
// Don't re-process these mentions.
|
|
|
|
$engine->setTextMetadata($metadata_key, array());
|
2011-06-24 19:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|