~ubuntu-branches/ubuntu/gutsy/moin/gutsy

« back to all changes in this revision

Viewing changes to MoinMoin/script/old/repair_language.py

  • Committer: Bazaar Package Importer
  • Author(s): Sivan Greenberg
  • Date: 2006-07-09 19:28:02 UTC
  • Revision ID: james.westby@ubuntu.com-20060709192802-oaeuvt4v3e9300uj
Tags: 1.5.3-1ubuntu1
* Merge new debian version.
* Reapply Ubuntu changes:
    + debian/rules:
      - Comment out usage of control.ubuntu.in (doesn't fit!).
    + debian/control.in:
      - Dropped python2.3 binary package.
    + debian/control:
      - Dropped python2.3 binary, again.
      - Dropped python2.3-dev from Build-Depends-Indep.
    + debian/patches/001-attachment-xss-fix.patch:
      - Dropped this patch. It's now in upstream's distribution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
""" repair-language - repair page language setting.
 
3
 
 
4
Usage:
 
5
 
 
6
    repair-language option
 
7
 
 
8
Options:
 
9
 
 
10
    verify - verify pages, does not change anything, print page revision
 
11
        that should be repaired.
 
12
 
 
13
    repair - repair all page revisions.
 
14
 
 
15
Step by step instructions:
 
16
 
 
17
 1. Stop your wiki.
 
18
 
 
19
 2. Make a backup of 'data' directory.
 
20
 
 
21
 3. Run this script from your wiki data directory, where your pages
 
22
    directory lives.
 
23
 
 
24
 4. Fix permissions on the data directory, as explained in HelpOnInstalling.
 
25
 
 
26
 5. Verify that pages are fine after repair, if you find a problem,
 
27
    restore your data directory from backup.
 
28
 
 
29
Why run this script?
 
30
 
 
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.
 
34
 
 
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.
 
38
 
 
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.
 
42
 
 
43
    You can run the script multiple times if needed.
 
44
 
 
45
@copyright: 2004 Nir Soffer <nirs AT freeshell DOT org>
 
46
@license: GPL, see COPYING for details
 
47
"""
 
48
 
 
49
import codecs
 
50
import os, sys
 
51
 
 
52
# Insert THIS moin dir first into sys path, or you would run another
 
53
# version of moin!
 
54
sys.path.insert(0, '../..')
 
55
 
 
56
from MoinMoin import i18n
 
57
valid_languages = i18n.wikiLanguages()
 
58
 
 
59
 
 
60
def listdir(path):
 
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
 
65
             name != 'CVS']
 
66
    return names
 
67
 
 
68
 
 
69
def repairText(text):
 
70
    """ Repair page text
 
71
 
 
72
    We change only this type of lines that currently are in moinmaster
 
73
    ##language:\s*xx
 
74
 
 
75
    Warning: will not repair the language if there is more text on the
 
76
    same line, e.g. ##language:fr make it french!
 
77
 
 
78
    @param text: the page text, unicode
 
79
    @rtype: 2 tuple, (unicode, int)
 
80
    @return: text after replacement, lines changed
 
81
    """
 
82
    lineend = u'\r\n'
 
83
    needle = u'##language:'
 
84
    changed = 0
 
85
 
 
86
    # Get text lines
 
87
    lines = text.splitlines()
 
88
    
 
89
    # Look in page header
 
90
    for i in range(len(lines)):
 
91
        line = lines[i]
 
92
        if not line.startswith(u'#'):
 
93
            break # end of header
 
94
        
 
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"-")
 
101
                
 
102
            # Validate lang, make new style language processing
 
103
            # instruction.
 
104
            if lang in valid_languages:
 
105
                line = u'#language %s' % lang
 
106
                lines[i] = line
 
107
                changed += 1
 
108
 
 
109
    if changed:
 
110
        # Join lines back, make sure there is trailing line end
 
111
        text = lineend.join(lines) + lineend
 
112
    return text, changed
 
113
 
 
114
 
 
115
def processPages(path, repair):
 
116
    """ Process page directory
 
117
    
 
118
    @param repair: repair or just test
 
119
    """
 
120
    charset = 'utf-8'
 
121
    
 
122
    pages = [p for p in listdir(path) if os.path.isdir(os.path.join(path, p))]
 
123
    for page in pages:
 
124
        revdir = os.path.join(path, page, 'revisions')
 
125
        if not os.path.isdir(revdir):
 
126
            print 'Error: %s: missing revisions directory' % page
 
127
            continue
 
128
        
 
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)
 
133
            text = f.read()
 
134
            f.close()
 
135
            text, changed = repairText(text)
 
136
 
 
137
            if changed and repair:
 
138
                # Save converted text
 
139
                f = codecs.open(revpath, 'wb', charset)
 
140
                f.write(text)
 
141
                f.close()
 
142
                print 'Repaired %s revision %s' % (page, rev)
 
143
            elif changed:
 
144
                print 'Should repair %s revision %s' % (page, rev)
 
145
 
 
146
 
 
147
if __name__ == '__main__':
 
148
 
 
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'
 
154
        print __doc__
 
155
        sys.exit(1)   
 
156
    
 
157
    options = {'verify': 0, 'repair': 1,}
 
158
    
 
159
    if len(sys.argv) != 2 or sys.argv[1] not in options:
 
160
        print __doc__
 
161
        sys.exit(1)
 
162
 
 
163
    processPages(path, repair=options[sys.argv[1]])
 
164
    
 
165
 
 
166
 
 
167