77148934227b3f0167dace94254edb04b1113399
[mw] / src / mw / clicommands.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009  Ian Weller <ian@ianweller.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 ###
19
20 import getpass
21 import mw.api
22 import mw.metadir
23 from optparse import OptionParser, OptionGroup
24 import os
25 import sys
26
27 class CommandBase(object):
28     def __init__(self, name, description, usage=None):
29         self.me = os.path.basename(sys.argv[0])
30         self.description = description
31         if usage is None:
32             usage = '%prog ' + name
33         self.parser = OptionParser(usage=usage, description=description)
34         self.name = name
35         self.metadir = mw.metadir.Metadir()
36         global_options = OptionGroup(self.parser, "Global Options")
37         global_options.add_option('-u', '--use-auth', action='store_true',
38                                   dest='use_auth', help='force authentication '
39                                   'even if not required')
40         self.parser.add_option_group(global_options)
41
42     def main(self):
43         (self.options, self.args) = self.parser.parse_args()
44         self.args = self.args[1:] # don't need the first thing
45         self._do_command()
46
47     def _login(self):
48         user = raw_input('Username: ')
49         passwd = getpass.getpass()
50
51     def _die_if_no_init(self):
52         if self.metadir.config is None:
53             print '%s: not a mw repo' % self.me
54             sys.exit(1)
55
56     def _api_setup(self):
57         self.api_url = self.metadir.config.get('remote', 'api_url')
58         self.api = mw.api.API(self.api_url)
59
60
61 class InitCommand(CommandBase):
62     def __init__(self):
63         usage = '%prog init API_URL'
64         CommandBase.__init__(self, 'init', 'start a mw repo', usage)
65
66     def _do_command(self):
67         if len(self.args) < 1:
68             self.parser.error('must have URL to remote api.php')
69         elif len(self.args) > 1:
70             self.parser.error('too many arguments')
71         self.metadir.create(self.args[0])
72
73
74 class FetchCommand(CommandBase):
75     def __init__(self):
76         usage = '%prog fetch [options] PAGENAME ...'
77         CommandBase.__init__(self, 'fetch', 'fetch remote pages', usage)
78
79     def _do_command(self):
80         self._die_if_no_init()
81         self._api_setup()
82         pages = []
83         pages += self.args
84         for these_pages in [pages[i:i+25] for i in range(0, len(pages), 25)]:
85             data = {
86                     'action': 'query',
87                     'titles': '|'.join(these_pages),
88                     'prop': 'info|revisions',
89                     'rvprop': 'ids|flags|timestamp|user|comment|content',
90             }
91             response = self.api.call(data)['query']['pages']
92             for pageid in response.keys():
93                 revid = [x['revid'] for x in response[pageid]['revisions']]
94                 self.metadir.add_page_info(int(pageid),
95                                            response[pageid]['title'],
96                                            revid)
97                 self.metadir.add_rv_info(response[pageid]['revisions'][0])
98                 fd = file(os.path.join(self.metadir.root, \
99                         response[pageid]['title'].replace(' ', '_') + \
100                         '.wiki'), 'w')
101                 fd.write(response[pageid]['revisions'][0]['*'].encode('utf-8'))

Benjamin Mako Hill || Want to submit a patch?