Navigation Menu

Skip to content

Commit

Permalink
Item13690: new perl api to register index handlers
Browse files Browse the repository at this point in the history
also:

- moved indexing meta comments to MetaCommentPlugin
- fixed fallback for polymorphic calling semantics in DBCALL{topic->method}
- docu updates
  • Loading branch information
MichaelDaum committed Sep 9, 2015
1 parent e1da513 commit d0c7785
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 60 deletions.
49 changes: 39 additions & 10 deletions data/System/DBCachePlugin.txt
Expand Up @@ -15,9 +15,9 @@ properties.

*DBCALL* is named "call" and not "include" as its main purpose is to ease
<nop>WikiApplications where <nop>TopicFunctions are treated as stored procedures.
It fetches pre-compiled topics or sections of it. "Glue" chars
(see Foswiki:Extensions:GluePlugin) are applied in advance so that the actual rendering time
is minimized. DBCALL does therefore not allow to call "external" pages as INCLUDE allows.

DBCALL does not allow to call "external" pages as INCLUDE allows.

DBCALL's "warn" parameter can only be set to "on" and "off" and does not allow
alternative content in case of a warning. The "pattern" feature used to extract a fragment from
an INCLUDEd text using regular expression has been dropped.
Expand Down Expand Up @@ -356,6 +356,24 @@ web one <nop>WikiApplication making use of this plugin.
returns a database object. See the Foswiki:Extensions::DBCacheContrib documentation
for its interfaces.

---++++ registerIndexTopicHandler()
This registers a function to the plugin to be called when a topic is indexed. It will then give each registered
index handler a chance to operate on the object being indexed. This can be used to add additional info to be stored.

Example usage:

<verbatim>
use Foswiki::Plugins::DBCachePlugin;

Foswiki::Plugins::DBCachePlugin::registerIndexTopicHandler(sub {
my ($db, $obj, $web, $topic, $meta, $text) = @_;

...
$obj->set("foo", "bar");
});
</verbatim>


---+++ <nop>DBCachePlugin::WebDB
This implements the database object that is used to access the topic records in
a web. Note, that for each web there is exactly one <nop>WebDB database object.
Expand All @@ -365,7 +383,7 @@ A database object is fetched (and initialized if needed) by the
---++++ dbQuery()
The DBQUERY functionality can be access from within perl using the plugins =dbQuery()= method.

| *(@$topicNames, %$topicObjs, $errorMsg) = $db-&gt;dbQuery([$search, @$topics, $order, $reverse, $include, $exclude])* ||
| *$hits = $db-&gt;dbQuery([$search, @$topics, $order, $reverse, $include, $exclude, $hits])* ||
| =webDB= | database object |
| =search= | search clause |
| =topics= | restrict search to this list of topics |
Expand All @@ -375,9 +393,19 @@ The DBQUERY functionality can be access from within perl using the plugins =dbQu
| =reverse=on,off= | revert the sorting order |
| =include= | pattern that topic names must match to be considered a hit |
| =exclude= | pattern that topic names must _not_ match to be considered a hit |
| =hits= | a result object holding all found topics; will be augmented if provided to =dbQuery= as an optional parameter |

Example usage:

<verbatim>
my $db = Foswiki::Plugins::DBCachePlugin::getDB("Main");
my $hits = $db->dbQuery($query, undef, $order, $reverse, $include, $exclude);

returns a list pointer of all found topics names, a hash pointer to an array of all found
topic objects (keys are the name of the topics) and a possibly defined error message.
while (my $obj = $hits->next) {
my $topicName = $obj->fastget("topic");
...
}
</verbatim>

---++++ getFormField()
Access the formfield value of an arbitrary topic.
Expand All @@ -388,8 +416,6 @@ Access the formfield value of an arbitrary topic.

returns the value of the named formfield

<!-- %RED%TODO: document the rest of the methods %ENDCOLOR% -->

---++ Plugin Settings
See also DBCacheContrib for additional configuration settings.

Expand Down Expand Up @@ -422,6 +448,9 @@ automatically from there on.
* Set SHORTDESCRIPTION = Lightweight frontend to the <nop>DBCacheContrib
-->
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 09 Sep 2015: | new perl api to register index handlers; \
moved indexing meta comments to Foswiki:Extensions/MetaCommentPlugin;\
fixed fallback of polymorphic calling semantics of DBCALL{topic->method} |
| 31 Aug 2015: | added support for DBQUERY of multiple webs |
| 25 May 2015: | added =sections= to database, a list of known named sections |
| 04 Mar 2015: | fixing interaction between DBDUMP generating a heading and Foswiki:Extensions/EditChapterPlugin |
Expand Down Expand Up @@ -556,8 +585,8 @@ automatically from there on.
%META:FIELD{name="Author" title="Author" value="Michael Daum"}%
%META:FIELD{name="Copyright" title="Copyright" value="2005-2015, Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Release" title="Release" value="31 Aug 2015"}%
%META:FIELD{name="Release" title="Release" value="%$RELEASE%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/DBCachePlugin"}%
%META:FIELD{name="Home" title="Home" value="Foswiki:Extensions/%TOPIC%"}%
%META:FIELD{name="Support" title="Support" value="Foswiki:Support/%TOPIC%"}%
%META:FIELD{name="Version" title="Version" value="8.00"}%
%META:FIELD{name="Version" title="Version" value="%$VERSION%"}%
10 changes: 8 additions & 2 deletions lib/Foswiki/Plugins/DBCachePlugin.pm
Expand Up @@ -25,15 +25,16 @@ use Foswiki::Plugins();
#Monitor::MonitorMethod('Foswiki::Contrib::DBCachePlugin::Core');
#Monitor::MonitorMethod('Foswiki::Contrib::DBCachePlugin::WebDB');

our $VERSION = '8.00';
our $RELEASE = '31 Aug 2015';
our $VERSION = '9.00';
our $RELEASE = '09 Sep 2015';
our $NO_PREFS_IN_TOPIC = 1;
our $SHORTDESCRIPTION = 'Lightweighted frontend to the DBCacheContrib';

our $isInitialized;
our $addDependency;
our $isEnabledSaveHandler;
our $isEnabledRenameHandler;
our @knownIndexTopicHandler = ();

###############################################################################
# plugin initializer
Expand Down Expand Up @@ -119,6 +120,7 @@ sub initPlugin {
sub finishPlugin {

my $session = $Foswiki::Plugins::SESSION;
@knownIndexTopicHandler = ();
delete $session->{dbcalls};
}

Expand Down Expand Up @@ -269,6 +271,10 @@ sub getTopicTitle {
return Foswiki::Plugins::DBCachePlugin::Core::getTopicTitle(@_);
}

sub registerIndexTopicHandler {
push @knownIndexTopicHandler, shift;
}

###############################################################################
# SMELL: remove this when Foswiki::Cache got into the core
sub nullHandler { }
Expand Down
18 changes: 12 additions & 6 deletions lib/Foswiki/Plugins/DBCachePlugin/Core.pm
Expand Up @@ -251,8 +251,10 @@ sub getTopicTitle {
if ($rev) {
my ($meta) = Foswiki::Func::readTopic($web, $topic, $rev);

my $topicTitleField = Foswiki::Func::getPreferencesValue("TOPICTITLE_FIELD") || "TopicTitle";

# read the formfield value
$topicTitle = $meta->get('FIELD', 'TopicTitle');
$topicTitle = $meta->get('FIELD', $topicTitleField);
$topicTitle = $topicTitle->{value} if $topicTitle;

# read the topic preference
Expand Down Expand Up @@ -520,25 +522,25 @@ sub findTopicMethod {
$topicType =~ s/^\s+//o;
$topicType =~ s/\s+$//o;

writeDebug(".... topicType=$topicType");
#writeDebug(".... topicType=$topicType");

writeDebug("1");
#writeDebug("1");

# find it in the web where this type is implemented
my $topicTypeObj = $formDB->fastget($topicType);
next unless $topicTypeObj;

writeDebug("2");
#writeDebug("2");

$form = $topicTypeObj->fastget('form');
next unless $form;

writeDebug("3");
#writeDebug("3");

$formObj = $topicTypeObj->fastget($form);
next unless $formObj;

writeDebug("4");
#writeDebug("4");

my $targetWeb;
my $target = $formObj->fastget('Target');
Expand All @@ -555,8 +557,12 @@ sub findTopicMethod {
return ($targetWeb, $theMethod) if $targetDB && $targetDB->fastget($theMethod);

#writeDebug("6");

return ($targetWeb, $theTopic) if $targetDB && $targetDB->fastget($theTopic);
}



writeDebug("... nothing found");
return;
}
Expand Down
72 changes: 30 additions & 42 deletions lib/Foswiki/Plugins/DBCachePlugin/WebDB.pm
Expand Up @@ -80,40 +80,40 @@ sub onReload {
my ($this, $topics) = @_;

writeDebug("called onReload()");
my $topicTitleField = Foswiki::Func::getPreferencesValue("TOPICTITLE_FIELD") || "TopicTitle";

foreach my $topicName (@$topics) {
my $topic = $this->fastget($topicName);
foreach my $topic (@$topics) {
my $obj = $this->fastget($topic);

# anything we get to see here should be in the dbcache already.
# however we still check for odd topics that did not make it into the cache
# for some odd reason
unless ($topic) {
writeDebug("trying to load topic '$topicName' in web '$this->{web}' but it wasn't found in the cache");
unless ($obj) {
writeDebug("trying to load topic '$topic' in web '$this->{web}' but it wasn't found in the cache");
next;
}

# get meta object
my ($meta, $text) = Foswiki::Func::readTopic($this->{web}, $topicName);
my ($meta, $text) = Foswiki::Func::readTopic($this->{web}, $topic);
my $origText = $text;

# SMELL: call getRevisionInfo to make sure the latest revision is loaded
# for get('TOPICINFO') further down the code
$meta->getRevisionInfo();

writeDebug("reloading $topicName");
writeDebug("reloading $topic");

# createdate
my ($createDate, $createAuthor) = Foswiki::Func::getRevisionInfo($this->{web}, $topicName, 1);
$topic->set('createdate', $createDate);
$topic->set('createauthor', $createAuthor);
my ($createDate, $createAuthor) = Foswiki::Func::getRevisionInfo($this->{web}, $topic, 1);
$obj->set('createdate', $createDate);
$obj->set('createauthor', $createAuthor);

# get default section
my $defaultSection = $text;
$defaultSection =~ s/.*?%STARTINCLUDE%//s;
$defaultSection =~ s/%STOPINCLUDE%.*//s;

#applyGlue($defaultSection);
$topic->set('_sectiondefault', $defaultSection);
$obj->set('_sectiondefault', $defaultSection);

# get named sections

Expand All @@ -128,23 +128,23 @@ sub onReload {
my $name = $attrs->{name} || $attrs->{_DEFAULT} || '';
my $sectionText = $2;
push @sections, $name;
$topic->set("_section$name", $sectionText);
$obj->set("_section$name", $sectionText);
}
$topic->set('_sections', join(", ", @sections));
$obj->set('_sections', join(", ", @sections));

# get topic title

# 1. get from preferences
my $topicTitle = $this->getPreference($topic, 'TOPICTITLE');
my $topicTitle = $this->getPreference($obj, 'TOPICTITLE');

# 2. get from form
unless (defined $topicTitle && $topicTitle ne '') {
my $form = $topic->fastget('form');
my $form = $obj->fastget('form');
if ($form) {

#print STDERR "trying form\n";
$form = $topic->fastget($form);
$topicTitle = $form->fastget('TopicTitle') || '';
$form = $obj->fastget($form);
$topicTitle = $form->fastget($topicTitleField) || '';
$topicTitle = urlDecode($topicTitle);
}
}
Expand All @@ -164,11 +164,11 @@ sub onReload {
#
# # strip some
# if (defined $topicTitle) {
# $topicTitle =~ s/\%TOPIC\%/$topicName/g;
# $topicTitle =~ s/\%TOPIC\%/$topic/g;
# $topicTitle =~ s/\[\[.*\]\[(.*)\]\]/$1/go;
# $topicTitle =~ s/\[\[(.*)\]\]/$1/go;
# $topicTitle =~ s/<a[^>]*>(.*)<\/a>/$1/go;
# $topicTitle = Foswiki::Func::expandCommonVariables($topicTitle, $topicName, $this->{web});
# $topicTitle = Foswiki::Func::expandCommonVariables($topicTitle, $topic, $this->{web});
# }
# }
# }
Expand All @@ -177,37 +177,25 @@ sub onReload {
unless ($topicTitle) {

#print STDERR "defaulting to topic name\n";
if ($topicName eq 'WebHome') {
if ($topic eq 'WebHome') {
$topicTitle = $this->{web};
$topicTitle =~ s/^.*[\.\/]//;
} else {
$topicTitle = $topicName;
$topicTitle = $topic;
}
}

#print STDERR "found topictitle=$topicTitle\n" if $topicTitle;
$topic->set('topictitle', $topicTitle);

# cache comments
my @comments = $meta->find('COMMENT');
my $commentDate = 0;
my $cmts;
foreach my $comment (@comments) {
my $cmt = $archivist->newMap(initial => $comment) ;
my $cmtDate = $comment->{date};
if ($cmtDate > $commentDate) {
$commentDate = $cmtDate;
}
$cmts = $topic->get('comments');
if (!defined($cmts)) {
$cmts = $archivist->newArray();
$topic->set('comments', $cmts);
}
$cmts->add($cmt);
}
if ($commentDate) {
$topic->set('commentdate', $commentDate);
$obj->set('topictitle', $topicTitle);

# call index topic handlers
my %seen;
foreach my $sub (@Foswiki::Plugins::DBCachePlugin::knownIndexTopicHandler) {
next if $seen{$sub};
&$sub($this, $obj, $this->{web}, $topic, $meta, $text);
$seen{$sub} = 1;
}

}

#print STDERR "DEBUG: DBCachePlugin::WebDB - done onReload()\n";
Expand Down

0 comments on commit d0c7785

Please sign in to comment.