1 | # -*- coding: utf-8 -*-
|
---|
2 | #
|
---|
3 | # Copyright (C) 2003-2008 Edgewall Software
|
---|
4 | # Copyright (C) 2003-2005 Jonas Borgström <jonas@edgewall.com>
|
---|
5 | # Copyright (C) 2004-2005 Christopher Lenz <cmlenz@gmx.de>
|
---|
6 | # All rights reserved.
|
---|
7 | #
|
---|
8 | # This software is licensed as described in the file COPYING, which
|
---|
9 | # you should have received as part of this distribution. The terms
|
---|
10 | # are also available at http://trac.edgewall.org/wiki/TracLicense.
|
---|
11 | #
|
---|
12 | # This software consists of voluntary contributions made by many
|
---|
13 | # individuals. For the exact contribution history, see the revision
|
---|
14 | # history and logs, available at http://trac.edgewall.org/log/.
|
---|
15 | #
|
---|
16 | # Author: Jonas Borgström <jonas@edgewall.com>
|
---|
17 | # Christopher Lenz <cmlenz@gmx.de>
|
---|
18 |
|
---|
19 | try:
|
---|
20 | import threading
|
---|
21 | except ImportError:
|
---|
22 | import dummy_threading as threading
|
---|
23 | import time
|
---|
24 | import urllib
|
---|
25 | import re
|
---|
26 | from StringIO import StringIO
|
---|
27 |
|
---|
28 | from genshi.builder import tag
|
---|
29 |
|
---|
30 | from trac.config import BoolOption
|
---|
31 | from trac.core import *
|
---|
32 | from trac.resource import IResourceManager
|
---|
33 | from trac.util import reversed
|
---|
34 | from trac.util.html import html
|
---|
35 | from trac.util.translation import _
|
---|
36 | from trac.wiki.parser import WikiParser
|
---|
37 |
|
---|
38 |
|
---|
39 | class IWikiChangeListener(Interface):
|
---|
40 | """Extension point interface for components that should get notified about
|
---|
41 | the creation, deletion and modification of wiki pages.
|
---|
42 | """
|
---|
43 |
|
---|
44 | def wiki_page_added(page):
|
---|
45 | """Called whenever a new Wiki page is added."""
|
---|
46 |
|
---|
47 | def wiki_page_changed(page, version, t, comment, author, ipnr):
|
---|
48 | """Called when a page has been modified."""
|
---|
49 |
|
---|
50 | def wiki_page_deleted(page):
|
---|
51 | """Called when a page has been deleted."""
|
---|
52 |
|
---|
53 | def wiki_page_version_deleted(page):
|
---|
54 | """Called when a version of a page has been deleted."""
|
---|
55 |
|
---|
56 |
|
---|
57 | class IWikiPageManipulator(Interface):
|
---|
58 | """Extension point interface for components that need to do specific
|
---|
59 | pre and post processing of wiki page changes.
|
---|
60 |
|
---|
61 | Unlike change listeners, a manipulator can reject changes being committed
|
---|
62 | to the database.
|
---|
63 | """
|
---|
64 |
|
---|
65 | def prepare_wiki_page(req, page, fields):
|
---|
66 | """Not currently called, but should be provided for future
|
---|
67 | compatibility."""
|
---|
68 |
|
---|
69 | def validate_wiki_page(req, page):
|
---|
70 | """Validate a wiki page after it's been populated from user input.
|
---|
71 |
|
---|
72 | Must return a list of `(field, message)` tuples, one for each problem
|
---|
73 | detected. `field` can be `None` to indicate an overall problem with the
|
---|
74 | page. Therefore, a return value of `[]` means everything is OK."""
|
---|
75 |
|
---|
76 |
|
---|
77 | class IWikiMacroProvider(Interface):
|
---|
78 | """Extension point interface for components that provide Wiki macros."""
|
---|
79 |
|
---|
80 | def get_macros():
|
---|
81 | """Return an iterable that provides the names of the provided macros."""
|
---|
82 |
|
---|
83 | def get_macro_description(name):
|
---|
84 | """Return a plain text description of the macro with the specified name.
|
---|
85 | """
|
---|
86 |
|
---|
87 | def render_macro(req, name, content):
|
---|
88 | """Return the HTML output of the macro (deprecated)"""
|
---|
89 |
|
---|
90 | def expand_macro(formatter, name, content):
|
---|
91 | """Called by the formatter when rendering the parsed wiki text.
|
---|
92 |
|
---|
93 | (since 0.11)
|
---|
94 | """
|
---|
95 |
|
---|
96 |
|
---|
97 | class IWikiSyntaxProvider(Interface):
|
---|
98 |
|
---|
99 | def get_wiki_syntax():
|
---|
100 | """Return an iterable that provides additional wiki syntax.
|
---|
101 |
|
---|
102 | Additional wiki syntax correspond to a pair of (regexp, cb),
|
---|
103 | the `regexp` for the additional syntax and the callback `cb`
|
---|
104 | which will be called if there's a match.
|
---|
105 | That function is of the form cb(formatter, ns, match).
|
---|
106 | """
|
---|
107 |
|
---|
108 | def get_link_resolvers():
|
---|
109 | """Return an iterable over (namespace, formatter) tuples.
|
---|
110 |
|
---|
111 | Each formatter should be a function of the form
|
---|
112 | fmt(formatter, ns, target, label), and should
|
---|
113 | return some HTML fragment.
|
---|
114 | The `label` is already HTML escaped, whereas the `target` is not.
|
---|
115 | """
|
---|
116 |
|
---|
117 |
|
---|
118 | def parse_args(args, strict=True):
|
---|
119 | """Utility for parsing macro "content" and splitting them into arguments.
|
---|
120 |
|
---|
121 | The content is split along commas, unless they are escaped with a
|
---|
122 | backquote (like this: \,).
|
---|
123 |
|
---|
124 | :param args: macros arguments, as plain text
|
---|
125 | :param strict: if `True`, only Python-like identifiers will be
|
---|
126 | recognized as keyword arguments
|
---|
127 |
|
---|
128 | Example usage:
|
---|
129 |
|
---|
130 | >>> parse_args('')
|
---|
131 | ([], {})
|
---|
132 | >>> parse_args('Some text')
|
---|
133 | (['Some text'], {})
|
---|
134 | >>> parse_args('Some text, mode= 3, some other arg\, with a comma.')
|
---|
135 | (['Some text', ' some other arg, with a comma.'], {'mode': ' 3'})
|
---|
136 | >>> parse_args('milestone=milestone1,status!=closed', strict=False)
|
---|
137 | ([], {'status!': 'closed', 'milestone': 'milestone1'})
|
---|
138 |
|
---|
139 | """
|
---|
140 | largs, kwargs = [], {}
|
---|
141 | if args:
|
---|
142 | for arg in re.split(r'(?<!\\),', args):
|
---|
143 | arg = arg.replace(r'\,', ',')
|
---|
144 | if strict:
|
---|
145 | m = re.match(r'\s*[a-zA-Z_]\w+=', arg)
|
---|
146 | else:
|
---|
147 | m = re.match(r'\s*[^=]+=', arg)
|
---|
148 | if m:
|
---|
149 | kw = arg[:m.end()-1].strip()
|
---|
150 | if strict:
|
---|
151 | kw = unicode(kw).encode('utf-8')
|
---|
152 | kwargs[kw] = arg[m.end():]
|
---|
153 | else:
|
---|
154 | largs.append(arg)
|
---|
155 | return largs, kwargs
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | class WikiSystem(Component):
|
---|
160 | """Represents the wiki system."""
|
---|
161 |
|
---|
162 | implements(IWikiChangeListener, IWikiSyntaxProvider, IResourceManager)
|
---|
163 |
|
---|
164 | change_listeners = ExtensionPoint(IWikiChangeListener)
|
---|
165 | macro_providers = ExtensionPoint(IWikiMacroProvider)
|
---|
166 | syntax_providers = ExtensionPoint(IWikiSyntaxProvider)
|
---|
167 |
|
---|
168 | INDEX_UPDATE_INTERVAL = 5 # seconds
|
---|
169 |
|
---|
170 | ignore_missing_pages = BoolOption('wiki', 'ignore_missing_pages', 'false',
|
---|
171 | """Enable/disable highlighting CamelCase links to missing pages
|
---|
172 | (''since 0.9'').""")
|
---|
173 |
|
---|
174 | split_page_names = BoolOption('wiki', 'split_page_names', 'false',
|
---|
175 | """Enable/disable splitting the WikiPageNames with space characters
|
---|
176 | (''since 0.10'').""")
|
---|
177 |
|
---|
178 | render_unsafe_content = BoolOption('wiki', 'render_unsafe_content', 'false',
|
---|
179 | """Enable/disable the use of unsafe HTML tags such as `<script>` or
|
---|
180 | `<embed>` with the HTML [wiki:WikiProcessors WikiProcessor]
|
---|
181 | (''since 0.10.4'').
|
---|
182 |
|
---|
183 | For public sites where anonymous users can edit the wiki it is
|
---|
184 | recommended to leave this option disabled (which is the default).""")
|
---|
185 |
|
---|
186 | def __init__(self):
|
---|
187 | self._index = None
|
---|
188 | self._last_index_update = 0
|
---|
189 | self._index_lock = threading.RLock()
|
---|
190 |
|
---|
191 | def _update_index(self):
|
---|
192 | self._index_lock.acquire()
|
---|
193 | try:
|
---|
194 | now = time.time()
|
---|
195 | if now > self._last_index_update + WikiSystem.INDEX_UPDATE_INTERVAL:
|
---|
196 | self.log.debug('Updating wiki page index')
|
---|
197 | db = self.env.get_db_cnx()
|
---|
198 | cursor = db.cursor()
|
---|
199 | cursor.execute("SELECT DISTINCT name FROM wiki")
|
---|
200 | self._index = {}
|
---|
201 | for (name,) in cursor:
|
---|
202 | self._index[name] = True
|
---|
203 | self._last_index_update = now
|
---|
204 | finally:
|
---|
205 | self._index_lock.release()
|
---|
206 |
|
---|
207 | # Public API
|
---|
208 |
|
---|
209 | def get_pages(self, prefix=None):
|
---|
210 | """Iterate over the names of existing Wiki pages.
|
---|
211 |
|
---|
212 | If the `prefix` parameter is given, only names that start with that
|
---|
213 | prefix are included.
|
---|
214 | """
|
---|
215 | self._update_index()
|
---|
216 | # Note: use of keys() is intentional since iterkeys() is prone to
|
---|
217 | # errors with concurrent modification
|
---|
218 | for page in self._index.keys():
|
---|
219 | if not prefix or page.startswith(prefix):
|
---|
220 | yield page
|
---|
221 |
|
---|
222 | def has_page(self, pagename):
|
---|
223 | """Whether a page with the specified name exists."""
|
---|
224 | self._update_index()
|
---|
225 | return self._index.has_key(pagename.rstrip('/'))
|
---|
226 |
|
---|
227 | # IWikiChangeListener methods
|
---|
228 |
|
---|
229 | def wiki_page_added(self, page):
|
---|
230 | if not self.has_page(page.name):
|
---|
231 | self.log.debug('Adding page %s to index' % page.name)
|
---|
232 | self._index[page.name] = True
|
---|
233 |
|
---|
234 | def wiki_page_changed(self, page, version, t, comment, author, ipnr):
|
---|
235 | pass
|
---|
236 |
|
---|
237 | def wiki_page_deleted(self, page):
|
---|
238 | if self.has_page(page.name):
|
---|
239 | self.log.debug('Removing page %s from index' % page.name)
|
---|
240 | del self._index[page.name]
|
---|
241 |
|
---|
242 | def wiki_page_version_deleted(self, page):
|
---|
243 | pass
|
---|
244 |
|
---|
245 | # IWikiSyntaxProvider methods
|
---|
246 |
|
---|
247 | XML_NAME = r"[\w:](?<!\d)(?:[\w:.-]*[\w-])?"
|
---|
248 | # See http://www.w3.org/TR/REC-xml/#id,
|
---|
249 | # here adapted to exclude terminal "." and ":" characters
|
---|
250 |
|
---|
251 | PAGE_SPLIT_RE = re.compile(r"([a-z])([A-Z])(?=[a-z])")
|
---|
252 |
|
---|
253 | def format_page_name(self, page, split=False):
|
---|
254 | if split or self.split_page_names:
|
---|
255 | return self.PAGE_SPLIT_RE.sub(r"\1 \2", page)
|
---|
256 | return page
|
---|
257 |
|
---|
258 | def get_wiki_syntax(self):
|
---|
259 | from trac.wiki.formatter import Formatter
|
---|
260 | lower = r'(?<![A-Z0-9_])' # No Upper case when looking behind
|
---|
261 | upper = r'(?<![a-z0-9_])' # No Lower case when looking behind
|
---|
262 | wiki_page_name = (
|
---|
263 | r"\w%s(?:\w%s)+(?:\w%s(?:\w%s)*[\w/]%s)+" % # wiki words
|
---|
264 | (upper, lower, upper, lower, lower) +
|
---|
265 | r"(?:@\d+)?" # optional version
|
---|
266 | r"(?:#%s)?" % self.XML_NAME + # optional fragment id
|
---|
267 | r"(?=:(?:\Z|\s)|[^:a-zA-Z]|\s|\Z)" # what should follow it
|
---|
268 | )
|
---|
269 |
|
---|
270 |
|
---|
271 | # Regular WikiPageNames
|
---|
272 | def wikipagename_link(formatter, match, fullmatch):
|
---|
273 | if not _check_unicode_camelcase(match):
|
---|
274 | return match
|
---|
275 | return self._format_link(formatter, 'wiki', match,
|
---|
276 | self.format_page_name(match),
|
---|
277 | self.ignore_missing_pages)
|
---|
278 |
|
---|
279 | yield (r"!?(?<!/)\b" + # start at a word boundary but not after '/'
|
---|
280 | wiki_page_name, wikipagename_link)
|
---|
281 |
|
---|
282 | # [WikiPageNames with label]
|
---|
283 | def wikipagename_with_label_link(formatter, match, fullmatch):
|
---|
284 | page, label = match[1:-1].split(' ', 1)
|
---|
285 | if not _check_unicode_camelcase(page):
|
---|
286 | return label
|
---|
287 | return self._format_link(formatter, 'wiki', page, label.strip(),
|
---|
288 | self.ignore_missing_pages)
|
---|
289 | yield (r"!?\[%s\s+(?:%s|[^\]]+)\]" % (wiki_page_name,
|
---|
290 | WikiParser.QUOTED_STRING),
|
---|
291 | wikipagename_with_label_link)
|
---|
292 |
|
---|
293 | # MoinMoin's ["internal free link"]
|
---|
294 | def internal_free_link(fmt, m, fullmatch):
|
---|
295 | return self._format_link(fmt, 'wiki', m[2:-2], m[2:-2], False)
|
---|
296 | yield (r"!?\[(?:%s)\]" % WikiParser.QUOTED_STRING, internal_free_link)
|
---|
297 |
|
---|
298 | def get_link_resolvers(self):
|
---|
299 | def link_resolver(formatter, ns, target, label):
|
---|
300 | return self._format_link(formatter, ns, target, label, False)
|
---|
301 | yield ('wiki', link_resolver)
|
---|
302 |
|
---|
303 | def _format_link(self, formatter, ns, pagename, label, ignore_missing):
|
---|
304 | pagename, query, fragment = formatter.split_link(pagename)
|
---|
305 | version = None
|
---|
306 | if '@' in pagename:
|
---|
307 | pagename, version = pagename.split('@', 1)
|
---|
308 | if version and query:
|
---|
309 | query = '&' + query[1:]
|
---|
310 | pagename = pagename.rstrip('/')
|
---|
311 | if 'WIKI_VIEW' in formatter.perm('wiki', pagename, version):
|
---|
312 | href = formatter.href.wiki(pagename, version=version) + query \
|
---|
313 | + fragment
|
---|
314 | if self.has_page(pagename):
|
---|
315 | return tag.a(label, href=href, class_='wiki')
|
---|
316 | else:
|
---|
317 | if ignore_missing:
|
---|
318 | return label
|
---|
319 | return tag.a(label+'?', href=href, class_='missing wiki',
|
---|
320 | rel='nofollow')
|
---|
321 | elif ignore_missing and not self.has_page(pagename):
|
---|
322 | return label
|
---|
323 | else:
|
---|
324 | return tag.span(label, class_='forbidden wiki',
|
---|
325 | title=_("no permission to view this wiki page"))
|
---|
326 |
|
---|
327 | # IResourceManager methods
|
---|
328 |
|
---|
329 | def get_resource_realms(self):
|
---|
330 | yield 'wiki'
|
---|
331 |
|
---|
332 | def get_resource_description(self, resource, format, **kwargs):
|
---|
333 | """
|
---|
334 | >>> from trac.test import EnvironmentStub
|
---|
335 | >>> from trac.resource import Resource, get_resource_description
|
---|
336 | >>> env = EnvironmentStub()
|
---|
337 | >>> main = Resource('wiki', 'WikiStart')
|
---|
338 | >>> get_resource_description(env, main)
|
---|
339 | 'WikiStart'
|
---|
340 |
|
---|
341 | >>> get_resource_description(env, main(version=3))
|
---|
342 | 'WikiStart'
|
---|
343 |
|
---|
344 | >>> get_resource_description(env, main(version=3), format='summary')
|
---|
345 | 'WikiStart'
|
---|
346 |
|
---|
347 | >>> env.config['wiki'].set('split_page_names', 'true')
|
---|
348 | >>> get_resource_description(env, main(version=3))
|
---|
349 | 'Wiki Start'
|
---|
350 | """
|
---|
351 | return self.format_page_name(resource.id)
|
---|
352 |
|
---|
353 |
|
---|
354 | def _check_unicode_camelcase(pagename):
|
---|
355 | """A camelcase word must have at least 2 humps (well...)
|
---|
356 |
|
---|
357 | >>> _check_unicode_camelcase(u"\xc9l\xe9phant")
|
---|
358 | False
|
---|
359 | >>> _check_unicode_camelcase(u"\xc9l\xe9Phant")
|
---|
360 | True
|
---|
361 | >>> _check_unicode_camelcase(u"\xe9l\xe9Phant")
|
---|
362 | False
|
---|
363 | >>> _check_unicode_camelcase(u"\xc9l\xe9PhanT")
|
---|
364 | False
|
---|
365 | """
|
---|
366 | if not pagename[0].isupper():
|
---|
367 | return False
|
---|
368 | pagename = pagename.split('@', 1)[0].split('#', 1)[0]
|
---|
369 | if not pagename[-1].islower():
|
---|
370 | return False
|
---|
371 | humps = 0
|
---|
372 | for i in xrange(1, len(pagename)):
|
---|
373 | if pagename[i-1].isupper():
|
---|
374 | if pagename[i].islower():
|
---|
375 | humps += 1
|
---|
376 | else:
|
---|
377 | return False
|
---|
378 | return humps > 1
|
---|
379 |
|
---|