~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to phabricator/src/applications/phragment/conduit/PhragmentQueryFragmentsConduitAPIMethod.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • Revision ID: package-import@ubuntu.com-20141023204926-ar20vnfjqwxysrce
Tags: upstream-0~git20141023
ImportĀ upstreamĀ versionĀ 0~git20141023

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
final class PhragmentQueryFragmentsConduitAPIMethod
 
4
  extends PhragmentConduitAPIMethod {
 
5
 
 
6
  public function getAPIMethodName() {
 
7
    return 'phragment.queryfragments';
 
8
  }
 
9
 
 
10
  public function getMethodStatus() {
 
11
    return self::METHOD_STATUS_UNSTABLE;
 
12
  }
 
13
 
 
14
  public function getMethodDescription() {
 
15
    return pht('Query fragments based on their paths.');
 
16
  }
 
17
 
 
18
  public function defineParamTypes() {
 
19
    return array(
 
20
      'paths' => 'required list<string>',
 
21
    );
 
22
  }
 
23
 
 
24
  public function defineReturnType() {
 
25
    return 'nonempty dict';
 
26
  }
 
27
 
 
28
  public function defineErrorTypes() {
 
29
    return array(
 
30
      'ERR_BAD_FRAGMENT' => 'No such fragment exists',
 
31
    );
 
32
  }
 
33
 
 
34
  protected function execute(ConduitAPIRequest $request) {
 
35
    $paths = $request->getValue('paths');
 
36
 
 
37
    $fragments = id(new PhragmentFragmentQuery())
 
38
      ->setViewer($request->getUser())
 
39
      ->withPaths($paths)
 
40
      ->execute();
 
41
    $fragments = mpull($fragments, null, 'getPath');
 
42
    foreach ($paths as $path) {
 
43
      if (!array_key_exists($path, $fragments)) {
 
44
        throw new ConduitException('ERR_BAD_FRAGMENT');
 
45
      }
 
46
    }
 
47
 
 
48
    $results = array();
 
49
    foreach ($fragments as $path => $fragment) {
 
50
      $mappings = $fragment->getFragmentMappings(
 
51
        $request->getUser(),
 
52
        $fragment->getPath());
 
53
 
 
54
      $file_phids = mpull(mpull($mappings, 'getLatestVersion'), 'getFilePHID');
 
55
      $files = id(new PhabricatorFileQuery())
 
56
        ->setViewer($request->getUser())
 
57
        ->withPHIDs($file_phids)
 
58
        ->execute();
 
59
      $files = mpull($files, null, 'getPHID');
 
60
 
 
61
      $result = array();
 
62
      foreach ($mappings as $cpath => $child) {
 
63
        $file_phid = $child->getLatestVersion()->getFilePHID();
 
64
        if (!isset($files[$file_phid])) {
 
65
          // Skip any files we don't have permission to access.
 
66
          continue;
 
67
        }
 
68
 
 
69
        $file = $files[$file_phid];
 
70
        $cpath = substr($child->getPath(), strlen($fragment->getPath()) + 1);
 
71
        $result[] = array(
 
72
          'phid' => $child->getPHID(),
 
73
          'phidVersion' => $child->getLatestVersionPHID(),
 
74
          'path' => $cpath,
 
75
          'hash' => $file->getContentHash(),
 
76
          'version' => $child->getLatestVersion()->getSequence(),
 
77
          'uri' => $file->getViewURI(),
 
78
        );
 
79
      }
 
80
      $results[$path] = $result;
 
81
    }
 
82
    return $results;
 
83
  }
 
84
 
 
85
}