3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
18 * This class manages creation and destruction of SQL triggers.
23 * The name of the output file.
30 * Queries written to file when the class is destructed.
34 private $enqueuedQueries = [];
37 * Build a list of triggers via hook and add them to (err, reconcile them
40 * @param string $tableName
41 * the specific table requiring a rebuild; or NULL to rebuild all tables.
46 public function rebuild($tableName = NULL, $force = FALSE) {
49 $logging = new \
CRM_Logging_Schema();
50 $logging->triggerInfo($info, $tableName, $force);
52 \CRM_Core_I18n_Schema
::triggerInfo($info, $tableName);
53 \CRM_Contact_BAO_Contact
::triggerInfo($info, $tableName);
55 \CRM_Utils_Hook
::triggerInfo($info, $tableName);
57 // drop all existing triggers on all tables
58 $logging->dropTriggers($tableName);
60 // now create the set of new triggers
61 $this->createTriggers($info, $tableName);
62 $this->writeEnqueuedQueriesToFile();
67 * per hook_civicrm_triggerInfo.
68 * @param string $onlyTableName
69 * the specific table requiring a rebuild; or NULL to rebuild all tables.
71 public function createTriggers($info, $onlyTableName = NULL) {
72 // Validate info array, should probably raise errors?
73 if (is_array($info) == FALSE) {
79 // now enumerate the tables and the events and collect the same set in a different format
80 foreach ($info as $value) {
82 // clean the incoming data, skip malformed entries
83 // TODO: malformed entries should raise errors or get logged.
84 if (isset($value['table']) == FALSE ||
85 isset($value['event']) == FALSE ||
86 isset($value['when']) == FALSE ||
87 isset($value['sql']) == FALSE
92 if (is_string($value['table']) == TRUE) {
93 $tables = [$value['table']];
96 $tables = $value['table'];
99 if (is_string($value['event']) == TRUE) {
100 $events = [strtolower($value['event'])];
103 $events = array_map('strtolower', $value['event']);
106 $whenName = strtolower($value['when']);
108 foreach ($tables as $tableName) {
109 if (!isset($triggers[$tableName])) {
110 $triggers[$tableName] = [];
113 foreach ($events as $eventName) {
114 $template_params = ['{tableName}', '{eventName}'];
115 $template_values = [$tableName, $eventName];
117 $sql = str_replace($template_params,
121 $variables = str_replace($template_params,
123 \CRM_Utils_Array
::value('variables', $value)
126 if (!isset($triggers[$tableName][$eventName])) {
127 $triggers[$tableName][$eventName] = [];
130 if (!isset($triggers[$tableName][$eventName][$whenName])) {
131 // We're leaving out cursors, conditions, and handlers for now
132 // they are kind of dangerous in this context anyway
133 // better off putting them in stored procedures
134 $triggers[$tableName][$eventName][$whenName] = [
141 $triggers[$tableName][$eventName][$whenName]['variables'][] = $variables;
144 $triggers[$tableName][$eventName][$whenName]['sql'][] = $sql;
149 // Sort tables alphabetically in order to output in a consistent order
150 // for sites that like to diff this output over time
151 // (ie. with the logging_no_trigger_permission setting in place).
153 // now spit out the sql
154 foreach ($triggers as $tableName => $tables) {
155 if ($onlyTableName != NULL && $onlyTableName != $tableName) {
158 foreach ($tables as $eventName => $events) {
159 foreach ($events as $whenName => $parts) {
160 $varString = implode("\n", $parts['variables']);
161 $sqlString = implode("\n", $parts['sql']);
162 $validName = \CRM_Core_DAO
::shortenSQLName($tableName, 48, TRUE);
163 $triggerName = "{$validName}_{$whenName}_{$eventName}";
164 $triggerSQL = "CREATE TRIGGER $triggerName $whenName $eventName ON $tableName FOR EACH ROW BEGIN $varString $sqlString END";
166 $this->enqueueQuery("DROP TRIGGER IF EXISTS $triggerName");
167 $this->enqueueQuery($triggerSQL);
174 * Wrapper function to drop triggers.
176 * @param string $tableName
177 * the specific table requiring a rebuild; or NULL to rebuild all tables.
179 public function dropTriggers($tableName = NULL) {
182 $logging = new \
CRM_Logging_Schema();
183 $logging->triggerInfo($info, $tableName);
185 // drop all existing triggers on all tables
186 $logging->dropTriggers($tableName);
190 * Enqueue a query which alters triggers.
192 * As this requires a high permission level we funnel the queries through here to
193 * facilitate them being taken 'offline'.
195 * @param string $triggerSQL
196 * The sql to run to create or drop the triggers.
197 * @param array $params
198 * Optional parameters to interpolate into the string.
200 public function enqueueQuery($triggerSQL, $params = []) {
201 if (\Civi
::settings()->get('logging_no_trigger_permission')) {
203 if (!file_exists($this->getFile())) {
204 // Ugh. Need to let user know somehow. This is the first change.
205 \CRM_Core_Session
::setStatus(ts('The mysql commands you need to run are stored in %1', [
206 1 => $this->getFile(),
213 $query = \CRM_Core_DAO
::composeQuery($triggerSQL, $params);
214 $this->enqueuedQueries
[$query] = $query;
217 \CRM_Core_DAO
::executeQuery($triggerSQL, $params, TRUE, NULL, FALSE, FALSE);
222 * @return NULL|string
224 public function getFile() {
225 if ($this->file
=== NULL) {
226 $prefix = 'trigger' . \CRM_Utils_Request
::id();
227 $config = \CRM_Core_Config
::singleton();
228 $this->file
= "{$config->configAndLogDir}CiviCRM." . $prefix . md5($config->dsn
) . '.sql';
234 * Write queries to file when the class is destructed.
236 * Note this is already written out for a full rebuild but it is
237 * possible (at least in terms of what is public) to call drop & create
238 * separately so this ensures they are output.
240 public function __destruct() {
241 $this->writeEnqueuedQueriesToFile();
245 * Write queries queued for write-to-file.
247 protected function writeEnqueuedQueriesToFile(): void
{
248 if (!empty($this->enqueuedQueries
) && $this->getFile()) {
249 $buf = "DELIMITER //\n";
250 foreach ($this->enqueuedQueries
as $query) {
251 if (strpos($query, 'CREATE TRIGGER') === 0) {
252 // The create triggers are long so put spaces between them. For the drops
253 // condensed is more readable.
256 $buf .= $query . " //\n";
258 $buf .= "DELIMITER ;\n";
259 file_put_contents($this->getFile(), $buf, FILE_APPEND
);
260 $this->enqueuedQueries
= [];