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