main_func_setting_sitemap.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from .tool.func import *
  2. def main_func_setting_sitemap():
  3. with get_db_connect() as conn:
  4. curs = conn.cursor()
  5. if admin_check() != 1:
  6. return re_error('/ban')
  7. if flask.request.method == 'POST':
  8. admin_check(None, 'make sitemap')
  9. data = '' + \
  10. '<?xml version="1.0" encoding="UTF-8"?>\n' + \
  11. '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + \
  12. ''
  13. if flask.request.form.get('exclude_domain', None):
  14. domain = ''
  15. else:
  16. domain = load_domain('full')
  17. sql_add = ''
  18. if flask.request.form.get('exclude_user_page', None):
  19. sql_add += ' title not like "user:%"'
  20. if flask.request.form.get('exclude_file_page', None):
  21. if sql_add != '':
  22. sql_add += ' and'
  23. sql_add += ' title not like "file:%"'
  24. if flask.request.form.get('exclude_category_page', None):
  25. if sql_add != '':
  26. sql_add += ' and'
  27. sql_add += ' title not like "category:%"'
  28. if sql_add != '':
  29. sql_add = ' where' + sql_add
  30. print(sql_add)
  31. curs.execute(db_change("select title from data" + sql_add))
  32. all_data = curs.fetchall()
  33. len_all_data = len(all_data)
  34. count = int(len_all_data / 30000)
  35. other_count = len_all_data % 30000
  36. for i in range(count + 1):
  37. data += '<sitemap><loc>' + domain + '/sitemap_' + str(i) + '.xml</loc></sitemap>\n'
  38. data += '' + \
  39. '</sitemapindex>' + \
  40. ''
  41. f = open("sitemap.xml", 'w')
  42. f.write(data)
  43. f.close()
  44. for i in range(count + 1):
  45. data = '' + \
  46. '<?xml version="1.0" encoding="UTF-8"?>\n' + \
  47. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + \
  48. ''
  49. if count == i:
  50. for x in all_data[30000 * i:]:
  51. data += '<url><loc>' + domain + '/w/' + url_pas(x[0]) + '</loc></url>\n'
  52. else:
  53. for x in all_data[30000 * i:30000 * (i + 1)]:
  54. data += '<url><loc>' + domain + '/w/' + url_pas(x[0]) + '</loc></url>\n'
  55. data += '' + \
  56. '</urlset>' + \
  57. ''
  58. f = open("sitemap_" + str(i) + ".xml", 'w')
  59. f.write(data)
  60. f.close()
  61. return redirect('/setting/sitemap')
  62. else:
  63. sitemap_list = ''
  64. if os.path.exists('sitemap.xml'):
  65. sitemap_list += '<a href="/sitemap.xml">(' + load_lang('view') + ')</a>'
  66. for_a = 0
  67. while os.path.exists('sitemap_' + str(for_a) + '.xml'):
  68. sitemap_list += ' <a href="/sitemap_' + str(for_a) + '.xml">(sitemap_' + str(for_a) + '.xml)</a>'
  69. for_a += 1
  70. return easy_minify(flask.render_template(skin_check(),
  71. imp = [load_lang('sitemap_management'), wiki_set(), wiki_custom(), wiki_css(['(' + load_lang('beta') + ')', 0])],
  72. data = '''
  73. ''' + sitemap_list + '''
  74. <hr class="main_hr">
  75. <form method="post">
  76. <input type="checkbox" name="exclude_domain"> ''' + load_lang('stiemap_exclude_domain') + '''
  77. <hr class="main_hr">
  78. <input type="checkbox" name="exclude_user_page"> ''' + load_lang('stiemap_exclude_user_page') + '''
  79. <hr class="main_hr">
  80. <input type="checkbox" name="exclude_file_page"> ''' + load_lang('stiemap_exclude_file_page') + '''
  81. <hr class="main_hr">
  82. <input type="checkbox" name="exclude_category_page"> ''' + load_lang('stiemap_exclude_category_page') + '''
  83. <hr class="main_hr">
  84. <button id="opennamu_save_button" type="submit">''' + load_lang('create') + '''</button>
  85. </form>
  86. ''',
  87. menu = [['setting', load_lang('return')]]
  88. ))