2014-07-30 10:44:03 -04:00
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
ErrorController = require "../Errors/ErrorController"
|
2014-10-31 08:15:03 -04:00
|
|
|
_ = require("underscore")
|
2015-02-24 17:07:36 -05:00
|
|
|
AuthenticationController = require("../Authentication/AuthenticationController")
|
2014-10-31 08:15:03 -04:00
|
|
|
|
|
|
|
other_lngs = ["es"]
|
2014-07-30 10:44:03 -04:00
|
|
|
|
|
|
|
module.exports = WikiController =
|
|
|
|
|
2015-02-24 17:07:36 -05:00
|
|
|
|
|
|
|
_checkIfLoginIsNeeded: (req, res, next)->
|
|
|
|
if settings.apis.wiki.requireLogin
|
|
|
|
AuthenticationController.requireLogin()(req, res, next)
|
2014-10-31 08:15:03 -04:00
|
|
|
else
|
2015-02-24 17:07:36 -05:00
|
|
|
next()
|
|
|
|
|
|
|
|
getPage: (req, res, next) ->
|
|
|
|
WikiController._checkIfLoginIsNeeded req, res, ->
|
|
|
|
|
|
|
|
page = req.url.replace(/^\/learn/, "").replace(/^\//, "")
|
|
|
|
if page == ""
|
|
|
|
page = "Main_Page"
|
|
|
|
|
|
|
|
logger.log page: page, "getting page from wiki"
|
|
|
|
if _.include(other_lngs, req.lng)
|
|
|
|
lngPage = "#{page}_#{req.lng}"
|
|
|
|
else
|
|
|
|
lngPage = page
|
2014-10-31 08:15:03 -04:00
|
|
|
|
2015-02-24 17:07:36 -05:00
|
|
|
WikiController._getPageContent "Contents", (error, contents) ->
|
2014-07-31 11:26:02 -04:00
|
|
|
return next(error) if error?
|
2015-02-24 17:07:36 -05:00
|
|
|
WikiController._getPageContent lngPage, (error, pageData) ->
|
|
|
|
return next(error) if error?
|
|
|
|
if pageData.content?.length > 280
|
|
|
|
if _.include(other_lngs, req.lng)
|
|
|
|
pageData.title = pageData.title.slice(0, pageData.title.length - (req.lng.length+1) )
|
2014-10-31 08:15:03 -04:00
|
|
|
WikiController._renderPage(pageData, contents, res)
|
2015-02-24 17:07:36 -05:00
|
|
|
else
|
|
|
|
WikiController._getPageContent page, (error, pageData) ->
|
|
|
|
return next(error) if error?
|
|
|
|
WikiController._renderPage(pageData, contents, res)
|
2014-10-31 08:15:03 -04:00
|
|
|
|
|
|
|
|
2015-02-24 17:07:36 -05:00
|
|
|
|
2014-10-31 08:15:03 -04:00
|
|
|
|
2014-07-31 11:26:02 -04:00
|
|
|
_getPageContent: (page, callback = (error, data = { content: "", title: "" }) ->) ->
|
2014-07-30 10:44:03 -04:00
|
|
|
request {
|
2014-07-31 11:26:02 -04:00
|
|
|
url: "#{settings.apis.wiki.url}/learn-scripts/api.php"
|
2014-07-30 10:44:03 -04:00
|
|
|
qs: {
|
2014-07-31 08:12:24 -04:00
|
|
|
page: decodeURI(page)
|
2014-07-30 10:44:03 -04:00
|
|
|
action: "parse"
|
|
|
|
format: "json"
|
|
|
|
}
|
|
|
|
}, (err, response, data)->
|
2014-07-31 11:26:02 -04:00
|
|
|
return callback(err) if err?
|
2014-07-30 10:44:03 -04:00
|
|
|
try
|
|
|
|
data = JSON.parse(data)
|
|
|
|
catch err
|
|
|
|
logger.err err:err, data:data, "error parsing data from wiki"
|
2014-10-31 08:15:03 -04:00
|
|
|
result =
|
2014-07-31 11:26:02 -04:00
|
|
|
content: data?.parse?.text?['*']
|
|
|
|
title: data?.parse?.title
|
2014-10-31 08:15:03 -04:00
|
|
|
|
|
|
|
callback null, result
|
|
|
|
|
|
|
|
|
|
|
|
_renderPage: (page, contents, res)->
|
|
|
|
if page.title == "Main Page"
|
|
|
|
title = "Documentation"
|
|
|
|
else
|
|
|
|
title = page.title
|
|
|
|
|
|
|
|
res.render "wiki/page", {
|
|
|
|
page: page
|
|
|
|
contents: contents
|
|
|
|
title: title
|
|
|
|
}
|