Skip to content

Commit 40a4426

Browse files
Merge pull request #8502 from Sesquipedalian/3.0/msg_topic_save
[3.0] Uses a more OOP-like approach to saving messages and topics to the database
2 parents b4091d9 + 855ef98 commit 40a4426

File tree

12 files changed

+755
-362
lines changed

12 files changed

+755
-362
lines changed

Sources/Actions/Admin/Permissions.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,7 @@ public static function loadIllegalPermissions(): array
23682368

23692369
self::$illegal = array_unique(self::$illegal);
23702370

2371+
// Call the deprecated integrate_load_illegal_permissions hook.
23712372
self::integrateLoadIllegalPermissions();
23722373

23732374
return self::$illegal;
@@ -3208,7 +3209,7 @@ protected static function loadAllPermissions(): void
32083209
// We also need to know which groups can't be given the bbc_html permission.
32093210
self::loadIllegalBBCHtmlGroups();
32103211

3211-
// Backward compatibility with the integrate_load_permissions hook.
3212+
// Call the deprecated integrate_load_permissions hook.
32123213
self::integrateLoadPermissions();
32133214

32143215
// Figure out which permissions should be hidden.
@@ -3420,7 +3421,7 @@ protected static function updateBoardManagers(): void
34203421
protected static function integrateLoadPermissions(): void
34213422
{
34223423
// Don't bother if nothing is using this hook.
3423-
if (empty(Config::$modSettings['integrate_load_permissions'])) {
3424+
if (empty(Config::$backward_compatibility) || empty(Config::$modSettings['integrate_load_permissions'])) {
34243425
return;
34253426
}
34263427

@@ -3506,7 +3507,7 @@ protected static function integrateLoadPermissions(): void
35063507
protected static function integrateLoadIllegalPermissions(): void
35073508
{
35083509
// Don't bother if nothing is using this hook.
3509-
if (empty(Config::$modSettings['integrate_load_illegal_permissions'])) {
3510+
if (empty(Config::$backward_compatibility) || empty(Config::$modSettings['integrate_load_illegal_permissions'])) {
35103511
return;
35113512
}
35123513

@@ -3546,7 +3547,7 @@ protected static function integrateLoadIllegalPermissions(): void
35463547
protected static function integrateLoadIllegalGuestPermissions(): void
35473548
{
35483549
// Don't bother if nothing is using this hook.
3549-
if (empty(Config::$modSettings['integrate_load_illegal_guest_permissions'])) {
3550+
if (empty(Config::$backward_compatibility) || empty(Config::$modSettings['integrate_load_illegal_guest_permissions'])) {
35503551
return;
35513552
}
35523553

Sources/Actions/Display.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,14 @@ protected function buildButtons(): void
13601360
}
13611361

13621362
// Allow adding new buttons easily.
1363-
// Note: Utils::$context['normal_buttons'] and Utils::$context['mod_buttons'] are added for backward compatibility with 2.0, but are deprecated and should not be used
1363+
// MOD AUTHORS: A future version of SMF will stop passing Utils::$context['normal_buttons'] to this hook.
1364+
// You should just interact with Utils::$context['normal_buttons'] directly in your hooked code.
13641365
IntegrationHook::call('integrate_display_buttons', [&Utils::$context['normal_buttons']]);
1365-
// Note: integrate_mod_buttons is no longer necessary and deprecated, but is kept for backward compatibility with 2.0
1366-
IntegrationHook::call('integrate_mod_buttons', [&Utils::$context['mod_buttons']]);
1366+
1367+
// NOD AUTHORS: integrate_mod_buttons is deprecated. Use integrate_display_buttons instead.
1368+
if (!empty(Config::$backward_compatibility)) {
1369+
IntegrationHook::call('integrate_mod_buttons', [&Utils::$context['mod_buttons']]);
1370+
}
13671371

13681372
// If any buttons have a 'test' check, run those tests now to keep things clean.
13691373
foreach (['normal_buttons', 'mod_buttons'] as $button_strip) {

Sources/Actions/MessageIndex.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,8 @@ protected function buildButtons(): void
11191119
}
11201120

11211121
// Allow adding new buttons easily.
1122-
// Note: Utils::$context['normal_buttons'] is added for backward compatibility with 2.0, but is deprecated and should not be used
1122+
// MOD AUTHORS: A future version of SMF will stop passing Utils::$context['normal_buttons'] to this hook.
1123+
// You should just interact with Utils::$context['normal_buttons'] directly in your hooked code.
11231124
IntegrationHook::call('integrate_messageindex_buttons', [&Utils::$context['normal_buttons']]);
11241125
}
11251126
}

Sources/Actions/Moderation/Home.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,14 @@ protected function reportedMembers(): void
537537
/**
538538
* Provides a home for the deprecated integrate_mod_centre_blocks hook.
539539
*
540-
* MOD AUTHORS: Please use the integrate_moderation_home_blocks instead.
540+
* MOD AUTHORS: Please use integrate_moderation_home_blocks instead.
541541
*/
542542
protected static function integrateModBlocks(): void
543543
{
544+
if (empty(Config::$backward_compatibility)) {
545+
return;
546+
}
547+
544548
$valid_blocks = [];
545549

546550
IntegrationHook::call('integrate_mod_centre_blocks', [&$valid_blocks]);

Sources/Actions/Profile/Notification.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,6 @@ public function configuration(bool $defaultSettings = false): void
415415
}
416416
}
417417

418-
// Now, now, we could pass this through global but we should really get into the habit of
419-
// passing content to hooks, not expecting hooks to splatter everything everywhere.
420418
IntegrationHook::call('integrate_alert_types', [&$this->alert_types, &$this->group_options]);
421419

422420
// Now we have to do some permissions testing - but only if we're not loading this from the admin center

Sources/Db/DatabaseApi.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use SMF\BackwardCompatibility;
1919
use SMF\Config;
2020
use SMF\ErrorHandler;
21+
use SMF\IP;
2122
use SMF\Utils;
23+
use SMF\Uuid;
2224

2325
/**
2426
* Class DatabaseApi
@@ -304,6 +306,113 @@ abstract class DatabaseApi
304306
'user_likes',
305307
];
306308

309+
/****************
310+
* Public methods
311+
****************/
312+
313+
/**
314+
* Figures out the best type indicators to use in SMF's query placeholder
315+
* strings and/or insert column type definitions for a given set of columns.
316+
*
317+
* In most cases, this is simply a matter of looking up the expected data
318+
* type for the target column and then translating it to the type indicator
319+
* string that Db::$db->quote() and Db::$db->insert() would expect for that
320+
* column's data type. There are a couple of special cases that we need to
321+
* check for, though, so this method takes care of that.
322+
*
323+
* Calling this method is usually only necessary if we are trying to save
324+
* values to custom columns that were added by mods.
325+
*
326+
* @param string $table_name The name of a table.
327+
* @param array $column_values Array where keys are possible column names
328+
* and values are the values we plan to set.
329+
* @return array List of recognized column names and their corresponding
330+
* data type indicators.
331+
*/
332+
public function getTypeIndicators(string $table_name, array $column_values): array
333+
{
334+
$columns = $this->list_columns($table_name, true);
335+
336+
$types = [];
337+
338+
foreach ($column_values as $column_name => $value) {
339+
if (!isset($columns[$column_name])) {
340+
continue;
341+
}
342+
343+
switch (strtolower($columns[$column_name]['type'])) {
344+
case 'decimal':
345+
case 'numeric':
346+
case 'float':
347+
case 'double':
348+
case 'real':
349+
case 'double precision':
350+
case 'money':
351+
$type = 'float';
352+
break;
353+
354+
case 'integer':
355+
case 'int':
356+
case 'tinyint':
357+
case 'smallint':
358+
case 'mediumint':
359+
case 'bigint':
360+
case 'bool':
361+
case 'boolean':
362+
$type = 'int';
363+
break;
364+
365+
case 'year':
366+
$type = 'int';
367+
break;
368+
369+
case 'datetime':
370+
case 'timestamp':
371+
$type = 'datetime';
372+
break;
373+
374+
case 'date':
375+
$type = 'date';
376+
break;
377+
378+
case 'time':
379+
$type = 'time';
380+
break;
381+
382+
case 'inet':
383+
$type = 'inet';
384+
break;
385+
386+
case 'uuid':
387+
$type = 'uuid';
388+
break;
389+
390+
case 'json':
391+
case 'enum':
392+
case 'set':
393+
$type = 'string';
394+
break;
395+
396+
default:
397+
$test = is_array($value) ? reset($value) : $value;
398+
399+
if (IP::create($test)->isValid()) {
400+
$types[$column_name] = 'inet';
401+
} elseif ($test instanceof Uuid || (string) (@Uuid::createFromString($test)) !== Uuid::NIL_UUID) {
402+
$types[$column_name] = 'uuid';
403+
} else {
404+
$type = 'string';
405+
}
406+
407+
break;
408+
}
409+
410+
$types[$column_name] = $type;
411+
}
412+
413+
return $types;
414+
}
415+
307416
/***********************
308417
* Public static methods
309418
***********************/

Sources/Forum.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,15 @@ public function __construct()
416416

417417
// Allow modifying $unlogged_actions easily.
418418
// Deprecated: Implement ActionInterface::isSimpleAction() instead of this hook.
419-
IntegrationHook::call('integrate_pre_log_stats', [&self::$unlogged_actions]);
419+
if (!empty(Config::$backward_compatibility)) {
420+
IntegrationHook::call('integrate_pre_log_stats', [&self::$unlogged_actions]);
421+
}
420422

421423
// Allow modifying $guest_access_actions easily.
422424
// Deprecated: Implement ActionInterface::isRestrictedGuestAccessAllowed() instead of this hook.
423-
IntegrationHook::call('integrate_guest_actions', [&self::$guest_access_actions]);
425+
if (!empty(Config::$backward_compatibility)) {
426+
IntegrationHook::call('integrate_guest_actions', [&self::$guest_access_actions]);
427+
}
424428
}
425429

426430
/**

0 commit comments

Comments
 (0)