#!/usr/bin/perl -W # # Copyright (C) 2008 Chris Jacobson # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # # As per the GPL, removal of this notice is prohibited. # # Automatically generates the WebNotify script for the specified BugsContrib # webs # perl -I bin tools/makebugsnotify -q # NOTICE: You must change %AGENT% to the TWiki username that will be altering # the WebNotify topic and give that user Change access to that topic, at least # temporarily # 24 May 2008 - ChrisJacobson - Initial version # 26 May 2008 - ChrisJacobson - Support for multiple comma or space separated # names in any name list # Fields to scan for are now a constant, allowing # more name fields such as a WatchedBy # 27 May 2008 - ChrisJacobson - Support for skipping notify based on CurrentState # 14 Jun 2008 - ChrisJacobson - Topics are now kept alive for notification for # a user configurable even after the state is # changed to a no-notify state. BEGIN { require 'setlib.cfg'; } use TWiki; use TWiki::Func; # -- BEGIN CONFIGURATION # Add field names that you wish to extract names from. my @METAFIELDS = ( 'ReportedBy', 'WaitingFor', 'WatchedBy' ); my $STATEFIELD = 'CurrentState'; my %NONOTIFYSTATES = ( 'Closed' => 1, 'No Action Required' => 1 ); my $NONOTIFYTIMEOUT = 60; # Set this to the user who will be making changes to the topic my $AGENT = ''%AGENT%'; # -- END CONFIGURATION my $twiki = new TWiki(); my @webs = (); my %notifications = (); my $verbose = 1; $TWiki::Plugins::SESSION = $twiki; my $session = $TWiki::Plugins::SESSION; $session->{user} = TWiki::Func::getCanonicalUserID($AGENT); foreach my $arg ( @ARGV ) { if ( $arg eq "-q" ) { $verbose = 0; } else { push( @webs, $arg ); } } sub addNotify { my ( $users, $topic ) = @_; return if ( !$users ); my @users = split(/\s*,\s*|\s+/, $users); foreach my $user (@users) { $user = TWiki::Func::getWikiName( $user ); next if (!$user); print "...Adding $topic to $user.\n" if $verbose; push( @{ $notifications{ $user } }, $topic ); } } my ( $meta, $text ); foreach my $web ( @webs ) { if ( !TWiki::Func::webExists( $web ) ) { print "No such web $web\n" if $verbose; next; } print "Entering $web\n" if $verbose; foreach my $topic ( TWiki::Func::getTopicList( $web ) ) { print "Scanning $web.$topic\n" if $verbose; ( $meta, $text) = TWiki::Func::readTopic( $web, $topic ); # Bail if the state is one that should ignore notifications my $currentState = $meta->get( 'FIELD', $STATEFIELD ); next if ( $currentState && $NONOTIFYSTATES{ $currentState->{value} } && (TWiki::Func::getRevisionAtTime( $web, $topic, time() ) == TWiki::Func::getRevisionAtTime( $web, $topic, time() - ($NONOTIFYTIMEOUT * 60) )) ); foreach my $fieldname ( @METAFIELDS ) { my $field = $meta->get( 'FIELD', $fieldname ); addNotify( $field->{value}, $topic ) if ( $field ); } } my $result = << "HEADER"; This topic is automatically generated by a script running on the server. The script analyses all the 'WaitingFor' and 'ReportedBy' fields in reports and generates this WebNotify. *Don't waste your time trying to edit this topic; you can't.* HEADER foreach $user ( sort keys %notifications ) { my $items = join(" ", sort @{ $notifications{$user} }); $items =~ s/\b(\w+)(\s+\1)+\b/$1/; $result .= " * $user: $items\n"; } print $result if $verbose; TWiki::Func::saveTopicText( $web, 'WebNotify', $result, 1, 1 ); }