/**
  * 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;
 }
 /**
  * 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();
 }
 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;
 }
 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;
 }
<?php

$table = new ManiphestTask();
$conn_w = $table->establishConnection('w');
$user_table = new PhabricatorUser();
$user_conn = $user_table->establishConnection('r');
foreach (new LiskMigrationIterator($table) as $task) {
    $id = $task->getID();
    echo pht('Checking task %s...', "T{$id}") . "\n";
    $owner_phid = $task->getOwnerPHID();
    if (!$owner_phid && !$task->getOwnerOrdering()) {
        // No owner and no ordering; we're all set.
        continue;
    }
    $owner_row = queryfx_one($user_conn, 'SELECT * FROM %T WHERE phid = %s', $user_table->getTableName(), $owner_phid);
    if ($owner_row) {
        $value = $owner_row['userName'];
    } else {
        $value = null;
    }
    if ($value !== $task->getOwnerOrdering()) {
        queryfx($conn_w, 'UPDATE %T SET ownerOrdering = %ns WHERE id = %d', $table->getTableName(), $value, $task->getID());
    }
}
echo pht('Done.') . "\n";
<?php

$task_table = new ManiphestTask();
$conn_w = $task_table->establishConnection('w');
$rows = new LiskRawMigrationIterator($conn_w, 'maniphest_transaction');
$conn_w->openTransaction();
// NOTE: These were the correct table names at the time of this patch.
$xaction_table_name = 'maniphest_transactionpro';
$comment_table_name = 'maniphest_transaction_comment';
foreach ($rows as $row) {
    $row_id = $row['id'];
    $task_id = $row['taskID'];
    echo pht('Migrating row %d (%s)...', $row_id, "T{$task_id}") . "\n";
    $task_row = queryfx_one($conn_w, 'SELECT phid FROM %T WHERE id = %d', $task_table->getTableName(), $task_id);
    if (!$task_row) {
        echo pht('Skipping, no such task.') . "\n";
        continue;
    }
    $task_phid = $task_row['phid'];
    $has_comment = strlen(trim($row['comments']));
    $xaction_type = $row['transactionType'];
    $xaction_old = $row['oldValue'];
    $xaction_new = $row['newValue'];
    $xaction_source = idx($row, 'contentSource', '');
    $xaction_meta = $row['metadata'];
    // Convert "aux" (auxiliary field) transactions to proper CustomField
    // transactions. The formats are very similar, except that the type constant
    // is different and the auxiliary key should be prefixed.
    if ($xaction_type == 'aux') {
        $xaction_meta = @json_decode($xaction_meta, true);
        $xaction_meta = nonempty($xaction_meta, array());
 /**
  * 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();
 }