Module: MediaWiki::Edit

Included in:
Butt
Defined in:
lib/mediawiki/edit.rb

Instance Method Summary collapse

Instance Method Details

#create_page(title, text, opts = {}) ⇒ String

Creates a new page.

Parameters:

  • title (String)

    The new page’s title.

  • text (String)

    The new page’s content.

  • opts (Hash<Symbol, Any>) (defaults to: {})

    The options hash for optional values in the request.

Options Hash (opts):

  • :summary (String)

    The edit summary. Defaults to “New page”.

  • :bot (Boolean)

    Will mark the edit as a bot edit if true. Defaults to true.

Returns:

  • (String)

    The new page ID

Raises:

  • (EditError)

    If there was some error when creating the page.

See Also:

Since:

  • 0.3.0



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mediawiki/edit.rb', line 56

def create_page(title, text, opts = {})
  opts[:bot] = opts.key?(:bot) ? opts[:bot] : true
  opts[:summary] ||= 'New page'
  params = {
    action: 'edit',
    title: title,
    text: text,
    summary: opts[:summary],
    createonly: 1,
    format: 'json',
    token: get_token
  }

  params[:bot] = '1' if opts[:bot]

  response = post(params)

  return response['edit']['pageid'] if response.dig('edit', 'result') == 'Success'
  raise MediaWiki::Butt::EditError.new(response.dig('error', 'code') || 'Unknown error code')
end

#delete(title, reason = nil) ⇒ Boolean

Deletes a page.

Parameters:

  • title (String)

    The page to delete.

  • reason (String) (defaults to: nil)

    The reason to be displayed in logs. Optional.

Returns:

  • (Boolean)

    True if successful.

Raises:

  • (EditError)

See Also:

Since:

  • 0.5.0



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mediawiki/edit.rb', line 156

def delete(title, reason = nil)
  params = {
    action: 'delete',
    title: title,
    token: get_token
  }

  params[:reason] = reason unless reason.nil?

  response = post(params)
  return true if response['delete']
  raise MediaWiki::Butt::EditError.new(response.dig('error', 'code') || 'Unknown error code')
end

#edit(title, text, opts = {}) ⇒ String, Boolean

Performs a standard non-creation edit.

Parameters:

  • title (String)

    The page title.

  • text (String)

    The new content.

  • opts (Hash<Symbol, Any>) (defaults to: {})

    The options hash for optional values in the request.

Options Hash (opts):

  • :minor (Boolean)

    Will mark the edit as minor if true.

  • :bot (Boolean)

    Will mark the edit as bot edit if true. Defaults to true.

  • :summary (String)

    The edit summary. Optional.

Returns:

  • (String)

    The new revision ID

  • (Boolean)

    False if there was no change in the edit.

Raises:

  • (EditError)

    if the edit failed somehow

See Also:

Since:

  • 0.2.0



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mediawiki/edit.rb', line 19

def edit(title, text, opts = {})
  opts[:bot] = opts.key?(:bot) ? opts[:bot] : true
  params = {
    action: 'edit',
    title: title,
    text: text,
    nocreate: 1,
    format: 'json',
    token: get_token
  }

  params[:summary] ||= opts[:summary]
  params[:minor] = '1' if opts[:minor]
  params[:bot] = '1' if opts[:bot]

  response = post(params)

  if response.dig('edit', 'result') == 'Success'
    return false if response.dig('edit', 'nochange')
    return response.dig('edit', 'newrevid')
  end

  raise MediaWiki::Butt::EditError.new(response.dig('error', 'code') || 'Unknown error code')
end

#move(from, to, opts = {}) ⇒ Boolean

Performs a move on a page.

Parameters:

  • from (String)

    The page to be moved.

  • to (String)

    The destination of the move.

  • opts (Hash<Symbol, Any>) (defaults to: {})

    The options hash for optional values in the request.

Options Hash (opts):

  • :reason (String)

    The reason for the move, which shows up in the log.

  • :talk (Boolean)

    Whether to move the associated talk page. Defaults to true.

  • :suppress_redirect (Boolean)

    Set to a truthy value in order to prevent the API from making a redirect page.

Returns:

  • (Boolean)

    True if it was successful.

Raises:

  • (EditError)

See Also:

Since:

  • 0.5.0



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mediawiki/edit.rb', line 128

def move(from, to, opts = {})
  opts[:talk] = opts.key?(:talk) ? opts[:talk] : true
  params = {
    action: 'move',
    from: from,
    to: to,
    token: get_token
  }

  params[:reason] ||= opts[:reason]
  params[:movetalk] = '1' if opts[:talk]
  params[:noredirect] = '1' if opts[:suppress_redirect]

  response = post(params)

  return true if response['move']
  raise MediaWiki::Butt::EditError.new(response.dig('error', 'code') || 'Unknown error code')
end

#patrol(opts = {}) ⇒ String

Patrol an edit by its recentchanges or revision ID.

Parameters:

  • opts (Hash<Symbol, String>) (defaults to: {})

    Options param.

  • :rcid (Hash)

    a customizable set of options

  • :revid (Hash)

    a customizable set of options

  • :tags (Hash)

    a customizable set of options

Returns:

  • (String)

    The title of the page that was patrolled, if it was patrolled.

Raises:

  • (PatrolError)

See Also:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/mediawiki/edit.rb', line 179

def patrol(opts = {})
  params = {
    action: 'patrol',
    token: get_token('patrol')
  }

  params[:rcid] = opts[:rcid] if opts[:rcid]
  params[:revid] = opts[:revid] if opts[:revid]
  params[:tags] = opts[:tags] if opts[:tags]

  if opts[:tags]
    params[:tags] = opts[:tags].is_a?(Array) ? opts[:tags].join('|') : opts[:tags]
  end

  response = post(params)
  return response['patrol']['title'] if response['patrol']
  raise MediaWiki::Butt::PatrolError.new(response.dig('error', 'code') || 'Unknown error code')
end

#upload(url, filename = nil) ⇒ Boolean

Uploads a file from a URL.

Parameters:

  • url (String)

    The URL to the file.

  • filename (String) (defaults to: nil)

    The preferred filename. This can include File: at the beginning, but it will be removed through regex. Optional. If omitted, it will be everything after the last slash in the URL.

Returns:

  • (Boolean)

    Whether the upload was successful. It is likely that if it returns false, it also raised a warning.

Raises:

  • (UploadInvalidFileExtError)

    When the file extension provided is not valid for the wiki.

  • (EditError)

See Also:

Since:

  • 0.3.0



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mediawiki/edit.rb', line 89

def upload(url, filename = nil)
  params = {
    action: 'upload',
    url: url,
    token: get_token
  }

  filename = filename.nil? ? url.split('/')[-1] : filename.sub(/^File:/, '')

  ext = filename.split('.')[-1]
  allowed_extensions = get_allowed_file_extensions
  raise MediaWiki::Butt::UploadInvalidFileExtError.new unless allowed_extensions.include?(ext)

  params[:filename] = filename

  response = post(params)

  response.dig('upload', 'warnings')&.each do |warning|
    warn warning
  end

  return true if response.dig('upload', 'result') == 'Success'
  raise MediaWiki::Butt::EditError.new(response.dig('error', 'code') || 'Unknown error code')
end