Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
182 lines (156 loc) · 6.09 KB
/
Copy pathPlugin.php
File metadata and controls
182 lines (156 loc) · 6.09 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
<?php
namespace Kanboard\Plugin\TaskLinker;
use InvalidArgumentException;
use Kanboard\Core\Plugin\Base;
use Kanboard\Core\Security\Role;
use Kanboard\Core\Translator;
use Kanboard\Plugin\TaskLinker\ExternalLink\NextcloudLinkProvider;
use Kanboard\Plugin\TaskLinker\ExternalLink\NextcloudUrlPolicy;
/**
* Registers TaskLinker's graph, settings and optional Nextcloud link provider.
*
* Related test files: tests/PluginIntegrationTest.php,
* tests/GraphIntegrationTest.php and tests/NextcloudProviderTest.php.
*/
class Plugin extends Base
{
public const GRAPH_MAX_NODES_KEY = 'task_linker_graph_max_nodes';
public const GRAPH_MAX_NODES_DEFAULT = 100;
public const GRAPH_MAX_NODES_MIN = 10;
public const GRAPH_MAX_NODES_MAX = 250;
public const NEXTCLOUD_ORIGINS_KEY = 'task_linker_nextcloud_origins';
public const EXTERNAL_LINK_STORAGE_KEY = 'task_linker_external_link_storage';
public const EXTERNAL_LINK_STORAGE_VERSION = 'weblink-v2';
public const EXTERNAL_LINK_CURSOR_KEY = 'task_linker_external_link_cursor';
public function initialize()
{
// Graph views inherit Kanboard's project role checks and settings remain administrator-only.
$this->projectAccessMap->add('TaskLinkerController', '*', Role::PROJECT_VIEWER);
$this->applicationAccessMap->add('TaskLinkerConfigController', '*', Role::APP_ADMIN);
$this->route->addRoute(
'/task/:task_id/relationship-graph',
'TaskLinkerController',
'show',
'TaskLinker'
);
$this->route->addRoute(
'/settings/task-linker',
'TaskLinkerConfigController',
'show',
'TaskLinker'
);
// Navigation hooks expose the graph from tasks and settings from the administrator area.
$this->template->hook->attach('template:task:sidebar:information', 'taskLinker:task/sidebar');
$this->template->hook->attach('template:config:sidebar', 'taskLinker:config/sidebar');
$this->migrateLegacyExternalLinks();
// Invalid stored values must not prevent Kanboard from loading the plugin.
try {
$origins = NextcloudUrlPolicy::parseOrigins(
(string) $this->configModel->get(self::NEXTCLOUD_ORIGINS_KEY, '')
);
} catch (InvalidArgumentException $exception) {
$origins = array();
$this->logger->warning('TaskLinker ignored invalid stored Nextcloud origins.');
}
if ($origins !== array()) {
$this->externalLinkManager->register(new NextcloudLinkProvider($this->container, $origins));
}
}
public function onStartup()
{
// English strings provide a complete fallback before an optional locale override is loaded.
Translator::load('en_US', __DIR__ . '/Locale');
$language = $this->languageModel->getCurrentLanguage();
if ($language !== 'en_US') {
Translator::load($language, __DIR__ . '/Locale');
}
}
public function getClasses()
{
return array(
'Plugin\TaskLinker\Model' => array(
'TaskGraphModel',
),
);
}
/**
* Migrate legacy provider rows without rescanning previously checked links.
*
* A high-water cursor allows re-upgrades to repair links created after a
* rollback while keeping normal plugin initialization bounded.
*/
private function migrateLegacyExternalLinks()
{
$latestLinkId = (int) $this->db->table('task_has_external_links')
->desc('id')
->findOneColumn('id');
$storageVersion = (string) $this->configModel->get(self::EXTERNAL_LINK_STORAGE_KEY, '');
$cursor = $storageVersion === self::EXTERNAL_LINK_STORAGE_VERSION
? max(0, (int) $this->configModel->get(self::EXTERNAL_LINK_CURSOR_KEY, 0))
: 0;
// Replay the migration when the database has been restored behind the saved cursor.
if ($latestLinkId < $cursor) {
$cursor = 0;
}
if ($latestLinkId > $cursor) {
$updated = $this->db->table('task_has_external_links')
->eq('link_type', NextcloudLinkProvider::LEGACY_TYPE)
->gt('id', $cursor)
->lte('id', $latestLinkId)
->update(array('link_type' => NextcloudLinkProvider::TYPE));
if (! $updated) {
$this->logger->error('TaskLinker could not migrate legacy external links.');
return;
}
}
if ($storageVersion !== self::EXTERNAL_LINK_STORAGE_VERSION || $latestLinkId !== $cursor) {
$saved = $this->configModel->save(array(
self::EXTERNAL_LINK_STORAGE_KEY => self::EXTERNAL_LINK_STORAGE_VERSION,
self::EXTERNAL_LINK_CURSOR_KEY => $latestLinkId,
));
if (! $saved) {
$this->logger->error('TaskLinker could not save its external-link migration cursor.');
}
}
}
/**
* Keep graph limits within the range supported by the interactive view.
*
* @param mixed $value
* @return integer
*/
public static function normalizeGraphMaxNodes($value)
{
$validated = filter_var($value, FILTER_VALIDATE_INT, array(
'options' => array(
'min_range' => self::GRAPH_MAX_NODES_MIN,
'max_range' => self::GRAPH_MAX_NODES_MAX,
),
));
return $validated === false ? self::GRAPH_MAX_NODES_DEFAULT : (int) $validated;
}
public function getPluginName()
{
return 'TaskLinker';
}
public function getPluginDescription()
{
return t('Visualise authorised task relationships and recognise links from configured Nextcloud sites.');
}
public function getPluginAuthor()
{
return 'aljawaid';
}
public function getPluginVersion()
{
return '1.1.3';
}
public function getCompatibleVersion()
{
return '>=1.2.54';
}
public function getPluginHomepage()
{
return 'https://github.com/aljawaid/TaskLinker';
}
}