Skip to content

Commit

Permalink
*8015* Avoid logging unfinished file downloads requests
Browse files Browse the repository at this point in the history
  • Loading branch information
beghelli committed Jun 3, 2013
1 parent 3e00cd1 commit 0d2ce05
Showing 1 changed file with 70 additions and 38 deletions.
108 changes: 70 additions & 38 deletions plugins/generic/usageStats/UsageStatsPlugin.inc.php
Expand Up @@ -17,6 +17,10 @@

class UsageStatsPlugin extends GenericPlugin {

/** @var $_currentUsageEvent array */
var $_currentUsageEvent;


//
// Implement methods from PKPPlugin.
//
Expand Down Expand Up @@ -182,7 +186,69 @@ function callbackLoadCategory($hookName, $args) {
* @return boolean
*/
function logUsageEvent($hookName, $args) {
$hookName = $args[0];
$usageEvent = $args[1];

if ($hookName == 'FileManager::downloadFileFinished' && !$usageEvent && $this->_currentUsageEvent) {
// File download is finished, try to log the current usage event.
$downloadSuccess = $args[2];
if ($downloadSuccess && !connection_aborted()) {
$this->_currentUsageEvent['downloadSuccess'] = true;
$usageEvent = $this->_currentUsageEvent;
}
}

if ($usageEvent && !$usageEvent['downloadSuccess']) {
// Don't log until we get the download finished hook call.
$this->_currentUsageEvent = $usageEvent;
return false;
}

if ($usageEvent) {
$this->_writeUsageEventInLogFile($usageEvent);
}

return false;
}

/**
* @see PKPPageRouter::route()
* @todo Remove this callback for OJS 3.0. The issue current
* operation should redirect to the view operation in core.
*/
function callbackLoadHandler($hookName, $args) {
// Check the page.
$page = $args[0];
if ($page !== 'issue') return;

// Check the operation.
$op = $args[1];
if ($op !== 'current') return;

// Check current issue.
$request =& Application::getRequest();
$journal = $request->getJournal();
$issueDao = DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getCurrent($journal->getId(), true);
if (!$issue) {
// Let the default current operation work.
return false;
}

// Replace the default Issue handler by ours.
define('HANDLER_CLASS', 'UsageStatsHandler');
$handlerFile =& $args[2];
$handlerFile = $this->getPluginPath() . '/' . 'UsageStatsHandler.inc.php';
}


//
// Private helper methods.
//
/**
* @param $usageEvent array
*/
private function _writeUsageEventInLogFile($usageEvent) {
$desiredParams = array($usageEvent['ip']);

if (isset($usageEvent['classification'])) {
Expand All @@ -198,8 +264,8 @@ function logUsageEvent($hookName, $args) {
}

$desiredParams = array_merge($desiredParams,
array('"' . $usageEvent['time'] . '"', $usageEvent['canonicalUrl'],
'"' . $usageEvent['userAgent'] . '"'));
array('"' . $usageEvent['time'] . '"', $usageEvent['canonicalUrl'],
'"' . $usageEvent['userAgent'] . '"'));

$usageLogEntry = implode(' ', $desiredParams) . PHP_EOL;

Expand All @@ -211,8 +277,8 @@ function logUsageEvent($hookName, $args) {

// Check the plugin file directory.
$usageEventFilesPath = realpath($fileMgr->getBasePath()) .
DIRECTORY_SEPARATOR . 'usageStats' .
DIRECTORY_SEPARATOR . 'usageEventLogs';
DIRECTORY_SEPARATOR . 'usageStats' .
DIRECTORY_SEPARATOR . 'usageEventLogs';
if (!$fileMgr->fileExists($usageEventFilesPath, 'dir')) {
$success = $fileMgr->mkdirtree($usageEventFilesPath);
if (!$success) {
Expand All @@ -232,41 +298,7 @@ function logUsageEvent($hookName, $args) {
assert(false);
}
fclose($fp);

return false;
}


/**
* @see PKPPageRouter::route()
* @todo Remove this callback for OJS 3.0. The issue current
* operation should redirect to the view operation in core.
*/
function callbackLoadHandler($hookName, $args) {
// Check the page.
$page = $args[0];
if ($page !== 'issue') return;

// Check the operation.
$op = $args[1];
if ($op !== 'current') return;

// Check current issue.
$request =& Application::getRequest();
$journal = $request->getJournal();
$issueDao = DAORegistry::getDAO('IssueDAO');
$issue = $issueDao->getCurrent($journal->getId(), true);
if (!$issue) {
// Let the default current operation work.
return false;
}

// Replace the default Issue handler by ours.
define('HANDLER_CLASS', 'UsageStatsHandler');
$handlerFile =& $args[2];
$handlerFile = $this->getPluginPath() . '/' . 'UsageStatsHandler.inc.php';
}

}

?>

0 comments on commit 0d2ce05

Please sign in to comment.