wikidata-diff-analyzer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a0defdbd97e1a5948fbad23957316731d09df73588f7368f634652fecb5a7d03
4
+ data.tar.gz: a4625921ba57e0f72844197976637d1b14fc3e6fdebc55ffd4ecf304d3caeff5
5
+ SHA512:
6
+ metadata.gz: fbbf223e6a2433d2d670d8e2ac9eebd83f997c1e88ecb71ef3e4ef6e597ae9c00638abba7f990453fe52391cfa3c7f17ee704f4b13e5249b23196c57e90cde79
7
+ data.tar.gz: c8f019b51965098960bc2f980739a4d58783225df433863a56a541ec4a7fbbc1a40b0bcd111100924ee65ba24e3ef721ac983a68256b7395abb3f8e713e25c04
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-22
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at saha23s2mtholyoke.edu. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in wikidata-diff-analyzer.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem 'mediawiki_api'
15
+
16
+ gem 'json'
17
+
18
+ gem 'dotenv'
19
+
data/Gemfile.lock ADDED
@@ -0,0 +1,91 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wikidata-diff-analyzer (0.1.0)
5
+ json
6
+ open-uri
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ date (3.3.3)
13
+ diff-lcs (1.5.0)
14
+ domain_name (0.5.20190701)
15
+ unf (>= 0.0.5, < 1.0.0)
16
+ dotenv (2.8.1)
17
+ faraday (0.17.6)
18
+ multipart-post (>= 1.2, < 3)
19
+ faraday-cookie_jar (0.0.7)
20
+ faraday (>= 0.8.0)
21
+ http-cookie (~> 1.0.0)
22
+ faraday_middleware (0.14.0)
23
+ faraday (>= 0.7.4, < 1.0)
24
+ http-cookie (1.0.5)
25
+ domain_name (~> 0.5)
26
+ json (2.6.3)
27
+ mediawiki_api (0.7.1)
28
+ faraday (~> 0.9, >= 0.9.0)
29
+ faraday-cookie_jar (~> 0.0, >= 0.0.6)
30
+ faraday_middleware (~> 0.10, >= 0.10.0)
31
+ multipart-post (2.3.0)
32
+ open-uri (0.3.0)
33
+ stringio
34
+ time
35
+ uri
36
+ parallel (1.23.0)
37
+ parser (3.2.2.1)
38
+ ast (~> 2.4.1)
39
+ rainbow (3.1.1)
40
+ rake (13.0.6)
41
+ regexp_parser (2.8.0)
42
+ rexml (3.2.5)
43
+ rspec (3.12.0)
44
+ rspec-core (~> 3.12.0)
45
+ rspec-expectations (~> 3.12.0)
46
+ rspec-mocks (~> 3.12.0)
47
+ rspec-core (3.12.2)
48
+ rspec-support (~> 3.12.0)
49
+ rspec-expectations (3.12.3)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.12.0)
52
+ rspec-mocks (3.12.5)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.12.0)
55
+ rspec-support (3.12.0)
56
+ rubocop (1.51.0)
57
+ json (~> 2.3)
58
+ parallel (~> 1.10)
59
+ parser (>= 3.2.0.0)
60
+ rainbow (>= 2.2.2, < 4.0)
61
+ regexp_parser (>= 1.8, < 3.0)
62
+ rexml (>= 3.2.5, < 4.0)
63
+ rubocop-ast (>= 1.28.0, < 2.0)
64
+ ruby-progressbar (~> 1.7)
65
+ unicode-display_width (>= 2.4.0, < 3.0)
66
+ rubocop-ast (1.28.1)
67
+ parser (>= 3.2.1.0)
68
+ ruby-progressbar (1.13.0)
69
+ stringio (3.0.6)
70
+ time (0.2.2)
71
+ date
72
+ unf (0.1.4)
73
+ unf_ext
74
+ unf_ext (0.0.8.2)
75
+ unicode-display_width (2.4.2)
76
+ uri (0.12.1)
77
+
78
+ PLATFORMS
79
+ x86_64-linux
80
+
81
+ DEPENDENCIES
82
+ dotenv
83
+ json
84
+ mediawiki_api
85
+ rake (~> 13.0)
86
+ rspec (~> 3.0)
87
+ rubocop (~> 1.21)
88
+ wikidata-diff-analyzer!
89
+
90
+ BUNDLED WITH
91
+ 2.4.8
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Wiki Education
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # WikidataDiffAnalyzer
2
+
3
+ Welcome to WikidataDiffAnalyzer(Alpha Version)! The WikidataDiffAnalyzer is a Ruby gem that provides functionality to parse the differences between Wikidata revisions and extract statistics about the changes. It enables accurate analysis of Wikidata edits, such as counting the number of claims, qualifiers, references, aliases, labels, descriptions and site-links added, removed, and changed. This gem has been developed to enhance Wikidata statistics on the Wiki Education Dashboard and Programs & Events Dashboard, but it can be utilized for various other purposes as well.
4
+
5
+ ## Installation
6
+
7
+ To install the WikidataDiffAnalyzer gem, add it to your Gemfile:
8
+ ```$ gem 'wikidata_diff_analyzer'```
9
+
10
+ Then, run the following command:
11
+ ```$ bundle install```
12
+
13
+ Alternatively, you can install it directly via:
14
+ ```$ gem install wikidata_diff_analyzer```
15
+
16
+ ## Usage
17
+ The main method of this gem is `WikidataDiffAnalyzer.analyze`, which receives an array of revision IDs and provides a comprehensive analysis of the differences among them.
18
+
19
+ **The input should be an array of integers**. Only the valid revision ids containing wikidata-item revisions and having a parent revision will be analyzed. Other revision ids (such as invalid revision IDs, revision IDs having no parent id/ deleted parent id, and revision ids having revisions of wikitext or lexeme) will be returned as not-analyzed in this version.
20
+
21
+ You can look at the HTML version of the difference between the edits with their parent revision below:
22
+ - 0 (does not exist)
23
+ - [123](https://www.wikidata.org/w/index.php?&diff=123) (No parent revision, is not analyzed)
24
+ - [622872009](https://www.wikidata.org/w/index.php?&diff=622872009) (added 1 claim)
25
+ - [1902995129](https://www.wikidata.org/w/index.php?&diff=1902995129) (removed 1 claim, 1 reference, and 1 qualifier)
26
+ - [1903003546](https://www.wikidata.org/w/index.php?&diff=1903003546) (changed 1 claim, added 1 qualifier)
27
+
28
+ ```
29
+ require 'wikidata_diff_analyzer'
30
+
31
+ revision_ids = [0, 123, 622872009, 1903003546, 1902995129]
32
+ result = WikidataDiffAnalyzer.analyze(revision_ids)
33
+
34
+ ```
35
+
36
+
37
+ The output is a hash including the information below:
38
+
39
+ ```
40
+ {
41
+ diffs_analyzed_count: 3,
42
+ diffs_not_analyzed: [0, 123],
43
+ diffs: {1780106722: {...}, 1903003546: {...}, 1902995129: {...}},
44
+ total: {claims_added: 0, claims_removed: 1, claims_changed: 3 ...}
45
+ }
46
+
47
+ ```
48
+ You can delve into the result as shown:
49
+
50
+ ```
51
+ puts result[:diffs_analyzed_count] # Prints the count of analyzed diffs
52
+ puts result[:diffs_not_analyzed] # Prints the list of revision IDs not analyzed
53
+ puts result[:diffs] # Prints the detailed analysis of each diff (Key is the revision ID)
54
+ puts result[:total] # Prints the total stats of all diffs
55
+
56
+ ```
57
+ Here's the full output structure:
58
+ ```
59
+ # the count of analyzed diffs
60
+ 3
61
+ # the list of revision IDs not analyzed
62
+ 0
63
+ 123
64
+ # the detailed analysis of each diff (Key is the revision ID)
65
+ {
66
+ {
67
+ 622872009=>{
68
+ :added_claims=>1,
69
+ :removed_claims=>0,
70
+ :changed_claims=>0,
71
+ :added_references=>0,
72
+ :removed_references=>0,
73
+ :changed_references=>0,
74
+ :added_qualifiers=>0,
75
+ :removed_qualifiers=>0,
76
+ :changed_qualifiers=>0,
77
+ :added_aliases=>0,
78
+ :removed_aliases=>0,
79
+ :changed_aliases=>0,
80
+ :added_labels=>0,
81
+ :removed_labels=>0,
82
+ :changed_labels=>0,
83
+ :added_descriptions=>0,
84
+ :removed_descriptions=>0,
85
+ :changed_descriptions=>0,
86
+ :added_sitelinks=>0,
87
+ :removed_sitelinks=>0,
88
+ :changed_sitelinks=>0
89
+ },
90
+ 1902995129=>{
91
+ :added_claims=>0,
92
+ :removed_claims=>1,
93
+ :changed_claims=>0,
94
+ :added_references=>0,
95
+ :removed_references=>1,
96
+ :changed_references=>0,
97
+ :added_qualifiers=>0,
98
+ :removed_qualifiers=>1,
99
+ :changed_qualifiers=>0,
100
+ :added_aliases=>0,
101
+ :removed_aliases=>0,
102
+ :changed_aliases=>0,
103
+ :added_labels=>0,
104
+ :removed_labels=>0,
105
+ :changed_labels=>0,
106
+ :added_descriptions=>0,
107
+ :removed_descriptions=>0,
108
+ :changed_descriptions=>0,
109
+ :added_sitelinks=>0,
110
+ :removed_sitelinks=>0,
111
+ :changed_sitelinks=>0
112
+ },
113
+ 1903003546=>{
114
+ :added_claims=>0,
115
+ :removed_claims=>0,
116
+ :changed_claims=>1,
117
+ :added_references=>0,
118
+ :removed_references=>0,
119
+ :changed_references=>0,
120
+ :added_qualifiers=>1,
121
+ :removed_qualifiers=>0,
122
+ :changed_qualifiers=>0,
123
+ :added_aliases=>0,
124
+ :removed_aliases=>0,
125
+ :changed_aliases=>0,
126
+ :added_labels=>0,
127
+ :removed_labels=>0,
128
+ :changed_labels=>0,
129
+ :added_descriptions=>0,
130
+ :removed_descriptions=>0,
131
+ :changed_descriptions=>0,
132
+ :added_sitelinks=>0,
133
+ :removed_sitelinks=>0,
134
+ :changed_sitelinks=>0
135
+ }
136
+ }
137
+
138
+ }
139
+ # the total stats of all diffs
140
+ {
141
+ :claims_added=>0,
142
+ :claims_removed=>1,
143
+ :claims_changed=>3,
144
+ :references_added=>1,
145
+ :references_removed=>2,
146
+ :references_changed=>0,
147
+ :qualifiers_added=>1,
148
+ :qualifiers_removed=>1,
149
+ :qualifiers_changed=>0,
150
+ :aliases_added=>0,
151
+ :aliases_removed=>0,
152
+ :aliases_changed=>0,
153
+ :labels_added=>0,
154
+ :labels_removed=>0,
155
+ :labels_changed=>0,
156
+ :descriptions_added=>0,
157
+ :descriptions_removed=>0,
158
+ :descriptions_changed=>0,
159
+ :sitelinks_added=>0,
160
+ :sitelinks_removed=>0,
161
+ :sitelinks_changed=>0
162
+ }
163
+
164
+ ```
165
+
166
+
167
+
168
+ ## Contributing
169
+
170
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/WikiEducationFoundation/wikidata-diff-analyzer](https://github.com/WikiEducationFoundation/wikidata-diff-analyzer). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/WikiEducationFoundation/wikidata-diff-analyzer/blob/master/CODE_OF_CONDUCT.md).
171
+
172
+ ## License
173
+
174
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
175
+
176
+ ## Code of Conduct
177
+
178
+ Everyone interacting in the WikidataDiffAnalyzer project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/WikiEducationFoundation/wikidata-diff-analyzer/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,71 @@
1
+ class AliasAnalyzer
2
+ def self.isolate_aliases_differences(current_content, parent_content)
3
+ return {
4
+ changed: [],
5
+ removed: [],
6
+ added: []
7
+ } if current_content.nil? && parent_content.nil?
8
+
9
+ current_aliases = current_content['aliases'] || {}
10
+ parent_aliases = parent_content['aliases'] || {}
11
+
12
+ changed_aliases = []
13
+ removed_aliases = []
14
+ added_aliases = []
15
+
16
+ if current_aliases.is_a?(Array) || parent_aliases.is_a?(Array)
17
+ return {
18
+ changed: changed_aliases,
19
+ removed: removed_aliases,
20
+ added: added_aliases
21
+ }
22
+ end
23
+
24
+ # Iterate over each language in the current aliases
25
+ (current_aliases || {}).each do |lang, current_aliases_arr|
26
+ parent_aliases_arr = parent_aliases[lang]
27
+
28
+ # Check if the language exists in the parent aliases
29
+ if parent_aliases_arr
30
+ # Ensure that current_aliases_arr is always an array
31
+ current_aliases_arr = [current_aliases_arr] unless current_aliases_arr.is_a?(Array)
32
+
33
+ current_aliases_arr.each_with_index do |current_alias, index|
34
+ parent_alias = parent_aliases_arr[index]
35
+ if parent_alias.nil?
36
+ added_aliases << { lang: lang, index: index }
37
+ elsif current_alias != parent_alias
38
+ changed_aliases << { lang: lang, index: index }
39
+ end
40
+ end
41
+ else
42
+ # Ensure that current_aliases_arr is always an array
43
+ current_aliases_arr = [current_aliases_arr] unless current_aliases_arr.is_a?(Array)
44
+
45
+ current_aliases_arr.each_with_index do |current_alias, index|
46
+ added_aliases << { lang: lang, index: index }
47
+ end
48
+ end
49
+ end
50
+
51
+ # Iterate over each language in the parent aliases to find removed aliases
52
+ (parent_aliases || {}).each do |lang, parent_aliases_arr|
53
+ # Ensure that parent_aliases_arr is always an array
54
+ parent_aliases_arr = [parent_aliases_arr] unless parent_aliases_arr.is_a?(Array)
55
+
56
+ current_aliases_arr = current_aliases[lang]
57
+
58
+ if current_aliases_arr.nil?
59
+ parent_aliases_arr.each_index do |index|
60
+ removed_aliases << { lang: lang, index: index }
61
+ end
62
+ end
63
+ end
64
+
65
+ {
66
+ changed: changed_aliases,
67
+ removed: removed_aliases,
68
+ added: added_aliases
69
+ }
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wikidata
4
+ module Diff
5
+ module Analyzer
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'large_batches_analyzer'
3
+ require_relative 'revision_analyzer'
4
+ require_relative 'total'
5
+ require_relative 'mediawiki_login'
6
+
7
+ module WikidataDiffAnalyzer
8
+ class Error < StandardError; end
9
+
10
+ # This method analyzes a set of revision ids and returns the differences between them.
11
+ def self.analyze(revision_ids)
12
+ diffs_analyzed_count = 0
13
+ diffs_analyzed = []
14
+ diffs_not_analyzed = []
15
+ diffs = {}
16
+ total = {
17
+ claims_added: 0,
18
+ claims_removed: 0,
19
+ claims_changed: 0,
20
+ references_added: 0,
21
+ references_removed: 0,
22
+ references_changed: 0,
23
+ qualifiers_added: 0,
24
+ qualifiers_removed: 0,
25
+ qualifiers_changed: 0,
26
+ aliases_added: 0,
27
+ aliases_removed: 0,
28
+ aliases_changed: 0,
29
+ labels_added: 0,
30
+ labels_removed: 0,
31
+ labels_changed: 0,
32
+ descriptions_added: 0,
33
+ descriptions_removed: 0,
34
+ descriptions_changed: 0,
35
+ sitelinks_added: 0,
36
+ sitelinks_removed: 0,
37
+ sitelinks_changed: 0
38
+ }
39
+
40
+ # if revision_ids has 0, then 0 can never be analyzed, so remove it and add in not analyzed
41
+ if revision_ids.include?(0)
42
+ revision_ids.delete(0)
43
+ diffs_not_analyzed << 0
44
+ end
45
+ # # if mediawiki can be logged in call for 500, otherwise call for 50
46
+ # if MediawikiLogin.mediawiki_login
47
+ # puts 'Logged in to mediawiki'
48
+ # result = LargeBatchesAnalyzer.handle_large_batches(revision_ids, 500)
49
+ # else
50
+ # puts 'Not logged in to mediawiki'
51
+ # result = LargeBatchesAnalyzer.handle_large_batches(revision_ids, 50)
52
+ # end
53
+
54
+ result = LargeBatchesAnalyzer.handle_large_batches(revision_ids, 50)
55
+ # result is a hash which has contents like this:
56
+ # result[revid] = { current_content: data[:content], parent_content: parent_content }
57
+
58
+ result.each do |revision_id, revision_data|
59
+ current_content = revision_data[:current_content]
60
+ parent_content = revision_data[:parent_content]
61
+ if current_content && parent_content
62
+ diff = RevisionAnalyzer.analyze_diff(current_content, parent_content)
63
+ diffs[revision_id] = diff
64
+ Total.accumulate_totals(diff, total)
65
+ diffs_analyzed << revision_id
66
+ diffs_analyzed_count += 1
67
+ end
68
+ end
69
+
70
+ # adding the bad rev_ids to the not_analyzed list
71
+ diffs_not_analyzed += revision_ids - diffs_analyzed
72
+
73
+ {
74
+ diffs_analyzed_count: diffs_analyzed_count,
75
+ diffs_not_analyzed: diffs_not_analyzed,
76
+ diffs: diffs,
77
+ total: total
78
+ }
79
+ end
80
+ end
81
+
82
+ # edge = [0, 123, 456, 1803628651]
83
+ # revision_ids = [1765004817]
84
+ # revision_idss = [1780106722, 1903003546, 1902995129, 1596238100, 1898156691]
85
+ # revisions =[0, 123, 456, 1780106722, 1596238100, 1898156691, 1895908644, 622872009, 1901195499, 1902995129, 1903003546, 1863882476, 535078533]
86
+
87
+ # Generate an array of 500 random revision IDs
88
+ random_revids = Array.new(500) { rand(1_000_000_000..2_000_000_000) }
89
+ example = [0, 123, 622872009, 1903003546, 1902995129]
90
+ # Analyze the revisions
91
+ result = WikidataDiffAnalyzer.analyze(example)
92
+ puts "final result"
93
+ puts result[:diffs_analyzed_count] # Prints the count of analyzed diffs
94
+ puts result[:diffs_not_analyzed] # Prints the list of revision IDs not analyzed
95
+ puts result[:diffs] # Prints the detailed analysis of each diff (Key is the revision ID)
96
+ puts result[:total] # Prints the total stats of all diffs
97
+
98
+
99
+
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+ require 'mediawiki_api'
3
+
4
+ class Api
5
+ def self.get_revision_contents(revision_ids)
6
+ api_url = 'https://www.wikidata.org/w/api.php'
7
+ client = MediawikiApi::Client.new(api_url)
8
+
9
+ # remove duplicates if revision_ids exists
10
+ # check if duplicate revision_ids exist and print if exists
11
+
12
+ revision_ids = revision_ids.uniq if revision_ids
13
+
14
+ begin
15
+ response = client.action(
16
+ 'query',
17
+ prop: 'revisions',
18
+ revids: revision_ids.join('|'),
19
+ rvslots: 'main',
20
+ rvprop: 'content|ids',
21
+ format: 'json'
22
+ )
23
+
24
+ if response.nil?
25
+ puts "No response received for revision IDs: #{revision_ids.join(', ')}"
26
+ return {}
27
+ end
28
+
29
+ parsed_contents = {}
30
+
31
+ # checks if it has pages
32
+ if response.data['pages'].nil?
33
+ puts "No pages found in the response for revision IDs: #{revision_ids.join(', ')}"
34
+ return nil
35
+ end
36
+
37
+ response.data['pages'].keys.each do |page|
38
+ page = response.data['pages'][page]
39
+ revisions = page['revisions']
40
+
41
+ revisions.each do |revision|
42
+ if revision['slots']['main']['contentmodel'] != 'wikibase-item'
43
+ puts "Content model is not wikibase-item"
44
+ # include the revision ids that are not wikibase-item
45
+ puts "Revision ID #{revision['revid']} is a #{revision['slots']['main']['contentmodel']}"
46
+ else
47
+ content = revision['slots']['main']['*']
48
+ revid = revision['revid']
49
+ if revid == 0
50
+ parsed_contents[revid] = {content: nil, parentid: 0}
51
+ else
52
+ parentid = revision['parentid']
53
+ parsed_contents[revid] = {content: JSON.parse(content), parentid: parentid}
54
+ end
55
+ end
56
+ end
57
+ end
58
+ return parsed_contents
59
+ rescue MediawikiApi::ApiError => e
60
+ puts "Error retrieving revision content: #{e.message}"
61
+ return {}
62
+ rescue JSON::ParserError => e
63
+ puts "Error parsing JSON content: #{e.message}"
64
+ raise e
65
+ end
66
+ end
67
+ end