Commit d77aa40b91d44dad31455d4c74def969df7f353f

Authored by Neil Blakey-Milner
1 parent 8358b360

Reimplement delete, restore, and expunge using document content versions


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4649 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/storage/ondiskpathstoragemanager.inc.php
... ... @@ -227,109 +227,50 @@ class KTOnDiskPathStorageManager extends KTStorageManager {
227 227 * return boolean true on successful move, false otherwhise
228 228 */
229 229 function delete($oDocument) {
230   - global $default;
231   - // current document path
232   - $sCurrentPath = $oDocument->getPath();
  230 + $oConfig =& KTConfig::getSingleton();
  231 + $sCurrentPath = $this->getPath($oDocument);
233 232  
234 233 // check if the deleted folder exists and create it if not
235   - $sDeletedPrefix = $default->documentRoot . "/Deleted";
  234 + $sDeletedPrefix = sprintf("%s/Deleted", $oConfig->get('urls/documentRoot'));
236 235 if (!file_exists($sDeletedPrefix)) {
237 236 mkdir($sDeletedPrefix, 0755);
238 237 }
239   -
240   - // move the file to the deleted folder, prefixed by its document id
241   - $sDeletedPrefix = $default->documentRoot . "/Deleted/" . $oDocument->getID() . "-" . $oDocument->getFileName();
242   -
243   - // find all the previous versions of this document and move them
244   - // ie. interrogate transaction history for all CHECKIN transactions and retrieve the versions
245   - // FIXME: refactor
246   - $sql = $default->db;
247   - $sQuery = "SELECT DISTINCT version FROM $default->document_transactions_table WHERE document_id = ? AND transaction_namespace = ?";/*ok*/
248   - $aParams = array($oDocument->getID(), 'ktcore.transactions.check_out');
249   - $result = $sql->query(array($sQuery, $aParams));
250   - if ($result) {
251   - while ($sql->next_record()) {
252   - $sVersion = $sql->f("version");
253   - if ($sVersion <> $oDocument->getVersion()) {
254   - $sVersionedPath = $sCurrentPath . "-" . $sVersion;
255   - $sDeletedPath = $sDeletedPrefix . "-" . $sVersion;
256   - // move it to the deleted folder
257   - $default->log->info("PhysicalDocumentManager::delete moving $sVersionedPath to $sDeletedPath");
258   - if (!PhysicalDocumentManager::move($sVersionedPath, $sDeletedPath)) {
259   - $default->log->error("PhysicalDocumentManager::delete error moving $sVersionedPath to $sDeletedPath; documentID=" . $oDocument->getID());
260   - // FIXME: can't bail now since we don't have transactions- so we doggedly continue deleting and logging errors
261   - }
262   - }
263   - }
264   - } else {
265   - $default->log->error("PhysicalDocumentManager::delete error looking up document versions, id=" . $oDocument->getID());
266   - }
267 238  
268   - // now move the current version
  239 + $sDocumentRoot = $oConfig->get('urls/documentRoot');
269 240  
270   - if (PhysicalDocumentManager::move($sCurrentPath, $sDeletedPrefix)) {
271   - return true;
272   - } else {
273   - $default->log->error("in OnDiskStorage, PhysicalDocumentManager::delete couldn't move $sCurrentPath to $sDeletedPrefix, documentID=" . $oDocument->getID());
274   - return false;
275   - }
  241 + $aVersions = KTDocumentContentVersion::getByDocument($oDocument);
  242 + foreach ($aVersions as $oVersion) {
  243 + $sOldPath = $oVersion->getStoragePath();
  244 + $sNewPath = sprintf("Deleted/%s-%s", $oVersion->getId(), $oVersion->getFileName());
  245 + $sFullOldPath = sprintf("%s/%s", $sDocumentRoot, $sOldPath);
  246 + $sFullNewPath = sprintf("%s/%s", $sDocumentRoot, $sNewPath);
  247 + KTUtil::moveFile($sFullOldPath, $sFullNewPath);
  248 + }
  249 + return true;
276 250 }
277 251  
278 252 /**
279 253 * Completely remove a document from the Deleted/ folder
280 254 *
281   - * return boolean true on successful move, false otherwhise
  255 + * return boolean true on successful expunge
282 256 */
283 257 function expunge($oDocument) {
284   - global $default;
285   - // deleted document path
286   - $sDeletedPrefix = $default->documentRoot . "/Deleted/" . $oDocument->getID() . "-" . $oDocument->getFileName();
  258 + $oConfig =& KTConfig::getSingleton();
  259 + $sCurrentPath = $this->getPath($oDocument);
287 260  
288   - // find all the previous versions of this document and delete them
289   - // ie. interrogate transaction history for all CHECKIN transactions and retrieve the versions
290   - // FIXME: refactor
291   - $sql = $default->db;
292   - $sQuery = "SELECT DISTINCT version FROM $default->document_transactions_table WHERE document_id = ? AND transaction_id = ?";/*ok*/
293   - $aParams = array($oDocument->getID(), CHECKOUT);
294   - $result = $sql->query(array($sQuery, $aParams));
295   - if ($result) {
296   - while ($sql->next_record()) {
297   - $sVersion = $sql->f("version");
298   - if ($sVersion <> $oDocument->getVersion()) {
299   - $sExpungePath = $sDeletedPrefix . "-" . $sVersion;
300   - // zap it
301   - $default->log->info("PhysicalDocumentManager::expunge rm'ing $sExpungePath");
302   - if (file_exists($sExpungePath)) {
303   - if (!unlink($sExpungePath)) {
304   - $default->log->error("PhysicalDocumentManager::expunge error deleting $sExpungePath; documentID=" . $oDocument->getID());
305   - // FIXME: can't bail now since we don't have transactions- so we doggedly continue deleting and logging errors
306   - }
307   - } else {
308   - $default->log->error("PhysicalDocumentManager::expunge can't rm $sExpungePath because it doesn't exist");
309   - }
310   - }
311   - }
312   - } else {
313   - $default->log->error("PhysicalDocumentManager::expunge error looking up document versions, id=" . $oDocument->getID());
314   - }
  261 + // check if the deleted folder exists and create it if not
  262 + $sDeletedPrefix = sprintf("%s/Deleted", $oConfig->get('urls/documentRoot'));
  263 + $sDocumentRoot = $oConfig->get('urls/documentRoot');
315 264  
316   - if (file_exists($sDeletedPrefix)) {
317   - // now delete the current version
318   - if (unlink($sDeletedPrefix)) {
319   - $default->log->info("PhysicalDocumentManager::expunge unlinkied $sDeletedPrefix");
320   - return true;
321   - } else {
322   - $default->log->info("PhysicalDocumentManager::expunge couldn't unlink $sDeletedPrefix");
323   - if (file_exists($sDeletedPrefix)) {
324   - return false;
325   - } else {
326   - return true;
327   - }
328   - }
329   - } else {
330   - $default->log->info("PhysicalDocumentManager::expunge can't rm $sDeletedPrefix because it doesn't exist");
331   - return true;
332   - }
  265 + $aVersions = KTDocumentContentVersion::getByDocument($oDocument);
  266 + foreach ($aVersions as $oVersion) {
  267 + $sPath = sprintf("Deleted/%s-%s", $oVersion->getId(), $oVersion->getFileName());
  268 + $sFullPath = sprintf("%s/%s", $sDocumentRoot, $sPath);
  269 + if (file_exists($sFullPath)) {
  270 + unlink($sFullPath);
  271 + }
  272 + }
  273 + return true;
333 274 }
334 275  
335 276 /**
... ... @@ -338,46 +279,22 @@ class KTOnDiskPathStorageManager extends KTStorageManager {
338 279 * return boolean true on successful move, false otherwhise
339 280 */
340 281 function restore($oDocument) {
341   - global $default;
  282 + $oConfig =& KTConfig::getSingleton();
  283 + $sCurrentPath = $this->getPath($oDocument);
342 284  
343   - // deleted document path (includes previous versions)
344   - $sDeletedPath = $default->documentRoot . "/Deleted/" . $oDocument->getID() . "-" . $oDocument->getFileName();
345   -
346   - // build the path to the new folder
347   - $sRestorePath = Folder::getFolderPath($oDocument->getFolderID()) . "/" . $oDocument->getFileName();
348   -
349   - // find all the previous versions of this document and move them
350   - // ie. interrogate transaction history for all CHECKIN transactions and retrieve the versions
351   - // FIXME: refactor
352   - $sql = $default->db;
353   - $sQuery = "SELECT DISTINCT version FROM $default->document_transactions_table WHERE document_id = ? AND transaction_id = ?";/*ok*/
354   - $aParams = array($oDocument->getID(), CHECKOUT);
355   - $result = $sql->query(array($sQuery, $aParams));
356   - if ($result) {
357   - while ($sql->next_record()) {
358   - $sVersion = $sql->f("version");
359   - if ($sVersion <> $oDocument->getVersion()) {
360   - $sVersionedDeletedPath = $sDeletedPath . "-" . $sVersion;
361   - $sVersionedRestorePath = $sRestorePath . "-" . $sVersion;
362   - // move it to the new folder
363   - $default->log->info("PhysicalDocumentManager::restore moving $sVersionedDeletedPath to $sVersionedRestorePath");
364   - if (!PhysicalDocumentManager::move($sVersionedDeletedPath, $sVersionedRestorePath)) {
365   - $default->log->error("PhysicalDocumentManager::restore error moving $sVersionedDeletedPath to $sVersionedRestorePath; documentID=" . $oDocument->getID());
366   - // FIXME: can't bail now since we don't have transactions- so we doggedly continue restoring and logging errors
367   - }
368   - }
369   - }
370   - } else {
371   - $default->log->error("PhysicalDocumentManager::expunge error looking up document versions, id=" . $oDocument->getID());
  285 + // check if the deleted folder exists and create it if not
  286 + $sDeletedPrefix = sprintf("%s/Deleted", $oConfig->get('urls/documentRoot'));
  287 + $sDocumentRoot = $oConfig->get('urls/documentRoot');
  288 +
  289 + $aVersions = KTDocumentContentVersion::getByDocument($oDocument);
  290 + foreach ($aVersions as $oVersion) {
  291 + $sNewPath = $oVersion->getStoragePath();
  292 + $sOldPath = sprintf("Deleted/%s-%s", $oVersion->getId(), $oVersion->getFileName());
  293 + $sFullNewPath = sprintf("%s/%s", $sDocumentRoot, $sNewPath);
  294 + $sFullOldPath = sprintf("%s/%s", $sDocumentRoot, $sOldPath);
  295 + KTUtil::moveFile($sFullOldPath, $sFullNewPath);
372 296 }
373   -
374   - // now move the current version
375   - if (PhysicalDocumentManager::move($sDeletedPath, $sRestorePath)) {
376   - return true;
377   - } else {
378   - $default->log->error("PhysicalDocumentManager::restore couldn't move $sDeletedPath to $sRestorePath, documentID=" . $oDocument->getID());
379   - return false;
380   - }
  297 + return true;
381 298 }
382 299  
383 300  
... ...