2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009 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/>.
25 class Metadir(object):
28 self.me = os.path.basename(sys.argv[0])
31 if '.mw' in os.listdir(root):
34 head = os.path.split(root)[0]
36 self.root = os.getcwd()
39 self.location = os.path.join(self.root, '.mw')
40 self.config_loc = os.path.join(self.location, 'config')
41 if os.path.isdir(self.location) and \
42 os.path.isfile(self.config_loc):
43 self.config = ConfigParser.RawConfigParser()
44 self.config.read(self.config_loc)
48 def create(self, api_url):
49 # create the directory
50 if os.path.isdir(self.location):
51 print '%s: you are already in a mw repo' % self.me
54 os.mkdir(self.location, 0755)
56 fd = file(os.path.join(self.location, 'version'), 'w')
57 fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
60 self.config = ConfigParser.RawConfigParser()
61 self.config.add_section('remote')
62 self.config.set('remote', 'api_url', api_url)
63 with open(self.config_loc, 'wb') as config_file:
64 self.config.write(config_file)
66 os.mkdir(os.path.join(self.location, 'cache'))
67 # create cache/pagedict
68 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
69 fd.write(json.dumps({}))
72 os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
74 def pagedict_add(self, pagename, pageid, currentrv):
75 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
76 pagedict = json.loads(fd.read())
77 pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
79 fd.write(json.dumps(pagedict))
83 def get_pageid_from_pagename(self, pagename):
84 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
85 pagedict = json.loads(fd.read())
86 if pagename in pagedict.keys():
87 return pagedict[pagename]
91 def pages_add_rv(self, pageid, rv):
92 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
93 fd = file(pagefile, 'w+')
94 pagedata_raw = fd.read()
95 if pagedata_raw == '':
98 pagedata = json.loads(pagedata_raw)
99 rvid = int(rv['revid'])
101 'user': rv['user'], 'timestamp': rv['timestamp'],
105 fd.write(json.dumps(pagedata))
109 def pages_get_rv_list(self, pageid):
110 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
111 fd = file(pagefile, 'r')
112 pagedata = json.loads(fd.read())
113 rvs = [int(x) for x in pagedata.keys()]
117 def pages_get_rv(self, pageid, rvid):
118 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
119 fd = file(pagefile, 'r')
120 pagedata = json.loads(fd.read())
121 return pagedata[str(rvid)]