OJS 3.1. and PHP Warning: array_flip(): Can only flip STRING and INTEGER values

Hi,

My test server had warnings turned on and I saw a lot of these in the logs:
[Wed Jan 24 17:59:30.930864 2018] [:error] [pid 27808] [client 10.8.0.10:50544] PHP Warning: array_flip(): Can only flip STRING and INTEGER values! in /var/www/site.fi/public_html/lib/pkp/classes/core/PKPApplication.inc.php on line 702, referer: https://site.fi/context/management/settings/publication

They are triggered here: https://github.com/pkp/pkp-lib/blob/master/controllers/grid/settings/preparedEmails/PreparedEmailsGridCellProvider.inc.php#L36-L43
When getRoleNames is called here https://github.com/pkp/pkp-lib/blob/master/classes/core/PKPApplication.inc.php#L683-L704

and this is the array_flip causing the warning: https://github.com/pkp/pkp-lib/blob/master/classes/core/PKPApplication.inc.php#L700

Hi @ajnyga,

Hmm, could you add something like the following to capture what’s getting passed in with $roleIds?

error_log(print_r($roleIds, true));

Then watch for the last error_log result before the warning appears.

Regards,
Alec Smecher
Public Knowledge Project Team

I did. In many cases there is an integer there, but in many cases the value is empty, and that empty value causes the warning.

edit: I can check an example for you in a while.

@asmecher, here are two cases:

[Wed Jan 24 17:59:30.690876 2018] [:error] [pid 27808] [client 10.8.0.10:50544] , referer: https://site.fi/context/management/settings/publication
[Wed Jan 24 17:59:30.690902 2018] [:error] [pid 27808] [client 10.8.0.10:50544] EmailTemplate Object\n(\n    [_data] => Array\n        (\n            [emailKey] => CITATION_EDITOR_AUTHOR_QUERY\n            [locale] => fi_FI\n            [subject] => Viittausten muokkaus\n            [body] => Hyv\xc3\xa4 {$authorFirstName},<br />\n<br />\nVoisitteko yst\xc3\xa4v\xc3\xa4llisesti vahvistaa seuraavan artikkelissanne {$submissionTitle} olevan l\xc3\xa4hdeviitteen tai antaa oikean viittauksen:<br />\n<br />\n{$rawCitation}<br />\n<br />\nKiitos!<br />\n<br />\n{$userFirstName}<br />\nTekninen toimittaja, {$contextName}<br />\n\n            [enabled] => 1\n            [canDisable] => 0\n        )\n\n    [_hasLoadableAdapters] => \n    [_metadataExtractionAdapters] => Array\n        (\n        )\n\n    [_extractionAdaptersLoaded] => \n    [_metadataInjectionAdapters] => Array\n        (\n        )\n\n    [_injectionAdaptersLoaded] => \n    [isCustomTemplate] => \n)\n, referer: https://site.fi/context/management/settings/publication
[Wed Jan 24 17:59:30.690919 2018] [:error] [pid 27808] [client 10.8.0.10:50544] PHP Warning:  array_flip(): Can only flip STRING and INTEGER values! in /var/www/site.fi/public_html/lib/pkp/classes/core/PKPApplication.inc.php on line 702, referer: https://site.fi/context/management/settings/publication
[Wed Jan 24 17:59:30.713642 2018] [:error] [pid 27808] [client 10.8.0.10:50544] 512, referer: https://site.fi/context/management/settings/publication
[Wed Jan 24 17:59:30.713679 2018] [:error] [pid 27808] [client 10.8.0.10:50544] EmailTemplate Object\n(\n    [_data] => Array\n        (\n            [emailKey] => COPYEDIT_REQUEST\n            [locale] => fi_FI\n            [subject] => Teknist\xc3\xa4 toimittamista pyydet\xc3\xa4\xc3\xa4n\n            [body] => Hyv\xc3\xa4 {$participantName},<br />\n<br />\nPyyt\xc3\xa4isin teit\xc3\xa4 toimittamaan teknisesti k\xc3\xa4sikirjoituksen &quot;{$submissionTitle}&quot; julkaisulle {$contextName}, seuraavia vaiheita noudattaen:<br />\n1. Klikatkaa alla olevaa k\xc3\xa4sikirjoituksen URL-osoitetta.<br />\n2. Avatkaa Luonnostiedostot-otsikon alla olevat tiedostot ja toimittakaa ne teknisesti. Voitte samalla lis\xc3\xa4t\xc3\xa4 tekniseen toimittamiseen liittyvi\xc3\xa4 keskusteluja tarpeen mukaan.<br />\n3. Tallentakaa teknisesti toimitetut tiedostot ja ladatkaa ne Teknisesti toimitettu -paneeliin.<br />\n4. Ilmoittakaa toimittajalle, ett\xc3\xa4 kaikki tiedostot ovat valmiina, ja ett\xc3\xa4 tuotantoprosessi voidaan aloittaa.<br />\n<br />\n{$contextName} URL: {$contextUrl}<br />\nK\xc3\xa4sikirjoituksen URL: <a href="{$submissionUrl}">{$submissionUrl}</a><br />\nK\xc3\xa4ytt\xc3\xa4j\xc3\xa4tunnus: {$participantUsername}<br />\n<br />\nYst\xc3\xa4v\xc3\xa4llisin terveisin,<br />\n{$editorialContactSignature}\n            [enabled] => 1\n            [canDisable] => 1\n            [fromRoleId] => 512\n            [toRoleId] => 8192\n        )\n\n    [_hasLoadableAdapters] => \n    [_metadataExtractionAdapters] => Array\n        (\n        )\n\n    [_extractionAdaptersLoaded] => \n    [_metadataInjectionAdapters] => Array\n        (\n        )\n\n    [_injectionAdaptersLoaded] => \n    [isCustomTemplate] => \n)\n, referer: https://site.fi/context/management/settings/publication

Basically the CITATION_EDITOR_AUTHOR_QUERY template does not return roleid (that is the first empty error line there), but for example the COPYEDIT_REQUEST template returns 512.

Edit: and in the PreparedEmailsGridProvider I am doing this:

case 'sender':
$roleId = $element->getFromRoleId();
error_log(print_r($roleId, true));
error_log(print_r($element, true));
$label = Application::getRoleNames(false, array($roleId));

Hi @ajnyga,

Can you try the change…

diff --git a/controllers/grid/settings/preparedEmails/PreparedEmailsGridCellProvider.inc.php b/controllers/grid/settings/preparedEmails/PreparedEmailsGridCellProvider.inc.php
index 4649289..322880b 100644
--- a/controllers/grid/settings/preparedEmails/PreparedEmailsGridCellProvider.inc.php
+++ b/controllers/grid/settings/preparedEmails/PreparedEmailsGridCellProvider.inc.php
@@ -35,6 +35,7 @@ class PreparedEmailsGridCellProvider extends DataObjectGridCellProvider {
 				return array('label' => ucwords(strtolower(str_replace('_', ' ', $label))));
 			case 'sender':
 				$roleId = $element->getFromRoleId();
+				if (!$roleId) return array('label' => '');
 				$label = Application::getRoleNames(false, array($roleId));
 				return array('label' => __(array_shift($label)));
 			case 'recipient':

I think this addition should take care of it. The warning is cosmetic, and it’s on a purely informational part of the code, but’ll be better to keep it out of the log.

Regards,
Alec Smecher
Public Knowledge Project Team

That does fix it. But you need a similar line to the recipient case as well right below that one.

			case 'sender':
 				$roleId = $element->getFromRoleId();
				if (!$roleId) return array('label' => '');
 				$label = Application::getRoleNames(false, array($roleId));
 				return array('label' => __(array_shift($label)));
			case 'recipient':
 				$roleId = $element->getToRoleId();
				if (!$roleId) return array('label' => '');				
				$label = Application::getRoleNames(false, array($roleId));
				return array('label' => __(array_shift($label)));
			case 'subject':

Hi @ajnyga,

Perfect – done, thanks!
https://github.com/pkp/pkp-lib/commit/dcf44315ef885f9f25f9e50250444d830bd79018

Regards,
Alec Smecher
Public Knowledge Project Team