]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/wiki.py
docs: Render images (and other uploaded files)
[ipfire.org.git] / src / backend / wiki.py
CommitLineData
181d08f3
MT
1#!/usr/bin/python3
2
4ed1dadb 3import difflib
181d08f3 4import logging
6ac7e934 5import os.path
181d08f3 6import re
9e90e800 7import urllib.parse
181d08f3
MT
8
9from . import misc
9523790a 10from . import util
181d08f3
MT
11from .decorators import *
12
181d08f3
MT
13class Wiki(misc.Object):
14 def _get_pages(self, query, *args):
15 res = self.db.query(query, *args)
16
17 for row in res:
18 yield Page(self.backend, row.id, data=row)
19
d398ca08
MT
20 def _get_page(self, query, *args):
21 res = self.db.get(query, *args)
22
23 if res:
24 return Page(self.backend, res.id, data=res)
25
86368c12
MT
26 def __iter__(self):
27 return self._get_pages(
28 "SELECT wiki.* FROM wiki_current current \
29 LEFT JOIN wiki ON current.id = wiki.id \
30 WHERE current.deleted IS FALSE \
31 ORDER BY page",
32 )
33
c78ad26e
MT
34 def make_path(self, page, path):
35 # Nothing to do for absolute links
36 if path.startswith("/"):
37 pass
38
39 # Relative links (one-level down)
40 elif path.startswith("./"):
41 path = os.path.join(page, path)
42
43 # All other relative links
44 else:
45 p = os.path.dirname(page)
46 path = os.path.join(p, path)
47
48 # Normalise links
49 return os.path.normpath(path)
50
9ff59d70
MT
51 def page_exists(self, path):
52 page = self.get_page(path)
53
54 # Page must have been found and not deleted
55 return page and not page.was_deleted()
56
6ac7e934 57 def get_page_title(self, page, default=None):
50c8dc11
MT
58 # Try to retrieve title from cache
59 title = self.memcache.get("wiki:title:%s" % page)
60 if title:
61 return title
62
63 # If the title has not been in the cache, we will
64 # have to look it up
6ac7e934
MT
65 doc = self.get_page(page)
66 if doc:
50c8dc11
MT
67 title = doc.title
68 else:
69 title = os.path.basename(page)
6ac7e934 70
50c8dc11
MT
71 # Save in cache for forever
72 self.memcache.set("wiki:title:%s" % page, title)
73
74 return title
6ac7e934 75
181d08f3
MT
76 def get_page(self, page, revision=None):
77 page = Page.sanitise_page_name(page)
78 assert page
79
80 if revision:
d398ca08 81 return self._get_page("SELECT * FROM wiki WHERE page = %s \
181d08f3
MT
82 AND timestamp = %s", page, revision)
83 else:
d398ca08 84 return self._get_page("SELECT * FROM wiki WHERE page = %s \
181d08f3
MT
85 ORDER BY timestamp DESC LIMIT 1", page)
86
11afe905
MT
87 def get_recent_changes(self, account, limit=None):
88 pages = self._get_pages("SELECT * FROM wiki \
11afe905
MT
89 ORDER BY timestamp DESC")
90
91 for page in pages:
92 if not page.check_acl(account):
93 continue
94
95 yield page
96
97 limit -= 1
98 if not limit:
99 break
181d08f3 100
495e9dc4 101 def create_page(self, page, author, content, changes=None, address=None):
181d08f3
MT
102 page = Page.sanitise_page_name(page)
103
aba5e58a
MT
104 # Write page to the database
105 page = self._get_page("INSERT INTO wiki(page, author_uid, markdown, changes, address) \
df01767e 106 VALUES(%s, %s, %s, %s, %s) RETURNING *", page, author.uid, content or None, changes, address)
181d08f3 107
50c8dc11 108 # Update cache
980e486d 109 self.memcache.set("wiki:title:%s" % page.page, page.title)
50c8dc11 110
aba5e58a
MT
111 # Send email to all watchers
112 page._send_watcher_emails(excludes=[author])
113
114 return page
115
495e9dc4 116 def delete_page(self, page, author, **kwargs):
181d08f3
MT
117 # Do nothing if the page does not exist
118 if not self.get_page(page):
119 return
120
121 # Just creates a blank last version of the page
495e9dc4 122 self.create_page(page, author=author, content=None, **kwargs)
181d08f3 123
3168788e
MT
124 def make_breadcrumbs(self, url):
125 # Split and strip all empty elements (double slashes)
181d08f3
MT
126 parts = list(e for e in url.split("/") if e)
127
3168788e 128 ret = []
b1bf7d48 129 for part in ("/".join(parts[:i]) for i in range(1, len(parts))):
3168788e 130 ret.append(("/%s" % part, self.get_page_title(part, os.path.basename(part))))
181d08f3 131
3168788e 132 return ret
181d08f3 133
11afe905 134 def search(self, query, account=None, limit=None):
9523790a
MT
135 res = self._get_pages("SELECT wiki.* FROM wiki_search_index search_index \
136 LEFT JOIN wiki ON search_index.wiki_id = wiki.id \
22e56c4a
MT
137 WHERE search_index.document @@ websearch_to_tsquery('english', %s) \
138 ORDER BY ts_rank(search_index.document, websearch_to_tsquery('english', %s)) DESC",
11afe905 139 query, query)
9523790a 140
df80be2c 141 pages = []
11afe905
MT
142 for page in res:
143 # Skip any pages the user doesn't have permission for
144 if not page.check_acl(account):
145 continue
146
147 # Return any other pages
df80be2c 148 pages.append(page)
11afe905 149
df80be2c
MT
150 # Break when we have found enough pages
151 if limit and len(pages) >= limit:
11afe905 152 break
9523790a 153
df80be2c
MT
154 return pages
155
9523790a
MT
156 def refresh(self):
157 """
158 Needs to be called after a page has been changed
159 """
160 self.db.execute("REFRESH MATERIALIZED VIEW wiki_search_index")
161
2f23c558
MT
162 def get_watchlist(self, account):
163 pages = self._get_pages(
164 "WITH pages AS (SELECT * FROM wiki_current \
165 LEFT JOIN wiki ON wiki_current.id = wiki.id) \
166 SELECT * FROM wiki_watchlist watchlist \
167 LEFT JOIN pages ON watchlist.page = pages.page \
168 WHERE watchlist.uid = %s",
169 account.uid,
170 )
171
172 return sorted(pages)
173
11afe905
MT
174 # ACL
175
176 def check_acl(self, page, account):
177 res = self.db.query("SELECT * FROM wiki_acls \
178 WHERE %s ILIKE (path || '%%') ORDER BY LENGTH(path) DESC LIMIT 1", page)
179
180 for row in res:
181 # Access not permitted when user is not logged in
182 if not account:
183 return False
184
185 # If user is in a matching group, we grant permission
186 for group in row.groups:
93402e56 187 if account.is_member_of_group(group):
11afe905
MT
188 return True
189
190 # Otherwise access is not permitted
191 return False
192
193 # If no ACLs are found, we permit access
194 return True
195
f2cfd873
MT
196 # Files
197
198 def _get_files(self, query, *args):
199 res = self.db.query(query, *args)
200
201 for row in res:
202 yield File(self.backend, row.id, data=row)
203
204 def _get_file(self, query, *args):
205 res = self.db.get(query, *args)
206
207 if res:
208 return File(self.backend, res.id, data=res)
209
210 def get_files(self, path):
211 files = self._get_files("SELECT * FROM wiki_files \
212 WHERE path = %s AND deleted_at IS NULL ORDER BY filename", path)
213
214 return list(files)
215
ff14dea3 216 def get_file_by_path(self, path, revision=None):
f2cfd873
MT
217 path, filename = os.path.dirname(path), os.path.basename(path)
218
ff14dea3
MT
219 if revision:
220 # Fetch a specific revision
221 return self._get_file("SELECT * FROM wiki_files \
222 WHERE path = %s AND filename = %s AND created_at <= %s \
223 ORDER BY created_at DESC LIMIT 1", path, filename, revision)
224
225 # Fetch latest version
226 return self._get_file("SELECT * FROM wiki_files \
227 WHERE path = %s AND filename = %s AND deleted_at IS NULL",
228 path, filename)
229
230 def get_file_by_path_and_filename(self, path, filename):
f2cfd873 231 return self._get_file("SELECT * FROM wiki_files \
ff14dea3
MT
232 WHERE path = %s AND filename = %s AND deleted_at IS NULL",
233 path, filename)
f2cfd873
MT
234
235 def upload(self, path, filename, data, mimetype, author, address):
ff14dea3
MT
236 # Replace any existing files
237 file = self.get_file_by_path_and_filename(path, filename)
238 if file:
239 file.delete(author)
240
f2cfd873 241 # Upload the blob first
a3a8a163
MT
242 blob = self.db.get("INSERT INTO wiki_blobs(data) VALUES(%s) \
243 ON CONFLICT (digest(data, %s)) DO UPDATE SET data = EXCLUDED.data \
244 RETURNING id", data, "MD5")
f2cfd873
MT
245
246 # Create entry for file
247 return self._get_file("INSERT INTO wiki_files(path, filename, author_uid, address, \
248 mimetype, blob_id, size) VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING *", path,
249 filename, author.uid, address, mimetype, blob.id, len(data))
250
2901b734
MT
251 def render(self, path, text):
252 r = WikiRenderer(self.backend, path)
181d08f3 253
2901b734 254 return r.render(text)
e2205cff 255
154f6179 256
2901b734 257class Page(misc.Object):
181d08f3
MT
258 def init(self, id, data=None):
259 self.id = id
260 self.data = data
261
dc847af5
MT
262 def __repr__(self):
263 return "<%s %s %s>" % (self.__class__.__name__, self.page, self.timestamp)
264
c21ffadb
MT
265 def __eq__(self, other):
266 if isinstance(other, self.__class__):
267 return self.id == other.id
268
0713d9ae
MT
269 return NotImplemented
270
181d08f3
MT
271 def __lt__(self, other):
272 if isinstance(other, self.__class__):
273 if self.page == other.page:
274 return self.timestamp < other.timestamp
275
276 return self.page < other.page
277
0713d9ae
MT
278 return NotImplemented
279
181d08f3
MT
280 @staticmethod
281 def sanitise_page_name(page):
282 if not page:
283 return "/"
284
285 # Make sure that the page name does NOT end with a /
286 if page.endswith("/"):
287 page = page[:-1]
288
289 # Make sure the page name starts with a /
290 if not page.startswith("/"):
291 page = "/%s" % page
292
293 # Remove any double slashes
294 page = page.replace("//", "/")
295
296 return page
297
298 @property
299 def url(self):
46b77977 300 return urllib.parse.urljoin("/docs", self.page)
181d08f3 301
4ed1dadb
MT
302 @property
303 def full_url(self):
46b77977 304 return "https://www.ipfire.org/docs%s" % self.url
4ed1dadb 305
181d08f3
MT
306 @property
307 def page(self):
308 return self.data.page
309
310 @property
311 def title(self):
51e7a876 312 return self._title or os.path.basename(self.page[1:])
181d08f3
MT
313
314 @property
315 def _title(self):
316 if not self.markdown:
317 return
318
319 # Find first H1 headline in markdown
320 markdown = self.markdown.splitlines()
321
0074e919 322 m = re.match(r"^#\s*(.*)( #)?$", markdown[0])
181d08f3
MT
323 if m:
324 return m.group(1)
325
3b05ef6e
MT
326 @lazy_property
327 def author(self):
328 if self.data.author_uid:
329 return self.backend.accounts.get_by_uid(self.data.author_uid)
330
181d08f3
MT
331 @property
332 def markdown(self):
c21ffadb 333 return self.data.markdown or ""
181d08f3
MT
334
335 @property
336 def html(self):
2901b734 337 return self.backend.wiki.render(self.page, self.markdown)
addc18d5 338
181d08f3
MT
339 @property
340 def timestamp(self):
341 return self.data.timestamp
342
343 def was_deleted(self):
4c13230c 344 return not self.markdown
181d08f3
MT
345
346 @lazy_property
347 def breadcrumbs(self):
348 return self.backend.wiki.make_breadcrumbs(self.page)
349
d4c68c5c
MT
350 def is_latest_revision(self):
351 return self.get_latest_revision() == self
352
181d08f3 353 def get_latest_revision(self):
7d699684
MT
354 revisions = self.get_revisions()
355
356 # Return first object
357 for rev in revisions:
358 return rev
359
360 def get_revisions(self):
361 return self.backend.wiki._get_pages("SELECT * FROM wiki \
362 WHERE page = %s ORDER BY timestamp DESC", self.page)
091ac36b 363
c21ffadb
MT
364 @lazy_property
365 def previous_revision(self):
366 return self.backend.wiki._get_page("SELECT * FROM wiki \
367 WHERE page = %s AND timestamp < %s ORDER BY timestamp DESC \
368 LIMIT 1", self.page, self.timestamp)
369
d398ca08
MT
370 @property
371 def changes(self):
372 return self.data.changes
373
11afe905
MT
374 # ACL
375
376 def check_acl(self, account):
377 return self.backend.wiki.check_acl(self.page, account)
378
091ac36b
MT
379 # Sidebar
380
381 @lazy_property
382 def sidebar(self):
383 parts = self.page.split("/")
384
385 while parts:
3cc5f666 386 sidebar = self.backend.wiki.get_page("%s/sidebar" % os.path.join(*parts))
091ac36b
MT
387 if sidebar:
388 return sidebar
389
390 parts.pop()
f2cfd873 391
d64a1e35
MT
392 # Watchers
393
4ed1dadb
MT
394 @lazy_property
395 def diff(self):
396 if self.previous_revision:
397 diff = difflib.unified_diff(
398 self.previous_revision.markdown.splitlines(),
399 self.markdown.splitlines(),
400 )
401
402 return "\n".join(diff)
403
aba5e58a
MT
404 @property
405 def watchers(self):
406 res = self.db.query("SELECT uid FROM wiki_watchlist \
407 WHERE page = %s", self.page)
408
409 for row in res:
410 # Search for account by UID and skip if none was found
411 account = self.backend.accounts.get_by_uid(row.uid)
412 if not account:
413 continue
414
415 # Return the account
416 yield account
417
f2e25ded 418 def is_watched_by(self, account):
d64a1e35
MT
419 res = self.db.get("SELECT 1 FROM wiki_watchlist \
420 WHERE page = %s AND uid = %s", self.page, account.uid)
421
422 if res:
423 return True
424
425 return False
426
427 def add_watcher(self, account):
f2e25ded 428 if self.is_watched_by(account):
d64a1e35
MT
429 return
430
431 self.db.execute("INSERT INTO wiki_watchlist(page, uid) \
432 VALUES(%s, %s)", self.page, account.uid)
433
434 def remove_watcher(self, account):
435 self.db.execute("DELETE FROM wiki_watchlist \
436 WHERE page = %s AND uid = %s", self.page, account.uid)
437
aba5e58a
MT
438 def _send_watcher_emails(self, excludes=[]):
439 # Nothing to do if there was no previous revision
440 if not self.previous_revision:
441 return
442
443 for watcher in self.watchers:
444 # Skip everyone who is excluded
445 if watcher in excludes:
446 logging.debug("Excluding %s" % watcher)
447 continue
448
516da0a9
MT
449 # Check permissions
450 if not self.backend.wiki.check_acl(self.page, watcher):
451 logging.debug("Watcher %s does not have permissions" % watcher)
452 continue
453
aba5e58a
MT
454 logging.debug("Sending watcher email to %s" % watcher)
455
4ed1dadb
MT
456 # Compose message
457 self.backend.messages.send_template("wiki/messages/page-changed",
ba14044c 458 account=watcher, page=self, priority=-10)
aba5e58a 459
9f1cfab7 460 def restore(self, author, address, comment=None):
d4c68c5c
MT
461 changes = "Restore to revision from %s" % self.timestamp.isoformat()
462
9f1cfab7
MT
463 # Append comment
464 if comment:
465 changes = "%s: %s" % (changes, comment)
466
d4c68c5c
MT
467 return self.backend.wiki.create_page(self.page,
468 author, self.markdown, changes=changes, address=address)
469
f2cfd873
MT
470
471class File(misc.Object):
472 def init(self, id, data):
473 self.id = id
474 self.data = data
475
ff14dea3
MT
476 def __eq__(self, other):
477 if isinstance(other, self.__class__):
478 return self.id == other.id
479
f2cfd873
MT
480 @property
481 def url(self):
482 return os.path.join(self.path, self.filename)
483
484 @property
485 def path(self):
486 return self.data.path
487
488 @property
489 def filename(self):
490 return self.data.filename
491
492 @property
493 def mimetype(self):
494 return self.data.mimetype
495
496 @property
497 def size(self):
498 return self.data.size
499
8cb0bea4
MT
500 @lazy_property
501 def author(self):
502 if self.data.author_uid:
503 return self.backend.accounts.get_by_uid(self.data.author_uid)
504
505 @property
506 def created_at(self):
507 return self.data.created_at
508
b26c705a
MT
509 def delete(self, author=None):
510 self.db.execute("UPDATE wiki_files SET deleted_at = NOW(), deleted_by = %s \
511 WHERE id = %s", author.uid if author else None, self.id)
ff14dea3
MT
512
513 @property
514 def deleted_at(self):
515 return self.data.deleted_at
516
517 def get_latest_revision(self):
518 revisions = self.get_revisions()
519
520 # Return first object
521 for rev in revisions:
522 return rev
523
524 def get_revisions(self):
525 revisions = self.backend.wiki._get_files("SELECT * FROM wiki_files \
2225edd9 526 WHERE path = %s AND filename = %s ORDER BY created_at DESC", self.path, self.filename)
ff14dea3
MT
527
528 return list(revisions)
529
8cb0bea4
MT
530 def is_pdf(self):
531 return self.mimetype in ("application/pdf", "application/x-pdf")
532
f2cfd873
MT
533 def is_image(self):
534 return self.mimetype.startswith("image/")
535
8a62e589
MT
536 def is_vector_image(self):
537 return self.mimetype in ("image/svg+xml",)
538
539 def is_bitmap_image(self):
540 return self.is_image() and not self.is_vector_image()
541
f2cfd873
MT
542 @lazy_property
543 def blob(self):
544 res = self.db.get("SELECT data FROM wiki_blobs \
545 WHERE id = %s", self.data.blob_id)
546
547 if res:
548 return bytes(res.data)
79dd9a0f
MT
549
550 def get_thumbnail(self, size):
8a62e589
MT
551 assert self.is_bitmap_image()
552
75d9b3da
MT
553 cache_key = "-".join((self.path, util.normalize(self.filename), self.created_at.isoformat(), "%spx" % size))
554
555 # Try to fetch the data from the cache
556 thumbnail = self.memcache.get(cache_key)
557 if thumbnail:
558 return thumbnail
559
560 # Generate the thumbnail
5ef115cd 561 thumbnail = util.generate_thumbnail(self.blob, size)
75d9b3da
MT
562
563 # Put it into the cache for forever
564 self.memcache.set(cache_key, thumbnail)
565
566 return thumbnail
2901b734
MT
567
568
569class WikiRenderer(misc.Object):
4ddad3e5
MT
570 schemas = (
571 "ftp://",
572 "git://",
573 "http://",
574 "https://",
575 "rsync://",
576 "sftp://",
577 "ssh://",
578 "webcal://",
579 )
580
581 # Links
582 links = re.compile(r"<a href=\"(.*?)\">(.*?)</a>")
2901b734 583
c78ad26e 584 # Images
e9c6d581 585 images = re.compile(r"<img alt(?:=\"(.*?)\")? src=\"(.*?)\" (?:title=\"(.*?)\" )?/>")
c78ad26e 586
2901b734
MT
587 def init(self, path):
588 self.path = path
589
4ddad3e5
MT
590 def _render_link(self, m):
591 url, text = m.groups()
2901b734 592
4ddad3e5
MT
593 # Emails
594 if "@" in url:
595 # Strip mailto:
596 if url.startswith("mailto:"):
597 url = url[7:]
2901b734 598
4ddad3e5
MT
599 return """<a class="link-external" href="mailto:%s">%s</a>""" % \
600 (url, text or url)
2901b734 601
4ddad3e5
MT
602 # External Links
603 for schema in self.schemas:
604 if url.startswith(schema):
605 return """<a class="link-external" href="%s">%s</a>""" % \
606 (url, text or url)
2901b734 607
4ddad3e5
MT
608 # Everything else must be an internal link
609 path = self.backend.wiki.make_path(self.path, url)
2901b734 610
46b77977 611 return """<a href="/docs%s">%s</a>""" % \
4ddad3e5 612 (path, text or self.backend.wiki.get_page_title(path))
2901b734 613
c78ad26e 614 def _render_image(self, m):
e9c6d581 615 alt_text, url, caption = m.groups()
2901b734 616
4a1bfdd5
MT
617 html = """
618 <figure class="image">
619 <img src="/docs%s" alt="%s">
620 <figcaption class="figure-caption">%s</figcaption>
621 </figure>
622 """
623
c78ad26e
MT
624 # Skip any absolute and external URLs
625 if url.startswith("/") or url.startswith("https://") or url.startswith("http://"):
4a1bfdd5 626 return html % (url, alt_text, caption or "")
2901b734 627
c78ad26e
MT
628 # Try to split query string
629 url, delimiter, qs = url.partition("?")
2901b734 630
c78ad26e
MT
631 # Parse query arguments
632 args = urllib.parse.parse_qs(qs)
2901b734 633
c78ad26e
MT
634 # Build absolute path
635 url = self.backend.wiki.make_path(self.path, url)
2901b734 636
c78ad26e
MT
637 # Find image
638 file = self.backend.wiki.get_file_by_path(url)
639 if not file or not file.is_image():
640 return "<!-- Could not find image %s in %s -->" % (url, self.path)
2901b734 641
c78ad26e
MT
642 # Scale down the image if not already done
643 if not "s" in args:
9ce45afb 644 args["s"] = "920"
2901b734 645
4a1bfdd5
MT
646 # Append arguments to the URL
647 if args:
648 url = "%s?%s" % (url, urllib.parse.urlencode(args))
649
650 return html % (url, caption, caption or "")
2901b734 651
c78ad26e
MT
652 def render(self, text):
653 logging.debug("Rendering %s" % self.path)
2901b734 654
9881e9ef
MT
655 # Borrow this from the blog
656 text = self.backend.blog._render_text(text, lang="markdown")
657
4ddad3e5
MT
658 # Postprocess links
659 text = self.links.sub(self._render_link, text)
660
9881e9ef 661 # Postprocess images to <figure>
c78ad26e
MT
662 text = self.images.sub(self._render_image, text)
663
9881e9ef 664 return text