2017-07-12 06:43:11 +00:00
package yatwiki
2017-07-09 01:26:26 +00:00
import (
"html/template"
"log"
"net/http"
"net/url"
2017-07-09 06:05:03 +00:00
"time"
2017-07-09 01:26:26 +00:00
)
2017-08-13 06:25:58 +00:00
func ( this * WikiServer ) serveErrorMessage ( w http . ResponseWriter , err error ) {
this . serveErrorText ( w , err . Error ( ) )
2017-07-09 01:26:26 +00:00
}
func ( this * WikiServer ) serveInternalError ( w http . ResponseWriter , r * http . Request , e error ) {
log . Printf ( "Internal error from %s while accessing %s(%s): %s" , r . RemoteAddr , r . Method , r . URL . Path , e . Error ( ) )
http . Error ( w , "An internal error occurred. Please ask an administrator to check the log file." , 500 )
}
2018-04-02 05:08:54 +00:00
func ( this * WikiServer ) serveDeleted ( w http . ResponseWriter , lookingFor string ) {
this . serveRedirect ( w , this . opts . ExternalBaseURL + "history/" + url . PathEscape ( lookingFor ) + "?error=" + url . QueryEscape ( ` The page you are looking for has been deleted. However, the history is still available. ` ) )
}
2017-08-13 06:25:58 +00:00
func ( this * WikiServer ) serveErrorText ( w http . ResponseWriter , msg string ) {
this . serveRedirect ( w , this . opts . ExpectBaseURL + "view/" + url . PathEscape ( this . opts . DefaultPage ) + "?error=" + url . QueryEscape ( msg ) )
}
func ( this * WikiServer ) serveNoSuchArticle ( w http . ResponseWriter , lookingFor string ) {
this . serveRedirect ( w , this . opts . ExpectBaseURL + "view/" + url . PathEscape ( this . opts . DefaultPage ) + "?notfound=" + url . QueryEscape ( lookingFor ) )
2017-07-09 01:26:26 +00:00
}
func ( this * WikiServer ) serveRedirect ( w http . ResponseWriter , location string ) {
w . Header ( ) . Set ( "Location" , location )
w . WriteHeader ( 302 ) // moved (not permanently)
}
func ( this * WikiServer ) servePageResponse ( w http . ResponseWriter , r * http . Request , pto * pageTemplateOptions ) {
w . WriteHeader ( 200 )
2017-08-13 06:25:58 +00:00
if noSuchArticleTarget , ok := r . URL . Query ( ) [ "notfound" ] ; ok {
pto . PageNotExistsError = true
pto . PageNotExistsTarget = noSuchArticleTarget [ 0 ]
} else {
pto . SessionMessage = r . URL . Query ( ) . Get ( "error" )
}
2017-07-09 01:26:26 +00:00
err := this . pageTmp . Execute ( w , pto )
if err != nil {
log . Println ( err . Error ( ) )
}
}
2017-07-09 06:05:03 +00:00
2017-11-18 02:30:29 +00:00
func ( this * WikiServer ) formatTimestamp ( m int64 ) template . HTML {
2017-07-09 06:05:03 +00:00
// TODO add a more detailed timestamp on hover
2017-11-18 02:30:29 +00:00
dt := time . Unix ( m , 0 ) . In ( this . loc )
return template . HTML ( ` <span title=" ` + template . HTMLEscapeString ( dt . Format ( time . RFC3339 ) ) + ` "> ` + template . HTMLEscapeString ( dt . Format ( this . opts . DateFormat ) ) + ` </span> ` )
2017-07-09 06:05:03 +00:00
}
2017-07-11 05:52:29 +00:00
func ( this * WikiServer ) viewLink ( articleTitle string ) template . HTML {
2017-08-13 05:51:44 +00:00
return template . HTML ( ` "<a href=" ` + template . HTMLEscapeString ( this . opts . ExpectBaseURL + ` view/ ` + url . PathEscape ( articleTitle ) ) + ` "> ` + template . HTMLEscapeString ( articleTitle ) + ` </a>" ` )
2017-07-11 05:52:29 +00:00
}