PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/storageverification.php

https://github.com/sfsergey/knowledgetree
PHP | 190 lines | 128 code | 25 blank | 37 comment | 11 complexity | f21d7d84a8854ec6a7767ea294403448 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * $Id$
  4. *
  5. * KnowledgeTree Community Edition
  6. * Document Management Made Simple
  7. * Copyright (C) 2008, 2009 KnowledgeTree Inc.
  8. * Portions copyright The Jam Warehouse Software (Pty) Limited
  9. *
  10. * This program is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU General Public License version 3 as published by the
  12. * Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
  23. * California 94120-7775, or email info@knowledgetree.com.
  24. *
  25. * The interactive user interfaces in modified source and object code versions
  26. * of this program must display Appropriate Legal Notices, as required under
  27. * Section 5 of the GNU General Public License version 3.
  28. *
  29. * In accordance with Section 7(b) of the GNU General Public License version 3,
  30. * these Appropriate Legal Notices must retain the display of the "Powered by
  31. * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  32. * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
  33. * must display the words "Powered by KnowledgeTree" and retain the original
  34. * copyright notice.
  35. * Contributor( s): ______________________________________
  36. */
  37. require_once(dirname(__FILE__) . '/../config/dmsDefaults.php');
  38. require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
  39. // TODO: this does not verify files that are in the storage system, but not on the database. It is better that the storage system
  40. // be in sync with the database. However, we should have a better way to query the storage driver to give us a list of files.
  41. class StorageVerification
  42. {
  43. private $count;
  44. private $lineCount;
  45. private $doc;
  46. const DOCS_PER_DOT = 100;
  47. const DOTS_PER_LINE = 80;
  48. private $nl;
  49. private $tab;
  50. private
  51. function error($msg)
  52. {
  53. $doc = $this->doc;
  54. $documentId = $doc->getId();
  55. $path = $doc->getFullPath();
  56. $filename = $doc->getFileName();
  57. $storagePath = $doc->getStoragePath();
  58. print "{$this->nl}{$this->nl}";
  59. print "Problem with Document ID: {$documentId}{$this->nl}";
  60. print "{$this->tab}Path: {$path}{$this->nl}";
  61. print "{$this->tab}Filename: {$filename}{$this->nl}";
  62. print "{$this->tab}StoragePath: {$storagePath}{$this->nl}";
  63. print "{$this->tab}Problem: {$msg}{$this->nl}{$this->nl}";
  64. flush();
  65. $this->count = 0;
  66. $this->lineCount = 0;
  67. $this->clearCache();
  68. }
  69. private
  70. function progress()
  71. {
  72. if ($this->count++ % StorageVerification::DOCS_PER_DOT == 0)
  73. {
  74. $this->lineCount++;
  75. print '.';
  76. flush();
  77. }
  78. if ($this->lineCount == StorageVerification::DOTS_PER_LINE )
  79. {
  80. print "{$this->nl}";
  81. flush();
  82. $this->lineCount = 0;
  83. }
  84. $this->clearCache();
  85. }
  86. private
  87. function clearCache()
  88. {
  89. $metadataid = $this->doc->getMetadataVersionId();
  90. $contentid = $this->doc->getContentVersionId();
  91. $iId = $this->doc->getId();
  92. $cache = KTCache::getSingleton();
  93. $cache->remove('KTDocumentMetadataVersion/id', $metadataid);
  94. $cache->remove('KTDocumentContentVersion/id', $contentid);
  95. $cache->remove('KTDocumentCore/id', $iId);
  96. $cache->remove('Document/id', $iId);
  97. unset($GLOBALS['_OBJECTCACHE']['KTDocumentMetadataVersion'][$metadataid]);
  98. unset($GLOBALS['_OBJECTCACHE']['KTDocumentContentVersion'][$contentid]);
  99. unset($GLOBALS['_OBJECTCACHE']['KTDocumentCore'][$iId]);
  100. unset($this->doc);
  101. }
  102. public
  103. function run()
  104. {
  105. global $argc;
  106. if (isset($argc))
  107. {
  108. $this->nl = "\n";
  109. $this->tab = "\t";
  110. print "Storage Verification{$this->nl}";
  111. print "===================={$this->nl}";
  112. }
  113. else
  114. {
  115. $this->nl = '<br>';
  116. $this->tab = '&nbsp;&nbsp;&nbsp;&nbsp;';
  117. print "<b>Storage Verification</b>{$this->nl}";
  118. }
  119. $sql = "SELECT
  120. dmv.id as metadata_version_id, dcv.document_id, dcv.md5hash, dcv.size
  121. FROM
  122. document_content_version dcv
  123. INNER JOIN document_metadata_version dmv ON dcv.id=dmv.content_version_id";
  124. $rows = DBUtil::getResultArray($sql);
  125. $this->count = 0;
  126. $this->lineCount = 0;
  127. $storage =& KTStorageManagerUtil::getSingleton();
  128. foreach($rows as $row)
  129. {
  130. $doc = Document::get($row['document_id'], $row['metadata_version_id']);
  131. if (PEAR::isError($doc))
  132. {
  133. $msg = $doc->getMessage();
  134. $this->error($doc, "Error with document: {$msg}");
  135. continue;
  136. }
  137. $this->doc = $doc;
  138. $tmpPath = $storage->temporaryFile($doc);
  139. if (!file_exists($tmpPath))
  140. {
  141. $this->error("Temporary file could not be resolved: {$tmpPath}");
  142. continue;
  143. }
  144. $expectedSize = $row['size'];
  145. $currentSize = filesize($tmpPath);
  146. if ($expectedSize != $currentSize)
  147. {
  148. $this->error("Filesize does not match. Expected: {$expectedSize} Current: {$currentSize}");
  149. continue;
  150. }
  151. $expectedHash = $row['md5hash'];
  152. $currentHash = md5_file($tmpPath);
  153. if ($expectedHash != $currentHash)
  154. {
  155. $this->error("Hash does not match. Expected: {$expectedHash} Current: {$currentHash}");
  156. continue;
  157. }
  158. $this->progress();
  159. }
  160. print "{$this->nl}Done.{$this->nl}{$this->nl}";
  161. }
  162. }
  163. $verification = new StorageVerification();
  164. $verification->run();
  165. ?>