change of image folder has no effect to static pages

Found a bug? Tell us!!
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

change of image folder has no effect to static pages

Post by stm999999999 »

I have images via media library in my articles, and I have some (even with the library and <!-- s9ymdb:132 -->) on my static pages.

If I change the path of an image-folder, the images in articles are changed to the new location automaticaly. The images in the static pages do not so. :-(


And BTW: It is not a bug but more like a feature request:

Using the usergallery you can add a link to a subpart of the library to see an an galery, like http://example.com/pages/gallery.html?g ... ubfolder2/

Would it be an interesting idea to add an internal id for the subfolder (like s9ymdb:132 for images) and then an oportunity to add a link to such a subfolder (like inserting an image via the library) with a result like this:

Code: Select all

<!-- s9yfoldermdb:132 --><a href="http://example.com/pages/gallery.html?gallery=folder1/subfolder2/">name of the folder</a>
and then - of course - in case of path change this should be updated, too.
Ciao, Stephan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: change of image folder has no effect to static pages

Post by garvinhicking »

Hi!

The staticpage plugin would need to apply the changes on the 'backend_media_rename' even thook and perform some regular expression syntaxes that are also performed. I've just committed that to the new plugin version as well as a required patch to the s9y nightly.

BTW: You misunderstood the '<!--s9ymdb-->' hook. This one is currently not utilized. So it would not be required to use that for the usergallery plugin. There you would need to use actual Path replacements using regular expressions on paths.

s9ymdb is meant for serendipity versions in the future, when more people are using it and we might want to do more advanced stuff with it.

Doing substition on your link can yield terrible results. If you had a folder like "media/images" and rename it to "media/uploads/images", also normal text in your entries/Staticpages would be replaced with the key value. IF you used "I want to maintain media/images in my blog, what should I choose" in your texts, that would also be changed.

For s9y images this is not bad, because a src|href="..." is prepended to be matched on it.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Re: change of image folder has no effect to static pages

Post by stm999999999 »

The staticpage plugin would need to apply the changes on the 'backend_media_rename' even thook and perform some regular expression syntaxes that are also performed. I've just committed that to the new plugin version as well as a required patch to the s9y nightly.
Thanks, I will have a look at it.
BTW: You misunderstood the '<!--s9ymdb-->' hook. This one is currently not utilized.
Oh, I thougt just this is the function of this.
For s9y images this is not bad, because a src|href="..." is prepended to be matched on it.
should not a regex to find

Code: Select all

href=".*/pages/gallery.html?gallery=.*"
(with "pages" substituded by a possible changed path-name in the plugin)

be just as well?
Ciao, Stephan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: change of image folder has no effect to static pages

Post by garvinhicking »

Hi!

Code: Select all

href=".*/pages/gallery.html?gallery=.*"
(with "pages" substituded by a possible changed path-name in the plugin)

be just as well?
Yes, that should suffice indeed. However you might need to take the "index.php?/pages...&gallery=" variants into consideration as well, so this might grow into a huge SQL regular expression mask. :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Re: change of image folder has no effect to static pages

Post by stm999999999 »

garvinhicking wrote: as well as a required patch to the s9y nightly.
Is there a reason why you changed it for

Code: Select all

if ($type == 'filedir')
and not for

Code: Select all

if ($type == 'file') 
some line above, too?
Ciao, Stephan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: change of image folder has no effect to static pages

Post by garvinhicking »

Hi!

I forgot about it. Now done :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

I tested it now

(with http://svn.berlios.de/wsvn/serendipity/ ... =1699&sc=1 and the previous and http://php-blog.cvs.sourceforge.net/php ... ortby=date)

but it did not work!


my opinion:

in functions_images.inc.php I added

Code: Select all

printf('debug:', $oldDir);
printf('debug:', $newDir);
to the begin of serendipity_moveMediaDirectory

but the output of the variables is emtpy!?!?!





second, i add

Code: Select all

printf('debug: dir');    
after

Code: Select all

if ($type == 'dir') {
and

Code: Select all

    if ($type == 'file') {
printf('debug: file');
and

Code: Select all

    if ($type == 'filedir') {
printf('debug: filedir');
and I got the output:

Code: Select all

debug: dir 
when I move an folder into another.

But in the " if ($type == 'dir') {"-clause there are no changes and - important - there is no

Code: Select all

serendipity_plugin_api::hook_event('backend_media_rename', $renameValues);
!!!

Then I moved an single image to another folder, I got "debug: filedir" but no change to the static page. AND I got "debug: debug:" for the

Code: Select all

printf('debug:', $oldDir);
printf('debug:', $newDir);
so my opinion:

1) there must be a code change to all three of these if-clauses
2) in the first, the hook must be inserted
3) the oldDir/newDir is empty and so, the plugin did not work because of

Code: Select all

                case 'backend_media_rename':
                    if (!isset($eventData['oldDir'])) {
                        echo "debug: static fault";
                        return true;
                    }

and: what is about changing the name of an image? Is this handled by serendipity_moveMediaDirectory as well, even if the function name did looks like it? If yes, perhaps the description should be changed:

Code: Select all

/**
 * Moves a media directory or an image to another directory or renames just an image
And btw, I do not know the reason for "if (!isset($eventData['oldDir']))", but the change in the static page should work for a renaming without old and newDir, too!?
Last edited by stm999999999 on Mon May 21, 2007 6:47 pm, edited 1 time in total.
Ciao, Stephan
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

Code: Select all

printf('debug:', $oldDir);
printf('debug:', $newDir);
to the begin of serendipity_moveMediaDirectory

but the output of the variables is emtpy!?!?!
OK, thats seems to be a problem with my output-command.

Code: Select all

echo 'debug:' . $oldDir;
printf('<br />');

echo 'debug:' . $newDir;
printf('<br />');
gives the right variable-contens.

But:
3) the oldDir/newDir is empty and so, the plugin did not work because of

Code: Select all

                case 'backend_media_rename':
                    if (!isset($eventData['oldDir'])) {
                        echo "debug: static fault";
                        return true;
                    }
gives me "debug: static fault"

and

Code: Select all

              case 'backend_media_rename':
                
                echo 'debug static dir:' . $oldDir;
				printf('<br />');
                
                    if (!isset($eventData['oldDir'])) {
                        echo "debug: static fault";
                        return true;
                    }
gives:

Code: Select all

debug static dir neu:
debug: static fault
the variable is empty!? :-(
Ciao, Stephan
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

stm999999999 wrote:
so my opinion:

1) there must be a code change to all three of these if-clauses
2) in the first, the hook must be inserted
3) the oldDir/newDir is empty and so, the plugin did not work because of

Code: Select all

                case 'backend_media_rename':
                    if (!isset($eventData['oldDir'])) {
                        echo "debug: static fault";
                        return true;
                    }
OK, I figured it out:


Point 1 and 2 are still recommended.

Solution to #3:

the hook-call must be changed:

Code: Select all

        serendipity_plugin_api::hook_event('backend_media_rename', $renameValues[0]);
because s9y uses a array-in-a-array here:

Code: Select all

$renameValues = array(array(
and btw:

the "MEDIA_DIRECTORY_MOVE_ENTRIES" in the plugin-code

Code: Select all

printf(MEDIA_DIRECTORY_MOVE_ENTRIES . '<br />', count($dirs));
should become a new lang-variable

Code: Select all

@define('MEDIA_DIRECTORY_MOVE_STATIC_PAGES', 'Moved the URL of the moved directory in %s static pages.');

@define('MEDIA_DIRECTORY_MOVE_STATIC_PAGES', 'Die URL der verschobenen Verzeichnisse wurde in %s statischen Seiten angepasst.');
this should go to the plugin-lang-file, I think.



and: what is about changing the name of an image? Is this handled by serendipity_moveMediaDirectory as well, even if the function name did looks like it? If yes, perhaps the description should be changed:

Code: Select all

/**
 * Moves a media directory or an image to another directory or renames just an image
And btw, I do not know the reason for "if (!isset($eventData['oldDir']))", but the change in the static page should work for a renaming without old and newDir, too!?
renaming an image I got:

Code: Select all

debug old:
debug new:IMG_4421-noch-ein-test
debug Variablen:file
debug: file
debug hook content oldDir:
debug static dir neu:
debug: static fault
in other word: renaming a file works not with this if-clause for static pages -because oldDir is empty in this case.
Ciao, Stephan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Thanks, I committed a fix to the staticpage only. The hook itself should not be changed.

I also added the language constants.

As for renaming a single file, I'd have to inspect the file properly, will try to do that later this week.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

garvinhicking wrote:Hi!

Thanks, I committed a fix to the staticpage only. The hook itself should not be changed.
OK, inserting the [0] to the staticpage-file is a right way, too. :-)

I thought it would be cleaner to make it with the [0] in the functions_images.inc.php because all other uses of this variable-array in this file is with the [0] - now we use $array and array[0] there. But never mind.



But, what is about adding the new code to the first id-clause? BTW, this is the case I meant in my first posting: moving of an entire folder! :-)

I tested it out and this works:

inserting:

Code: Select all

        $renameValues = array(array(
            'from'   => $oldfile,
            'to'     => $newfile,
            'thumb'  => $serendipity['thumbSuffix'],
            'fthumb' => $pick['thumbnail_name'],
            'oldDir' => $oldDir,
            'newDir' => $newDir,
            'type'   => $type,
            'item_id'=> $item_id,
            'file'   => $file
        ));

        serendipity_plugin_api::hook_event('backend_media_rename', $renameValues);
between ...

Code: Select all

        if (!rename($real_oldDir, $real_newDir)) {
            printf(MEDIA_DIRECTORY_MOVE_ERROR . '<br />', $newDir);
            return false;
        }
... and ...

Code: Select all

        $dirs = serendipity_db_query("SELECT id, path
Even a renaming (instead of moving) of a folder works well!
Ciao, Stephan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

I just committed a slightly different approach, can you test it?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

hm, I do not understand the reason for your version.

your structure is:

Code: Select all

function serendipity_moveMediaDirectory

if ($type == 'dir') {do something for dir}

if ($type == 'file') {do something for file

} elseif ($type == 'filedir') {do something for filedir

} elseif ($type == 'dir') {do something for dir AGAIN !?

why not

Code: Select all

function serendipity_moveMediaDirectory

if ($type == 'dir') {do [del]something[/del] all things for dir

} elseif ($type == 'file') {do something for file

} elseif ($type == 'filedir') {do something for filedir}


Ciao, Stephan
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

OK, moving an entire folder works perfect.

But there are three other problems:

1) renaming a single file does not make a change in static pages.

reason:

in staticpage_event

Code: Select all

                   if (!isset($eventData[0]['oldDir'])) {
                        echo "debug: static fault";
                        return true;
                    }
and the fact that these function needs and uses oldDir and newDir.

solution:

move the code

Code: Select all

              $renameValues = array(array(
              'from'   => $oldfile,
              'to'     => $newfile,
              'thumb'  => $serendipity['thumbSuffix'],
              'fthumb' => $pick['thumbnail_name'],
              'oldDir' => $oldDir,
              'newDir' => $newDir,
              'type'   => $type,
              'item_id'=> $item_id,
              'file'   => $file
               ));


echo 'debug hook content oldDir:' . $renameValues[0]['oldDir'];
				printf('<br />');

              serendipity_plugin_api::hook_event('backend_media_rename', $renameValues);

bottom to:

Code: Select all

serendipity_updateImageInDatabase(array('thumbnail_name' => $renameValues[0]['thumb'], 'realname' => $newDir, 'name' => $newDir), $item_id);
                $oldDir = $file_old;
                $newDir = $file_new;

there are these both variables redefined (for other reasons, no special code change my my purpose) to the old and the new filename. With this the change in the staticpages works fine.
Ciao, Stephan
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

the second problem:

moving ONE Image from folder1 to folder2 has the result: the path of every other image in folder1 will be changed to folder2 in the staticpage-sourcecode (but of course, these other images are not moved into folder2).

problem: the hook-call only gives the path(=folder-name) of origin and destination, not the image-name. So the regex finds and changes all staticpages with an image of folder1 - with the one, right image, but even with all the other images!

problem 3:

As I see in functions_images all the regex-code works only with mysql(i)

Code: Select all

    // Only MySQL supported, since I don't know how to use REGEXPs differently.
The code you add into the staticpage-php is similar to the code in functions_images, so I think, these code needs mysql(i), too. Right?


Solution to all problems:

in the moment, in functions_images the hook is called in every of the three if-clauses - but it is allways the same code!

We can delete the new code in all theses if-clauses!

and then add after:

Code: Select all

    // Only MySQL supported, since I don't know how to use REGEXPs differently.
    if ($serendipity['dbType'] != 'mysql' && $serendipity['dbType'] != 'mysqli') {
        echo MEDIA_DIRECTORY_MOVE_ENTRY . '<br />';
        return true;
    }

    $q = "SELECT id, body, extended
            FROM {$serendipity['dbPrefix']}entries
           WHERE body     REGEXP '(src=|href=|window.open.)(\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . "|" . serendipity_db_escape_string($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ")'
              OR extended REGEXP '(src=|href=|window.open.)(\'|\")(" . serendipity_db_escape_String($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . "|" . serendipity_db_escape_string($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ")'
    ";

    $dirs = serendipity_db_query($q);
    if (is_array($dirs)) {
        foreach($dirs AS $dir) {
            $dir['body']     = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\1\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['body']);
            $dir['extended'] = preg_replace('@(src=|href=|window.open.)(\'|")(' . preg_quote($serendipity['baseURL'] . $serendipity['uploadHTTPPath'] . $oldDir) . '|' . preg_quote($serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $oldDir) . ')@', '\1\2' . $serendipity['serendipityHTTPPath'] . $serendipity['uploadHTTPPath'] . $newDir, $dir['extended']);

            $uq = "UPDATE {$serendipity['dbPrefix']}entries
                                     SET body     = '" . serendipity_db_escape_string($dir['body']) . "' ,
                                         extended = '" . serendipity_db_escape_string($dir['extended']) . "'
                                   WHERE id       = " . serendipity_db_escape_string($dir['id']);
            serendipity_db_query($uq);
        }

        printf(MEDIA_DIRECTORY_MOVE_ENTRIES . '<br />', count($dirs));
    }
this:

Code: Select all

    $renameValues = array(array(
    'from'   => $oldfile,
    'to'     => $newfile,
    'thumb'  => $serendipity['thumbSuffix'],
    'fthumb' => $pick['thumbnail_name'],
    'oldDir' => $oldDir,
    'newDir' => $newDir,
    'type'   => $type,
    'item_id'=> $item_id,
    'file'   => $file
     ));

     serendipity_plugin_api::hook_event('backend_media_rename', $renameValues);


    return true;
}
advantage:

$oldDir and $newDir are allways with the right and needed content - because the sql-statement for the entries gets the same values! It has the folder when moving an entire folder. It has the folder+filename if only a file is moved or renamed.

and all these code - which works only with mysql(i) - is behind the mysql-test. Or, if you think, that the hook should be called even if there is not mysql(i), - perhaps some further plugin would use it for something independant from mysql(i) - we can move the new code directly before the

Code: Select all

    // Only MySQL supported, since I don't know how to use REGEXPs differently.
But in this case we have to add these mysql(i)-test in the staticpage-php, because this special code needs mysql(i).






btw, so we are able to make the structure of the if-clauses more clean with elseif:

Code: Select all

if ($type == 'dir') {do something

} elseif ($type == 'file') {do something 

} elseif ($type == 'filedir') {do something }
Ciao, Stephan
Post Reply