[devel-15: various bits of refactoring ganesh**Sun Jan 05 04:01:15 GMT 2003 devel-15: various bits of refactoring fixed bugs in firstpic/lastpic/rand(om)pic code a few minor updates ] { hunk ./MERGING 28 +devel-15: various bits of refactoring + fixed bugs in firstpic/lastpic/rand(om)pic code + a few minor updates hunk ./MERGING 70 - devel-13 -> devel-14 + devel-13 -> devel-15 hunk ./PhotoPub/AlbumHelper.pm 4 -@EXPORT_OK = qw(&rangelink); +@EXPORT_OK = qw(&rangelink &dispimgwithdefaults); hunk ./PhotoPub/AlbumHelper.pm 6 +use PhotoPub::RangeHelper qw(&dispimg); +use PhotoPub::Annots qw(&getannot); + hunk ./PhotoPub/AlbumHelper.pm 18 +sub dispimgwithdefaults($,$,$,$) { + my ($other,$photo,$extra)=@_; + &dispimg($other,$photo,&getannot($photo,'options'),$extra); +} + hunk ./PhotoPub/RangeHelper.pm 52 - my $out = '

'; + my $out = '

'; hunk ./PhotoPub/Utils.pm 5 - $pathre $rangere &untaint %errorstrings &checkpath &md5_uri); + $pathre $rangere &untaint %errorstrings &checkpath &md5_uri + &handleparameters &includetag &excludetag &handlepages); hunk ./PhotoPub/Utils.pm 14 +use HTML::Entities; hunk ./PhotoPub/Utils.pm 140 +sub handleparameters($,\$) { + my ($query,$templateref)=@_; + my @params=(); + + # Handle parameters declared in template file + while($$templateref=~s/\//i) { + my $name=lc($1); + my $extra=$2; + my $val=$query->param($name); + if(defined $val) { + push @params,$name; + } else { + if($extra=~/DEFAULT=(.*)/i) { + $val=$1; + } else { + &$error(type => 'userinput', + message => "Must specify $name parameter for this template"); + } + } + $$templateref=~s/\$$name/&encode_entities($val)/ieg; + } + return @params; +} + +sub includetag($,\$,$) { + my ($tag,$textref,$count)=@_; + $count=1 if !defined $count; + $$textref=~s{<$tag>(.*?)}{$1x$count}sieg; +} + +sub excludetag($,\$) { + &includetag(@_,0); +} + +sub handlepages($,\$,\$,\$,&,&,&) { + my ($query,$templateref,$startnumref,$endnumref,$haveprevornext,$geturl)=@_; + + my ($isprev,$isnext)=(0,0); + my $page=1; + my $max; + + if($$templateref=~/\/i) { + $max=$1; + if($max!~/none/i) { + $page=defined $query->param('page') ? $query->param('page') : 1; + untaint(\$page,'\d+',"page must be a positive integer"); + my $startnum=($page-1)*$max; + my $endnum=$page*$max-1; + if($startnum<=$$startnumref) { + $isprev=0; + } else { + $isprev=1; + $$startnumref=$startnum; + } + if($endnum>=$$endnumref) { + $isnext=0; + } else { + $isnext=1; + $$endnumref=$endnum; + } + } + } + + $$templateref=~s/\//ig; + $$templateref=~s/\$page/$page/g; + + + if($isprev || $isnext) { + &$haveprevornext(); + } + + if($isnext) { + &includetag('ifnext',$templateref); + # $page was untainted to only contain numbers earlier + my $url=&$geturl($page+1); + $$templateref=~s{}{}sig; + } else { + &excludetag('ifnext',$templateref); + } + + if($isprev) { + &includetag('ifprev',$templateref); + # $page was untainted to only contain numbers earlier + my $url=&$geturl($page-1); + $$templateref=~s{}{}sig; + } else { + &excludetag('ifprev',$templateref); + } +} + + hunk ./cgi-bin/album.pl 25 -use PhotoPub::Utils qw($pathre &slurp &doifcount &untaint &uri_escape); +use PhotoPub::Utils qw($pathre &slurp &doifcount &untaint &uri_escape + &handleparameters &includetag &excludetag &handlepages); hunk ./cgi-bin/album.pl 28 -use PhotoPub::Annots qw(&getannot); hunk ./cgi-bin/album.pl 29 -use PhotoPub::AlbumHelper qw(&rangelink); -use PhotoPub::RangeHelper qw(&dispimg); +use PhotoPub::AlbumHelper qw(&rangelink &dispimgwithdefaults); hunk ./cgi-bin/album.pl 31 -use HTML::Entities; - hunk ./cgi-bin/album.pl 71 -my $template=slurp($albumtemplate) +my $template=&slurp($albumtemplate) hunk ./cgi-bin/album.pl 73 -my @params=(); hunk ./cgi-bin/album.pl 74 -# Handle parameters declared in template file -while($template=~s/\//i) { - my $name=lc($1); - my $extra=$2; - my $val=$query->param($name); - if(defined $val) { - push @params,$name; - } else { - if($extra=~/DEFAULT=(.*)/i) { - $val=$1; - } else { - &usererror("Must specify $name parameter for this template"); - } - } - $template=~s/\$$name/encode_entities($val)/ieg; -} +my @params=&handleparameters($query,\$template); hunk ./cgi-bin/album.pl 78 -my ($startnum,$endnum,$isprev,$isnext)=(0,$#$items,0,0); -my $page = 1; -my $max; +my ($startnum,$endnum)=(0,scalar(@$items)-1); hunk ./cgi-bin/album.pl 80 - hunk ./cgi-bin/album.pl 89 -if($template=~/\/i) { - $max=$1; - if($max!~/none/i) { - $page=defined $query->param('page') ? $query->param('page') : 1; - untaint(\$page,'\d+',"page must be a positive integer"); - $startnum=($page-1)*$max; - $endnum=$page*$max-1; - $isprev=1; - if($startnum<=0) { - $startnum=0; - $isprev=0; - } - $isnext=1; - if($endnum>=$#$items) { - $endnum=$#$items; - $isnext=0; - } - } -} - -$template=~s/\//ig; -$template=~s/\$page/$page/g; - -# Work out the string for links to different pages of the same album, -# if it will be needed - +# string for links to different pages of the same album, if needed hunk ./cgi-bin/album.pl 92 -if($isprev || $isnext) { - $albumquery=$linkroots{'album'}.'?album='.&uri_escape($album); - foreach my $param (defined $query->param('style') ? ('style') : (), - @params) { - $albumquery.='&'.&uri_escape($param) - .'='.&uri_escape($query->param($param)); - } -} +&handlepages($query,\$template,\$startnum,\$endnum, + sub { + $albumquery=$linkroots{'album'}.'?album='.&uri_escape($album); + foreach my $param (defined $query->param('style') + ? ('style') : (), + @params) { + $albumquery.='&'.&uri_escape($param) + .'='.&uri_escape($query->param($param)); + } + }, + sub { + my ($pagenum)=@_; + return "$albumquery&page=$pagenum"; + }); hunk ./cgi-bin/album.pl 107 -# Deal with link to the next page hunk ./cgi-bin/album.pl 108 -if($isnext) { - $template=~s/\(.*?)\<\/IFNEXT\>/$1/sig; - # $page was untainted to only contain numbers earlier - my $nextpage=$page+1; - $template=~s/\//sig; -} else { - $template=~s/\.*?\<\/IFNEXT\>//sig; -} - -# Deal with link to the previous page - -if($isprev) { - $template=~s/\(.*?)\<\/IFPREV\>/$1/sig; - # $page was untainted to only contain numbers earlier - my $prevpage=$page-1; - $template=~s/\//sig; -} else { - $template=~s/\.*?\<\/IFPREV\>//sig; -} - - hunk ./cgi-bin/album.pl 165 - die( "Unsupported item type ".$item->{'type'}." found when processing order=reversegroups" ); + &$error(type=>'internal', + message=>"Unsupported item type ".$item->{'type'} + ." found when processing order=reversegroups"); hunk ./cgi-bin/album.pl 190 - $out=~s/\(.*?)\<\/IFNEXTUPLEVEL\>/$1x$upcount/sieg; + &includetag('ifnextuplevel',\$out,$upcount); hunk ./cgi-bin/album.pl 192 - $out=~s/\(.*?)\<\/IFNEXTDOWNLEVEL\>/$1x$downcount/sieg; + &includetag('ifnextdownlevel',\$out,$downcount); hunk ./cgi-bin/album.pl 197 - $text=~s/\(.*?)\<\/IFGROUP\>/$1/sig; - $text=~s/\(.*?)\<\/IFLINK\>//sig; + &includetag('ifgroup',\$text); + &excludetag('iflink',\$text); hunk ./cgi-bin/album.pl 229 - $out =~ /\/i ) { + $out =~ /\/i || + $out =~ /(?:first|last|rand|random)pic/i ) { hunk ./cgi-bin/album.pl 233 - my $numphotos = $#photos + 1; + my $numphotos = scalar(@photos); hunk ./cgi-bin/album.pl 236 - $out=~s/\(.*?)\<\/IFPLURAL\>//sig; - $out=~s/\(.*?)\<\/IFSINGULAR\>/$1/sig; + &excludetag('ifplural',\$out); + &includetag('ifsingular',\$out); hunk ./cgi-bin/album.pl 239 - $out=~s/\(.*?)\<\/IFPLURAL\>/$1/sig; - $out=~s/\(.*?)\<\/IFSINGULAR\>//sig; + &includetag('ifplural',\$out); + &excludetag('ifsingular',\$out); hunk ./cgi-bin/album.pl 243 - $out =~ s/\(.*?)\<\/NOPHOTOS\>//sig; - if($out=~/\(.*?)\<\/IFPHOTOS\>/sig) { - my $ifph = $1; - my ($pnum,$ptag); - if($ifph =~ /(firstpic)/i) { - $pnum = 0; - $ptag = $1; - my $options=getannot($photos[$pnum],'options'); - $ifph =~ s/\/&dispimg($1,$photos[$pnum],$options,$2)/gies; - } - if($ifph =~ /(lastpic)/i) { - $pnum = scalar(@photos) - 1; - $ptag = $1; - my $options=getannot($photos[$pnum],'options'); - $ifph =~ s/\/&dispimg($1,$photos[$pnum],$options,$2)/gies; - } - if($ifph =~ /(randompic)/i || $ifph =~ /(randpic)/i) { - $pnum = int(rand(scalar(@photos))); - $ptag = $1; - my $options=getannot($photos[$pnum],'options'); - $ifph =~ s/\/&dispimg($1,$photos[$pnum],$options,$2)/gies; - } - $out=~s/\(.*?)\<\/IFPHOTOS\>/$ifph/sig; - } - } else { - $out =~ s/\(.*?)\<\/NOPHOTOS\>/$1/sig; - $out=~s/\(.*?)\<\/IFPHOTOS\>//sig; - } + &excludetag('nophotos',\$out); + &includetag('ifphotos',\$out); + + my $randnum=int(rand($numphotos)); + my $lastnum=$numphotos-1; + + $out =~ s{\} + {&dispimgwithdefaults($1,$photos[0],$2)}sieg; + + $out =~ s{\} + {&dispimgwithdefaults($1,$photos[$lastnum],$2)}sieg; + + $out =~ s{\} + {&dispimgwithdefaults + ($1,$photos[$randnum],$2)}sieg; + } else { + &includetag('nophotos',\$out); + &excludetag('ifphotos',\$out); + } hunk ./cgi-bin/album.pl 264 - $text=~s/\(.*?)\<\/IFGROUP\>//sig; - $text=~s/\(.*?)\<\/IFLINK\>/$1/sig; + &excludetag('ifgroup',\$text); + &includetag('iflink',\$text); + hunk ./cgi-bin/album.pl 281 - $out=~s/\(.*?)\<\/IFUPPREVLEVEL\>/$1x$upcount/sieg; + &includetag('ifupprevlevel',\$out,$upcount); hunk ./cgi-bin/album.pl 283 - $out=~s/\(.*?)\<\/IFDOWNPREVLEVEL\>/$1x$downcount/sieg; + &includetag('ifdownprevlevel',\$out,$downcount); hunk ./cgi-bin/album.pl 288 - $out=~s/\(.*?)\<\/IFNEXTUPLEVEL\>/$1x$oldlevel/sieg; - $out=~s/\(.*?)\<\/IFNEXTDOWNLEVEL\>//sig; + &includetag('ifnextuplevel',\$out,$oldlevel); + &excludetag('ifnextdownlevel',\$out); hunk ./cgi-bin/range.pl 24 -use PhotoPub::Utils qw(&slurp &doifcount &untaint &uri_escape); +use PhotoPub::Utils qw(&slurp &doifcount &untaint &uri_escape + &handleparameters &includetag &excludetag &handlepages); hunk ./cgi-bin/range.pl 56 -my @params=(); -while($template=~s/\//i) { - my $name=lc($1); - my $extra=$2; - my $val=$query->param($name); - if(defined $val) { - push @params,$name; - } else { - if($extra=~/DEFAULT=(.*)/i) { - $val=$1; - } else { - &usererror("Must specify $name parameter for this template"); - } - } - $template=~s/\$$name/&encode_entities($val)/ieg; -} +my @params=&handleparameters($query,\$template); hunk ./cgi-bin/range.pl 69 -my ($startnum,$isprev,$isnext)=(0,0,0); -my $endnum = @photos; -my $page; -my $max; +my $startnum=0; +my $endnum=scalar @photos - 1; hunk ./cgi-bin/range.pl 82 -if($endnum == 0 ) { +if($endnum == -1 ) { hunk ./cgi-bin/range.pl 87 -if($template=~/\/i) { - $max=$1; - if($max!~/none/i) { - $page=defined $query->param('page') ? $query->param('page') : 1; - untaint(\$page,'\d+',"page must be a positive integer"); hunk ./cgi-bin/range.pl 88 - $startnum=($page-1)*$max; - $endnum=$page*$max-1; +# string for links to different pages of the same range, if needed hunk ./cgi-bin/range.pl 90 - $isprev=1; - if($startnum<=0) { - $startnum=0; - $isprev=0; - } - $isnext=1; - if($endnum>=(@photos-1)) { - $endnum=@photos - 1; - $isnext=0; - } - # Now shorten @photos to account for the pages - my @fullphotos = @photos; - @photos = @fullphotos[$startnum..$endnum]; - - if( $debug >= 1 ) { - print "

\nstart $startnum\nend $endnum\n";
-      foreach my $p (@photos) {
-        print $p."\n";
-      }
-      print "
\n"; - } - } -} - -$template=~s/\//ig; -$template=~s/\$page/$page/g; - -# Work out the string for links to different pages of the same range, -# if it will be needed - hunk ./cgi-bin/range.pl 92 -if($isprev || $isnext) { - $rangequery=$linkroots{'range'}.'?range='.&uri_escape($range); - if(defined $query->param('titlehash')) { - $rangequery.="&titlehash=".&uri_escape($query->param('titlehash')); - } - $rangequery.="&style=".&uri_escape($style); - foreach my $param (@params) { - $rangequery.='&'.&uri_escape($param) - .'='.&uri_escape($query->param($param)); - } +&handlepages($query,\$template,\$startnum,\$endnum, + sub { + $rangequery=$linkroots{'range'}.'?range='.&uri_escape($range); + if(defined $query->param('titlehash')) { + $rangequery.="&titlehash=" + .&uri_escape($query->param('titlehash')); + } + $rangequery.="&style=".&uri_escape($style); + foreach my $param (@params) { + $rangequery.='&'.&uri_escape($param) + .'='.&uri_escape($query->param($param)); + } + }, + sub { + my ($pagenum)=@_; + return "$rangequery&page=$pagenum"; + }); hunk ./cgi-bin/range.pl 110 - # Handle has pages / no pages - $template=~s/\(.*?)\<\/IFPAGES\>/$1/sig; - $template=~s/\(.*?)\<\/NOPAGES\>//sig; -} else { - # Handle has pages / no pages - $template=~s/\(.*?)\<\/IFPAGES\>//sig; - $template=~s/\(.*?)\<\/NOPAGES\>/$1/sig; -} +# shorten @photos to account for the pages +@photos = @photos[$startnum..$endnum]; hunk ./cgi-bin/range.pl 113 -# Deal with link to the next page - -if($isnext) { - $template=~s/\(.*?)\<\/IFNEXT\>/$1/sig; - # $page was untainted to only contain numbers earlier - my $nextpage=$page+1; - $template=~s/\//sig; - $template=~s/\.*?\<\/ELSENEXT\>//sig; -} else { - $template=~s/\.*?\<\/IFNEXT\>//sig; - $template=~s/\(.*?)\<\/ELSENEXT\>/$1/sig; +if( $debug >= 1 ) { + print "
\nstart $startnum\nend $endnum\n";
+  foreach my $p (@photos) {
+    print $p."\n";
+  }
+  print "
\n"; hunk ./cgi-bin/range.pl 121 -# Deal with link to the previous page - -if($isprev) { - $template=~s/\(.*?)\<\/IFPREV\>/$1/sig; - # $page was untainted to only contain numbers earlier - my $prevpage=$page-1; - $template=~s/\
//sig; - $template=~s/\.*?\<\/ELSEPREV\>//sig; -} else { - $template=~s/\.*?\<\/IFPREV\>//sig; - $template=~s/\(.*?)\<\/ELSEPREV\>/$1/sig; -} - hunk ./cgi-bin/range.pl 128 - $template=~s/\(.*?)\<\/IFTITLE\>/$1/sig; - $template=~s/\(.*?)\<\/IFNOTITLE\>//sig; + &includetag('iftitle',\$template); + &excludetag('ifnotitle',\$template); hunk ./cgi-bin/range.pl 132 - $template=~s/\(.*?)\<\/IFTITLE\>//sig; - $template=~s/\(.*?)\<\/IFNOTITLE\>/$1/sig; + &excludetag('iftitle',\$template); + &includetag('ifnotitle',\$template); + $template=~s/\$title//ig; hunk ./cgi-bin/range.pl 152 - $section=~s/\(.*?)\<\/ifdescr\>/$1/sig; + &includetag('ifdescr',\$section); hunk ./cgi-bin/range.pl 157 - $section=~s/\.*?\<\/ifdescr\>//sig; - $section=~s/\$description//gi; + &excludetag('ifdescr',\$section); + $section=~s/\$description//ig; hunk ./dist/album-book.thtml 22 - + hunk ./dist/downwards.thtml 1 + hunk ./dist/downwards.thtml 4 - + + + + + + hunk ./dist/downwards.thtml 24 +Previous $maxitems items +Next $maxitems items hunk ./doc/thtml-guide 28 - .... - replace this with a link to the group, - with a style of foo (style="foo" is optional) - .... - replace this with a link to the album, - with a style of foo (style="foo" is optional) + + .... + .... + - replace this with a link to the group/album, with a style of "foo". + In each case style="foo" is optional if an appropriate default exists, + and "foo" can be either a relative or an absolute reference. + }