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/>.
24 from optparse import OptionParser, OptionGroup
26 import simplemediawiki
30 class CommandBase(object):
32 def __init__(self, name, description, usage=None):
33 self.me = os.path.basename(sys.argv[0])
34 self.description = description
36 usage = '%prog ' + name
38 usage = '%%prog %s %s' % (name, usage)
39 self.parser = OptionParser(usage=usage, description=description)
41 self.metadir = mw.metadir.Metadir()
45 (self.options, self.args) = self.parser.parse_args()
46 self.args = self.args[1:] # don't need the first thing
49 def _do_command(self):
53 user = raw_input('Username: ')
54 passwd = getpass.getpass()
55 result = self.api.call({'action': 'login',
57 'lgpassword': passwd})
58 if result['login']['result'] == 'Success':
59 # cookies are saved to a file
60 print 'Login successful! (yay)'
61 elif result['login']['result'] == 'NeedToken':
62 print 'Login with token'
63 result = self.api.call({'action': 'login',
66 'lgtoken': result['login']['token']})
67 if result['login']['result'] == 'Success':
68 print 'Login successful! (yay)'
70 print 'Login failed: %s' % result['login']['result']
72 print 'Login failed: %s' % result['login']['result']
74 def _die_if_no_init(self):
75 if self.metadir.config is None:
76 print '%s: not a mw repo' % self.me
80 cookie_file = os.path.join(self.metadir.location, 'cookies')
81 self.api_url = self.metadir.config.get('remote', 'api_url')
82 self.api = simplemediawiki.MediaWiki(self.api_url,
83 cookie_file=cookie_file)
86 class InitCommand(CommandBase):
90 CommandBase.__init__(self, 'init', 'start a mw repo', usage)
92 def _do_command(self):
93 if len(self.args) < 1:
94 self.parser.error('must have URL to remote api.php')
95 elif len(self.args) > 1:
96 self.parser.error('too many arguments')
97 self.metadir.create(self.args[0])
100 class LoginCommand(CommandBase):
103 CommandBase.__init__(self, 'login', 'authenticate with wiki')
105 def _do_command(self):
106 self._die_if_no_init()
111 class LogoutCommand(CommandBase):
114 CommandBase.__init__(self, 'logout', 'forget authentication')
116 def _do_command(self):
117 self._die_if_no_init()
119 os.unlink(os.path.join(self.metadir.location, 'cookies'))
124 class PullCommand(CommandBase):
127 usage = '[options] PAGENAME ...'
128 CommandBase.__init__(self, 'pull', 'add remote pages to repo', usage)
130 def _do_command(self):
131 self._die_if_no_init()
135 for these_pages in [pages[i:i + 25] for i in range(0, len(pages), 25)]:
138 'titles': '|'.join(these_pages),
139 'prop': 'info|revisions',
140 'rvprop': 'ids|flags|timestamp|user|comment|content',
142 response = self.api.call(data)['query']['pages']
143 for pageid in response.keys():
144 pagename = response[pageid]['title']
145 if 'missing' in response[pageid].keys():
146 print '%s: %s: page does not exist, file not created' % \
149 revids = [x['revid'] for x in response[pageid]['revisions']]
151 self.metadir.pagedict_add(pagename, pageid, revids[-1])
152 self.metadir.pages_add_rv(int(pageid),
153 response[pageid]['revisions'][0])
154 filename = mw.metadir.pagename_to_filename(pagename)
155 with file(os.path.join(self.metadir.root, filename + '.wiki'),
157 data = response[pageid]['revisions'][0]['*']
158 data = data.encode('utf-8')
162 class StatusCommand(CommandBase):
165 CommandBase.__init__(self, 'status', 'check repo status')
166 self.shortcuts.append('st')
168 def _do_command(self):
169 self._die_if_no_init()
170 status = self.metadir.working_dir_status()
172 print '%s %s' % (status[file], file)
175 class DiffCommand(CommandBase):
178 CommandBase.__init__(self, 'diff', 'diff wiki to working directory')
180 def _do_command(self):
181 self._die_if_no_init()
182 status = self.metadir.working_dir_status()
184 if status[file] == 'U':
185 print self.metadir.diff_rv_to_working(
186 mw.metadir.filename_to_pagename(file[:-5])),
189 class CommitCommand(CommandBase):
193 CommandBase.__init__(self, 'commit', 'commit changes to wiki', usage)
194 self.shortcuts.append('ci')
195 self.parser.add_option('-m', '--message', dest='edit_summary',
196 help='don\'t prompt for edit summary and '
198 self.parser.add_option('-b', '--bot', dest='bot', action='store_true',
199 help='mark actions as a bot (won\'t affect '
200 'anything if you don\'t have the bot right',
203 def _do_command(self):
204 self._die_if_no_init()
206 status = self.metadir.working_dir_status(files=self.args)
207 nothing_to_commit = True
209 print '%s %s' % (status[file], file)
210 if status[file] in ['U']:
211 nothing_to_commit = False
212 if nothing_to_commit:
213 print 'nothing to commit'
215 if self.options.edit_summary == None:
216 print 'Edit summary:',
217 edit_summary = raw_input()
219 edit_summary = self.options.edit_summary
221 if status[file] in ['U']:
225 'prop': 'info|revisions',
227 'titles': mw.metadir.filename_to_pagename(file[:-5]),
229 response = self.api.call(data)
230 pageid = response['query']['pages'].keys()[0]
231 revid = response['query']['pages'][pageid]['revisions'][0]\
233 awaitedrevid = self.metadir.pages_get_rv_list({'id': pageid})\
235 if revid != awaitedrevid:
236 print 'warning: edit conflict detected on %s (%s -> %s) ' \
237 '-- skipping!' % (file, awaitedrevid, revid)
239 edittoken = response['query']['pages'][pageid]['edittoken']
240 filename = os.path.join(self.metadir.root, file)
241 text = codecs.open(filename, 'r', 'utf-8').read()
242 text = text.encode('utf-8')
243 if (len(text) != 0) and (text[-1] == '\n'):
247 textmd5 = md5.hexdigest()
250 'title': mw.metadir.filename_to_pagename(file[:-5]),
254 'summary': edit_summary,
258 response = self.api.call(data)
259 if response['edit']['result'] == 'Success':
260 if 'nochange' in response['edit']:
261 print 'warning: no changes detected in %s - ' \
262 'skipping and removing ending LF' % file
263 self.metadir.clean_page(file[:-5])
265 if response['edit']['oldrevid'] != revid:
266 print 'warning: edit conflict detected on %s -- ' \
271 'revids': response['edit']['newrevid'],
272 'prop': 'info|revisions',
274 'ids|flags|timestamp|user|comment|content',
276 response = self.api.call(data)['query']['pages']
277 self.metadir.pages_add_rv(int(pageid),
278 response[pageid]['revisions'][0])
280 print 'error: committing %s failed: %s' % \
281 (file, response['edit']['result'])