[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phid/query/ -> PhabricatorObjectListQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorObjectListQuery {
   4  
   5    private $viewer;
   6    private $objectList;
   7    private $allowedTypes = array();
   8    private $allowPartialResults;
   9  
  10    public function setAllowPartialResults($allow_partial_results) {
  11      $this->allowPartialResults = $allow_partial_results;
  12      return $this;
  13    }
  14  
  15    public function getAllowPartialResults() {
  16      return $this->allowPartialResults;
  17    }
  18  
  19    public function setAllowedTypes(array $allowed_types) {
  20      $this->allowedTypes = $allowed_types;
  21      return $this;
  22    }
  23  
  24    public function getAllowedTypes() {
  25      return $this->allowedTypes;
  26    }
  27  
  28    public function setViewer(PhabricatorUser $viewer) {
  29      $this->viewer = $viewer;
  30      return $this;
  31    }
  32  
  33    public function getViewer() {
  34      return $this->viewer;
  35    }
  36  
  37    public function setObjectList($object_list) {
  38      $this->objectList = $object_list;
  39      return $this;
  40    }
  41  
  42    public function getObjectList() {
  43      return $this->objectList;
  44    }
  45  
  46    public function execute() {
  47      $names = $this->getObjectList();
  48      $names = array_unique(array_filter(preg_split('/[\s,]+/', $names)));
  49  
  50      $objects = $this->loadObjects($names);
  51  
  52      $types = array();
  53      foreach ($objects as $name => $object) {
  54        $types[phid_get_type($object->getPHID())][] = $name;
  55      }
  56  
  57      $invalid = array();
  58      if ($this->getAllowedTypes()) {
  59        $allowed = array_fuse($this->getAllowedTypes());
  60        foreach ($types as $type => $names_of_type) {
  61          if (empty($allowed[$type])) {
  62            $invalid[] = $names_of_type;
  63          }
  64        }
  65      }
  66      $invalid = array_mergev($invalid);
  67  
  68      $missing = array();
  69      foreach ($names as $name) {
  70        if (empty($objects[$name])) {
  71          $missing[] = $name;
  72        }
  73      }
  74  
  75      // NOTE: We could couple this less tightly with Differential, but it is
  76      // currently the only thing that uses it, and we'd have to add a lot of
  77      // extra API to loosen this. It's not clear that this will be useful
  78      // elsewhere any time soon, so let's cross that bridge when we come to it.
  79  
  80      if (!$this->getAllowPartialResults()) {
  81        if ($invalid && $missing) {
  82          throw new DifferentialFieldParseException(
  83            pht(
  84              'The objects you have listed include objects of the wrong '.
  85              'type (%s) and objects which do not exist (%s).',
  86              implode(', ', $invalid),
  87              implode(', ', $missing)));
  88        } else if ($invalid) {
  89          throw new DifferentialFieldParseException(
  90            pht(
  91              'The objects you have listed include objects of the wrong '.
  92              'type (%s).',
  93              implode(', ', $invalid)));
  94        } else if ($missing) {
  95          throw new DifferentialFieldParseException(
  96            pht(
  97              'The objects you have listed include objects which do not '.
  98              'exist (%s).',
  99              implode(', ', $missing)));
 100        }
 101      }
 102  
 103      return array_values(array_unique(mpull($objects, 'getPHID')));
 104    }
 105  
 106    private function loadObjects($names) {
 107      // First, try to load visible objects using monograms. This covers most
 108      // object types, but does not cover users or user email addresses.
 109      $query = id(new PhabricatorObjectQuery())
 110        ->setViewer($this->getViewer())
 111        ->withNames($names);
 112  
 113      $query->execute();
 114      $objects = $query->getNamedResults();
 115  
 116      $results = array();
 117      foreach ($names as $key => $name) {
 118        if (isset($objects[$name])) {
 119          $results[$name] = $objects[$name];
 120          unset($names[$key]);
 121        }
 122      }
 123  
 124      if ($names) {
 125        // We still have some symbols we haven't been able to resolve, so try to
 126        // load users. Try by username first...
 127        $users = id(new PhabricatorPeopleQuery())
 128          ->setViewer($this->getViewer())
 129          ->withUsernames($names)
 130          ->execute();
 131  
 132        $user_map = array();
 133        foreach ($users as $user) {
 134          $user_map[phutil_utf8_strtolower($user->getUsername())] = $user;
 135        }
 136  
 137        foreach ($names as $key => $name) {
 138          $normal_name = phutil_utf8_strtolower($name);
 139          if (isset($user_map[$normal_name])) {
 140            $results[$name] = $user_map[$normal_name];
 141            unset($names[$key]);
 142          }
 143        }
 144      }
 145  
 146      $mailing_list_app = PhabricatorApplication::getByClass(
 147        'PhabricatorMailingListsApplication');
 148      if ($mailing_list_app->isInstalled()) {
 149        if ($names) {
 150          // We still haven't been able to resolve everything; try mailing lists
 151          // by name as a last resort.
 152          $lists = id(new PhabricatorMailingListQuery())
 153            ->setViewer($this->getViewer())
 154            ->withNames($names)
 155            ->execute();
 156  
 157          $lists = mpull($lists, null, 'getName');
 158          foreach ($names as $key => $name) {
 159            if (isset($lists[$name])) {
 160              $results[$name] = $lists[$name];
 161              unset($names[$key]);
 162            }
 163          }
 164        }
 165      }
 166  
 167      return $results;
 168    }
 169  
 170  
 171  }


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