diff --git a/src/applications/audit/editor/comment/PhabricatorAuditCommentEditor.php b/src/applications/audit/editor/comment/PhabricatorAuditCommentEditor.php index 2fa6cd5764..aa990e2ff0 100644 --- a/src/applications/audit/editor/comment/PhabricatorAuditCommentEditor.php +++ b/src/applications/audit/editor/comment/PhabricatorAuditCommentEditor.php @@ -1,404 +1,408 @@ commit = $commit; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function setAttachInlineComments($attach_inline_comments) { $this->attachInlineComments = $attach_inline_comments; return $this; } public function addComment(PhabricatorAuditComment $comment) { $commit = $this->commit; $user = $this->user; $other_comments = id(new PhabricatorAuditComment())->loadAllWhere( 'targetPHID = %s', $commit->getPHID()); $inline_comments = array(); if ($this->attachInlineComments) { $inline_comments = id(new PhabricatorAuditInlineComment())->loadAllWhere( 'authorPHID = %s AND commitPHID = %s AND auditCommentID IS NULL', $user->getPHID(), $commit->getPHID()); } $comment ->setActorPHID($user->getPHID()) ->setTargetPHID($commit->getPHID()) ->save(); if ($inline_comments) { foreach ($inline_comments as $inline) { $inline->setAuditCommentID($comment->getID()); $inline->save(); } } // When a user submits an audit comment, we update all the audit requests // they have authority over to reflect the most recent status. The general // idea here is that if audit has triggered for, e.g., several packages, but // a user owns all of them, they can clear the audit requirement in one go // without auditing the commit for each trigger. $audit_phids = self::loadAuditPHIDsForUser($this->user); $audit_phids = array_fill_keys($audit_phids, true); $requests = id(new PhabricatorRepositoryAuditRequest()) ->loadAllWhere( 'commitPHID = %s', $commit->getPHID()); $action = $comment->getAction(); // TODO: We should validate the action, currently we allow anyone to, e.g., // close an audit if they muck with form parameters. I'll followup with this // and handle the no-effect cases (e.g., closing and already-closed audit). $user_is_author = ($user->getPHID() == $commit->getAuthorPHID()); if ($action == PhabricatorAuditActionConstants::CLOSE) { // "Close" means wipe out all the concerns. $concerned_status = PhabricatorAuditStatusConstants::CONCERNED; foreach ($requests as $request) { if ($request->getAuditStatus() == $concerned_status) { $request->setAuditStatus(PhabricatorAuditStatusConstants::CLOSED); $request->save(); } } } else { $have_any_requests = false; foreach ($requests as $request) { if (empty($audit_phids[$request->getAuditorPHID()])) { continue; } $request_is_for_user = ($request->getAuditorPHID() == $user->getPHID()); $have_any_requests = true; $new_status = null; switch ($action) { case PhabricatorAuditActionConstants::COMMENT: // Comments don't change audit statuses. break; case PhabricatorAuditActionConstants::ACCEPT: if (!$user_is_author || $request_is_for_user) { // When modifying your own commits, you act only on behalf of // yourself, not your packages/projects -- the idea being that // you can't accept your own commits. $new_status = PhabricatorAuditStatusConstants::ACCEPTED; } break; case PhabricatorAuditActionConstants::CONCERN: if (!$user_is_author || $request_is_for_user) { // See above. $new_status = PhabricatorAuditStatusConstants::CONCERNED; } break; case PhabricatorAuditActionConstants::RESIGN: // NOTE: Resigning resigns ONLY your user request, not the requests // of any projects or packages you are a member of. if ($request_is_for_user) { $new_status = PhabricatorAuditStatusConstants::RESIGNED; } break; default: throw new Exception("Unknown action '{$action}'!"); } if ($new_status !== null) { $request->setAuditStatus($new_status); $request->save(); } } // If the user has no current authority over any audit trigger, make a // new one to represent their audit state. if (!$have_any_requests) { $new_status = null; switch ($action) { case PhabricatorAuditActionConstants::COMMENT: $new_status = PhabricatorAuditStatusConstants::AUDIT_NOT_REQUIRED; break; case PhabricatorAuditActionConstants::ACCEPT: $new_status = PhabricatorAuditStatusConstants::ACCEPTED; break; case PhabricatorAuditActionConstants::CONCERN: $new_status = PhabricatorAuditStatusConstants::CONCERNED; break; case PhabricatorAuditActionConstants::RESIGN: // If you're on an audit because of a package, we write an explicit // resign row to remove it from your queue. $new_status = PhabricatorAuditStatusConstants::RESIGNED; break; case PhabricatorAuditActionConstants::CLOSE: // Impossible to reach this block with 'close'. default: throw new Exception("Unknown or invalid action '{$action}'!"); } $request = id(new PhabricatorRepositoryAuditRequest()) ->setCommitPHID($commit->getPHID()) ->setAuditorPHID($user->getPHID()) ->setAuditStatus($new_status) ->setAuditReasons(array("Voluntary Participant")) ->save(); $requests[] = $request; } } $commit->updateAuditStatus($requests); $commit->save(); $this->publishFeedStory($comment, array_keys($audit_phids)); PhabricatorSearchCommitIndexer::indexCommit($commit); $this->sendMail($comment, $other_comments, $inline_comments); } /** * Load the PHIDs for all objects the user has the authority to act as an * audit for. This includes themselves, and any packages they are an owner * of. */ public static function loadAuditPHIDsForUser(PhabricatorUser $user) { $phids = array(); // The user can audit on their own behalf. $phids[$user->getPHID()] = true; // The user can audit on behalf of all packages they own. $owned_packages = id(new PhabricatorOwnersOwner())->loadAllWhere( 'userPHID = %s', $user->getPHID()); if ($owned_packages) { $packages = id(new PhabricatorOwnersPackage())->loadAllWhere( 'id IN (%Ld)', mpull($owned_packages, 'getPackageID')); foreach (mpull($packages, 'getPHID') as $phid) { $phids[$phid] = true; } } // The user can audit on behalf of all projects they are a member of. $query = new PhabricatorProjectQuery(); $query->setMembers(array($user->getPHID())); $projects = $query->execute(); foreach ($projects as $project) { $phids[$project->getPHID()] = true; } return array_keys($phids); } private function publishFeedStory( PhabricatorAuditComment $comment, array $more_phids) { $commit = $this->commit; $user = $this->user; $related_phids = array_merge( array( $user->getPHID(), $commit->getPHID(), ), $more_phids); id(new PhabricatorFeedStoryPublisher()) ->setRelatedPHIDs($related_phids) ->setStoryAuthorPHID($user->getPHID()) ->setStoryTime(time()) ->setStoryType(PhabricatorFeedStoryTypeConstants::STORY_AUDIT) ->setStoryData( array( 'commitPHID' => $commit->getPHID(), 'action' => $comment->getAction(), 'content' => $comment->getContent(), )) ->publish(); } private function sendMail( PhabricatorAuditComment $comment, array $other_comments, array $inline_comments) { + assert_instances_of($other_comments, 'PhabricatorAuditComment'); + assert_instances_of($inline_comments, 'PhabricatorAuditInlineComment'); + $commit = $this->commit; $data = $commit->loadCommitData(); $summary = $data->getSummary(); $commit_phid = $commit->getPHID(); $phids = array($commit_phid); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $handle = $handles[$commit_phid]; $name = $handle->getName(); $map = array( PhabricatorAuditActionConstants::CONCERN => 'Raised Concern', PhabricatorAuditActionConstants::ACCEPT => 'Accepted', PhabricatorAuditActionConstants::RESIGN => 'Resigned', PhabricatorAuditActionConstants::CLOSE => 'Closed', ); $verb = idx($map, $comment->getAction(), 'Commented On'); $reply_handler = self::newReplyHandlerForCommit($commit); $prefix = PhabricatorEnv::getEnvConfig('metamta.diffusion.subject-prefix'); $subject = "{$prefix} [{$verb}] {$name}: {$summary}"; $threading = self::getMailThreading($commit->getPHID()); list($thread_id, $thread_topic) = $threading; $is_new = !count($other_comments); $body = $this->renderMailBody( $comment, "{$name}: {$summary}", $handle, $reply_handler, $inline_comments); $email_to = array(); $author_phid = $data->getCommitDetail('authorPHID'); if ($author_phid) { $email_to[] = $author_phid; } $email_cc = array(); foreach ($other_comments as $other_comment) { $email_cc[] = $other_comment->getActorPHID(); } $phids = array_merge($email_to, $email_cc); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $template = id(new PhabricatorMetaMTAMail()) ->setSubject($subject) ->setFrom($comment->getActorPHID()) ->setThreadID($thread_id, $is_new) ->addHeader('Thread-Topic', $thread_topic) ->setRelatedPHID($commit->getPHID()) ->setIsBulk(true) ->setBody($body); $mails = $reply_handler->multiplexMail( $template, array_select_keys($handles, $email_to), array_select_keys($handles, $email_cc)); foreach ($mails as $mail) { $mail->saveAndSend(); } } public static function getMailThreading($phid) { return array( '', 'Diffusion Audit '.$phid, ); } public static function newReplyHandlerForCommit($commit) { $reply_handler = PhabricatorEnv::newObjectFromConfig( 'metamta.diffusion.reply-handler'); $reply_handler->setMailReceiver($commit); return $reply_handler; } private function renderMailBody( PhabricatorAuditComment $comment, $cname, PhabricatorObjectHandle $handle, PhabricatorMailReplyHandler $reply_handler, array $inline_comments) { + assert_instances_of($inline_comments, 'PhabricatorAuditInlineComment'); $commit = $this->commit; $user = $this->user; $name = $user->getUsername(); $verb = PhabricatorAuditActionConstants::getActionPastTenseVerb( $comment->getAction()); $body = array(); $body[] = "{$name} {$verb} commit {$cname}."; if ($comment->getContent()) { $body[] = $comment->getContent(); } if ($inline_comments) { $block = array(); $path_map = id(new DiffusionPathQuery()) ->withPathIDs(mpull($inline_comments, 'getPathID')) ->execute(); $path_map = ipull($path_map, 'path', 'id'); foreach ($inline_comments as $inline) { $path = idx($path_map, $inline->getPathID()); if ($path === null) { continue; } $start = $inline->getLineNumber(); $len = $inline->getLineLength(); if ($len) { $range = $start.'-'.($start + $len); } else { $range = $start; } $content = $inline->getContent(); $block[] = "{$path}:{$range} {$content}"; } $body[] = "INLINE COMMENTS\n ".implode("\n ", $block); } $body[] = "COMMIT\n ".PhabricatorEnv::getProductionURI($handle->getURI()); $reply_instructions = $reply_handler->getReplyHandlerInstructions(); if ($reply_instructions) { $body[] = "REPLY HANDLER ACTIONS\n ".$reply_instructions; } return implode("\n\n", $body)."\n"; } } diff --git a/src/applications/audit/view/commitlist/PhabricatorAuditCommitListView.php b/src/applications/audit/view/commitlist/PhabricatorAuditCommitListView.php index 58be5cbbdc..715293c32a 100644 --- a/src/applications/audit/view/commitlist/PhabricatorAuditCommitListView.php +++ b/src/applications/audit/view/commitlist/PhabricatorAuditCommitListView.php @@ -1,113 +1,115 @@ user = $user; return $this; } public function setNoDataString($no_data_string) { $this->noDataString = $no_data_string; return $this; } public function setCommits(array $commits) { + assert_instances_of($commits, 'PhabricatorRepositoryCommit'); $this->commits = $commits; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setAuthorityPHIDs(array $phids) { $this->authorityPHIDs = $phids; return $this; } public function getRequiredHandlePHIDs() { $phids = array(); foreach ($this->commits as $commit) { if ($commit->getAuthorPHID()) { $phids[$commit->getAuthorPHID()] = true; } $phids[$commit->getPHID()] = true; } return array_keys($phids); } private function getHandle($phid) { $handle = idx($this->handles, $phid); if (!$handle) { throw new Exception("No handle for '{$phid}'!"); } return $handle; } public function render() { $rows = array(); foreach ($this->commits as $commit) { $commit_name = $this->getHandle($commit->getPHID())->renderLink(); $author_name = null; if ($commit->getAuthorPHID()) { $author_name = $this->getHandle($commit->getAuthorPHID())->renderLink(); } $rows[] = array( $commit_name, $author_name, phutil_escape_html($commit->getCommitData()->getSummary()), PhabricatorAuditCommitStatusConstants::getStatusName( $commit->getAuditStatus()), phabricator_datetime($commit->getEpoch(), $this->user), ); } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Commit', 'Author', 'Summary', 'Audit Status', 'Date', )); $table->setColumnClasses( array( 'n', '', 'wide', '', '', )); if ($this->noDataString) { $table->setNoDataString($this->noDataString); } return $table->render(); } } diff --git a/src/applications/audit/view/list/PhabricatorAuditListView.php b/src/applications/audit/view/list/PhabricatorAuditListView.php index b98a016bac..8df1a80727 100644 --- a/src/applications/audit/view/list/PhabricatorAuditListView.php +++ b/src/applications/audit/view/list/PhabricatorAuditListView.php @@ -1,190 +1,193 @@ audits = $audits; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setAuthorityPHIDs(array $phids) { $this->authorityPHIDs = $phids; return $this; } public function setNoDataString($no_data_string) { $this->noDataString = $no_data_string; return $this; } public function getNoDataString() { return $this->noDataString; } public function setCommits(array $commits) { + assert_instances_of($commits, 'PhabricatorRepositoryCommit'); $this->commits = mpull($commits, null, 'getPHID'); return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function setShowDescriptions($show_descriptions) { $this->showDescriptions = $show_descriptions; return $this; } public function getRequiredHandlePHIDs() { $phids = array(); foreach ($this->audits as $audit) { $phids[$audit->getCommitPHID()] = true; $phids[$audit->getAuditorPHID()] = true; } return array_keys($phids); } private function getHandle($phid) { $handle = idx($this->handles, $phid); if (!$handle) { throw new Exception("No handle for '{$phid}'!"); } return $handle; } private function getCommitDescription($phid) { if ($this->commits === null) { return null; } $commit = idx($this->commits, $phid); if (!$commit) { return null; } return $commit->getCommitData()->getSummary(); } public function render() { $user = $this->user; $authority = array_fill_keys($this->authorityPHIDs, true); $rowc = array(); $last = null; $rows = array(); foreach ($this->audits as $audit) { $commit_phid = $audit->getCommitPHID(); if ($last == $commit_phid) { $commit_name = null; $commit_desc = null; } else { $commit_name = $this->getHandle($commit_phid)->renderLink(); $commit_desc = $this->getCommitDescription($commit_phid); $last = $commit_phid; } $reasons = $audit->getAuditReasons(); foreach ($reasons as $key => $reason) { $reasons[$key] = phutil_escape_html($reason); } $reasons = implode('
', $reasons); $status_code = $audit->getAuditStatus(); $status = PhabricatorAuditStatusConstants::getStatusName($status_code); $auditor_handle = $this->getHandle($audit->getAuditorPHID()); $rows[] = array( $commit_name, phutil_escape_html($commit_desc), $auditor_handle->renderLink(), phutil_escape_html($status), $reasons, ); $row_class = null; $has_authority = !empty($authority[$audit->getAuditorPHID()]); if ($has_authority) { $commit_author = $this->commits[$commit_phid]->getAuthorPHID(); // You don't have authority over package and project audits on your own // commits. $auditor_is_user = ($audit->getAuditorPHID() == $user->getPHID()); $user_is_author = ($commit_author == $user->getPHID()); if ($auditor_is_user || !$user_is_author) { $row_class = 'highlighted'; } } $rowc[] = $row_class; } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Commit', 'Description', 'Auditor', 'Status', 'Details', )); $table->setColumnClasses( array( 'pri', ($this->showDescriptions ? 'wide' : ''), '', '', ($this->showDescriptions ? '' : 'wide'), )); $table->setRowClasses($rowc); $table->setColumnVisibility( array( $this->showDescriptions, $this->showDescriptions, true, true, true, )); if ($this->noDataString) { $table->setNoDataString($this->noDataString); } return $table->render(); } } diff --git a/src/applications/conduit/controller/log/PhabricatorConduitLogController.php b/src/applications/conduit/controller/log/PhabricatorConduitLogController.php index 009c372cdf..7734c5c38b 100644 --- a/src/applications/conduit/controller/log/PhabricatorConduitLogController.php +++ b/src/applications/conduit/controller/log/PhabricatorConduitLogController.php @@ -1,110 +1,113 @@ getRequest(); $conn_table = new PhabricatorConduitConnectionLog(); $call_table = new PhabricatorConduitMethodCallLog(); $conn_r = $call_table->establishConnection('r'); $pager = new AphrontPagerView(); $pager->setOffset($request->getInt('page')); $calls = $call_table->loadAllWhere( '1 = 1 ORDER BY id DESC LIMIT %d, %d', $pager->getOffset(), $pager->getPageSize() + 1); $calls = $pager->sliceResults($calls); $pager->setURI(new PhutilURI('/conduit/log/'), 'page'); $pager->setEnableKeyboardShortcuts(true); $min = $pager->getOffset() + 1; $max = ($min + count($calls) - 1); $conn_ids = array_filter(mpull($calls, 'getConnectionID')); $conns = array(); if ($conn_ids) { $conns = $conn_table->loadAllWhere( 'id IN (%Ld)', $conn_ids); } $table = $this->renderCallTable($calls, $conns); $panel = new AphrontPanelView(); $panel->setHeader('Conduit Method Calls ('.$min.'-'.$max.')'); $panel->appendChild($table); $panel->appendChild($pager); $this->setFilter('log'); return $this->buildStandardPageResponse( $panel, array( 'title' => 'Conduit Logs', )); } private function renderCallTable(array $calls, array $conns) { + assert_instances_of($calls, 'PhabricatorConduitMethodCallLog'); + assert_instances_of($conns, 'PhabricatorConduitConnectionLog'); + $user = $this->getRequest()->getUser(); $rows = array(); foreach ($calls as $call) { $conn = idx($conns, $call->getConnectionID()); if (!$conn) { // If there's no connection, use an empty object. $conn = new PhabricatorConduitConnectionLog(); } $rows[] = array( $call->getConnectionID(), phutil_escape_html($conn->getUserName()), phutil_escape_html($call->getMethod()), phutil_escape_html($call->getError()), number_format($call->getDuration()).' us', phabricator_datetime($call->getDateCreated(), $user), ); } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Connection', 'User', 'Method', 'Error', 'Duration', 'Date', )); $table->setColumnClasses( array( '', '', 'wide', '', 'n', 'right', )); return $table; } } diff --git a/src/applications/conduit/method/differential/parsecommitmessage/ConduitAPI_differential_parsecommitmessage_Method.php b/src/applications/conduit/method/differential/parsecommitmessage/ConduitAPI_differential_parsecommitmessage_Method.php index 0785e28cc0..a25d977652 100644 --- a/src/applications/conduit/method/differential/parsecommitmessage/ConduitAPI_differential_parsecommitmessage_Method.php +++ b/src/applications/conduit/method/differential/parsecommitmessage/ConduitAPI_differential_parsecommitmessage_Method.php @@ -1,188 +1,189 @@ 'required string', 'partial' => 'optional bool', ); } public function defineReturnType() { return 'nonempty dict'; } public function defineErrorTypes() { return array( ); } protected function execute(ConduitAPIRequest $request) { $corpus = $request->getValue('corpus'); $is_partial = $request->getValue('partial'); $aux_fields = DifferentialFieldSelector::newSelector() ->getFieldSpecifications(); foreach ($aux_fields as $key => $aux_field) { if (!$aux_field->shouldAppearOnCommitMessage()) { unset($aux_fields[$key]); } $aux_field->setUser($request->getUser()); } $aux_fields = mpull($aux_fields, null, 'getCommitMessageKey'); // Build a map from labels (like "Test Plan") to field keys // (like "testPlan"). $label_map = $this->buildLabelMap($aux_fields); $field_map = $this->parseCommitMessage($corpus, $label_map); $fields = array(); $errors = array(); foreach ($field_map as $field_key => $field_value) { $field = $aux_fields[$field_key]; try { $fields[$field_key] = $field->parseValueFromCommitMessage($field_value); $field->setValueFromParsedCommitMessage($fields[$field_key]); } catch (DifferentialFieldParseException $ex) { $field_label = $field->renderLabelForCommitMessage(); $errors[] = "Error parsing field '{$field_label}': ".$ex->getMessage(); } } if (!$is_partial) { foreach ($aux_fields as $field_key => $aux_field) { try { $aux_field->validateField(); } catch (DifferentialFieldValidationException $ex) { $field_label = $aux_field->renderLabelForCommitMessage(); $errors[] = "Invalid or missing field '{$field_label}': ". $ex->getMessage(); } } } return array( 'errors' => $errors, 'fields' => $fields, ); } private function buildLabelMap(array $aux_fields) { + assert_instances_of($aux_fields, 'DifferentialFieldSpecification'); $label_map = array(); foreach ($aux_fields as $key => $aux_field) { $labels = $aux_field->getSupportedCommitMessageLabels(); foreach ($labels as $label) { $normal_label = strtolower($label); if (!empty($label_map[$normal_label])) { $previous = $label_map[$normal_label]; throw new Exception( "Field label '{$label}' is parsed by two fields: '{$key}' and ". "'{$previous}'. Each label must be parsed by only one field."); } $label_map[$normal_label] = $key; } } return $label_map; } private function buildLabelRegexp(array $label_map) { $field_labels = array_keys($label_map); foreach ($field_labels as $key => $label) { $field_labels[$key] = preg_quote($label, '/'); } $field_labels = implode('|', $field_labels); $field_pattern = '/^(?P'.$field_labels.'):(?P.*)$/i'; return $field_pattern; } private function parseCommitMessage($corpus, array $label_map) { $label_regexp = $this->buildLabelRegexp($label_map); // Note, deliberately not populating $seen with 'title' because it is // optional to include the 'Title:' label. We're doing a little special // casing to consume the first line as the title regardless of whether you // label it as such or not. $field = 'title'; $seen = array(); $lines = explode("\n", trim($corpus)); $field_map = array(); foreach ($lines as $key => $line) { $match = null; if (preg_match($label_regexp, $line, $match)) { $lines[$key] = trim($match['text']); $field = $label_map[strtolower($match['field'])]; if (!empty($seen[$field])) { throw new Exception( "Field '{$field}' occurs twice in commit message!"); } $seen[$field] = true; } $field_map[$key] = $field; } $fields = array(); foreach ($lines as $key => $line) { $fields[$field_map[$key]][] = $line; } // This is a piece of special-cased magic which allows you to omit the // field labels for "title" and "summary". If the user enters a large block // of text at the beginning of the commit message with an empty line in it, // treat everything before the blank line as "title" and everything after // as "summary". if (isset($fields['title']) && empty($fields['summary'])) { $lines = $fields['title']; for ($ii = 0; $ii < count($lines); $ii++) { if (strlen(trim($lines[$ii])) == 0) { break; } } if ($ii != count($lines)) { $fields['title'] = array_slice($lines, 0, $ii); $fields['summary'] = array_slice($lines, $ii); } } // Implode all the lines back into chunks of text. foreach ($fields as $name => $lines) { $data = rtrim(implode("\n", $lines)); $data = ltrim($data, "\n"); $fields[$name] = $data; } return $fields; } } diff --git a/src/applications/conduit/method/diffusion/getcommits/ConduitAPI_diffusion_getcommits_Method.php b/src/applications/conduit/method/diffusion/getcommits/ConduitAPI_diffusion_getcommits_Method.php index f6f228e162..3147edf992 100644 --- a/src/applications/conduit/method/diffusion/getcommits/ConduitAPI_diffusion_getcommits_Method.php +++ b/src/applications/conduit/method/diffusion/getcommits/ConduitAPI_diffusion_getcommits_Method.php @@ -1,274 +1,275 @@ 'required list', ); } public function defineReturnType() { return 'nonempty list>'; } public function defineErrorTypes() { return array( ); } protected function execute(ConduitAPIRequest $request) { $results = array(); $commits = $request->getValue('commits'); $commits = array_fill_keys($commits, array()); foreach ($commits as $name => $info) { $matches = null; if (!preg_match('/^r([A-Z]+)([0-9a-f]+)$/', $name, $matches)) { $results[$name] = array( 'error' => 'ERR-UNPARSEABLE', ); unset($commits[$name]); continue; } $commits[$name] = array( 'callsign' => $matches[1], 'commitIdentifier' => $matches[2], ); } if (!$commits) { return $results; } $callsigns = ipull($commits, 'callsign'); $callsigns = array_unique($callsigns); $repos = id(new PhabricatorRepository())->loadAllWhere( 'callsign IN (%Ls)', $callsigns); $repos = mpull($repos, null, 'getCallsign'); foreach ($commits as $name => $info) { $repo = idx($repos, $info['callsign']); if (!$repo) { $results[$name] = $info + array( 'error' => 'ERR-UNKNOWN-REPOSITORY', ); unset($commits[$name]); continue; } $commits[$name] += array( 'repositoryPHID' => $repo->getPHID(), 'repositoryID' => $repo->getID(), ); } if (!$commits) { return $results; } // Execute a complicated query to figure out the primary commit information // for each referenced commit. $cdata = $this->queryCommitInformation($commits, $repos); // We've built the queries so that each row also has the identifier we used // to select it, which might be a git prefix rather than a full identifier. $ref_map = ipull($cdata, 'commitIdentifier', 'commitRef'); $cobjs = id(new PhabricatorRepositoryCommit())->loadAllFromArray($cdata); $cobjs = mgroup($cobjs, 'getRepositoryID', 'getCommitIdentifier'); foreach ($commits as $name => $commit) { // Expand short git names into full identifiers. For SVN this map is just // the identity. $full_identifier = idx($ref_map, $commit['commitIdentifier']); $repo_id = $commit['repositoryID']; unset($commits[$name]['repositoryID']); if (empty($full_identifier) || empty($cobjs[$commit['repositoryID']][$full_identifier])) { $results[$name] = $commit + array( 'error' => 'ERR-UNKNOWN-COMMIT', ); unset($commits[$name]); continue; } $cobj_arr = $cobjs[$commit['repositoryID']][$full_identifier]; $cobj = head($cobj_arr); $commits[$name] += array( 'epoch' => $cobj->getEpoch(), 'commitPHID' => $cobj->getPHID(), 'commitID' => $cobj->getID(), ); // Upgrade git short references into full commit identifiers. $identifier = $cobj->getCommitIdentifier(); $commits[$name]['commitIdentifier'] = $identifier; $callsign = $commits[$name]['callsign']; $uri = "/r{$callsign}{$identifier}"; $commits[$name]['uri'] = PhabricatorEnv::getProductionURI($uri); } if (!$commits) { return $results; } $commits = $this->addRepositoryCommitDataInformation($commits); $commits = $this->addDifferentialInformation($commits); foreach ($commits as $name => $commit) { $results[$name] = $commit; } return $results; } /** * Retrieve primary commit information for all referenced commits. */ private function queryCommitInformation(array $commits, array $repos) { + assert_instances_of($repos, 'PhabricatorRepository'); $conn_r = id(new PhabricatorRepositoryCommit())->establishConnection('r'); $repos = mpull($repos, null, 'getID'); $groups = array(); foreach ($commits as $name => $commit) { $groups[$commit['repositoryID']][] = $commit['commitIdentifier']; } // NOTE: MySQL goes crazy and does a massive table scan if we build a more // sensible version of this query. Make sure the query plan is OK if you // attempt to reduce the craziness here. METANOTE: The addition of prefix // selection for Git further complicates matters. $query = array(); $commit_table = id(new PhabricatorRepositoryCommit())->getTableName(); foreach ($groups as $repository_id => $identifiers) { $vcs = $repos[$repository_id]->getVersionControlSystem(); $is_git = ($vcs == PhabricatorRepositoryType::REPOSITORY_TYPE_GIT); if ($is_git) { foreach ($identifiers as $identifier) { if (strlen($identifier) < 7) { // Don't bother with silly stuff like 'rX2', which will select // 1/16th of all commits. Note that with length 7 we'll still get // collisions in repositories at the tens-of-thousands-of-commits // scale. continue; } $query[] = qsprintf( $conn_r, 'SELECT %T.*, %s commitRef FROM %T WHERE repositoryID = %d AND commitIdentifier LIKE %>', $commit_table, $identifier, $commit_table, $repository_id, $identifier); } } else { $query[] = qsprintf( $conn_r, 'SELECT %T.*, commitIdentifier commitRef FROM %T WHERE repositoryID = %d AND commitIdentifier IN (%Ls)', $commit_table, $commit_table, $repository_id, $identifiers); } } return queryfx_all( $conn_r, '%Q', implode(' UNION ALL ', $query)); } /** * Enhance the commit list with RepositoryCommitData information. */ private function addRepositoryCommitDataInformation(array $commits) { $commit_ids = ipull($commits, 'commitID'); $data = id(new PhabricatorRepositoryCommitData())->loadAllWhere( 'commitID in (%Ld)', $commit_ids); $data = mpull($data, null, 'getCommitID'); foreach ($commits as $name => $commit) { if (isset($data[$commit['commitID']])) { $dobj = $data[$commit['commitID']]; $commits[$name] += array( 'commitMessage' => $dobj->getCommitMessage(), 'commitDetails' => $dobj->getCommitDetails(), ); } // Remove this information so we don't expose it via the API since // external services shouldn't be storing internal Commit IDs. unset($commits[$name]['commitID']); } return $commits; } /** * Enhance the commit list with Differential information. */ private function addDifferentialInformation(array $commits) { $commit_phids = ipull($commits, 'commitPHID'); $rev_conn_r = id(new DifferentialRevision())->establishConnection('r'); $revs = queryfx_all( $rev_conn_r, 'SELECT r.id id, r.phid phid, c.commitPHID commitPHID FROM %T r JOIN %T c ON r.id = c.revisionID WHERE c.commitPHID in (%Ls)', id(new DifferentialRevision())->getTableName(), DifferentialRevision::TABLE_COMMIT, $commit_phids); $revs = ipull($revs, null, 'commitPHID'); foreach ($commits as $name => $commit) { if (isset($revs[$commit['commitPHID']])) { $rev = $revs[$commit['commitPHID']]; $commits[$name] += array( 'differentialRevisionID' => 'D'.$rev['id'], 'differentialRevisionPHID' => $rev['phid'], ); } } return $commits; } } diff --git a/src/applications/conduit/method/maniphest/base/ConduitAPI_maniphest_Method.php b/src/applications/conduit/method/maniphest/base/ConduitAPI_maniphest_Method.php index 48c7d947a6..517caae2cb 100644 --- a/src/applications/conduit/method/maniphest/base/ConduitAPI_maniphest_Method.php +++ b/src/applications/conduit/method/maniphest/base/ConduitAPI_maniphest_Method.php @@ -1,213 +1,214 @@ buildTaskInfoDictionaries(array($task)); return idx($results, $task->getPHID()); } protected function getTaskFields($is_new) { $fields = array(); if (!$is_new) { $fields += array( 'id' => 'optional int', 'phid' => 'optional int', ); } $fields += array( 'title' => $is_new ? 'required string' : 'optional string', 'description' => 'optional string', 'ownerPHID' => 'optional phid', 'ccPHIDs' => 'optional list', 'priority' => 'optional int', 'projectPHIDs' => 'optional list', 'filePHIDs' => 'optional list', 'auxiliary' => 'optional dict', ); if (!$is_new) { $fields += array( 'status' => 'optional int', 'comments' => 'optional string', ); } return $fields; } protected function applyRequest( ManiphestTask $task, ConduitAPIRequest $request, $is_new) { $changes = array(); if ($is_new) { $task->setTitle((string)$request->getValue('title')); $task->setDescription((string)$request->getValue('description')); $changes[ManiphestTransactionType::TYPE_STATUS] = ManiphestTaskStatus::STATUS_OPEN; } else { $comments = $request->getValue('comments'); if (!$is_new && $comments !== null) { $changes[ManiphestTransactionType::TYPE_NONE] = null; } $title = $request->getValue('title'); if ($title !== null) { $changes[ManiphestTransactionType::TYPE_TITLE] = $title; } $desc = $request->getValue('description'); if ($desc !== null) { $changes[ManiphestTransactionType::TYPE_DESCRIPTION] = $desc; } $status = $request->getValue('status'); if ($status !== null) { $changes[ManiphestTransactionType::TYPE_STATUS] = $status; } } $priority = $request->getValue('priority'); if ($priority !== null) { $changes[ManiphestTransactionType::TYPE_PRIORITY] = $priority; } $owner_phid = $request->getValue('ownerPHID'); if ($owner_phid !== null) { $changes[ManiphestTransactionType::TYPE_OWNER] = $owner_phid; } $ccs = $request->getValue('ccPHIDs'); if ($ccs !== null) { $changes[ManiphestTransactionType::TYPE_CCS] = $ccs; } $project_phids = $request->getValue('projectPHIDs'); if ($project_phids !== null) { $changes[ManiphestTransactionType::TYPE_PROJECTS] = $project_phids; } $file_phids = $request->getValue('filePHIDs'); if ($file_phids !== null) { $file_map = array_fill_keys($file_phids, true); $attached = $task->getAttached(); $attached[PhabricatorPHIDConstants::PHID_TYPE_FILE] = $file_map; $changes[ManiphestTransactionType::TYPE_ATTACH] = $attached; } $content_source = PhabricatorContentSource::newForSource( PhabricatorContentSource::SOURCE_CONDUIT, array()); $template = new ManiphestTransaction(); $template->setContentSource($content_source); $template->setAuthorPHID($request->getUser()->getPHID()); $transactions = array(); foreach ($changes as $type => $value) { $transaction = clone $template; $transaction->setTransactionType($type); $transaction->setNewValue($value); if ($type == ManiphestTransactionType::TYPE_NONE) { $transaction->setComments($comments); } $transactions[] = $transaction; } $auxiliary = $request->getValue('auxiliary'); if ($auxiliary) { $task->loadAndAttachAuxiliaryAttributes(); foreach ($auxiliary as $aux_key => $aux_value) { $transaction = clone $template; $transaction->setTransactionType( ManiphestTransactionType::TYPE_AUXILIARY); $transaction->setMetadataValue('aux:key', $aux_key); $transaction->setNewValue($aux_value); $transactions[] = $transaction; } } $event = new PhabricatorEvent( PhabricatorEventType::TYPE_MANIPHEST_WILLEDITTASK, array( 'task' => $task, 'new' => $is_new, 'transactions' => $transactions, )); $event->setUser($request->getUser()); $event->setConduitRequest($request); PhutilEventEngine::dispatchEvent($event); $task = $event->getValue('task'); $transactions = $event->getValue('transactions'); $editor = new ManiphestTransactionEditor(); $editor->applyTransactions($task, $transactions); } protected function buildTaskInfoDictionaries(array $tasks) { + assert_instances_of($tasks, 'ManiphestTask'); if (!$tasks) { return array(); } $all_aux = id(new ManiphestTaskAuxiliaryStorage())->loadAllWhere( 'taskPHID in (%Ls)', mpull($tasks, 'getPHID')); $all_aux = mgroup($all_aux, 'getTaskPHID'); $result = array(); foreach ($tasks as $task) { $auxiliary = idx($all_aux, $task->getPHID(), array()); $auxiliary = mpull($auxiliary, 'getValue', 'getName'); $result[$task->getPHID()] = array( 'id' => $task->getID(), 'phid' => $task->getPHID(), 'authorPHID' => $task->getAuthorPHID(), 'ownerPHID' => $task->getOwnerPHID(), 'ccPHIDs' => $task->getCCPHIDs(), 'status' => $task->getStatus(), 'priority' => ManiphestTaskPriority::getTaskPriorityName( $task->getPriority()), 'title' => $task->getTitle(), 'description' => $task->getDescription(), 'projectPHIDs' => $task->getProjectPHIDs(), 'uri' => PhabricatorEnv::getProductionURI('/T'.$task->getID()), 'auxiliary' => $auxiliary, 'objectName' => 'T'.$task->getID(), 'dateCreated' => $task->getDateCreated(), 'dateModified' => $task->getDateModified(), ); } return $result; } } diff --git a/src/applications/conduit/method/project/base/ConduitAPI_project_Method.php b/src/applications/conduit/method/project/base/ConduitAPI_project_Method.php index ca3d642828..a5762bcaa7 100644 --- a/src/applications/conduit/method/project/base/ConduitAPI_project_Method.php +++ b/src/applications/conduit/method/project/base/ConduitAPI_project_Method.php @@ -1,53 +1,54 @@ buildProjectInfoDictionaries(array($project)); return idx($results, $project->getPHID()); } protected function buildProjectInfoDictionaries(array $projects) { + assert_instances_of($projects, 'PhabricatorProject'); if (!$projects) { return array(); } $result = array(); foreach ($projects as $project) { $member_phids = mpull($project->getAffiliations(), 'getUserPHID'); $member_phids = array_values($member_phids); $result[$project->getPHID()] = array( 'id' => $project->getID(), 'phid' => $project->getPHID(), 'name' => $project->getName(), 'members' => $member_phids, 'dateCreated' => $project->getDateCreated(), 'dateModified' => $project->getDateModified(), ); } return $result; } } diff --git a/src/applications/daemon/view/daemonlogevents/PhabricatorDaemonLogEventsView.php b/src/applications/daemon/view/daemonlogevents/PhabricatorDaemonLogEventsView.php index c33b5f746d..ba55442742 100644 --- a/src/applications/daemon/view/daemonlogevents/PhabricatorDaemonLogEventsView.php +++ b/src/applications/daemon/view/daemonlogevents/PhabricatorDaemonLogEventsView.php @@ -1,130 +1,131 @@ events = $events; return $this; } public function setCombinedLog($is_combined) { $this->combinedLog = $is_combined; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function render() { $rows = array(); if (!$this->user) { throw new Exception("Call setUser() before rendering!"); } foreach ($this->events as $event) { // Limit display log size. If a daemon gets stuck in an output loop this // page can be like >100MB if we don't truncate stuff. Try to do cheap // line-based truncation first, and fall back to expensive UTF-8 character // truncation if that doesn't get things short enough. $message = $event->getMessage(); $more_lines = null; $more_chars = null; $line_limit = 12; if (substr_count($message, "\n") > $line_limit) { $message = explode("\n", $message); $more_lines = count($message) - $line_limit; $message = array_slice($message, 0, $line_limit); $message = implode("\n", $message); } $char_limit = 8192; if (strlen($message) > $char_limit) { $message = phutil_utf8v($message); $more_chars = count($message) - $char_limit; $message = array_slice($message, 0, $char_limit); $message = implode('', $message); } $more = null; if ($more_chars) { $more = number_format($more_chars); $more = "\n<... {$more} more characters ...>"; } else if ($more_lines) { $more = number_format($more_lines); $more = "\n<... {$more} more lines ...>"; } $row = array( phutil_escape_html($event->getLogType()), phabricator_date($event->getEpoch(), $this->user), phabricator_time($event->getEpoch(), $this->user), str_replace("\n", '
', phutil_escape_html($message.$more)), ); if ($this->combinedLog) { array_unshift( $row, phutil_render_tag( 'a', array( 'href' => '/daemon/log/'.$event->getLogID().'/', ), phutil_escape_html('Daemon '.$event->getLogID()))); } $rows[] = $row; } $classes = array( '', '', 'right', 'wide wrap', ); $headers = array( 'Type', 'Date', 'Time', 'Message', ); if ($this->combinedLog) { array_unshift($classes, 'pri'); array_unshift($headers, 'Daemon'); } $log_table = new AphrontTableView($rows); $log_table->setHeaders($headers); $log_table->setColumnClasses($classes); return $log_table->render(); } } diff --git a/src/applications/daemon/view/daemonloglist/PhabricatorDaemonLogListView.php b/src/applications/daemon/view/daemonloglist/PhabricatorDaemonLogListView.php index 02ae2b4557..8c637fb95c 100644 --- a/src/applications/daemon/view/daemonloglist/PhabricatorDaemonLogListView.php +++ b/src/applications/daemon/view/daemonloglist/PhabricatorDaemonLogListView.php @@ -1,118 +1,119 @@ daemonLogs = $daemon_logs; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function render() { $rows = array(); if (!$this->user) { throw new Exception("Call setUser() before rendering!"); } foreach ($this->daemonLogs as $log) { $epoch = $log->getDateCreated(); if ($log->getHost() == php_uname('n')) { $pid = $log->getPID(); $is_running = PhabricatorDaemonReference::isProcessRunning($pid); if ($is_running) { $running = phutil_render_tag( 'span', array( 'style' => 'color: #00cc00', 'title' => 'Running', ), '•'); } else { $running = phutil_render_tag( 'span', array( 'style' => 'color: #cc0000', 'title' => 'Not running', ), '•'); } } else { $running = phutil_render_tag( 'span', array( 'style' => 'color: #888888', 'title' => 'Not on this host', ), '?'); } $rows[] = array( $running, phutil_escape_html($log->getDaemon()), phutil_escape_html($log->getHost()), $log->getPID(), phabricator_date($epoch, $this->user), phabricator_time($epoch, $this->user), phutil_render_tag( 'a', array( 'href' => '/daemon/log/'.$log->getID().'/', 'class' => 'button small grey', ), 'View Log'), ); } $daemon_table = new AphrontTableView($rows); $daemon_table->setHeaders( array( '', 'Daemon', 'Host', 'PID', 'Date', 'Time', 'View', )); $daemon_table->setColumnClasses( array( '', 'wide wrap', '', '', '', 'right', 'action', )); return $daemon_table->render(); } } diff --git a/src/applications/daemon/view/daemonloglist/__init__.php b/src/applications/daemon/view/daemonloglist/__init__.php index c476f15cd3..982400ce70 100644 --- a/src/applications/daemon/view/daemonloglist/__init__.php +++ b/src/applications/daemon/view/daemonloglist/__init__.php @@ -1,17 +1,18 @@ revision = $revision; $this->actorPHID = $actor_phid; } public static function newRevisionFromConduitWithDiff( array $fields, DifferentialDiff $diff, $user_phid) { $revision = new DifferentialRevision(); $revision->setPHID($revision->generatePHID()); $revision->setAuthorPHID($user_phid); $revision->setStatus(ArcanistDifferentialRevisionStatus::NEEDS_REVIEW); $editor = new DifferentialRevisionEditor($revision, $user_phid); $editor->copyFieldsFromConduit($fields); $editor->addDiff($diff, null); $editor->save(); return $revision; } public function copyFieldsFromConduit(array $fields) { $revision = $this->revision; $revision->loadRelationships(); $aux_fields = DifferentialFieldSelector::newSelector() ->getFieldSpecifications(); $user = id(new PhabricatorUser())->loadOneWhere( 'phid = %s', $this->actorPHID); foreach ($aux_fields as $key => $aux_field) { $aux_field->setRevision($revision); $aux_field->setUser($user); if (!$aux_field->shouldAppearOnCommitMessage()) { unset($aux_fields[$key]); } } $aux_fields = mpull($aux_fields, null, 'getCommitMessageKey'); foreach ($fields as $field => $value) { if (empty($aux_fields[$field])) { throw new Exception( "Parsed commit message contains unrecognized field '{$field}'."); } $aux_fields[$field]->setValueFromParsedCommitMessage($value); } foreach ($aux_fields as $aux_field) { $aux_field->validateField(); } $aux_fields = array_values($aux_fields); $this->setAuxiliaryFields($aux_fields); } public function setAuxiliaryFields(array $auxiliary_fields) { + assert_instances_of($auxiliary_fields, 'DifferentialAuxiliaryField'); $this->auxiliaryFields = $auxiliary_fields; return $this; } public function getRevision() { return $this->revision; } public function setReviewers(array $reviewers) { $this->reviewers = $reviewers; return $this; } public function setCCPHIDs(array $cc) { $this->cc = $cc; return $this; } public function setContentSource(PhabricatorContentSource $content_source) { $this->contentSource = $content_source; return $this; } public function addDiff(DifferentialDiff $diff, $comments) { if ($diff->getRevisionID() && $diff->getRevisionID() != $this->getRevision()->getID()) { $diff_id = (int)$diff->getID(); $targ_id = (int)$this->getRevision()->getID(); $real_id = (int)$diff->getRevisionID(); throw new Exception( "Can not attach diff #{$diff_id} to Revision D{$targ_id}, it is ". "already attached to D{$real_id}."); } $this->diff = $diff; $this->comments = $comments; return $this; } protected function getDiff() { return $this->diff; } protected function getComments() { return $this->comments; } protected function getActorPHID() { return $this->actorPHID; } public function isNewRevision() { return !$this->getRevision()->getID(); } /** * A silent update does not trigger Herald rules or send emails. This is used * for auto-amends at commit time. */ public function setSilentUpdate($silent) { $this->silentUpdate = $silent; return $this; } public function save() { $revision = $this->getRevision(); $is_new = $this->isNewRevision(); if ($is_new) { $this->initializeNewRevision($revision); } $revision->loadRelationships(); $this->willWriteRevision(); if ($this->reviewers === null) { $this->reviewers = $revision->getReviewers(); } if ($this->cc === null) { $this->cc = $revision->getCCPHIDs(); } $diff = $this->getDiff(); if ($diff) { $revision->setLineCount($diff->getLineCount()); } // Save the revision, to generate its ID and PHID if it is new. We need // the ID/PHID in order to record them in Herald transcripts, but don't // want to hold a transaction open while running Herald because it is // potentially somewhat slow. The downside is that we may end up with a // saved revision/diff pair without appropriate CCs. We could be better // about this -- for example: // // - Herald can't affect reviewers, so we could compute them before // opening the transaction and then save them in the transaction. // - Herald doesn't *really* need PHIDs to compute its effects, we could // run it before saving these objects and then hand over the PHIDs later. // // But this should address the problem of orphaned revisions, which is // currently the only problem we experience in practice. $revision->openTransaction(); $revision->save(); if ($diff) { $diff->setRevisionID($revision->getID()); $diff->save(); } $revision->saveTransaction(); // We're going to build up three dictionaries: $add, $rem, and $stable. The // $add dictionary has added reviewers/CCs. The $rem dictionary has // reviewers/CCs who have been removed, and the $stable array is // reviewers/CCs who haven't changed. We're going to send new reviewers/CCs // a different ("welcome") email than we send stable reviewers/CCs. $old = array( 'rev' => array_fill_keys($revision->getReviewers(), true), 'ccs' => array_fill_keys($revision->getCCPHIDs(), true), ); $xscript_header = null; $xscript_uri = null; $new = array( 'rev' => array_fill_keys($this->reviewers, true), 'ccs' => array_fill_keys($this->cc, true), ); $rem_ccs = array(); $xscript_phid = null; if ($diff) { $adapter = new HeraldDifferentialRevisionAdapter( $revision, $diff); $adapter->setExplicitCCs($new['ccs']); $adapter->setExplicitReviewers($new['rev']); $adapter->setForbiddenCCs($revision->getUnsubscribedPHIDs()); $xscript = HeraldEngine::loadAndApplyRules($adapter); $xscript_uri = PhabricatorEnv::getProductionURI( '/herald/transcript/'.$xscript->getID().'/'); $xscript_phid = $xscript->getPHID(); $xscript_header = $xscript->getXHeraldRulesHeader(); $xscript_header = HeraldTranscript::saveXHeraldRulesHeader( $revision->getPHID(), $xscript_header); $sub = array( 'rev' => array(), 'ccs' => $adapter->getCCsAddedByHerald(), ); $rem_ccs = $adapter->getCCsRemovedByHerald(); } else { $sub = array( 'rev' => array(), 'ccs' => array(), ); } // Remove any CCs which are prevented by Herald rules. $sub['ccs'] = array_diff_key($sub['ccs'], $rem_ccs); $new['ccs'] = array_diff_key($new['ccs'], $rem_ccs); $add = array(); $rem = array(); $stable = array(); foreach (array('rev', 'ccs') as $key) { $add[$key] = array(); if ($new[$key] !== null) { $add[$key] += array_diff_key($new[$key], $old[$key]); } $add[$key] += array_diff_key($sub[$key], $old[$key]); $combined = $sub[$key]; if ($new[$key] !== null) { $combined += $new[$key]; } $rem[$key] = array_diff_key($old[$key], $combined); $stable[$key] = array_diff_key($old[$key], $add[$key] + $rem[$key]); } self::alterReviewers( $revision, $this->reviewers, array_keys($rem['rev']), array_keys($add['rev']), $this->actorPHID); // We want to attribute new CCs to a "reasonPHID", representing the reason // they were added. This is either a user (if some user explicitly CCs // them, or uses "Add CCs...") or a Herald transcript PHID, indicating that // they were added by a Herald rule. if ($add['ccs'] || $rem['ccs']) { $reasons = array(); foreach ($add['ccs'] as $phid => $ignored) { if (empty($new['ccs'][$phid])) { $reasons[$phid] = $xscript_phid; } else { $reasons[$phid] = $this->actorPHID; } } foreach ($rem['ccs'] as $phid => $ignored) { if (empty($new['ccs'][$phid])) { $reasons[$phid] = $this->actorPHID; } else { $reasons[$phid] = $xscript_phid; } } } else { $reasons = $this->actorPHID; } self::alterCCs( $revision, $this->cc, array_keys($rem['ccs']), array_keys($add['ccs']), $reasons); $this->updateAuxiliaryFields(); // Add the author and users included from Herald rules to the relevant set // of users so they get a copy of the email. if (!$this->silentUpdate) { if ($is_new) { $add['rev'][$this->getActorPHID()] = true; if ($diff) { $add['rev'] += $adapter->getEmailPHIDsAddedByHerald(); } } else { $stable['rev'][$this->getActorPHID()] = true; if ($diff) { $stable['rev'] += $adapter->getEmailPHIDsAddedByHerald(); } } } $mail = array(); $phids = array($this->getActorPHID()); $handles = id(new PhabricatorObjectHandleData($phids)) ->loadHandles(); $actor_handle = $handles[$this->getActorPHID()]; $changesets = null; $comment = null; if ($diff) { $changesets = $diff->loadChangesets(); // TODO: This should probably be in DifferentialFeedbackEditor? if (!$is_new) { $comment = $this->createComment(); } if ($comment) { $mail[] = id(new DifferentialNewDiffMail( $revision, $actor_handle, $changesets)) ->setIsFirstMailAboutRevision($is_new) ->setIsFirstMailToRecipients($is_new) ->setComments($this->getComments()) ->setToPHIDs(array_keys($stable['rev'])) ->setCCPHIDs(array_keys($stable['ccs'])); } // Save the changes we made above. $diff->setDescription(preg_replace('/\n.*/s', '', $this->getComments())); $diff->save(); $this->updateAffectedPathTable($revision, $diff, $changesets); $this->updateRevisionHashTable($revision, $diff); // An updated diff should require review, as long as it's not committed // or accepted. The "accepted" status is "sticky" to encourage courtesy // re-diffs after someone accepts with minor changes/suggestions. $status = $revision->getStatus(); if ($status != ArcanistDifferentialRevisionStatus::COMMITTED && $status != ArcanistDifferentialRevisionStatus::ACCEPTED) { $revision->setStatus(ArcanistDifferentialRevisionStatus::NEEDS_REVIEW); } } else { $diff = $revision->loadActiveDiff(); if ($diff) { $changesets = $diff->loadChangesets(); } else { $changesets = array(); } } $revision->save(); $this->didWriteRevision(); $event_data = array( 'revision_id' => $revision->getID(), 'revision_phid' => $revision->getPHID(), 'revision_name' => $revision->getTitle(), 'revision_author_phid' => $revision->getAuthorPHID(), 'action' => $is_new ? DifferentialAction::ACTION_CREATE : DifferentialAction::ACTION_UPDATE, 'feedback_content' => $is_new ? phutil_utf8_shorten($revision->getSummary(), 140) : $this->getComments(), 'actor_phid' => $revision->getAuthorPHID(), ); id(new PhabricatorTimelineEvent('difx', $event_data)) ->recordEvent(); id(new PhabricatorFeedStoryPublisher()) ->setStoryType(PhabricatorFeedStoryTypeConstants::STORY_DIFFERENTIAL) ->setStoryData($event_data) ->setStoryTime(time()) ->setStoryAuthorPHID($revision->getAuthorPHID()) ->setRelatedPHIDs( array( $revision->getPHID(), $revision->getAuthorPHID(), )) ->publish(); // TODO: Move this into a worker task thing. PhabricatorSearchDifferentialIndexer::indexRevision($revision); if ($this->silentUpdate) { return; } $revision->loadRelationships(); if ($add['rev']) { $message = id(new DifferentialNewDiffMail( $revision, $actor_handle, $changesets)) ->setIsFirstMailAboutRevision($is_new) ->setIsFirstMailToRecipients(true) ->setToPHIDs(array_keys($add['rev'])); if ($is_new) { // The first time we send an email about a revision, put the CCs in // the "CC:" field of the same "Review Requested" email that reviewers // get, so you don't get two initial emails if you're on a list that // is CC'd. $message->setCCPHIDs(array_keys($add['ccs'])); } $mail[] = $message; } // If we added CCs, we want to send them an email, but only if they were not // already a reviewer and were not added as one (in these cases, they got // a "NewDiff" mail, either in the past or just a moment ago). You can still // get two emails, but only if a revision is updated and you are added as a // reviewer at the same time a list you are on is added as a CC, which is // rare and reasonable. $implied_ccs = self::getImpliedCCs($revision); $implied_ccs = array_fill_keys($implied_ccs, true); $add['ccs'] = array_diff_key($add['ccs'], $implied_ccs); if (!$is_new && $add['ccs']) { $mail[] = id(new DifferentialCCWelcomeMail( $revision, $actor_handle, $changesets)) ->setIsFirstMailToRecipients(true) ->setToPHIDs(array_keys($add['ccs'])); } foreach ($mail as $message) { $message->setHeraldTranscriptURI($xscript_uri); $message->setXHeraldRulesHeader($xscript_header); $message->send(); } } public static function addCCAndUpdateRevision( $revision, $phid, $reason) { self::addCC($revision, $phid, $reason); $unsubscribed = $revision->getUnsubscribed(); if (isset($unsubscribed[$phid])) { unset($unsubscribed[$phid]); $revision->setUnsubscribed($unsubscribed); $revision->save(); } } public static function removeCCAndUpdateRevision( $revision, $phid, $reason) { self::removeCC($revision, $phid, $reason); $unsubscribed = $revision->getUnsubscribed(); if (empty($unsubscribed[$phid])) { $unsubscribed[$phid] = true; $revision->setUnsubscribed($unsubscribed); $revision->save(); } } public static function addCC( DifferentialRevision $revision, $phid, $reason) { return self::alterCCs( $revision, $revision->getCCPHIDs(), $rem = array(), $add = array($phid), $reason); } public static function removeCC( DifferentialRevision $revision, $phid, $reason) { return self::alterCCs( $revision, $revision->getCCPHIDs(), $rem = array($phid), $add = array(), $reason); } protected static function alterCCs( DifferentialRevision $revision, array $stable_phids, array $rem_phids, array $add_phids, $reason_phid) { $dont_add = self::getImpliedCCs($revision); $add_phids = array_diff($add_phids, $dont_add); return self::alterRelationships( $revision, $stable_phids, $rem_phids, $add_phids, $reason_phid, DifferentialRevision::RELATION_SUBSCRIBED); } private static function getImpliedCCs(DifferentialRevision $revision) { return array_merge( $revision->getReviewers(), array($revision->getAuthorPHID())); } public static function alterReviewers( DifferentialRevision $revision, array $stable_phids, array $rem_phids, array $add_phids, $reason_phid) { return self::alterRelationships( $revision, $stable_phids, $rem_phids, $add_phids, $reason_phid, DifferentialRevision::RELATION_REVIEWER); } private static function alterRelationships( DifferentialRevision $revision, array $stable_phids, array $rem_phids, array $add_phids, $reason_phid, $relation_type) { $rem_map = array_fill_keys($rem_phids, true); $add_map = array_fill_keys($add_phids, true); $seq_map = array_values($stable_phids); $seq_map = array_flip($seq_map); foreach ($rem_map as $phid => $ignored) { if (!isset($seq_map[$phid])) { $seq_map[$phid] = count($seq_map); } } foreach ($add_map as $phid => $ignored) { if (!isset($seq_map[$phid])) { $seq_map[$phid] = count($seq_map); } } $raw = $revision->getRawRelations($relation_type); $raw = ipull($raw, null, 'objectPHID'); $sequence = count($seq_map); foreach ($raw as $phid => $ignored) { if (isset($seq_map[$phid])) { $raw[$phid]['sequence'] = $seq_map[$phid]; } else { $raw[$phid]['sequence'] = $sequence++; } } $raw = isort($raw, 'sequence'); foreach ($raw as $phid => $ignored) { if (isset($rem_map[$phid])) { unset($raw[$phid]); } } foreach ($add_phids as $add) { $reason = is_array($reason_phid) ? idx($reason_phid, $add) : $reason_phid; $raw[$add] = array( 'objectPHID' => $add, 'sequence' => idx($seq_map, $add, $sequence++), 'reasonPHID' => $reason, ); } $conn_w = $revision->establishConnection('w'); $sql = array(); foreach ($raw as $relation) { $sql[] = qsprintf( $conn_w, '(%d, %s, %s, %d, %s)', $revision->getID(), $relation_type, $relation['objectPHID'], $relation['sequence'], $relation['reasonPHID']); } $conn_w->openTransaction(); queryfx( $conn_w, 'DELETE FROM %T WHERE revisionID = %d AND relation = %s', DifferentialRevision::RELATIONSHIP_TABLE, $revision->getID(), $relation_type); if ($sql) { queryfx( $conn_w, 'INSERT INTO %T (revisionID, relation, objectPHID, sequence, reasonPHID) VALUES %Q', DifferentialRevision::RELATIONSHIP_TABLE, implode(', ', $sql)); } $conn_w->saveTransaction(); $revision->loadRelationships(); } private function createComment() { $revision_id = $this->revision->getID(); $comment = id(new DifferentialComment()) ->setAuthorPHID($this->getActorPHID()) ->setRevisionID($revision_id) ->setContent($this->getComments()) ->setAction(DifferentialAction::ACTION_UPDATE) ->setMetadata( array( DifferentialComment::METADATA_DIFF_ID => $this->getDiff()->getID(), )); if ($this->contentSource) { $comment->setContentSource($this->contentSource); } $comment->save(); return $comment; } private function updateAuxiliaryFields() { $aux_map = array(); foreach ($this->auxiliaryFields as $aux_field) { $key = $aux_field->getStorageKey(); if ($key !== null) { $val = $aux_field->getValueForStorage(); $aux_map[$key] = $val; } } if (!$aux_map) { return; } $revision = $this->revision; $fields = id(new DifferentialAuxiliaryField())->loadAllWhere( 'revisionPHID = %s AND name IN (%Ls)', $revision->getPHID(), array_keys($aux_map)); $fields = mpull($fields, null, 'getName'); foreach ($aux_map as $key => $val) { $obj = idx($fields, $key); if (!strlen($val)) { // If the new value is empty, just delete the old row if one exists and // don't add a new row if it doesn't. if ($obj) { $obj->delete(); } } else { if (!$obj) { $obj = new DifferentialAuxiliaryField(); $obj->setRevisionPHID($revision->getPHID()); $obj->setName($key); } if ($obj->getValue() !== $val) { $obj->setValue($val); $obj->save(); } } } } private function willWriteRevision() { foreach ($this->auxiliaryFields as $aux_field) { $aux_field->willWriteRevision($this); } } private function didWriteRevision() { foreach ($this->auxiliaryFields as $aux_field) { $aux_field->didWriteRevision($this); } } /** * Update the table which links Differential revisions to paths they affect, * so Diffusion can efficiently find pending revisions for a given file. */ private function updateAffectedPathTable( DifferentialRevision $revision, DifferentialDiff $diff, array $changesets) { $project = $diff->loadArcanistProject(); if (!$project) { // Probably an old revision from before projects. return; } $repository = $project->loadRepository(); if (!$repository) { // Probably no project <-> repository link, or the repository where the // project lives is untracked. return; } $path_prefix = null; $local_root = $diff->getSourceControlPath(); if ($local_root) { // We're in a working copy which supports subdirectory checkouts (e.g., // SVN) so we need to figure out what prefix we should add to each path // (e.g., trunk/projects/example/) to get the absolute path from the // root of the repository. DVCS systems like Git and Mercurial are not // affected. // Normalize both paths and check if the repository root is a prefix of // the local root. If so, throw it away. Note that this correctly handles // the case where the remote path is "/". $local_root = id(new PhutilURI($local_root))->getPath(); $local_root = rtrim($local_root, '/'); $repo_root = id(new PhutilURI($repository->getRemoteURI()))->getPath(); $repo_root = rtrim($repo_root, '/'); if (!strncmp($repo_root, $local_root, strlen($repo_root))) { $path_prefix = substr($local_root, strlen($repo_root)); } } $paths = array(); foreach ($changesets as $changeset) { $paths[] = $path_prefix.'/'.$changeset->getFilename(); } // Mark this as also touching all parent paths, so you can see all pending // changes to any file within a directory. $all_paths = array(); foreach ($paths as $local) { foreach (DiffusionPathIDQuery::expandPathToRoot($local) as $path) { $all_paths[$path] = true; } } $all_paths = array_keys($all_paths); $path_map = id(new DiffusionPathIDQuery($all_paths))->loadPathIDs(); $table = new DifferentialAffectedPath(); $conn_w = $table->establishConnection('w'); $sql = array(); foreach ($all_paths as $path) { $path_id = idx($path_map, $path); if (!$path_id) { // Don't bother creating these, it probably means we're either adding // a file (in which case having this row is irrelevant since Diffusion // won't be querying for it) or something is misconfigured (in which // case we'd just be writing garbage). continue; } $sql[] = qsprintf( $conn_w, '(%d, %d, %d, %d)', $repository->getID(), $path_id, time(), $revision->getID()); } queryfx( $conn_w, 'DELETE FROM %T WHERE revisionID = %d', $table->getTableName(), $revision->getID()); foreach (array_chunk($sql, 256) as $chunk) { queryfx( $conn_w, 'INSERT INTO %T (repositoryID, pathID, epoch, revisionID) VALUES %Q', $table->getTableName(), implode(', ', $chunk)); } } /** * Update the table connecting revisions to DVCS local hashes, so we can * identify revisions by commit/tree hashes. */ private function updateRevisionHashTable( DifferentialRevision $revision, DifferentialDiff $diff) { $vcs = $diff->getSourceControlSystem(); if ($vcs == DifferentialRevisionControlSystem::SVN) { // Subversion has no local commit or tree hash information, so we don't // have to do anything. return; } $property = id(new DifferentialDiffProperty())->loadOneWhere( 'diffID = %d AND name = %s', $diff->getID(), 'local:commits'); if (!$property) { return; } $hashes = array(); $data = $property->getData(); switch ($vcs) { case DifferentialRevisionControlSystem::GIT: foreach ($data as $commit) { $hashes[] = array( ArcanistDifferentialRevisionHash::HASH_GIT_COMMIT, $commit['commit'], ); $hashes[] = array( ArcanistDifferentialRevisionHash::HASH_GIT_TREE, $commit['tree'], ); } break; case DifferentialRevisionControlSystem::MERCURIAL: foreach ($data as $commit) { $hashes[] = array( ArcanistDifferentialRevisionHash::HASH_MERCURIAL_COMMIT, $commit['rev'], ); } break; } $conn_w = $revision->establishConnection('w'); $sql = array(); foreach ($hashes as $info) { list($type, $hash) = $info; $sql[] = qsprintf( $conn_w, '(%d, %s, %s)', $revision->getID(), $type, $hash); } queryfx( $conn_w, 'DELETE FROM %T WHERE revisionID = %d', ArcanistDifferentialRevisionHash::TABLE_NAME, $revision->getID()); if ($sql) { queryfx( $conn_w, 'INSERT INTO %T (revisionID, type, hash) VALUES %Q', ArcanistDifferentialRevisionHash::TABLE_NAME, implode(', ', $sql)); } } private function initializeNewRevision(DifferentialRevision $revision) { // These fields aren't nullable; set them to sensible defaults if they // haven't been configured. We're just doing this so we can generate an // ID for the revision if we don't have one already. $revision->setLineCount(0); if ($revision->getStatus() === null) { $revision->setStatus(ArcanistDifferentialRevisionStatus::NEEDS_REVIEW); } if ($revision->getTitle() === null) { $revision->setTitle('Untitled Revision'); } if ($revision->getAuthorPHID() === null) { $revision->setAuthorPHID($this->getActorPHID()); } if ($revision->getSummary() === null) { $revision->setSummary(''); } if ($revision->getTestPlan() === null) { $revision->setTestPlan(''); } } } diff --git a/src/applications/differential/view/revisiondetail/DifferentialRevisionDetailView.php b/src/applications/differential/view/revisiondetail/DifferentialRevisionDetailView.php index 3013828282..5c52c767b0 100644 --- a/src/applications/differential/view/revisiondetail/DifferentialRevisionDetailView.php +++ b/src/applications/differential/view/revisiondetail/DifferentialRevisionDetailView.php @@ -1,87 +1,88 @@ revision = $revision; return $this; } public function setActions(array $actions) { $this->actions = $actions; return $this; } public function setUser($user) { $this->user = $user; return $this; } public function setAuxiliaryFields(array $fields) { + assert_instances_of($fields, 'DifferentialAuxiliaryField'); $this->auxiliaryFields = $fields; return $this; } public function render() { require_celerity_resource('differential-core-view-css'); require_celerity_resource('differential-revision-detail-css'); $revision = $this->revision; $dict = array(); foreach ($this->auxiliaryFields as $field) { $value = $field->renderValueForRevisionView(); if (strlen($value)) { $label = rtrim($field->renderLabelForRevisionView(), ':'); $dict[$label] = $value; } } $actions = array(); foreach ($this->actions as $action) { $obj = new AphrontHeadsupActionView(); $obj->setName($action['name']); $obj->setURI(idx($action, 'href')); $obj->setWorkflow(idx($action, 'sigil') == 'workflow'); $obj->setClass(idx($action, 'class')); $obj->setInstant(idx($action, 'instant')); $obj->setUser($this->user); $actions[] = $obj; } $action_list = new AphrontHeadsupActionListView(); $action_list->setActions($actions); $action_panel = new AphrontHeadsupView(); $action_panel->setActionList($action_list); $action_panel->setHasKeyboardShortcuts(true); $action_panel->setProperties($dict); $action_panel->setObjectName('D'.$revision->getID()); $action_panel->setHeader($revision->getTitle()); return $action_panel->render(); } } diff --git a/src/applications/directory/controller/main/PhabricatorDirectoryMainController.php b/src/applications/directory/controller/main/PhabricatorDirectoryMainController.php index e170b9a08c..d5a69044a5 100644 --- a/src/applications/directory/controller/main/PhabricatorDirectoryMainController.php +++ b/src/applications/directory/controller/main/PhabricatorDirectoryMainController.php @@ -1,718 +1,723 @@ filter = idx($data, 'filter'); $this->subfilter = idx($data, 'subfilter'); } public function shouldRequireAdmin() { // These controllers are admin-only by default, but this one is public, // so allow non-admin users to view it. return false; } public function processRequest() { $user = $this->getRequest()->getUser(); $nav = $this->buildNav(); $this->filter = $nav->selectFilter($this->filter, 'home'); switch ($this->filter) { case 'jump': break; case 'home': case 'feed': $project_query = new PhabricatorProjectQuery(); $project_query->setMembers(array($user->getPHID())); $projects = $project_query->execute(); break; default: throw new Exception("Unknown filter '{$this->filter}'!"); } switch ($this->filter) { case 'feed': return $this->buildFeedResponse($nav, $projects); case 'jump': return $this->buildJumpResponse($nav); default: return $this->buildMainResponse($nav, $projects); } } - private function buildMainResponse($nav, $projects) { + private function buildMainResponse($nav, array $projects) { + assert_instances_of($projects, 'PhabricatorProject'); + if (PhabricatorEnv::getEnvConfig('maniphest.enabled')) { $unbreak_panel = $this->buildUnbreakNowPanel(); $triage_panel = $this->buildNeedsTriagePanel($projects); $tasks_panel = $this->buildTasksPanel(); } else { $unbreak_panel = null; $triage_panel = null; $tasks_panel = null; } $flagged_panel = $this->buildFlaggedPanel(); $jump_panel = $this->buildJumpPanel(); $revision_panel = $this->buildRevisionPanel(); $app_panel = $this->buildAppPanel(); $audit_panel = $this->buildAuditPanel(); $commit_panel = $this->buildCommitPanel(); $content = array( $app_panel, $jump_panel, $unbreak_panel, $triage_panel, $revision_panel, $tasks_panel, $flagged_panel, $audit_panel, $commit_panel, ); $nav->appendChild($content); return $this->buildStandardPageResponse( $nav, array( 'title' => 'Phabricator', )); } private function buildJumpResponse($nav) { $request = $this->getRequest(); if ($request->isFormPost()) { $jump = $request->getStr('jump'); $response = PhabricatorJumpNavHandler::jumpPostResponse($jump); if ($response) { return $response; } else { $query = new PhabricatorSearchQuery(); $query->setQuery($jump); $query->save(); return id(new AphrontRedirectResponse()) ->setURI('/search/'.$query->getQueryKey().'/'); } } $nav->appendChild($this->buildJumpPanel()); return $this->buildStandardPageResponse( $nav, array( 'title' => 'Jump Nav', )); } - private function buildFeedResponse($nav, $projects) { + private function buildFeedResponse($nav, array $projects) { + assert_instances_of($projects, 'PhabricatorProject'); $subnav = new AphrontSideNavFilterView(); $subnav->setBaseURI(new PhutilURI('/feed/')); $subnav->addFilter('all', 'All Activity', '/feed/'); $subnav->addFilter('projects', 'My Projects'); $filter = $subnav->selectFilter($this->subfilter, 'all'); switch ($filter) { case 'all': $phids = array(); break; case 'projects': $phids = mpull($projects, 'getPHID'); break; } $view = $this->buildFeedView($phids); $subnav->appendChild($view); $nav->appendChild($subnav); return $this->buildStandardPageResponse( $nav, array( 'title' => 'Feed', )); } private function buildUnbreakNowPanel() { $user = $this->getRequest()->getUser(); $user_phid = $user->getPHID(); $task_query = new ManiphestTaskQuery(); $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN); $task_query->withPriority(ManiphestTaskPriority::PRIORITY_UNBREAK_NOW); $task_query->setLimit(10); $tasks = $task_query->execute(); if (!$tasks) { return $this->renderMiniPanel( 'No "Unbreak Now!" Tasks', 'Nothing appears to be critically broken right now.'); } $panel = new AphrontPanelView(); $panel->setHeader('Unbreak Now!'); $panel->setCaption('Open tasks with "Unbreak Now!" priority.'); $panel->addButton( phutil_render_tag( 'a', array( 'href' => '/maniphest/view/all/', 'class' => 'grey button', ), "View All Unbreak Now \xC2\xBB")); $panel->appendChild($this->buildTaskListView($tasks)); return $panel; } private function buildFlaggedPanel() { $user = $this->getRequest()->getUser(); $flag_query = id(new PhabricatorFlagQuery()) ->withOwnerPHIDs(array($user->getPHID())) ->needHandles(true) ->setLimit(10); $flags = $flag_query->execute(); if (!$flags) { return $this->renderMiniPanel( 'No Flags', "You haven't flagged anything."); } $panel = new AphrontPanelView(); $panel->setHeader('Flagged Objects'); $panel->setCaption("Objects you've flagged."); $flag_view = new PhabricatorFlagListView(); $flag_view->setFlags($flags); $flag_view->setUser($user); $panel->appendChild($flag_view); $panel->addButton( phutil_render_tag( 'a', array( 'href' => '/flag/', 'class' => 'grey button', ), "View All Flags \xC2\xBB")); return $panel; } private function buildNeedsTriagePanel(array $projects) { + assert_instances_of($projects, 'PhabricatorProject'); + $user = $this->getRequest()->getUser(); $user_phid = $user->getPHID(); if ($projects) { $task_query = new ManiphestTaskQuery(); $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN); $task_query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE); $task_query->withProjects(mpull($projects, 'getPHID')); $task_query->withAnyProject(true); $task_query->setLimit(10); $tasks = $task_query->execute(); } else { $tasks = array(); } if (!$tasks) { return $this->renderMiniPanel( 'No "Needs Triage" Tasks', 'No tasks in projects you are a member of '. 'need triage.

'); } $panel = new AphrontPanelView(); $panel->setHeader('Needs Triage'); $panel->setCaption( 'Open tasks with "Needs Triage" priority in '. 'projects you are a member of.'); $panel->addButton( phutil_render_tag( 'a', array( // TODO: This should filter to just your projects' need-triage // tasks? 'href' => '/maniphest/view/projecttriage/', 'class' => 'grey button', ), "View All Triage \xC2\xBB")); $panel->appendChild($this->buildTaskListView($tasks)); return $panel; } private function buildRevisionPanel() { $user = $this->getRequest()->getUser(); $user_phid = $user->getPHID(); $revision_query = new DifferentialRevisionQuery(); $revision_query->withStatus(DifferentialRevisionQuery::STATUS_OPEN); $revision_query->withResponsibleUsers(array($user_phid)); $revision_query->needRelationships(true); // NOTE: We need to unlimit this query to hit the responsible user // fast-path. $revision_query->setLimit(null); $revisions = $revision_query->execute(); list($active, $waiting) = DifferentialRevisionQuery::splitResponsible( $revisions, $user_phid); if (!$active) { return $this->renderMiniPanel( 'No Waiting Revisions', 'No revisions are waiting on you.'); } $panel = new AphrontPanelView(); $panel->setHeader('Revisions Waiting on You'); $panel->setCaption('Revisions waiting for you for review or commit.'); $panel->addButton( phutil_render_tag( 'a', array( 'href' => '/differential/', 'class' => 'button grey', ), "View Active Revisions \xC2\xBB")); $fields = $revision_view = id(new DifferentialRevisionListView()) ->setRevisions($active) ->setFields(DifferentialRevisionListView::getDefaultFields()) ->setUser($user); $phids = array_merge( array($user_phid), $revision_view->getRequiredHandlePHIDs()); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $revision_view->setHandles($handles); $panel->appendChild($revision_view); return $panel; } private function buildTasksPanel() { $user = $this->getRequest()->getUser(); $user_phid = $user->getPHID(); $task_query = new ManiphestTaskQuery(); $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN); $task_query->setGroupBy(ManiphestTaskQuery::GROUP_PRIORITY); $task_query->withOwners(array($user_phid)); $task_query->setLimit(10); $tasks = $task_query->execute(); if (!$tasks) { return $this->renderMiniPanel( 'No Assigned Tasks', 'You have no assigned tasks.'); } $panel = new AphrontPanelView(); $panel->setHeader('Assigned Tasks'); $panel->setCaption('Tasks assigned to you.'); $panel->addButton( phutil_render_tag( 'a', array( 'href' => '/maniphest/', 'class' => 'button grey', ), "View Active Tasks \xC2\xBB")); $panel->appendChild($this->buildTaskListView($tasks)); return $panel; } - private function buildTaskListView(array $tasks) { + assert_instances_of($tasks, 'ManiphestTask'); $user = $this->getRequest()->getUser(); $phids = array_merge( array_filter(mpull($tasks, 'getOwnerPHID')), array_mergev(mpull($tasks, 'getProjectPHIDs'))); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $view = new ManiphestTaskListView(); $view->setTasks($tasks); $view->setUser($user); $view->setHandles($handles); return $view; } private function buildFeedView(array $phids) { $request = $this->getRequest(); $user = $request->getUser(); $user_phid = $user->getPHID(); $feed_query = new PhabricatorFeedQuery(); if ($phids) { $feed_query->setFilterPHIDs($phids); } // TODO: All this limit stuff should probably be consolidated into the // feed query? $old_link = null; $new_link = null; $feed_query->setAfter($request->getStr('after')); $feed_query->setBefore($request->getStr('before')); $limit = 500; // Grab one more story than we intend to display so we can figure out // if we need to render an "Older Posts" link or not (with reasonable // accuracy, at least). $feed_query->setLimit($limit + 1); $feed = $feed_query->execute(); $extra_row = (count($feed) == $limit + 1); $have_new = ($request->getStr('before')) || ($request->getStr('after') && $extra_row); $have_old = ($request->getStr('after')) || ($request->getStr('before') && $extra_row) || (!$request->getStr('before') && !$request->getStr('after') && $extra_row); $feed = array_slice($feed, 0, $limit, $preserve_keys = true); if ($have_old) { $old_link = phutil_render_tag( 'a', array( 'href' => '?before='.end($feed)->getChronologicalKey(), 'class' => 'phabricator-feed-older-link', ), "Older Stories \xC2\xBB"); } if ($have_new) { $new_link = phutil_render_tag( 'a', array( 'href' => '?after='.reset($feed)->getChronologicalKey(), 'class' => 'phabricator-feed-newer-link', ), "\xC2\xAB Newer Stories"); } $builder = new PhabricatorFeedBuilder($feed); $builder->setUser($user); $feed_view = $builder->buildView(); return '
'. '
'. '

Feed

'. '
'. $feed_view->render(). '
'. $new_link. $old_link. '
'. '
'; } private function buildJumpPanel() { $request = $this->getRequest(); $user = $request->getUser(); $uniq_id = celerity_generate_unique_node_id(); Javelin::initBehavior( 'phabricator-autofocus', array( 'id' => $uniq_id, )); require_celerity_resource('phabricator-jump-nav'); $doc_href = PhabricatorEnv::getDocLink('article/Jump_Nav_User_Guide.html'); $doc_link = phutil_render_tag( 'a', array( 'href' => $doc_href, ), 'Jump Nav User Guide'); $jump_input = phutil_render_tag( 'input', array( 'type' => 'text', 'class' => 'phabricator-jump-nav', 'name' => 'jump', 'id' => $uniq_id, )); $jump_caption = phutil_render_tag( 'p', array( 'class' => 'phabricator-jump-nav-caption', ), 'Enter the name of an object like D123 to quickly jump to '. 'it. See '.$doc_link.' or type help.'); $panel = new AphrontPanelView(); $panel->addClass('aphront-unpadded-panel-view'); $panel->appendChild( phabricator_render_form( $user, array( 'action' => '/jump/', 'method' => 'POST', 'class' => 'phabricator-jump-nav-form', ), $jump_input. $jump_caption)); return $panel; } private function buildAppPanel() { require_celerity_resource('phabricator-app-buttons-css'); $nav_buttons = array(); $nav_buttons[] = array( 'Differential', '/differential/', 'differential', 'Code Reviews'); if (PhabricatorEnv::getEnvConfig('maniphest.enabled')) { $nav_buttons[] = array( 'Maniphest', '/maniphest/', 'maniphest', 'Tasks'); $nav_buttons[] = array( 'Create Task', '/maniphest/task/create/', 'create-task'); } $nav_buttons[] = array( 'Upload File', '/file/', 'upload-file', 'Share Files'); $nav_buttons[] = array( 'Create Paste', '/paste/', 'create-paste', 'Share Text'); if (PhabricatorEnv::getEnvConfig('phriction.enabled')) { $nav_buttons[] = array( 'Phriction', '/w/', 'phriction', 'Browse Wiki'); } $nav_buttons[] = array( 'Diffusion', '/diffusion/', 'diffusion', 'Browse Code'); $nav_buttons[] = array( 'Audit', '/audit/', 'audit', 'Audit Code'); $view = new AphrontNullView(); $view->appendChild('
'); foreach ($nav_buttons as $info) { // Subtitle is optional. list($name, $uri, $icon, $subtitle) = array_merge($info, array(null)); if ($subtitle) { $subtitle = '
'. phutil_escape_html($subtitle). '
'; } $button = phutil_render_tag( 'a', array( 'href' => $uri, 'class' => 'app-button icon-'.$icon, ), phutil_render_tag( 'div', array( 'class' => 'app-icon icon-'.$icon, ), '')); $caption = phutil_render_tag( 'a', array( 'href' => $uri, 'class' => 'phabricator-button-caption', ), phutil_escape_html($name).$subtitle); $view->appendChild( '
'. $button. $caption. '
'); } $view->appendChild('
'); return $view; } private function renderMiniPanel($title, $body) { $panel = new AphrontMiniPanelView(); $panel->appendChild( phutil_render_tag( 'p', array( ), ''.$title.': '.$body)); return $panel; } public function buildAuditPanel() { $request = $this->getRequest(); $user = $request->getUser(); $phids = PhabricatorAuditCommentEditor::loadAuditPHIDsForUser($user); $query = new PhabricatorAuditQuery(); $query->withAuditorPHIDs($phids); $query->withStatus(PhabricatorAuditQuery::STATUS_OPEN); $query->withAwaitingUser($user); $query->needCommitData(true); $query->setLimit(10); $audits = $query->execute(); $commits = $query->getCommits(); if (!$audits) { return $this->renderMinipanel( 'No Audits', 'No commits are waiting for you to audit them.'); } $view = new PhabricatorAuditListView(); $view->setAudits($audits); $view->setCommits($commits); $phids = $view->getRequiredHandlePHIDs(); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $view->setHandles($handles); $panel = new AphrontPanelView(); $panel->setHeader('Audits'); $panel->setCaption('Commits awaiting your audit.'); $panel->appendChild($view); $panel->addButton( phutil_render_tag( 'a', array( 'href' => '/audit/', 'class' => 'button grey', ), "View Active Audits \xC2\xBB")); return $panel; } public function buildCommitPanel() { $request = $this->getRequest(); $user = $request->getUser(); $phids = array($user->getPHID()); $query = new PhabricatorAuditCommitQuery(); $query->withAuthorPHIDs($phids); $query->withStatus(PhabricatorAuditQuery::STATUS_OPEN); $query->needCommitData(true); $query->setLimit(10); $commits = $query->execute(); if (!$commits) { return $this->renderMinipanel( 'No Problem Commits', 'No one has raised concerns with your commits.'); } $view = new PhabricatorAuditCommitListView(); $view->setCommits($commits); $view->setUser($user); $phids = $view->getRequiredHandlePHIDs(); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $view->setHandles($handles); $panel = new AphrontPanelView(); $panel->setHeader('Problem Commits'); $panel->setCaption('Commits which auditors have raised concerns about.'); $panel->appendChild($view); $panel->addButton( phutil_render_tag( 'a', array( 'href' => '/audit/', 'class' => 'button grey', ), "View Problem Commits \xC2\xBB")); return $panel; } } diff --git a/src/applications/feed/builder/feed/PhabricatorFeedBuilder.php b/src/applications/feed/builder/feed/PhabricatorFeedBuilder.php index b51a42e722..f326c32aeb 100644 --- a/src/applications/feed/builder/feed/PhabricatorFeedBuilder.php +++ b/src/applications/feed/builder/feed/PhabricatorFeedBuilder.php @@ -1,92 +1,93 @@ stories = $stories; } public function setFramed($framed) { $this->framed = $framed; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function buildView() { if (!$this->user) { throw new Exception('Call setUser() before buildView()!'); } $user = $this->user; $stories = $this->stories; $handles = array(); if ($stories) { $handle_phids = array_mergev(mpull($stories, 'getRequiredHandlePHIDs')); $object_phids = array_mergev(mpull($stories, 'getRequiredObjectPHIDs')); $handles = id(new PhabricatorObjectHandleData($handle_phids)) ->loadHandles(); } $null_view = new AphrontNullView(); require_celerity_resource('phabricator-feed-css'); $last_date = null; foreach ($stories as $story) { $story->setHandles($handles); $story->setFramed($this->framed); $date = ucfirst(phabricator_relative_date($story->getEpoch(), $user)); if ($date !== $last_date) { if ($last_date !== null) { $null_view->appendChild( ''); } $last_date = $date; $null_view->appendChild( phutil_render_tag( 'div', array( 'class' => 'phabricator-feed-story-date', ), phutil_escape_html($date))); } $view = $story->renderView(); $view->setViewer($user); $null_view->appendChild($view); } return id(new AphrontNullView())->appendChild( '
'. $null_view->render(). '
'); } } diff --git a/src/applications/feed/story/base/PhabricatorFeedStory.php b/src/applications/feed/story/base/PhabricatorFeedStory.php index 7bc3a46cb0..fb466b9439 100644 --- a/src/applications/feed/story/base/PhabricatorFeedStory.php +++ b/src/applications/feed/story/base/PhabricatorFeedStory.php @@ -1,116 +1,117 @@ data = $data; } abstract public function renderView(); public function getRequiredHandlePHIDs() { return array(); } public function getRequiredObjectPHIDs() { return array(); } final public function setFramed($framed) { $this->framed = $framed; return $this; } final public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } final protected function getHandles() { return $this->handles; } final protected function getHandle($phid) { if (isset($this->handles[$phid])) { if ($this->handles[$phid] instanceof PhabricatorObjectHandle) { return $this->handles[$phid]; } } $handle = new PhabricatorObjectHandle(); $handle->setPHID($phid); $handle->setName("Unloaded Object '{$phid}'"); return $handle; } final public function getStoryData() { return $this->data; } final public function getEpoch() { return $this->getStoryData()->getEpoch(); } final public function getChronologicalKey() { return $this->getStoryData()->getChronologicalKey(); } final protected function renderHandleList(array $phids) { $list = array(); foreach ($phids as $phid) { $list[] = $this->linkTo($phid); } return implode(', ', $list); } final protected function linkTo($phid) { $handle = $this->getHandle($phid); // NOTE: We render our own link here to customize the styling and add // the '_top' target for framed feeds. return phutil_render_tag( 'a', array( 'href' => $handle->getURI(), 'target' => $this->framed ? '_top' : null, ), phutil_escape_html($handle->getLinkName())); } final protected function renderString($str) { return ''.phutil_escape_html($str).''; } final protected function renderSummary($text, $len = 128) { if ($len) { $text = phutil_utf8_shorten($text, $len); } $text = phutil_escape_html($text); $text = str_replace("\n", '
', $text); return $text; } } diff --git a/src/applications/flag/view/list/PhabricatorFlagListView.php b/src/applications/flag/view/list/PhabricatorFlagListView.php index a367737d32..8b26658f27 100644 --- a/src/applications/flag/view/list/PhabricatorFlagListView.php +++ b/src/applications/flag/view/list/PhabricatorFlagListView.php @@ -1,105 +1,106 @@ flags = $flags; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function render() { $user = $this->user; require_celerity_resource('phabricator-flag-css'); $rows = array(); foreach ($this->flags as $flag) { $class = PhabricatorFlagColor::getCSSClass($flag->getColor()); $rows[] = array( phutil_render_tag( 'div', array( 'class' => 'phabricator-flag-icon '.$class, ), ''), $flag->getHandle()->renderLink(), phutil_escape_html($flag->getNote()), phabricator_datetime($flag->getDateCreated(), $user), phabricator_render_form( $user, array( 'method' => 'POST', 'action' => '/flag/edit/'.$flag->getObjectPHID().'/', 'sigil' => 'workflow', ), phutil_render_tag( 'button', array( 'class' => 'small grey', ), 'Edit Flag')), phabricator_render_form( $user, array( 'method' => 'POST', 'action' => '/flag/delete/'.$flag->getID().'/', 'sigil' => 'workflow', ), phutil_render_tag( 'button', array( 'class' => 'small grey', ), 'Remove Flag')), ); } $table = new AphrontTableView($rows); $table->setHeaders( array( '', 'Flagged Object', 'Note', 'Flagged On', '', '', )); $table->setColumnClasses( array( '', 'pri', 'wide', '', 'action', 'action', )); $table->setNoDataString('No flags.'); return $table->render(); } } diff --git a/src/applications/flag/view/list/__init__.php b/src/applications/flag/view/list/__init__.php index affe0c6169..2ea2704cda 100644 --- a/src/applications/flag/view/list/__init__.php +++ b/src/applications/flag/view/list/__init__.php @@ -1,19 +1,20 @@ repository = $repository; $this->commit = $commit; $this->commitData = $commit_data; } public function getPHID() { return $this->commit->getPHID(); } public function getEmailPHIDs() { return array_keys($this->emailPHIDs); } public function getAuditMap() { return $this->auditMap; } public function getHeraldName() { return 'r'. $this->repository->getCallsign(). $this->commit->getCommitIdentifier(); } public function getHeraldTypeName() { return HeraldContentTypeConfig::CONTENT_TYPE_COMMIT; } public function loadAffectedPaths() { if ($this->affectedPaths === null) { $result = PhabricatorOwnerPathQuery::loadAffectedPaths( $this->repository, $this->commit); $this->affectedPaths = $result; } return $this->affectedPaths; } public function loadAffectedPackages() { if ($this->affectedPackages === null) { $packages = PhabricatorOwnersPackage::loadAffectedPackages( $this->repository, $this->loadAffectedPaths()); $this->affectedPackages = $packages; } return $this->affectedPackages; } public function loadAuditNeededPackage() { if ($this->auditNeededPackages === null) { $status_arr = array( PhabricatorAuditStatusConstants::AUDIT_REQUIRED, PhabricatorAuditStatusConstants::CONCERNED, ); $requests = id(new PhabricatorRepositoryAuditRequest()) ->loadAllWhere( "commitPHID = %s AND auditStatus IN (%Ls)", $this->commit->getPHID(), $status_arr); $packages = mpull($requests, 'getAuditorPHID'); $this->auditNeededPackages = $packages; } return $this->auditNeededPackages; } public function loadDifferentialRevision() { if ($this->affectedRevision === null) { $this->affectedRevision = false; $data = $this->commitData; $revision_id = $data->getCommitDetail('differential.revisionID'); if ($revision_id) { $revision = id(new DifferentialRevision())->load($revision_id); if ($revision) { $revision->loadRelationships(); $this->affectedRevision = $revision; } } } return $this->affectedRevision; } public function getHeraldField($field) { $data = $this->commitData; switch ($field) { case HeraldFieldConfig::FIELD_BODY: return $data->getCommitMessage(); case HeraldFieldConfig::FIELD_AUTHOR: return $data->getCommitDetail('authorPHID'); case HeraldFieldConfig::FIELD_REVIEWER: return $data->getCommitDetail('reviewerPHID'); case HeraldFieldConfig::FIELD_DIFF_FILE: return $this->loadAffectedPaths(); case HeraldFieldConfig::FIELD_REPOSITORY: return $this->repository->getPHID(); case HeraldFieldConfig::FIELD_DIFF_CONTENT: // TODO! return null; /* try { $diff = $this->loadDiff(); } catch (Exception $ex) { // See rE280053 for an example. return array( '<<< Failed to load diff, this usually means the change committed '. 'a binary file as text. >>>', ); } $dict = array(); $changes = $diff->getChangesets(); $lines = array(); foreach ($changes as $change) { $lines = array(); foreach ($change->getHunks() as $hunk) { $lines[] = $hunk->makeChanges(); } $dict[$change->getTrueFilename()] = implode("\n", $lines); } return $dict; */ case HeraldFieldConfig::FIELD_AFFECTED_PACKAGE: $packages = $this->loadAffectedPackages(); return mpull($packages, 'getPHID'); case HeraldFieldConfig::FIELD_AFFECTED_PACKAGE_OWNER: $packages = $this->loadAffectedPackages(); $owners = PhabricatorOwnersOwner::loadAllForPackages($packages); return mpull($owners, 'getUserPHID'); case HeraldFieldConfig::FIELD_NEED_AUDIT_FOR_PACKAGE: return $this->loadAuditNeededPackage(); case HeraldFieldConfig::FIELD_DIFFERENTIAL_REVISION: $revision = $this->loadDifferentialRevision(); if (!$revision) { return null; } return $revision->getID(); case HeraldFieldConfig::FIELD_DIFFERENTIAL_REVIEWERS: $revision = $this->loadDifferentialRevision(); if (!$revision) { return null; } return $revision->getReviewers(); case HeraldFieldConfig::FIELD_DIFFERENTIAL_CCS: $revision = $this->loadDifferentialRevision(); if (!$revision) { return null; } return $revision->getCCPHIDs(); default: throw new Exception("Invalid field '{$field}'."); } } public function applyHeraldEffects(array $effects) { + assert_instances_of($effects, 'HeraldEffect'); $result = array(); foreach ($effects as $effect) { $action = $effect->getAction(); switch ($action) { case HeraldActionConfig::ACTION_NOTHING: $result[] = new HeraldApplyTranscript( $effect, true, 'Great success at doing nothing.'); break; case HeraldActionConfig::ACTION_EMAIL: foreach ($effect->getTarget() as $phid) { $this->emailPHIDs[$phid] = true; } $result[] = new HeraldApplyTranscript( $effect, true, 'Added address to email targets.'); break; case HeraldActionConfig::ACTION_AUDIT: foreach ($effect->getTarget() as $phid) { if (empty($this->auditMap[$phid])) { $this->auditMap[$phid] = array(); } $this->auditMap[$phid][] = $effect->getRuleID(); } $result[] = new HeraldApplyTranscript( $effect, true, 'Triggered an audit.'); break; case HeraldActionConfig::ACTION_FLAG: $result[] = parent::applyFlagEffect( $effect, $this->commit->getPHID()); break; default: throw new Exception("No rules to handle action '{$action}'."); } } return $result; } } diff --git a/src/applications/herald/adapter/dryrun/HeraldDryRunAdapter.php b/src/applications/herald/adapter/dryrun/HeraldDryRunAdapter.php index 9755eef022..fcb68c01b6 100644 --- a/src/applications/herald/adapter/dryrun/HeraldDryRunAdapter.php +++ b/src/applications/herald/adapter/dryrun/HeraldDryRunAdapter.php @@ -1,47 +1,48 @@ getHeraldTypeName(); $rules = HeraldRule::loadAllByContentTypeWithFullData( $content_type, $object->getPHID()); $engine = new HeraldEngine(); $effects = $engine->applyRules($rules, $object); $engine->applyEffects($effects, $object, $rules); return $engine->getTranscript(); } public function applyRules(array $rules, HeraldObjectAdapter $object) { + assert_instances_of($rules, 'HeraldRule'); $t_start = microtime(true); $rules = mpull($rules, null, 'getID'); $this->transcript = new HeraldTranscript(); $this->transcript->setObjectPHID((string)$object->getPHID()); $this->fieldCache = array(); $this->results = array(); $this->rules = $rules; $this->object = $object; $effects = array(); foreach ($rules as $id => $rule) { $this->stack = array(); try { if (($rule->getRepetitionPolicy() == HeraldRepetitionPolicyConfig::FIRST) && $rule->getRuleApplied($object->getPHID())) { // This rule is only supposed to be applied a single time, and it's // aleady been applied, so this is an automatic failure. $xscript = id(new HeraldRuleTranscript()) ->setRuleID($id) ->setResult(false) ->setRuleName($rule->getName()) ->setRuleOwner($rule->getAuthorPHID()) ->setReason( "This rule is only supposed to be repeated a single time, ". "and it has already been applied." ); $this->transcript->addRuleTranscript($xscript); $rule_matches = false; } else { $rule_matches = $this->doesRuleMatch($rule, $object); } } catch (HeraldRecursiveConditionsException $ex) { $names = array(); foreach ($this->stack as $rule_id => $ignored) { $names[] = '"'.$rules[$rule_id]->getName().'"'; } $names = implode(', ', $names); foreach ($this->stack as $rule_id => $ignored) { $xscript = new HeraldRuleTranscript(); $xscript->setRuleID($rule_id); $xscript->setResult(false); $xscript->setReason( "Rules {$names} are recursively dependent upon one another! ". "Don't do this! You have formed an unresolvable cycle in the ". "dependency graph!"); $xscript->setRuleName($rules[$rule_id]->getName()); $xscript->setRuleOwner($rules[$rule_id]->getAuthorPHID()); $this->transcript->addRuleTranscript($xscript); } $rule_matches = false; } $this->results[$id] = $rule_matches; if ($rule_matches) { foreach ($this->getRuleEffects($rule, $object) as $effect) { $effects[] = $effect; } } } $object_transcript = new HeraldObjectTranscript(); $object_transcript->setPHID($object->getPHID()); $object_transcript->setName($object->getHeraldName()); $object_transcript->setType($object->getHeraldTypeName()); $object_transcript->setFields($this->fieldCache); $this->transcript->setObjectTranscript($object_transcript); $t_end = microtime(true); $this->transcript->setDuration($t_end - $t_start); return $effects; } public function applyEffects( array $effects, HeraldObjectAdapter $object, array $rules) { + assert_instances_of($effects, 'HeraldEffect'); + assert_instances_of($rules, 'HeraldRule'); $this->transcript->setDryRun($object instanceof HeraldDryRunAdapter); $xscripts = $object->applyHeraldEffects($effects); foreach ($xscripts as $apply_xscript) { if (!($apply_xscript instanceof HeraldApplyTranscript)) { throw new Exception( "Heraldable must return HeraldApplyTranscripts from ". "applyHeraldEffect()."); } $this->transcript->addApplyTranscript($apply_xscript); } if (!$this->transcript->getDryRun()) { $rules = mpull($rules, null, 'getID'); $applied_ids = array(); $first_policy = HeraldRepetitionPolicyConfig::toInt( HeraldRepetitionPolicyConfig::FIRST); // Mark all the rules that have had their effects applied as having been // executed for the current object. $rule_ids = mpull($xscripts, 'getRuleID'); foreach ($rule_ids as $rule_id) { if (!$rule_id) { // Some apply transcripts are purely informational and not associated // with a rule, e.g. carryover emails from earlier revisions. continue; } $rule = idx($rules, $rule_id); if (!$rule) { continue; } if ($rule->getRepetitionPolicy() == $first_policy) { $applied_ids[] = $rule_id; } } if ($applied_ids) { $conn_w = id(new HeraldRule())->establishConnection('w'); $sql = array(); foreach ($applied_ids as $id) { $sql[] = qsprintf( $conn_w, '(%s, %d)', $object->getPHID(), $id); } queryfx( $conn_w, 'INSERT IGNORE INTO %T (phid, ruleID) VALUES %Q', HeraldRule::TABLE_RULE_APPLIED, implode(', ', $sql)); } } } public function getTranscript() { $this->transcript->save(); return $this->transcript; } protected function doesRuleMatch( HeraldRule $rule, HeraldObjectAdapter $object) { $id = $rule->getID(); if (isset($this->results[$id])) { // If we've already evaluated this rule because another rule depends // on it, we don't need to reevaluate it. return $this->results[$id]; } if (isset($this->stack[$id])) { // We've recursed, fail all of the rules on the stack. This happens when // there's a dependency cycle with "Rule conditions match for rule ..." // conditions. foreach ($this->stack as $rule_id => $ignored) { $this->results[$rule_id] = false; } throw new HeraldRecursiveConditionsException(); } $this->stack[$id] = true; $all = $rule->getMustMatchAll(); $conditions = $rule->getConditions(); $result = null; $local_version = id(new HeraldRule())->getConfigVersion(); if ($rule->getConfigVersion() > $local_version) { $reason = "Rule could not be processed, it was created with a newer ". "version of Herald."; $result = false; } else if (!$conditions) { $reason = "Rule failed automatically because it has no conditions."; $result = false; } else if ($rule->hasInvalidOwner()) { $reason = "Rule failed automatically because its owner is invalid ". "or disabled."; $result = false; } else { foreach ($conditions as $condition) { $match = $this->doesConditionMatch($rule, $condition, $object); if (!$all && $match) { $reason = "Any condition matched."; $result = true; break; } if ($all && !$match) { $reason = "Not all conditions matched."; $result = false; break; } } if ($result === null) { if ($all) { $reason = "All conditions matched."; $result = true; } else { $reason = "No conditions matched."; $result = false; } } } $rule_transcript = new HeraldRuleTranscript(); $rule_transcript->setRuleID($rule->getID()); $rule_transcript->setResult($result); $rule_transcript->setReason($reason); $rule_transcript->setRuleName($rule->getName()); $rule_transcript->setRuleOwner($rule->getAuthorPHID()); $this->transcript->addRuleTranscript($rule_transcript); return $result; } protected function doesConditionMatch( HeraldRule $rule, HeraldCondition $condition, HeraldObjectAdapter $object) { $object_value = $this->getConditionObjectValue($condition, $object); $test_value = $condition->getValue(); $cond = $condition->getFieldCondition(); $transcript = new HeraldConditionTranscript(); $transcript->setRuleID($rule->getID()); $transcript->setConditionID($condition->getID()); $transcript->setFieldName($condition->getFieldName()); $transcript->setCondition($cond); $transcript->setTestValue($test_value); $result = null; switch ($cond) { case HeraldConditionConfig::CONDITION_CONTAINS: // "Contains" can take an array of strings, as in "Any changed // filename" for diffs. foreach ((array)$object_value as $value) { $result = (stripos($value, $test_value) !== false); if ($result) { break; } } break; case HeraldConditionConfig::CONDITION_NOT_CONTAINS: $result = (stripos($object_value, $test_value) === false); break; case HeraldConditionConfig::CONDITION_IS: $result = ($object_value == $test_value); break; case HeraldConditionConfig::CONDITION_IS_NOT: $result = ($object_value != $test_value); break; case HeraldConditionConfig::CONDITION_IS_ME: $result = ($object_value == $rule->getAuthorPHID()); break; case HeraldConditionConfig::CONDITION_IS_NOT_ME: $result = ($object_value != $rule->getAuthorPHID()); break; case HeraldConditionConfig::CONDITION_IS_ANY: $test_value = array_flip($test_value); $result = isset($test_value[$object_value]); break; case HeraldConditionConfig::CONDITION_IS_NOT_ANY: $test_value = array_flip($test_value); $result = !isset($test_value[$object_value]); break; case HeraldConditionConfig::CONDITION_INCLUDE_ALL: if (!is_array($object_value)) { $transcript->setNote('Object produced bad value!'); $result = false; } else { $have = array_select_keys(array_flip($object_value), $test_value); $result = (count($have) == count($test_value)); } break; case HeraldConditionConfig::CONDITION_INCLUDE_ANY: $result = (bool)array_select_keys(array_flip($object_value), $test_value); break; case HeraldConditionConfig::CONDITION_INCLUDE_NONE: $result = !array_select_keys(array_flip($object_value), $test_value); break; case HeraldConditionConfig::CONDITION_EXISTS: $result = (bool)$object_value; break; case HeraldConditionConfig::CONDITION_NOT_EXISTS: $result = !$object_value; break; case HeraldConditionConfig::CONDITION_REGEXP: foreach ((array)$object_value as $value) { $result = @preg_match($test_value, $value); if ($result === false) { $transcript->setNote( "Regular expression is not valid!"); break; } if ($result) { break; } } $result = (bool)$result; break; case HeraldConditionConfig::CONDITION_REGEXP_PAIR: // Match a JSON-encoded pair of regular expressions against a // dictionary. The first regexp must match the dictionary key, and the // second regexp must match the dictionary value. If any key/value pair // in the dictionary matches both regexps, the condition is satisfied. $regexp_pair = json_decode($test_value, true); if (!is_array($regexp_pair)) { $result = false; $transcript->setNote("Regular expression pair is not valid JSON!"); break; } if (count($regexp_pair) != 2) { $result = false; $transcript->setNote("Regular expression pair is not a pair!"); break; } $key_regexp = array_shift($regexp_pair); $value_regexp = array_shift($regexp_pair); foreach ((array)$object_value as $key => $value) { $key_matches = @preg_match($key_regexp, $key); if ($key_matches === false) { $result = false; $transcript->setNote("First regular expression is invalid!"); break 2; } if ($key_matches) { $value_matches = @preg_match($value_regexp, $value); if ($value_matches === false) { $result = false; $transcript->setNote("Second regular expression is invalid!"); break 2; } if ($value_matches) { $result = true; break 2; } } } $result = false; break; case HeraldConditionConfig::CONDITION_RULE: case HeraldConditionConfig::CONDITION_NOT_RULE: $rule = idx($this->rules, $test_value); if (!$rule) { $transcript->setNote( "Condition references a rule which does not exist!"); $result = false; } else { $is_not = ($cond == HeraldConditionConfig::CONDITION_NOT_RULE); $result = $this->doesRuleMatch($rule, $object); if ($is_not) { $result = !$result; } } break; default: throw new HeraldInvalidConditionException( "Unknown condition '{$cond}'."); } $transcript->setResult($result); $this->transcript->addConditionTranscript($transcript); return $result; } protected function getConditionObjectValue( HeraldCondition $condition, HeraldObjectAdapter $object) { $field = $condition->getFieldName(); return $this->getObjectFieldValue($field); } public function getObjectFieldValue($field) { if (isset($this->fieldCache[$field])) { return $this->fieldCache[$field]; } $result = null; switch ($field) { case HeraldFieldConfig::FIELD_RULE: $result = null; break; case HeraldFieldConfig::FIELD_TITLE: case HeraldFieldConfig::FIELD_BODY: case HeraldFieldConfig::FIELD_DIFF_FILE: case HeraldFieldConfig::FIELD_DIFF_CONTENT: // TODO: Type should be string. $result = $this->object->getHeraldField($field); break; case HeraldFieldConfig::FIELD_AUTHOR: case HeraldFieldConfig::FIELD_REPOSITORY: case HeraldFieldConfig::FIELD_MERGE_REQUESTER: // TODO: Type should be PHID. $result = $this->object->getHeraldField($field); break; case HeraldFieldConfig::FIELD_TAGS: case HeraldFieldConfig::FIELD_REVIEWER: case HeraldFieldConfig::FIELD_REVIEWERS: case HeraldFieldConfig::FIELD_CC: case HeraldFieldConfig::FIELD_DIFFERENTIAL_REVIEWERS: case HeraldFieldConfig::FIELD_DIFFERENTIAL_CCS: // TODO: Type should be list. $result = $this->object->getHeraldField($field); break; case HeraldFieldConfig::FIELD_AFFECTED_PACKAGE: case HeraldFieldConfig::FIELD_AFFECTED_PACKAGE_OWNER: case HeraldFieldConfig::FIELD_NEED_AUDIT_FOR_PACKAGE: $result = $this->object->getHeraldField($field); if (!is_array($result)) { throw new HeraldInvalidFieldException( "Value of field type {$field} is not an array!"); } break; case HeraldFieldConfig::FIELD_DIFFERENTIAL_REVISION: // TODO: Type should be boolean I guess. $result = $this->object->getHeraldField($field); break; default: throw new HeraldInvalidConditionException( "Unknown field type '{$field}'!"); } $this->fieldCache[$field] = $result; return $result; } protected function getRuleEffects( HeraldRule $rule, HeraldObjectAdapter $object) { $effects = array(); foreach ($rule->getActions() as $action) { $effect = new HeraldEffect(); $effect->setObjectPHID($object->getPHID()); $effect->setAction($action->getAction()); $effect->setTarget($action->getTarget()); $effect->setRuleID($rule->getID()); $name = $rule->getName(); $id = $rule->getID(); $effect->setReason( 'Conditions were met for Herald rule "'.$name.'" (#'.$id.').'); $effects[] = $effect; } return $effects; } } diff --git a/src/applications/herald/storage/rule/HeraldRule.php b/src/applications/herald/storage/rule/HeraldRule.php index dd6e199c00..87d0592c29 100644 --- a/src/applications/herald/storage/rule/HeraldRule.php +++ b/src/applications/herald/storage/rule/HeraldRule.php @@ -1,231 +1,238 @@ loadAllWhere( 'contentType = %s', $content_type); if (!$rules) { return array(); } self::flagDisabledUserRules($rules); $rule_ids = mpull($rules, 'getID'); $conditions = id(new HeraldCondition())->loadAllWhere( 'ruleID in (%Ld)', $rule_ids); $actions = id(new HeraldAction())->loadAllWhere( 'ruleID in (%Ld)', $rule_ids); $applied = queryfx_all( id(new HeraldRule())->establishConnection('r'), 'SELECT * FROM %T WHERE phid = %s', self::TABLE_RULE_APPLIED, $object_phid); $applied = ipull($applied, null, 'ruleID'); $conditions = mgroup($conditions, 'getRuleID'); $actions = mgroup($actions, 'getRuleID'); $applied = igroup($applied, 'ruleID'); foreach ($rules as $rule) { $rule->setRuleApplied($object_phid, isset($applied[$rule->getID()])); $rule->attachConditions(idx($conditions, $rule->getID(), array())); $rule->attachActions(idx($actions, $rule->getID(), array())); } return $rules; } private static function flagDisabledUserRules(array $rules) { + assert_instances_of($rules, 'HeraldRule'); $users = array(); foreach ($rules as $rule) { if ($rule->getRuleType() != HeraldRuleTypeConfig::RULE_TYPE_PERSONAL) { continue; } $users[$rule->getAuthorPHID()] = true; } $handles = id(new PhabricatorObjectHandleData(array_keys($users))) ->loadHandles(); foreach ($rules as $key => $rule) { if ($rule->getRuleType() != HeraldRuleTypeConfig::RULE_TYPE_PERSONAL) { continue; } $handle = $handles[$rule->getAuthorPHID()]; if (!$handle->isComplete() || $handle->isDisabled()) { $rule->invalidOwner = true; } } } public function getRuleApplied($phid) { if (idx($this->ruleApplied, $phid) === null) { throw new Exception("Call setRuleApplied() before getRuleApplied()!"); } return $this->ruleApplied[$phid]; } public function setRuleApplied($phid, $applied) { $this->ruleApplied[$phid] = $applied; return $this; } public function loadConditions() { if (!$this->getID()) { return array(); } return id(new HeraldCondition())->loadAllWhere( 'ruleID = %d', $this->getID()); } public function attachConditions(array $conditions) { + assert_instances_of($conditions, 'HeraldCondition'); $this->conditions = $conditions; return $this; } public function getConditions() { // TODO: validate conditions have been attached. return $this->conditions; } public function loadActions() { if (!$this->getID()) { return array(); } return id(new HeraldAction())->loadAllWhere( 'ruleID = %d', $this->getID()); } public function attachActions(array $actions) { // TODO: validate actions have been attached. + assert_instances_of($actions, 'HeraldAction'); $this->actions = $actions; return $this; } public function getActions() { return $this->actions; } public function loadEdits() { if (!$this->getID()) { return array(); } $edits = id(new HeraldRuleEdit())->loadAllWhere( 'ruleID = %d ORDER BY dateCreated DESC', $this->getID()); return $edits; } public function logEdit($editor_phid, $action) { id(new HeraldRuleEdit()) ->setRuleID($this->getID()) ->setRuleName($this->getName()) ->setEditorPHID($editor_phid) ->setAction($action) ->save(); } public function saveConditions(array $conditions) { + assert_instances_of($conditions, 'HeraldCondition'); return $this->saveChildren( id(new HeraldCondition())->getTableName(), $conditions); } public function saveActions(array $actions) { + assert_instances_of($actions, 'HeraldAction'); return $this->saveChildren( id(new HeraldAction())->getTableName(), $actions); } protected function saveChildren($table_name, array $children) { + assert_instances_of($children, 'HeraldDAO'); + if (!$this->getID()) { throw new Exception("Save rule before saving children."); } foreach ($children as $child) { $child->setRuleID($this->getID()); } // TODO: // $this->openTransaction(); queryfx( $this->establishConnection('w'), 'DELETE FROM %T WHERE ruleID = %d', $table_name, $this->getID()); foreach ($children as $child) { $child->save(); } // $this->saveTransaction(); } public function delete() { // TODO: // $this->openTransaction(); queryfx( $this->establishConnection('w'), 'DELETE FROM %T WHERE ruleID = %d', id(new HeraldCondition())->getTableName(), $this->getID()); queryfx( $this->establishConnection('w'), 'DELETE FROM %T WHERE ruleID = %d', id(new HeraldAction())->getTableName(), $this->getID()); parent::delete(); // $this->saveTransaction(); } public function hasInvalidOwner() { return $this->invalidOwner; } } diff --git a/src/applications/herald/view/edithistory/HeraldRuleEditHistoryView.php b/src/applications/herald/view/edithistory/HeraldRuleEditHistoryView.php index 0305f10ac6..e8545e6003 100644 --- a/src/applications/herald/view/edithistory/HeraldRuleEditHistoryView.php +++ b/src/applications/herald/view/edithistory/HeraldRuleEditHistoryView.php @@ -1,93 +1,94 @@ edits = $edits; return $this; } public function getEdits() { return $this->edits; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setUser($user) { $this->user = $user; return $this; } public function render() { $rows = array(); foreach ($this->edits as $edit) { $name = nonempty($edit->getRuleName(), 'Unknown Rule'); $rule_name = phutil_render_tag( 'strong', array(), phutil_escape_html($name)); switch ($edit->getAction()) { case 'create': $details = "Created rule '{$rule_name}'."; break; case 'delete': $details = "Deleted rule '{$rule_name}'."; break; case 'edit': default: $details = "Edited rule '{$rule_name}'."; break; } $rows[] = array( $edit->getRuleID(), $this->handles[$edit->getEditorPHID()]->renderLink(), $details, phabricator_datetime($edit->getDateCreated(), $this->user), ); } $table = new AphrontTableView($rows); $table->setNoDataString("No edits for rule."); $table->setHeaders( array( 'Rule ID', 'Editor', 'Details', 'Edit Date', )); $table->setColumnClasses( array( '', '', 'wide', '', )); return $table->render(); } } diff --git a/src/applications/herald/view/rulelist/HeraldRuleListView.php b/src/applications/herald/view/rulelist/HeraldRuleListView.php index d1726742c2..cdefcf30bd 100644 --- a/src/applications/herald/view/rulelist/HeraldRuleListView.php +++ b/src/applications/herald/view/rulelist/HeraldRuleListView.php @@ -1,129 +1,131 @@ rules = $rules; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setShowAuthor($show_author) { $this->showAuthor = $show_author; return $this; } public function setShowRuleType($show_rule_type) { $this->showRuleType = $show_rule_type; return $this; } public function setUser($user) { $this->user = $user; return $this; } public function render() { $type_map = HeraldRuleTypeConfig::getRuleTypeMap(); $rows = array(); foreach ($this->rules as $rule) { if ($rule->getRuleType() == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL) { $author = null; } else { $author = $this->handles[$rule->getAuthorPHID()]->renderLink(); } $name = phutil_render_tag( 'a', array( 'href' => '/herald/rule/'.$rule->getID().'/', ), phutil_escape_html($rule->getName())); $edit_log = phutil_render_tag( 'a', array( 'href' => '/herald/history/'.$rule->getID().'/', ), 'View Edit Log'); $delete = javelin_render_tag( 'a', array( 'href' => '/herald/delete/'.$rule->getID().'/', 'sigil' => 'workflow', 'class' => 'button small grey', ), 'Delete'); $rows[] = array( $type_map[$rule->getRuleType()], $author, $name, $edit_log, $delete, ); } $table = new AphrontTableView($rows); $table->setNoDataString("No matching rules."); $table->setHeaders( array( 'Rule Type', 'Author', 'Rule Name', 'Edit Log', '', )); $table->setColumnClasses( array( '', '', 'wide pri', '', 'action' )); $table->setColumnVisibility( array( $this->showRuleType, $this->showAuthor, true, true, true, )); return $table->render(); } } diff --git a/src/applications/herald/view/rulelist/__init__.php b/src/applications/herald/view/rulelist/__init__.php index 69a972726d..bd042d9859 100644 --- a/src/applications/herald/view/rulelist/__init__.php +++ b/src/applications/herald/view/rulelist/__init__.php @@ -1,17 +1,18 @@ view = idx($data, 'view'); } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); if ($request->isFormPost()) { $uri = $request->getRequestURI(); $project = head($request->getArr('set_project')); $project = nonempty($project, null); $uri = $uri->alter('project', $project); $window = $request->getStr('set_window'); $uri = $uri->alter('window', $window); return id(new AphrontRedirectResponse())->setURI($uri); } $base_nav = $this->buildBaseSideNav(); $base_nav->selectFilter('report', 'report'); $nav = new AphrontSideNavFilterView(); $nav->setBaseURI(new PhutilURI('/maniphest/report/')); $nav->addLabel('Open Tasks'); $nav->addFilter('user', 'By User'); $nav->addFilter('project', 'By Project'); $nav->addSpacer(); $nav->addLabel('Burnup'); $nav->addFilter('burn', 'Burnup Rate'); $this->view = $nav->selectFilter($this->view, 'user'); require_celerity_resource('maniphest-report-css'); switch ($this->view) { case 'burn': $core = $this->renderBurn(); break; case 'user': case 'project': $core = $this->renderOpenTasks(); break; default: return new Aphront404Response(); } $nav->appendChild($core); $base_nav->appendChild($nav); return $this->buildStandardPageResponse( $base_nav, array( 'title' => 'Maniphest Reports', )); } public function renderBurn() { $request = $this->getRequest(); $user = $request->getUser(); $handle = null; $project_phid = $request->getStr('project'); if ($project_phid) { $phids = array($project_phid); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $handle = $handles[$project_phid]; } $table = new ManiphestTransaction(); $conn = $table->establishConnection('r'); $joins = ''; if ($project_phid) { $joins = qsprintf( $conn, 'JOIN %T t ON x.taskID = t.id JOIN %T p ON p.taskPHID = t.phid AND p.projectPHID = %s', id(new ManiphestTask())->getTableName(), id(new ManiphestTaskProject())->getTableName(), $project_phid); } $data = queryfx_all( $conn, 'SELECT x.oldValue, x.newValue, x.dateCreated FROM %T x %Q WHERE transactionType = %s ORDER BY x.dateCreated ASC', $table->getTableName(), $joins, ManiphestTransactionType::TYPE_STATUS); $stats = array(); $day_buckets = array(); $open_tasks = array(); foreach ($data as $key => $row) { // NOTE: Hack to avoid json_decode(). $oldv = trim($row['oldValue'], '"'); $newv = trim($row['newValue'], '"'); $old_is_open = ($oldv === (string)ManiphestTaskStatus::STATUS_OPEN); $new_is_open = ($newv === (string)ManiphestTaskStatus::STATUS_OPEN); $is_open = ($new_is_open && !$old_is_open); $is_close = ($old_is_open && !$new_is_open); $data[$key]['_is_open'] = $is_open; $data[$key]['_is_close'] = $is_close; if (!$is_open && !$is_close) { // This is either some kind of bogus event, or a resolution change // (e.g., resolved -> invalid). Just skip it. continue; } $day_bucket = __phabricator_format_local_time( $row['dateCreated'], $user, 'z'); $day_buckets[$day_bucket] = $row['dateCreated']; if (empty($stats[$day_bucket])) { $stats[$day_bucket] = array( 'open' => 0, 'close' => 0, ); } $stats[$day_bucket][$is_close ? 'close' : 'open']++; } $template = array( 'open' => 0, 'close' => 0, ); $rows = array(); $rowc = array(); $last_month = null; $last_month_epoch = null; $last_week = null; $last_week_epoch = null; $week = null; $month = null; $last = key($stats) - 1; $period = $template; foreach ($stats as $bucket => $info) { $epoch = $day_buckets[$bucket]; $week_bucket = __phabricator_format_local_time( $epoch, $user, 'W'); if ($week_bucket != $last_week) { if ($week) { $rows[] = $this->formatBurnRow( 'Week of '.phabricator_date($last_week_epoch, $user), $week); $rowc[] = 'week'; } $week = $template; $last_week = $week_bucket; $last_week_epoch = $epoch; } $month_bucket = __phabricator_format_local_time( $epoch, $user, 'm'); if ($month_bucket != $last_month) { if ($month) { $rows[] = $this->formatBurnRow( __phabricator_format_local_time($last_month_epoch, $user, 'F, Y'), $month); $rowc[] = 'month'; } $month = $template; $last_month = $month_bucket; $last_month_epoch = $epoch; } $rows[] = $this->formatBurnRow(phabricator_date($epoch, $user), $info); $rowc[] = null; $week['open'] += $info['open']; $week['close'] += $info['close']; $month['open'] += $info['open']; $month['close'] += $info['close']; $period['open'] += $info['open']; $period['close'] += $info['close']; } if ($week) { $rows[] = $this->formatBurnRow( 'Week To Date', $week); $rowc[] = 'week'; } if ($month) { $rows[] = $this->formatBurnRow( 'Month To Date', $month); $rowc[] = 'month'; } $rows[] = $this->formatBurnRow( 'All Time', $period); $rowc[] = 'aggregate'; $rows = array_reverse($rows); $rowc = array_reverse($rowc); $table = new AphrontTableView($rows); $table->setRowClasses($rowc); $table->setHeaders( array( 'Period', 'Opened', 'Closed', 'Change', )); $table->setColumnClasses( array( 'right wide', 'n', 'n', 'n', )); if ($handle) { $header = "Task Burn Rate for Project ".$handle->renderLink(); $caption = "

NOTE: This table reflects tasks currently in ". "the project. If a task was opened in the past but added to ". "the project recently, it is counted on the day it was ". "opened, not the day it was categorized. If a task was part ". "of this project in the past but no longer is, it is not ". "counted at all.

"; } else { $header = "Task Burn Rate for All Tasks"; $caption = null; } $panel = new AphrontPanelView(); $panel->setHeader($header); $panel->setCaption($caption); $panel->appendChild($table); $tokens = array(); if ($handle) { $tokens = array( $handle->getPHID() => $handle->getFullName(), ); } $filter = $this->renderReportFilters($tokens, $has_window = false); $id = celerity_generate_unique_node_id(); $chart = phutil_render_tag( 'div', array( 'id' => $id, 'style' => 'border: 1px solid #6f6f6f; '. 'margin: 1em 2em; '. 'height: 400px; ', ), ''); list($burn_x, $burn_y) = $this->buildSeries($data); require_celerity_resource('raphael-core'); require_celerity_resource('raphael-g'); require_celerity_resource('raphael-g-line'); Javelin::initBehavior('burn-chart', array( 'hardpoint' => $id, 'x' => array( $burn_x, ), 'y' => array( $burn_y, ), )); return array($filter, $chart, $panel); } private function renderReportFilters(array $tokens, $has_window) { $request = $this->getRequest(); $user = $request->getUser(); - $form = id(new AphrontFormView()) ->setUser($user) ->appendChild( id(new AphrontFormTokenizerControl()) ->setDatasource('/typeahead/common/searchproject/') ->setLabel('Project') ->setLimit(1) ->setName('set_project') ->setValue($tokens)); if ($has_window) { list($window_str, $ignored, $window_error) = $this->getWindow(); $form ->appendChild( id(new AphrontFormTextControl()) ->setLabel('"Recently" Means') ->setName('set_window') ->setCaption( 'Configure the cutoff for the "Recently Closed" column.') ->setValue($window_str) ->setError($window_error)); } $form ->appendChild( id(new AphrontFormSubmitControl()) ->setValue('Filter By Project')); $filter = new AphrontListFilterView(); $filter->appendChild($form); return $filter; } private function buildSeries(array $data) { $out = array(); $counter = 0; foreach ($data as $row) { $t = (int)$row['dateCreated']; if ($row['_is_close']) { --$counter; $out[$t] = $counter; } else if ($row['_is_open']) { ++$counter; $out[$t] = $counter; } } return array(array_keys($out), array_values($out)); } private function formatBurnRow($label, $info) { $delta = $info['open'] - $info['close']; $fmt = number_format($delta); if ($delta > 0) { $fmt = '+'.$fmt; $fmt = ''.$fmt.''; } else { $fmt = ''.$fmt.''; } return array( $label, number_format($info['open']), number_format($info['close']), $fmt); } public function renderOpenTasks() { $request = $this->getRequest(); $user = $request->getUser(); $query = id(new ManiphestTaskQuery()) ->withStatus(ManiphestTaskQuery::STATUS_OPEN); $project_phid = $request->getStr('project'); $project_handle = null; if ($project_phid) { $phids = array($project_phid); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $project_handle = $handles[$project_phid]; $query->withProjects($phids); } $tasks = $query->execute(); $recently_closed = $this->loadRecentlyClosedTasks(); $date = phabricator_date(time(), $user); switch ($this->view) { case 'user': $result = mgroup($tasks, 'getOwnerPHID'); $leftover = idx($result, '', array()); unset($result['']); $result_closed = mgroup($recently_closed, 'getOwnerPHID'); $leftover_closed = idx($result_closed, '', array()); unset($result_closed['']); $leftover_name = phutil_render_tag( 'a', array( 'href' => '/maniphest/?users=PHID-!!!!-UP-FOR-GRABS', ), '(Up For Grabs)'); $col_header = 'User'; $header = 'Open Tasks by User and Priority ('.$date.')'; $base_link = '/maniphest/?users='; break; case 'project': $result = array(); $leftover = array(); foreach ($tasks as $task) { $phids = $task->getProjectPHIDs(); if ($phids) { foreach ($phids as $project_phid) { $result[$project_phid][] = $task; } } else { $leftover[] = $task; } } $result_closed = array(); $leftover_closed = array(); foreach ($recently_closed as $task) { $phids = $task->getProjectPHIDs(); if ($phids) { foreach ($phids as $project_phid) { $result_closed[$project_phid][] = $task; } } else { $leftover_closed[] = $task; } } $leftover_name = phutil_render_tag( 'a', array( 'href' => '/maniphest/view/all/?projects=PHID-!!!!-NO_PROJECT', ), '(No Project)'); $col_header = 'Project'; $header = 'Open Tasks by Project and Priority ('.$date.')'; $base_link = '/maniphest/view/all/?projects='; break; } $phids = array_keys($result); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $handles = msort($handles, 'getName'); $order = $request->getStr('order', 'name'); list($order, $reverse) = AphrontTableView::parseSort($order); require_celerity_resource('aphront-tooltip-css'); Javelin::initBehavior('phabricator-tooltips', array()); $rows = array(); $pri_total = array(); foreach (array_merge($handles, array(null)) as $handle) { if ($handle) { if (($project_handle) && ($project_handle->getPHID() == $handle->getPHID())) { // If filtering by, e.g., "bugs", don't show a "bugs" group. continue; } $tasks = idx($result, $handle->getPHID(), array()); $name = phutil_render_tag( 'a', array( 'href' => $base_link.$handle->getPHID(), ), phutil_escape_html($handle->getName())); $closed = idx($result_closed, $handle->getPHID(), array()); } else { $tasks = $leftover; $name = $leftover_name; $closed = $leftover_closed; } $taskv = $tasks; $tasks = mgroup($tasks, 'getPriority'); $row = array(); $row[] = $name; $total = 0; foreach (ManiphestTaskPriority::getTaskPriorityMap() as $pri => $label) { $n = count(idx($tasks, $pri, array())); if ($n == 0) { $row[] = '-'; } else { $row[] = number_format($n); } $total += $n; } $row[] = number_format($total); list($link, $oldest_all) = $this->renderOldest($taskv); $row[] = $link; $normal_or_better = array(); foreach ($taskv as $id => $task) { if ($task->getPriority() < ManiphestTaskPriority::PRIORITY_NORMAL) { continue; } $normal_or_better[$id] = $task; } list($link, $oldest_pri) = $this->renderOldest($normal_or_better); $row[] = $link; if ($closed) { $task_ids = implode(',', mpull($closed, 'getID')); $row[] = phutil_render_tag( 'a', array( 'href' => '/maniphest/view/custom/?s=oc&tasks='.$task_ids, 'target' => '_blank', ), phutil_escape_html(number_format(count($closed)))); } else { $row[] = '-'; } switch ($order) { case 'total': $row['sort'] = $total; break; case 'oldest-all': $row['sort'] = $oldest_all; break; case 'oldest-pri': $row['sort'] = $oldest_pri; break; case 'closed': $row['sort'] = count($closed); break; case 'name': default: $row['sort'] = $handle ? $handle->getName() : '~'; break; } $rows[] = $row; } $rows = isort($rows, 'sort'); foreach ($rows as $k => $row) { unset($rows[$k]['sort']); } if ($reverse) { $rows = array_reverse($rows); } $cname = array($col_header); $cclass = array('pri right wide'); $pri_map = ManiphestTaskPriority::getTaskBriefPriorityMap(); foreach ($pri_map as $pri => $label) { $cname[] = $label; $cclass[] = 'n'; } $cname[] = 'Total'; $cclass[] = 'n'; $cname[] = javelin_render_tag( 'span', array( 'sigil' => 'has-tooltip', 'meta' => array( 'tip' => 'Oldest open task.', 'size' => 200, ), ), 'Oldest (All)'); $cclass[] = 'n'; $cname[] = javelin_render_tag( 'span', array( 'sigil' => 'has-tooltip', 'meta' => array( 'tip' => 'Oldest open task, excluding those with Low or Wishlist '. 'priority.', 'size' => 200, ), ), 'Oldest (Pri)'); $cclass[] = 'n'; list($ignored, $window_epoch) = $this->getWindow(); $cname[] = javelin_render_tag( 'span', array( 'sigil' => 'has-tooltip', 'meta' => array( 'tip' => 'Closed after '.phabricator_datetime($window_epoch, $user), 'size' => 260 ), ), 'Recently Closed'); $cclass[] = 'n'; $table = new AphrontTableView($rows); $table->setHeaders($cname); $table->setColumnClasses($cclass); $table->makeSortable( $request->getRequestURI(), 'order', $order, $reverse, array( 'name', null, null, null, null, null, null, 'total', 'oldest-all', 'oldest-pri', 'closed', )); $panel = new AphrontPanelView(); $panel->setHeader($header); $panel->appendChild($table); $tokens = array(); if ($project_handle) { $tokens = array( $project_handle->getPHID() => $project_handle->getFullName(), ); } $filter = $this->renderReportFilters($tokens, $has_window = true); return array($filter, $panel); } /** * 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(), // TODO: Gross. This table is not meant to be queried like this. Build // real stats tables. 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); } /** * Parse the "Recently Means" filter into: * * - A string representation, like "12 AM 7 days ago" (default); * - a locale-aware epoch representation; and * - a possible error. */ private function getWindow() { $request = $this->getRequest(); $user = $request->getUser(); $window_str = $this->getRequest()->getStr('window', '12 AM 7 days ago'); $error = null; $window_epoch = null; // Do locale-aware parsing so that the user's timezone is assumed for // time windows like "3 PM", rather than assuming the server timezone. $timezone = new DateTimeZone($user->getTimezoneIdentifier()); try { $date = new DateTime($window_str, $timezone); $window_epoch = $date->format('U'); } catch (Exception $e) { $error = 'Invalid'; $window_epoch = time() - (60 * 60 * 24 * 7); } // If the time ends up in the future, convert it to the corresponding time // and equal distance in the past. This is so users can type "6 days" (which // means "6 days from now") and get the behavior of "6 days ago", rather // than no results (because the window epoch is in the future). This might // be a little confusing because it casues "tomorrow" to mean "yesterday" // and "2022" (or whatever) to mean "ten years ago", but these inputs are // nonsense anyway. if ($window_epoch > time()) { $window_epoch = time() - ($window_epoch - time()); } return array($window_str, $window_epoch, $error); } private function renderOldest(array $tasks) { + assert_instances_of($tasks, 'ManiphestTask'); $oldest = null; foreach ($tasks as $id => $task) { if (($oldest === null) || ($task->getDateCreated() < $tasks[$oldest]->getDateCreated())) { $oldest = $id; } } if ($oldest === null) { return array('-', 0); } $oldest = $tasks[$oldest]; $raw_age = (time() - $oldest->getDateCreated()); $age = number_format($raw_age / (24 * 60 * 60)).' d'; $link = javelin_render_tag( 'a', array( 'href' => '/T'.$oldest->getID(), 'sigil' => 'has-tooltip', 'meta' => array( 'tip' => 'T'.$oldest->getID().': '.$oldest->getTitle(), ), 'target' => '_blank', ), phutil_escape_html($age)); return array($link, $raw_age); } } diff --git a/src/applications/maniphest/editor/transaction/ManiphestTransactionEditor.php b/src/applications/maniphest/editor/transaction/ManiphestTransactionEditor.php index a0c463c86e..ac22b5ee54 100644 --- a/src/applications/maniphest/editor/transaction/ManiphestTransactionEditor.php +++ b/src/applications/maniphest/editor/transaction/ManiphestTransactionEditor.php @@ -1,419 +1,426 @@ auxiliaryFields = $fields; return $this; } public function setParentMessageID($parent_message_id) { $this->parentMessageID = $parent_message_id; return $this; } - public function applyTransactions($task, array $transactions) { + public function applyTransactions(ManiphestTask $task, array $transactions) { + assert_instances_of($transactions, 'ManiphestTransaction'); $email_cc = $task->getCCPHIDs(); $email_to = array(); $email_to[] = $task->getOwnerPHID(); $pri_changed = $this->isCreate($transactions); foreach ($transactions as $key => $transaction) { $type = $transaction->getTransactionType(); $new = $transaction->getNewValue(); $email_to[] = $transaction->getAuthorPHID(); $value_is_phid_set = false; switch ($type) { case ManiphestTransactionType::TYPE_NONE: $old = null; break; case ManiphestTransactionType::TYPE_STATUS: $old = $task->getStatus(); break; case ManiphestTransactionType::TYPE_OWNER: $old = $task->getOwnerPHID(); break; case ManiphestTransactionType::TYPE_CCS: $old = $task->getCCPHIDs(); $value_is_phid_set = true; break; case ManiphestTransactionType::TYPE_PRIORITY: $old = $task->getPriority(); break; case ManiphestTransactionType::TYPE_ATTACH: $old = $task->getAttached(); break; case ManiphestTransactionType::TYPE_TITLE: $old = $task->getTitle(); break; case ManiphestTransactionType::TYPE_DESCRIPTION: $old = $task->getDescription(); break; case ManiphestTransactionType::TYPE_PROJECTS: $old = $task->getProjectPHIDs(); $value_is_phid_set = true; break; case ManiphestTransactionType::TYPE_AUXILIARY: $aux_key = $transaction->getMetadataValue('aux:key'); if (!$aux_key) { throw new Exception( "Expected 'aux:key' metadata on TYPE_AUXILIARY transaction."); } $old = $task->getAuxiliaryAttribute($aux_key); break; default: throw new Exception('Unknown action type.'); } $old_cmp = $old; $new_cmp = $new; if ($value_is_phid_set) { // Normalize the old and new values if they are PHID sets so we don't // get any no-op transactions where the values differ only by keys, // order, duplicates, etc. if (is_array($old)) { $old = array_filter($old); $old = array_unique($old); sort($old); $old = array_values($old); $old_cmp = $old; } if (is_array($new)) { $new = array_filter($new); $new = array_unique($new); $transaction->setNewValue($new); $new_cmp = $new; sort($new_cmp); $new_cmp = array_values($new_cmp); } } if (($old !== null) && ($old_cmp == $new_cmp)) { if (count($transactions) > 1 && !$transaction->hasComments()) { // If we have at least one other transaction and this one isn't // doing anything and doesn't have any comments, just throw it // away. unset($transactions[$key]); continue; } else { $transaction->setOldValue(null); $transaction->setNewValue(null); $transaction->setTransactionType(ManiphestTransactionType::TYPE_NONE); } } else { switch ($type) { case ManiphestTransactionType::TYPE_NONE: break; case ManiphestTransactionType::TYPE_STATUS: $task->setStatus($new); break; case ManiphestTransactionType::TYPE_OWNER: if ($new) { $handles = id(new PhabricatorObjectHandleData(array($new))) ->loadHandles(); $task->setOwnerOrdering($handles[$new]->getName()); } else { $task->setOwnerOrdering(null); } $task->setOwnerPHID($new); break; case ManiphestTransactionType::TYPE_CCS: $task->setCCPHIDs($new); break; case ManiphestTransactionType::TYPE_PRIORITY: $task->setPriority($new); $pri_changed = true; break; case ManiphestTransactionType::TYPE_ATTACH: $task->setAttached($new); break; case ManiphestTransactionType::TYPE_TITLE: $task->setTitle($new); break; case ManiphestTransactionType::TYPE_DESCRIPTION: $task->setDescription($new); break; case ManiphestTransactionType::TYPE_PROJECTS: $task->setProjectPHIDs($new); break; case ManiphestTransactionType::TYPE_AUXILIARY: $aux_key = $transaction->getMetadataValue('aux:key'); $task->setAuxiliaryAttribute($aux_key, $new); break; default: throw new Exception('Unknown action type.'); } $transaction->setOldValue($old); $transaction->setNewValue($new); } } if ($pri_changed) { $subpriority = ManiphestTransactionEditor::getNextSubpriority( $task->getPriority(), null); $task->setSubpriority($subpriority); } $task->save(); foreach ($transactions as $transaction) { $transaction->setTaskID($task->getID()); $transaction->save(); } $email_to[] = $task->getOwnerPHID(); $email_cc = array_merge( $email_cc, $task->getCCPHIDs()); $this->publishFeedStory($task, $transactions); // TODO: Do this offline via timeline PhabricatorSearchManiphestIndexer::indexTask($task); $this->sendEmail($task, $transactions, $email_to, $email_cc); } protected function getSubjectPrefix() { return PhabricatorEnv::getEnvConfig('metamta.maniphest.subject-prefix'); } private function sendEmail($task, $transactions, $email_to, $email_cc) { $email_to = array_filter(array_unique($email_to)); $email_cc = array_filter(array_unique($email_cc)); $phids = array(); foreach ($transactions as $transaction) { foreach ($transaction->extractPHIDs() as $phid) { $phids[$phid] = true; } } foreach ($email_to as $phid) { $phids[$phid] = true; } foreach ($email_cc as $phid) { $phids[$phid] = true; } $phids = array_keys($phids); $handles = id(new PhabricatorObjectHandleData($phids)) ->loadHandles(); $view = new ManiphestTransactionDetailView(); $view->setTransactionGroup($transactions); $view->setHandles($handles); $view->setAuxiliaryFields($this->auxiliaryFields); list($action, $body) = $view->renderForEmail($with_date = false); $is_create = $this->isCreate($transactions); $task_uri = PhabricatorEnv::getURI('/T'.$task->getID()); $reply_handler = $this->buildReplyHandler($task); if ($is_create) { $body .= "\n\n". "TASK DESCRIPTION\n". " ".$task->getDescription(); } $body .= "\n\n". "TASK DETAIL\n". " ".$task_uri."\n"; $reply_instructions = $reply_handler->getReplyHandlerInstructions(); if ($reply_instructions) { $body .= "\n". "REPLY HANDLER ACTIONS\n". " ".$reply_instructions."\n"; } $thread_id = 'getPHID().'>'; $task_id = $task->getID(); $title = $task->getTitle(); $prefix = $this->getSubjectPrefix(); $subject = trim("{$prefix} [{$action}] T{$task_id}: {$title}"); $mailtags = $this->getMailTags($transactions); $template = id(new PhabricatorMetaMTAMail()) ->setSubject($subject) ->setFrom($transaction->getAuthorPHID()) ->setParentMessageID($this->parentMessageID) ->addHeader('Thread-Topic', 'Maniphest Task '.$task->getID()) ->setThreadID($thread_id, $is_create) ->setRelatedPHID($task->getPHID()) ->setIsBulk(true) ->setMailTags($mailtags) ->setBody($body); $mails = $reply_handler->multiplexMail( $template, array_select_keys($handles, $email_to), array_select_keys($handles, $email_cc)); foreach ($mails as $mail) { $mail->saveAndSend(); } } public function buildReplyHandler(ManiphestTask $task) { $handler_object = PhabricatorEnv::newObjectFromConfig( 'metamta.maniphest.reply-handler'); $handler_object->setMailReceiver($task); return $handler_object; } private function publishFeedStory(ManiphestTask $task, array $transactions) { + assert_instances_of($transactions, 'ManiphestTransaction'); + $actions = array(ManiphestAction::ACTION_UPDATE); $comments = null; foreach ($transactions as $transaction) { if ($transaction->hasComments()) { $comments = $transaction->getComments(); } switch ($transaction->getTransactionType()) { case ManiphestTransactionType::TYPE_OWNER: $actions[] = ManiphestAction::ACTION_ASSIGN; break; case ManiphestTransactionType::TYPE_STATUS: if ($task->getStatus() != ManiphestTaskStatus::STATUS_OPEN) { $actions[] = ManiphestAction::ACTION_CLOSE; } else if ($this->isCreate($transactions)) { $actions[] = ManiphestAction::ACTION_CREATE; } break; default: break; } } $action_type = ManiphestAction::selectStrongestAction($actions); $owner_phid = $task->getOwnerPHID(); $actor_phid = head($transactions)->getAuthorPHID(); $author_phid = $task->getAuthorPHID(); id(new PhabricatorFeedStoryPublisher()) ->setStoryType(PhabricatorFeedStoryTypeConstants::STORY_MANIPHEST) ->setStoryData(array( 'taskPHID' => $task->getPHID(), 'transactionIDs' => mpull($transactions, 'getID'), 'ownerPHID' => $owner_phid, 'action' => $action_type, 'comments' => $comments, 'description' => $task->getDescription(), )) ->setStoryTime(time()) ->setStoryAuthorPHID($actor_phid) ->setRelatedPHIDs( array_merge( array_filter( array( $task->getPHID(), $author_phid, $actor_phid, $owner_phid, )), $task->getProjectPHIDs())) ->publish(); } private function isCreate(array $transactions) { + assert_instances_of($transactions, 'ManiphestTransaction'); $is_create = false; foreach ($transactions as $transaction) { $type = $transaction->getTransactionType(); if (($type == ManiphestTransactionType::TYPE_STATUS) && ($transaction->getOldValue() === null) && ($transaction->getNewValue() == ManiphestTaskStatus::STATUS_OPEN)) { $is_create = true; } } return $is_create; } private function getMailTags(array $transactions) { + assert_instances_of($transactions, 'ManiphestTransaction'); + $tags = array(); foreach ($transactions as $xaction) { switch ($xaction->getTransactionType()) { case ManiphestTransactionType::TYPE_CCS: $tags[] = MetaMTANotificationType::TYPE_MANIPHEST_CC; break; case ManiphestTransactionType::TYPE_PROJECTS: $tags[] = MetaMTANotificationType::TYPE_MANIPHEST_PROJECTS; break; case ManiphestTransactionType::TYPE_PRIORITY: $tags[] = MetaMTANotificationType::TYPE_MANIPHEST_PRIORITY; break; default: $tags[] = MetaMTANotificationType::TYPE_MANIPHEST_OTHER; break; } if ($xaction->hasComments()) { $tags[] = MetaMTANotificationType::TYPE_MANIPHEST_COMMENT; } } return array_unique($tags); } public static function getNextSubpriority($pri, $sub) { if ($sub === null) { $next = id(new ManiphestTask())->loadOneWhere( 'priority = %d ORDER BY subpriority ASC LIMIT 1', $pri); if ($next) { return $next->getSubpriority() - ((double)(2 << 16)); } } else { $next = id(new ManiphestTask())->loadOneWhere( 'priority = %d AND subpriority > %s ORDER BY subpriority ASC LIMIT 1', $pri, $sub); if ($next) { return ($sub + $next->getSubpriority()) / 2; } } return (double)(2 << 32); } } diff --git a/src/applications/maniphest/query/ManiphestTaskQuery.php b/src/applications/maniphest/query/ManiphestTaskQuery.php index d967dc1bae..27ee4d5d44 100644 --- a/src/applications/maniphest/query/ManiphestTaskQuery.php +++ b/src/applications/maniphest/query/ManiphestTaskQuery.php @@ -1,546 +1,547 @@ authorPHIDs = $authors; return $this; } public function withTaskIDs(array $ids) { $this->taskIDs = $ids; return $this; } public function withOwners(array $owners) { $this->includeUnowned = false; foreach ($owners as $k => $phid) { if ($phid == ManiphestTaskOwner::OWNER_UP_FOR_GRABS) { $this->includeUnowned = true; unset($owners[$k]); break; } } $this->ownerPHIDs = $owners; return $this; } public function withProjects(array $projects) { $this->includeNoProject = false; foreach ($projects as $k => $phid) { if ($phid == ManiphestTaskOwner::PROJECT_NO_PROJECT) { $this->includeNoProject = true; unset($projects[$k]); } } $this->projectPHIDs = $projects; return $this; } public function withoutProjects(array $projects) { $this->xprojectPHIDs = $projects; return $this; } public function withStatus($status) { $this->status = $status; return $this; } public function withPriority($priority) { $this->priority = $priority; return $this; } public function withSubscribers(array $subscribers) { $this->subscriberPHIDs = $subscribers; return $this; } public function setGroupBy($group) { $this->groupBy = $group; return $this; } public function setOrderBy($order) { $this->orderBy = $order; return $this; } public function setLimit($limit) { $this->limit = $limit; return $this; } public function setOffset($offset) { $this->offset = $offset; return $this; } public function setCalculateRows($calculate_rows) { $this->calculateRows = $calculate_rows; return $this; } public function getRowCount() { if ($this->rowCount === null) { throw new Exception( "You must execute a query with setCalculateRows() before you can ". "retrieve a row count."); } return $this->rowCount; } public function withAnyProject($any_project) { $this->anyProject = $any_project; return $this; } 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 = 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; } private function buildTaskIDsWhereClause($conn) { if (!$this->taskIDs) { return null; } return qsprintf( $conn, 'id in (%Ld)', $this->taskIDs); } private function buildStatusWhereClause($conn) { switch ($this->status) { case self::STATUS_ANY: return null; case self::STATUS_OPEN: return 'status = 0'; case self::STATUS_CLOSED: return 'status > 0'; default: throw new Exception("Unknown status query '{$this->status}'!"); } } private function buildPriorityWhereClause($conn) { if ($this->priority === null) { return null; } return qsprintf( $conn, 'priority = %d', $this->priority); } private function buildAuthorWhereClause($conn) { if (!$this->authorPHIDs) { return null; } return qsprintf( $conn, 'authorPHID in (%Ls)', $this->authorPHIDs); } private function buildOwnerWhereClause($conn) { if (!$this->ownerPHIDs) { if ($this->includeUnowned === null) { return null; } else if ($this->includeUnowned) { return qsprintf( $conn, 'ownerPHID IS NULL'); } else { return qsprintf( $conn, 'ownerPHID IS NOT NULL'); } } if ($this->includeUnowned) { return qsprintf( $conn, 'ownerPHID IN (%Ls) OR ownerPHID IS NULL', $this->ownerPHIDs); } else { return qsprintf( $conn, 'ownerPHID IN (%Ls)', $this->ownerPHIDs); } } private function buildSubscriberWhereClause($conn) { if (!$this->subscriberPHIDs) { return null; } return qsprintf( $conn, 'subscriber.subscriberPHID IN (%Ls)', $this->subscriberPHIDs); } private function buildProjectWhereClause($conn) { if (!$this->projectPHIDs && !$this->includeNoProject) { return null; } $parts = array(); if ($this->projectPHIDs) { $parts[] = qsprintf( $conn, 'project.projectPHID in (%Ls)', $this->projectPHIDs); } if ($this->includeNoProject) { $parts[] = qsprintf( $conn, 'project.projectPHID IS NULL'); } return '('.implode(') OR (', $parts).')'; } private function buildProjectJoinClause($conn) { if (!$this->projectPHIDs && !$this->includeNoProject) { return null; } $project_dao = new ManiphestTaskProject(); return qsprintf( $conn, '%Q JOIN %T project ON project.taskPHID = task.phid', ($this->includeNoProject ? 'LEFT' : ''), $project_dao->getTableName()); } private function buildXProjectWhereClause($conn) { if (!$this->xprojectPHIDs) { return null; } return qsprintf( $conn, 'xproject.projectPHID IS NULL'); } private function buildXProjectJoinClause($conn) { if (!$this->xprojectPHIDs) { return null; } $project_dao = new ManiphestTaskProject(); return qsprintf( $conn, 'LEFT JOIN %T xproject ON xproject.taskPHID = task.phid AND xproject.projectPHID IN (%Ls)', $project_dao->getTableName(), $this->xprojectPHIDs); } private function buildSubscriberJoinClause($conn) { if (!$this->subscriberPHIDs) { return null; } $subscriber_dao = new ManiphestTaskSubscriber(); return qsprintf( $conn, 'JOIN %T subscriber ON subscriber.taskPHID = task.phid', $subscriber_dao->getTableName()); } private function buildOrderClause($conn) { $order = array(); switch ($this->groupBy) { case self::GROUP_NONE: break; case self::GROUP_PRIORITY: $order[] = 'priority'; break; case self::GROUP_OWNER: $order[] = 'ownerOrdering'; break; case self::GROUP_STATUS: $order[] = 'status'; break; case self::GROUP_PROJECT: // NOTE: We have to load the entire result set and apply this grouping // in the PHP process for now. break; default: throw new Exception("Unknown group query '{$this->groupBy}'!"); } switch ($this->orderBy) { case self::ORDER_PRIORITY: $order[] = 'priority'; $order[] = 'subpriority'; $order[] = 'dateModified'; break; case self::ORDER_CREATED: $order[] = 'id'; break; case self::ORDER_MODIFIED: $order[] = 'dateModified'; break; default: throw new Exception("Unknown order query '{$this->orderBy}'!"); } $order = array_unique($order); if (empty($order)) { return null; } foreach ($order as $k => $column) { switch ($column) { case 'subpriority': case 'ownerOrdering': $order[$k] = "task.{$column} ASC"; break; default: $order[$k] = "task.{$column} DESC"; break; } } return 'ORDER BY '.implode(', ', $order); } /** * To get paging to work for "group by project", we need to do a bunch of * server-side magic since there's currently no way to sort by project name on * the database. * * TODO: Move this all to the database. */ private function applyGroupByProject(array $tasks) { + assert_instances_of($tasks, 'ManiphestTask'); $project_phids = array(); foreach ($tasks as $task) { foreach ($task->getProjectPHIDs() as $phid) { $project_phids[$phid] = true; } } $handles = id(new PhabricatorObjectHandleData(array_keys($project_phids))) ->loadHandles(); $max = 1; foreach ($handles as $handle) { $max = max($max, strlen($handle->getName())); } $items = array(); $ii = 0; foreach ($tasks as $key => $task) { $phids = $task->getProjectPHIDs(); if (!$this->anyProject && $this->projectPHIDs) { $phids = array_diff($phids, $this->projectPHIDs); } if ($phids) { foreach ($phids as $phid) { $items[] = array( 'key' => $key, 'seq' => sprintf( '%'.$max.'s%d', $handles[$phid]->getName(), $ii), ); } } else { // Sort "no project" tasks first. $items[] = array( 'key' => $key, 'seq' => '', ); } ++$ii; } $items = isort($items, 'seq'); $items = array_slice( $items, nonempty($this->offset), nonempty($this->limit, self::DEFAULT_PAGE_SIZE)); $result = array(); foreach ($items as $item) { $result[] = $tasks[$item['key']]; } return $result; } } diff --git a/src/applications/maniphest/view/tasklist/ManiphestTaskListView.php b/src/applications/maniphest/view/tasklist/ManiphestTaskListView.php index b8572def03..9c3425ca70 100644 --- a/src/applications/maniphest/view/tasklist/ManiphestTaskListView.php +++ b/src/applications/maniphest/view/tasklist/ManiphestTaskListView.php @@ -1,71 +1,73 @@ tasks = $tasks; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function setShowBatchControls($show_batch_controls) { $this->showBatchControls = $show_batch_controls; return $this; } public function setShowSubpriorityControls($show_subpriority_controls) { $this->showSubpriorityControls = $show_subpriority_controls; return $this; } public function render() { $views = array(); foreach ($this->tasks as $task) { $view = new ManiphestTaskSummaryView(); $view->setTask($task); $view->setShowBatchControls($this->showBatchControls); $view->setShowSubpriorityControls($this->showSubpriorityControls); $view->setUser($this->user); $view->setHandles($this->handles); $views[] = $view->render(); } return implode("\n", $views); } } diff --git a/src/applications/maniphest/view/tasklist/__init__.php b/src/applications/maniphest/view/tasklist/__init__.php index be0ea53278..de618c9c54 100644 --- a/src/applications/maniphest/view/tasklist/__init__.php +++ b/src/applications/maniphest/view/tasklist/__init__.php @@ -1,13 +1,15 @@ handles = $handles; return $this; } public function render() { require_celerity_resource('phabricator-project-tag-css'); $show = array_slice($this->handles, 0, 2); $tags = array(); foreach ($show as $handle) { $tags[] = phutil_render_tag( 'a', array( 'href' => $handle->getURI(), 'class' => 'phabricator-project-tag', ), phutil_escape_html( phutil_utf8_shorten($handle->getName(), 24))); } if (count($this->handles) > 2) { require_celerity_resource('aphront-tooltip-css'); Javelin::initBehavior('phabricator-tooltips'); $all = array(); foreach ($this->handles as $handle) { $all[] = $handle->getName(); } $tags[] = javelin_render_tag( 'span', array( 'class' => 'phabricator-project-tag', 'sigil' => 'has-tooltip', 'meta' => array( 'tip' => implode(', ', $all), 'size' => 200, ), ), "\xE2\x80\xA6"); } return implode("\n", $tags); } } diff --git a/src/applications/maniphest/view/tasksummary/ManiphestTaskSummaryView.php b/src/applications/maniphest/view/tasksummary/ManiphestTaskSummaryView.php index 9eaf0f3dff..e2dff15f90 100644 --- a/src/applications/maniphest/view/tasksummary/ManiphestTaskSummaryView.php +++ b/src/applications/maniphest/view/tasksummary/ManiphestTaskSummaryView.php @@ -1,159 +1,160 @@ task = $task; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function setShowBatchControls($show_batch_controls) { $this->showBatchControls = $show_batch_controls; return $this; } public function setShowSubpriorityControls($show_subpriority_controls) { $this->showSubpriorityControls = $show_subpriority_controls; return $this; } public static function getPriorityClass($priority) { $classes = array( ManiphestTaskPriority::PRIORITY_UNBREAK_NOW => 'pri-unbreak', ManiphestTaskPriority::PRIORITY_TRIAGE => 'pri-triage', ManiphestTaskPriority::PRIORITY_HIGH => 'pri-high', ManiphestTaskPriority::PRIORITY_NORMAL => 'pri-normal', ManiphestTaskPriority::PRIORITY_LOW => 'pri-low', ManiphestTaskPriority::PRIORITY_WISH => 'pri-wish', ); return idx($classes, $priority); } public function render() { if (!$this->user) { throw new Exception("Call setUser() before rendering!"); } $task = $this->task; $handles = $this->handles; require_celerity_resource('maniphest-task-summary-css'); $pri_class = self::getPriorityClass($task->getPriority()); $status_map = ManiphestTaskStatus::getTaskStatusMap(); $batch = null; if ($this->showBatchControls) { $batch = ''. javelin_render_tag( 'input', array( 'type' => 'checkbox', 'name' => 'batch[]', 'value' => $task->getID(), 'sigil' => 'maniphest-batch', ), null). ''; } $projects_view = new ManiphestTaskProjectsView(); $projects_view->setHandles( array_select_keys( $this->handles, $task->getProjectPHIDs())); $control_class = null; $control_sigil = null; if ($this->showSubpriorityControls) { $control_class = 'maniphest-active-handle'; $control_sigil = 'maniphest-task-handle'; } $handle = javelin_render_tag( 'td', array( 'class' => 'maniphest-task-handle '.$pri_class.' '.$control_class, 'sigil' => $control_sigil, ), ''); return javelin_render_tag( 'table', array( 'class' => 'maniphest-task-summary', 'sigil' => 'maniphest-task', 'meta' => array( 'taskID' => $task->getID(), ), ), ''. $handle. $batch. ''. 'T'.$task->getID(). ''. ''. idx($status_map, $task->getStatus(), 'Unknown'). ''. ''. ($task->getOwnerPHID() ? $handles[$task->getOwnerPHID()]->renderLink() : 'None'). ''. ''. phutil_render_tag( 'a', array( 'href' => '/T'.$task->getID(), ), phutil_escape_html($task->getTitle())). ''. ''. $projects_view->render(). ''. ''. phabricator_date($task->getDateModified(), $this->user). ''. ''); } } diff --git a/src/applications/maniphest/view/transactiondetail/ManiphestTransactionDetailView.php b/src/applications/maniphest/view/transactiondetail/ManiphestTransactionDetailView.php index b94dbd262f..82ca8c0de1 100644 --- a/src/applications/maniphest/view/transactiondetail/ManiphestTransactionDetailView.php +++ b/src/applications/maniphest/view/transactiondetail/ManiphestTransactionDetailView.php @@ -1,628 +1,631 @@ auxiliaryFields = mpull($fields, null, 'getAuxiliaryKey'); return $this; } public function getAuxiliaryField($key) { return idx($this->auxiliaryFields, $key); } public function setTransactionGroup(array $transactions) { + assert_instances_of($transactions, 'ManiphestTransaction'); $this->transactions = $transactions; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setMarkupEngine(PhutilMarkupEngine $engine) { $this->markupEngine = $engine; return $this; } public function setPreview($preview) { $this->preview = $preview; return $this; } public function setRenderSummaryOnly($render_summary_only) { $this->renderSummaryOnly = $render_summary_only; return $this; } public function getRenderSummaryOnly() { return $this->renderSummaryOnly; } public function setRenderFullSummary($render_full_summary) { $this->renderFullSummary = $render_full_summary; return $this; } public function getRenderFullSummary() { return $this->renderFullSummary; } public function setCommentNumber($comment_number) { $this->commentNumber = $comment_number; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function setRangeSpecification($range) { $this->rangeSpecification = $range; return $this; } public function getRangeSpecification() { return $this->rangeSpecification; } public function renderForEmail($with_date) { $this->forEmail = true; $transaction = reset($this->transactions); $author = $this->renderHandles(array($transaction->getAuthorPHID())); $action = null; $descs = array(); $comments = null; foreach ($this->transactions as $transaction) { list($verb, $desc, $classes) = $this->describeAction($transaction); if ($desc === null) { continue; } if ($action === null) { $action = $verb; } $desc = $author.' '.$desc.'.'; if ($with_date) { // NOTE: This is going into a (potentially multi-recipient) email so // we can't use a single user's timezone preferences. Use the server's // instead, but make the timezone explicit. $datetime = date('M jS \a\t g:i A T', $transaction->getDateCreated()); $desc = "On {$datetime}, {$desc}"; } $descs[] = $desc; if ($transaction->hasComments()) { $comments = $transaction->getComments(); } } $descs = implode("\n", $descs); if ($comments) { $descs .= "\n".$comments; } foreach ($this->transactions as $transaction) { $supplemental = $this->renderSupplementalInfoForEmail($transaction); if ($supplemental) { $descs .= "\n\n".$supplemental; } } $this->forEmail = false; return array($action, $descs); } public function render() { if (!$this->user) { throw new Exception("Call setUser() before render()!"); } $handles = $this->handles; $transactions = $this->transactions; require_celerity_resource('maniphest-transaction-detail-css'); $comment_transaction = null; foreach ($this->transactions as $transaction) { if ($transaction->hasComments()) { $comment_transaction = $transaction; break; } } $any_transaction = reset($transactions); $author = $this->handles[$any_transaction->getAuthorPHID()]; $more_classes = array(); $descs = array(); foreach ($transactions as $transaction) { list($verb, $desc, $classes) = $this->describeAction($transaction); if ($desc === null) { continue; } $more_classes = array_merge($more_classes, $classes); $full_summary = null; if ($this->getRenderFullSummary()) { $full_summary = $this->renderFullSummary($transaction); } $descs[] = javelin_render_tag( 'div', array( 'sigil' => 'maniphest-transaction-description', ), $author->renderLink().' '.$desc.'.'.$full_summary); } if ($this->getRenderSummaryOnly()) { return implode("\n", $descs); } if ($comment_transaction && $comment_transaction->hasComments()) { $comments = $comment_transaction->getCache(); if (!strlen($comments)) { $comments = $comment_transaction->getComments(); if (strlen($comments)) { $comments = $this->markupEngine->markupText($comments); $comment_transaction->setCache($comments); if ($comment_transaction->getID() && !$this->preview) { $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $comment_transaction->save(); unset($unguarded); } } } $comment_block = '
'. $comments. '
'; } else { $comment_block = null; } $source_transaction = nonempty($comment_transaction, $any_transaction); $xaction_view = id(new PhabricatorTransactionView()) ->setUser($this->user) ->setImageURI($author->getImageURI()) ->setContentSource($source_transaction->getContentSource()) ->setActions($descs); foreach ($more_classes as $class) { $xaction_view->addClass($class); } if ($this->preview) { $xaction_view->setIsPreview($this->preview); } else { $xaction_view->setEpoch($any_transaction->getDateCreated()); if ($this->commentNumber) { $anchor_name = 'comment-'.$this->commentNumber; $anchor_text = 'T'.$any_transaction->getTaskID().'#'.$anchor_name; $xaction_view->setAnchor($anchor_name, $anchor_text); } } $xaction_view->appendChild($comment_block); return $xaction_view->render(); } private function renderSupplementalInfoForEmail($transaction) { $handles = $this->handles; $type = $transaction->getTransactionType(); $new = $transaction->getNewValue(); $old = $transaction->getOldValue(); switch ($type) { case ManiphestTransactionType::TYPE_DESCRIPTION: return "NEW DESCRIPTION\n ".trim($new)."\n\n". "PREVIOUS DESCRIPTION\n ".trim($old); case ManiphestTransactionType::TYPE_ATTACH: $old_raw = nonempty($old, array()); $new_raw = nonempty($new, array()); $attach_types = array( PhabricatorPHIDConstants::PHID_TYPE_DREV, PhabricatorPHIDConstants::PHID_TYPE_FILE, ); foreach ($attach_types as $attach_type) { $old = array_keys(idx($old_raw, $attach_type, array())); $new = array_keys(idx($new_raw, $attach_type, array())); if ($old != $new) { break; } } $added = array_diff($new, $old); if (!$added) { break; } $links = array(); foreach (array_select_keys($handles, $added) as $handle) { $links[] = ' '.PhabricatorEnv::getProductionURI($handle->getURI()); } $links = implode("\n", $links); switch ($attach_type) { case PhabricatorPHIDConstants::PHID_TYPE_DREV: $title = 'ATTACHED REVISIONS'; break; case PhabricatorPHIDConstants::PHID_TYPE_FILE: $title = 'ATTACHED FILES'; break; } return $title."\n".$links; default: break; } return null; } private function describeAction($transaction) { $verb = null; $desc = null; $classes = array(); $handles = $this->handles; $type = $transaction->getTransactionType(); $author_phid = $transaction->getAuthorPHID(); $new = $transaction->getNewValue(); $old = $transaction->getOldValue(); switch ($type) { case ManiphestTransactionType::TYPE_TITLE: $verb = 'Retitled'; $desc = 'changed the title from '.$this->renderString($old). ' to '.$this->renderString($new); break; case ManiphestTransactionType::TYPE_DESCRIPTION: $verb = 'Edited'; if ($this->forEmail || $this->getRenderFullSummary()) { $desc = 'updated the task description'; } else { $desc = 'updated the task description; '. $this->renderExpandLink($transaction); } break; case ManiphestTransactionType::TYPE_NONE: $verb = 'Commented On'; $desc = 'added a comment'; break; case ManiphestTransactionType::TYPE_OWNER: if ($transaction->getAuthorPHID() == $new) { $verb = 'Claimed'; $desc = 'claimed this task'; $classes[] = 'claimed'; } else if (!$new) { $verb = 'Up For Grabs'; $desc = 'placed this task up for grabs'; $classes[] = 'upforgrab'; } else if (!$old) { $verb = 'Assigned'; $desc = 'assigned this task to '.$this->renderHandles(array($new)); $classes[] = 'assigned'; } else { $verb = 'Reassigned'; $desc = 'reassigned this task from '. $this->renderHandles(array($old)). ' to '. $this->renderHandles(array($new)); $classes[] = 'reassigned'; } break; case ManiphestTransactionType::TYPE_CCS: if ($this->preview) { $verb = 'Changed CC'; $desc = 'changed CCs..'; break; } $added = array_diff($new, $old); $removed = array_diff($old, $new); if ($added && !$removed) { $verb = 'Added CC'; if (count($added) == 1) { $desc = 'added '.$this->renderHandles($added).' to CC'; } else { $desc = 'added CCs: '.$this->renderHandles($added); } } else if ($removed && !$added) { $verb = 'Removed CC'; if (count($removed) == 1) { $desc = 'removed '.$this->renderHandles($removed).' from CC'; } else { $desc = 'removed CCs: '.$this->renderHandles($removed); } } else { $verb = 'Changed CC'; $desc = 'changed CCs, added: '.$this->renderHandles($added).'; '. 'removed: '.$this->renderHandles($removed); } break; case ManiphestTransactionType::TYPE_PROJECTS: if ($this->preview) { $verb = 'Changed Projects'; $desc = 'changed projects..'; break; } $added = array_diff($new, $old); $removed = array_diff($old, $new); if ($added && !$removed) { $verb = 'Added Project'; if (count($added) == 1) { $desc = 'added project '.$this->renderHandles($added); } else { $desc = 'added projects: '.$this->renderHandles($added); } } else if ($removed && !$added) { $verb = 'Removed Project'; if (count($removed) == 1) { $desc = 'removed project '.$this->renderHandles($removed); } else { $desc = 'removed projectss: '.$this->renderHandles($removed); } } else { $verb = 'Changed Projects'; $desc = 'changed projects, added: '.$this->renderHandles($added).'; '. 'removed: '.$this->renderHandles($removed); } break; case ManiphestTransactionType::TYPE_STATUS: if ($new == ManiphestTaskStatus::STATUS_OPEN) { if ($old) { $verb = 'Reopened'; $desc = 'reopened this task'; $classes[] = 'reopened'; } else { $verb = 'Created'; $desc = 'created this task'; $classes[] = 'created'; } } else if ($new == ManiphestTaskStatus::STATUS_CLOSED_SPITE) { $verb = 'Spited'; $desc = 'closed this task out of spite'; $classes[] = 'spited'; } else if ($new == ManiphestTaskStatus::STATUS_CLOSED_DUPLICATE) { $verb = 'Merged'; $desc = 'closed this task as a duplicate'; $classes[] = 'duplicate'; } else { $verb = 'Closed'; $full = idx(ManiphestTaskStatus::getTaskStatusMap(), $new, '???'); $desc = 'closed this task as "'.$full.'"'; $classes[] = 'closed'; } break; case ManiphestTransactionType::TYPE_PRIORITY: $old_name = ManiphestTaskPriority::getTaskPriorityName($old); $new_name = ManiphestTaskPriority::getTaskPriorityName($new); if ($old == ManiphestTaskPriority::PRIORITY_TRIAGE) { $verb = 'Triaged'; $desc = 'triaged this task as "'.$new_name.'" priority'; } else if ($old > $new) { $verb = 'Lowered Priority'; $desc = 'lowered the priority of this task from "'.$old_name.'" to '. '"'.$new_name.'"'; } else { $verb = 'Raised Priority'; $desc = 'raised the priority of this task from "'.$old_name.'" to '. '"'.$new_name.'"'; } if ($new == ManiphestTaskPriority::PRIORITY_UNBREAK_NOW) { $classes[] = 'unbreaknow'; } break; case ManiphestTransactionType::TYPE_ATTACH: if ($this->preview) { $verb = 'Changed Attached'; $desc = 'changed attachments..'; break; } $old_raw = nonempty($old, array()); $new_raw = nonempty($new, array()); foreach (array( PhabricatorPHIDConstants::PHID_TYPE_DREV, PhabricatorPHIDConstants::PHID_TYPE_TASK, PhabricatorPHIDConstants::PHID_TYPE_FILE) as $attach_type) { $old = array_keys(idx($old_raw, $attach_type, array())); $new = array_keys(idx($new_raw, $attach_type, array())); if ($old != $new) { break; } } $added = array_diff($new, $old); $removed = array_diff($old, $new); $add_desc = $this->renderHandles($added); $rem_desc = $this->renderHandles($removed); switch ($attach_type) { case PhabricatorPHIDConstants::PHID_TYPE_DREV: $singular = 'Differential Revision'; $plural = 'Differential Revisions'; break; case PhabricatorPHIDConstants::PHID_TYPE_FILE: $singular = 'file'; $plural = 'files'; break; case PhabricatorPHIDConstants::PHID_TYPE_TASK: $singular = 'Maniphest Task'; $plural = 'Maniphest Tasks'; $dependency = true; break; } if ($added && !$removed) { $verb = 'Attached'; if (count($added) == 1) { $desc = 'attached '.$singular.': '.$add_desc; } else { $desc = 'attached '.$plural.': '.$add_desc; } } else if ($removed && !$added) { $verb = 'Detached'; if (count($removed) == 1) { $desc = 'detached '.$singular.': '.$rem_desc; } else { $desc = 'detached '.$plural.': '.$rem_desc; } } else { $verb = 'Changed Attached'; $desc = 'changed attached '.$plural.', added: '.$add_desc.'; '. 'removed: '.$rem_desc; } break; case ManiphestTransactionType::TYPE_AUXILIARY: $aux_key = $transaction->getMetadataValue('aux:key'); $aux_field = $this->getAuxiliaryField($aux_key); $verb = null; if ($aux_field) { $verb = $aux_field->renderTransactionEmailVerb($transaction); } if ($verb === null) { if ($old === null) { $verb = "Set Field"; } else if ($new === null) { $verb = "Removed Field"; } else { $verb = "Updated Field"; } } $desc = null; if ($aux_field) { $use_field = $aux_field; } else { $use_field = id(new ManiphestAuxiliaryFieldDefaultSpecification()) ->setFieldType( ManiphestAuxiliaryFieldDefaultSpecification::TYPE_STRING); } $desc = $use_field->renderTransactionDescription( $transaction, $this->forEmail ? ManiphestAuxiliaryFieldSpecification::RENDER_TARGET_TEXT : ManiphestAuxiliaryFieldSpecification::RENDER_TARGET_HTML); break; default: return array($type, ' brazenly '.$type."'d", $classes); } return array($verb, $desc, $classes); } private function renderFullSummary($transaction) { switch ($transaction->getTransactionType()) { case ManiphestTransactionType::TYPE_DESCRIPTION: $id = $transaction->getID(); $old_text = wordwrap($transaction->getOldValue(), 80); $new_text = wordwrap($transaction->getNewValue(), 80); $engine = new PhabricatorDifferenceEngine(); $changeset = $engine->generateChangesetFromFileContent($old_text, $new_text); $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL; $parser = new DifferentialChangesetParser(); $parser->setChangeset($changeset); $parser->setRenderingReference($id); $parser->setWhitespaceMode($whitespace_mode); $spec = $this->getRangeSpecification(); list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec); $output = $parser->render($range_s, $range_e, $mask); return $output; } return null; } private function renderExpandLink($transaction) { $id = $transaction->getID(); Javelin::initBehavior('maniphest-transaction-expand'); return javelin_render_tag( 'a', array( 'href' => '/maniphest/task/descriptionchange/'.$id.'/', 'sigil' => 'maniphest-expand-transaction', 'mustcapture' => true, ), 'show details'); } private function renderHandles($phids) { $links = array(); foreach ($phids as $phid) { if ($this->forEmail) { $links[] = $this->handles[$phid]->getName(); } else { $links[] = $this->handles[$phid]->renderLink(); } } return implode(', ', $links); } private function renderString($string) { if ($this->forEmail) { return '"'.$string.'"'; } else { return '"'.phutil_escape_html($string).'"'; } } } diff --git a/src/applications/maniphest/view/transactionlist/ManiphestTransactionListView.php b/src/applications/maniphest/view/transactionlist/ManiphestTransactionListView.php index 01b84cbc9d..1e5e5718d8 100644 --- a/src/applications/maniphest/view/transactionlist/ManiphestTransactionListView.php +++ b/src/applications/maniphest/view/transactionlist/ManiphestTransactionListView.php @@ -1,123 +1,126 @@ transactions = $transactions; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function setMarkupEngine(PhutilMarkupEngine $engine) { $this->markupEngine = $engine; return $this; } public function setPreview($preview) { $this->preview = $preview; return $this; } public function setAuxiliaryFields(array $fields) { + assert_instances_of($fields, 'ManiphestAuxiliaryFieldSpecification'); $this->auxiliaryFields = $fields; return $this; } public function render() { $views = array(); $last = null; $group = array(); $groups = array(); $has_description_transaction = false; foreach ($this->transactions as $transaction) { if ($transaction->getTransactionType() == ManiphestTransactionType::TYPE_DESCRIPTION) { $has_description_transaction = true; } if ($last === null) { $last = $transaction; $group[] = $transaction; continue; } else if ($last->canGroupWith($transaction)) { $group[] = $transaction; if ($transaction->hasComments()) { $last = $transaction; } } else { $groups[] = $group; $last = $transaction; $group = array($transaction); } } if ($group) { $groups[] = $group; } if ($has_description_transaction) { require_celerity_resource('differential-changeset-view-css'); require_celerity_resource('syntax-highlighting-css'); $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL; Javelin::initBehavior('differential-show-more', array( 'uri' => '/maniphest/task/descriptionchange/', 'whitespace' => $whitespace_mode, )); } $sequence = 1; foreach ($groups as $group) { $view = new ManiphestTransactionDetailView(); $view->setUser($this->user); $view->setAuxiliaryFields($this->auxiliaryFields); $view->setTransactionGroup($group); $view->setHandles($this->handles); $view->setMarkupEngine($this->markupEngine); $view->setPreview($this->preview); $view->setCommentNumber($sequence++); $views[] = $view->render(); } return '
'. implode("\n", $views). '
'; } } diff --git a/src/applications/maniphest/view/transactionlist/__init__.php b/src/applications/maniphest/view/transactionlist/__init__.php index 9f8ccfe06d..4d98cfa8a7 100644 --- a/src/applications/maniphest/view/transactionlist/__init__.php +++ b/src/applications/maniphest/view/transactionlist/__init__.php @@ -1,17 +1,19 @@ validateMailReceiver($mail_receiver); $this->mailReceiver = $mail_receiver; return $this; } final public function getMailReceiver() { return $this->mailReceiver; } final public function setActor(PhabricatorUser $actor) { $this->actor = $actor; return $this; } final public function getActor() { return $this->actor; } abstract public function validateMailReceiver($mail_receiver); abstract public function getPrivateReplyHandlerEmailAddress( PhabricatorObjectHandle $handle); abstract public function getReplyHandlerDomain(); abstract public function getReplyHandlerInstructions(); abstract public function receiveEmail(PhabricatorMetaMTAReceivedMail $mail); public function supportsPrivateReplies() { return (bool)$this->getReplyHandlerDomain() && !$this->supportsPublicReplies(); } public function supportsPublicReplies() { if (!PhabricatorEnv::getEnvConfig('metamta.public-replies')) { return false; } if (!$this->getReplyHandlerDomain()) { return false; } return (bool)$this->getPublicReplyHandlerEmailAddress(); } final public function supportsReplies() { return $this->supportsPrivateReplies() || $this->supportsPublicReplies(); } public function getPublicReplyHandlerEmailAddress() { return null; } final public function multiplexMail( PhabricatorMetaMTAMail $mail_template, array $to_handles, array $cc_handles) { + assert_instances_of($to_handles, 'PhabricatorObjectHandle'); + assert_instances_of($cc_handles, 'PhabricatorObjectHandle'); $result = array(); // If private replies are not supported, simply send one email to all // recipients and CCs. This covers cases where we have no reply handler, // or we have a public reply handler. if (!$this->supportsPrivateReplies()) { $mail = clone $mail_template; $mail->addTos(mpull($to_handles, 'getPHID')); $mail->addCCs(mpull($cc_handles, 'getPHID')); if ($this->supportsPublicReplies()) { $reply_to = $this->getPublicReplyHandlerEmailAddress(); $mail->setReplyTo($reply_to); } $result[] = $mail; return $result; } // Merge all the recipients together. TODO: We could keep the CCs as real // CCs and send to a "noreply@domain.com" type address, but keep it simple // for now. $recipients = mpull($to_handles, null, 'getPHID') + mpull($cc_handles, null, 'getPHID'); // This grouping is just so we can use the public reply-to for any // recipients without a private reply-to, e.g. mailing lists. $groups = array(); foreach ($recipients as $recipient) { $private = $this->getPrivateReplyHandlerEmailAddress($recipient); $groups[$private][] = $recipient; } // When multiplexing mail, explicitly include To/Cc information in the // message body and headers. $add_headers = array(); $body = $mail_template->getBody(); $body .= "\n"; if ($to_handles) { $body .= "To: ".implode(', ', mpull($to_handles, 'getName'))."\n"; $add_headers['X-Phabricator-To'] = $this->formatPHIDList($to_handles); } if ($cc_handles) { $body .= "Cc: ".implode(', ', mpull($cc_handles, 'getName'))."\n"; $add_headers['X-Phabricator-Cc'] = $this->formatPHIDList($cc_handles); } foreach ($groups as $reply_to => $group) { $mail = clone $mail_template; $mail->addTos(mpull($group, 'getPHID')); $mail->setBody($body); foreach ($add_headers as $header => $value) { $mail->addHeader($header, $value); } if (!$reply_to && $this->supportsPublicReplies()) { $reply_to = $this->getPublicReplyHandlerEmailAddress(); } if ($reply_to) { $mail->setReplyTo($reply_to); } $result[] = $mail; } return $result; } protected function formatPHIDList(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $list = array(); foreach ($handles as $handle) { $list[] = '<'.$handle->getPHID().'>'; } return implode(', ', $list); } protected function getDefaultPublicReplyHandlerEmailAddress($prefix) { $receiver = $this->getMailReceiver(); $receiver_id = $receiver->getID(); $domain = $this->getReplyHandlerDomain(); // We compute a hash using the object's own PHID to prevent an attacker // from blindly interacting with objects that they haven't ever received // mail about by just sending to D1@, D2@, etc... $hash = PhabricatorMetaMTAReceivedMail::computeMailHash( $receiver->getMailKey(), $receiver->getPHID()); $address = "{$prefix}{$receiver_id}+public+{$hash}@{$domain}"; return $this->getSingleReplyHandlerPrefix($address); } protected function getSingleReplyHandlerPrefix($address) { $single_handle_prefix = PhabricatorEnv::getEnvConfig( 'metamta.single-reply-handler-prefix'); return ($single_handle_prefix) ? $single_handle_prefix . '+' . $address : $address; } protected function getDefaultPrivateReplyHandlerEmailAddress( PhabricatorObjectHandle $handle, $prefix) { if ($handle->getType() != PhabricatorPHIDConstants::PHID_TYPE_USER) { // You must be a real user to get a private reply handler address. return null; } $receiver = $this->getMailReceiver(); $receiver_id = $receiver->getID(); $user_id = $handle->getAlternateID(); $hash = PhabricatorMetaMTAReceivedMail::computeMailHash( $receiver->getMailKey(), $handle->getPHID()); $domain = $this->getReplyHandlerDomain(); $address = "{$prefix}{$receiver_id}+{$user_id}+{$hash}@{$domain}"; return $this->getSingleReplyHandlerPrefix($address); } } diff --git a/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php b/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php index b5193031e8..458c7545c0 100644 --- a/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php +++ b/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php @@ -1,635 +1,637 @@ status = self::STATUS_QUEUE; $this->retryCount = 0; $this->nextRetry = time(); $this->parameters = array(); parent::__construct(); } public function getConfiguration() { return array( self::CONFIG_SERIALIZATION => array( 'parameters' => self::SERIALIZATION_JSON, ), ) + parent::getConfiguration(); } protected function setParam($param, $value) { $this->parameters[$param] = $value; return $this; } protected function getParam($param) { return idx($this->parameters, $param); } /** * Set tags (@{class:MetaMTANotificationType} constants) which identify the * content of this mail in a general way. These tags are used to allow users * to opt out of receiving certain types of mail, like updates when a task's * projects change. * * @param list List of @{class:MetaMTANotificationType} constants. * @return this */ public function setMailTags(array $tags) { $this->setParam('mailtags', $tags); return $this; } /** * In Gmail, conversations will be broken if you reply to a thread and the * server sends back a response without referencing your Message-ID, even if * it references a Message-ID earlier in the thread. To avoid this, use the * parent email's message ID explicitly if it's available. This overwrites the * "In-Reply-To" and "References" headers we would otherwise generate. This * needs to be set whenever an action is triggered by an email message. See * T251 for more details. * * @param string The "Message-ID" of the email which precedes this one. * @return this */ public function setParentMessageID($id) { $this->setParam('parent-message-id', $id); return $this; } public function getParentMessageID() { return $this->getParam('parent-message-id'); } public function getSubject() { return $this->getParam('subject'); } public function addTos(array $phids) { $phids = array_unique($phids); $this->setParam('to', $phids); return $this; } public function addCCs(array $phids) { $phids = array_unique($phids); $this->setParam('cc', $phids); return $this; } public function addHeader($name, $value) { $this->parameters['headers'][$name] = $value; return $this; } public function addAttachment(PhabricatorMetaMTAAttachment $attachment) { $this->parameters['attachments'][] = $attachment; return $this; } public function getAttachments() { return $this->getParam('attachments'); } public function setAttachments(array $attachments) { + assert_instances_of($attachments, 'PhabricatorMetaMTAAttachment'); $this->setParam('attachments', $attachments); return $this; } public function setFrom($from) { $this->setParam('from', $from); return $this; } public function setReplyTo($reply_to) { $this->setParam('reply-to', $reply_to); return $this; } public function setSubject($subject) { $this->setParam('subject', $subject); return $this; } public function setBody($body) { $this->setParam('body', $body); return $this; } public function getBody() { return $this->getParam('body'); } public function setIsHTML($html) { $this->setParam('is-html', $html); return $this; } public function getSimulatedFailureCount() { return nonempty($this->getParam('simulated-failures'), 0); } public function setSimulatedFailureCount($count) { $this->setParam('simulated-failures', $count); return $this; } public function getWorkerTaskID() { return $this->getParam('worker-task'); } public function setWorkerTaskID($id) { $this->setParam('worker-task', $id); return $this; } /** * Flag that this is an auto-generated bulk message and should have bulk * headers added to it if appropriate. Broadly, this means some flavor of * "Precedence: bulk" or similar, but is implementation and configuration * dependent. * * @param bool True if the mail is automated bulk mail. * @return this */ public function setIsBulk($is_bulk) { $this->setParam('is-bulk', $is_bulk); return $this; } /** * Use this method to set an ID used for message threading. MetaMTA will * set appropriate headers (Message-ID, In-Reply-To, References and * Thread-Index) based on the capabilities of the underlying mailer. * * @param string Unique identifier, appropriate for use in a Message-ID, * In-Reply-To or References headers. * @param bool If true, indicates this is the first message in the thread. * @return this */ public function setThreadID($thread_id, $is_first_message = false) { $this->setParam('thread-id', $thread_id); $this->setParam('is-first-message', $is_first_message); return $this; } /** * Save a newly created mail to the database and attempt to send it * immediately if the server is configured for immediate sends. When * applications generate new mail they should generally use this method to * deliver it. If the server doesn't use immediate sends, this has the same * effect as calling save(): the mail will eventually be delivered by the * MetaMTA daemon. * * @return this */ public function saveAndSend() { $ret = null; if (PhabricatorEnv::getEnvConfig('metamta.send-immediately')) { $ret = $this->sendNow(); } else { $ret = $this->save(); } return $ret; } protected function didWriteData() { parent::didWriteData(); if (!$this->getWorkerTaskID()) { $mailer_task = new PhabricatorWorkerTask(); $mailer_task->setTaskClass('PhabricatorMetaMTAWorker'); $mailer_task->setData($this->getID()); $mailer_task->save(); $this->setWorkerTaskID($mailer_task->getID()); $this->save(); } } public function buildDefaultMailer() { return PhabricatorEnv::newObjectFromConfig('metamta.mail-adapter'); } /** * Attempt to deliver an email immediately, in this process. * * @param bool Try to deliver this email even if it has already been * delivered or is in backoff after a failed delivery attempt. * @param PhabricatorMailImplementationAdapter Use a specific mail adapter, * instead of the default. * * @return void */ public function sendNow( $force_send = false, PhabricatorMailImplementationAdapter $mailer = null) { if ($mailer === null) { $mailer = $this->buildDefaultMailer(); } if (!$force_send) { if ($this->getStatus() != self::STATUS_QUEUE) { throw new Exception("Trying to send an already-sent mail!"); } if (time() < $this->getNextRetry()) { throw new Exception("Trying to send an email before next retry!"); } } try { $parameters = $this->parameters; $phids = array(); foreach ($parameters as $key => $value) { switch ($key) { case 'from': case 'to': case 'cc': if (!is_array($value)) { $value = array($value); } foreach (array_filter($value) as $phid) { $phids[] = $phid; } break; } } $handles = id(new PhabricatorObjectHandleData($phids)) ->loadHandles(); $exclude = array(); $params = $this->parameters; $default = PhabricatorEnv::getEnvConfig('metamta.default-address'); if (empty($params['from'])) { $mailer->setFrom($default); } else { $from = $params['from']; // If the user has set their preferences to not send them email about // things they do, exclude them from being on To or Cc. $from_user = id(new PhabricatorUser())->loadOneWhere( 'phid = %s', $from); if ($from_user) { $pref_key = PhabricatorUserPreferences::PREFERENCE_NO_SELF_MAIL; $exclude_self = $from_user ->loadPreferences() ->getPreference($pref_key); if ($exclude_self) { $exclude[$from] = true; } } if (!PhabricatorEnv::getEnvConfig('metamta.can-send-as-user')) { $handle = $handles[$from]; if (empty($params['reply-to'])) { $params['reply-to'] = $handle->getEmail(); $params['reply-to-name'] = $handle->getFullName(); } $mailer->setFrom( $default, $handle->getFullName()); unset($params['from']); } } $is_first = idx($params, 'is-first-message'); unset($params['is-first-message']); $is_threaded = (bool)idx($params, 'thread-id'); $reply_to_name = idx($params, 'reply-to-name', ''); unset($params['reply-to-name']); $add_cc = array(); $add_to = array(); foreach ($params as $key => $value) { switch ($key) { case 'from': $mailer->setFrom($handles[$value]->getEmail()); break; case 'reply-to': $mailer->addReplyTo($value, $reply_to_name); break; case 'to': $emails = $this->getDeliverableEmailsFromHandles( $value, $handles, $exclude); if ($emails) { $add_to = $emails; } break; case 'cc': $emails = $this->getDeliverableEmailsFromHandles( $value, $handles, $exclude); if ($emails) { $add_cc = $emails; } break; case 'headers': foreach ($value as $header_key => $header_value) { // NOTE: If we have \n in a header, SES rejects the email. $header_value = str_replace("\n", " ", $header_value); $mailer->addHeader($header_key, $header_value); } break; case 'attachments': foreach ($value as $attachment) { $mailer->addAttachment( $attachment->getData(), $attachment->getFilename(), $attachment->getMimeType() ); } break; case 'body': $mailer->setBody($value); break; case 'subject': if ($is_threaded) { $add_re = PhabricatorEnv::getEnvConfig('metamta.re-prefix'); // If this message has a single recipient, respect their "Re:" // preference. Otherwise, use the global setting. $to = idx($params, 'to', array()); $cc = idx($params, 'cc', array()); if (count($to) == 1 && count($cc) == 0) { $user = id(new PhabricatorUser())->loadOneWhere( 'phid = %s', head($to)); if ($user) { $prefs = $user->loadPreferences(); $pref_key = PhabricatorUserPreferences::PREFERENCE_RE_PREFIX; $add_re = $prefs->getPreference($pref_key, $add_re); } } if ($add_re) { $value = 'Re: '.$value; } } $mailer->setSubject($value); break; case 'is-html': if ($value) { $mailer->setIsHTML(true); } break; case 'is-bulk': if ($value) { if (PhabricatorEnv::getEnvConfig('metamta.precedence-bulk')) { $mailer->addHeader('Precedence', 'bulk'); } } break; case 'thread-id': if ($is_first && $mailer->supportsMessageIDHeader()) { $mailer->addHeader('Message-ID', $value); } else { $in_reply_to = $value; $references = array($value); $parent_id = $this->getParentMessageID(); if ($parent_id) { $in_reply_to = $parent_id; // By RFC 2822, the most immediate parent should appear last // in the "References" header, so this order is intentional. $references[] = $parent_id; } $references = implode(' ', $references); $mailer->addHeader('In-Reply-To', $in_reply_to); $mailer->addHeader('References', $references); } $thread_index = $this->generateThreadIndex($value, $is_first); $mailer->addHeader('Thread-Index', $thread_index); break; case 'mailtags': // Handled below. break; default: // Just discard. } } $mailer->addHeader('X-Phabricator-Sent-This-Message', 'Yes'); $mailer->addHeader('X-Mail-Transport-Agent', 'MetaMTA'); // If the message has mailtags, filter out any recipients who don't want // to receive this type of mail. $mailtags = $this->getParam('mailtags'); if ($mailtags && ($add_to || $add_cc)) { $tag_header = array(); foreach ($mailtags as $mailtag) { $tag_header[] = '<'.$mailtag.'>'; } $tag_header = implode(', ', $tag_header); $mailer->addHeader('X-Phabricator-Mail-Tags', $tag_header); $exclude = array(); $all_recipients = array_merge( array_keys($add_to), array_keys($add_cc)); $all_prefs = id(new PhabricatorUserPreferences())->loadAllWhere( 'userPHID in (%Ls)', $all_recipients); $all_prefs = mpull($all_prefs, null, 'getUserPHID'); foreach ($all_recipients as $recipient) { $prefs = idx($all_prefs, $recipient); if (!$prefs) { continue; } $user_mailtags = $prefs->getPreference( PhabricatorUserPreferences::PREFERENCE_MAILTAGS, array()); // The user must have elected to receive mail for at least one // of the mailtags. $send = false; foreach ($mailtags as $tag) { if (idx($user_mailtags, $tag, true)) { $send = true; break; } } if (!$send) { $exclude[$recipient] = true; } } $add_to = array_diff_key($add_to, $exclude); $add_cc = array_diff_key($add_cc, $exclude); } if ($add_to) { $mailer->addTos($add_to); if ($add_cc) { $mailer->addCCs($add_cc); } } else if ($add_cc) { // If we have CC addresses but no "to" address, promote the CCs to // "to". $mailer->addTos($add_cc); } else { $this->setStatus(self::STATUS_VOID); $this->setMessage( "Message has no valid recipients: all To/CC are disabled or ". "configured not to receive this mail."); return $this->save(); } } catch (Exception $ex) { $this->setStatus(self::STATUS_FAIL); $this->setMessage($ex->getMessage()); return $this->save(); } if ($this->getRetryCount() < $this->getSimulatedFailureCount()) { $ok = false; $error = 'Simulated failure.'; } else { try { $ok = $mailer->send(); $error = null; } catch (Exception $ex) { $ok = false; $error = $ex->getMessage()."\n".$ex->getTraceAsString(); } } if (!$ok) { $this->setMessage($error); if ($this->getRetryCount() > self::MAX_RETRIES) { $this->setStatus(self::STATUS_FAIL); } else { $this->setRetryCount($this->getRetryCount() + 1); $next_retry = time() + ($this->getRetryCount() * self::RETRY_DELAY); $this->setNextRetry($next_retry); } } else { $this->setStatus(self::STATUS_SENT); } return $this->save(); } public static function getReadableStatus($status_code) { static $readable = array( self::STATUS_QUEUE => "Queued for Delivery", self::STATUS_FAIL => "Delivery Failed", self::STATUS_SENT => "Sent", self::STATUS_VOID => "Void", ); $status_code = coalesce($status_code, '?'); return idx($readable, $status_code, $status_code); } private function generateThreadIndex($seed, $is_first_mail) { // When threading, Outlook ignores the 'References' and 'In-Reply-To' // headers that most clients use. Instead, it uses a custom 'Thread-Index' // header. The format of this header is something like this (from // camel-exchange-folder.c in Evolution Exchange): /* A new post to a folder gets a 27-byte-long thread index. (The value * is apparently unique but meaningless.) Each reply to a post gets a * 32-byte-long thread index whose first 27 bytes are the same as the * parent's thread index. Each reply to any of those gets a * 37-byte-long thread index, etc. The Thread-Index header contains a * base64 representation of this value. */ // The specific implementation uses a 27-byte header for the first email // a recipient receives, and a random 5-byte suffix (32 bytes total) // thereafter. This means that all the replies are (incorrectly) siblings, // but it would be very difficult to keep track of the entire tree and this // gets us reasonable client behavior. $base = substr(md5($seed), 0, 27); if (!$is_first_mail) { // Not totally sure, but it seems like outlook orders replies by // thread-index rather than timestamp, so to get these to show up in the // right order we use the time as the last 4 bytes. $base .= ' '.pack('N', time()); } return base64_encode($base); } private function getDeliverableEmailsFromHandles( array $phids, array $handles, array $exclude) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $emails = array(); foreach ($phids as $phid) { if ($handles[$phid]->isDisabled()) { continue; } if (!$handles[$phid]->isComplete()) { continue; } if (isset($exclude[$phid])) { continue; } $emails[$phid] = $handles[$phid]->getEmail(); } return $emails; } } diff --git a/src/applications/owners/controller/list/PhabricatorOwnersListController.php b/src/applications/owners/controller/list/PhabricatorOwnersListController.php index 5930a7b85c..06fea5393c 100644 --- a/src/applications/owners/controller/list/PhabricatorOwnersListController.php +++ b/src/applications/owners/controller/list/PhabricatorOwnersListController.php @@ -1,318 +1,319 @@ view = idx($data, 'view', 'owned'); $this->setSideNavFilter('view/'.$this->view); } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $package = new PhabricatorOwnersPackage(); $owner = new PhabricatorOwnersOwner(); $path = new PhabricatorOwnersPath(); $repository_phid = ''; if ($request->getStr('repository') != '') { $repository_phid = id(new PhabricatorRepository()) ->loadOneWhere('callsign = %s', $request->getStr('repository')) ->getPHID(); } switch ($this->view) { case 'search': $packages = array(); $conn_r = $package->establishConnection('r'); $where = array('1 = 1'); $join = array(); if ($request->getStr('name')) { $where[] = qsprintf( $conn_r, 'p.name LIKE %~', $request->getStr('name')); } if ($repository_phid || $request->getStr('path')) { $join[] = qsprintf( $conn_r, 'JOIN %T path ON path.packageID = p.id', $path->getTableName()); if ($repository_phid) { $where[] = qsprintf( $conn_r, 'path.repositoryPHID = %s', $repository_phid); } if ($request->getStr('path')) { $where[] = qsprintf( $conn_r, 'path.path LIKE %~ OR %s LIKE CONCAT(path.path, %s)', $request->getStr('path'), $request->getStr('path'), '%'); } } if ($request->getArr('owner')) { $join[] = qsprintf( $conn_r, 'JOIN %T o ON o.packageID = p.id', $owner->getTableName()); $where[] = qsprintf( $conn_r, 'o.userPHID IN (%Ls)', $request->getArr('owner')); } $data = queryfx_all( $conn_r, 'SELECT p.* FROM %T p %Q WHERE %Q GROUP BY p.id', $package->getTableName(), implode(' ', $join), '('.implode(') AND (', $where).')'); $packages = $package->loadAllFromArray($data); $header = 'Search Results'; $nodata = 'No packages match your query.'; break; case 'owned': $data = queryfx_all( $package->establishConnection('r'), 'SELECT p.* FROM %T p JOIN %T o ON p.id = o.packageID WHERE o.userPHID = %s GROUP BY p.id', $package->getTableName(), $owner->getTableName(), $user->getPHID()); $packages = $package->loadAllFromArray($data); $header = 'Owned Packages'; $nodata = 'No owned packages'; break; case 'all': $packages = $package->loadAll(); $header = 'All Packages'; $nodata = 'There are no defined packages.'; break; } $content = $this->renderPackageTable( $packages, $header, $nodata); $filter = new AphrontListFilterView(); $filter->addButton( phutil_render_tag( 'a', array( 'href' => '/owners/new/', 'class' => 'green button', ), 'Create New Package')); $owners_search_value = array(); if ($request->getArr('owner')) { $phids = $request->getArr('owner'); $phid = reset($phids); $handles = id(new PhabricatorObjectHandleData(array($phid))) ->loadHandles(); $owners_search_value = array( $phid => $handles[$phid]->getFullName(), ); } $callsigns = array('' => '(Any Repository)'); $repositories = id(new PhabricatorRepository()) ->loadAllWhere('1 = 1 ORDER BY callsign'); foreach ($repositories as $repository) { $callsigns[$repository->getCallsign()] = $repository->getCallsign().': '.$repository->getName(); } $form = id(new AphrontFormView()) ->setUser($user) ->setAction('/owners/view/search/') ->setMethod('GET') ->appendChild( id(new AphrontFormTextControl()) ->setName('name') ->setLabel('Name') ->setValue($request->getStr('name'))) ->appendChild( id(new AphrontFormTokenizerControl()) ->setDatasource('/typeahead/common/users/') ->setLimit(1) ->setName('owner') ->setLabel('Owner') ->setValue($owners_search_value)) ->appendChild( id(new AphrontFormSelectControl()) ->setName('repository') ->setLabel('Repository') ->setOptions($callsigns) ->setValue($request->getStr('repository'))) ->appendChild( id(new AphrontFormTextControl()) ->setName('path') ->setLabel('Path') ->setValue($request->getStr('path'))) ->appendChild( id(new AphrontFormSubmitControl()) ->setValue('Search for Packages')); $filter->appendChild($form); return $this->buildStandardPageResponse( array( $filter, $content, ), array( 'title' => 'Package Index', )); } private function renderPackageTable(array $packages, $header, $nodata) { + assert_instances_of($packages, 'PhabricatorOwnersPackage'); if ($packages) { $package_ids = mpull($packages, 'getID'); $owners = id(new PhabricatorOwnersOwner())->loadAllWhere( 'packageID IN (%Ld)', $package_ids); $paths = id(new PhabricatorOwnersPath())->loadAllWhere( 'packageID in (%Ld)', $package_ids); $phids = array(); foreach ($owners as $owner) { $phids[$owner->getUserPHID()] = true; } foreach ($paths as $path) { $phids[$path->getRepositoryPHID()] = true; } $phids = array_keys($phids); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $owners = mgroup($owners, 'getPackageID'); $paths = mgroup($paths, 'getPackageID'); } else { $handles = array(); $owners = array(); $paths = array(); } $rows = array(); foreach ($packages as $package) { $pkg_owners = idx($owners, $package->getID(), array()); foreach ($pkg_owners as $key => $owner) { $pkg_owners[$key] = $handles[$owner->getUserPHID()]->renderLink(); if ($owner->getUserPHID() == $package->getPrimaryOwnerPHID()) { $pkg_owners[$key] = ''.$pkg_owners[$key].''; } } $pkg_owners = implode('
', $pkg_owners); $pkg_paths = idx($paths, $package->getID(), array()); foreach ($pkg_paths as $key => $path) { $repo = $handles[$path->getRepositoryPHID()]->getName(); $pkg_paths[$key] = ''.phutil_escape_html($repo).' '. phutil_render_tag( 'a', array( 'href' => '/diffusion/'.$repo.'/browse/:'.$path->getPath(), ), phutil_escape_html($path->getPath())); } $pkg_paths = implode('
', $pkg_paths); $rows[] = array( phutil_render_tag( 'a', array( 'href' => '/owners/package/'.$package->getID().'/', ), phutil_escape_html($package->getName())), $pkg_owners, $pkg_paths, phutil_render_tag( 'a', array( 'href' => '/audit/view/packagecommits/?phid='.$package->getPHID(), ), phutil_escape_html('Related Commits')) ); } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Name', 'Owners', 'Paths', 'Related Commits', )); $table->setColumnClasses( array( 'pri', '', 'wide wrap', 'narrow', )); $panel = new AphrontPanelView(); $panel->setHeader($header); $panel->appendChild($table); return $panel; } protected function getExtraPackageViews() { switch ($this->view) { case 'search': $extra = array(array('name' => 'Search Results', 'key' => 'view/search')); break; default: $extra = array(); break; } return $extra; } } diff --git a/src/applications/owners/storage/owner/PhabricatorOwnersOwner.php b/src/applications/owners/storage/owner/PhabricatorOwnersOwner.php index 4d1cef4ecc..ab61a556ca 100644 --- a/src/applications/owners/storage/owner/PhabricatorOwnersOwner.php +++ b/src/applications/owners/storage/owner/PhabricatorOwnersOwner.php @@ -1,39 +1,40 @@ false, ) + parent::getConfiguration(); } public static function loadAllForPackages(array $packages) { + assert_instances_of($packages, 'PhabricatorOwnersPackage'); if (!$packages) { return array(); } return id(new PhabricatorOwnersOwner())->loadAllWhere( 'packageID IN (%Ls)', mpull($packages, 'getID')); } } diff --git a/src/applications/phriction/controller/diff/PhrictionDiffController.php b/src/applications/phriction/controller/diff/PhrictionDiffController.php index 9d31e53983..c5af468c5a 100644 --- a/src/applications/phriction/controller/diff/PhrictionDiffController.php +++ b/src/applications/phriction/controller/diff/PhrictionDiffController.php @@ -1,275 +1,276 @@ id = $data['id']; } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $document = id(new PhrictionDocument())->load($this->id); if (!$document) { return new Aphront404Response(); } $current = id(new PhrictionContent())->load($document->getContentID()); $l = $request->getInt('l'); $r = $request->getInt('r'); $ref = $request->getStr('ref'); if ($ref) { list($l, $r) = explode(',', $ref); } $content = id(new PhrictionContent())->loadAllWhere( 'documentID = %d AND version IN (%Ld)', $document->getID(), array($l, $r)); $content = mpull($content, null, 'getVersion'); $content_l = idx($content, $l, null); $content_r = idx($content, $r, null); if (!$content_l || !$content_r) { return new Aphront404Response(); } $text_l = $content_l->getContent(); $text_r = $content_r->getContent(); $text_l = wordwrap($text_l, 80); $text_r = wordwrap($text_r, 80); $engine = new PhabricatorDifferenceEngine(); $changeset = $engine->generateChangesetFromFileContent($text_l, $text_r); $changeset->setOldProperties( array( 'Title' => $content_l->getTitle(), )); $changeset->setNewProperties( array( 'Title' => $content_r->getTitle(), )); $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL; $parser = new DifferentialChangesetParser(); $parser->setChangeset($changeset); $parser->setRenderingReference("{$l},{$r}"); $parser->setWhitespaceMode($whitespace_mode); $spec = $request->getStr('range'); list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec); $output = $parser->render($range_s, $range_e, $mask); if ($request->isAjax()) { return id(new PhabricatorChangesetResponse()) ->setRenderedChangeset($output); } require_celerity_resource('differential-changeset-view-css'); require_celerity_resource('syntax-highlighting-css'); require_celerity_resource('phriction-document-css'); Javelin::initBehavior('differential-show-more', array( 'uri' => '/phriction/diff/'.$document->getID().'/', 'whitespace' => $whitespace_mode, )); $slug = $document->getSlug(); $revert_l = $this->renderRevertButton($content_l, $current); $revert_r = $this->renderRevertButton($content_r, $current); $crumbs = new AphrontCrumbsView(); $crumbs->setCrumbs( array( 'Phriction', phutil_render_tag( 'a', array( 'href' => PhrictionDocument::getSlugURI($slug), ), phutil_escape_html($current->getTitle())), phutil_render_tag( 'a', array( 'href' => '/phriction/history/'.$document->getSlug().'/', ), 'History'), phutil_escape_html("Changes Between Version {$l} and Version {$r}"), )); $comparison_table = $this->renderComparisonTable( array( $content_r, $content_l, )); $navigation_table = null; if ($l + 1 == $r) { $nav_l = ($l > 1); $nav_r = ($r != $current->getVersion()); $uri = $request->getRequestURI(); if ($nav_l) { $link_l = phutil_render_tag( 'a', array( 'href' => $uri->alter('l', $l - 1)->alter('r', $r - 1), ), "\xC2\xAB Previous Change"); } else { $link_l = 'Original Change'; } $link_r = null; if ($nav_r) { $link_r = phutil_render_tag( 'a', array( 'href' => $uri->alter('l', $l + 1)->alter('r', $r + 1), ), "Next Change \xC2\xBB"); } else { $link_r = 'Most Recent Change'; } $navigation_table = '
'; } $output = '
'. $comparison_table->render(). '
'. '
'. $navigation_table. ''. ''. '
'.$revert_l.''.$revert_r.'
'. $output. '
'; return $this->buildStandardPageResponse( array( $crumbs, $output, ), array( 'title' => 'Document History', )); } private function renderRevertButton( PhrictionContent $content, PhrictionContent $current) { $document_id = $content->getDocumentID(); $version = $content->getVersion(); if ($content->getChangeType() == PhrictionChangeType::CHANGE_DELETE) { // Don't show an edit/revert button for changes which deleted the content // since it's silly. return null; } if ($content->getID() == $current->getID()) { return phutil_render_tag( 'a', array( 'href' => '/phriction/edit/'.$document_id.'/', 'class' => 'button', ), 'Edit Current Version'); } return phutil_render_tag( 'a', array( 'href' => '/phriction/edit/'.$document_id.'/?revert='.$version, 'class' => 'button', ), 'Revert to Version '.phutil_escape_html($version).'...'); } private function renderComparisonTable(array $content) { + assert_instances_of($content, 'PhrictionContent'); $user = $this->getRequest()->getUser(); $phids = mpull($content, 'getAuthorPHID'); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $rows = array(); foreach ($content as $c) { $rows[] = array( phabricator_date($c->getDateCreated(), $user), phabricator_time($c->getDateCreated(), $user), phutil_escape_html('Version '.$c->getVersion()), $handles[$c->getAuthorPHID()]->renderLink(), phutil_escape_html($c->getDescription()), ); } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Date', 'Time', 'Version', 'Author', 'Description', )); $table->setColumnClasses( array( '', 'right', 'pri', '', 'wide', )); return $table; } } diff --git a/src/applications/project/controller/profile/PhabricatorProjectProfileController.php b/src/applications/project/controller/profile/PhabricatorProjectProfileController.php index c2ed64909e..07919ba71d 100644 --- a/src/applications/project/controller/profile/PhabricatorProjectProfileController.php +++ b/src/applications/project/controller/profile/PhabricatorProjectProfileController.php @@ -1,380 +1,381 @@ id = idx($data, 'id'); $this->page = idx($data, 'page'); } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $project = id(new PhabricatorProject())->load($this->id); if (!$project) { return new Aphront404Response(); } $profile = $project->loadProfile(); if (!$profile) { $profile = new PhabricatorProjectProfile(); } $src_phid = $profile->getProfileImagePHID(); if (!$src_phid) { $src_phid = $user->getProfileImagePHID(); } $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $src_phid); if ($file) { $picture = $file->getBestURI(); } else { $picture = null; } $members = mpull($project->loadAffiliations(), null, 'getUserPHID'); $nav_view = new AphrontSideNavFilterView(); $uri = new PhutilURI('/project/view/'.$project->getID().'/'); $nav_view->setBaseURI($uri); $external_arrow = "\xE2\x86\x97"; $tasks_uri = '/maniphest/view/all/?projects='.$project->getPHID(); $slug = PhrictionDocument::normalizeSlug($project->getName()); $phriction_uri = '/w/projects/'.$slug; $edit_uri = '/project/edit/'.$project->getID().'/'; $nav_view->addFilter('dashboard', 'Dashboard'); $nav_view->addSpacer(); $nav_view->addFilter('feed', 'Feed'); $nav_view->addFilter(null, 'Tasks '.$external_arrow, $tasks_uri); $nav_view->addFilter(null, 'Wiki '.$external_arrow, $phriction_uri); $nav_view->addFilter('people', 'People'); $nav_view->addFilter('about', 'About'); $nav_view->addSpacer(); $nav_view->addFilter(null, "Edit Project\xE2\x80\xA6", $edit_uri); $this->page = $nav_view->selectFilter($this->page, 'dashboard'); require_celerity_resource('phabricator-profile-css'); switch ($this->page) { case 'dashboard': $content = $this->renderTasksPage($project, $profile); $query = new PhabricatorFeedQuery(); $query->setFilterPHIDs( array( $project->getPHID(), )); $stories = $query->execute(); $content .= $this->renderStories($stories); break; case 'about': $content = $this->renderAboutPage($project, $profile); break; case 'people': $content = $this->renderPeoplePage($project, $profile); break; case 'feed': $content = $this->renderFeedPage($project, $profile); break; default: throw new Exception("Unimplemented filter '{$this->page}'."); } $content = '
'.$content.'
'; $nav_view->appendChild($content); $header = new PhabricatorProfileHeaderView(); $header->setName($project->getName()); $header->setDescription( phutil_utf8_shorten($profile->getBlurb(), 1024)); $header->setProfilePicture($picture); $action = null; if (empty($members[$user->getPHID()])) { $action = phabricator_render_form( $user, array( 'action' => '/project/update/'.$project->getID().'/join/', 'method' => 'post', ), phutil_render_tag( 'button', array( 'class' => 'green', ), 'Join Project')); } else { $action = javelin_render_tag( 'a', array( 'href' => '/project/update/'.$project->getID().'/leave/', 'sigil' => 'workflow', 'class' => 'grey button', ), 'Leave Project...'); } $header->addAction($action); $header->appendChild($nav_view); return $this->buildStandardPageResponse( $header, array( 'title' => $project->getName().' Project', )); } private function renderAboutPage( PhabricatorProject $project, PhabricatorProjectProfile $profile) { $viewer = $this->getRequest()->getUser(); $blurb = $profile->getBlurb(); $blurb = phutil_escape_html($blurb); $blurb = str_replace("\n", '
', $blurb); $phids = array_merge( array($project->getAuthorPHID()), $project->getSubprojectPHIDs() ); $phids = array_unique($phids); $handles = id(new PhabricatorObjectHandleData($phids)) ->loadHandles(); $timestamp = phabricator_datetime($project->getDateCreated(), $viewer); $about = '

About

Creator '.$handles[$project->getAuthorPHID()]->renderLink().'
Created '.$timestamp.'
PHID '.phutil_escape_html($project->getPHID()).'
Blurb '.$blurb.'
'; if ($project->getSubprojectPHIDs()) { $table = $this->renderSubprojectTable( $handles, $project->getSubprojectPHIDs()); $subproject_list = $table->render(); } else { $subproject_list = '

No subprojects.

'; } $about .= '
'. '

Subprojects

'. '
'. $subproject_list. '
'. '
'; return $about; } private function renderPeoplePage( PhabricatorProject $project, PhabricatorProjectProfile $profile) { $affiliations = $project->loadAffiliations(); $phids = mpull($affiliations, 'getUserPHID'); $handles = id(new PhabricatorObjectHandleData($phids)) ->loadHandles(); $affiliated = array(); foreach ($affiliations as $affiliation) { $user = $handles[$affiliation->getUserPHID()]->renderLink(); $role = phutil_escape_html($affiliation->getRole()); $affiliated[] = '
  • '.$user.' — '.$role.'
  • '; } if ($affiliated) { $affiliated = '
      '.implode("\n", $affiliated).'
    '; } else { $affiliated = '

    No one is affiliated with this project.

    '; } return '
    '. '

    People

    '. '
    '. $affiliated. '
    '. '
    '; } private function renderFeedPage( PhabricatorProject $project, PhabricatorProjectProfile $profile) { $query = new PhabricatorFeedQuery(); $query->setFilterPHIDs(array($project->getPHID())); $stories = $query->execute(); if (!$stories) { return 'There are no stories about this project.'; } $query = new PhabricatorFeedQuery(); $query->setFilterPHIDs( array( $project->getPHID(), )); $stories = $query->execute(); return $this->renderStories($stories); } private function renderStories(array $stories) { + assert_instances_of($stories, 'PhabricatorFeedStory'); $builder = new PhabricatorFeedBuilder($stories); $builder->setUser($this->getRequest()->getUser()); $view = $builder->buildView(); return '
    '. '

    Activity Feed

    '. '
    '. $view->render(). '
    '. '
    '; } private function renderTasksPage( PhabricatorProject $project, PhabricatorProjectProfile $profile) { $query = id(new ManiphestTaskQuery()) ->withProjects(array($project->getPHID())) ->withStatus(ManiphestTaskQuery::STATUS_OPEN) ->setOrderBy(ManiphestTaskQuery::ORDER_PRIORITY) ->setLimit(10) ->setCalculateRows(true); $tasks = $query->execute(); $count = $query->getRowCount(); $phids = mpull($tasks, 'getOwnerPHID'); $phids = array_filter($phids); $handles = id(new PhabricatorObjectHandleData($phids)) ->loadHandles(); $task_views = array(); foreach ($tasks as $task) { $view = id(new ManiphestTaskSummaryView()) ->setTask($task) ->setHandles($handles) ->setUser($this->getRequest()->getUser()); $task_views[] = $view->render(); } if (empty($tasks)) { $task_views = 'No open tasks.'; } else { $task_views = implode('', $task_views); } $open = number_format($count); $more_link = phutil_render_tag( 'a', array( 'href' => '/maniphest/view/all/?projects='.$project->getPHID(), ), "View All Open Tasks \xC2\xBB"); $content = '

    '. "Open Tasks ({$open})". '

    '. '
    '. $task_views. ''. '
    '; return $content; } private function renderSubprojectTable( PhabricatorObjectHandleData $handles, $subprojects_phids) { $rows = array(); foreach ($subprojects_phids as $subproject_phid) { $phid = $handles[$subproject_phid]->getPHID(); $rows[] = array( phutil_escape_html($handles[$phid]->getFullName()), phutil_render_tag( 'a', array( 'class' => 'small grey button', 'href' => $handles[$phid]->getURI(), ), 'View Project Profile'), ); } $table = new AphrontTableView($rows); $table->setHeaders( array( 'Name', '', )); $table->setColumnClasses( array( 'pri', 'action right', )); return $table; } } diff --git a/src/applications/project/editor/project/PhabricatorProjectEditor.php b/src/applications/project/editor/project/PhabricatorProjectEditor.php index 00ee8478e8..63190d6cee 100644 --- a/src/applications/project/editor/project/PhabricatorProjectEditor.php +++ b/src/applications/project/editor/project/PhabricatorProjectEditor.php @@ -1,237 +1,238 @@ project = $project; } public function setUser(PhabricatorUser $user) { $this->user = $user; return $this; } public function applyTransactions(array $transactions) { + assert_instances_of($transactions, 'PhabricatorProjectTransaction'); if (!$this->user) { throw new Exception('Call setUser() before save()!'); } $user = $this->user; $project = $this->project; $is_new = !$project->getID(); if ($is_new) { $project->setAuthorPHID($user->getPHID()); } foreach ($transactions as $key => $xaction) { $type = $xaction->getTransactionType(); $this->setTransactionOldValue($project, $xaction); if (!$this->transactionHasEffect($xaction)) { unset($transactions[$key]); continue; } $this->applyTransactionEffect($project, $xaction); } if (!$transactions) { return $this; } try { $project->save(); foreach ($transactions as $xaction) { $xaction->setAuthorPHID($user->getPHID()); $xaction->setProjectID($project->getID()); $xaction->save(); } foreach ($this->remAffiliations as $affil) { $affil->delete(); } foreach ($this->addAffiliations as $affil) { $affil->setProjectPHID($project->getPHID()); $affil->save(); } foreach ($transactions as $xaction) { $this->publishTransactionStory($project, $xaction); } } catch (AphrontQueryDuplicateKeyException $ex) { // We already validated the slug, but might race. Try again to see if // that's the issue. If it is, we'll throw a more specific exception. If // not, throw the original exception. $this->validateName($project); throw $ex; } // TODO: If we rename a project, we should move its Phriction page. Do // that once Phriction supports document moves. return $this; } private function validateName(PhabricatorProject $project) { $slug = $project->getPhrictionSlug(); $name = $project->getName(); if ($slug == '/') { throw new PhabricatorProjectNameCollisionException( "Project names must be unique and contain some letters or numbers."); } $id = $project->getID(); $collision = id(new PhabricatorProject())->loadOneWhere( '(name = %s OR phrictionSlug = %s) AND id %Q %nd', $name, $slug, $id ? '!=' : 'IS NOT', $id ? $id : null); if ($collision) { $other_name = $collision->getName(); $other_id = $collision->getID(); throw new PhabricatorProjectNameCollisionException( "Project names must be unique. The name '{$name}' is too similar to ". "the name of another project, '{$other_name}' (Project ID: ". "{$other_id}). Choose a unique name."); } } private function setTransactionOldValue( PhabricatorProject $project, PhabricatorProjectTransaction $xaction) { $type = $xaction->getTransactionType(); switch ($type) { case PhabricatorProjectTransactionType::TYPE_NAME: $xaction->setOldValue($project->getName()); break; case PhabricatorProjectTransactionType::TYPE_STATUS: $xaction->setOldValue($project->getStatus()); break; case PhabricatorProjectTransactionType::TYPE_MEMBERS: $affils = $project->loadAffiliations(); $project->attachAffiliations($affils); $old_value = mpull($affils, 'getUserPHID'); $old_value = array_values($old_value); $xaction->setOldValue($old_value); $new_value = $xaction->getNewValue(); $new_value = array_filter($new_value); $new_value = array_unique($new_value); $new_value = array_values($new_value); $xaction->setNewValue($new_value); break; default: throw new Exception("Unknown transaction type '{$type}'!"); } return $this; } private function applyTransactionEffect( PhabricatorProject $project, PhabricatorProjectTransaction $xaction) { $type = $xaction->getTransactionType(); switch ($type) { case PhabricatorProjectTransactionType::TYPE_NAME: $project->setName($xaction->getNewValue()); $project->setPhrictionSlug($xaction->getNewValue()); $this->validateName($project); break; case PhabricatorProjectTransactionType::TYPE_STATUS: $project->setStatus($xaction->getNewValue()); break; case PhabricatorProjectTransactionType::TYPE_MEMBERS: $old = array_fill_keys($xaction->getOldValue(), true); $new = array_fill_keys($xaction->getNewValue(), true); $add = array(); $rem = array(); foreach ($project->getAffiliations() as $affil) { if (empty($new[$affil->getUserPHID()])) { $rem[] = $affil; } } foreach ($new as $phid => $ignored) { if (empty($old[$phid])) { $affil = new PhabricatorProjectAffiliation(); $affil->setRole(''); $affil->setUserPHID($phid); $add[] = $affil; } } $this->addAffiliations = $add; $this->remAffiliations = $rem; break; default: throw new Exception("Unknown transaction type '{$type}'!"); } } private function publishTransactionStory( PhabricatorProject $project, PhabricatorProjectTransaction $xaction) { $related_phids = array( $project->getPHID(), $xaction->getAuthorPHID(), ); id(new PhabricatorFeedStoryPublisher()) ->setStoryType(PhabricatorFeedStoryTypeConstants::STORY_PROJECT) ->setStoryData( array( 'projectPHID' => $project->getPHID(), 'transactionID' => $xaction->getID(), 'type' => $xaction->getTransactionType(), 'old' => $xaction->getOldValue(), 'new' => $xaction->getNewValue(), )) ->setStoryTime(time()) ->setStoryAuthorPHID($xaction->getAuthorPHID()) ->setRelatedPHIDs($related_phids) ->publish(); } private function transactionHasEffect( PhabricatorProjectTransaction $xaction) { return ($xaction->getOldValue() !== $xaction->getNewValue()); } } diff --git a/src/applications/project/storage/project/PhabricatorProject.php b/src/applications/project/storage/project/PhabricatorProject.php index fd54c93edc..429558cafc 100644 --- a/src/applications/project/storage/project/PhabricatorProject.php +++ b/src/applications/project/storage/project/PhabricatorProject.php @@ -1,102 +1,103 @@ true, self::CONFIG_SERIALIZATION => array( 'subprojectPHIDs' => self::SERIALIZATION_JSON, ), ) + parent::getConfiguration(); } public function generatePHID() { return PhabricatorPHID::generateNewPHID( PhabricatorPHIDConstants::PHID_TYPE_PROJ); } public function setSubprojectPHIDs(array $phids) { $this->subprojectPHIDs = $phids; $this->subprojectsNeedUpdate = true; return $this; } public function loadProfile() { $profile = id(new PhabricatorProjectProfile())->loadOneWhere( 'projectPHID = %s', $this->getPHID()); return $profile; } public function getAffiliations() { if ($this->affiliations === null) { throw new Exception('Attach affiliations first!'); } return $this->affiliations; } public function attachAffiliations(array $affiliations) { + assert_instances_of($affiliations, 'PhabricatorProjectAffiliation'); $this->affiliations = $affiliations; return $this; } public function loadAffiliations() { $affils = PhabricatorProjectAffiliation::loadAllForProjectPHIDs( array($this->getPHID())); return $affils[$this->getPHID()]; } public function setPhrictionSlug($slug) { // NOTE: We're doing a little magic here and stripping out '/' so that // project pages always appear at top level under projects/ even if the // display name is "Hack / Slash" or similar (it will become // 'hack_slash' instead of 'hack/slash'). $slug = str_replace('/', ' ', $slug); $slug = PhrictionDocument::normalizeSlug($slug); $this->phrictionSlug = $slug; return $this; } public function save() { $result = parent::save(); if ($this->subprojectsNeedUpdate) { // If we've changed the project PHIDs for this task, update the link // table. PhabricatorProjectSubproject::updateProjectSubproject($this); $this->subprojectsNeedUpdate = false; } return $result; } } diff --git a/src/applications/repository/storage/commit/PhabricatorRepositoryCommit.php b/src/applications/repository/storage/commit/PhabricatorRepositoryCommit.php index 91d08983d2..243daa8c9c 100644 --- a/src/applications/repository/storage/commit/PhabricatorRepositoryCommit.php +++ b/src/applications/repository/storage/commit/PhabricatorRepositoryCommit.php @@ -1,126 +1,128 @@ true, self::CONFIG_TIMESTAMPS => false, ) + parent::getConfiguration(); } public function generatePHID() { return PhabricatorPHID::generateNewPHID( PhabricatorPHIDConstants::PHID_TYPE_CMIT); } public function loadCommitData() { if (!$this->getID()) { return null; } return id(new PhabricatorRepositoryCommitData())->loadOneWhere( 'commitID = %d', $this->getID()); } public function attachCommitData(PhabricatorRepositoryCommitData $data) { $this->commitData = $data; return $this; } public function getCommitData() { if (!$this->commitData) { throw new Exception("Attach commit data with attachCommitData() first!"); } return $this->commitData; } public function save() { if (!$this->mailKey) { $this->mailKey = Filesystem::readRandomCharacters(20); } return parent::save(); } public function delete() { $data = $this->loadCommitData(); $this->openTransaction(); if ($data) { $data->delete(); } $result = parent::delete(); $this->saveTransaction(); return $result; } /** * Synchronize a commit's overall audit status with the individual audit * triggers. */ public function updateAuditStatus(array $requests) { + assert_instances_of($requests, 'PhabricatorRepositoryAuditRequest'); + $any_concern = false; $any_accept = false; $any_need = false; foreach ($requests as $request) { switch ($request->getAuditStatus()) { case PhabricatorAuditStatusConstants::AUDIT_REQUIRED: $any_need = true; break; case PhabricatorAuditStatusConstants::ACCEPTED: $any_accept = true; break; case PhabricatorAuditStatusConstants::CONCERNED: $any_concern = true; break; } } if ($any_concern) { $status = PhabricatorAuditCommitStatusConstants::CONCERN_RAISED; } else if ($any_accept) { if ($any_need) { $status = PhabricatorAuditCommitStatusConstants::PARTIALLY_AUDITED; } else { $status = PhabricatorAuditCommitStatusConstants::FULLY_AUDITED; } } else if ($any_need) { $status = PhabricatorAuditCommitStatusConstants::NEEDS_AUDIT; } else { $status = PhabricatorAuditCommitStatusConstants::NONE; } return $this->setAuditStatus($status); } } diff --git a/src/applications/repository/worker/commitmessageparser/base/PhabricatorRepositoryCommitMessageParserWorker.php b/src/applications/repository/worker/commitmessageparser/base/PhabricatorRepositoryCommitMessageParserWorker.php index ec1b7fa2ce..cc1dbda6bc 100644 --- a/src/applications/repository/worker/commitmessageparser/base/PhabricatorRepositoryCommitMessageParserWorker.php +++ b/src/applications/repository/worker/commitmessageparser/base/PhabricatorRepositoryCommitMessageParserWorker.php @@ -1,171 +1,173 @@ commit; $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere( 'commitID = %d', $commit->getID()); if (!$data) { $data = new PhabricatorRepositoryCommitData(); } $data->setCommitID($commit->getID()); $data->setAuthorName($author); $data->setCommitMessage($message); $repository = $this->repository; $detail_parser = $repository->getDetail( 'detail-parser', 'PhabricatorRepositoryDefaultCommitMessageDetailParser'); if ($detail_parser) { PhutilSymbolLoader::loadClass($detail_parser); $parser_obj = newv($detail_parser, array($commit, $data)); $parser_obj->parseCommitDetails(); } $author_phid = $data->getCommitDetail('authorPHID'); if ($author_phid) { $commit->setAuthorPHID($author_phid); $commit->save(); } $data->save(); $conn_w = id(new DifferentialRevision())->establishConnection('w'); // NOTE: The `differential_commit` table has a unique ID on `commitPHID`, // preventing more than one revision from being associated with a commit. // Generally this is good and desirable, but with the advent of hash // tracking we may end up in a situation where we match several different // revisions. We just kind of ignore this and pick one, we might want to // revisit this and do something differently. (If we match several revisions // someone probably did something very silly, though.) $revision_id = $data->getCommitDetail('differential.revisionID'); if (!$revision_id) { $hashes = $this->getCommitHashes( $this->repository, $this->commit); if ($hashes) { $query = new DifferentialRevisionQuery(); $query->withCommitHashes($hashes); $revisions = $query->execute(); if (!empty($revisions)) { $revision = $this->identifyBestRevision($revisions); $revision_id = $revision->getID(); } } } if ($revision_id) { $revision = id(new DifferentialRevision())->load($revision_id); if ($revision) { queryfx( $conn_w, 'INSERT IGNORE INTO %T (revisionID, commitPHID) VALUES (%d, %s)', DifferentialRevision::TABLE_COMMIT, $revision->getID(), $commit->getPHID()); if ($revision->getStatus() != ArcanistDifferentialRevisionStatus::COMMITTED) { $message = null; $committer = $data->getCommitDetail('authorPHID'); if (!$committer) { $committer = $revision->getAuthorPHID(); $message = 'Change committed by '.$data->getAuthorName().'.'; } $editor = new DifferentialCommentEditor( $revision, $committer, DifferentialAction::ACTION_COMMIT); $editor->setIsDaemonWorkflow(true); $editor->setMessage($message)->save(); } } } } /** * When querying for revisions by hash, more than one revision may be found. * This function identifies the "best" revision from such a set. Typically, * there is only one revision found. Otherwise, we try to pick an accepted * revision first, followed by an open revision, and otherwise we go with a * committed or abandoned revision as a last resort. */ private function identifyBestRevision(array $revisions) { + assert_instances_of($revisions, 'DifferentialRevision'); // get the simplest, common case out of the way if (count($revisions) == 1) { return reset($revisions); } $first_choice = array(); $second_choice = array(); $third_choice = array(); foreach ($revisions as $revision) { switch ($revision->getStatus()) { // "Accepted" revisions -- ostensibly what we're looking for! case ArcanistDifferentialRevisionStatus::ACCEPTED: $first_choice[] = $revision; break; // "Open" revisions case ArcanistDifferentialRevisionStatus::NEEDS_REVIEW: case ArcanistDifferentialRevisionStatus::NEEDS_REVISION: $second_choice[] = $revision; break; // default is a wtf? here default: case ArcanistDifferentialRevisionStatus::ABANDONED: case ArcanistDifferentialRevisionStatus::COMMITTED: $third_choice[] = $revision; break; } } // go down the ladder like a bro at last call if (!empty($first_choice)) { return $this->identifyMostRecentRevision($first_choice); } if (!empty($second_choice)) { return $this->identifyMostRecentRevision($second_choice); } if (!empty($third_choice)) { return $this->identifyMostRecentRevision($third_choice); } } /** * Given a set of revisions, returns the revision with the latest * updated time. This is ostensibly the most recent revision. */ private function identifyMostRecentRevision(array $revisions) { + assert_instances_of($revisions, 'DifferentialRevision'); $revisions = msort($revisions, 'getDateModified'); return end($revisions); } } diff --git a/src/applications/repository/worker/herald/PhabricatorRepositoryCommitHeraldWorker.php b/src/applications/repository/worker/herald/PhabricatorRepositoryCommitHeraldWorker.php index ebf2bf86b0..ad35e89df2 100644 --- a/src/applications/repository/worker/herald/PhabricatorRepositoryCommitHeraldWorker.php +++ b/src/applications/repository/worker/herald/PhabricatorRepositoryCommitHeraldWorker.php @@ -1,272 +1,273 @@ loadOneWhere( 'commitID = %d', $commit->getID()); if (!$data) { // TODO: Permanent failure. return; } $rules = HeraldRule::loadAllByContentTypeWithFullData( HeraldContentTypeConfig::CONTENT_TYPE_COMMIT, $commit->getPHID()); $adapter = new HeraldCommitAdapter( $repository, $commit, $data); $engine = new HeraldEngine(); $effects = $engine->applyRules($rules, $adapter); $engine->applyEffects($effects, $adapter, $rules); $audit_phids = $adapter->getAuditMap(); if ($audit_phids) { $this->createAudits($commit, $audit_phids, $rules); } $this->createAuditsFromCommitMessage($commit, $data); $email_phids = $adapter->getEmailPHIDs(); if (!$email_phids) { return; } if ($repository->getDetail('herald-disabled')) { // This just means "disable email"; audits are (mostly) idempotent. return; } $xscript = $engine->getTranscript(); $revision = $adapter->loadDifferentialRevision(); if ($revision) { $name = $revision->getTitle(); } else { $name = $data->getSummary(); } $author_phid = $data->getCommitDetail('authorPHID'); $reviewer_phid = $data->getCommitDetail('reviewerPHID'); $phids = array_filter( array( $author_phid, $reviewer_phid, $commit->getPHID(), )); $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $commit_handle = $handles[$commit->getPHID()]; $commit_name = $commit_handle->getName(); if ($author_phid) { $author_name = $handles[$author_phid]->getName(); } else { $author_name = $data->getAuthorName(); } if ($reviewer_phid) { $reviewer_name = $handles[$reviewer_phid]->getName(); } else { $reviewer_name = null; } $who = implode(', ', array_filter(array($author_name, $reviewer_name))); $description = $data->getCommitMessage(); $commit_uri = PhabricatorEnv::getProductionURI($commit_handle->getURI()); $differential = $revision ? PhabricatorEnv::getProductionURI('/D'.$revision->getID()) : 'No revision.'; $files = $adapter->loadAffectedPaths(); sort($files); $files = implode("\n ", $files); $xscript_id = $xscript->getID(); $manage_uri = PhabricatorEnv::getProductionURI('/herald/view/commits/'); $why_uri = PhabricatorEnv::getProductionURI( '/herald/transcript/'.$xscript_id.'/'); $reply_handler = PhabricatorAuditCommentEditor::newReplyHandlerForCommit( $commit); $reply_instructions = $reply_handler->getReplyHandlerInstructions(); if ($reply_instructions) { $reply_instructions = "\n". "REPLY HANDLER ACTIONS\n". " ".$reply_instructions."\n"; } $body = <<getPHID()); list($thread_id, $thread_topic) = $threading; $template = new PhabricatorMetaMTAMail(); $template->setRelatedPHID($commit->getPHID()); $template->setSubject($subject); $template->setBody($body); $template->setThreadID($thread_id, $is_new = true); $template->addHeader('Thread-Topic', $thread_topic); $template->setIsBulk(true); $template->addHeader('X-Herald-Rules', $xscript->getXHeraldRulesHeader()); if ($author_phid) { $template->setFrom($author_phid); } $mails = $reply_handler->multiplexMail( $template, id(new PhabricatorObjectHandleData($email_phids))->loadHandles(), array()); foreach ($mails as $mail) { $mail->saveAndSend(); } } private function createAudits( PhabricatorRepositoryCommit $commit, array $map, array $rules) { + assert_instances_of($rules, 'HeraldRule'); $requests = id(new PhabricatorRepositoryAuditRequest())->loadAllWhere( 'commitPHID = %s', $commit->getPHID()); $requests = mpull($requests, null, 'getAuditorPHID'); $rules = mpull($rules, null, 'getID'); foreach ($map as $phid => $rule_ids) { $request = idx($requests, $phid); if ($request) { continue; } $reasons = array(); foreach ($rule_ids as $id) { $rule_name = '?'; if ($rules[$id]) { $rule_name = $rules[$id]->getName(); } $reasons[] = 'Herald Rule #'.$id.' "'.$rule_name.'" Triggered Audit'; } $request = new PhabricatorRepositoryAuditRequest(); $request->setCommitPHID($commit->getPHID()); $request->setAuditorPHID($phid); $request->setAuditStatus(PhabricatorAuditStatusConstants::AUDIT_REQUIRED); $request->setAuditReasons($reasons); $request->save(); } $commit->updateAuditStatus($requests); $commit->save(); } /** * Find audit requests in the "Auditors" field if it is present and trigger * explicit audit requests. */ private function createAuditsFromCommitMessage( PhabricatorRepositoryCommit $commit, PhabricatorRepositoryCommitData $data) { $message = $data->getCommitMessage(); $matches = null; if (!preg_match('/^Auditors:\s*(.*)$/im', $message, $matches)) { return; } $phids = DifferentialFieldSpecification::parseCommitMessageObjectList( $matches[1], $include_mailables = false, $allow_partial = true); if (!$phids) { return; } $requests = id(new PhabricatorRepositoryAuditRequest())->loadAllWhere( 'commitPHID = %s', $commit->getPHID()); $requests = mpull($requests, null, 'getAuditorPHID'); foreach ($phids as $phid) { if (isset($requests[$phid])) { continue; } $request = new PhabricatorRepositoryAuditRequest(); $request->setCommitPHID($commit->getPHID()); $request->setAuditorPHID($phid); $request->setAuditStatus( PhabricatorAuditStatusConstants::AUDIT_REQUESTED); $request->setAuditReasons( array( 'Requested by Author', )); $request->save(); $requests[$phid] = $request; } $commit->updateAuditStatus($requests); $commit->save(); } } diff --git a/src/applications/slowvote/controller/poll/PhabricatorSlowvotePollController.php b/src/applications/slowvote/controller/poll/PhabricatorSlowvotePollController.php index f046b4cb24..12ca241ae9 100644 --- a/src/applications/slowvote/controller/poll/PhabricatorSlowvotePollController.php +++ b/src/applications/slowvote/controller/poll/PhabricatorSlowvotePollController.php @@ -1,460 +1,472 @@ id = $data['id']; } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $viewer_phid = $user->getPHID(); $poll = id(new PhabricatorSlowvotePoll())->load($this->id); if (!$poll) { return new Aphront404Response(); } $options = id(new PhabricatorSlowvoteOption())->loadAllWhere( 'pollID = %d', $poll->getID()); $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere( 'pollID = %d', $poll->getID()); $comments = id(new PhabricatorSlowvoteComment())->loadAllWhere( 'pollID = %d', $poll->getID()); $choices_by_option = mgroup($choices, 'getOptionID'); $comments_by_user = mpull($comments, null, 'getAuthorPHID'); $choices_by_user = mgroup($choices, 'getAuthorPHID'); $viewer_choices = idx($choices_by_user, $viewer_phid, array()); $viewer_comment = idx($comments_by_user, $viewer_phid, null); $comment_text = null; if ($viewer_comment) { $comment_text = $viewer_comment->getCommentText(); } if ($request->isFormPost()) { $comment = idx($comments_by_user, $viewer_phid, null); if ($comment) { $comment->delete(); } $comment_text = $request->getStr('comments'); if (strlen($comment_text)) { id(new PhabricatorSlowvoteComment()) ->setAuthorPHID($viewer_phid) ->setPollID($poll->getID()) ->setCommentText($comment_text) ->save(); } $votes = $request->getArr('vote'); switch ($poll->getMethod()) { case PhabricatorSlowvotePoll::METHOD_PLURALITY: // Enforce only one vote. $votes = array_slice($votes, 0, 1); break; case PhabricatorSlowvotePoll::METHOD_APPROVAL: // No filtering. break; default: throw new Exception("Unknown poll method!"); } foreach ($viewer_choices as $viewer_choice) { $viewer_choice->delete(); } foreach ($votes as $vote) { id(new PhabricatorSlowvoteChoice()) ->setAuthorPHID($viewer_phid) ->setPollID($poll->getID()) ->setOptionID($vote) ->save(); } return id(new AphrontRedirectResponse())->setURI('/V'.$poll->getID()); } require_celerity_resource('phabricator-slowvote-css'); $phids = array_merge( mpull($choices, 'getAuthorPHID'), mpull($comments, 'getAuthorPHID'), array( $poll->getAuthorPHID(), )); $query = new PhabricatorObjectHandleData($phids); $handles = $query->loadHandles(); $objects = $query->loadObjects(); if ($poll->getShuffle()) { shuffle($options); } $option_markup = array(); foreach ($options as $option) { $option_markup[] = $this->renderPollOption( $poll, $viewer_choices, $option); } $option_markup = implode("\n", $option_markup); $comments_by_option = array(); switch ($poll->getMethod()) { case PhabricatorSlowvotePoll::METHOD_PLURALITY: $choice_ids = array(); foreach ($choices_by_user as $user_phid => $user_choices) { $choice_ids[$user_phid] = head($user_choices)->getOptionID(); } foreach ($comments as $comment) { $choice = idx($choice_ids, $comment->getAuthorPHID()); if ($choice) { $comments_by_option[$choice][] = $comment; } } break; case PhabricatorSlowvotePoll::METHOD_APPROVAL: // All comments are grouped in approval voting. break; default: throw new Exception("Unknown poll method!"); } $result_markup = $this->renderResultMarkup( $poll, $options, $choices, $comments, $viewer_choices, $choices_by_option, $comments_by_option, $handles, $objects); if ($viewer_choices) { $instructions = 'Your vote has been recorded... but there is still ample time to '. 'rethink your position. Have you thoroughly considered all possible '. 'eventualities?'; } else { $instructions = 'This is a weighty matter indeed. Consider your choices with the '. 'greatest of care.'; } $form = id(new AphrontFormView()) ->setUser($user) ->appendChild( '

    '.$instructions.'

    ') ->appendChild( id(new AphrontFormMarkupControl()) ->setLabel('Vote') ->setValue($option_markup)) ->appendChild( id(new AphrontFormTextAreaControl()) ->setLabel('Comments') ->setHeight(AphrontFormTextAreaControl::HEIGHT_SHORT) ->setName('comments') ->setValue($comment_text)) ->appendChild( id(new AphrontFormSubmitControl()) ->setValue('Cautiously Engage in Deliberations')); $panel = new AphrontPanelView(); $panel->setHeader(phutil_escape_html($poll->getQuestion())); $panel->setWidth(AphrontPanelView::WIDTH_WIDE); $panel->appendChild($form); $panel->appendChild('

    '); $panel->appendChild($result_markup); return $this->buildStandardPageResponse( $panel, array( 'title' => 'V'.$poll->getID().' '.$poll->getQuestion(), )); } private function renderComments(array $comments, array $handles) { + assert_instances_of($comments, 'PhabricatorSlowvoteComment'); + assert_instances_of($handles, 'PhabricatorObjectHandle'); + $viewer = $this->getRequest()->getUser(); $engine = PhabricatorMarkupEngine::newSlowvoteMarkupEngine(); $comment_markup = array(); foreach ($comments as $comment) { $handle = $handles[$comment->getAuthorPHID()]; $markup = $engine->markupText($comment->getCommentText()); require_celerity_resource('phabricator-remarkup-css'); $comment_markup[] = ''. ''. $handle->renderLink(). '
    '. phabricator_datetime($comment->getDateCreated(), $viewer). '
    '. ''. '
    '. $markup. '
    '. ''. ''; } if ($comment_markup) { $comment_markup = phutil_render_tag( 'table', array( 'class' => 'phabricator-slowvote-comments', ), implode("\n", $comment_markup)); } else { $comment_markup = null; } return $comment_markup; } private function renderPollOption( PhabricatorSlowvotePoll $poll, array $viewer_choices, PhabricatorSlowvoteOption $option) { + assert_instances_of($viewer_choices, 'PhabricatorSlowvoteChoice'); $id = $option->getID(); switch ($poll->getMethod()) { case PhabricatorSlowvotePoll::METHOD_PLURALITY: // Render a radio button. $selected_option = head($viewer_choices); if ($selected_option) { $selected = $selected_option->getOptionID(); } else { $selected = null; } if ($selected == $id) { $checked = "checked"; } else { $checked = null; } $input = phutil_render_tag( 'input', array( 'type' => 'radio', 'name' => 'vote[]', 'value' => $id, 'checked' => $checked, )); break; case PhabricatorSlowvotePoll::METHOD_APPROVAL: // Render a check box. $checked = null; foreach ($viewer_choices as $choice) { if ($choice->getOptionID() == $id) { $checked = 'checked'; break; } } $input = phutil_render_tag( 'input', array( 'type' => 'checkbox', 'name' => 'vote[]', 'checked' => $checked, 'value' => $id, )); break; default: throw new Exception("Unknown poll method!"); } if ($checked) { $checked_class = 'phabricator-slowvote-checked'; } else { $checked_class = null; } return phutil_render_tag( 'label', array( 'class' => 'phabricator-slowvote-label '.$checked_class, ), $input.phutil_escape_html($option->getName())); } private function renderVoteCount( PhabricatorSlowvotePoll $poll, array $choices, array $chosen) { + assert_instances_of($choices, 'PhabricatorSlowvoteChoice'); + assert_instances_of($chosen, 'PhabricatorSlowvoteChoice'); switch ($poll->getMethod()) { case PhabricatorSlowvotePoll::METHOD_PLURALITY: $out_of_total = count($choices); break; case PhabricatorSlowvotePoll::METHOD_APPROVAL: // Count unique respondents for approval votes. $out_of_total = count(mpull($choices, null, 'getAuthorPHID')); break; default: throw new Exception("Unknown poll method!"); } return sprintf( '%d / %d (%d%%)', number_format(count($chosen)), number_format($out_of_total), $out_of_total ? round(100 * count($chosen) / $out_of_total) : 0); } private function renderResultMarkup( PhabricatorSlowvotePoll $poll, array $options, array $choices, array $comments, array $viewer_choices, array $choices_by_option, array $comments_by_option, array $handles, array $objects) { + assert_instances_of($options, 'PhabricatorSlowvoteOption'); + assert_instances_of($choices, 'PhabricatorSlowvoteChoice'); + assert_instances_of($comments, 'PhabricatorSlowvoteComment'); + assert_instances_of($viewer_choices, 'PhabricatorSlowvoteChoice'); + assert_instances_of($handles, 'PhabricatorObjectHandle'); + assert_instances_of($objects, 'PhabricatorLiskDAO'); $viewer_phid = $this->getRequest()->getUser()->getPHID(); $can_see_responses = false; $need_vote = false; switch ($poll->getResponseVisibility()) { case PhabricatorSlowvotePoll::RESPONSES_VISIBLE: $can_see_responses = true; break; case PhabricatorSlowvotePoll::RESPONSES_VOTERS: $can_see_responses = (bool)$viewer_choices; $need_vote = true; break; case PhabricatorSlowvotePoll::RESPONSES_OWNER: $can_see_responses = ($viewer_phid == $poll->getAuthorPHID()); break; } $result_markup = id(new AphrontFormLayoutView()) ->appendChild('

    Ongoing Deliberation

    '); if (!$can_see_responses) { if ($need_vote) { $reason = "You must vote to see the results."; } else { $reason = "The results are not public."; } $result_markup ->appendChild( '

    '.$reason.'

    '); return $result_markup; } foreach ($options as $option) { $id = $option->getID(); $chosen = idx($choices_by_option, $id, array()); $users = array_select_keys($handles, mpull($chosen, 'getAuthorPHID')); if ($users) { $user_markup = array(); foreach ($users as $handle) { $object = idx($objects, $handle->getPHID()); if (!$object) { continue; } $profile_image = $handle->getImageURI(); $user_markup[] = phutil_render_tag( 'a', array( 'href' => $handle->getURI(), 'class' => 'phabricator-slowvote-facepile', ), phutil_render_tag( 'img', array( 'src' => $profile_image, ))); } $user_markup = implode('', $user_markup); } else { $user_markup = 'This option has failed to appeal to anyone.'; } $comment_markup = $this->renderComments( idx($comments_by_option, $id, array()), $handles); $vote_count = $this->renderVoteCount( $poll, $choices, $chosen); $result_markup->appendChild( '
    '. '
    '. $vote_count. '
    '. '

    '.phutil_escape_html($option->getName()).'

    '. '
    '. $user_markup. '
    '. '
    '. $comment_markup. '
    '); } if ($poll->getMethod() == PhabricatorSlowvotePoll::METHOD_APPROVAL && $comments) { $comment_markup = $this->renderComments( $comments, $handles); $result_markup->appendChild( '

    Motions Proposed for Consideration

    '); $result_markup->appendChild($comment_markup); } return $result_markup; } } diff --git a/src/view/control/objectselector/PhabricatorObjectSelectorDialog.php b/src/view/control/objectselector/PhabricatorObjectSelectorDialog.php index ffc29369be..7f44dab14a 100644 --- a/src/view/control/objectselector/PhabricatorObjectSelectorDialog.php +++ b/src/view/control/objectselector/PhabricatorObjectSelectorDialog.php @@ -1,199 +1,200 @@ user = $user; return $this; } public function setFilters(array $filters) { $this->filters = $filters; return $this; } public function setSelectedFilter($selected_filter) { $this->selectedFilter = $selected_filter; return $this; } public function setHandles(array $handles) { + assert_instances_of($handles, 'PhabricatorObjectHandle'); $this->handles = $handles; return $this; } public function setCancelURI($cancel_uri) { $this->cancelURI = $cancel_uri; return $this; } public function setSubmitURI($submit_uri) { $this->submitURI = $submit_uri; return $this; } public function setSearchURI($search_uri) { $this->searchURI = $search_uri; return $this; } public function setTitle($title) { $this->title = $title; return $this; } public function setHeader($header) { $this->header = $header; return $this; } public function setButtonText($button_text) { $this->buttonText = $button_text; return $this; } public function setInstructions($instructions) { $this->instructions = $instructions; return $this; } public function buildDialog() { $user = $this->user; $filter_id = celerity_generate_unique_node_id(); $query_id = celerity_generate_unique_node_id(); $results_id = celerity_generate_unique_node_id(); $current_id = celerity_generate_unique_node_id(); $search_id = celerity_generate_unique_node_id(); $form_id = celerity_generate_unique_node_id(); require_celerity_resource('phabricator-object-selector-css'); $options = array(); foreach ($this->filters as $key => $label) { $options[] = phutil_render_tag( 'option', array( 'value' => $key, 'selected' => ($key == $this->selectedFilter) ? 'selected' : null, ), $label); } $options = implode("\n", $options); $instructions = null; if ($this->instructions) { $instructions = '

    '. $this->instructions. '

    '; } $search_box = phabricator_render_form( $user, array( 'method' => 'POST', 'action' => $this->submitURI, 'id' => $search_id, ), ''); $result_box = '
    '. '
    '; $attached_box = '
    '. '
    '. '
    '. phutil_escape_html($this->header). '
    '. '
    '. '
    '. $instructions. '
    '. '
    '; $dialog = new AphrontDialogView(); $dialog ->setUser($this->user) ->setTitle($this->title) ->setClass('phabricator-object-selector-dialog') ->appendChild($search_box) ->appendChild($result_box) ->appendChild($attached_box) ->setRenderDialogAsDiv() ->setFormID($form_id) ->addSubmitButton($this->buttonText); if ($this->cancelURI) { $dialog->addCancelButton($this->cancelURI); } $handle_views = array(); foreach ($this->handles as $handle) { $phid = $handle->getPHID(); $view = new PhabricatorHandleObjectSelectorDataView($handle); $handle_views[$phid] = $view->renderData(); } $dialog->addHiddenInput('phids', implode(';', array_keys($this->handles))); Javelin::initBehavior( 'phabricator-object-selector', array( 'filter' => $filter_id, 'query' => $query_id, 'search' => $search_id, 'results' => $results_id, 'current' => $current_id, 'form' => $form_id, 'uri' => $this->searchURI, 'handles' => $handle_views, )); return $dialog; } } diff --git a/src/view/control/objectselector/__init__.php b/src/view/control/objectselector/__init__.php index 319c6a84a3..931cb614b9 100644 --- a/src/view/control/objectselector/__init__.php +++ b/src/view/control/objectselector/__init__.php @@ -1,18 +1,19 @@