2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2010 Ian Weller <ian@ianweller.org>
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.
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.
15 # You should have received a copy of the GNU General Public License along
16 # with this program. If not, see <http://www.gnu.org/licenses/>.
22 from optparse import OptionParser, OptionGroup
27 class CommandBase(object):
29 def __init__(self, name, description, usage=None):
30 self.me = os.path.basename(sys.argv[0])
31 self.description = description
33 usage = '%prog ' + name
35 usage = '%%prog %s %s' % (name, usage)
36 self.parser = OptionParser(usage=usage, description=description)
38 self.metadir = mw.metadir.Metadir()
39 #global_options = OptionGroup(self.parser, "Global Options")
40 #global_options.add_option('-u', '--use-auth', action='store_true',
41 # dest='use_auth', help='force authentication '
42 # 'even if not required')
43 #self.parser.add_option_group(global_options)
47 (self.options, self.args) = self.parser.parse_args()
48 self.args = self.args[1:] # don't need the first thing
51 def _do_command(self):
55 user = raw_input('Username: ')
56 passwd = getpass.getpass()
57 result = self.api.call({'action': 'login',
59 'lgpassword': passwd})
60 if result['login']['result'] == 'Success':
61 # cookies are saved to a file
62 print 'Login successful! (yay)'
64 print 'Login failed: %s' % result['login']['result']
66 def _die_if_no_init(self):
67 if self.metadir.config is None:
68 print '%s: not a mw repo' % self.me
72 self.api_url = self.metadir.config.get('remote', 'api_url')
73 self.api = mw.api.API(self.api_url, self.metadir)
76 class InitCommand(CommandBase):
80 CommandBase.__init__(self, 'init', 'start a mw repo', usage)
82 def _do_command(self):
83 if len(self.args) < 1:
84 self.parser.error('must have URL to remote api.php')
85 elif len(self.args) > 1:
86 self.parser.error('too many arguments')
87 self.metadir.create(self.args[0])
90 class LoginCommand(CommandBase):
93 CommandBase.__init__(self, 'login', 'authenticate with wiki')
95 def _do_command(self):
96 self._die_if_no_init()
101 class PullCommand(CommandBase):
104 usage = '[options] PAGENAME ...'
105 CommandBase.__init__(self, 'pull', 'add remote pages to repo', usage)
107 def _do_command(self):
108 self._die_if_no_init()
112 for these_pages in [pages[i:i + 25] for i in range(0, len(pages), 25)]:
115 'titles': '|'.join(these_pages),
116 'prop': 'info|revisions',
117 'rvprop': 'ids|flags|timestamp|user|comment|content',
119 response = self.api.call(data)['query']['pages']
120 for pageid in response.keys():
121 pagename = response[pageid]['title']
122 if 'missing' in response[pageid].keys():
123 print '%s: %s: page does not exist, file not created' % \
126 revids = [x['revid'] for x in response[pageid]['revisions']]
128 self.metadir.pagedict_add(pagename, pageid, revids[-1])
129 self.metadir.pages_add_rv(int(pageid),
130 response[pageid]['revisions'][0])
131 filename = mw.api.pagename_to_filename(pagename)
132 fd = file(os.path.join(self.metadir.root, filename + '.wiki'),
134 fd.write(response[pageid]['revisions'][0]['*'].encode('utf-8'))
137 class StatusCommand(CommandBase):
140 CommandBase.__init__(self, 'status', 'check repo status')
141 self.shortcuts.append('st')
143 def _do_command(self):
144 self._die_if_no_init()
145 status = self.metadir.working_dir_status()
147 print '%s %s' % (status[file], file)
150 class DiffCommand(CommandBase):
153 CommandBase.__init__(self, 'diff', 'diff wiki to working directory')
155 def _do_command(self):
156 self._die_if_no_init()
157 status = self.metadir.working_dir_status()
159 if status[file] == 'U':
160 print self.metadir.diff_rv_to_working(
161 mw.api.filename_to_pagename(file[:-5])
165 class CommitCommand(CommandBase):
168 CommandBase.__init__(self, 'commit', 'commit changes to wiki')
169 self.shortcuts.append('ci')
171 def _do_command(self):
172 self._die_if_no_init()