This file is indexed.

/usr/share/perl5/XMLTV.pm is in libxmltv-perl 0.5.63-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
# -*- perl -*-
# $Id: XMLTV.pm.in,v 1.154 2012/06/14 06:59:46 rmeden Exp $
package XMLTV;

use strict;
use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw(read_data parse parsefile parsefiles write_data
		    best_name list_channel_keys list_programme_keys);

# For the time being the version of this library is tied to that of
# the xmltv package as a whole.  This number should be checked by the
# mkdist tool.
#
our $VERSION = '0.5.63';

# Work around changing behaviour of XML::Twig.  On some systems (like
# mine) it always returns UTF-8 data unless KeepEncoding is specified.
# However the encoding() method tells you the encoding of the original
# document, not of the data you receive.  To be sure of what you're
# getting, it is easiest on such a system to not give KeepEncoding and
# just use UTF-8.
#
# But on other systems (seemingly perl 5.8 and above), XML::Twig tries
# to keep the original document's encoding in the strings returned.
# You then have to call encoding() to find out what you're getting.
# To make sure of this behaviour we set KeepEncoding to true on such a
# system.
#
# Setting KeepEncoding true everywhere seems to do no harm, it's a
# pity that we lose conversion to UTF-8 but at least it's the same
# everywhere.  So the library is distributed with this flag on.
#
my $KEEP_ENCODING = 1;

my %warned_unknown_key;
sub warn_unknown_keys( $$ );

=pod

=head1 NAME

XMLTV - Perl extension to read and write TV listings in XMLTV format

=head1 SYNOPSIS

  use XMLTV;
  my $data = XMLTV::parsefile('tv.xml');
  my ($encoding, $credits, $ch, $progs) = @$data;
  my $langs = [ 'en', 'fr' ];
  print 'source of listings is: ', $credits->{'source-info-name'}, "\n"
      if defined $credits->{'source-info-name'};
  foreach (values %$ch) {
      my ($text, $lang) = @{XMLTV::best_name($langs, $_->{'display-name'})};
      print "channel $_->{id} has name $text\n";
      print "...in language $lang\n" if defined $lang;
  }
  foreach (@$progs) {
      print "programme on channel $_->{channel} at time $_->{start}\n";
      next if not defined $_->{desc};
      foreach (@{$_->{desc}}) {
          my ($text, $lang) = @$_;
          print "has description $text\n";
          print "...in language $lang\n" if defined $lang;
      }
  }

The value of $data will be something a bit like:

  [ 'UTF-8',
    { 'source-info-name' => 'Ananova', 'generator-info-name' => 'XMLTV' },
    { 'radio-4.bbc.co.uk' => { 'display-name' => [ [ 'en',  'BBC Radio 4' ],
						   [ 'en',  'Radio 4'     ],
						   [ undef, '4'           ] ],
			       'id' => 'radio-4.bbc.co.uk' },
      ... },
    [ { start => '200111121800', title => [ [ 'Simpsons', 'en' ] ],
        channel => 'radio-4.bbc.co.uk' },
      ... ] ]

=head1 DESCRIPTION

This module provides an interface to read and write files in XMLTV
format (a TV listings format defined by xmltv.dtd).  In general element
names in the XML correspond to hash keys in the Perl data structure.
You can think of this module as a bit like B<XML::Simple>, but
specialized to the XMLTV file format.

The Perl data structure corresponding to an XMLTV file has four
elements.  The first gives the character encoding used for text data,
typically UTF-8 or ISO-8859-1.  (The encoding value could also be
undef meaning 'unknown', when the library canE<39>t work out what it
is.)  The second element gives the attributes of the root <tv>
element, which give information about the source of the TV listings.
The third element is a list of channels, each list element being a
hash corresponding to one <channel> element.  The fourth element is
similarly a list of programmes.  More details about the data structure
are given later.  The easiest way to find out what it looks like is to
load some small XMLTV files and use B<Data::Dumper> to print out the
resulting structure.

=head1 USAGE

=cut

use XML::Twig;
use XML::Writer 0.600;
use Date::Manip;
use Carp;
use Data::Dumper;

# Use Lingua::Preferred if available, else kludge a replacement.
sub my_which_lang { return $_[1]->[0] }
BEGIN {
    eval { require Lingua::Preferred };
    *which_lang = $@ ? \&my_which_lang : \&Lingua::Preferred::which_lang;
}

# Use Log::TraceMessages if installed.
BEGIN {
    eval { require Log::TraceMessages };
    if ($@) {
	*t = sub {};
	*d = sub { '' };
    }
    else {
	*t = \&Log::TraceMessages::t;
	*d = \&Log::TraceMessages::d;
    }
}

# Attributes and subelements of channel.  Each subelement additionally
# needs a handler defined.  Multiplicity is given for both, but for
# attributes the only allowable values are '1' and '?'.
#
# Ordering of attributes is not really important, but we keep the same
# order as they are given in the DTD so that output looks nice.
#
# The ordering of the subelements list gives the order in which these
# elements must appear in the DTD.  In fact, these lists just
# duplicate information in the DTD and add details of what handlers
# to call.
#
our @Channel_Attributes = ([ 'id', '1' ]);
our @Channel_Handlers =
  (
   [ 'display-name', 'with-lang', '+' ],
   [ 'icon',         'icon',      '*' ],
   [ 'url',          'scalar',    '*' ],
  );

# Same for <programme> elements.
our @Programme_Attributes =
  (
   [ 'start',     '1' ],
   [ 'stop',      '?' ],
   [ 'pdc-start', '?' ],
   [ 'vps-start', '?' ],
   [ 'showview',  '?' ],
   [ 'videoplus', '?' ],
   [ 'channel',   '1' ],
   [ 'clumpidx',  '?' ],
  );
our @Programme_Handlers =
  (
   [ 'title',            'with-lang',          '+' ],
   [ 'sub-title',        'with-lang',          '*' ],
   [ 'desc',             'with-lang/m',        '*' ],
   [ 'credits',          'credits',            '?' ],
   [ 'date',             'scalar',             '?' ],
   [ 'category',         'with-lang',          '*' ],
   [ 'language',         'with-lang',          '?' ],
   [ 'orig-language',    'with-lang',          '?' ],
   [ 'length',           'length',             '?' ],
   [ 'icon',             'icon',               '*' ],
   [ 'url',              'scalar',             '*' ],
   [ 'country',          'with-lang',          '*' ],
   [ 'episode-num',      'episode-num',        '*' ],
   [ 'video',            'video',              '?' ],
   [ 'audio',            'audio',              '?' ],
   [ 'previously-shown', 'previously-shown',   '?' ],
   [ 'premiere',         'with-lang/em',       '?' ],
   [ 'last-chance',      'with-lang/em',       '?' ],
   [ 'new',              'presence',           '?' ],
   [ 'subtitles',        'subtitles',          '*' ],
   [ 'rating',           'rating',             '*' ],
   [ 'star-rating',      'star-rating',        '*' ],
  );

# And a hash mapping names like 'with-lang' to pairs of subs.  The
# first for reading, the second for writing.  Note that the writers
# alter the passed-in data as a side effect!  (If the writing sub is
# called with an undef XML::Writer then it writes nothing but still
# warns for (most) bad data checks - and still alters the data.)
#
our %Handlers = ();

# Undocumented interface for adding extensions to the XMLTV format:
# first add an entry to @XMLTV::Channel_Handlers or
# @XMLTV::Programme_Handlers with your new element's name, 'type' and
# multiplicity.  The 'type' should be a string you invent yourself.
# Then $XMLTV::Handlers{'type'} should be a pair of subroutines, a
# reader and a writer.  (Unless you want to use one of the existing
# types such as 'with-lang' or 'scalar'.)
#
# Note that elements and attributes beginning 'x-' are skipped over
# _automatically_, so you can't parse them with this method.  A better
# way to add extensions is needed - doing this not encouraged but is
# sometimes necessary.
#

# read_data() is a deprecated name for parsefile().
sub read_data( $ ) { # FIXME remove altogether
    warn "XMLTV::read_data() deprecated, use XMLTV::parsefile() instead\n";
    &parsefile;
}

# Private.
sub sanity( $ ) {
    for (shift) {
    	croak 'no <tv> element found' if not /<tv/;
    }
}

=over

=item parse(document)

Takes an XMLTV document (a string) and returns the Perl data
structure.  It is assumed that the document is valid XMLTV; if not
the routine may die() with an error (although the current implementation
just warns and continues for most small errors).

The first element of the listref returned, the encoding, may vary
according to the encoding of the input document, the versions of perl
and C<XML::Parser> installed, the configuration of the XMLTV library
and other factors including, but not limited to, the phase of the
moon.  With luck it should always be either the encoding of the input
file or UTF-8.

Attributes and elements in the XML file whose names begin with 'x-'
are skipped silently.  You can use these to include information which
is not currently handled by the XMLTV format, or by this module.

=cut

sub parse( $ ) {
    my $str = shift;
    sanity($str);
    # FIXME commonize with parsefiles()
    my ($encoding, $credits);
    my %channels;
    my @programmes;
    parse_callback($str,
		   sub { $encoding = shift },
		   sub { $credits = shift },
		   sub { for (shift) { $channels{$_->{id}} = $_ } },
		   sub { push @programmes, shift });
    return [ $encoding, $credits, \%channels, \@programmes ];
}

=pod

=item parsefiles(filename...)

Like C<parse()> but takes one or more filenames instead of a string
document.  The data returned is the merging of those file contents:
the programmes will be concatenated in their original order, the
channels just put together in arbitrary order (ordering of channels
should not matter).

It is necessary that each file have the same character encoding, if
not, an exception is thrown.  Ideally the credits information would
also be the same between all the files, since there is no obvious way to
merge it - but if the credits information differs from one file to the
next, one file is picked arbitrarily to provide credits and a warning
is printed.  If two files give differing channel definitions for the
same XMLTV channel id, then one is picked arbitrarily and a warning
is printed.

In the simple case, with just one file, you neednE<39>t worry
about mismatching of encodings, credits or channels.

The deprecated function C<parsefile()> is a wrapper allowing just one
filename.

=cut

sub parsefiles( @ ) {
    die 'one or more filenames required' if not @_;
    my ($encoding, $credits);
    my %channels;
    my @programmes;
    parsefiles_callback(sub { $encoding = shift },
			sub { $credits = shift },
			sub { for (shift) { $channels{$_->{id}} = $_ } },
			sub { push @programmes, shift },
			@_);
    return [ $encoding, $credits, \%channels, \@programmes ];
}

sub parsefile( $ ) { parsefiles(@_) }

=pod

=item parse_callback(document, encoding_callback, credits_callback,
                     channel_callback, programme_callback)

An alternative interface.  Whereas C<parse()> reads the whole document
and then returns a finished data structure, with this routine you
specify a subroutine to be called as each <channel> element is read
and another for each <programme> element.

The first argument is the document to parse.  The remaining arguments
are code references, one for each part of the document.

The callback for encoding will be called once with a string giving the
encoding.  In present releases of this module, it is also possible for
the value to be undefined meaning 'unknown', but itE<39>s hoped that
future releases will always be able to figure out the encoding used.

The callback for credits will be called once with a hash reference.
For channels and programmes, the appropriate function will be called
zero or more times depending on how many channels / programmes are
found in the file.

The four subroutines will be called in order, that is, the encoding
and credits will be done before the channel handler is called and all
the channels will be dealt with before the first programme handler is
called.

If any of the code references is undef, nothing is called for that part
of the file.

For backwards compatibility, if the value for 'encoding callback' is
not a code reference but a scalar reference, then the encoding found
will be stored in that scalar.  Similarly if the 'credits callback'
is a scalar reference, the scalar it points to will be set to point
to the hash of credits.  This style of interface is deprecated: new
code should just use four callbacks.

For example:

    my $document = '<tv>...</tv>';

    my $encoding;
    sub encoding_cb( $ ) { $encoding = shift }

    my $credits;
    sub credits_cb( $ ) { $credits = shift }

    # The callback for each channel populates this hash.
    my %channels;
    sub channel_cb( $ ) {
	my $c = shift;
	$channels{$c->{id}} = $c;
    }

    # The callback for each programme.  We know that channels are
    # always read before programmes, so the %channels hash will be
    # fully populated.
    #
    sub programme_cb( $ ) {
        my $p = shift;
        print "got programme: $p->{title}->[0]->[0]\n";
        my $c = $channels{$p->{channel}};
        print 'channel name is: ', $c->{'display-name'}->[0]->[0], "\n";
    }

    # Let's go.
    XMLTV::parse_callback($document, \&encoding_cb, \&credits_cb,
                          \&channel_cb, \&programme_cb);

=cut

# Private.
sub new_doc_callback( $$$$ ) {
    my ($enc_cb, $cred_cb, $ch_cb, $p_cb) = @_;
    t 'creating new XML::Twig';
    t '\@Channel_Handlers=' . d \@Channel_Handlers;
    t '\@Programme_Handlers=' . d \@Programme_Handlers;
    new XML::Twig(StartTagHandlers =>
		  { '/tv' => sub {
			my ($t, $node) = @_;
			my $enc;
			if ($KEEP_ENCODING) {
			    t 'KeepEncoding on, get original encoding';
			    $enc = $t->encoding();
			}
			else {
			    t 'assuming UTF-8 encoding';
			    $enc = 'UTF-8';
			}

			if (defined $enc_cb) {
			    for (ref $enc_cb) {
				if ($_ eq 'CODE') {
				    $enc_cb->($enc);
				}
				elsif ($_ eq 'SCALAR') {
				    $$enc_cb = $enc;
				}
				else {
				    die "callback should be code ref or scalar ref, or undef";
				}
			    }
			}

			if (defined $cred_cb) {
			    my $cred = get_attrs($node);
			    for (ref $cred_cb) {
				if ($_ eq 'CODE') {
				    $cred_cb->($cred);
				}
				elsif ($_ eq 'SCALAR') {
				    $$cred_cb = $cred;
				}
				else {
				    die "callback should be code ref or scalar ref, or undef";
				}
			    }
			}
			# Most of the above code can be removed in the
			# next release.
			#
		    },
		  },

		  TwigHandlers =>
		  { '/tv/channel'   => sub {
			my ($t, $node) = @_;
			die if not defined $node;
			my $c = node_to_channel($node);
			$t->purge();
			if (not $c) {
			    warn "skipping bad channel element\n";
			}
			else {
			    $ch_cb->($c);
			}
		    },
		
		    '/tv/programme' => sub {
			my ($t, $node) = @_;
			die if not defined $node;
			my $p = node_to_programme($node);
			$t->purge();
			if (not $p) {
			    warn "skipping bad programme element\n";
			}
			else {
			    $p_cb->($p);
			}
		    },
		  },

		  KeepEncoding => $KEEP_ENCODING,
		 );
}

sub parse_callback( $$$$$ ) {
    my ($str, $enc_cb, $cred_cb, $ch_cb, $p_cb) = @_;
    sanity($str);
    new_doc_callback($enc_cb, $cred_cb, $ch_cb, $p_cb)->parse($str);
}

=pod

=item parsefiles_callback(encoding_callback, credits_callback,
                          channel_callback, programme_callback,
                          filenames...)

As C<parse_callback()> but takes one or more filenames to open,
merging their contents in the same manner as C<parsefiles()>.  Note
that the reading is still gradual - you get the channels and
programmes one at a time, as they are read.

Note that the same <channel> may be present in more than one file, so
the channel callback will get called more than once.  ItE<39>s your
responsibility to weed out duplicate channel elements (since writing
them out again requires that each have a unique id).

For compatibility, there is an alias C<parsefile_callback()> which is
the same but takes only a single filename, B<before> the callback
arguments.  This is deprecated.

=cut

sub parsefile_callback( $$$$$ ) {
    my ($f, $enc_cb, $cred_cb, $ch_cb, $p_cb) = @_;
    parsefiles_callback($enc_cb, $cred_cb, $ch_cb, $p_cb, $f);
}

sub parsefiles_callback( $$$$@ ) {
    my ($enc_cb, $cred_cb, $ch_cb, $p_cb, @files) = @_;
    die "one or more files required" if not @files;
    my $all_encoding; my $have_encoding = 0;
    my $all_credits;
    my %all_channels;

    my $do_next_file; # to be defined below
    my $my_enc_cb = sub( $ ) {
	my $e = shift;
	t 'encoding callback';
	if ($have_encoding) {
	    t 'seen encoding before, just check';
	    my ($da, $de) = (defined $all_encoding, defined $e);
	    if (not $da and not $de) {
		warn "two files both have unspecified character encodings, hope they're the same\n";
	    }
	    elsif (not $da and $de) {
		warn "encoding $e not being returned to caller\n";
		$all_encoding = $e;
	    }
	    elsif ($da and not $de) {
		warn "input file with unspecified encoding, assuming same as others ($all_encoding)\n";
	    }
	    elsif ($da and $de) {
		if (uc($all_encoding) ne uc($e)) {
		    die "this file's encoding $e differs from others' $all_encoding - aborting\n";
		}
	    }
	    else { die }
	}
	else {
	    t 'not seen encoding before, call user';
	    $enc_cb->($e) if $enc_cb;
	    $all_encoding = $e;
	    $have_encoding = 1;
	}
    };

    my $my_cred_cb = sub( $ ) {
	my $c = shift;
	if (defined $all_credits) {
	    if (Dumper($all_credits) ne Dumper($c)) {
		warn "different files have different credits, picking one arbitrarily\n";
		# In fact, we pick the last file in the list since this is the
		# first to be opened.
		#
	    }
	}
	else {
	    $cred_cb->($c) if $cred_cb;
	    $all_credits = $c;
	}
    };

    my $my_ch_cb = sub( $ ) {
	my $c = shift;
	my $id = $c->{id};
	if (defined $all_channels{$id} and Dumper($all_channels{$id}) ne Dumper($c)) {
	    warn "differing channels with id $id, picking one arbitrarily\n";
	}
	else {
	    $all_channels{$id} = $c;
	    $ch_cb->($c) if $ch_cb;
	}
    };

    my $my_p_cb = sub( $ ) {
	$do_next_file->(); # if any
	$p_cb->(@_) if $p_cb;
    };

    $do_next_file = sub() {
	while (@files) {
	    # Last first.
	    my $f = pop @files;

	    # In older versions of perl there were segmentation faults
	    # when calling die() inside the parsing callbacks, so we
	    # needed to override $SIG{__DIE__} here.  Assume that
	    # newer perls don't have this issue.
	    #

	    my $t = new_doc_callback($my_enc_cb, $my_cred_cb, $my_ch_cb, $my_p_cb);
	    $t->parsefile($f);
	}
    };

    # Let's go.
    $do_next_file->();
}

=pod

=item write_data(data, options...)

Takes a data structure and writes it as XML to standard output.  Any
extra arguments are passed on to XML::WriterE<39>s constructor, for example

    my $f = new IO::File '>out.xml'; die if not $f;
    write_data($data, OUTPUT => $f);

The encoding used for the output is given by the first element of the
data.

Normally, there will be a warning for any Perl data which is not
understood and cannot be written as XMLTV, such as strange keys in
hashes.  But as an exception, any hash key beginning with an
underscore will be skipped over silently.  You can store 'internal use
only' data this way.

If a programme or channel hash contains a key beginning with 'debug',
this key and its value will be written out as a comment inside the
<programme> or <channel> element.  This lets you include small
debugging messages in the XML output.

=cut

sub write_data( $;@ ) {
    my $data = shift;
    my $writer = new XMLTV::Writer(encoding => $data->[0], @_);
    $writer->start($data->[1]);
    $writer->write_channels($data->[2]);
    $writer->write_programme($_) foreach @{$data->[3]};
    $writer->end();
}


# Private.
#
# get_attrs()
#
# Given a node, return a hashref of its attributes.  Skips over
# the 'x-whatever' attributes.
#
sub get_attrs( $ ) {
    my $node = shift; die if not defined $node;
    my %r = %{$node->atts()};
    foreach (keys %r) {
	if (/^x-/) {
	    delete $r{$_};
	}
	else {
	    tidy(\$r{$_});
	}
    }
    return \%r;
}


# Private.
#
# get_text()
#
# Given a node containing only text, return that text (with whitespace
# either side stripped).  If the node has no children (as in
# <foo></foo> or <foo />), this is considered to be the empty string.
#
# Parameter: whether newlines are allowed (defaults to false)
#
sub get_text( $;$ ) {
    my $node = shift;
    my $allow_nl = shift; $allow_nl = 0 if not defined $allow_nl;
    my @children = get_subelements($node);
    if (@children == 0) {
	return '';
    }
    elsif (@children == 1) {
	my $v = $children[0]->pcdata();
	t 'got pcdata: ' . d $v;
	if (not defined $v) {
	    my $name = get_name($node);
	    warn "node $name expected to contain text has other stuff\n";
	}
	else {
	    # Just hope that the encoding we got uses \n...
	    if (not $allow_nl and $v =~ tr/\n//d) {
		my $name = get_name($node);
		warn "removing newlines from content of node $name\n";
	    }
	    tidy(\$v);
	}
	t 'returning: ' . d $v;
	return $v;
    }
    elsif (@children > 1) {
	my $name = get_name($node);
	warn "node $name expected to contain text has more than one child\n";
	return undef;
    }
    else { die }
}

# Private.  Clean up parsed text.  Takes ref to scalar.
sub tidy( $ ) {
    our $v; local *v = shift; die if not defined $v;
    if ($XML::Twig::VERSION < 3.01 || $KEEP_ENCODING) {
	# Old versions of XML::Twig had stupid behaviour with
	# entities - and so do the new ones if KeepEncoding is on.
	#
	for ($v) {
	    s/&gt;/>/g;
	    s/&lt;/</g;
	    s/&apos;/\'/g;
	    s/&quot;/\"/g;
	    s/&amp;/&/g;	# needs to be last
	}
    }
    else {
	t 'new XML::Twig, not KeepEncoding, entities already dealt with';
    }

    for ($v) {
	s/^\s+//;
	s/\s+$//;

	# On Windows there seems to be an inconsistency between
	# XML::Twig and XML::Writer.  The former returns text with
	# \r\n line endings to the application, but the latter adds \r
	# characters to text outputted.  So reading some text and
	# writing it again accumulates an extra \r character.  We fix
	# this by removing \r from the input here.
	#
	tr/\r//d;
    }
}

# Private.
#
# get_subelements()
#
# Return a list of all subelements of a node.  Whitespace is
# ignored; anything else that isn't a subelement is warned about.
# Skips over elements with name 'x-whatever'.
#
sub get_subelements( $ ) {
    grep { (my $tmp = get_name($_)) !~ /^x-/ } $_[0]->children();
}

# Private.
#
# get_name()
#
# Return the element name of a node.
#
sub get_name( $ ) { $_[0]->gi() }
	
# Private.
#
# dump_node()
#
# Return some information about a node for debugging.
#
sub dump_node( $ ) {
    my $n = shift;
    # Doesn't seem to be easy way to get 'type' of node.
    my $r = 'name: ' . get_name($n) . "\n";
    for (trunc($n->text())) {
	$r .= "value: $_\n" if defined and length;
    }
    return $r;
}
# Private.  Truncate a string to a reasonable length and add '...' if
# necessary.
#
sub trunc {
    local $_ = shift;
    return undef if not defined;
    if (length > 1000) {
	return substr($_, 0, 1000) . '...';
    }
    return $_;
}

=pod

=item best_name(languages, pairs [, comparator])

The XMLTV format contains many places where human-readable text is
given an optional 'lang' attribute, to allow mixed languages.  This is
represented in Perl as a pair [ text, lang ], although the second
element may be missing or undef if the language is unknown.  When
several alernatives for an element (such as <title>) can be given, the
representation is a list of [ text, lang ] pairs.  Given such a list,
what is the best text to use?  It depends on the userE<39>s preferred
language.

This function takes a list of acceptable languages and a list of [string,
language] pairs, and finds the best one to use.  This means first finding
the appropriate language and then picking the 'best' string in that
language.

The best is normally defined as the first one found in a usable
language, since the XMLTV format puts the most canonical versions
first.  But you can pass in your own comparison function, for example
if you want to choose the shortest piece of text that is in an
acceptable language.

The acceptable languages should be a reference to a list of language
codes looking like 'ru', or like 'de_DE'.  The text pairs should be a
reference to a list of pairs [ string, language ].  (As a special case
if this list is empty or undef, that means no text is present, and the
result is undef.)  The third argument if present should be a cmp-style
function that compares two strings of text and returns 1 if the first
argument is better, -1 if the second better, 0 if theyE<39>re equally
good.

Returns: [s, l] pair, where s is the best of the strings to use and l
is its language.  This pair is 'live' - it is one of those from the
list passed in.  So you can use C<best_name()> to find the best pair
from a list and then modify the content of that pair.

(This routine depends on the C<Lingua::Preferred> module being
installed; if that module is missing then the first available
language is always chosen.)

Example:

    my $langs = [ 'de', 'fr' ]; # German or French, please

    # Say we found the following under $p->{title} for a programme $p.
    my $pairs = [ [ 'La CitE des enfants perdus', 'fr' ],
                  [ 'The City of Lost Children', 'en_US' ] ];

    my $best = best_name($langs, $pairs);
    print "chose title $best->[0]\n";

=cut

sub best_name( $$;$ ) {
    my ($wanted_langs, $pairs, $compare) = @_;
    t 'best_name() ENTRY';
    t 'wanted langs: ' . d $wanted_langs;
    t '[text,lang] pairs: ' . d $pairs;
    t 'comparison fn: ' . d $compare;
    return undef if not defined $pairs;
    my @pairs = @$pairs;

    my @avail_langs;
    my (%seen_lang, $seen_undef);
    # Collect the list of available languages.
    foreach (map { $_->[1] } @pairs) {
	if (defined) {
	    next if $seen_lang{$_}++;
	}
	else {
	    next if $seen_undef++;
	}
	push @avail_langs, $_;
    }

    my $pref_lang = which_lang($wanted_langs, \@avail_langs);

    # Gather up [text, lang] pairs which have the desired language.
    my @candidates;
    foreach (@pairs) {
	my ($text, $lang) = @$_;
	next unless ((not defined $lang)
		     or (defined $pref_lang and $lang eq $pref_lang));
	push @candidates, $_;
    }

    return undef if not @candidates;

    # If a comparison function was passed in, use it to compare the
    # text strings from the candidate pairs.
    #
    @candidates = sort { $compare->($a->[0], $b->[0]) } @candidates
      if defined $compare;

    # Pick the first candidate.  This will be the one ordered first by
    # the comparison function if given, otherwise the earliest in the
    # original list.
    #
    return $candidates[0];
}


=item list_channel_keys(), list_programme_keys()

Some users of this module may wish to enquire at runtime about which
keys a programme or channel hash can contain.  The data in the hash
comes from the attributes and subelements of the corresponding element
in the XML.  The values of attributes are simply stored as strings,
while subelements are processed with a handler which may return a
complex data structure.  These subroutines returns a hash mapping key
to handler name and multiplicity.  This lets you know what data types
can be expected under each key.  For keys which come from attributes
rather than subelements, the handler is set to 'scalar', just as for
subelements which give a simple string.  See L<"DATA STRUCTURE"> for
details on what the different handler names mean.

It is not possible to find out which keys are mandatory and which
optional, only a list of all those which might possibly be present.
An example use of these routines is the L<tv_grep> program, which
creates its allowed command line arguments from the names of programme
subelements.

=cut

# Private.
sub list_keys( $$ ) {
    my %r;

    # Attributes.
    foreach (@{shift()}) {
	my ($k, $mult) = @$_;
	$r{$k} = [ 'scalar', $mult ];
    }

    # Subelements.
    foreach (@{shift()}) {
	my ($k, $h_name, $mult) = @$_;
	$r{$k} = [ $h_name, $mult ];
    }

    return \%r;
}
# Public.
sub list_channel_keys() {
    list_keys(\@Channel_Attributes, \@Channel_Handlers);
}
sub list_programme_keys() {
    list_keys(\@Programme_Attributes, \@Programme_Handlers);
}

=pod

=item catfiles(w_args, filename...)

Concatenate several listings files, writing the output to somewhere
specified by C<w_args>.  Programmes are catenated together, channels
are merged, for credits we just take the first and warn if the others
differ.

The first argument is a hash reference giving information to pass to
C<XMLTV::Writer>E<39>s constructor.  But do not specify encoding, this
will be taken from the input files.  Currently C<catfiles()> will fail
work if the input files have different encodings.

=cut

sub catfiles( $@ ) {
    my $w_args = shift;
    my $w;
    my %seen_ch;
    XMLTV::parsefiles_callback
      (sub {
	   die if defined $w;
	   $w = new XMLTV::Writer(%$w_args, encoding => shift);
       },
       sub { $w->start(shift) },
       sub {
	   my $c = shift;
	   my $id = $c->{id};
	   if (not defined $seen_ch{$id}) {
	       $w->write_channel($c);
	       $seen_ch{$id} = $c;
	   }
	   elsif (Dumper($seen_ch{$id}) eq Dumper($c)) {
	       # They're identical, okay.
	   }
	   else {
	       warn "channel $id may differ between two files, "
		 . "picking one arbitrarily\n";
	   }
       },
       sub { $w->write_programme(shift) },
       @_);
    $w->end();
}

=pod

=item cat(data, ...)

Concatenate (and merge) listings data.  Programmes are catenated
together, channels are merged, for credits we just take the first and
warn if the others differ (except that the 'date' of the result is the
latest date of all the inputs).

Whereas C<catfiles()> reads and writes files, this function takes
already-parsed listings data and returns some more listings data.  It
is much more memory-hungry.

=cut

sub cat( @ ) { cat_aux(1, @_) }

=pod

=item cat_noprogrammes

Like C<cat()> but ignores the programme data and just returns
encoding, credits and channels.  This is in case for scalability
reasons you want to handle programmes individually, but still
merge the smaller data.

=cut

sub cat_noprogrammes( @ ) { cat_aux(0, @_) }

sub cat_aux( @ ) {
    my $all_encoding;
    my ($all_credits_nodate, $all_credits_date);
    my %all_channels;
    my @all_progs;
    my $do_progs = shift;

    foreach (@_) {
	t 'doing arg: ' . d $_;
	my ($encoding, $credits, $channels, $progs) = @$_;

	if (not defined $all_encoding) {
	    $all_encoding = $encoding;
	}
	elsif ($encoding ne $all_encoding) {
	    die "different files have different encodings, cannot continue\n";
	}

	# If the credits are different between files there's not a lot
	# we can do to merge them.  Apart from 'date', that is.  There
	# we can say that the date of the concatenated listings is the
	# newest date from all the sources.
	#
 	my %credits_nodate = %$credits; # copy
 	my $d = delete $credits_nodate{date};
 	if (defined $d) {
	    # Need to 'require' rather than 'use' this because
	    # XMLTV.pm is loaded during the build process and
	    # XMLTV::Date isn't available then.  Urgh.
	    #
	    require XMLTV::Date;
 	    my $dp = XMLTV::Date::parse_date($d);
 	    for ($all_credits_date) {
 		if (not defined
		    or Date_Cmp(XMLTV::Date::parse_date($_), $dp) < 0) {
 		    $_ = $d;
 		}
 	    }
 	}
	
 	# Now in uniqueness checks ignore the date.
	if (not defined $all_credits_nodate) {
	    $all_credits_nodate = \%credits_nodate;
	}
	elsif (Dumper(\%credits_nodate) ne Dumper($all_credits_nodate)) {
	    warn "different files have different credits, taking from first file\n";
	}

	foreach (keys %$channels) {
	    if (not defined $all_channels{$_}) {
		$all_channels{$_} = $channels->{$_};
	    }
	    elsif (Dumper($all_channels{$_}) ne Dumper($channels->{$_})) {
		warn "channel $_ differs between two files, taking first appearance\n";
	    }
	}

	push @all_progs, @$progs if $do_progs;
    }

    $all_encoding = 'UTF-8' if not defined $all_encoding;

    my %all_credits;
    %all_credits = %$all_credits_nodate
      if defined $all_credits_nodate;
    $all_credits{date} = $all_credits_date
      if defined $all_credits_date;

    if ($do_progs) {
	return [ $all_encoding, \%all_credits, \%all_channels, \@all_progs ];
    }
    else {
	return [ $all_encoding, \%all_credits, \%all_channels ];
    }
}


# For each subelement of programme, we define a subroutine to read it
# and one to write it.  The reader takes an node for a single
# subelement and returns its value as a Perl scalar (warning and
# returning undef if error).  The writer takes an XML::Writer, an
# element name and a scalar value and writes a subelement for that
# value.  Note that the element name is passed in to the writer just
# for symmetry, so that neither the writer or the reader have to know
# what their element is called.
#
=pod

=back

=head1 DATA STRUCTURE

For completeness, we describe more precisely how channels and
programmes are represented in Perl.  Each element of the channels list
is a hashref corresponding to one <channel> element, and likewise for
programmes.  The possible keys of a channel (programme) hash are the
names of attributes or subelements of <channel> (<programme>).

The values for attributes are not processed in any way; an attribute
C<fred="jim"> in the XML will become a hash element with key C<'fred'>,
value C<'jim'>.

But for subelements, there is further processing needed to turn the
XML content of a subelement into Perl data.  What is done depends on
what type of data is stored under that subelement.  Also, if a certain
element can appear several times then the hash key for that element
points to a list of values rather than just one.

The conversion of a subelementE<39>s content to and from Perl data is
done by a handler.  The most common handler is I<with-lang>, used for
human-readable text content plus an optional 'lang' attribute.  There
are other handlers for other data structures in the file format.
Often two subelements will share the same handler, since they hold the
same type of data.  The handlers defined are as follows; note that
many of them will silently strip leading and trailing whitespace in
element content.  Look at the DTD itself for an explanation of the
whole file format.

Unless specified otherwise, it is not allowed for an element expected
to contain text to have empty content, nor for the text to contain
newline characters.

=over

=item I<credits>

Turns a list of credits (for director, actor, writer, etc.) into a
hash mapping 'role' to a list of names.  The names in each role are
kept in the same order.

=cut

$Handlers{credits}->[0] = sub( $ ) {
    my $node = shift;
    my @roles = qw(director actor writer adapter producer composer
                   editor presenter commentator guest);
    my %known_role; ++$known_role{$_} foreach @roles;
    my %r;
    foreach (get_subelements($node)) {
	my $role = get_name($_);
	unless ($known_role{$role}++) {
	    warn "unknown thing in credits: $role";
	    next;
	}
	push @{$r{$role}}, get_text($_);
    }
    return \%r;
};
$Handlers{credits}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_; die if not defined $v;
    my %h = %$v;
    return if not %h; # don't write empty element
    t 'writing credits: ' . d \%h;
    # TODO some 'do nothing' setting in XML::Writer to replace this
    # convention of passing undef.
    #
    $w->startTag($e) if $w;
    foreach ( qw[director actor writer adapter producer composer 
                 editor presenter commentator guest] ) {
	next unless defined $h{$_};
	my @people = @{delete $h{$_}};
	foreach my $person (@people) {
	    die if not defined $person;
	    $w->dataElement($_, $person) if $w;
	}
    }
    warn_unknown_keys($e, \%h);
    $w->endTag($e) if $w;
};

=pod

=item I<scalar>

Reads and writes a simple string as the content of the XML element.

=cut

$Handlers{scalar}->[0] = sub( $ ) {
    my $node = shift;
    return get_text($node);
};
$Handlers{scalar}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    t 'scalar';
    $w->dataElement($e, $v) if $w;
};

=pod

=item I<length>

Converts the content of a <length> element into a number of seconds
(so <length units="minutes">5</minutes> would be returned as 300).  On
writing out again tries to convert a number of seconds to a time in
minutes or hours if that would look better.

=cut

$Handlers{length}->[0] = sub( $ ) {
    my $node = shift; die if not defined $node;
    my %attrs = %{get_attrs($node)};
    my $d = get_text($node);
    if ($d =~ /^\s*$/) {
	warn "empty 'length' element";
	return undef;
    }
    if ($d !~ tr/0-9// or $d =~ tr/0-9//c) {
	warn "bad content of 'length' element: $d";
	return undef;
    }
    my $units = $attrs{units};
    if (not defined $units) {
	warn "missing 'units' attr in 'length' element";
	return undef;
    }
    # We want to return a length in seconds.
    if ($units eq 'seconds') {
	# Okay.
    }
    elsif ($units eq 'minutes') {
	$d *= 60;
    }
    elsif ($units eq 'hours') {
	$d *= 60 * 60;
    }
    else {
	warn "bad value of 'units': $units";
	return undef;
    }
    return $d;
};
$Handlers{length}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    t 'length';
    my $units;
    if ($v % 3600 == 0) {
	$units = 'hours';
	$v /= 3600;
    }
    elsif ($v % 60 == 0) {
	$units = 'minutes';
	$v /= 60;
    }
    else {
	$units = 'seconds';
    }
    $w->dataElement($e, $v, units => $units) if $w;
};

=pod

=item I<episode-num>

The representation in Perl of XMLTVE<39>s odd episode numbers is as a
pair of [ content, system ].  As specified by the DTD, if the system is
not given in the file then 'onscreen' is assumed.  Whitespace in the
'xmltv_ns' system is unimportant, so on reading it is normalized to
a single space on either side of each dot.

=cut

$Handlers{'episode-num'}->[0] = sub( $ ) {
    my $node = shift; die if not defined $node;
    my %attrs = %{get_attrs($node)};
    my $system = $attrs{system};
    $system = 'onscreen' if not defined $system;
    my $content = get_text($node);
    if ($system eq 'xmltv_ns') {
	# Make it look nice.
	$content =~ s/\s+//g;
	$content =~ s/\./ . /g;
    }
    return [ $content, $system ];
};
$Handlers{'episode-num'}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    t 'episode number';
    if (not ref $v or ref $v ne 'ARRAY') {
	warn "not writing episode-num whose content is not an array";
	return;
    }
    my ($content, $system) = @$v;
    $system = 'onscreen' if not defined $system;
    $w->dataElement($e, $content, system => $system) if $w;
};

=pod

=item I<video>

The <video> section is converted to a hash.  The <present> subelement
corresponds to the key 'present' of this hash, 'yes' and 'no' are
converted to Booleans.  The same applies to <colour>.  The content of
the <aspect> subelement is stored under the key 'aspect'.  These keys
can be missing in the hash just as the subelements can be missing in
the XML.

=cut

$Handlers{video}->[0] = sub ( $ ) {
    my $node = shift;
    my %r;
    foreach (get_subelements($node)) {
	my $name = get_name($_);
	my $value = get_text($_);
	if ($name eq 'present') {
	    warn "'present' seen twice" if defined $r{present};
	    $r{present} = decode_boolean($value);
	}
	elsif ($name eq 'colour') {
	    warn "'colour' seen twice" if defined $r{colour};
	    $r{colour} = decode_boolean($value);
	}
	elsif ($name eq 'aspect') {
	    warn "'aspect' seen twice" if defined $r{aspect};
	    $value =~ /^\d+:\d+$/ or warn "bad aspect ratio: $value";
	    $r{aspect} = $value;
	}
	elsif ($name eq 'quality') {
	    warn "'quality' seen twice" if defined $r{quality};
	    $r{quality} = $value;
	}
    }
    return \%r;
};
$Handlers{video}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    t "'video' element";
    my %h = %$v;
    return if not %h; # don't write empty element
    $w->startTag($e) if $w;
    if (defined (my $val = delete $h{present})) {
	$w->dataElement('present', encode_boolean($val)) if $w;
    }
    if (defined (my $val = delete $h{colour})) {
	$w->dataElement('colour', encode_boolean($val)) if $w;
    }
    if (defined (my $val = delete $h{aspect})) {
	$w->dataElement('aspect', $val) if $w;
    }
    if (defined (my $val = delete $h{quality})) {
	$w->dataElement('quality', $val) if $w;
    }
    warn_unknown_keys("zz $e", \%h);
    $w->endTag($e) if $w;
};

=pod

=item I<audio>

This is similar to I<video>.  <present> is a Boolean value, while
the content of <stereo> is stored unchanged.

=cut

$Handlers{audio}->[0] = sub( $ ) {
    my $node = shift;
    my %r;
    foreach (get_subelements($node)) {
	my $name = get_name($_);
	my $value = get_text($_);
	if ($name eq 'present') {
	    warn "'present' seen twice" if defined $r{present};
	    $r{present} = decode_boolean($value);
	}
	elsif ($name eq 'stereo') {
	    warn "'stereo' seen twice" if defined $r{stereo};
	    if ($value eq '') {
		warn "empty 'stereo' element not permitted, should be <stereo>stereo</stereo>";
		$value = 'stereo';
	    }
	    warn "bad value for 'stereo': '$value'"
	      if ($value ne 'mono'
              and $value ne 'stereo'
              and $value ne 'bilingual'
              and $value ne 'surround'
              and $value ne 'dolby digital'
              and $value ne 'dolby');
	    $r{stereo} = $value;
	}
    }
    return \%r;
};
$Handlers{audio}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    my %h = %$v;
    return if not %h; # don't write empty element
    $w->startTag($e) if $w;
    if (defined (my $val = delete $h{present})) {
	$w->dataElement('present', encode_boolean($val)) if $w;
    }
    if (defined (my $val = delete $h{stereo})) {
	$w->dataElement('stereo', $val) if $w;
    }
    warn_unknown_keys($e, \%h);
    $w->endTag($e) if $w;
};

=pod

=item I<previously-shown>

The 'start' and 'channel' attributes are converted to keys in a hash.

=cut

$Handlers{'previously-shown'}->[0] = sub( $ ) {
    my $node = shift; die if not defined $node;
    my %attrs = %{get_attrs($node)};
    my $r = {};
    foreach (qw(start channel)) {
	my $v = delete $attrs{$_};
	$r->{$_} = $v if defined $v;
    }
    foreach (keys %attrs) {
	warn "unknown attribute $_ in previously-shown";
    }
    return $r;
};
$Handlers{'previously-shown'}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    $w->emptyTag($e, %$v) if $w;
};

=pod

=item I<presence>

The content of the element is ignored: it signfies something by its
very presence.  So the conversion from XML to Perl is a constant true
value whenever the element is found; the conversion from Perl to XML
is to write out the element if true, donE<39>t write anything if false.

=cut

$Handlers{presence}->[0] = sub( $ ) {
    my $node = shift;
    # The 'new' element is empty, it signifies newness by its very
    # presence.
    #
    return 1;
};
$Handlers{presence}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    if (not $v) {
	# Not new, so don't create an element.
    }
    else {
	$w->emptyTag($e) if $w;
    }
};

=pod

=item I<subtitles>

The 'type' attribute and the 'language' subelement (both optional)
become keys in a hash.  But see I<language> for what to pass as the
value of that element.

=cut

$Handlers{subtitles}->[0] = sub( $ ) {
    my $node = shift; die if not defined $node;
    my %attrs = %{get_attrs($node)};
    my %r;
    $r{type} = $attrs{type} if defined $attrs{type};
    foreach (get_subelements($node)) {
	my $name = get_name($_);
	if ($name eq 'language') {
	    warn "'language' seen twice" if defined $r{language};
	    $r{language} = read_with_lang($_, 0, 0);
	}
	else {
	    warn "bad content of 'subtitles' element: $name";
	}
    }
    return \%r;
};
$Handlers{subtitles}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    t 'subtitles';
    my ($type, $language) = ($v->{type}, $v->{language});
    my %attrs; $attrs{type} = $type if defined $type;
    if (defined $language) {
	$w->startTag($e, %attrs) if $w;
	write_with_lang($w, 'language', $language, 0, 0);
	$w->endTag($e) if $w;
    }
    else {
	$w->emptyTag($e, %attrs) if $w;
    }
};

=pod

=item I<rating>

The rating is represented as a tuple of [ rating, system, icons ].
The last element is itself a listref of structures returned by the
I<icon> handler.

=cut

$Handlers{rating}->[0] = sub( $ ) {
    my $node = shift; die if not defined $node;
    my %attrs = %{get_attrs($node)};
    my $system = delete $attrs{system} if exists $attrs{system};
    foreach (keys %attrs) {
	warn "unknown attribute in rating: $_";
    }
    my @children = get_subelements($node);

    # First child node is value.
    my $value_node = shift @children;
    if (not defined $value_node) {
	warn "missing 'value' element inside rating";
	return undef;
    }
    if ((my $name = get_name($value_node)) ne 'value') {
	warn "expected 'value' node inside rating, got '$name'";
	return undef;
    }

    my $rating = read_value($value_node);

    # Remaining children are icons.
    my @icons = map { read_icon($_) } @children;
	
    return [ $rating, $system, \@icons ];
};
$Handlers{rating}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    if (not ref $v or ref $v ne 'ARRAY') {
	warn "not writing rating whose content is not an array";
	return;
    }
    my ($rating, $system, $icons) = @$v;
    if (defined $system) {
	$w->startTag($e, system => $system) if $w;
    }
    else {
	$w->startTag($e) if $w;
    }

    write_value($w, 'value', $rating) if $w;
    if ($w) { write_icon($w, 'icon', $_) foreach @$icons };
    $w->endTag($e) if $w;
};

=pod

=item I<star-rating>

In XML this is a string 'X/Y' plus a list of icons.  In Perl represented
as a pair [ rating, icons ] similar to I<rating>.

Multiple star ratings are now supported. For backward compatability,
you may specify a single [rating,icon] or the preferred double array
[[rating,system,icon],[rating2,system2,icon2]] (like 'ratings')


=cut

$Handlers{'star-rating'}->[0] = sub( $ ) {
    my $node = shift;
    my %attrs = %{get_attrs($node)};
    my $system = delete $attrs{system} if exists $attrs{system};
    my @children = get_subelements($node);

    # First child node is value.
    my $value_node = shift @children;
    if (not defined $value_node) {
	warn "missing 'value' element inside star-rating";
	return undef;
    }
    if ((my $name = get_name($value_node)) ne 'value') {
	warn "expected 'value' node inside star-rating, got '$name'";
	return undef;
    }
    my $rating = read_value($value_node);

    # Remaining children are icons.
    my @icons = map { read_icon($_) } @children;
	
    return [ $rating, $system, \@icons ];
};
$Handlers{'star-rating'}->[1] = sub ( $$$ ) {
    my ($w, $e, $v) = @_;
#
# 10/31/2007 star-rating can now have multiple values (and system=)
# let's make it so old code still works!
#
    if (not ref $v or ref $v ne 'ARRAY') {
	   $v=[$v];
#	   warn "not writing star-rating whose content is not an array";
#	return;
    }
    my ($rating, $system, $icons) = @$v;
    if (defined $system) {
	$w->startTag($e, system => $system) if $w;
    }
    else {
	$w->startTag($e) if $w;
    }
    write_value($w, 'value', $rating) if $w;
    if ($w) { write_icon($w, 'icon', $_) foreach @$icons };
    $w->endTag($e) if $w;
};

=pod

=item I<icon>

An icon in XMLTV files is like the <img> element in HTML.  It is
represented in Perl as a hashref with 'src' and optionally 'width'
and 'height' keys.

=cut

sub write_icon( $$$ ) {
    my ($w, $e, $v) = @_;
    croak "no 'src' attribute for icon\n" if not defined $v->{src};
    croak "bad width $v->{width} for icon\n"
      if defined $v->{width} and $v->{width} !~ /^\d+$/;
    croak "bad height $v->{height} for icon\n"
      if defined $v->{height} and $v->{height} !~ /^\d+$/;

    foreach (keys %$v) {
	warn "unrecognized key in icon: $_\n"
	  if $_ ne 'src' and $_ ne 'width' and $_ ne 'height';
    }

    $w->emptyTag($e, %$v);
}
sub read_icon( $ ) {
    my $node = shift; die if not defined $node;
    my %attrs = %{get_attrs($node)};
    warn "missing 'src' attribute in icon" if not defined $attrs{src};
    return \%attrs;
}
$Handlers{icon}->[0] = \&read_icon;
$Handlers{icon}->[1] = sub( $$$ ) {
    my ($w, $e, $v) = @_;
    write_icon($w, $e, $v) if $w;
};

# To keep things tidy some elements that can have icons store their
# textual content inside a subelement called 'value'.  These two
# routines are a bit trivial but they're here for consistency.
#
sub read_value( $ ) {
    my $value_node = shift;
    my $v = get_text($value_node);
    if (not defined $v or $v eq '') {
	warn "no content of 'value' element";
	return undef;
    }
    return $v;
}
sub write_value( $$$ ) {
    my ($w, $e, $v) = @_;
    $w->dataElement($e, $v) if $w;
};


# Booleans in XMLTV files are 'yes' or 'no'.
sub decode_boolean( $ ) {
    my $value = shift;
    if ($value eq 'no') {
	return 0;
    }
    elsif ($value eq 'yes') {
	return 1;
    }
    else {
	warn "bad boolean: $value";
	return undef;
    }
}
sub encode_boolean( $ ) {
    my $v = shift;
    warn "expected a Perl boolean like 0 or 1, not '$v'\n"
      if $v and $v != 1;
    return $v ? 'yes' : 'no';
}


=pod

=item I<with-lang>

In XML something like title can be either <title>Foo</title>
or <title lang="en">Foo</title>.  In Perl these are stored as
[ 'Foo' ] and [ 'Foo', 'en' ].  For the former [ 'Foo', undef ]
would also be okay.

This handler also has two modifiers which may be added to the name
after '/'.  I</e> means that empty text is allowed, and will be
returned as the empty tuple [], to mean that the element is present
but has no text.  When writing with I</e>, undef will also be
understood as present-but-empty.  You cannot however specify a
language if the text is empty.

The modifier I</m> means that the text is allowed to span multiple
lines.

So for example I<with-lang/em> is a handler for text with language,
where the text may be empty and may contain newlines.  Note that the
I<with-lang-or-empty> of earlier releases has been replaced by
I<with-lang/e>.

=cut

sub read_with_lang( $$$ ) {
    my ($node, $allow_empty, $allow_nl) = @_;
    die if not defined $node;
    my %attrs = %{get_attrs($node)};
    my $lang = $attrs{lang} if exists $attrs{lang};
    my $value = get_text($node, $allow_nl);
    if (not length $value) {
	if (not $allow_empty) {
	    warn 'empty string for with-lang value';
	    return undef;
	}
	warn 'empty string may not have language' if defined $lang;
	return [];
    }
    if (defined $lang) {
	return [ $value, $lang ];
    }
    else {
	return [ $value ];
    }
}
$Handlers{'with-lang'}->[0]    = sub( $ ) { read_with_lang($_[0], 0, 0) };
$Handlers{'with-lang/'}->[0]   = sub( $ ) { read_with_lang($_[0], 0, 0) };
$Handlers{'with-lang/e'}->[0]  = sub( $ ) { read_with_lang($_[0], 1, 0) };
$Handlers{'with-lang/m'}->[0]  = sub( $ ) { read_with_lang($_[0], 0, 1) };
$Handlers{'with-lang/em'}->[0] = sub( $ ) { read_with_lang($_[0], 1, 1) };
$Handlers{'with-lang/me'}->[0] = sub( $ ) { read_with_lang($_[0], 1, 1) };

sub write_with_lang( $$$$$ ) {
    my ($w, $e, $v, $allow_empty, $allow_nl) = @_;
    if (not ref $v or ref $v ne 'ARRAY') {
	warn "not writing with-lang whose content is not an array";
	return;
    }

    if (not @$v) {
	if (not $allow_empty) {
	    warn "not writing no content for $e";
	    return;
	}
	$v = [ '' ];
    }

    my ($text, $lang) = @$v;
    t 'writing character data: ' . d $text;
    if (not defined $text) {
	warn "not writing undefined value for $e";
	return;
    }

#
# strip whitespace silently.
# we used to use a warn, but later on the code catches this and drops the record
#
    my $old_text = $text;
    $text =~ s/^\s+//;
    $text =~ s/\s+$//;  

    if (not length $text) {
	if (not $allow_empty) {
	    warn "not writing empty content for $e";
	    return;
	}
	if (defined $lang) {
	    warn "not writing empty content with language for $e";
	    return;
	}
	$w->emptyTag($e) if $w;
	return;
    }

    if (not $allow_nl and $text =~ tr/\n//) {
	warn "not writing text containing newlines for $e";
	return;
    }

    if (defined $lang) {
	$w->dataElement($e, $text, lang => $lang) if $w;
    }
    else {
	$w->dataElement($e, $text) if $w;
    }
}
$Handlers{'with-lang'}->[1]    = sub( $$$ ) { write_with_lang($_[0], $_[1], $_[2], 0, 0) };
$Handlers{'with-lang/'}->[1]   = sub( $$$ ) { write_with_lang($_[0], $_[1], $_[2], 0, 0) };
$Handlers{'with-lang/e'}->[1]  = sub( $$$ ) { write_with_lang($_[0], $_[1], $_[2], 1, 0) };
$Handlers{'with-lang/m'}->[1]  = sub( $$$ ) { write_with_lang($_[0], $_[1], $_[2], 0, 1) };
$Handlers{'with-lang/em'}->[1] = sub( $$$ ) { write_with_lang($_[0], $_[1], $_[2], 1, 1) };
$Handlers{'with-lang/me'}->[1] = sub( $$$ ) { write_with_lang($_[0], $_[1], $_[2], 1, 1) };

# Sanity check.
foreach (keys %Handlers) {
    my $v = $Handlers{$_};
    if (@$v != 2
        or ref($v->[0]) ne 'CODE'
        or ref($v->[1]) ne 'CODE') {
        die "bad handler pair for $_\n";
    }
}

=pod

=back

Now, which handlers are used for which subelements (keys) of channels
and programmes?  And what is the multiplicity (should you expect a
single value or a list of values)?

The following tables map subelements of <channel> and of <programme>
to the handlers used to read and write them.  Many elements have their
own handler with the same name, and most of the others use
I<with-lang>.  The third column specifies the multiplicity of the
element: B<*> (any number) will give a list of values in Perl, B<+>
(one or more) will give a nonempty list, B<?> (maybe one) will give a
scalar, and B<1> (exactly one) will give a scalar which is not undef.

=head2 Handlers for <channel>


=over

=item display-name, I<with-lang>, B<+>

=item icon, I<icon>, B<*>

=item url, I<scalar>, B<*>


=back

=head2 Handlers for <programme>


=over

=item title, I<with-lang>, B<+>

=item sub-title, I<with-lang>, B<*>

=item desc, I<with-lang/m>, B<*>

=item credits, I<credits>, B<?>

=item date, I<scalar>, B<?>

=item category, I<with-lang>, B<*>

=item language, I<with-lang>, B<?>

=item orig-language, I<with-lang>, B<?>

=item length, I<length>, B<?>

=item icon, I<icon>, B<*>

=item url, I<scalar>, B<*>

=item country, I<with-lang>, B<*>

=item episode-num, I<episode-num>, B<*>

=item video, I<video>, B<?>

=item audio, I<audio>, B<?>

=item previously-shown, I<previously-shown>, B<?>

=item premiere, I<with-lang/em>, B<?>

=item last-chance, I<with-lang/em>, B<?>

=item new, I<presence>, B<?>

=item subtitles, I<subtitles>, B<*>

=item rating, I<rating>, B<*>

=item star-rating, I<star-rating>, B<*>


=back

At present, no parsing or validation on dates is done because dates
may be partially specified in XMLTV.  For example '2001' means that
the year is known but not the month, day or time of day.  Maybe in the
future dates will be automatically converted to and from
B<Date::Manip> objects.  For now they just use the I<scalar> handler.
Similar remarks apply to URLs.

=cut

# Private.
sub node_to_programme( $ ) {
    my $node = shift; die if not defined $node;
    my %programme;

    # Attributes of programme element.
    %programme = %{get_attrs($node)};
    t 'attributes: ' . d \%programme;

    # Check the required attributes are there.  As with most checking,
    # this isn't an alternative to using a validator but it does save
    # some headscratching during debugging.
    #
    foreach (qw(start channel)) {
	if (not defined $programme{$_}) {
	    warn "programme missing '$_' attribute\n";
	    return undef;
	}
    }
    my @known_attrs = map { $_->[0] } @Programme_Attributes;
    my %ka; ++$ka{$_} foreach @known_attrs;
    foreach (keys %programme) {
	unless ($ka{$_}) {
	    warn "deleting unknown attribute '$_'";
	    delete $programme{$_};
	}
    }

    call_handlers_read($node, \@Programme_Handlers, \%programme);
    return \%programme;
}


# Private.
sub node_to_channel( $ ) {
    my $node = shift; die if not defined $node;
    my %channel;
    t 'node_to_channel() ENTRY';

    %channel = %{get_attrs($node)};
    t 'attributes: ' . d \%channel;
    if (not defined $channel{id}) {
	warn "channel missing 'id' attribute\n";
    }
    foreach (keys %channel) {
	unless (/^_/ or $_ eq 'id') {
	    warn "deleting unknown attribute '$_'";
	    delete $channel{$_};
	}
    }
		
    t '\@Channel_Handlers=' . d \@Channel_Handlers;
    call_handlers_read($node, \@Channel_Handlers, \%channel);
    return \%channel;
}



# Private.
#
# call_handlers_read()
#
# Read the subelements of a node according to a list giving a
# handler subroutine for each subelement.
#
# Parameters:
#   node
#   Reference to list of handlers: tuples of
#     [element-name, handler-name, multiplicity]
#   Reference to hash for storing results
#
# Warns if errors, but attempts to contine.
#
sub call_handlers_read( $$$ ) {
    my ($node, $handlers, $r) = @_;
    t 'call_handlers_read() using handlers: ' . d $handlers;

    die unless ref($r) eq 'HASH';
    our %r; local *r = $r;
    t 'going through each child of node';

    # Current position in handlers.  We expect to read the subelements
    # in the correct order as specified by the DTD.
    #
    my $handler_pos = 0;

    SUBELEMENT: foreach (get_subelements($node)) {
	t 'doing subelement';
	my $name = get_name($_);
	t "tag name: $name";

	# Search for a handler - from $handler_pos onwards.  But
	# first, just warn if somebody is trying to use an element in
	# the wrong place (trying to go backwards in the list).
	#
	my $found_pos;
	foreach my $i (0 .. $handler_pos - 1) {
	    if ($name eq $handlers->[$i]->[0]) {
		warn "element $name not expected here";
		next SUBELEMENT;
	    }
	}
	for (my $i = $handler_pos; $i < @$handlers; $i++) {
	    if ($handlers->[$i]->[0] eq $name) {
		t 'found handler';
		$found_pos = $i;
		last;
	    }
	    else {
		t "doesn't match name $handlers->[$i]->[0]";
		my ($handler_name, $h, $multiplicity)
		  = @{$handlers->[$i]};
		die if not defined $handler_name;
		die if $handler_name eq '';

		# Before we skip over this element, check that we got
		# the necessary values for it.
		#
		if ($multiplicity eq '?') {
		    # Don't need to check whether this set.
		}
		elsif ($multiplicity eq '1') {
		    if (not defined $r{$handler_name}) {
			warn "no element $handler_name found";
		    }
		}
		elsif ($multiplicity eq '*') {
		    # It's okay if nothing was ever set.  We don't
		    # insist on putting in an empty list.
		    #
		}
		elsif ($multiplicity eq '+') {
		    if (not defined $r{$handler_name}) {
			warn "no element $handler_name found";
		    }
		    elsif (not @{$r{$handler_name}}) {
			warn "strangely, empty list for $handler_name";
		    }
		}
		else {
		    warn "bad value of $multiplicity: $!";
		}
	    }
	}
	if (not defined $found_pos) {
	    warn "unknown element $name";
	    next;
	}
	# Next time we begin searching from this position.
	$handler_pos = $found_pos;

	# Call the handler.
	t 'calling handler';
	my ($handler_name, $h_name, $multiplicity)
	  = @{$handlers->[$found_pos]};
	die if $handler_name ne $name;
	my $h = $Handlers{$h_name}; die "no handler $h_name" if not $h;
	my $result = $h->[0]->($_); # call reader sub
	t 'result: ' . d $result;
	warn("skipping bad $name\n"), next if not defined $result;

	# Now set the value.  We can't do multiplicity checking yet
	# because there might be more elements of this type still to
	# come.
	#
	if ($multiplicity eq '?' or $multiplicity eq '1') {
	    warn "seen $name twice"
	      if defined $r{$name};
	    $r{$name} = $result;
	}
	elsif ($multiplicity eq '*' or $multiplicity eq '+') {
	    push @{$r{$name}}, $result;
	}
	else {
	    warn "bad multiplicity: $multiplicity";
	}
    }
}

sub warn_unknown_keys( $$ ) {
    my $elem_name = shift;
    our %k; local *k = shift;
    foreach (keys %k) {
        /^_/
          or $warned_unknown_key{$elem_name}->{$_}++
          or warn "unknown key $_ in $elem_name hash\n";
    }
}

package XMLTV::Writer;
use base 'XML::Writer';
use Carp;

use Date::Manip qw/UnixDate DateCalc/;

# Use Log::TraceMessages if installed.
BEGIN {
    eval { require Log::TraceMessages };
    if ($@) {
	*t = sub {};
	*d = sub { '' };
    }
    else {
	*t = \&Log::TraceMessages::t;
	*d = \&Log::TraceMessages::d;
    }
}

BEGIN {
  Date::Manip::Date_Init("TZ=UTC");
}

# Override dataElement() to refuse writing empty or whitespace
# elements.
#
sub dataElement( $$$@ ) {
    my ($self, $elem, $content, @rest) = @_;
    if ($content !~ /\S/) {
        warn "not writing empty content for $elem";
        return;
    }
    return $self->SUPER::dataElement($elem, $content, @rest);
}

=pod

=head1 WRITING

When reading a file you have the choice of using C<parse()> to gulp
the whole file and return a data structure, or using
C<parse_callback()> to get the programmes one at a time, although
channels and other data are still read all at once.

There is a similar choice when writing data: the C<write_data()>
routine prints a whole XMLTV document at once, but if you want to
write an XMLTV document incrementally you can manually create an
C<XMLTV::Writer> object and call methods on it.  Synopsis:

  use XMLTV;
  my $w = new XMLTV::Writer();
  $w->comment("Hello from XML::Writer's comment() method");
  $w->start({ 'generator-info-name' => 'Example code in pod' });
  my %ch = (id => 'test-channel', 'display-name' => [ [ 'Test', 'en' ] ]);
  $w->write_channel(\%ch);
  my %prog = (channel => 'test-channel', start => '200203161500',
	      title => [ [ 'News', 'en' ] ]);
  $w->write_programme(\%prog);
  $w->end();

XMLTV::Writer inherits from XML::Writer, and provides the following extra
or overridden methods:

=over

=item new(), the constructor

Creates an XMLTV::Writer object and starts writing an XMLTV file, printing
the DOCTYPE line.  Arguments are passed on to XML::WriterE<39>s constructor,
except for the following:

the 'encoding' key if present gives the XML character encoding.
For example:

  my $w = new XMLTV::Writer(encoding => 'ISO-8859-1');

If encoding is not specified, XML::WriterE<39>s default is used
(currently UTF-8).

XMLTW::Writer can also filter out specific days from the data. This is
useful if the datasource provides data for periods of time that does not
match the days that the user has asked for. The filtering is controlled
with the days, offset and cutoff arguments:

  my $w = new XMLTV::Writer(
      offset => 1,
      days => 2,
      cutoff => "050000" );

In this example, XMLTV::Writer will discard all entries that do not have
starttimes larger than or equal to 05:00 tomorrow and less than 05:00
two days after tomorrow. The time offset is stripped off the starttime before
the comparison is made.

=cut

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my %args = @_;
    croak 'OUTPUT requires a filehandle, not a filename or anything else'
      if exists $args{OUTPUT} and not ref $args{OUTPUT};
    my $encoding = delete $args{encoding};
    my $days = delete $args{days};
    my $offset = delete $args{offset};
    my $cutoff = delete $args{cutoff};

    my $self = $class->SUPER::new(DATA_MODE => 1, DATA_INDENT => 2, %args);
    bless($self, $class);

    if (defined $encoding) {
	$self->xmlDecl($encoding);
    }
    else {
	# XML::Writer puts in 'encoding="UTF-8"' even if you don't ask
	# for it.
	#
	warn "assuming default UTF-8 encoding for output\n";
	$self->xmlDecl();
    }

#    $Log::TraceMessages::On = 1;
    $self->{mintime} = "19700101000000"; 	 
    $self->{maxtime} = "29991231235959"; 	 
    

    if (defined( $days ) and defined( $offset ) and defined( $cutoff )) {
      $self->{mintime} = UnixDate( 
          DateCalc( "today", "+" . $offset . " days" ),
          "%Y%m%d") . $cutoff;
      t "using mintime $self->{mintime}";

      $self->{maxtime} = UnixDate( 
          DateCalc("today", "+" . $offset+$days . " days"), 	 
          "%Y%m%d" ) . $cutoff;
      t "using maxtime $self->{maxtime}";
    }
    elsif (defined( $days ) or defined( $offset ) or defined($cutoff)) {
      croak 'You must specify days, offset and cutoff or none of them';
    }

    {
	local $^W = 0; $self->doctype('tv', undef, 'xmltv.dtd');
    }
    $self->{xmltv_writer_state} = 'new';
    return $self;
}

=pod

=item start()

Write the start of the <tv> element.  Parameter is a hashref which gives
the attributes of this element.

=cut

sub start {
    my $self = shift;
    die 'usage: XMLTV::Writer->start(hashref of attrs)' if @_ != 1;
    my $attrs = shift;

    for ($self->{xmltv_writer_state}) {
	if ($_ eq 'new') {
	    # Okay.
	}
	elsif ($_ eq 'channels' or $_ eq 'programmes') {
	    croak 'cannot call start() more than once on XMLTV::Writer';
	}
	elsif ($_ eq 'end') {
	    croak 'cannot do anything with end()ed XMLTV::Writer';
	}
	else { die }

	$_ = 'channels';
    }
    $self->startTag('tv', order_attrs(%{$attrs}));
}

=pod

=item write_channels()

Write several channels at once.  Parameter is a reference to a hash
mapping channel id to channel details.  They will be written sorted
by id, which is reasonable since the order of channels in an XMLTV
file isnE<39>t significant.

=cut

sub write_channels {
    my ($self, $channels) = @_;
    t('write_channels(' . d($self) . ', ' . d($channels) . ') ENTRY');
    croak 'expected hashref of channels' if ref $channels ne 'HASH';

    for ($self->{xmltv_writer_state}) {
	if ($_ eq 'new') {
	    croak 'must call start() on XMLTV::Writer first';
	}
	elsif ($_ eq 'channels') {
	    # Okay.
	}
	elsif ($_ eq 'programmes') {
	    croak 'cannot write channels after writing programmes';
	}
	elsif ($_ eq 'end') {
	    croak 'cannot do anything with end()ed XMLTV::Writer';
	}
	else { die }
    }

    my @ids = sort keys %$channels;
    t 'sorted list of channel ids: ' . d \@ids;
    foreach (@ids) {
	t "writing channel with id $_";
	my $ch = $channels->{$_};
	$self->write_channel($ch);
    }
    t('write_channels() EXIT');
}

=pod

=item write_channel()

Write a single channel.  You can call this routine if you want, but
most of the time C<write_channels()> is a better interface.

=cut

sub write_channel {
    my ($self, $ch) = @_;
    croak 'undef channel hash passed' if not defined $ch;
    croak "expected a hashref, got: $ch" if ref $ch ne 'HASH';

    for ($self->{xmltv_writer_state}) {
	if ($_ eq 'new') {
	    croak 'must call start() on XMLTV::Writer first';
	}
	elsif ($_ eq 'channels') {
	    # Okay.
	}
	elsif ($_ eq 'programmes') {
	    croak 'cannot write channels after writing programmes';
	}
	elsif ($_ eq 'end') {
	    croak 'cannot do anything with end()ed XMLTV::Writer';
	}
	else { die }
    }

    my %ch = %$ch; # make a copy
    my $id = delete $ch{id};
    die "no 'id' attribute in channel" if not defined $id;
    write_element_with_handlers($self, 'channel', { id => $id },
				\@XMLTV::Channel_Handlers, \%ch);
}

=pod

=item write_programme()

Write details for a single programme as XML.

=cut

sub write_programme {
    my $self = shift;
    die 'usage: XMLTV::Writer->write_programme(programme hash)' if @_ != 1;
    my $ref = shift;
    croak 'write_programme() expects programme hashref'
      if ref $ref ne 'HASH';
    t('write_programme(' . d($self) . ', ' . d($ref) . ') ENTRY');

    for ($self->{xmltv_writer_state}) {
	if ($_ eq 'new') {
	    croak 'must call start() on XMLTV::Writer first';
	}
	elsif ($_ eq 'channels') {
	    $_ = 'programmes';
	}
	elsif ($_ eq 'programmes') {
	    # Okay.
	}
	elsif ($_ eq 'end') {
	    croak 'cannot do anything with end()ed XMLTV::Writer';
	}
	else { die }
    }

    # We make a copy of the programme hash and delete elements from it
    # as they are dealt with; then we can easily spot any unhandled
    # elements at the end.
    #
    my %p = %$ref;

    # First deal with those hash keys that refer to metadata on when
    # the programme is broadcast.  After taking those out of the hash,
    # we can use the handlers to output individual details.
    #
    my %attrs;
    die if not @XMLTV::Programme_Attributes;
    foreach (@XMLTV::Programme_Attributes) {
	my ($name, $mult) = @$_;
	t "looking for key $name";
	my $val = delete $p{$name};
	if ($mult eq '?') {
	    # No need to check anything.
	}
	elsif ($mult eq '1') {
	    if (not defined $val) {
		warn "programme hash missing $name key, skipping";
		return;
	    }
	}
	else { die "bad multiplicity for attribute: $mult" }
	$attrs{$name} = $val if defined $val;
    }

    # We use string comparisons without timeoffsets for comparing times.
    my( $start ) = split( /\s+/, $attrs{start} );
    if( $start lt $self->{mintime} or
        $start ge $self->{maxtime} ) {
      t "skipping programme with start $attrs{start}";
      return;
    }

    t "beginning 'programme' element";
    write_element_with_handlers($self, 'programme', \%attrs,
				\@XMLTV::Programme_Handlers, \%p);
}

=pod

=item end()

Say youE<39>ve finished writing programmes.  This ends the <tv> element
and the file.

=cut

sub end {
    my $self = shift;

    for ($self->{xmltv_writer_state}) {
	if ($_ eq 'new') {
	    croak 'must call start() on XMLTV::Writer first';
	}
	elsif ($_ eq 'channels' or $_ eq 'programmes') {
	    $_ = 'end';
	}
	elsif ($_ eq 'end') {
	    croak 'cannot do anything with end()ed XMLTV::Writer';
	}
	else { die }
    }

    $self->endTag('tv');
    $self->SUPER::end(@_);
}


# Private.
# order_attrs()
#
# In XML the order of attributes is not significant.  But to make
# things look nice we try to output them in the same order as given in
# the DTD.
#
# Takes a list of (key, value, key, value, ...) and returns one with
# keys in a nice-looking order.
#
sub order_attrs {
    die "expected even number of elements, from a hash"
      if @_ % 2;
    my @a = ((map { $_->[0] } (@XMLTV::Channel_Attributes,
			       @XMLTV::Programme_Attributes)),
	     qw(date source-info-url source-info-name source-data-url
		generator-info-name generator-info-url));

    my @r;
    my %in = @_;
    foreach (@a) {
	if (exists $in{$_}) {
	    my $v = delete $in{$_};
	    push @r, $_, $v;
	}
    }

    foreach (sort keys %in) {
	warn "unknown attribute $_" unless /^_/;
	push @r, $_, $in{$_};
    }

    return @r;
}


# Private.
#
# Writes the elements of a hash to an XMLTV::Writer using a list of
# handlers.  Deletes keys (modifying the hash passed in) as they are
# written.
#
# Requires all mandatory keys be present in the hash - if you're not
# sure then use check_multiplicity() first.
#
# Returns true if the element was successfully written, or if any
# errors found don't look serious enough to cause bad XML.  If the
# XML::Writer object passed in is undef then nothing is written (since
# the write handlers are coded like that.)
#
sub call_handlers_write( $$$ ) {
    my ($self, $handlers, $input) = @_;
    t 'writing input hash: ' . d $input;
    die if not defined $input;

    my $bad = 0;
    foreach (@$handlers) {
	my ($name, $h_name, $multiplicity) = @$_;
	my $h = $XMLTV::Handlers{$h_name}; die "no handler $h_name" if not $h;
	my $writer = $h->[1]; die if not defined $writer;
	t "doing handler for $name$multiplicity";
	local $SIG{__WARN__} = sub {
	    warn "$name element: $_[0]";
	    $bad = 1;
	};
	my $val = delete $input->{$name};
	t 'got value(s): ' . d $val;
	if ($multiplicity eq '1') {
	    $writer->($self, $name, $val);
	}
	elsif ($multiplicity eq '?') {
	    $writer->($self, $name, $val) if defined $val;
	}
	elsif ($multiplicity eq '*' or $multiplicity eq '+') {
	    croak "value for key $name should be an array ref"
	      if defined $val and ref $val ne 'ARRAY';
	    foreach (@{$val}) {
		t 'writing value: ' . d $_;
		$writer->($self, $name, $_);
		t 'finished writing multiple values';
	    }
	}
	else {
	    warn "bad multiplicity specifier: $multiplicity";
	}
    }
    t 'leftover keys: ' . d([ sort keys %$input ]);
    return not $bad;
}


# Private.
#
# Warns about missing keys that are supposed to be mandatory.  Returns
# true iff everything is okay.
#
sub check_multiplicity( $$ ) {
    my ($handlers, $input) = @_;
    foreach (@$handlers) {
	my ($name, $h_name, $multiplicity) = @$_;
	t "checking handler for $name: $h_name with multiplicity $multiplicity";
	if ($multiplicity eq '1') {
	    if (not defined $input->{$name}) {
		warn "hash missing value for $name";
		return 0;
	    }
	}
	elsif ($multiplicity eq '?') {
	    # Okay if not present.
	}
	elsif ($multiplicity eq '*') {
	    # Not present, or undef, is treated as empty list.
	}
	elsif ($multiplicity eq '+') {
	    t 'one or more, checking for a listref with no undef values';
	    my $val = $input->{$name};
	    if (not defined $val) {
		warn "hash missing value for $name (expected list)";
		return 0;
	    }
	    if (ref($val) ne 'ARRAY') {
		die "hash has bad contents for $name (expected list)";
		return 0;
	    }

	    t 'all values: ' . d $val;
            my @new_val = grep { defined } @$val;
	    t 'values that are defined: ' . d \@new_val;
            if (@new_val != @$val) {
                warn "hash had some undef elements in list for $name, removed";
                @$val = @new_val;
            }

	    if (not @$val) {
		warn "hash has empty list of $name properties (expected at least one)";
		return 0;
	    }
	}
	else {
	    warn "bad multiplicity specifier: $multiplicity";
	}
    }
    return 1;
}


# Private.
#
# Write a complete element with attributes, and subelements written
# using call_handlers_write().  The advantage over doing it by hand is
# that if some required keys are missing, nothing is written (rather
# than an incomplete and invalid element).
#
sub write_element_with_handlers( $$$$$ ) {
    my ($w, $name, $attrs, $handlers, $hash) = @_;
    if (not check_multiplicity($handlers, $hash)) {
        warn "keys missing in $name hash, not writing";
        return;
    }

    # Special 'debug' keys written as comments inside the element.
    my %debug_keys;
    foreach (grep /^debug/, keys %$hash) {
	$debug_keys{$_} = delete $hash->{$_};
    }

    # Call all the handlers with no writer object and make sure
    # they're happy.
    #
    if (not call_handlers_write(undef, $handlers, { %$hash })) {
	warn "bad data inside $name element, not writing\n";
	return;
    }

    $w->startTag($name, order_attrs(%$attrs));
    foreach (sort keys %debug_keys) {
	my $val = $debug_keys{$_};
	$w->comment((defined $val) ? "$_: $val" : $_);
    }
    call_handlers_write($w, $handlers, $hash);
    XMLTV::warn_unknown_keys($name, $hash);
    $w->endTag($name);
}

=pod

=back

=head1 AUTHOR

Ed Avis, ed@membled.com

=head1 SEE ALSO

The file format is defined by the DTD xmltv.dtd, which is included in
the xmltv package along with this module.  It should be installed in
your systemE<39>s standard place for SGML and XML DTDs.

The xmltv package has a web page at
<http://xmltv.org/> which carries
information about the file format and the various tools and apps which
are distributed with this module.

=cut

1;