|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 4 | + |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import optparse |
| 8 | +import json |
| 9 | + |
| 10 | +from ..core import AtWikiAPI |
| 11 | +from ..uri import AtWikiURI |
| 12 | + |
| 13 | +class AtWikiDump(object): |
| 14 | + """ |
| 15 | + ``atwiki-dump`` is a simple tool to dump every Wiki source. |
| 16 | +
|
| 17 | + Restrictions: |
| 18 | + - Contents other than Wiki source, page number and page name will not be dumped. |
| 19 | + This means that followings are not included in the dump: |
| 20 | + - WYSIWYG mode (i.e., syntax of the source) |
| 21 | + - history backup |
| 22 | + - tags |
| 23 | + - ACLs |
| 24 | + - No consistency between pages. |
| 25 | + """ |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def start(cls, args): |
| 29 | + USAGE = 'atwiki-dump [--output-dir OUTPUT_DIR] WIKI_URI' |
| 30 | + |
| 31 | + parser = optparse.OptionParser(description='@wiki Dump Tool', usage=USAGE) |
| 32 | + parser.add_option('--output-dir', '-o', type='string', default='.', |
| 33 | + help='directory to output Wiki source') |
| 34 | + |
| 35 | + (parsed, target) = parser.parse_args(args) |
| 36 | + if len(target) != 1: |
| 37 | + parser.error('A single WIKI_URI must be specified') |
| 38 | + |
| 39 | + output_dir = parsed.output_dir |
| 40 | + target_site = target[0] |
| 41 | + |
| 42 | + api = AtWikiAPI(AtWikiURI(target_site)) |
| 43 | + page_meta = {'version': 1, 'meta': {}} |
| 44 | + |
| 45 | + for page in api.get_list(): |
| 46 | + page_id = page['id'] |
| 47 | + page_name = page['name'] |
| 48 | + page_meta['meta'][page_id] = {'name': page_name} |
| 49 | + path = '{0}/{1}.wiki'.format(output_dir, page_id) |
| 50 | + |
| 51 | + print('dumping: page {0} ({1}) to {2}'.format(page_id, page_name, path)) |
| 52 | + page_src = api.get_source(page_id) |
| 53 | + with open(path, 'w') as f: |
| 54 | + f.write(page_src) |
| 55 | + time.sleep(1) |
| 56 | + |
| 57 | + path = '{0}/meta.json'.format(output_dir) |
| 58 | + print('dumping: meta data to {0}'.format(path)) |
| 59 | + with open(path, 'w') as f: |
| 60 | + json.dump(page_meta, f) |
| 61 | + print('done!') |
| 62 | + |
| 63 | + return 0 |
| 64 | + |
| 65 | +def main(): |
| 66 | + sys.exit(AtWikiDump.start(sys.argv[1:])) |
0 commit comments