17 require(dirname(dirname(dirname(dirname(__FILE__)))) .
'/tools/bootstrap.inc.php');
20 import(
'lib.pkp.classes.task.FileLoader');
39 parent::__construct(
$argv);
43 if (count($this->argv) < 1 || count($this->argv) > 2) {
50 $this->_usageStatsDir = $plugin->getFilesPath();
51 $this->_tmpDir = $this->_usageStatsDir . DIRECTORY_SEPARATOR .
'tmp';
54 $this->_egrepPath = escapeshellarg(
Config::getVar(
'cli',
'egrep'));
55 if ($this->_egrepPath ==
"''") {
56 printf(__(
'admin.error.executingUtil', array(
'utilPath' => $this->_egrepPath,
'utilVar' =>
'egrep')) .
"\n");
61 $fileLoaderDirs = array(FILE_LOADER_PATH_STAGING, FILE_LOADER_PATH_PROCESSING,
62 FILE_LOADER_PATH_ARCHIVE, FILE_LOADER_PATH_REJECT);
64 $usageStatsFiles = array();
65 foreach ($fileLoaderDirs as $dir) {
66 $dirFiles = glob($this->_usageStatsDir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR .
'*');
67 if (is_array($dirFiles) && count($dirFiles) > 0) {
68 foreach ($dirFiles as $file) {
69 if (!is_file($file))
continue;
70 $fileBasename = pathinfo($file, PATHINFO_BASENAME);
71 if (pathinfo($file, PATHINFO_EXTENSION) ==
'gz') {
73 $fileBasename = substr($fileBasename, 0, -3);
75 $usageStatsFiles[] = $fileBasename;
80 $this->_usageStatsFiles = $usageStatsFiles;
84 $contextFactory = $contextDao->getAll();
85 $contextPaths = array();
86 while ($context =& $contextFactory->next()) {
88 $contextPaths[] = escapeshellarg($context->getPath());
90 $contextPaths = implode(
'/|/', $contextPaths);
91 $this->_contextPaths = $contextPaths;
98 echo
"\n" . __(
'admin.copyAccessLogFileTool.usage', array(
'scriptName' => $this->scriptName)) .
"\n\n";
109 $filePath = current($this->argv);
113 if ($fileMgr->fileExists($tmpDir,
'dir')) {
114 $fileMgr->rmtree($tmpDir);
117 if (!$fileMgr->mkdir($tmpDir)) {
118 printf(__(
'admin.copyAccessLogFileTool.error.creatingFolder', array(
'tmpDir' => $tmpDir)) .
"\n");
122 if ($fileMgr->fileExists($filePath,
'dir')) {
124 $filesToCopy = glob($filePath . DIRECTORY_SEPARATOR .
'*');
125 foreach ($filesToCopy as $file) {
127 if (count($this->argv) == 2) {
128 $baseFilename = $this->argv[1];
129 if (strpos(pathinfo($file, PATHINFO_BASENAME), $baseFilename) !== 0) {
137 if ($fileMgr->fileExists($filePath)) {
142 printf(__(
'admin.copyAccessLogFileTool.error.acessingFile', array(
'filePath' => $filePath)) .
"\n");
146 $fileMgr->rmtree($tmpDir);
162 $fileName = pathinfo($filePath, PATHINFO_BASENAME);
165 $isCompressed =
false;
166 $uncompressedFileName = $fileName;
167 if (pathinfo($filePath, PATHINFO_EXTENSION) ==
'gz') {
168 $isCompressed =
true;
169 $uncompressedFileName = substr($fileName, 0, -3);
172 if (in_array($uncompressedFileName, $usageStatsFiles)) {
173 printf(__(
'admin.copyAccessLogFileTool.warning.fileAlreadyExists', array(
'filePath' => $filePath)) .
"\n");
177 $tmpFilePath = $tmpDir . DIRECTORY_SEPARATOR . $fileName;
180 if (!$fileMgr->copyFile($filePath, $tmpFilePath)) {
181 printf(__(
'admin.copyAccessLogFileTool.error.copyingFile', array(
'filePath' => $filePath,
'tmpFilePath' => $tmpFilePath)) .
"\n");
189 $tmpFilePath = $fileMgr->decompressFile($tmpFilePath);
190 }
catch (Exception $e) {
191 printf($e->getMessage() .
"\n");
198 $destinationPath = $usageStatsDir . DIRECTORY_SEPARATOR .
199 FILE_LOADER_PATH_STAGING . DIRECTORY_SEPARATOR .
200 pathinfo($tmpFilePath, PATHINFO_BASENAME);
204 exec($egrepPath .
" -i '" . $this->_contextPaths .
"' " . escapeshellarg($tmpFilePath) .
" > " . escapeshellarg($destinationPath), $output, $returnValue);
205 if ($returnValue > 1) {
206 printf(__(
'admin.error.executingUtil', array(
'utilPath' => $egrepPath,
'utilVar' =>
'egrep')) .
"\n");
209 if (!$fileMgr->deleteByPath($tmpFilePath)) {
210 printf(__(
'admin.copyAccessLogFileTool.error.deletingFile', array(
'tmpFilePath' => $tmpFilePath)) .
"\n");
214 printf(__(
'admin.copyAccessLogFileTool.success', array(
'filePath' => $filePath,
'destinationPath' => $destinationPath)) .
"\n");