# Path: wikked/web.py
# def get_wiki():
# def uncaching_wiki_updater(wiki, url):
# def user_loader(username):
# def get_read_url(url):
# def get_edit_url(url):
# def get_rev_url(url, rev):
# def get_diff_url(url, rev1=None, rev2=None):
#     def async_updater(wiki):
#     def on_request_started(sender, **extra):
#     def on_request_tearing_down(sender, **extra):
# 
# Path: wikked/webimpl/decorators.py
# def requires_permission(perm):
#     if isinstance(perm, str):
#         perm = [perm]
# 
#     p_bit = 0
#     for p in perm:
#         p_bit |= PERM_NAMES[p]
# 
#     def decorator(f):
#         @functools.wraps(f)
#         def wrapper(*args, **kwargs):
#             wiki = get_wiki()
#             if not wiki.auth.hasPermission(current_user.get_id(), p_bit):
#                 raise UserPermissionError(
#                     perm, "You don't have permission for this.")
#             return f(*args, **kwargs)
#         return wrapper
#     return decorator
# 
# Path: wikked/webimpl/special.py
# def get_orphans(wiki, user):
#     def builder_func():
#         wiki.resolve()
# 
#         pages = {}
#         rev_links = {}
#         for p in wiki.getPages(
#                 no_endpoint_only=True,
#                 fields=['url', 'title', 'local_meta', 'meta', 'links']):
#             pages[p.url] = p
#             rev_links.setdefault(p.url, 0)
# 
#             for l in p.links:
#                 abs_l = get_absolute_url(p.url, l)
#                 cnt = rev_links.get(abs_l, 0)
#                 rev_links[abs_l] = cnt + 1
# 
#         or_pages = []
#         for tgt, cnt in rev_links.items():
#             if cnt == 0:
#                 or_pages.append(pages[tgt])
#         return or_pages
# 
#     fields = ['url', 'title', 'local_meta', 'meta', 'links']
#     pages = get_or_build_pagelist(wiki, 'orphans', builder_func, fields)
#     return build_pagelist_view_data(pages, user)
# 
# def get_broken_redirects(wiki, user):
#     def filter_func(page):
#         redirect_meta = page.getMeta('redirect')
#         if redirect_meta is None:
#             return False
# 
#         path = get_absolute_url(page.url, redirect_meta)
#         try:
#             target, visited = get_redirect_target(
#                     path,
#                     fields=['url', 'local_meta', 'meta'])
#         except CircularRedirectError:
#             return True
#         except RedirectNotFoundError:
#             return True
#         return False
# 
#     return generic_pagelist_view(wiki, user, 'broken_redirects', filter_func)
# 
# def get_double_redirects(wiki, user):
#     def builder_func():
#         wiki.resolve()
# 
#         pages = {}
#         redirs = {}
#         for p in wiki.getPages(
#                 no_endpoint_only=True,
#                 fields=['url', 'title', 'local_meta', 'meta']):
#             pages[p.url] = p
# 
#             target = p.getMeta('redirect')
#             if target:
#                 target = get_absolute_url(p.url, target)
#                 redirs[p.url] = target
# 
#         dr_pages = []
#         for src, tgt in redirs.items():
#             if tgt in redirs:
#                 dr_pages.append(pages[src])
#         return dr_pages
# 
#     fields = ['url', 'title', 'local_meta', 'meta']
#     pages = get_or_build_pagelist(wiki, 'double_redirects', builder_func,
#                                   fields)
#     return build_pagelist_view_data(pages, user)
# 
# def get_dead_ends(wiki, user):
#     def filter_func(page):
#         return len(page.links) == 0
# 
#     return generic_pagelist_view(
#             wiki, user, 'dead_ends', filter_func,
#             fields=['url', 'title', 'local_meta', 'meta', 'links'])
# 
# def get_search_results(wiki, user, query):
#     readable_hits = []
#     hits = list(wiki.index.search(query))
#     for h in hits:
#         try:
#             get_page_or_raise(wiki, h.url,
#                               check_perms=(user, 'read'))
#         except UserPermissionError:
#             continue
# 
#         readable_hits.append({
#                 'url': h.url,
#                 'title': h.title,
#                 'text': h.hl_text})
# 
#     result = {
#             'query': query,
#             'hit_count': len(readable_hits),
#             'hits': readable_hits}
#     return result
# 
# def get_search_preview_results(wiki, user, query):
#     readable_hits = []
#     hits = list(wiki.index.previewSearch(query))
#     for h in hits:
#         try:
#             get_page_or_raise(wiki, h.url,
#                               check_perms=(user, 'read'))
#         except UserPermissionError:
#             continue
# 
#         readable_hits.append({'url': h.url, 'title': h.title})
# 
#     result = {
#             'query': query,
#             'hit_count': len(readable_hits),
#             'hits': readable_hits}
#     return result


from flask import jsonify, request, abort
from flask_login import current_user
from wikked.web import app, get_wiki
from wikked.webimpl.decorators import requires_permission
from wikked.webimpl.special import (
        get_orphans, get_broken_redirects, get_double_redirects,
        get_dead_ends, get_search_results, get_search_preview_results)


def call_api(api_func, *args, **kwargs):
    wiki = get_wiki()
    user = current_user.get_id()
    result = api_func(wiki, user, *args, **kwargs)
    return jsonify(result)


@app.route('/api/orphans')
def api_special_orphans():
    return call_api(get_orphans)


@app.route('/api/broken-redirects')
def api_special_broken_redirects():
    return call_api(get_broken_redirects)


@app.route('/api/double-redirects')
def api_special_double_redirects():
    return call_api(get_double_redirects)



# Path: wikked/web.py
# def get_wiki():
# def uncaching_wiki_updater(wiki, url):
# def user_loader(username):
# def get_read_url(url):
# def get_edit_url(url):
# def get_rev_url(url, rev):
# def get_diff_url(url, rev1=None, rev2=None):
#     def async_updater(wiki):
#     def on_request_started(sender, **extra):
#     def on_request_tearing_down(sender, **extra):
# 
# Path: wikked/webimpl/decorators.py
# def requires_permission(perm):
#     if isinstance(perm, str):
#         perm = [perm]
# 
#     p_bit = 0
#     for p in perm:
#         p_bit |= PERM_NAMES[p]
# 
#     def decorator(f):
#         @functools.wraps(f)
#         def wrapper(*args, **kwargs):
#             wiki = get_wiki()
#             if not wiki.auth.hasPermission(current_user.get_id(), p_bit):
#                 raise UserPermissionError(
#                     perm, "You don't have permission for this.")
#             return f(*args, **kwargs)
#         return wrapper
#     return decorator
# 
# Path: wikked/webimpl/special.py
# def get_orphans(wiki, user):
#     def builder_func():
#         wiki.resolve()
# 
#         pages = {}
#         rev_links = {}
#         for p in wiki.getPages(
#                 no_endpoint_only=True,
#                 fields=['url', 'title', 'local_meta', 'meta', 'links']):
#             pages[p.url] = p
#             rev_links.setdefault(p.url, 0)
# 
#             for l in p.links:
#                 abs_l = get_absolute_url(p.url, l)
#                 cnt = rev_links.get(abs_l, 0)
#                 rev_links[abs_l] = cnt + 1
# 
#         or_pages = []
#         for tgt, cnt in rev_links.items():
#             if cnt == 0:
#                 or_pages.append(pages[tgt])
#         return or_pages
# 
#     fields = ['url', 'title', 'local_meta', 'meta', 'links']
#     pages = get_or_build_pagelist(wiki, 'orphans', builder_func, fields)
#     return build_pagelist_view_data(pages, user)
# 
# def get_broken_redirects(wiki, user):
#     def filter_func(page):
#         redirect_meta = page.getMeta('redirect')
#         if redirect_meta is None:
#             return False
# 
#         path = get_absolute_url(page.url, redirect_meta)
#         try:
#             target, visited = get_redirect_target(
#                     path,
#                     fields=['url', 'local_meta', 'meta'])
#         except CircularRedirectError:
#             return True
#         except RedirectNotFoundError:
#             return True
#         return False
# 
#     return generic_pagelist_view(wiki, user, 'broken_redirects', filter_func)
# 
# def get_double_redirects(wiki, user):
#     def builder_func():
#         wiki.resolve()
# 
#         pages = {}
#         redirs = {}
#         for p in wiki.getPages(
#                 no_endpoint_only=True,
#                 fields=['url', 'title', 'local_meta', 'meta']):
#             pages[p.url] = p
# 
#             target = p.getMeta('redirect')
#             if target:
#                 target = get_absolute_url(p.url, target)
#                 redirs[p.url] = target
# 
#         dr_pages = []
#         for src, tgt in redirs.items():
#             if tgt in redirs:
#                 dr_pages.append(pages[src])
#         return dr_pages
# 
#     fields = ['url', 'title', 'local_meta', 'meta']
#     pages = get_or_build_pagelist(wiki, 'double_redirects', builder_func,
#                                   fields)
#     return build_pagelist_view_data(pages, user)
# 
# def get_dead_ends(wiki, user):
#     def filter_func(page):
#         return len(page.links) == 0
# 
#     return generic_pagelist_view(
#             wiki, user, 'dead_ends', filter_func,
#             fields=['url', 'title', 'local_meta', 'meta', 'links'])
# 
# def get_search_results(wiki, user, query):
#     readable_hits = []
#     hits = list(wiki.index.search(query))
#     for h in hits:
#         try:
#             get_page_or_raise(wiki, h.url,
#                               check_perms=(user, 'read'))
#         except UserPermissionError:
#             continue
# 
#         readable_hits.append({
#                 'url': h.url,
#                 'title': h.title,
#                 'text': h.hl_text})
# 
#     result = {
#             'query': query,
#             'hit_count': len(readable_hits),
#             'hits': readable_hits}
#     return result
# 
# def get_search_preview_results(wiki, user, query):
#     readable_hits = []
#     hits = list(wiki.index.previewSearch(query))
#     for h in hits:
#         try:
#             get_page_or_raise(wiki, h.url,
#                               check_perms=(user, 'read'))
#         except UserPermissionError:
#             continue
# 
#         readable_hits.append({'url': h.url, 'title': h.title})
# 
#     result = {
#             'query': query,
#             'hit_count': len(readable_hits),
#             'hits': readable_hits}
#     return result


# Path: wikked/api/special.py
from flask import jsonify, request, abort
from flask_login import current_user
from wikked.web import app, get_wiki
from wikked.webimpl.decorators import requires_permission
from wikked.webimpl.special import (
        get_orphans, get_broken_redirects, get_double_redirects,
        get_dead_ends, get_search_results, get_search_preview_results)



def call_api(api_func, *args, **kwargs):
    wiki = get_wiki()
    user = current_user.get_id()
    result = api_func(wiki, user, *args, **kwargs)
    return jsonify(result)


@app.route('/api/orphans')
def api_special_orphans():
    return call_api(get_orphans)


@app.route('/api/broken-redirects')
def api_special_broken_redirects():
    return call_api(get_broken_redirects)


@app.route('/api/double-redirects')
def api_special_double_redirects():
    return call_api(get_double_redirects)



@app.route('/api/dead-ends')