Path: blob/master/src/applications/diffusion/query/DiffusionQuery.php
12242 views
<?php12abstract class DiffusionQuery extends PhabricatorQuery {34private $request;56final protected function __construct() {7// <protected>8}910protected static function newQueryObject(11$base_class,12DiffusionRequest $request) {1314$repository = $request->getRepository();1516$obj = self::initQueryObject($base_class, $repository);17$obj->request = $request;1819return $obj;20}2122final protected static function initQueryObject(23$base_class,24PhabricatorRepository $repository) {2526$map = array(27PhabricatorRepositoryType::REPOSITORY_TYPE_GIT => 'Git',28PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL => 'Mercurial',29PhabricatorRepositoryType::REPOSITORY_TYPE_SVN => 'Svn',30);3132$name = idx($map, $repository->getVersionControlSystem());33if (!$name) {34throw new Exception(pht('Unsupported VCS!'));35}3637$class = str_replace('Diffusion', 'Diffusion'.$name, $base_class);38$obj = new $class();39return $obj;40}4142final protected function getRequest() {43return $this->request;44}4546final public static function callConduitWithDiffusionRequest(47PhabricatorUser $user,48DiffusionRequest $drequest,49$method,50array $params = array(),51$return_future = false) {5253$repository = $drequest->getRepository();5455$core_params = array(56'repository' => $repository->getPHID(),57);5859if ($drequest->getBranch() !== null) {60$core_params['branch'] = $drequest->getBranch();61}6263// If the method we're calling doesn't actually take some of the implicit64// parameters we derive from the DiffusionRequest, omit them.65$method_object = ConduitAPIMethod::getConduitMethod($method);66$method_params = $method_object->getParamTypes();67foreach ($core_params as $key => $value) {68if (empty($method_params[$key])) {69unset($core_params[$key]);70}71}7273$params = $params + $core_params;7475$future = $repository->newConduitFuture(76$user,77$method,78$params,79$drequest->getIsClusterRequest());8081if (!$return_future) {82return $future->resolve();83}8485return $future;86}8788public function execute() {89return $this->executeQuery();90}9192abstract protected function executeQuery();939495/* -( Query Utilities )---------------------------------------------------- */969798final public static function loadCommitsByIdentifiers(99array $identifiers,100DiffusionRequest $drequest) {101if (!$identifiers) {102return array();103}104105$commits = array();106$commit_data = array();107108$repository = $drequest->getRepository();109110$commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(111'repositoryID = %d AND commitIdentifier IN (%Ls)',112$repository->getID(),113$identifiers);114$commits = mpull($commits, null, 'getCommitIdentifier');115116// Build empty commit objects for every commit, so we can show unparsed117// commits in history views (as "Importing") instead of not showing them.118// This makes the process of importing and parsing commits clearer to the119// user.120121$commit_list = array();122foreach ($identifiers as $identifier) {123$commit_obj = idx($commits, $identifier);124if (!$commit_obj) {125$commit_obj = new PhabricatorRepositoryCommit();126$commit_obj->setRepositoryID($repository->getID());127$commit_obj->setCommitIdentifier($identifier);128$commit_obj->makeEphemeral();129}130$commit_list[$identifier] = $commit_obj;131}132$commits = $commit_list;133134$commit_ids = array_filter(mpull($commits, 'getID'));135if ($commit_ids) {136$commit_data = id(new PhabricatorRepositoryCommitData())->loadAllWhere(137'commitID in (%Ld)',138$commit_ids);139$commit_data = mpull($commit_data, null, 'getCommitID');140}141142foreach ($commits as $commit) {143if (!$commit->getID()) {144continue;145}146if (idx($commit_data, $commit->getID())) {147$commit->attachCommitData($commit_data[$commit->getID()]);148}149}150151return $commits;152}153154final public static function loadHistoryForCommitIdentifiers(155array $identifiers,156DiffusionRequest $drequest) {157158if (!$identifiers) {159return array();160}161162$repository = $drequest->getRepository();163$commits = self::loadCommitsByIdentifiers($identifiers, $drequest);164165if (!$commits) {166return array();167}168169$path = $drequest->getPath();170171$conn_r = $repository->establishConnection('r');172173$path_normal = DiffusionPathIDQuery::normalizePath($path);174$paths = queryfx_all(175$conn_r,176'SELECT id, path FROM %T WHERE pathHash IN (%Ls)',177PhabricatorRepository::TABLE_PATH,178array(md5($path_normal)));179$paths = ipull($paths, 'id', 'path');180$path_id = idx($paths, $path_normal);181182$commit_ids = array_filter(mpull($commits, 'getID'));183184$path_changes = array();185if ($path_id && $commit_ids) {186$path_changes = queryfx_all(187$conn_r,188'SELECT * FROM %T WHERE commitID IN (%Ld) AND pathID = %d',189PhabricatorRepository::TABLE_PATHCHANGE,190$commit_ids,191$path_id);192$path_changes = ipull($path_changes, null, 'commitID');193}194195$history = array();196foreach ($identifiers as $identifier) {197$item = new DiffusionPathChange();198$item->setCommitIdentifier($identifier);199$commit = idx($commits, $identifier);200if ($commit) {201$item->setCommit($commit);202try {203$item->setCommitData($commit->getCommitData());204} catch (Exception $ex) {205// Ignore, commit just doesn't have data.206}207$change = idx($path_changes, $commit->getID());208if ($change) {209$item->setChangeType($change['changeType']);210$item->setFileType($change['fileType']);211}212}213$history[] = $item;214}215216return $history;217}218}219220221