Module: MediaWiki::Gateway::Files
- Included in:
- MediaWiki::Gateway
- Defined in:
- lib/media_wiki/gateway/files.rb
Instance Method Summary collapse
-
#download(file_name, options = {}) ⇒ Object
Download file_name (without “File:” or “Image:” prefix).
-
#image_info(file_name_or_page_id, options = {}) ⇒ Object
Requests image info from MediaWiki.
-
#images(article_or_pageid, imlimit = 200, options = {}) ⇒ Object
Get image list for given article.
-
#upload(path, options = {}) ⇒ Object
Upload a file, or get the status of pending uploads.
Instance Method Details
#download(file_name, options = {}) ⇒ Object
Download file_name (without “File:” or “Image:” prefix). Returns file contents. All options are passed to #image_info however options is forced to url. You can still set other options to control what file you want to download.
161 162 163 164 165 |
# File 'lib/media_wiki/gateway/files.rb', line 161 def download(file_name, = {}) if attributes = image_info(file_name, .merge('iiprop' => 'url')) RestClient.get(attributes['url']) end end |
#image_info(file_name_or_page_id, options = {}) ⇒ Object
Requests image info from MediaWiki. Follows redirects.
file_name_or_page_id should be either:
-
a file name (String) you want info about without File: prefix.
-
or a Fixnum page id you of the file.
options is Hash
passed as query arguments. See www.mediawiki.org/wiki/API:Query_-_Properties#imageinfo_.2F_ii for more information.
options should be either a string of properties joined by ‘|’ or an Array
(or more precisely something that responds to #join).
Hash
like object is returned where keys are image properties.
Example:
mw.image_info(
'Trooper.jpg', 'iiprop' => ['timestamp', 'user']
).each do |key, value|
puts "#{key.inspect} => #{value.inspect}"
end
Output:
"timestamp" => "2009-10-31T12:59:11Z"
"user" => "Valdas"
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/media_wiki/gateway/files.rb', line 131 def image_info(file_name_or_page_id, = {}) if ['iiprop'].respond_to?(:join) ['iiprop'] = ['iiprop'].join('|') end form_data = .merge( 'action' => 'query', 'prop' => 'imageinfo', 'redirects' => true ) file_name_or_page_id.is_a?(Fixnum) ? form_data['pageids'] = file_name_or_page_id : form_data['titles'] = "File:#{file_name_or_page_id}" xml = send_request(form_data) if valid_page?(page = xml.elements['query/pages/page']) if xml.elements['query/redirects/r'] # We're dealing with redirect here. image_info(page.attributes['pageid'].to_i, ) else page.elements['imageinfo/ii'].attributes end end end |
#images(article_or_pageid, imlimit = 200, options = {}) ⇒ Object
Get image list for given article. Follows redirects.
article_or_pageid is the title or pageid of a single article imlimit is the maximum number of images to return (defaults to 200) options is the hash of additional options
Example:
images = mw.images('Gaborone')
images would contain [‘File:Gaborone at night.jpg’, ‘File:Gaborone2.png’, …]
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/media_wiki/gateway/files.rb', line 82 def images(article_or_pageid, imlimit = 200, = {}) form_data = .merge( 'action' => 'query', 'prop' => 'images', 'imlimit' => imlimit, 'redirects' => true ) form_data[article_or_pageid.is_a?(Fixnum) ? 'pageids' : 'titles'] = article_or_pageid xml = send_request(form_data) if valid_page?(page = xml.elements['query/pages/page']) if xml.elements['query/redirects/r'] # We're dealing with redirect here. images(page.attributes['pageid'].to_i, imlimit) else REXML::XPath.match(page, 'images/im').map { |x| x.attributes['title'] } end end end |
#upload(path, options = {}) ⇒ Object
Upload a file, or get the status of pending uploads. Several methods are available:
-
Upload file contents directly.
-
Have the MediaWiki server fetch a file from a URL, using the ‘url’ parameter
Requires Mediawiki 1.16+
Arguments:
- path
-
Path to file to upload. Set to nil if uploading from URL.
- options
-
Hash of additional options
Note that queries using session keys must be done in the same login session as the query that originally returned the key (i.e. do not log out and then log back in).
Options:
-
‘filename’ - Target filename (defaults to local name if not given), options is alias for this.
-
‘comment’ - Upload comment. Also used as the initial page text for new files if ‘text’ is not specified.
-
‘text’ - Initial page text for new files
-
‘watch’ - Watch the page
-
‘ignorewarnings’ - Ignore any warnings
-
‘url’ - Url to fetch the file from. Set path to nil if you want to use this.
Deprecated but still supported options:
-
:description - Description of this file. Used as ‘text’.
-
:target - Target filename, same as ‘filename’.
-
:summary - Edit summary for history. Used as ‘comment’. Also used as ‘text’ if neither it or :description is specified.
Examples:
mw.upload('/path/to/local/file.jpg', 'filename' => 'RemoteFile.jpg')
mw.upload(nil, 'filename' => 'RemoteFile2.jpg', 'url' => 'http://remote.com/server/file.jpg')
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/media_wiki/gateway/files.rb', line 41 def upload(path, = {}) if [:description] ['text'] = .delete(:description) end if [:target] ['filename'] = .delete(:target) end if [:summary] ['text'] ||= [:summary] ['comment'] = .delete(:summary) end ['comment'] ||= 'Uploaded by MediaWiki::Gateway' ['file'] = File.new(path) if path full_name = path || ['url'] ['filename'] ||= File.basename(full_name) if full_name unless ['file'] || ['url'] || ['sessionkey'] raise ArgumentError, "One of the 'file', 'url' or 'sessionkey' options must be specified!" end send_request(.merge( 'action' => 'upload', 'token' => get_token('edit', ['filename']) )) end |