]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/wiki.py
33ed40405d4004bfae53435bbadde72dd7353aae
12 from .decorators
import *
14 class Wiki(misc
.Object
):
15 def _get_pages(self
, query
, *args
):
16 res
= self
.db
.query(query
, *args
)
19 yield Page(self
.backend
, row
.id, data
=row
)
21 def _get_page(self
, query
, *args
):
22 res
= self
.db
.get(query
, *args
)
25 return Page(self
.backend
, res
.id, data
=res
)
27 def get_page_title(self
, page
, default
=None):
28 doc
= self
.get_page(page
)
32 return default
or os
.path
.basename(page
)
34 def get_page(self
, page
, revision
=None):
35 page
= Page
.sanitise_page_name(page
)
39 return self
._get
_page
("SELECT * FROM wiki WHERE page = %s \
40 AND timestamp = %s", page
, revision
)
42 return self
._get
_page
("SELECT * FROM wiki WHERE page = %s \
43 ORDER BY timestamp DESC LIMIT 1", page
)
45 def get_recent_changes(self
, limit
=None):
46 return self
._get
_pages
("SELECT * FROM wiki \
47 WHERE timestamp >= NOW() - INTERVAL '4 weeks' \
48 ORDER BY timestamp DESC LIMIT %s", limit
)
50 def create_page(self
, page
, author
, content
, changes
=None, address
=None):
51 page
= Page
.sanitise_page_name(page
)
53 return self
._get
_page
("INSERT INTO wiki(page, author_uid, markdown, changes, address) \
54 VALUES(%s, %s, %s, %s, %s) RETURNING *", page
, author
.uid
, content
or None, changes
, address
)
56 def delete_page(self
, page
, author
, **kwargs
):
57 # Do nothing if the page does not exist
58 if not self
.get_page(page
):
61 # Just creates a blank last version of the page
62 self
.create_page(page
, author
=author
, content
=None, **kwargs
)
64 def make_breadcrumbs(self
, url
):
65 # Split and strip all empty elements (double slashes)
66 parts
= list(e
for e
in url
.split("/") if e
)
69 for part
in ("/".join(parts
[:i
]) for i
in range(1, len(parts
))):
70 ret
.append(("/%s" % part
, self
.get_page_title(part
, os
.path
.basename(part
))))
74 def search(self
, query
, limit
=None):
75 query
= util
.parse_search_query(query
)
77 res
= self
._get
_pages
("SELECT wiki.* FROM wiki_search_index search_index \
78 LEFT JOIN wiki ON search_index.wiki_id = wiki.id \
79 WHERE search_index.document @@ to_tsquery('english', %s) \
80 ORDER BY ts_rank(search_index.document, to_tsquery('english', %s)) DESC \
81 LIMIT %s", query
, query
, limit
)
87 Needs to be called after a page has been changed
89 self
.db
.execute("REFRESH MATERIALIZED VIEW wiki_search_index")
93 def _get_files(self
, query
, *args
):
94 res
= self
.db
.query(query
, *args
)
97 yield File(self
.backend
, row
.id, data
=row
)
99 def _get_file(self
, query
, *args
):
100 res
= self
.db
.get(query
, *args
)
103 return File(self
.backend
, res
.id, data
=res
)
105 def get_files(self
, path
):
106 files
= self
._get
_files
("SELECT * FROM wiki_files \
107 WHERE path = %s AND deleted_at IS NULL ORDER BY filename", path
)
111 def get_file_by_path(self
, path
):
112 path
, filename
= os
.path
.dirname(path
), os
.path
.basename(path
)
114 return self
._get
_file
("SELECT * FROM wiki_files \
115 WHERE path = %s AND filename = %s AND deleted_at IS NULL", path
, filename
)
117 def upload(self
, path
, filename
, data
, mimetype
, author
, address
):
118 # Upload the blob first
119 blob
= self
.db
.get("INSERT INTO wiki_blobs(data) VALUES(%s) RETURNING id", data
)
121 # Create entry for file
122 return self
._get
_file
("INSERT INTO wiki_files(path, filename, author_uid, address, \
123 mimetype, blob_id, size) VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING *", path
,
124 filename
, author
.uid
, address
, mimetype
, blob
.id, len(data
))
126 def find_image(self
, path
, filename
):
127 for p
in (path
, os
.path
.dirname(path
)):
128 file = self
.get_file_by_path(os
.path
.join(p
, filename
))
130 if file and file.is_image():
134 class Page(misc
.Object
):
135 def init(self
, id, data
=None):
139 def __lt__(self
, other
):
140 if isinstance(other
, self
.__class
__):
141 if self
.page
== other
.page
:
142 return self
.timestamp
< other
.timestamp
144 return self
.page
< other
.page
147 def sanitise_page_name(page
):
151 # Make sure that the page name does NOT end with a /
152 if page
.endswith("/"):
155 # Make sure the page name starts with a /
156 if not page
.startswith("/"):
159 # Remove any double slashes
160 page
= page
.replace("//", "/")
170 return self
.data
.page
174 return self
._title
or self
.page
[1:]
178 if not self
.markdown
:
181 # Find first H1 headline in markdown
182 markdown
= self
.markdown
.splitlines()
184 m
= re
.match(r
"^# (.*)( #)?$", markdown
[0])
190 if self
.data
.author_uid
:
191 return self
.backend
.accounts
.get_by_uid(self
.data
.author_uid
)
193 def _render(self
, text
):
194 logging
.debug("Rendering %s" % self
)
198 for match
in re
.finditer(r
"!\[(.*)\]\((.*)\)", text
):
199 alt_text
, url
= match
.groups()
201 # Skip any absolute and external URLs
202 if url
.startswith("/") or url
.startswith("https://") or url
.startswith("http://"):
205 # Try to split query string
206 url
, delimiter
, qs
= url
.partition("?")
208 # Parse query arguments
209 args
= urllib
.parse
.parse_qs(qs
)
212 file = self
.backend
.wiki
.find_image(self
.page
, url
)
216 # Scale down the image if not already done
221 url
= "%s?%s" % (file.url
, urllib
.parse
.urlencode(args
))
223 replacements
.append((match
.span(), file, alt_text
, url
))
225 # Apply all replacements
226 for (start
, end
), file, alt_text
, url
in reversed(replacements
):
227 text
= text
[:start
] + "[![%s](%s)](%s?action=detail)" % (alt_text
, url
, file.url
) + text
[end
:]
231 (r
"\[\[([\w\d\/]+)(?:\|([\w\d\s]+))\]\]", r
"/\1", r
"\2", None, None),
232 (r
"\[\[([\w\d\/\-]+)\]\]", r
"/\1", r
"\1", self
.backend
.wiki
.get_page_title
, r
"\1"),
235 for pattern
, link
, title
, repl
, args
in patterns
:
238 for match
in re
.finditer(pattern
, text
):
239 l
= match
.expand(link
)
240 t
= match
.expand(title
)
243 t
= repl(match
.expand(args
)) or t
245 replacements
.append((match
.span(), t
or l
, l
))
247 # Apply all replacements
248 for (start
, end
), t
, l
in reversed(replacements
):
249 text
= text
[:start
] + "[%s](%s)" % (t
, l
) + text
[end
:]
251 # Borrow this from the blog
252 return self
.backend
.blog
._render
_text
(text
, lang
="markdown")
256 return self
.data
.markdown
260 return self
.data
.html
or self
._render
(self
.markdown
)
264 return self
.data
.timestamp
266 def was_deleted(self
):
267 return self
.markdown
is None
270 def breadcrumbs(self
):
271 return self
.backend
.wiki
.make_breadcrumbs(self
.page
)
273 def get_latest_revision(self
):
274 revisions
= self
.get_revisions()
276 # Return first object
277 for rev
in revisions
:
280 def get_revisions(self
):
281 return self
.backend
.wiki
._get
_pages
("SELECT * FROM wiki \
282 WHERE page = %s ORDER BY timestamp DESC", self
.page
)
286 return self
.data
.changes
292 parts
= self
.page
.split("/")
295 sidebar
= self
.backend
.wiki
.get_page("%s/sidebar" % os
.path
.join(*parts
))
302 class File(misc
.Object
):
303 def init(self
, id, data
):
309 return os
.path
.join(self
.path
, self
.filename
)
313 return self
.data
.path
317 return self
.data
.filename
321 return self
.data
.mimetype
325 return self
.data
.size
329 if self
.data
.author_uid
:
330 return self
.backend
.accounts
.get_by_uid(self
.data
.author_uid
)
333 def created_at(self
):
334 return self
.data
.created_at
337 return self
.mimetype
in ("application/pdf", "application/x-pdf")
340 return self
.mimetype
.startswith("image/")
344 res
= self
.db
.get("SELECT data FROM wiki_blobs \
345 WHERE id = %s", self
.data
.blob_id
)
348 return bytes(res
.data
)
350 def get_thumbnail(self
, size
):
351 cache_key
= "-".join((self
.path
, util
.normalize(self
.filename
), self
.created_at
.isoformat(), "%spx" % size
))
353 # Try to fetch the data from the cache
354 thumbnail
= self
.memcache
.get(cache_key
)
358 # Generate the thumbnail
359 thumbnail
= self
._generate
_thumbnail
(size
)
361 # Put it into the cache for forever
362 self
.memcache
.set(cache_key
, thumbnail
)
366 def _generate_thumbnail(self
, size
):
367 image
= PIL
.Image
.open(io
.BytesIO(self
.blob
))
369 # Resize the image to the desired resolution
370 image
.thumbnail((size
, size
), PIL
.Image
.ANTIALIAS
)
372 with io
.BytesIO() as f
:
373 # If writing out the image does not work with optimization,
374 # we try to write it out without any optimization.
376 image
.save(f
, image
.format
, optimize
=True, quality
=98)
378 image
.save(f
, image
.format
, quality
=98)