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
29 class CommandBase(object):
31 def __init__(self, name, description, usage=None):
32 self.me = os.path.basename(sys.argv[0])
33 self.description = description
35 usage = '%prog ' + name
37 usage = '%%prog %s %s' % (name, usage)
38 self.parser = OptionParser(usage=usage, description=description)
40 self.metadir = mw.metadir.Metadir()
44 (self.options, self.args) = self.parser.parse_args()
45 self.args = self.args[1:] # don't need the first thing
48 def _do_command(self):
52 user = raw_input('Username: ')
53 passwd = getpass.getpass()
54 result = self.api.call({'action': 'login',
56 'lgpassword': passwd})
57 if result['login']['result'] == 'Success':
58 # cookies are saved to a file
59 print 'Login successful! (yay)'
60 elif result['login']['result'] == 'NeedToken':
61 print 'Login with token'
62 result = self.api.call({'action': 'login',
65 'lgtoken': result['login']['token']})
66 if result['login']['result'] == 'Success':
67 print 'Login successful! (yay)'
69 print 'Login failed: %s' % result['login']['result']
71 print 'Login failed: %s' % result['login']['result']
73 def _die_if_no_init(self):
74 if self.metadir.config is None:
75 print '%s: not a mw repo' % self.me
79 self.api_url = self.metadir.config.get('remote', 'api_url')
80 self.api = mw.api.API(self.api_url, self.metadir)
83 class InitCommand(CommandBase):
87 CommandBase.__init__(self, 'init', 'start a mw repo', usage)
89 def _do_command(self):
90 if len(self.args) < 1:
91 self.parser.error('must have URL to remote api.php')
92 elif len(self.args) > 1:
93 self.parser.error('too many arguments')
94 self.metadir.create(self.args[0])
97 class LoginCommand(CommandBase):
100 CommandBase.__init__(self, 'login', 'authenticate with wiki')
102 def _do_command(self):
103 self._die_if_no_init()
108 class LogoutCommand(CommandBase):
111 CommandBase.__init__(self, 'logout', 'forget authentication')
113 def _do_command(self):
114 self._die_if_no_init()
116 os.unlink(os.path.join(self.metadir.location, 'cookies'))
121 class PullCommand(CommandBase):
124 usage = '[options] PAGENAME ...'
125 CommandBase.__init__(self, 'pull', 'add remote pages to repo', usage)
127 def _do_command(self):
128 self._die_if_no_init()
132 for these_pages in [pages[i:i + 25] for i in range(0, len(pages), 25)]:
135 'titles': '|'.join(these_pages),
136 'prop': 'info|revisions',
137 'rvprop': 'ids|flags|timestamp|user|comment|content',
139 response = self.api.call(data)['query']['pages']
140 for pageid in response.keys():
141 pagename = response[pageid]['title']
142 if 'missing' in response[pageid].keys():
143 print '%s: %s: page does not exist, file not created' % \
146 revids = [x['revid'] for x in response[pageid]['revisions']]
148 self.metadir.pagedict_add(pagename, pageid, revids[-1])
149 self.metadir.pages_add_rv(int(pageid),
150 response[pageid]['revisions'][0])
151 filename = mw.api.pagename_to_filename(pagename)
152 with file(os.path.join(self.metadir.root, filename + '.wiki'),
154 data = response[pageid]['revisions'][0]['*']
155 data = data.encode('utf-8')
159 class StatusCommand(CommandBase):
162 CommandBase.__init__(self, 'status', 'check repo status')
163 self.shortcuts.append('st')
165 def _do_command(self):
166 self._die_if_no_init()
167 status = self.metadir.working_dir_status()
169 print '%s %s' % (status[file], file)
172 class DiffCommand(CommandBase):
175 CommandBase.__init__(self, 'diff', 'diff wiki to working directory')
177 def _do_command(self):
178 self._die_if_no_init()
179 status = self.metadir.working_dir_status()
181 if status[file] == 'U':
182 print self.metadir.diff_rv_to_working(
183 mw.api.filename_to_pagename(file[:-5])),
186 class CommitCommand(CommandBase):
190 CommandBase.__init__(self, 'commit', 'commit changes to wiki', usage)
191 self.shortcuts.append('ci')
192 self.parser.add_option('-m', '--message', dest='edit_summary',
193 help='don\'t prompt for edit summary and '
195 self.parser.add_option('-b', '--bot', dest='bot', action='store_true',
196 help='mark actions as a bot (won\'t affect '
197 'anything if you don\'t have the bot right',
200 def _do_command(self):
201 self._die_if_no_init()
203 status = self.metadir.working_dir_status(files=self.args)
204 nothing_to_commit = True
206 print '%s %s' % (status[file], file)
207 if status[file] in ['U']:
208 nothing_to_commit = False
209 if nothing_to_commit:
210 print 'nothing to commit'
212 if self.options.edit_summary == None:
213 print 'Edit summary:',
214 edit_summary = raw_input()
216 edit_summary = self.options.edit_summary
218 if status[file] in ['U']:
222 'prop': 'info|revisions',
224 'titles': mw.api.filename_to_pagename(file[:-5]),
226 response = self.api.call(data)
227 pageid = response['query']['pages'].keys()[0]
228 revid = response['query']['pages'][pageid]['revisions'][0]\
230 awaitedrevid = self.metadir.pages_get_rv_list({'id': pageid})\
232 if revid != awaitedrevid:
233 print 'warning: edit conflict detected on %s (%s -> %s) ' \
234 '-- skipping!' % (file, awaitedrevid, revid)
236 edittoken = response['query']['pages'][pageid]['edittoken']
237 filename = os.path.join(self.metadir.root, file)
238 text = codecs.open(filename, 'r', 'utf-8').read()
239 text = text.encode('utf-8')
240 if (len(text) != 0) and (text[-1] == '\n'):
244 textmd5 = md5.hexdigest()
247 'title': mw.api.filename_to_pagename(file[:-5]),
251 'summary': edit_summary,
255 response = self.api.call(data)
256 if response['edit']['result'] == 'Success':
257 if 'nochange' in response['edit']:
258 print 'warning: no changes detected in %s - ' \
259 'skipping and removing ending LF' % file
260 self.metadir.clean_page(file[:-5])
262 if response['edit']['oldrevid'] != revid:
263 print 'warning: edit conflict detected on %s -- ' \
268 'revids': response['edit']['newrevid'],
269 'prop': 'info|revisions',
271 'ids|flags|timestamp|user|comment|content',
273 response = self.api.call(data)['query']['pages']
274 self.metadir.pages_add_rv(int(pageid),
275 response[pageid]['revisions'][0])
277 print 'error: committing %s failed: %s' % \
278 (file, response['edit']['result'])