adding RandomThread macro thanks to Wouter Vijvers

This commit is contained in:
JT Smith 2005-12-01 06:40:27 +00:00
parent ff6fa1c893
commit 440ca63333
6 changed files with 245 additions and 0 deletions

View file

@ -4,6 +4,7 @@
about 13 times faster than Date::Manip. Depending upon how date intensive
your pages are, you will see a 2% to 100% performance increase from this
change.
- Added RandomThread macro thanks to Wouter Vijvers.
- Switched from Apache::Registry/CGI to a pure mod_perl2 interface, which
increased performance by over 70% to the entire system, and in some cases
as much as 100%. See gotcha.txt for details.

View file

@ -763,6 +763,7 @@ sub updateConfigFile {
push(@{$newConfig{assets}}, "WebGUI::Asset::Wobject::StockData") unless isIn("WebGUI::Asset::Wobject::StockData",@{$newConfig{assets}});
push(@{$newConfig{assets}}, "WebGUI::Asset::Wobject::WeatherData") unless isIn("WebGUI::Asset::Wobject::WeatherData",@{$newConfig{assets}});
push(@{$newConfig{assets}}, "WebGUI::Asset::Wobject::MultiSearch") unless isIn("WebGUI::Asset::Wobject::MultiSearch",@{$newConfig{assets}});
$newConfig{macros}{RandomThread}{RandomThread};
$newConfig{gateway} = "/";
$conf->purge;
$conf->set(%newConfig);

View file

@ -224,6 +224,7 @@ macros=# => Hash_userId, \
PageTitle => PageTitle, \
PageUrl => PageUrl, \
RandomAssetProxy => RandomAssetProxy, \
RandomThread => RandomThread, \
RawHeadTags => RawHeadTags, \
RootTitle => RootTitle, \
Spacer => Spacer, \

View file

@ -0,0 +1,31 @@
package WebGUI::Help::Macro_RandomThread;
our $HELP = {
'random thread' => {
title => 'random thread title',
body => 'random thread body',
fields => [
],
related => [
{
tag => 'macros using',
namespace => 'Macros'
},
{
tag => 'macros list',
namespace => 'Macros'
},
{
tag => 'random asset proxy',
namespace => 'Macro_RandomAssetProxy'
},
{
tag => 'collaboration post list template variables',
namespace => 'Asset_Collaboration'
}
]
},
};
1;

View file

@ -0,0 +1,145 @@
package WebGUI::Macro::RandomThread;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2005 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use WebGUI::Asset;
use WebGUI::Asset::Template;
use WebGUI::ErrorHandler;
use WebGUI::Macro;
use WebGUI::Session;
use WebGUI::Utility;
=head1 NAME
Package WebGUI::Macro::RandomThread
=head1 DESCRIPTION
This macro displays the starting post of a random thread on the website (a "submission" in the pre-6.3 WebGUI language). The thread is chosen from a (possibly random) Collaboration System based on the parameters. The way the post is displayed is controlled by a template.
=head1 SYNOPSIS
^RandomThread( [ startURL, relatives, templateURL ] );
=head1 METHODS
These functions are available from this package:
=cut
#-------------------------------------------------------------------
=head2 process ( [ startURL, relatives, templateURL ] )
Main function that returns the HTML output of the macro.
=head3 startURL
URL of the asset you want to use as the starting point for finding a random CS. If omitted it defaults to 'home' (i.e. the root page of most websites). Must be a valid URL within WebGUI.
=head3 relatives
Only Collaboration Systems that are relatives of the asset in the starting point in the way specified by this parameter are used. Allowed values for this parameter are 'siblings', 'children', 'ancestors', 'self', 'descendants' and 'pedigree'. Default value is descendants. So, if the first two parameters are omitted you get a random thread from all the Collaboration Systems on your site (or more accurately: your home-root).
=head3 templateURL
URL of the template to use to display the random thread. Must be a valid URL within WebGUI. IMPORTANT NOTE: if omitted, a default debug template is used that outputs a list of all the available template variables. Since you almost certainly will not want this output in a production-environment, it makes sense to not omit this parameter.
=cut
sub process {
my ($startURL, $relatives, $templateURL) = @_;
# Seed the randomizer:
srand;
# Set defaults (default template is set by id later):
$startURL ||= 'home';
$relatives ||= 'descendants';
my $numberOfTries = 2; # try this many times in case we select a thread the user cannot view
# Sanity check of parameters:
my $startAsset = WebGUI::Asset->newByUrl($startURL);
unless ($startAsset) {
WebGUI::ErrorHandler::warn('Error: invalid startURL. Check parameters of macro on page '.$session{asset}->get('url'));
return '';
}
$relatives = lc($relatives);
unless ( isIn($relatives, ('siblings','children','ancestors','self','descendants','pedigree')) ) {
WebGUI::ErrorHandler::warn('Error: invalid relatives specified. Must be one of siblings, children, ancestors, self, descendants, pedigree. Check parameters of macro on page '.$session{asset}->get('url'));
return '';
}
my $template = $templateURL ? WebGUI::Asset::Template->newByUrl($templateURL) : WebGUI::Asset::Template->new('WVtmpl0000000000000001');
unless ($template) {
WebGUI::ErrorHandler::warn('Error: invalid template URL. Check parameters of macro on page '.$session{asset}->get('url'));
return '';
}
# Get all CS's that we'll use to pick a thread from:
my $lineage = $startAsset->getLineage([$relatives],{includeOnlyClasses => ['WebGUI::Asset::Wobject::Collaboration']});
unless ( scalar(@{$lineage}) ) {
WebGUI::ErrorHandler::warn('Error: no Collaboration Systems found with current parameters. Check parameters of macro on page '.$session{asset}->get('url'));
return '';
}
# Try to get a random thread that the user can see:
my $randomThread = _getRandomThread($lineage);
my $i = 0;
while ($i < $numberOfTries) {
if($randomThread->canView()) {
# Get all vars and process template:
my $var = $randomThread->getTemplateVars;
return $template->process($var);
} else {
# Keep trying until we find a thread we can actually view:
$randomThread = _getRandomThread($lineage);
$i++;
}
}
# If we reach this point, we had no success in finding an asset the user can view:
WebGUI::ErrorHandler::warn("Could not find a random thread that was viewable by the user $session{user}{username} after $numberOfTries tries. Check parameters of macro on page ".$session{asset}->get('url'));
return '';
}
#-------------------------------------------------------------------
=head2 _getRandomThread ( lineage )
Helper function that returns a random thread.
=head3 lineage
Reference to an array with lineage of Collaboration Systems to select a random thread from.
=cut
sub _getRandomThread {
my $lineage = shift;
# Get random CS:
my $randomIndex = int(rand(scalar(@{$lineage})));
my $randomCSId = $lineage->[$randomIndex];
my $randomCS = WebGUI::Asset->new($randomCSId,'WebGUI::Asset::Wobject::Collaboration');
# Get random thread in that CS:
$lineage = $randomCS->getLineage(['children'],{includeOnlyClasses => ['WebGUI::Asset::Post::Thread']});
$randomIndex = int(rand(scalar(@{$lineage})));
my $randomThreadId = $lineage->[$randomIndex];
return WebGUI::Asset->new($randomThreadId,'WebGUI::Asset::Post::Thread');
}
1;

View file

@ -0,0 +1,66 @@
package WebGUI::i18n::English::Macro_RandomThread;
our $I18N = {
'macroName' => {
message => q|Random Thread|,
lastUpdated => 1132970060,
},
'random thread title' => {
message => q|Random Thread Macro|,
lastUpdated => 1132970060,
},
'random thread body' => {
message => q|<b>&#94;RandomThread( [ startURL, relatives, templateURL ] );</b><br />
<p>The Collaboration System can be used for much more than just a forum. A few examples of its
possible usages are a FAQ, photo gallery, job listings, quotes database or weblog. For these
applications (and others that you might think of) it can be nice to display a random entry
from such a CS somewhere on your site. That is exactly the functionality that this macro provides.</p>
<p>It displays the start post of a thread that is randomly selected from a (possibly random) CS,
depending on the parameters. The way the selected post is displayed is controlled by a template. All
the template variables that are normally available in a CS Post template are available in this macro as well.</p>
<p><b>Parameters</b></p>
<p>Although all the parameters can be omitted, it usually makes sense specify them all. If you
want to display a random thread from a single CS, we suggest you use the URL of the CS as the <i>startURL</i>
and &quot;self&quot; as <i>relatives</i>.</p>
<dl>
<dt><i>startURL</i></dt>
<dd>URL of the asset you want to use as the starting point for finding a random CS. If omitted
it defaults to 'home' (i.e. the root page of most websites). Must be a valid URL within WebGUI.</dd>
<dt><i>relatives</i></dt>
<dd>Only posts from Collaboration Systems that are relatives of the start-asset in this way
are used. Allowed values for this parameter are 'siblings', 'children', 'ancestors', 'self',
'descendants' and 'pedigree'. Default value is descendants.</dd>
<dt><i>templateURL</i></dt>
<dd>URL of the template to use to display the random thread. Must be a valid URL within WebGUI.
<br /><br />IMPORTANT NOTE: if omitted, a default debug template is used that outputs a list of all the
available template variables. Since you almost certainly will not want this output in a
production-environment, it makes sense to not omit this parameter.</dd>
</dl>
<p><b>Examples</b></p>
<dl>
<dt><tt>&#94;RandomThread(home/photo_album, descendants, templates/randomPhoto);</tt></dt>
<dd>If you have a page with many subpages with photo galleries, you can use the parameters above
to easily retrieve a random thumbnail from all your photo albums.</dd>
<dt><tt>&#94;RandomThread(home/quotes/quotes-db, self, templates/randomQuote);</tt></dt>
<dd>If you have one CS that you use to keep a database of interesting quotes, you could use
the above example to display a random quote on your website.</dd>
<dt><tt>&#94;RandomThread(home/faq, children, templates/faq);</tt></dt>
<dd>Suppose you have one page with a couple of Collaboration Systems (for different categories
of questions), then the example above can be used to display a random question from any category.</dd>
<dt><tt>&#94;RandomThread;</tt></dt>
<dd>Gets a random post from all Collaboration Systems in root 'home' with debug output.</dd>
</dl>
|,
lastUpdated => 1132970060,
},
};
1;