-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacklinkHandler.inc.php
More file actions
341 lines (278 loc) · 11.4 KB
/
BacklinkHandler.inc.php
File metadata and controls
341 lines (278 loc) · 11.4 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
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
<?php
/**
* @file plugins/generic/backlinkTracker/BacklinkHandler.inc.php
*
* Copyright (c) 2024
* Distributed under the GNU GPL v3.
*
* @class BacklinkHandler
* @ingroup plugins_generic_backlinkTracker
*
* @brief Handle AJAX requests for backlink operations
*/
import('classes.handler.Handler');
class BacklinkHandler extends Handler {
/** @var BacklinkTrackerPlugin */
var $_plugin;
/**
* Constructor
*/
function __construct() {
parent::__construct();
$this->_plugin = PluginRegistry::getPlugin('generic', 'backlinktrackerplugin');
}
/**
* Fetch backlinks for an article
*/
function fetch($args, $request) {
$articleId = $request->getUserVar('articleId');
$context = $request->getContext();
if (!$articleId || !$context) {
return new \PKP\core\JSONMessage(false, 'Invalid request');
}
// Get article
$article = \APP\facades\Repo::submission()->get($articleId);
if (!$article) {
return new \PKP\core\JSONMessage(false, 'Article not found');
}
// Get URLs
$urls = $this->_plugin->getArticleUrls($article);
// Get backlinks from database (with redirect mapping applied)
$backlinks = $this->getBacklinksForUrls($context->getId(), $urls);
// Add target type to each backlink
$backlinks = $this->addTargetType($backlinks, $urls);
// Apply domain filtering
$backlinks = $this->applyDomainFiltering($context->getId(), $backlinks);
// Group by domain (includes deduplication)
$grouped = $this->groupBacklinksByDomain($backlinks);
// Calculate total deduplicated backlinks
$totalDeduplicated = 0;
foreach ($grouped as $group) {
$totalDeduplicated += $group['count'];
}
// Prepare response
$data = array(
'total' => $totalDeduplicated,
'domains' => count($grouped),
'grouped_links' => $grouped,
'urls' => $urls,
'last_updated' => $this->_plugin->getEffectiveSetting($context->getId(), 'backlinkDataDate')
);
return new \PKP\core\JSONMessage(true, $data);
}
/**
* Upload backlink file
*/
function upload($args, $request) {
$context = $request->getContext();
if (!isset($_FILES['backlinkFile'])) {
return new \PKP\core\JSONMessage(false, 'No file uploaded');
}
$file = $_FILES['backlinkFile'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, array('xlsx', 'xls', 'csv'))) {
return new \PKP\core\JSONMessage(false, 'Invalid file format. Please upload Excel or CSV file.');
}
try {
$backlinks = $this->parseBacklinkFile($file['tmp_name'], $ext);
$this->saveBacklinks($context->getId(), $backlinks);
$message = 'Successfully imported ' . count($backlinks) . ' backlinks.';
return new \PKP\core\JSONMessage(true, array('message' => $message));
} catch (Exception $e) {
error_log('Backlink upload error: ' . $e->getMessage());
return new \PKP\core\JSONMessage(false, 'Error processing file: ' . $e->getMessage());
}
}
/**
* Clear all backlinks
*/
function clear($args, $request) {
$context = $request->getContext();
$this->_plugin->updateEffectiveSetting($context->getId(), 'backlinkData', null);
$this->_plugin->updateEffectiveSetting($context->getId(), 'backlinkDataStatus', null);
$this->_plugin->updateEffectiveSetting($context->getId(), 'backlinkDataCount', 0);
$this->_plugin->updateEffectiveSetting($context->getId(), 'backlinkDataDate', null);
return new \PKP\core\JSONMessage(true, 'Cleared');
}
/**
* Parse backlink file (Excel or CSV)
*/
private function parseBacklinkFile($filePath, $ext) {
$backlinks = array();
if ($ext === 'csv') {
$handle = fopen($filePath, 'r');
$headers = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== false) {
if (count($row) >= 5) {
$backlinks[] = array(
'source_url' => $row[2],
'target_url' => $row[3],
'anchor' => $row[4],
'source_title' => $row[1],
'ascore' => $row[0]
);
}
}
fclose($handle);
} else {
// Excel parsing (not implemented - ask user to convert to CSV)
throw new Exception('Please convert Excel file to CSV format before uploading.');
}
return $backlinks;
}
/**
* Save backlinks to plugin settings
*/
private function saveBacklinks($contextId, $backlinks) {
$this->_plugin->updateEffectiveSetting($contextId, 'backlinkData', json_encode($backlinks), 'string');
$this->_plugin->updateEffectiveSetting($contextId, 'backlinkDataStatus', 'active', 'string');
$this->_plugin->updateEffectiveSetting($contextId, 'backlinkDataCount', count($backlinks), 'int');
$this->_plugin->updateEffectiveSetting($contextId, 'backlinkDataDate', date('Y-m-d H:i:s'), 'string');
}
/**
* Get backlinks for specific URLs
*/
private function getBacklinksForUrls($contextId, $urls) {
$allBacklinks = json_decode($this->_plugin->getEffectiveSetting($contextId, 'backlinkData'), true);
if (!$allBacklinks) {
return array();
}
// Load redirect mappings
$redirectMappings = json_decode($this->_plugin->getEffectiveSetting($contextId, 'redirectMappingData'), true);
if (!$redirectMappings) {
$redirectMappings = array();
}
$matchedBacklinks = array();
// Normalize URLs for comparison
$normalizedUrls = array();
foreach ($urls as $key => $url) {
// Remove trailing slashes, query strings, and fragments
$parsed = parse_url($url);
$normalized = $parsed['scheme'] . '://' . $parsed['host'] . rtrim($parsed['path'], '/');
$normalizedUrls[$key] = strtolower($normalized);
}
foreach ($allBacklinks as $backlink) {
$targetUrl = $backlink['target_url'];
// Normalize target URL
$parsed = parse_url($targetUrl);
if (!isset($parsed['host'])) {
continue;
}
$normalizedTarget = $parsed['scheme'] . '://' . $parsed['host'] . rtrim($parsed['path'], '/');
$normalizedTarget = strtolower($normalizedTarget);
// Apply redirect mapping if exists
if (isset($redirectMappings[$normalizedTarget])) {
$normalizedTarget = $redirectMappings[$normalizedTarget];
$backlink['_redirected_from'] = $backlink['target_url'];
$backlink['target_url'] = $normalizedTarget;
}
// Check for exact match
foreach ($normalizedUrls as $key => $url) {
if ($normalizedTarget === $url) {
$backlink['matched_url_type'] = $key;
$matchedBacklinks[] = $backlink;
break;
}
}
}
return $matchedBacklinks;
}
/**
* Add target type (Abstract Page or Full Text) to backlinks
*/
private function addTargetType($backlinks, $urls) {
foreach ($backlinks as &$backlink) {
if (isset($backlink['matched_url_type'])) {
if ($backlink['matched_url_type'] === 'ojs') {
$backlink['target_type'] = 'Abstract Page';
} elseif ($backlink['matched_url_type'] === 'repo') {
$backlink['target_type'] = 'Full Text';
} else {
$backlink['target_type'] = 'Article';
}
}
}
return $backlinks;
}
/**
* Apply domain filtering
*/
private function applyDomainFiltering($contextId, $backlinks) {
$blockedDomainsStr = $this->_plugin->getEffectiveSetting($contextId, 'blockedDomains');
if (empty($blockedDomainsStr)) {
return $backlinks;
}
// Parse blocked domains (one per line)
$blockedDomains = array_filter(array_map('trim', explode("\n", $blockedDomainsStr)));
if (empty($blockedDomains)) {
return $backlinks;
}
// Normalize blocked domains
$normalizedBlockedDomains = array_map('strtolower', $blockedDomains);
// Filter out backlinks from blocked domains
$filteredBacklinks = array();
foreach ($backlinks as $backlink) {
$domain = parse_url($backlink['source_url'], PHP_URL_HOST);
if (!$domain) {
continue;
}
$domain = strtolower($domain);
// Check if domain is blocked (exact match or subdomain)
$isBlocked = false;
foreach ($normalizedBlockedDomains as $blockedDomain) {
// Check for exact match or subdomain match
if ($domain === $blockedDomain || str_ends_with($domain, '.' . $blockedDomain)) {
$isBlocked = true;
break;
}
}
if (!$isBlocked) {
$filteredBacklinks[] = $backlink;
}
}
return $filteredBacklinks;
}
/**
* Group backlinks by domain
*/
private function groupBacklinksByDomain($backlinks) {
$grouped = array();
foreach ($backlinks as $backlink) {
$domain = parse_url($backlink['source_url'], PHP_URL_HOST);
if (!$domain) continue;
if (!isset($grouped[$domain])) {
$grouped[$domain] = array(
'domain' => $domain,
'count' => 0,
'links' => array(),
'uniqueUrls' => array() // Track unique URLs without query strings
);
}
// Get URL without query string and normalize protocol for deduplication
$parsed = parse_url($backlink['source_url']);
// Always use https for comparison to deduplicate http/https variants
$baseUrl = 'https://' . $parsed['host'] . ($parsed['path'] ?? '/');
$baseUrl = strtolower($baseUrl);
// Only add if this base URL hasn't been added yet
if (!isset($grouped[$domain]['uniqueUrls'][$baseUrl])) {
$grouped[$domain]['count']++;
$grouped[$domain]['links'][] = array(
'url' => $backlink['source_url'],
'anchor' => $backlink['anchor'],
'title' => $backlink['source_title'],
'target_type' => $backlink['target_type'] ?? 'Article'
);
$grouped[$domain]['uniqueUrls'][$baseUrl] = true;
}
}
// Remove temporary uniqueUrls tracking
foreach ($grouped as &$group) {
unset($group['uniqueUrls']);
}
// Sort by count (descending)
uasort($grouped, function($a, $b) {
return $b['count'] - $a['count'];
});
return array_values($grouped);
}
}