2011-04-11 12:02:19 +02:00
|
|
|
<?php
|
|
|
|
|
2013-02-26 23:57:41 +01:00
|
|
|
final class DiffusionRemarkupRule
|
2013-02-27 17:04:54 +01:00
|
|
|
extends PhabricatorRemarkupRuleObject {
|
2011-04-11 12:02:19 +02:00
|
|
|
|
2012-12-12 22:46:28 +01:00
|
|
|
protected function getObjectNamePrefix() {
|
2013-02-27 17:04:54 +01:00
|
|
|
return '';
|
2011-04-11 12:02:19 +02:00
|
|
|
}
|
|
|
|
|
2013-05-20 22:27:03 +02:00
|
|
|
protected function getObjectNamePrefixBeginsWithWordCharacter() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:46:28 +01:00
|
|
|
protected function getObjectIDPattern() {
|
2013-02-27 17:04:54 +01:00
|
|
|
$min_unqualified = PhabricatorRepository::MINIMUM_UNQUALIFIED_HASH;
|
|
|
|
$min_qualified = PhabricatorRepository::MINIMUM_QUALIFIED_HASH;
|
|
|
|
|
2013-06-25 00:55:08 +02:00
|
|
|
// NOTE: The "(?<!/)" negative lookbehind prevents this rule from matching
|
|
|
|
// hashes or hash-like substrings in most URLs. For example, this will not
|
|
|
|
// match: http://www.example.com/article/28903218328/
|
|
|
|
|
2013-02-27 17:04:54 +01:00
|
|
|
return
|
2013-03-01 22:31:11 +01:00
|
|
|
'r[A-Z]+[1-9]\d*'.
|
2013-02-27 17:04:54 +01:00
|
|
|
'|'.
|
2013-03-01 22:31:11 +01:00
|
|
|
'r[A-Z]+[a-f0-9]{'.$min_qualified.',40}'.
|
2013-02-27 17:04:54 +01:00
|
|
|
'|'.
|
2013-06-25 00:55:08 +02:00
|
|
|
'(?<!/)[a-f0-9]{'.$min_unqualified.',40}';
|
2013-02-27 17:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadObjects(array $ids) {
|
|
|
|
$viewer = $this->getEngine()->getConfig('viewer');
|
|
|
|
$min_qualified = PhabricatorRepository::MINIMUM_QUALIFIED_HASH;
|
|
|
|
|
|
|
|
$commits = id(new DiffusionCommitQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withIdentifiers($ids)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
if (!$commits) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = array_fuse($ids);
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
foreach ($commits as $commit) {
|
|
|
|
$prefix = 'r'.$commit->getRepository()->getCallsign();
|
|
|
|
$suffix = $commit->getCommitIdentifier();
|
|
|
|
|
|
|
|
if ($commit->getRepository()->isSVN()) {
|
|
|
|
if (isset($ids[$prefix.$suffix])) {
|
|
|
|
$result[$prefix.$suffix][] = $commit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This awkward contruction is so we can link the commits up in O(N)
|
|
|
|
// time instead of O(N^2).
|
|
|
|
for ($ii = $min_qualified; $ii <= strlen($suffix); $ii++) {
|
|
|
|
$part = substr($suffix, 0, $ii);
|
|
|
|
if (isset($ids[$prefix.$part])) {
|
|
|
|
$result[$prefix.$part][] = $commit;
|
|
|
|
}
|
|
|
|
if (isset($ids[$part])) {
|
|
|
|
$result[$part][] = $commit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($result as $identifier => $commits) {
|
|
|
|
if (count($commits) == 1) {
|
|
|
|
$result[$identifier] = head($commits);
|
|
|
|
} else {
|
|
|
|
// This reference is ambiguous -- it matches more than one commit -- so
|
|
|
|
// don't link it. We could potentially improve this, but it's a bit
|
|
|
|
// tricky since the superclass expects a single object.
|
|
|
|
unset($result[$identifier]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2011-04-11 12:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|