Beispiel #1
0
    function index($id, $mode)
    {
        global $db, $user, $auth, $template, $cache;
        global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
        if (isset($_REQUEST['action']) && is_array($_REQUEST['action'])) {
            $action = request_var('action', array('' => false));
            $action = key($action);
        } else {
            $action = request_var('action', '');
        }
        $this->state = explode(',', $config['search_indexing_state']);
        if (isset($_POST['cancel'])) {
            $action = '';
            $this->state = array();
            $this->save_state();
        }
        if ($action) {
            switch ($action) {
                case 'progress_bar':
                    $type = request_var('type', '');
                    $this->display_progress_bar($type);
                    break;
                case 'delete':
                    $this->state[1] = 'delete';
                    break;
                case 'create':
                    $this->state[1] = 'create';
                    break;
                default:
                    trigger_error('NO_ACTION', E_USER_ERROR);
                    break;
            }
            if (empty($this->state[0])) {
                $this->state[0] = request_var('search_type', '');
            }
            $this->search = null;
            $error = false;
            if ($this->init_search($this->state[0], $this->search, $error)) {
                trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
            }
            $name = ucfirst(strtolower(str_replace('_', ' ', $this->state[0])));
            $action =& $this->state[1];
            $this->max_post_id = $this->get_max_post_id();
            $post_counter = isset($this->state[2]) ? $this->state[2] : 0;
            $this->state[2] =& $post_counter;
            $this->save_state();
            switch ($action) {
                case 'delete':
                    if (method_exists($this->search, 'delete_index')) {
                        // pass a reference to myself so the $search object can make use of save_state() and attributes
                        if ($error = $this->search->delete_index($this, append_sid("{$phpbb_admin_path}index.{$phpEx}", "i={$id}&mode={$mode}&action=delete", false))) {
                            $this->state = array('');
                            $this->save_state();
                            trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
                        }
                    } else {
                        $starttime = explode(' ', microtime());
                        $starttime = $starttime[1] + $starttime[0];
                        $row_count = 0;
                        while (still_on_time() && $post_counter <= $this->max_post_id) {
                            $sql = 'SELECT post_id, poster_id, forum_id
								FROM ' . POSTS_TABLE . '
								WHERE post_id >= ' . (int) ($post_counter + 1) . '
									AND post_id <= ' . (int) ($post_counter + $this->batch_size);
                            $result = $db->sql_query($sql);
                            $ids = $posters = $forum_ids = array();
                            while ($row = $db->sql_fetchrow($result)) {
                                $ids[] = $row['post_id'];
                                $posters[] = $row['poster_id'];
                                $forum_ids[] = $row['forum_id'];
                            }
                            $db->sql_freeresult($result);
                            $row_count += sizeof($ids);
                            if (sizeof($ids)) {
                                $this->search->index_remove($ids, $posters, $forum_ids);
                            }
                            $post_counter += $this->batch_size;
                        }
                        // save the current state
                        $this->save_state();
                        if ($post_counter <= $this->max_post_id) {
                            $mtime = explode(' ', microtime());
                            $totaltime = $mtime[0] + $mtime[1] - $starttime;
                            $rows_per_second = $row_count / $totaltime;
                            meta_refresh(1, append_sid($this->u_action . '&amp;action=delete&amp;skip_rows=' . $post_counter));
                            trigger_error(sprintf($user->lang['SEARCH_INDEX_DELETE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
                        }
                    }
                    $this->search->tidy();
                    $this->state = array('');
                    $this->save_state();
                    add_log('admin', 'LOG_SEARCH_INDEX_REMOVED', $name);
                    trigger_error($user->lang['SEARCH_INDEX_REMOVED'] . adm_back_link($this->u_action) . $this->close_popup_js());
                    break;
                case 'create':
                    if (method_exists($this->search, 'create_index')) {
                        // pass a reference to acp_search so the $search object can make use of save_state() and attributes
                        if ($error = $this->search->create_index($this, append_sid("{$phpbb_admin_path}index.{$phpEx}", "i={$id}&mode={$mode}&action=create", false))) {
                            $this->state = array('');
                            $this->save_state();
                            trigger_error($error . adm_back_link($this->u_action) . $this->close_popup_js(), E_USER_WARNING);
                        }
                    } else {
                        $sql = 'SELECT forum_id, enable_indexing
							FROM ' . FORUMS_TABLE;
                        $result = $db->sql_query($sql, 3600);
                        while ($row = $db->sql_fetchrow($result)) {
                            $forums[$row['forum_id']] = (bool) $row['enable_indexing'];
                        }
                        $db->sql_freeresult($result);
                        $starttime = explode(' ', microtime());
                        $starttime = $starttime[1] + $starttime[0];
                        $row_count = 0;
                        while (still_on_time() && $post_counter <= $this->max_post_id) {
                            $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id
								FROM ' . POSTS_TABLE . '
								WHERE post_id >= ' . (int) ($post_counter + 1) . '
									AND post_id <= ' . (int) ($post_counter + $this->batch_size);
                            $result = $db->sql_query($sql);
                            $buffer = $db->sql_buffer_nested_transactions();
                            if ($buffer) {
                                $rows = $db->sql_fetchrowset($result);
                                $rows[] = false;
                                // indicate end of array for while loop below
                                $db->sql_freeresult($result);
                            }
                            $i = 0;
                            while ($row = $buffer ? $rows[$i++] : $db->sql_fetchrow($result)) {
                                // Indexing enabled for this forum or global announcement?
                                // Global announcements get indexed by default.
                                if (!$row['forum_id'] || isset($forums[$row['forum_id']]) && $forums[$row['forum_id']]) {
                                    $this->search->index('post', $row['post_id'], $row['post_text'], $row['post_subject'], $row['poster_id'], $row['forum_id']);
                                }
                                $row_count++;
                            }
                            if (!$buffer) {
                                $db->sql_freeresult($result);
                            }
                            $post_counter += $this->batch_size;
                        }
                        // save the current state
                        $this->save_state();
                        // pretend the number of posts was as big as the number of ids we indexed so far
                        // just an estimation as it includes deleted posts
                        $num_posts = $config['num_posts'];
                        $config['num_posts'] = min($config['num_posts'], $post_counter);
                        $this->search->tidy();
                        $config['num_posts'] = $num_posts;
                        if ($post_counter <= $this->max_post_id) {
                            $mtime = explode(' ', microtime());
                            $totaltime = $mtime[0] + $mtime[1] - $starttime;
                            $rows_per_second = $row_count / $totaltime;
                            meta_refresh(1, append_sid($this->u_action . '&amp;action=create&amp;skip_rows=' . $post_counter));
                            trigger_error(sprintf($user->lang['SEARCH_INDEX_CREATE_REDIRECT'], $post_counter, $row_count, $rows_per_second));
                        }
                    }
                    $this->search->tidy();
                    $this->state = array('');
                    $this->save_state();
                    add_log('admin', 'LOG_SEARCH_INDEX_CREATED', $name);
                    trigger_error($user->lang['SEARCH_INDEX_CREATED'] . adm_back_link($this->u_action) . $this->close_popup_js());
                    break;
            }
        }
        $search_types = $this->get_search_types();
        $search = null;
        $error = false;
        $search_options = '';
        foreach ($search_types as $type) {
            if ($this->init_search($type, $search, $error) || !method_exists($search, 'index_created')) {
                continue;
            }
            $name = ucfirst(strtolower(str_replace('_', ' ', $type)));
            $data = array();
            if (method_exists($search, 'index_stats')) {
                $data = $search->index_stats();
            }
            $statistics = array();
            foreach ($data as $statistic => $value) {
                $n = sizeof($statistics);
                if ($n && sizeof($statistics[$n - 1]) < 3) {
                    $statistics[$n - 1] += array('statistic_2' => $statistic, 'value_2' => $value);
                } else {
                    $statistics[] = array('statistic_1' => $statistic, 'value_1' => $value);
                }
            }
            $template->assign_block_vars('backend', array('L_NAME' => $name, 'NAME' => $type, 'S_ACTIVE' => $type == $config['search_type'] ? true : false, 'S_HIDDEN_FIELDS' => build_hidden_fields(array('search_type' => $type)), 'S_INDEXED' => (bool) $search->index_created(), 'S_STATS' => (bool) sizeof($statistics)));
            foreach ($statistics as $statistic) {
                $template->assign_block_vars('backend.data', array('STATISTIC_1' => $statistic['statistic_1'], 'VALUE_1' => $statistic['value_1'], 'STATISTIC_2' => isset($statistic['statistic_2']) ? $statistic['statistic_2'] : '', 'VALUE_2' => isset($statistic['value_2']) ? $statistic['value_2'] : ''));
            }
        }
        unset($search);
        unset($error);
        unset($statistics);
        unset($data);
        $this->tpl_name = 'acp_search';
        $this->page_title = 'ACP_SEARCH_INDEX';
        $template->assign_vars(array('S_INDEX' => true, 'U_ACTION' => $this->u_action, 'U_PROGRESS_BAR' => append_sid("{$phpbb_admin_path}index.{$phpEx}", "i={$id}&amp;mode={$mode}&amp;action=progress_bar"), 'UA_PROGRESS_BAR' => addslashes(append_sid("{$phpbb_admin_path}index.{$phpEx}", "i={$id}&amp;mode={$mode}&amp;action=progress_bar"))));
        if (isset($this->state[1])) {
            $template->assign_vars(array('S_CONTINUE_INDEXING' => $this->state[1], 'U_CONTINUE_INDEXING' => $this->u_action . '&amp;action=' . $this->state[1], 'L_CONTINUE' => $this->state[1] == 'create' ? $user->lang['CONTINUE_INDEXING'] : $user->lang['CONTINUE_DELETING_INDEX'], 'L_CONTINUE_EXPLAIN' => $this->state[1] == 'create' ? $user->lang['CONTINUE_INDEXING_EXPLAIN'] : $user->lang['CONTINUE_DELETING_INDEX_EXPLAIN']));
        }
    }
Beispiel #2
0
	/**
	* Sync function being executed at the middle, some functions need to be executed after a successful sync.
	*/
	function sync_forums($sync_batch)
	{
		global $template, $user, $db, $phpbb_root_path, $phpEx, $config, $cache;
		global $convert;

		$template->assign_block_vars('checks', array(
			'S_LEGEND'	=> true,
			'LEGEND'	=> $user->lang['SYNC_TOPICS'],
		));

		$batch_size = $convert->batch_size;

		$sql = 'SELECT MIN(topic_id) as min_value, MAX(topic_id) AS max_value
			FROM ' . TOPICS_TABLE;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		// Set values of minimum/maximum primary value for this table.
		$primary_min = $row['min_value'];
		$primary_max = $row['max_value'];

		if ($sync_batch == 0)
		{
			$sync_batch = (int) $primary_min;
		}

		if ($sync_batch == 0)
		{
			$sync_batch = 1;
		}

		// Fetch a batch of rows, process and insert them.
		while ($sync_batch <= $primary_max && still_on_time())
		{
			$end = ($sync_batch + $batch_size - 1);

			// Sync all topics in batch mode...
			sync('topic_approved', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, false);
			sync('topic', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, true);

			$template->assign_block_vars('checks', array(
				'TITLE'		=> sprintf($user->lang['SYNC_TOPIC_ID'], $sync_batch, ($sync_batch + $batch_size)) . ((defined('DEBUG_EXTRA') && function_exists('memory_get_usage')) ? ' [' . ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] . ']' : ''),
				'RESULT'	=> $user->lang['DONE'],
			));

			$sync_batch += $batch_size;
		}

		if ($sync_batch >= $primary_max)
		{
			$url = $this->save_convert_progress('&amp;final_jump=1');

			$template->assign_vars(array(
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
				'U_ACTION'		=> $url,
			));

			$this->meta_refresh($url);
			return;
		}
		else
		{
			$sync_batch--;
		}

		$url = $this->save_convert_progress('&amp;sync_batch=' . $sync_batch);

		$template->assign_vars(array(
			'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
			'U_ACTION'		=> $url,
		));

		$this->meta_refresh($url);
		return;
	}
    /**
     * Runs this cron task.
     *
     * @return null
     */
    public function run()
    {
        global $phpbb_root_path, $phpbb_container, $user;
        $sql = 'SELECT log FROM ' . $this->config_table;
        $result = $this->db->sql_query($sql);
        $sconfig['log'] = $this->db->sql_fetchfield('log');
        $unique_aray = $module_aray = $browser_aray = $os_aray = $country_aray = $user_aray = $screen_aray = $referer_aray = $search_aray = array();
        $sql = 'SELECT MIN(time) AS start_time FROM ' . $this->online_table;
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $start_time = $row && $row['start_time'] ? $row['start_time'] : 0;
        if ($start_time) {
            $sql = 'SELECT time, uname, agent, ip_addr, module, host, domain, scr_res, page, referer, se_terms FROM ' . $this->online_table . ' 
					WHERE time BETWEEN ' . $start_time . ' AND ' . strtotime("24:00", $start_time) . ' ORDER BY id ASC';
            $result = $this->db->sql_query($sql);
            $starttime = explode(' ', microtime());
            $starttime = $starttime[1] + $starttime[0];
            $row_count = 0;
            include $phpbb_root_path . 'ext/forumhulp/statistics/vendor/find_os.' . $this->php_ext;
            while (still_on_time() && ($row = $this->db->sql_fetchrow($result))) {
                $module_aray = $row['module'] != '' ? $this->count_array($module_aray, $row['module']) : null;
                $os = new \find_os();
                $os->setUserAgent($row['agent']);
                $browser_aray = $row['agent'] != '' ? $this->count_array($browser_aray, $os->getBrowser() . ' ' . $os->getVersion()) : null;
                $os_aray = $row['agent'] != '' ? $this->count_array($os_aray, $os->getPlatform()) : null;
                $country_aray = $row['domain'] != '' ? $this->count_array($country_aray, $row['domain']) : null;
                $user_aray = $row['uname'] != '' ? $this->count_array($user_aray, $row['uname']) : null;
                $screen_aray = $row['scr_res'] != '' ? $this->count_array($screen_aray, $row['scr_res']) : null;
                $referer_aray = $row['referer'] != '' ? $this->count_array($referer_aray, $this->url_to_domain($row['referer'])) : null;
                $search_aray = $row['se_terms'] != '' ? $this->split_array($search_aray, $row['se_terms']) : null;
                $unique_aray = $row['ip_addr'] != '' ? $this->count_array($unique_aray, $row['ip_addr']) : null;
                $row_count++;
            }
            $this->db->sql_freeresult($result);
            $unique_visiors = array($start_time => count($unique_aray));
            $this->store($module_aray, 1);
            $this->store($browser_aray, 2);
            $this->store($os_aray, 3);
            $this->store($country_aray, 4);
            $this->store($user_aray, 5);
            $this->store($screen_aray, 6);
            $this->store($referer_aray, 7);
            $this->store($search_aray, 8);
            $this->store_unique_visitors($unique_visiors, 9);
            unset($module_aray, $browser_aray, $os_aray, $country_aray, $user_aray, $screen_aray, $referer_aray, $search_aray, $unique_visiors, $unique_aray);
            $sql = 'DELETE FROM ' . $this->online_table . ' WHERE time BETWEEN ' . $start_time . ' AND ' . strtotime("24:00", $start_time);
            $this->db->sql_query($sql);
            $sql = 'OPTIMIZE TABLE ' . $this->online_table;
            $this->db->sql_query($sql);
            $sql_ary = array('year' => date('Y', $start_time), 'month' => date('n', $start_time), 'day' => date('j', $start_time), 'hits' => $row_count);
            $sql = 'INSERT INTO ' . $this->stats_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary) . ' ON DUPLICATE KEY UPDATE hits = hits + ' . $sql_ary['hits'];
            $this->db->sql_query($sql);
            $mtime = explode(' ', microtime());
            $totaltime = $mtime[0] + $mtime[1] - $starttime;
            $rows_per_second = $row_count / $totaltime;
            $sconfig['log'] ? add_log('admin', 'LOG_STATISTICS_PRUNED', $totaltime, $rows_per_second) : null;
        } else {
            $sconfig['log'] ? add_log('admin', 'LOG_STATISTICS_NO_PRUNE') : null;
        }
        $sql = 'SELECT MIN(time) AS start_time FROM ' . $this->online_table;
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        if ($row) {
            $newtime = mktime(0, 0, 0) - $row['start_time'] >= 0 ? time() + 3600 : mktime(0, 0, 0);
        } else {
            $newtime = mktime(0, 0, 0);
        }
        $this->config->set('delete_statistics_last_gc', $newtime);
    }