/** * Provides the object used to log information to the file system logfile * * @param string $plugin the plugin * @param string $entity the entity type * @param boolean $manual Set to true if a manual run * @param integer $starttime the time used in the filename * @return object the fslogger */ function get_fslogger($plugin, $entity = '', $manual = false, $starttime = 0) { global $CFG; require_once $CFG->dirroot . '/local/datahub/lib/rlip_fslogger.class.php'; //set up the file-system logger $filepath = get_config($plugin, 'logfilelocation'); //get filename $filename = rlip_log_file_name('import', $plugin, $filepath, $entity, $manual, $starttime); if (!empty($filename)) { $this->set_log_path($filename); $fileplugin = rlip_fileplugin_factory::factory($filename, NULL, true); return rlip_fslogger_factory::factory($plugin, $fileplugin, $manual); } return null; }
/** * Validate that the library function rlip_log_file_name() * creates log files directories */ public function test_rlip_log_file_name() { global $CFG; $dataroot = rtrim($CFG->dataroot, DIRECTORY_SEPARATOR); $exportpath = '/phpunit/datahub/exportlogpath'; $importpath = '/phpunit/datahub/importlogpath'; $logfile = rlip_log_file_name('bogus', 'bogus', $exportpath); $this->assertTrue(file_exists($dataroot . $exportpath)); $logfile = rlip_log_file_name('bogus', 'bogus', $importpath); $this->assertTrue(file_exists($dataroot . $importpath)); $this->delete_full_path($dataroot, $exportpath); $this->delete_full_path($dataroot, $importpath); }
/** * Validate that log files are archived for a variety of import and * export plugins, for a variety of configured log paths * * @param string $plugintype One of 'import' or 'export' * @param string $plugin The import plugin to associate log files to * @param string $logfilelocation The logfilelocation setting value to use * @dataProvider importpluginprovider */ public function test_logfilesarchived($plugintype, $plugin, $logfilelocation) { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/local/datahub/fileplugins/log/log.class.php'; require_once $CFG->libdir . '/filestorage/zip_archive.php'; // Clean-up any existing log & zip files. self::cleanup_log_files(); self::cleanup_zip_files(); // Set up the log path. set_config('logfilelocation', $logfilelocation, $plugin); $format = get_string('logfile_timestamp', 'local_datahub'); $USER->timezone = 99; // Create some log files to be zipped by the cron job. // Way earlier then any real existing files! $starttime = make_timestamp(1971, 1, 3); $filenames = array(); for ($i = 0; $i < 10; ++$i) { $filenames[$i] = rlip_log_file_name($plugintype, $plugin, $logfilelocation, 'user', false, $starttime + $i * 3600); // Write out a line to the logfile. $logfile = new rlip_fileplugin_log($filenames[$i]); $logfile->open(RLIP_FILE_WRITE); $logfile->write(array('test entry')); $logfile->close(); } // Call cron job that zips the specified day's log files. $zipfiles = rlip_compress_logs_cron('bogus', 0, $starttime); $this->assertTrue(!empty($zipfiles)); // Was a zip file created?. // Verify that the compressed file exists. $exists = file_exists($zipfiles[0]); $this->assertTrue($exists); // Open zip_archive and verify all logs included. $zip = new zip_archive(); $result = $zip->open($zipfiles[0]); $this->assertTrue($result); $this->assertEquals(10, $zip->count()); $zip->close(); // Verify that the log files created are gone.... for ($i = 0; $i < 10; ++$i) { $exists = file_exists($filenames[$i]); $this->assertFalse($exists); } // Validate that the zip file name corresponds to the plugin. // E.g. pugin is 'dhimport_version1' and file name starts with 'import_version1_'. $parts = explode('/', $zipfiles[0]); $this->assertStringStartsWith($plugin . '_', 'dh' . $parts[count($parts) - 1]); // Delete the test zip. @unlink($zipfiles[0]); }
/** * Validate that a manual import log file generates the proper name */ public function testversion1importlogname() { global $CFG; // Pass manual and then scheduled and a timestamp and verify that the name is correct. $filepath = $CFG->dataroot . RLIP_DEFAULT_LOG_PATH; $plugintype = 'import'; $plugin = 'dhimport_version1'; $manual = true; $entity = 'user'; $timestamp = time(); $format = get_string('logfile_timestamp', 'local_datahub'); $entity = 'user'; $filename = rlip_log_file_name($plugintype, $plugin, '', $entity, $manual, $timestamp); $testfilename = $filepath . '/' . $plugintype . '_version1_manual_' . $entity . '_' . userdate($timestamp, $format) . '.log'; // Get most recent logfile +1 as that is what is returned by rlip_log_file_name. $testfilename = self::get_next_logfile($testfilename); $this->assertEquals($filename, $testfilename); }
/** * Default export plugin constructor * * @param object $fileplugin the file plugin used for output * @param boolean $manual Set to true if a manual run */ function __construct($fileplugin, $manual = false) { global $CFG; require_once $CFG->dirroot . '/local/datahub/lib/rlip_dblogger.class.php'; require_once $CFG->dirroot . '/local/datahub/lib/rlip_fslogger.class.php'; $this->fileplugin = $fileplugin; $this->manual = $manual; $this->dblogger = new rlip_dblogger_export($this->manual); //convert class name to plugin name $class = get_class($this); $this->plugin = str_replace('rlip_exportplugin_', 'dhexport_', $class); //track the start time as the current time - moved from run() in order to support rlip_log_file_name $this->dblogger->set_starttime(time()); //set up the file-system logger, if exists $filepath = get_config($this->plugin, 'logfilelocation'); $filename = rlip_log_file_name('export', $this->plugin, $filepath, '', $manual, $this->dblogger->starttime); if (!empty($filename)) { $this->dblogger->set_log_path($filename); $fileplugin = rlip_fileplugin_factory::factory($filename, NULL, true, $manual); $this->fslogger = rlip_fslogger_factory::factory($this->plugin, $fileplugin, $this->manual); } $this->dblogger->set_plugin($this->plugin); }