/
node.php
376 lines (337 loc) · 11.4 KB
/
node.php
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
<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*========================================================================*\
|| ###################################################################### ||
|| # vBulletin 5.1.9 - Licence Number LD18132D6F
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2015 vBulletin Solutions Inc. All Rights Reserved. # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| ###################################################################### ||
\*========================================================================*/
/**
* @package vBulletin
*/
class vB_Node
{
/**
* Calculates textcount, textunpubcount, totalcount, totalunpubcount for a nodeid.
* Used for fixing node counts and for verifying integrity
* @param int $nodeId
* @return array
*/
public static function getCounts($nodeId)
{
$excluded = self::getExcludedTypes();
$counts = vB::getDbAssertor()->getRow('vBForum:getContentCounts', array(
vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_STORED,
'excludeTypes' => $excluded, 'parentid' => $nodeId
));
return $counts;
}
/**
* Returns the content types that don't affect node counts and last data.
*
*/
public static function getExcludedTypes()
{
static $exclude = array();
if (empty($exclude))
{
$types = vB_Types::instance()->getContentTypes();
foreach ($types as $className => $type)
{
try
{
if (class_exists('vB_Api_Content_' . $type['class'], true))
{
$contentAPI = vB_Api::instanceInternal('Content_' . $type['class']);
if ($contentAPI->getTextCountChange() == 0)
{
$exclude[] = $type['id'];
}
}
}
catch(exception $e)
{
}
}
}
return $exclude;
}
/**
* Verifies integrity of a node in db. Checks "last" data, counts, that it has a route, etc.
*
* Note that this is intended for testing and diagnostics and may be slow
*
* @param mixed node record. Should have at least the information from node library getNode().
* @return mixed either true or an array of error strings
*/
public static function validateRecord($record)
{
$db = vB::getDbAssertor();
$result['errors'] = array();
// -- VERIFY ROUTE --
// @TODO remove the check and check for all types when photo and attachs have routeid
$photoType = vB_Types::instance()->getContentTypeID('vBForum_Photo');
$attachType = vB_Types::instance()->getContentTypeID('vBForum_Attach');
if (!in_array($record['contenttypeid'], array($photoType, $attachType)) AND !intval($record['routeid']))
{
$result['errors'][] = "Invalid routeid for node: " . $record['nodeid'] . " should be greater than 0";
}
// -- VERIFY COUNTS --
$counts = self::getCounts($record['nodeid']);
foreach($counts as $key=>$count)
{
if ($count == NULL)
{
$count = 0;
}
if (!isset($record[$key]))
{
$result['errors'][] = "Couldn't check $key";
}
else if ($record[$key] != $count)
{
$result['errors'][] = "Invalid count: $key values do not match (current:{$record[$key]} - expected:$count)";
}
}
// -- VERIFY SHOW PUBLISHED --
// This could be a little more rigorous by checking parent status and making sure that the showpublished is
// completely right. However for now we'll simply make sure that if the node isn't published showpublished
// isn't on (since we've seen that problem in the wild.
$timenow = vB::getRequest()->getTimeNow();
if ($record['publishdate'] > $timenow OR ($record['unpublishdate'] > 0 AND $record['unpublishdate'] <= $timenow))
{
if ($record['showpublished'] == 1)
{
$result['errors'][] = "Node is not published but showpushlished is true";
}
}
// -- VERIFY LAST DATA --
if (($counts['textcount'] + $counts['totalcount']) > 0)
{
//verify lastcontentid value is valid
if (intval($record['lastcontentid']) == 0)
{
$result['errors'][] = 'Invalid lastcontentid value: the node has children that are not reflected in last content value';
}
else
{
// verify last content exists
$excluded = self::getExcludedTypes();
$lastcontent = $db->getRow('vBForum:getLastData', array(
'parentid' => $record['nodeid'],
'timenow' => vB::getRequest()->getTimeNow(),
'excludeTypes' => $excluded
));
if (!$lastcontent)
{
$result['errors'][] = 'Couldn\'t find last content';
}
else
{
$skipCheck = false;
if ($lastcontent['nodeid'] != $record['lastcontentid'])
{
$checkLast = vB_Library::instance('node')->getNode($lastcontent['nodeid']);
if ($checkLast['publishdate'] == $record['lastcontent'])
{
//The selected last is just as good.
$skipCheck = true;
}
else
{
$result['errors'][] = "Invalid lastcontentid: values do not match (expected:{$lastcontent['nodeid']} - current:{$record['lastcontentid']})";
}
}
if (!$skipCheck)
{
if ($lastcontent['authorname'] != $record['lastcontentauthor'])
{
$result['errors'][] = "Invalid lastcontentauthor: values do not match (expected:{$lastcontent['authorname']} - current:{$record['lastcontentauthor']})";
}
if ($lastcontent['userid'] != $record['lastauthorid'])
{
$result['errors'][] = "Invalid lastauthorid: values do not match (expected:{$lastcontent['userid']} - current:{$record['lastauthorid']})";
}
}
}
}
}
// If the counts are zero, then there should be NO child that's approved and published
else
{
$excluded = self::getExcludedTypes();
$counts = vB::getDbAssertor()->getRow('vBForum:getApprovedAndPublishedChildren',
array(
vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_STORED,
'excluded' => $excluded, 'parentid' => $record['nodeid']
)
);
if (count($counts) > 0)
{
$result['errors'][] = "Incorrect total count. Node {$record['nodeid']} should have 0 approved & published children.";
}
}
//check to see that getNodeContent returns valid information. This will delete some kinds of failure in search or node API's.
$nodeid = $record['nodeid'];
$node = vB_Library::instance('node')->getNodeContent($nodeid);
if (empty($node) OR empty($node[$nodeid]) OR empty($node[$nodeid]['userid']))
{
$result['errors'][] = "getNodeContent for $nodeid fails- probably a permissions error;";
}
// check that the node has a parent. Only nodeid = 1 should lack a parent.
// Check for !empty($node[$nodeid]) to not propagate on any getNodeContent failures
if (intval($nodeid) !== 1)
{
if (!empty($node[$nodeid]) AND intval($node[$nodeid]['parentid']) < 1)
{
$result['errors'][] = "Node $nodeid does not seem to have a valid parentid (found:{$node[$nodeid]['parentid']}). Only nodeid=1 should have a parentid of 0";
}
else // we have a valid parentid for all but the first node, so we can do some checks
{
/* If the node has open=1 & showopen=0, its parent must have showopen=0 (VBV-9700)
* Rather than checking for this specific case, let's do a general check of
* node.showopen = (node.open AND parent.showopen)
*/
// first we need the parent info
$parentid = $node[$nodeid]['parentid'];
$parent = vB_Library::instance('node')->getNodeContent($parentid);
if (empty($parent) OR empty($parent[$parentid]) OR !isset($parent[$parentid]['showopen']))
{
$result['errors'][] = "getNodeContent for $parentid (parent of $nodeid) fails- probably a permissions error;";
}
else
{
// do a general check of node.showopen = (node.open AND parent.showopen)
// this should also catch the case where node.open = 0 & node.showopen = 1, which should never happen.
if ($node[$nodeid]['showopen'] != ($node[$nodeid]['open'] AND $parent[$parentid]['showopen']))
{
$result['errors'][] = "Invalid showopen value: Node showopen={$node[$nodeid]['showopen']} but has node.open={$node[$nodeid]['open']} and parent.showopen={$parent[$parentid]['showopen']}";
}
}
}
}
if (empty($result['errors']))
{
return true;
}
else
{
return $result;
}
}
public static function validateClosure($nodeid)
{
$result = array('errors' => array());
$db = vB::getDbAssertor();
$closures = $db->getRows('vBForum:closure',
array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT, 'child' => $nodeid),
array('field' => 'depth', 'direction' => vB_dB_Query::SORT_ASC)
);
$nodeids = array();
foreach($closures as $closure)
{
$nodeids[] = $closure['parent'];
}
$nodes = $db->getRows('vBForum:node',
array(
vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT,
'nodeid' => $nodeids,
vB_dB_Query::COLUMNS_KEY => array('nodeid', 'parentid')
),
false,
'nodeid'
);
$depth = 0;
$nextparent = $nodeid;
$foundOne = false;
foreach($closures as $closure)
{
if ($foundOne)
{
$result['errors'][] = "Found nodeid 1, but it was not the last closure record";
}
else if ($closure['depth'] != $depth)
{
$result['errors'][] = "Expected depth of $depth, found depth of " . $closure['depth'];
}
else if ($closure['parent'] != $nextparent)
{
$result['errors'][] = "Expected parent at depth of $depth to be $nextparent, found parent " . $closure['parent'];
}
if ($nextparent == 1)
{
$foundOne = true;
}
if (isset($nodes[$nextparent]))
{
$nextparent = $nodes[$nextparent]['parentid'];
}
else
{
$result['errors'][] = "Did not find node for parent $nextparent";
}
$depth++;
}
if (!$foundOne)
{
$result['errors'][] = "Nodeid 1 was not amoung the nodes parents";
}
if (empty($result['errors']))
{
return true;
}
else
{
return $result;
}
}
/**
* This method fixes the count values for a node
* @param int $nodeId - Node to fix
*/
public static function fixNodeCount($nodeId, $noLast = false)
{
$counts = self::getCounts($nodeId);
$changes = array(
'textcount'=> $counts['textcount'],
'textunpubcount'=> $counts['textunpubcount'],
'totalcount' => $counts['totalcount'],
'totalunpubcount' => $counts['totalunpubcount']
);
if (!$noLast)
{
$last = vB::getDbAssertor()->assertQuery(
'vBForum:getLastData', array(
'parentid' => $nodeId,
'timenow' => vB::getRequest()->getTimeNow(),
'excludeTypes' => self::getExcludedTypes())
);
}
if (isset($last) AND $last->valid())
{
$lastData = $last->current();
$changes['lastcontent'] = $lastData['publishdate'];
$changes['lastcontentauthor'] = $lastData['authorname'];
$changes['lastcontentid'] = $lastData['nodeid'];
$changes['lastauthorid'] = $lastData['userid'];
}
else
{
$changes['lastcontent'] = '';
$changes['lastcontentauthor'] = '';
$changes['lastcontentid'] = 0;
$changes['lastauthorid'] = 0;
}
vB::getDbAssertor()->update('vBForum:node', $changes, array('nodeid' => $nodeId));
vB_Cache::instance()->allCacheEvent("nodeChg_" .$nodeId);
}
}
/*=========================================================================*\
|| #######################################################################
|| # Downloaded: 15:45, Tue Sep 8th 2015
|| # CVS: $RCSfile$ - $Revision: 83435 $
|| #######################################################################
\*=========================================================================*/