From 2d87bb29acca98a225cf8abb8dbc45a588a0b9f3 Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Mon, 8 Jul 2013 17:06:49 -0700 Subject: [PATCH] Legalpad - denormalize a field or two from DocumentBody to Document Summary: this let's the list controller save a query. Fixes T3488. Note didn't bother denormalizing document body at all since I don't think we want to show a snippet. Test Plan: viewed a list of legalpad documents - yay. viewed a legalpad document - yay. created a legalpad document - yay. edited a legalpad document - yay. edited with N authors - yay. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T3488 Differential Revision: https://secure.phabricator.com/D6382 --- .../patches/20130703.legalpaddocdenorm.php | 46 +++++++++++++++++++ .../patches/20130703.legalpaddocdenorm.sql | 8 ++++ .../LegalpadDocumentEditController.php | 2 + .../LegalpadDocumentListController.php | 8 ++-- .../editor/LegalpadDocumentEditor.php | 11 ++++- .../query/LegalpadDocumentSearchEngine.php | 2 - .../legalpad/storage/LegalpadDocument.php | 6 +++ .../patch/PhabricatorBuiltinPatchList.php | 8 ++++ 8 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 resources/sql/patches/20130703.legalpaddocdenorm.php create mode 100644 resources/sql/patches/20130703.legalpaddocdenorm.sql diff --git a/resources/sql/patches/20130703.legalpaddocdenorm.php b/resources/sql/patches/20130703.legalpaddocdenorm.php new file mode 100644 index 0000000000..9492e26bc8 --- /dev/null +++ b/resources/sql/patches/20130703.legalpaddocdenorm.php @@ -0,0 +1,46 @@ +openTransaction(); + +foreach (new LiskMigrationIterator($table) as $document) { + $updated = false; + $id = $document->getID(); + + echo "Document {$id}: "; + if (!$document->getTitle()) { + $document_body = id(new LegalpadDocumentBody()) + ->loadOneWhere('phid = %s', $document->getDocumentBodyPHID()); + $title = $document_body->getTitle(); + $document->setTitle($title); + $updated = true; + echo "Added title: $title\n"; + } else { + echo "-\n"; + } + + if (!$document->getContributorCount() || + !$document->getRecentContributorPHIDs()) { + $updated = true; + $type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_CONTRIBUTOR; + $contributors = PhabricatorEdgeQuery::loadDestinationPHIDs( + $document->getPHID(), + $type); + $document->setRecentContributorPHIDs(array_slice($contributors, 0, 3)); + echo "Added recent contributor phids.\n"; + $document->setContributorCount(count($contributors)); + echo "Added contributor count.\n"; + } + + if (!$updated) { + echo "-\n"; + continue; + } + + $document->save(); +} + +$table->saveTransaction(); +echo "Done.\n"; diff --git a/resources/sql/patches/20130703.legalpaddocdenorm.sql b/resources/sql/patches/20130703.legalpaddocdenorm.sql new file mode 100644 index 0000000000..de21bca934 --- /dev/null +++ b/resources/sql/patches/20130703.legalpaddocdenorm.sql @@ -0,0 +1,8 @@ +ALTER TABLE {$NAMESPACE}_legalpad.legalpad_document + ADD recentContributorPHIDs LONGTEXT NOT NULL COLLATE utf8_bin AFTER phid, + ADD contributorCount INT UNSIGNED NOT NULL DEFAULT 0 AFTER phid, + ADD title VARCHAR(255) NOT NULL COLLATE utf8_general_ci AFTER phid; + +ALTER TABLE {$NAMESPACE}_legalpad.legalpad_document + DROP KEY key_creator, + ADD KEY key_creator (creatorPHID, dateModified); diff --git a/src/applications/legalpad/controller/LegalpadDocumentEditController.php b/src/applications/legalpad/controller/LegalpadDocumentEditController.php index 99307fa3b0..a3d8e1b5f9 100644 --- a/src/applications/legalpad/controller/LegalpadDocumentEditController.php +++ b/src/applications/legalpad/controller/LegalpadDocumentEditController.php @@ -21,6 +21,8 @@ final class LegalpadDocumentEditController extends LegalpadController { $document = id(new LegalpadDocument()) ->setVersions(0) ->setCreatorPHID($user->getPHID()) + ->setContributorCount(0) + ->setRecentContributorPHIDs(array()) ->setViewPolicy(PhabricatorPolicies::POLICY_USER) ->setEditPolicy(PhabricatorPolicies::POLICY_USER); $body = id(new LegalpadDocumentBody()) diff --git a/src/applications/legalpad/controller/LegalpadDocumentListController.php b/src/applications/legalpad/controller/LegalpadDocumentListController.php index 8a17eb42e5..47d4f63c47 100644 --- a/src/applications/legalpad/controller/LegalpadDocumentListController.php +++ b/src/applications/legalpad/controller/LegalpadDocumentListController.php @@ -29,18 +29,18 @@ final class LegalpadDocumentListController extends LegalpadController $user = $this->getRequest()->getUser(); - $contributors = array_mergev(mpull($documents, 'getContributors')); + $contributors = array_mergev( + mpull($documents, 'getRecentContributorPHIDs')); $this->loadHandles($contributors); $list = new PhabricatorObjectItemListView(); $list->setUser($user); foreach ($documents as $document) { - $document_body = $document->getDocumentBody(); $last_updated = phabricator_date($document->getDateModified(), $user); $updater = $this->getHandle( - $document_body->getCreatorPHID())->renderLink(); + reset($document->getRecentContributorPHIDs()))->renderLink(); - $title = $document_body->getTitle(); + $title = $document->getTitle(); $item = id(new PhabricatorObjectItemView()) ->setObjectName('L'.$document->getID()) diff --git a/src/applications/legalpad/editor/LegalpadDocumentEditor.php b/src/applications/legalpad/editor/LegalpadDocumentEditor.php index d588eadc33..4f4fb40f1b 100644 --- a/src/applications/legalpad/editor/LegalpadDocumentEditor.php +++ b/src/applications/legalpad/editor/LegalpadDocumentEditor.php @@ -56,6 +56,7 @@ final class LegalpadDocumentEditor switch ($xaction->getTransactionType()) { case LegalpadTransactionType::TYPE_TITLE: + $object->setTitle($xaction->getNewValue()); $body = $object->getDocumentBody(); $body->setTitle($xaction->getNewValue()); $this->setIsContribution(true); @@ -86,7 +87,6 @@ final class LegalpadDocumentEditor $body->save(); $object->setDocumentBodyPHID($body->getPHID()); - $object->save(); $actor = $this->getActor(); $type = PhabricatorEdgeConfig::TYPE_CONTRIBUTED_TO_OBJECT; @@ -94,6 +94,15 @@ final class LegalpadDocumentEditor ->addEdge($actor->getPHID(), $type, $object->getPHID()) ->setActor($actor) ->save(); + + $type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_CONTRIBUTOR; + $contributors = PhabricatorEdgeQuery::loadDestinationPHIDs( + $object->getPHID(), + $type); + $object->setRecentContributorPHIDs(array_slice($contributors, 0, 3)); + $object->setContributorCount(count($contributors)); + + $object->save(); } } diff --git a/src/applications/legalpad/query/LegalpadDocumentSearchEngine.php b/src/applications/legalpad/query/LegalpadDocumentSearchEngine.php index ae14b8387a..b743a72f3f 100644 --- a/src/applications/legalpad/query/LegalpadDocumentSearchEngine.php +++ b/src/applications/legalpad/query/LegalpadDocumentSearchEngine.php @@ -24,8 +24,6 @@ final class LegalpadDocumentSearchEngine public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { $query = id(new LegalpadDocumentQuery()) - ->needDocumentBodies(true) - ->needContributors(true) ->withCreatorPHIDs($saved->getParameter('creatorPHIDs', array())) ->withContributorPHIDs($saved->getParameter('contributorPHIDs', array())); diff --git a/src/applications/legalpad/storage/LegalpadDocument.php b/src/applications/legalpad/storage/LegalpadDocument.php index 98b1714eb6..2ec63fa679 100644 --- a/src/applications/legalpad/storage/LegalpadDocument.php +++ b/src/applications/legalpad/storage/LegalpadDocument.php @@ -10,6 +10,9 @@ final class LegalpadDocument extends LegalpadDAO PhabricatorApplicationTransactionInterface { protected $phid; + protected $title; + protected $contributorCount; + protected $recentContributorPHIDs = array(); protected $creatorPHID; protected $versions; protected $documentBodyPHID; @@ -23,6 +26,9 @@ final class LegalpadDocument extends LegalpadDAO public function getConfiguration() { return array( self::CONFIG_AUX_PHID => true, + self::CONFIG_SERIALIZATION => array( + 'recentContributorPHIDs' => self::SERIALIZATION_JSON, + ), ) + parent::getConfiguration(); } diff --git a/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php b/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php index 44adcaf273..6db6e45dd6 100644 --- a/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php +++ b/src/infrastructure/storage/patch/PhabricatorBuiltinPatchList.php @@ -1422,6 +1422,14 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList { 'type' => 'php', 'name' => $this->getPatchPath('legalpad-mailkey-populate.php'), ), + '20130703.legalpaddocdenorm.sql' => array( + 'type' => 'sql', + 'name' => $this->getPatchPath('20130703.legalpaddocdenorm.sql'), + ), + '20130703.legalpaddocdenorm.php' => array( + 'type' => 'php', + 'name' => $this->getPatchPath('20130703.legalpaddocdenorm.php'), + ), ); } }