Page MenuHomePhabricator

PHP 8.1 Deprecation errors for fields being null
Closed, ResolvedPublicBUG REPORT

Description

ME 1.39.6
PHP 8.1.27
Cargo @fc658e0df5c2369090a7d37970432f2d2124017c

The following four Cargo deprecation errors appear in my logs. I added a patch with a workaround solution that solves them in my case. I figured, you can spot the affected code more easily this way. Essentially they all share the fact that a cargo field value is null. I did not inspect any further, if you need to know how to actually reproduce all three of them, please let me know.

edit: the following function calls throw the deprecation errors for passing a param as null

  • CargoListFormat.php(37): trim() param #1
  • CargoTemplateFormat.php(38): str_replace() param #3
  • CargoTemplateFormat.php(32): htmlspecialchars_decode() param #1
  • CargoStore.php(396): explode() param #2
diff --git a/includes/formats/CargoListFormat.php b/includes/formats/CargoListFormat.php
index a8869fc..aa815fd 100644
--- a/includes/formats/CargoListFormat.php
+++ b/includes/formats/CargoListFormat.php
@@ -33,7 +33,7 @@ class CargoListFormat extends CargoDisplayFormat {
 			if ( !array_key_exists( $fieldName, $row ) ) {
 				continue;
 			}
-			$fieldValue = $row[$fieldName];
+			$fieldValue = is_null($row[$fieldName]) ? '' : $row[$fieldName];
 			if ( trim( $fieldValue ) == '' ) {
 				continue;
 			}
diff --git a/includes/formats/CargoTemplateFormat.php b/includes/formats/CargoTemplateFormat.php
index 5586707..4052701 100644
--- a/includes/formats/CargoTemplateFormat.php
+++ b/includes/formats/CargoTemplateFormat.php
@@ -28,6 +28,7 @@ class CargoTemplateFormat extends CargoDisplayFormat {
 				// because it's the only one that uses the
 				// unformatted values - the formatted values
 				// do this HTML-encoding on their own.
+				$row[$fieldName] = is_null($row[$fieldName]) ? '' : $row[$fieldName];
 				if ( $fieldDescription->mType == 'Wikitext' || $fieldDescription->mType == 'Wikitext string' ) {
 					$value = htmlspecialchars_decode( $row[$fieldName] );
 				} else {
diff --git a/includes/parserfunctions/CargoStore.php b/includes/parserfunctions/CargoStore.php
index fe57c6b..3fa4c88 100644
--- a/includes/parserfunctions/CargoStore.php
+++ b/includes/parserfunctions/CargoStore.php
@@ -393,7 +393,8 @@ class CargoStore {
 			if ( $fieldDescription->mIsList ) {
 				$fieldTableName = $tableName . '__' . $fieldName;
 				$delimiter = $fieldDescription->getDelimiter();
-				$individualValues = explode( $delimiter, $tableFieldValues[$fieldName] );
+				$valuesForField = is_null($tableFieldValues[$fieldName]) ? '' : $tableFieldValues[$fieldName];
+				$individualValues = explode( $delimiter, $valuesForField );
 				$valueNum = 1;
 				foreach ( $individualValues as $individualValue ) {
 					$individualValue = trim( $individualValue );

Event Timeline

@Schtom - thank you for this work! These are some great improvements.

I think these patches can be simplified a little, though, with the use of the null coalescing operator (https://en.wikipedia.org/wiki/Null_coalescing_operator#PHP). @Schtom - please don't make these changes yourself, if possible. :) I am making improving this code a microtask for Google Summer of Code applicants.

hi yaron, thanks for your comment. no worries :) i just added the patch for visibility reasons. i build-patch my docker image that way until you fix it upstream, so the deprecation errors disappear.

Change 1008073 had a related patch set uploaded (by Jayanthvikashs; author: JayanthVikashS):

[mediawiki/extensions/Cargo@master] Fix PHP 8.1 deprecation errors for fields being null

https://gerrit.wikimedia.org/r/1008073

@Yaron_Koren I have created a patch for this. Do you want me to work on another microtask (for GSoC purpose) ?

@Jayanthvikashs - this looks great! And it will definitely work. But to make this a little more of a challenge: I think it would be slightly better if the code in CargoTemplateFormat.php and CargoStore.php did not modify the original array in each case. Could you change the code so that these arrays don't get modified? (For CargoTemplateFormat.php, this could have the nice effect of simplifying the code in general.)

No need to do another microtask, I would say - if you accomplish this one successfully, it will be enough work.

@Jayanthvikashs - this looks great! And it will definitely work. But to make this a little more of a challenge: I think it would be slightly better if the code in CargoTemplateFormat.php and CargoStore.php did not modify the original array in each case. Could you change the code so that these arrays don't get modified? (For CargoTemplateFormat.php, this could have the nice effect of simplifying the code in general.)

No need to do another microtask, I would say - if you accomplish this one successfully, it will be enough work.

I have updated the patch now

Great! The code looks very simple now. But I think there's a way to make CargoTemplateFormat.php even simpler! (Let me know if you want a hint.)

Great! The code looks very simple now. But I think there's a way to make CargoTemplateFormat.php even simpler! (Let me know if you want a hint.)

Updated. Is it better now?

Change 1008073 merged by jenkins-bot:

[mediawiki/extensions/Cargo@master] Fix PHP 8.1 deprecation errors for fields being null

https://gerrit.wikimedia.org/r/1008073

Yaron_Koren claimed this task.

Excellent! Thank you for this patch.