Attachment 'PageActions.py'
Download 1 # -*- coding: iso-8859-1 -*-
2
3 import re
4 from MoinMoin import config, wikiutil
5 from MoinMoin.support import difflib
6 from MoinMoin.Page import Page
7 from MoinMoin.action import get_available_actions
8
9 # Ripped from the LikePages action
10 # renamed by clauz
11 def executeold(pagename, request):
12 _ = request.getText
13 from MoinMoin.formatter.text_html import Formatter
14 request.formatter = Formatter(request)
15
16 request.emit_http_headers()
17
18 # This action generate data using the user language
19 request.setContentLanguage(request.lang)
20 request.theme.send_title(_('Actions for %s') % pagename, page_name=pagename)
21
22 # Start content - IMPORTANT - without content div, there is no
23 # direction support!
24 request.write(request.formatter.startContent("content"))
25
26 # Just list the actions
27 request.write(availableactions(request))
28
29 # End content and send footer
30 request.write(request.formatter.endContent())
31 request.theme.send_footer(pagename)
32
33 # copied from LikePages by Clauz
34 def closeMatches(pagename, pages):
35 """ Get close matches.
36
37 Return all matching pages with rank above cutoff value.
38
39 @param pagename: page name to match
40 @param pages: list of page names
41 @rtype: list
42 @return: list of matching pages, sorted by rank
43 """
44 # Match using case insensitive matching
45 # Make mapping from lowerpages to pages - pages might have same name
46 # with different case (although its stupid).
47 lower = {}
48 for name in pages:
49 key = name.lower()
50 if key in lower:
51 lower[key].append(name)
52 else:
53 lower[key] = [name]
54
55 # Get all close matches
56 all_matches = difflib.get_close_matches(pagename.lower(), lower.keys(),
57 len(lower), cutoff=0.6)
58
59 # Replace lower names with original names
60 matches = []
61 for name in all_matches:
62 matches.extend(lower[name])
63
64 return matches
65
66
67 # copied from LikePages by Clauz
68 def findMatches(pagename, request, s_re=None, e_re=None):
69 """ Find like pages
70
71 @param pagename: name to match
72 @param request: current reqeust
73 @param s_re: start re for wiki matching
74 @param e_re: end re for wiki matching
75 @rtype: tuple
76 @return: start word, end word, matches dict
77 """
78 # Get full list of pages, with no filtering - very fast. We will
79 # first search for like pages, then filter the results.
80 pages = request.rootpage.getPageList(user='', exists='')
81
82 # Remove current page
83 try:
84 pages.remove(pagename)
85 except ValueError:
86 pass
87
88 # Get matches using wiki way, start and end of word
89 start, end, matches = wikiMatches(pagename, pages, start_re=s_re,
90 end_re=e_re)
91
92 # Get the best 10 close matches
93 close_matches = {}
94 found = 0
95 for name in closeMatches(pagename, pages):
96 # Skip names already in matches
97 if name in matches:
98 continue
99
100 # Filter deleted pages or pages the user can't read
101 page = Page(request, name)
102 if page.exists() and request.user.may.read(name):
103 close_matches[name] = 8
104 found += 1
105 # Stop after 10 matches
106 if found == 10:
107 break
108
109 # Filter deleted pages or pages the user can't read from
110 # matches. Order is important!
111 for name in matches.keys(): # we need .keys() because we modify the dict
112 page = Page(request, name)
113 if not (page.exists() and request.user.may.read(name)):
114 del matches[name]
115
116 # Finally, merge both dicts
117 matches.update(close_matches)
118
119 return start, end, matches
120
121 # Ripped from the LikePages action by clauz
122 def execute(pagename, request):
123 _ = request.getText
124 #start, end, matches = findMatches(pagename, request)
125
126 # Error?
127 #if isinstance(matches, (str, unicode)):
128 # request.theme.add_msg(wikiutil.escape(matches), "info")
129 # Page(request, pagename).send_page()
130 # return
131
132
133 # One match - display it
134 #if len(matches) == 1:
135 # request.theme.add_msg(_('Exactly one page like "%s" found, redirecting to page.') % (wikiutil.escape(pagename), ), "info")
136 # Page(request, matches.keys()[0]).send_page()
137 # return
138
139 # more than one match, list 'em
140 # This action generate data using the user language
141 request.setContentLanguage(request.lang)
142
143 request.theme.send_title(_('Actions for "%s"') % (pagename), pagename=pagename)
144
145 # Start content - IMPORTANT - without content div, there is no
146 # direction support!
147 request.write(request.formatter.startContent("content"))
148
149 request.write(availableactions(request))
150
151 # End content and send footer
152 request.write(request.formatter.endContent())
153 request.theme.send_footer(pagename)
154 request.theme.send_closing_html()
155
156 # Make a link to action
157 def actionlink(request, action, title, comment=''):
158 page = request.page
159 _ = request.getText
160 # Always add spaces: AttachFile -> Attach File
161 # XXX TODO do not make a page object just for split_title
162 title = Page(request, title).split_title(request) #, force=1)
163 # Use translated version if available
164 title = _(title, formatted=False)
165 params = '%s?action=%s' % (page.page_name, action)
166 link = wikiutil.link_tag(request, params, _(title))
167 return u''.join([ u'<li>', link, comment, u'</li>' ])
168
169
170 # Rippped from the theme code
171 # Renamed by Clauz
172 def oldavailableactions(request):
173 page = request.page
174 _ = request.getText
175 html = ''
176 links = []
177 available = request.getAvailableActions(page)
178 # try:
179 # available = available.keys()
180 # except AttributeError:
181 # pass
182 for action in available:
183 links.append(actionlink(request, action, action))
184 if page.isWritable() and request.user.may.write(page.page_name):
185 links.append(actionlink(request, 'edit', 'EditText'))
186 if request.user.valid and request.user.email:
187 action = ("Subscribe", "Unsubscribe")[request.user.isSubscribedTo([page.page_name])]
188 links.append(actionlink(request, 'subscribe', action))
189 if request.user.valid:
190 links.append(actionlink(request, 'userform&logout=logout', 'Logout'))
191 links.append(actionlink(request, 'print', 'PrintView'))
192 links.append(actionlink(request, 'raw', 'ViewRawText'))
193 links.append(actionlink(request, 'refresh', 'DeleteCache'))
194 html = u'<ul>%s</ul>' % u''.join(links)
195 return html
196
197 # copied from classic.py by Clauz
198 def availableactions(d):
199 """
200 assemble HTML code for the available actions
201
202 @param d: parameter dictionary
203 @rtype: string
204 @return: available actions html
205 """
206 request = d
207 _ = request.getText
208 html = []
209 page = request.page
210 available = get_available_actions(request.cfg, page, request.user)
211 if available:
212 available = list(available)
213 available.sort()
214 for action in available:
215 # Always add spaces: AttachFile -> Attach File
216 # XXX do not make a page object just for split_title
217 #title = Page(request, action).split_title(force=1)
218 title = action
219 # Use translated version if available
220 title = _(title)
221 querystr = {'action': action}
222 link = page.link_to(request, text=title, querystr=querystr, rel='nofollow')
223 html.append(link)
224
225 title = _("DeleteCache")
226 link = page.link_to(request, text=title, querystr={'action': 'refresh'}, rel='nofollow')
227
228 html = u'<p>%s %s</ul></p>\n' % (_('Available actions: <ul><li>'),
229 u'</li><li>'.join(html))
230 return html
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.