<commit_before><?php

use Sunlight\Article;
use Sunlight\Post\PostService;
use Sunlight\Database\Database as DB;
use Sunlight\Extend;
use Sunlight\Gallery;
use Sunlight\GenericTemplates;
use Sunlight\Message;
use Sunlight\Paginator;
use Sunlight\Post\Post;
use Sunlight\Router;
use Sunlight\Settings;
use Sunlight\User;
use Sunlight\Util\Form;
use Sunlight\Util\Request;
use Sunlight\Util\StringManipulator;
use Sunlight\Util\UrlHelper;
use Sunlight\Xsrf;

defined('SL_ROOT') or exit;

if (!User::isLoggedIn() && Settings::get('notpublicsite')) {
    $_index->unauthorized();
    return;
}

if (!Settings::get('search')) {
    $_index->notFound();
    return;
}

if (isset($_GET['q']) && Xsrf::check(true)) {
    $search_query = trim(Request::get('q', ''));
    $page = isset($_GET['page']);
    $art = isset($_GET['art']);
    $post = isset($_GET['post']);
    $image = isset($_GET['img']);
} else {
    $search_query = '';
    $page = true;
    $art = true;
    $post = true;
    $image = true;
}

// output
$_index->title = _lang('mod.search');

$output .= '
<p class="bborder">' . _lang('mod.search.p') . '</p>

<form action="' . _e(Router::module('search')) . '" method="get" class="fullsearchform">
<p><input type="search" name="q" class="inputmedium" value="' . _e($search_query) . '"> <input type="submit" value="' . _lang('mod.search.submit') . '"></p>
<p>
    ' . _lang('mod.search.where') . ':
    <label><input type="checkbox" name="page" value="1"' . Form::activateCheckbox($page) . '> ' . _lang('mod.search.where.page') . '</label>
    <label><input type="checkbox" name="art" value="1"' . Form::activateCheckbox($art) . '> ' . _lang('mod.search.where.articles') . '</label>
    <label><input type="checkbox" name="post" value="1"' . Form::activateCheckbox($post) . '> ' . _lang('mod.search.where.posts') . '</label>
    <label><input type="checkbox" name="img" value="1"' . Form::activateCheckbox($image) . '> ' . _lang('mod.search.where.images') . '</label>
</p>

' . Xsrf::getInput() . '
</form>

';

// search
if ($search_query != '') {
    if (mb_strlen($search_query) >= 3) {
        $search_query_sql = DB::esc('%' . $search_query . '%');
        $results = []; // item format: array(link, title, perex)
        $public = !User::isLoggedIn();

        // functions
        $searchQuery = function ($alias, $cols) {
            if ($alias === null) {
                $alias = '';
            } else {
                $alias .= '.';
            }

            $output = '(';

            for ($i = 0, $last = (count($cols) - 1); isset($cols[$i]); ++$i) {
                $output .= $alias . $cols[$i] . ' LIKE \'' . $GLOBALS['search_query_sql'] . '\'';

                if ($i !== $last) {
                    $output .= ' OR ';
                }
            }

            $output .= ')';

            return $output;
        };

        // search pages
        if ($page) {
            $q = DB::query(
                'SELECT id,title,slug,perex FROM ' . DB::table('page')
                . ' WHERE level<=' . User::getLevel() . ' AND ' . ($public ? 'public=1 AND ' : '')
                . $searchQuery(null, ['title', 'slug', 'description', 'perex', 'content'])
                . ' LIMIT 50'
            );

            while ($r = DB::row($q)) {
                $results[] = [
                    Router::page($r['id'], $r['slug']),
                    $r['title'],
                    strip_tags($r['perex'])
                ];
            }

            DB::free($q);
        }

        // search articles
        if ($art) {
            [$joins, $cond] = Article::createFilter('art', [], $searchQuery('art', ['title', 'slug', 'perex', 'description', 'content']));
            $q = DB::query('SELECT art.id,art.title,art.slug,art.perex,cat1.slug AS cat_slug FROM ' . DB::table('article') . ' art ' . $joins . ' WHERE ' . $cond . 'ORDER BY time DESC LIMIT 100');

            while ($r = DB::row($q)) {
                $results[] = [
                    Router::article($r['id'], $r['slug'], $r['cat_slug']),
                    $r['title'],
                    StringManipulator::ellipsis(strip_tags($r['perex']), 255, false)
                ];
            }

            DB::free($q);
        }

        // search posts
        if ($post) {
            $types = [Post::SECTION_COMMENT, Post::ARTICLE_COMMENT, Post::BOOK_ENTRY, Post::FORUM_TOPIC, Post::PLUGIN];
            [$columns, $joins, $cond] = Post::createFilter('post', $types, [], $searchQuery('post', ['subject', 'text']));
            $userQuery = User::createQuery('post.author');
            $columns .= ',' . $userQuery['column_list'];
            $joins .= ' ' . $userQuery['joins'];

            $q = DB::query($x = 'SELECT ' . $columns . ' FROM ' . DB::table('post') . ' post ' . $joins . ' WHERE ' . $cond . ' ORDER BY id DESC LIMIT 100');

            while ($r = DB::row($q)) {
                // load title, link and page number
                $pagenum = null;
                $post_anchor = true;<commit_msg>Remove unused variable<commit_after><?php

use Sunlight\Article;
use Sunlight\Post\PostService;
use Sunlight\Database\Database as DB;
use Sunlight\Extend;
use Sunlight\Gallery;
use Sunlight\GenericTemplates;
use Sunlight\Message;
use Sunlight\Paginator;
use Sunlight\Post\Post;
use Sunlight\Router;
use Sunlight\Settings;
use Sunlight\User;
use Sunlight\Util\Form;
use Sunlight\Util\Request;
use Sunlight\Util\StringManipulator;
use Sunlight\Util\UrlHelper;
use Sunlight\Xsrf;

defined('SL_ROOT') or exit;

if (!User::isLoggedIn() && Settings::get('notpublicsite')) {
    $_index->unauthorized();
    return;
}

if (!Settings::get('search')) {
    $_index->notFound();
    return;
}

if (isset($_GET['q']) && Xsrf::check(true)) {
    $search_query = trim(Request::get('q', ''));
    $page = isset($_GET['page']);
    $art = isset($_GET['art']);
    $post = isset($_GET['post']);
    $image = isset($_GET['img']);
} else {
    $search_query = '';
    $page = true;
    $art = true;
    $post = true;
    $image = true;
}

// output
$_index->title = _lang('mod.search');

$output .= '
<p class="bborder">' . _lang('mod.search.p') . '</p>

<form action="' . _e(Router::module('search')) . '" method="get" class="fullsearchform">
<p><input type="search" name="q" class="inputmedium" value="' . _e($search_query) . '"> <input type="submit" value="' . _lang('mod.search.submit') . '"></p>
<p>
    ' . _lang('mod.search.where') . ':
    <label><input type="checkbox" name="page" value="1"' . Form::activateCheckbox($page) . '> ' . _lang('mod.search.where.page') . '</label>
    <label><input type="checkbox" name="art" value="1"' . Form::activateCheckbox($art) . '> ' . _lang('mod.search.where.articles') . '</label>
    <label><input type="checkbox" name="post" value="1"' . Form::activateCheckbox($post) . '> ' . _lang('mod.search.where.posts') . '</label>
    <label><input type="checkbox" name="img" value="1"' . Form::activateCheckbox($image) . '> ' . _lang('mod.search.where.images') . '</label>
</p>

' . Xsrf::getInput() . '
</form>

';

// search
if ($search_query != '') {
    if (mb_strlen($search_query) >= 3) {
        $search_query_sql = DB::esc('%' . $search_query . '%');
        $results = []; // item format: array(link, title, perex)
        $public = !User::isLoggedIn();

        // functions
        $searchQuery = function ($alias, $cols) {
            if ($alias === null) {
                $alias = '';
            } else {
                $alias .= '.';
            }

            $output = '(';

            for ($i = 0, $last = (count($cols) - 1); isset($cols[$i]); ++$i) {
                $output .= $alias . $cols[$i] . ' LIKE \'' . $GLOBALS['search_query_sql'] . '\'';

                if ($i !== $last) {
                    $output .= ' OR ';
                }
            }

            $output .= ')';

            return $output;
        };

        // search pages
        if ($page) {
            $q = DB::query(
                'SELECT id,title,slug,perex FROM ' . DB::table('page')
                . ' WHERE level<=' . User::getLevel() . ' AND ' . ($public ? 'public=1 AND ' : '')
                . $searchQuery(null, ['title', 'slug', 'description', 'perex', 'content'])
                . ' LIMIT 50'
            );

            while ($r = DB::row($q)) {
                $results[] = [
                    Router::page($r['id'], $r['slug']),
                    $r['title'],
                    strip_tags($r['perex'])
                ];
            }

            DB::free($q);
        }

        // search articles
        if ($art) {
            [$joins, $cond] = Article::createFilter('art', [], $searchQuery('art', ['title', 'slug', 'perex', 'description', 'content']));
            $q = DB::query('SELECT art.id,art.title,art.slug,art.perex,cat1.slug AS cat_slug FROM ' . DB::table('article') . ' art ' . $joins . ' WHERE ' . $cond . 'ORDER BY time DESC LIMIT 100');

            while ($r = DB::row($q)) {
                $results[] = [
                    Router::article($r['id'], $r['slug'], $r['cat_slug']),
                    $r['title'],
                    StringManipulator::ellipsis(strip_tags($r['perex']), 255, false)
                ];
            }

            DB::free($q);
        }

        // search posts
        if ($post) {
            $types = [Post::SECTION_COMMENT, Post::ARTICLE_COMMENT, Post::BOOK_ENTRY, Post::FORUM_TOPIC, Post::PLUGIN];
            [$columns, $joins, $cond] = Post::createFilter('post', $types, [], $searchQuery('post', ['subject', 'text']));
            $userQuery = User::createQuery('post.author');
            $columns .= ',' . $userQuery['column_list'];
            $joins .= ' ' . $userQuery['joins'];

            $q = DB::query('SELECT ' . $columns . ' FROM ' . DB::table('post') . ' post ' . $joins . ' WHERE ' . $cond . ' ORDER BY id DESC LIMIT 100');

            while ($r = DB::row($q)) {
                // load title, link and page number
                $pagenum = null;
                $post_anchor = true;
{'fraction_non_alphanumeric': 0.20982231982431623, 'fraction_numerical': 0.003992812936713915, 'mean_word_length': 2.189748487742757, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209736', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3200, 'n_words': 616}