[OGDev] A couple of recent hacks.
Kake L Pugh
kake at earth.li
Wed Feb 28 01:09:51 GMT 2007
Here are a few things that people might be interested in. They all
might require tweaking for your particular guide; don't run them
unless you understand them.
First of all, an importer thingy that reads data from a YAML file and
uses WWW::Mechanize to add it to a guide. (It won't overwrite
existing data; it checks first and warns you if you're attempting to
create a page that already exists.) Because it's YAML, you can't use
asterisks in the content or it gets confused. If you don't use
streetmap x/y links and OS x/y coords then you'll need to change that
part. You can include any fields in the YAML file that are present in
the edit form. This is mainly useful for creating stub pages so a
location search will find them. I've used it to add all London's Tube
stations and Good Beer Guide pubs.
rgl.pl is the importer and rgldata is an example datafile.
Secondly, a search thingy that finds all things in category A within N
metres of things in Category B. It's quite fast and efficient because
it uses raw SQL rather than the abstraction layers. It may be
Postgres-specific, and it's certainly OS X/Y specific, but it should
be obvious how to change that to suit your own guide. An example
(plan your own Circle Line pub crawl):
http://london.randomness.org.uk/find.cgi?cat1=Circle+Line&distance=650&cat2=Pubs&do_search=1
This is, unsurprisingly, find.cgi.
Finally, a javascript snippet to find nearest Tube stations and show
what lines they're on. Again, will need tweaking for non-London guides.
show_tube_stations.html goes somewhere in node.tt and show_tube_stations.js
goes in header.tt. nearest-tube.cgi lives in the same directory as wiki.cgi
Hope these are of interest.
Kake
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rgl.pl
Type: text/x-perl
Size: 2878 bytes
Desc: not available
Url : http://lists.openguides.org/pipermail/openguides-dev/attachments/20070228/14fa24d7/attachment.pl
-------------- next part --------------
---
pagename: Brewery Tap, TW8 8BD
postcode: TW8 8BD
address: 47 Catherine Wheel Road
hours_text: noon-midnight daily
extra_content: The Good Beer Guide says you should book for Sunday lunch, since lunches are popular. Food served until 7:30pm weekdays
map_link: http://www.streetmap.co.uk/newmap.srf?x=517710&y=177302
locales: TW8, Brentford
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Express Tavern, TW8 0EW
postcode: TW8 0EW
address: 56 Kew Bridge Road
hours_text: 11:30am-11pm Mon-Wed; 11:30am-midnight Thu-Fri; 6:30pm-midnight Sat; noon-10:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=518943&y=178067
locales: TW8, Brentford
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Magpie and Crown, TW8 8EW
postcode: TW8 8EW
address: 128 High Street
hours_text: 11am-midnight Mon-Wed; 11am-1am Thu-Sat; noon-midnight Sun
extra_content: Thai food available evenings Tuesday-Saturday.
map_link: http://www.streetmap.co.uk/newmap.srf?x=517475&y=177320
locales: TW8, Brentford
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Real Cider, Thai Food, Food Served Lunchtimes, Food Served Evenings
---
pagename: Jolly Gardeners, TW5 9WB
postcode: TW5 9WB
address: 144 High Street
hours_text: 11am-midnight Mon-Thu; 11am-2am Fri-Sat; noon-11pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=510789&y=177570
locales: TW5, Cranford
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Queen's Head, TW5 9PB
postcode: TW5 9PB
address: 123 High Street
hours_text: 11am-midnight Mon-Sat; noon-11pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=510826&y=177438
locales: TW5, Cranford
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Moon on the Square, TW13 4AU
postcode: TW13 4AU
address: 30 The Centre, High Street
extra_content: Staff are friendly.
hours_text: 9am-midnight Mon-Thu; 9am-1am Fri-Sat; 9am-midnight Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=510581&y=173123
locales: TW13, Feltham
summary: Good Beer Guide Wetherspoons pub very close to Feltham station.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Real Cider, Food Served Lunchtimes, Food Served Evenings, Wetherspoons
---
pagename: Roebuck, TW12 1JN
postcode: TW12 1JN
address: 72 Hampton Road
hours_text: 11am-11pm Mon-Thu; 11am-11:30pm Fri-Sat; noon-4pm, 7pm-11pm Sun
extra_content: Lunch served weekdays.
map_link: http://www.streetmap.co.uk/newmap.srf?x=514607
locales: TW12, Hampton Hill
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Botwell Inn, UB3 3EB
postcode: UB3 3EB
address: 25-29 Coldharbour Lane
hours_text: 9am-midnight Mon-Thu; 9am-1am Fri-Sat; 9am-midnight Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=509990&y=180014
locales: UB3, Hayes, Hayes Town
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Wetherspoons, Food Served Lunchtimes, Food Served Evenings
---
pagename: Star, UB10 0LY
postcode: UB10 0LY
address: Blenheim Parade, Uxbridge Road
hours_text: 11am-11pm Mon-Thu; 11am-1am Fri-Sat; noon-10:30pm Sun
extra_content: Only food served is Sunday roast.
map_link: http://www.streetmap.co.uk/newmap.srf?x=507948&y=182313
locales: UB10, Hillingdon, Hayes End
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007
---
pagename: Moon Under Water, TW3 3LF
postcode: TW3 3LF
address: 84-86 Staines Road
hours_text: 10am-11pm Mon-Sat; noon-10:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=513367&y=175485
locales: TW3, Hounslow
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Wetherspoons, Real Cider, Food Served Lunchtimes, Food Served Evenings
---
pagename: Red Lion, TW7 6QJ
postcode: TW7 6QJ
address: 92-94 Linkfield Road
hours_text: 11am-11pm Mon-Thu; 11am-midnight Fri-Sat; noon-11pm Sun
extra_content: Lunches served weekdays.
map_link: http://www.streetmap.co.uk/newmap.srf?x=515861&y=176247
locales: TW7, Isleworth
website: http://red-lion.info/
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Real Cider, Food Served Lunchtimes
---
pagename: Royal Oak, TW7 6EP
postcode: TW7 6EP
address: 128 Worton Road
hours_text: noon-11pm Mon-Sat; noon-10:30pm Sun
extra_content: Food served all day. Riverside patio.
map_link: http://www.streetmap.co.uk/newmap.srf?x=515468&y=175559
locales: TW7, Isleworth
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Conservative and Unionist Club, UB1 3HB
postcode: UB1 3HB
address: Fairlawn, High Street
hours_text: 11:30am-2:30pm, 7pm-11pm Mon-Thu; 11:30am-3pm, 6pm-11pm Fri-Sat; noon-3pm, 7pm-10:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=512884&y=180464
locales: UB1, Southall
summary: Private club in the Good Beer Guide.
extra_content: All welcome on production of CAMRA membership card or a copy of the 2007 Good Beer Guide.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Lion, TW11 9DN
postcode: TW11 9DN
address: 27 Wick Road
website: http://www.thelionpub.co.uk/
hours_text: noon-11pm Mon-Tue; noon-11:30pm Wed-Thu; noon-midnight Fri-Sat; noon-11pm Sun
extra_content: No food Sunday evenings.
map_link: http://www.streetmap.co.uk/newmap.srf?x=517009&y=170132
locales: TW11, Teddington, Hampton Wick
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Queen Dowager, TW11 0HU
postcode: TW11 0HU
address: 49 North Lane
hours_text: 11am-11pm Mon-Sat; noon-10:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=515683&y=170827
locales: TW11, Teddington
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Cabbage Patch, TW1 3SZ
postcode: TW1 3SZ
address: 67 London Road
hours_text: 11am-midnight Mon-Sat; noon-11:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=516188&y=173453
locales: TW1, Twickenham
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Fox, TW1 3NR
postcode: TW1 3NR
address: 39 Church Street
hours_text: 11am-11pm Mon-Wed; 11am-12:30am Thu-Sat; 11am-11pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=516353&y=173290
locales: TW1, Twickenham
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes
---
pagename: Old Anchor, TW1 3AW
postcode: TW1 3AW
address: 71 Richmond Road
hours_text: noon-11pm Mon-Thu; noon-midnight Fri-Sat; noon-10:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=516609&y=173606
locales: TW1, Twickenham
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Prince Albert, TW2 5QB
postcode: TW2 5QB
address: 30 Hampton Road
hours_text: 11am-11pm Mon-Thu; 11am-midnight Fri-Sat; 11am-11pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=515150&y=172741
locales: TW2, Twickenham
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Thai Food, Food Served Lunchtimes, Food Served Evenings
---
pagename: Prince Blucher, TW2 5AG
postcode: TW2 5AG
address: 124 The Green
hours_text: 11am-11pm Mon-Thu; 11am-midnight Fri-Sat; noon-11pm Sun
extra_content: Home-made food served all day.
map_link: http://www.streetmap.co.uk/newmap.srf?x=515218&y=173024
locales: TW2, Twickenham
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Turk's Head, TW1 1LF
postcode: TW1 1LF
address: 28 Winchester Road
hours_text: noon-11pm Mon-Wed; noon-11:30pm Thu; noon-midnight Fri-Sat; noon-10:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=516612&y=174348
locales: TW1, Twickenham
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
pagename: Load of Hay, UB8 2PU
postcode: UB8 2PU
address: 33 Villier Street
hours_text: 11am-11:30pm Mon-Sat; noon-11:30pm Sun
map_link: http://www.streetmap.co.uk/newmap.srf?x=505685&y=182994
locales: UB8, Uxbridge
summary: Good Beer Guide pub.
categories: Good Beer Guide, Pubs, Good Beer Guide 2007, Food Served Lunchtimes, Food Served Evenings
---
-------------- next part --------------
#!/opt/csw/bin/perl
use strict;
use warnings;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );
use OpenGuides;
use OpenGuides::Config;
use Wiki::Toolkit::Plugin::Locator::Grid;
my $config_file = $ENV{OPENGUIDES_CONFIG_FILE} || "wiki.conf";
my $config = OpenGuides::Config->new( file => $config_file );
my $guide = OpenGuides->new( config => $config );
my $wiki = $guide->wiki;
my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new( x => "os_x", y => "os_y" );
$wiki->register_plugin( plugin => $locator );
my $formatter = $wiki->formatter;
my $q = CGI->new;
print $q->header;
my $self_url = $q->url( -relative );
print <<EOHTML;
<head>
<link rel="stylesheet" href="http://london.randomness.org.uk/london.css"
type="text/css" />
<title>Randomness guide Kakesearch</title>
</head>
<body>
<h1 id="header">Randomness guide Kakesearch</h1>
<div id="content">
EOHTML
print_form();
if ( $q->param( "do_search" ) ) {
my $cat1 = $q->param( "cat1" );
my $cat2 = $q->param( "cat2" );
my $dist = $q->param( "distance" );
$dist ||= 0;
$dist =~ s/[^0-9]//g;
if ( !$dist || !$cat1 || !$cat2 ) {
print "<p>Must supply both categories, and a distance.</p>";
} else {
my $dbh = $wiki->store->dbh;
my $sql = "
SELECT node.id, node.name, mx.metadata_value as x, my.metadata_value as y
FROM node
INNER JOIN metadata as mc
ON ( node.id=mc.node_id
AND node.version=mc.version
AND lower(mc.metadata_type)='category'
AND lower(mc.metadata_value)=?)
INNER JOIN metadata as mx
ON ( node.id=mx.node_id
AND node.version=mx.version
AND lower(mx.metadata_type)='os_x' )
INNER JOIN metadata as my
ON ( node.id=my.node_id
AND node.version=my.version
AND lower(my.metadata_type)='os_y' )
ORDER BY node.name
";
my $sth = $dbh->prepare( $sql );
my @cat1stuff;
my @cat2stuff;
$sth->execute( lc( $cat1 ) ) or die $dbh->errstr;
while ( my ( $id, $name, $x, $y ) = $sth->fetchrow_array ) {
push @cat1stuff, { id => $id, name => $name, x => $x, y => $y };
}
$sth->execute( lc( $cat2 ) ) or die $dbh->errstr;
while ( my ( $id, $name, $x, $y ) = $sth->fetchrow_array ) {
push @cat2stuff, { id => $id, name => $name, x => $x, y => $y };
}
my @results;
foreach my $origin ( @cat1stuff ) {
my @thisres;
foreach my $end ( @cat2stuff ) {
my $thisdist = int( sqrt( ( $origin->{x} - $end->{x} )**2
+ ( $origin->{y} - $end->{y} )**2
) + 0.5 );
if ( $thisdist <= $dist ) {
push @thisres, { origin => $origin, end => $end, dist => $thisdist };
}
}
@thisres = sort { $a->{dist} <=> $b->{dist} } @thisres;
push @results, @thisres;
}
if ( @results == 0 ) {
print "<p>No results, sorry.</p>";
} else {
my $base_url = $config->script_url . $config->script_name . "?";
my $last_origin = "";
print "<table border=\"1\">\n"
. "<tr><th><b>$cat1</b></th><th><b>$cat2</b></th>"
. "<th><b>Distance (metres)</b></th></tr>\n";
foreach my $set ( @results ) {
my $origin_name = $set->{origin}->{name};
my $end_name = $set->{end}->{name};
my $origin_url = $base_url
. $formatter->node_name_to_node_param( $origin_name );
my $end_url = $base_url
. $formatter->node_name_to_node_param( $end_name );
print "<tr>\n";
if ( $last_origin ne $origin_name ) {
print "<td><a href=\"$origin_url\">$origin_name</a></td>\n";
} else {
print "<td> </td>\n";
}
print "<td><a href=\"$end_url\">$end_name</a></td>\n"
. "<td>" . $set->{dist} . "</td>\n"
. "</tr>\n";
$last_origin = $origin_name;
}
print "</table>\n";
}
}
}
print <<EOHTML;
</body>
</html>
EOHTML
sub print_form {
my $any_string = " -- any -- ";
my @categories = $wiki->list_nodes_by_metadata(
metadata_type => "category",
metadata_value => "category",
ignore_case => 1,
);
@categories = map { s/^Category //; $_; } @categories;
@categories = sort( @categories );
my $catbox1 = $q->popup_menu( -name => "cat1",
-values => [ "", @categories ],
-labels => { "" => $any_string,
map { $_ => $_ } @categories }
);
my $catbox2 = $q->popup_menu( -name => "cat2",
-values => [ "", @categories ],
-labels => { "" => $any_string,
map { $_ => $_ } @categories }
);
my $distbox = qq( <input type="text" size="4" maxlength="4" name="distance");
if ( $q->param( "distance" ) ) {
$distbox .= "value=\"" . $q->param( "distance" ) . "\"";
}
$distbox .= "> metres ";
print <<EOHTML;
<form action="$self_url" method="GET">
<p>Find me things in category $catbox1 within $distbox of things in
category $catbox2.</p>
<input type="hidden" name="do_search" value="1">
<input type="submit" name="Search" value="Search">
</form>
EOHTML
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.openguides.org/pipermail/openguides-dev/attachments/20070228/14fa24d7/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: show_tube_stations.js
Type: application/x-javascript
Size: 1099 bytes
Desc: not available
Url : http://lists.openguides.org/pipermail/openguides-dev/attachments/20070228/14fa24d7/attachment.js
-------------- next part --------------
#!/opt/csw/bin/perl
use strict;
use warnings;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );
use Data::Dumper;
use OpenGuides;
use OpenGuides::Config;
use Wiki::Toolkit::Plugin::Locator::Grid;
use Wiki::Toolkit::Plugin::Categoriser;
my $config_file = $ENV{OPENGUIDES_CONFIG_FILE} || "wiki.conf";
my $config = OpenGuides::Config->new( file => $config_file );
my $guide = OpenGuides->new( config => $config );
my $wiki = $guide->wiki;
my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new( x => "os_x", y => "os_y" );
$wiki->register_plugin( plugin => $locator );
my $categoriser = Wiki::Toolkit::Plugin::Categoriser->new;
$wiki->register_plugin( plugin => $categoriser );
my $formatter = $wiki->formatter;
my $q = CGI->new;
print $q->header;
if ( $q->param( "origin" ) ) {
my $origin = $q->param( "origin" );
$origin = $formatter->node_param_to_node_name( $origin );
my @nearby = $locator->find_within_distance( node => $origin,
metres => 1000 );
my @stations = $wiki->list_nodes_by_metadata(
metadata_type => "category",
metadata_value => "Tube",
ignore_case => 1,
);
my %stationhash = map { $_ => 1 } @stations;
my $base_url = $config->script_url . $config->script_name . "?";
my @results;
foreach my $near ( @nearby ) {
if ( $stationhash{ $near } ) {
my $distance = $locator->distance( from_node => $origin,
to_node => $near );
my @lines = $categoriser->categories( node => $near );
@lines = grep { /Line$/ } @lines;
# Hack around bug in Wiki::Toolkit::Plugin::Categoriser
@lines = grep { $_ !~ /Hammersmith &/ } @lines;
my %uniq = map { $_ => 1 } @lines;
@lines = keys( %uniq );
@lines = map { s/ Line$//; $_; } @lines;
my $url = $base_url . $formatter->node_name_to_node_param( $near );
$near =~ s/ Station$//;
push @results, { name => $near, distance => $distance, lines => \@lines,
url => $url };
}
}
if ( @results == 0 ) {
print "Nothing within 1km, sorry.\n";
} else {
@results = sort { $a->{distance} <=> $b->{distance} } @results;
if ( @results > 5 ) {
@results = @results[0..4];
}
my @pretty;
foreach my $result ( @results ) {
push @pretty, "<a href=\"" . $result->{url} . "\">" . $result->{name} . "</a> (" . $result->{distance} . "m, "
. join( ", ", @{$result->{lines}} ) . ")";
}
print join( "; ", @pretty );
}
} else {
print "<p>No origin specified.</p>\n";
}
More information about the OpenGuides-Dev
mailing list