XoopsXmlRss2Parser   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 58
dl 0
loc 140
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setItems() 0 3 1
A setImageData() 0 3 1
A getItems() 0 3 1
A getTempArr() 0 3 1
A resetTempArr() 0 4 1
A setChannelData() 0 6 2
A getImageData() 0 12 3
A __construct() 0 27 1
A setTempArr() 0 6 2
A getChannelData() 0 11 3
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project http://xoops.org/
14
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
21
require_once(XOOPS_ROOT_PATH . '/class/xml/saxparser.php');
22
require_once(XOOPS_ROOT_PATH . '/class/xml/xmltaghandler.php');
23
24
/**
25
 * Class XoopsXmlRss2Parser
26
 */
27
class XoopsXmlRss2Parser extends SaxParser
28
{
29
    public $_tempArr     = array();
30
    public $_channelData = array();
31
    public $_imageData   = array();
32
    public $_items       = array();
33
34
    /**
35
     * @param $input
36
     */
37
    public function __construct($input)
38
    {
39
        parent::__construct($input);
40
        $this->useUtfEncoding();
41
        $this->addTagHandler(new RssChannelHandler());
42
        $this->addTagHandler(new RssTitleHandler());
43
        $this->addTagHandler(new RssLinkHandler());
44
        $this->addTagHandler(new RssGeneratorHandler());
45
        $this->addTagHandler(new RssDescriptionHandler());
46
        $this->addTagHandler(new RssCopyrightHandler());
47
        $this->addTagHandler(new RssNameHandler());
48
        $this->addTagHandler(new RssManagingEditorHandler());
49
        $this->addTagHandler(new RssLanguageHandler());
50
        $this->addTagHandler(new RssLastBuildDateHandler());
51
        $this->addTagHandler(new RssWebMasterHandler());
52
        $this->addTagHandler(new RssImageHandler());
53
        $this->addTagHandler(new RssUrlHandler());
54
        $this->addTagHandler(new RssWidthHandler());
55
        $this->addTagHandler(new RssHeightHandler());
56
        $this->addTagHandler(new RssItemHandler());
57
        $this->addTagHandler(new RssCategoryHandler());
58
        $this->addTagHandler(new RssPubDateHandler());
59
        $this->addTagHandler(new RssCommentsHandler());
60
        $this->addTagHandler(new RssSourceHandler());
61
        $this->addTagHandler(new RssAuthorHandler());
62
        $this->addTagHandler(new RssGuidHandler());
63
        $this->addTagHandler(new RssTextInputHandler());
64
    }
65
66
    /**
67
     * @param $name
68
     * @param $value
69
     */
70
    public function setChannelData($name, &$value)
71
    {
72
        if (!isset($this->_channelData[$name])) {
73
            $this->_channelData[$name] =& $value;
74
        } else {
75
            $this->_channelData[$name] .= $value;
76
        }
77
    }
78
79
    /**
80
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by beckmi
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
81
     *
82
     * @return array|bool
83
     */
84
    public function &getChannelData($name = null)
85
    {
86
        if (isset($name)) {
87
            if (isset($this->_channelData[$name])) {
88
                return $this->_channelData[$name];
89
            }
90
91
            return false;
92
        }
93
94
        return $this->_channelData;
95
    }
96
97
    /**
98
     * @param $name
99
     * @param $value
100
     */
101
    public function setImageData($name, &$value)
102
    {
103
        $this->_imageData[$name] =& $value;
104
    }
105
106
    /**
107
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by beckmi
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
108
     *
109
     * @return array|bool
110
     */
111
    public function &getImageData($name = null)
112
    {
113
        if (isset($name)) {
114
            if (isset($this->_imageData[$name])) {
115
                return $this->_imageData[$name];
116
            }
117
            $return = false;
118
119
            return $return;
120
        }
121
122
        return $this->_imageData;
123
    }
124
125
    /**
126
     * @param $itemarr
127
     */
128
    public function setItems(&$itemarr)
129
    {
130
        $this->_items[] =& $itemarr;
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function &getItems()
137
    {
138
        return $this->_items;
139
    }
140
141
    /**
142
     * @param        $name
143
     * @param        $value
144
     * @param string $delim
145
     */
146
    public function setTempArr($name, &$value, $delim = '')
147
    {
148
        if (!isset($this->_tempArr[$name])) {
149
            $this->_tempArr[$name] =& $value;
150
        } else {
151
            $this->_tempArr[$name] .= $delim . $value;
152
        }
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getTempArr()
159
    {
160
        return $this->_tempArr;
161
    }
162
163
    public function resetTempArr()
164
    {
165
        unset($this->_tempArr);
166
        $this->_tempArr = array();
167
    }
168
}
169
170
/**
171
 * Class RssChannelHandler
172
 */
173
class RssChannelHandler extends XmlTagHandler
174
{
175
    /**
176
     * RssChannelHandler constructor.
177
     */
178
    public function __construct()
179
    {
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getName()
186
    {
187
        return 'channel';
188
    }
189
}
190
191
/**
192
 * Class RssTitleHandler
193
 */
194
class RssTitleHandler extends XmlTagHandler
195
{
196
    /**
197
     * RssTitleHandler constructor.
198
     */
199
    public function __construct()
200
    {
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getName()
207
    {
208
        return 'title';
209
    }
210
211
    /**
212
     * @param $parser
213
     * @param $data
214
     */
215
    public function handleCharacterData($parser, &$data)
216
    {
217
        switch ($parser->getParentTag()) {
218
            case 'channel':
219
                $parser->setChannelData('title', $data);
220
                break;
221
            case 'image':
222
                $parser->setImageData('title', $data);
223
                break;
224
            case 'item':
225
            case 'textInput':
226
                $parser->setTempArr('title', $data);
227
                break;
228
            default:
229
                break;
230
        }
231
    }
232
}
233
234
/**
235
 * Class RssLinkHandler
236
 */
237
class RssLinkHandler extends XmlTagHandler
238
{
239
    /**
240
     * RssLinkHandler constructor.
241
     */
242
    public function __construct()
243
    {
244
    }
245
246
    /**
247
     * @return string
248
     */
249
    public function getName()
250
    {
251
        return 'link';
252
    }
253
254
    /**
255
     * @param $parser
256
     * @param $data
257
     */
258
    public function handleCharacterData($parser, &$data)
259
    {
260
        switch ($parser->getParentTag()) {
261
            case 'channel':
262
                $parser->setChannelData('link', $data);
263
                break;
264
            case 'image':
265
                $parser->setImageData('link', $data);
266
                break;
267
            case 'item':
268
            case 'textInput':
269
                $parser->setTempArr('link', $data);
270
                break;
271
            default:
272
                break;
273
        }
274
    }
275
}
276
277
/**
278
 * Class RssDescriptionHandler
279
 */
280
class RssDescriptionHandler extends XmlTagHandler
281
{
282
    /**
283
     * RssDescriptionHandler constructor.
284
     */
285
    public function __construct()
286
    {
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function getName()
293
    {
294
        return 'description';
295
    }
296
297
    /**
298
     * @param $parser
299
     * @param $data
300
     */
301
    public function handleCharacterData($parser, &$data)
302
    {
303
        switch ($parser->getParentTag()) {
304
            case 'channel':
305
                $parser->setChannelData('description', $data);
306
                break;
307
            case 'image':
308
                $parser->setImageData('description', $data);
309
                break;
310
            case 'item':
311
            case 'textInput':
312
                $parser->setTempArr('description', $data);
313
                break;
314
            default:
315
                break;
316
        }
317
    }
318
}
319
320
/**
321
 * Class RssGeneratorHandler
322
 */
323
class RssGeneratorHandler extends XmlTagHandler
324
{
325
    /**
326
     * RssGeneratorHandler constructor.
327
     */
328
    public function __construct()
329
    {
330
    }
331
332
    /**
333
     * @return string
334
     */
335
    public function getName()
336
    {
337
        return 'generator';
338
    }
339
340
    /**
341
     * @param $parser
342
     * @param $data
343
     */
344
    public function handleCharacterData($parser, &$data)
345
    {
346
        switch ($parser->getParentTag()) {
347
            case 'channel':
348
                $parser->setChannelData('generator', $data);
349
                break;
350
            default:
351
                break;
352
        }
353
    }
354
}
355
356
/**
357
 * Class RssCopyrightHandler
358
 */
359
class RssCopyrightHandler extends XmlTagHandler
360
{
361
    /**
362
     * RssCopyrightHandler constructor.
363
     */
364
    public function __construct()
365
    {
366
    }
367
368
    /**
369
     * @return string
370
     */
371
    public function getName()
372
    {
373
        return 'copyright';
374
    }
375
376
    /**
377
     * @param $parser
378
     * @param $data
379
     */
380
    public function handleCharacterData($parser, &$data)
381
    {
382
        switch ($parser->getParentTag()) {
383
            case 'channel':
384
                $parser->setChannelData('copyright', $data);
385
                break;
386
            default:
387
                break;
388
        }
389
    }
390
}
391
392
/**
393
 * Class RssNameHandler
394
 */
395
class RssNameHandler extends XmlTagHandler
396
{
397
    /**
398
     * RssNameHandler constructor.
399
     */
400
    public function __construct()
401
    {
402
    }
403
404
    /**
405
     * @return string
406
     */
407
    public function getName()
408
    {
409
        return 'name';
410
    }
411
412
    /**
413
     * @param $parser
414
     * @param $data
415
     */
416
    public function handleCharacterData($parser, &$data)
417
    {
418
        switch ($parser->getParentTag()) {
419
            case 'textInput':
420
                $parser->setTempArr('name', $data);
421
                break;
422
            default:
423
                break;
424
        }
425
    }
426
}
427
428
/**
429
 * Class RssManagingEditorHandler
430
 */
431
class RssManagingEditorHandler extends XmlTagHandler
432
{
433
    /**
434
     * RssManagingEditorHandler constructor.
435
     */
436
    public function __construct()
437
    {
438
    }
439
440
    /**
441
     * @return string
442
     */
443
    public function getName()
444
    {
445
        return 'managingEditor';
446
    }
447
448
    /**
449
     * @param $parser
450
     * @param $data
451
     */
452
    public function handleCharacterData($parser, &$data)
453
    {
454
        switch ($parser->getParentTag()) {
455
            case 'channel':
456
                $parser->setChannelData('editor', $data);
457
                break;
458
            default:
459
                break;
460
        }
461
    }
462
}
463
464
/**
465
 * Class RssLanguageHandler
466
 */
467
class RssLanguageHandler extends XmlTagHandler
468
{
469
    /**
470
     * RssLanguageHandler constructor.
471
     */
472
    public function __construct()
473
    {
474
    }
475
476
    /**
477
     * @return string
478
     */
479
    public function getName()
480
    {
481
        return 'language';
482
    }
483
484
    /**
485
     * @param $parser
486
     * @param $data
487
     */
488
    public function handleCharacterData($parser, &$data)
489
    {
490
        switch ($parser->getParentTag()) {
491
            case 'channel':
492
                $parser->setChannelData('language', $data);
493
                break;
494
            default:
495
                break;
496
        }
497
    }
498
}
499
500
/**
501
 * Class RssWebMasterHandler
502
 */
503
class RssWebMasterHandler extends XmlTagHandler
504
{
505
    /**
506
     * RssWebMasterHandler constructor.
507
     */
508
    public function __construct()
509
    {
510
    }
511
512
    /**
513
     * @return string
514
     */
515
    public function getName()
516
    {
517
        return 'webMaster';
518
    }
519
520
    /**
521
     * @param $parser
522
     * @param $data
523
     */
524
    public function handleCharacterData($parser, &$data)
525
    {
526
        switch ($parser->getParentTag()) {
527
            case 'channel':
528
                $parser->setChannelData('webmaster', $data);
529
                break;
530
            default:
531
                break;
532
        }
533
    }
534
}
535
536
/**
537
 * Class RssDocsHandler
538
 */
539
class RssDocsHandler extends XmlTagHandler
540
{
541
    /**
542
     * RssDocsHandler constructor.
543
     */
544
    public function __construct()
545
    {
546
    }
547
548
    /**
549
     * @return string
550
     */
551
    public function getName()
552
    {
553
        return 'docs';
554
    }
555
556
    /**
557
     * @param $parser
558
     * @param $data
559
     */
560
    public function handleCharacterData($parser, &$data)
561
    {
562
        switch ($parser->getParentTag()) {
563
            case 'channel':
564
                $parser->setChannelData('docs', $data);
565
                break;
566
            default:
567
                break;
568
        }
569
    }
570
}
571
572
/**
573
 * Class RssTtlHandler
574
 */
575
class RssTtlHandler extends XmlTagHandler
576
{
577
    /**
578
     * RssTtlHandler constructor.
579
     */
580
    public function __construct()
581
    {
582
    }
583
584
    /**
585
     * @return string
586
     */
587
    public function getName()
588
    {
589
        return 'ttl';
590
    }
591
592
    /**
593
     * @param $parser
594
     * @param $data
595
     */
596
    public function handleCharacterData($parser, &$data)
597
    {
598
        switch ($parser->getParentTag()) {
599
            case 'channel':
600
                $parser->setChannelData('ttl', $data);
601
                break;
602
            default:
603
                break;
604
        }
605
    }
606
}
607
608
/**
609
 * Class RssTextInputHandler
610
 */
611
class RssTextInputHandler extends XmlTagHandler
612
{
613
    public function RssWebMasterHandler()
614
    {
615
    }
616
617
    /**
618
     * @return string
619
     */
620
    public function getName()
621
    {
622
        return 'textInput';
623
    }
624
625
    /**
626
     * @param $parser
627
     * @param $attributes
628
     */
629
    public function handleBeginElement($parser, &$attributes)
630
    {
631
        $parser->resetTempArr();
632
    }
633
634
    /**
635
     * @param $parser
636
     */
637
    public function handleEndElement($parser)
638
    {
639
        $parser->setChannelData('textinput', $parser->getTempArr());
640
    }
641
}
642
643
/**
644
 * Class RssLastBuildDateHandler
645
 */
646
class RssLastBuildDateHandler extends XmlTagHandler
647
{
648
    /**
649
     * RssLastBuildDateHandler constructor.
650
     */
651
    public function __construct()
652
    {
653
    }
654
655
    /**
656
     * @return string
657
     */
658
    public function getName()
659
    {
660
        return 'lastBuildDate';
661
    }
662
663
    /**
664
     * @param $parser
665
     * @param $data
666
     */
667
    public function handleCharacterData($parser, &$data)
668
    {
669
        switch ($parser->getParentTag()) {
670
            case 'channel':
671
                $parser->setChannelData('lastbuilddate', $data);
672
                break;
673
            default:
674
                break;
675
        }
676
    }
677
}
678
679
/**
680
 * Class RssImageHandler
681
 */
682
class RssImageHandler extends XmlTagHandler
683
{
684
    /**
685
     * RssImageHandler constructor.
686
     */
687
    public function __construct()
688
    {
689
    }
690
691
    /**
692
     * @return string
693
     */
694
    public function getName()
695
    {
696
        return 'image';
697
    }
698
}
699
700
/**
701
 * Class RssUrlHandler
702
 */
703
class RssUrlHandler extends XmlTagHandler
704
{
705
    /**
706
     * RssUrlHandler constructor.
707
     */
708
    public function __construct()
709
    {
710
    }
711
712
    /**
713
     * @return string
714
     */
715
    public function getName()
716
    {
717
        return 'url';
718
    }
719
720
    /**
721
     * @param $parser
722
     * @param $data
723
     */
724
    public function handleCharacterData($parser, &$data)
725
    {
726
        if ($parser->getParentTag() === 'image') {
727
            $parser->setImageData('url', $data);
728
        }
729
    }
730
}
731
732
/**
733
 * Class RssWidthHandler
734
 */
735
class RssWidthHandler extends XmlTagHandler
736
{
737
    /**
738
     * RssWidthHandler constructor.
739
     */
740
    public function __construct()
741
    {
742
    }
743
744
    /**
745
     * @return string
746
     */
747
    public function getName()
748
    {
749
        return 'width';
750
    }
751
752
    /**
753
     * @param $parser
754
     * @param $data
755
     */
756
    public function handleCharacterData($parser, &$data)
757
    {
758
        if ($parser->getParentTag() === 'image') {
759
            $parser->setImageData('width', $data);
760
        }
761
    }
762
}
763
764
/**
765
 * Class RssHeightHandler
766
 */
767
class RssHeightHandler extends XmlTagHandler
768
{
769
    /**
770
     * RssHeightHandler constructor.
771
     */
772
    public function __construct()
773
    {
774
    }
775
776
    /**
777
     * @return string
778
     */
779
    public function getName()
780
    {
781
        return 'height';
782
    }
783
784
    /**
785
     * @param $parser
786
     * @param $data
787
     */
788
    public function handleCharacterData($parser, &$data)
789
    {
790
        if ($parser->getParentTag() === 'image') {
791
            $parser->setImageData('height', $data);
792
        }
793
    }
794
}
795
796
/**
797
 * Class RssItemHandler
798
 */
799
class RssItemHandler extends XmlTagHandler
800
{
801
    /**
802
     * RssItemHandler constructor.
803
     */
804
    public function __construct()
805
    {
806
    }
807
808
    /**
809
     * @return string
810
     */
811
    public function getName()
812
    {
813
        return 'item';
814
    }
815
816
    /**
817
     * @param $parser
818
     * @param $attributes
819
     */
820
    public function handleBeginElement($parser, &$attributes)
821
    {
822
        $parser->resetTempArr();
823
    }
824
825
    /**
826
     * @param $parser
827
     */
828
    public function handleEndElement($parser)
829
    {
830
        $items = $parser->getTempArr();
831
        $parser->setItems($items);
832
    }
833
}
834
835
/**
836
 * Class RssCategoryHandler
837
 */
838
class RssCategoryHandler extends XmlTagHandler
839
{
840
    /**
841
     * RssCategoryHandler constructor.
842
     */
843
    public function __construct()
844
    {
845
    }
846
847
    /**
848
     * @return string
849
     */
850
    public function getName()
851
    {
852
        return 'category';
853
    }
854
855
    /**
856
     * @param $parser
857
     * @param $data
858
     */
859
    public function handleCharacterData($parser, &$data)
860
    {
861
        switch ($parser->getParentTag()) {
862
            case 'channel':
863
                $parser->setChannelData('category', $data);
864
                break;
865
            case 'item':
866
                $parser->setTempArr('category', $data, ', ');
867
                break;
868
            default:
869
                break;
870
        }
871
    }
872
}
873
874
/**
875
 * Class RssCommentsHandler
876
 */
877
class RssCommentsHandler extends XmlTagHandler
878
{
879
    /**
880
     * RssCommentsHandler constructor.
881
     */
882
    public function __construct()
883
    {
884
    }
885
886
    /**
887
     * @return string
888
     */
889
    public function getName()
890
    {
891
        return 'comments';
892
    }
893
894
    /**
895
     * @param $parser
896
     * @param $data
897
     */
898
    public function handleCharacterData($parser, &$data)
899
    {
900
        if ($parser->getParentTag() === 'item') {
901
            $parser->setTempArr('comments', $data);
902
        }
903
    }
904
}
905
906
/**
907
 * Class RssPubDateHandler
908
 */
909
class RssPubDateHandler extends XmlTagHandler
910
{
911
    /**
912
     * RssPubDateHandler constructor.
913
     */
914
    public function __construct()
915
    {
916
    }
917
918
    /**
919
     * @return string
920
     */
921
    public function getName()
922
    {
923
        return 'pubDate';
924
    }
925
926
    /**
927
     * @param $parser
928
     * @param $data
929
     */
930
    public function handleCharacterData($parser, &$data)
931
    {
932
        switch ($parser->getParentTag()) {
933
            case 'channel':
934
                $parser->setChannelData('pubdate', $data);
935
                break;
936
            case 'item':
937
                $parser->setTempArr('pubdate', $data);
938
                break;
939
            default:
940
                break;
941
        }
942
    }
943
}
944
945
/**
946
 * Class RssGuidHandler
947
 */
948
class RssGuidHandler extends XmlTagHandler
949
{
950
    /**
951
     * RssGuidHandler constructor.
952
     */
953
    public function __construct()
954
    {
955
    }
956
957
    /**
958
     * @return string
959
     */
960
    public function getName()
961
    {
962
        return 'guid';
963
    }
964
965
    /**
966
     * @param $parser
967
     * @param $data
968
     */
969
    public function handleCharacterData($parser, &$data)
970
    {
971
        if ($parser->getParentTag() === 'item') {
972
            $parser->setTempArr('guid', $data);
973
        }
974
    }
975
}
976
977
/**
978
 * Class RssAuthorHandler
979
 */
980
class RssAuthorHandler extends XmlTagHandler
981
{
982
    public function RssGuidHandler()
983
    {
984
    }
985
986
    /**
987
     * @return string
988
     */
989
    public function getName()
990
    {
991
        return 'author';
992
    }
993
994
    /**
995
     * @param $parser
996
     * @param $data
997
     */
998
    public function handleCharacterData($parser, &$data)
999
    {
1000
        if ($parser->getParentTag() === 'item') {
1001
            $parser->setTempArr('author', $data);
1002
        }
1003
    }
1004
}
1005
1006
/**
1007
 * Class RssSourceHandler
1008
 */
1009
class RssSourceHandler extends XmlTagHandler
1010
{
1011
    /**
1012
     * RssSourceHandler constructor.
1013
     */
1014
    public function __construct()
1015
    {
1016
    }
1017
1018
    /**
1019
     * @return string
1020
     */
1021
    public function getName()
1022
    {
1023
        return 'source';
1024
    }
1025
1026
    /**
1027
     * @param $parser
1028
     * @param $attributes
1029
     */
1030
    public function handleBeginElement($parser, &$attributes)
1031
    {
1032
        if ($parser->getParentTag() === 'item') {
1033
            $parser->setTempArr('source_url', $attributes['url']);
1034
        }
1035
    }
1036
1037
    /**
1038
     * @param $parser
1039
     * @param $data
1040
     */
1041
    public function handleCharacterData($parser, &$data)
1042
    {
1043
        if ($parser->getParentTag() === 'item') {
1044
            $parser->setTempArr('source', $data);
1045
        }
1046
    }
1047
}
1048