-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmwwikiprov.js
More file actions
205 lines (163 loc) · 5.85 KB
/
mwwikiprov.js
File metadata and controls
205 lines (163 loc) · 5.85 KB
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
/**
* Queries MediaWiki API and processes the response
* @param {string} queryParams - URL query parameters for the API
* @param {Function} callback - Function to handle the API response
* @param {string} lang - Language code (e.g., 'en', 'fr')
* @param {string} divId - ID of the target div element
* @param {string} url - Original Wikipedia URL
*/
async function queryMediaWiki(queryParams, callback, lang, divId, url) {
const div = document.getElementById(divId);
if (!div) {
console.error(`Element with ID "${divId}" not found`);
return;
}
const fetchText = document.createElement('h4');
fetchText.textContent = 'Fetching data...';
fetchText.className = 'loading-message';
div.appendChild(fetchText);
// Validate language code
const sanitizedLang = encodeURIComponent(lang);
const endpointUrl = `https://${sanitizedLang}.wikipedia.org/w/api.php`;
const fullUrl = `${endpointUrl}${queryParams}&origin=*&format=json`;
console.log('Fetching:', fullUrl);
try {
const response = await fetch(fullUrl, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json = await response.json();
// Remove loading message
if (fetchText.parentNode) {
div.removeChild(fetchText);
}
console.log('API Response:', json);
// Check for API errors
if (json.error) {
throw new Error(`MediaWiki API error: ${json.error.info}`);
}
callback(divId, json, url);
} catch (error) {
console.error('Error fetching data:', error);
if (fetchText.parentNode) {
div.removeChild(fetchText);
}
const errorMessage = document.createElement('p');
errorMessage.textContent = `Error: ${error.message}`;
errorMessage.className = 'error-message';
errorMessage.style.color = 'red';
div.appendChild(errorMessage);
}
}
/**
* Extracts and displays reference count from Wikipedia article
* @param {string} divId - ID of the target div element
* @param {Object} json - MediaWiki API response
* @param {string} url - Original Wikipedia URL
*/
function showReferences(divId, json, url) {
console.log('Processing references...');
// Validate JSON structure
if (!json.parse || !json.parse.wikitext || !json.parse.wikitext['*']) {
console.error('Invalid JSON structure:', json);
const errorDiv = document.getElementById(divId);
if (errorDiv) {
const errorMsg = document.createElement('p');
errorMsg.textContent = 'Error: Unable to parse article content';
errorMsg.style.color = 'red';
errorDiv.appendChild(errorMsg);
}
return;
}
const wikitext = json.parse.wikitext['*'];
// Improved regex for matching references
// Handles both <ref>...</ref> and <ref ... /> formats
const refRegex = /<ref(?:\s[^>]*)?>[\s\S]*?<\/ref>|<ref[^>]*\/>/gi;
// Extract all references
const references = wikitext.match(refRegex) || [];
const count = references.length;
console.log(`Found ${count} references`);
// Log first few references for debugging
references.slice(0, 3).forEach((ref, index) => {
console.log(`Reference ${index + 1}:`, ref.substring(0, 100) + '...');
});
const referenceDetails = document.getElementById(divId);
if (!referenceDetails) {
console.error(`Element with ID "${divId}" not found`);
return;
}
// Clear previous content
referenceDetails.innerHTML = '';
const referenceCount = document.createElement('p');
referenceCount.textContent = `Total ${count} reference${count !== 1 ? 's' : ''} found`;
referenceCount.className = 'reference-count';
referenceDetails.appendChild(referenceCount);
// Optionally, display individual references
if (count > 0 && count <= 10) {
const refList = document.createElement('ul');
refList.className = 'reference-list';
references.forEach((ref, index) => {
const listItem = document.createElement('li');
// Strip HTML tags for preview
const preview = ref.replace(/<[^>]*>/g, '').substring(0, 100);
listItem.textContent = `${index + 1}. ${preview}...`;
refList.appendChild(listItem);
});
referenceDetails.appendChild(refList);
}
}
/**
* Main function to analyze references from a Wikipedia URL
*/
function analyseReferences() {
let url = 'https://en.wikipedia.org/wiki/Main_Page';
// Parse URL from query string
if (window.location.search.length > 0) {
const urlParams = new URLSearchParams(window.location.search);
const urlParam = urlParams.get('url');
if (urlParam) {
url = decodeURIComponent(urlParam);
}
}
// Validate Wikipedia URL
const wikiUrlRegex = /^https:\/\/[a-z]{2,3}\.wikipedia\.org\/wiki\/.+$/;
if (!wikiUrlRegex.test(url)) {
console.error('Invalid Wikipedia URL:', url);
alert('Please provide a valid Wikipedia URL');
return;
}
// Extract article title
const titleMatch = url.match(/\/wiki\/(.+)$/);
if (!titleMatch) {
console.error('Could not extract title from URL:', url);
return;
}
const title = titleMatch[1];
const encodedTitle = encodeURIComponent(title);
console.log('Article title:', title);
// Display title
const titleDiv = document.getElementById('itemCode');
if (titleDiv) {
titleDiv.textContent = title.replace(/_/g, ' ');
}
// Extract language code
const langMatch = url.match(/^https:\/\/([a-z]{2,3})\.wikipedia\.org/);
const lang = langMatch ? langMatch[1] : 'en';
console.log('Language:', lang);
// Build query parameters
const queryParams = `?action=parse&page=${encodedTitle}&prop=wikitext`;
// Query MediaWiki API
queryMediaWiki(queryParams, showReferences, lang, 'references', url);
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', analyseReferences);
} else {
// DOMContentLoaded already fired
analyseReferences();
}