module MediaWiki::Gateway::Files
Public Instance Methods
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.
# File lib/media_wiki/gateway/files.rb 161 def download(file_name, options = {}) 162 if attributes = image_info(file_name, options.merge('iiprop' => 'url')) 163 RestClient.get(attributes['url']) 164 end 165 end
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"
# File lib/media_wiki/gateway/files.rb 131 def image_info(file_name_or_page_id, options = {}) 132 if options['iiprop'].respond_to?(:join) 133 options['iiprop'] = options['iiprop'].join('|') 134 end 135 136 form_data = options.merge( 137 'action' => 'query', 138 'prop' => 'imageinfo', 139 'redirects' => true 140 ) 141 142 file_name_or_page_id.is_a?(Fixnum) ? 143 form_data['pageids'] = file_name_or_page_id : 144 form_data['titles'] = "File:#{file_name_or_page_id}" 145 146 xml = send_request(form_data) 147 148 if valid_page?(page = xml.elements['query/pages/page']) 149 if xml.elements['query/redirects/r'] 150 # We're dealing with redirect here. 151 image_info(page.attributes['pageid'].to_i, options) 152 else 153 page.elements['imageinfo/ii'].attributes 154 end 155 end 156 end
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', …]
# File lib/media_wiki/gateway/files.rb 82 def images(article_or_pageid, imlimit = 200, options = {}) 83 form_data = options.merge( 84 'action' => 'query', 85 'prop' => 'images', 86 'imlimit' => imlimit, 87 'redirects' => true 88 ) 89 90 form_data[article_or_pageid.is_a?(Fixnum) ? 91 'pageids' : 'titles'] = article_or_pageid 92 93 xml = send_request(form_data) 94 95 if valid_page?(page = xml.elements['query/pages/page']) 96 if xml.elements['query/redirects/r'] 97 # We're dealing with redirect here. 98 images(page.attributes['pageid'].to_i, imlimit) 99 else 100 REXML::XPath.match(page, 'images/im').map { |x| x.attributes['title'] } 101 end 102 end 103 end
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')
# File lib/media_wiki/gateway/files.rb 41 def upload(path, options = {}) 42 if options[:description] 43 options['text'] = options.delete(:description) 44 end 45 46 if options[:target] 47 options['filename'] = options.delete(:target) 48 end 49 50 if options[:summary] 51 options['text'] ||= options[:summary] 52 options['comment'] = options.delete(:summary) 53 end 54 55 options['comment'] ||= 'Uploaded by MediaWiki::Gateway' 56 57 options['file'] = File.new(path) if path 58 59 full_name = path || options['url'] 60 options['filename'] ||= File.basename(full_name) if full_name 61 62 unless options['file'] || options['url'] || options['sessionkey'] 63 raise ArgumentError, 64 "One of the 'file', 'url' or 'sessionkey' options must be specified!" 65 end 66 67 send_request(options.merge( 68 'action' => 'upload', 69 'token' => get_token('edit', options['filename']) 70 )) 71 end