implemented put/add-page methods

This commit is contained in:
Michael Mandl 2018-03-29 20:30:30 +02:00
parent 7183624f63
commit 3b05cd7c99
4 changed files with 88 additions and 14 deletions

View file

@ -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<impl> pimpl;

View file

@ -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);
}

View file

@ -2,9 +2,10 @@
#include <iostream>
template <>
template<>
xmlrpc_c::value DokuWiki::impl::executeCommand(const std::string &command,
const std::list<std::string> &params)
const std::list<std::string> &params,
const std::map<std::string, xmlrpc_c::value> &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<std::string> &params)
const std::list<std::string> &params,
const std::map<std::string, xmlrpc_c::value> &attributes)
{
try
{
return xmlrpc_c::value_struct(executeCommand<xmlrpc_c::value>(command, params));
return xmlrpc_c::value_struct(executeCommand<xmlrpc_c::value>(
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<std::string> &params)
const std::list<std::string> &params,
const std::map<std::string, xmlrpc_c::value> &attributes)
{
try
{
return xmlrpc_c::value_string(executeCommand<xmlrpc_c::value>(command, params));
return xmlrpc_c::value_string(executeCommand<xmlrpc_c::value>(
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<std::string> &params)
const std::list<std::string> &params,
const std::map<std::string, xmlrpc_c::value> &attributes)
{
try
{
return xmlrpc_c::value_int(executeCommand<xmlrpc_c::value>(command, params));
return xmlrpc_c::value_int(executeCommand<xmlrpc_c::value>(
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<std::string> &params)
const std::list<std::string> &params,
const std::map<std::string, xmlrpc_c::value> &attributes)
{
try
{
return xmlrpc_c::value_boolean(executeCommand<xmlrpc_c::value>(command, params));
return xmlrpc_c::value_boolean(executeCommand<xmlrpc_c::value>(
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<std::map<std::string, xmlrpc_c::value>>(executeCommand<xmlrpc_c::value_struct>("wiki.getPageInfo", { pageName }));
auto values = static_cast<std::map<std::string, xmlrpc_c::value>>(
executeCommand<xmlrpc_c::value_struct>("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<bool>("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);
}

View file

@ -24,8 +24,13 @@ class DokuWiki::impl
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 <typename ReturnType>
ReturnType executeCommand(const std::string &command,
const std::list<std::string> &params = {});
const std::list<std::string> &params = {},
const std::map<std::string, xmlrpc_c::value> &attributes = {});
};