From ea52bcbcd65136ad001d1360394e41d3c4493ad0 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 15 Jul 2013 04:20:10 -0700 Subject: [PATCH] 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 --- .../sql/patches/20130715.votecomments.php | 101 ++++++++++++++ src/__phutil_library_map__.php | 6 + .../PhabricatorContentSource.php | 1 + .../PhabricatorContentSourceView.php | 15 ++- .../PhabricatorSlowvotePollController.php | 126 +++++------------- .../PhabricatorSlowvoteVoteController.php | 27 ---- .../PhabricatorSlowvoteTransactionQuery.php | 10 ++ .../PhabricatorSlowvoteTransaction.php | 2 +- .../patch/PhabricatorBuiltinPatchList.php | 4 + 9 files changed, 164 insertions(+), 128 deletions(-) create mode 100644 resources/sql/patches/20130715.votecomments.php create mode 100644 src/applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php diff --git a/resources/sql/patches/20130715.votecomments.php b/resources/sql/patches/20130715.votecomments.php new file mode 100644 index 0000000000..3611c0eae3 --- /dev/null +++ b/resources/sql/patches/20130715.votecomments.php @@ -0,0 +1,101 @@ +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"; diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 4e0a23a68a..e55ceb28b5 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1536,6 +1536,9 @@ phutil_register_library_map(array( 'PhabricatorSlowvotePollController' => 'applications/slowvote/controller/PhabricatorSlowvotePollController.php', 'PhabricatorSlowvoteQuery' => 'applications/slowvote/query/PhabricatorSlowvoteQuery.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', 'PhabricatorSlug' => 'infrastructure/util/PhabricatorSlug.php', 'PhabricatorSlugTestCase' => 'infrastructure/util/__tests__/PhabricatorSlugTestCase.php', @@ -3500,6 +3503,9 @@ phutil_register_library_map(array( 'PhabricatorSlowvotePollController' => 'PhabricatorSlowvoteController', 'PhabricatorSlowvoteQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorSlowvoteSearchEngine' => 'PhabricatorApplicationSearchEngine', + 'PhabricatorSlowvoteTransaction' => 'PhabricatorApplicationTransaction', + 'PhabricatorSlowvoteTransactionComment' => 'PhabricatorApplicationTransactionComment', + 'PhabricatorSlowvoteTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhabricatorSlowvoteVoteController' => 'PhabricatorSlowvoteController', 'PhabricatorSlugTestCase' => 'PhabricatorTestCase', 'PhabricatorSortTableExample' => 'PhabricatorUIExample', diff --git a/src/applications/metamta/contentsource/PhabricatorContentSource.php b/src/applications/metamta/contentsource/PhabricatorContentSource.php index 518f5b38f4..03208832a6 100644 --- a/src/applications/metamta/contentsource/PhabricatorContentSource.php +++ b/src/applications/metamta/contentsource/PhabricatorContentSource.php @@ -9,6 +9,7 @@ final class PhabricatorContentSource { const SOURCE_MOBILE = 'mobile'; const SOURCE_TABLET = 'tablet'; const SOURCE_FAX = 'fax'; + const SOURCE_LEGACY = 'legacy'; private $source; private $params = array(); diff --git a/src/applications/metamta/contentsource/PhabricatorContentSourceView.php b/src/applications/metamta/contentsource/PhabricatorContentSourceView.php index 81ffda8de8..f519cf98f2 100644 --- a/src/applications/metamta/contentsource/PhabricatorContentSourceView.php +++ b/src/applications/metamta/contentsource/PhabricatorContentSourceView.php @@ -13,19 +13,20 @@ final class PhabricatorContentSourceView extends AphrontView { require_celerity_resource('phabricator-content-source-view-css'); $map = array( - PhabricatorContentSource::SOURCE_WEB => 'Web', - PhabricatorContentSource::SOURCE_CONDUIT => 'Conduit', - PhabricatorContentSource::SOURCE_EMAIL => 'Email', - PhabricatorContentSource::SOURCE_MOBILE => 'Mobile', - PhabricatorContentSource::SOURCE_TABLET => 'Tablet', - PhabricatorContentSource::SOURCE_FAX => 'Fax', + PhabricatorContentSource::SOURCE_WEB => pht('Web'), + PhabricatorContentSource::SOURCE_CONDUIT => pht('Conduit'), + PhabricatorContentSource::SOURCE_EMAIL => pht('Email'), + PhabricatorContentSource::SOURCE_MOBILE => pht('Mobile'), + PhabricatorContentSource::SOURCE_TABLET => pht('Tablet'), + PhabricatorContentSource::SOURCE_FAX => pht('Fax'), + PhabricatorContentSource::SOURCE_LEGACY => pht('Old World'), ); $source = $this->contentSource->getSource(); $type = idx($map, $source, null); if (!$type) { - return; + return null; } return phutil_tag( diff --git a/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php b/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php index 107d59234f..35264f5cd5 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvotePollController.php @@ -32,20 +32,10 @@ final class PhabricatorSlowvotePollController $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->isAjax()) { $embed = id(new SlowvoteEmbedView()) @@ -63,7 +53,6 @@ final class PhabricatorSlowvotePollController $phids = array_merge( mpull($choices, 'getAuthorPHID'), - mpull($comments, 'getAuthorPHID'), array( $poll->getAuthorPHID(), )); @@ -85,22 +74,14 @@ final class PhabricatorSlowvotePollController $option); } - $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!"); @@ -110,10 +91,8 @@ final class PhabricatorSlowvotePollController $poll, $options, $choices, - $comments, $viewer_choices, $choices_by_option, - $comments_by_option, $handles, $objects); @@ -139,12 +118,6 @@ final class PhabricatorSlowvotePollController id(new AphrontFormMarkupControl()) ->setLabel(pht('Vote')) ->setValue($option_markup)) - ->appendChild( - id(new AphrontFormTextAreaControl()) - ->setLabel(pht('Comments')) - ->setHeight(AphrontFormTextAreaControl::HEIGHT_SHORT) - ->setName('comments') - ->setValue($comment_text)) ->appendChild( id(new AphrontFormSubmitControl()) ->setValue(pht('Engage in Deliberations'))); @@ -166,11 +139,14 @@ final class PhabricatorSlowvotePollController hsprintf('

'), $panel); + $xactions = $this->buildTransactions($poll); + return $this->buildApplicationPage( array( $crumbs, $header, $content, + $xactions, ), array( '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( - ''. - ''. - '%s'. - '
%s
'. - ''. - ''. - '
%s
'. - ''. - '', - $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( PhabricatorSlowvotePoll $poll, @@ -330,15 +261,12 @@ final class PhabricatorSlowvotePollController 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'); @@ -407,10 +335,6 @@ final class PhabricatorSlowvotePollController $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( $poll, $choices, @@ -422,26 +346,42 @@ final class PhabricatorSlowvotePollController '

%s

'. '
'. '%s'. - '
'. + '
'. '
'. - '%s'. '
', $vote_count, $option->getName(), - 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); + phutil_tag('div', array(), $user_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; + } + } diff --git a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php index dd5ea2aab6..99facb5f5f 100644 --- a/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php +++ b/src/applications/slowvote/controller/PhabricatorSlowvoteVoteController.php @@ -32,33 +32,6 @@ final class PhabricatorSlowvoteVoteController $poll->getID(), $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'); if ($request->isAjax()) { diff --git a/src/applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php b/src/applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php new file mode 100644 index 0000000000..cf75862c62 --- /dev/null +++ b/src/applications/slowvote/query/PhabricatorSlowvoteTransactionQuery.php @@ -0,0 +1,10 @@ + 'sql', 'name' => $this->getPatchPath('20130714.votexactions.sql'), ), + '20130715.votecomments.php' => array( + 'type' => 'php', + 'name' => $this->getPatchPath('20130715.votecomments.php'), + ), ); } }