'Search links page.'

@base_listing
@require_oauth2_scope('read')
@validate(query=VLength('q', max_length=512), sort=VMenu('sort', SearchSortMenu, remember=False), recent=VMenu('t', TimeMenu, remember=False), restrict_sr=VBoolean('restrict_sr', default=False), include_facets=VBoolean('include_facets', default=False), result_types=VResultTypes('type'), syntax=VOneOf('syntax', options=g.search_syntaxes))
@api_doc(api_section.search, supports_rss=True, uses_site=True)
def GET_search(self, query, num, reverse, after, count, sort, recent, restrict_sr, include_facets, result_types, syntax, sr_detail):

    if (c.site.login_required and (not c.user_is_loggedin)):
        raise UserRequiredException
    if (request.GET.get('over18') == 'yes'):
        u = UrlParser(request.fullurl)
        del u.query_dict['over18']
        search_url = u.unparse()
        return self.intermediate_redirect('/over18', sr_path=False, fullpath=search_url)
    if (query and ('.' in query)):
        url = sanitize_url(query, require_scheme=True)
        if url:
            return self.redirect(('/submit' + query_string({'url': url})))
    if (not restrict_sr):
        site = DefaultSR()
    else:
        site = c.site
    has_query = (query or (not isinstance(site, (DefaultSR, AllSR))))
    if (not syntax):
        syntax = g.search.SearchQuery.default_syntax
    is_api_or_rss = ((c.render_style in API_TYPES) or (c.render_style in RSS_TYPES))
    if is_api_or_rss:
        include_over18 = ((not c.obey_over18) or c.over18)
    elif feature.is_enabled('safe_search'):
        include_over18 = c.over18
    else:
        include_over18 = True
    faceting = {}
    if ((syntax == 'cloudsearch') or (query and (':' in query))):
        result_types = (result_types - {'sr'})
    if ((not after) and (not restrict_sr) and (result_types == {'link', 'sr'})):
        sr_num = min(3, int((num / 3)))
        num = (num - sr_num)
    elif (result_types == {'sr'}):
        sr_num = num
        num = 0
    else:
        sr_num = 0
    content = None
    subreddits = None
    nav_menus = None
    cleanup_message = None
    converted_data = None
    subreddit_facets = None
    legacy_render_class = (feature.is_enabled('legacy_search') or c.user.pref_legacy_search)
    if ((num > 0) and has_query):
        nav_menus = [SearchSortMenu(default=sort), TimeMenu(default=recent)]
        try:
            q = g.search.SearchQuery(query, site, sort=sort, faceting=faceting, include_over18=include_over18, recent=recent, syntax=syntax)
            content = self._search(q, num=num, after=after, reverse=reverse, count=count, sr_detail=sr_detail, heading=_('posts'), nav_menus=nav_menus, legacy_render_class=legacy_render_class)
            converted_data = q.converted_data
            subreddit_facets = content.subreddit_facets
        except g.search.InvalidQuery:
            g.stats.simple_event('cloudsearch.error.invalidquery')
            cleaned = re.sub('[^\\w\\s]+', '    ', query)
            cleaned = cleaned.lower().strip()
            q = g.search.SearchQuery(cleaned, site, sort=sort, faceting=faceting, include_over18=include_over18, recent=recent)
            content = self._search(q, num=num, after=after, reverse=reverse, count=count, heading=_('posts'), nav_menus=nav_menus, legacy_render_class=legacy_render_class)
            converted_data = q.converted_data
            subreddit_facets = content.subreddit_facets
            if cleaned:
                cleanup_message = (strings.invalid_search_query % {'clean_query': cleaned})
                cleanup_message += '    '
                cleanup_message += (strings.search_help % {'search_help': self.search_help_page})
            else:
                cleanup_message = strings.completely_invalid_search_query
    if ((sr_num > 0) and has_query):
        sr_q = g.search.SubredditSearchQuery(query, sort='relevance', faceting={}, include_over18=include_over18)
        subreddits = self._search(sr_q, num=sr_num, reverse=reverse, after=after, count=count, type='sr', skip_deleted_authors=False, heading=_('subreddits'), legacy_render_class=legacy_render_class)
        if (subreddit_facets and (not subreddits.things)):
            names = [sr._fullname for (sr, count) in subreddit_facets]
            builder = IDBuilder(names, num=sr_num)
            listing = SearchListing(builder, nextprev=False)
            subreddits = listing.listing(legacy_render_class=legacy_render_class)
        if (is_api() and (not content)):
            content = subreddits
            subreddits = None
    event_target = {'target_sort': sort, 'target_filter_time': recent}
    if after:
        event_target['target_count'] = count
        if reverse:
            event_target['target_before'] = after._fullname
        else:
            event_target['target_after'] = after._fullname
    extra_js_config = {'event_target': event_target}
    res = SearchPage(_('search    results'), query, content=content, subreddits=subreddits, nav_menus=nav_menus, search_params=dict(sort=sort, t=recent), infotext=cleanup_message, simple=False, site=c.site, restrict_sr=restrict_sr, syntax=syntax, converted_data=converted_data, facets=subreddit_facets, sort=sort, recent=recent, extra_js_config=extra_js_config).render()
    return res
