/** * Count the total number of bitstreams in the system. Provide an asset store * dao if you wish to count the number in a single asset store. * * @param null|AssetstoreDao $assetstoreDao * @return int */ public function countAll($assetstoreDao = null) { $sql = $this->database->select()->setIntegrityCheck(false)->from('bitstream', array('count' => 'count(*)')); if ($assetstoreDao) { $sql->where('assetstore_id = ?', $assetstoreDao->getKey()); } $row = $this->database->fetchRow($sql); return $row['count']; }
/** * Move all bitstreams from one asset store to another. * * @param AssetstoreDao $srcAssetstore The source asset store * @param AssetstoreDao $dstAssetstore The destination asset store * @param null|ProgressDao $progressDao Progress dao for asynchronous updating * @throws Zend_Exception */ public function moveBitstreams($srcAssetstore, $dstAssetstore, $progressDao = null) { $current = 0; /** @var ProgressModel $progressModel */ $progressModel = MidasLoader::loadModel('Progress'); /** @var BitstreamModel $bitstreamModel */ $bitstreamModel = MidasLoader::loadModel('Bitstream'); $sql = $this->database->select()->setIntegrityCheck(false)->from('bitstream')->where('assetstore_id = ?', $srcAssetstore->getKey()); $rows = $this->database->fetchAll($sql); $srcPath = $srcAssetstore->getPath(); $dstPath = $dstAssetstore->getPath(); foreach ($rows as $row) { $bitstream = $this->initDao('Bitstream', $row); if ($progressDao) { ++$current; $message = $current . ' / ' . $progressDao->getMaximum() . ': Moving ' . $bitstream->getName() . ' (' . UtilityComponent::formatSize($bitstream->getSizebytes()) . ')'; $progressModel->updateProgress($progressDao, $current, $message); } // Move the file on disk to its new location $dir1 = substr($bitstream->getChecksum(), 0, 2); $dir2 = substr($bitstream->getChecksum(), 2, 2); if (!is_dir($dstPath . '/' . $dir1)) { if (!mkdir($dstPath . '/' . $dir1)) { throw new Zend_Exception('Failed to mkdir ' . $dstPath . '/' . $dir1); } } if (!is_dir($dstPath . '/' . $dir1 . '/' . $dir2)) { if (!mkdir($dstPath . '/' . $dir1 . '/' . $dir2)) { throw new Zend_Exception('Failed to mkdir ' . $dstPath . '/' . $dir1 . '/' . $dir2); } } if (is_file($dstPath . '/' . $bitstream->getPath())) { if (is_file($srcPath . '/' . $bitstream->getPath())) { unlink($srcPath . '/' . $bitstream->getPath()); } } else { if (!rename($srcPath . '/' . $bitstream->getPath(), $dstPath . '/' . $bitstream->getPath())) { throw new Zend_Exception('Error moving ' . $bitstream->getPath()); } } // Update the asset store id on the bitstream record once it has been moved $bitstream->setAssetstoreId($dstAssetstore->getKey()); $bitstreamModel->save($bitstream); } }