Skip to content

Commit

Permalink
Item14450:Item14439:
Browse files Browse the repository at this point in the history
   * fixed restricted search
   * added random sorting of search results
   * performance improvements re-loading cache files
   * fixed "Can't use string as a HASH ref"
   * added =length()= function to formating
  • Loading branch information
MichaelDaum committed Jul 28, 2017
1 parent 323a29a commit 74afa87
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 107 deletions.
3 changes: 2 additions & 1 deletion data/System/DBCachePlugin.txt
Expand Up @@ -394,7 +394,7 @@ The DBQUERY functionality can be access from within perl using the plugins =dbQu
| =topics= | restrict search to this list of topics |
| =order= | define a sorting on the hit set; this can be any formfield accessor or \
one of the shortcuts "created" (for createdate) or "modified" (for info.date) provided \
for compatibility with default %SEARCH |
for compatibility with default %SEARCH, or "random" |
| =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 |
Expand Down Expand Up @@ -454,6 +454,7 @@ automatically from there on.
---++ Change History

%TABLE{columnwidths="7em" tablewidth="100%"}%
| 28 Jul 2017: | fixed restricted search; added random sorting of search results; performance improvements re-loading cache files; fixed "Can't use string as a HASH ref"; added =length()= function to formating |
| 16 Jan 2017: | oo-ify core and other code cleanup |
| 11 Jul 2016: | make use of clean-up feature of latest <nop>DBCacheContrib; performance improvements |
| 08 Mar 2016: | fixed use of uninitialized variable in sorting code; \
Expand Down
28 changes: 15 additions & 13 deletions lib/Foswiki/Plugins/DBCachePlugin.pm
Expand Up @@ -32,8 +32,8 @@ our $SHORTDESCRIPTION = 'Lightweighted frontend to the <nop>DBCacheContrib';

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

###############################################################################
Expand Down Expand Up @@ -101,8 +101,8 @@ sub initPlugin {
$addDependency = \&nullHandler;
}

$isEnabledSaveHandler = 1;
$isEnabledRenameHandler = 1;
@isEnabledSaveHandler = ();
@isEnabledRenameHandler = ();

return 1;
}
Expand All @@ -112,8 +112,10 @@ sub finishPlugin {

my $session = $Foswiki::Plugins::SESSION;
@knownIndexTopicHandler = ();
@isEnabledSaveHandler = ();
@isEnabledRenameHandler = ();

#$core->finish if defined $core;
$core->finish if defined $core;
$core = undef;
}

Expand Down Expand Up @@ -152,22 +154,22 @@ sub restUpdateCache {

###############################################################################
sub disableSaveHandler {
$isEnabledSaveHandler = 0;
push @isEnabledSaveHandler, 1;
}

###############################################################################
sub enableSaveHandler {
$isEnabledSaveHandler = 1;
pop @isEnabledSaveHandler;
}

###############################################################################
sub disableRenameHandler {
$isEnabledRenameHandler = 0;
push @isEnabledRenameHandler, 1;
}

###############################################################################
sub enableRenameHandler {
$isEnabledRenameHandler = 1;
pop @isEnabledRenameHandler;
}

###############################################################################
Expand All @@ -180,7 +182,7 @@ sub loadTopic {
sub afterSaveHandler {
#my ($text, $topic, $web, $meta) = @_;

return unless $isEnabledSaveHandler;
return if scalar(@isEnabledSaveHandler);

# Temporarily disable afterSaveHandler during a "createweb" action:
# The "createweb" action calls save serveral times during its operation.
Expand All @@ -205,7 +207,7 @@ sub afterSaveHandler {
# deprecated: use afterUploadSaveHandler instead
sub afterAttachmentSaveHandler {
#my ($attrHashRef, $topic, $web) = @_;
return unless $isEnabledSaveHandler;
return if scalar(@isEnabledSaveHandler);

return if $Foswiki::Plugins::VERSION >= 2.1 ||
$Foswiki::cfg{DBCachePlugin}{UseUploadHandler}; # set this to true if you backported the afterUploadHandler
Expand All @@ -216,7 +218,7 @@ sub afterAttachmentSaveHandler {
###############################################################################
# Foswiki::Plugins::VERSION >= 2.1
sub afterUploadHandler {
return unless $isEnabledSaveHandler;
return if scalar(@isEnabledSaveHandler);

my ($attrHashRef, $meta) = @_;
my $web = $meta->web;
Expand All @@ -227,7 +229,7 @@ sub afterUploadHandler {
###############################################################################
# Foswiki::Plugins::VERSION >= 2.1
sub afterRenameHandler {
return unless $isEnabledRenameHandler;
return if scalar(@isEnabledRenameHandler);

my ($web, $topic, $attachment, $newWeb, $newTopic, $newAttachment) = @_;

Expand Down
71 changes: 50 additions & 21 deletions lib/Foswiki/Plugins/DBCachePlugin/Core.pm
Expand Up @@ -47,6 +47,7 @@ sub new {
tagNameRegex => Foswiki::Func::getRegularExpression('tagNameRegex'),
webKeys => undef,
currentWeb => undef,
isModifiedDB => undef,
@_
},
$class
Expand Down Expand Up @@ -179,14 +180,13 @@ sub afterSaveHandler {

# move/rename
if ($newWeb eq $web) {

if ($topic ne $newTopic) {
$db->loadTopic($web, $newTopic)
# handled by afterSaveHandler
#$db->loadTopic($web, $newTopic)
}
} else { # crossing webs
$db = $this->getDB($newWeb);
unless ($db) {
print STDERR "WARNING: DBCachePlugin can't get cache for web '$newWeb'\n";
return;
}
$db->loadTopic($newWeb, $topic);
Expand Down Expand Up @@ -357,8 +357,7 @@ sub handleDBQUERY {
if ($theWebs eq 'all') {
@webs = Foswiki::Func::getListOfWebs();
} else {
my $webPattern = '^('.join("|", split(/\s*,\s*/, $theWebs)).')$';
@webs = grep {/$webPattern/} Foswiki::Func::getListOfWebs();
@webs = split(/\s*,\s*/, $theWebs);
}
} else {
push @webs, $thisWeb;
Expand Down Expand Up @@ -468,7 +467,7 @@ sub handleDBQUERY {
$text = _expandVariables($text, $thisWeb, $thisTopic, count=>($hits?$hits->count:0), web=>$thisWeb);
$text = _expandFormatTokens($text);

$this->fixInclude($session, $thisWeb, $text) if $theRemote;
$this->fixInclude($thisWeb, $text) if $theRemote;

return $text;
}
Expand Down Expand Up @@ -704,7 +703,7 @@ sub handleDBCALL {
delete $context->{insideInclude};

# fix local linx
$this->fixInclude($session, $thisWeb, $sectionText) if $remote;
$this->fixInclude($thisWeb, $sectionText) if $remote;

# cleanup
delete $this->{dbcalls}{$key};
Expand Down Expand Up @@ -733,6 +732,7 @@ sub handleDBSTATS {
my $theSearch = $params->{_DEFAULT} || $params->{search} || '';
my $thisWeb = $params->{web} || $baseWeb;
my $thisTopic = $params->{topic} || $baseTopic;
my $thisTopics = $params->{topics};
my $thePattern = $params->{pattern} || '^(.*)$';
my $theSplit = $params->{split} || '\s*,\s*';
my $theHeader = $params->{header} || '';
Expand Down Expand Up @@ -782,16 +782,22 @@ sub handleDBSTATS {
my $theDB = $this->getDB($thisWeb);
return _inlineError("ERROR: DBSTATS can't find web '$thisWeb'") unless $theDB;

my @topicNames = $theDB->getKeys();
my @topicNames;
if ($thisTopics) {
@topicNames = split(/\s*,\s*/, $thisTopics);
} else {
@topicNames = $theDB->getKeys();
}
foreach my $topicName (@topicNames) { # loop over all topics
my $topicObj = $theDB->fastget($topicName);
next unless $topicObj;
next if $search && !$search->matches($topicObj); # that match the query
next unless $theDB->checkAccessPermission('VIEW', $wikiName, $topicObj);

#_writeDebug("found topic $topicName");
my $createDate = $topicObj->fastget('createdate');
my $modified = $topicObj->get('info.date');
my $publishDate = $topicObj->get('publishdate');
my $publishDate = $topicObj->get('publishdate') || 0;
foreach my $field (split(/\s*,\s*/, $theFields)) { # loop over all fields
my $fieldValue = $topicObj->fastget($field);
if (!$fieldValue || ref($fieldValue)) {
Expand Down Expand Up @@ -827,8 +833,8 @@ sub handleDBSTATS {
$record->{createdate_to} = $createDate if $record->{createdate_to} < $createDate;
$record->{modified_from} = $modified if $record->{modified_from} > $modified;
$record->{modified_to} = $modified if $record->{modified_to} < $modified;
$record->{publishdate_from} = $publishDate if $record->{publishdate_from} > $publishDate;
$record->{publishdate_to} = $publishDate if $record->{publishdate_to} < $publishDate;
$record->{publishdate_from} = $publishDate if defined $publishDate && $record->{publishdate_from} > $publishDate;
$record->{publishdate_to} = $publishDate if defined $publishDate && $record->{publishdate_to} < $publishDate;
push @{$record->{topics}}, $topicName;
} else {
my %record = (
Expand Down Expand Up @@ -891,7 +897,10 @@ sub handleDBSTATS {
} keys %statistics
} elsif ($theSort eq 'count') {
@sortedKeys = sort {
$statistics{$a}->{count} <=> $statistics{$b}->{count}
$statistics{$a}->{count} <=> $statistics{$b}->{count} or
$statistics{$b}->{modified_from} <=> $statistics{$a}->{modified_from} or # just to break ties
$statistics{$b}->{modified_to} <=> $statistics{$a}->{modified_to} or
$a cmp $b
} keys %statistics
} else {
@sortedKeys = sort keys %statistics;
Expand Down Expand Up @@ -924,6 +933,8 @@ sub handleDBSTATS {
last if $theLimit && $index == $theLimit;
}

return "" unless @result;

my $text = _expandVariables($theHeader.join($theSep, @result).$theFooter, $thisWeb, $thisTopic,
'min'=>$min,
'max'=>$max,
Expand Down Expand Up @@ -1000,9 +1011,9 @@ sub handleDBRECURSE {
($thisWeb, $thisTopic) =
Foswiki::Func::normalizeWebTopicName($thisWeb, $thisTopic);

$params->{format} ||= ' $indent* [[$web.$topic][$topic]]';
$params->{format} //= ' $indent* [[$web.$topic][$topic]]';
$params->{single} ||= $params->{format};
$params->{separator} ||= $params->{sep} || "\n";
$params->{separator} //= $params->{sep} // "\n";
$params->{header} ||= '';
$params->{subheader} ||= '';
$params->{singleheader} ||= $params->{header};
Expand Down Expand Up @@ -1068,8 +1079,7 @@ sub formatRecursive {
my ($this, $theDB, $theWeb, $theTopic, $params, $seen, $depth, $number) = @_;

# protection agains infinite recursion
my %thisSeen;
$seen ||= \%thisSeen;
$seen ||= {};
return if $seen->{$theTopic};
$seen->{$theTopic} = 1;
$depth ||= 0;
Expand Down Expand Up @@ -1194,8 +1204,17 @@ sub getDB {
#_writeDebug("webKey=$webKey");

my $db = $webDB{$webKey};
my $isModified = 1;
$isModified = $db->getArchivist->isModified() if $db;
my $isModified;

if ($db) {
$isModified = $this->{isModifiedDB}{$webKey};
unless (defined $isModified) {
$isModified = $db->getArchivist->isModified();
$this->{isModifiedDB}{$webKey} = 0 unless $isModified; # only cache a negative result
}
} else {
$isModified = 1;
}

if ($isModified) {
$db = $webDB{$webKey} = $this->newDB($theWeb);
Expand All @@ -1206,6 +1225,7 @@ sub getDB {
my $baseWeb = $Foswiki::Plugins::SESSION->{webName};
my $baseTopic = $Foswiki::Plugins::SESSION->{topicName};
$db->load($refresh, $baseWeb, $baseTopic);
$this->{doRefresh} = 0;
}

return $db;
Expand All @@ -1232,13 +1252,22 @@ sub unloadDB {

delete $webDB{$web};
delete $this->{webKeys}{$web};
delete $this->{isModifiedDB}{$web};
}

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

undef $this->{isModifiedDB};
undef $this->{dbcalls};
undef $this->{currentWeb};
}

###############################################################################
# from Foswiki::_INCLUDE
sub fixInclude {
my $this = shift;
my $session = shift;
my $thisWeb = shift;
# $text next

Expand All @@ -1253,7 +1282,7 @@ sub fixInclude {
$_[0] = _takeOutBlocks($_[0], 'noautolink', $removed);

# 'TopicName' to 'Web.TopicName'
$_[0] =~ s/(^|[\s(])($this->{webNameRegex}\.$$this->{wikiWordRegex})/$1$TranslationToken$2/g;
$_[0] =~ s/(^|[\s(])($this->{webNameRegex}\.$this->{wikiWordRegex})/$1$TranslationToken$2/g;
$_[0] =~ s/(^|[\s(])($this->{wikiWordRegex})/$1\[\[$thisWeb\.$2\]\[$2\]\]/g;
$_[0] =~ s/(^|[\s(])$TranslationToken/$1/g;

Expand Down Expand Up @@ -1346,7 +1375,7 @@ sub _dbDumpArray {
foreach my $obj (sort $array->getValues()) {
$result .= "<tr><th>";
if (UNIVERSAL::can($obj, "fastget")) {
$result .= $obj->fastget('name');
$result .= ($obj->fastget('name') || '');
} else {
$result .= $index;
}
Expand Down

0 comments on commit 74afa87

Please sign in to comment.