PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/diffusion/query/filecontent/base/DiffusionFileContentQuery.php

http://github.com/facebook/phabricator
PHP | 147 lines | 97 code | 32 blank | 18 comment | 5 complexity | 06597055b6896ce2a3809ea37d8a3f1d MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * Copyright 2011 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. abstract class DiffusionFileContentQuery {
  18. private $request;
  19. private $needsBlame;
  20. private $fileContent;
  21. final private function __construct() {
  22. // <private>
  23. }
  24. final public static function newFromDiffusionRequest(
  25. DiffusionRequest $request) {
  26. $repository = $request->getRepository();
  27. switch ($repository->getVersionControlSystem()) {
  28. case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
  29. $class = 'DiffusionGitFileContentQuery';
  30. break;
  31. case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
  32. $class = 'DiffusionMercurialFileContentQuery';
  33. break;
  34. case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
  35. $class = 'DiffusionSvnFileContentQuery';
  36. break;
  37. default:
  38. throw new Exception("Unsupported VCS!");
  39. }
  40. PhutilSymbolLoader::loadClass($class);
  41. $query = new $class();
  42. $query->request = $request;
  43. return $query;
  44. }
  45. public function getSupportsBlameOnBlame() {
  46. return false;
  47. }
  48. public function getPrevRev($rev) {
  49. // TODO: support git once the 'parent' info of a commit is saved
  50. // to the database.
  51. throw new Exception("Unsupported VCS!");
  52. }
  53. final protected function getRequest() {
  54. return $this->request;
  55. }
  56. final public function loadFileContent() {
  57. $this->fileContent = $this->executeQuery();
  58. }
  59. abstract protected function executeQuery();
  60. final public function getRawData() {
  61. return $this->fileContent->getCorpus();
  62. }
  63. final public function getBlameData() {
  64. $raw_data = $this->getRawData();
  65. $text_list = array();
  66. $rev_list = array();
  67. $blame_dict = array();
  68. if (!$this->getNeedsBlame()) {
  69. $text_list = explode("\n", rtrim($raw_data));
  70. } else {
  71. foreach (explode("\n", rtrim($raw_data)) as $k => $line) {
  72. list($rev_id, $author, $text) = $this->tokenizeLine($line);
  73. $text_list[$k] = $text;
  74. $rev_list[$k] = $rev_id;
  75. if (!isset($blame_dict[$rev_id]) &&
  76. !isset($blame_dict[$rev_id]['author'] )) {
  77. $blame_dict[$rev_id]['author'] = $author;
  78. }
  79. }
  80. $repository = $this->getRequest()->getRepository();
  81. $commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
  82. 'repositoryID = %d AND commitIdentifier IN (%Ls)', $repository->getID(),
  83. array_unique($rev_list));
  84. foreach ($commits as $commit) {
  85. $blame_dict[$commit->getCommitIdentifier()]['epoch'] =
  86. $commit->getEpoch();
  87. }
  88. $commits_data = id(new PhabricatorRepositoryCommitData())->loadAllWhere(
  89. 'commitID IN (%Ls)',
  90. mpull($commits, 'getID'));
  91. $phids = array();
  92. foreach ($commits_data as $data) {
  93. $phids[] = $data->getCommitDetail('authorPHID');
  94. }
  95. $handles = id(new PhabricatorObjectHandleData(array_unique($phids)))
  96. ->loadHandles();
  97. foreach ($commits_data as $data) {
  98. if ($data->getCommitDetail('authorPHID')) {
  99. $commit_identifier =
  100. $commits[$data->getCommitID()]->getCommitIdentifier();
  101. $blame_dict[$commit_identifier]['handle'] =
  102. $handles[$data->getCommitDetail('authorPHID')];
  103. }
  104. }
  105. }
  106. return array($text_list, $rev_list, $blame_dict);
  107. }
  108. abstract protected function tokenizeLine($line);
  109. public function setNeedsBlame($needs_blame) {
  110. $this->needsBlame = $needs_blame;
  111. }
  112. public function getNeedsBlame() {
  113. return $this->needsBlame;
  114. }
  115. }