# # RandomOTDEntry 1.1 for Movable Type # Mark Paschal # 1 February 2003 # # Shows a random entry from the current day in some other year. The same # historical entry is shown no matter how many times you rebuild your # templates. # # To use: # 1. Put this RandomOTDEntry.pl in your Movable Type plugins directory. # 2. Modify your template to use the tag. All entry # context tags (, etc) can be used inside the # MTRandomOTDEntry tag. # # If you want to show or list *all* your entries from the day on past # years, see Brad Choate's OnThisDay plugin: # http://www.bradchoate.com/past/mtonthisday.php # # # 1.1, 1 February 2003: weblogs' first posts were included, due to silly # "optimization." # # # Copyright 2003 Mark Paschal # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # use MT::Template::Context; use MT::Entry; use POSIX qw(strftime); MT::Template::Context->add_container_tag('RandomOTDEntry' => \&RandomOTDEntry); sub RandomOTDEntry { my $ctx = shift; my $builder = $ctx->stash('builder'); my $blog_id = $ctx->stash('blog_id'); # When are we? my $year, $month, $day, $thisYear; my $date = strftime "%Y%m%d", localtime; my $thisYear = substr($date, 0, 4); $date = substr($entryIn->created_on, 0, 8) if(my $entryIn = $ctx->stash('entry')); $year = substr($date, 0, 4); $month = substr($date, 4, 2); $day = substr($date, 6, 2); # What entries are those? my @entries; # Well, what year do we start with? my $firstYear = $year; my @firstEntries = MT::Entry->load({ blog_id => $blog_id }, { sort => 'created_on', limit => 1 }); $firstYear = substr($firstEntries[0]->created_on, 0, 4) if @firstEntries; # OK, so, what are the entries from which we want to select? for(my $i = $firstYear; $i <= $thisYear; $i++) { next if $i == $year; my @yearEntries = MT::Entry->load( { blog_id => $blog_id, created_on => [$i.$month.$day.'000000', $i.$month.$day.'235959'] }, { range => {created_on => 1} } ); push @entries, @yearEntries; }; return '' unless @entries; # So which entry *do* we want? # Use a lazy hash, so entry selection is entirely deterministic. my $e = $entries["$year$month$day" * 1997 % scalar(@entries)]; my $tokens = $ctx->stash ('tokens'); local $ctx->{__stash}{entry} = $e; local $ctx->{current_timestamp} = $e->created_on; # Yay! my $out = $builder->build($ctx, $tokens, { DateHeader => 1, DateFooter => 1, EntryIfExtended => $e->text_more ? 1 : 0, EntryIfAllowComments => $e->allow_comments, EntryIfAllowPings => $e->allow_pings, EntriesHeader => 1, EntriesFooter => 1, }); return $out || $ctx->error($ctx->errstr); }