qswiki/handlers/handlerpageview.cpp

170 خطوط
5.2 KiB
C++

2018-11-03 17:12:20 +01:00
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "handlerpageview.h"
#include "../database/exceptions.h"
#include "../logger.h"
#include "../parser.h"
#include "../htmllink.h"
#include "../dynamic/dynamiccontentpostlist.h"
#include "../dynamic/dynamiccontentincludepage.h"
#include "../dynamic/dynamiccontentsetvar.h"
#include "../dynamic/dynamiccontentgetvar.h"
2022-08-20 11:23:27 +02:00
#include "../revisionrenderer.h"
2018-11-03 17:12:20 +01:00
bool HandlerPageView::canAccess(std::string page)
{
return effectivePermissions(page).canRead();
}
std::string HandlerPageView::accessErrorMessage()
{
return "You don't have permission to view this page";
2018-11-03 17:12:20 +01:00
}
std::string HandlerPageView::createIndexContent(IParser &parser, std::string content)
{
std::vector<Headline> headlines = parser.extractHeadlines(content);
std::string indexcontent = "";
2019-08-26 20:51:59 +02:00
unsigned int previous = 0;
2018-11-03 17:12:20 +01:00
for(const Headline &h : headlines)
{
2019-08-26 20:51:59 +02:00
if(h.level > previous)
2018-11-03 17:12:20 +01:00
{
indexcontent += "<ul>";
}
2019-08-26 20:51:59 +02:00
else if(h.level < previous)
2018-11-03 17:12:20 +01:00
{
2019-08-26 20:51:59 +02:00
unsigned int diff = previous - h.level;
for(unsigned int i = 0; i < diff; i++)
{
indexcontent += "</ul>";
}
2018-11-03 17:12:20 +01:00
}
2019-08-26 20:51:59 +02:00
previous = h.level;
2018-11-03 17:12:20 +01:00
HtmlLink link;
link.href = "#" + utils::strreplace(h.title, " ", "");
2018-11-03 17:12:20 +01:00
link.innervalue = h.title;
link.cssclass = "indexlink";
indexcontent += "<li>" + link.render() + "</li>";
}
2019-08-26 20:51:59 +02:00
for(unsigned int i = 0; i < previous; i++)
{
indexcontent += "</ul>";
}
2018-11-03 17:12:20 +01:00
return indexcontent;
}
Response HandlerPageView::handleRequest(PageDao &pageDao, std::string pagename, const Request &r)
{
std::string revisionparam = r.get("revision");
unsigned int revisionid = 0;
if(!revisionparam.empty())
{
try
{
revisionid = utils::toUInt(revisionparam);
}
catch(const std::exception &e)
{
return errorResponse("Error", "Supplied revisionid is misformated");
}
}
std::optional<Revision> revision;
std::string templatepartname;
auto revisionDao = this->database->createRevisionDao();
2018-11-03 17:12:20 +01:00
try
{
if(revisionid > 0)
{
if(!effectivePermissions(pagename).canSeePageHistory())
{
auto current = revisionDao->getCurrentForPage(pagename);
if(current && current->revision > revisionid)
{
return errorResponse("Error", "You are not allowed to view older revisions of this page");
}
}
revision = revisionDao->getRevisionForPage(pagename, revisionid);
2018-11-03 17:12:20 +01:00
if(!revision)
{
return errorResponse("Revision not found", "No such revision found");
}
templatepartname = "page_view_revision";
}
else
{
if(!this->userSession->loggedIn)
{
auto content = this->cache->get("page:foranon:" + pagename);
if(content)
{
Response r;
r.setBody(*content);
// TODO: etag?
return r;
}
}
revision = revisionDao->getCurrentForPage(pagename);
2018-11-03 17:12:20 +01:00
templatepartname = "page_view";
}
}
catch(const DatabaseException &e)
{
Logger::error() << "DatabaseException in handlerpageview: " << e.what();
return errorResponse("Database error", "While trying to fetch revision, a database error occured");
}
Parser parser;
Response result;
result.setStatus(200);
RevisionRenderer revisionRenderer{*this->templ, *this->database, *this->urlProvider, *this->userSession};
std::string customtitle = parser.extractCommand("pagetitle", revision->content);
2022-08-20 11:23:27 +02:00
std::string parsedcontent = revisionRenderer.renderContent(revision.value(), customtitle);
/* TODO: Dynamic includes not considered, probably fine in practise */
std::string indexcontent = createIndexContent(parser, revision->content);
std::string revisionstr = std::to_string(revision->revision);
2022-08-20 11:23:27 +02:00
TemplatePage page = this->templ->getPage(templatepartname);
2018-11-03 17:12:20 +01:00
page.setVar("content", parsedcontent);
page.setVar("index", indexcontent);
page.setVar("editedby", revision->author);
page.setVar("editedon", utils::toISODateTime(revision->timestamp));
2018-11-03 17:12:20 +01:00
page.setVar("historyurl", this->urlProvider->pageHistory(pagename));
page.setVar("revision", revisionstr);
2018-11-03 17:12:20 +01:00
setPageVars(page, pagename);
if(!customtitle.empty())
{
page.setVar("title", createPageTitle(customtitle));
}
2018-11-03 17:12:20 +01:00
std::string body = page.render();
if(revisionid == 0 && !this->userSession->loggedIn)
{
this->cache->put("page:foranon:" + pagename, body);
}
result.addHeader("ETAG", revisionstr + "+" + std::to_string(this->userSession->loggedIn));
2018-11-03 17:12:20 +01:00
result.setBody(body);
return result;
}