2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2011 Ian Weller <ian@ianweller.org> and others
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)
70 self.config.add_section('merge')
71 self.config.set('merge', 'tool', 'kidff3 %s %s -o %s')
74 os.mkdir(os.path.join(self.location, 'cache'))
75 # create cache/pagedict
76 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
77 fd.write(json.dumps({}))
80 os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
82 def clean_page(self, pagename):
83 filename = pagename_to_filename(pagename) + '.wiki'
84 cur_content = codecs.open(filename, 'r', 'utf-8').read()
85 if len(cur_content) != 0 and cur_content[-1] == '\n':
86 cur_content = cur_content[:-1]
87 fd = file(filename, 'w')
88 fd.write(cur_content.encode('utf-8'))
91 def pagedict_add(self, pagename, pageid, currentrv):
92 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
93 pagedict = json.loads(fd.read())
94 pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
96 fd.write(json.dumps(pagedict))
100 def get_pageid_from_pagename(self, pagename):
101 fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
102 pagedict = json.loads(fd.read())
103 pagename = pagename.decode('utf-8')
104 if pagename in pagedict.keys():
105 return pagedict[pagename]
109 def pages_add_rv(self, pageid, rv):
110 pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
111 fd = file(pagefile, 'w+')
112 pagedata_raw = fd.read()
113 if pagedata_raw == '':
116 pagedata = json.loads(pagedata_raw)
117 rvid = int(rv['revid'])
120 'timestamp': rv['timestamp'],
123 pagedata[rvid]['content'] = rv['*']
125 fd.write(json.dumps(pagedata))
129 def pages_get_rv_list(self, pageid):
130 pagefile = os.path.join(self.location, 'cache', 'pages',
132 if os.path.exists(pagefile):
133 fd = file(pagefile, 'r')
134 pagedata = json.loads(fd.read())
135 rvs = [int(x) for x in pagedata.keys()]
141 def pages_get_rv(self, pageid, rvid):
142 pagefile = os.path.join(self.location, 'cache', 'pages',
144 if os.path.exists(pagefile):
145 fd = file(pagefile, 'r')
146 pagedata = json.loads(fd.read())
147 return pagedata[str(rvid)]
151 def working_dir_status(self, files=None):
154 if files == None or files == []:
155 for root, dirs, files in os.walk(self.root):
156 if root == self.root:
159 check.append(os.path.join(root, name))
162 check.append(os.path.join(os.getcwd(), file))
165 name = os.path.split(full)[1]
166 if name[-5:] == '.wiki':
167 pagename = filename_to_pagename(name[:-5])
168 pageid = self.get_pageid_from_pagename(pagename)
170 status[os.path.relpath(full, self.root)] = '?'
172 rvid = self.pages_get_rv_list(pageid)[-1]
173 rv = self.pages_get_rv(pageid, rvid)
174 cur_content = codecs.open(full, 'r', 'utf-8').read()
175 if (len(cur_content) != 0) and (cur_content[-1] == '\n'):
176 cur_content = cur_content[:-1]
177 if cur_content != rv['content']:
178 status[os.path.relpath(full, self.root)] = 'M' # modified
180 status[os.path.relpath(full, self.root)] = 'C' # clean
183 def diff_rv_to_working(self, pagename, oldrvid=0, newrvid=0):
184 # oldrvid=0 means latest fetched revision
185 # newrvid=0 means working copy
186 filename = pagename_to_filename(pagename) + '.wiki'
187 filename = filename.decode('utf-8')
188 pageid = self.get_pageid_from_pagename(pagename)
190 raise ValueError('page named %s has not been fetched' % pagename)
193 oldrvid = self.pages_get_rv_list(pageid)[-1]
194 oldrv = self.pages_get_rv(pageid, oldrvid)
195 oldname = 'a/%s (revision %i)' % (filename, oldrvid)
196 old = [i + '\n' for i in \
197 oldrv['content'].encode('utf-8').split('\n')]
199 cur_content = codecs.open(filename, 'r', 'utf-8').read()
200 cur_content = cur_content.encode('utf-8')
201 if (len(cur_content) != 0) and (cur_content[-1] == '\n'):
202 cur_content = cur_content[:-1]
203 newname = 'b/%s (working copy)' % filename
204 new = [i + '\n' for i in cur_content.split('\n')]
206 newrv = self.pages_get_rv(pageid, newrvid)
207 newname = 'b/%s (revision %i)' % (filename, newrvid)
208 new = [i + '\n' for i in newrv['content'].split('\n')]
210 bzrlib.diff.internal_diff(oldname, old, newname, new, diff_fd)
211 diff = diff_fd.getvalue()
217 def pagename_to_filename(name):
218 name = name.replace(' ', '_')
219 name = name.replace('/', '!')
223 def filename_to_pagename(name):
224 name = name.replace('!', '/')
225 name = name.replace('_', ' ')