mediawiki_cli 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.8)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ git (1.2.5)
8
+ i18n (0.6.0)
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.7.4)
15
+ mediawiki-gateway (0.5.1)
16
+ activesupport
17
+ rest-client (>= 1.3.0)
18
+ mime-types (1.19)
19
+ multi_json (1.3.6)
20
+ nokogiri (1.5.5)
21
+ rake (0.9.2.2)
22
+ rdoc (3.12)
23
+ json (~> 1.4)
24
+ rest-client (1.6.7)
25
+ mime-types (>= 1.16)
26
+ thor (0.15.4)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (>= 1.0.0)
33
+ jeweler (~> 1.8.4)
34
+ mediawiki-gateway
35
+ nokogiri
36
+ rdoc (~> 3.12)
37
+ thor
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Matt Yeh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # mediawiki_cli
2
+
3
+ mediawiki_cli is a command line interface tool for MediaWiki.
4
+
5
+ All mediawiki_cli commands require configuration options to be passed at run-time.
6
+
7
+ mw <action> <subcommand> <options> -W <wiki>
8
+
9
+ Options:
10
+ * -W, --wiki => (required) The URL to the MediaWiki API (eg, http://my.wiki/w/api.php) or the unique name of the wiki in the configuration file.
11
+ * -U, --username => (optional)
12
+ * -P, --password => (optional)
13
+ * -F, --file => (optional) A hashable configuration file (eg, YAML) whose top-level keys are unique names of the wiki. Each top-level key will contain a sub-hash that contain the keys "wiki", "username", and "password"
14
+
15
+ Actions:
16
+ * list
17
+ * export
18
+ * import
19
+ * xml
20
+
21
+ ## Listing
22
+ To retrieve a list of MediaWiki resources:
23
+
24
+ mw list [resource_subcommand] [options] -W [wiki]
25
+
26
+ Subcommands:
27
+ * categories
28
+ * properties
29
+ * templates
30
+ * all => (default) List all categories, properties, and templates.
31
+ * namespaces
32
+ * members
33
+ * -c, --categories => A list of category names (eg, `mw list members -n "Category:Foo" "Category:Bar"`)
34
+ * -n, --namespaces => A list of namespace names (eg, `mw list members -n "Property:"`)
35
+ * -f, --file => A hashable file (eg, YAML) that contains keys for "categories" and "namespaces"
36
+
37
+ ## Exporting
38
+ MediaWiki export files will conform to the MediaWiki XML Schema. The 'export' subcommand accepts input from standard input, such as from piped command output.
39
+
40
+ To export MediaWiki resources:
41
+
42
+ mw export [subcommand] [options] -W [wiki]
43
+
44
+ Subcommands:
45
+ * export => (default)
46
+ * -p, --pages => A list of page names to export (eg, `mw export -p "Main Page" "My Other Page"`)
47
+ * batch
48
+ * -c, --categories => A list of category names whose members will be exported (eg, `mw export batch -c "Category:Foo" "Category:Bar" -W [wiki]`)
49
+ * -n, --namespaces => A list of namespace names that will be exported (eg, `mw export batch -n "Property:" "Concept:" -W [wiki]`)
50
+ * -p, --pages => A list of page names that will be exported (eg, `mw export batch -p "Main Page" "My Other Page" -W [wiki]`)
51
+ * -f, --file => A hashable file (eg, YAML) that contains keys for "categories", "namespaces", and "pages"
52
+
53
+ ## Importing
54
+ Import a MediaWiki XML export file to the specified wiki.
55
+
56
+ mw import [*files] -W [wiki]
57
+
58
+ ## Xml
59
+ It may be necessary to perform XML manipulation for the MediaWiki XML files.
60
+
61
+ mw xml [subcommand] [options] -W [wiki]
62
+
63
+ Subcommands:
64
+ * merge[*args] => Merge pages elements of MediaWiki XML dumps (eg, `mw xml merge "export1.xml" "export2.xml" -W [wiki]`)
65
+
66
+ ## Contributing to mediawiki_cli
67
+
68
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
69
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
70
+ * Fork the project.
71
+ * Start a feature/bugfix branch.
72
+ * Commit and push until you are happy with your contribution.
73
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
74
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
75
+
76
+ ## Copyright
77
+
78
+ Copyright (c) 2012 Matt Yeh. See LICENSE.txt for
79
+ further details.
80
+
data/bin/mw ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ lib = Pathname.new(__FILE__).dirname.join('..', 'lib').expand_path
5
+ $LOAD_PATH.unshift lib.to_s
6
+
7
+ require 'mediawiki_cli/cli'
8
+
9
+ Mw::Cli.start
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'media_wiki'
4
+ require 'mediawiki_cli/list'
5
+ require 'mediawiki_cli/export'
6
+ require 'mediawiki_cli/import'
7
+ require 'mediawiki_cli/xml'
8
+ require 'thor'
9
+
10
+ module Mw
11
+ class Cli < Thor
12
+ include FileHandler
13
+ namespace :mw
14
+
15
+ class_option :wiki, :type=>:string, :aliases=>"-W", :required=>true
16
+ class_option :username, :type=>:string, :aliases=>"-U"
17
+ class_option :password, :type=>:string, :aliases=>"-P"
18
+ class_option :file, :type=>:string, :aliases=>"-F"
19
+
20
+ desc "setup", "setup"
21
+ def setup
22
+ config = symbolize_keys(to_hash(options.file)).fetch(options.wiki.to_sym) if options.file
23
+
24
+ config ||= options
25
+ $mw ||= MediaWiki::Gateway.new(config[:wiki])
26
+ if config[:username] and config[:password]
27
+ $mw.login(config[:username], config[:password])
28
+ end
29
+ end
30
+
31
+ desc "list", "list"
32
+ subcommand "list", List
33
+
34
+ desc "export", "export"
35
+ subcommand "export", Export
36
+
37
+ desc "import", "import"
38
+ subcommand "import", Import
39
+
40
+ desc "xml", "xml"
41
+ subcommand "xml", Xml
42
+ end
43
+ end
44
+
@@ -0,0 +1,48 @@
1
+ require 'mediawiki_cli/mwgateway'
2
+ require 'mediawiki_cli/filehandler'
3
+ require 'thor'
4
+
5
+ class Export < Thor
6
+ include MwGateway, FileHandler
7
+ namespace "mw:export"
8
+ default_task :export
9
+
10
+ desc "batch", "Export categories, pages, namespaces"
11
+ method_option :categories, :type=>:array, :aliases=>"-c"
12
+ method_option :pages, :type => :array, :aliases=>"-p"
13
+ method_option :namespaces, :type=>:array, :aliases=>"-n"
14
+ method_option :file, :type=>:string, :aliases=>"-f"
15
+ def batch
16
+ pages = []
17
+
18
+ # TODO combine options
19
+ export ||= to_hash(options.file) if options.file
20
+ export ||= options
21
+
22
+ export.each { |key, value|
23
+ case key
24
+ when "categories", "namespaces"
25
+ pages.push invoke "mw:list:members", [], :categories => export["categories"],
26
+ :namespaces => export["namespaces"],
27
+ :wiki => parent_options.wiki,
28
+ :username => parent_options.username,
29
+ :password => parent_options.password,
30
+ :file => parent_options.file
31
+ when "pages"
32
+ pages.push export["pages"]
33
+ end
34
+ }
35
+ return output $mw.export(pages.flatten)
36
+ end
37
+
38
+ desc "export", "Export a list of pages"
39
+ method_option :pages, :type=>:array, :aliases=>"-p", :required=>true
40
+ def export
41
+ invoke "mw:setup", [], :wiki => parent_options.wiki,
42
+ :username => parent_options.username,
43
+ :password => parent_options.password,
44
+ :file => parent_options.file
45
+ opts = (STDIN.tty?) ? options.pages : $stdin.read.split("\n")
46
+ return output $mw.export(opts)
47
+ end
48
+ end
@@ -0,0 +1,18 @@
1
+
2
+ module FileHandler
3
+ private
4
+ # TODO when does thor create symbol keys vs string keys?
5
+ def to_hash(file)
6
+ case File.extname file
7
+ when '.yaml', '.yml'
8
+ require 'yaml'
9
+ return YAML::load_file(file)
10
+ end
11
+ end
12
+
13
+ def symbolize_keys(hash)
14
+ return hash if not hash.is_a?(Hash)
15
+ result = hash.inject({}){|h,(k,v)| h[k.to_sym] = symbolize_keys(v); h}
16
+ return result
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ require 'mediawiki_cli/mwgateway'
2
+ require 'thor'
3
+
4
+ class Import < Thor
5
+ include MwGateway
6
+ namespace "mw:import"
7
+ default_task :import
8
+
9
+ desc "import", "Import pages from file."
10
+ def import(*file)
11
+ create_gateway(wiki)
12
+ file.each { |f|
13
+ $mw.import(file)
14
+ }
15
+ end
16
+ end
17
+
@@ -0,0 +1,64 @@
1
+ require 'mediawiki_cli/mwgateway'
2
+ require 'mediawiki_cli/filehandler'
3
+ require 'thor'
4
+
5
+ class List < Thor
6
+ include MwGateway, FileHandler
7
+ namespace "mw:list"
8
+ default_task :all
9
+
10
+ desc "all", "List categories, properties, templates, and namespaces"
11
+ def all
12
+ create_gateway
13
+ invoke :categories
14
+ invoke :properties
15
+ invoke :templates
16
+ end
17
+
18
+ desc "members", "List members in given categories."
19
+ method_option :categories, :type=>:array, :aliases=>"-c"
20
+ method_option :namespaces, :type=>:array, :aliases=>"-n"
21
+ method_option :file, :type=>:string, :aliases=>"-f"
22
+ def members
23
+ create_gateway
24
+ members = []
25
+ options[:categories].each { |c|
26
+ members += $mw.category_members(c)
27
+ members << c
28
+ } if options[:categories]
29
+ options[:namespaces].each { |n|
30
+ members += list_by_namespace(n)
31
+ } if options[:namespaces]
32
+ return output members
33
+ end
34
+
35
+ desc "namespaces", "List all namespaces"
36
+ def namespaces
37
+ create_gateway
38
+ return output $mw.namespaces_by_prefix.keys
39
+ end
40
+
41
+ ## TODO metaprogramming on these methods
42
+ desc "categories", "List all categories"
43
+ def categories
44
+ list_by_namespace("Category:")
45
+ end
46
+
47
+ desc "properties", "List all properties"
48
+ def properties
49
+ list_by_namespace("Property:")
50
+ end
51
+
52
+ desc "templates", "List all templates"
53
+ def templates
54
+ list_by_namespace("Template:")
55
+ end
56
+ ##
57
+
58
+ no_tasks {
59
+ def list_by_namespace( namespace )
60
+ create_gateway
61
+ return output $mw.list( namespace )
62
+ end
63
+ }
64
+ end
@@ -0,0 +1,15 @@
1
+
2
+ module MwGateway
3
+ private
4
+ def create_gateway
5
+ opts = parent_options.merge options
6
+ invoke "mw:setup", [], :wiki => opts.wiki,
7
+ :username => opts.username,
8
+ :password => opts.password,
9
+ :file => opts.file
10
+ end
11
+ def output(obj)
12
+ puts obj
13
+ obj
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'nokogiri'
2
+ require 'thor'
3
+
4
+ class Xml < Thor
5
+ namespace "mw:xml"
6
+
7
+ desc "merge", "Merge MediaWiki XML dumps"
8
+ def merge(*xml)
9
+
10
+ mwns = "http://www.mediawiki.org/xml/export-0.5/"
11
+
12
+ first = xml.shift
13
+ f = File.open(first) if File.exists? first
14
+ master = Nokogiri::XML f
15
+
16
+ others = xml.each { |x|
17
+ x = File.open(x) if File.exists? x
18
+ doc = Nokogiri::XML x
19
+ doc.xpath("//mediawiki:page", "mediawiki"=>mwns).each {|n|
20
+ master.root.add_child n
21
+ }
22
+ }
23
+
24
+ puts master.root.to_xml
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #require 'media_wiki'
4
+ require 'media_wiki'
5
+ require 'thor'
6
+ require 'mediawiki_cli/cli'
7
+ require 'mediawiki_cli/list'
8
+ require 'mediawiki_cli/export'
9
+ require 'mediawiki_cli/import'
10
+ require 'mediawiki_cli/xml'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mediawiki_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Yeh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: &2153095600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2153095600
25
+ - !ruby/object:Gem::Dependency
26
+ name: mediawiki-gateway
27
+ requirement: &2153093860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2153093860
36
+ - !ruby/object:Gem::Dependency
37
+ name: nokogiri
38
+ requirement: &2153092960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2153092960
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &2153091960 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2153091960
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &2153090740 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.0.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2153090740
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &2153089320 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.4
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2153089320
80
+ description: A command line interface for MediaWiki that offers listing, exporting,
81
+ and importing features.
82
+ email: mtyeh411@gmail.com
83
+ executables:
84
+ - mw
85
+ extensions: []
86
+ extra_rdoc_files:
87
+ - LICENSE.txt
88
+ - README.md
89
+ files:
90
+ - Gemfile.lock
91
+ - LICENSE.txt
92
+ - README.md
93
+ - bin/mw
94
+ - lib/mediawiki_cli.rb
95
+ - lib/mediawiki_cli/cli.rb
96
+ - lib/mediawiki_cli/export.rb
97
+ - lib/mediawiki_cli/filehandler.rb
98
+ - lib/mediawiki_cli/import.rb
99
+ - lib/mediawiki_cli/list.rb
100
+ - lib/mediawiki_cli/mwgateway.rb
101
+ - lib/mediawiki_cli/xml.rb
102
+ homepage: http://github.com/mtyeh411/mediawiki_cli
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: -2002296685323973285
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.10
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: A command line interface for MediaWiki
130
+ test_files: []