2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2011 Ian Weller <ian@ianweller.org> and others
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
32 class CommandBase(object):
34 def __init__(self, name, description, usage=None):
35 self.me = os.path.basename(sys.argv[0])
36 self.description = description
38 usage = '%prog ' + name
40 usage = '%%prog %s %s' % (name, usage)
41 self.parser = OptionParser(usage=usage, description=description)
43 self.metadir = mw.metadir.Metadir()
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)'
63 elif result['login']['result'] == 'NeedToken':
64 print 'Login with token'
65 result = self.api.call({'action': 'login',
68 'lgtoken': result['login']['token']})
69 if result['login']['result'] == 'Success':
70 print 'Login successful! (yay)'
72 print 'Login failed: %s' % result['login']['result']
74 print 'Login failed: %s' % result['login']['result']
76 def _die_if_no_init(self):
77 if self.metadir.config is None:
78 print '%s: not a mw repo' % self.me
82 cookie_filename = os.path.join(self.metadir.location, 'cookies')
83 self.api_url = self.metadir.config.get('remote', 'api_url')
84 self.api = simplemediawiki.MediaWiki(self.api_url,
85 cookie_file=cookie_filename)
88 class InitCommand(CommandBase):
92 CommandBase.__init__(self, 'init', 'start a mw repo', usage)
94 def _do_command(self):
95 if len(self.args) < 1:
96 self.parser.error('must have URL to remote api.php')
97 elif len(self.args) > 1:
98 self.parser.error('too many arguments')
99 self.metadir.create(self.args[0])
102 class LoginCommand(CommandBase):
105 CommandBase.__init__(self, 'login', 'authenticate with wiki')
107 def _do_command(self):
108 self._die_if_no_init()
113 class LogoutCommand(CommandBase):
116 CommandBase.__init__(self, 'logout', 'forget authentication')
118 def _do_command(self):
119 self._die_if_no_init()
121 os.unlink(os.path.join(self.metadir.location, 'cookies'))
126 class PullCategoryMembersCommand(CommandBase):
129 usage = '[options] PAGENAME ...'
130 CommandBase.__init__(self, 'pull_commandat', 'add remote pages to repo '
131 'belonging to the given category', usage)
133 def _do_command(self):
134 self._die_if_no_init()
138 for these_pages in [pages[i:i + 25] for i in range(0, len(pages), 25)]:
141 'gcmtitle': '|'.join(these_pages),
142 'generator': 'categorymembers',
145 response = self.api.call(data)['query']['pages']
146 for pageid in response.keys():
147 pagename = response[pageid]['title']
149 pull_command = PullCommand()
150 pull_command.args = [pagename.encode('utf-8')]
151 pull_command._do_command()
154 class PullCommand(CommandBase):
157 usage = '[options] PAGENAME ...'
158 CommandBase.__init__(self, 'pull', 'add remote pages to repo', usage)
160 def _do_command(self):
161 self._die_if_no_init()
166 # Pull should work with pagename, filename, or working directory
169 pages = self.metadir.working_dir_status().keys()
170 for pagename in pages:
171 if '.wiki' in pagename:
172 converted_pages.append(
173 mw.metadir.filename_to_pagename(pagename[:-5]))
175 converted_pages.append(pagename)
176 pages = converted_pages
178 for these_pages in [pages[i:i + 25] for i in range(0, len(pages), 25)]: # XXX ?
181 'titles': '|'.join(these_pages),
182 'prop': 'info|revisions',
183 'rvprop': 'ids|flags|timestamp|user|comment|content',
185 response = self.api.call(data)['query']['pages']
186 # for every pageid, returns dict.keys() = {'lastrevid', 'pageid', 'title', 'counter', 'length', 'touched': u'2011-02-02T19:32:04Z', 'ns', 'revisions' {...}}
187 for pageid in response.keys():
188 pagename = response[pageid]['title']
190 # If no revisions, then error, perhaps page deleted
191 if 'revisions' not in response[pageid]:
192 print 'skipping: "%s" -- cannot find page, perhaps deleted' % (pagename)
195 # Is the revisions list a sorted one, should I use [0] or [-1]? -- reagle
196 if 'comment' in response[pageid]['revisions'][0]:
197 last_wiki_rev_comment = response[pageid]['revisions'][0]['comment']
199 last_wiki_rev_comment = ''
200 last_wiki_rev_user = response[pageid]['revisions'][0]['user']
202 # check if working file is modified or if wiki page doesn't exists
203 status = self.metadir.working_dir_status()
204 filename = mw.metadir.pagename_to_filename(pagename)
205 full_filename = os.path.join(self.metadir.root, filename + '.wiki')
206 if filename + '.wiki' in status and \
207 status[filename + '.wiki' ] in ['M']:
208 print 'skipping: "%s" -- uncommitted modifications ' % (pagename)
210 if 'missing' in response[pageid].keys():
211 print 'error: "%s": -- page does not exist, file not created' % \
215 wiki_revids = sorted([x['revid'] for x in response[pageid]['revisions']])
216 last_wiki_revid = wiki_revids[-1]
217 working_revids = sorted(self.metadir.pages_get_rv_list({'id' : pageid}))
218 last_working_revid = working_revids[-1]
219 if ( os.path.exists(full_filename) and
220 last_wiki_revid == last_working_revid):
221 print 'wiki unchanged: "%s"' % (pagename)
223 print 'pulling: "%s" : "%s" by "%s"' % (
224 pagename, last_wiki_rev_comment, last_wiki_rev_user)
225 self.metadir.pagedict_add(pagename, pageid, last_wiki_revid)
226 self.metadir.pages_add_rv(int(pageid),
227 response[pageid]['revisions'][0])
228 with file(full_filename, 'w') as fd:
229 data = response[pageid]['revisions'][0]['*']
230 data = data.encode('utf-8')
233 class StatusCommand(CommandBase):
236 CommandBase.__init__(self, 'status', 'check repo status')
237 self.shortcuts.append('st')
239 def _do_command(self):
240 self._die_if_no_init()
241 status = self.metadir.working_dir_status()
242 for filename in status:
243 print '%s %s' % (status[filename], filename)
246 class DiffCommand(CommandBase):
249 CommandBase.__init__(self, 'diff', 'diff wiki to working directory')
251 def _do_command(self):
252 self._die_if_no_init()
253 status = self.metadir.working_dir_status()
254 for filename in status:
255 if status[filename] == 'M':
256 print self.metadir.diff_rv_to_working(
257 mw.metadir.filename_to_pagename(filename[:-5])),
260 class MergeCommand(CommandBase):
263 CommandBase.__init__(self, 'merge', 'merge local and wiki copies', usage)
265 def _do_command(self):
266 self._die_if_no_init()
267 self.merge_tool = self.metadir.config.get('merge', 'tool')
268 status = self.metadir.working_dir_status()
269 for filename in status:
270 if status[filename] == 'M':
271 full_filename = os.path.join(self.metadir.root, filename)
272 pagename = mw.metadir.filename_to_pagename(filename[:-5])
273 # mv local to filename.wiki.local
274 os.rename(full_filename, full_filename + '.local')
276 pull_command = PullCommand()
277 pull_command.args = [pagename.encode('utf-8')]
278 pull_command._do_command()
279 # mv remote to filename.wiki.remote
280 os.rename(full_filename, full_filename + '.remote')
282 merge_command = self.merge_tool % (full_filename + '.local',
283 full_filename + '.remote', full_filename + '.merge')
284 subprocess.call(merge_command.split(' '))
285 # mv filename.merge filename and delete tmp files
286 os.rename(full_filename + '.merge', full_filename)
287 os.remove(full_filename + '.local')
288 os.remove(full_filename + '.remote')
290 commit_command = CommitCommand()
291 commit_command.args = [pagename.encode('utf-8')]
292 commit_command._do_command()
295 class CommitCommand(CommandBase):
299 CommandBase.__init__(self, 'commit', 'commit changes to wiki', usage)
300 self.shortcuts.append('ci')
301 self.parser.add_option('-m', '--message', dest='edit_summary',
302 help='don\'t prompt for edit summary and '
304 self.parser.add_option('-b', '--bot', dest='bot', action='store_true',
305 help='mark actions as a bot (won\'t affect '
306 'anything if you don\'t have the bot right',
309 def _do_command(self):
310 self._die_if_no_init()
312 files_to_commit = 0 # how many files to process
313 status = self.metadir.working_dir_status(files=self.args)
314 for filename in status:
315 print '%s %s' % (status[filename], filename)
316 if status[filename] in ['M']:
318 if not files_to_commit:
319 print 'nothing to commit'
321 if self.options.edit_summary == None:
322 print 'Edit summary:',
323 edit_summary = raw_input()
325 edit_summary = self.options.edit_summary
326 for filename in status:
327 if status[filename] in ['M']:
332 'prop': 'info|revisions',
334 'titles': mw.metadir.filename_to_pagename(filename[:-5]),
336 response = self.api.call(data)
337 pages = response['query']['pages']
338 pageid = pages.keys()[0]
339 revid = pages[pageid]['revisions'][0]['revid']
341 self.metadir.pages_get_rv_list({'id': pageid})[0]
342 if revid != awaitedrevid:
343 print 'warning: edit conflict detected on "%s" (%s -> %s) ' \
344 '-- skipping! (try merge)' % (filename, awaitedrevid, revid)
346 edittoken = pages[pageid]['edittoken']
347 full_filename = os.path.join(self.metadir.root, filename)
348 text = codecs.open(full_filename, 'r', 'utf-8').read()
349 text = text.encode('utf-8')
350 if (len(text) != 0) and (text[-1] == '\n'):
354 textmd5 = md5.hexdigest()
357 'title': mw.metadir.filename_to_pagename(filename[:-5]),
361 'summary': edit_summary,
365 response = self.api.call(data)
366 if 'error' in response:
367 if 'code' in response['error']:
368 if response['error']['code'] == 'permissiondenied':
369 print 'Permission denied -- try running "mw login"'
371 if response['edit']['result'] == 'Success':
372 if 'nochange' in response['edit']:
373 print 'warning: no changes detected in %s - ' \
374 'skipping and removing ending LF' % filename
375 pagename = mw.metadir.filename_to_pagename(filename[:-5])
376 self.metadir.clean_page(pagename)
378 if response['edit']['oldrevid'] != revid:
379 print 'warning: edit conflict detected on %s (%s -> %s) ' \
380 '-- skipping!' % (file,
381 response['edit']['oldrevid'], revid)
385 'revids': response['edit']['newrevid'],
386 'prop': 'info|revisions',
388 'ids|flags|timestamp|user|comment|content',
390 response = self.api.call(data)['query']['pages']
391 self.metadir.pages_add_rv(int(pageid),
392 response[pageid]['revisions'][0])
393 # need to write latest rev to file too, as text may be changed
394 #such as a sig, e.g., -~ => -[[User:Reagle|Reagle]]
395 with file(full_filename, 'w') as fd:
396 data = response[pageid]['revisions'][0]['*']
397 data = data.encode('utf-8')
400 print 'waiting 3s before processing the next file'
403 print 'error: committing %s failed: %s' % \
404 (filename, response['edit']['result'])