Open Journal Systems  3.3.0
copyAccessLogFileTool.php
1 <?php
2 
17 require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');
18 
19 // Bring in the file loader folder constants.
20 import('lib.pkp.classes.task.FileLoader');
21 
23 
25 
26  var $_tmpDir;
27 
29 
31 
33 
38  function __construct($argv = array()) {
39  parent::__construct($argv);
40 
41  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_ADMIN);
42 
43  if (count($this->argv) < 1 || count($this->argv) > 2) {
44  $this->usage();
45  exit(1);
46  }
47 
48  $plugin = PluginRegistry::getPlugin('generic', 'usagestatsplugin'); /* @var $plugin UsageStatsPlugin */
49 
50  $this->_usageStatsDir = $plugin->getFilesPath();
51  $this->_tmpDir = $this->_usageStatsDir . DIRECTORY_SEPARATOR . 'tmp';
52 
53  // This tool needs egrep path configured.
54  $this->_egrepPath = escapeshellarg(Config::getVar('cli', 'egrep'));
55  if ($this->_egrepPath == "''") {
56  printf(__('admin.error.executingUtil', array('utilPath' => $this->_egrepPath, 'utilVar' => 'egrep')) . "\n");
57  exit(1);
58  }
59 
60  // Get a list of files currently inside the usage stats dir.
61  $fileLoaderDirs = array(FILE_LOADER_PATH_STAGING, FILE_LOADER_PATH_PROCESSING,
62  FILE_LOADER_PATH_ARCHIVE, FILE_LOADER_PATH_REJECT);
63 
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') {
72  // Always save the filename without compression extension.
73  $fileBasename = substr($fileBasename, 0, -3);
74  }
75  $usageStatsFiles[] = $fileBasename;
76  }
77  }
78  }
79 
80  $this->_usageStatsFiles = $usageStatsFiles;
81 
82  // Get a list of context paths.
83  $contextDao =& Application::getContextDAO(); /* @var $contextDao ContextDAO */
84  $contextFactory = $contextDao->getAll();
85  $contextPaths = array();
86  while ($context =& $contextFactory->next()) {
87  /* @var $context Context */
88  $contextPaths[] = escapeshellarg($context->getPath());
89  }
90  $contextPaths = implode('/|/', $contextPaths);
91  $this->_contextPaths = $contextPaths;
92  }
93 
97  function usage() {
98  echo "\n" . __('admin.copyAccessLogFileTool.usage', array('scriptName' => $this->scriptName)) . "\n\n";
99  }
100 
106  function execute() {
107  $fileMgr = new FileManager();
108  $filesDir = Config::getVar('files', 'files_dir');
109  $filePath = current($this->argv);
110  $usageStatsDir = $this->_usageStatsDir;
111  $tmpDir = $this->_tmpDir;
112 
113  if ($fileMgr->fileExists($tmpDir, 'dir')) {
114  $fileMgr->rmtree($tmpDir);
115  }
116 
117  if (!$fileMgr->mkdir($tmpDir)) {
118  printf(__('admin.copyAccessLogFileTool.error.creatingFolder', array('tmpDir' => $tmpDir)) . "\n");
119  exit(1);
120  }
121 
122  if ($fileMgr->fileExists($filePath, 'dir')) {
123  // Directory.
124  $filesToCopy = glob($filePath . DIRECTORY_SEPARATOR . '*');
125  foreach ($filesToCopy as $file) {
126  // If a base filename is given as a parameter, check it.
127  if (count($this->argv) == 2) {
128  $baseFilename = $this->argv[1];
129  if (strpos(pathinfo($file, PATHINFO_BASENAME), $baseFilename) !== 0) {
130  continue;
131  }
132  }
133 
134  $this->_copyFile($file);
135  }
136  } else {
137  if ($fileMgr->fileExists($filePath)) {
138  // File.
139  $this->_copyFile($filePath);
140  } else {
141  // Can't access.
142  printf(__('admin.copyAccessLogFileTool.error.acessingFile', array('filePath' => $filePath)) . "\n");
143  }
144  }
145 
146  $fileMgr->rmtree($tmpDir);
147  }
148 
149 
150  //
151  // Private helper methods.
152  //
158  function _copyFile($filePath) {
159  $usageStatsFiles = $this->_usageStatsFiles;
160  $usageStatsDir = $this->_usageStatsDir;
161  $tmpDir = $this->_tmpDir;
162  $fileName = pathinfo($filePath, PATHINFO_BASENAME);
163  $fileMgr = new FileManager();
164 
165  $isCompressed = false;
166  $uncompressedFileName = $fileName;
167  if (pathinfo($filePath, PATHINFO_EXTENSION) == 'gz') {
168  $isCompressed = true;
169  $uncompressedFileName = substr($fileName, 0, -3);
170  }
171 
172  if (in_array($uncompressedFileName, $usageStatsFiles)) {
173  printf(__('admin.copyAccessLogFileTool.warning.fileAlreadyExists', array('filePath' => $filePath)) . "\n");
174  return;
175  }
176 
177  $tmpFilePath = $tmpDir . DIRECTORY_SEPARATOR . $fileName;
178 
179  // Copy the file to a temporary directory.
180  if (!$fileMgr->copyFile($filePath, $tmpFilePath)) {
181  printf(__('admin.copyAccessLogFileTool.error.copyingFile', array('filePath' => $filePath, 'tmpFilePath' => $tmpFilePath)) . "\n");
182  exit(1);
183  }
184 
185  // Uncompress it, if needed.
186  if ($isCompressed) {
187  $fileMgr = new FileManager();
188  try {
189  $tmpFilePath = $fileMgr->decompressFile($tmpFilePath);
190  } catch (Exception $e) {
191  printf($e->getMessage() . "\n");
192  exit(1);
193  }
194  }
195 
196  // Filter only entries that contains context paths.
197  $egrepPath = $this->_egrepPath;
198  $destinationPath = $usageStatsDir . DIRECTORY_SEPARATOR .
199  FILE_LOADER_PATH_STAGING . DIRECTORY_SEPARATOR .
200  pathinfo($tmpFilePath, PATHINFO_BASENAME);
201  // Each context path is already escaped, see the constructor.
202  $output = null;
203  $returnValue = 0;
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");
207  exit(1);
208  }
209  if (!$fileMgr->deleteByPath($tmpFilePath)) {
210  printf(__('admin.copyAccessLogFileTool.error.deletingFile', array('tmpFilePath' => $tmpFilePath)) . "\n");
211  exit(1);
212  }
213 
214  printf(__('admin.copyAccessLogFileTool.success', array('filePath' => $filePath, 'destinationPath' => $destinationPath)) . "\n");
215  }
216 }
217 
218 $tool = new CopyAccessLogFileTool(isset($argv) ? $argv : array());
219 $tool->execute();
220 
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
CopyAccessLogFileTool
CLI tool to copy apache log files while filtering entries related only to the current instalation.
Definition: copyAccessLogFileTool.php:22
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
CopyAccessLogFileTool\execute
execute()
Definition: copyAccessLogFileTool.php:106
CopyAccessLogFileTool\$_tmpDir
$_tmpDir
Definition: copyAccessLogFileTool.php:26
CopyAccessLogFileTool\_copyFile
_copyFile($filePath)
Definition: copyAccessLogFileTool.php:158
CommandLineTool
Initialization code for command-line scripts.
Definition: CliTool.inc.php:44
$tool
$tool
Definition: mergeCoverageReportTool.php:120
CopyAccessLogFileTool\$_usageStatsDir
$_usageStatsDir
Definition: copyAccessLogFileTool.php:24
CopyAccessLogFileTool\__construct
__construct($argv=array())
Definition: copyAccessLogFileTool.php:38
CopyAccessLogFileTool\$_contextPaths
$_contextPaths
Definition: copyAccessLogFileTool.php:30
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
CopyAccessLogFileTool\$_usageStatsFiles
$_usageStatsFiles
Definition: copyAccessLogFileTool.php:28
CopyAccessLogFileTool\$_egrepPath
$_egrepPath
Definition: copyAccessLogFileTool.php:32
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
FileManager
Class defining basic operations for file management.
Definition: FileManager.inc.php:35
CommandLineTool\$argv
$argv
Definition: CliTool.inc.php:53
CopyAccessLogFileTool\usage
usage()
Definition: copyAccessLogFileTool.php:97