From bc525d96285ac2a82e129780173326bd4021fc22 Mon Sep 17 00:00:00 2001 From: Neil Blakey-Milner Date: Thu, 15 Sep 2005 09:14:27 +0000 Subject: [PATCH] Add an abstracted import storage system, and implement importing from the filesystem. This can be extended to import from various archive files (such as ZIP), from other document management tools, from CVS or SVN. Includes the idea that multiple content versions exists. Does not handle importing metadata at all. --- lib/import/bulkimport.inc.php | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/import/fsimportstorage.inc.php | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/import/importstorage.inc.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 256 insertions(+), 0 deletions(-) create mode 100644 lib/import/bulkimport.inc.php create mode 100644 lib/import/fsimportstorage.inc.php create mode 100644 lib/import/importstorage.inc.php diff --git a/lib/import/bulkimport.inc.php b/lib/import/bulkimport.inc.php new file mode 100644 index 0000000..7f4bfe7 --- /dev/null +++ b/lib/import/bulkimport.inc.php @@ -0,0 +1,90 @@ +oFolder =& $oFolder; + $this->oStorage =& $oStorage; + $this->oUser =& $oUser; + $this->aOptions =& $aOptions; + if (is_null($aOptions)) { + $aOptions = array(); + } + $this->aMetadata = KTUtil::arrayGet($aOptions, 'metadata', array()); + } + + function import() { + $res = $this->_importfolder($this->oFolder, "/"); + if (PEAR::isError($res)) { + DBUtil::rollback(); + return $res; + } + return; + } + + function _importfolder($oFolder, $sPath) { + $aDocPaths = $this->oStorage->listDocuments($sPath); + foreach ($aDocPaths as $sDocumentPath) { + $res = $this->_importdocument($oFolder, $sDocumentPath); + if (PEAR::isError($res)) { + return $res; + } + } + $aFolderPaths = $this->oStorage->listFolders($sPath); + foreach ($aFolderPaths as $sFolderPath) { + $oThisFolder = KTFolderUtil::add($oFolder, basename($sFolderPath), $this->oUser); + if (PEAR::isError($oThisFolder)) { + return $oThisFolder; + } + $res = $this->_importfolder($oThisFolder, $sFolderPath); + if (PEAR::isError($res)) { + return $res; + } + } + } + + function _importdocument($oFolder, $sPath) { + $aInfo = $this->oStorage->getDocumentInfo($sPath); + if (PEAR::isError($aInfo)) { + return $aInfo; + } + $aOptions = array( + // XXX: Multiversion Import + 'contents' => $aInfo->aVersions[0], + 'metadata' => $this->aMetadata, + ); + $oDocument =& KTDocumentUtil::add($oFolder, basename($sPath), $this->oUser, $aOptions); + return $oDocument; + } +} + +?> diff --git a/lib/import/fsimportstorage.inc.php b/lib/import/fsimportstorage.inc.php new file mode 100644 index 0000000..9f8dff5 --- /dev/null +++ b/lib/import/fsimportstorage.inc.php @@ -0,0 +1,102 @@ +sBasePath = $sBasePath; + } + + function listDocuments($sFolderPath) { + $ret = array(); + if (substr($sFolderPath, -1) === "/") { + $sFolderPath = substr($sFolderPath, 0, -1); + } + $sFullPath = sprintf("%s/%s", $this->sBasePath, $sFolderPath); + if (!is_dir($sFullPath)) { + return PEAR::raiseError('Path is not a folder'); + } + $rDir = @opendir($sFullPath); + if ($rDir === false) { + return PEAR::raiseError('Failed to open folder'); + } + while (($sFilename = readdir($rDir)) !== false) { + $sThisPath = sprintf("%s/%s", $sFullPath, $sFilename); + if (is_file($sThisPath)) { + if (empty($sFolderPath)) { + $ret[] = $sFilename; + } else { + $ret[] = sprintf("%s/%s", $sFolderPath, $sFilename); + } + } + } + closedir($rDir); + return $ret; + } + + function listFolders($sFolderPath) { + $ret = array(); + if (substr($sFolderPath, -1) === "/") { + $sFolderPath = substr($sFolderPath, 0, -1); + } + $sFullPath = sprintf("%s/%s", $this->sBasePath, $sFolderPath); + if (!is_dir($sFullPath)) { + return PEAR::raiseError('Path is not a folder'); + } + $rDir = @opendir($sFullPath); + if ($rDir === false) { + return PEAR::raiseError('Failed to open folder'); + } + while (($sFilename = readdir($rDir)) !== false) { + if (in_array($sFilename, array(".", ".."))) { + continue; + } + $sThisPath = sprintf("%s/%s", $sFullPath, $sFilename); + if (is_dir($sThisPath)) { + if (empty($sFolderPath)) { + $ret[] = $sFilename; + } else { + $ret[] = sprintf("%s/%s", $sFolderPath, $sFilename); + } + } + } + closedir($rDir); + return $ret; + } + + function getDocumentInfo($sDocumentPath) { + return new KTImportStorageInfo( + basename($sDocumentPath), + array( + new KTFSFileLike(sprintf("%s/%s", $this->sBasePath, $sDocumentPath)) + ) + ); + } +} + +?> diff --git a/lib/import/importstorage.inc.php b/lib/import/importstorage.inc.php new file mode 100644 index 0000000..bb638a6 --- /dev/null +++ b/lib/import/importstorage.inc.php @@ -0,0 +1,64 @@ +sFilename = $sFilename; + $this->aVersions = $aVersions; + } + + function getFilename() { + return $this->sFilename; + } +} + +?> -- libgit2 0.21.4