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/>.
27 class Metadir(object):
30 self.me = os.path.basename(sys.argv[0])
33 if '.mw' in os.listdir(root):
36 head = os.path.split(root)[0]
38 self.root = os.getcwd()
41 self.location = os.path.join(self.root, '.mw')
42 self.config_loc = os.path.join(self.location, 'config')
43 if os.path.isdir(self.location) and \
44 os.path.isfile(self.config_loc):
45 self.config = ConfigParser.RawConfigParser()
46 self.config.read(self.config_loc)
50 def create(self, api_url):
51 # create the directory
52 if os.path.isdir(self.location):
53 print '%s: you are already in a mw repo' % self.me
56 os.mkdir(self.location, 0755)
58 fd = file(os.path.join(self.location, 'version'), 'w')
59 fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
62 self.config = ConfigParser.RawConfigParser()
63 self.config.add_section('remote')
64 self.config.set('remote', 'api_url', api_url)
65 with open(self.config_loc, 'wb') as config_file:
66 self.config.write(config_file)
68 os.mkdir(os.path.join(self.location, 'cache'))
69 # create cache/pagedict
70 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
71 fd.write(json.dumps({}))
74 os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
76 def pagedict_add(self, pagename, pageid, currentrv):
77 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
78 pagedict = json.loads(fd.read())
79 pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
81 fd.write(json.dumps(pagedict))
85 def get_pageid_from_pagename(self, pagename):
86 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
87 pagedict = json.loads(fd.read())
88 if pagename in pagedict.keys():
89 return pagedict[pagename]
93 def pages_add_rv(self, pageid, rv):
94 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
95 fd = file(pagefile, 'w+')
96 pagedata_raw = fd.read()
97 if pagedata_raw == '':
100 pagedata = json.loads(pagedata_raw)
101 rvid = int(rv['revid'])
103 'user': rv['user'], 'timestamp': rv['timestamp']
106 pagedata[rvid]['content'] = rv['*']
108 fd.write(json.dumps(pagedata))
112 def pages_get_rv_list(self, pageid):
113 pagefile = os.path.join(self.location, 'cache', 'pages',
115 fd = file(pagefile, 'r')
116 pagedata = json.loads(fd.read())
117 rvs = [int(x) for x in pagedata.keys()]
121 def pages_get_rv(self, pageid, rvid):
122 pagefile = os.path.join(self.location, 'cache', 'pages',
124 fd = file(pagefile, 'r')
125 pagedata = json.loads(fd.read())
126 return pagedata[str(rvid)]
128 def working_dir_status(self):
131 for root, dirs, files in os.walk(self.root):
132 if root == self.root:
135 check.append(os.path.join(root, name))
138 name = os.path.split(full)[1]
139 if name[-5:] == '.wiki':
140 pagename = mw.api.filename_to_pagename(name[:-5])
141 pageid = self.get_pageid_from_pagename(pagename)
143 status[os.path.relpath(full, self.root)] = '?'
145 rvid = self.pages_get_rv_list(pageid)[-1]
146 rv = self.pages_get_rv(pageid, rvid)
147 cur_content = codecs.open(full, 'r', 'utf-8').read()
148 if cur_content != rv['content']:
149 status[os.path.relpath(full, self.root)] = 'U'