Gentoo Archives: gentoo-commits

From: "Dean Stephens (desultory)" <desultory@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-projects commit in forums/scripts: sitemap.pl
Date: Thu, 30 Jul 2009 11:36:58
Message-Id: E1MWTwO-0007br-67@stork.gentoo.org
1 desultory 09/07/30 11:36:56
2
3 Added: sitemap.pl
4 Log:
5 Basic sitemap script.
6
7 Revision Changes Path
8 1.1 forums/scripts/sitemap.pl
9
10 file : http://sources.gentoo.org/viewcvs.py/gentoo-projects/forums/scripts/sitemap.pl?rev=1.1&view=markup
11 plain: http://sources.gentoo.org/viewcvs.py/gentoo-projects/forums/scripts/sitemap.pl?rev=1.1&content-type=text/plain
12
13 Index: sitemap.pl
14 ===================================================================
15 #!/usr/bin/perl
16
17 # -----------------------------------------------------------------------------
18 #
19 # sitemap.pl
20 #
21 # date : 2009-07-30
22 # author : Dean Stephens <desultory@g.o>
23 # version : 0.1
24 # license : GPL2
25 # description : This script generates a basic site map consisting of links to
26 # -----------------------------------------------------------------------------
27 #
28 # This program is free software; you can redistribute it and/or modify it under
29 # the terms of the GNU General Public License as published by the Free Software
30 # Foundation; either version 2 of the License, or (at your option) any later
31 # version.
32 #
33 # -----------------------------------------------------------------------------
34
35 use strict;
36 use warnings;
37 use DBI;
38
39 my $connectString = "DBI:mysql:;mysql_read_default_file=~/.my.cnf;mysql_read_default_group=forum_ro";
40 my $dbh = DBI->connect( $connectString, '', '', { RaiseError => 1, AutoCommit => 0 } ) || die 'Failed to connect to the DB.';
41
42 sub query( $ ) {
43 my $sth = $dbh->prepare( shift );
44 my $return = $dbh->selectall_arrayref($sth);
45
46 $sth->finish;
47
48 return $return;
49 }
50
51 sub page_header($){
52 my $title = shift;
53
54 return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.
55 "\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n<header><title>$title</title></header>\n".
56 "<body><h1>$title</h1>\n<ul>\n";
57 }
58
59 sub page_footer($){
60 my $info = shift;
61
62 return "</ul>\n$info\n</body></html>\n";
63 }
64
65 my $sitemap = "index.html";
66 my $suffix = "new";
67
68 my $sitenameQuery = 'SELECT config_value FROM phpbb_config WHERE config_name = "sitename";';
69 my $sitename = query($sitenameQuery)->[0][0]; #phpbb_config.sitename
70 my $pagesizeQuery = 'SELECT config_value FROM phpbb_config WHERE config_name = "posts_per_page";';
71 my $posts_per_page = query($pagesizeQuery)->[0][0]; #phpbb_config.posts_per_page
72 my $dustbinQuery = 'SELECT config_value FROM phpbb_config WHERE config_name = "dustbin_forum_id";';
73 my $dustbin = query($dustbinQuery)->[0][0]; #phpbb_config.dustbin_forum_id
74 my $forumsQuery = 'SELECT forum_id,forum_name,forum_desc,cat_id,forum_order,parent_id FROM phpbb_forums WHERE auth_view=0 AND auth_read=0 AND forum_id != "'. $dustbin .'" ORDER BY cat_id, forum_order;';
75 my $forums = query($forumsQuery); # public,readable,viewable forums, aside from dustbin, it's an arayref.
76
77 open( SITEMAP, '>', "$sitemap.$suffix" );
78
79 print SITEMAP page_header( "${sitename} site map" );
80
81 foreach my $forum (@$forums){
82 my $forumId = $forum->[0];
83 my $forumName = $forum->[1];
84 my $topicsQuery = 'SELECT topic_id,topic_title,topic_replies FROM phpbb_topics WHERE forum_id = '.$forumId.' ORDER BY topic_time DESC;';
85 my $topics = query( $topicsQuery );
86
87 my $sectionIndex = $forumId.'.html';
88 my $newSectionIndex = "${sectionIndex}.$suffix";
89
90 open( SECTIONMAP, '>', "$newSectionIndex" );
91
92 print SECTIONMAP page_header( 'Index of '.$forumName );
93
94 foreach my $topic (@$topics){
95 my $topicId = $topic->[0];
96 my $topicTitle = $topic->[1];
97 my $topicReplies = $topic->[2];
98
99 my $offset = 0;
100 my $page = 1;
101
102 do{
103 print SECTIONMAP '<li><a href="../viewtopic-t-'.$topicId.'-start-'.$offset.'.html">"'.$topicTitle.'" Page:'.$page."</a></li>\n";
104
105 $offset += $posts_per_page;
106 $page++;
107 }while( $offset < $topicReplies );
108 }
109
110 print SECTIONMAP page_footer( 'Generated at'.scalar(localtime).', "'.$forumName.'" contained '.scalar(@{$topics}).' topics.' );
111
112 close(SECTIONMAP);
113
114 rename( $newSectionIndex, $sectionIndex ) or die "Could not rename $newSectionIndex to $sectionIndex\n";
115
116 print SITEMAP "<li><a href=\"./$forumId.html\">\"$forumName\"</a></li>\n";
117 }
118
119
120 print SITEMAP page_footer( 'Generated at'.scalar(localtime) );
121
122 close(SITEMAP);
123
124 $dbh->disconnect;
125
126 rename( "$sitemap.$suffix", "$sitemap" );