Domino Code Fragment

Code Name*
Refresh Agent
Date*
04/29/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.3.145.156.46
Description*
This agent refreshes non-private http pages nightly at 3:00 AM provided that they have not been brought in already that day. This is a system agent which ships with the Web Navigator template. Please refer to the Web Navigator Administration Guide for more information.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
Sub Initialize
'*
'* Declarations
'*
Dim Session As New NotesSession 'Represents the current session
Dim WebDB As NotesDatabase 'Represents the Web database
Dim WebAllView As NotesView 'Represents the $All view
Dim WebAdminView As NotesView 'Represents the $Admin view
Dim WebColl As NotesDocumentCollection
Dim WebAdminDoc As NotesDocument 'Represents the Admin document
Dim WebDoc As NotesDocument 'Represents the current retrieved Web document
Dim CurrentDoc As NotesDocument 'Represents the current cached Web document
Dim RefreshFlag As Variant
Dim VerboseFlag As Variant
Dim Url As Variant
Dim CreationString As Variant
Dim CuttoffTime As New NotesDateTime ("1/1/94")
Dim NumDocsReloadErrors As Integer
Dim NumDocsRefreshed As Integer
Dim NumDocsRefreshedNotToday As Integer
Dim NumDocsRefreshedBadLMDate As Integer
Dim DebugFlag As String
Dim InterNotesServer As String
Dim HttpServer As String
Dim LastModifiedString As String
Dim LastModifiedLocalTime As String

'*
'* We have started! Check for debug flag in the configuration
'* file to see if we should log debug information.
'*
DebugFlag = Session.GetEnvironmentString("WebDebugRefreshAgent",True)

If ( DebugFlag = "1" ) Then
'*
'* We are going to write a whole bunch of stuff to the Notes log to
'* help "understand" which Web documents get refreshed and why.
'*
Print "=============================================================="
Print "Starting in debug mode..."
Print Session.NotesVersion
Print Session.Platform
Print "Last modified by: " + Session.EffectiveUserName
Print "Last run on: " + Cdat(Session.LastRun)
Print "Last exit status: " + Str(Session.LastExitStatus)
Print "=============================================================="
End If

'*
'* Instantiate the Web database as the DB we are currently working on.
'*
Set WebDB = Session.CurrentDatabase

If ( DebugFlag = "1" ) Then
Print "Database title: " + WebDB.Title
Print "Database created on: " + Cdat(WebDB.Created)
Print "Database template name: " + WebDB.DesignTemplateName
End If

'*
'* Find the Administration view in the Web database
'* so we can find our Admin document.
'*
Set WebAdminView = WebDB.GetView("($Admin)")

If ( DebugFlag = "1" ) Then
Print "Using Admin view: " + WebAdminView.Name
End If

'*
'* We need the InterNotes server name for this agent so we can find
'* the correct Admin document. If this agent is running on the client,
'* then the server name can be gotten from the database object. If
'* this agent is running on the server, then get the name from the
'* user name on the session.
'*
If ( Session.IsOnServer ) Then
InterNotesServer = Session.Username
Else
InterNotesServer = WebDB.Server
End If

If ( DebugFlag = "1" ) Then
Print "InterNotes server: " + InterNotesServer
End If

'*
'* Get the Admin document using the InterNotes server name as the key.
'* Get the refresh and verbose flags out of the document. Close the
'* Admin document when we are done.
'*
Set WebAdminDoc = WebAdminView.GetDocumentByKey(InterNotesServer)
RefreshFlag = WebAdminDoc.GetItemValue("Refresh")
VerboseFlag = WebAdminDoc.GetItemValue("Verbose")

If ( DebugFlag = "1" ) Then
Print "Admin refresh flag: " + RefreshFlag(0)
Print "Admin verbose flag: " + VerboseFlag(0)
End If

'*
'* Normal startup message...
'*
If ((VerboseFlag(0) = "1") And (DebugFlag <> "1") ) Then
Print "Starting..."
Print "Last run on: " + Cdat(Session.LastRun)
Print "Last exit status: " + Str(Session.LastExitStatus)
End If

If ( RefreshFlag(0) <> "1" ) Then
'*
'* The refresh option is not enabled in the Admin document for this server.
'*
If ( (VerboseFlag(0) = "1") Or (DebugFlag = "1") ) Then
Print "Refresh is not enabled in the administration document"
End If
Exit Sub
End If

'*
'* The Administrator has enabled "refresh" in the Admin document, so let's get to it!
'*
'* We now have to search the database for candidates for refreshing.
'* We only want to refresh documents that pass the following criteria:
'* 1. Only Web pages, not ratings, auto-launch documents, etc. Web
'* Web page always have form name "HTMLForm", or if they conatin
'* an HTML form, the title is "HTMLFormDoc".
'* 2. Only public pages should be refreshed, not private pages.
'* 3. Only HTTP pages should be refreshed because they are the only
'* ones we can check for "Last-Modified" date/time.
'* 4. Don't refresh pages that need authentication (Status = "-14")
'* This will be supported in a later release.
'*
If ( DebugFlag = "1" ) Then
Print "Searching the database for candidates to refresh..."
End If

NumDocsReloadErrors = 0
NumDocsRefreshed = 0
NumDocsRefreshedNotToday = 0
NumDocsRefreshedBadLMDate = 0
Set WebColl = WebDB.Search("(((Form=""HTMLForm"") | ($Title = ""HTMLFormDoc"")) & (Privacy_flags!=1) & (@contains(url;""http"")) & (status <>
""-14""))", CuttoffTime, 0)

If ( DebugFlag = "1" ) Then
Print "Found " + Str(WebColl.Count) + " documents that are candidates to refresh"
End If

If ( WebColl.Count = 0 ) Then
'*
'* There are no documents in the database to refresh.
'*
If ( (VerboseFlag(0) = "1") Or (DebugFlag = "1") ) Then
Print "There are no documents in the database to refresh"
End If
Exit Sub
End If

'*
'* Loop over all of the documents returned in the collection to check if their
'* last modification time is AFTER the creation time of the document which
'* is the same as the retrieval date/time.
'*
For i = 1 To WebColl.Count
'*
'* Get the next document in the collection
'*
Set CurrentDoc = WebColl.GetNthDocument(i)
If ( DebugFlag = "1" ) Then
Print "------------------------------------------------------------------------------------------------------------"
Print "Current document number: " + Cstr(i)
End If
'*
'* Make sure there is a URL in the document
'* that we can use for the retrieval.
'*
If ( Not CurrentDoc.HasItem("url") ) Then
'*
'* We can't do anything if we don't have a URL.
'* Go onto the next document.
'*
If ( DebugFlag = "1" ) Then
Print "Document does not contain a URL item, skipping..."
End If
Goto NextDocument
End If
'*
'* Get the URL out of the current document.
'*
Url = CurrentDoc.GetItemValue("url")
If ( DebugFlag = "1" ) Then
Print "Current URL: " + Url(0)
End If
'*
'* Get the last modification date of the URL from the HTTP
'* server using the Get Header command.
'*
LastModifiedString = WebDB.GetUrlHeaderInfo(Url(0), "Last-modified")
If ( DebugFlag = "1" ) Then
Print "Last-modified from GetUrlHeaderInfo: " + LastModifiedString
End If

'*
'* Get the creation date of the current document and
'* save it for future comparisions.
'*
CreationString = CurrentDoc.GetItemValue("time_date")
Dim CreationDate As New NotesDateTime(CreationString(0))
If ( DebugFlag = "1" ) Then
Print "Creation date (GMT): " + Cdat(CreationDate.LSGMTTime)
End If

If ( LastModifiedString <> "" ) Then
'*
'* The HTTP server returned a Last-modified time. Let's
'* check it against the creation date/time of the current
'* document to see if it needs to be refreshed.
'*
On Error Resume Next
Dim LastModifiedDate As New NotesDateTime(LastModifiedString)

'*
'* Check to make sure the date/time returned from the HTTP
'* server was in a format we could understand.
'*
If ( Isdate(Cdat(LastModifiedDate.LSGMTTime)) ) Then
'*
'* Looks like a good date/time. Compare the last modified
'* date/time against the creation date/time. If it has
'* a later modification date, then we need to reload.
'*
If ( DebugFlag = "1" ) Then
Print "Last modified date (GMT): " + Cdat(LastModifiedDate.LSGMTTime)
End If

If ( Cdat(LastModifiedDate.LSGMTTime) > Cdat(CreationDate.LSGMTTime) ) Then
'*
'* The document has been modified. We need to retrieve the new copy.
'*
If ( DebugFlag = "1" ) Then
Print "Document has been modified, refreshing..."
End If
'*
'* Force the reload the URL using the reload flag on the GetDocumentByUrl method.
'*
Set WebDoc = WebDB.GetDocumentByUrl(Url(0), 1)
WebStatusFlag = WebDoc.GetItemValue("Status")
If ( DebugFlag = "1" ) Then
Print "Retrieve status :" + WebStatusFlag(0)
End If
If ( WebStatusFlag(0) = "0" ) Then
NumDocsRefreshed = NumDocsRefreshed+1
Else
NumDocsReloadErrors = NumDocsReloadErrors + 1
End If
Else
'*
'* The document has not been modified. No need to refresh.
'*
If ( DebugFlag = "1" ) Then
Print "Document has not been modified, skipping..."
End If
End If
Else
'*
'* We could not parse the date returned by the HTTP server. It
'* is probably not in the standard format. In any case, we should
'* refresh the document just to be safe.
'*
If ( DebugFlag = "1" ) Then
HttpServer = WebDB.GetUrlHeaderInfo(Url(0), "Server")
Print "HTTP Server: " + HttpServer
Print "Modification date is not in a format we can understand, refreshing..."
End If
'*
'* Force the reload the URL using the reload flag on the GetDocumentByUrl method.
'*
Set WebDoc = WebDB.GetDocumentByUrl(Url(0), 1)
WebStatusFlag = WebDoc.GetItemValue("Status")
If ( DebugFlag = "1" ) Then
Print "Retrieve status :" + WebStatusFlag(0)
End If
If ( WebStatusFlag(0) = "0" ) Then
NumDocsRefreshedBadLMDate = NumDocsRefreshedBadLMDate+1
Else
NumDocsReloadErrors = NumDocsReloadErrors + 1
End If
End If
Else
'*
'* The HTTP server did not return a Last-Modified date.
'* So we will force a reload of the document if the creation
'* date is prior to today.
'*
If ( DebugFlag = "1" ) Then
HttpServer = WebDB.GetUrlHeaderInfo(Url(0), "Server")
Print "HTTP Server: " + HttpServer
End If

If ( (Cstr(Year(CreationDate.LSLocalTime)) <> Cstr(Year(Today()))) Or _
(Cstr(Month(CreationDate.LSLocalTime)) <> Cstr(Month(Today()))) Or _
(Cstr(Day(CreationDate.LSLocalTime)) <> Cstr(Day(Today()))) ) Then
'*
'* We didn't get a last modified date from the HTTP server and the creation date
'* of the document is not today, so reload the document.
'*
If ( DebugFlag = "1" ) Then
Print "Document not retrieved today, refreshing..."
End If
'*
'* Force the reload the URL using the reload flag on the GetDocumentByUrl method.
'*
Set WebDoc = WebDB.GetDocumentByUrl(Url(0), 1)
WebStatusFlag = WebDoc.GetItemValue("Status")
If ( DebugFlag = "1" ) Then
Print "Retrieve status :" + WebStatusFlag(0)
End If
If ( WebStatusFlag(0) = "0" ) Then
NumDocsRefreshedNotToday = NumDocsRefreshedNotToday+1
Else
NumDocsReloadErrors = NumDocsReloadErrors + 1
End If
Else
'*
'* The HTTP server did not return a Last-Modified date, but the current
'* document was already retrieved today. Don't refresh the document.
'*
If ( DebugFlag = "1" ) Then
Print "Document already retrieved today, skipping..."
End If
End If
End If

NextDocument:
'*
'* We are done with the current document,
'* continue with the next.
'*
Next
'*
'* Let's be good and refresh the view so that
'* it doesn't take so long for the first user
'* that connects when we're done.
'*
Set WebAllView = WebDB.GetView("($All)")
Call WebAllView.Refresh
'*
'* Display how many documents we refreshed out of the total number
'* we found that were candidates for refreshing.
'*
If ( DebugFlag = "1" ) Then
Print "------------------------------------------------------------------------------------------------------------"
End If
If ( DebugFlag = "1" ) Then
Print "Refreshed " + Cstr(NumDocsRefreshed) + " because they had been modified"
Print "Refreshed " + Cstr(NumDocsRefreshedNotToday) + " because their HTTP server did not return a Last-modified date and they were not retrieved today"
Print "Refreshed " + Cstr(NumDocsRefreshedBadLMDate) + " because their HTTP server returned a Last-modified date we couldn't parse"
Print "Unable to refresh " + Cstr(NumDocsReloadErrors) + " because of network or HTTP server problems (Status <> 0)"
End If
If ( (VerboseFlag(0) = "1") Or (DebugFlag = "1") ) Then
Print "Refreshed "+ Cstr(NumDocsRefreshed+NumDocsRefreshedNotToday+NumDocsRefreshedBadLMDate) +" document(s) out of " + Cstr( webcoll.count)
End If
If ( DebugFlag = "1" ) Then
Print "=============================================================="
End If

End Sub