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/>.
24 from StringIO import StringIO
28 class Metadir(object):
31 self.me = os.path.basename(sys.argv[0])
34 if '.mw' in os.listdir(root):
37 head = os.path.split(root)[0]
39 self.root = os.getcwd()
42 self.location = os.path.join(self.root, '.mw')
43 self.config_loc = os.path.join(self.location, 'config')
44 if os.path.isdir(self.location) and \
45 os.path.isfile(self.config_loc):
46 self.config = ConfigParser.RawConfigParser()
47 self.config.read(self.config_loc)
51 def save_config(self):
52 with open(self.config_loc, 'wb') as config_file:
53 self.config.write(config_file)
55 def create(self, api_url):
56 # create the directory
57 if os.path.isdir(self.location):
58 print '%s: you are already in a mw repo' % self.me
61 os.mkdir(self.location, 0755)
63 fd = file(os.path.join(self.location, 'version'), 'w')
64 fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
67 self.config = ConfigParser.RawConfigParser()
68 self.config.add_section('remote')
69 self.config.set('remote', 'api_url', api_url)
72 os.mkdir(os.path.join(self.location, 'cache'))
73 # create cache/pagedict
74 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
75 fd.write(json.dumps({}))
78 os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
80 def clean_page(self, pagename):
81 filename = pagename_to_filename(pagename) + '.wiki'
82 cur_content = codecs.open(filename, 'r', 'utf-8').read()
83 if len(cur_content) != 0 and cur_content[-1] == '\n':
84 cur_content = cur_content[:-1]
85 fd = file(filename, 'w')
86 fd.write(cur_content.encode('utf-8'))
89 def pagedict_add(self, pagename, pageid, currentrv):
90 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
91 pagedict = json.loads(fd.read())
92 pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
94 fd.write(json.dumps(pagedict))
98 def get_pageid_from_pagename(self, pagename):
99 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
100 pagedict = json.loads(fd.read())
101 pagename = pagename.decode('utf-8')
102 if pagename in pagedict.keys():
103 return pagedict[pagename]
107 def pages_add_rv(self, pageid, rv):
108 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
109 fd = file(pagefile, 'w+')
110 pagedata_raw = fd.read()
111 if pagedata_raw == '':
114 pagedata = json.loads(pagedata_raw)
115 rvid = int(rv['revid'])
118 'timestamp': rv['timestamp'],
121 pagedata[rvid]['content'] = rv['*']
123 fd.write(json.dumps(pagedata))
127 def pages_get_rv_list(self, pageid):
128 pagefile = os.path.join(self.location, 'cache', 'pages',
130 fd = file(pagefile, 'r')
131 pagedata = json.loads(fd.read())
132 rvs = [int(x) for x in pagedata.keys()]
136 def pages_get_rv(self, pageid, rvid):
137 pagefile = os.path.join(self.location, 'cache', 'pages',
139 fd = file(pagefile, 'r')
140 pagedata = json.loads(fd.read())
141 return pagedata[str(rvid)]
143 def working_dir_status(self, files=None):
146 if files == None or files == []:
147 for root, dirs, files in os.walk(self.root):
148 if root == self.root:
151 check.append(os.path.join(root, name))
154 check.append(os.path.join(os.getcwd(), file))
157 name = os.path.split(full)[1]
158 if name[-5:] == '.wiki':
159 pagename = filename_to_pagename(name[:-5])
160 pageid = self.get_pageid_from_pagename(pagename)
162 status[os.path.relpath(full, self.root)] = '?'
164 rvid = self.pages_get_rv_list(pageid)[-1]
165 rv = self.pages_get_rv(pageid, rvid)
166 cur_content = codecs.open(full, 'r', 'utf-8').read()
167 if (len(cur_content) != 0) and (cur_content[-1] == '\n'):
168 cur_content = cur_content[:-1]
169 if cur_content != rv['content']:
170 status[os.path.relpath(full, self.root)] = 'M'
173 def diff_rv_to_working(self, pagename, oldrvid=0, newrvid=0):
174 # oldrvid=0 means latest fetched revision
175 # newrvid=0 means working copy
176 filename = pagename_to_filename(pagename) + '.wiki'
177 filename = filename.decode('utf-8')
178 pageid = self.get_pageid_from_pagename(pagename)
180 raise ValueError('page named %s has not been fetched' % pagename)
183 oldrvid = self.pages_get_rv_list(pageid)[-1]
184 oldrv = self.pages_get_rv(pageid, oldrvid)
185 oldname = 'a/%s (revision %i)' % (filename, oldrvid)
186 old = [i + '\n' for i in \
187 oldrv['content'].encode('utf-8').split('\n')]
189 cur_content = codecs.open(filename, 'r', 'utf-8').read()
190 cur_content = cur_content.encode('utf-8')
191 if (len(cur_content) != 0) and (cur_content[-1] == '\n'):
192 cur_content = cur_content[:-1]
193 newname = 'b/%s (working copy)' % filename
194 new = [i + '\n' for i in cur_content.split('\n')]
196 newrv = self.pages_get_rv(pageid, newrvid)
197 newname = 'b/%s (revision %i)' % (filename, newrvid)
198 new = [i + '\n' for i in newrv['content'].split('\n')]
200 bzrlib.diff.internal_diff(oldname, old, newname, new, diff_fd)
201 diff = diff_fd.getvalue()
207 def pagename_to_filename(name):
208 name = name.replace(' ', '_')
209 name = name.replace('/', '!')
213 def filename_to_pagename(name):
214 name = name.replace('!', '/')
215 name = name.replace('_', ' ')