<commit_before><?php
/*
 *	Made by Samerton
 *  https://github.com/NamelessMC/Nameless/
 *  NamelessMC version 2.0.0-pr5
 *
 *  License: MIT
 *
 *  Statistics Widget // By Xemah // https://xemah.me
 */
 
class StatsWidget extends WidgetBase {
	private $_cache, $_smarty, $_language;

    public function __construct($pages = array(), $smarty, $language, $cache){
    	$this->_cache = $cache;
    	$this->_smarty = $smarty;
    	$this->_language = $language;

        parent::__construct($pages);

        // Get order
        $order = DB::getInstance()->query('SELECT `order` FROM nl2_widgets WHERE `name` = ?', array('Statistics'))->first();

        // Set widget variables
        $this->_module = 'Core';
        $this->_name = 'Statistics';
        $this->_location = 'right';
        $this->_description = 'Displays the basic statistics of your website.';
        $this->_order = $order->order;
    }

    public function initialise(){
        $queries = new Queries();
        $user = new User();
        
        $this->_cache->setCache('statistics');
        
    	if($this->_cache->isCached('statistics')){
    	    
    		$users_query = $this->_cache->retrieve('statistics');
    		$users_registered = $users_query['users_registered'];
    		$latest_member = $users_query['latest_member'];
    		
    	} else {

    		$users_query = $queries->orderAll('users', 'joined', 'DESC');
    		$users_registered = count($users_query);
    		$latest_member = array(
    			'style' => $user->getGroupClass($users_query[0]->id),
    			'profile' => URL::build('/profile/' . Output::getClean($users_query[0]->username)),
    			'avatar' => $user->getAvatar($users_query[0]->id),
    			'username' => Output::getClean($users_query[0]->username),
    			'nickname' => Output::getClean($users_query[0]->nickname),
    			'id' => Output::getClean($users_query[0]->id)
    		);
    		
        	$users_query = null;
    
            $this->_cache->store('statistics', array(
    			'users_registered' => $users_registered,
    			'latest_member' => $latest_member
    		), 120);
    		
    	};

	    if(!$this->_cache->isCached('online_users')){
		    $online_users = DB::getInstance()->query('SELECT count(*) FROM nl2_users WHERE last_online > ?', array(strtotime('-5 minutes')))->first();
		    $online_users = $online_users->{'count(*)'};
		    $this->_cache->store('online_users', $online_users, 60);
	    } else {
		    $online_users = $this->_cache->retrieve('online_users');
	    }

	    if(!$this->_cache->isCached('online_guests')){
		    $online_guests = DB::getInstance()->query('SELECT count(*) FROM nl2_online_guests WHERE last_seen > ?', array(strtotime('-5 minutes')))->first();
		    $online_guests = $online_guests->{'count(*)'};
		    $this->_cache->store('online_guests', $online_guests, 60);
	    } else {
		    $online_guests = $this->_cache->retrieve('online_guests');
	    }
    	
        $forum_module = $queries->getWhere('modules', array('name', '=', 'Forum'));
        $forum_module = $forum_module[0];
        
    	if ($forum_module->enabled) {
		    $this->_cache->setCache('forum_stats');
		    if(!$this->_cache->isCached('total_topics')){
			    $total_topics = DB::getInstance()->query('SELECT count(*) FROM nl2_topics WHERE deleted = 0')->first();
			    $total_topics = $total_topics->{'count(*)'};
			    $this->_cache->store('total_topics', $total_topics, 60);
		    } else {
			    $total_topics = $this->_cache->retrieve('total_topics');
		    }

		    if(!$this->_cache->isCached('total_posts')){
			    $total_posts = DB::getInstance()->query('SELECT count(*) FROM nl2_posts WHERE deleted = 0')->first();
			    $total_posts = $total_posts->{'count(*)'};
			    $this->_cache->store('total_posts', $total_posts, 60);
		    } else {
			    $total_posts = $this->_cache->retrieve('total_posts');
		    }
        	
        	$this->_smarty->assign(array(
        	    'FORUM_STATISTICS' => $this->_language['forum_stats'],
                'TOTAL_THREADS' =>  $this->_language['total_threads'],
                'TOTAL_THREADS_VALUE' => $total_topics,
                'TOTAL_POSTS' =>  $this->_language['total_posts'],<commit_msg>Add check to prevent online guests query before upgrade script is run<commit_after><?php
/*
 *	Made by Samerton
 *  https://github.com/NamelessMC/Nameless/
 *  NamelessMC version 2.0.0-pr5
 *
 *  License: MIT
 *
 *  Statistics Widget // By Xemah // https://xemah.me
 */
 
class StatsWidget extends WidgetBase {
	private $_cache, $_smarty, $_language;

    public function __construct($pages = array(), $smarty, $language, $cache){
    	$this->_cache = $cache;
    	$this->_smarty = $smarty;
    	$this->_language = $language;

        parent::__construct($pages);

        // Get order
        $order = DB::getInstance()->query('SELECT `order` FROM nl2_widgets WHERE `name` = ?', array('Statistics'))->first();

        // Set widget variables
        $this->_module = 'Core';
        $this->_name = 'Statistics';
        $this->_location = 'right';
        $this->_description = 'Displays the basic statistics of your website.';
        $this->_order = $order->order;
    }

    public function initialise(){
        $queries = new Queries();
        $user = new User();
        
        $this->_cache->setCache('statistics');
        
    	if($this->_cache->isCached('statistics')){
    	    
    		$users_query = $this->_cache->retrieve('statistics');
    		$users_registered = $users_query['users_registered'];
    		$latest_member = $users_query['latest_member'];
    		
    	} else {

    		$users_query = $queries->orderAll('users', 'joined', 'DESC');
    		$users_registered = count($users_query);
    		$latest_member = array(
    			'style' => $user->getGroupClass($users_query[0]->id),
    			'profile' => URL::build('/profile/' . Output::getClean($users_query[0]->username)),
    			'avatar' => $user->getAvatar($users_query[0]->id),
    			'username' => Output::getClean($users_query[0]->username),
    			'nickname' => Output::getClean($users_query[0]->nickname),
    			'id' => Output::getClean($users_query[0]->id)
    		);
    		
        	$users_query = null;
    
            $this->_cache->store('statistics', array(
    			'users_registered' => $users_registered,
    			'latest_member' => $latest_member
    		), 120);
    		
    	};

	    if(!$this->_cache->isCached('online_users')){
		    $online_users = DB::getInstance()->query('SELECT count(*) FROM nl2_users WHERE last_online > ?', array(strtotime('-5 minutes')))->first();
		    $online_users = $online_users->{'count(*)'};
		    $this->_cache->store('online_users', $online_users, 60);
	    } else {
		    $online_users = $this->_cache->retrieve('online_users');
	    }

	    if(!$this->_cache->isCached('online_guests')){
	    	try {
			    $online_guests = DB::getInstance()->query('SELECT count(*) FROM nl2_online_guests WHERE last_seen > ?', array(strtotime('-5 minutes')))->first();
			    $online_guests = $online_guests->{'count(*)'};
			    $this->_cache->store('online_guests', $online_guests, 60);
		    } catch(Exception $e){
	    		// Upgrade script hasn't been run
			    $online_guests = 0;
		    }
	    } else {
		    $online_guests = $this->_cache->retrieve('online_guests');
	    }
    	
        $forum_module = $queries->getWhere('modules', array('name', '=', 'Forum'));
        $forum_module = $forum_module[0];
        
    	if ($forum_module->enabled) {
		    $this->_cache->setCache('forum_stats');
		    if(!$this->_cache->isCached('total_topics')){
			    $total_topics = DB::getInstance()->query('SELECT count(*) FROM nl2_topics WHERE deleted = 0')->first();
			    $total_topics = $total_topics->{'count(*)'};
			    $this->_cache->store('total_topics', $total_topics, 60);
		    } else {
			    $total_topics = $this->_cache->retrieve('total_topics');
		    }

		    if(!$this->_cache->isCached('total_posts')){
			    $total_posts = DB::getInstance()->query('SELECT count(*) FROM nl2_posts WHERE deleted = 0')->first();
			    $total_posts = $total_posts->{'count(*)'};
			    $this->_cache->store('total_posts', $total_posts, 60);
		    } else {
			    $total_posts = $this->_cache->retrieve('total_posts');
		    }
        	
        	$this->_smarty->assign(array(
        	    'FORUM_STATISTICS' => $this->_language['forum_stats'],
                'TOTAL_THREADS' =>  $this->_language['total_threads'],
                'TOTAL_THREADS_VALUE' => $total_topics,
                'TOTAL_POSTS' =>  $this->_language['total_posts'],
{'fraction_non_alphanumeric': 0.18424770426183187, 'fraction_numerical': 0.007417000235460325, 'mean_word_length': 3.0184484389782402, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 177, 'https://': 4, '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': '2120848', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2740, 'n_words': 424}