Skip to content

Commit c973567

Browse files
sgiehldiosmosis
andauthored
Update PHP extension requirements & deprecate Common::mb_* methods (matomo-org#16754)
* Require polyfill for mbstring and iconv * remove mbstring methods from upgrade.php * remove checks for some php extensions * deprecate Common::mb_* methods, as we can directly use mb_* functions instead * updates expected test files * Directly use mb_* methods in favor of Common::mb_* * Update expected screenshot Co-authored-by: diosmosis <[email protected]>
1 parent 70b05de commit c973567

File tree

52 files changed

+179
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+179
-160
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"symfony/console": "~2.6.0",
5454
"symfony/event-dispatcher": "~2.6.0",
5555
"symfony/monolog-bridge": "~2.6.0",
56+
"symfony/polyfill-iconv": "^1.20",
57+
"symfony/polyfill-mbstring": "^1.20",
5658
"szymach/c-pchart": "^2.0",
5759
"tecnickcom/tcpdf": "~6.0",
5860
"tedivm/jshrink": "~v1.4.0",

composer.lock

+81-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/API/ApiRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function buildDataTableRenderer($dataTable)
120120
*/
121121
public static function factory($format, $request)
122122
{
123-
if (Common::mb_strtolower($format) === 'json2') {
123+
if (mb_strtolower($format) === 'json2') {
124124
$format = 'json';
125125
}
126126
$formatToCheck = '\\' . ucfirst(strtolower($format));

core/CliMulti/Output.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public function get()
6363
$search = '#!/usr/bin/env php';
6464
if (!empty($content)
6565
&& is_string($content)
66-
&& Common::mb_substr(trim($content), 0, strlen($search)) === $search) {
67-
$content = trim(Common::mb_substr(trim($content), strlen($search)));
66+
&& mb_substr(trim($content), 0, strlen($search)) === $search) {
67+
$content = trim(mb_substr(trim($content), strlen($search)));
6868
}
6969
return $content;
7070
}

core/Common.php

+10-32
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,13 @@ public static function isRunningConsoleCommand()
184184
*
185185
* @param string $string
186186
* @param int $start
187-
* @param int ... optional length
187+
* @param int|null $length optional length
188188
* @return string
189-
* @api
189+
* @deprecated since 4.4 - directly use mb_substr instead
190190
*/
191-
public static function mb_substr($string, $start)
191+
public static function mb_substr($string, $start, $length = null)
192192
{
193-
$length = func_num_args() > 2
194-
? func_get_arg(2)
195-
: self::mb_strlen($string);
196-
197-
if (function_exists('mb_substr')) {
198-
return mb_substr($string, $start, $length, 'UTF-8');
199-
}
200-
201-
return substr($string, $start, $length);
193+
return mb_substr($string, $start, $length, 'UTF-8');
202194
}
203195

204196
/**
@@ -233,15 +225,11 @@ public static function getProcessId()
233225
*
234226
* @param string $string
235227
* @return int
236-
* @api
228+
* @deprecated since 4.4 - directly use mb_strlen instead
237229
*/
238230
public static function mb_strlen($string)
239231
{
240-
if (function_exists('mb_strlen')) {
241-
return mb_strlen($string, 'UTF-8');
242-
}
243-
244-
return strlen($string);
232+
return mb_strlen($string, 'UTF-8');
245233
}
246234

247235
/**
@@ -251,16 +239,11 @@ public static function mb_strlen($string)
251239
*
252240
* @param string $string
253241
* @return string
254-
* @api
242+
* @deprecated since 4.4 - directly use mb_strtolower instead
255243
*/
256244
public static function mb_strtolower($string)
257245
{
258-
if (function_exists('mb_strtolower')) {
259-
return mb_strtolower($string, 'UTF-8');
260-
}
261-
262-
// return unchanged string as using `strtolower` might cause unicode problems
263-
return $string;
246+
return mb_strtolower($string, 'UTF-8');
264247
}
265248

266249
/**
@@ -270,16 +253,11 @@ public static function mb_strtolower($string)
270253
*
271254
* @param string $string
272255
* @return string
273-
* @api
256+
* @deprecated since 4.4 - directly use mb_strtoupper instead
274257
*/
275258
public static function mb_strtoupper($string)
276259
{
277-
if (function_exists('mb_strtoupper')) {
278-
return mb_strtoupper($string, 'UTF-8');
279-
}
280-
281-
// return unchanged string as using `strtoupper` might cause unicode problems
282-
return $string;
260+
return mb_strtoupper($string, 'UTF-8');
283261
}
284262

285263
/**

core/Concurrency/Lock.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ public function acquireLock($id, $ttlInSeconds = 60)
8282
{
8383
$this->lockKey = $this->lockKeyStart . $id;
8484

85-
if (Common::mb_strlen($this->lockKey) > self::MAX_KEY_LEN) {
85+
if (mb_strlen($this->lockKey) > self::MAX_KEY_LEN) {
8686
// Lock key might be too long for DB column, so we hash it but leave the start of the original as well
8787
// to make it more readable
8888
$md5Len = 32;
89-
$this->lockKey = Common::mb_substr($id, 0, self::MAX_KEY_LEN - $md5Len - 1) . md5($id);
89+
$this->lockKey = mb_substr($id, 0, self::MAX_KEY_LEN - $md5Len - 1) . md5($id);
9090
}
9191

9292
$lockValue = substr(Common::generateUniqId(), 0, 12);

core/CronArchive.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -673,11 +673,11 @@ public function logError($m)
673673
{
674674
if (!defined('PIWIK_ARCHIVE_NO_TRUNCATE')) {
675675
$m = str_replace(array("\n", "\t"), " ", $m);
676-
if (Common::mb_strlen($m) > self::TRUNCATE_ERROR_MESSAGE_SUMMARY) {
676+
if (mb_strlen($m) > self::TRUNCATE_ERROR_MESSAGE_SUMMARY) {
677677
$numCharactersKeepFromEnd = 100;
678-
$m = Common::mb_substr($m, 0, self::TRUNCATE_ERROR_MESSAGE_SUMMARY - $numCharactersKeepFromEnd)
678+
$m = mb_substr($m, 0, self::TRUNCATE_ERROR_MESSAGE_SUMMARY - $numCharactersKeepFromEnd)
679679
. ' ... ' .
680-
Common::mb_substr($m, -1 * $numCharactersKeepFromEnd);
680+
mb_substr($m, -1 * $numCharactersKeepFromEnd);
681681
}
682682
}
683683
$this->errors[] = $m;

core/DataAccess/LogAggregator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ public function getSegmentTmpTableName()
212212
$bind = $this->getGeneralQueryBindParams();
213213
$tableName = self::LOG_TABLE_SEGMENT_TEMPORARY_PREFIX . md5(json_encode($bind) . $this->segment->getString());
214214

215-
$lengthPrefix = Common::mb_strlen(Common::prefixTable(''));
215+
$lengthPrefix = mb_strlen(Common::prefixTable(''));
216216
$maxLength = Db\Schema\Mysql::MAX_TABLE_NAME_LENGTH - $lengthPrefix;
217217

218-
return Common::mb_substr($tableName, 0, $maxLength);
218+
return mb_substr($tableName, 0, $maxLength);
219219
}
220220

221221
public function cleanup()

core/Date.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ public function getLocalized($template, $ucfirst = true)
800800
}
801801

802802
if ($ucfirst) {
803-
$out = Common::mb_strtoupper(Common::mb_substr($out, 0, 1)) . Common::mb_substr($out, 1);
803+
$out = mb_strtoupper(mb_substr($out, 0, 1)) . mb_substr($out, 1);
804804
}
805805

806806
return $out;

core/DeviceDetector/DeviceDetectorFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function makeInstance($userAgent)
3939

4040
public static function getNormalizedUserAgent($userAgent)
4141
{
42-
return Common::mb_substr(trim($userAgent), 0, 500);
42+
return mb_substr(trim($userAgent), 0, 500);
4343
}
4444

4545
/**

core/Tracker/Failures.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function getParamsWithTokenAnonymized(Request $request)
101101
if (!empty($token) && $value === $token) {
102102
$params[$key] = '__TOKEN_AUTH__'; // user accidentally posted the token in a wrong field
103103
} elseif (!empty($value) && is_string($value)
104-
&& Common::mb_strlen($value) >= 29 && Common::mb_strlen($value) <= 36
104+
&& mb_strlen($value) >= 29 && mb_strlen($value) <= 36
105105
&& ctype_xdigit($value)) {
106106
$params[$key] = '__TOKEN_AUTH__'; // user maybe posted a token in a different field... it looks like it might be a token
107107
}

core/Tracker/GoalManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ protected function recordStandardGoals(VisitProperties $visitProperties, Request
731731
if (empty($lastActionTime)) {
732732
$conversion['buster'] = $this->makeRandomMySqlUnsignedInt(10);
733733
} else {
734-
$conversion['buster'] = $this->makeRandomMySqlUnsignedInt(2) . Common::mb_substr($visitProperties->getProperty('visit_last_action_time'), 2);
734+
$conversion['buster'] = $this->makeRandomMySqlUnsignedInt(2) . mb_substr($visitProperties->getProperty('visit_last_action_time'), 2);
735735
}
736736
}
737737

core/Tracker/PageUrl.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected static function cleanupHostAndHashTag($parsedUrl, $idSite = false)
179179
}
180180

181181
if (!empty($parsedUrl['host'])) {
182-
$parsedUrl['host'] = Common::mb_strtolower($parsedUrl['host']);
182+
$parsedUrl['host'] = mb_strtolower($parsedUrl['host']);
183183
}
184184

185185
if (!empty($parsedUrl['fragment'])) {
@@ -365,7 +365,7 @@ public static function shouldUseHttpsHost($idSite, $host)
365365
$hostSiteCache = false;
366366

367367
foreach ($siteUrlCache as $siteUrl) {
368-
if (strpos(Common::mb_strtolower($siteUrl), Common::mb_strtolower('https://' . $host)) === 0) {
368+
if (strpos(mb_strtolower($siteUrl), mb_strtolower('https://' . $host)) === 0) {
369369
$hostSiteCache = true;
370370
break;
371371
}

core/Tracker/Request.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ public function isRequestExcluded()
262262
$valueRightMember = urldecode($matches[3]);
263263
}
264264
$actual = Common::getRequestVar($leftMember, '', 'string', $this->params);
265-
$actual = Common::mb_strtolower($actual);
266-
$valueRightMember = Common::mb_strtolower($valueRightMember);
265+
$actual = mb_strtolower($actual);
266+
$valueRightMember = mb_strtolower($valueRightMember);
267267
switch ($operation) {
268268
case SegmentExpression::MATCH_EQUAL:
269269
if ($actual === $valueRightMember) {

core/Tracker/Visit.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public static function isHostKnownAliasHost($urlHost, $idSite)
411411

412412
private static function toCanonicalHost($host)
413413
{
414-
$hostLower = Common::mb_strtolower($host);
414+
$hostLower = mb_strtolower($host);
415415
return str_replace('www.', '', $hostLower);
416416
}
417417

core/Twig.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
function piwik_filter_truncate($string, $size)
2828
{
29-
if (Common::mb_strlen(html_entity_decode($string)) <= $size) {
29+
if (mb_strlen(html_entity_decode($string)) <= $size) {
3030
return $string;
3131
} else {
3232
preg_match('/^(&(?:[a-z\d]+|#\d+|#x[a-f\d]+);|.){'.$size.'}/i', $string, $shortenString);

core/Updates/4.0.0-b1.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function doUpdate(Updater $updater)
238238
$sessions = Db::fetchAll('SELECT id from ' . Common::prefixTable('session'));
239239

240240
foreach ($sessions as $session) {
241-
if (!empty($session['id']) && Common::mb_strlen($session['id']) != 128) {
241+
if (!empty($session['id']) && mb_strlen($session['id']) != 128) {
242242
$bind = [ hash('sha512', $session['id'] . $salt), $session['id'] ];
243243
try {
244244
Db::query(sprintf('UPDATE %s SET id = ? WHERE id = ?', Common::prefixTable('session')), $bind);

core/Url.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ public static function isValidHost($host = false)
240240
}
241241
$trustedHosts = str_replace("/", "\\/", $trustedHosts);
242242

243-
$untrustedHost = Common::mb_strtolower($host);
243+
$untrustedHost = mb_strtolower($host);
244244
$untrustedHost = rtrim($untrustedHost, '.');
245245

246-
$hostRegex = Common::mb_strtolower('/(^|.)' . implode('$|', $trustedHosts) . '$/');
246+
$hostRegex = mb_strtolower('/(^|.)' . implode('$|', $trustedHosts) . '$/');
247247

248248
$result = preg_match($hostRegex, $untrustedHost);
249249
return 0 !== $result;
@@ -666,7 +666,7 @@ public static function getHostFromUrl($url)
666666
return;
667667
}
668668

669-
return Common::mb_strtolower($parsedUrl['host']);
669+
return mb_strtolower($parsedUrl['host']);
670670
}
671671

672672
/**
@@ -684,11 +684,11 @@ public static function isHostInUrls($host, $urls)
684684
return false;
685685
}
686686

687-
$host = Common::mb_strtolower($host);
687+
$host = mb_strtolower($host);
688688

689689
if (!empty($urls)) {
690690
foreach ($urls as $url) {
691-
if (Common::mb_strtolower($url) === $host) {
691+
if (mb_strtolower($url) === $host) {
692692
return true;
693693
}
694694

core/Validators/CharacterLength.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function validate($value)
4444
return;
4545
}
4646

47-
$lenValue = Common::mb_strlen($value);
47+
$lenValue = mb_strlen($value);
4848

4949
if (isset($this->min) && $this->min > $lenValue) {
5050
throw new Exception(Piwik::translate('General_ValidatorErrorCharacterTooShort', array($lenValue, $this->min)));

0 commit comments

Comments
 (0)