| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Icybee package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Olivier Laviale <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Icybee\Modules\Files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ICanBoogie\Updater\AssertionFailed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use ICanBoogie\Updater\Update; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Icybee\Modules\Files\Storage\IndexKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Icybee\Modules\Files\Storage\Pathname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * - Rename table `resources_files` as `files`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @module files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class Update20120101 extends Update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public function update_table_forms() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$db = $this->app->db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		if (!$db->table_exists('resources_files')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			throw new AssertionFailed('assert_table_exists', 'resources_files'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$db("RENAME TABLE `{prefix}resources_files` TO `files`"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	public function update_constructor_type() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$db = $this->app->db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$db("UPDATE {prefix}nodes SET constructor = 'files' WHERE constructor = 'resources.files'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * - Add column `extension`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * @module files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * @property \ICanBoogie\Core|Binding\CoreBindings $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | class Update20150902 extends Update | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	public function update_column_extension() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$model = $this->module->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			->assert_not_has_column('extension') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			->create_column('extension'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$update = $model->prepare("UPDATE {self} SET extension = ? WHERE nid = ?"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		foreach ($model->select("nid, path")->mode(\PDO::FETCH_NUM) as list($nid, $path)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$update('.' . pathinfo($path, PATHINFO_EXTENSION), $nid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	public function update_storage_index() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$model = $this->module->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$model->assert_has_column('path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$storage = $this->app->file_storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$index = $this->app->file_storage_index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$root = $index->root; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		if (!file_exists($root)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			mkdir($root, 0705); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$document_root = rtrim(\ICanBoogie\DOCUMENT_ROOT, DIRECTORY_SEPARATOR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		foreach ($model->select("nid, uuid, path")->mode(\PDO::FETCH_NUM) as list($nid, $uuid, $path)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			$path = $document_root . $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			if (!file_exists($path)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 				echo "!! file $path does not exists.\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			$hash = $storage->add($path)->hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			$index->add(IndexKey::from([ $nid, $uuid, $hash ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$model->remove_column('path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  * - Add column `short_hash`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  * @module files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  * @property \ICanBoogie\Core|Binding\CoreBindings $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | class Update20160709 extends Update | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | 	public function update_column_short_hash() | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | 	{ | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | 		$model = $this->module->model; | 
            
                                                        
            
                                    
            
            
                | 116 |  |  | 		$model | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | 			->assert_not_has_column('short_hash') | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | 			->create_column('short_hash'); | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 120 |  |  | 		$update = $model->prepare("UPDATE {self} SET short_hash = ? WHERE nid = ?"); | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  | 		foreach ($model->all as $record) | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | 		{ | 
            
                                                        
            
                                    
            
            
                | 124 |  |  | 			$update($record->pathname->short_hash, $record->nid); | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  | 
            
                        
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.