This looks great Tiffany. I will incorporate this in the next release--with
a couple changes. I decided to generalize the RenameStamps function just a
bit to make it more robust, and made it a library function rather than a
command. It will likely be useful to developers in other ways--I'm thinking
renaming images or the like, or moving files across directories perhaps. I
have a full powered script in the file tools module, which has multiple
"tree" functions like this and is quite fast because it has its own direct
file utilities, but this is definitely useful enough to have in the core
for other developers--and possible expand. Some of the tree parameters
include:

clean (remove special ms chars)
copy (duplicate whole trees)
delete
dir (show all dirs and count of files in each)
edit (str_replace multiple files)
find (strpos multiple files)
move (move to new dir and opt to rename)
rename (rename but keep in same dir)
purge (delete files older than x days)
view (quick page list)

Here's the code I ended up with. To get it ready for more possibilities we
would probably need to improve the $pat default just a bit. IE, to move
across directories we would need to allow for dir=pages from='/folder/file'
to=/folder2/file2, but it works good enough right now for stamps, and I
need to think about security implications a bit more. Let me know if
someone has a need for more at the moment. We could probably implement some
of the above in this quite easily...

in the rename command:

if ($args['renamestamps'] == 'true') BOLTrenametree($from, $to, 'stamps',
$args);

in library.php

function BOLTrenametree($from, $to, $dir, $args='', $pat='') {
if ($pat == '') $pat = str_replace(".", "\.", "/^$from.([-_a-z0-9.]*)$/");
$trees = BOLTlistpages($pat, $dir);
foreach($trees as $tree) {
$new = str_replace($from, $to, $tree);
$success = rename("$dir/$tree", "$dir/$new");
if (! $success) BOLTmessage("rename_fail::$tree", $args);
  }
  }

What do you think? Thanks for sharing this!

Cheers,
Dan

On Fri, Jun 5, 2015 at 12:50 AM Tiffany Grenier <[email protected]>
wrote:

> What do you think of that?
>
> function BOLTXrename($value, $field, $args='') {
>  global $BOLTindexPages, $BOLTid, $systemPath;
>  $from = BOLTpageshortcuts($args['from']);
>  if (BOLTexists($from) == false) return BOLTabort(
> "rename_fail_nofile::$from");
>  $to = BOLTpageshortcuts($args['to']);
>  if (BOLTexists($to) != false) return BOLTabort(
> "rename_fail_exists::$from::$to");
>  if (!BOLTauth($to, $BOLTid, 'write')) return BOLTabort(
> "rename_fail_auth::$to");
>  $to2 = BOLTfolders($to);
>  $from2 = BOLTfolders($from);
>  if ((file_exists("$systemPath/$from")) && (! file_exists("pages/$from2"
> ))) $success = copy("$systemPath/$from", "pages/$to2");
>  else $success = rename("pages/$from2", "pages/$to2");  // change back to
> rename
>  if ($success) {
>  BOLTmessage("rename_success::$from", $args);
>  if ($args['renamestamps'] == 'true') {//NEW LINE
>  BOLTXrenamestamps($value,$field,$args);//NEW LINE
>  }//NEW LINE
>  $BOLTindexPages[] = $to;
>  $BOLTindexPages[] = $from;
>  }
>  else BOLTmessage("rename_fail::$from", $args);
>  return $success;//NEW LINE
>  }
>
>
> function BOLTXrenamestamps($value, $field, $args='') {
>  $from = BOLTpageshortcuts($args['from']);
>  $to = BOLTpageshortcuts($args['to']);
>  $pat = "/^" . str_replace(".", "\.", $from) . "\.\d{10}$/";
>  $stamps = BOLTlistpages($pat,"stamps");
>  foreach($stamps as $stampfile) {
>  $args['from']=$stampfile;
>  $args['to']=str_replace($from,$to,$stampfile);
>  if(!BOLTXrename($stampfile,$field,$args)) {//don't pollute by assessing
> success for all well-renamed stamps (that may be a lot of files!)
>  BOLTmessage("rename_fail::$stampfile", $args);
>  }
>  }
>  }
>
> I derived this piece of code from my "treename" function that also rename
> children of a given page. It is, however, untested at the moment. It would
> allow to rename manually old stamps with the renamestamps command, and one
> could also create a webhook only for renaming the stamps (in case the stamp
> format name had been changed through a webhook of stamp and unstamp, for
> example).
> I could also create a webhook of the rename command to include that as a
> plugin, but since you were interested in having this functionality in the
> core...
>
> Cheers,
> Tiffany
>
>
> Le vendredi 5 décembre 2014 16:17:01 UTC+1, Dan a écrit :
>
>> This is a good idea--to make it an optional parameter in the rename
>> command to also do the stamps. I'll add it to the todo list.
>>
>> Cheers,
>> Dan
>>
>> On Fri Dec 05 2014 at 7:44:04 AM Tiffany Grenier <[email protected]>
>> wrote:
>>
> Hi again,
>>>
>>> I was just wondering: when a page is renamed, via the "rename" action,
>>> it looses its history. Should we also rename the stamps files? Should it be
>>> an option (like "copy data too" on the "copy" action page), so we can
>>> choose the behaviour we want? Should it be a
>>> plugin/webhook/toolmapping/what-ever-other-way-we-have-not-to-generalize-this-feature?
>>>
>>> What do you think?
>>>
>>> Cheers,
>>> Tiffany
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "BoltWire" group.
>>>
>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to [email protected].
>>> To post to this group, send email to [email protected].
>>
>>
>>> Visit this group at http://groups.google.com/group/boltwire.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "BoltWire" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/boltwire.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"BoltWire" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/boltwire.
For more options, visit https://groups.google.com/d/optout.

Reply via email to