@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use PhrictionDocumentQuery to load documents

Summary: Ref T4029. We use a lot of very outdated content loading in Phriction, which blocks T4029.

Test Plan:
- Called phriction.info
- Called phriction.history
- Called phriction.edit
- Viewed document list.
- Deleted a document.
- Viewed history.
- Viewed a diff.
- Created a document.
- Edited a document.
- Moved a document.
- Tried to overwrite a document with "new".
- Tried to overwrite a document with "move".
- Viewed a moved document note.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: shadowhand, epriestley

Maniphest Tasks: T4029

Differential Revision: https://secure.phabricator.com/D9194

+127 -82
+13 -3
src/applications/phriction/conduit/ConduitAPI_phriction_edit_Method.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group conduit 5 - */ 6 3 final class ConduitAPI_phriction_edit_Method 7 4 extends ConduitAPI_phriction_Method { 8 5 ··· 30 27 31 28 protected function execute(ConduitAPIRequest $request) { 32 29 $slug = $request->getValue('slug'); 30 + 31 + $doc = id(new PhrictionDocumentQuery()) 32 + ->setViewer($request->getUser()) 33 + ->withSlugs(array(PhabricatorSlug::normalize($slug))) 34 + ->requireCapabilities( 35 + array( 36 + PhabricatorPolicyCapability::CAN_VIEW, 37 + PhabricatorPolicyCapability::CAN_EDIT, 38 + )) 39 + ->executeOne(); 40 + if (!$doc) { 41 + throw new Exception(pht('No such document.')); 42 + } 33 43 34 44 $editor = id(PhrictionDocumentEditor::newForSlug($slug)) 35 45 ->setActor($request->getUser())
+5 -7
src/applications/phriction/conduit/ConduitAPI_phriction_history_Method.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group conduit 5 - */ 6 3 final class ConduitAPI_phriction_history_Method 7 4 extends ConduitAPI_phriction_Method { 8 5 9 6 public function getMethodDescription() { 10 - return "Retrieve history about a Phriction docuemnt."; 7 + return pht('Retrieve history about a Phriction document.'); 11 8 } 12 9 13 10 public function defineParamTypes() { ··· 28 25 29 26 protected function execute(ConduitAPIRequest $request) { 30 27 $slug = $request->getValue('slug'); 31 - $doc = id(new PhrictionDocument())->loadOneWhere( 32 - 'slug = %s', 33 - PhabricatorSlug::normalize($slug)); 28 + $doc = id(new PhrictionDocumentQuery()) 29 + ->setViewer($request->getUser()) 30 + ->withSlugs(array(PhabricatorSlug::normalize($slug))) 31 + ->executeOne(); 34 32 if (!$doc) { 35 33 throw new ConduitException('ERR-BAD-DOCUMENT'); 36 34 }
+10 -13
src/applications/phriction/conduit/ConduitAPI_phriction_info_Method.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group conduit 5 - */ 6 3 final class ConduitAPI_phriction_info_Method 7 4 extends ConduitAPI_phriction_Method { 8 5 9 6 public function getMethodDescription() { 10 - return "Retrieve information about a Phriction document."; 7 + return pht('Retrieve information about a Phriction document.'); 11 8 } 12 9 13 10 public function defineParamTypes() { ··· 29 26 protected function execute(ConduitAPIRequest $request) { 30 27 $slug = $request->getValue('slug'); 31 28 32 - $doc = id(new PhrictionDocument())->loadOneWhere( 33 - 'slug = %s', 34 - PhabricatorSlug::normalize($slug)); 35 - 36 - if (!$doc) { 29 + $document = id(new PhrictionDocumentQuery()) 30 + ->setViewer($request->getUser()) 31 + ->withSlugs(array(PhabricatorSlug::normalize($slug))) 32 + ->needContent(true) 33 + ->executeOne(); 34 + if (!$document) { 37 35 throw new ConduitException('ERR-BAD-DOCUMENT'); 38 36 } 39 37 40 - $content = id(new PhrictionContent())->load($doc->getContentID()); 41 - $doc->attachContent($content); 42 - 43 - return $this->buildDocumentInfoDictionary($doc); 38 + return $this->buildDocumentInfoDictionary( 39 + $document, 40 + $document->getContent()); 44 41 } 45 42 46 43 }
+4 -3
src/applications/phriction/controller/PhrictionController.php
··· 56 56 $ancestral_slugs[] = $slug; 57 57 if ($ancestral_slugs) { 58 58 $empty_slugs = array_fill_keys($ancestral_slugs, null); 59 - $ancestors = id(new PhrictionDocument())->loadAllWhere( 60 - 'slug IN (%Ls)', 61 - $ancestral_slugs); 59 + $ancestors = id(new PhrictionDocumentQuery()) 60 + ->setViewer($this->getRequest()->getUser()) 61 + ->withSlugs($ancestral_slugs) 62 + ->execute(); 62 63 $ancestors = mpull($ancestors, null, 'getSlug'); 63 64 64 65 $ancestor_phids = mpull($ancestors, 'getPHID');
+9 -5
src/applications/phriction/controller/PhrictionDeleteController.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group phriction 5 - */ 6 3 final class PhrictionDeleteController extends PhrictionController { 7 4 8 5 private $id; ··· 12 9 } 13 10 14 11 public function processRequest() { 15 - 16 12 $request = $this->getRequest(); 17 13 $user = $request->getUser(); 18 14 19 - $document = id(new PhrictionDocument())->load($this->id); 15 + $document = id(new PhrictionDocumentQuery()) 16 + ->setViewer($user) 17 + ->withIDs(array($this->id)) 18 + ->requireCapabilities( 19 + array( 20 + PhabricatorPolicyCapability::CAN_EDIT, 21 + PhabricatorPolicyCapability::CAN_VIEW, 22 + )) 23 + ->executeOne(); 20 24 if (!$document) { 21 25 return new Aphront404Response(); 22 26 }
+6 -3
src/applications/phriction/controller/PhrictionDiffController.php
··· 13 13 } 14 14 15 15 public function processRequest() { 16 - 17 16 $request = $this->getRequest(); 18 17 $user = $request->getUser(); 19 18 20 - $document = id(new PhrictionDocument())->load($this->id); 19 + $document = id(new PhrictionDocumentQuery()) 20 + ->setViewer($user) 21 + ->withIDs(array($this->id)) 22 + ->needContent(true) 23 + ->executeOne(); 21 24 if (!$document) { 22 25 return new Aphront404Response(); 23 26 } 24 27 25 - $current = id(new PhrictionContent())->load($document->getContentID()); 28 + $current = $document->getContent(); 26 29 27 30 $l = $request->getInt('l'); 28 31 $r = $request->getInt('r');
+8 -3
src/applications/phriction/controller/PhrictionDocumentController.php
··· 115 115 $core_content = $notice->render(); 116 116 } else if ($current_status == PhrictionChangeType::CHANGE_MOVE_AWAY) { 117 117 $new_doc_id = $content->getChangeRef(); 118 - $new_doc = new PhrictionDocument(); 119 - $new_doc->load($new_doc_id); 118 + $new_doc = id(new PhrictionDocumentQuery()) 119 + ->setViewer($user) 120 + ->withIDs(array($new_doc_id)) 121 + ->exectueOne(); 120 122 121 123 $slug_uri = PhrictionDocument::getSlugURI($new_doc->getSlug()); 122 124 ··· 135 137 $move_notice = null; 136 138 if ($current_status == PhrictionChangeType::CHANGE_MOVE_HERE) { 137 139 $from_doc_id = $content->getChangeRef(); 138 - $from_doc = id(new PhrictionDocument())->load($from_doc_id); 140 + $from_doc = id(new PhrictionDocumentQuery()) 141 + ->setViewer($user) 142 + ->withIDs(array($from_doc_id)) 143 + ->executeOne(); 139 144 $slug_uri = PhrictionDocument::getSlugURI($from_doc->getSlug()); 140 145 141 146 $move_notice = id(new AphrontErrorView())
+15 -8
src/applications/phriction/controller/PhrictionEditController.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group phriction 5 - */ 6 3 final class PhrictionEditController 7 4 extends PhrictionController { 8 5 ··· 18 15 $user = $request->getUser(); 19 16 20 17 if ($this->id) { 21 - $document = id(new PhrictionDocument())->load($this->id); 18 + $document = id(new PhrictionDocumentQuery()) 19 + ->setViewer($user) 20 + ->withIDs(array($this->id)) 21 + ->requireCapabilities( 22 + array( 23 + PhabricatorPolicyCapability::CAN_VIEW, 24 + PhabricatorPolicyCapability::CAN_EDIT, 25 + )) 26 + ->executeOne(); 22 27 if (!$document) { 23 28 return new Aphront404Response(); 24 29 } ··· 43 48 return new Aphront404Response(); 44 49 } 45 50 46 - $document = id(new PhrictionDocument())->loadOneWhere( 47 - 'slug = %s', 48 - $slug); 51 + $document = id(new PhrictionDocumentQuery()) 52 + ->setViewer($user) 53 + ->withSlugs(array($slug)) 54 + ->needContent(true) 55 + ->executeOne(); 49 56 50 57 if ($document) { 51 - $content = id(new PhrictionContent())->load($document->getContentID()); 58 + $content = $document->getContent(); 52 59 } else { 53 60 if (PhrictionDocument::isProjectSlug($slug)) { 54 61 $project = id(new PhabricatorProjectQuery())
+6 -8
src/applications/phriction/controller/PhrictionHistoryController.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group phriction 5 - */ 6 3 final class PhrictionHistoryController 7 4 extends PhrictionController { 8 5 ··· 17 14 $request = $this->getRequest(); 18 15 $user = $request->getUser(); 19 16 20 - $document = id(new PhrictionDocument())->loadOneWhere( 21 - 'slug = %s', 22 - PhabricatorSlug::normalize($this->slug)); 23 - 17 + $document = id(new PhrictionDocumentQuery()) 18 + ->setViewer($user) 19 + ->withSlugs(array(PhabricatorSlug::normalize($this->slug))) 20 + ->needContent(true) 21 + ->executeOne(); 24 22 if (!$document) { 25 23 return new Aphront404Response(); 26 24 } 27 25 28 - $current = id(new PhrictionContent())->load($document->getContentID()); 26 + $current = $document->getContent(); 29 27 30 28 $pager = new AphrontPagerView(); 31 29 $pager->setOffset($request->getInt('page'));
+25 -7
src/applications/phriction/controller/PhrictionMoveController.php
··· 17 17 $user = $request->getUser(); 18 18 19 19 if ($this->id) { 20 - $document = id(new PhrictionDocument())->load($this->id); 20 + $document = id(new PhrictionDocumentQuery()) 21 + ->setViewer($user) 22 + ->withIDs(array($this->id)) 23 + ->requireCapabilities( 24 + array( 25 + PhabricatorPolicyCapability::CAN_VIEW, 26 + PhabricatorPolicyCapability::CAN_EDIT, 27 + )) 28 + ->executeOne(); 21 29 } else { 22 30 $slug = PhabricatorSlug::normalize( 23 31 $request->getStr('slug')); ··· 25 33 return new Aphront404Response(); 26 34 } 27 35 28 - $document = id(new PhrictionDocument())->loadOneWhere( 29 - 'slug = %s', 30 - $slug); 36 + $document = id(new PhrictionDocumentQuery()) 37 + ->setViewer($user) 38 + ->withSlugs(array($slug)) 39 + ->requireCapabilities( 40 + array( 41 + PhabricatorPolicyCapability::CAN_VIEW, 42 + PhabricatorPolicyCapability::CAN_EDIT, 43 + )) 44 + ->executeOne(); 31 45 } 32 46 33 47 if (!$document) { ··· 68 82 69 83 if ($request->isFormPost() && !count($errors)) { 70 84 if (!count($errors)) { // First check if the target document exists 71 - $target_document = id(new PhrictionDocument())->loadOneWhere( 72 - 'slug = %s', 73 - $target_slug); 85 + 86 + // NOTE: We use the ominpotent user because we can't let users overwrite 87 + // documents even if they can't see them. 88 + $target_document = id(new PhrictionDocumentQuery()) 89 + ->setViewer(PhabricatorUser::getOmnipotentUser()) 90 + ->withSlugs(array($target_slug)) 91 + ->executeOne(); 74 92 75 93 // Considering to overwrite existing docs? Nuke this! 76 94 if ($target_document && $target_document->getStatus() ==
+4 -7
src/applications/phriction/controller/PhrictionNewController.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group phriction 5 - */ 6 3 final class PhrictionNewController extends PhrictionController { 7 4 8 5 public function processRequest() { 9 - 10 6 $request = $this->getRequest(); 11 7 $user = $request->getUser(); 12 8 $slug = PhabricatorSlug::normalize($request->getStr('slug')); 13 9 14 10 if ($request->isFormPost()) { 15 - $document = id(new PhrictionDocument())->loadOneWhere( 16 - 'slug = %s', 17 - $slug); 11 + $document = id(new PhrictionDocumentQuery()) 12 + ->setViewer($user) 13 + ->withSlugs(array($slug)) 14 + ->executeOne(); 18 15 $prompt = $request->getStr('prompt', 'no'); 19 16 $document_exists = $document && $document->getStatus() == 20 17 PhrictionDocumentStatus::STATUS_EXISTS;
+2
src/applications/phriction/editor/PhrictionDocumentEditor.php
··· 23 23 24 24 public static function newForSlug($slug) { 25 25 $slug = PhabricatorSlug::normalize($slug); 26 + 27 + // TODO: Get rid of this. 26 28 $document = id(new PhrictionDocument())->loadOneWhere( 27 29 'slug = %s', 28 30 $slug);
+1
src/applications/phriction/phid/PhrictionPHIDTypeDocument.php
··· 25 25 array $phids) { 26 26 27 27 return id(new PhrictionDocumentQuery()) 28 + ->needContent(true) 28 29 ->withPHIDs($phids); 29 30 } 30 31
+18 -12
src/applications/phriction/query/PhrictionDocumentQuery.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group phriction 5 - */ 6 3 final class PhrictionDocumentQuery 7 4 extends PhabricatorCursorPagedPolicyAwareQuery { 8 5 9 6 private $ids; 10 7 private $phids; 11 8 private $slugs; 9 + 10 + private $needContent; 12 11 13 12 private $status = 'status-any'; 14 13 const STATUS_ANY = 'status-any'; ··· 39 38 return $this; 40 39 } 41 40 41 + public function needContent($need_content) { 42 + $this->needContent = $need_content; 43 + return $this; 44 + } 45 + 42 46 public function setOrder($order) { 43 47 $this->order = $order; 44 48 return $this; ··· 60 64 } 61 65 62 66 protected function willFilterPage(array $documents) { 63 - $contents = id(new PhrictionContent())->loadAllWhere( 64 - 'id IN (%Ld)', 65 - mpull($documents, 'getContentID')); 67 + if ($this->needContent) { 68 + $contents = id(new PhrictionContent())->loadAllWhere( 69 + 'id IN (%Ld)', 70 + mpull($documents, 'getContentID')); 66 71 67 - foreach ($documents as $key => $document) { 68 - $content_id = $document->getContentID(); 69 - if (empty($contents[$content_id])) { 70 - unset($documents[$key]); 71 - continue; 72 + foreach ($documents as $key => $document) { 73 + $content_id = $document->getContentID(); 74 + if (empty($contents[$content_id])) { 75 + unset($documents[$key]); 76 + continue; 77 + } 78 + $document->attachContent($contents[$content_id]); 72 79 } 73 - $document->attachContent($contents[$content_id]); 74 80 } 75 81 76 82 foreach ($documents as $document) {
+1
src/applications/phriction/query/PhrictionSearchEngine.php
··· 14 14 15 15 public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { 16 16 $query = id(new PhrictionDocumentQuery()) 17 + ->needContent(true) 17 18 ->withStatus(PhrictionDocumentQuery::STATUS_NONSTUB); 18 19 19 20 $status = $saved->getParameter('status');
-3
src/applications/phriction/search/PhrictionSearchIndexer.php
··· 1 1 <?php 2 2 3 - /** 4 - * @group phriction 5 - */ 6 3 final class PhrictionSearchIndexer 7 4 extends PhabricatorSearchDocumentIndexer { 8 5