Commit 9530c067 authored by cividesk's avatar cividesk

Adding StackExchange statistics

parent ae801670
......@@ -713,6 +713,46 @@ CREATE TABLE IF NOT EXISTS `sourceforge_download` (
`value` int(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `stackexchange_history`
--
DROP TABLE IF EXISTS `stackexchange_history`;
CREATE TABLE IF NOT EXISTS `stackexchange_history` (
`ts_created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`total_users` INT(10) NOT NULL,
`total_badges` INT(10) NOT NULL,
`total_questions` INT(10) NOT NULL,
`total_answers` INT(10) NOT NULL,
`total_unanswered` INT(10) NOT NULL,
`total_accepted` INT(10) NOT NULL,
`total_votes` INT(10) NOT NULL,
`total_comments` INT(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `stackexchange_users`
--
DROP TABLE IF EXISTS `stackexchange_users`;
CREATE TABLE IF NOT EXISTS `stackexchange_users` (
`account_id` INT(10) NOT NULL,
`display_name` VARCHAR(64) NOT NULL,
`user_type` VARCHAR(32) NOT NULL,
`location` VARCHAR(255) NULL,
`reputation` INT(10) NOT NULL,
`accept_rate` INT(10) NULL,
`badges_gold` INT(10) NOT NULL,
`badges_silver` INT(10) NOT NULL,
`badges_bronze` INT(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
require_once('stackapi.php');
$queries[] = array(
'file' => 'stackexchange-info.json',
'query' => "
SELECT " . implode(', ', $se_fields['site']) ."
FROM stackexchange_history
ORDER BY date DESC
LIMIT 1
",
);
$queries[] = array(
'file' => 'stackexchange-history.json',
'query' => "
SELECT LEFT(ts_created, 7) AS month, MAX(". implode('), MAX(', $se_fields['site']) . ")
FROM stackexchange_history
ORDER BY month ASC
",
);
$queries[] = array(
'file' => 'stackexchange-top-users-by-reputation.json',
'query' => "
SELECT display_name, COALESCE(location, ''), reputation
FROM stackexchange_users
ORDER BY reputation DESC
LIMIT 100
",
);
$queries[] = array(
'file' => 'stackexchange-top-users-by-accept-rate.json',
'query' => "
SELECT display_name, COALESCE(location, ''), accept_rate
FROM stackexchange_users
ORDER BY accept_rate DESC
LIMIT 100
",
);
$queries[] = array(
'file' => 'stackexchange-top-users-by-badges.json',
'query' => "
SELECT display_name, COALESCE(location, ''), badges_gold, badges_silver, badges_bronze
FROM stackexchange_users
ORDER BY badges_gold DESC, badges_silver DESC, badges_bronze DESC
LIMIT 100
",
);
\ No newline at end of file
<?php
require_once(dirname(__DIR__) . '/config.php');
require_once('stackapi.php');
// Initialize database
$dbh = new PDO('mysql:dbname='.DBNAME.';host='.DBHOST, DBUSER, DBPASS);
// Get civicrm SE site statistics
$url = "https://api.stackexchange.com/2.2/info?site=civicrm";
$fields = $se_fields['site'];
$result = json_decode(stackapi($url));
if ($result->items) {
$values = array();
foreach ($fields as $field) {
$values[$field] = $result->items[0]->$field;
}
$dbh->query("
INSERT INTO stackexchange_history (" . implode(',', $fields) . ")
VALUES (" . implode(',', $values) . ")
");
}
// Get civicrm SE site users
$dbh->query("TRUNCATE stackexchange_users");
$url = "https://api.stackexchange.com/2.2/users?site=civicrm&pagesize=100";
$fields = $se_fields['users'];
$page = 1; $count = 1;
do {
$result = json_decode(stackapi($url . "&page=$page"));
$lines = array();
foreach ($result->items as $item) {
$line = array();
foreach ($fields as $field) {
if (substr($field, 0, 6) == 'badges') {
$badge = substr($field, 7);
$line[$field] = $item->badge_counts->$badge;
} elseif (isset($item->$field)) {
$line[$field] = is_numeric($item->$field) ? $item->$field : $dbh->quote($item->$field);
} else
$line[$field] = 'NULL'; // SE remove fields that are NULL from API output
}
$lines[] = '(' . implode(',', $line) . ')';
$count++;
}
$dbh->query("
INSERT INTO stackexchange_users (" . implode(',', $fields) . ")
VALUES " . implode(',', $lines)
);
echo '.';
$page++;
} while($result->has_more);
echo " $count users created or updated" . PHP_EOL;
\ No newline at end of file
<?php
// StackExchange compresses all API output regardless of http headers!
// see. https://api.stackexchange.com/docs/compression
// So we need a special function to deal with this ...
function stackapi($url) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StackApi');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl_handle);
if($data === FALSE)
throw new \Exception('cURL ERROR "' . curl_error($curl_handle) . '"');
return $data;
}
$se_fields = array(
'site' => array('total_users', 'total_badges', 'total_questions', 'total_answers', 'total_unanswered', 'total_accepted', 'total_votes', 'total_comments'),
'users' => array('account_id', 'display_name', 'user_type', 'location', 'reputation', 'accept_rate', 'badges_gold', 'badges_silver', 'badges_bronze'),
);
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment