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

Provide a paste.create Conduit method

Summary:
  - Allow the console to handle abstract classes correctly.
  - Move paste dictionary generation to an abstract base class.
  - Add paste.create.
  - Add 'objectName', 'parentPHID', and 'content' to Paste info dictionaries
(you can use filePHID with file.download to get the content but I think just
always sending it back is reasonable).

Test Plan:
  - Use paste.create to create new pastes.
  - Used paste.info to get existing pastes.
  - Checked console UI to make sure "paste." didn't show up or anything
silly/dumb like that.
  - Tried to call the method "paste" and got the right exception.

Reviewed By: codeblock
Reviewers: codeblock, jungejason, tuomaspelkonen, aran
CC: aran, codeblock
Differential Revision: 747
This commit is contained in:
epriestley 2011-07-29 18:31:14 -07:00
parent 90cbf8459c
commit 1048669158
9 changed files with 185 additions and 19 deletions

View file

@ -109,6 +109,8 @@ phutil_register_library_map(array(
'ConduitAPI_file_info_Method' => 'applications/conduit/method/file/info',
'ConduitAPI_file_upload_Method' => 'applications/conduit/method/file/upload',
'ConduitAPI_maniphest_info_Method' => 'applications/conduit/method/maniphest/info',
'ConduitAPI_paste_Method' => 'applications/conduit/method/paste/base',
'ConduitAPI_paste_create_Method' => 'applications/conduit/method/paste/create',
'ConduitAPI_paste_info_Method' => 'applications/conduit/method/paste/info',
'ConduitAPI_path_getowners_Method' => 'applications/conduit/method/path/getowners',
'ConduitAPI_slowvote_info_Method' => 'applications/conduit/method/slowvote/info',
@ -728,7 +730,9 @@ phutil_register_library_map(array(
'ConduitAPI_file_info_Method' => 'ConduitAPIMethod',
'ConduitAPI_file_upload_Method' => 'ConduitAPIMethod',
'ConduitAPI_maniphest_info_Method' => 'ConduitAPIMethod',
'ConduitAPI_paste_info_Method' => 'ConduitAPIMethod',
'ConduitAPI_paste_Method' => 'ConduitAPIMethod',
'ConduitAPI_paste_create_Method' => 'ConduitAPI_paste_Method',
'ConduitAPI_paste_info_Method' => 'ConduitAPI_paste_Method',
'ConduitAPI_path_getowners_Method' => 'ConduitAPIMethod',
'ConduitAPI_slowvote_info_Method' => 'ConduitAPIMethod',
'ConduitAPI_user_find_Method' => 'ConduitAPIMethod',

View file

@ -55,8 +55,13 @@ class PhabricatorConduitAPIController
"'{$method_class}', or need to run 'arc build'.");
}
// Fake out checkModule, the class has already been autoloaded by the
// class_exists() call above.
$class_info = new ReflectionClass($method_class);
if ($class_info->isAbstract()) {
throw new Exception(
"Method '{$method}' is not valid; the implementation is an abstract ".
"base class.");
}
$method_handler = newv($method_class, array());
if (isset($_REQUEST['params']) && is_array($_REQUEST['params'])) {

View file

@ -179,7 +179,16 @@ class PhabricatorConduitConsoleController
->setAncestorClass('ConduitAPIMethod')
->setType('class')
->selectSymbolsWithoutLoading();
return array_values(ipull($classes, 'name'));
$class_names = array_values(ipull($classes, 'name'));
foreach ($class_names as $key => $class_name) {
$class_info = new ReflectionClass($class_name);
if ($class_info->isAbstract()) {
unset($class_names[$key]);
}
}
return array_values($class_names);
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* Copyright 2011 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
*/
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(),
'phid' => $paste->getPHID(),
'authorPHID' => $paste->getAuthorPHID(),
'filePHID' => $paste->getFilePHID(),
'title' => $paste->getTitle(),
'dateCreated' => $paste->getDateCreated(),
'language' => $paste->getLanguage(),
'uri' => PhabricatorEnv::getProductionURI('/P'.$paste->getID()),
'parentPHID' => $paste->getParentPHID(),
'content' => $content,
);
}
}

View file

@ -0,0 +1,16 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_module('phabricator', 'applications/conduit/method/base');
phutil_require_module('phabricator', 'applications/files/storage/file');
phutil_require_module('phabricator', 'infrastructure/env');
phutil_require_module('phutil', 'utils');
phutil_require_source('ConduitAPI_paste_Method.php');

View file

@ -0,0 +1,78 @@
<?php
/*
* Copyright 2011 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
*/
class ConduitAPI_paste_create_Method extends ConduitAPI_paste_Method {
public function getMethodDescription() {
return 'Create a new paste.';
}
public function defineParamTypes() {
return array(
'content' => 'required string',
'title' => 'optional string',
'language' => 'optional string',
);
}
public function defineReturnType() {
return 'nonempty dict';
}
public function defineErrorTypes() {
return array(
'ERR-NO-PASTE' => 'Paste may not be empty.',
);
}
protected function execute(ConduitAPIRequest $request) {
$content = $request->getValue('content');
$title = $request->getValue('title');
$language = $request->getValue('language');
if (!strlen($content)) {
throw new ConduitException('ERR-NO-PASTE');
}
$title = nonempty($title, 'Masterwork From Distant Lands');
$language = nonempty($language, '');
$user = $request->getUser();
$paste_file = PhabricatorFile::newFromFileData(
$content,
array(
'name' => $title,
'mime-type' => 'text/plain; charset=utf-8',
'authorPHID' => $user->getPHID(),
));
$paste = new PhabricatorPaste();
$paste->setTitle($title);
$paste->setLanguage($language);
$paste->setFilePHID($paste_file->getPHID());
$paste->setAuthorPHID($user->getPHID());
$paste->save();
return $this->buildPasteInfoDictionary($paste);
}
}

View file

@ -0,0 +1,17 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_module('phabricator', 'applications/conduit/method/paste/base');
phutil_require_module('phabricator', 'applications/conduit/protocol/exception');
phutil_require_module('phabricator', 'applications/files/storage/file');
phutil_require_module('phabricator', 'applications/paste/storage/paste');
phutil_require_module('phutil', 'utils');
phutil_require_source('ConduitAPI_paste_create_Method.php');

View file

@ -19,7 +19,7 @@
/**
* @group conduit
*/
class ConduitAPI_paste_info_Method extends ConduitAPIMethod {
class ConduitAPI_paste_info_Method extends ConduitAPI_paste_Method {
public function getMethodDescription() {
return "Retrieve an array of information about a paste.";
@ -48,18 +48,7 @@ class ConduitAPI_paste_info_Method extends ConduitAPIMethod {
throw new ConduitException('ERR_BAD_PASTE');
}
$result = array(
'id' => $paste->getID(),
'phid' => $paste->getPHID(),
'authorPHID' => $paste->getAuthorPHID(),
'filePHID' => $paste->getFilePHID(),
'title' => $paste->getTitle(),
'dateCreated' => $paste->getDateCreated(),
'language' => $paste->getLanguage(),
'uri' => PhabricatorEnv::getProductionURI('/P'.$paste->getID()),
);
return $result;
return $this->buildPasteInfoDictionary($paste);
}
}

View file

@ -6,10 +6,9 @@
phutil_require_module('phabricator', 'applications/conduit/method/base');
phutil_require_module('phabricator', 'applications/conduit/method/paste/base');
phutil_require_module('phabricator', 'applications/conduit/protocol/exception');
phutil_require_module('phabricator', 'applications/paste/storage/paste');
phutil_require_module('phabricator', 'infrastructure/env');
phutil_require_module('phutil', 'utils');