[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/almanac/query/ -> AlmanacInterfaceQuery.php (source)

   1  <?php
   2  
   3  final class AlmanacInterfaceQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $networkPHIDs;
   9    private $devicePHIDs;
  10    private $addresses;
  11  
  12    public function withIDs(array $ids) {
  13      $this->ids = $ids;
  14      return $this;
  15    }
  16  
  17    public function withPHIDs(array $phids) {
  18      $this->phids = $phids;
  19      return $this;
  20    }
  21  
  22    public function withNetworkPHIDs(array $phids) {
  23      $this->networkPHIDs = $phids;
  24      return $this;
  25    }
  26  
  27    public function withDevicePHIDs(array $phids) {
  28      $this->devicePHIDs = $phids;
  29      return $this;
  30    }
  31  
  32    public function withAddresses(array $addresses) {
  33      $this->addresses = $addresses;
  34      return $this;
  35    }
  36  
  37    protected function loadPage() {
  38      $table = new AlmanacInterface();
  39      $conn_r = $table->establishConnection('r');
  40  
  41      $data = queryfx_all(
  42        $conn_r,
  43        'SELECT * FROM %T %Q %Q %Q',
  44        $table->getTableName(),
  45        $this->buildWhereClause($conn_r),
  46        $this->buildOrderClause($conn_r),
  47        $this->buildLimitClause($conn_r));
  48  
  49      return $table->loadAllFromArray($data);
  50    }
  51  
  52    protected function willFilterPage(array $interfaces) {
  53      $network_phids = mpull($interfaces, 'getNetworkPHID');
  54      $device_phids = mpull($interfaces, 'getDevicePHID');
  55  
  56      $networks = id(new AlmanacNetworkQuery())
  57        ->setParentQuery($this)
  58        ->setViewer($this->getViewer())
  59        ->withPHIDs($network_phids)
  60        ->execute();
  61      $networks = mpull($networks, null, 'getPHID');
  62  
  63      $devices = id(new AlmanacDeviceQuery())
  64        ->setParentQuery($this)
  65        ->setViewer($this->getViewer())
  66        ->withPHIDs($device_phids)
  67        ->execute();
  68      $devices = mpull($devices, null, 'getPHID');
  69  
  70      foreach ($interfaces as $key => $interface) {
  71        $network = idx($networks, $interface->getNetworkPHID());
  72        $device = idx($devices, $interface->getDevicePHID());
  73        if (!$network || !$device) {
  74          $this->didRejectResult($interface);
  75          unset($interfaces[$key]);
  76          continue;
  77        }
  78  
  79        $interface->attachNetwork($network);
  80        $interface->attachDevice($device);
  81      }
  82  
  83      return $interfaces;
  84    }
  85  
  86    protected function buildWhereClause($conn_r) {
  87      $where = array();
  88  
  89      if ($this->ids !== null) {
  90        $where[] = qsprintf(
  91          $conn_r,
  92          'id IN (%Ld)',
  93          $this->ids);
  94      }
  95  
  96      if ($this->phids !== null) {
  97        $where[] = qsprintf(
  98          $conn_r,
  99          'phid IN (%Ls)',
 100          $this->phids);
 101      }
 102  
 103      if ($this->networkPHIDs !== null) {
 104        $where[] = qsprintf(
 105          $conn_r,
 106          'networkPHID IN (%Ls)',
 107          $this->networkPHIDs);
 108      }
 109  
 110      if ($this->devicePHIDs !== null) {
 111        $where[] = qsprintf(
 112          $conn_r,
 113          'devicePHID IN (%Ls)',
 114          $this->devicePHIDs);
 115      }
 116  
 117      if ($this->addresses !== null) {
 118        $parts = array();
 119        foreach ($this->addresses as $address) {
 120          $parts[] = qsprintf(
 121            $conn_r,
 122            '(networkPHID = %s AND address = %s AND port = %d)',
 123            $address->getNetworkPHID(),
 124            $address->getAddress(),
 125            $address->getPort());
 126        }
 127        $where[] = implode(' OR ', $parts);
 128      }
 129  
 130      $where[] = $this->buildPagingClause($conn_r);
 131  
 132      return $this->formatWhereClause($where);
 133    }
 134  
 135    public function getQueryApplicationClass() {
 136      return 'PhabricatorAlmanacApplication';
 137    }
 138  
 139  }


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