From 3b05cd7c99d9aa7f5aa0ed0dea145f3806901bda Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 29 Mar 2018 20:30:30 +0200 Subject: [PATCH] implemented put/add-page methods --- include/dokuwiki.h | 4 +++ source/dokuwiki.cpp | 16 +++++++++ source/dokuwiki.impl.cpp | 73 +++++++++++++++++++++++++++++++++------- source/dokuwiki.impl.h | 9 +++-- 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/include/dokuwiki.h b/include/dokuwiki.h index 500708c..c4026de 100644 --- a/include/dokuwiki.h +++ b/include/dokuwiki.h @@ -25,6 +25,10 @@ class DokuWiki std::string getWikiTitle(); PageInfo getPageInfo(const std::string &pageName); + bool pageExists(const std::string &pageName); + void putPage(const std::string &pageName, const std::string &content); + void addPage(const std::string &pageName, const std::string &content); + private: class impl; std::unique_ptr pimpl; diff --git a/source/dokuwiki.cpp b/source/dokuwiki.cpp index f688ea6..0ae6490 100644 --- a/source/dokuwiki.cpp +++ b/source/dokuwiki.cpp @@ -36,3 +36,19 @@ DokuWiki::PageInfo DokuWiki::getPageInfo(const std::string &pageName) { return pimpl->getPageInfo(pageName); } + +bool DokuWiki::pageExists(const std::string &pageName) +{ + return pimpl->pageExists(pageName); +} + +void DokuWiki::putPage(const std::string &pageName, const std::string &content) +{ + pimpl->putPage(pageName, content); +} + +void DokuWiki::addPage(const std::string &pageName, const std::string &content) +{ + pimpl->addPage(pageName, content); +} + diff --git a/source/dokuwiki.impl.cpp b/source/dokuwiki.impl.cpp index 5f74746..6fcae37 100644 --- a/source/dokuwiki.impl.cpp +++ b/source/dokuwiki.impl.cpp @@ -2,9 +2,10 @@ #include -template <> +template<> xmlrpc_c::value DokuWiki::impl::executeCommand(const std::string &command, - const std::list ¶ms) + const std::list ¶ms, + const std::map &attributes) { xmlrpc_c::client_xml client(&m_clientTransport); @@ -14,6 +15,11 @@ xmlrpc_c::value DokuWiki::impl::executeCommand(const std::string &command, requestParams.add(xmlrpc_c::value_string(param)); } + if (!attributes.empty()) + { + requestParams.add(xmlrpc_c::value_struct(attributes)); + } + xmlrpc_c::rpcPtr rpc(command, requestParams); xmlrpc_c::carriageParm_curl0 carriageParm(m_url); @@ -25,11 +31,13 @@ xmlrpc_c::value DokuWiki::impl::executeCommand(const std::string &command, template<> xmlrpc_c::value_struct DokuWiki::impl::executeCommand(const std::string &command, - const std::list ¶ms) + const std::list ¶ms, + const std::map &attributes) { try { - return xmlrpc_c::value_struct(executeCommand(command, params)); + return xmlrpc_c::value_struct(executeCommand( + command, params, attributes)); } catch (girerr::error &e) { @@ -39,11 +47,13 @@ xmlrpc_c::value_struct DokuWiki::impl::executeCommand(const std::string &command template<> std::string DokuWiki::impl::executeCommand(const std::string &command, - const std::list ¶ms) + const std::list ¶ms, + const std::map &attributes) { try { - return xmlrpc_c::value_string(executeCommand(command, params)); + return xmlrpc_c::value_string(executeCommand( + command, params, attributes)); } catch (girerr::error &e) { @@ -53,11 +63,13 @@ std::string DokuWiki::impl::executeCommand(const std::string &command, template<> int DokuWiki::impl::executeCommand(const std::string &command, - const std::list ¶ms) + const std::list ¶ms, + const std::map &attributes) { try { - return xmlrpc_c::value_int(executeCommand(command, params)); + return xmlrpc_c::value_int(executeCommand( + command, params, attributes)); } catch (girerr::error &e) { @@ -67,11 +79,13 @@ int DokuWiki::impl::executeCommand(const std::string &command, template<> bool DokuWiki::impl::executeCommand(const std::string &command, - const std::list ¶ms) + const std::list ¶ms, + const std::map &attributes) { try { - return xmlrpc_c::value_boolean(executeCommand(command, params)); + return xmlrpc_c::value_boolean(executeCommand( + command, params, attributes)); } catch (girerr::error &e) { @@ -79,7 +93,8 @@ bool DokuWiki::impl::executeCommand(const std::string &command, } } -DokuWiki::impl::impl(const std::string &url, const std::string &username, const std::string &password) +DokuWiki::impl::impl(const std::string &url, const std::string &username, + const std::string &password) : m_url(url) { login(username, password); @@ -117,7 +132,8 @@ std::string DokuWiki::impl::getWikiTitle() DokuWiki::PageInfo DokuWiki::impl::getPageInfo(const std::string &pageName) { - auto values = static_cast>(executeCommand("wiki.getPageInfo", { pageName })); + auto values = static_cast>( + executeCommand("wiki.getPageInfo", { pageName })); std::time_t modificationTime = xmlrpc_c::value_int(values["version"]); @@ -131,3 +147,36 @@ DokuWiki::PageInfo DokuWiki::impl::getPageInfo(const std::string &pageName) return pageInfo; } +bool DokuWiki::impl::pageExists(const std::string &pageName) +{ + try + { + getPageInfo(pageName); + return true; + } + catch (std::runtime_error &) + { + return false; + } +} + +void DokuWiki::impl::putPage(const std::string &pageName, const std::string &content) +{ + if (!executeCommand("wiki.putPage", { pageName, content }, + {{ "sum", xmlrpc_c::value_string("Added automatically") }, + {"minor", xmlrpc_c::value_boolean(false) }})) + { + throw std::runtime_error("failed to add page"); + } +} + +void DokuWiki::impl::addPage(const std::string &pageName, const std::string &content) +{ + if (pageExists(pageName)) + { + throw std::runtime_error("page already exists"); + } + + putPage(pageName, content); +} + diff --git a/source/dokuwiki.impl.h b/source/dokuwiki.impl.h index c8f39a1..7ada3b0 100644 --- a/source/dokuwiki.impl.h +++ b/source/dokuwiki.impl.h @@ -23,9 +23,14 @@ class DokuWiki::impl std::string getWikiTitle(); DokuWiki::PageInfo getPageInfo(const std::string &pageName); - + + bool pageExists(const std::string &pageName); + void putPage(const std::string &pageName, const std::string &content); + void addPage(const std::string &pageName, const std::string &content); + template ReturnType executeCommand(const std::string &command, - const std::list ¶ms = {}); + const std::list ¶ms = {}, + const std::map &attributes = {}); };