| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Database Optimize & Repair Tool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright (c) 2013, 2019 Matt Friedman | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license GNU General Public License, version 2 (GPL-2.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace vse\dbtool\console\command\db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use phpbb\console\command\command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use phpbb\db\driver\driver_interface as db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use phpbb\db\tools\tools_interface as phpbb_db_tools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use phpbb\language\language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use phpbb\lock\db as db_lock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use phpbb\user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use vse\dbtool\ext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use vse\dbtool\tool\tool as db_tool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class tool extends command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	/** @var InputInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	protected $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** @var OutputInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	protected $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** @var db */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** @var phpbb_db_tools */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	protected $phpbb_db_tools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** @var db_tool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	protected $db_tool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** @var db_lock */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	protected $db_lock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** @var language */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	protected $language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @param \phpbb\user                       $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * @param \phpbb\db\driver\driver_interface $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param \phpbb\db\tools\tools_interface   $phpbb_db_tools | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param \vse\dbtool\tool\tool             $db_tool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param \phpbb\lock\db                    $db_lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param \phpbb\language\language          $language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	public function __construct(user $user, db $db, phpbb_db_tools $phpbb_db_tools, db_tool $db_tool, db_lock $db_lock, language $language) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$this->db = $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->phpbb_db_tools = $phpbb_db_tools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$this->db_tool = $db_tool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$this->db_lock = $db_lock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->language = $language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->language->add_lang('dbtool_acp', 'vse/dbtool'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		parent::__construct($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			->setName('db:tool') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			->setDescription($this->language->lang('CLI_DBTOOL_EXPLAIN')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				'table', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 				$this->language->lang('CLI_DBTOOL_ARG_TABLE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				'disable-board', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				'D', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 				InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 				$this->language->lang('DISABLE_BOARD_EXPLAIN') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * Execute the DB Tool command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @param InputInterface  $input  An InputInterface instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @param OutputInterface $output An OutputInterface instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * @return int 0 if all is well, 1 if any errors occurred | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$io = new SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		if (!$this->db_tool->is_mysql()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$io->error($this->language->lang('WARNING_MYSQL')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$io->note($this->br2nl($this->language->lang('WARNING_EXPLAIN'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		if ($io->confirm($this->language->lang('CLI_DBTOOL_CONTINUE'))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			$operation = $io->choice($this->language->lang('CLI_DBTOOL_OPERATION'), [ext::OPTIMIZE, ext::REPAIR, ext::CHECK], ext::CHECK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			$disable_board = (int) $input->getOption('disable-board'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			$table = $input->getArgument('table'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			$tables = ($table !== null) ? [$table] : $this->phpbb_db_tools->sql_list_tables(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			if ($this->db_tool->is_valid_operation($operation)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 				if (!$this->db_lock->acquire()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 				{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 					$io->error($this->language->lang('CLI_DBTOOL_LOCK_ERROR', $operation)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 					return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 				$results = $this->db_tool->run($operation, $tables, $disable_board); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 				$this->db_lock->release(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 				$io->success($this->br2nl($this->language->lang($operation . '_SUCCESS'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 				$io->table([$this->language->lang('TH_NAME'), $this->language->lang('CLI_DBTOOL_RESULTS', $operation)], array_map(function ($row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 					return explode(' ... ', $row); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 				}, $results)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * Replace HTML break tags with newlines. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 * Formats lang strings used in the ACP for the CLI. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * @return string|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | 	 */ | 
            
                                                        
            
                                    
            
            
                | 150 |  |  | 	protected function br2nl($text) | 
            
                                                        
            
                                    
            
            
                | 151 |  |  | 	{ | 
            
                                                        
            
                                    
            
            
                | 152 |  |  | 		return preg_replace('/<br(?:\s+)?\/?>/i', "\n", $text); | 
            
                                                        
            
                                    
            
            
                | 153 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  |