From 10486691581eb7667d1a181ad3770133e70060b3 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 29 Jul 2011 18:31:14 -0700 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 6 +- .../api/PhabricatorConduitAPIController.php | 9 ++- .../PhabricatorConduitConsoleController.php | 11 ++- .../paste/base/ConduitAPI_paste_Method.php | 49 ++++++++++++ .../conduit/method/paste/base/__init__.php | 16 ++++ .../create/ConduitAPI_paste_create_Method.php | 78 +++++++++++++++++++ .../conduit/method/paste/create/__init__.php | 17 ++++ .../info/ConduitAPI_paste_info_Method.php | 15 +--- .../conduit/method/paste/info/__init__.php | 3 +- 9 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 src/applications/conduit/method/paste/base/ConduitAPI_paste_Method.php create mode 100644 src/applications/conduit/method/paste/base/__init__.php create mode 100644 src/applications/conduit/method/paste/create/ConduitAPI_paste_create_Method.php create mode 100644 src/applications/conduit/method/paste/create/__init__.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 9741be3d59..b586b02f48 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php b/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php index 887a1e685e..f2f08e6177 100644 --- a/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php +++ b/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php @@ -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'])) { diff --git a/src/applications/conduit/controller/console/PhabricatorConduitConsoleController.php b/src/applications/conduit/controller/console/PhabricatorConduitConsoleController.php index d7e2c4fc79..c90dc32883 100644 --- a/src/applications/conduit/controller/console/PhabricatorConduitConsoleController.php +++ b/src/applications/conduit/controller/console/PhabricatorConduitConsoleController.php @@ -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); } } diff --git a/src/applications/conduit/method/paste/base/ConduitAPI_paste_Method.php b/src/applications/conduit/method/paste/base/ConduitAPI_paste_Method.php new file mode 100644 index 0000000000..4c611027e4 --- /dev/null +++ b/src/applications/conduit/method/paste/base/ConduitAPI_paste_Method.php @@ -0,0 +1,49 @@ +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, + ); + } + +} diff --git a/src/applications/conduit/method/paste/base/__init__.php b/src/applications/conduit/method/paste/base/__init__.php new file mode 100644 index 0000000000..0bf1ccb7af --- /dev/null +++ b/src/applications/conduit/method/paste/base/__init__.php @@ -0,0 +1,16 @@ + '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); + } + +} diff --git a/src/applications/conduit/method/paste/create/__init__.php b/src/applications/conduit/method/paste/create/__init__.php new file mode 100644 index 0000000000..8e5dd50790 --- /dev/null +++ b/src/applications/conduit/method/paste/create/__init__.php @@ -0,0 +1,17 @@ + $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); } } diff --git a/src/applications/conduit/method/paste/info/__init__.php b/src/applications/conduit/method/paste/info/__init__.php index eafc577b4e..1c1790f2a6 100644 --- a/src/applications/conduit/method/paste/info/__init__.php +++ b/src/applications/conduit/method/paste/info/__init__.php @@ -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');