Class: Gollum::Wiki

Inherits:
Object
  • Object
show all
Includes:
Pagination
Defined in:
lib/gollum/wiki.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pagination

included, #log_pagination_options, #page_to_skip

Constructor Details

#initialize(path, options = {}) ⇒ Wiki

Public: Initialize a new Gollum Repo.

path - The String path to the Git repository that holds the Gollum

site.

options - Optional Hash:

:base_path     - String base path for all Wiki links.
                 Default: "/"
:page_class    - The page Class. Default: Gollum::Page
:file_class    - The file Class. Default: Gollum::File
:markup_class  - The markup Class. Default: Gollum::Markup
:sanitization  - An instance of Sanitization.
:page_file_dir - String the directory in which all page files reside
:ref - String the repository ref to retrieve pages from

Returns a fresh Gollum::Repo.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gollum/wiki.rb', line 122

def initialize(path, options = {})
  if path.is_a?(GitAccess)
    options[:access] = path
    path             = path.path
  end
  @path          = path
  @page_file_dir = options[:page_file_dir]
  @access        = options[:access]       || GitAccess.new(path, @page_file_dir)
  @base_path     = options[:base_path]    || "/"
  @page_class    = options[:page_class]   || self.class.page_class
  @file_class    = options[:file_class]   || self.class.file_class
  @markup_class  = options[:markup_class] || self.class.markup_class
  @repo          = @access.repo
  @ref           = options[:ref] || self.class.default_ref
  @sanitization  = options[:sanitization] || self.class.sanitization
  @history_sanitization = options[:history_sanitization] ||
    self.class.history_sanitization
end

Class Attribute Details

.default_committer_emailObject

Sets the default email for commits.



22
23
24
# File 'lib/gollum/wiki.rb', line 22

def default_committer_email
  @default_committer_email
end

.default_committer_nameObject

Sets the default name for commits.



19
20
21
# File 'lib/gollum/wiki.rb', line 19

def default_committer_name
  @default_committer_name
end

.default_refObject

Sets the default ref for the wiki.



16
17
18
# File 'lib/gollum/wiki.rb', line 16

def default_ref
  @default_ref
end

.file_classObject

Gets the file class used by all instances of this Wiki. Default: Gollum::File.



45
46
47
48
49
50
51
52
# File 'lib/gollum/wiki.rb', line 45

def file_class
  @file_class ||
    if superclass.respond_to?(:file_class)
      superclass.file_class
    else
      ::Gollum::File
    end
end

.history_sanitizationObject

Gets the default sanitization options for older page revisions used by instances of this Wiki.



76
77
78
79
80
81
82
83
# File 'lib/gollum/wiki.rb', line 76

def history_sanitization
  if @history_sanitization.nil?
    @history_sanitization = sanitization ?
      sanitization.history_sanitization  :
      false
  end
  @history_sanitization
end

.markup_classObject

Gets the markup class used by all instances of this Wiki. Default: Gollum::Markup



56
57
58
59
60
61
62
63
# File 'lib/gollum/wiki.rb', line 56

def markup_class
  @markup_class ||
    if superclass.respond_to?(:markup_class)
      superclass.markup_class
    else
      ::Gollum::Markup
    end
end

.page_classObject

Gets the page class used by all instances of this Wiki. Default: Gollum::Page.



34
35
36
37
38
39
40
41
# File 'lib/gollum/wiki.rb', line 34

def page_class
  @page_class ||
    if superclass.respond_to?(:page_class)
      superclass.page_class
    else
      ::Gollum::Page
    end
end

.sanitizationObject

Gets the default sanitization options for current pages used by instances of this Wiki.



67
68
69
70
71
72
# File 'lib/gollum/wiki.rb', line 67

def sanitization
  if @sanitization.nil?
    @sanitization = Sanitization.new
  end
  @sanitization
end

Instance Attribute Details

#base_pathObject (readonly)

The String base path to prefix to internal links. For example, when set to “/wiki”, the page “Hobbit” will be linked as “/wiki/Hobbit”. Defaults to “/”.



93
94
95
# File 'lib/gollum/wiki.rb', line 93

def base_path
  @base_path
end

#file_classObject (readonly)

Gets the file class used by all instances of this Wiki.



492
493
494
# File 'lib/gollum/wiki.rb', line 492

def file_class
  @file_class
end

#history_sanitizationObject (readonly)

Gets the sanitization options for older page revisions used by this Wiki.



99
100
101
# File 'lib/gollum/wiki.rb', line 99

def history_sanitization
  @history_sanitization
end

#markup_classObject (readonly)

Gets the markup class used by all instances of this Wiki.



495
496
497
# File 'lib/gollum/wiki.rb', line 495

def markup_class
  @markup_class
end

#page_classObject (readonly)

Gets the page class used by all instances of this Wiki.



489
490
491
# File 'lib/gollum/wiki.rb', line 489

def page_class
  @page_class
end

#page_file_dirObject (readonly)

Gets the String directory in which all page files reside.



105
106
107
# File 'lib/gollum/wiki.rb', line 105

def page_file_dir
  @page_file_dir
end

#pathObject (readonly)

The String path to the Git repository that holds the Gollum site.

Returns the String path.



486
487
488
# File 'lib/gollum/wiki.rb', line 486

def path
  @path
end

#refObject (readonly)

Gets the String ref in which all page files reside.



102
103
104
# File 'lib/gollum/wiki.rb', line 102

def ref
  @ref
end

#repoObject (readonly)

The Grit::Repo associated with the wiki.

Returns the Grit::Repo.



481
482
483
# File 'lib/gollum/wiki.rb', line 481

def repo
  @repo
end

#sanitizationObject (readonly)

Gets the sanitization options for current pages used by this Wiki.



96
97
98
# File 'lib/gollum/wiki.rb', line 96

def sanitization
  @sanitization
end

Instance Method Details

#clear_cacheObject

Public: Refreshes just the cached Git reference data. This should be called after every Gollum update.

Returns nothing.



448
449
450
# File 'lib/gollum/wiki.rb', line 448

def clear_cache
  @access.refresh
end

#commit_for(ref) ⇒ Object

Gets the commit object for the given ref or sha.

ref - A string ref or SHA pointing to a valid commit.

Returns a Grit::Commit instance.



580
581
582
583
# File 'lib/gollum/wiki.rb', line 580

def commit_for(ref)
  @access.commit(ref)
rescue Grit::GitRuby::Repository::NoSuchShaFound
end

#default_committer_emailObject

Gets the default email for commits.

Returns the String email address.



570
571
572
573
# File 'lib/gollum/wiki.rb', line 570

def default_committer_email
  @default_committer_email ||= \
    @repo.config['user.email'] || self.class.default_committer_email
end

#default_committer_nameObject

Gets the default name for commits.

Returns the String name.



562
563
564
565
# File 'lib/gollum/wiki.rb', line 562

def default_committer_name
  @default_committer_name ||= \
    @repo.config['user.name'] || self.class.default_committer_name
end

#delete_page(page, commit) ⇒ Object

Public: Delete a page.

page - The Gollum::Page to delete. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Grit::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of 
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/gollum/wiki.rb', line 293

def delete_page(page, commit)
  multi_commit = false

  committer = if obj = commit[:committer]
    multi_commit = true
    obj
  else
    Committer.new(self, commit)
  end

  committer.delete(page.path)

  committer.after_commit do |index, sha|
    dir = ::File.dirname(page.path)
    dir = '' if dir == '.'

    @access.refresh
    index.update_working_dir(dir, page.name, page.format)
  end

  multi_commit ? committer : committer.commit
end

#exist?Boolean

Public: check whether the wiki’s git repo exists on the filesystem.

Returns true if the repo exists, and false if it does not.

Returns:

  • (Boolean)


144
145
146
# File 'lib/gollum/wiki.rb', line 144

def exist?
  @access.exist?
end

#file(name, version = @ref) ⇒ Object

Public: Get the static file for a given name.

name - The full String pathname to the file. version - The String version ID to find (default: @ref).

Returns a Gollum::File or nil if no matching file was found.



164
165
166
# File 'lib/gollum/wiki.rb', line 164

def file(name, version = @ref)
  @file_class.new(self).find(name, version)
end

#full_reverse_diff(sha1, sha2 = nil) ⇒ Object

Creates a reverse diff for the given SHAs.

sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child.

Returns a String of the reverse Diff to apply.



555
556
557
# File 'lib/gollum/wiki.rb', line 555

def full_reverse_diff(sha1, sha2 = nil)
  full_reverse_diff_for(nil, sha1, sha2)
end

#full_reverse_diff_for(page, sha1, sha2 = nil) ⇒ Object

Creates a reverse diff for the given SHAs on the given Gollum::Page.

page - The Gollum::Page to scope the patch to, or a String Path. sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child.

Returns a String of the reverse Diff to apply.



539
540
541
542
543
544
545
546
# File 'lib/gollum/wiki.rb', line 539

def full_reverse_diff_for(page, sha1, sha2 = nil)
  sha1, sha2 = "#{sha1}^", sha1 if sha2.nil?
  args = [{:R => true}, sha1, sha2]
  if page
    args << '--' << (page.respond_to?(:path) ? page.path : page.to_s)
  end
  repo.git.native(:diff, *args)
end

#history_sanitizerObject

Public: Creates a Sanitize instance using the Wiki’s history sanitization options.

Returns a Sanitize instance.



466
467
468
469
470
# File 'lib/gollum/wiki.rb', line 466

def history_sanitizer
  if options = history_sanitization
    @history_sanitizer ||= options.to_sanitize
  end
end

#log(options = {}) ⇒ Object

Public: All of the versions that have touched the Page.

options - The options Hash:

:page     - The Integer page number (default: 1).
:per_page - The Integer max count of items to return.

Returns an Array of Grit::Commit.



440
441
442
# File 'lib/gollum/wiki.rb', line 440

def log(options = {})
  @repo.log(@ref, nil, log_pagination_options(options))
end

#normalize(data) ⇒ Object

Normalize the data.

data - The String data to be normalized.

Returns the normalized data String.



502
503
504
# File 'lib/gollum/wiki.rb', line 502

def normalize(data)
  data.gsub(/\r/, '')
end

#page(name, version = @ref) ⇒ Object

Public: Get the formatted page for a given page name.

name - The human or canonical String page name of the wiki page. version - The String version ID to find (default: @ref).

Returns a Gollum::Page or nil if no matching page was found.



154
155
156
# File 'lib/gollum/wiki.rb', line 154

def page(name, version = @ref)
  @page_class.new(self).find(name, version)
end

#page_file_name(name, format) ⇒ Object

Assemble a Page’s filename from its name and format.

name - The String name of the page (may be in human format). format - The Symbol format of the page.

Returns the String filename.



512
513
514
515
# File 'lib/gollum/wiki.rb', line 512

def page_file_name(name, format)
  ext = @page_class.format_to_ext(format)
  @page_class.cname(name) + '.' + ext
end

#pages(treeish = nil) ⇒ Object

Public: Lists all pages for this wiki.

treeish - The String commit ID or ref to find (default: @ref)

Returns an Array of Gollum::Page instances.



396
397
398
# File 'lib/gollum/wiki.rb', line 396

def pages(treeish = nil)
  tree_list(treeish || @ref)
end

#preview_page(name, data, format) ⇒ Object

Public: Create an in-memory Page with the given data and format. This is useful for previewing what content will look like before committing it to the repository.

name - The String name of the page. format - The Symbol format of the page. data - The new String contents of the page.

Returns the in-memory Gollum::Page.



177
178
179
180
181
182
183
184
185
# File 'lib/gollum/wiki.rb', line 177

def preview_page(name, data, format)
  page = @page_class.new(self)
  ext  = @page_class.format_to_ext(format.to_sym)
  name = @page_class.cname(name) + '.' + ext
  blob = OpenStruct.new(:name => name, :data => data)
  page.populate(blob)
  page.version = @access.commit('master')
  page
end

#revert_commit(sha1, sha2 = nil, commit = {}) ⇒ Object

Public: Applies a reverse diff to the repo. If only 1 SHA is given, the reverse diff will be taken from its parent (^SHA…SHA). If two SHAs are given, the reverse diff is taken from SHA1…SHA2.

sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child. commit - The commit Hash details:

:message - The String commit message.
:name    - The String author full name.
:email   - The String email address.

Returns a String SHA1 of the new commit, or nil if the reverse diff does not apply.



387
388
389
# File 'lib/gollum/wiki.rb', line 387

def revert_commit(sha1, sha2 = nil, commit = {})
  revert_page(nil, sha1, sha2, commit)
end

#revert_page(page, sha1, sha2 = nil, commit = {}) ⇒ Object

Public: Applies a reverse diff for a given page. If only 1 SHA is given, the reverse diff will be taken from its parent (^SHA…SHA). If two SHAs are given, the reverse diff is taken from SHA1…SHA2.

page - The Gollum::Page to delete. sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child. commit - The commit Hash details:

:message - The String commit message.
:name    - The String author full name.
:email   - The String email address.
:parent  - Optional Grit::Commit parent to this update.

Returns a String SHA1 of the new commit, or nil if the reverse diff does not apply.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/gollum/wiki.rb', line 332

def revert_page(page, sha1, sha2 = nil, commit = {})
  if sha2.is_a?(Hash)
    commit = sha2
    sha2   = nil
  end

  patch     = full_reverse_diff_for(page, sha1, sha2)
  committer = Committer.new(self, commit)
  parent    = committer.parents[0]
  committer.options[:tree] = @repo.git.apply_patch(parent.sha, patch)
  return false unless committer.options[:tree]
  committer.after_commit do |index, sha|
    @access.refresh

    files = []
    if page
      files << [page.path, page.name, page.format]
    else
      # Grit::Diff can't parse reverse diffs.... yet
      patch.each_line do |line|
        if line =~ %r{^diff --git b/.+? a/(.+)$}
          path = $1
          ext  = ::File.extname(path)
          name = ::File.basename(path, ext)
          if format = ::Gollum::Page.format_for(ext)
            files << [path, name, format]
          end
        end
      end
    end

    files.each do |(path, name, format)|
      dir = ::File.dirname(path)
      dir = '' if dir == '.'
      index.update_working_dir(dir, name, format)
    end
  end

  committer.commit
end

#sanitizerObject

Public: Creates a Sanitize instance using the Wiki’s sanitization options.

Returns a Sanitize instance.



456
457
458
459
460
# File 'lib/gollum/wiki.rb', line 456

def sanitizer
  if options = sanitization
    @sanitizer ||= options.to_sanitize
  end
end

#search(query) ⇒ Object

Public: Search all pages for this wiki.

query - The string to search for

Returns an Array with Objects of page name and count of matches



418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/gollum/wiki.rb', line 418

def search(query)
  args = [{}, '-i', '-c', query, @ref, '--']
  args << '--' << @page_file_dir if @page_file_dir

  @repo.git.grep(*args).split("\n").map! do |line|
    result = line.split(':')
    file_name = Gollum::Page.canonicalize_filename(::File.basename(result[1]))

    {
      :count  => result[2].to_i,
      :name   => file_name
    }
  end
end

#size(ref = nil) ⇒ Object

Public: Returns the number of pages accessible from a commit

ref - A String ref that is either a commit SHA or references one.

Returns a Fixnum



405
406
407
408
409
410
411
# File 'lib/gollum/wiki.rb', line 405

def size(ref = nil)
  tree_map_for(ref || @ref).inject(0) do |num, entry|
    num + (@page_class.valid_page_name?(entry.name) ? 1 : 0)
  end
rescue Grit::GitRuby::Repository::NoSuchShaFound
  0
end

#tree_list(ref) ⇒ Object

Fill an array with a list of pages.

ref - A String ref that is either a commit SHA or references one.

Returns a flat Array of Gollum::Page instances.



522
523
524
525
526
527
528
529
# File 'lib/gollum/wiki.rb', line 522

def tree_list(ref)
  sha    = @access.ref_to_sha(ref)
  commit = @access.commit(sha)
  tree_map_for(sha).inject([]) do |list, entry|
    next list unless @page_class.valid_page_name?(entry.name)
    list << entry.page(self, commit)
  end
end

#tree_map_for(ref) ⇒ Object

Finds a full listing of files and their blob SHA for a given ref. Each listing is cached based on its actual commit SHA.

ref - A String ref that is either a commit SHA or references one.

Returns an Array of BlobEntry instances.



591
592
593
594
595
# File 'lib/gollum/wiki.rb', line 591

def tree_map_for(ref)
  @access.tree(ref)
rescue Grit::GitRuby::Repository::NoSuchShaFound
  []
end

#update_page(page, name, format, data, commit = {}) ⇒ Object

Public: Update an existing page with new content. The location of the page inside the repository will not change. If the given format is different than the current format of the page, the filename will be changed to reflect the new format.

page - The Gollum::Page to update. name - The String extension-less name of the page. format - The Symbol format of the page. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Grit::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of 
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/gollum/wiki.rb', line 247

def update_page(page, name, format, data, commit = {})
  name   ||= page.name
  format ||= page.format
  dir      = ::File.dirname(page.path)
  dir      = '' if dir == '.'
  multi_commit = false

  committer = if obj = commit[:committer]
    multi_commit = true
    obj
  else
    Committer.new(self, commit)
  end

  if page.name == name && page.format == format
    committer.add(page.path, normalize(data))
  else
    committer.delete(page.path)
    committer.add_to_index(dir, name, format, data, :allow_same_ext)
  end

  committer.after_commit do |index, sha|
    @access.refresh
    index.update_working_dir(dir, page.name, page.format)
    index.update_working_dir(dir, name, format)
  end

  multi_commit ? committer : committer.commit
end

#write_page(name, format, data, commit = {}) ⇒ Object

Public: Write a new version of a page to the Gollum repo root.

name - The String name of the page. format - The Symbol format of the page. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Grit::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of 
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/gollum/wiki.rb', line 205

def write_page(name, format, data, commit = {})
  multi_commit = false

  committer = if obj = commit[:committer]
    multi_commit = true
    obj
  else
    Committer.new(self, commit)
  end

  committer.add_to_index('', name, format, data)

  committer.after_commit do |index, sha|
    @access.refresh
    index.update_working_dir('', name, format)
  end

  multi_commit ? committer : committer.commit
end