Commit eee55603 authored by Mark Jordan's avatar Mark Jordan
Browse files

Initial commit.

parents
This diff is collapsed.
# Islandora Usage Stats Test Data Generator
<?php
/**
* @file
* Implementation of Drush hooks.
*/
/**
* Implements hook_drush_command().
*/
function islandora_usage_stats_generate_test_access_data_drush_command() {
$items = array();
$items['islandora_usage_stats_generate_test_access_data'] = array(
'aliases' => array('iusgtad'),
'description' => 'Generate test usage stats data for the Islandora Usate Stats module.',
'drupal dependencies' => array('islandora_usage_stats'),
'options' => array(
'number_hits' => array(
'description' => 'How many access hits to generate',
),
'pids' => array(
'description' => 'A comma-separated list of object PIDs to attach the usage data to.',
'required' => TRUE,
),
'base_url' => array(
'description' => 'Base URL of the Islandora instance. Defaults to http://islandora:8000.',
),
'updated_db_ids_file' => array(
'description' => 'Path to a file where IDs of the updated rows in the db are saved. Defaults to "updated_db_ids.txt".',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_usage_stats_delete_test_access_data'] = array(
'aliases' => array('iusdtad'),
'description' => "Delete test usage data based on an input file containing the database IDs of the data.",
'options' => array(
'input_file' => array(
'description' => 'The full or relative path to the input file.',
'required' => TRUE,
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
return $items;
}
/**
* Drush command function.
*/
function drush_islandora_usage_stats_generate_test_access_data() {
$number_hits = drush_get_option('number_hits', 1);
$pid_list = drush_get_option('pids');
$base_url = drush_get_option('base_url', 'http://localhost:8000');
$pids = explode(',', $pid_list);
foreach ($pids as &$pid) {
$pid = trim($pid);
if (islandora_object_load($pid)) {
$url = $base_url . '/islandora/object/' . $pid;
drush_print(dt("Hitting !url !num_hits times", array('!url' => $url, '!num_hits' => $number_hits)));
for ($i = 1; $i <= $number_hits; $i++) {
$result = drupal_http_request($url);
if ($result->code == 200) {
$update = islandora_usage_stats_generate_test_access_data_update_db($pid);
drush_print(dt("Row !id in the islandora_usage_stats_object_access_log table now uses !time and !ip",
array(
'!id' => $update['access_id'],
'!time' => $update['time'],
'!ip' => $update['ip']))
);
}
else {
drush_set_error('HTTP ERROR',
dt('HTTP request returned !code', array('!code' => $result->code)));
}
if ($number_hits > 1) {
sleep(2);
}
}
}
else {
drush_set_error('CANNOT LOAD OBJECT',
dt('Sorry, cannot load object !pid', array('!pid' => $pid)));
}
}
}
/**
* Utility function to randomly generate a random timestamp/IP address pair.
*/
function islandora_usage_stats_generate_test_access_data_get_random() {
$timestamp = mt_rand(1452384000, 1544572800);
$ip = long2ip(rand(0, '4294967295'));
return array('time' => $timestamp, 'ip' => $ip);
}
/**
* Utility function to update a db row with test data.
*/
function islandora_usage_stats_generate_test_access_data_update_db($pid) {
module_load_include('inc', 'islandora_usage_stats', 'includes/db');
$pid_id = islandora_usage_stats_pid_id($pid);
$random = islandora_usage_stats_generate_test_access_data_get_random();
$last_id = db_query('SELECT MAX(id) FROM {islandora_usage_stats_object_access_log}')->fetchField();
islandora_usage_stats_generate_test_access_data_log_db_id($last_id);
$num_updated = db_update('islandora_usage_stats_object_access_log')
->fields(array(
'time' => $random['time'],
'ip' => $random['ip'],
))
->condition('id', $last_id, '=')
->execute();
$return = array_merge($random, array('access_id' => $last_id));
return $return;
}
/**
* Returns a list of database IDs from a file.
*
* @param string $path
* The absolute path to the file.
*
* @return array
* A list of IDs.
*/
function islandora_usage_stats_generate_test_access_data_read_id_file($path) {
if (!file_exists($path)) {
drush_set_error('ID_FILE_NOT_FOUND',
dt('The specified ID file (!id_file) does not exist.',
array('!id_file' => $path)));
drupal_exit();
}
$ids = array();
$lines = file($path);
foreach ($lines as $id) {
$id = trim($id);
}
return $pids;
}
/**
* Appends a db ID to a file.
*
* @param string $id
* The ID to be appended to the file.
*/
function islandora_usage_stats_generate_test_access_data_log_db_id($id) {
$path = drush_get_option('updated_db_ids_file', 'updated_db_ids.txt');
file_put_contents($path, $id . PHP_EOL, FILE_APPEND);
}
name = Module to test ISLANDORA-2170
description = "A test module for ISLANDORA-2170."
version = 7.x-dev
core = 7.x
dependencies[] = islandora
dependencies[] = devel
<?php
/**
* @file
* Placeholder .module file.
*/
Supports Markdown
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