mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-23 14:00:56 +01:00
Add 'paste.query' conduit method and deprecate 'paste.info'
Summary: - Modernize the Paste Conduit API. - Deprecate 'paste.info'. - Make queries policy-aware. - Move methods into the application to support greater modularity. Test Plan: Made `paste.query`, `paste.info`, `paste.create` calls. Browsed Paste interfaces. Forked a paste. Reviewers: vrana, btrahan Reviewed By: btrahan CC: aran Differential Revision: https://secure.phabricator.com/D3379
This commit is contained in:
parent
85bf88e400
commit
d814245eea
8 changed files with 145 additions and 23 deletions
|
@ -158,9 +158,10 @@ phutil_register_library_map(array(
|
|||
'ConduitAPI_maniphest_query_Method' => 'applications/conduit/method/maniphest/ConduitAPI_maniphest_query_Method.php',
|
||||
'ConduitAPI_maniphest_update_Method' => 'applications/conduit/method/maniphest/ConduitAPI_maniphest_update_Method.php',
|
||||
'ConduitAPI_owners_query_Method' => 'applications/conduit/method/owners/ConduitAPI_owners_query_Method.php',
|
||||
'ConduitAPI_paste_Method' => 'applications/conduit/method/paste/ConduitAPI_paste_Method.php',
|
||||
'ConduitAPI_paste_create_Method' => 'applications/conduit/method/paste/ConduitAPI_paste_create_Method.php',
|
||||
'ConduitAPI_paste_info_Method' => 'applications/conduit/method/paste/ConduitAPI_paste_info_Method.php',
|
||||
'ConduitAPI_paste_Method' => 'applications/paste/conduit/ConduitAPI_paste_Method.php',
|
||||
'ConduitAPI_paste_create_Method' => 'applications/paste/conduit/ConduitAPI_paste_create_Method.php',
|
||||
'ConduitAPI_paste_info_Method' => 'applications/paste/conduit/ConduitAPI_paste_info_Method.php',
|
||||
'ConduitAPI_paste_query_Method' => 'applications/paste/conduit/ConduitAPI_paste_query_Method.php',
|
||||
'ConduitAPI_phid_Method' => 'applications/conduit/method/phid/ConduitAPI_phid_Method.php',
|
||||
'ConduitAPI_phid_info_Method' => 'applications/conduit/method/phid/ConduitAPI_phid_info_Method.php',
|
||||
'ConduitAPI_phid_lookup_Method' => 'applications/conduit/method/phid/ConduitAPI_phid_lookup_Method.php',
|
||||
|
@ -1352,6 +1353,7 @@ phutil_register_library_map(array(
|
|||
'ConduitAPI_paste_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_paste_create_Method' => 'ConduitAPI_paste_Method',
|
||||
'ConduitAPI_paste_info_Method' => 'ConduitAPI_paste_Method',
|
||||
'ConduitAPI_paste_query_Method' => 'ConduitAPI_paste_Method',
|
||||
'ConduitAPI_phid_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_phid_info_Method' => 'ConduitAPI_phid_Method',
|
||||
'ConduitAPI_phid_lookup_Method' => 'ConduitAPI_phid_Method',
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Facebook, Inc.
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -22,15 +22,6 @@
|
|||
abstract class ConduitAPI_paste_Method extends ConduitAPIMethod {
|
||||
|
||||
protected function buildPasteInfoDictionary(PhabricatorPaste $paste) {
|
||||
|
||||
$content = null;
|
||||
$file = id(new PhabricatorFile())->loadOneWhere(
|
||||
'phid = %s',
|
||||
$paste->getFilePHID());
|
||||
if ($file) {
|
||||
$content = $file->loadFileData();
|
||||
}
|
||||
|
||||
return array(
|
||||
'id' => $paste->getID(),
|
||||
'objectName' => 'P'.$paste->getID(),
|
||||
|
@ -42,7 +33,7 @@ abstract class ConduitAPI_paste_Method extends ConduitAPIMethod {
|
|||
'language' => $paste->getLanguage(),
|
||||
'uri' => PhabricatorEnv::getProductionURI('/P'.$paste->getID()),
|
||||
'parentPHID' => $paste->getParentPHID(),
|
||||
'content' => $content,
|
||||
'content' => $paste->getContent(),
|
||||
);
|
||||
}
|
||||
|
|
@ -72,6 +72,8 @@ final class ConduitAPI_paste_create_Method extends ConduitAPI_paste_Method {
|
|||
$paste->setAuthorPHID($user->getPHID());
|
||||
$paste->save();
|
||||
|
||||
$paste->attachContent($content);
|
||||
|
||||
return $this->buildPasteInfoDictionary($paste);
|
||||
}
|
||||
|
|
@ -21,6 +21,14 @@
|
|||
*/
|
||||
final class ConduitAPI_paste_info_Method extends ConduitAPI_paste_Method {
|
||||
|
||||
public function getMethodStatus() {
|
||||
return self::METHOD_STATUS_DEPRECATED;
|
||||
}
|
||||
|
||||
public function getMethodStatusDescription() {
|
||||
return "Replaced by 'paste.query'.";
|
||||
}
|
||||
|
||||
public function getMethodDescription() {
|
||||
return "Retrieve an array of information about a paste.";
|
||||
}
|
||||
|
@ -43,11 +51,14 @@ final class ConduitAPI_paste_info_Method extends ConduitAPI_paste_Method {
|
|||
|
||||
protected function execute(ConduitAPIRequest $request) {
|
||||
$paste_id = $request->getValue('paste_id');
|
||||
$paste = id(new PhabricatorPaste())->load($paste_id);
|
||||
$paste = id(new PhabricatorPasteQuery())
|
||||
->setViewer($request->getUser())
|
||||
->withObjectIDs(array($paste_id))
|
||||
->needContent(true)
|
||||
->executeOne();
|
||||
if (!$paste) {
|
||||
throw new ConduitException('ERR_BAD_PASTE');
|
||||
}
|
||||
|
||||
return $this->buildPasteInfoDictionary($paste);
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group conduit
|
||||
*/
|
||||
final class ConduitAPI_paste_query_Method extends ConduitAPI_paste_Method {
|
||||
|
||||
public function getMethodDescription() {
|
||||
return "Query Pastes.";
|
||||
}
|
||||
|
||||
public function defineParamTypes() {
|
||||
return array(
|
||||
'ids' => 'optional list<int>',
|
||||
'phids' => 'optional list<phid>',
|
||||
'authorPHIDs' => 'optional list<phid>',
|
||||
'after' => 'optional int',
|
||||
'limit' => 'optional int, default = 100',
|
||||
);
|
||||
}
|
||||
|
||||
public function defineReturnType() {
|
||||
return 'list<dict>';
|
||||
}
|
||||
|
||||
public function defineErrorTypes() {
|
||||
return array();
|
||||
}
|
||||
|
||||
protected function execute(ConduitAPIRequest $request) {
|
||||
$query = id(new PhabricatorPasteQuery())
|
||||
->setViewer($request->getUser())
|
||||
->needContent(true);
|
||||
|
||||
if ($request->getValue('ids')) {
|
||||
$query->withIDs($request->getValue('ids'));
|
||||
}
|
||||
|
||||
if ($request->getValue('phids')) {
|
||||
$query->withPHIDs($request->getValue('phids'));
|
||||
}
|
||||
|
||||
if ($request->getValue('authorPHIDs')) {
|
||||
$query->withAuthorPHIDs($request->getValue('authorPHIDs'));
|
||||
}
|
||||
|
||||
if ($request->getValue('after')) {
|
||||
$query->setAfterID($request->getValue('after'));
|
||||
}
|
||||
|
||||
$limit = $request->getValue('limit', 100);
|
||||
if ($limit) {
|
||||
$query->setLimit($limit);
|
||||
}
|
||||
|
||||
$pastes = $query->execute();
|
||||
|
||||
$results = array();
|
||||
foreach ($pastes as $paste) {
|
||||
$results[$paste->getPHID()] = $this->buildPasteInfoDictionary($paste);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
}
|
|
@ -43,6 +43,7 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
$parent = id(new PhabricatorPasteQuery())
|
||||
->setViewer($user)
|
||||
->withIDs(array($parent_id))
|
||||
->needContent(true)
|
||||
->execute();
|
||||
$parent = head($parent);
|
||||
|
||||
|
@ -105,11 +106,7 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
|||
if ($is_create && $parent) {
|
||||
$paste->setTitle('Fork of '.$parent->getFullName());
|
||||
$paste->setLanguage($parent->getLanguage());
|
||||
|
||||
$parent_file = id(new PhabricatorFile())->loadOneWhere(
|
||||
'phid = %s',
|
||||
$parent->getFilePHID());
|
||||
$text = $parent_file->loadFileData();
|
||||
$text = $parent->getContent();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,8 @@ final class PhabricatorPasteQuery extends PhabricatorCursorPagedPolicyQuery {
|
|||
private $authorPHIDs;
|
||||
private $parentPHIDs;
|
||||
|
||||
private $needContent;
|
||||
|
||||
public function withIDs(array $ids) {
|
||||
$this->ids = $ids;
|
||||
return $this;
|
||||
|
@ -43,6 +45,11 @@ final class PhabricatorPasteQuery extends PhabricatorCursorPagedPolicyQuery {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function needContent($need_content) {
|
||||
$this->needContent = $need_content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function loadPage() {
|
||||
$table = new PhabricatorPaste();
|
||||
$conn_r = $table->establishConnection('r');
|
||||
|
@ -55,9 +62,25 @@ final class PhabricatorPasteQuery extends PhabricatorCursorPagedPolicyQuery {
|
|||
$this->buildOrderClause($conn_r),
|
||||
$this->buildLimitClause($conn_r));
|
||||
|
||||
$results = $table->loadAllFromArray($data);
|
||||
$pastes = $table->loadAllFromArray($data);
|
||||
|
||||
return $this->processResults($results);
|
||||
if ($pastes && $this->needContent) {
|
||||
$file_phids = mpull($pastes, 'getFilePHID');
|
||||
$files = id(new PhabricatorFile())->loadAllWhere(
|
||||
'phid IN (%Ls)',
|
||||
$file_phids);
|
||||
$files = mpull($files, null, 'getPHID');
|
||||
foreach ($pastes as $paste) {
|
||||
$file = idx($files, $paste->getFilePHID());
|
||||
if ($file) {
|
||||
$paste->attachContent($file->loadFileData());
|
||||
} else {
|
||||
$paste->attachContent('');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->processResults($pastes);
|
||||
}
|
||||
|
||||
protected function buildWhereClause($conn_r) {
|
||||
|
|
|
@ -26,6 +26,8 @@ final class PhabricatorPaste extends PhabricatorPasteDAO
|
|||
protected $language;
|
||||
protected $parentPHID;
|
||||
|
||||
private $content;
|
||||
|
||||
public function getURI() {
|
||||
return '/P'.$this->getID();
|
||||
}
|
||||
|
@ -67,4 +69,16 @@ final class PhabricatorPaste extends PhabricatorPasteDAO
|
|||
return 'P'.$this->getID().' '.$title;
|
||||
}
|
||||
|
||||
public function getContent() {
|
||||
if ($this->content === null) {
|
||||
throw new Exception("Call attachContent() before getContent()!");
|
||||
}
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function attachContent($content) {
|
||||
$this->content = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue