/** * Constructor. * @param $argv array task arguments */ function PKPUsageStatsLoader($args) { $plugin = PluginRegistry::getPlugin('generic', 'usagestatsplugin'); /* @var $plugin UsageStatsPlugin */ $this->_plugin = $plugin; if ($plugin->getSetting(CONTEXT_ID_NONE, 'compressArchives')) { $this->setCompressArchives(true); } $arg = current($args); switch ($arg) { case 'autoStage': if ($plugin->getSetting(0, 'createLogFiles')) { $this->_autoStage = true; } break; case 'externalLogFiles': $this->_externalLogFiles = true; break; } // Define the base filesystem path. $args[0] = $plugin->getFilesPath(); parent::FileLoader($args); if ($plugin->getEnabled()) { // Load the metric type constant. PluginRegistry::loadCategory('reports'); import('classes.statistics.StatisticsHelper'); $statsHelper = new StatisticsHelper(); $geoLocationTool = $statsHelper->getGeoLocationTool(); $this->_geoLocationTool = $geoLocationTool; $plugin->import('UsageStatsTemporaryRecordDAO'); $statsDao = new UsageStatsTemporaryRecordDAO(); DAORegistry::registerDAO('UsageStatsTemporaryRecordDAO', $statsDao); $this->_counterRobotsListFile = $this->_getCounterRobotListFile(); $contextDao = Application::getContextDAO(); /* @var $contextDao ContextDAO */ $contextFactory = $contextDao->getAll(); /* @var $contextFactory DAOResultFactory */ $contextsByPath = array(); while ($context = $contextFactory->next()) { /* @var $context Context */ $contextsByPath[$context->getPath()] = $context; } $this->_contextsByPath = $contextsByPath; $this->checkFolderStructure(true); } }
/** * Constructor. * @param $argv array task arguments */ function UsageStatsLoader($args) { $plugin =& PluginRegistry::getPlugin('generic', 'usagestatsplugin'); /* @var $plugin UsageStatsPlugin */ $this->_plugin =& $plugin; $arg = current($args); switch ($arg) { case 'autoStage': if ($plugin->getSetting(0, 'createLogFiles')) { $this->_autoStage = true; } break; case 'externalLogFiles': $this->_externalLogFiles = true; break; } // Define the base filesystem path. $args[0] = $plugin->getFilesPath(); parent::FileLoader($args); if ($plugin->getEnabled()) { // Load the metric type constant. PluginRegistry::loadCategory('reports'); $geoLocationTool =& StatisticsHelper::getGeoLocationTool(); $this->_geoLocationTool =& $geoLocationTool; $plugin->import('UsageStatsTemporaryRecordDAO'); $statsDao = new UsageStatsTemporaryRecordDAO(); DAORegistry::registerDAO('UsageStatsTemporaryRecordDAO', $statsDao); $this->_counterRobotsListFile = $this->_getCounterRobotListFile(); $journalDao =& DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */ $journalFactory =& $journalDao->getJournals(); /* @var $journalFactory DAOResultFactory */ $journalsByPath = array(); while ($journal =& $journalFactory->next()) { /* @var $journal Journal */ $journalsByPath[$journal->getPath()] =& $journal; } $this->_journalsByPath = $journalsByPath; $this->checkFolderStructure(true); } }
/** * Fetch regions from the passed request * variable country id. * @param $args array * @param $request Request * @return JSONMessage JSON object */ function fetchRegions($args, $request) { $this->validate(); $countryId = (string) $request->getUserVar('countryId'); import('lib.pkp.classes.core.JSONMessage'); if ($countryId) { $statsHelper = new StatisticsHelper(); $geoLocationTool = $statsHelper->getGeoLocationTool(); if ($geoLocationTool) { $regions = $geoLocationTool->getRegions($countryId); if (!empty($regions)) { $regionsData = array(); foreach ($regions as $id => $name) { $regionsData[] = array('id' => $id, 'name' => $name); } return new JSONMessage(true, $regionsData); } } } return new JSONMessage(false); }
/** * Generate statistics reports from passed * request arguments. * @param $args array * @param $request PKPRequest */ function generateReport($args, $request) { $this->setupTemplate($request); AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION); $router = $request->getRouter(); $context = $router->getContext($request); import('classes.statistics.StatisticsHelper'); $statsHelper = new StatisticsHelper(); $metricType = $request->getUserVar('metricType'); if (is_null($metricType)) { $metricType = $context->getDefaultMetricType(); } // Generates only one metric type report at a time. if (is_array($metricType)) { $metricType = current($metricType); } if (!is_scalar($metricType)) { $metricType = null; } $reportPlugin = $statsHelper->getReportPluginByMetricType($metricType); if (!$reportPlugin || is_null($metricType)) { $request->redirect(null, null, 'tools', 'statistics'); } $columns = $request->getUserVar('columns'); $filters = unserialize($request->getUserVar('filters')); if (!$filters) { $filters = $request->getUserVar('filters'); } $orderBy = $request->getUserVar('orderBy'); if ($orderBy) { $orderBy = unserialize($orderBy); if (!$orderBy) { $orderBy = $request->getUserVar('orderBy'); } } else { $orderBy = array(); } $metrics = $reportPlugin->getMetrics($metricType, $columns, $filters, $orderBy); $allColumnNames = $statsHelper->getColumnNames(); $columnOrder = array_keys($allColumnNames); $columnNames = array(); foreach ($columnOrder as $column) { if (in_array($column, $columns)) { $columnNames[$column] = $allColumnNames[$column]; } if ($column == STATISTICS_DIMENSION_ASSOC_TYPE && in_array(STATISTICS_DIMENSION_ASSOC_ID, $columns)) { $columnNames['common.title'] = __('common.title'); } } // Make sure the metric column will always be present. if (!in_array(STATISTICS_METRIC, $columnNames)) { $columnNames[STATISTICS_METRIC] = $allColumnNames[STATISTICS_METRIC]; } header('content-type: text/comma-separated-values'); header('content-disposition: attachment; filename=statistics-' . date('Ymd') . '.csv'); $fp = fopen('php://output', 'wt'); fputcsv($fp, array($reportPlugin->getDisplayName())); fputcsv($fp, array($reportPlugin->getDescription())); fputcsv($fp, array(__('common.metric') . ': ' . $metricType)); fputcsv($fp, array(__('manager.statistics.reports.reportUrl') . ': ' . $request->getCompleteUrl())); fputcsv($fp, array('')); // Just for better displaying. $columnNames = array_merge(array(''), $columnNames); fputcsv($fp, $columnNames); foreach ($metrics as $record) { $row = array(); foreach ($columnNames as $key => $name) { if (empty($name)) { // Column just for better displaying. $row[] = ''; continue; } // Give a chance for subclasses to set the row values. if ($returner = $this->getReportRowValue($key, $record)) { $row[] = $returner; continue; } switch ($key) { case 'common.title': $assocId = $record[STATISTICS_DIMENSION_ASSOC_ID]; $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE]; $row[] = $this->getObjectTitle($assocId, $assocType); break; case STATISTICS_DIMENSION_ASSOC_TYPE: $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE]; $row[] = $statsHelper->getObjectTypeString($assocType); break; case STATISTICS_DIMENSION_CONTEXT_ID: $assocId = $record[STATISTICS_DIMENSION_CONTEXT_ID]; $assocType = Application::getContextAssocType(); $row[] = $this->getObjectTitle($assocId, $assocType); break; case STATISTICS_DIMENSION_SUBMISSION_ID: if (isset($record[STATISTICS_DIMENSION_SUBMISSION_ID])) { $assocId = $record[STATISTICS_DIMENSION_SUBMISSION_ID]; $assocType = ASSOC_TYPE_SUBMISSION; $row[] = $this->getObjectTitle($assocId, $assocType); } else { $row[] = ''; } break; case STATISTICS_DIMENSION_REGION: if (isset($record[STATISTICS_DIMENSION_REGION]) && isset($record[STATISTICS_DIMENSION_COUNTRY])) { $geoLocationTool = $statsHelper->getGeoLocationTool(); if ($geoLocationTool) { $regions = $geoLocationTool->getRegions($record[STATISTICS_DIMENSION_COUNTRY]); $regionId = $record[STATISTICS_DIMENSION_REGION]; if (strlen($regionId) == 1) { $regionId = '0' . $regionId; } if (isset($regions[$regionId])) { $row[] = $regions[$regionId]; break; } } } $row[] = ''; break; case STATISTICS_DIMENSION_PKP_SECTION_ID: $sectionId = null; if (isset($record[STATISTICS_DIMENSION_PKP_SECTION_ID])) { $sectionId = $record[STATISTICS_DIMENSION_PKP_SECTION_ID]; } if ($sectionId) { $row[] = $this->getObjectTitle($sectionId, ASSOC_TYPE_SECTION); } else { $row[] = ''; } break; case STATISTICS_DIMENSION_FILE_TYPE: if ($record[$key]) { $row[] = $statsHelper->getFileTypeString($record[$key]); } else { $row[] = ''; } break; default: $row[] = $record[$key]; break; } } fputcsv($fp, $row); } fclose($fp); }
/** * @copydoc Form::fetch() */ function fetch($request) { $router = $request->getRouter(); $context = $router->getContext($request); $columns = $this->_columns; $statsHelper = new StatisticsHelper(); $availableMetricTypeStrings = $statsHelper->getAllMetricTypeStrings(); if (count($availableMetricTypeStrings) > 1) { $this->setData('metricTypeOptions', $availableMetricTypeStrings); } $reportTemplateOptions = array(); $reportTemplates = $this->_defaultReportTemplates; foreach ($reportTemplates as $reportTemplate) { $reportTemplateOptions[] = __($reportTemplate['nameLocaleKey']); } if (!empty($reportTemplateOptions)) { $this->setData('reportTemplateOptions', $reportTemplateOptions); } $reportTemplateIndex = (int) $this->_reportTemplateIndex; if (!is_null($reportTemplateIndex) && isset($reportTemplates[$reportTemplateIndex])) { $reportTemplate = $reportTemplates[$reportTemplateIndex]; $reportColumns = $reportTemplate['columns']; if (!is_array($reportColumns)) { continue; } $this->setData('columns', $reportColumns); $this->setData('reportTemplate', $reportTemplateIndex); if (isset($reportTemplate['aggregationColumns'])) { $aggreationColumns = $reportTemplate['aggregationColumns']; if (!is_array($aggreationColumns)) { continue; } $aggreationOptions = $selectedAggregationOptions = array(); foreach ($aggreationColumns as $column) { $columnName = $statsHelper->getColumnNames($column); if (!$columnName) { continue; } $aggreationOptions[$column] = $columnName; } $this->setData('aggregationOptions', $aggreationOptions); $this->setData('selectedAggregationOptions', array_intersect($aggreationColumns, $reportColumns)); } if (isset($reportTemplate['filter']) && is_array($reportTemplate['filter'])) { foreach ($reportTemplate['filter'] as $dimension => $filter) { switch ($dimension) { case STATISTICS_DIMENSION_ASSOC_TYPE: $this->setData('objectTypes', $filter); break; } } } } $timeFilterSelectedOption = $request->getUserVar('timeFilterOption'); if (is_null($timeFilterSelectedOption)) { $timeFilterSelectedOption = TIME_FILTER_OPTION_CURRENT_MONTH; } switch ($timeFilterSelectedOption) { case TIME_FILTER_OPTION_YESTERDAY: $this->setData('yesterday', true); break; case TIME_FILTER_OPTION_CURRENT_MONTH: default: $this->setData('currentMonth', true); break; case TIME_FILTER_OPTION_RANGE_DAY: $this->setData('byDay', true); break; case TIME_FILTER_OPTION_RANGE_MONTH: $this->setData('byMonth', true); break; } $startTime = $request->getUserDateVar('dateStart'); $endTime = $request->getUserDateVar('dateEnd'); if (!$startTime) { $startTime = time(); } if (!$endTime) { $endTime = time(); } $this->setData('dateStart', $startTime); $this->setData('dateEnd', $endTime); if (isset($columns[STATISTICS_DIMENSION_COUNTRY])) { $geoLocationTool = $statsHelper->getGeoLocationTool(); if ($geoLocationTool) { $countryCodes = $geoLocationTool->getAllCountryCodes(); if (!$countryCodes) { $countryCodes = array(); } $countryCodes = array_combine($countryCodes, $countryCodes); $this->setData('countriesOptions', $countryCodes); } $this->setData('showRegionInput', isset($columns[STATISTICS_DIMENSION_REGION])); $this->setData('showCityInput', isset($columns[STATISTICS_DIMENSION_CITY])); } $this->setData('showMonthInputs', isset($columns[STATISTICS_DIMENSION_MONTH])); $this->setData('showDayInputs', isset($columns[STATISTICS_DIMENSION_DAY])); $orderColumns = $this->_columns; $nonOrderableColumns = array(STATISTICS_DIMENSION_ASSOC_TYPE, STATISTICS_DIMENSION_SUBMISSION_ID, STATISTICS_DIMENSION_CONTEXT_ID, STATISTICS_DIMENSION_REGION, STATISTICS_DIMENSION_FILE_TYPE, STATISTICS_DIMENSION_METRIC_TYPE); foreach ($nonOrderableColumns as $column) { unset($orderColumns[$column]); } $this->setData('metricType', $this->_metricType); $this->setData('objectTypesOptions', $this->_objects); if ($this->_fileTypes) { $this->setData('fileTypesOptions', $this->_fileTypes); } $this->setData('fileAssocTypes', $this->getFileAssocTypes()); $this->setData('orderColumnsOptions', $orderColumns); $this->setData('orderDirectionsOptions', array(STATISTICS_ORDER_ASC => __('manager.statistics.reports.orderDir.asc'), STATISTICS_ORDER_DESC => __('manager.statistics.reports.orderDir.desc'))); $columnsOptions = $this->_columns; // Reports will always include this column. unset($columnsOptions[STATISTICS_METRIC]); $this->setData('columnsOptions', $columnsOptions); $this->setData('optionalColumns', $this->_optionalColumns); return parent::fetch($request); }
/** * Constructor. * @param $argv array task arguments */ function PKPUsageStatsLoader($args) { $plugin = PluginRegistry::getPlugin('generic', 'usagestatsplugin'); /* @var $plugin UsageStatsPlugin */ $this->_plugin = $plugin; $arg = current($args); switch ($arg) { case 'autoStage': if ($plugin->getSetting(0, 'createLogFiles')) { $this->_autoStage = true; } break; case 'externalLogFiles': $this->_externalLogFiles = true; break; } // Define the base filesystem path. $args[0] = $plugin->getFilesPath(); parent::FileLoader($args); if ($plugin->getEnabled()) { // Load the metric type constant. PluginRegistry::loadCategory('reports'); import('classes.statistics.StatisticsHelper'); $statsHelper = new StatisticsHelper(); $geoLocationTool = $statsHelper->getGeoLocationTool(); $this->_geoLocationTool = $geoLocationTool; $plugin->import('UsageStatsTemporaryRecordDAO'); $statsDao = new UsageStatsTemporaryRecordDAO(); DAORegistry::registerDAO('UsageStatsTemporaryRecordDAO', $statsDao); $this->_counterRobotsListFile = $this->_getCounterRobotListFile(); $contextDao = Application::getContextDAO(); /* @var $contextDao ContextDAO */ $contextFactory = $contextDao->getAll(); /* @var $contextFactory DAOResultFactory */ $contextsByPath = array(); while ($context = $contextFactory->next()) { /* @var $context Context */ $contextsByPath[$context->getPath()] = $context; } $this->_contextsByPath = $contextsByPath; $this->checkFolderStructure(true); if ($this->_autoStage) { // Copy all log files to stage directory, except the current day one. $fileMgr = new FileManager(); $logFiles = array(); $logsDirFiles = glob($plugin->getUsageEventLogsPath() . DIRECTORY_SEPARATOR . '*'); // It's possible that the processing directory have files that // were being processed but the php process was stopped before // finishing the processing. Just copy them to the stage directory too. $processingDirFiles = glob($this->getProcessingPath() . DIRECTORY_SEPARATOR . '*'); if (is_array($logsDirFiles)) { $logFiles = array_merge($logFiles, $logsDirFiles); } if (is_array($processingDirFiles)) { $logFiles = array_merge($logFiles, $processingDirFiles); } foreach ($logFiles as $filePath) { // Make sure it's a file. if ($fileMgr->fileExists($filePath)) { // Avoid current day file. $filename = pathinfo($filePath, PATHINFO_BASENAME); $currentDayFilename = $plugin->getUsageEventCurrentDayLogName(); if ($filename == $currentDayFilename) { continue; } $this->moveFile(pathinfo($filePath, PATHINFO_DIRNAME), $this->getStagePath(), $filename); } } } } }
/** * Fetch regions from the passed request * variable country id. * @param $args array * @param $request Request * @return string JSON response */ function fetchRegions(&$args, &$request) { $this->validate(); $countryId = (string) $request->getUserVar('countryId'); import('lib.pkp.classes.core.JSONMessage'); $json = new JSONMessage(false); if ($countryId) { $geoLocationTool =& StatisticsHelper::getGeoLocationTool(); if ($geoLocationTool) { $regions = $geoLocationTool->getRegions($countryId); if (!empty($regions)) { $regionsData = array(); foreach ($regions as $id => $name) { $regionsData[] = array('id' => $id, 'name' => $name); } $json->setStatus(true); $json->setContent($regionsData); } } } return $json->getString(); }