Class: WikiLib::PMWiki

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki_lib/pm_wiki.rb

Overview

Class for interacting with the PMWiki

Instance Method Summary collapse

Constructor Details

#initialize(base_page, username, password) ⇒ PMWiki

Initializes a new PMWiki instance

Parameters:

  • base_page (String)

    The base page of the wiki. Probably it’s address, but could be a subset of the namespace

  • username (String)

    Username used to sign off the edits

  • password (String)

    Password used to make edits



15
16
17
18
19
20
# File 'lib/wiki_lib/pm_wiki.rb', line 15

def initialize(base_page, username, password)
  @base = base_page
  @pass = password
  @user = username
  @agent = ::Mechanize.new
end

Instance Method Details

#delete_page(page, summary) ⇒ Object

Deletes a PMWiki page (sets text to delete)

Parameters:

  • page (String)

    The page to delete

  • summary (String)

    The reason for deletion



97
98
99
# File 'lib/wiki_lib/pm_wiki.rb', line 97

def delete_page(page, summary)
  upload_page(page, 'delete', summary)
end

#edit_url(page) ⇒ String

returns a PMWiki page edit url

Parameters:

  • page (String)

    The name of the page, relative to the base page

Returns:

  • (String)

    The URL



38
39
40
# File 'lib/wiki_lib/pm_wiki.rb', line 38

def edit_url(page)
"#{page_url(page)}?action=edit"
end

#get_edit_text(page) ⇒ String

Returns the edit text of a PMWiki page

Parameters:

  • page (String)

    The page to get the edit text for

Returns:

  • (String)

    The edit text



48
49
50
51
52
53
54
# File 'lib/wiki_lib/pm_wiki.rb', line 48

def get_edit_text(page)
  # get the form, logging in if nessessary
  form = (edit_url(page))

  # return the text
  form['text']
end

#get_page(page) ⇒ Hpricot::Doc

Returns the full page, parsed by hpricot

Parameters:

  • page (String)

    Page to retrieve the raw text for

Returns:

  • (Hpricot::Doc)

    The parsed page



62
63
64
# File 'lib/wiki_lib/pm_wiki.rb', line 62

def get_page(page)
  @agent.get(page_url(page))
end

#page_url(page) ⇒ String

returns a PMWiki page url

Parameters:

  • page (String)

    The name of the page, relative to the base page

Returns:

  • (String)

    The URL



28
29
30
# File 'lib/wiki_lib/pm_wiki.rb', line 28

def page_url(page)
"#{@base}/#{page}"
end

#upload_page(page, text, summary, minor = false) ⇒ Object

Uploads a page to the PM wiki, creating it if needed

Parameters:

  • page (String)

    Page to create/update

  • text (String)

    Text to place on the new page

  • summary (String)

    Edit summary for the edit

  • minor (TrueClass, FalseClass) (defaults to: false)

    tick the minor edit checkbox?



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wiki_lib/pm_wiki.rb', line 76

def upload_page(page, text, summary, minor=false)
  # get the form, logging in if nessessary
  form = (edit_url(page))

  # fill out our form
  form['text']    = text
  form['csum']    = summary
  form['author']  = @user

  edit.checkboxes.first.check if minor

  # upload
  @agent.submit(form, form.buttons.first)
end