/usr/share/pyshared/MoinMoin/action/LikePages.py is in python-moinmoin 1.9.3-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | # -*- coding: iso-8859-1 -*-
"""
MoinMoin - LikePages action
This action generates a list of pages that either start or end
with the same word as the current pagename. If only one matching
page is found, that page is displayed directly.
@copyright: 2001 Richard Jones <richard@bizarsoftware.com.au>,
2001 Juergen Hermann <jh@web.de>
@license: GNU GPL, see COPYING for details.
"""
import re
from MoinMoin import config, wikiutil
from MoinMoin.support import difflib
from MoinMoin.Page import Page
def execute(pagename, request):
_ = request.getText
start, end, matches = findMatches(pagename, request)
# Error?
if isinstance(matches, (str, unicode)):
request.theme.add_msg(wikiutil.escape(matches), "info")
Page(request, pagename).send_page()
return
# No matches
if not matches:
request.theme.add_msg(_('No pages like "%s"!') % (wikiutil.escape(pagename), ), "error")
Page(request, pagename).send_page()
return
# One match - display it
if len(matches) == 1:
request.theme.add_msg(_('Exactly one page like "%s" found, redirecting to page.') % (wikiutil.escape(pagename), ), "info")
Page(request, matches.keys()[0]).send_page()
return
# more than one match, list 'em
# This action generate data using the user language
request.setContentLanguage(request.lang)
request.theme.send_title(_('Pages like "%s"') % (pagename), pagename=pagename)
# Start content - IMPORTANT - without content div, there is no
# direction support!
request.write(request.formatter.startContent("content"))
showMatches(pagename, request, start, end, matches)
# End content and send footer
request.write(request.formatter.endContent())
request.theme.send_footer(pagename)
request.theme.send_closing_html()
def findMatches(pagename, request, s_re=None, e_re=None):
""" Find like pages
@param pagename: name to match
@param request: current reqeust
@param s_re: start re for wiki matching
@param e_re: end re for wiki matching
@rtype: tuple
@return: start word, end word, matches dict
"""
# Get full list of pages, with no filtering - very fast. We will
# first search for like pages, then filter the results.
pages = request.rootpage.getPageList(user='', exists='')
# Remove current page
try:
pages.remove(pagename)
except ValueError:
pass
# Get matches using wiki way, start and end of word
start, end, matches = wikiMatches(pagename, pages, start_re=s_re,
end_re=e_re)
# Get the best 10 close matches
close_matches = {}
found = 0
for name in closeMatches(pagename, pages):
# Skip names already in matches
if name in matches:
continue
# Filter deleted pages or pages the user can't read
page = Page(request, name)
if page.exists() and request.user.may.read(name):
close_matches[name] = 8
found += 1
# Stop after 10 matches
if found == 10:
break
# Filter deleted pages or pages the user can't read from
# matches. Order is important!
for name in matches.keys(): # we need .keys() because we modify the dict
page = Page(request, name)
if not (page.exists() and request.user.may.read(name)):
del matches[name]
# Finally, merge both dicts
matches.update(close_matches)
return start, end, matches
def wikiMatches(pagename, pages, start_re=None, end_re=None):
"""
Get pages that starts or ends with same word as this page
Matches are ranked like this:
4 - page is subpage of pagename
3 - match both start and end
2 - match end
1 - match start
@param pagename: page name to match
@param pages: list of page names
@param start_re: start word re (compile regex)
@param end_re: end word re (compile regex)
@rtype: tuple
@return: start, end, matches dict
"""
if start_re is None:
start_re = re.compile('([%s][%s]+)' % (config.chars_upper,
config.chars_lower))
if end_re is None:
end_re = re.compile('([%s][%s]+)$' % (config.chars_upper,
config.chars_lower))
# If we don't get results with wiki words matching, fall back to
# simple first word and last word, using spaces.
words = pagename.split()
match = start_re.match(pagename)
if match:
start = match.group(1)
else:
start = words[0]
match = end_re.search(pagename)
if match:
end = match.group(1)
else:
end = words[-1]
matches = {}
subpage = pagename + '/'
# Find any matching pages and rank by type of match
for name in pages:
if name.startswith(subpage):
matches[name] = 4
else:
if name.startswith(start):
matches[name] = 1
if name.endswith(end):
matches[name] = matches.get(name, 0) + 2
return start, end, matches
def closeMatches(pagename, pages):
""" Get close matches.
Return all matching pages with rank above cutoff value.
@param pagename: page name to match
@param pages: list of page names
@rtype: list
@return: list of matching pages, sorted by rank
"""
# Match using case insensitive matching
# Make mapping from lowerpages to pages - pages might have same name
# with different case (although its stupid).
lower = {}
for name in pages:
key = name.lower()
if key in lower:
lower[key].append(name)
else:
lower[key] = [name]
# Get all close matches
all_matches = difflib.get_close_matches(pagename.lower(), lower.keys(),
len(lower), cutoff=0.6)
# Replace lower names with original names
matches = []
for name in all_matches:
matches.extend(lower[name])
return matches
def showMatches(pagename, request, start, end, matches, show_count=True):
keys = matches.keys()
keys.sort()
_showMatchGroup(request, matches, keys, 8, pagename, show_count)
_showMatchGroup(request, matches, keys, 4, "%s/..." % pagename, show_count)
_showMatchGroup(request, matches, keys, 3, "%s...%s" % (start, end), show_count)
_showMatchGroup(request, matches, keys, 1, "%s..." % (start, ), show_count)
_showMatchGroup(request, matches, keys, 2, "...%s" % (end, ), show_count)
def _showMatchGroup(request, matches, keys, match, title, show_count=True):
_ = request.getText
matchcount = matches.values().count(match)
if matchcount:
if show_count:
# Render title line
request.write(request.formatter.paragraph(1))
request.write(request.formatter.strong(1))
request.write(request.formatter.text(
_('%(matchcount)d %(matches)s for "%(title)s"') % {
'matchcount': matchcount,
'matches': ' ' + (_('match'), _('matches'))[matchcount != 1],
'title': title}))
request.write(request.formatter.strong(0))
request.write(request.formatter.paragraph(0))
# Render links
request.write(request.formatter.bullet_list(1))
for key in keys:
if matches[key] == match:
request.write(request.formatter.listitem(1))
request.write(request.formatter.pagelink(1, key, generated=True))
request.write(request.formatter.text(key))
request.write(request.formatter.pagelink(0, key, generated=True))
request.write(request.formatter.listitem(0))
request.write(request.formatter.bullet_list(0))
|