1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 08:42:41 +01:00

Recognize "#project" as a formal object name

Summary: Ref T4264. Ref T4262. Ref T2628. Ref T3190. To write Herald object rules which bind to a project, I want to take the low budget approach and have the user just type `#project` into a text field. Formally recognize `#project` as an object name, by moving all the existing stuff from the remarkup rule to the PHID type declaration.

Test Plan: Typed `#project` into jump nav and `phid.lookup` in Conduit. Typed `#project` into Remarkup.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T3190, T4264, T2628, T4262

Differential Revision: https://secure.phabricator.com/D7882
This commit is contained in:
epriestley 2014-01-03 12:24:21 -08:00
parent 637e3f38f3
commit ee2680794f
2 changed files with 66 additions and 34 deletions

View file

@ -41,9 +41,57 @@ final class PhabricatorProjectPHIDTypeProject extends PhabricatorPHIDType {
} }
} }
public function canLoadNamedObject($name) { public static function getProjectMonogramPatternFragment() {
// TODO: We should be able to load named projects by hashtag, e.g. "#yolo". // NOTE: This explicitly does not match strings which contain only
return false; // digits, because digit strings like "#123" are used to reference tasks at
// Facebook and are somewhat conventional in general.
return '[^\s.!,:;]*[^\s\d.!,:;]+[^\s.!,:;]*';
} }
public function canLoadNamedObject($name) {
$fragment = self::getProjectMonogramPatternFragment();
return preg_match('/^#'.$fragment.'$/i', $name);
}
public function loadNamedObjects(
PhabricatorObjectQuery $query,
array $names) {
// If the user types "#YoloSwag", we still want to match "#yoloswag", so
// we normalize, query, and then map back to the original inputs.
$map = array();
foreach ($names as $key => $slug) {
$map[$this->normalizeSlug(substr($slug, 1))][] = $slug;
}
$projects = id(new PhabricatorProjectQuery())
->setViewer($query->getViewer())
->withPhrictionSlugs(array_keys($map))
->execute();
$result = array();
foreach ($projects as $project) {
$slugs = array($project->getPhrictionSlug());
foreach ($slugs as $slug) {
foreach ($map[$slug] as $original) {
$result[$original] = $project;
}
}
}
return $result;
}
private function normalizeSlug($slug) {
// NOTE: We're using phutil_utf8_strtolower() (and not PhabricatorSlug's
// normalize() method) because this normalization should be only somewhat
// liberal. We want "#YOLO" to match against "#yolo", but "#\\yo!!lo"
// should not. normalize() strips out most punctuation and leads to
// excessively aggressive matches.
return phutil_utf8_strtolower($slug).'/';
}
} }

View file

@ -11,50 +11,34 @@ final class ProjectRemarkupRule
} }
protected function getObjectIDPattern() { protected function getObjectIDPattern() {
// NOTE: This explicitly does not match strings which contain only return
// digits, because digit strings like "#123" are used to reference tasks at PhabricatorProjectPHIDTypeProject::getProjectMonogramPatternFragment();
// Facebook and are somewhat conventional in general.
return '[^\s.!,:;]*[^\s\d.!,:;]+[^\s.!,:;]*';
} }
protected function loadObjects(array $ids) { protected function loadObjects(array $ids) {
$viewer = $this->getEngine()->getConfig('viewer'); $viewer = $this->getEngine()->getConfig('viewer');
// If the user types "#YoloSwag", we still want to match "#yoloswag", so // Put the "#" back on the front of these IDs.
// we normalize, query, and then map back to the original inputs. $names = array();
foreach ($ids as $id) {
$map = array(); $names[] = '#'.$id;
foreach ($ids as $key => $slug) {
$map[$this->normalizeSlug($slug)][] = $slug;
} }
$projects = id(new PhabricatorProjectQuery()) // Issue a query by object name.
$query = id(new PhabricatorObjectQuery())
->setViewer($viewer) ->setViewer($viewer)
->withPhrictionSlugs(array_keys($map)) ->withNames($names);
->execute();
$query->execute();
$projects = $query->getNamedResults();
// Slice the "#" off again.
$result = array(); $result = array();
foreach ($projects as $project) { foreach ($projects as $name => $project) {
$slugs = array($project->getPhrictionSlug()); $result[substr($name, 1)] = $project;
foreach ($slugs as $slug) {
foreach ($map[$slug] as $original) {
$result[$original] = $project;
}
}
} }
return $result; return $result;
} }
private function normalizeSlug($slug) {
// NOTE: We're using phutil_utf8_strtolower() (and not PhabricatorSlug's
// normalize() method) because this normalization should be only somewhat
// liberal. We want "#YOLO" to match against "#yolo", but "#\\yo!!lo"
// should not. normalize() strips out most punctuation and leads to
// excessively aggressive matches.
return phutil_utf8_strtolower($slug).'/';
}
} }