Piki is a minimal wiki
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

page.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. import difflib
  2. from django.conf import settings
  3. from django.utils.translation import gettext as _
  4. import fstools
  5. import json
  6. import logging
  7. from pages import messages, url_page
  8. import mycreole
  9. import os
  10. import shutil
  11. import time
  12. from . import timestamp_to_datetime
  13. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  14. SPLITCHAR = ":"
  15. HISTORY_FOLDER_NAME = 'history'
  16. def full_path_all_pages(expression="*"):
  17. system_pages = fstools.dirlist(settings.SYSTEM_PAGES_ROOT, expression=expression, rekursive=False)
  18. system_pages = [os.path.join(settings.PAGES_ROOT, os.path.basename(path)) for path in system_pages]
  19. pages = fstools.dirlist(settings.PAGES_ROOT, expression=expression, rekursive=False)
  20. rv = []
  21. for path in set(system_pages + pages):
  22. p = page_wrapped(None, path)
  23. if p.is_available():
  24. rv.append(path)
  25. return rv
  26. def full_path_deleted_pages(expression="*"):
  27. system_pages = fstools.dirlist(settings.SYSTEM_PAGES_ROOT, expression=expression, rekursive=False)
  28. system_pages = [os.path.join(settings.PAGES_ROOT, os.path.basename(path)) for path in system_pages]
  29. pages = fstools.dirlist(settings.PAGES_ROOT, expression=expression, rekursive=False)
  30. rv = []
  31. for path in set(system_pages + pages):
  32. p = page_wrapped(None, path)
  33. if not p.is_available():
  34. rv.append(path)
  35. return rv
  36. class meta_data(dict):
  37. META_FILE_NAME = 'meta.json'
  38. #
  39. KEY_CREATION_TIME = "creation_time"
  40. KEY_MODIFIED_TIME = "modified_time"
  41. KEY_MODIFIED_USER = "modified_user"
  42. KEY_TAGS = "tags"
  43. def __init__(self, path, history_version=None):
  44. self._path = path
  45. self._history_version = history_version
  46. #
  47. # Load data from disk
  48. try:
  49. with open(self.filename, 'r') as fh:
  50. super().__init__(json.load(fh))
  51. except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
  52. super().__init__()
  53. def delete(self):
  54. os.remove(self.filename)
  55. @property
  56. def filename(self):
  57. if not self._history_version:
  58. return os.path.join(self._path, self.META_FILE_NAME)
  59. else:
  60. return self.history_filename(self._history_version)
  61. def history_filename(self, history_version):
  62. return os.path.join(self._path, HISTORY_FOLDER_NAME, "%05d_%s" % (history_version, self.META_FILE_NAME))
  63. def update_required(self, tags):
  64. return tags != self.get(self.KEY_TAGS)
  65. def update(self, username, tags):
  66. if self._history_version:
  67. logger.error("A history version %05d can not be updated!", self._history_version)
  68. return False
  69. else:
  70. if username:
  71. self[self.KEY_MODIFIED_TIME] = int(time.time())
  72. self[self.KEY_MODIFIED_USER] = username
  73. if self.KEY_CREATION_TIME not in self:
  74. self[self.KEY_CREATION_TIME] = self[self.KEY_MODIFIED_TIME]
  75. if tags:
  76. self[self.KEY_TAGS] = tags
  77. #
  78. if username or tags:
  79. self.save()
  80. return True
  81. def save(self):
  82. if self._history_version:
  83. logger.error("A history version %05d can not be updated!", self._history_version)
  84. return False
  85. else:
  86. with open(self.filename, 'w') as fh:
  87. json.dump(self, fh, indent=4)
  88. return True
  89. def store_to_history(self, history_number):
  90. history_filename = self.history_filename(history_number)
  91. fstools.mkdir(os.path.dirname(history_filename))
  92. shutil.copy(self.filename, history_filename)
  93. class page_data(object):
  94. PAGE_FILE_NAME = 'page'
  95. def __init__(self, path, history_version=None):
  96. self._history_version = history_version
  97. self._path = path
  98. self._raw_page_src = None
  99. def _load_page_src(self):
  100. if self._raw_page_src is None:
  101. try:
  102. with open(self.filename, 'r') as fh:
  103. self._raw_page_src = fh.read()
  104. except FileNotFoundError:
  105. self._raw_page_src = ""
  106. def delete(self):
  107. os.remove(self.filename)
  108. def rename(self, page_name):
  109. # Change backslash to slash and remove double slashes
  110. page_name = page_name.replace("\\", "/")
  111. while "//" in page_name:
  112. page_name = page_name.replace("//", "/")
  113. # move path
  114. target_path = os.path.join(settings.PAGES_ROOT, page_name.replace("/", 2*SPLITCHAR))
  115. shutil.move(self._path, target_path)
  116. # set my path
  117. self._path = target_path
  118. def update_required(self, page_txt):
  119. return page_txt.replace("\r\n", "\n") != self.raw_page_src
  120. def update_page(self, page_txt):
  121. if self._history_version:
  122. logger.error("A history version %05d can not be updated!", self._history_version)
  123. return False
  124. else:
  125. # save the new page content
  126. fstools.mkdir(os.path.dirname(self.filename))
  127. with open(self.filename, 'w') as fh:
  128. fh.write(page_txt)
  129. self._raw_page_src = page_txt
  130. return True
  131. @property
  132. def filename(self):
  133. if not self._history_version:
  134. return os.path.join(self._path, self.PAGE_FILE_NAME)
  135. else:
  136. return self.history_filename(self._history_version)
  137. def history_filename(self, history_version):
  138. return os.path.join(self._path, HISTORY_FOLDER_NAME, "%05d_%s" % (history_version, self.PAGE_FILE_NAME))
  139. @property
  140. def rel_path(self):
  141. return os.path.basename(self._path).replace(2*SPLITCHAR, "/")
  142. def is_available(self):
  143. is_a = os.path.isfile(self.filename)
  144. if not is_a:
  145. logger.debug("page.is_available: Not available - %s", self.filename)
  146. return is_a
  147. @property
  148. def title(self):
  149. return os.path.basename(self._path).split(2*SPLITCHAR)[-1]
  150. @property
  151. def raw_page_src(self):
  152. self._load_page_src()
  153. return self._raw_page_src
  154. def store_to_history(self, history_number):
  155. history_filename = self.history_filename(history_number)
  156. fstools.mkdir(os.path.dirname(history_filename))
  157. shutil.copy(self.filename, history_filename)
  158. class page_django(page_data):
  159. FOLDER_ATTACHMENTS = "attachments"
  160. def __init__(self, request, path, history_version=None) -> None:
  161. self._request = request
  162. super().__init__(path, history_version=history_version)
  163. @property
  164. def attachment_path(self):
  165. return os.path.join(os.path.basename(self._path), self.FOLDER_ATTACHMENTS)
  166. def render_to_html(self):
  167. if self.is_available():
  168. return self.render_text(self._request, self.raw_page_src)
  169. else:
  170. messages.unavailable_msg_page(self._request, self.rel_path)
  171. return ""
  172. def history_numbers_list(self):
  173. history_folder = os.path.join(self._path, HISTORY_FOLDER_NAME)
  174. return list(set([int(os.path.basename(filename)[:5]) for filename in fstools.filelist(history_folder)]))
  175. def render_meta(self, ctime, mtime, user, tags):
  176. #
  177. # Page meta data
  178. #
  179. meta = f'=== {_("Meta data")}\n'
  180. meta += f'|{_("Created")}:|{timestamp_to_datetime(self._request, ctime)}|\n'
  181. meta += f'|{_("Modified")}:|{timestamp_to_datetime(self._request, mtime)}|\n'
  182. meta += f'|{_("Editor")}|{user}|\n'
  183. meta += f'|{_("Tags")}|{tags}|\n'
  184. #
  185. # List of hostory page versions
  186. #
  187. hnl = self.history_numbers_list()
  188. if hnl:
  189. meta += f'=== {_("History")}\n'
  190. meta += f'| ={_("Version")} | ={_("Date")} | ={_("Page")} | ={_("Meta data")} | \n'
  191. # Current
  192. name = _("Current")
  193. meta += f"| {name} \
  194. | {timestamp_to_datetime(self._request, mtime)} \
  195. | [[{url_page(self.rel_path)} | Page]] \
  196. | [[{url_page(self.rel_path, meta=None)} | Meta]]\n"
  197. # History
  198. for num in reversed(hnl):
  199. p = page_wrapped(self._request, self._path, history_version=num)
  200. meta += f"| {num} \
  201. | {timestamp_to_datetime(self._request, p.modified_time)} \
  202. | [[{url_page(p.rel_path, history=num)} | Page]] \
  203. | [[{url_page(p.rel_path, meta=None, history=num)} | Meta]] (with page changes)\n"
  204. # Diff
  205. html_diff = ""
  206. if self._history_version:
  207. meta += f'=== {_("Page differences")}\n'
  208. #
  209. c = page_django(self._request, self._path)
  210. left_lines = c.raw_page_src.splitlines()
  211. right_lines = self.raw_page_src.splitlines()
  212. html_diff = difflib.HtmlDiff(wrapcolumn=80).make_table(left_lines, right_lines)
  213. #
  214. return mycreole.render_simple(meta) + html_diff
  215. def render_text(self, request, txt):
  216. macros = {
  217. "subpages": self.macro_subpages,
  218. "allpages": self.macro_allpages,
  219. "subpagetree": self.macro_subpagetree,
  220. "allpagestree": self.macro_allpagestree,
  221. }
  222. return mycreole.render(request, txt, self.attachment_path, macros=macros)
  223. def macro_allpages(self, *args, **kwargs):
  224. kwargs["allpages"] = True
  225. return self.macro_subpages(*args, **kwargs)
  226. def macro_subpages(self, *args, **kwargs):
  227. allpages = kwargs.pop("allpages", False)
  228. tree = kwargs.pop("tree", False)
  229. #
  230. def parse_depth(s: str):
  231. try:
  232. return int(s)
  233. except ValueError:
  234. pass
  235. params = kwargs.get('', '')
  236. filter_str = ''
  237. depth = parse_depth(params)
  238. if depth is None:
  239. params = params.split(",")
  240. depth = parse_depth(params[0])
  241. if len(params) == 2:
  242. filter_str = params[1]
  243. elif depth is None:
  244. filter_str = params[0]
  245. #
  246. rv = ""
  247. # create a page_list
  248. if allpages:
  249. expression = "*"
  250. parent_rel_path = ""
  251. else:
  252. expression = os.path.basename(self._path) + 2 * SPLITCHAR + "*"
  253. parent_rel_path = self.rel_path
  254. #
  255. pl = page_list(
  256. self._request,
  257. [page_django(self._request, path) for path in full_path_all_pages(expression)]
  258. )
  259. if tree:
  260. return page_tree(pl).html()
  261. else:
  262. return pl.html_list(depth=depth, filter_str=filter_str, parent_rel_path=parent_rel_path)
  263. def macro_allpagestree(self, *args, **kwargs):
  264. kwargs["allpages"] = True
  265. kwargs["tree"] = True
  266. return self.macro_subpages(*args, **kwargs)
  267. def macro_subpagetree(self, * args, **kwargs):
  268. kwargs["tree"] = True
  269. return self.macro_subpages(*args, **kwargs)
  270. class page_list(list):
  271. def __init__(self, request, *args, **kwargs):
  272. self._request = request
  273. return super().__init__(*args, **kwargs)
  274. def sort_basename(self):
  275. return list.sort(self, key=lambda x: os.path.basename(x.rel_path))
  276. def creole_list(self, depth=None, filter_str='', parent_rel_path=''):
  277. self.sort_basename()
  278. depth = depth or 9999 # set a random high value if None
  279. #
  280. parent_rel_path = parent_rel_path + "/" if len(parent_rel_path) > 0 else ""
  281. #
  282. rv = ""
  283. last_char = None
  284. for page in self:
  285. name = page.rel_path[len(parent_rel_path):]
  286. if name.startswith(filter_str) and name != filter_str:
  287. if name.count('/') < depth:
  288. first_char = os.path.basename(name)[0].upper()
  289. if last_char != first_char:
  290. last_char = first_char
  291. rv += f"=== {first_char}\n"
  292. rv += f"* [[{url_page(page.rel_path)} | {name} ]]\n"
  293. return rv
  294. def html_list(self, depth=9999, filter_str='', parent_rel_path=''):
  295. return mycreole.render_simple(self.creole_list(depth, filter_str, parent_rel_path))
  296. class page_tree(dict):
  297. T_PATTERN = "├── "
  298. L_PATTERN = "└── "
  299. I_PATTERN = "│   "
  300. D_PATTERN = "   &nbsp;&nbsp;&nbsp;"
  301. def __init__(self, pl: page_list):
  302. super().__init__()
  303. for page in pl:
  304. store_item = self
  305. for entry in page.rel_path.split("/"):
  306. if not entry in store_item:
  307. store_item[entry] = {}
  308. store_item = store_item[entry]
  309. def html(self, rel_path=None, fill=""):
  310. base = self
  311. try:
  312. for key in rel_path.split("/"):
  313. base = base[key]
  314. except AttributeError:
  315. rel_path = ''
  316. #
  317. rv = ""
  318. #
  319. l = len(base)
  320. for entry in sorted(list(base.keys())):
  321. l -= 1
  322. page_path = os.path.join(rel_path, entry)
  323. page = page_wrapped(None, page_path)
  324. if page.is_available():
  325. entry = f'<a href="{url_page(page_path)}">{entry}</a>'
  326. rv += fill + (self.L_PATTERN if l == 0 else self.T_PATTERN) + entry + "<br>\n"
  327. rv += self.html(page_path, fill=fill+(self.D_PATTERN if l == 0 else self.I_PATTERN))
  328. return rv
  329. class page_wrapped(object):
  330. """
  331. This class holds different page and meta instances and decides which will be used in which case.
  332. """
  333. def __init__(self, request, path, history_version=None):
  334. """_summary_
  335. Args:
  336. request (_type_): The django request or None (if None, the page functionality is limited)
  337. path (_type_): A rel_path of the django page or the filesystem path to the page
  338. history_version (_type_, optional): The history version of the page to be created
  339. """
  340. self._request = request
  341. #
  342. page_path = self.__page_path__(path)
  343. system_page_path = self.__system_page_path__(path)
  344. # Page
  345. if request:
  346. self._page = page_django(request, page_path, history_version=history_version)
  347. else:
  348. self._page = page_data(page_path, history_version=history_version)
  349. self._page_meta = meta_data(page_path, history_version=history_version)
  350. # System page
  351. if request:
  352. self._system_page = page_django(request, system_page_path)
  353. else:
  354. self._system_page = page_data(system_page_path)
  355. self._system_meta_data = meta_data(system_page_path)
  356. def __page_path__(self, path):
  357. if path.startswith(settings.PAGES_ROOT):
  358. # must be a filesystem path
  359. return path
  360. else:
  361. # must be a relative url
  362. return os.path.join(settings.PAGES_ROOT, path.replace("/", 2*SPLITCHAR))
  363. def __system_page_path__(self, path):
  364. return os.path.join(settings.SYSTEM_PAGES_ROOT, os.path.basename(path))
  365. def __page_choose__(self):
  366. if not self._page.is_available():
  367. return self._system_page
  368. else:
  369. return self._page
  370. def __meta_choose__(self):
  371. if not self._page.is_available():
  372. return self._system_meta_data
  373. else:
  374. return self._page_meta
  375. def __store_history__(self):
  376. if self._page.is_available():
  377. try:
  378. history_number = max(self._page.history_numbers_list()) + 1
  379. except ValueError:
  380. history_number = 1 # no history yet
  381. self._page.store_to_history(history_number)
  382. self._page_meta.store_to_history(history_number)
  383. #
  384. # meta_data
  385. #
  386. @property
  387. def creation_time(self):
  388. meta = self.__meta_choose__()
  389. rv = meta.get(meta.KEY_CREATION_TIME)
  390. return rv
  391. def delete(self):
  392. self.__store_history__()
  393. self._page.delete()
  394. self._page_meta.delete()
  395. @property
  396. def modified_time(self):
  397. meta = self.__meta_choose__()
  398. rv = meta.get(meta.KEY_MODIFIED_TIME)
  399. return rv
  400. @property
  401. def modified_user(self):
  402. meta = self.__meta_choose__()
  403. rv = meta.get(meta.KEY_MODIFIED_USER)
  404. return rv
  405. def rename(self, page_name):
  406. self._page.rename(page_name)
  407. @property
  408. def tags(self):
  409. meta = self.__meta_choose__()
  410. rv = meta.get(meta.KEY_TAGS)
  411. return rv
  412. #
  413. # page
  414. #
  415. @property
  416. def attachment_path(self):
  417. page = self.__page_choose__()
  418. rv = page.attachment_path
  419. return rv
  420. def is_available(self):
  421. return self._page.is_available() or self._system_page.is_available()
  422. def userpage_is_available(self):
  423. return self._page.is_available()
  424. @property
  425. def raw_page_src(self):
  426. page = self.__page_choose__()
  427. rv = page.raw_page_src
  428. return rv
  429. @property
  430. def rel_path(self):
  431. page = self.__page_choose__()
  432. rv = page.rel_path
  433. return rv
  434. def render_meta(self):
  435. page = self.__page_choose__()
  436. rv = page.render_meta(self.creation_time, self.modified_time, self.modified_user, self.tags)
  437. return rv
  438. def render_to_html(self):
  439. page = self.__page_choose__()
  440. rv = page.render_to_html()
  441. return rv
  442. def render_text(self, request, txt):
  443. page = self.__page_choose__()
  444. rv = page.render_text(request, txt)
  445. return rv
  446. @property
  447. def title(self):
  448. page = self.__page_choose__()
  449. rv = page.title
  450. return rv
  451. def update_page(self, txt, tags):
  452. if self._page.update_required(txt) or self._page_meta.update_required(tags):
  453. rv = False
  454. # Store history
  455. self.__store_history__()
  456. username = None
  457. if self._page.update_required(txt):
  458. # Update page
  459. rv |= self._page.update_page(txt)
  460. # Identify username, to update meta
  461. try:
  462. if self._request.user.is_authenticated:
  463. username = self._request.user.username
  464. else:
  465. logger.warning("Page edit without having a logged in user. This is not recommended. Check your access definitions!")
  466. except AttributeError:
  467. logger.exception("Page edit without having a request object. Check programming!")
  468. rv |= self._page_meta.update(username, tags)
  469. # Update search index
  470. from pages.search import update_item
  471. update_item(self)
  472. return rv