view_w.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. from .tool.func import *
  2. from .go_api_w_raw import api_w_raw
  3. from .go_api_w_render import api_w_render
  4. async def view_w(name = 'Test', do_type = ''):
  5. with get_db_connect() as conn:
  6. curs = conn.cursor()
  7. sub = 0
  8. history_color = 0
  9. menu = []
  10. user_doc = ''
  11. category_total = ''
  12. file_data = ''
  13. doc_type = ''
  14. redirect_to = None
  15. now_time = get_time()
  16. ip = ip_check()
  17. uppage = re.sub(r"/([^/]+)$", '', name)
  18. uppage = 0 if uppage == name else uppage
  19. curs.execute(db_change("select sub from rd where title = ? and not stop = 'O' order by date desc"), [name])
  20. topic = 1 if curs.fetchall() else 0
  21. curs.execute(db_change("select title from data where title like ?"), [name + '/%'])
  22. down = 1 if curs.fetchall() else 0
  23. if re.search(r'^category:', name):
  24. name_view = name
  25. doc_type = 'category'
  26. category_doc = ''
  27. category_sub = ''
  28. count_sub_category = 0
  29. count_category = 0
  30. curs.execute(db_change("select distinct link from back where title = ? and type = 'cat' order by link asc"), [name])
  31. category_sql = curs.fetchall()
  32. for data in category_sql:
  33. link_view = data[0]
  34. if get_main_skin_set(conn, flask.session, 'main_css_category_change_title', ip) != 'off':
  35. curs.execute(db_change("select data from back where title = ? and link = ? and type = 'cat_view' limit 1"), [name, data[0]])
  36. db_data = curs.fetchall()
  37. if db_data and db_data[0][0] != '':
  38. link_view = db_data[0][0]
  39. link_blur = ''
  40. curs.execute(db_change("select data from back where title = ? and link = ? and type = 'cat_blur' limit 1"), [name, data[0]])
  41. db_data = curs.fetchall()
  42. if db_data:
  43. link_blur = 'opennamu_category_blur'
  44. if data[0].startswith('category:'):
  45. category_sub += '<li><a class="' + link_blur + '" href="/w/' + url_pas(data[0]) + '">' + html.escape(link_view) + '</a></li>'
  46. count_sub_category += 1
  47. else:
  48. category_doc += '' + \
  49. '<li>' + \
  50. '<a class="' + link_blur + '" href="/w/' + url_pas(data[0]) + '">' + html.escape(link_view) + '</a> ' + \
  51. '<a class="opennamu_link_inter" href="/xref/' + url_pas(data[0]) + '">(' + get_lang(conn, 'backlink') + ')</a>' + \
  52. '</li>' + \
  53. ''
  54. count_category += 1
  55. if category_sub != '':
  56. category_total += '' + \
  57. '<h2 id="cate_under">' + get_lang(conn, 'under_category') + '</h2>' + \
  58. '<ul>' + \
  59. '<li>' + get_lang(conn, 'all') + ' : ' + str(count_sub_category) + '</li>' + \
  60. category_sub + \
  61. '</ul>' + \
  62. ''
  63. if category_doc != '':
  64. category_total += '' + \
  65. '<h2 id="cate_normal">' + get_lang(conn, 'category_title') + '</h2>' + \
  66. '<ul>' + \
  67. '<li>' + get_lang(conn, 'all') + ' : ' + str(count_category) + '</li>' + \
  68. category_doc + \
  69. '</ul>' + \
  70. ''
  71. elif re.search(r"^user:([^/]*)", name):
  72. name_view = name
  73. doc_type = 'user'
  74. user_name = ''
  75. match = re.search(r"^user:([^/]*)", name)
  76. if match:
  77. user_name = html.escape(match.group(1))
  78. user_doc = ''
  79. # S admin or owner 특수 틀 추가
  80. if acl_check(tool = 'all_admin_auth', ip = user_name) != 1:
  81. if acl_check(tool = 'owner_auth', ip = user_name) != 1:
  82. curs.execute(db_change('select data from other where name = "phrase_user_page_owner"'))
  83. db_data = curs.fetchall()
  84. if db_data and db_data[0][0] != '':
  85. user_doc += db_data[0][0] + '<br>'
  86. else:
  87. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  88. db_data = curs.fetchall()
  89. if db_data and db_data[0][0] != '':
  90. user_doc += db_data[0][0] + '<br>'
  91. else:
  92. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  93. db_data = curs.fetchall()
  94. if db_data and db_data[0][0] != '':
  95. user_doc += db_data[0][0] + '<br>'
  96. # E
  97. user_doc += '''
  98. <div id="opennamu_get_user_info">''' + html.escape(user_name) + '''</div>
  99. <hr class="main_hr">
  100. '''
  101. if name == 'user:' + user_name:
  102. menu += [['w/' + url_pas(name) + '/' + url_pas(now_time.split()[0]), get_lang(conn, 'today_doc')]]
  103. elif re.search(r"^file:", name):
  104. curs.execute(db_change('select id from history where title = ? order by date desc limit 1'), [name])
  105. db_data = curs.fetchall()
  106. rev = db_data[0][0] if db_data else '1'
  107. name_view = name
  108. doc_type = 'file'
  109. mime_type = re.search(r'([^.]+)$', name)
  110. if mime_type:
  111. mime_type = mime_type.group(1)
  112. else:
  113. mime_type = 'jpg'
  114. file_name = re.sub(r'\.([^.]+)$', '', name)
  115. file_name = re.sub(r'^file:', '', file_name)
  116. file_all_name = sha224_replace(file_name) + '.' + mime_type
  117. file_path_name = os.path.join(load_image_url(conn), file_all_name)
  118. if os.path.exists(file_path_name):
  119. try:
  120. img = Image.open(file_path_name)
  121. width, height = img.size
  122. file_res = str(width) + 'x' + str(height)
  123. except:
  124. file_res = 'Vector'
  125. file_size = str(round(os.path.getsize(file_path_name) / 1000, 1))
  126. file_data = '''
  127. <img src="/image/''' + url_pas(file_all_name) + '''.cache_v''' + rev + '''">
  128. <h2>''' + get_lang(conn, 'data') + '''</h2>
  129. <table>
  130. <tr><td>''' + get_lang(conn, 'url') + '''</td><td><a href="/image/''' + url_pas(file_all_name) + '''">''' + get_lang(conn, 'link') + '''</a></td></tr>
  131. <tr><td>''' + get_lang(conn, 'volume') + '''</td><td>''' + file_size + '''KB</td></tr>
  132. <tr><td>''' + get_lang(conn, 'resolution') + '''</td><td>''' + file_res + '''</td></tr>
  133. </table>
  134. <h2>''' + get_lang(conn, 'content') + '''</h2>
  135. '''
  136. menu += [['delete_file/' + url_pas(name), get_lang(conn, 'file_delete')]]
  137. else:
  138. file_data = ''
  139. else:
  140. curs.execute(db_change("select link from back where title = ? and type = 'include' limit 1"), [name])
  141. doc_type = 'include' if curs.fetchall() else doc_type
  142. curs.execute(db_change("select title, data from back where link = ? and type = 'redirect' limit 1"), [name])
  143. db_data = curs.fetchall()
  144. if db_data:
  145. doc_type = 'redirect'
  146. curs.execute(db_change("select title from data where title = ?"), [db_data[0][0]])
  147. if curs.fetchall():
  148. redirect_to = url_pas(db_data[0][0]) + db_data[0][1]
  149. name_view = name
  150. doc_data = json.loads((await api_w_raw(name)).data)
  151. if doc_data["response"] != "not exist":
  152. render_data = json.loads((await api_w_render(name, request_method = 'POST', request_data = {
  153. 'name' : name,
  154. 'data' : doc_data["data"]
  155. })).data)
  156. end_data = render_data["data"] + '<script>document.addEventListener("DOMContentLoaded", function() {' + render_data["js_data"] + '});</script>'
  157. else:
  158. end_data = ''
  159. curs.execute(db_change("select data from data where title = ?"), [name])
  160. data = curs.fetchall()
  161. description = ''
  162. if acl_check(name, 'render') == 1:
  163. response_data = 401
  164. curs.execute(db_change('select data from other where name = "error_401"'))
  165. sql_d = curs.fetchall()
  166. if sql_d and sql_d[0][0] != '':
  167. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul><li>' + sql_d[0][0] + '</li></ul>'
  168. else:
  169. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul><li>' + get_lang(conn, 'authority_error') + '</li></ul>'
  170. elif not data:
  171. response_data = 404
  172. curs.execute(db_change('select data from other where name = "error_404"'))
  173. db_data = curs.fetchall()
  174. if db_data and db_data[0][0] != '':
  175. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul><li>' + db_data[0][0] + '</li></ul>'
  176. else:
  177. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul><li>' + get_lang(conn, 'decument_404_error') + '</li></ul>'
  178. curs.execute(db_change('select ip from history where title = ? limit 1'), [name])
  179. db_data = curs.fetchall()
  180. history_color = 1 if db_data else 0
  181. else:
  182. response_data = 200
  183. description = data[0][0].replace('\r', '').replace('\n', ' ')[0:200]
  184. curs.execute(db_change("select title from acl where title = ?"), [name])
  185. acl = 1 if curs.fetchall() else 0
  186. menu_acl = 1 if acl_check(name, 'document_edit') == 1 else 0
  187. if response_data == 404:
  188. menu += [['edit/' + url_pas(name), get_lang(conn, 'create'), menu_acl]]
  189. else:
  190. menu += [['edit/' + url_pas(name), get_lang(conn, 'edit'), menu_acl]]
  191. menu += [
  192. ['topic/' + url_pas(name), get_lang(conn, 'discussion'), topic],
  193. ['history/' + url_pas(name), get_lang(conn, 'history'), history_color],
  194. ['xref/' + url_pas(name), get_lang(conn, 'backlink')],
  195. ['acl/' + url_pas(name), get_lang(conn, 'setting'), acl],
  196. ]
  197. if flask.session and 'lastest_document' in flask.session:
  198. if type(flask.session['lastest_document']) != type([]):
  199. flask.session['lastest_document'] = []
  200. else:
  201. flask.session['lastest_document'] = []
  202. if do_type == 'from':
  203. menu += [['w/' + url_pas(name), get_lang(conn, 'pass')]]
  204. last_page = ''
  205. for for_a in reversed(range(0, len(flask.session['lastest_document']))):
  206. last_page = flask.session['lastest_document'][for_a]
  207. curs.execute(db_change("select link from back where (title = ? or link = ?) and type = 'redirect' limit 1"), [last_page, last_page])
  208. if curs.fetchall():
  209. break
  210. if last_page != name:
  211. redirect_text = '{0} ➤ {1}'
  212. curs.execute(db_change('select data from other where name = "redirect_text"'))
  213. db_data = curs.fetchall()
  214. if db_data and db_data[0][0] != '':
  215. redirect_text = db_data[0][0]
  216. try:
  217. redirect_text = redirect_text.format('<a href="/w_from/' + url_pas(last_page) + '">' + html.escape(last_page) + '</a>', '<b>' + html.escape(name) + '</b>')
  218. except:
  219. redirect_text = '{0} ➤ {1}'
  220. redirect_text = redirect_text.format('<a href="/w_from/' + url_pas(last_page) + '">' + html.escape(last_page) + '</a>', '<b>' + html.escape(name) + '</b>')
  221. end_data = '''
  222. <div class="opennamu_redirect" id="redirect">
  223. ''' + redirect_text + '''
  224. </div>
  225. <hr class="main_hr">
  226. ''' + end_data
  227. if len(flask.session['lastest_document']) >= 10:
  228. flask.session['lastest_document'] = flask.session['lastest_document'][-9:] + [name]
  229. else:
  230. flask.session['lastest_document'] += [name]
  231. flask.session['lastest_document'] = list(reversed(dict.fromkeys(reversed(flask.session['lastest_document']))))
  232. if redirect_to and do_type != 'from':
  233. return redirect(conn, '/w_from/' + redirect_to)
  234. view_history_on = get_main_skin_set(conn, flask.session, 'main_css_view_history', ip)
  235. if view_history_on == 'on':
  236. end_data = '' + \
  237. '<div class="opennamu_trace">' + \
  238. '<a class="opennamu_trace_button" href="javascript:opennamu_do_trace_spread();"> (+)</a>' + \
  239. get_lang(conn, 'trace') + ' : ' + \
  240. ' ← '.join(
  241. [
  242. '<a href="/w/' + url_pas(for_a) + '">' + html.escape(for_a) + '</a>'
  243. for for_a in reversed(flask.session['lastest_document'])
  244. ]
  245. ) + \
  246. '</div>' + \
  247. '<hr class="main_hr">' + \
  248. '' + end_data
  249. if uppage != 0:
  250. menu += [['w/' + url_pas(uppage), get_lang(conn, 'upper')]]
  251. if down:
  252. menu += [['down/' + url_pas(name), get_lang(conn, 'sub')]]
  253. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'last_edit'"), [name])
  254. r_date = curs.fetchall()
  255. r_date = r_date[0][0] if r_date else 0
  256. curs.execute(db_change("select data from other where name = 'not_use_view_count'"))
  257. view_count_set = curs.fetchall()
  258. if view_count_set and view_count_set[0][0] != "":
  259. view_count = 0
  260. else:
  261. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'view_count' and doc_rev = ''"), [name])
  262. view_count = curs.fetchall()
  263. view_count = view_count[0][0] if view_count else 0
  264. div = file_data + user_doc + end_data + category_total
  265. if doc_type == '':
  266. curs.execute(db_change('select data from other where name = "outdated_doc_warning_date"'))
  267. db_data = curs.fetchall()
  268. if db_data and db_data[0][0] != '' and r_date != 0:
  269. time_1 = datetime.datetime.strptime(r_date, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days = int(number_check(db_data[0][0])))
  270. time_2 = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
  271. if time_2 > time_1:
  272. curs.execute(db_change('select data from other where name = "outdated_doc_warning"'))
  273. db_data = curs.fetchall()
  274. div = (db_data[0][0] if db_data and db_data[0][0] != '' else get_lang(conn, 'old_page_warning')) + '<hr class="main_hr">' + div
  275. curs.execute(db_change("select data from other where name = 'body'"))
  276. body = curs.fetchall()
  277. div = (body[0][0] + div) if body else div
  278. curs.execute(db_change("select data from other where name = 'bottom_body'"))
  279. body = curs.fetchall()
  280. div += body[0][0] if body else ''
  281. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'document_top'"), [name])
  282. body = curs.fetchall()
  283. div = (body[0][0] + div) if body else div
  284. if ip_or_user(ip) == 0:
  285. curs.execute(db_change("select data from user_set where id = ? and data = ?"), [ip, name])
  286. watch_list = 2 if curs.fetchall() else 1
  287. menu += [['star_doc_from/' + url_pas(name), ('☆' if watch_list == 1 else '★'), watch_list - 1]]
  288. else:
  289. watch_list = 0
  290. menu += [['doc_watch_list/1/' + url_pas(name), get_lang(conn, 'watchlist')]]
  291. return easy_minify(conn, flask.render_template(skin_check(conn),
  292. imp = [name_view, wiki_set(conn), wiki_custom(conn), wiki_css([sub, r_date, watch_list, description, view_count])],
  293. data = div,
  294. menu = menu
  295. )), response_data