Skip to content

Commit

Permalink
Item14089: mode application extension. Includes TopicStubAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
BramVan-Oosterhout committed Apr 20, 2017
1 parent d4fc7ac commit 02b511a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 118 deletions.
4 changes: 2 additions & 2 deletions data/System/CopyContrib.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="BramVanOosterhout" comment="" date="1490246301" format="1.1" version="5"}%
%META:TOPICINFO{author="BramVanOosterhout" comment="" date="1490961236" format="1.1" version="7"}%
---+!! %TOPIC%
%FORMFIELD{"Description"}%

Expand Down Expand Up @@ -43,7 +43,7 @@ a copy agent is created to perform the actual operation. The basic skeleton for
<li> =topic=: copy one topic or parts of it to a destination topic </li>\
<li> =topics=: copy a list of topic to a destination </li>\
<li> =web=: copy a web to a destination </li>\
<li> =application>=: Create a web suported by a given application </li>\
<li> =application=: Create a web supported by a given application </li>\
<li> =stub=: create a stub for a given application topic </li> </ul> | |
| =debug= | boolean flag to switch on/off debug output; messages are printed to the error log of the web browser | =off= |
| =dry= | boolean flag to switch on/off a dry mode; if enabled none of the operations will actually be saved permanently | =off= |
Expand Down
2 changes: 1 addition & 1 deletion lib/Foswiki/Contrib/CopyContrib.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ our %agentImpls = (
'topics' => 'Foswiki::Contrib::CopyContrib::TopicListAgent',
'stub' => 'Foswiki::Contrib::CopyContrib::TopicStubAgent',
'web' => 'Foswiki::Contrib::CopyContrib::WebAgent',
'application' => 'Foswiki::Contrib::CopyContrib::ApplicationAgent',
'application' => 'Foswiki::Contrib::CopyContrib::ApplicationAgent',
# 'webs' => 'Foswiki::Contrib::CopyContrib::WebListAgent',
);

Expand Down
75 changes: 23 additions & 52 deletions lib/Foswiki/Contrib/CopyContrib/ApplicationAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use warnings;
use Foswiki::Func();
use Foswiki::Contrib::CopyContrib::CopyAgent ();
use Foswiki::Contrib::CopyContrib::TopicAgent ();
use Foswiki::Contrib::CopyContrib::TopicStubAgent ();
use Error qw( :try );

use Data::Dumper;
#use Data::Dumper;

our @ISA = qw( Foswiki::Contrib::CopyContrib::CopyAgent );

Expand All @@ -43,17 +44,15 @@ sub parseRequestObject {
unless @source;
$request->delete('source');

$this->writeDebug("source topics:");
# $this->writeDebug("source topics:");
foreach (@source) {
foreach my $item ( split(/\s*,\s*/) ) {

# $item is of the form: source_topic_name => target_topic_name [copy_type]
$this->writeDebug("... item: $item");
# $this->writeDebug("... item: $item");
my ( $from, $to, $type ) = ( $item =~
m!\A\s*([\/\.\w]+)\s*(?:=>\s*(\w+))?\s*(\[\w+\])?\s*\Z! );
unless ($from) {
$this->writeDebug(
" >>> not a valid input source parameter. Ignored.");
next;
}

Expand All @@ -69,7 +68,7 @@ sub parseRequestObject {
target => $to,
type => $type
};
$this->writeDebug("... web.topic=>target: $web.$topic => $to");
# $this->writeDebug("... web.topic=>target: $web.$topic => $to");
}
}
}
Expand All @@ -78,11 +77,11 @@ sub parseRequestObject {
unless ( defined $this->{dstWeb} ) {
$this->{dstWeb} = $request->param('destination');
$request->delete('destination');
$this->writeDebug("dstWeb=$this->{dstWeb}") if defined $this->{dstWeb};
# $this->writeDebug("dstWeb=$this->{dstWeb}") if defined $this->{dstWeb};
}

# Get the template web. SMELL: Why don't we delete the parameter?
$this->{templateWeb} = $request->param('template')
$this->{templateWeb} = $request->param('template') || '_default'
unless defined $this->{templateWeb};

return $this;
Expand All @@ -92,12 +91,9 @@ sub parseRequestObject {
sub copy {
my $this = shift;

$this->writeDebug(
"called copy() " . ( $this->{dry} ? '...dry run' : '' ) );
# $this->writeDebug("called copy() " . ( $this->{dry} ? '...dry run' : '' ) );

## check destination web. If it does not exists. create it as is done in the WebAgent (Can't use web agent. It expects source topics.)
## check source list and use topic agent to copy each [normal] topic
## Consider a StubAgent, which copies a given topic to a stub. Web must extist.

throw Error::Simple("No destination") unless defined $this->{dstWeb};

Expand All @@ -106,27 +102,28 @@ sub copy {

unless ( Foswiki::Func::webExists( $this->{dstWeb} ) ) {
my $template = $this->{templateWeb} || '_default';
$this->writeDebug(
"creating destination web '$this->{dstWeb}' using template '$template'"
);
Foswiki::Func::createWeb( $this->{dstWeb}, $template )
# $this->writeDebug("creating destination web '$this->{dstWeb}' using template '$this->{templateWeb}'");
Foswiki::Func::createWeb( $this->{dstWeb}, $this->{templateWeb} )
unless $this->{dry};
}

#print "=== Sleeping...\n"; sleep(60);

if ( Foswiki::Func::webExists( $this->{dstWeb} ) ) {

# copy all topics to a destination web
#BvO $this->{dstWeb} = $this->{dst};

foreach my $item ( @{ $this->{srcTopics} } ) {
if ( $item->{type} eq '[stub]' ) {
$count++;
$this->writeDebug(
"... copying $item->{web}.$item->{topic} to $this->{dstWeb}.$item->{target} AS STUB"
my $agent = new Foswiki::Contrib::CopyContrib::TopicStubAgent(
$this->{session},
srcWeb => $item->{web},
srcTopic => $item->{topic},
dstWeb => $this->{dstWeb},
dstTopic => $item->{target},
dry => $this->{dry},
debug => $this->{debug},
);
copyStub( $this, $item );
# $this->writeDebug("... copying $item->{web}.$item->{topic} to $this->{dstWeb}.$item->{target} AS STUB");
$count++;
$agent->parseRequestObject($request)->copy();
}
else { # else[topic]
my $agent = new Foswiki::Contrib::CopyContrib::TopicAgent(
Expand All @@ -140,41 +137,15 @@ sub copy {
debug => $this->{debug},
);

# $this->writeDebug("... copying $item->{web}.$item->{topic} to $this->{dstWeb}.$item->{target}");
$count++;
$this->writeDebug(
"... copying $item->{web}.$item->{topic} to $this->{dstWeb}.$item->{target}"
);
$agent->parseRequestObject($request)->copy();
}
}

return ( "topiclist_success", $count, $this->{dstWeb} );
return ( "web_success", $count, $this->{dstWeb} );
}
}

sub copyStub {
my ( $this, $item ) = @_;

# Foswiki::Func::saveTopic( $web, $topic, $meta, $text );
Foswiki::Func::saveTopic( $this->{dstWeb}, $item->{target}, undef(),
topicStubTemplate( $item->{web}, $item->{web} . '.' . $item->{topic} )
);

}

sub topicStubTemplate {
my ( $application, $target ) = @_;
my $text = <<"END_HERE";
%META:FORM{name="Applications.TopicStub"}%
%META:FIELD{name="TopicType" title="TopicType" value="TopicStub, TopicType"}%
%META:FIELD{name="TopicTitle" title="<nop>TopicTitle" value=""}%
%META:FIELD{name="Summary" title="Summary" value=""}%
%META:FIELD{name="WikiApplication" title="WikiApplication" value="$application"}%
%META:FIELD{name="Target" title="Target" value="$target"}%
END_HERE

return $text;
}

1;

1 change: 0 additions & 1 deletion lib/Foswiki/Contrib/CopyContrib/CopyAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ sub parseRequestObject {
$this->{minor} = Foswiki::Func::isTrue(scalar $request->param('minor'), 0)
unless defined $this->{minor};


# TODO
# redirectto

Expand Down
16 changes: 8 additions & 8 deletions lib/Foswiki/Contrib/CopyContrib/TopicAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ sub parseRequestObject {
($this->{dstWeb}, $this->{dstTopic}) = Foswiki::Func::normalizeWebTopicName($this->{baseWeb}, $this->{dst})
unless defined $this->{dstWeb} && defined $this->{dstTopic};

$this->writeDebug("srcWeb=$this->{srcWeb}, srcTopic=$this->{srcTopic}");
$this->writeDebug("dstWeb=$this->{dstWeb}, dstTopic=$this->{dstTopic}");
#$this->writeDebug("srcWeb=$this->{srcWeb}, srcTopic=$this->{srcTopic}");
#$this->writeDebug("dstWeb=$this->{dstWeb}, dstTopic=$this->{dstTopic}");

my @includeParts = ();
my $includeParts = $request->param('includeparts');
Expand Down Expand Up @@ -148,7 +148,7 @@ sub checkAccess {
sub read {
my ($this, $doReload) = @_;

$this->writeDebug("called read");
#$this->writeDebug("called read");

# read/create destination object
if (!defined($this->{dstMeta}) || $doReload) {
Expand Down Expand Up @@ -178,7 +178,7 @@ sub copyPart {
!$this->{excludeparts}{$partId}
);

$this->writeDebug("called copyPart($partId)");
#$this->writeDebug("called copyPart($partId)");
$this->read;

# special handling of attachments
Expand All @@ -204,7 +204,7 @@ sub copyPart {
throw Error::Simple("Unknown meta data id '$partId'")
unless defined $metaDataName;

$this->writeDebug("metaDataName=$metaDataName");
#$this->writeDebug("metaDataName=$metaDataName");

my $exclude = $this->{'exclude'.$partId};
my $include = $this->{'include'.$partId};
Expand All @@ -230,7 +230,7 @@ sub copyPart {
$count++;
}

$this->writeDebug("copied $count $partId item(s)") if $count;
#$this->writeDebug("copied $count $partId item(s)") if $count;
}

###############################################################################
Expand Down Expand Up @@ -281,8 +281,8 @@ sub copyAttachments {
sub copy {
my $this = shift;

$this->writeDebug("called copy() ".($this->{dry}?'...dry run':''));
$this->writeDebug("doClear=".$this->{doClear});
#$this->writeDebug("called copy() ".($this->{dry}?'...dry run':''));
#$this->writeDebug("doClear=".$this->{doClear});

throw Error::Simple("Topic $this->{srcWeb}.$this->{srcTopic} does not exist")
unless Foswiki::Func::topicExists($this->{srcWeb}, $this->{srcTopic});
Expand Down
53 changes: 5 additions & 48 deletions lib/Foswiki/Contrib/CopyContrib/TopicStubAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@ package Foswiki::Contrib::CopyContrib::TopicStubAgent;
use strict;
use warnings;

#use Foswiki::Contrib::CopyContrib::CopyAgent ();
use Foswiki::Contrib::CopyContrib::TopicAgent ();

our @ISA = qw( Foswiki::Contrib::CopyContrib::TopicAgent );

use Data::Dumper ();

sub printdump { print "========== dump of $_[0]\n"; $Data::Dumper::Maxdepth = 3; print Data::Dumper->Dump( [$_[1]] ) }

###############################################################################
sub DISABLE_parseRequestObject {
my ($this, $request) = @_;

$this->SUPER::parseRequestObject($request);

## TopicStubAgent performs a specialised copy of TopicAgent.
## TopicStubAgent will copy the source topic as a stub
## TopicStubAgent will NOT copy TopicStubs (TODO special case?)
## TopicStubAgent:
## * does NOT copy attachments
## * does NOT copy text
Expand All @@ -31,7 +20,8 @@ sub DISABLE_parseRequestObject {
## * Summary
## * WikiApplication
## * does insert the following meta data (Not available through TopicAgent)
## * META:FIELD{name="TopicType" title="TopicType" value="TopicStub, <types listed in the source topic>"}%
## * META:FIELD{name="TopicType" title="TopicType"
## value="TopicStub, <types listed in the source topic>"}%
## * META:FORM{name="Applications.TopicType"}
## * META:FIELD{name="Target" title="Target" value="<fully qualified source topic>"}
## * META:FIELD{name="Section" title="Section" value=""}%
Expand All @@ -40,42 +30,11 @@ sub DISABLE_parseRequestObject {
## * source
## * destination


$this->{src} = $request->param('source') || $this->{baseWeb}.'.'.$this->{baseTopic};
($this->{srcWeb}, $this->{srcTopic}) = Foswiki::Func::normalizeWebTopicName($this->{baseWeb}, $this->{src})
unless defined $this->{srcWeb} && defined $this->{srcTopic};

$this->{dst} = $request->param('destination') || $this->{src};
($this->{dstWeb}, $this->{dstTopic}) = Foswiki::Func::normalizeWebTopicName($this->{baseWeb}, $this->{dst})
unless defined $this->{dstWeb} && defined $this->{dstTopic};

$this->writeDebug("srcWeb=$this->{srcWeb}, srcTopic=$this->{srcTopic}");
$this->writeDebug("dstWeb=$this->{dstWeb}, dstTopic=$this->{dstTopic}");

# if ($this->{debug}) {
# print STDERR Data::Dumper->Dump([$this])"\n";
# }

return $this;
}

###############################################################################
# getKnownMetaAliases returns aliases for selected existing meta data:
# (this is the META data Foswiki knows, not the META data on the source topic.)
# VERSIONS - ignored
# TOPICMOVED - ignored
# TOPICINFO - ignored
# FORM as form
# PREFERENCE as preferences
# FILEATTACHMENT as attachments
# FIELD as fields
# TOPICPARENT as parent

###############################################################################
sub copy {
my $this = shift;

$this->writeDebug("called copy() ".($this->{dry}?'...dry run':''));
# $this->writeDebug("called copy() ".($this->{dry}?'...dry run':''));

throw Error::Simple("Topic $this->{srcWeb}.$this->{srcTopic} does not exist")
unless Foswiki::Func::topicExists($this->{srcWeb}, $this->{srcTopic});
Expand All @@ -96,7 +55,7 @@ sub copy {
# If the source is a TopicStub, then we can save the topic directly under the destination name.
if ( $this->isTopicStub() ) {
if ($this->{srcMeta} && !$this->{dry}) {
$this->writeDebug("saving to $this->{dstWeb}.$this->{dstTopic} as copied stub");
# $this->writeDebug("saving to $this->{dstWeb}.$this->{dstTopic} as copied stub");
$this->{srcMeta}->saveAs(
web => $this->{dstWeb},
topic => $this->{dstTopic},
Expand All @@ -116,7 +75,7 @@ sub copy {

# save the destination topic
if ($this->{dstMeta} && !$this->{dry}) {
$this->writeDebug("saving to $this->{dstWeb}.$this->{dstTopic}");
# $this->writeDebug("saving to $this->{dstWeb}.$this->{dstTopic}");
$this->{dstMeta}->save(
forcenewrevision => $this->{forceNewRevision},
dontlog => $this->{dontLog},
Expand Down Expand Up @@ -173,8 +132,6 @@ sub addMetadata {
$this->{dstMeta}->put( 'FORM', { name => 'Applications.TopicStub' } );

$this->{dstMeta} ->putAll( 'FIELD', $this->defineFields() );

## printdump( "Destination topic (including Target):", $this->{dstMeta} );
}

1;
Expand Down
12 changes: 6 additions & 6 deletions lib/Foswiki/Contrib/CopyContrib/WebAgent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ sub copy {

unless (Foswiki::Func::webExists($this->{dstWeb})) {
my $template = $this->{templateWeb} || '_empty';
$this->writeDebug("creating destination web '$this->{dstWeb}' using template '$template'");
# $this->writeDebug("creating destination web '$this->{dstWeb}' using template '$template'");
Foswiki::Func::createWeb($this->{dstWeb}, $this->{templateWeb})
unless $this->{dry};
}

my $searchString = $this->{search};
$searchString = '1' unless defined $searchString;

$this->writeDebug("search=$searchString");
# $this->writeDebug("search=$searchString");

my $matches = Foswiki::Func::query(
$searchString,
Expand All @@ -84,8 +84,8 @@ sub copy {
my $request = Foswiki::Func::getRequestObject();
my $count = 0;

$this->writeDebug("include=$this->{include}") if defined $this->{include};
$this->writeDebug("exclude=$this->{exclude}") if defined $this->{exclude};
# $this->writeDebug("include=$this->{include}") if defined $this->{include};
# $this->writeDebug("exclude=$this->{exclude}") if defined $this->{exclude};

if (Foswiki::Func::getContext()->{DBCachePluginEnabled}) {
# $this->writeDebug("disabling DBCachePlugin's saveHandler temporarily during bulk operation");
Expand All @@ -111,7 +111,7 @@ sub copy {
debug => $this->{debug},
);
$count++;
$this->writeDebug("... copying $web.$topic to $this->{dstWeb}.$topic");
# $this->writeDebug("... copying $web.$topic to $this->{dstWeb}.$topic");
$agent->parseRequestObject($request)->copy();
}
} finally {
Expand All @@ -121,7 +121,7 @@ sub copy {
# }
};

$this->writeDebug("copied $count topic(s)") if $count;
# $this->writeDebug("copied $count topic(s)") if $count;

return ("web_success", $count, $this->{dstWeb});
}
Expand Down

0 comments on commit 02b511a

Please sign in to comment.