1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01:00

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
This commit is contained in:
epriestley 2014-05-19 12:41:12 -07:00
parent 3a31554268
commit 4d7c1026f4
16 changed files with 127 additions and 82 deletions

View file

@ -1,8 +1,5 @@
<?php <?php
/**
* @group conduit
*/
final class ConduitAPI_phriction_edit_Method final class ConduitAPI_phriction_edit_Method
extends ConduitAPI_phriction_Method { extends ConduitAPI_phriction_Method {
@ -31,6 +28,19 @@ final class ConduitAPI_phriction_edit_Method
protected function execute(ConduitAPIRequest $request) { protected function execute(ConduitAPIRequest $request) {
$slug = $request->getValue('slug'); $slug = $request->getValue('slug');
$doc = id(new PhrictionDocumentQuery())
->setViewer($request->getUser())
->withSlugs(array(PhabricatorSlug::normalize($slug)))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$doc) {
throw new Exception(pht('No such document.'));
}
$editor = id(PhrictionDocumentEditor::newForSlug($slug)) $editor = id(PhrictionDocumentEditor::newForSlug($slug))
->setActor($request->getUser()) ->setActor($request->getUser())
->setTitle($request->getValue('title')) ->setTitle($request->getValue('title'))

View file

@ -1,13 +1,10 @@
<?php <?php
/**
* @group conduit
*/
final class ConduitAPI_phriction_history_Method final class ConduitAPI_phriction_history_Method
extends ConduitAPI_phriction_Method { extends ConduitAPI_phriction_Method {
public function getMethodDescription() { public function getMethodDescription() {
return "Retrieve history about a Phriction docuemnt."; return pht('Retrieve history about a Phriction document.');
} }
public function defineParamTypes() { public function defineParamTypes() {
@ -28,9 +25,10 @@ final class ConduitAPI_phriction_history_Method
protected function execute(ConduitAPIRequest $request) { protected function execute(ConduitAPIRequest $request) {
$slug = $request->getValue('slug'); $slug = $request->getValue('slug');
$doc = id(new PhrictionDocument())->loadOneWhere( $doc = id(new PhrictionDocumentQuery())
'slug = %s', ->setViewer($request->getUser())
PhabricatorSlug::normalize($slug)); ->withSlugs(array(PhabricatorSlug::normalize($slug)))
->executeOne();
if (!$doc) { if (!$doc) {
throw new ConduitException('ERR-BAD-DOCUMENT'); throw new ConduitException('ERR-BAD-DOCUMENT');
} }

View file

@ -1,13 +1,10 @@
<?php <?php
/**
* @group conduit
*/
final class ConduitAPI_phriction_info_Method final class ConduitAPI_phriction_info_Method
extends ConduitAPI_phriction_Method { extends ConduitAPI_phriction_Method {
public function getMethodDescription() { public function getMethodDescription() {
return "Retrieve information about a Phriction document."; return pht('Retrieve information about a Phriction document.');
} }
public function defineParamTypes() { public function defineParamTypes() {
@ -29,18 +26,18 @@ final class ConduitAPI_phriction_info_Method
protected function execute(ConduitAPIRequest $request) { protected function execute(ConduitAPIRequest $request) {
$slug = $request->getValue('slug'); $slug = $request->getValue('slug');
$doc = id(new PhrictionDocument())->loadOneWhere( $document = id(new PhrictionDocumentQuery())
'slug = %s', ->setViewer($request->getUser())
PhabricatorSlug::normalize($slug)); ->withSlugs(array(PhabricatorSlug::normalize($slug)))
->needContent(true)
if (!$doc) { ->executeOne();
if (!$document) {
throw new ConduitException('ERR-BAD-DOCUMENT'); throw new ConduitException('ERR-BAD-DOCUMENT');
} }
$content = id(new PhrictionContent())->load($doc->getContentID()); return $this->buildDocumentInfoDictionary(
$doc->attachContent($content); $document,
$document->getContent());
return $this->buildDocumentInfoDictionary($doc);
} }
} }

View file

@ -56,9 +56,10 @@ abstract class PhrictionController extends PhabricatorController {
$ancestral_slugs[] = $slug; $ancestral_slugs[] = $slug;
if ($ancestral_slugs) { if ($ancestral_slugs) {
$empty_slugs = array_fill_keys($ancestral_slugs, null); $empty_slugs = array_fill_keys($ancestral_slugs, null);
$ancestors = id(new PhrictionDocument())->loadAllWhere( $ancestors = id(new PhrictionDocumentQuery())
'slug IN (%Ls)', ->setViewer($this->getRequest()->getUser())
$ancestral_slugs); ->withSlugs($ancestral_slugs)
->execute();
$ancestors = mpull($ancestors, null, 'getSlug'); $ancestors = mpull($ancestors, null, 'getSlug');
$ancestor_phids = mpull($ancestors, 'getPHID'); $ancestor_phids = mpull($ancestors, 'getPHID');

View file

@ -1,8 +1,5 @@
<?php <?php
/**
* @group phriction
*/
final class PhrictionDeleteController extends PhrictionController { final class PhrictionDeleteController extends PhrictionController {
private $id; private $id;
@ -12,11 +9,18 @@ final class PhrictionDeleteController extends PhrictionController {
} }
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$document = id(new PhrictionDocument())->load($this->id); $document = id(new PhrictionDocumentQuery())
->setViewer($user)
->withIDs(array($this->id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_EDIT,
PhabricatorPolicyCapability::CAN_VIEW,
))
->executeOne();
if (!$document) { if (!$document) {
return new Aphront404Response(); return new Aphront404Response();
} }

View file

@ -13,16 +13,19 @@ final class PhrictionDiffController
} }
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$document = id(new PhrictionDocument())->load($this->id); $document = id(new PhrictionDocumentQuery())
->setViewer($user)
->withIDs(array($this->id))
->needContent(true)
->executeOne();
if (!$document) { if (!$document) {
return new Aphront404Response(); return new Aphront404Response();
} }
$current = id(new PhrictionContent())->load($document->getContentID()); $current = $document->getContent();
$l = $request->getInt('l'); $l = $request->getInt('l');
$r = $request->getInt('r'); $r = $request->getInt('r');

View file

@ -115,8 +115,10 @@ final class PhrictionDocumentController
$core_content = $notice->render(); $core_content = $notice->render();
} else if ($current_status == PhrictionChangeType::CHANGE_MOVE_AWAY) { } else if ($current_status == PhrictionChangeType::CHANGE_MOVE_AWAY) {
$new_doc_id = $content->getChangeRef(); $new_doc_id = $content->getChangeRef();
$new_doc = new PhrictionDocument(); $new_doc = id(new PhrictionDocumentQuery())
$new_doc->load($new_doc_id); ->setViewer($user)
->withIDs(array($new_doc_id))
->exectueOne();
$slug_uri = PhrictionDocument::getSlugURI($new_doc->getSlug()); $slug_uri = PhrictionDocument::getSlugURI($new_doc->getSlug());
@ -135,7 +137,10 @@ final class PhrictionDocumentController
$move_notice = null; $move_notice = null;
if ($current_status == PhrictionChangeType::CHANGE_MOVE_HERE) { if ($current_status == PhrictionChangeType::CHANGE_MOVE_HERE) {
$from_doc_id = $content->getChangeRef(); $from_doc_id = $content->getChangeRef();
$from_doc = id(new PhrictionDocument())->load($from_doc_id); $from_doc = id(new PhrictionDocumentQuery())
->setViewer($user)
->withIDs(array($from_doc_id))
->executeOne();
$slug_uri = PhrictionDocument::getSlugURI($from_doc->getSlug()); $slug_uri = PhrictionDocument::getSlugURI($from_doc->getSlug());
$move_notice = id(new AphrontErrorView()) $move_notice = id(new AphrontErrorView())

View file

@ -1,8 +1,5 @@
<?php <?php
/**
* @group phriction
*/
final class PhrictionEditController final class PhrictionEditController
extends PhrictionController { extends PhrictionController {
@ -18,7 +15,15 @@ final class PhrictionEditController
$user = $request->getUser(); $user = $request->getUser();
if ($this->id) { if ($this->id) {
$document = id(new PhrictionDocument())->load($this->id); $document = id(new PhrictionDocumentQuery())
->setViewer($user)
->withIDs(array($this->id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$document) { if (!$document) {
return new Aphront404Response(); return new Aphront404Response();
} }
@ -43,12 +48,14 @@ final class PhrictionEditController
return new Aphront404Response(); return new Aphront404Response();
} }
$document = id(new PhrictionDocument())->loadOneWhere( $document = id(new PhrictionDocumentQuery())
'slug = %s', ->setViewer($user)
$slug); ->withSlugs(array($slug))
->needContent(true)
->executeOne();
if ($document) { if ($document) {
$content = id(new PhrictionContent())->load($document->getContentID()); $content = $document->getContent();
} else { } else {
if (PhrictionDocument::isProjectSlug($slug)) { if (PhrictionDocument::isProjectSlug($slug)) {
$project = id(new PhabricatorProjectQuery()) $project = id(new PhabricatorProjectQuery())

View file

@ -1,8 +1,5 @@
<?php <?php
/**
* @group phriction
*/
final class PhrictionHistoryController final class PhrictionHistoryController
extends PhrictionController { extends PhrictionController {
@ -17,15 +14,16 @@ final class PhrictionHistoryController
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$document = id(new PhrictionDocument())->loadOneWhere( $document = id(new PhrictionDocumentQuery())
'slug = %s', ->setViewer($user)
PhabricatorSlug::normalize($this->slug)); ->withSlugs(array(PhabricatorSlug::normalize($this->slug)))
->needContent(true)
->executeOne();
if (!$document) { if (!$document) {
return new Aphront404Response(); return new Aphront404Response();
} }
$current = id(new PhrictionContent())->load($document->getContentID()); $current = $document->getContent();
$pager = new AphrontPagerView(); $pager = new AphrontPagerView();
$pager->setOffset($request->getInt('page')); $pager->setOffset($request->getInt('page'));

View file

@ -17,7 +17,15 @@ final class PhrictionMoveController
$user = $request->getUser(); $user = $request->getUser();
if ($this->id) { if ($this->id) {
$document = id(new PhrictionDocument())->load($this->id); $document = id(new PhrictionDocumentQuery())
->setViewer($user)
->withIDs(array($this->id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
} else { } else {
$slug = PhabricatorSlug::normalize( $slug = PhabricatorSlug::normalize(
$request->getStr('slug')); $request->getStr('slug'));
@ -25,9 +33,15 @@ final class PhrictionMoveController
return new Aphront404Response(); return new Aphront404Response();
} }
$document = id(new PhrictionDocument())->loadOneWhere( $document = id(new PhrictionDocumentQuery())
'slug = %s', ->setViewer($user)
$slug); ->withSlugs(array($slug))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
} }
if (!$document) { if (!$document) {
@ -68,9 +82,13 @@ final class PhrictionMoveController
if ($request->isFormPost() && !count($errors)) { if ($request->isFormPost() && !count($errors)) {
if (!count($errors)) { // First check if the target document exists if (!count($errors)) { // First check if the target document exists
$target_document = id(new PhrictionDocument())->loadOneWhere(
'slug = %s', // NOTE: We use the ominpotent user because we can't let users overwrite
$target_slug); // documents even if they can't see them.
$target_document = id(new PhrictionDocumentQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withSlugs(array($target_slug))
->executeOne();
// Considering to overwrite existing docs? Nuke this! // Considering to overwrite existing docs? Nuke this!
if ($target_document && $target_document->getStatus() == if ($target_document && $target_document->getStatus() ==

View file

@ -1,20 +1,17 @@
<?php <?php
/**
* @group phriction
*/
final class PhrictionNewController extends PhrictionController { final class PhrictionNewController extends PhrictionController {
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$slug = PhabricatorSlug::normalize($request->getStr('slug')); $slug = PhabricatorSlug::normalize($request->getStr('slug'));
if ($request->isFormPost()) { if ($request->isFormPost()) {
$document = id(new PhrictionDocument())->loadOneWhere( $document = id(new PhrictionDocumentQuery())
'slug = %s', ->setViewer($user)
$slug); ->withSlugs(array($slug))
->executeOne();
$prompt = $request->getStr('prompt', 'no'); $prompt = $request->getStr('prompt', 'no');
$document_exists = $document && $document->getStatus() == $document_exists = $document && $document->getStatus() ==
PhrictionDocumentStatus::STATUS_EXISTS; PhrictionDocumentStatus::STATUS_EXISTS;

View file

@ -23,6 +23,8 @@ final class PhrictionDocumentEditor extends PhabricatorEditor {
public static function newForSlug($slug) { public static function newForSlug($slug) {
$slug = PhabricatorSlug::normalize($slug); $slug = PhabricatorSlug::normalize($slug);
// TODO: Get rid of this.
$document = id(new PhrictionDocument())->loadOneWhere( $document = id(new PhrictionDocument())->loadOneWhere(
'slug = %s', 'slug = %s',
$slug); $slug);

View file

@ -25,6 +25,7 @@ final class PhrictionPHIDTypeDocument extends PhabricatorPHIDType {
array $phids) { array $phids) {
return id(new PhrictionDocumentQuery()) return id(new PhrictionDocumentQuery())
->needContent(true)
->withPHIDs($phids); ->withPHIDs($phids);
} }

View file

@ -1,8 +1,5 @@
<?php <?php
/**
* @group phriction
*/
final class PhrictionDocumentQuery final class PhrictionDocumentQuery
extends PhabricatorCursorPagedPolicyAwareQuery { extends PhabricatorCursorPagedPolicyAwareQuery {
@ -10,6 +7,8 @@ final class PhrictionDocumentQuery
private $phids; private $phids;
private $slugs; private $slugs;
private $needContent;
private $status = 'status-any'; private $status = 'status-any';
const STATUS_ANY = 'status-any'; const STATUS_ANY = 'status-any';
const STATUS_OPEN = 'status-open'; const STATUS_OPEN = 'status-open';
@ -39,6 +38,11 @@ final class PhrictionDocumentQuery
return $this; return $this;
} }
public function needContent($need_content) {
$this->needContent = $need_content;
return $this;
}
public function setOrder($order) { public function setOrder($order) {
$this->order = $order; $this->order = $order;
return $this; return $this;
@ -60,6 +64,7 @@ final class PhrictionDocumentQuery
} }
protected function willFilterPage(array $documents) { protected function willFilterPage(array $documents) {
if ($this->needContent) {
$contents = id(new PhrictionContent())->loadAllWhere( $contents = id(new PhrictionContent())->loadAllWhere(
'id IN (%Ld)', 'id IN (%Ld)',
mpull($documents, 'getContentID')); mpull($documents, 'getContentID'));
@ -72,6 +77,7 @@ final class PhrictionDocumentQuery
} }
$document->attachContent($contents[$content_id]); $document->attachContent($contents[$content_id]);
} }
}
foreach ($documents as $document) { foreach ($documents as $document) {
$document->attachProject(null); $document->attachProject(null);

View file

@ -14,6 +14,7 @@ final class PhrictionSearchEngine
public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) {
$query = id(new PhrictionDocumentQuery()) $query = id(new PhrictionDocumentQuery())
->needContent(true)
->withStatus(PhrictionDocumentQuery::STATUS_NONSTUB); ->withStatus(PhrictionDocumentQuery::STATUS_NONSTUB);
$status = $saved->getParameter('status'); $status = $saved->getParameter('status');

View file

@ -1,8 +1,5 @@
<?php <?php
/**
* @group phriction
*/
final class PhrictionSearchIndexer final class PhrictionSearchIndexer
extends PhabricatorSearchDocumentIndexer { extends PhabricatorSearchDocumentIndexer {