Stats.php 9.45 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

namespace Q2A\Controllers\Admin;

use Q2A\Auth\NoPermissionException;
use Q2A\Middleware\Auth\MinimumUserLevel;

/**
 * Controller for admin page showing usage statistics and clean-up buttons
 */
class Stats extends \Q2A\Controllers\BaseController
{
	public function __construct()
	{
		require_once QA_INCLUDE_DIR . 'db/recalc.php';
		require_once QA_INCLUDE_DIR . 'app/admin.php';
		require_once QA_INCLUDE_DIR . 'db/admin.php';
		require_once QA_INCLUDE_DIR . 'app/format.php';

		parent::__construct();

		$this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_ADMIN));
	}

	public function index()
	{
		// Get the information to display

		$qcount = (int)qa_opt('cache_qcount');
		$qcount_anon = qa_db_count_posts('Q', false);
		$qcount_unans = (int)qa_opt('cache_unaqcount');

		$acount = (int)qa_opt('cache_acount');
		$acount_anon = qa_db_count_posts('A', false);

		$ccount = (int)qa_opt('cache_ccount');
		$ccount_anon = qa_db_count_posts('C', false);


		// Prepare content for theme

		$qa_content = qa_content_prepare();

		$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/stats_title');

		$qa_content['error'] = qa_admin_page_error();

		$qa_content['form'] = array(
			'style' => 'wide',

			'fields' => array(
				'q2a_version' => array(
					'label' => qa_lang_html('admin/q2a_version'),
					'value' => qa_html(QA_VERSION),
				),

				'q2a_date' => array(
					'label' => qa_lang_html('admin/q2a_build_date'),
					'value' => qa_html(QA_BUILD_DATE),
				),

				'q2a_latest' => array(
					'label' => qa_lang_html('admin/q2a_latest_version'),
					'type' => 'custom',
					'html' => '<span id="q2a-version">...</span>',
				),

				'break0' => array(
					'type' => 'blank',
				),

				'db_version' => array(
					'label' => qa_lang_html('admin/q2a_db_version'),
					'value' => qa_html(qa_opt('db_version')),
				),

				'db_size' => array(
					'label' => qa_lang_html('admin/q2a_db_size'),
					'value' => qa_html(qa_format_number(qa_db_table_size() / 1048576, 1) . ' MB'),
				),

				'break1' => array(
					'type' => 'blank',
				),

				'php_version' => array(
					'label' => qa_lang_html('admin/php_version'),
					'value' => qa_html(phpversion()),
				),

				'mysql_version' => array(
					'label' => qa_lang_html('admin/mysql_version'),
					'value' => qa_html(qa_db_mysql_version()),
				),

				'break2' => array(
					'type' => 'blank',
				),

				'qcount' => array(
					'label' => qa_lang_html('admin/total_qs'),
					'value' => qa_html(qa_format_number($qcount)),
				),

				'qcount_unans' => array(
					'label' => qa_lang_html('admin/total_qs_unans'),
					'value' => qa_html(qa_format_number($qcount_unans)),
				),

				'qcount_users' => array(
					'label' => qa_lang_html('admin/from_users'),
					'value' => qa_html(qa_format_number($qcount - $qcount_anon)),
				),

				'qcount_anon' => array(
					'label' => qa_lang_html('admin/from_anon'),
					'value' => qa_html(qa_format_number($qcount_anon)),
				),

				'break3' => array(
					'type' => 'blank',
				),

				'acount' => array(
					'label' => qa_lang_html('admin/total_as'),
					'value' => qa_html(qa_format_number($acount)),
				),

				'acount_users' => array(
					'label' => qa_lang_html('admin/from_users'),
					'value' => qa_html(qa_format_number($acount - $acount_anon)),
				),

				'acount_anon' => array(
					'label' => qa_lang_html('admin/from_anon'),
					'value' => qa_html(qa_format_number($acount_anon)),
				),

				'break4' => array(
					'type' => 'blank',
				),

				'ccount' => array(
					'label' => qa_lang_html('admin/total_cs'),
					'value' => qa_html(qa_format_number($ccount)),
				),

				'ccount_users' => array(
					'label' => qa_lang_html('admin/from_users'),
					'value' => qa_html(qa_format_number($ccount - $ccount_anon)),
				),

				'ccount_anon' => array(
					'label' => qa_lang_html('admin/from_anon'),
					'value' => qa_html(qa_format_number($ccount_anon)),
				),

				'break5' => array(
					'type' => 'blank',
				),

				'users' => array(
					'label' => qa_lang_html('admin/users_registered'),
					'value' => QA_FINAL_EXTERNAL_USERS ? '' : qa_html(qa_format_number(qa_db_count_users())),
				),

				'users_active' => array(
					'label' => qa_lang_html('admin/users_active'),
					'value' => qa_html(qa_format_number((int)qa_opt('cache_userpointscount'))),
				),

				'users_posted' => array(
					'label' => qa_lang_html('admin/users_posted'),
					'value' => qa_html(qa_format_number(qa_db_count_active_users('posts'))),
				),

				'users_voted' => array(
					'label' => qa_lang_html('admin/users_voted'),
					'value' => qa_html(qa_format_number(qa_db_count_active_users('uservotes'))),
				),
			),
		);

Scott committed
200
		if (QA_FINAL_EXTERNAL_USERS) {
Scott committed
201
			unset($qa_content['form']['fields']['users']);
Scott committed
202
		} else {
Scott committed
203
			unset($qa_content['form']['fields']['users_active']);
Scott committed
204
		}
Scott committed
205 206

		foreach ($qa_content['form']['fields'] as $index => $field) {
Scott committed
207
			if (empty($field['type'])) {
Scott committed
208
				$qa_content['form']['fields'][$index]['type'] = 'static';
Scott committed
209
			}
Scott committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		}

		$qa_content['form_2'] = array(
			'tags' => 'method="post" action="' . qa_path_html('admin/recalc') . '"',

			'title' => qa_lang_html('admin/database_cleanup'),

			'style' => 'basic',

			'buttons' => array(
				'recount_posts' => array(
					'label' => qa_lang_html('admin/recount_posts'),
					'tags' => 'name="dorecountposts" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/recount_posts_stop')) . ', \'recount_posts_note\');"',
					'note' => '<span id="recount_posts_note">' . qa_lang_html('admin/recount_posts_note') . '</span>',
				),

				'reindex_content' => array(
					'label' => qa_lang_html('admin/reindex_content'),
					'tags' => 'name="doreindexcontent" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/reindex_content_stop')) . ', \'reindex_content_note\');"',
					'note' => '<span id="reindex_content_note">' . qa_lang_html('admin/reindex_content_note') . '</span>',
				),

				'recalc_points' => array(
					'label' => qa_lang_html('admin/recalc_points'),
					'tags' => 'name="dorecalcpoints" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/recalc_stop')) . ', \'recalc_points_note\');"',
					'note' => '<span id="recalc_points_note">' . qa_lang_html('admin/recalc_points_note') . '</span>',
				),

				'refill_events' => array(
					'label' => qa_lang_html('admin/refill_events'),
					'tags' => 'name="dorefillevents" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/recalc_stop')) . ', \'refill_events_note\');"',
					'note' => '<span id="refill_events_note">' . qa_lang_html('admin/refill_events_note') . '</span>',
				),

				'recalc_categories' => array(
					'label' => qa_lang_html('admin/recalc_categories'),
					'tags' => 'name="dorecalccategories" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/recalc_stop')) . ', \'recalc_categories_note\');"',
					'note' => '<span id="recalc_categories_note">' . qa_lang_html('admin/recalc_categories_note') . '</span>',
				),

				'delete_hidden' => array(
					'label' => qa_lang_html('admin/delete_hidden'),
					'tags' => 'name="dodeletehidden" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/delete_stop')) . ', \'delete_hidden_note\');"',
					'note' => '<span id="delete_hidden_note">' . qa_lang_html('admin/delete_hidden_note') . '</span>',
				),
			),

			'hidden' => array(
				'code' => qa_get_form_security_code('admin/recalc'),
			),
		);

Scott committed
262
		if (!qa_using_categories()) {
Scott committed
263
			unset($qa_content['form_2']['buttons']['recalc_categories']);
Scott committed
264
		}
Scott committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

		if (defined('QA_BLOBS_DIRECTORY')) {
			if (qa_db_has_blobs_in_db()) {
				$qa_content['form_2']['buttons']['blobs_to_disk'] = array(
					'label' => qa_lang_html('admin/blobs_to_disk'),
					'tags' => 'name="doblobstodisk" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/blobs_stop')) . ', \'blobs_to_disk_note\');"',
					'note' => '<span id="blobs_to_disk_note">' . qa_lang_html('admin/blobs_to_disk_note') . '</span>',
				);
			}

			if (qa_db_has_blobs_on_disk()) {
				$qa_content['form_2']['buttons']['blobs_to_db'] = array(
					'label' => qa_lang_html('admin/blobs_to_db'),
					'tags' => 'name="doblobstodb" onclick="return qa_recalc_click(this.name, this, ' . qa_js(qa_lang_html('admin/blobs_stop')) . ', \'blobs_to_db_note\');"',
					'note' => '<span id="blobs_to_db_note">' . qa_lang_html('admin/blobs_to_db_note') . '</span>',
				);
			}
		}


		$qa_content['script_rel'][] = 'qa-content/qa-admin.js?' . QA_VERSION;
		$qa_content['script_var']['qa_warning_recalc'] = qa_lang('admin/stop_recalc_warning');

		$qa_content['script_onloads'][] = array(
			"qa_version_check('https://raw.githubusercontent.com/q2a/question2answer/master/VERSION.txt', " . qa_js(qa_html(QA_VERSION), true) . ", 'q2a-version', true);"
		);

		$qa_content['navigation']['sub'] = qa_admin_sub_navigation();


		return $qa_content;
	}
}