diff --git a/config/siteMap.inc b/config/siteMap.inc index e33470d..4d4e996 100644 --- a/config/siteMap.inc +++ b/config/siteMap.inc @@ -143,6 +143,7 @@ $default->siteMap->addPage("manageHelp", "/presentation/lookAndFeel/knowledgeTre $default->siteMap->addPage("managePermissions", "/presentation/lookAndFeel/knowledgeTree/administration/permissions/managePermissions.php", "Administration", SysAdmin, _("Permissions Administration"), true, 18); $default->siteMap->addPage("manageLookupTrees", "/presentation/lookAndFeel/knowledgeTree/administration/docfieldmanagement/manageLookupTrees.php", "Administration", SysAdmin, _("Lookup Tree Administration"), true, 19); $default->siteMap->addPage("manageWorkflows", "/presentation/lookAndFeel/knowledgeTree/administration/workflow/workflows.php", "Administration", SysAdmin, _("Workflow Management"), true, 20); +$default->siteMap->addPage("manageCleanup", "/presentation/lookAndFeel/knowledgeTree/administration/documentmanagement/manageCleanup.php", "Administration", SysAdmin, _("Repository Cleanup"), true, 21); $default->siteMap->addSectionColour("Administration", "th", "056DCE"); $default->siteMap->addSectionColour("Administration", "td", "6699FF"); diff --git a/presentation/lookAndFeel/knowledgeTree/administration/documentmanagement/manageCleanup.php b/presentation/lookAndFeel/knowledgeTree/administration/documentmanagement/manageCleanup.php new file mode 100644 index 0000000..25b4748 --- /dev/null +++ b/presentation/lookAndFeel/knowledgeTree/administration/documentmanagement/manageCleanup.php @@ -0,0 +1,166 @@ +get('urls/documentRoot'); + +$aIgnore = array( + '.', '..', + 'CVS', + '.empty', + '.htaccess', + '.cvsignore', +); + +$aFoldersToRemove = array(); +$aFilesToRemove = array(); +$aRepoDocumentProblems = array(); +$aRepoFolderProblems = array(); +$aRepoVersionProblems = array(); + +function checkFileVersion($path, $version) { + $fod = KTBrowseUtil::folderOrDocument($path); + if ($fod === false) { + // No document by that name, so no point checking version + // information. + return; + } + return true; +} + +function checkFile($path, $first = true) { + $pattern = "/^(.*)-((?:\d+)\.(?:\d+))$/"; + if (preg_match($pattern, $path, $matches)) { + if (checkFileVersion($matches[1], $matches[2])) { + // If it's a version, then don't check for full path + // below... + return; + } + } + $fod = KTBrowseUtil::folderOrDocument($path); + if ($fod === false) { + $GLOBALS["aFilesToRemove"][] = $path; + return; + } +} + +function checkDirectory($path) { + global $fsPath, $aIgnore; + $fullpath = sprintf("%s/%s", $fsPath, $path); + + if (!is_dir($fullpath)) { + print "Not a directory: $fullpath\n"; + } + + if ($path === '/Deleted') { + // Deleted files handled separately. + return; + } + + if (!empty($path)) { + $fod = KTBrowseUtil::folderOrDocument($path); + if ($fod === false) { + $GLOBALS["aFoldersToRemove"][] = $path; + return; + } + } + + $dh = @opendir($fullpath); + if ($dh === false) { + print "Could not open directory: $fullpath\n"; + } + while (($filename = readdir($dh)) !== false) { + if (in_array($filename, $aIgnore)) { continue; } + $subrelpath = sprintf("%s/%s", $path, $filename); + $subfullpath = sprintf("%s/%s", $fsPath, $subrelpath); + if (is_dir($subfullpath)) { + checkDirectory($subrelpath); + } + if (is_file($subfullpath)) { + checkFile($subrelpath); + } + } +} + +function checkRepoFolder($oFolder) { + global $fsPath, $aRepoFolderProblems; + $sFolderPath = sprintf("%s/%s", $oFolder->getFullPath(), $oFolder->getName()); + $sFullPath = sprintf("%s/%s", $fsPath, $sFolderPath); + if (!is_dir($sFullPath)) { + $aRepoFolderProblems[] = $sFolderPath; + } +} + +function checkRepoDocument($oDocument) { + global $fsPath, $aRepoDocumentProblems; + $sDocumentPath = $oDocument->getStoragePath(); + $sFullPath = sprintf("%s/%s", $fsPath, $sDocumentPath); + if (!is_file($sFullPath)) { + $aRepoDocumentProblems[] = $sDocumentPath; + } + checkRepoVersions($oDocument); +} + +function checkRepoVersions($oDocument) { + global $fsPath, $aRepoVersionProblems; + $table = "document_transactions"; + $aVersions = DBUtil::getResultArrayKey(array("SELECT DISTINCT version FROM $table WHERE document_id = ?", array($oDocument->getID())), "version"); foreach($aVersions as $sVersion) { + if ($sVersion == $oDocument->getVersion()) { + continue; + } + $sDocumentPath = $oDocument->getStoragePath(); + $sFullPath = sprintf("%s/%s-%s", $fsPath, $sDocumentPath, $sVersion); + if (!is_file($sFullPath)) { + $aRepoVersionProblems[] = array($sDocumentPath, $sVersion); + continue; + } + } +} + +class ManageCleanupDispatcher extends KTAdminDispatcher { + function do_main() { + global $aFoldersToRemove; + global $aFilesToRemove; + global $aRepoDocumentProblems; + global $aRepoFolderProblems; + global $aRepoVersionProblems; + + checkDirectory(""); + $aFolders =& Folder::getList(); + foreach ($aFolders as $oFolder) { + checkRepoFolder($oFolder); + } + $aDocuments =& Document::getList(array("status_id = ?", array(LIVE))); + foreach ($aDocuments as $oDocument) { + checkRepoDocument($oDocument); + } + $oTemplate =& $this->oValidator->validateTemplate('ktcore/document/cleanup'); + $oTemplate->setData(array( + 'aFoldersToRemove' => $aFoldersToRemove, + 'aFilesToRemove' => $aFilesToRemove, + 'aRepoDocumentProblems' => $aRepoDocumentProblems, + 'aRepoFolderProblems' => $aRepoFolderProblems, + 'aRepoVersionProblems' => $aRepoVersionProblems, + )); + return $oTemplate->render(); + } +} + +$oDispatcher = new ManageCleanupDispatcher(); +$oDispatcher->dispatch(); + +?> diff --git a/templates/ktcore/document/cleanup.smarty b/templates/ktcore/document/cleanup.smarty new file mode 100644 index 0000000..164f049 --- /dev/null +++ b/templates/ktcore/document/cleanup.smarty @@ -0,0 +1,44 @@ +{if $aFoldersToRemove} +

Would remove these folders (and all their contents):

+ +{/if} + +{if $aFilesToRemove} +

Would remove these files:

+ +{/if} + +{if $aRepoFolderProblems} +

These folders are not on the filesystem

+ +{/if} + +{if $aRepoDocumentProblems} +

These documents are not on the filesystem

+ +{/if} + +{if $aRepoVersionProblems} +

These documents have versions not on the filesystem

+ +{/if}