This repository was archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBoard.php
More file actions
1054 lines (891 loc) · 34.1 KB
/
Board.php
File metadata and controls
1054 lines (891 loc) · 34.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Foolz\FoolFuuka\Model;
use Foolz\FoolFrame\Model\DoctrineConnection;
use Foolz\Cache\Cache;
use Foolz\FoolFrame\Model\Model;
use Foolz\Plugin\PlugSuit;
use Foolz\Profiler\Profiler;
class BoardException extends \Exception {}
class BoardThreadNotFoundException extends BoardException {}
class BoardPostNotFoundException extends BoardException {}
class BoardMalformedInputException extends BoardException {}
class BoardNotCompatibleMethodException extends BoardException {}
class BoardMissingOptionsException extends BoardException {}
/**
* Deals with all the data in the board tables
*/
class Board extends Model
{
use PlugSuit;
/**
* Array of Comment sorted for output
*
* @var CommentBulk[]
*/
protected $comments = null;
/**
* Array of Comment in a plain array
*
* @var CommentBulk[]
*/
protected $comments_unsorted = null;
/**
* The count of the query without LIMIT
*
* @var int
*/
protected $total_count = null;
/**
* The method selected to retrieve comments
*
* @var string
*/
protected $method_fetching = null;
/**
* The method selected to retrieve the comment's count without LIMIT
*
* @var string
*/
protected $method_counting = null;
/**
* The options to give to the retrieving method
*
* @var array
*/
protected $options = [];
/**
* The options to give to the Comment class
*
* @var array
*/
protected $comment_options = [];
/**
* The selected Radix
*
* @var Radix
*/
protected $radix = null;
/**
* The options for the API. If null it means we're not using API mode
*
* @var array
*/
protected $api = null;
/**
* @var DoctrineConnection
*/
protected $dc;
/**
* @var Profiler
*/
protected $profiler;
/**
* @var CommentFactory
*/
protected $comment_factory;
public function __construct(\Foolz\FoolFrame\Model\Context $context)
{
parent::__construct($context);
$this->dc = $context->getService('doctrine');
$this->profiler = $context->getService('profiler');
$this->comment_factory = $context->getService('foolfuuka.comment_factory');
}
/**
* Creates a new instance of Comment
*
* @return \Foolz\FoolFuuka\Model\Comment
*/
public static function forge($context)
{
return new static($context);
}
/**
* Returns a single comment, and executes the query if not already executed
*
* @return \Foolz\FoolFuuka\Model\Comment A single comment object
*/
protected function p_getComment()
{
$this->getComments();
return current($this->comments);
}
/**
* Returns the comments, and executes the query if not already executed
*
* @return \Foolz\FoolFuuka\Model\Comment[] The array of comment objects
*/
protected function p_getComments()
{
if ($this->comments === null) {
if (method_exists($this, 'p_'.$this->method_fetching)) {
$this->profiler->log('Start Board::getComments() with method '.$this->method_fetching);
$this->{$this->method_fetching}();
$this->loadBacklinks();
$this->profiler->log('End Board::getComments() with method '.$this->method_fetching);
} else {
$this->comments = false;
}
}
return $this->comments;
}
/**
* Returns the comments in a plain array, and executes the query if not already executed
*
* @return \Foolz\FoolFuuka\Model\Comment[] The array of comment objects
*/
protected function p_getCommentsUnsorted()
{
$this->getComments();
return $this->comments_unsorted;
}
/**
* Returns the count without LIMIT, and executes the query if not already executed
*
* @return int The count
*/
protected function p_getCount()
{
if ($this->total_count === null) {
if (method_exists($this, 'p_'.$this->method_counting)) {
$this->profiler->log('Start Board::getCount() with method '.$this->method_counting);
$this->{$this->method_counting}();
$this->profiler->log('End Board::getCount() with method '.$this->method_counting);
} else {
$this->total_count = false;
}
}
return $this->total_count;
}
/**
* Returns the number of pages of the result
*
* @return int The number of pages
*/
protected function p_getPages()
{
return ceil($this->getCount() / $this->options['per_page']);
}
/**
* Returns the comment with the highest item
*
* @param string $key The key (column) on which to calculate the "highest" Comment
*
* @return \Foolz\FoolFuuka\Model\Board
*/
protected function p_getHighest($key)
{
$temp = $this->comments_unsorted[0];
foreach ($this->comments_unsorted as $comment) {
if ($temp->comment->$key < $comment->comment->$key) {
$temp = $comment;
}
}
return $temp;
}
/**
* Sets the fetching method
*
* @param string $name The method name of the fetching method, without p_
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_setMethodFetching($name)
{
$this->method_fetching = $name;
return $this;
}
/**
* Sets the counting method
*
* @param string $name The method name of the counting method, without p_
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_setMethodCounting($name)
{
$this->method_counting = $name;
return $this;
}
/**
* Set the options to pass to the counting and fetching methods. These are usually exploded in the method.
*
* @param string|array $name The name of the variable, if associative array it will be used instead
* @param mixed $value The value of the variable
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
public function setOptions($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => $item) {
$this->setOptions($key, $item);
}
return $this;
}
$this->options[$name] = $value;
return $this;
}
/**
* Set the options to pass to the Comment object
*
* @param string|array $name The name of the variable, if associative array it will be used instead
* @param mixed $value The value of the variable
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_setCommentOptions($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => $item) {
$this->setCommentOptions($key, $item);
}
return $this;
}
$this->comment_options[$name] = $value;
return $this;
}
/**
* Sets the Radix object
*
* @param Radix $radix The Radix object pertaining this Board
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_setRadix(Radix $radix = null)
{
$this->radix = $radix;
return $this;
}
/**
* Sets the page to fetch
*
* @param int $page The page to fetch
*
* @return \Foolz\FoolFuuka\Model\Board The current object
* @throws \Foolz\FoolFuuka\Model\BoardException If the page number is not valid
*/
protected function p_setPage($page)
{
$page = intval($page);
if ($page < 1) {
throw new BoardException(_i('The page number is not valid.'));
}
$this->setOptions('page', $page);
return $this;
}
/**
* Checks if the string is a valid post number
*
* @param string $str The string to test on
*
* @return boolean True if the post number is valid, false otherwise
*/
public static function isValidPostNumber($str)
{
return ctype_digit((string) $str) || preg_match('/^[0-9]+(,|_)[0-9]+$/', $str);
}
/**
* Splits the post number in num and subnum, even if there's no subnum in the string.
*
* @param string $num The string to split. Must be a "valid post number" (check with static::isValidPostNumber())
*
* @return array Two keys: num, subnum
*/
public static function splitPostNumber($num)
{
if (strpos($num, ',') !== false) {
$arr = explode(',', $num);
} elseif (strpos($num, '_') !== false) {
$arr = explode('_', $num);
} else {
$result['num'] = $num;
$result['subnum'] = 0;
return $result;
}
$result['num'] = $arr[0];
$result['subnum'] = isset($arr[1]) ? $arr[1] : 0;
return $result;
}
/**
* Loops over the unsorted comments to fetch all the backlinks
*/
protected function loadBacklinks()
{
if ($this->comments_unsorted === null) {
return;
}
$c = new Comment($this->getContext());
foreach ($this->comments_unsorted as $bulk) {
$c->setBulk($bulk);
$c->processComment(true);
$bulk->clean();
}
}
/**
* Sets the board to the "latest" mode, to create index pages with a couple of the last posts per thread
* Options: page, per_page, order[by_post, by_thread, ghost]
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getLatest()
{
$this
->setMethodFetching('getLatestComments')
->setMethodCounting('getLatestCount')
->setOptions([
'per_page' => 20,
'per_thread' => 5,
'order' => 'by_post'
]);
return $this;
}
/**
* Returns the latest threads with a couple of the latest posts in each thread
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getLatestComments()
{
$this->profiler->log('Board::getLatestComments Start');
extract($this->options);
try {
// not archives, not ghosts, under 10 pages, 10 per page
if (!$this->radix->archive && $order !== 'ghost' && $page <= 10 && $per_page == 10) {
list($results, $query_posts) = Cache::item('foolfuuka.model.board.getLatestComments.query.'
.$this->radix->shortname.'.'.$order.'.'.$page)->get();
} else {
// lots of cases we don't want to handle go dynamic
throw new \OutOfBoundsException;
}
} catch(\OutOfBoundsException $e) {
switch ($order) {
case 'by_post':
$query = $this->dc->qb()
->select('*, thread_num AS unq_thread_num')
->from($this->radix->getTable('_threads'), 'rt')
->orderBy('rt.sticky', 'DESC')
->addOrderBy('rt.time_bump', 'DESC')
->setMaxResults($per_page)
->setFirstResult(($page * $per_page) - $per_page);
break;
case 'by_thread':
$query = $this->dc->qb()
->select('*, thread_num AS unq_thread_num')
->from($this->radix->getTable('_threads'), 'rt')
->orderBy('rt.sticky', 'DESC')
->addOrderBy('rt.thread_num', 'DESC')
->setMaxResults($per_page)
->setFirstResult(($page * $per_page) - $per_page);
break;
case 'ghost':
$query = $this->dc->qb()
->select('*, thread_num AS unq_thread_num')
->from($this->radix->getTable('_threads'), 'rt')
->where('rt.time_ghost_bump IS NOT null')
->orderBy('rt.sticky', 'DESC')
->addOrderBy('rt.time_ghost_bump', 'DESC')
->setMaxResults($per_page)
->setFirstResult(($page * $per_page) - $per_page);
break;
}
$threads = $query
->execute()
->fetchAll();
if (!count($threads)) {
$this->comments = [];
$this->comments_unsorted = [];
$this->profiler->logMem('Board $this', $this);
$this->profiler->log('Board::getLatestComments End Prematurely');
return $this;
}
// populate arrays with posts
$results = [];
$sql_arr = [];
foreach ($threads as $thread) {
$sql_arr[] = '('.$this->dc->qb()
->select('*')
->from($this->radix->getTable(), 'r')
->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->where('r.thread_num = '.$thread['unq_thread_num'])
->orderBy('op', 'DESC')
->addOrderBy('num', 'DESC')
->addOrderBy('subnum', 'DESC')
->setMaxResults($per_thread + 1)
->setFirstResult(0)
->getSQL().')';
$omitted = ($thread['nreplies'] - ($per_thread + 1));
$results[$thread['thread_num']] = [
'omitted' => $omitted > 0 ? $omitted : 0,
'images_omitted' => ($thread['nimages'] - 1)
];
}
$query_posts = $this->dc->getConnection()
->executeQuery(implode(' UNION ', $sql_arr))
->fetchAll();
// not archives, not ghosts, under 10 pages, 10 per page
if (!$this->radix->archive && $order !== 'ghost' && $page <= 10 && $per_page == 10) {
Cache::item('foolfuuka.model.board.getLatestComments.query.'
.$this->radix->shortname.'.'.$order.'.'.$page)->set([$results, $query_posts], 300);
}
}
foreach ($query_posts as $key => $row) {
$data = new CommentBulk();
$data->import($row, $this->radix);
unset($query_posts[$key]);
$this->comments_unsorted[] = $data;
}
unset($query_posts);
$this->profiler->logMem('Board $this->comments_unsorted', $this->comments_unsorted);
// populate results array and order posts
foreach ($this->comments_unsorted as $bulk) {
if ($bulk->comment->op == 0) {
if ($bulk->media !== null && $bulk->media->preview_orig) {
$results[$bulk->comment->thread_num]['images_omitted']--;
}
if (!isset($results[$bulk->comment->thread_num]['posts'])) {
$results[$bulk->comment->thread_num]['posts'] = [];
}
array_unshift($results[$bulk->comment->thread_num]['posts'], $bulk);
} else {
$results[$bulk->comment->thread_num]['op'] = $bulk;
}
}
$this->comments = $results;
$this->profiler->logMem('Board $this->comments', $this->comments);
$this->profiler->logMem('Board $this', $this);
$this->profiler->log('Board::getLatestComments End');
return $this;
}
/**
* Returns the count of the threads available
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getLatestCount()
{
$this->profiler->log('Board::getLatestCount Start');
extract($this->options);
$type_cache = 'thread_num';
if ($order == 'ghost') {
$type_cache = 'ghost_num';
}
try {
$this->total_count = Cache::item('Foolz_FoolFuuka_Model_Board.getLatestCount.result.'.$type_cache)->get();
return $this;
} catch (\OutOfBoundsException $e) {
switch ($order) {
// these two are the same
case 'by_post':
case 'by_thread':
$query_threads = $this->dc->qb()
->select('COUNT(thread_num) AS threads')
->from($this->radix->getTable('_threads'), 'rt');
break;
case 'ghost':
$query_threads = $this->dc->qb()
->select('COUNT(thread_num) AS threads')
->from($this->radix->getTable('_threads'), 'rt')
->where('rt.time_ghost_bump IS NOT null');
break;
}
$result = $query_threads
->execute()
->fetch();
$this->total_count = $result['threads'];
Cache::item('Foolz_FoolFuuka_Model_Board.getLatestCount.result.'.$type_cache)->set($this->total_count, 300);
}
$this->profiler->logMem('Board $this', $this);
$this->profiler->log('Board::getLatestCount End');
return $this;
}
/**
* Sets the "thread" mode
* Options: page, per_page
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getThreads()
{
$this
->setMethodFetching('getThreadsComments')
->setMethodCounting('getThreadsCount')
->setOptions([
'per_page' => 20,
'order' => 'by_post'
]);
return $this;
}
/**
* Fetches a bunch of threads (in example for gallery)
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getThreadsComments()
{
$this->profiler->log('Board::getThreadsComments Start');
extract($this->options);
try {
// not archives, not ghosts, under 10 pages, 10 per page
if (!$this->radix->archive && $page <= 10 && $per_page == 100) {
$result = Cache::item('foolfuuka.model.board.getThreadsComments.query.'
.$this->radix->shortname.'.'.$page)->get();
} else {
// lots of cases we don't want to handle go dynamic
throw new \OutOfBoundsException;
}
} catch (\OutOfBoundsException $e) {
$inner_query = $this->dc->qb()
->select('*, thread_num as unq_thread_num')
->from($this->radix->getTable('_threads'), 'rt')
->orderBy('rt.time_op', 'DESC')
->setMaxResults($per_page)
->setFirstResult(($page * $per_page) - $per_page)
->getSQL();
$result = $this->dc->qb()
->select('*')
->from('('.$inner_query.')', 'g')
->join('g', $this->radix->getTable(), 'r', 'r.num = g.unq_thread_num AND r.subnum = 0')
->leftJoin('g', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->execute()
->fetchAll();
// not archives, not ghosts, under 10 pages, 10 per page
if (!$this->radix->archive && $page <= 10 && $per_page == 100) {
Cache::item('foolfuuka.model.board.getThreadsComments.query.'
.$this->radix->shortname.'.'.$page)->set($result, 300);
}
}
if (!count($result)) {
$this->comments = [];
$this->comments_unsorted = [];
$this->profiler->logMem('Board $this', $this);
$this->profiler->log('Board::get_threadscomments End Prematurely');
return $this;
}
foreach ($result as $key => $row) {
$data = new CommentBulk();
$data->import($row, $this->radix);
unset($result[$key]);
$this->comments_unsorted[] = $data;
}
unset($result);
$this->comments = $this->comments_unsorted;
$this->profiler->logMem('Board $this->comments', $this->comments);
$this->profiler->logMem('Board $this', $this);
$this->profiler->log('Board::getThreadsComments End');
return $this;
}
/**
* Counts the available threads
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getThreadsCount()
{
extract($this->options);
try {
$this->total_count = Cache::item('Foolz_FoolFuuka_Model_Board.getThreadsCount.result')->get();
} catch (\OutOfBoundsException $e) {
$result = $this->dc->qb()
->select('COUNT(thread_num) AS threads')
->from($this->radix->getTable('_threads'), 'rt')
->execute()
->fetch();
$this->total_count = $result['threads'];
Cache::item('Foolz_FoolFuuka_Model_Board.getThreadsCount.result')->set($this->total_count, 300);
}
return $this;
}
/**
* Sets the Board object to Thread mode
* Options: type=[from_doc_id, ghosts, last_x], (int)num(thread number)
* Options for "from_doc_id": (int)latest_doc_id
* Options for "last_x": (int)last_limit
*
* @param int $num The number of the thread
*
* @return \Foolz\FoolFuuka\Model\Board The current object
* @throws BoardMalformedInputException
*/
protected function p_getThread($num)
{
// default variables
$this
->setMethodFetching('getThreadComments')
->setOptions(['type' => 'thread', 'realtime' => false]);
if (!ctype_digit((string) $num) || $num < 1) {
throw new BoardMalformedInputException(_i('The thread number is invalid.'));
}
$this->setOptions('num', $num);
return $this;
}
/**
* Gets a thread
*
* @return \Foolz\FoolFuuka\Model\Board The current object
* @throws BoardThreadNotFoundException If the thread wasn't found
*/
protected function p_getThreadComments()
{
$this->profiler->log('Board::getThreadComments Start');
extract($this->options);
// determine type
switch ($type) {
case 'from_doc_id':
$query_result = $this->dc->qb()
->select('*')
->from($this->radix->getTable(), 'r')
->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->where('thread_num = :thread_num')
->andWhere('doc_id > :latest_doc_id')
->orderBy('num', 'ASC')
->addOrderBy('subnum', 'ASC')
->setParameter(':thread_num', $num)
->setParameter(':latest_doc_id', $latest_doc_id)
->execute()
->fetchAll();
break;
case 'ghosts':
$query_result = $this->dc->qb()
->select('*')
->from($this->radix->getTable(), 'r')
->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->where('thread_num = :thread_num')
->where('subnum <> 0')
->orderBy('num', 'ASC')
->addOrderBy('subnum', 'ASC')
->setParameter(':thread_num', $num)
->execute()
->fetchAll();
break;
case 'last_x':
try {
// we save some cache memory by only saving last_50, so it must always fail otherwise
if ($last_limit != 50) {
throw new \OutOfBoundsException;
}
$query_result = Cache::item('foolfuuka.model.board.getThreadComments.last_50.'
.md5(serialize([$this->radix->shortname, $num])))->get();
} catch (\OutOfBoundsException $e) {
$subquery_first = $this->dc->qb()
->select('*')
->from($this->radix->getTable(), 'xr')
->where('num = '.$this->dc->getConnection()->quote($num))
->setMaxResults(1)
->getSQL();
$subquery_last = $this->dc->qb()
->select('*')
->from($this->radix->getTable(), 'xrr')
->where('thread_num = '.$this->dc->getConnection()->quote($num))
->orderBy('num', 'DESC')
->addOrderBy('subnum', 'DESC')
->setMaxResults($last_limit)
->getSQL();
$query_result = $this->dc->qb()
->select('*')
->from('(('.$subquery_first.') UNION ('.$subquery_last.'))', 'r')
->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->orderBy('num', 'ASC')
->addOrderBy('subnum', 'ASC')
->execute()
->fetchAll();
// cache only if it's last_50
if ($last_limit == 50) {
$cache_time = 300;
if ($this->radix->archive) {
$cache_time = 30;
// over 7 days is old
$old = time() - 604800;
// set a very long cache time for archive threads older than a week, in case a ghost post will bump it
foreach ($query_result as $k => $r) {
if ($r['timestamp'] < $old) {
$cache_time = 300;
break;
}
}
}
Cache::item('foolfuuka.model.board.getThreadComments.last_50.'
.md5(serialize([$this->radix->shortname, $num])))->set($query_result, $cache_time);
}
}
break;
case 'thread':
try {
$query_result = Cache::item('foolfuuka.model.board.getThreadComments.thread.'
.md5(serialize([$this->radix->shortname, $num])))->get();
} catch (\OutOfBoundsException $e) {
$query_result = $this->dc->qb()
->select('*')
->from($this->radix->getTable(), 'r')
->leftJoin('r', $this->radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->where('thread_num = :thread_num')
->orderBy('num', 'ASC')
->addOrderBy('subnum', 'ASC')
->setParameter(':thread_num', $num)
->execute()
->fetchAll();
$cache_time = 300;
if ($this->radix->archive) {
$cache_time = 30;
// over 7 days is old
$old = time() - 604800;
// set a very long cache time for archive threads older than a week, in case a ghost post will bump it
foreach ($query_result as $k => $r) {
if ($r['timestamp'] < $old) {
$cache_time = 300;
break;
}
}
}
Cache::item('foolfuuka.model.board.getThreadComments.thread.'
.md5(serialize([$this->radix->shortname, $num])))->set($query_result, $cache_time);
}
break;
}
if (!count($query_result) && isset($latest_doc_id)) {
return $this->comments = $this->comments_unsorted = [];
}
if (!count($query_result)) {
throw new BoardThreadNotFoundException(_i('There\'s no such a thread.'));
}
foreach ($query_result as $key => $row) {
$data = new CommentBulk();
$data->import($row, $this->radix);
unset($query_result[$key]);
$this->comments_unsorted[] = $data;
}
unset($query_result);
foreach ($this->comments_unsorted as $key => $bulk) {
if ($bulk->comment->op == 0) {
$this->comments[$bulk->comment->thread_num]['posts']
[$bulk->comment->num.(($bulk->comment->subnum == 0) ? '' : '_'.$bulk->comment->subnum)] = &$this->comments_unsorted[$key];
} else {
$this->comments[$bulk->comment->num]['op'] = &$this->comments_unsorted[$key];
}
}
$this->profiler->logMem('Board $this->comments', $this->comments);
$this->profiler->logMem('Board $this', $this);
$this->profiler->log('Board::getThreadComments End');
return $this;
}
/**
* Returns an array specifying the thread statuses.
* Returned array keys: closed, dead, disable_image_upload
*
* @return array An associative array with boolean values
* @throws BoardNotCompatibleMethodException If the specified fetching method is not "getThreadComments"
* @throws BoardThreadNotFoundException If the thread was not found
*/
protected function p_getThreadStatus()
{
if ($this->method_fetching !== 'getThreadComments') {
throw new BoardNotCompatibleMethodException;
}
extract($this->options);
$thread = $this->dc->qb()
->select('*')
->from($this->radix->getTable('_threads'), 't')
->where('thread_num = :thread_num')
->setParameter(':thread_num', $num)
->execute()
->fetch();
if (!$thread) {
throw new BoardThreadNotFoundException(_i('The thread you were looking for does not exist.'));
}
// define variables to override
$ghost_post_present = false;
$last_modified = $thread['time_last_modified'];
if ($thread['time_ghost'] !== null) {
$ghost_post_present = true;
}
if ($this->radix->archive) {
$timestamp = new \DateTime(date('Y-m-d H:i:s', $last_modified), new \DateTimeZone('America/New_York'));
$timestamp->setTimezone(new \DateTimeZone('UTC'));
$last_modified = strtotime($timestamp->format('Y-m-d H:i:s'));
}
$result = [
'sticky' => (bool) $thread['sticky'],
'closed' => (bool) $thread['locked'],
'dead' => (bool) $this->radix->archive,
'ghost_exist' => $ghost_post_present,
'disable_image_upload' => (bool) $this->radix->archive,
'last_modified' => $last_modified
];
if (($this->radix->getValue('thread_lifetime') > 0 && time() - $thread['time_last'] > $this->radix->getValue('thread_lifetime')) || $ghost_post_present) {
$result['dead'] = true;
$result['disable_image_upload'] = true;
}
if ($thread['nreplies'] > $this->radix->getValue('max_posts_count')) {
$result['dead'] = true;
$result['disable_image_upload'] = true;
} elseif ($thread['nimages'] >= $this->radix->getValue('max_images_count')) {
$result['disable_image_upload'] = true;
}
if ($this->radix->getValue('disable_ghost') && $result['dead']) {
$result['closed'] = true;
}
return $result;
}
/**
* Sets the Board object to fetch a post
* Options available: num OR doc_id
*
* @param string $num If specified, a valid post number
*
* @return \Foolz\FoolFuuka\Model\Board The current object
*/
protected function p_getPost($num = null)
{
// default variables
$this->setMethodFetching('getPostComment');
if ($num !== null) {
$this->setOptions('num', $num);
}
return $this;
}