48 return 'Dead File Revisions';
61 return 'A test to check for dead file revisions found in the data/file_repository directory, but the asset has been deleted.';
81 private static function getAssetDirs($dir)
84 if (!$dh = opendir($dir)) {
88 while (($file = readdir($dh)) !==
false) {
89 if ($file ==
'.' || $file ==
'..') {
92 $fullpath = $dir .
'/' . $file;
93 if (is_dir($fullpath)) {
99 foreach ($dirs as $dir) {
100 $subdirs = self::getAssetDirs($dir);
101 if (empty($subdirs)) {
102 self::$dirList[] = $dir;
104 $dirs = array_merge($dirs, $subdirs);
121 private static function findAssetsToDelete($assetIdsOnly=TRUE)
123 $file_repo_dir = SQ_DATA_PATH .
'/file_repository';
124 $private_dir = SQ_DATA_PATH .
'/private';
125 if (empty(self::$dirList)) {
126 self::getAssetDirs($file_repo_dir);
129 $dirs = self::$dirList;
132 $assetsToDelete = Array();
134 $fullPaths = Array();
140 foreach ($dirs as $dir) {
141 $dir = str_replace($file_repo_dir,
'', $dir);
142 if (!is_dir($private_dir . $dir)) {
143 $dir_parts = explode(
'/', $dir);
144 $assetid = array_pop($dir_parts);
145 if ($assetid ==
'varieties') {
146 $dir = implode(
'/', $dir_parts);
147 $assetid = array_pop($dir_parts);
149 $assetsToDelete[$assetid] = $assetid;
150 $fullPaths[$assetid] = $dir;
155 return $assetsToDelete;
176 public static function execute(&$messages, &$errors)
179 if (SQ_CONF_ROLLBACK_ENABLED) {
180 $messages[] =
"Rollback is enabled, so will not check for orphaned file repository entries.\n\tThey may be required when viewing the system in rollback mode.";
185 $assetsToDelete = self::findAssetsToDelete();
187 $assetsCleanedUp = Array();
189 if (empty($assetsToDelete)) {
190 $messages[] =
'There are no orphaned file repository entries.';
194 $messages[] =
'There are ' .
sizeof($assetsToDelete) .
' orphaned file repository entries to clean up.';
195 $errors[] =
'Will clean up these asset ids: ' . implode(
',', $assetsToDelete);
197 $fullPaths = self::findAssetsToDelete(FALSE);
199 foreach ($assetsToDelete as $assetid) {
201 $exists = $GLOBALS[
'SQ_SYSTEM']->am->assetExists($assetid);
203 catch (Exception $e) {
208 $msg =
"There is a problem with the data directories for asset $assetid\n";
209 $msg .=
"\tThis asset still exists. Skipping.";
211 unset($assetsToDelete[$assetid]);
212 unset($fullPaths[$assetid]);
215 $filedir = $fullPaths[$assetid];
216 $path = SQ_DATA_PATH .
'/file_repository' . $filedir;
217 $file_list = list_files($path, TRUE);
219 if (is_dir($path.
'/varieties')) {
220 $file_list = array_merge($file_list, list_files($path.
'/varieties', TRUE));
222 $bad_files = Array();
223 foreach ($file_list as $file) {
226 $bad_files[] = $file;
229 if (empty($bad_files)) {
230 if (is_dir($path.
'/varieties')) {
231 rmdir($path.
'/varieties');
234 $assetsCleanedUp[] = $assetid;
236 $msg =
"Unable to delete the following files:\n";
237 $msg .= implode(
"\n\t\t", $bad_files);
239 unset($assetsToDelete[$assetid]);
240 unset($fullPaths[$assetid]);
249 if (!empty($fullPaths)) {
251 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
253 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
257 foreach (array_chunk($fullPaths, 999) as $chunk) {
258 $query =
"SELECT fileid FROM sq_file_vers_file WHERE path IN (";
259 foreach ($chunk as $path) {
262 $query = rtrim($query,
',') .
')';
266 foreach ($fileInfo as $row => $details) {
271 foreach (array_chunk($fileIds, 999) as $chunk) {
272 $query =
"DELETE FROM sq_file_vers_file WHERE fileid IN (" . implode(
',', $chunk) .
")";
274 $query =
"DELETE FROM sq_file_vers_history WHERE fileid IN (" . implode(
',', $chunk) .
")";
278 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
279 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
282 $msg =
"The following assets were cleaned up successfully:\n";
283 $msg .=
"\t" . implode(
',', $assetsCleanedUp);
300 public static function test(&$messages, &$errors)
303 if (SQ_CONF_ROLLBACK_ENABLED) {
304 $messages[] =
"Rollback is enabled, so will not check for orphaned file repository entries.\n\tThey may be required when viewing the system in rollback mode.";
308 $assetsToDelete = self::findAssetsToDelete();
310 if (empty($assetsToDelete)) {
311 $messages[] =
'There are no orphaned file repository entries.';
314 $messages[] =
'There are ' .
sizeof($assetsToDelete) .
' orphaned file repository entries to clean up.';
315 $errors[] =
'Will clean up these asset ids: ' . implode(
',', $assetsToDelete);