Skip to content

Commit

Permalink
Ensure to remove table prefix only from the beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Jan 31, 2025
1 parent 9c33e51 commit cb9a9f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 4 additions & 6 deletions core/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,16 @@ public static function prefixTables(...$tables)
*/
public static function unprefixTable($table)
{
static $prefixTable = null;
if (is_null($prefixTable)) {
$prefixTable = Config::getInstance()->database['tables_prefix'];
}
$prefixTable = Config::getInstance()->database['tables_prefix'];

if (
empty($prefixTable)
|| strpos($table, $prefixTable) !== 0
) {
return $table;
}
$count = 1;
return str_replace($prefixTable, '', $table, $count);

return substr($table, strlen($prefixTable));
}

/*
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPUnit/Unit/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Piwik\Application\Environment;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Filesystem;
use Piwik\Intl\Data\Provider\RegionDataProvider;
Expand Down Expand Up @@ -624,4 +625,31 @@ public function getLanguageChainTestData(): array
['it', 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7'],
];
}

/**
* @dataProvider getTableNames
*/
public function testUnprefixTable($prefix, $tableName)
{
$originalValue = Config::getInstance()->database['tables_prefix'];

Config::getInstance()->database['tables_prefix'] = $prefix;

$manipluatedName = Common::unprefixTable(Common::prefixTable($tableName));

Config::getInstance()->database['tables_prefix'] = $originalValue;

self::assertEquals($tableName, $manipluatedName);
}

public function getTableNames(): array
{
return [
['matomo_', 'session'],
['blob_', 'archive_blob_2014_07'],
['14_', 'archive_blob_2014_07'],
['ic_', 'archive_numeric_2014_07'],
['log_', 'log_link_visit_action'],
];
}
}

0 comments on commit cb9a9f7

Please sign in to comment.