-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacklinkTrackerPlugin.inc.php
More file actions
477 lines (398 loc) · 16.1 KB
/
BacklinkTrackerPlugin.inc.php
File metadata and controls
477 lines (398 loc) · 16.1 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* @file plugins/generic/backlinkTracker/BacklinkTrackerPlugin.inc.php
*
* Copyright (c) 2024
* Distributed under the GNU GPL v3.
*
* @class BacklinkTrackerPlugin
* @ingroup plugins_generic_backlinkTracker
*
* @brief Backlink Tracker plugin - Tracks backlinks from uploaded data files
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class BacklinkTrackerPlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
// Hook into article view template
HookRegistry::register('Templates::Article::Main', array($this, 'addBacklinkDisplay'));
// Add handler for AJAX requests
HookRegistry::register('LoadHandler', array($this, 'setupBacklinkHandler'));
}
return $success;
}
/**
* Provide a name for this plugin
*/
public function getDisplayName() {
return __('plugins.generic.backlinkTracker.displayName');
}
/**
* Provide a description for this plugin
*/
public function getDescription() {
return __('plugins.generic.backlinkTracker.description');
}
/**
* Get the locale filename for this plugin
*/
public function getLocaleFilename($locale) {
// Try exact locale match first (e.g., en_US)
$localeFilename = $this->getPluginPath() . "/locale/$locale/locale.xml";
if (file_exists($localeFilename)) {
return $localeFilename;
}
// Try language-only fallback (e.g., en for en_US)
$language = substr($locale, 0, 2);
$localeFilename = $this->getPluginPath() . "/locale/$language/locale.xml";
if (file_exists($localeFilename)) {
return $localeFilename;
}
return null;
}
/**
* Get the plugin's actions
*/
public function getActions($request, $actionArgs) {
$actions = parent::getActions($request, $actionArgs);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
$linkAction = new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
);
array_unshift($actions, $linkAction);
return $actions;
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request) {
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
$this->import('BacklinkTrackerSettingsForm');
$form = new BacklinkTrackerSettingsForm($this, $context->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new \PKP\core\JSONMessage(true);
}
} else {
$form->initData();
}
return new \PKP\core\JSONMessage(true, $form->fetch($request));
case 'uploadBacklinks':
return $this->uploadBacklinks($request);
case 'clearBacklinks':
return $this->clearBacklinks($request);
case 'uploadRedirectMapping':
return $this->uploadRedirectMapping($request);
case 'clearRedirectMapping':
return $this->clearRedirectMapping($request);
}
return parent::manage($args, $request);
}
/**
* Upload backlinks file
*/
public function uploadBacklinks($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');
}
try {
$backlinks = $this->parseBacklinkFile($file['tmp_name'], $ext);
$this->saveBacklinks($context->getId(), $backlinks);
return new \PKP\core\JSONMessage(true, array('message' => 'Successfully imported ' . count($backlinks) . ' backlinks.'));
} catch (Exception $e) {
error_log('Backlink upload error: ' . $e->getMessage());
return new \PKP\core\JSONMessage(false, 'Error: ' . $e->getMessage());
}
}
/**
* Clear backlinks
*/
public function clearBacklinks($request) {
$context = $request->getContext();
$this->updateEffectiveSetting($context->getId(), 'backlinkData', null);
$this->updateEffectiveSetting($context->getId(), 'backlinkDataStatus', null);
$this->updateEffectiveSetting($context->getId(), 'backlinkDataCount', 0);
$this->updateEffectiveSetting($context->getId(), 'backlinkDataDate', null);
return new \PKP\core\JSONMessage(true, 'Cleared');
}
/**
* Upload redirect mapping file
*/
public function uploadRedirectMapping($request) {
$context = $request->getContext();
if (!isset($_FILES['redirectMappingFile'])) {
return new \PKP\core\JSONMessage(false, 'No file uploaded');
}
$file = $_FILES['redirectMappingFile'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if ($ext !== 'csv') {
return new \PKP\core\JSONMessage(false, 'Invalid file format. Please upload a CSV file.');
}
try {
$mappings = $this->parseRedirectMappingFile($file['tmp_name']);
$this->saveRedirectMappings($context->getId(), $mappings);
return new \PKP\core\JSONMessage(true, array('message' => 'Successfully imported ' . count($mappings) . ' URL redirect mappings.'));
} catch (Exception $e) {
error_log('Redirect mapping upload error: ' . $e->getMessage());
return new \PKP\core\JSONMessage(false, 'Error: ' . $e->getMessage());
}
}
/**
* Clear redirect mappings
*/
public function clearRedirectMapping($request) {
$context = $request->getContext();
$this->updateEffectiveSetting($context->getId(), 'redirectMappingData', null);
$this->updateEffectiveSetting($context->getId(), 'redirectMappingCount', 0);
$this->updateEffectiveSetting($context->getId(), 'redirectMappingDate', null);
return new \PKP\core\JSONMessage(true, 'Cleared');
}
/**
* Parse redirect mapping CSV file
*/
private function parseRedirectMappingFile($filePath) {
$mappings = array();
$handle = fopen($filePath, 'r');
if (!$handle) {
throw new Exception('Unable to open file');
}
// Skip header row if exists
$firstRow = fgetcsv($handle);
// Check if first row is header (contains "old" or "new" keywords)
if ($firstRow && (stripos($firstRow[0], 'old') !== false || stripos($firstRow[0], 'source') !== false)) {
// It's a header, continue to next row
} else {
// Not a header, process it as data
if (count($firstRow) >= 2 && !empty($firstRow[0]) && !empty($firstRow[1])) {
$oldUrl = $this->normalizeUrl(trim($firstRow[0]));
$newUrl = $this->normalizeUrl(trim($firstRow[1]));
if ($oldUrl && $newUrl) {
$mappings[$oldUrl] = $newUrl;
}
}
}
while (($row = fgetcsv($handle)) !== false) {
if (count($row) >= 2 && !empty($row[0]) && !empty($row[1])) {
$oldUrl = $this->normalizeUrl(trim($row[0]));
$newUrl = $this->normalizeUrl(trim($row[1]));
if ($oldUrl && $newUrl) {
$mappings[$oldUrl] = $newUrl;
}
}
}
fclose($handle);
if (empty($mappings)) {
throw new Exception('No valid URL mappings found in CSV. Expected format: old_url,new_url');
}
return $mappings;
}
/**
* Save redirect mappings
*/
private function saveRedirectMappings($contextId, $mappings) {
$this->updateEffectiveSetting($contextId, 'redirectMappingData', json_encode($mappings), 'string');
$this->updateEffectiveSetting($contextId, 'redirectMappingCount', count($mappings), 'int');
$this->updateEffectiveSetting($contextId, 'redirectMappingDate', date('Y-m-d H:i:s'), 'string');
}
/**
* Normalize URL for comparison
*/
private function normalizeUrl($url) {
if (empty($url)) {
return null;
}
$parsed = parse_url($url);
if (!isset($parsed['host'])) {
return null;
}
$normalized = $parsed['scheme'] . '://' . $parsed['host'] . rtrim($parsed['path'] ?? '/', '/');
return strtolower($normalized);
}
/**
* Parse backlink file
*/
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 {
// Use OJS's PhpSpreadsheet with correct path
$phpSpreadsheetPath = dirname(__FILE__) . '/../../../lib/pkp/lib/vendor/phpoffice/phpspreadsheet/src/Bootstrap.php';
if (!file_exists($phpSpreadsheetPath)) {
// Try alternate path
$phpSpreadsheetPath = dirname(__FILE__) . '/../../../lib/pkp/lib/vendor/autoload.php';
}
if (file_exists($phpSpreadsheetPath)) {
require_once($phpSpreadsheetPath);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($filePath);
$spreadsheet = $reader->load($filePath);
$sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray();
array_shift($rows); // Remove header
foreach ($rows as $row) {
if (count($row) >= 5 && !empty($row[3])) {
$backlinks[] = array(
'source_url' => $row[2],
'target_url' => $row[3],
'anchor' => $row[4],
'source_title' => $row[1],
'ascore' => $row[0]
);
}
}
} else {
throw new Exception('PhpSpreadsheet library not found. Please convert Excel to CSV and upload.');
}
}
return $backlinks;
}
/**
* Save backlinks
*/
private function saveBacklinks($contextId, $backlinks) {
$this->updateEffectiveSetting($contextId, 'backlinkData', json_encode($backlinks), 'string');
$this->updateEffectiveSetting($contextId, 'backlinkDataStatus', 'active', 'string');
$this->updateEffectiveSetting($contextId, 'backlinkDataCount', count($backlinks), 'int');
$this->updateEffectiveSetting($contextId, 'backlinkDataDate', date('Y-m-d H:i:s'), 'string');
}
/**
* Setup handler for backlink operations
*/
public function setupBacklinkHandler($hookName, $params) {
$page =& $params[0];
if ($page === 'backlink') {
$this->import('BacklinkHandler');
define('HANDLER_CLASS', 'BacklinkHandler');
return true;
}
return false;
}
/**
* Add backlink display to article page
*/
public function addBacklinkDisplay($hookName, $params) {
$templateMgr = $params[1];
$output =& $params[2];
$request = Application::get()->getRequest();
$context = $request->getContext();
if (!$this->getEnabled($context->getId())) {
return false;
}
// Get current article
$article = $templateMgr->getTemplateVars('article');
if (!$article) {
return false;
}
// Prepare template data
$templateMgr->assign(array(
'articleId' => $article->getId(),
'pluginUrl' => $request->getBaseUrl() . '/' . $this->getPluginPath(),
));
$output .= $templateMgr->fetch($this->getTemplateResource('backlinkDisplay.tpl'));
return false;
}
/**
* Get article URLs (OJS and Repository)
*/
public function getArticleUrls($article) {
$urls = array();
// Get OJS URL
$request = Application::get()->getRequest();
$dispatcher = $request->getDispatcher();
$urls['ojs'] = $dispatcher->url(
$request,
ROUTE_PAGE,
null,
'article',
'view',
$article->getBestId()
);
// Get Repository URL from Galley using OJS 3.4 method
$publication = $article->getCurrentPublication();
if ($publication) {
$galleys = $publication->getData('galleys');
if ($galleys) {
$repoLabel = $this->getSetting($request->getContext()->getId(), 'repoGalleyLabel') ?: 'REPO';
foreach ($galleys as $galley) {
$label = $galley->getLabel();
// Check if this is a repository galley
if (stripos($label, $repoLabel) !== false ||
stripos($label, 'repository') !== false ||
stripos($label, 'full text') !== false) {
$remoteUrl = $galley->getRemoteURL();
if ($remoteUrl) {
$urls['repo'] = $remoteUrl;
break;
}
}
}
}
}
return $urls;
}
/**
* Get effective setting (site-wide or per-journal)
* Returns site-wide setting if useSiteWideData is enabled, otherwise per-journal setting
*/
public function getEffectiveSetting($contextId, $settingName) {
$useSiteWide = $this->getSetting($contextId, 'useSiteWideData');
if ($useSiteWide) {
// Use site-wide setting (context ID = 0)
return $this->getSetting(0, $settingName);
} else {
// Use per-journal setting
return $this->getSetting($contextId, $settingName);
}
}
/**
* Update effective setting (site-wide or per-journal)
*/
public function updateEffectiveSetting($contextId, $settingName, $value, $type = null) {
$useSiteWide = $this->getSetting($contextId, 'useSiteWideData');
if ($useSiteWide) {
// Update site-wide setting (context ID = 0)
$this->updateSetting(0, $settingName, $value, $type);
} else {
// Update per-journal setting
$this->updateSetting($contextId, $settingName, $value, $type);
}
}
}