public function ImportFromDirectory() { /** * get the file list with complete paths */ $arrFiles = $this->ListDir($this->strTemplatePath); $intTotalFilesToProcess = count($arrFiles); if ($intTotalFilesToProcess == 1 && pathinfo($arrFiles[0], PATHINFO_EXTENSION) == 'xpi') { return $this->ImportFromXpi($arrFiles[0]); } if ($intTotalFilesToProcess > __MAXIMUM_FILE_COUNT_TO_IMPORT__) { NarroLogger::LogError(sprintf('Too many files to process: %d. The maximum number of files to import is set in the configuration file at %d', $intTotalFilesToProcess, __MAXIMUM_FILE_COUNT_TO_IMPORT__)); return false; } NarroLogger::LogInfo(sprintf('Starting to process %d files using directory %s', $intTotalFilesToProcess, $this->strTemplatePath)); $arrDirectories = array(); NarroProgress::SetProgress(0, $this->objProject->ProjectId, 'import', $intTotalFilesToProcess); if (is_array($arrFiles)) { foreach ($arrFiles as $intFileNo => $strFileToImport) { if (preg_match('/\\/CVS|\\/\\.svn|\\/\\.hg.*|\\/\\.git/', $strFileToImport)) { continue; } $strFilePath = str_replace($this->strTemplatePath, '', $strFileToImport); $arrFileParts = explode('/', $strFilePath); $strFileName = $arrFileParts[count($arrFileParts) - 1]; unset($arrFileParts[count($arrFileParts) - 1]); unset($arrFileParts[0]); /** * create directories */ $strPath = ''; $intParentId = null; foreach ($arrFileParts as $intPos => $strDir) { $strPath = $strPath . '/' . $strDir; if (!isset($arrDirectories[$strPath])) { if ($intParentId) { $objFile = NarroFile::LoadByProjectIdFileNameParentId($this->objProject->ProjectId, $strDir, $intParentId); } else { $objFile = NarroFile::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroFile()->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroFile()->FileName, $strDir), QQ::IsNull(QQN::NarroFile()->ParentId))); } if (!$objFile instanceof NarroFile) { /** * add the file */ $objFile = new NarroFile(); $objFile->FileName = $strDir; $objFile->TypeId = NarroFileType::Folder; if ($intParentId) { $objFile->ParentId = $intParentId; } $objFile->ProjectId = $this->objProject->ProjectId; $objFile->FilePath = $strPath; $objFile->Modified = QDateTime::Now(); $objFile->Created = QDateTime::Now(); $objFile->Active = 1; if ($this->blnOnlySuggestions == false) { $objFile->Save(); } // NarroLogger::LogDebug(sprintf('Added folder "%s" from "%s"', $strDir, $strPath)); NarroImportStatistics::$arrStatistics['Imported folders']++; } $arrDirectories[$strPath] = $objFile->FileId; if (!$objFile->Active) { $objFile->Active = 1; if ($this->blnOnlySuggestions == false) { $objFile->Save(); } } // Create the file progress if needed $objFile->FileProgressForCurrentLanguage; $this->arrFileId[$objFile->FileId] = 1; } $intParentId = $arrDirectories[$strPath]; } /** * import the file */ $intFileType = $this->GetFileType($strFileName); if (!is_readable($strFileToImport)) { NarroLogger::LogError(sprintf('Cannot read "%s"', $strFileToImport)); return false; } $objFile = NarroFile::LoadByProjectIdFileNameParentId($this->objProject->ProjectId, $strFileName, $intParentId, QQ::Expand(QQN::NarroFile()->NarroFileProgressAsFile, QQ::Equal(QQN::NarroFile()->NarroFileProgressAsFile->LanguageId, $this->objTargetLanguage->LanguageId))); if ($objFile instanceof NarroFile) { $strMd5File = md5_file($strFileToImport); if ($strMd5File == $objFile->FileMd5) { $blnSourceFileChanged = false; NarroImportStatistics::$arrStatistics['Unchanged files']++; } else { $objFile->FileMd5 = $strMd5File; $objFile->Save(); // Set the md5 for all languages to null to force import NarroFile::GetDatabase()->NonQuery(sprintf('UPDATE narro_file_progress SET file_md5=NULL WHERE file_id=%d', $objFile->FileId)); $blnSourceFileChanged = true; NarroImportStatistics::$arrStatistics['Changed files']++; } NarroImportStatistics::$arrStatistics['Kept files']++; } else { /** * add the file */ $objFile = new NarroFile(); $objFile->FileName = $strFileName; $objFile->TypeId = $intFileType; if ($intParentId) { $objFile->ParentId = $intParentId; } $objFile->ProjectId = $this->objProject->ProjectId; $objFile->Active = 1; $objFile->FilePath = $strFilePath; $objFile->FileMd5 = md5_file($strFileToImport); $objFile->Modified = QDateTime::Now(); $objFile->Created = QDateTime::Now(); if ($this->blnOnlySuggestions == false) { $objFile->Save(); } $blnSourceFileChanged = true; QApplication::$PluginHandler->ActivateFile($objFile, $this->objProject); // NarroLogger::LogDebug(sprintf('Added file "%s" from "%s"', $strFileName, $strPath)); NarroImportStatistics::$arrStatistics['Imported files']++; } if ($objFile->Active == 0) { $objFile->Active = 1; if ($this->blnOnlySuggestions == false) { $objFile->Save(); } } $this->arrFileId[$objFile->FileId] = 1; $strTranslatedFileToImport = str_replace($this->strTemplatePath, $this->strTranslationPath, $strFileToImport); if ($objFile->FileId > 0) { $intTime = time(); if (file_exists($strTranslatedFileToImport)) { $this->ImportFile($objFile, $strFileToImport, $strTranslatedFileToImport); } else { // it's ok, equal strings won't be imported $this->ImportFile($objFile, $strFileToImport); } $intElapsedTime = time() - $intTime; // NarroLogger::LogDebug(sprintf('Processed file "%s" in %d seconds, %d files left', str_replace($this->strTemplatePath, '', $strFileToImport), $intElapsedTime, (count($arrFiles) - $intFileNo - 1))); } NarroProgress::SetProgress(intval(($intFileNo + 1) * 100 / $intTotalFilesToProcess), $this->objProject->ProjectId, 'import', $intTotalFilesToProcess, 1); } } return true; }