<?php /*

 Composr
 Copyright (c) ocProducts, 2004-2016

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  ocProducts Ltd
 * @package    nusearch
 */

/**
 * Hook class.
 */
class Hook_cron_composr_fulltext_indexer
{
    /**
     * Run function for CRON hooks. Searches for tasks to perform.
     */
    public function run()
    {
        $time = time();
        $_last_cron_time = get_value('last_composr_fulltext_indexer_time', null, true);
        $last_cron_time = ($_last_cron_time === null) ? null : intval($_last_cron_time);

        if (!$GLOBALS['SITE_DB']->table_exists('ft_index_commonality')) {
            $GLOBALS['SITE_DB']->change_primary_key('db_meta_indices', array('i_table', 'i_name'));
            $GLOBALS['SITE_DB']->alter_table_field('db_meta_indices', 'i_fields', 'LONG_TEXT');

            //$GLOBALS['SITE_DB']->drop_table_if_exists('ft_index_commonality');

            $GLOBALS['SITE_DB']->create_table('ft_index_commonality', array(
                'id' => '*AUTO',
                'c_ngram' => 'SHORT_TEXT',
                'c_commonality' => 'REAL',
            ));
        }

        if ($last_cron_time === null) {
            disable_php_memory_limit();
        }

        $GLOBALS['NO_QUERY_LIMIT'] = true; // TODO: Change in v11

        $GLOBALS['NO_DB_SCOPE_CHECK'] = true; // For small performance gain

        $total_singular_ngram_tokens = 0;
        $statistics_map = array();

        require_code('search');

        $hooks = find_all_hooks('modules', 'search'); // TODO: Change in v11
        foreach (array_keys($hooks) as $hook) {
            $subhook = get_param_string('limit_subhook', null);
            if (($subhook !== null) && ($subhook !== $hook)) {
                continue;
            }

            require_code('hooks/modules/search/' . $hook);
            $ob = object_factory('Hook_search_' . $hook);

            if (method_exists($ob, 'index_for_search')) {
                $info = $ob->info(false);
                if ($info !== null) {
                    $ob->index_for_search($last_cron_time, $total_singular_ngram_tokens, $statistics_map);
                }
            }
        }

        // Log all ngram commonality to the database, used as part of the search ranking algorithm, and useful for manually gauging which stop words to add
        if (!empty($statistics_map)) { // If was a full reindex of all hooks handled in this run (usually all, but may be limited by limit_subhook URL parameter)
            $GLOBALS['SITE_DB']->query_delete('ft_index_commonality');
            foreach ($statistics_map as $lang => $_statistics_map) {
                arsort($_statistics_map);

                foreach ($_statistics_map as $ngram => $total) {
                    if (strlen($ngram) > 255) {
                        continue;
                    }

                    if (trim($ngram, '0123456789') == '') {
                        continue;
                    }

                    $GLOBALS['SITE_DB']->query_insert('ft_index_commonality', array(
                        'c_ngram' => $ngram,
                        'c_commonality' => floatval($total) / floatval($total_singular_ngram_tokens),
                    ));
                }
            }
        }

        set_value('last_composr_fulltext_indexer_time', strval($time), true);
    }
}
