home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / scripts / ckermit / ksitemap.~2~ < prev    next >
Text File  |  2010-12-08  |  13KB  |  286 lines

  1. #!/net/u/1/f/fdc/kermit/wermit +
  2. #
  3. # ksitemap - sitemap.xml file constructor
  4. # Wed Dec  8 10:51:32 2010
  5. #
  6. .version = 1.00         # Version of this script
  7. .testingdest = ~/tmp/   # Result directory for testing - change if necessary
  8. #
  9. # Builds a sitemap.xml file for a website, with Google image extensions.
  10. # Requires: C-Kermit 9.0 Alpha.03 or later.
  11. #
  12. # Documentation: http://www.kermit.edu/ksitemap.html
  13. #
  14. # Optional command-line argument: path of filelist file.  If the argument is
  15. # given the web directory is assumed to be the same directory where the
  16. # filelist is.  If not given, a file named "filelist" in the current directory
  17. # is assumed if it exists.  For details see the documentation or read below.
  18. #
  19. # Data file summary... ON TOP:
  20. # enconding=value -- The character set in which the fileliest is encoded
  21. # home=value -- URL of website home directory (required)
  22. # geo=value  -- String: Default location for images (optional)
  23. # lic=value  -- Filename of page containing copyright/license info (optional)
  24. #
  25. # REST OF FILE: Information for each page to be included in sitemap:
  26. # url=value  -- Filename of web page (required)
  27. # pri=value  -- Priority for indexing (0.0 to 1.0) (optional)
  28. # img=value  -- Filename of an image used in this web page (optional)
  29. # cap=value  -- Caption for this image (optional)
  30. # title=value - Title for thie image (optional)
  31. #
  32. # Author: Frank da Cruz, December 2010.
  33. #
  34. if llt \v(version) 900299 exit 1 "C-Kermit 9.0 or later required"
  35.  
  36. if def \$(DEBUG) set debug message on   # DEBUG env variable requests debugging
  37. .unix = 0                               # For "if unix ..."
  38. if equ "\v(system)" "UNIX" .unix = 1
  39. .usedenv = 0
  40.  
  41. def errexit {                           # Fatal error macro
  42.     echo \v(timestamp) \v(dir) sitemap.ksc:
  43.     echo Error: \%*
  44.     exit 1
  45. }
  46. if def \%1 {                            # Command-line argument if any
  47.     .webdirectory := \fdirname(\%1)     # is pathname of file list file.
  48.     if def webdirectory {               # If it includes a directory part
  49.         if not directory \m(webdirectory) { # Check it
  50.             errexit NOT A DIRECTORY: \m(webdirectory)
  51.         }
  52.         cd \m(webdirectory)             # and CD to it
  53.         if fail errexit CD FAILED: \m(webdirectory)
  54.     }
  55.     .filelist := \fbasename(\%1)        # And this is the name of the file
  56.     if not def filelist .filelist = filelist
  57. } else if def \$(KSITEMAPDIR) {         # Env variable KSITEMAPDIR exists
  58.     .webdirectory := \$(KSITEMAPDIR) 
  59.     if not directory \m(webdirectory) { # Check it
  60.         errexit "NOT A DIRECTORY: \m(webdirectory) [From $KSITEMAPDIR]"
  61.     }
  62.     cd \m(webdirectory)                 # and CD to it
  63.     if fail errexit "CD \m(webdirectory) [From $KSITEMAPDIR]"
  64.     .filelist = filelist                # And the file-list file is filelist
  65.     .usedenv = 1
  66. } else {                                # Otherwise
  67.     .webdirectory := \v(dir)            # assume the current directory
  68.     .filelist = filelist                # And default the filename to filelist
  69. }
  70. .resultdirectory := \m(webdirectory)    # Where to put sitemap.xml
  71.  
  72. if debug {                              # Debugging
  73.     .resultdirectory := \m(testingdest)
  74.     echo DEBUGGING...
  75.     echo \fbasename(\%0) V\m(version)
  76.     if usedenv echo Parameters obtained from $KSITEMAPDIR environment variable:
  77.     show mac webdirectory filelist
  78.     echo Writing result to \m(testingdest)\m(filelist)
  79.     echo current directory is \v(dir)
  80. }
  81. if not exist \m(filelist) {             # Check that the file list file exists
  82.     errexit FILE LIST NOT FOUND: \m(webdirectory)\m(filelist)
  83. }
  84. # Define some macros...
  85.  
  86. define FERREXIT {                       # Fatal error reading file list file
  87.     exit 1 [\flpad(\m(lineno),3,0)] \%1 [\m(line)]
  88. define FERRWARN {                       # Warning about a file list line
  89.     echo [\flpad(\m(lineno),3,0)] \%1 [\m(line)]
  90. }
  91.  
  92. define FINISHIMAGE {                    # Macro to write Image epilog
  93.     if inimg {                          # If we were doing an image...
  94.         if def geo {                    # if location defined
  95.             .\%9 := <image:geo_location>\m(geo)</image:geo_location>
  96.             fwrite /line \%o "    \%9"  # add it.
  97.         }
  98.         if def lic {                    # If license URL defined
  99.             .\%9 := <image:license>\m(home)\m(lic)</image:license>
  100.             fwrite /line \%o "    \%9"  # Add it
  101.         }
  102.         fwrite /line \%o "  </image:image>" # Close image clause
  103.         .inimg = 0                      # No longer doing an image
  104.     }
  105. }
  106. def FINISHFILE {                        # Macro to write URL epilog
  107.     if not inurl end 0
  108.     finishimage                         # Finish current image if any
  109.     fwrite /line \%o </url>             # End of this URL
  110.     .inurl = 0                          # No longer doing a URL
  111. }
  112. # Begin execution...
  113.  
  114. fopen /read \%c \m(filelist)            # Open the file-list file
  115. if fail errexit "\v(lastcommand)"       # Make sure it is open
  116.  
  117. fopen /write \%o sitemap.tmp            # Open the temporary sitemap file.
  118. if fail errexit "OPEN /WRITE FAILED"    # Check
  119.  
  120. # Write XML prolog to sitemap file...
  121. fwrite /line \%o <?xml version="1.0" encoding="UTF-8"?> # First line
  122. if fail errexit "WRITE FAILED: sitemap.tmp" # Check that FWRITE succeeded
  123.  
  124. # If we get here all writes should succeed - continue the XML prolog...
  125. fwrite /line \%o <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  126. fwrite /line \%o -
  127. { xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">}
  128.  
  129. .tags = |url|pri|img|cap|title|home|geo|lic|encoding|  # Valid tags
  130. .urls = 0                               # Initialize URL counter
  131. .imgs = 0                               # Image counter
  132. .imginurl = 0                           # Image within URL counter
  133. .inurl = 0                              # State flag: doing a URL
  134. .inimg = 0                              # State flag: doing an image (in a URL)
  135. .lineno = 0                             # File list file line number
  136. .doutf8 = 0                             # Convert encoding to UTF-8
  137. .mjd := \fmjd(today)                    # Today's date MJD format
  138. .encoding = UTF-8                       # Default encoding for file-list file
  139. .home =                                 # Web home directory (none yet)
  140. .globalgeo =                            # Global geographic location (ditto)
  141. .globallic =                            # Global license page (ditto)
  142.  
  143. while true {                            # Loop to read and process file list
  144.     fread /line /trim \%c line          # Read a line and trim trailing blanks
  145.     if fail break                       # Fail = end of file = all done
  146.     increment lineno                    # Count this line
  147.     if not defined line continue        # If empty line read the next one
  148.     .line := \fltrim(\m(line))          # Trim leading blanks
  149.     if eq "\s(line[1:1])" "#" continue  # If it's a comment line keep reading
  150.     .\%9 := \fsplit(\m(line),&x,=,CSV) # Split line on '='
  151.     if < \%9 2 { ferrwarn "TAG WITH NO VALUE", continue }
  152.     .s1 := \&x[1]                       # Tag
  153.     .s2 := \&x[2]                       # Value
  154.     if doutf8 {
  155.         if not equ "7BIT" "\fstringtype(\m(s2))" {
  156.             .s2 := \fcvtcset(\m(s2),\m(encoding),utf-8)
  157.         }
  158.     }
  159.     if not \findex(|\m(s1)|,\m(tags)) { # Preverify tag
  160.         ferrwarn "UNKNOWN TAG '\m(s1)'- SKIPPING"
  161.         continue
  162.     }
  163.     if equ "\m(s1)" "encoding" {        # Encoding of filelist file
  164.         .encoding := \m(s2)             # Save it here
  165.         message Encoding=\m(encoding)
  166.         if not equ "\m(s2)" "UTF-8" .doutf8 = 1 # Most convert to UTF-8
  167.         continue
  168.     }
  169.     if equ "\m(s1)" "home" {            # Website home directory
  170.         # In Unix supply trailing slash if necessary
  171.         if unix if neq "\fright(\m(s2),1)" "/" .s2 := \m(s2)/
  172.         .home := \m(s2)                 # to be used in building URLs
  173.         continue
  174.     }
  175.     if equ "\m(s1)" "geo" {             # Image geographic location
  176.         if == 0 urls {                  # If geo given at the head of filelist
  177.             .globalgeo := \m(s2)        # make it the global default value
  178.         } else {
  179.             .geo := \m(s2)              # set the local value in any case
  180.         }
  181.         continue
  182.     }
  183.     if equ "\m(s1)" "lic" {             # Website license page
  184.         if == 0 urls {                  # Same as for geo
  185.             .globallic := \m(s2)
  186.         } else {
  187.             .lic := \m(s2)
  188.         }
  189.         continue
  190.     }
  191.     if equ "\m(s1)" "url" {             # Wep age URL
  192.         if not def home errexit "URL BEFORE HOME DEFINED"
  193.         if inurl do finishfile          # Finish previous URL if any
  194.         .imginurl = 0                   # How many images in this URL
  195.         .priority = 0.5                 # Default page priority (0.0-1.0)
  196.         .name := \m(s2)                 # Filename of this web page
  197.         if not exist \m(name) { ferrwarn "NOT EXIST \m(name)", continue }
  198.         if not readable \m(name) { ferrwarn "NOT READABLE \m(name)" }
  199.         .inurl = 1                      # We are doing a URL now
  200.  
  201.         message \m(name)...             # List the name if debugging
  202.         incr urls                       # Count the URL
  203.         fwrite /line \%o <url>          # Start the XML URL section
  204.         # Add URL of this file to sitemap...
  205.         if eq "\m(name)" "index-en.html" { # (Special for multilingual sites)
  206.             fwrite /line \%o "  <loc>\m(home)</loc>"
  207.         } else if eq "\m(name)" "index.html" {
  208.             fwrite /line \%o "  <loc>\m(home)</loc>"
  209.         } else {
  210.             fwrite /line \%o "  <loc>\m(home)\m(name)</loc>"
  211.         }
  212.         .s := \fcvtd(\fdate(\m(name)),3) # Modification date of file
  213.         .s := \s(s[1:4])-\s(s[5:2])-\s(s[7:2]) # Just the date is enough
  214.         fwrite /line \%o "  <lastmod>\m(s)</lastmod>" # Add to sitemap
  215.         .\%x := \fmjd(\fdate(\m(name))) # Modification date as MJD
  216.         .\%y ::= \m(today) - \%x        # How many days ago
  217.         .c = yearly                     # Default change frequency is yearly
  218.         if < \%y 8 .c =  daily          # If modified in last 7 days daily
  219.         else if < \%y 30 .c =  weekly   # or in last 30 days say weekly
  220.         else if < \%y 100 .c =  monthly # or in last 100 days say monthly
  221.         fwrite /line \%o "  <changefreq>\m(c)</changefreq>" # Add to sitemap
  222.         continue
  223.     }
  224.     if equ "\m(s1)" "pri" {             # Page priority
  225.         if not inurl ferrexit "PRIORITY NOT IN URL"
  226.         if not float \m(s2) ferrexit "PRIORITY NOT NUMERIC"
  227.         if ( > \m(s2) 1.0 || < \m(s2) 0.0 ) ferrexit "PRIORITY OUT OF RANGE"
  228.         .priority := \m(s2)             # Save it for epilog (see finishfile)
  229.         continue
  230.     }
  231.     if equ "\m(s1)" "img" {             # Image
  232.         finishimage                     # Finish previous image if any
  233.         if == 0 imginurl {              # First image for this URL?
  234.             # Add page priority before listing any images
  235.             fwrite /line \%o "  <priority>\m(priority)</priority>"
  236.         }
  237.         .geo := \m(globalgeo)           # If a global one defined use it
  238.         .lic := \m(globallic)           #  wherever a local one is not given.
  239.         if not inurl ferrexit "img not in url"
  240.         if not exist \m(s2) { ferrwarn "IMG NOT EXIST: \m(s2)", continue }
  241.         if not readabl \m(s2) { ferrwarn "IMG NOT READABLE: \m(s2)", continue }
  242.         increment imgs                  # Count this image
  243.         incr imginurl                   # Count image for this URL
  244.         fwrite /line \%o "  <image:image>" # Start image clause
  245.         fwrite /line \%o "    <image:loc>\m(home)\m(s2)</image:loc>" # Put URL
  246.         .inimg = 1                      # We are doing an image now
  247.         continue
  248.     }
  249.     if equ "\m(s1)" "cap" {             # Image caption
  250.         if not inimg ferrexit "CAP WITH NO IMG"
  251.         if def s2 {                     # If the caption is not empty add it
  252.             fwrite /line \%o "    <image:caption>\m(s2)</image:caption>"
  253.         }
  254.         continue
  255.     }
  256.     if equ "\m(s1)" "title" {           # Image title
  257.         if not inimg ferrexit "TITLE WITH NO IMG"
  258.         if def s2 {                     # If the title is not empty add it
  259.             fwrite /line \%o "    <image:title>\m(s2)</image:title>"
  260.         }
  261.     }
  262. }
  263. do finishfile                           # End of file list - finish last URL
  264. fwrite /line \%o </urlset>              # Finish the sitemap
  265. fclose \%o                              # Close the temporary sitemap file
  266. if exist sitemap.xml {                  # Rotate previous ones
  267.     if exist sitemap.ayer copy /preserve sitemap.ayer sitemap.ante
  268.     if fail message "FAILURE TO ROTATE OLD SITEMAP[1]"
  269.     copy /preserve sitemap.xml sitemap.ayer
  270.     if fail message "FAILURE TO ROTATE OLD SITEMAP[2]"
  271. }
  272. rename sitemap.tmp \m(resultdirectory)sitemap.xml # Install the new sitemap
  273. if fail errexit "FAILURE TO INSTALL NEW SITEMAP"
  274. if unix {                               # Unix...
  275.     chmod 644 sitemap.xml               # Make it world readable
  276.     if fail errexit "CHMOD FAILURE"
  277. }
  278. # When run in a cron job this message arrives in email
  279. exit 0 "[\v(timestamp)] sitemap.ksc: OK - URLs: \m(urls); IMGs: \m(imgs)"
  280.  
  281. ; Local Variables:
  282. ; comment-column:40
  283. ; comment-start:"# "
  284. ; End:
  285.