Migrate Slowvote comments to ApplicationTransactions

Summary:
Move comments from the old table to ApplicationTransactions. Patch dances around which objects it uses since I intend to delete the comment table.

NOTE: This temporarily disables comment writes. I'll restore them shortly.

Test Plan: {F50166}

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D6454
This commit is contained in:
epriestley
2013-07-15 04:20:10 -07:00
parent ef1bedef02
commit ea52bcbcd6
9 changed files with 164 additions and 128 deletions

View File

@@ -0,0 +1,101 @@
<?php
echo "Moving Slowvote comments to transactions...\n";
$viewer = PhabricatorUser::getOmnipotentUser();
$table_xaction = new PhabricatorSlowvoteTransaction();
$table_comment = new PhabricatorSlowvoteTransactionComment();
$conn_w = $table_xaction->establishConnection('w');
$comments = new LiskRawMigrationIterator($conn_w, 'slowvote_comment');
$conn_w->openTransaction();
foreach ($comments as $comment) {
$id = $comment['id'];
$poll_id = $comment['pollID'];
$author_phid = $comment['authorPHID'];
$text = $comment['commentText'];
$date_created = $comment['dateCreated'];
$date_modified = $comment['dateModified'];
echo "Migrating comment {$id}.\n";
$poll = id(new PhabricatorSlowvoteQuery())
->setViewer($viewer)
->withIDs(array($poll_id))
->executeOne();
if (!$poll) {
echo "No poll.\n";
continue;
}
$user = id(new PhabricatorPeopleQuery())
->setViewer($viewer)
->withPHIDs(array($author_phid))
->executeOne();
if (!$user) {
echo "No user.\n";
continue;
}
$comment_phid = PhabricatorPHID::generateNewPHID(
PhabricatorPHIDConstants::PHID_TYPE_XCMT);
$xaction_phid = PhabricatorPHID::generateNewPHID(
PhabricatorPHIDConstants::PHID_TYPE_XACT,
PhabricatorPHIDConstants::PHID_TYPE_POLL);
$source = PhabricatorContentSource::newForSource(
PhabricatorContentSource::SOURCE_LEGACY,
array())->serialize();
queryfx(
$conn_w,
'INSERT INTO %T (phid, transactionPHID, authorPHID, viewPolicy, editPolicy,
commentVersion, content, contentSource, isDeleted,
dateCreated, dateModified)
VALUES (%s, %s, %s, %s, %s,
%d, %s, %s, %d,
%d, %d)',
$table_comment->getTableName(),
$comment_phid,
$xaction_phid,
$user->getPHID(),
PhabricatorPolicies::POLICY_PUBLIC,
$user->getPHID(),
1,
$text,
$source,
0,
$date_created,
$date_modified);
queryfx(
$conn_w,
'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy,
commentPHID, commentVersion, transactionType, oldValue, newValue,
contentSource, metadata, dateCreated, dateModified)
VALUES (%s, %s, %s, %s, %s,
%s, %d, %s, %s, %s,
%s, %s, %d, %d)',
$table_xaction->getTableName(),
$xaction_phid,
$user->getPHID(),
$poll->getPHID(),
PhabricatorPolicies::POLICY_PUBLIC,
$user->getPHID(),
$comment_phid,
1,
PhabricatorTransactions::TYPE_COMMENT,
null,
null,
$source,
'{}',
$date_created,
$date_modified);
}
$conn_w->saveTransaction();
echo "Done.\n";

View File

@@ -1536,6 +1536,9 @@ phutil_register_library_map(array(
'PhabricatorSlowvotePollController' => 'applications/slowvote/controller/PhabricatorSlowvotePollController.php', 'PhabricatorSlowvotePollController' => 'applications/slowvote/controller/PhabricatorSlowvotePollController.php',
'PhabricatorSlowvoteQuery' => 'applications/slowvote/query/PhabricatorSlowvoteQuery.php', 'PhabricatorSlowvoteQuery' => 'applications/slowvote/query/PhabricatorSlowvoteQuery.php',
'PhabricatorSlowvoteSearchEngine' => 'applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php', 'PhabricatorSlowvoteSearchEngine' => 'applications/slowvote/query/PhabricatorSlowvoteSearchEngine.php',
'PhabricatorSlowvoteTransaction' => 'applications/slowvote/storage/PhabricatorSlowvoteTransaction.php',
'PhabricatorSlowvoteTransactionComment' => 'applications/slowvote/storage/PhabricatorSlowvoteTransactionComment.php',
'PhabricatorSlowvoteTransactionQuery' => 'applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php',
'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php', 'PhabricatorSlowvoteVoteController' => 'applications/slowvote/controller/PhabricatorSlowvoteVoteController.php',
'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php', 'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php',
'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php', 'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php',
@@ -3500,6 +3503,9 @@ phutil_register_library_map(array(
'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController',
'PhabricatorSlowvoteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorSlowvoteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorSlowvoteSearchEngine' => 'PhabricatorApplicationSearchEngine', 'PhabricatorSlowvoteSearchEngine' => 'PhabricatorApplicationSearchEngine',
'PhabricatorSlowvoteTransaction' => 'PhabricatorApplicationTransaction',
'PhabricatorSlowvoteTransactionComment' => 'PhabricatorApplicationTransactionComment',
'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController',
'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 'PhabricatorSlugTestCase' => 'PhabricatorTestCase',
'PhabricatorSortTableExample' => 'PhabricatorUIExample', 'PhabricatorSortTableExample' => 'PhabricatorUIExample',

View File

@@ -9,6 +9,7 @@ final class PhabricatorContentSource {
const SOURCE_MOBILE = 'mobile'; const SOURCE_MOBILE = 'mobile';
const SOURCE_TABLET = 'tablet'; const SOURCE_TABLET = 'tablet';
const SOURCE_FAX = 'fax'; const SOURCE_FAX = 'fax';
const SOURCE_LEGACY = 'legacy';
private $source; private $source;
private $params = array(); private $params = array();

View File

@@ -13,19 +13,20 @@ final class PhabricatorContentSourceView extends AphrontView {
require_celerity_resource('phabricator-content-source-view-css'); require_celerity_resource('phabricator-content-source-view-css');
$map = array( $map = array(
PhabricatorContentSource::SOURCE_WEB => 'Web', PhabricatorContentSource::SOURCE_WEB => pht('Web'),
PhabricatorContentSource::SOURCE_CONDUIT => 'Conduit', PhabricatorContentSource::SOURCE_CONDUIT => pht('Conduit'),
PhabricatorContentSource::SOURCE_EMAIL => 'Email', PhabricatorContentSource::SOURCE_EMAIL => pht('Email'),
PhabricatorContentSource::SOURCE_MOBILE => 'Mobile', PhabricatorContentSource::SOURCE_MOBILE => pht('Mobile'),
PhabricatorContentSource::SOURCE_TABLET => 'Tablet', PhabricatorContentSource::SOURCE_TABLET => pht('Tablet'),
PhabricatorContentSource::SOURCE_FAX => 'Fax', PhabricatorContentSource::SOURCE_FAX => pht('Fax'),
PhabricatorContentSource::SOURCE_LEGACY => pht('Old World'),
); );
$source = $this->contentSource->getSource(); $source = $this->contentSource->getSource();
$type = idx($map, $source, null); $type = idx($map, $source, null);
if (!$type) { if (!$type) {
return; return null;
} }
return phutil_tag( return phutil_tag(

View File

@@ -32,20 +32,10 @@ final class PhabricatorSlowvotePollController
$choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere( $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere(
'pollID = %d', 'pollID = %d',
$poll->getID()); $poll->getID());
$comments = id(new PhabricatorSlowvoteComment())->loadAllWhere(
'pollID = %d',
$poll->getID());
$choices_by_option = mgroup($choices, 'getOptionID'); $choices_by_option = mgroup($choices, 'getOptionID');
$comments_by_user = mpull($comments, null, 'getAuthorPHID');
$choices_by_user = mgroup($choices, 'getAuthorPHID'); $choices_by_user = mgroup($choices, 'getAuthorPHID');
$viewer_choices = idx($choices_by_user, $viewer_phid, array()); $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->isAjax()) { if ($request->isAjax()) {
$embed = id(new SlowvoteEmbedView()) $embed = id(new SlowvoteEmbedView())
@@ -63,7 +53,6 @@ final class PhabricatorSlowvotePollController
$phids = array_merge( $phids = array_merge(
mpull($choices, 'getAuthorPHID'), mpull($choices, 'getAuthorPHID'),
mpull($comments, 'getAuthorPHID'),
array( array(
$poll->getAuthorPHID(), $poll->getAuthorPHID(),
)); ));
@@ -85,22 +74,14 @@ final class PhabricatorSlowvotePollController
$option); $option);
} }
$comments_by_option = array();
switch ($poll->getMethod()) { switch ($poll->getMethod()) {
case PhabricatorSlowvotePoll::METHOD_PLURALITY: case PhabricatorSlowvotePoll::METHOD_PLURALITY:
$choice_ids = array(); $choice_ids = array();
foreach ($choices_by_user as $user_phid => $user_choices) { foreach ($choices_by_user as $user_phid => $user_choices) {
$choice_ids[$user_phid] = head($user_choices)->getOptionID(); $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; break;
case PhabricatorSlowvotePoll::METHOD_APPROVAL: case PhabricatorSlowvotePoll::METHOD_APPROVAL:
// All comments are grouped in approval voting.
break; break;
default: default:
throw new Exception("Unknown poll method!"); throw new Exception("Unknown poll method!");
@@ -110,10 +91,8 @@ final class PhabricatorSlowvotePollController
$poll, $poll,
$options, $options,
$choices, $choices,
$comments,
$viewer_choices, $viewer_choices,
$choices_by_option, $choices_by_option,
$comments_by_option,
$handles, $handles,
$objects); $objects);
@@ -139,12 +118,6 @@ final class PhabricatorSlowvotePollController
id(new AphrontFormMarkupControl()) id(new AphrontFormMarkupControl())
->setLabel(pht('Vote')) ->setLabel(pht('Vote'))
->setValue($option_markup)) ->setValue($option_markup))
->appendChild(
id(new AphrontFormTextAreaControl())
->setLabel(pht('Comments'))
->setHeight(AphrontFormTextAreaControl::HEIGHT_SHORT)
->setName('comments')
->setValue($comment_text))
->appendChild( ->appendChild(
id(new AphrontFormSubmitControl()) id(new AphrontFormSubmitControl())
->setValue(pht('Engage in Deliberations'))); ->setValue(pht('Engage in Deliberations')));
@@ -166,11 +139,14 @@ final class PhabricatorSlowvotePollController
hsprintf('<br /><br />'), hsprintf('<br /><br />'),
$panel); $panel);
$xactions = $this->buildTransactions($poll);
return $this->buildApplicationPage( return $this->buildApplicationPage(
array( array(
$crumbs, $crumbs,
$header, $header,
$content, $content,
$xactions,
), ),
array( array(
'title' => 'V'.$poll->getID().' '.$poll->getQuestion(), 'title' => 'V'.$poll->getID().' '.$poll->getQuestion(),
@@ -179,51 +155,6 @@ final class PhabricatorSlowvotePollController
)); ));
} }
private function renderComments(array $comments, array $handles) {
assert_instances_of($comments, 'PhabricatorSlowvoteComment');
assert_instances_of($handles, 'PhabricatorObjectHandle');
$viewer = $this->getRequest()->getUser();
$engine = PhabricatorMarkupEngine::newSlowvoteMarkupEngine();
$engine->setConfig('viewer', $viewer);
$comment_markup = array();
foreach ($comments as $comment) {
$handle = $handles[$comment->getAuthorPHID()];
$markup = $engine->markupText($comment->getCommentText());
require_celerity_resource('phabricator-remarkup-css');
$comment_markup[] = hsprintf(
'<tr>'.
'<th>'.
'%s'.
'<div class="phabricator-slowvote-datestamp">%s</div>'.
'</th>'.
'<td>'.
'<div class="phabricator-remarkup">%s</div>'.
'</td>'.
'</tr>',
$handle->renderLink(),
phabricator_datetime($comment->getDateCreated(), $viewer),
$markup);
}
if ($comment_markup) {
$comment_markup = phutil_tag(
'table',
array(
'class' => 'phabricator-slowvote-comments',
),
$comment_markup);
} else {
$comment_markup = null;
}
return $comment_markup;
}
private function renderPollOption( private function renderPollOption(
PhabricatorSlowvotePoll $poll, PhabricatorSlowvotePoll $poll,
@@ -330,15 +261,12 @@ final class PhabricatorSlowvotePollController
PhabricatorSlowvotePoll $poll, PhabricatorSlowvotePoll $poll,
array $options, array $options,
array $choices, array $choices,
array $comments,
array $viewer_choices, array $viewer_choices,
array $choices_by_option, array $choices_by_option,
array $comments_by_option,
array $handles, array $handles,
array $objects) { array $objects) {
assert_instances_of($options, 'PhabricatorSlowvoteOption'); assert_instances_of($options, 'PhabricatorSlowvoteOption');
assert_instances_of($choices, 'PhabricatorSlowvoteChoice'); assert_instances_of($choices, 'PhabricatorSlowvoteChoice');
assert_instances_of($comments, 'PhabricatorSlowvoteComment');
assert_instances_of($viewer_choices, 'PhabricatorSlowvoteChoice'); assert_instances_of($viewer_choices, 'PhabricatorSlowvoteChoice');
assert_instances_of($handles, 'PhabricatorObjectHandle'); assert_instances_of($handles, 'PhabricatorObjectHandle');
assert_instances_of($objects, 'PhabricatorLiskDAO'); assert_instances_of($objects, 'PhabricatorLiskDAO');
@@ -407,10 +335,6 @@ final class PhabricatorSlowvotePollController
$user_markup = pht('This option has failed to appeal to anyone.'); $user_markup = pht('This option has failed to appeal to anyone.');
} }
$comment_markup = $this->renderComments(
idx($comments_by_option, $id, array()),
$handles);
$vote_count = $this->renderVoteCount( $vote_count = $this->renderVoteCount(
$poll, $poll,
$choices, $choices,
@@ -422,26 +346,42 @@ final class PhabricatorSlowvotePollController
'<h1>%s</h1>'. '<h1>%s</h1>'.
'<hr class="phabricator-slowvote-hr" />'. '<hr class="phabricator-slowvote-hr" />'.
'%s'. '%s'.
'<div style="clear: both;" />'. '<div style="clear: both;"></div>'.
'<hr class="phabricator-slowvote-hr" />'. '<hr class="phabricator-slowvote-hr" />'.
'%s'.
'</div>', '</div>',
$vote_count, $vote_count,
$option->getName(), $option->getName(),
phutil_tag('div', array(), $user_markup), phutil_tag('div', array(), $user_markup)));
$comment_markup));
}
if ($poll->getMethod() == PhabricatorSlowvotePoll::METHOD_APPROVAL &&
$comments) {
$comment_markup = $this->renderComments(
$comments,
$handles);
$result_markup->appendChild(
phutil_tag('h1', array(), pht('Motions Proposed for Consideration')));
$result_markup->appendChild($comment_markup);
} }
return $result_markup; return $result_markup;
} }
private function buildTransactions(PhabricatorSlowvotePoll $poll) {
$viewer = $this->getRequest()->getUser();
$xactions = id(new PhabricatorSlowvoteTransactionQuery())
->setViewer($viewer)
->withObjectPHIDs(array($poll->getPHID()))
->execute();
$engine = id(new PhabricatorMarkupEngine())
->setViewer($viewer);
foreach ($xactions as $xaction) {
if ($xaction->getComment()) {
$engine->addObject(
$xaction->getComment(),
PhabricatorApplicationTransactionComment::MARKUP_FIELD_COMMENT);
}
}
$engine->process();
$timeline = id(new PhabricatorApplicationTransactionView())
->setUser($viewer)
->setTransactions($xactions)
->setMarkupEngine($engine);
return $timeline;
}
} }

View File

@@ -32,33 +32,6 @@ final class PhabricatorSlowvoteVoteController
$poll->getID(), $poll->getID(),
$user->getPHID()); $user->getPHID());
$comment_text = $request->getStr('comments');
$old_comment = id(new PhabricatorSlowvoteComment())->loadOneWhere(
'pollID = %d AND authorPHID = %s',
$poll->getID(),
$user->getPHID());
$update_comment = false;
if ($old_comment && $comment_text &&
$old_comment->getCommentText() !== $comment_text) {
$update_comment = true;
} else if (!$old_comment && $comment_text) {
$update_comment = true;
}
if ($update_comment) {
if ($old_comment) {
$old_comment->delete();
}
id(new PhabricatorSlowvoteComment())
->setAuthorPHID($user->getPHID())
->setPollID($poll->getID())
->setCommentText($comment_text)
->save();
}
$old_votes = mpull($user_choices, null, 'getOptionID'); $old_votes = mpull($user_choices, null, 'getOptionID');
if ($request->isAjax()) { if ($request->isAjax()) {

View File

@@ -0,0 +1,10 @@
<?php
final class PhabricatorSlowvoteTransactionQuery
extends PhabricatorApplicationTransactionQuery {
protected function getTemplateApplicationTransaction() {
return new PhabricatorSlowvoteTransaction();
}
}

View File

@@ -16,7 +16,7 @@ final class PhabricatorSlowvoteTransaction
} }
public function getApplicationTransactionCommentObject() { public function getApplicationTransactionCommentObject() {
return new PhabricatorMacroTransactionComment(); return new PhabricatorSlowvoteTransactionComment();
} }
public function getApplicationObjectTypeName() { public function getApplicationObjectTypeName() {

View File

@@ -1446,6 +1446,10 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
'type' => 'sql', 'type' => 'sql',
'name' => $this->getPatchPath('20130714.votexactions.sql'), 'name' => $this->getPatchPath('20130714.votexactions.sql'),
), ),
'20130715.votecomments.php' => array(
'type' => 'php',
'name' => $this->getPatchPath('20130715.votecomments.php'),
),
); );
} }
} }