_login() now actually logs in to remote wiki
[mw] / src / mw / metadir.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009  Ian Weller <ian@ianweller.org>
4 #
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.
9 #
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.
14 #
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/>.
17 ###
18
19 import ConfigParser
20 import json
21 import os
22 import sys
23 import time
24
25 class Metadir(object):
26     def __init__(self):
27         self.me = os.path.basename(sys.argv[0])
28         root = os.getcwd()
29         while True:
30             if '.mw' in os.listdir(root):
31                 self.root = root
32                 break
33             (head, tail) = os.path.split(root)
34             if head == root:
35                 self.root = os.getcwd()
36                 break
37             root = head
38         self.location = os.path.join(self.root, '.mw')
39         self.config_loc = os.path.join(self.location, 'config')
40         if os.path.isdir(self.location) and \
41            os.path.isfile(self.config_loc):
42             self.config = ConfigParser.RawConfigParser()
43             self.config.read(self.config_loc)
44         else:
45             self.config = None
46
47     def create(self, api_url):
48         # create the directory
49         if os.path.isdir(self.location):
50             print '%s: you are already in a mw repo' % self.me
51             sys.exit(1)
52         else:
53             os.mkdir(self.location, 0755)
54         # metadir versioning
55         fd = file(os.path.join(self.location, 'version'), 'w')
56         fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
57         fd.close()
58         # create config
59         self.config = ConfigParser.RawConfigParser()
60         self.config.add_section('remote')
61         self.config.set('remote', 'api_url', api_url)
62         with open(self.config_loc, 'wb') as config_file:
63             self.config.write(config_file)
64         # create cache/
65         os.mkdir(os.path.join(self.location, 'cache'))
66         # create cache/pagedict
67         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
68         fd.write(json.dumps({}))
69         fd.close()
70         # create cache/pages/
71         os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
72
73     def pagedict_add(self, pagename, pageid, currentrv):
74         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
75         pagedict = json.loads(fd.read())
76         pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
77         fd.seek(0)
78         fd.write(json.dumps(pagedict))
79         fd.truncate()
80         fd.close()
81
82     def get_pageid_from_pagename(self, pagename):
83         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
84         pagedict = json.loads(fd.read())
85         if pagename in pagedict.keys():
86             return pagedict[pagename]
87         else:
88             return None
89
90     def pages_add_rv(self, pageid, rv):
91         pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
92         fd = file(pagefile, 'w+')
93         pagedata_raw = fd.read()
94         if pagedata_raw == '':
95             pagedata = {}
96         else:
97             pagedata = json.loads(pagedata_raw)
98         rvid = int(rv['revid'])
99         pagedata[rvid] = {
100                 'user': rv['user'], 'timestamp': rv['timestamp'],
101                 'content': rv['*'],
102         }
103         fd.seek(0)
104         fd.write(json.dumps(pagedata))
105         fd.truncate()
106         fd.close()
107
108     def pages_get_rv_list(self, pageid):
109         pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
110         fd = file(pagefile, 'r')
111         pagedata = json.loads(fd.read())
112         rvs = [int(x) for x in pagedata.keys()]
113         rvs.sort()
114         return rvs
115
116     def pages_get_rv(self, pageid, rvid):
117         pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
118         fd = file(pagefile, 'r')
119         pagedata = json.loads(fd.read())
120         return pagedata[str(rvid)]

Benjamin Mako Hill || Want to submit a patch?