Skip to content

Commit cc873fc

Browse files
michalkleinernathangavinmneudert
authored
Migrate annotations to a separate db table, update model, update tests (#23564)
* Create a model for annotations in a separate db table * Add index in a separate db call * Update version to 5.5.0-b2 * Update controller to work with slightly changed API * Update test suite and API to work together correctly * Use custom migrations to migrate and purge annotations * Adjust sanitisation to sanitise on annotation output * Update PHPStan baseline * Add system test for annotation add * Allow to test APIs without providing a date * Add system test for Annotations.getAll API call when no date is provided * Ensure we handle today/yesterday dates correctly when adding/saving annotations * Add tests for today/yesterday date handling * Add annotations with potentially harmful characters/strings * Add annotations UI test to cover potentially dangerous characters --------- Co-authored-by: Nathan Gavin <nathangavin987@gmail.com> Co-authored-by: Marc Neudert <marc@innocraft.com>
1 parent 4e283a9 commit cc873fc

38 files changed

Lines changed: 1429 additions & 728 deletions

core/Common.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,4 +1246,24 @@ protected static function checkValidLanguagesIsSet($validLanguages)
12461246
}
12471247
return $validLanguages;
12481248
}
1249+
1250+
/**
1251+
* Flatten variously nested arrays into a single flat list of values
1252+
*
1253+
* @param array $array
1254+
* @return array
1255+
*/
1256+
public static function flattenArray(array $array): array
1257+
{
1258+
$result = [];
1259+
foreach ($array as $value) {
1260+
if (is_array($value)) {
1261+
$result = array_merge($result, static::flattenArray($value));
1262+
} else {
1263+
$result[] = $value;
1264+
}
1265+
}
1266+
1267+
return $result;
1268+
}
12491269
}

core/Db/Schema/Mysql.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,17 @@ public function getTablesCreateSql()
393393
UNIQUE KEY unique_plugin_version_title (`plugin_name`, `version`, `title`(100))
394394
) $tableOptions
395395
",
396+
'annotations' => "CREATE TABLE `{$prefixTables}annotations` (
397+
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
398+
`idsite` INTEGER UNSIGNED NOT NULL,
399+
`date` DATETIME NOT NULL,
400+
`note` TEXT NOT NULL,
401+
`starred` TINYINT(1) NOT NULL DEFAULT 0,
402+
`user` VARCHAR(100) NOT NULL,
403+
PRIMARY KEY(`id`),
404+
INDEX index_idsite_date (`idsite`, `date`)
405+
) $tableOptions
406+
",
396407
);
397408

398409
return $tables;

core/Piwik.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ public static function isUserHasWriteAccess($idSites)
599599
/**
600600
* Checks that the current user has view access to the requested list of sites
601601
*
602-
* @param int|array $idSites The list of site IDs to check access for.
602+
* @param int|array|string $idSites The list of site IDs to check access for.
603603
* @throws Exception if the current user does not have view access to every site in the list.
604604
* @api
605605
*/

core/Site.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public function getCreatorLogin()
418418
* @param string|array $ids Comma separated idSite list, eg, `'1,2,3,4'` or an array of IDs, eg,
419419
* `array(1, 2, 3, 4)`.
420420
* @param bool|string $_restrictSitesToLogin Implementation detail. Used only when running as a scheduled task.
421-
* @return array An array of valid, unique integers.
421+
* @return array<string>|array<int> An array of valid, unique integers.
422422
*/
423423
public static function getIdSitesFromIdSitesString($ids, $_restrictSitesToLogin = false)
424424
{

core/Updates/5.5.0-b2.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
namespace Piwik\Updates;
11+
12+
use Piwik\Plugins\CoreAdminHome\Commands\MigrateAnnotations;
13+
use Piwik\Plugins\CoreAdminHome\Commands\PurgeLegacyAnnotations;
14+
use Piwik\Updater;
15+
use Piwik\Updater\Migration\Custom as CustomMigration;
16+
use Piwik\Updates as PiwikUpdates;
17+
use Piwik\Updater\Migration;
18+
19+
/**
20+
* Update for version 5.5.0-b2
21+
*/
22+
class Updates_5_5_0_b2 extends PiwikUpdates
23+
{
24+
/**
25+
* Migrations
26+
*
27+
* @param Updater $updater
28+
*
29+
* @return Migration[]
30+
*/
31+
public function getMigrations(Updater $updater)
32+
{
33+
$migrations = [];
34+
35+
$migrateCmd = './console core:matomo550-migrate-annotations';
36+
$annotationsMigrate = new CustomMigration([MigrateAnnotations::class, 'migrate'], $migrateCmd);
37+
$migrations[] = $annotationsMigrate;
38+
39+
$purgeCmd = './console core:matomo550-purge-legacy-annotations';
40+
$annotationsPurge = new CustomMigration([PurgeLegacyAnnotations::class, 'purge'], $purgeCmd);
41+
$migrations[] = $annotationsPurge;
42+
43+
return $migrations;
44+
}
45+
46+
public function doUpdate(Updater $updater)
47+
{
48+
$updater->executeMigrations(__FILE__, $this->getMigrations($updater));
49+
}
50+
}

core/Version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class Version
2222
* The current Matomo version.
2323
* @var string
2424
*/
25-
public const VERSION = '5.5.0-b1';
25+
public const VERSION = '5.5.0-b2';
2626

2727
public const MAJOR_VERSION = 5;
2828

phpstan-baseline.neon

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,31 +3718,6 @@ parameters:
37183718
count: 1
37193719
path: plugins/Actions/VisitorDetails.php
37203720

3721-
-
3722-
message: "#^PHPDoc tag @param for parameter \\$lastN with type bool\\|int is not subtype of native type int\\|null\\.$#"
3723-
count: 1
3724-
path: plugins/Annotations/API.php
3725-
3726-
-
3727-
message: "#^Parameter \\#1 \\$idSites of static method Piwik\\\\Piwik\\:\\:checkUserHasViewAccess\\(\\) expects array\\|int, string given\\.$#"
3728-
count: 2
3729-
path: plugins/Annotations/API.php
3730-
3731-
-
3732-
message: "#^Parameter \\#5 \\$starred of method Piwik\\\\Plugins\\\\Annotations\\\\AnnotationList\\:\\:update\\(\\) expects int\\|null, bool\\|null given\\.$#"
3733-
count: 1
3734-
path: plugins/Annotations/API.php
3735-
3736-
-
3737-
message: "#^Cannot call method getTimestamp\\(\\) on string\\|false\\.$#"
3738-
count: 1
3739-
path: plugins/Annotations/AnnotationList.php
3740-
3741-
-
3742-
message: "#^Method Piwik\\\\Plugins\\\\Annotations\\\\Annotations\\:\\:getDateRangeForPeriod\\(\\) should return array\\<Piwik\\\\Date\\> but returns array\\<int, false\\>\\.$#"
3743-
count: 1
3744-
path: plugins/Annotations/Annotations.php
3745-
37463721
-
37473722
message: "#^Parameter \\#1 \\$array of function reset is passed by reference, so it expects variables only\\.$#"
37483723
count: 1
@@ -4858,11 +4833,6 @@ parameters:
48584833
count: 1
48594834
path: plugins/Events/VisitorDetails.php
48604835

4861-
-
4862-
message: "#^Parameter \\#1 \\$idSites of static method Piwik\\\\Piwik\\:\\:checkUserHasViewAccess\\(\\) expects array\\|int, string given\\.$#"
4863-
count: 3
4864-
path: plugins/ExamplePlugin/API.php
4865-
48664836
-
48674837
message: "#^Parameter \\#3 \\$escapeComment of static method Piwik\\\\Plugins\\\\Diagnostics\\\\Diagnostic\\\\DiagnosticResult\\:\\:informationalResult\\(\\) expects bool, string given\\.$#"
48684838
count: 1
@@ -5958,11 +5928,6 @@ parameters:
59585928
count: 1
59595929
path: plugins/Referrers/API.php
59605930

5961-
-
5962-
message: "#^Parameter \\#1 \\$idSites of static method Piwik\\\\Piwik\\:\\:checkUserHasViewAccess\\(\\) expects array\\|int, string given\\.$#"
5963-
count: 3
5964-
path: plugins/Referrers/API.php
5965-
59665931
-
59675932
message: "#^Parameter \\#1 \\$name of method Piwik\\\\DataTable\\:\\:getColumn\\(\\) expects string, int given\\.$#"
59685933
count: 1
@@ -6688,11 +6653,6 @@ parameters:
66886653
count: 1
66896654
path: plugins/VisitTime/API.php
66906655

6691-
-
6692-
message: "#^Parameter \\#1 \\$idSites of static method Piwik\\\\Piwik\\:\\:checkUserHasViewAccess\\(\\) expects array\\|int, string given\\.$#"
6693-
count: 1
6694-
path: plugins/VisitTime/API.php
6695-
66966656
-
66976657
message: "#^Parameter \\#1 \\$idsite of static method Piwik\\\\Site\\:\\:getTimezoneFor\\(\\) expects int, string given\\.$#"
66986658
count: 1

0 commit comments

Comments
 (0)