Monday, November 08, 2010 at 12:05 AM.
system.verbs.builtins.radio.weblog.isDateOnHomePage
on isDateOnHomePage (d, adrblog=radio.weblog.init (), catname=nil) {
<<Changes
<<11/13/01; 11:44:10 PM by JES
<<New optional parameter: catname. If specified, then the script will return true if the date is on the index page for the given category. Rewrote the algorithm used to find posts for the given date.
<<bundle //old, wonky code
<<local (ctdays = 0, i)
<<local (day, month, year, hour, minute, second)
<<local (lastday, lastmonth, lastyear)
<<if catname == nil
<<for i = ctposts downto 1 //find the newest post that's not on the home page
<<date.get (adrposts^[i].when, @day, @month, @year, @hour, @minute, @second)
<<if day == lastday and month == lastmonth and year == lastyear
<<continue
<<ctdays++
<<if ctdays > adrprefs^.ctDaysToDisplay
<<break
<<lastday = day
<<lastmonth = month
<<lastyear = year
<<return (d >= adrposts^[i].when)
<<else
<<local (adrpost)
<<for i = ctposts downto 1 //find the newest post that's not on the home page
<<adrpost = @adrposts^[i]
<<bundle //look for reasons to skip the post
<<if not defined (adrpost^.categories) //no categories
<<continue
<<if not defined (adrpost^.categories.[catname]) //never been in this category
<<continue
<<if not adrpost^.categories.[catname] //removed from this category
<<continue
<<date.get (adrpost^.when, @day, @month, @year, @hour, @minute, @second)
<<if day == lastday and month == lastmonth and year == lastyear //same date as last checked post
<<continue
<<ctdays++
<<if ctdays > adrprefs^.ctDaysToDisplay
<<break
<<lastday = day
<<lastmonth = month
<<lastyear = year
<<if defined (adrpost^.categories)
<<if defined (adrpost^.categories.[catname])
<<if adrpost^.categories.[catname]
<<return (d >= adrpost^.when)
<<return (false)
<<9/4/01; 12:30:53 AM by JES
<<Created. Return true if a given date is on the home page, or false if not.
local (adrprefs = @adrblog^.prefs);
local (adrposts = @adrblog^.posts);
local (ctposts = sizeOf (adrposts^));
local (min = 0, max = ctposts);
if max == 0 { //no posts; return false
return (false)};
local (ixpost);
bundle { //find the index of a post on the last day which appears on the home page
local (adrpost);
local (lastday, lastmonth, lastyear);
local (ctdays = 0);
ixpost = ctposts;
loop {
adrpost = @adrposts^[ixpost];
local (flCheckPost = true);
if catname != nil {
if not radio.weblog.isPostInCategory (adrpost, catname) {
flCheckPost = false}};
if flCheckPost {
local (postday, postmonth, postyear, posthour, postminute, postsecond);
date.get (adrpost^.when, @postday, @postmonth, @postyear, @posthour, @postminute, @postsecond);
if not (postday == lastday and postmonth == lastmonth and postyear == lastyear) { //new day
ctdays++};
if ctdays >= adrprefs^.ctDaysToDisplay { //break -- ixpost is on the last day on the home page
break}};
ixpost--;
if ixpost == 0 { //no posts [for category] found -- any date will be on the home page
return (true)}}};
local (adrlastpost = @adrposts^[ixpost]);
local (theDay, theMonth, theYear, theHour, theMinute, theSecond);
date.get (adrlastpost^.when, @theDay, @theMonth, @theYear, @theHour, @theMinute, @theSecond);
local (lastDateOnHomePage = date.set (theDay, theMonth, theYear, 0, 0, 0));
return (d >= lastDateOnHomePage);
}
<<bundle //broken code
<<bundle //first, do a binary search for a post on the given date
<<loop
<<ixpost = min + ((max - min) / 2)
<<if ixpost <= 0 //no posts on, or after the given date
<<return (false)
<<adrpost = @adrposts^[ixpost]
<<local (day, month, year, hour, minute, second)
<<date.get (adrpost^.when, @day, @month, @year, @hour, @minute, @second)
<<if day == theDay and month == theMonth and year == theYear //got one
<<if catname == nil //weblog homepage -- return true
<<return (true)
<<else //have to find a post in the category -- break and let code below search for the post
<<break
<<if ixpost == min or ixpost == max //that's it for the search -- break
<<break
<<if adrpost^.when > d //split downwards
<<max = ixpost
<<if adrpost^.when < d //split upwards
<<min = ixpost
<<
<<bundle //then expand out 'till we find a post for the given day, or return false if none found
<<on searchForPostInCategory (ixsearch, direction)
<<loop //search more recent posts
<<ixsearch = ixsearch + direction
<<case direction
<<-1
<<if ixsearch == 0
<<break
<<1
<<if ixsearch > ctposts
<<break
<<local (day, month, year, hour, minute, second)
<<date.get (adrpost^.when, @day, @month, @year, @hour, @minute, @second)
<<if inCat (adrpost) //return true
<<return (true)
<<if not (day == theDay and month == theMonth and year == theYear) //off the day we're looking for -- break
<<break
<<return (false)
<<if searchForPostInCategory (ixpost, -1) //search downwards
<<return (true)
<<if searchForPostInCategory (ixpost, 1) //search upwards
<<return (true)
<<bundle //testing
<<local (d = date ("10/27/01"))
<<local (d = date ("10/28/01"))
<<local (d = date ("11/3/01"))
<<local (d = date ("11/7/01"))
<<local (d = date ("11/8/01"))
<<local (d = date ("11/11/01"))
<<local (d = date ("11/12/01"))
<<dialog.notify (isDateOnHomePage (d, catname:"foo"))
<<dialog.notify (isDateOnHomePage (d, catname:"Quotations"))
<<dialog.notify (isDateOnHomePage (d))
This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.