Skip to content

Commit e5b247e

Browse files
Cherrglafriks
authored andcommitted
wiki - page revisions list (#7369)
fix #7 * add wiki page revision list * mobile improvements * css improvements for long usernames * split renderWikiPage into 3 functions Signed-off-by: Michael Gnehr <[email protected]>
1 parent d7211c5 commit e5b247e

File tree

7 files changed

+334
-64
lines changed

7 files changed

+334
-64
lines changed

Diff for: options/locale/locale_en-US.ini

+3
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,9 @@ wiki.save_page = Save Page
10341034
wiki.last_commit_info = %s edited this page %s
10351035
wiki.edit_page_button = Edit
10361036
wiki.new_page_button = New Page
1037+
wiki.file_revision = Page Revision
1038+
wiki.wiki_page_revisions = Wiki Page Revisions
1039+
wiki.back_to_wiki = Back to wiki page
10371040
wiki.delete_page_button = Delete Page
10381041
wiki.delete_page_notice_1 = Deleting the wiki page '%s' cannot be undone. Continue?
10391042
wiki.page_already_exists = A wiki page with the same name already exists.

Diff for: public/css/index.css

+5
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ footer .ui.left,footer .ui.right{line-height:40px}
290290
.markdown:not(code) .csv-data tr{border-top:0}
291291
.markdown:not(code) .csv-data th{font-weight:700;background:#f8f8f8;border-top:0}
292292
.markdown:not(code) .ui.list .list,.markdown:not(code) ol.ui.list ol,.markdown:not(code) ul.ui.list ul{padding-left:2em}
293+
.repository.wiki.revisions .ui.container>.ui.stackable.grid{flex-direction:row-reverse}
294+
.repository.wiki.revisions .ui.container>.ui.stackable.grid>.header{margin-top:0}
295+
.repository.wiki.revisions .ui.container>.ui.stackable.grid>.header .sub.header{padding-left:52px}
296+
.file-revisions-btn{display:block;float:left;margin-bottom:2px!important;padding:11px!important;margin-right:10px!important}
297+
.file-revisions-btn i{-webkit-touch-callout:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}
293298
.home .logo{max-width:220px}
294299
@media only screen and (max-width:767px){.home .hero h1{font-size:3.5em}
295300
.home .hero h2{font-size:2em}

Diff for: public/less/_markdown.less

+32
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,35 @@
494494
padding-left: 2em;
495495
}
496496
}
497+
498+
.repository.wiki.revisions {
499+
.ui.container > .ui.stackable.grid {
500+
-ms-flex-direction: row-reverse;
501+
flex-direction: row-reverse;
502+
503+
> .header {
504+
margin-top: 0;
505+
506+
.sub.header {
507+
padding-left: 52px;
508+
}
509+
}
510+
}
511+
}
512+
513+
.file-revisions-btn {
514+
display: block;
515+
float: left;
516+
margin-bottom: 2px !important;
517+
padding: 11px !important;
518+
margin-right: 10px !important;
519+
520+
i {
521+
-webkit-touch-callout: none;
522+
-webkit-user-select: none;
523+
-khtml-user-select: none;
524+
-moz-user-select: none;
525+
-ms-user-select: none;
526+
user-select: none;
527+
}
528+
}

Diff for: routers/repo/wiki.go

+188-64
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323
)
2424

2525
const (
26-
tplWikiStart base.TplName = "repo/wiki/start"
27-
tplWikiView base.TplName = "repo/wiki/view"
28-
tplWikiNew base.TplName = "repo/wiki/new"
29-
tplWikiPages base.TplName = "repo/wiki/pages"
26+
tplWikiStart base.TplName = "repo/wiki/start"
27+
tplWikiView base.TplName = "repo/wiki/view"
28+
tplWikiRevision base.TplName = "repo/wiki/revision"
29+
tplWikiNew base.TplName = "repo/wiki/new"
30+
tplWikiPages base.TplName = "repo/wiki/pages"
3031
)
3132

3233
// MustEnableWiki check if wiki is enabled, if external then redirect
@@ -107,18 +108,20 @@ func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
107108

108109
// wikiContentsByName returns the contents of a wiki page, along with a boolean
109110
// indicating whether the page exists. Writes to ctx if an error occurs.
110-
func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName string) ([]byte, bool) {
111-
entry, err := findEntryForFile(commit, models.WikiNameToFilename(wikiName))
112-
if err != nil {
111+
func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName string) ([]byte, *git.TreeEntry, string, bool) {
112+
var entry *git.TreeEntry
113+
var err error
114+
pageFilename := models.WikiNameToFilename(wikiName)
115+
if entry, err = findEntryForFile(commit, pageFilename); err != nil {
113116
ctx.ServerError("findEntryForFile", err)
114-
return nil, false
117+
return nil, nil, "", false
115118
} else if entry == nil {
116-
return nil, false
119+
return nil, nil, "", true
117120
}
118-
return wikiContentsByEntry(ctx, entry), true
121+
return wikiContentsByEntry(ctx, entry), entry, pageFilename, false
119122
}
120123

121-
func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, *git.TreeEntry) {
124+
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
122125
wikiRepo, commit, err := findWikiRepoCommit(ctx)
123126
if err != nil {
124127
if !git.IsErrNotExist(err) {
@@ -128,88 +131,176 @@ func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, *gi
128131
}
129132

130133
// Get page list.
131-
if isViewPage {
132-
entries, err := commit.ListEntries()
133-
if err != nil {
134-
ctx.ServerError("ListEntries", err)
135-
return nil, nil
134+
entries, err := commit.ListEntries()
135+
if err != nil {
136+
ctx.ServerError("ListEntries", err)
137+
return nil, nil
138+
}
139+
pages := make([]PageMeta, 0, len(entries))
140+
for _, entry := range entries {
141+
if !entry.IsRegular() {
142+
continue
136143
}
137-
pages := make([]PageMeta, 0, len(entries))
138-
for _, entry := range entries {
139-
if !entry.IsRegular() {
140-
continue
141-
}
142-
wikiName, err := models.WikiFilenameToName(entry.Name())
143-
if err != nil {
144-
if models.IsErrWikiInvalidFileName(err) {
145-
continue
146-
}
147-
ctx.ServerError("WikiFilenameToName", err)
148-
return nil, nil
149-
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
144+
wikiName, err := models.WikiFilenameToName(entry.Name())
145+
if err != nil {
146+
if models.IsErrWikiInvalidFileName(err) {
150147
continue
151148
}
152-
pages = append(pages, PageMeta{
153-
Name: wikiName,
154-
SubURL: models.WikiNameToSubURL(wikiName),
155-
})
149+
ctx.ServerError("WikiFilenameToName", err)
150+
return nil, nil
151+
} else if wikiName == "_Sidebar" || wikiName == "_Footer" {
152+
continue
156153
}
157-
ctx.Data["Pages"] = pages
154+
pages = append(pages, PageMeta{
155+
Name: wikiName,
156+
SubURL: models.WikiNameToSubURL(wikiName),
157+
})
158158
}
159+
ctx.Data["Pages"] = pages
159160

161+
// get requested pagename
160162
pageName := models.NormalizeWikiName(ctx.Params(":page"))
161163
if len(pageName) == 0 {
162164
pageName = "Home"
163165
}
164166
ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
165-
166167
ctx.Data["old_title"] = pageName
167168
ctx.Data["Title"] = pageName
168169
ctx.Data["title"] = pageName
169170
ctx.Data["RequireHighlightJS"] = true
170171

171-
pageFilename := models.WikiNameToFilename(pageName)
172-
var entry *git.TreeEntry
173-
if entry, err = findEntryForFile(commit, pageFilename); err != nil {
174-
ctx.ServerError("findEntryForFile", err)
175-
return nil, nil
176-
} else if entry == nil {
172+
//lookup filename in wiki - get filecontent, gitTree entry , real filename
173+
data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)
174+
if noEntry {
177175
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
176+
}
177+
if entry == nil || ctx.Written() {
178178
return nil, nil
179179
}
180-
data := wikiContentsByEntry(ctx, entry)
180+
181+
sidebarContent, _, _, _ := wikiContentsByName(ctx, commit, "_Sidebar")
181182
if ctx.Written() {
182183
return nil, nil
183184
}
184185

185-
if isViewPage {
186-
sidebarContent, sidebarPresent := wikiContentsByName(ctx, commit, "_Sidebar")
187-
if ctx.Written() {
188-
return nil, nil
189-
}
186+
footerContent, _, _, _ := wikiContentsByName(ctx, commit, "_Footer")
187+
if ctx.Written() {
188+
return nil, nil
189+
}
190190

191-
footerContent, footerPresent := wikiContentsByName(ctx, commit, "_Footer")
192-
if ctx.Written() {
193-
return nil, nil
191+
metas := ctx.Repo.Repository.ComposeMetas()
192+
ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
193+
ctx.Data["sidebarPresent"] = sidebarContent != nil
194+
ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
195+
ctx.Data["footerPresent"] = footerContent != nil
196+
ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
197+
198+
// get commit count - wiki revisions
199+
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
200+
ctx.Data["CommitCount"] = commitsCount
201+
202+
return wikiRepo, entry
203+
}
204+
205+
func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
206+
wikiRepo, commit, err := findWikiRepoCommit(ctx)
207+
if err != nil {
208+
if !git.IsErrNotExist(err) {
209+
ctx.ServerError("GetBranchCommit", err)
194210
}
211+
return nil, nil
212+
}
213+
214+
// get requested pagename
215+
pageName := models.NormalizeWikiName(ctx.Params(":page"))
216+
if len(pageName) == 0 {
217+
pageName = "Home"
218+
}
219+
ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
220+
ctx.Data["old_title"] = pageName
221+
ctx.Data["Title"] = pageName
222+
ctx.Data["title"] = pageName
223+
ctx.Data["RequireHighlightJS"] = true
195224

196-
metas := ctx.Repo.Repository.ComposeMetas()
197-
ctx.Data["content"] = markdown.RenderWiki(data, ctx.Repo.RepoLink, metas)
198-
ctx.Data["sidebarPresent"] = sidebarPresent
199-
ctx.Data["sidebarContent"] = markdown.RenderWiki(sidebarContent, ctx.Repo.RepoLink, metas)
200-
ctx.Data["footerPresent"] = footerPresent
201-
ctx.Data["footerContent"] = markdown.RenderWiki(footerContent, ctx.Repo.RepoLink, metas)
202-
} else {
203-
ctx.Data["content"] = string(data)
204-
ctx.Data["sidebarPresent"] = false
205-
ctx.Data["sidebarContent"] = ""
206-
ctx.Data["footerPresent"] = false
207-
ctx.Data["footerContent"] = ""
225+
//lookup filename in wiki - get filecontent, gitTree entry , real filename
226+
data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)
227+
if noEntry {
228+
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
229+
}
230+
if entry == nil || ctx.Written() {
231+
return nil, nil
208232
}
209233

234+
ctx.Data["content"] = string(data)
235+
ctx.Data["sidebarPresent"] = false
236+
ctx.Data["sidebarContent"] = ""
237+
ctx.Data["footerPresent"] = false
238+
ctx.Data["footerContent"] = ""
239+
240+
// get commit count - wiki revisions
241+
commitsCount, _ := wikiRepo.FileCommitsCount("master", pageFilename)
242+
ctx.Data["CommitCount"] = commitsCount
243+
244+
// get page
245+
page := ctx.QueryInt("page")
246+
if page <= 1 {
247+
page = 1
248+
}
249+
250+
// get Commit Count
251+
commitsHistory, err := wikiRepo.CommitsByFileAndRange("master", pageFilename, page)
252+
if err != nil {
253+
ctx.ServerError("CommitsByFileAndRange", err)
254+
return nil, nil
255+
}
256+
commitsHistory = models.ValidateCommitsWithEmails(commitsHistory)
257+
commitsHistory = models.ParseCommitsWithSignature(commitsHistory)
258+
259+
ctx.Data["Commits"] = commitsHistory
260+
261+
pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
262+
pager.SetDefaultParams(ctx)
263+
ctx.Data["Page"] = pager
264+
210265
return wikiRepo, entry
211266
}
212267

268+
func renderEditPage(ctx *context.Context) {
269+
_, commit, err := findWikiRepoCommit(ctx)
270+
if err != nil {
271+
if !git.IsErrNotExist(err) {
272+
ctx.ServerError("GetBranchCommit", err)
273+
}
274+
return
275+
}
276+
277+
// get requested pagename
278+
pageName := models.NormalizeWikiName(ctx.Params(":page"))
279+
if len(pageName) == 0 {
280+
pageName = "Home"
281+
}
282+
ctx.Data["PageURL"] = models.WikiNameToSubURL(pageName)
283+
ctx.Data["old_title"] = pageName
284+
ctx.Data["Title"] = pageName
285+
ctx.Data["title"] = pageName
286+
ctx.Data["RequireHighlightJS"] = true
287+
288+
//lookup filename in wiki - get filecontent, gitTree entry , real filename
289+
data, entry, _, noEntry := wikiContentsByName(ctx, commit, pageName)
290+
if noEntry {
291+
ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages")
292+
}
293+
if entry == nil || ctx.Written() {
294+
return
295+
}
296+
297+
ctx.Data["content"] = string(data)
298+
ctx.Data["sidebarPresent"] = false
299+
ctx.Data["sidebarContent"] = ""
300+
ctx.Data["footerPresent"] = false
301+
ctx.Data["footerContent"] = ""
302+
}
303+
213304
// Wiki renders single wiki page
214305
func Wiki(ctx *context.Context) {
215306
ctx.Data["PageIsWiki"] = true
@@ -221,7 +312,7 @@ func Wiki(ctx *context.Context) {
221312
return
222313
}
223314

224-
wikiRepo, entry := renderWikiPage(ctx, true)
315+
wikiRepo, entry := renderViewPage(ctx)
225316
if ctx.Written() {
226317
return
227318
}
@@ -247,6 +338,39 @@ func Wiki(ctx *context.Context) {
247338
ctx.HTML(200, tplWikiView)
248339
}
249340

341+
// WikiRevision renders file revision list of wiki page
342+
func WikiRevision(ctx *context.Context) {
343+
ctx.Data["PageIsWiki"] = true
344+
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(models.UnitTypeWiki) && !ctx.Repo.Repository.IsArchived
345+
346+
if !ctx.Repo.Repository.HasWiki() {
347+
ctx.Data["Title"] = ctx.Tr("repo.wiki")
348+
ctx.HTML(200, tplWikiStart)
349+
return
350+
}
351+
352+
wikiRepo, entry := renderRevisionPage(ctx)
353+
if ctx.Written() {
354+
return
355+
}
356+
if entry == nil {
357+
ctx.Data["Title"] = ctx.Tr("repo.wiki")
358+
ctx.HTML(200, tplWikiStart)
359+
return
360+
}
361+
362+
// Get last change information.
363+
wikiPath := entry.Name()
364+
lastCommit, err := wikiRepo.GetCommitByPath(wikiPath)
365+
if err != nil {
366+
ctx.ServerError("GetCommitByPath", err)
367+
return
368+
}
369+
ctx.Data["Author"] = lastCommit.Author
370+
371+
ctx.HTML(200, tplWikiRevision)
372+
}
373+
250374
// WikiPages render wiki pages list page
251375
func WikiPages(ctx *context.Context) {
252376
if !ctx.Repo.Repository.HasWiki() {
@@ -399,7 +523,7 @@ func EditWiki(ctx *context.Context) {
399523
return
400524
}
401525

402-
renderWikiPage(ctx, false)
526+
renderEditPage(ctx)
403527
if ctx.Written() {
404528
return
405529
}

Diff for: routers/routes/routes.go

+1
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ func RegisterRoutes(m *macaron.Macaron) {
804804
m.Group("/wiki", func() {
805805
m.Get("/?:page", repo.Wiki)
806806
m.Get("/_pages", repo.WikiPages)
807+
m.Get("/:page/_revision", repo.WikiRevision)
807808

808809
m.Group("", func() {
809810
m.Combo("/_new").Get(repo.NewWiki).

0 commit comments

Comments
 (0)