Class: SitelinkAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidata/diff/sitelink_analyzer.rb

Class Method Summary collapse

Class Method Details



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
# File 'lib/wikidata/diff/sitelink_analyzer.rb', line 2

def self.isolate_sitelinks_differences(current_content, parent_content)
    added_sitelinks = {}
    removed_sitelinks = {}
    changed_sitelinks = {}
  
    # Check if both current and parent content exist
    if current_content && parent_content
      current_sitelinks = current_content['sitelinks']
      parent_sitelinks = parent_content['sitelinks']
  
      # Check added sitelinks
      if current_sitelinks.respond_to?(:each)
        current_sitelinks.each do |site_key, current_sitelink|
          unless parent_sitelinks.respond_to?(:key?) && parent_sitelinks.key?(site_key)
            added_sitelinks[site_key] = current_sitelink
          end
        end
      end

      # Check removed sitelinks
      if parent_sitelinks.respond_to?(:each)
        parent_sitelinks.each do |site_key, parent_sitelink|
          unless current_sitelinks.respond_to?(:key?) && current_sitelinks.key?(site_key)
            removed_sitelinks[site_key] = parent_sitelink
          end
        end
      end
      # Check changed sitelinks
      if current_sitelinks && parent_sitelinks
        current_sitelinks.each do |site_key, current_sitelink|
          if parent_sitelinks.respond_to?(:key?) && parent_sitelinks.key?(site_key)
            parent_sitelink = parent_sitelinks[site_key]
            if current_sitelink != parent_sitelink
              changed_sitelinks[site_key] = {
                current: current_sitelink,
                parent: parent_sitelink
              }
            end
          end
        end
      end
    elsif current_content
      # All sitelinks are added if parent content is nil
      added_sitelinks = current_content['sitelinks']
    elsif parent_content
      # All sitelinks are removed if current content is nil
      removed_sitelinks = parent_content['sitelinks']
    end

    {
      added_sitelinks: added_sitelinks,
      removed_sitelinks: removed_sitelinks,
      changed_sitelinks: changed_sitelinks
    }
end