Announcement

#1 2023-01-05 23:32:47

mgrandpa
Member
2017-01-02
21

Error Using Import Tree

Hello/Hi/Greetings,

I'm trying to use import tree to add photos

Getting error
malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "end of string)*) at Line 190



Piwigo 13.4.0 Check for upgrade
Installed on 22 November 2022, 1 month 1 week 6 days ago
Operating system: WINNT
PHP: 7.4.26 (Show info) [2023-01-05 22:31:05]
MySQL: 5.7.36 [2023-01-05 16:31:05]
Graphics Library: GD bundled (2.1.0 compatible)
Cache size N/A   never calculated Refresh

Offline

 

#2 2023-01-06 08:45:50

TOnin
Member
1970-01-01
106

Re: Error Using Import Tree

Hello,
I found this corresponding extension.
[extension by plg] Piwigo Import Tree

Last edited by TOnin (2023-01-06 08:49:31)

Offline

 

#3 2023-01-07 17:26:00

mgrandpa
Member
2017-01-02
21

Re: Error Using Import Tree

I am using Piwigo Import Tree.  At least trying too

Last edited by mgrandpa (2023-01-07 17:26:38)

Offline

 

#4 2023-01-07 17:57:20

erAck
Only trying to help
2015-09-06
1998

Re: Error Using Import Tree

At line 190 of what file even? I think you omitted some of the message.


Running Piwigo at https://erack.net/gallery/

Offline

 

#5 2023-01-08 00:56:37

mgrandpa
Member
2017-01-02
21

Re: Error Using Import Tree

of piwigo_import_tree.pl

Offline

 

#6 2023-01-08 14:43:28

erAck
Only trying to help
2015-09-06
1998

Re: Error Using Import Tree

Makes no sense to me. There's no code on line 190, it's a blank line. Make sure you're using the latest version 1.0. On line 191 would be

    return from_json($response->content)->{result}{id};

that could throw that error.

As you're using Windows: note the comment at line 23

# make it compatible with Windows, but breaks Linux
#use utf8;

so uncomment the #use utf8; that it reads

use utf8;

maybe that helps, but it's just a guess.

Other than that, if the error is indeed at that return from_json() call, you could try to insert a

print $response->content ."\n";

before, hopefully that would print the content of the response, maybe it's not the expected JSON data but some other message content.


Running Piwigo at https://erack.net/gallery/

Offline

 

#7 2023-02-10 19:18:53

Tirs
Member
Canyelles
2016-09-18
7

Re: Error Using Import Tree

Hello guys! I think I found the solution (or at least a solution). I was having the same problem but piwigo-import-tree works fine for me now... without even modifying it! I had to tweak Piwigo a bit, though. Nothing serious, believe me.

I'm going to give a bit of explanation first, so if you are like "Oh, com'on! Just gimme da solution!", search for "[SOLUTION]" in this post and go straight there ;)

As you probably know, newer versions of PHP are much stricter with things like typecasting, empty parameters, etc. Well, one of them is breaking the import process.

piwigo-import-tree (I'll call it "the script" for short) uses the function "from_json", which obviously expects a JSON string. This JSON string is sent by Piwigo as a response to a request (I won't discuss Piwigo's JSON/AJAX interface here). Let's see a chunk of code from the script, where it asks Piwigo to send the list of albums and then tries to do something with it:

Code:

my $response = $ua->post(
    $conf{base_url}.'/ws.php?format=json',
    {
        method => 'pwg.categories.getList',
        recursive => 1,
        fullname => 1,
    }
);
my $albums_aref = from_json($response->content)->{result}->{categories};

The problem is, the PHP engine under Piwigo is returning a message like "warning: this function does not accept null parametres any more blah blah blah". And this is not JSON, so it breaks the from_json($response->content) above.

Now let's go to Piwigo and look at this function:

Code:

    $row['comment'] = strip_tags(
      trigger_change(
        'render_category_description',
        $row['comment'],
        'ws_categories_getList'
        )

From the parameter names, we can assume Piwigo is trying to strip potentially dangerous tags from the results it returns. The problem is: if the album does not have a description, strip_tags() receives a null string and complains, and this complaint (see "blah blah blah" above) makes it back to the script, which breaks.

[SOLUTION]
As promised, here is what to do:
1. Edit the file {your_piwigo_base_directory}/include/ws_functions/pwg.categories.php
2. Locate the chunk of code shown above (in version 13.5.0, this is at line 323; I don't think it changes across minor versions)
3. In the line $row['comment'], insert a dot and a space surrounded by single quotes right before the final comma. The above code should look like this:

Code:

    $row['comment'] = strip_tags(
      trigger_change(
        'render_category_description',
        $row['comment'] . ' ',
        'ws_categories_getList'
        )

4. Save the file and try to use piwigo_import_tree again. Voilà! It works (or should work!)

[OH, THANK YOU SO MUCH, BUT... WHY DOES THIS WORK?]
By adding this whitespace to the parameter, we guarantee that the album will always have a description, even if that description is just a blank character, so PHP won't complain about null parameters and the return value will be correct.

BTW: You will find a very similar chunk of code right before this one, but with ['name'] instead of ['comment']. Unless you have albums with no name (huh?), no need to mess with this one.

The only drawback here is that we should check the file pwg.categories.php after every update just to make sure it was not overwritten, and re-apply this patch if necessary.

Hope this loooong post helps somebody!

Offline

 

#8 2023-02-11 18:12:26

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13786

Re: Error Using Import Tree

Piwigo 13.6.0 will be fixed for this, see the way we did it [Github] Piwigo issue #1786

Offline

 

#9 2023-02-11 19:58:32

Tirs
Member
Canyelles
2016-09-18
7

Re: Error Using Import Tree

That's great. Thank you!

The version 8 of PHP with its more restrictive behaviour has been a source of headaches for lots of people, including myself. Of course, it encourages good programming practices, but still, it's a continuous pool of surprises like this one. Anyway, thanks a lot for addressing the problem.

You guys just cast the return as "string". Way more elegant than my solution :) (for some reason, using the cast never comes to my mind in these cases)

Last edited by Tirs (2023-02-11 20:01:34)

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact