Class: Api

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

Class Method Summary collapse

Class Method Details

.get_revision_contents(revision_ids) ⇒ Object



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

def self.get_revision_contents(revision_ids)
    api_url = 'https://www.wikidata.org/w/api.php'
    client = MediawikiApi::Client.new(api_url)

    # remove duplicates if revision_ids exists
    revision_ids = revision_ids.uniq if revision_ids

    begin
    response = client.action(
        'query',
        prop: 'revisions',
        revids: revision_ids.join('|'),
        rvslots: 'main',
        rvprop: 'content|ids|comment',
        format: 'json'
    )

    if response.nil?
        return {}
    end

    parsed_contents = {}

    # checks if it has pages
    if response.data['pages'].nil?
        return nil
    end

    response.data['pages'].keys.each do |page|
        page = response.data['pages'][page]
        revisions = page['revisions']
      
        revisions.each do |revision|
          content_model = revision['slots']['main']['contentmodel']
          if content_model == 'wikibase-item' || content_model == 'wikibase-property' || content_model == 'wikibase-lexeme'
            if revision.key?('texthidden')
              # "Content has been hidden or deleted"
              revid = revision['revid']
              parentid = revision['parentid']
              parsed_contents[revid] = { content: nil, comment: nil, parentid: parentid, model: content_model }
            # checking if comment has been deleted
            elsif revision.key?('commenthidden')
              # "Comment has been hidden or deleted"
              revid = revision['revid']
              content = revision['slots']['main']['*']
              parentid = revision['parentid']
              parsed_contents[revid] = { content: JSON.parse(content), comment: nil, parentid: parentid, model: content_model }
            else
              content = revision['slots']['main']['*']
              revid = revision['revid']
              comment = revision['comment']
              parentid = revision['parentid']
              if revid == 0 || revid.nil?
                parsed_contents[revid] = { content: nil, comment: nil, parentid: nil, model: 'wikibase-item' }
              else
                parsed_contents[revid] = { content: JSON.parse(content), comment: comment, parentid: parentid, model: content_model}
              end
            end
          # in the other cases, the content model is wikitext, so we won't be handling those
          end
        end
      end
    return parsed_contents
    rescue MediawikiApi::ApiError => e
    puts "Error retrieving revision content: #{e.message}"
    return {}
    rescue JSON::ParserError => e
    puts "Error parsing JSON content: #{e.message}"
    raise e
    end
end