| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * The InnoDB indexer is responsible for creating and destroying |
| 30 | * full-text indices on InnoDB classes. |
| 31 | */ |
| 32 | class CRM_Core_InnoDBIndexer { |
| 33 | const IDX_PREFIX = "civicrm_fts_"; |
| 34 | |
| 35 | /** |
| 36 | * @var CRM_Core_InnoDBIndexer |
| 37 | */ |
| 38 | private static $singleton = NULL; |
| 39 | |
| 40 | /** |
| 41 | * @param bool $fresh |
| 42 | * @return CRM_Core_InnoDBIndexer |
| 43 | */ |
| 44 | public static function singleton($fresh = FALSE) { |
| 45 | if ($fresh || self::$singleton === NULL) { |
| 46 | $indices = array( |
| 47 | 'civicrm_address' => array( |
| 48 | array('street_address', 'city', 'postal_code'), |
| 49 | ), |
| 50 | 'civicrm_activity' => array( |
| 51 | array('subject', 'details'), |
| 52 | ), |
| 53 | 'civicrm_contact' => array( |
| 54 | array('sort_name', 'nick_name', 'display_name'), |
| 55 | ), |
| 56 | 'civicrm_contribution' => array( |
| 57 | array('source', 'amount_level', 'trxn_Id', 'invoice_id'), |
| 58 | ), |
| 59 | 'civicrm_email' => array( |
| 60 | array('email'), |
| 61 | ), |
| 62 | 'civicrm_membership' => array( |
| 63 | array('source'), |
| 64 | ), |
| 65 | 'civicrm_note' => array( |
| 66 | array('subject', 'note'), |
| 67 | ), |
| 68 | 'civicrm_participant' => array( |
| 69 | array('source', 'fee_level'), |
| 70 | ), |
| 71 | 'civicrm_phone' => array( |
| 72 | array('phone'), |
| 73 | ), |
| 74 | 'civicrm_tag' => array( |
| 75 | array('name'), |
| 76 | ), |
| 77 | ); |
| 78 | $active = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, 'enable_innodb_fts', NULL, FALSE); |
| 79 | self::$singleton = new self($active, $indices); |
| 80 | } |
| 81 | return self::$singleton; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * (Setting Callback) |
| 86 | * Respond to changes in the "enable_innodb_fts" setting |
| 87 | * |
| 88 | * @param bool $oldValue |
| 89 | * @param bool $newValue |
| 90 | * @param array $metadata |
| 91 | * Specification of the setting (per *.settings.php). |
| 92 | */ |
| 93 | public static function onToggleFts($oldValue, $newValue, $metadata) { |
| 94 | $indexer = CRM_Core_InnoDBIndexer::singleton(); |
| 95 | $indexer->setActive($newValue); |
| 96 | $indexer->fixSchemaDifferences(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @var array (string $table => array $indices) |
| 101 | * |
| 102 | * ex: $indices['civicrm_contact'][0] = array('first_name', 'last_name'); |
| 103 | */ |
| 104 | protected $indices; |
| 105 | |
| 106 | /** |
| 107 | * @var bool |
| 108 | */ |
| 109 | protected $isActive; |
| 110 | |
| 111 | public function __construct($isActive, $indices) { |
| 112 | $this->isActive = $isActive; |
| 113 | $this->indices = $this->normalizeIndices($indices); |
| 114 | } |
| 115 | |
| 116 | public function fixSchemaDifferences() { |
| 117 | // Limitation: This won't pick up stale indices on tables which are not |
| 118 | // declared in $this->indices. That's not much of an issue for now b/c |
| 119 | // we have a static list of tables. |
| 120 | foreach ($this->indices as $tableName => $ign) { |
| 121 | $todoSqls = $this->reconcileIndexSqls($tableName); |
| 122 | foreach ($todoSqls as $todoSql) { |
| 123 | CRM_Core_DAO::executeQuery($todoSql); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Determine if an index is expected to exist |
| 130 | * |
| 131 | * @param string $table |
| 132 | * @param array $fields |
| 133 | * List of field names that must be in the index. |
| 134 | * @return bool |
| 135 | */ |
| 136 | public function hasDeclaredIndex($table, $fields) { |
| 137 | if (!$this->isActive) { |
| 138 | return FALSE; |
| 139 | } |
| 140 | |
| 141 | if (isset($this->indices[$table])) { |
| 142 | foreach ($this->indices[$table] as $idxFields) { |
| 143 | // TODO determine if $idxFields must be exact match or merely a subset |
| 144 | // if (sort($fields) == sort($idxFields)) { |
| 145 | if (array_diff($fields, $idxFields) == array()) { |
| 146 | return TRUE; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return FALSE; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get a list of FTS index names that are currently defined in the database. |
| 156 | * |
| 157 | * @param string $table |
| 158 | * @return array |
| 159 | * (string $indexName => string $indexName) |
| 160 | */ |
| 161 | public function findActualFtsIndexNames($table) { |
| 162 | $mysqlVersion = CRM_Core_DAO::singleValueQuery('SELECT VERSION()'); |
| 163 | if (version_compare($mysqlVersion, '5.6', '<')) { |
| 164 | // If we're not on 5.6+, then there cannot be any InnoDB FTS indices! |
| 165 | // Also: information_schema.innodb_sys_indexes is only available on 5.6+. |
| 166 | return array(); |
| 167 | } |
| 168 | |
| 169 | // Note: this only works in MySQL 5.6, but this whole system is intended to only work in MySQL 5.6 |
| 170 | $sql = " |
| 171 | SELECT i.name as index_name |
| 172 | FROM information_schema.innodb_sys_tables t |
| 173 | JOIN information_schema.innodb_sys_indexes i USING (table_id) |
| 174 | WHERE t.name = concat(database(),'/$table') |
| 175 | AND i.name like '" . self::IDX_PREFIX . "%' |
| 176 | "; |
| 177 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 178 | $indexNames = array(); |
| 179 | while ($dao->fetch()) { |
| 180 | $indexNames[$dao->index_name] = $dao->index_name; |
| 181 | } |
| 182 | return $indexNames; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Generate a "CREATE INDEX" statement for each desired |
| 187 | * FTS index. |
| 188 | * |
| 189 | * @param $table |
| 190 | * @return array |
| 191 | * (string $indexName => string $sql) |
| 192 | */ |
| 193 | public function buildIndexSql($table) { |
| 194 | $sqls = array(); // array (string $idxName => string $sql) |
| 195 | if ($this->isActive && isset($this->indices[$table])) { |
| 196 | foreach ($this->indices[$table] as $fields) { |
| 197 | $name = self::IDX_PREFIX . md5($table . '::' . implode(',', $fields)); |
| 198 | $sqls[$name] = sprintf("CREATE FULLTEXT INDEX %s ON %s (%s)", $name, $table, implode(',', $fields)); |
| 199 | } |
| 200 | } |
| 201 | return $sqls; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Generate a "DROP INDEX" statement for each existing FTS index |
| 206 | * |
| 207 | * @param string $table |
| 208 | * @return array |
| 209 | * (string $idxName => string $sql) |
| 210 | */ |
| 211 | public function dropIndexSql($table) { |
| 212 | $sqls = array(); |
| 213 | $names = $this->findActualFtsIndexNames($table); |
| 214 | foreach ($names as $name) { |
| 215 | $sqls[$name] = sprintf("DROP INDEX %s ON %s", $name, $table); |
| 216 | } |
| 217 | return $sqls; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Construct a set of SQL statements which will create (or preserve) |
| 222 | * required indices and destroy unneeded indices. |
| 223 | * |
| 224 | * @param $table |
| 225 | * @return array |
| 226 | */ |
| 227 | public function reconcileIndexSqls($table) { |
| 228 | $buildIndexSqls = $this->buildIndexSql($table); |
| 229 | $dropIndexSqls = $this->dropIndexSql($table); |
| 230 | |
| 231 | $allIndexNames = array_unique(array_merge( |
| 232 | array_keys($dropIndexSqls), |
| 233 | array_keys($buildIndexSqls) |
| 234 | )); |
| 235 | |
| 236 | $todoSqls = array(); |
| 237 | foreach ($allIndexNames as $indexName) { |
| 238 | if (isset($buildIndexSqls[$indexName]) && isset($dropIndexSqls[$indexName])) { |
| 239 | // already exists |
| 240 | } |
| 241 | elseif (isset($buildIndexSqls[$indexName])) { |
| 242 | $todoSqls[] = $buildIndexSqls[$indexName]; |
| 243 | } |
| 244 | else { |
| 245 | $todoSqls[] = $dropIndexSqls[$indexName]; |
| 246 | } |
| 247 | } |
| 248 | return $todoSqls; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Put the indices into a normalized format |
| 253 | * |
| 254 | * @param $indices |
| 255 | * @return array |
| 256 | */ |
| 257 | public function normalizeIndices($indices) { |
| 258 | $result = array(); |
| 259 | foreach ($indices as $table => $indicesByTable) { |
| 260 | foreach ($indicesByTable as $k => $fields) { |
| 261 | sort($fields); |
| 262 | $result[$table][] = $fields; |
| 263 | } |
| 264 | } |
| 265 | return $result; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @param bool $isActive |
| 270 | */ |
| 271 | public function setActive($isActive) { |
| 272 | $this->isActive = $isActive; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return bool |
| 277 | */ |
| 278 | public function getActive() { |
| 279 | return $this->isActive; |
| 280 | } |
| 281 | |
| 282 | } |