yass_3.1.0_9bcb0cc5/src/sitemaps.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
--    Copyright 2019-2021 Bartek thindil Jasicki
--
--    This file is part of YASS.
--
--    YASS is free software: you can redistribute it and/or modify
--    it under the terms of the GNU General Public License as published by
--    the Free Software Foundation, either version 3 of the License, or
--    (at your option) any later version.
--
--    YASS is distributed in the hope that it will be useful,
--    but WITHOUT ANY WARRANTY; without even the implied warranty of
--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--    GNU General Public License for more details.
--
--    You should have received a copy of the GNU General Public License
--    along with YASS.  If not, see <http://www.gnu.org/licenses/>.

with Ada.Calendar;
with Ada.Directories; use Ada.Directories;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;
with Ada.Text_IO.Text_Streams;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with DOM.Core; use DOM.Core;
with DOM.Core.Documents; use DOM.Core.Documents;
with DOM.Core.Elements;
with DOM.Core.Nodes; use DOM.Core.Nodes;
with DOM.Readers;
with Input_Sources.File;
with AtomFeed;
with Config; use Config;

package body Sitemaps is

   -- ****iv* Sitemaps/Sitemaps.Sitemap
   -- FUNCTION
   -- The content of the sitemap of the project
   -- SOURCE
   Sitemap: Document; --## rule line off GLOBAL_REFERENCES
   -- ****

   -- ****if* Sitemaps/Sitemaps.Get_Sitemap
   -- FUNCTION
   -- Get the project sitemap content
   -- RESULT
   -- The Document with sitemap of the current project
   -- SOURCE
   function Get_Sitemap return Document is
      -- ****
   begin
      return Sitemap;
   end Get_Sitemap;

   -- ****if* Sitemaps/Sitemaps.Set_Sitemap
   -- FUNCTION
   -- Set the project sitemap content
   -- PARAMETERS
   -- New_Sitemap - The sitemap which will be used as the project sitemap
   -- SOURCE
   procedure Set_Sitemap(New_Sitemap: Document) is
      -- ****
   begin
      Sitemap := New_Sitemap;
   end Set_Sitemap;

   -- ****iv* Sitemaps/Sitemaps.Main_Node
   -- FUNCTION
   -- The main XML node of the project's sitemap
   -- SOURCE
   Main_Node: DOM.Core.Element; --## rule line off GLOBAL_REFERENCES
   -- ****

   -- ****if* Sitemaps/Set_Main_Node
   -- FUNCTION
   -- Set the new main XML node for the sitemap of the project
   -- PARAMETERS
   -- New_Main_Node - The XML node which will be set as the new main sitemap
   -- node
   -- SOURCE
   procedure Set_Main_Node(New_Main_Node: DOM.Core.Element) is
      -- ****
   begin
      Main_Node := New_Main_Node;
   end Set_Main_Node;

   -- ****iv* Sitemaps/Sitemaps.Sitemap_File_Name
   -- FUNCTION
   -- The name of the file which contains the project's sitemap
   -- SOURCE
   Sitemap_File_Name: Unbounded_String; --## rule line off GLOBAL_REFERENCES
   -- ****

   -- ****if* Sitemaps/Get_Sitemap_File_Name
   -- FUNCTION
   -- Get the name of file which contains the project's sitemap
   -- RESULT
   -- Unbounded_String with the name of sitemap file
   -- SOURCE
   function Get_Sitemap_File_Name return Unbounded_String is
      -- ****
   begin
      return Sitemap_File_Name;
   end Get_Sitemap_File_Name;

   procedure Start_Sitemap is
      use DOM.Core.Elements;
      use DOM.Readers;
      use Input_Sources.File;

      Sitemap_File: File_Input;
      --## rule off IMPROPER_INITIALIZATION
      Reader: Tree_Reader;
      New_Sitemap: DOM_Implementation;
      Nodes_List: Node_List;
      Local_Sitemap: Document;
      Local_Main_Node: DOM.Core.Element;
      --## rule on IMPROPER_INITIALIZATION
   begin
      if not Yass_Config.Sitemap_Enabled then
         return;
      end if;
      Sitemap_File_Name :=
        Yass_Config.Output_Directory &
        To_Unbounded_String(Source => Dir_Separator & "sitemap.xml");
      -- Load existing sitemap data
      if Exists(Name => To_String(Source => Get_Sitemap_File_Name)) then
         Open
           (Filename => To_String(Source => Get_Sitemap_File_Name),
            Input => Sitemap_File);
         --## rule off IMPROPER_INITIALIZATION
         Parse(Parser => Reader, Input => Sitemap_File);
         Close(Input => Sitemap_File);
         Local_Sitemap := Get_Tree(Read => Reader);
         --## rule on IMPROPER_INITIALIZATION
         Nodes_List :=
           DOM.Core.Documents.Get_Elements_By_Tag_Name
             (Doc => Local_Sitemap, Tag_Name => "urlset");
         Local_Main_Node := Item(List => Nodes_List, Index => 0);
         Set_Attribute
           (Elem => Local_Main_Node, Name => "xmlns",
            Value => "http://www.sitemaps.org/schemas/sitemap/0.9");
         -- Create new sitemap data
      else
         Local_Sitemap := Create_Document(Implementation => New_Sitemap);
         Local_Main_Node :=
           Create_Element(Doc => Local_Sitemap, Tag_Name => "urlset");
         Set_Attribute
           (Elem => Local_Main_Node, Name => "xmlns",
            Value => "http://www.sitemaps.org/schemas/sitemap/0.9");
         Local_Main_Node :=
           Append_Child(N => Local_Sitemap, New_Child => Local_Main_Node);
      end if;
      Set_Sitemap(New_Sitemap => Local_Sitemap);
      Set_Main_Node(New_Main_Node => Local_Main_Node);
   end Start_Sitemap;

   procedure Add_Page_To_Sitemap
     (File_Name, Change_Frequency, Page_Priority: String) is
      use Ada.Calendar;
      use AtomFeed;

      Url: constant String :=
        To_String(Source => Yass_Config.Base_Url) & "/" &
        Slice
          (Source => To_Unbounded_String(Source => File_Name),
           Low =>
             Length(Source => Yass_Config.Output_Directory & Dir_Separator) +
             1,
           High => File_Name'Length);
      Urls_List: Node_List;
      Children_List: Node_List; --## rule line off IMPROPER_INITIALIZATION
      Added, Frequency_Updated, Priority_Updated: Boolean := False;
      Url_Node, Url_Data, Old_Main_Node, Remove_Frequency,
      Remove_Priority: DOM.Core.Element;
      Url_Text: Text;
      Last_Modified: constant String := To_HTTP_Date(Date => Clock);
      Local_Sitemap: constant Document := Get_Sitemap;
      Local_Main_Node: DOM.Core.Element := Main_Node;
   begin
      if not Yass_Config.Sitemap_Enabled then
         return;
      end if;
      Urls_List :=
        DOM.Core.Documents.Get_Elements_By_Tag_Name
          (Doc => Local_Sitemap, Tag_Name => "loc");
      Load_Existing_Urls_Loop :
      for I in 0 .. Length(List => Urls_List) - 1 loop
         if Node_Value
             (N => First_Child(N => Item(List => Urls_List, Index => I))) /=
           Url then
            goto End_Of_Loop;
         end if;
         -- Update sitemap entry if exists
         Url_Node := Parent_Node(N => Item(List => Urls_List, Index => I));
         Children_List := Child_Nodes(N => Url_Node);
         Update_Entry_Loop :
         for J in 0 .. Length(List => Children_List) - 1 loop
            if Node_Name(N => Item(List => Children_List, Index => J)) =
              "lastmod" then
               Url_Text :=
                 First_Child(N => Item(List => Children_List, Index => J));
               Set_Node_Value(N => Url_Text, Value => Last_Modified);
            elsif Node_Name(N => Item(List => Children_List, Index => J)) =
              "changefreq" then
               if Change_Frequency'Length > 0 then
                  Url_Text :=
                    First_Child(N => Item(List => Children_List, Index => J));
                  Set_Node_Value(N => Url_Text, Value => Change_Frequency);
               else
                  Remove_Frequency := Item(List => Children_List, Index => J);
               end if;
               Frequency_Updated := True;
            elsif Node_Name(N => Item(List => Children_List, Index => J)) =
              "priority" then
               if Page_Priority'Length > 0 then
                  Url_Text :=
                    First_Child(N => Item(List => Children_List, Index => J));
                  Set_Node_Value(N => Url_Text, Value => Page_Priority);
               else
                  Remove_Priority := Item(List => Children_List, Index => J);
               end if;
               Priority_Updated := True;
            end if;
         end loop Update_Entry_Loop;
         if Change_Frequency'Length > 0 and not Frequency_Updated then
            Url_Data :=
              Append_Child
                (N => Url_Node,
                 New_Child =>
                   Create_Element
                     (Doc => Local_Sitemap, Tag_Name => "changefreq"));
            Url_Text :=
              Append_Child
                (N => Url_Data,
                 New_Child =>
                   Create_Text_Node
                     (Doc => Local_Sitemap, Data => Change_Frequency));
         end if;
         if Page_Priority'Length > 0 and not Priority_Updated then
            Url_Data :=
              Append_Child
                (N =>
                   Create_Element
                     (Doc => Local_Sitemap, Tag_Name => "priority"),
                 New_Child => Url_Data);
            Url_Text :=
              Append_Child
                (N => Url_Data,
                 New_Child =>
                   Create_Text_Node
                     (Doc => Local_Sitemap, Data => Page_Priority));
         end if;
         if Remove_Frequency /= null then
            Url_Node :=
              Remove_Child(N => Url_Node, Old_Child => Remove_Frequency);
         end if;
         if Remove_Priority /= null then
            Url_Node :=
              Remove_Child(N => Url_Node, Old_Child => Remove_Priority);
         end if;
         Added := True;
         exit Load_Existing_Urls_Loop;
         <<End_Of_Loop>>
      end loop Load_Existing_Urls_Loop;
      -- Add new sitemap entry
      if not Added then
         Url_Node := Create_Element(Doc => Local_Sitemap, Tag_Name => "url");
         Old_Main_Node := Local_Main_Node;
         Local_Main_Node :=
           Append_Child(N => Local_Main_Node, New_Child => Url_Node);
         --## rule off ASSIGNMENTS
         Local_Main_Node := Old_Main_Node;
         Url_Data :=
           Append_Child
             (N => Url_Node,
              New_Child =>
                Create_Element(Doc => Local_Sitemap, Tag_Name => "loc"));
         Url_Text :=
           Append_Child
             (N => Url_Data,
              New_Child =>
                Create_Text_Node(Doc => Local_Sitemap, Data => Url));
         Url_Data :=
           Append_Child
             (N => Url_Node,
              New_Child =>
                Create_Element(Doc => Local_Sitemap, Tag_Name => "lastmod"));
         Url_Text :=
           Append_Child
             (N => Url_Data,
              New_Child =>
                Create_Text_Node(Doc => Local_Sitemap, Data => Last_Modified));
         --## rule on ASSIGNMENTS
         if Change_Frequency /= "" then
            Url_Data :=
              Append_Child
                (N => Url_Node,
                 New_Child =>
                   Create_Element
                     (Doc => Local_Sitemap, Tag_Name => "changefreq"));
            Url_Text :=
              Append_Child
                (N => Url_Data,
                 New_Child =>
                   Create_Text_Node
                     (Doc => Local_Sitemap, Data => Change_Frequency));
         end if;
         if Page_Priority /= "" then
            Url_Data :=
              Append_Child
                (N => Url_Node,
                 New_Child =>
                   Create_Element
                     (Doc => Local_Sitemap, Tag_Name => "priority"));
            Url_Text :=
              Append_Child
                (N => Url_Data,
                 New_Child =>
                   Create_Text_Node
                     (Doc => Local_Sitemap, Data => Page_Priority));
         end if;
      end if;
      Set_Sitemap(New_Sitemap => Local_Sitemap);
      Set_Main_Node(New_Main_Node => Local_Main_Node);
   end Add_Page_To_Sitemap;

   procedure Save_Sitemap is
      use Ada.Text_IO;
      use Ada.Text_IO.Text_Streams;

      Sitemap_File: File_Type;
   begin
      if not Yass_Config.Sitemap_Enabled then
         return;
      end if;
      -- If the sitemap file not exists - create or open existing robot.txt file and append address to the sitemap
      if not Exists(Name => To_String(Source => Get_Sitemap_File_Name)) then
         if Exists
             (Name =>
                Containing_Directory
                  (Name => To_String(Source => Get_Sitemap_File_Name)) &
                Dir_Separator & "robots.txt") then
            Open
              (File => Sitemap_File, Mode => Append_File,
               Name =>
                 Containing_Directory
                   (Name => To_String(Source => Get_Sitemap_File_Name)) &
                 Dir_Separator & "robots.txt");
         else
            Create
              (File => Sitemap_File, Mode => Append_File,
               Name =>
                 Containing_Directory
                   (Name => To_String(Source => Get_Sitemap_File_Name)) &
                 Dir_Separator & "robots.txt");
         end if;
         Put_Line
           (File => Sitemap_File,
            Item =>
              "Sitemap: " & To_String(Source => Yass_Config.Base_Url) &
              "/sitemap.xml");
         Close(File => Sitemap_File);
      end if;
      -- Save the sitemap to the file
      Create
        (File => Sitemap_File, Mode => Out_File,
         Name => To_String(Source => Get_Sitemap_File_Name));
      Write
        (Stream => Stream(File => Sitemap_File), N => Get_Sitemap, Pretty_Print => True);
      Close(File => Sitemap_File);
   end Save_Sitemap;

end Sitemaps;