| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * Precise Similar Topics | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * @copyright (c) 2014 Matt Friedman | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | * @license GNU General Public License, version 2 (GPL-2.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace vse\similartopics\core; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class fulltext_support | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	/** @var \phpbb\db\driver\driver_interface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	protected $engine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	* Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	* @param \phpbb\db\driver\driver_interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	* @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	public function __construct(\phpbb\db\driver\driver_interface $db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		$this->db = $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	* Check if the database is using MySQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	* @return bool True if is mysql, false otherwise | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  | 	*/ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	public function is_mysql() | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 		return ($this->db->get_sql_layer() == 'mysql4' || $this->db->get_sql_layer() == 'mysqli'); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	* Check for FULLTEXT index support | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	* @return bool True if FULLTEXT is supported, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function is_supported() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		// FULLTEXT is supported on InnoDB since MySQL 5.6.4 according to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		// http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		return ($this->get_engine() === 'myisam' || ($this->get_engine() === 'innodb' && phpbb_version_compare($this->db->sql_server_info(true), '5.6.4', '>='))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	* Get the database storage engine name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	* @return string The storage engine name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function get_engine() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		return (isset($this->engine)) ? $this->engine : $this->set_engine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	* Set the database storage engine name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	* @return string The storage engine name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	public function set_engine() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$this->engine = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		if ($this->is_mysql()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			$info = $this->get_table_info(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			// Modern MySQL uses 'Engine', but older may still use 'Type' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			foreach (array('Engine', 'Type') as $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 				if (isset($info[$name])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 					$this->engine = strtolower($info[$name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 					break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		return $this->engine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	* Check if a field is a FULLTEXT index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	* @param string $field name of a field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	* @return bool True if field is a FULLTEXT index, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	public function index($field = 'topic_title') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$sql = 'SHOW INDEX | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			FROM ' . TOPICS_TABLE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$result = $this->db->sql_query($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		while ($row = $this->db->sql_fetchrow($result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			// deal with older MySQL versions which didn't use Index_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			$index_type = (isset($row['Index_type'])) ? $row['Index_type'] : $row['Comment']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			if ($index_type == 'FULLTEXT' && $row['Key_name'] == $field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 				return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$this->db->sql_freeresult($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * Get topics table information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 * @return mixed Array with the table info, false if the table does not exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	protected function get_table_info() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$result = $this->db->sql_query('SHOW TABLE STATUS LIKE \'' . TOPICS_TABLE . '\''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$info = $this->db->sql_fetchrow($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		$this->db->sql_freeresult($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		return $info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |