/**
  * Load all the tasks that have been recently closed.
  */
 private function loadRecentlyClosedTasks()
 {
     list($ignored, $window_epoch) = $this->getWindow();
     $table = new ManiphestTask();
     $xtable = new ManiphestTransaction();
     $conn_r = $table->establishConnection('r');
     // TODO: Gross. This table is not meant to be queried like this. Build
     // real stats tables.
     $open_status_list = array();
     foreach (ManiphestTaskStatus::getOpenStatusConstants() as $constant) {
         $open_status_list[] = json_encode((string) $constant);
     }
     $rows = queryfx_all($conn_r, 'SELECT t.id FROM %T t JOIN %T x ON x.objectPHID = t.phid
     WHERE t.status NOT IN (%Ls)
     AND x.oldValue IN (null, %Ls)
     AND x.newValue NOT IN (%Ls)
     AND t.dateModified >= %d
     AND x.dateCreated >= %d', $table->getTableName(), $xtable->getTableName(), ManiphestTaskStatus::getOpenStatusConstants(), $open_status_list, $open_status_list, $window_epoch, $window_epoch);
     if (!$rows) {
         return array();
     }
     $ids = ipull($rows, 'id');
     $query = id(new ManiphestTaskQuery())->setViewer($this->getRequest()->getUser())->withIDs($ids);
     switch ($this->view) {
         case 'project':
             $query->needProjectPHIDs(true);
             break;
     }
     return $query->execute();
 }
 /**
  * Load all the tasks that have been recently closed.
  */
 private function loadRecentlyClosedTasks()
 {
     list($ignored, $window_epoch) = $this->getWindow();
     $table = new ManiphestTask();
     $xtable = new ManiphestTransaction();
     $conn_r = $table->establishConnection('r');
     $tasks = queryfx_all($conn_r, 'SELECT t.* FROM %T t JOIN %T x ON x.taskID = t.id
     WHERE t.status != 0
     AND x.oldValue IN (null, %s, %s)
     AND x.newValue NOT IN (%s, %s)
     AND t.dateModified >= %d
     AND x.dateCreated >= %d', $table->getTableName(), $xtable->getTableName(), json_encode((int) ManiphestTaskStatus::STATUS_OPEN), json_encode((string) ManiphestTaskStatus::STATUS_OPEN), json_encode((int) ManiphestTaskStatus::STATUS_OPEN), json_encode((string) ManiphestTaskStatus::STATUS_OPEN), $window_epoch, $window_epoch);
     return id(new ManiphestTask())->loadAllFromArray($tasks);
 }
 public function execute()
 {
     $task_dao = new ManiphestTask();
     $conn = $task_dao->establishConnection('r');
     if ($this->calculateRows) {
         $calc = 'SQL_CALC_FOUND_ROWS';
     } else {
         $calc = '';
     }
     $where = array();
     $where[] = $this->buildTaskIDsWhereClause($conn);
     $where[] = $this->buildStatusWhereClause($conn);
     $where[] = $this->buildPriorityWhereClause($conn);
     $where[] = $this->buildAuthorWhereClause($conn);
     $where[] = $this->buildOwnerWhereClause($conn);
     $where[] = $this->buildSubscriberWhereClause($conn);
     $where[] = $this->buildProjectWhereClause($conn);
     $where[] = $this->buildXProjectWhereClause($conn);
     $where[] = $this->buildFullTextWhereClause($conn);
     $where = array_filter($where);
     if ($where) {
         $where = 'WHERE (' . implode(') AND (', $where) . ')';
     } else {
         $where = '';
     }
     $join = array();
     $join[] = $this->buildProjectJoinClause($conn);
     $join[] = $this->buildXProjectJoinClause($conn);
     $join[] = $this->buildSubscriberJoinClause($conn);
     $join = array_filter($join);
     if ($join) {
         $join = implode(' ', $join);
     } else {
         $join = '';
     }
     $having = '';
     $count = '';
     $group = '';
     if (count($this->projectPHIDs) > 1) {
         // If we're searching for more than one project:
         //  - We'll get multiple rows for tasks when they join the project table
         //    multiple times. We use GROUP BY to make them distinct again.
         //  - We want to treat the query as an intersection query, not a union
         //    query. We sum the project count and require it be the same as the
         //    number of projects we're searching for. (If 'anyProject' is set,
         //    we do union instead.)
         $group = 'GROUP BY task.id';
         if (!$this->anyProject) {
             $count = ', COUNT(project.projectPHID) projectCount';
             $having = qsprintf($conn, 'HAVING projectCount = %d', count($this->projectPHIDs));
         }
     }
     $order = $this->buildOrderClause($conn);
     $offset = (int) nonempty($this->offset, 0);
     $limit = (int) nonempty($this->limit, self::DEFAULT_PAGE_SIZE);
     if ($this->groupBy == self::GROUP_PROJECT) {
         $limit = PHP_INT_MAX;
         $offset = 0;
     }
     $data = queryfx_all($conn, 'SELECT %Q * %Q FROM %T task %Q %Q %Q %Q %Q LIMIT %d, %d', $calc, $count, $task_dao->getTableName(), $join, $where, $group, $having, $order, $offset, $limit);
     if ($this->calculateRows) {
         $count = queryfx_one($conn, 'SELECT FOUND_ROWS() N');
         $this->rowCount = $count['N'];
     } else {
         $this->rowCount = null;
     }
     $tasks = $task_dao->loadAllFromArray($data);
     if ($this->groupBy == self::GROUP_PROJECT) {
         $tasks = $this->applyGroupByProject($tasks);
     }
     return $tasks;
 }
 protected function loadPage()
 {
     $task_dao = new ManiphestTask();
     $conn = $task_dao->establishConnection('r');
     $where = $this->buildWhereClause($conn);
     $group_column = '';
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $group_column = qsprintf($conn, ', projectGroupName.indexedObjectPHID projectGroupPHID');
             break;
     }
     $rows = queryfx_all($conn, '%Q %Q FROM %T task %Q %Q %Q %Q %Q %Q', $this->buildSelectClause($conn), $group_column, $task_dao->getTableName(), $this->buildJoinClause($conn), $where, $this->buildGroupClause($conn), $this->buildHavingClause($conn), $this->buildOrderClause($conn), $this->buildLimitClause($conn));
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $data = ipull($rows, null, 'id');
             break;
         default:
             $data = $rows;
             break;
     }
     $tasks = $task_dao->loadAllFromArray($data);
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $results = array();
             foreach ($rows as $row) {
                 $task = clone $tasks[$row['id']];
                 $task->attachGroupByProjectPHID($row['projectGroupPHID']);
                 $results[] = $task;
             }
             $tasks = $results;
             break;
     }
     return $tasks;
 }
<?php

// NOTE: If you need to make any significant updates to this to deal with
// future changes to objects, it's probably better to just wipe the whole
// migration. This feature doesn't see overwhelming amounts of use, and users
// who do use it can recreate their queries fairly easily with the new
// interface. By the time this needs to be updated, the vast majority of
// users who it impacts will likely have migrated their data already.
$table = new ManiphestTask();
$conn_w = $table->establishConnection('w');
$search_table = new PhabricatorSearchQuery();
$search_conn_w = $search_table->establishConnection('w');
// See T1812. This is an old status constant from the time of this migration.
$old_open_status = 0;
echo "Updating saved Maniphest queries...\n";
$rows = new LiskRawMigrationIterator($conn_w, 'maniphest_savedquery');
foreach ($rows as $row) {
    $id = $row['id'];
    echo "Updating query {$id}...\n";
    $data = queryfx_one($search_conn_w, 'SELECT parameters FROM %T WHERE queryKey = %s', $search_table->getTableName(), $row['queryKey']);
    if (!$data) {
        echo "Unable to locate query data.\n";
        continue;
    }
    $data = json_decode($data['parameters'], true);
    if (!is_array($data)) {
        echo "Unable to decode query data.\n";
        continue;
    }
    if (idx($data, 'view') != 'custom') {
        echo "Query is not a custom query.\n";
 public function loadPage()
 {
     // TODO: (T603) It is possible for a user to find the PHID of a project
     // they can't see, then query for tasks in that project and deduce the
     // identity of unknown/invisible projects. Before we allow the user to
     // execute a project-based PHID query, we should verify that they
     // can see the project.
     $task_dao = new ManiphestTask();
     $conn = $task_dao->establishConnection('r');
     $where = array();
     $where[] = $this->buildTaskIDsWhereClause($conn);
     $where[] = $this->buildTaskPHIDsWhereClause($conn);
     $where[] = $this->buildStatusWhereClause($conn);
     $where[] = $this->buildStatusesWhereClause($conn);
     $where[] = $this->buildPrioritiesWhereClause($conn);
     $where[] = $this->buildAuthorWhereClause($conn);
     $where[] = $this->buildOwnerWhereClause($conn);
     $where[] = $this->buildSubscriberWhereClause($conn);
     $where[] = $this->buildProjectWhereClause($conn);
     $where[] = $this->buildAnyProjectWhereClause($conn);
     $where[] = $this->buildAnyUserProjectWhereClause($conn);
     $where[] = $this->buildXProjectWhereClause($conn);
     $where[] = $this->buildFullTextWhereClause($conn);
     if ($this->dateCreatedAfter) {
         $where[] = qsprintf($conn, 'task.dateCreated >= %d', $this->dateCreatedAfter);
     }
     if ($this->dateCreatedBefore) {
         $where[] = qsprintf($conn, 'task.dateCreated <= %d', $this->dateCreatedBefore);
     }
     if ($this->dateModifiedAfter) {
         $where[] = qsprintf($conn, 'task.dateModified >= %d', $this->dateModifiedAfter);
     }
     if ($this->dateModifiedBefore) {
         $where[] = qsprintf($conn, 'task.dateModified <= %d', $this->dateModifiedBefore);
     }
     $where[] = $this->buildPagingClause($conn);
     $where = $this->formatWhereClause($where);
     $having = '';
     $count = '';
     if (count($this->projectPHIDs) > 1) {
         // We want to treat the query as an intersection query, not a union
         // query. We sum the project count and require it be the same as the
         // number of projects we're searching for.
         $count = ', COUNT(project.dst) projectCount';
         $having = qsprintf($conn, 'HAVING projectCount = %d', count($this->projectPHIDs));
     }
     $order = $this->buildCustomOrderClause($conn);
     // TODO: Clean up this nonstandardness.
     if (!$this->getLimit()) {
         $this->setLimit(self::DEFAULT_PAGE_SIZE);
     }
     $group_column = '';
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $group_column = qsprintf($conn, ', projectGroupName.indexedObjectPHID projectGroupPHID');
             break;
     }
     $rows = queryfx_all($conn, 'SELECT task.* %Q %Q FROM %T task %Q %Q %Q %Q %Q %Q', $count, $group_column, $task_dao->getTableName(), $this->buildJoinsClause($conn), $where, $this->buildGroupClause($conn), $having, $order, $this->buildLimitClause($conn));
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $data = ipull($rows, null, 'id');
             break;
         default:
             $data = $rows;
             break;
     }
     $tasks = $task_dao->loadAllFromArray($data);
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $results = array();
             foreach ($rows as $row) {
                 $task = clone $tasks[$row['id']];
                 $task->attachGroupByProjectPHID($row['projectGroupPHID']);
                 $results[] = $task;
             }
             $tasks = $results;
             break;
     }
     return $tasks;
 }
 /**
  * Load all the tasks that have been recently closed.
  */
 private function loadRecentlyClosedTasks()
 {
     list(, , $window_epoch) = $this->getWindow($this->request);
     $table = new ManiphestTask();
     $xtable = new ManiphestTransaction();
     $conn_r = $table->establishConnection('r');
     // TODO: Gross. This table is not meant to be queried like this. Build
     // real stats tables.
     $rows = queryfx_all($conn_r, 'SELECT t.id FROM %T t JOIN %T x ON x.objectPHID = t.phid
     WHERE t.status NOT IN (%Ls)
     AND x.oldValue IN (null, %Ls)
     AND x.newValue NOT IN (%Ls)
     AND t.dateModified >= %d
     AND x.dateCreated >= %d', $table->getTableName(), $xtable->getTableName(), ManiphestTaskStatus::getOpenStatusConstants(), $this->getOpenStatusList(), $this->getOpenStatusList(), $window_epoch, $window_epoch);
     if (!$rows) {
         return array();
     }
     $ids = ipull($rows, 'id');
     return id(new ManiphestTaskQuery())->setViewer($this->request->getUser())->withIDs($ids)->needProjectPHIDs(true)->execute();
 }