[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/owners/conduit/ -> OwnersQueryConduitAPIMethod.php (source)

   1  <?php
   2  
   3  final class OwnersQueryConduitAPIMethod extends OwnersConduitAPIMethod {
   4  
   5    public function getAPIMethodName() {
   6      return 'owners.query';
   7    }
   8  
   9    public function getMethodDescription() {
  10      return 'Query for packages by one of the following: repository/path, '.
  11        'packages with a given user or project owner, or packages affiliated '.
  12        'with a user (owned by either the user or a project they are a member '.
  13        'of.) You should only provide at most one search query.';
  14    }
  15  
  16    public function defineParamTypes() {
  17      return array(
  18        'userOwner'                  => 'optional string',
  19        'projectOwner'               => 'optional string',
  20        'userAffiliated'             => 'optional string',
  21        'repositoryCallsign'         => 'optional string',
  22        'path'                       => 'optional string',
  23      );
  24    }
  25  
  26    public function defineReturnType() {
  27      return 'dict<phid -> dict of package info>';
  28    }
  29  
  30    public function defineErrorTypes() {
  31      return array(
  32        'ERR-INVALID-USAGE' =>
  33          'Provide one of a single owner phid (user/project), a single '.
  34          'affiliated user phid (user), or a repository/path.',
  35        'ERR-INVALID-PARAMETER' => 'parameter should be a phid',
  36        'ERR_REP_NOT_FOUND'  => 'The repository callsign is not recognized',
  37      );
  38    }
  39  
  40    protected static function queryAll() {
  41      return id(new PhabricatorOwnersPackage())->loadAll();
  42    }
  43  
  44    protected static function queryByOwner($owner) {
  45      $is_valid_phid =
  46        phid_get_type($owner) == PhabricatorPeopleUserPHIDType::TYPECONST ||
  47        phid_get_type($owner) == PhabricatorProjectProjectPHIDType::TYPECONST;
  48  
  49      if (!$is_valid_phid) {
  50        throw id(new ConduitException('ERR-INVALID-PARAMETER'))
  51          ->setErrorDescription(
  52            'Expected user/project PHID for owner, got '.$owner);
  53      }
  54  
  55      $owners = id(new PhabricatorOwnersOwner())->loadAllWhere(
  56        'userPHID = %s',
  57        $owner);
  58  
  59      $package_ids = mpull($owners, 'getPackageID');
  60      $packages = array();
  61      foreach ($package_ids as $id) {
  62        $packages[] = id(new PhabricatorOwnersPackage())->load($id);
  63      }
  64      return $packages;
  65    }
  66  
  67    private static function queryByPath(
  68      PhabricatorUser $viewer,
  69      $repo_callsign,
  70      $path) {
  71  
  72      $repository = id(new PhabricatorRepositoryQuery())
  73        ->setViewer($viewer)
  74        ->withCallsigns(array($repo_callsign))
  75        ->executeOne();
  76  
  77      if (!$repository) {
  78        throw id(new ConduitException('ERR_REP_NOT_FOUND'))
  79          ->setErrorDescription(
  80            'Repository callsign '.$repo_callsign.' not recognized');
  81      }
  82      if ($path == null) {
  83        return PhabricatorOwnersPackage::loadPackagesForRepository($repository);
  84      } else {
  85        return PhabricatorOwnersPackage::loadOwningPackages(
  86          $repository, $path);
  87      }
  88    }
  89  
  90    public static function buildPackageInformationDictionaries($packages) {
  91      assert_instances_of($packages, 'PhabricatorOwnersPackage');
  92  
  93      $result = array();
  94      foreach ($packages as $package) {
  95        $p_owners = $package->loadOwners();
  96        $p_paths = $package->loadPaths();
  97  
  98        $owners = array_values(mpull($p_owners, 'getUserPHID'));
  99        $paths = array();
 100        foreach ($p_paths as $p) {
 101          $paths[] = array($p->getRepositoryPHID(), $p->getPath());
 102        }
 103  
 104        $result[$package->getPHID()] = array(
 105          'phid' => $package->getPHID(),
 106          'name' => $package->getName(),
 107          'description' => $package->getDescription(),
 108          'primaryOwner' => $package->getPrimaryOwnerPHID(),
 109          'owners' => $owners,
 110          'paths' => $paths,
 111        );
 112      }
 113      return $result;
 114    }
 115  
 116    protected function execute(ConduitAPIRequest $request) {
 117      $is_owner_query =
 118        ($request->getValue('userOwner') ||
 119         $request->getValue('projectOwner')) ?
 120        1 : 0;
 121  
 122      $is_affiliated_query = $request->getValue('userAffiliated') ? 1 : 0;
 123  
 124      $repo = $request->getValue('repositoryCallsign');
 125      $path = $request->getValue('path');
 126      $is_path_query = $repo ? 1 : 0;
 127  
 128      if ($is_owner_query + $is_path_query + $is_affiliated_query === 0) {
 129        // if no search terms are provided, return everything
 130        $packages = self::queryAll();
 131      } else if ($is_owner_query + $is_path_query + $is_affiliated_query > 1) {
 132        // otherwise, exactly one of these should be provided
 133        throw new ConduitException('ERR-INVALID-USAGE');
 134      }
 135  
 136      if ($is_affiliated_query) {
 137        $query = id(new PhabricatorOwnersPackageQuery())
 138          ->setViewer($request->getUser());
 139  
 140        $query->withOwnerPHIDs(array($request->getValue('userAffiliated')));
 141  
 142        $packages = $query->execute();
 143      } else if ($is_owner_query) {
 144        $owner = nonempty(
 145          $request->getValue('userOwner'),
 146          $request->getValue('projectOwner'));
 147  
 148        $packages = self::queryByOwner($owner);
 149  
 150      } else if ($is_path_query) {
 151        $packages = self::queryByPath($request->getUser(), $repo, $path);
 152      }
 153  
 154      return self::buildPackageInformationDictionaries($packages);
 155    }
 156  
 157  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1