|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.helper.nav |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @property array $subnodes |
|
11
|
|
|
* @package midcom.helper.nav |
|
12
|
|
|
*/ |
|
13
|
|
|
class midcom_helper_nav_node extends midcom_helper_nav_item |
|
14
|
|
|
{ |
|
15
|
|
|
private ?midcom_db_topic $topic = null; |
|
16
|
|
|
|
|
17
|
|
|
private int $topic_id; |
|
18
|
|
|
|
|
19
|
142 |
|
public function __construct($topic) |
|
20
|
|
|
{ |
|
21
|
142 |
|
if ($topic instanceof midcom_db_topic) { |
|
22
|
60 |
|
$this->topic = $topic; |
|
23
|
60 |
|
$this->topic_id = $topic->id; |
|
24
|
|
|
} else { |
|
25
|
93 |
|
$this->topic_id = (int) $topic; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
130 |
|
public function is_readable_by(string $user_id) : bool |
|
30
|
|
|
{ |
|
31
|
130 |
|
return ( !$user_id |
|
32
|
130 |
|
|| !$this->guid |
|
33
|
130 |
|
|| midcom::get()->auth->acl->can_do_byguid('midgard:read', $this->guid, midcom_db_topic::class, $user_id)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
10 |
|
public function get_subnodes() : array |
|
37
|
|
|
{ |
|
38
|
10 |
|
if (!isset($this->subnodes)) { |
|
39
|
10 |
|
if ($this->topic_id == 0) { |
|
40
|
|
|
$this->subnodes = []; |
|
41
|
|
|
} else { |
|
42
|
|
|
// Use midgard_collector to get the subnodes |
|
43
|
10 |
|
$mc = midcom_db_topic::new_collector('up', $this->topic_id); |
|
44
|
10 |
|
$mc->add_constraint('name', '<>', ''); |
|
45
|
10 |
|
$mc->add_order('metadata.score', 'DESC'); |
|
46
|
10 |
|
$mc->add_order('metadata.created'); |
|
47
|
|
|
|
|
48
|
|
|
//we always write all the subnodes to cache and filter for ACLs after the fact |
|
49
|
10 |
|
midcom::get()->auth->request_sudo('midcom.helper.nav'); |
|
50
|
10 |
|
$this->subnodes = $mc->get_values('id'); |
|
51
|
10 |
|
midcom::get()->auth->drop_sudo(); |
|
52
|
|
|
} |
|
53
|
10 |
|
$this->get_cache()->put_node($this->topic_id, $this->get_data()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
10 |
|
return $this->subnodes; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return midcom_helper_nav_leaf[] |
|
61
|
|
|
*/ |
|
62
|
44 |
|
public function get_leaves() : array |
|
63
|
|
|
{ |
|
64
|
44 |
|
$leaves = $this->get_cache()->get_leaves("{$this->id}-leaves"); |
|
65
|
44 |
|
$from_cache = (null !== $leaves); |
|
66
|
44 |
|
if (!$from_cache) { |
|
67
|
44 |
|
debug_add('The leaves have not yet been loaded from the database, we do this now.'); |
|
68
|
|
|
|
|
69
|
|
|
// we always write all the leaves to cache and filter for ACLs after the fact |
|
70
|
44 |
|
midcom::get()->auth->request_sudo('midcom.helper.nav'); |
|
71
|
44 |
|
if ($nap = $this->get_component_nap($this->object)) { |
|
72
|
44 |
|
$leaves = $nap->get_leaves(); |
|
73
|
|
|
} |
|
74
|
44 |
|
midcom::get()->auth->drop_sudo(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
44 |
|
$result = []; |
|
78
|
44 |
|
foreach ($leaves as $id => $leaf) { |
|
79
|
32 |
|
$leaf = new midcom_helper_nav_leaf($this, $leaf, $id, $from_cache); |
|
80
|
32 |
|
$result[$leaf->id] = $leaf; |
|
81
|
|
|
} |
|
82
|
44 |
|
if (!$from_cache) { |
|
83
|
44 |
|
$this->write_leaves_to_cache($result); |
|
84
|
|
|
} |
|
85
|
44 |
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Writes the passed leaves to the cache, assigning them to the specified node. |
|
90
|
|
|
* |
|
91
|
|
|
* The function will bail out on any critical error. Data inconsistencies will be |
|
92
|
|
|
* logged and overwritten silently otherwise. |
|
93
|
|
|
* |
|
94
|
|
|
* @param midcom_helper_nav_leaf[] $leaves The leaves to store in the cache. |
|
95
|
|
|
*/ |
|
96
|
44 |
|
private function write_leaves_to_cache(array $leaves) |
|
97
|
|
|
{ |
|
98
|
44 |
|
if (!$this->get_cache()->get_node($this->id)) { |
|
99
|
35 |
|
debug_add("NAP Caching Engine: Tried to update the topic {$this->name} (#{$this->object->id}) " |
|
100
|
35 |
|
. 'which was supposed to be in the cache already, but failed to load the object from the database. |
|
101
|
35 |
|
Aborting write_to_cache, this is a critical cache inconsistency.', MIDCOM_LOG_WARN); |
|
102
|
35 |
|
return; |
|
103
|
|
|
} |
|
104
|
9 |
|
$cachedata = []; |
|
105
|
9 |
|
foreach ($leaves as $leaf) { |
|
106
|
2 |
|
$cachedata[$leaf->id] = $leaf->write_to_cache(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
9 |
|
debug_add('Writing ' . count($cachedata) . ' leaves to the cache.'); |
|
110
|
9 |
|
$this->get_cache()->put_leaves("{$this->id}-leaves", $cachedata); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
142 |
|
protected function prepare_data() : array |
|
114
|
|
|
{ |
|
115
|
142 |
|
$data = $this->get_cache()->get_node($this->topic_id); |
|
116
|
|
|
|
|
117
|
142 |
|
if (!$data) { |
|
118
|
131 |
|
midcom::get()->auth->request_sudo('midcom.helper.nav'); |
|
119
|
131 |
|
$data = $this->load_data(); |
|
120
|
131 |
|
midcom::get()->auth->drop_sudo(); |
|
121
|
|
|
|
|
122
|
131 |
|
if ($data === null) { |
|
123
|
16 |
|
debug_add('We got null for this node, so we do not have any NAP information, returning directly.'); |
|
124
|
16 |
|
return []; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
120 |
|
$this->get_cache()->put_node($data[MIDCOM_NAV_ID], $data); |
|
|
|
|
|
|
128
|
120 |
|
debug_add("Added the ID {$data[MIDCOM_NAV_ID]} to the cache."); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
131 |
|
return $data; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
131 |
|
private function load_data() : ?array |
|
135
|
|
|
{ |
|
136
|
131 |
|
$topic = $this->topic ?? new midcom_core_dbaproxy($this->topic_id, midcom_db_topic::class); |
|
137
|
|
|
|
|
138
|
|
|
// Retrieve a NAP instance |
|
139
|
131 |
|
$nap = $this->get_component_nap($topic); |
|
|
|
|
|
|
140
|
131 |
|
if (!$nap) { |
|
141
|
16 |
|
return null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Get the node data and verify this is a node that actually has any relevant NAP |
|
145
|
|
|
// information. Internal components which don't have |
|
146
|
|
|
// a NAP interface yet return null here, to be exempt from any NAP processing. |
|
147
|
120 |
|
$data = $nap->get_node(); |
|
148
|
120 |
|
if ($data === null) { |
|
149
|
|
|
debug_add("The component '{$topic->component}' did return null for the topic {$topic->id}, indicating no NAP information is available."); |
|
|
|
|
|
|
150
|
|
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Now complete the node data structure |
|
154
|
120 |
|
$data[MIDCOM_NAV_NAME] = trim($data[MIDCOM_NAV_NAME]) == '' ? $topic->name : $data[MIDCOM_NAV_NAME]; |
|
|
|
|
|
|
155
|
120 |
|
$data[MIDCOM_NAV_URL] = $topic->name . '/'; |
|
156
|
120 |
|
$data[MIDCOM_NAV_GUID] = $topic->guid; |
|
157
|
120 |
|
$data[MIDCOM_NAV_ID] = $topic->id; |
|
158
|
120 |
|
$data[MIDCOM_NAV_NODEID] = $topic->up; |
|
|
|
|
|
|
159
|
120 |
|
$data[MIDCOM_NAV_TYPE] = 'node'; |
|
160
|
120 |
|
$data[MIDCOM_NAV_SCORE] = $topic->metadata->score; |
|
|
|
|
|
|
161
|
120 |
|
$data[MIDCOM_NAV_COMPONENT] = $topic->component; |
|
162
|
120 |
|
$data[MIDCOM_NAV_SORTABLE] = true; |
|
163
|
120 |
|
$data[MIDCOM_NAV_OBJECT] = $topic; |
|
164
|
120 |
|
$data[MIDCOM_NAV_NOENTRY] ??= (bool) $topic->metadata->get('navnoentry'); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
120 |
|
return $data; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param midcom_db_topic $topic |
|
171
|
|
|
*/ |
|
172
|
152 |
|
private function get_component_nap($topic) : ?midcom_baseclasses_components_navigation |
|
173
|
|
|
{ |
|
174
|
152 |
|
if (!$topic->component) { |
|
175
|
16 |
|
return null; |
|
176
|
|
|
} |
|
177
|
141 |
|
$interface = midcom::get()->componentloader->get_interface_class($topic->component); |
|
178
|
141 |
|
$nap = $interface->get_nap_instance(); |
|
179
|
141 |
|
if (!$nap->set_object($topic)) { |
|
180
|
|
|
debug_add("Could not set the NAP instance of '{$topic->component}' to the topic {$topic->id}.", MIDCOM_LOG_ERROR); |
|
181
|
|
|
return null; |
|
182
|
|
|
} |
|
183
|
141 |
|
return $nap; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|