2
""" repair-language - repair page language setting.
10
verify - verify pages, does not change anything, print page revision
11
that should be repaired.
13
repair - repair all page revisions.
15
Step by step instructions:
19
2. Make a backup of 'data' directory.
21
3. Run this script from your wiki data directory, where your pages
24
4. Fix permissions on the data directory, as explained in HelpOnInstalling.
26
5. Verify that pages are fine after repair, if you find a problem,
27
restore your data directory from backup.
31
In patch-325 a new #language processing instruction has been added.
32
Pages that specify the language with it are displayed using correct
33
direction, even if language_default use different direction.
35
In the past, pages used to have ##language:xx comment. This comment
36
has no effect, and should be replaced with newer #language xx
37
processing instruction.
39
This script replace ##language:xx to #language xx in page headers.
40
It convert all page revisions, so you can safely revert back to old
41
revision and get correct page direction.
43
You can run the script multiple times if needed.
45
@copyright: 2004 Nir Soffer <nirs AT freeshell DOT org>
46
@license: GPL, see COPYING for details
52
# Insert THIS moin dir first into sys path, or you would run another
54
sys.path.insert(0, '../..')
56
from MoinMoin import i18n
57
valid_languages = i18n.wikiLanguages()
61
""" Return list of files in path, filtering certain files """
62
names = [name for name in os.listdir(path)
63
if not name.startswith('.') and
64
not name.endswith('.pickle') and
72
We change only this type of lines that currently are in moinmaster
75
Warning: will not repair the language if there is more text on the
76
same line, e.g. ##language:fr make it french!
78
@param text: the page text, unicode
79
@rtype: 2 tuple, (unicode, int)
80
@return: text after replacement, lines changed
83
needle = u'##language:'
87
lines = text.splitlines()
90
for i in range(len(lines)):
92
if not line.startswith(u'#'):
95
if line.startswith(needle):
96
# Get language from rest of line
97
lang = line[len(needle):].strip()
98
# Normalize language names. Language files are named xx_yy,
99
# but iso names use xx-yy. This can confuse people.
100
lang = lang.replace(u"_", u"-")
102
# Validate lang, make new style language processing
104
if lang in valid_languages:
105
line = u'#language %s' % lang
110
# Join lines back, make sure there is trailing line end
111
text = lineend.join(lines) + lineend
115
def processPages(path, repair):
116
""" Process page directory
118
@param repair: repair or just test
122
pages = [p for p in listdir(path) if os.path.isdir(os.path.join(path, p))]
124
revdir = os.path.join(path, page, 'revisions')
125
if not os.path.isdir(revdir):
126
print 'Error: %s: missing revisions directory' % page
129
for rev in listdir(revdir):
130
revpath = os.path.join(revdir, rev)
131
# Open file, read text
132
f = codecs.open(revpath, 'rb', charset)
135
text, changed = repairText(text)
137
if changed and repair:
138
# Save converted text
139
f = codecs.open(revpath, 'wb', charset)
142
print 'Repaired %s revision %s' % (page, rev)
144
print 'Should repair %s revision %s' % (page, rev)
147
if __name__ == '__main__':
149
# Check for pages directory in current directory
150
path = os.path.abspath('pages')
151
if not os.path.isdir(path):
152
print "Error: could not find 'pages' directory"
153
print 'Run this script from your wiki data directory'
157
options = {'verify': 0, 'repair': 1,}
159
if len(sys.argv) != 2 or sys.argv[1] not in options:
163
processPages(path, repair=options[sys.argv[1]])