]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/wiki.py
7 import markdown
.extensions
8 import markdown
.preprocessors
15 from .decorators
import *
17 class Wiki(misc
.Object
):
18 def _get_pages(self
, query
, *args
):
19 res
= self
.db
.query(query
, *args
)
22 yield Page(self
.backend
, row
.id, data
=row
)
24 def _get_page(self
, query
, *args
):
25 res
= self
.db
.get(query
, *args
)
28 return Page(self
.backend
, res
.id, data
=res
)
31 return self
._get
_pages
("""
37 wiki ON current.id = wiki.id
39 current.deleted IS FALSE
44 def make_path(self
, page
, path
):
45 # Nothing to do for absolute links
46 if path
.startswith("/"):
49 # Relative links (one-level down)
50 elif path
.startswith("./"):
51 path
= os
.path
.join(page
, path
)
53 # All other relative links
55 p
= os
.path
.dirname(page
)
56 path
= os
.path
.join(p
, path
)
59 return os
.path
.normpath(path
)
61 def page_exists(self
, path
):
62 page
= self
.get_page(path
)
64 # Page must have been found and not deleted
65 return page
and not page
.was_deleted()
67 def get_page_title(self
, page
, default
=None):
68 doc
= self
.get_page(page
)
72 title
= os
.path
.basename(page
)
76 def get_page(self
, page
, revision
=None):
77 page
= Page
.sanitise_page_name(page
)
79 # Split the path into parts
80 parts
= page
.split("/")
82 # Check if this is an action
83 if any((part
.startswith("_") for part
in parts
)):
87 return self
._get
_page
("SELECT * FROM wiki WHERE page = %s \
88 AND timestamp = %s", page
, revision
)
90 return self
._get
_page
("SELECT * FROM wiki WHERE page = %s \
91 ORDER BY timestamp DESC LIMIT 1", page
)
93 def get_recent_changes(self
, account
, limit
=None):
94 pages
= self
._get
_pages
("SELECT * FROM wiki \
95 ORDER BY timestamp DESC")
98 if not page
.check_acl(account
):
107 def create_page(self
, page
, author
, content
, changes
=None, address
=None):
108 page
= Page
.sanitise_page_name(page
)
110 # Write page to the database
111 page
= self
._get
_page
("""
124 """, page
, author
.uid
, content
or None, changes
, address
,
127 # Store any linked files
128 page
._store
_linked
_files
()
130 # Send email to all watchers
131 page
._send
_watcher
_emails
(excludes
=[author
])
135 def delete_page(self
, page
, author
, **kwargs
):
136 # Do nothing if the page does not exist
137 if not self
.get_page(page
):
140 # Just creates a blank last version of the page
141 self
.create_page(page
, author
=author
, content
=None, **kwargs
)
143 def make_breadcrumbs(self
, url
):
144 # Split and strip all empty elements (double slashes)
145 parts
= list(e
for e
in url
.split("/") if e
)
148 for part
in ("/".join(parts
[:i
]) for i
in range(1, len(parts
))):
149 ret
.append(("/%s" % part
, self
.get_page_title(part
, os
.path
.basename(part
))))
153 def search(self
, query
, account
=None, limit
=None):
154 res
= self
._get
_pages
("""
158 wiki_search_index search_index
160 wiki ON search_index.wiki_id = wiki.id
162 search_index.document @@ websearch_to_tsquery('english', %s)
164 ts_rank(search_index.document, websearch_to_tsquery('english', %s)) DESC
170 # Skip any pages the user doesn't have permission for
171 if not page
.check_acl(account
):
174 # Return any other pages
177 # Break when we have found enough pages
178 if limit
and len(pages
) >= limit
:
185 Needs to be called after a page has been changed
187 self
.db
.execute("REFRESH MATERIALIZED VIEW wiki_search_index")
189 def get_watchlist(self
, account
):
190 pages
= self
._get
_pages
("""
197 wiki ON wiki_current.id = wiki.id
203 wiki_watchlist watchlist
205 pages ON watchlist.page = pages.page
215 def check_acl(self
, page
, account
):
216 res
= self
.db
.query("""
222 %s ILIKE (path || '%%')
230 # Access not permitted when user is not logged in
234 # If user is in a matching group, we grant permission
235 for group
in row
.groups
:
236 if account
.is_member_of_group(group
):
239 # Otherwise access is not permitted
242 # If no ACLs are found, we permit access
247 def _get_files(self
, query
, *args
):
248 res
= self
.db
.query(query
, *args
)
251 yield File(self
.backend
, row
.id, data
=row
)
253 def _get_file(self
, query
, *args
):
254 res
= self
.db
.get(query
, *args
)
257 return File(self
.backend
, res
.id, data
=res
)
259 def get_files(self
, path
):
260 files
= self
._get
_files
("""
275 def get_file_by_path(self
, path
, revision
=None):
276 path
, filename
= os
.path
.dirname(path
), os
.path
.basename(path
)
279 # Fetch a specific revision
280 return self
._get
_file
("""
294 """, path
, filename
, revision
,
297 # Fetch latest version
298 return self
._get
_file
("""
312 def get_file_by_path_and_filename(self
, path
, filename
):
313 return self
._get
_file
("""
327 def upload(self
, path
, filename
, data
, mimetype
, author
, address
):
328 # Replace any existing files
329 file = self
.get_file_by_path_and_filename(path
, filename
)
333 # Upload the blob first
334 blob
= self
.db
.get("""
342 SET data = EXCLUDED.data
347 # Create entry for file
348 return self
._get
_file
("""
360 %s, %s, %s, %s, %s, %s, %s
363 """, path
, filename
, author
.uid
, address
, mimetype
, blob
.id, len(data
),
366 def render(self
, path
, text
, **kwargs
):
367 return WikiRenderer(self
.backend
, path
, text
, **kwargs
)
370 class Page(misc
.Object
):
371 def init(self
, id, data
=None):
376 return "<%s %s %s>" % (self
.__class
__.__name
__, self
.page
, self
.timestamp
)
378 def __eq__(self
, other
):
379 if isinstance(other
, self
.__class
__):
380 return self
.id == other
.id
382 return NotImplemented
384 def __lt__(self
, other
):
385 if isinstance(other
, self
.__class
__):
386 if self
.page
== other
.page
:
387 return self
.timestamp
< other
.timestamp
389 return self
.page
< other
.page
391 return NotImplemented
394 return hash(self
.page
)
397 def sanitise_page_name(page
):
401 # Make sure that the page name does NOT end with a /
402 if page
.endswith("/"):
405 # Make sure the page name starts with a /
406 if not page
.startswith("/"):
409 # Remove any double slashes
410 page
= page
.replace("//", "/")
416 return "/docs%s" % self
.page
420 return "https://www.ipfire.org%s" % self
.url
424 return self
.data
.page
428 return self
._title
or os
.path
.basename(self
.page
[1:])
432 if not self
.markdown
:
435 # Find first H1 headline in markdown
436 markdown
= self
.markdown
.splitlines()
438 m
= re
.match(r
"^#\s*(.*)( #)?$", markdown
[0])
444 if self
.data
.author_uid
:
445 return self
.backend
.accounts
.get_by_uid(self
.data
.author_uid
)
449 return self
.data
.markdown
or ""
455 # Strip off the first line if it contains a heading (as it will be shown separately)
456 for i
, line
in enumerate(self
.markdown
.splitlines()):
457 if i
== 0 and line
.startswith("#"):
462 renderer
= self
.backend
.wiki
.render(self
.page
, "\n".join(lines
), revision
=self
.timestamp
)
470 renderer
= self
.backend
.wiki
.render(self
.page
, self
.markdown
, revision
=self
.timestamp
)
472 return renderer
.files
474 def _store_linked_files(self
):
475 self
.db
.executemany("INSERT INTO wiki_linked_files(page_id, path) \
476 VALUES(%s, %s)", ((self
.id, file) for file in self
.files
))
480 return self
.data
.timestamp
482 def was_deleted(self
):
483 return not self
.markdown
486 def breadcrumbs(self
):
487 return self
.backend
.wiki
.make_breadcrumbs(self
.page
)
489 def is_latest_revision(self
):
490 return self
.get_latest_revision() == self
492 def get_latest_revision(self
):
493 revisions
= self
.get_revisions()
495 # Return first object
496 for rev
in revisions
:
499 def get_revisions(self
):
500 return self
.backend
.wiki
._get
_pages
("SELECT * FROM wiki \
501 WHERE page = %s ORDER BY timestamp DESC", self
.page
)
504 def previous_revision(self
):
505 return self
.backend
.wiki
._get
_page
("SELECT * FROM wiki \
506 WHERE page = %s AND timestamp < %s ORDER BY timestamp DESC \
507 LIMIT 1", self
.page
, self
.timestamp
)
511 return self
.data
.changes
515 def check_acl(self
, account
):
516 return self
.backend
.wiki
.check_acl(self
.page
, account
)
522 if self
.previous_revision
:
523 diff
= difflib
.unified_diff(
524 self
.previous_revision
.markdown
.splitlines(),
525 self
.markdown
.splitlines(),
528 return "\n".join(diff
)
532 res
= self
.db
.query("SELECT uid FROM wiki_watchlist \
533 WHERE page = %s", self
.page
)
536 # Search for account by UID and skip if none was found
537 account
= self
.backend
.accounts
.get_by_uid(row
.uid
)
544 def is_watched_by(self
, account
):
545 res
= self
.db
.get("SELECT 1 FROM wiki_watchlist \
546 WHERE page = %s AND uid = %s", self
.page
, account
.uid
)
553 def add_watcher(self
, account
):
554 if self
.is_watched_by(account
):
557 self
.db
.execute("INSERT INTO wiki_watchlist(page, uid) \
558 VALUES(%s, %s)", self
.page
, account
.uid
)
560 def remove_watcher(self
, account
):
561 self
.db
.execute("DELETE FROM wiki_watchlist \
562 WHERE page = %s AND uid = %s", self
.page
, account
.uid
)
564 def _send_watcher_emails(self
, excludes
=[]):
565 # Nothing to do if there was no previous revision
566 if not self
.previous_revision
:
569 for watcher
in self
.watchers
:
570 # Skip everyone who is excluded
571 if watcher
in excludes
:
572 logging
.debug("Excluding %s" % watcher
)
576 if not self
.backend
.wiki
.check_acl(self
.page
, watcher
):
577 logging
.debug("Watcher %s does not have permissions" % watcher
)
580 logging
.debug("Sending watcher email to %s" % watcher
)
583 self
.backend
.messages
.send_template("wiki/messages/page-changed",
584 account
=watcher
, page
=self
, priority
=-10)
586 def restore(self
, author
, address
, comment
=None):
587 changes
= "Restore to revision from %s" % self
.timestamp
.isoformat()
591 changes
= "%s: %s" % (changes
, comment
)
593 return self
.backend
.wiki
.create_page(self
.page
,
594 author
, self
.markdown
, changes
=changes
, address
=address
)
597 class File(misc
.Object
):
598 def init(self
, id, data
):
602 def __eq__(self
, other
):
603 if isinstance(other
, self
.__class
__):
604 return self
.id == other
.id
606 return NotImplemented
610 return "/docs%s" % os
.path
.join(self
.path
, self
.filename
)
614 return self
.data
.path
618 return self
.data
.filename
622 return self
.data
.mimetype
626 return self
.data
.size
630 if self
.data
.author_uid
:
631 return self
.backend
.accounts
.get_by_uid(self
.data
.author_uid
)
634 def created_at(self
):
635 return self
.data
.created_at
637 timestamp
= created_at
639 def delete(self
, author
=None):
640 if not self
.can_be_deleted():
641 raise RuntimeError("Cannot delete %s" % self
)
643 self
.db
.execute("UPDATE wiki_files SET deleted_at = NOW(), deleted_by = %s \
644 WHERE id = %s", author
.uid
if author
else None, self
.id)
646 def can_be_deleted(self
):
647 # Cannot be deleted if still in use
655 def deleted_at(self
):
656 return self
.data
.deleted_at
658 def get_latest_revision(self
):
659 revisions
= self
.get_revisions()
661 # Return first object
662 for rev
in revisions
:
665 def get_revisions(self
):
666 revisions
= self
.backend
.wiki
._get
_files
("SELECT * FROM wiki_files \
667 WHERE path = %s AND filename = %s ORDER BY created_at DESC", self
.path
, self
.filename
)
669 return list(revisions
)
672 return self
.mimetype
in ("application/pdf", "application/x-pdf")
675 return self
.mimetype
.startswith("image/")
677 def is_vector_image(self
):
678 return self
.mimetype
in ("image/svg+xml",)
680 def is_bitmap_image(self
):
681 return self
.is_image() and not self
.is_vector_image()
685 res
= self
.db
.get("SELECT data FROM wiki_blobs \
686 WHERE id = %s", self
.data
.blob_id
)
689 return bytes(res
.data
)
691 async def get_thumbnail(self
, size
, format
=None):
692 assert self
.is_bitmap_image()
694 cache_key
= ":".join((
698 util
.normalize(self
.filename
),
699 self
.created_at
.isoformat(),
704 # Try to fetch the data from the cache
705 thumbnail
= await self
.backend
.cache
.get(cache_key
)
709 # Generate the thumbnail
710 thumbnail
= util
.generate_thumbnail(self
.blob
, size
, format
=format
, quality
=95)
712 # Put it into the cache for forever
713 await self
.backend
.cache
.set(cache_key
, thumbnail
)
720 Returns a list of all pages this file is linked by
722 pages
= self
.backend
.wiki
._get
_pages
("""
728 wiki_current ON wiki_linked_files.page_id = wiki_current.id
730 wiki ON wiki_linked_files.page_id = wiki.id
732 wiki_linked_files.path = %s
735 """, os
.path
.join(self
.path
, self
.filename
),
741 class WikiRenderer(misc
.Object
):
754 _links
= re
.compile(r
"<a href=\"(.*?
)\">(.*?
)</a
>")
757 _images = re.compile(r"<img
alt(?
:=\"(.*?
)\")? src
=\"(.*?
)\" (?
:title
=\"(.*?
)\" )?
/>")
759 def init(self, path, text, revision=None):
763 # Optionally, the revision of the rendered page
764 self.revision = revision
767 self.renderer = markdown.Markdown(
769 LinkedFilesExtractorExtension(),
770 PrettyLinksExtension(),
782 self.html = self._render()
784 def _render_link(self, m):
785 url, text = m.groups()
788 for schema in self.schemas:
789 if url.startswith(schema):
790 return """<a class="link
-external
" href="%s">%s</a>""" % \
796 if url.startswith("mailto
:"):
799 return """<a class="link
-external
" href="mailto
:%s">%s</a>""" % \
802 # Everything else must be an internal link
803 path = self.backend.wiki.make_path(self.path, url)
805 return """<a href="/docs
%s">%s</a>""" % \
806 (path, text or self.backend.wiki.get_page_title(path))
808 def _render_image(self, m):
809 alt_text, url, caption = m.groups()
811 # Compute a hash over the URL
812 h = hashlib.new("md5
")
813 h.update(url.encode())
817 <div class="columns
is-centered
">
818 <div class="column
is-8">
819 <figure class="image modal
-trigger
" data-target="%(id)s">
820 <img src="/docs
%(url)s?s
=640&
;%(args)s" alt="%(caption)s">
822 <figcaption class="figure
-caption
">%(caption)s</figcaption>
825 <div class="modal
is-large
" id="%(id)s">
826 <div class="modal
-background
"></div>
828 <div class="modal
-content
">
830 <img src="/docs
%(url)s?s
=1920&
;%(args)s" alt="%(caption)s"
834 <a class="button
is-small
" href="/docs
%(url)s?action
=detail
">
836 <i class="fa
-solid fa
-circle
-info
"></i>
841 <button class="modal
-close
is-large
" aria-label="close
"></button>
847 # Skip any absolute and external URLs
848 if url.startswith("https
://") or url.startswith("http
://"):
850 "caption
" : caption or "",
856 # Try to split query string
857 url, delimiter, qs = url.partition("?
")
859 # Parse query arguments
860 args = urllib.parse.parse_qs(qs)
862 # Build absolute path
863 url = self.backend.wiki.make_path(self.path, url)
866 file = self.backend.wiki.get_file_by_path(url, revision=self.revision)
867 if not file or not file.is_image():
868 return "<!-- Could
not find image
%s in %s -->" % (url, self.path)
870 # Remove any requested size
874 # Link the image that has been the current version at the time of the page edit
876 args["revision
"] = file.timestamp
879 "caption
" : caption or "",
882 "args
" : urllib.parse.urlencode(args),
886 logging.debug("Rendering
%s" % self.path)
889 text = self.renderer.convert(self.text)
892 text = self._links.sub(self._render_link, text)
894 # Postprocess images to <figure>
895 text = self._images.sub(self._render_image, text)
902 A list of all linked files that have been part of the rendered markup
906 for url in self.renderer.files:
907 # Skip external images
908 if url.startswith("https
://") or url.startswith("http
://"):
911 # Make the URL absolute
912 url = self.backend.wiki.make_path(self.path, url)
914 # Check if this is a file (it could also just be a page)
915 file = self.backend.wiki.get_file_by_path(url)
922 class PrettyLinksExtension(markdown.extensions.Extension):
923 def extendMarkdown(self, md):
924 # Create links to Bugzilla
925 md.preprocessors.register(BugzillaLinksPreprocessor(md), "bugzilla
", 10)
927 # Create links to CVE
928 md.preprocessors.register(CVELinksPreprocessor(md), "cve
", 10)
931 class BugzillaLinksPreprocessor(markdown.preprocessors.Preprocessor):
932 regex = re.compile(r"(?
:#(\d{5,}))", re.I)
934 def run(self
, lines
):
936 yield self
.regex
.sub(r
"[#\1](https://bugzilla.ipfire.org/show_bug.cgi?id=\1)", line
)
939 class CVELinksPreprocessor(markdown
.preprocessors
.Preprocessor
):
940 regex
= re
.compile(r
"(?:CVE)[\s\-](\d{4}\-\d+)")
942 def run(self
, lines
):
944 yield self
.regex
.sub(r
"[CVE-\1](https://cve.mitre.org/cgi-bin/cvename.cgi?name=\1)", line
)
947 class LinkedFilesExtractor(markdown
.treeprocessors
.Treeprocessor
):
949 Finds all Linked Files
954 # Find all images and store the URLs
955 for image
in root
.findall(".//img"):
956 src
= image
.get("src")
958 self
.md
.files
.append(src
)
961 for link
in root
.findall(".//a"):
962 href
= link
.get("href")
964 self
.md
.files
.append(href
)
967 class LinkedFilesExtractorExtension(markdown
.extensions
.Extension
):
968 def extendMarkdown(self
, md
):
969 md
.treeprocessors
.register(LinkedFilesExtractor(md
), "linked-files-extractor", 10)