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/>.
25 from StringIO import StringIO
29 class Metadir(object):
32 self.me = os.path.basename(sys.argv[0])
35 if '.mw' in os.listdir(root):
38 head = os.path.split(root)[0]
40 self.root = os.getcwd()
43 self.location = os.path.join(self.root, '.mw')
44 self.config_loc = os.path.join(self.location, 'config')
45 if os.path.isdir(self.location) and \
46 os.path.isfile(self.config_loc):
47 self.config = ConfigParser.RawConfigParser()
48 self.config.read(self.config_loc)
52 def save_config(self):
53 with open(self.config_loc, 'wb') as config_file:
54 self.config.write(config_file)
56 def create(self, api_url):
57 # create the directory
58 if os.path.isdir(self.location):
59 print '%s: you are already in a mw repo' % self.me
62 os.mkdir(self.location, 0755)
64 fd = file(os.path.join(self.location, 'version'), 'w')
65 fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
68 self.config = ConfigParser.RawConfigParser()
69 self.config.add_section('remote')
70 self.config.set('remote', 'api_url', api_url)
73 os.mkdir(os.path.join(self.location, 'cache'))
74 # create cache/pagedict
75 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
76 fd.write(json.dumps({}))
79 os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
83 def clean_page(self, pagename):
84 filename = mw.api.pagename_to_filename(pagename) + '.wiki'
85 cur_content = codecs.open(filename, 'r', 'utf-8').read()
86 if ( (len(cur_content) != 0) and (cur_content[-1] == '\n') ):
87 cur_content = cur_content[:-1]
89 fd = file(filename, 'w')
90 fd.write(cur_content.encode('utf-8'))
93 def pagedict_add(self, pagename, pageid, currentrv):
94 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
95 pagedict = json.loads(fd.read())
96 pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
98 fd.write(json.dumps(pagedict))
102 def get_pageid_from_pagename(self, pagename):
103 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
104 pagedict = json.loads(fd.read())
105 pagename = pagename.decode('utf-8')
106 if pagename in pagedict.keys():
107 return pagedict[pagename]
111 def pages_add_rv(self, pageid, rv):
112 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
113 fd = file(pagefile, 'w+')
114 pagedata_raw = fd.read()
115 if pagedata_raw == '':
118 pagedata = json.loads(pagedata_raw)
119 rvid = int(rv['revid'])
122 'timestamp': rv['timestamp'],
125 pagedata[rvid]['content'] = rv['*']
127 fd.write(json.dumps(pagedata))
131 def pages_get_rv_list(self, pageid):
132 pagefile = os.path.join(self.location, 'cache', 'pages',
134 fd = file(pagefile, 'r')
135 pagedata = json.loads(fd.read())
136 rvs = [int(x) for x in pagedata.keys()]
140 def pages_get_rv(self, pageid, rvid):
141 pagefile = os.path.join(self.location, 'cache', 'pages',
143 fd = file(pagefile, 'r')
144 pagedata = json.loads(fd.read())
145 return pagedata[str(rvid)]
147 def working_dir_status(self, files=None):
150 if files == None or files == []:
151 for root, dirs, files in os.walk(self.root):
152 if root == self.root:
155 check.append(os.path.join(root, name))
158 check.append(os.path.join(os.getcwd(), file))
161 name = os.path.split(full)[1]
162 if name[-5:] == '.wiki':
163 pagename = mw.api.filename_to_pagename(name[:-5])
164 pageid = self.get_pageid_from_pagename(pagename)
166 status[os.path.relpath(full, self.root)] = '?'
168 rvid = self.pages_get_rv_list(pageid)[-1]
169 rv = self.pages_get_rv(pageid, rvid)
170 cur_content = codecs.open(full, 'r', 'utf-8').read()
171 if (len(cur_content) != 0) and (cur_content[-1] == '\n'):
172 cur_content = cur_content[:-1]
173 if cur_content != rv['content']:
174 status[os.path.relpath(full, self.root)] = 'U'
177 def diff_rv_to_working(self, pagename, oldrvid=0, newrvid=0):
178 # oldrvid=0 means latest fetched revision
179 # newrvid=0 means working copy
180 filename = mw.api.pagename_to_filename(pagename) + '.wiki'
181 filename = filename.decode('utf-8')
182 pageid = self.get_pageid_from_pagename(pagename)
184 raise ValueError('page named %s has not been fetched' % pagename)
187 oldrvid = self.pages_get_rv_list(pageid)[-1]
188 oldrv = self.pages_get_rv(pageid, oldrvid)
189 oldname = 'a/%s (revision %i)' % (filename, oldrvid)
190 old = [i + '\n' for i in oldrv['content'].encode('utf-8').split('\n')]
192 cur_content = codecs.open(filename, 'r', 'utf-8').read().encode('utf-8')
193 if (len(cur_content) != 0) and (cur_content[-1] == '\n'):
194 cur_content = cur_content[:-1]
195 newname = 'b/%s (working copy)' % filename
196 new = [i + '\n' for i in cur_content.split('\n')]
198 newrv = self.pages_get_rv(pageid, newrvid)
199 newname = 'b/%s (revision %i)' % (filename, newrvid)
200 new = [i + '\n' for i in newrv['content'].split('\n')]
202 bzrlib.diff.internal_diff(oldname, old, newname, new, diff_fd)
203 diff = diff_fd.getvalue()