Module: MediaWiki::Edit
- Included in:
- Butt
- Defined in:
- lib/mediawiki/edit.rb
Instance Method Summary collapse
-
#create_page(title, text, opts = {}) ⇒ String
Creates a new page.
-
#delete(title, reason = nil) ⇒ Boolean
Deletes a page.
-
#edit(title, text, opts = {}) ⇒ String, Boolean
Performs a standard non-creation edit.
-
#move(from, to, opts = {}) ⇒ Boolean
Performs a move on a page.
-
#patrol(opts = {}) ⇒ String
Patrol an edit by its recentchanges or revision ID.
-
#upload(url, filename = nil) ⇒ Boolean
Uploads a file from a URL.
Instance Method Details
#create_page(title, text, opts = {}) ⇒ String
Creates a new page.
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.
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.
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.
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.
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.
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 |