[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorFilesManagementCompactWorkflow 4 extends PhabricatorFilesManagementWorkflow { 5 6 public function didConstruct() { 7 $this 8 ->setName('compact') 9 ->setSynopsis( 10 pht( 11 'Merge identical files to share the same storage. In some cases, '. 12 'this can repair files with missing data.')) 13 ->setArguments( 14 array( 15 array( 16 'name' => 'dry-run', 17 'help' => pht('Show what would be compacted.'), 18 ), 19 array( 20 'name' => 'all', 21 'help' => pht('Compact all files.'), 22 ), 23 array( 24 'name' => 'names', 25 'wildcard' => true, 26 ), 27 )); 28 } 29 30 public function execute(PhutilArgumentParser $args) { 31 $console = PhutilConsole::getConsole(); 32 33 $iterator = $this->buildIterator($args); 34 if (!$iterator) { 35 throw new PhutilArgumentUsageException( 36 pht( 37 'Either specify a list of files to compact, or use `--all` '. 38 'to compact all files.')); 39 } 40 41 $is_dry_run = $args->getArg('dry-run'); 42 43 foreach ($iterator as $file) { 44 $monogram = $file->getMonogram(); 45 46 $hash = $file->getContentHash(); 47 if (!$hash) { 48 $console->writeOut( 49 "%s\n", 50 pht('%s: No content hash.', $monogram)); 51 continue; 52 } 53 54 // Find other files with the same content hash. We're going to point 55 // them at the data for this file. 56 $similar_files = id(new PhabricatorFile())->loadAllWhere( 57 'contentHash = %s AND id != %d AND 58 (storageEngine != %s OR storageHandle != %s)', 59 $hash, 60 $file->getID(), 61 $file->getStorageEngine(), 62 $file->getStorageHandle()); 63 if (!$similar_files) { 64 $console->writeOut( 65 "%s\n", 66 pht('%s: No other files with the same content hash.', $monogram)); 67 continue; 68 } 69 70 // Only compact files into this one if we can load the data. This 71 // prevents us from breaking working files if we're missing some data. 72 try { 73 $data = $file->loadFileData(); 74 } catch (Exception $ex) { 75 $data = null; 76 } 77 78 if ($data === null) { 79 $console->writeOut( 80 "%s\n", 81 pht( 82 '%s: Unable to load file data; declining to compact.', 83 $monogram)); 84 continue; 85 } 86 87 foreach ($similar_files as $similar_file) { 88 if ($is_dry_run) { 89 $console->writeOut( 90 "%s\n", 91 pht( 92 '%s: Would compact storage with %s.', 93 $monogram, 94 $similar_file->getMonogram())); 95 continue; 96 } 97 98 $console->writeOut( 99 "%s\n", 100 pht( 101 '%s: Compacting storage with %s.', 102 $monogram, 103 $similar_file->getMonogram())); 104 105 $old_instance = null; 106 try { 107 $old_instance = $similar_file->instantiateStorageEngine(); 108 $old_engine = $similar_file->getStorageEngine(); 109 $old_handle = $similar_file->getStorageHandle(); 110 } catch (Exception $ex) { 111 // If the old stuff is busted, we just won't try to delete the 112 // old data. 113 phlog($ex); 114 } 115 116 $similar_file 117 ->setStorageEngine($file->getStorageEngine()) 118 ->setStorageHandle($file->getStorageHandle()) 119 ->save(); 120 121 if ($old_instance) { 122 $similar_file->deleteFileDataIfUnused( 123 $old_instance, 124 $old_engine, 125 $old_handle); 126 } 127 } 128 } 129 130 return 0; 131 } 132 133 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |