bibtexbrowser /bibtexbrowser.php

Language PHP Lines 3496
MD5 Hash 4f9f18f4e3f0b0f17ae103fa5235259f
Repository https://github.com/emir-munoz/bibtexbrowser.git View Raw File
   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
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
<?php /* bibtexbrowser: publication lists with bibtex and PHP
<!--this is version v20120703 -->
URL: http://www.monperrus.net/martin/bibtexbrowser/ 
Feedback & Bug Reports: martin.monperrus@gmail.com

(C) 2006-2012 Martin Monperrus
(C) 2005-2006 The University of Texas at El Paso / Joel Garcia, Leonardo Ruiz, and Yoonsik Cheon
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

*/

// it is be possible to include( 'bibtexbrowser.php' ); several times in the same script
// added on Wednesday, June 01 2011, bug found by Carlos Bras
if (!defined('BIBTEXBROWSER')) {
// this if block ends at the very end of this file, after all class and function declarations.
define('BIBTEXBROWSER','v20120703');


// *************** CONFIGURATION
// I recommend to put your changes in bibtexbrowser.local.php
// it will help you to upgrade the script with a new version
@include(dirname(__FILE__).'/bibtexbrowser.local.php');
// there is no encoding transformation from the bibtex file to the html file
// if your bibtex file contains 8 bits characters in utf-8
// change the following parameter
@define('ENCODING','UTF-8');//@define('ENCODING','iso-8859-1');//define('ENCODING','windows-1252');
// number of bib items per page
// we use the same parameter 'num' as Google
@define('PAGE_SIZE',isset($_GET['num'])?(preg_match('/^\d+$/',$_GET['num'])?$_GET['num']:10000):14);
// bibtexbrowser uses a small piece of Javascript to improve the user experience
// see http://en.wikipedia.org/wiki/Progressive_enhancement
// if you don't like it, you can be disable it by adding in bibtexbrowser.local.php
// @define('BIBTEXBROWSER_USE_PROGRESSIVE_ENHANCEMENT',false);
@define('BIBTEXBROWSER_USE_PROGRESSIVE_ENHANCEMENT',true);
// if you disable the Javascript progressive enhancement, 
// you may want the links to be open in a new window/tab
// if yes, add in bibtexbrowser.local.php  define('BIBTEXBROWSER_BIB_IN_NEW_WINDOW',true);
@define('BIBTEXBROWSER_BIB_IN_NEW_WINDOW',false);
@define('BIBLIOGRAPHYSTYLE','DefaultBibliographyStyle');// this is the name of a function
@define('BIBLIOGRAPHYSECTIONS','DefaultBibliographySections');// this is the name of a function
// can we load bibtex files on external servers?
@define('BIBTEXBROWSER_LOCAL_BIB_ONLY', true);

// the default view in {SimpleDisplay,AcademicDisplay,RSSDisplay,BibtexDisplay}
@define('BIBTEXBROWSER_DEFAULT_DISPLAY','SimpleDisplay');

// the target frame of menu links
@define('BIBTEXBROWSER_MENU_TARGET','main'); // might be define('BIBTEXBROWSER_MENU_TARGET','_self'); in bibtexbrowser.local.php 

@define('ABBRV_TYPE','index');// may be year/x-abbrv/key/none/index

// Wrapper to use when we are included by another script 
@define('BIBTEXBROWSER_EMBEDDED_WRAPPER', 'NoWrapper');
// default order functions
@define('ORDER_FUNCTION','compare_bib_entry_by_year_and_month');
// can be @define('ORDER_FUNCTION','compare_bib_entry_by_title');

// only displaying the n newest entries
@define('BIBTEXBROWSER_NEWEST',5);

@define('BIBTEXBROWSER_NO_DEFAULT', false);

// do we add [bibtex] links ?
// suggested by Sascha Schnepp
@define('BIBTEXBROWSER_BIBTEX_LINKS',true);

@define('BIBTEXBROWSER_DEBUG',false);

@define('COMMA_NAMES',false);// do have authors in a comma separated form?
@define('TYPES_SIZE',10); // number of entry types per table
@define('YEAR_SIZE',20); // number of years per table
@define('AUTHORS_SIZE',30); // number of authors per table
@define('TAGS_SIZE',30); // number of keywords per table
@define('READLINE_LIMIT',1024);
@define('Q_YEAR', 'year');
@define('Q_YEAR_PAGE', 'year_page');
@define('Q_FILE', 'bib');
@define('Q_AUTHOR', 'author');
@define('Q_AUTHOR_PAGE', 'author_page');
@define('Q_TAG', 'keywords');
@define('Q_TAG_PAGE', 'keywords_page');
@define('Q_TYPE', 'type');
@define('Q_TYPE_PAGE', 'type_page');
@define('Q_ALL', 'all');
@define('Q_ENTRY', 'entry');
@define('Q_KEY', 'key');
@define('Q_SEARCH', 'search');
@define('Q_EXCLUDE', 'exclude');
@define('Q_RESULT', 'result');
@define('Q_ACADEMIC', 'academic');
@define('Q_DB', 'bibdb');
@define('Q_LATEST', 'latest');
@define('AUTHOR', 'author');
@define('EDITOR', 'editor');
@define('SCHOOL', 'school');
@define('TITLE', 'title');
@define('BOOKTITLE', 'booktitle');
@define('YEAR', 'year');
@define('BUFFERSIZE',100000);
@define('MULTIPLE_BIB_SEPARATOR',';');
@define('METADATA_GS',true);
@define('METADATA_DC',true);
@define('METADATA_EPRINTS',false);

// in embedded mode, we still need a URL for displaying bibtex entries alone
// this is usually resolved to bibtexbrowser.php
// but can be overridden in bibtexbrowser.local.php 
// for instance with @define('BIBTEXBROWSER_URL',''); // links to the current page with ?
@define('BIBTEXBROWSER_URL',basename(__FILE__));

// *************** END CONFIGURATION

// for clean search engine links
// we disable url rewriting
// ... and hope that your php configuration will accept one of these
@ini_set("session.use_only_cookies",1);
@ini_set("session.use_trans_sid",0);
@ini_set("url_rewriter.tags","");

// we ensure that the pages won't get polluted
// if future versions of PHP change warning mechanisms...

@error_reporting(E_ERROR);


/** parses $_GET[Q_FILE] and puts the result (an object of type BibDataBase) in $_GET[Q_DB].
See also zetDB().
  */
function setDB() {
  list($db, $parsed, $updated, $saved) = _zetDB(@$_GET[Q_FILE]);
  $_GET[Q_DB] = $db;
  return $updated;
}

/** parses the $bibtex_filenames (usually semi-column separated) and returns an object of type BibDataBase.
See also setDB()
*/
function zetDB($bibtex_filenames) {
  list($db, $parsed, $updated, $saved) = _zetDB($bibtex_filenames);
  return $db;
}

/** @nodoc */
function default_message() {

  if (BIBTEXBROWSER_NO_DEFAULT) { return; }

  ?>
  <div id="bibtexbrowser_message">
  Congratulations! bibtexbrowser is correctly installed!<br/>
  Now you have to pass the name of the bibtex file as parameter (e.g. bibtexbrowser.php?bib=mybib.php)<br/>
  You may browse:<br/>
  <?php
  foreach (glob("*.bib") as $bibfile) {
    $url="?bib=".$bibfile; echo '<a href="'.$url.'" rel="nofollow">'.$bibfile.'</a><br/>';
  }
  echo "</div>";
}

/** @nodoc */
function _zetDB($bibtex_filenames) {

  $db = null;
  // Check if magic_quotes_runtime is active
  if(get_magic_quotes_runtime())
  {
      // Deactivate
      // otherwise it does not work
      set_magic_quotes_runtime(false);
  }
  
  // default bib file, if no file is specified in the query string.
  if (!isset($bibtex_filenames) || $bibtex_filenames == "") {
    default_message();
    exit;
  }

  // first does the bibfiles exist:
  // $bibtex_filenames can be urlencoded for instance if they contain slashes
  // so we decode it
  $bibtex_filenames = urldecode($bibtex_filenames);

  // ---------------------------- HANDLING unexistent files
  foreach(explode(MULTIPLE_BIB_SEPARATOR, $bibtex_filenames) as $bib) {
  
    // this is a security protection
    if (BIBTEXBROWSER_LOCAL_BIB_ONLY && !file_exists($bib)) {
     // to automate dectection of faulty links with tools such as webcheck
     header('HTTP/1.1 404 Not found');
     die('<b>the bib file '.$bib.' does not exist !</b>');
    }
  } // end for each

  // ---------------------------- HANDLING HTTP If-modified-since
  // testing with $ curl -v --header "If-Modified-Since: Fri, 23 Oct 2010 19:22:47 GMT" "... bibtexbrowser.php?key=wasylkowski07&bib=..%252Fstrings.bib%253B..%252Fentries.bib"
  // and $ curl -v --header "If-Modified-Since: Fri, 23 Oct 2000 19:22:47 GMT" "... bibtexbrowser.php?key=wasylkowski07&bib=..%252Fstrings.bib%253B..%252Fentries.bib"

  // save bandwidth and server cpu
  // (imagine the number of requests from search engine bots...)
  $bib_is_unmodified = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ;
  foreach(explode(MULTIPLE_BIB_SEPARATOR, $bibtex_filenames) as $bib) {
      $bib_is_unmodified = 
                    $bib_is_unmodified                               
                    &&  (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])>filemtime($bib));
  } // end for each
  if ( $bib_is_unmodified && !headers_sent()) { 
    header("HTTP/1.1 304 Not Modified");
    exit; 
  }



  // ---------------------------- HANDLING caching of compiled bibtex files
  // for sake of performance, once the bibtex file is parsed
  // we try to save a "compiled" in a txt file
  $compiledbib = 'bibtexbrowser_'.md5($bibtex_filenames).'.dat';

  $parse=false;
  foreach(explode(MULTIPLE_BIB_SEPARATOR, $bibtex_filenames) as $bib) {
  // do we have a compiled version ?
  if (is_file($compiledbib) 
     && is_readable($compiledbib) 
     && filesize($compiledbib)>0
   ) {

    $f = fopen($compiledbib,'r');
    //we use a lock to avoid that a call to bibbtexbrowser made while we write the object loads an incorrect object
    if (flock($f,LOCK_EX)) { 
      $s = filesize($compiledbib);
      $ser = fread($f,$s);
      $db = @unserialize($ser);
      flock($f,LOCK_UN);
    } else { die('could not get the lock'); }
    fclose($f);
    // basic test
    // do we have an correct version of the file
    if (!is_a($db,'BibDataBase')) {
      unlink($compiledbib);
      if (BIBTEXBROWSER_DEBUG) { die('$db not a BibDataBase. please reload.'); }      
      $parse=true;
    }
  } else {$parse=true;}
  } // end for each

  // we don't have a compiled version
  if ($parse) {
    //echo '<!-- parsing -->';
    // then parsing the file
    $db = createBibDataBase();
    foreach(explode(MULTIPLE_BIB_SEPARATOR, $bibtex_filenames) as $bib) {
      $db->load($bib);
    }    
  }
  
  $updated = false; 
  // now we may update the database
  if (!file_exists($compiledbib)) {
    @touch($compiledbib);
    $updated = true; // limit case
  } else foreach(explode(MULTIPLE_BIB_SEPARATOR, $bibtex_filenames) as $bib) {
      // is it up to date ? wrt to the bib file and the script
    // then upgrading with a new version of bibtexbrowser triggers a new compilation of the bib file
    if (filemtime($bib)>filemtime($compiledbib) || filemtime(__FILE__)>filemtime($compiledbib)) {
      //echo "updating...";
      $db->update($bib);
      $updated = true;
    }
  }
  
  //echo var_export($parse);
  //echo var_export($updated);

  $saved = false;
  // are we able to save the compiled version ?
  // note that the compiled version is saved in the current working directory
  if ( ($parse || $updated ) && is_writable($compiledbib)) {
    // we use 'a' because the file is not locked between fopen and flock
    $f = fopen($compiledbib,'a');
    //we use a lock to avoid that a call to bibbtexbrowser made while we write the object loads an incorrect object
    if (flock($f,LOCK_EX)) { 
//       echo '<!-- saving -->';
      ftruncate($f,0);
      fwrite($f,serialize($db));
      flock($f,LOCK_UN);
      $saved = true;
    } else { die('could not get the lock'); }
    fclose($f);
  } // end saving the cached verions
  //else echo '<!-- please chmod the directory containing the bibtex file to be able to keep a compiled version (much faster requests for large bibtex files) -->';
  

  return array(&$db, $parse, $updated, $saved);
} // end function setDB


// factories
// may be overridden in bibtexbrowser.local.php
if (!function_exists('createBibDataBase')) {
  /** factory method for openness @nodoc */
  function createBibDataBase() { $x = new BibDataBase(); return $x;}
}
if (!function_exists('createBibEntry')) {
  /** factory method for openness @nodoc */
  function createBibEntry() { $x = new BibEntry(); return $x;}
}
if (!function_exists('createBibDBBuilder')) {
  /** factory method for openness @nodoc */
  function createBibDBBuilder() { $x = new BibDBBuilder(); return $x;}
}
if (!function_exists('createBasicDisplay')) {
  /** factory method for openness @nodoc */
  function createBasicDisplay() { $x = new SimpleDisplay(); return $x;}
}
if (!function_exists('createBibEntryDisplay')) {
  /** factory method for openness @nodoc */
  function createBibEntryDisplay() { $x = new BibEntryDisplay(); return $x;}
}
if (!function_exists('createMenuManager')) {
  /** factory method for openness @nodoc */
  function createMenuManager() { $x = new MenuManager(); return $x;}
}


////////////////////////////////////////////////////////

/** is a generic parser of bibtex files.
usage:
<pre>
  $delegate = new XMLPrettyPrinter();// or another delegate such as BibDBBuilder
  $parser = new StateBasedBibtexParser($delegate);
  $parser->parse('foo.bib');
</pre>
notes:
 - It has no dependencies, it can be used outside of bibtexbrowser
 - The delegate is expected to have some methods, see classes BibDBBuilder and XMLPrettyPrinter
 */
class StateBasedBibtexParser {
  
  var $delegate;
 
  function StateBasedBibtexParser(&$delegate) {
    $this->delegate = &$delegate;
  }

  function parse($bibfilename) {
    $delegate = &$this->delegate;
    // STATE DEFINITIONS
    @define('NOTHING',1);
    @define('GETTYPE',2);
    @define('GETKEY',3);
    @define('GETVALUE',4);
    @define('GETVALUEDELIMITEDBYQUOTES',5);
    @define('GETVALUEDELIMITEDBYQUOTES_ESCAPED',6);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS',7);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_ESCAPED',8);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL',9);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL_ESCAPED',10);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL',11);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL_ESCAPED',12);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL',13);
    @define('GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL_ESCAPED',14);


    $state=NOTHING;
    $entrytype='';
    $entrykey='';
    $entryvalue='';
    $finalkey='';
    $entrysource='';

    // metastate
    $isinentry = false;

    $delegate->beginFile();

    $handle = fopen($bibfilename, "r");
    if (!$handle) die ('cannot open '.$bibfilename);
    // if you encounter this error "Allowed memory size of xxxxx bytes exhausted"
    // then decrease the size of the temp buffer below
    $bufsize=BUFFERSIZE;
    while (!feof($handle)) {
    $sread=fread($handle,$bufsize);
    //foreach(str_split($sread) as $s) {
    for ( $i=0; $i < strlen( $sread ); $i++) { $s=$sread[$i];

    if ($isinentry) $entrysource.=$s;

    if ($state==NOTHING) {
      // this is the beginning of an entry
      if ($s=='@') {
      $delegate->beginEntry();
      $state = GETTYPE;
      $isinentry = true;
      $entrysource='@';
      }
    }

    else if ($state==GETTYPE) {
      // this is the beginning of a key
      if ($s=='{') {
      $state = GETKEY;
      $delegate->setEntryType($entrytype);
      $entrytype='';
      }
      else   $entrytype=$entrytype.$s;
    }

    else if ($state==GETKEY) {
      // now we get the value
      if ($s=='=') {
      $state = GETVALUE;
      $finalkey=$entrykey;
      $entrykey='';}
      // oups we only have the key :-) anyway
      else if ($s=='}') {
      $state = NOTHING;$isinentry = false;$delegate->endEntry($entrysource);
      $entrykey='';
      }
      // OK now we look for values
      else if ($s==',') {
      $state=GETKEY;
      $delegate->setEntryKey($entrykey);
      $entrykey='';}
      else { $entrykey=$entrykey.$s; }
      }
      // we just got a =, we can now receive the value, but we don't now whether the value
      // is delimited by curly brackets, double quotes or nothing
      else if ($state==GETVALUE) {

        // the value is delimited by double quotes
        if ($s=='"') {
        $state = GETVALUEDELIMITEDBYQUOTES;
        }
        // the value is delimited by curly brackets
        else if ($s=='{') {
        $state = GETVALUEDELIMITEDBYCURLYBRACKETS;
        }
        // the end of the key and no value found: it is the bibtex key e.g. \cite{Descartes1637}
        else if ($s==',') {
        $state = GETKEY;
        $delegate->setEntryField(trim($finalkey),$entryvalue);
        $entryvalue=''; // resetting the value buffer
        }
        // this is the end of the value AND of the entry
        else if ($s=='}') {
        $state = NOTHING;
        $delegate->setEntryField(trim($finalkey),$entryvalue);
        $isinentry = false;$delegate->endEntry($entrysource);
        $entryvalue=''; // resetting the value buffer
        }
        else if ($s==' ' || $s=="\t"  || $s=="\n" || $s=="\r" ) {
          // blank characters are not taken into account when values are not in quotes or curly brackets
        }
        else { $entryvalue=$entryvalue.$s;}
      }


    /* GETVALUEDELIMITEDBYCURLYBRACKETS* handle entries delimited by curly brackets and the possible nested curly brackets */
    else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS) {

      if ($s=='\\') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_ESCAPED;
      $entryvalue=$entryvalue.$s;}
      else if ($s=='{') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL;$entryvalue=$entryvalue.$s;}
      else if ($s=='}') {
      $state = GETVALUE;}
      else { $entryvalue=$entryvalue.$s;}
    }
      // handle anti-slashed brackets
      else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_ESCAPED) {
        $state = GETVALUEDELIMITEDBYCURLYBRACKETS;
        $entryvalue=$entryvalue.$s;
        }
    // in first level of curly bracket
    else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL) {
      if ($s=='\\') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL_ESCAPED;
      $entryvalue=$entryvalue.$s;}
      else if ($s=='{') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL;$entryvalue=$entryvalue.$s;}
      else if ($s=='}') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS;$entryvalue=$entryvalue.$s;}
      else { $entryvalue=$entryvalue.$s;}
    }
      // handle anti-slashed brackets
      else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL_ESCAPED) {
        $state = GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL;
        $entryvalue=$entryvalue.$s;
        }

    // in second level of curly bracket
    else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL) {
      if ($s=='\\') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL_ESCAPED;
      $entryvalue=$entryvalue.$s;}
      else if ($s=='{') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL;$entryvalue=$entryvalue.$s;}
      else if ($s=='}') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_1NESTEDLEVEL;$entryvalue=$entryvalue.$s;}
      else { $entryvalue=$entryvalue.$s;}
      }
      // handle anti-slashed brackets
      else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL_ESCAPED) {
        $state = GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL;
        $entryvalue=$entryvalue.$s;
      }

    // in third level of curly bracket
    else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL) {
      if ($s=='\\') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL_ESCAPED;
      $entryvalue=$entryvalue.$s;}
      else if ($s=='}') {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_2NESTEDLEVEL;$entryvalue=$entryvalue.$s;}
      else { $entryvalue=$entryvalue.$s;}
    }
    // handle anti-slashed brackets
    else if ($state==GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL_ESCAPED) {
      $state = GETVALUEDELIMITEDBYCURLYBRACKETS_3NESTEDLEVEL;
      $entryvalue=$entryvalue.$s;
    }

    /* handles entries delimited by double quotes */
    else if ($state==GETVALUEDELIMITEDBYQUOTES) {

      if ($s=='\\') {
      $state = GETVALUEDELIMITEDBYQUOTES_ESCAPED;
      $entryvalue=$entryvalue.$s;}
      else if ($s=='"') {
      $state = GETVALUE;
      }
      else {  $entryvalue=$entryvalue.$s;}
    }
    // handle anti-double quotes
    else if ($state==GETVALUEDELIMITEDBYQUOTES_ESCAPED) {
      $state = GETVALUEDELIMITEDBYQUOTES;
      $entryvalue=$entryvalue.$s;
    }

    } // end for
    } // end while
    $delegate->endFile();
    fclose($handle);
    //$d = &$this->delegate;print_r($d);
  } // end function
} // end class

/** is a delegate for StateBasedBibParser.
usage:
see snippet of [[#StateBasedBibParser]]
*/
class XMLPrettyPrinter {
  function beginFile() {
    header('Content-type: text/xml;');
    print '<?xml version="1.0" encoding="'.ENCODING.'"?>';
    print '<bibfile>';
  }


  function endFile() {
    print '</bibfile>';
  }
  function setEntryField($finalkey,$entryvalue) {
    print "<data>\n<key>".$finalkey."</key>\n<value>".$entryvalue."</value>\n</data>\n";
  }

  function setEntryType($entrytype) {
    print '<type>'.$entrytype.'</type>';
  }

  function setEntryKey($entrykey) {
    print '<keyonly>'.$entrykey.'</keyonly>';
  }

  function beginEntry() {
    print "<entry>\n";
  }

  function endEntry($entrysource) {
    print "</entry>\n";
  }
} // end class XMLPrettyPrinter

/** builds arrays of BibEntry objects from a bibtex file.
usage:
<pre>
  $empty_array = array();
  $db = new BibDBBuilder(); // see also factory method createBibDBBuilder
  $db->build('foo.bib'); // parses foo.bib
  print_r($db->builtdb);// an associated array key -> BibEntry objects
  print_r($db->stringdb);// an associated array key -> strings representing @string
</pre>
notes: 
 method build can be used several times, bibtex entries are accumulated in the builder
*/
class BibDBBuilder {

  /** A hashtable from keys to bib entries (BibEntry). */
  var $builtdb  = array();

  /** A hashtable of constant strings */
  var $stringdb = array();

  var $filename;

  var $currentEntry;

  function setData(&$builtdb, &$stringdb) {
    $this->builtdb = $builtdb;
    $this->stringdb = $stringdb;
    return $this;
  }

  function build($filename) {
    $this->filename = $filename;    
    $parser = new StateBasedBibtexParser($this);
    $parser->parse($filename);
    //print_r(array_keys(&$this->builtdb));
    //print_r(&$this->builtdb);
  }
  

  function getBuiltDb() {
    //print_r($this->builtdb);
    return $this->builtdb;
  }

  function beginFile() {
  }

  function endFile() {
    // resolving crossrefs
    // we are careful with PHP 4 semantics 
    foreach (array_keys($this->builtdb) as $key) {
      $bib = &$this->builtdb[$key];
      if ($bib->hasField('crossref')) {
        if (isset($this->builtdb[$bib->getField('crossref')])) {
          $crossrefEntry = $this->builtdb[$bib->getField('crossref')];
          $bib->crossref = $crossrefEntry;
          foreach($crossrefEntry->getFields() as $k => $v) {
            // copying the fields of the cross ref
            // only if they don't exist yet
            if (!$bib->hasField($k)) {
              $bib->setField($k,$v);
            }
          }
        }
      }
    }
    //print_r($this->builtdb);
  }

  function setEntryField($finalkey,$entryvalue) {
    // is it a constant? then we replace the value
    // we support advanced features of bibtexbrowser
    // see http://newton.ex.ac.uk/tex/pack/bibtex/btxdoc/node3.html
    $entryvalue_array=explode('#',$entryvalue);
    foreach ($entryvalue_array as $k=>$v) {
      // spaces are allowed when using #, they are not taken into account
      // however # is not istself replaced by a space
      // warning: @strings are not case sensitive
      // see http://newton.ex.ac.uk/tex/pack/bibtex/btxdoc/node3.html
      $stringKey=strtolower(trim($v));
      if (isset($this->stringdb[$stringKey]))
      {
        // this field will be formated later by xtrim and latex2html
        $entryvalue_array[$k]=$this->stringdb[$stringKey];

        // we keep a trace of this replacement
        // so as to produce correct bibtex snippets
        $this->currentEntry->constants[$stringKey]=$this->stringdb[$stringKey];
      }
    }
    $entryvalue=implode('',$entryvalue_array);

    $this->currentEntry->setField($finalkey,$entryvalue);
  }

  function setEntryType($entrytype) {
    $this->currentEntry->setType($entrytype);
  }

  function setEntryKey($entrykey) {
    //echo "new entry:".$entrykey."\n";
    $this->currentEntry->setKey($entrykey);
  }

  function beginEntry() {
    $this->currentEntry = createBibEntry();
    $this->currentEntry->setFile($this->filename);
  }

  function endEntry($entrysource) {
  
    // we add a timestamp
    $this->currentEntry->setTimestamp();
    
    // we set the fulltext
    $this->currentEntry->text = $entrysource;
    
    // we format the author names in a special field
    // to enable search
    if ($this->currentEntry->hasField('author')) {
      $this->currentEntry->setField('_author',$this->currentEntry->getFormattedAuthorsImproved());
    }

    // ignoring jabref comments
    if (($this->currentEntry->getType()=='comment')) {
      /* do nothing for jabref comments */
    } 
    
    // we add it to the string database
    else if ($this->currentEntry->getType()=='string') {
      foreach($this->currentEntry->fields as $k => $v) {
        $k!='type' and $this->stringdb[$k]=$v;
      }
    } 
    
    // we add it to the database
    else {
      $this->builtdb[$this->currentEntry->getKey()] = $this->currentEntry;      
    }
  }
} // end class BibDBBuilder




/** is an extended version of the trim function, removes linebreaks, tabs, etc.
 */
function xtrim($line) {
  $line = trim($line);
  // we remove the unneeded line breaks
  // this is *required* to correctly split author lists into names
  // 2010-06-30
  // bug found by Thomas
  // windows new line is **\r\n"** and not the other way around!!
  $line = str_replace("\r\n",' ', $line);//windows like
  $line = str_replace("\n",' ', $line);//unix-like
  // we also replace tabs
  $line = str_replace("\t",' ', $line);
  // remove superfluous spaces e.g. John+++Bar
  $line = preg_replace('/ {2,}/',' ', $line);
  return $line;
}

/** encapsulates the conversion of a single latex chars to the corresponding HTML entity.
It expects a **lower-case** char.
*/
function char2html($line,$latexmodifier,$char,$entitiyfragment) {
  $line = str_replace('\\'.$latexmodifier.$char,'&'.$char.''.$entitiyfragment.';', $line);
  $line = str_replace('\\'.$latexmodifier.'{'.$char.'}','&'.$char.''.$entitiyfragment.';', $line);
  $line = str_replace('\\'.$latexmodifier.strtoupper($char),'&'.strtoupper($char).''.$entitiyfragment.';', $line);
  $line = str_replace('\\'.$latexmodifier.'{'.strtoupper($char).'}','&'.strtoupper($char).''.$entitiyfragment.';', $line);
  return $line;
}

/** converts latex chars to HTML entities.
(I still look for a comprehensive translation table from late chars to html, better than [[http://isdc.unige.ch/Newsletter/help.html]])
 */
function latex2html($line) {
  $line = preg_replace('/([^\\\\])~/','\\1&nbsp;', $line);

  // performance increases with this test
  // bug found by Serge Barral: what happens if we have curly braces only (typically to ensure case in Latex)
  // added && strpos($line,'{')===false
  if (strpos($line,'\\')===false && strpos($line,'{')===false) return $line;
  
  // we should better replace this before the others
  // in order not to mix with the HTML entities coming after (just in case)
  $line = str_replace('\\&','&amp;', $line);

  // handling \url{....}
  // often used in howpublished for @misc
  $line = preg_replace('/\\\\url\{(.*)\}/U','<a href="\\1">\\1</a>', $line);

  // Friday, April 01 2011
  // added support for accented i
  // for instance \`\i
  // see http://en.wikibooks.org/wiki/LaTeX/Accents
  // " the letters "i" and "j" require special treatment when they are given accents because it is often desirable to replace the dot with the accent. For this purpose, the commands \i and \j can be used to produce dotless letters."
  $line = preg_replace('/\\\\([ij])/i','\\1', $line);

  $line = char2html($line,"'",'a',"acute");
  $line = char2html($line,"'",'e',"acute");
  $line = char2html($line,"'",'i',"acute");
  $line = char2html($line,"'",'o',"acute");
  $line = char2html($line,"'",'u',"acute");
  $line = char2html($line,"'",'y',"acute");
  $line = char2html($line,"'",'n',"acute");
  
  $line = char2html($line,'`','a',"grave");
  $line = char2html($line,'`','e',"grave");
  $line = char2html($line,'`','i',"grave");
  $line = char2html($line,'`','o',"grave");
  $line = char2html($line,'`','u',"grave");

  $line = char2html($line,'~','a',"tilde");
  $line = char2html($line,'~','n',"tilde");
  $line = char2html($line,'~','o',"tilde");

  $line = char2html($line,'"','a',"uml");
  $line = char2html($line,'"','e',"uml");
  $line = char2html($line,'"','i',"uml");
  $line = char2html($line,'"','o',"uml");
  $line = char2html($line,'"','u',"uml");
  $line = char2html($line,'"','y',"uml");

  $line = char2html($line,'^','a',"circ");
  $line = char2html($line,'^','e',"circ");
  $line = char2html($line,'^','i',"circ");
  $line = char2html($line,'^','o',"circ");
  $line = char2html($line,'^','u',"circ");

  $line = char2html($line,'.','a',"ring");

  $line = char2html($line,'c','c',"cedil");

  $line = str_replace('\\ae','&aelig;', $line);
  $line = str_replace('\\ss','&szlig;', $line);

  $line = str_replace('\\o','&oslash;', $line);
  $line = str_replace('\\O','&Oslash;', $line);
  $line = str_replace('\\aa','&aring;', $line);
  $line = str_replace('\\AA','&Aring;', $line);

  // clean out extra tex curly brackets, usually used for preserving capitals
  $line = str_replace('}','', $line);
  $line = str_replace('{','', $line);

  return $line;
}

/** encodes strings for Z3988 URLs. Note that & are encoded as %26 and not as &amp. */
function s3988($s) {return urlencode(utf8_encode($s));}

/**
see BibEntry->formatAuthor($author) 
@deprecated 
@nodoc
*/
function formatAuthor() {
  die('Sorry, this function does not exist anymore, however, you can simply use $bibentry->formatAuthor($author) instead.');
}

// ----------------------------------------------------------------------
// BIB ENTRIES
// ----------------------------------------------------------------------

/** represents a bibliographic entry.
usage:
<pre>
  $db = zetDB('metrics.bib');
  $entry = $db->getEntryByKey('Schmietendorf2000');
  echo bib2html($entry);
</pre>
notes:
- BibEntry are usually obtained with getEntryByKey or multisearch
*/
class BibEntry {

  /** The fields (fieldName -> value) of this bib entry. */
  var $fields;

  /** The constants @STRINGS referred to by this entry */
  var $constants;

  /** The crossref entry if there is one */
  var $crossref;

  /** The verbatim copy (i.e., whole text) of this bib entry. */
  var $text;

  /** A timestamp to trace when entries have been created */
  var $timestamp;
  
  /** The name of the file containing this entry */
  var $filename;
  
  /** The short name of the entry (parameterized by ABBRV_TYPE) */
  var $abbrv;
  
  /** The index in a list of publications (e.g. [1] Foo */
  var $index = '';
  
  /** Creates an empty new bib entry. Each bib entry is assigned a unique
   * identification number. */
  function BibEntry() {
    $this->fields = array();
    $this->constants = array();
    $this->text ='';
  }

  /** Sets the name of the file containing this entry */
  function setFile($filename) {  
    $this->filename = $filename;
    return $this;
  }
  
  /** Adds timestamp to this object */
  function setTimestamp() {
    $this->timestamp = time();
  }
  /** Returns the timestamp of this object */
  function getTimestamp() {
    return $this->timestamp;
  }

  /** Returns the type of this bib entry. */
  function getType() {
    // strtolower is important to be case-insensitive
    return strtolower($this->getField(Q_TYPE));
  }

  /** Sets the key of this bib entry. */
  function setKey($value) {
    $this->setField('key',$value);
  }

  /** Sets a field of this bib entry. */
  function setField($name, $value) {
    $name = strtolower($name);
    // fields that should not be transformed
    // we assume that "comment" is never latex code
    // but instead could contain HTML code (with links using the character "~" for example)
    // so "comment" is not transformed too
    if ($name!='url' && $name!='comment') { 
      $value = xtrim($value); 
      $value = latex2html($value);
    } else {
      //echo "xx".$value."xx\n";
    }
    
    $this->fields[$name] = $value;
  }

  /** Sets a type of this bib entry. */
  function setType($value) {
    // 2009-10-25 added trim
    // to support space e.g. "@article  {"
    // as generated by ams.org
    // thanks to Jacob Kellner 
    $this->fields[Q_TYPE] =trim($value);
  }
  
  function setIndex($index) { $this->index = $index; }

  /** Tries to build a good URL for this entry */
  function getURL() {
    if (defined('BIBTEXBROWSER_URL_BUILDER')) {
      $f = BIBTEXBROWSER_URL_BUILDER;
      return $f($this);
    }
//     echo $this->filename;
//     echo $this->getKey();
    return BIBTEXBROWSER_URL.'?'.createQueryString(array('key'=>$this->getKey(), Q_FILE=>$this->filename));
  }

  /** Tries to build a good absolute URL for this entry */
  function getAbsoluteURL() {
    if (defined('BIBTEXBROWSER_URL_BUILDER')) {
      $f = BIBTEXBROWSER_URL_BUILDER;
      return $f($this);
    }
    return "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/'.$this->getURL();
  }

  /** returns a "[pdf]" link if relevant */
  function getUrlLink() {
    if ($this->hasField('url')) return ' <a'.(BIBTEXBROWSER_BIB_IN_NEW_WINDOW?' target="_blank" ':'').' href="'.$this->getField('url').'">[pdf]</a>';
    return '';
  }

  /** Reruns the abstract */
  function getAbstract() {
    if ($this->hasField('abstract')) return $this->getField('abstract');
    else return '';
  }

  /**
    * Returns the last name of an author name.
    */
  function getLastName($author){
      list($firstname, $lastname) = splitFullName($author);
      return $lastname;
  }


  /** Has this entry the given field? */
  function hasField($name) {
    return isset($this->fields[strtolower($name)]);
  }

  /** Returns the authors of this entry. If "author" is not given,
   * return a string 'Unknown'. */
  function getAuthor() {
    if (array_key_exists(AUTHOR, $this->fields)) {
      return $this->fields[AUTHOR];
    }
    // 2010-03-02: commented the following, it results in misleading author lists
    // issue found by Alan P. Sexton
    //if (array_key_exists(EDITOR, $this->fields)) {
    //  return $this->fields[EDITOR];
    //}
    return 'Unknown';
  }

  /** Returns the key of this entry */
  function getKey() {
    return $this->getField('key');
  }

  /** Returns the title of this entry? */
  function getTitle() {
    return $this->getField('title');
  }

   /** Returns the publisher of this entry
    * It encodes a specific logic
    * */
  function getPublisher() {
    // citation_publisher
    if ($this->hasField("publisher")) {
      return $this->getField("publisher");
    }
    if ($this->getType()=="phdthesis") {
      return $this->getField(SCHOOL);
    }
    if ($this->getType()=="mastersthesis") {
      return $this->getField(SCHOOL);
    }
    if ($this->getType()=="bachelorsthesis") {
      return $this->getField(SCHOOL);
    }
    if ($this->getType()=="techreport") {
      return $this->getField("institution");
    }
    // then we don't know
    return '';
  }

  /** Returns the authors of this entry as an array */
  function getRawAuthors() {
    $authors = array();
    foreach (preg_split('/ and /i', $this->getAuthor()) as $author) {
      $authors[]=$author;
    }
    return $authors;
  }
  
  /**
  * Returns the formated author name w.r.t to the user preference encoded in COMMA_NAMES
  */
  function formatAuthor($author){
    if (COMMA_NAMES) {
      return $this->formatAuthorCommaSeparated($author);
    }
    else return $this->formatAuthorCanonical($author);
  }

  /**
  * Returns the formated author name as "FirstName LastName".
  */
  function formatAuthorCanonical($author){
      list($firstname, $lastname) = splitFullName($author);
      if ($firstname!='') return $firstname.' '.$lastname;
      else return $lastname;
  }

  /**
  * Returns the formated author name as "LastName, FirstName".
  */
  function formatAuthorCommaSeparated($author){
      list($firstname, $lastname) = splitFullName($author);
      if ($firstname!='') return $lastname.', '.$firstname;
      else return $lastname;
  }


  /** Returns the authors as a string depending on the configuration parameter COMMA_NAMES  */
  function getFormattedAuthors() {
    $authors = array();
    foreach ($this->getRawAuthors() as $author) {
      $authors[]=$this->formatAuthor($author);
    }
    return $authors;
  }

  /** @deprecated
  *   @see getFormattedAuthorsImproved()
  */
  function formattedAuthors() {  return $this->getFormattedAuthorsImproved(); }
  
  /** Adds to getFormattedAuthors() the home page links and returns a string (not an array)
  */
  function getFormattedAuthorsImproved() {
    $array_authors = $this->getFormattedAuthors();
    foreach ($array_authors as $k => $author) {
      $array_authors[$k]=$this->addHomepageLink($author);
    }
    
    if (COMMA_NAMES) {$sep = '; ';} else {$sep = ', ';}
    return implode($sep ,$array_authors);
  }

  /** Returns the authors of this entry as an array in a canonical form */
  function getCanonicalAuthors() {
    $authors = array();
    foreach ($this->getRawAuthors() as $author) {
      $authors[]=$this->formatAuthorCanonical($author);
    }
    return $authors;
  }

  /** Returns the authors of this entry as an array in a comma-separated form */
  function getArrayOfCommaSeparatedAuthors() {
    $authors = array();
    foreach ($this->getRawAuthors() as $author) {
      $authors[]=$this->formatAuthorCommaSeparated($author);
    }
    return $authors;
  }

  /**
  * Returns a compacted string form of author names by throwing away
  * all author names except for the first one and appending ", et al."
  */
  function getCompactedAuthors($author){
    $authors = $this->getAuthors();
    $etal = count($authors) > 1 ? ', et al.' : '';
    return $this->formatAuthor($authors[0]) . $etal;
  }


  /** add the link to the homepage if it is defined in a string
   *  e.g. @string{hp_MartinMonperrus="http://www.monperrus.net/martin"}
   *  The string is a concatenation of firstname, lastname, prefixed by hp_ 
   * Warning: by convention @string are case sensitive so please be keep the same case as author names
   * @thanks Eric Bodden for the idea
   */
  function addHomepageLink($author) {
    // hp as home page
    // accents are handled normally
    // e.g. @STRING{hp_Jean-MarcJĂŠzĂŠquel="http://www.irisa.fr/prive/jezequel/"}
    $homepage = strtolower('hp_'.preg_replace('/ /', '', $author));
    if (isset($_GET[Q_DB]->stringdb[$homepage]))
      $author='<a href="'.$_GET[Q_DB]->stringdb[$homepage].'">'.$author.'</a>';
    return $author;
  }


  /** Returns the editors of this entry as an arry */
  function getEditors() {
    $editors = array();
    foreach (preg_split('/ and /i', $this->getField(EDITOR)) as $editor) {
      $editors[]=$editor;
    }
    return $editors;
  }

  /** Returns the editors of this entry as an arry */
  function getFormattedEditors() {
    $editors = array();
    foreach ($this->getEditors() as $editor) {
      $editors[]=$this->addHomepageLink($this->formatAuthor($editor));
    }
    if (COMMA_NAMES) {$sep = '; ';} else {$sep = ', ';}
    return implode($sep, $editors).', '.(count($editors)>1?'eds.':'ed.');
  }


  /** Returns the year of this entry? */
  function getYear() {
    return $this->getField('year');
  }

  /** Returns the value of the given field? */
  function getField($name) {
    // 2010-06-07: profiling showed that this is very costly
    // hence returning the value directly    
    //if ($this->hasField($name))
    //    {return $this->fields[strtolower($name)];}
    //else return 'missing '.$name;
    
    return @$this->fields[strtolower($name)];
  }



  /** Returns the fields */
  function getFields() {
    return $this->fields;
  }
    
  /** Returns the abbreviation. */
  function getAbbrv() {
    if (ABBRV_TYPE == 'index') return $this->index;
    if (ABBRV_TYPE == 'none') return '';
    if (ABBRV_TYPE == 'key') return '['.$this->getKey().']';
    if (ABBRV_TYPE == 'year') return '['.$this->getYear().']';
    if (ABBRV_TYPE == 'x-abbrv') {
      if ($this->hasField('x-abbrv')) {return $this->getField('x-abbrv');}
      return $this->abbrv;
    }
    
    // otherwise it is a user-defined function in bibtexbrowser.local.php
    $f = ABBRV_TYPE;
    return $f($this);
  }


  /** Sets the abbreviation (e.g. [OOPSLA] or [1]) */
  function setAbbrv($abbrv) {
    //if (!is_string($abbrv)) { throw new Exception('Illegal argument'); }
    $this->abbrv = $abbrv;
    return $this;
  }

  function getText() {
  /** Returns the verbatim text of this bib entry. */
    return $this->text;
  }
 
  /** Returns true if this bib entry contains the given phrase
   * in the given field. if $field is null, all fields are considered.
   * Note that this method is NOT case sensitive */
  function hasPhrase($phrase, $field = null) {
  
    // 2010-01-25
    // bug found by jacob kellner
    // we have to search in the formatted fileds and not in the raw entry
    // i.e. all latex markups are not considered for searches
    // i.e. added join(" ",$this->getFields())
    // and html_entity_decode
    if (!$field) {
      // warning html_entity_decode supports encoding since PHP5
      return preg_match('/'.$phrase.'/i',$this->getConstants().' '.@html_entity_decode(join(" ",$this->getFields()),ENT_NOQUOTES,ENCODING));
      //return stripos($this->getText(), $phrase) !== false;
    }
    if ($this->hasField($field) &&  (preg_match('/'.$phrase.'/i',$this->getField($field)) ) ) {
    //if ($this->hasField($field) &&  (stripos($this->getField($field), $phrase) !== false) ) {
      return true;
    }

    return false;
  }


  /** Outputs an HTML line (<tr>)with two TDS inside
  */
  function toTR() {
        echo '<tr class="bibline">';
        echo '<td  class="bibref">';
        //echo '<a name="'.$this->getId().'"></a>';
 
        echo $this->getAbbrv().'</td> ';
        echo '<td class="bibitem">';
        echo bib2html($this);

        $href = 'href="'.$this->getURL().'"';

        if (BIBTEXBROWSER_BIBTEX_LINKS) {
          // we add biburl and title to be able to retrieve this important information
          // using Xpath expressions on the XHTML source
          echo " <a".(BIBTEXBROWSER_BIB_IN_NEW_WINDOW?' target="_blank" ':'')." class=\"biburl\" title=\"".$this->getKey()."\" {$href}>[bibtex]</a>";
        }
        
        // returns an empty string if no url present
        echo $this->getUrlLink();

        if ($this->hasField('doi')) {
            echo ' <a href="http://dx.doi.org/'.$this->getField("doi").'">[doi]</a>';
        }
        
        // Google Scholar ID
        if ($this->hasField('gsid')) {
            echo ' <a href="http://scholar.google.com/scholar?cites='.$this->getField("gsid").'">[cites]</a>';
        }
        echo "</td></tr>\n";
  }

  /** Outputs an coins URL: see http://ocoins.info/cobg.html
   * Used by Zotero, mendeley, etc.
  */
  function toCoins() {
    $url_parts=array();
    $url_parts[]='ctx_ver=Z39.88-2004';

    $type = $this->getType();
    if ($type=="book") {
      $url_parts[]='rft_val_fmt='.s3988('info:ofi/fmt:kev:mtx:book');
      $url_parts[]='rft.btitle='.s3988($this->getTitle());
      $url_parts[]='rft.genre=book';
    } else if ($type=="inproceedings") {
      $url_parts[]='rft_val_fmt='.s3988('info:ofi/fmt:kev:mtx:book');
      $url_parts[]='rft.atitle='.s3988($this->getTitle());
      $url_parts[]='rft.btitle='.s3988($this->getField(BOOKTITLE));

      // zotero does not support with this proceeding and conference
      // they give the wrong title
      //$url_parts[]='rft.genre=proceeding';
      //$url_parts[]='rft.genre=conference';
      $url_parts[]='rft.genre=bookitem';
    } else if ($type=="incollection" ) {
      $url_parts[]='rft_val_fmt='.s3988('info:ofi/fmt:kev:mtx:book');
      $url_parts[]='rft.btitle='.s3988($this->getField(BOOKTITLE));
      $url_parts[]='rft.atitle='.s3988($this->getTitle());
      $url_parts[]='rft.genre=bookitem';
    } else if ($type=="article") {
      $url_parts[]='rft_val_fmt='.s3988('info:ofi/fmt:kev:mtx:journal');
      $url_parts[]='rft.atitle='.s3988($this->getTitle());
      $url_parts[]='rft.jtitle='.s3988($this->getField("journal"));
      $url_parts[]='rft.volume='.s3988($this->getField("volume"));
      $url_parts[]='rft.issue='.s3988($this->getField("issue"));
    } else { // techreport, phdthesis
      $url_parts[]='rft_val_fmt='.s3988('info:ofi/fmt:kev:mtx:book');
      $url_parts[]='rft.btitle='.s3988($this->getTitle());
      $url_parts[]='rft.genre=report';
    }

    $url_parts[]='rft.pub='.s3988($this->getPublisher());

    // referent
    if ($this->hasField('url')) {
      $url_parts[]='rft_id='.s3988($this->getField("url"));
    } else if ($this->hasField('doi')) {
      $url_parts[]='rft_id='.s3988('info:doi/'.$this->getField("doi"));
    }

    // referrer, the id pf a collection of objects
    // see also http://www.openurl.info/registry/docs/pdf/info-sid.pdf    
    $url_parts[]='rfr_id='.s3988('info:sid/'.$_SERVER['HTTP_HOST'].':'.@$_GET[Q_FILE]);

    $url_parts[]='rft.date='.s3988($this->getYear());

    foreach ($this->getFormattedAuthors() as $au) $url_parts[]='rft.au='.s3988($au);


    return '<span class="Z3988" title="'.implode('&amp;',$url_parts).'"></span>';

  }



   /**
   * rebuild the set of constants used if any as a string
   */
  function getConstants() {
    $result='';
    foreach ($this->constants as $k=>$v) {
      $result.='@string{'.$k.'="'.$v."\"}\n";
    }
    return $result;
  }

   /**
   * Displays a <pre> text of the given bib entry.
   * URLs are replaced by HTML links.
   */
  function toEntryUnformatted() {
    $result = "";
    $result .= '<pre class="purebibtex">'; // pre is nice when it is embedded with no CSS available
    $entry = str_replace('<','&lt;',$this->getFullText());
    if ($this->hasField('url')) {
      $url = $this->getField('url');
      // this is not a parsing but a simple replacement
      $entry = str_replace($url,'<a href="'.$url.'">'.$url.'</a>', $entry);
    }

    $result .=  $entry;
    $result .=  '</pre>';
    return $result;
   }

   /**
   * Gets the raw text of the entry (crossref + strings + entry)
   */
  function getFullText() {
    $s = '';
    // adding the crossref if necessary
    if ($this->crossref!=null) { $s .= $this->crossref->getFullText()."\n";}
    $s.=$this->getConstants();
    $s.=$this->getText();
    return $s;
  }
}


/** this function encapsulates the user-defined name for bib to HTML*/
function bib2html(&$bibentry) {
  $function = BIBLIOGRAPHYSTYLE;
  return $function($bibentry);
}

/** encapsulates the user-defined sections. @nodoc */
function _DefaultBibliographySections() {
  $function = BIBLIOGRAPHYSECTIONS;
  return $function();
}

/** compares two instances of BibEntry by modification time
 */
function compare_bib_entry_by_mtime($a, $b)
{
  return -($a->getTimestamp()-$b->getTimestamp());
}

/** compares two instances of BibEntry by year
 */
function compare_bib_entry_by_year_and_month($a, $b)
{
  $year_cmp =  -strcmp($a->getYear(),$b->getYear());
  if ($year_cmp==0) { return compare_bib_entry_by_month($a, $b);}
  return $year_cmp;
}

/** compares two instances of BibEntry by title
 */
function compare_bib_entry_by_title($a, $b)
{
  return strcmp($a->getTitle(),$b->getTitle());
}


/** compares two instances of BibEntry by month 
 * @author Jan Geldmacher
 */
function compare_bib_entry_by_month($a, $b)
{  
  // this was the old behavior
  // return strcmp($a->getKey(),$b->getKey());
  
  //bibkey which is used for sorting
  $sort_key = 'month';
  //desired order of values
  $sort_order_values = array('jan','january','feb','february','mar','march','apr','april','may','jun','june','jul','july','aug','august','sep','september','oct','october','nov','november','dec','december');
  //order: 1=as specified in $sort_order_values  or -1=reversed
  $order = -1;


  //first check if the search key exists
  if (!array_key_exists($sort_key,$a->fields)  && !array_key_exists($sort_key,$b->fields)) {
    //neither a nor b have the key -> we compare the keys
    $retval=strcmp($a->getKey(),$b->getKey());
  }
  elseif (!array_key_exists($sort_key,$a->fields)) {
    //only b has the field -> b is greater
    $retval=-1;
  }
  elseif  (!array_key_exists($sort_key,$b->fields)) {
    //only a has the key -> a is greater
    $retval=1;
  }
  else {
    //both have the key, sort using the order given in $sort_order_values

    $val_a = array_search(strtolower($a->fields[$sort_key]), $sort_order_values);
    $val_b = array_search(strtolower($b->fields[$sort_key]), $sort_order_values);
    
    if (($val_a === FALSE && $val_b === FALSE) || ($val_a === $val_b)) {
      //neither a nor b are in the search array or a=b -> both are equal
      $retval=0;
    }
    elseif (($val_a === FALSE) || ($val_a < $val_b)) {
      //a is not in the search array or a<b -> b is greater
      $retval=-1;
    }
    elseif (($val_b === FALSE) || (($val_a > $val_b))){
      //b is not in the search array or a>b -> a is greater
      $retval=1;
    }   
  }
 
  return $order*$retval;
}

/** is the default sectioning for AcademicDisplay (books, articles, proceedings, etc. ) */
function DefaultBibliographySections() {
return  
  array(
  // Books
    array(
      'query' => array(Q_TYPE=>'book'),
      'title' => 'Books'
    ),
  // Journal / Bookchapters
    array(
      'query' => array(Q_TYPE=>'article|incollection'),
      'title' => 'Refereed Articles and Book Chapters'
    ),
  // conference papers
    array(
      'query' => array(Q_TYPE=>'inproceedings|conference',Q_EXCLUDE=>'workshop'),
      'title' => 'Refereed Conference Papers'
    ),
  // workshop papers
    array(
      'query' => array(Q_TYPE=>'inproceedings',Q_SEARCH=>'workshop'),
      'title' => 'Refereed Workshop Papers'
    ),
  // misc and thesis
    array(
      'query' => array(Q_TYPE=>'misc|phdthesis|mastersthesis|bachelorsthesis|techreport'),
      'title' => 'Other Publications'
    )
  );
}


/** transforms a $bibentry into an HTML string.
  It is called by function bib2html if the user did not choose a specific style
  the default usable CSS styles are
  .bibtitle { font-weight:bold; }
  .bibbooktitle { font-style:italic; }
  .bibauthor { }
  .bibpublisher { }
*/
function DefaultBibliographyStyle(&$bibentry) {
  $title = $bibentry->getTitle();
  $type = $bibentry->getType();

  // later on, all values of $entry will be joined by a comma
  $entry=array();

  // title
  // usually in bold: .bibtitle { font-weight:bold; }
  $title = '<span class="bibtitle">'.$title.'</span>';
  if ($bibentry->hasField('url')) $title = ' <a'.(BIBTEXBROWSER_BIB_IN_NEW_WINDOW?' target="_blank" ':'').' href="'.$bibentry->getField("url").'">'.$title.'</a>';
  

  // author
  if ($bibentry->hasField('author')) {
    $coreInfo = $title . ' <span class="bibauthor">('.$bibentry->getFormattedAuthorsImproved().')</span>';}
  else $coreInfo = $title;

  // core info usually contains title + author
  $entry[] = $coreInfo;

  // now the book title
  $booktitle = '';
  if ($type=="inproceedings") {
      $booktitle = 'In '.$bibentry->getField(BOOKTITLE); }
  if ($type=="incollection") {
      $booktitle = 'Chapter in '.$bibentry->getField(BOOKTITLE);}
  if ($type=="inbook") {
      $booktitle = 'Chapter in '.$bibentry->getField('chapter');}
  if ($type=="article") {
      $booktitle = 'In '.$bibentry->getField("journal");}

  //// we may add the editor names to the booktitle
  $editor='';
  if ($bibentry->hasField(EDITOR)) {
    $editor = $bibentry->getFormattedEditors();
  }
  if ($editor!='') $booktitle .=' ('.$editor.')';
  // end editor section

  // is the booktitle available
  if ($booktitle!='') {
    $entry[] = '<span class="bibbooktitle">'.$booktitle.'</span>';
  }


  $publisher='';
  if ($type=="phdthesis") {
      $publisher = 'PhD thesis, '.$bibentry->getField(SCHOOL);
  }
  if ($type=="mastersthesis") {
      $publisher = 'Master\'s thesis, '.$bibentry->getField(SCHOOL);
  }
  if ($type=="bachelorsthesis") {
      $publisher = 'Bachelor\'s thesis, '.$bibentry->getField(SCHOOL);
  }
  if ($type=="techreport") {
      $publisher = 'Technical report, '.$bibentry->getField("institution");
  }
  
  if ($type=="misc") {
      $publisher = $bibentry->getField('howpublished');
  }

  if ($bibentry->hasField("publisher")) {
    $publisher = $bibentry->getField("publisher");
  }

  if ($publisher!='') $entry[] = '<span class="bibpublisher">'.$publisher.'</span>';


  if ($bibentry->hasField('volume')) $entry[] =  "volume ".$bibentry->getField("volume");


  if ($bibentry->hasField(YEAR)) $entry[] = $bibentry->getYear();

  $result = implode(", ",$entry).'.';

  // some comments (e.g. acceptance rate)?
  if ($bibentry->hasField('comment')) {
      $result .=  " (".$bibentry->getField("comment").")";
  }
  if ($bibentry->hasField('note')) {
      $result .=  " (".$bibentry->getField("note").")";
  }

  // add the Coin URL
  $result .=  "\n".$bibentry->toCoins();

  return $result;
}



/** is the Bibtexbrowser style contributed by Janos Tapolcai. It looks like the IEEE transaction style.
usage:
Add the following line in "bibtexbrowser.local.php"
<pre>
include( 'bibtexbrowser-style-janos.php' );
define('BIBLIOGRAPHYSTYLE','JanosBibliographyStyle');
</pre>
*/
function JanosBibliographyStyle(&$bibentry) {
  $title = $bibentry->getTitle();
  $type = $bibentry->getType();

  $entry=array();

  // author
  if ($bibentry->hasField('author')) {
    $entry[] = $bibentry->formattedAuthors();
  }

  // title
  $title = '"'.$title.'"';
  if ($bibentry->hasField('url')) $title = ' <a'.(BIBTEXBROWSER_BIB_IN_NEW_WINDOW?' target="_blank" ':'').' href="'.$bibentry->getField("url").'">'.$title.'</a>';
  $entry[] = $title;


  // now the origin of the publication is in italic
  $booktitle = '';

  if (($type=="misc") && $bibentry->hasField("note")) {
    $booktitle = $bibentry->getField("note");
  }

  if ($type=="inproceedings") {
      $booktitle = 'In '.$bibentry->getField(BOOKTITLE);
  }

  if ($type=="incollection") {
      $booktitle = 'Chapter in '.$bibentry->getField(BOOKTITLE);
  }

  if ($type=="article") {
      $booktitle = 'In '.$bibentry->getField("journal");
  }



  //// ******* EDITOR
  $editor='';
  if ($bibentry->hasField(EDITOR)) {
    $editor = $bibentry->getFormattedEditors();
  }

  if ($booktitle!='') {
    if ($editor!='') $booktitle .=' ('.$editor.')';
    $entry[] = '<i>'.$booktitle.'</i>';
  }


  $publisher='';
  if ($type=="phdthesis") {
      $publisher = 'PhD thesis, '.$bibentry->getField(SCHOOL);
  }

  if ($type=="mastersthesis") {
      $publisher = 'Master\'s thesis, '.$bibentry->getField(SCHOOL);
  }
  if ($type=="techreport") {
      $publisher = 'Technical report, '.$bibentry->getField("institution");
  }
  if ($bibentry->hasField("publisher")) {
    $publisher = $bibentry->getField("publisher");
  }

  if ($publisher!='') $entry[] = $publisher;

  if ($bibentry->hasField('volume')) $entry[] =  "vol. ".$bibentry->getField("volume");
  if ($bibentry->hasField('number')) $entry[] =  'no. '.$bibentry->getField("number");

  if ($bibentry->hasField('address')) $entry[] =  $bibentry->getField("address");

  if ($bibentry->hasField('pages')) $entry[] = str_replace("--", "-", "pp. ".$bibentry->getField("pages"));


  if ($bibentry->hasField(YEAR)) $entry[] = $bibentry->getYear();

  $result = implode(", ",$entry).'.';

  // some comments (e.g. acceptance rate)?
  if ($bibentry->hasField('comment')) {
      $result .=  " (".$bibentry->getField("comment").")";
  }

  // add the Coin URL
  $result .=  "\n".$bibentry->toCoins();

  return $result;
}






// ----------------------------------------------------------------------
// DISPLAY MANAGEMENT
// ----------------------------------------------------------------------


/** creates a query string given an array of parameter, with all specifities of bibtexbrowser_ (such as adding the bibtex file name &bib=foo.bib
 */
function createQueryString($array_param) {
  if (isset($_GET[Q_FILE]) && !isset($array_param[Q_FILE])) {
    // first we add the name of the bib file
    $array_param[Q_FILE] = urlencode($_GET[Q_FILE]);
  }
 // then a simple transformation and implode
 foreach ($array_param as $key => $val) {
      if($key == '_author') { $key = 'author'; }
      $array_param[$key]=$key .'='. urlencode($val);
 }
 return implode("&amp;",$array_param);
}

/** returns a href string of the form: href="?bib=testing.bib&search=JML.
Based on createQueryString.
@nodoc
 */
function makeHref($query = NULL) {
  return 'href="?'. createQueryString($query) .'"';
}


/** returns the splitted name of an author name as an array. The argument is assumed to be
 "FirstName LastName" or "LastName, FirstName".
 */
function splitFullName($author){
    $author = trim($author);
    // the author format is "Joe Dupont"
    if (strpos($author,',')===false) {
      $parts=explode(' ', $author);
      // get the last name
      $lastname = array_pop($parts);
      $firstname = implode(" ", $parts);
    }
    // the author format is "Dupont, J."
    else {
      $parts=explode(',', $author);
      // get the last name
      $lastname = str_replace(',','',array_shift($parts));
      $firstname = implode(" ", $parts);
    }
  return array(trim($firstname), trim($lastname));
}


/** outputs an horizontal  year-based menu
usage:
<pre>
  $_GET['library']=1;
  $_GET['bib']='metrics.bib';
  $_GET['all']=1;
  include( 'bibtexbrowser.php' );
  setDB();
  new IndependentYearMenu();
</pre>
 */
class IndependentYearMenu  {
  function IndependentYearMenu() { 
    if (!isset($_GET[Q_DB])) {die('Did you forget to call setDB() before instantiating this class?');}
    $yearIndex = $_GET[Q_DB]->yearIndex();
    echo '<div id="yearmenu">Year: ';
    $formatedYearIndex = array();
    $formatedYearIndex[] = '<a '.makeHref(array(Q_YEAR=>'.*')).'>All</a>';    
    foreach($yearIndex as $year) {
      $formatedYearIndex[] = '<a '.makeHref(array(Q_YEAR=>$year)).'>'.$year.'</a>';
    }
    
    // by default the separator is a |
    echo implode('|',$formatedYearIndex);
    echo '</div>';
  }
}

/** Returns the powered by part. @nodoc */
function poweredby() {
  $poweredby = "\n".'<div style="text-align:right;font-size: xx-small;opacity: 0.6;" class="poweredby">';
  $poweredby .= '<!-- If you like bibtexbrowser, thanks to keep the link :-) -->';
  $poweredby .= 'Powered by <a href="http://www.monperrus.net/martin/bibtexbrowser/">bibtexbrowser</a><!--v20120703-->';
  $poweredby .= '</div>'."\n";
  return $poweredby;
  }


/** ^^adds a touch of AJAX in bibtexbrowser to display bibtex entries inline.
   It uses the HIJAX design pattern: the Javascript code fetches the normal bibtex HTML page
   and extracts the bibtex.
   In other terms, URLs and content are left perfectly optimized for crawlers 
   Note how beautiful is this piece of code thanks to JQuery.^^
  */
function javascript() {
  // we use jquery with the official content delivery URLs
  // Microsoft and Google also provide jquery with their content delivery networks
?><script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script> 
<script type="text/javascript" ><!--
// Javascript progressive enhancement for bibtexbrowser
$('a.biburl').each(function() { // for each url "[bibtex]"
  var biburl = $(this);
  if (biburl.attr('bibtexbrowser') === undefined)
  {
  biburl.click(function(ev) { // we change the click semantics
    ev.preventDefault(); // no open url
    if (biburl.nextAll('pre').length == 0) { // we don't have yet the bibtex data
      var bibtexEntryUrl = $(this).attr('href');
      $.ajax({url: bibtexEntryUrl,  dataType: 'xml', success: function (data) { // we download it
        // elem is the element containing bibtex entry, creating a new element is required for Chrome and IE
        var elem = $('<pre class="purebibtex"/>');         
        elem.text($('.purebibtex', data).text()); // both text() are required for IE
        // we add a link so that users clearly see that even with AJAX
        // there is still one URL per paper (which is important for crawlers and metadata)
        elem.append(
           $('<div>%% Bibtex entry URL: <a href="'+bibtexEntryUrl+'">'+bibtexEntryUrl+'</a></div>')
           ).appendTo(biburl.parent());
      }, error: function() {window.location.href = biburl.attr('href');}});
    } else {biburl.nextAll('pre').toggle();}  // we toggle the view    
  });
  biburl.attr('bibtexbrowser','done');
  } // end if biburl.bibtexbrowser;
});


--></script><?php
} // end function javascript



/** is used for creating menus (by type, by year, by author, etc.).
usage:
<pre>
  $db = zetDB('metrics.bib');
  $menu = new MenuManager();
  $menu->setDB($db);
  $menu->year_size=100;// should display all years :)
  $menu->display();
</pre>
 */
class MenuManager {

  /** The bibliographic database, an instance of class BibDataBase. */
  var $db;

  var $type_size = TYPES_SIZE;
  var $year_size = YEAR_SIZE;
  var $author_size = AUTHORS_SIZE;
  var $tag_size = TAGS_SIZE;
  
  function MenuManager() {
  }
  
  /** sets the database that is used to create the menu */
  function setDB(&$db) {
    $this->db =$db;
    return $this;
  }

  function getTitle() {
    return '';
  } 
  
  
  /** function called back by HTMLWrapper */
  function display() {
  echo $this->searchView().'<br/>';
  echo $this->typeVC().'<br/>';
  echo $this->yearVC().'<br/>';
  echo $this->authorVC().'<br/>';
  echo $this->tagVC().'<br/>';
  }

  /** Displays the title in a table. */
  function titleView() {
    ?>
    <table>
      <tr>
        <td class="title">Generated from <?php echo $_GET[Q_FILE]; ?></td>
      </tr>
    </table>
    <?php
  }

  /** Displays the search view in a form. */
  function searchView() {
    ?>
    <form action="?" method="get" target="<?php echo BIBTEXBROWSER_MENU_TARGET;?>">
      <input type="text" name="<?php echo Q_SEARCH; ?>" class="input_box" size="18"/>
      <input type="hidden" name="<?php echo Q_FILE; ?>" value="<?php echo $_GET[Q_FILE]; ?>"/>
      <br/>
      <input type="submit" value="search" class="input_box"/>
    </form>
    <?php
  }

  /** Displays and controls the types menu in a table. */
  function typeVC() {
    $types = array();
    foreach ($this->db->getTypes() as $type) {
      $types[$type] = $type;
    }
    $types[''] = 'all types';
    // retreive or calculate page number to display
    if (isset($_GET[Q_TYPE_PAGE])) {
      $page = $_GET[Q_TYPE_PAGE];
    }
    else $page = 1;

    $this->displayMenu('Types', $types, $page, $this->type_size, Q_TYPE_PAGE, Q_TYPE);
  }

  /** Displays and controls the authors menu in a table. */
  function authorVC() {
    // retrieve authors list to display
      $authors = $this->db->authorIndex();

    // determine the authors page to display
    if (isset($_GET[Q_AUTHOR_PAGE])) {
      $page = $_GET[Q_AUTHOR_PAGE];
    }
    else $page = 1;


    $this->displayMenu('Authors', $authors, $page, $this->author_size, Q_AUTHOR_PAGE,
         Q_AUTHOR);
  }

  /** Displays and controls the tag menu in a table. */
  function tagVC() {
    // retrieve authors list to display
      $tags = $this->db->tagIndex();

    // determine the authors page to display
    if (isset($_GET[Q_TAG_PAGE])) {
      $page = $_GET[Q_TAG_PAGE];
    }  else $page = 1;


    if (count($tags)>0) $this->displayMenu('Keywords', $tags, $page, $this->tag_size, Q_TAG_PAGE,
         Q_TAG);
  }

  /** Displays and controls the tag menu in a table. */
  function yearVC() {
    // retrieve authors list to display
      $years = $this->db->yearIndex();

    // determine the authors page to display
    if (isset($_GET[Q_YEAR_PAGE])) {
      $page = $_GET[Q_YEAR_PAGE];
    }
else $page = 1;


    $this->displayMenu('Years', $years, $page, $this->year_size, Q_YEAR_PAGE,
         Q_YEAR);
  }

  /** Displays the main contents . */
  function mainVC() {
      $this->display->display();
  }

  /** Displays a list menu in a table.
   *
   * $title: title of the menu (string)
   * $list: list of menu items (string)
   * $page: page number to display (number)
   * $pageSize: size of each page
   * $pageKey: URL query name to send the page number to the server
   * $targetKey: URL query name to send the target of the menu item
   */
  function displayMenu($title, $list, $page, $pageSize, $pageKey,
         $targetKey) {
    $numEntries = count($list);
    $startIndex = ($page - 1) * $pageSize;
    $endIndex = $startIndex + $pageSize;
    ?>
    <table style="width:100%"  class="menu">
      <tr>
        <td>
        <!-- this table is used to have the label on the left 
        and the navigation links on the right -->
        <table style="width:100%" border="0" cellspacing="0" cellpadding="0">
          <tr class="btb-nav-title">
            <td><b><?php echo $title; ?></b></td>
            <td class="btb-nav"><b>
                <?php echo $this->menuPageBar($pageKey, $numEntries, $page,
           $pageSize, $startIndex, $endIndex);?></b></td>
          </tr>
        </table>
        </td>
      </tr>
      <tr>
        <td class="btb-menu-items">
          <?php $this->displayMenuItems($list, $startIndex, $endIndex,
     $targetKey); ?>
        </td>
      </tr>
    </table>
  <?php
  }

  /** Returns a string to displays forward and reverse page controls.
   *
   * $queryKey: key to send the page number as a URL query string
   * $page: current page number to display
   * $numEntries: number of menu items
   * $start: start index of the current page
   * $end: end index of the current page
   */
  function menuPageBar($queryKey, $numEntries, $page, $pageSize,
         $start, $end) {

    $result = '';

    // (1 page) reverse (<)
    if ($start > 0) {
      $href = makeHref(array($queryKey => $page - 1,'menu'=>''));//menuPageBar
      $result .= '<a '. $href ."><b>[prev]</b></a>\n";
    }

    // (1 page) forward (>)
    if ($end < $numEntries) {
      $href = makeHref(array($queryKey => $page + 1,'menu'=>''));//menuPageBar
      $result .= '<a '. $href ."><b>[next]</b></a>\n";
    }

    return $result;
  }

  /**
   * Displays menu items (anchors) from the start index (inclusive) to
   * the end index (exclusive). For each menu, the following form of
   * string is printed:
   *
   * <a href="...?bib=cheon.bib&search_author=Yoonsik+Cheon">
   *    Cheon, Yoonsik</a>
   * <div class="mini_se"></div>
   */
  function displayMenuItems($items, $startIndex, $endIndex, $queryKey) {
    $index = 0;
    foreach ($items as $key => $item) {
      if ($index >= $startIndex && $index < $endIndex) {
 $href = makeHref(array($queryKey => $key));
 echo '<a '. $href .' target="'.BIBTEXBROWSER_MENU_TARGET.'">'. $item ."</a>\n";
 echo "<div class=\"mini_se\"></div>\n";
      }
      $index++;
    }
  }
}

/** transforms an array representing a query into a formatted string */
function query2title(&$query) {
    $headers = array();
    foreach($query as $k=>$v) {
      if($k == '_author') { $k = 'author'; }
      if($k == 'type') { $v = substr($v,1,strlen($v)-2); }
      $headers[$k] = ucwords($k).': '.ucwords($v);
  }
    // special cases
    if (isset($headers[Q_ALL])) $headers[Q_ALL] = 'Publications in '.$_GET[Q_FILE];
    if (isset($headers[Q_AUTHOR])) $headers[Q_AUTHOR] = 'Publications of '.$_GET[Q_AUTHOR];
    return join(' &amp; ',$headers);
}


/** displays the latest modified bibtex entries.
usage:
<pre>
  $db = zetDB('metrics.bib');
  $d = new NewEntriesDisplay();
  $d->setDB($db);
  $d->setN(7);// optional 
  $d->display();
</pre>
 */
class NewEntriesDisplay {
  var $n=5;
  var $db;
  
  function setDB(&$bibdatabase) {
    $this->db = $bibdatabase;
  }
  
  function setN($n) {$this->n = $n;return $this;}

  /** sets the entries to be shown */
  function setEntries(&$entries) {
    $this->db = createBibDataBase();
    $this->db->bibdb = $entries;
  }

    /** Displays a set of bibtex entries in an HTML table */
  function display() {
    $array = $this->db->getLatestEntries($this->n);
    $delegate = createBasicDisplay();
    $delegate->setEntries($array);
    $delegate->display();
  }
}


/** displays the entries by year in reverse chronological order. 
usage:
<pre>
  $db = zetDB('metrics.bib');
  $d = new YearDisplay();
  $d->setDB($db);
  $d->display();
</pre>
*/
class YearDisplay {
  
  /** is an array of strings representing years */
  var $yearIndex;

  function setDB(&$bibdatabase) {
    $this->setEntries($bibdatabase->bibdb);
  }

  /** creates a YearDisplay */
  function setOptions(&$options) {}

  function getTitle() {return '';}
  
  /** sets the entries to be shown */
  function setEntries(&$entries) {
    $this->entries = $entries;
    $db= createBibDataBase();
    $db->bibdb = $entries;
    $this->yearIndex = $db->yearIndex();    
  }

  /** Displays a set of bibtex entries in an HTML table */
  function display() {   
    $delegate = createBasicDisplay();
    $delegate->setEntries($this->entries);
    $index = count($this->entries);
    foreach($this->yearIndex as $year) {
      $x = array();
      uasort($x,'compare_bib_entry_by_month');
      foreach($this->entries as $e) {
        if ($e->getYear() == $year) {
          $x[] = $e;
        }
      }
      
      if (count($x)>0) {
        echo '<div  class="btb-header">'.$year.'</div>';
        $delegate->setEntries($x);
        $delegate->display();
      }
      
      $index = $index - count($x);
    }
  }
}


/** displays the summary information of all bib entries.
usage:
<pre>
  $db = zetDB('metrics.bib');
  $d = new SimpleDisplay();
  $d->setDB($db);
  $d->display();
</pre>
  */
class SimpleDisplay  {
 
  var $options = array();
  
  function setDB(&$bibdatabase) {
    $this->setEntries($bibdatabase->bibdb);
  }

  /** sets the entries to be shown */
  function setEntries(&$entries) {
    $this->entries = $entries;
  }

  function indexUp() {
    $index=1;
    foreach ($this->entries as $bib) {      
      $bib->setAbbrv((string)$index++);
    } // end foreach
    return $this->entries;
  }
  
  function newest(&$entries) {
    return array_slice($entries,0,BIBTEXBROWSER_NEWEST);
  }

  function indexDown() {
    $index=count($this->entries);
    foreach ($this->entries as $bib) {      
      $bib->setAbbrv((string)$index--);
    } // end foreach
    return $this->entries;
  }
  
  function setTitle($title) { $this->title = $title; return $this; }
  function getTitle() { return @$this->title ; }
  
  /** Displays a set of bibtex entries in an HTML table */
  function display() {
   
    uasort($this->entries, ORDER_FUNCTION);

    if ($this->options) {
      foreach($this->options as $fname=>$opt) {
        $this->$fname($opt,$entries);
      }
    }

    if (BIBTEXBROWSER_DEBUG) {
      echo 'Style: '.BIBLIOGRAPHYSTYLE.'<br/>';
      echo 'Order: '.ORDER_FUNCTION.'<br/>';
      echo 'Abbrv: '.ABBRV_TYPE.'<br/>';
      echo 'Options: '.@implode(',',$this->options).'<br/>';      
    }
    
    ?>
    
    <table class="result" >
    <?php
    $count = count($this->entries);
    $i=0;
    foreach ($this->entries as $bib) {    
      // by default, index are in decrasing order
      // so that when you add a publicaton recent , the indices of preceding publications don't change
      $bib->setIndex('['.($count-($i++)).']');
      $bib->toTR();
    } // end foreach
    ?>
    </table>
    <?php    
  } // end function
} // end class


/** returns an HTTP 404 and displays en error message. */
function nonExistentBibEntryError() {
  header('HTTP/1.1 404 Not found');
  ?>
  <b>Sorry, this bib entry does not exist.</b>
  <a href="?">Back to bibtexbrowser</a>
  <?php
  exit;
}


/** displays the publication records sorted by publication types (as configured by constant BIBLIOGRAPHYSECTIONS).
usage:
<pre>
  $db = zetDB('metrics.bib');
  $d = new AcademicDisplay();
  $d->setDB($db);
  $d->display();
</pre>
  */
class AcademicDisplay  {

  function getTitle() { return $this->title; }
  function setTitle($title) { $this->title = $title; return $this; }

  function setDB(&$bibdatabase) {
    $this->setEntries($bibdatabase->bibdb);
  }
  
  /** sets the entries to be shown */
  function setEntries(&$entries) {
    $this->entries = $entries;
  }

  /** transforms a query to HTML 
   * $ query is an array (e.g. array(Q_TYPE=>'book'))
   * $title is a string, the title of the section
   */
  function search2html($query, $title) {
    $entries = $this->db->multisearch($query);
    uasort($entries, ORDER_FUNCTION);
    if (count($entries)>0) {
    echo "\n".'<div class="btb-header">'.$title.'</div>'."\n";
    echo '<table class="result">'."\n";

    // by default the abbreviation is incermented over all 
    // searches
    
    // since we don't know before hand all section, we can not index in decreasing order
    static $count;
    if ($count == NULL) { $count = 1; } // init
    $id = $count;
    foreach ($entries as $bib) {
        $bib->setIndex('['.($id++).']');
        $bib->toTR();
    } // end foreach
    $count = @$count + count($entries);
    echo '</table>';
    }

  }
  
  function display() {
    $this->db = createBibDataBase();
    $this->db->bibdb = $this->entries;


    foreach (_DefaultBibliographySections() as $section) {
      $this->search2html($section['query'],$section['title']);
    }

    echo poweredby();
    
  }

}




/** displays a single bib entry.
usage:
<pre>
  $db = zetDB('metrics.bib');
  $dis = new BibEntryDisplay($db->getEntryByKey('Schmietendorf2000'));
  $dis->display();
</pre>
notes:
- the top-level header (usually &lt;H1>) must be done by the caller.  
- this view is optimized for Google Scholar
 */
class BibEntryDisplay {

  /** the bib entry to display */
  var $bib;

  function BibEntryDisplay($bib=null) {
    $this->bib = $bib;
  }
  
  function setEntries(&$entries) {
    $this->bib = $entries[0];
    //$this->title = $this->bib->getTitle().' (bibtex)'.$this->bib->getUrlLink();
  }

  /** returns the title */
  function getTitle() {
    return $this->bib->getTitle().' (bibtex)';
  }
  
  /** 2011/10/02: new display, inspired from Tom Zimmermann's home page */
  function displayOnSteroids() {
      $subtitle = '<div class="bibentry-by">by '.$this->bib->getFormattedAuthorsImproved().'</div>';

      $abstract = '';
      if ($this->bib->hasField('abstract')) {
        $abstract = '<div class="bibentry-label">Abstract:</div><div class="bibentry-abstract">'.$this->bib->getAbstract().'</div>';
      }
      
      $download = '';
      if ($this->bib->hasField('url')) {
        $download = '<div class="bibentry-document-link"><a href="'.$this->bib->getField('url').'">View PDF</a></div>';
      }
      $reference= '<div class="bibentry-label">Reference:</div><div class="bibentry-reference">'.strip_tags(bib2html($this->bib)).'</div>';

      $bibtex = '<div class="bibentry-label">Bibtex Entry:</div>'.$this->bib->toEntryUnformatted().'';
      return $subtitle.$abstract.$download.$reference.$bibtex.$this->bib->toCoins();
  }

  function display() {
    // we encapsulate everything so that the output of display() is still valid XHTML
    echo '<div>';
    //echo $this->display_old();
    echo $this->displayOnSteroids();
    echo poweredby();
    echo '</div>';
  }

  // old display
  function display_old() {
    return $this->bib->toCoins().$this->bib->toEntryUnformatted();
  }

  /** Creates metadata for Google Scholar
   * + a description
   * @see http://scholar.google.com/intl/en/scholar/inclusion.html
   * @see http://www.monperrus.net/martin/accurate+bibliographic+metadata+and+google+scholar
   * */
  function metadata() {
    $result=array();
    
    if (METADATA_GS) {
    // the description may mix with the Google Scholar tags
    // we remove it
    // $result[] = array('description',trim(strip_tags(str_replace('"','',bib2html($this->bib)))));
    $result[] = array('citation_title',$this->bib->getTitle());
    $authors = $this->bib->getArrayOfCommaSeparatedAuthors();
    $result[] = array('citation_authors',implode("; ",$authors));
    foreach($authors as $author) {
      $result[] = array('citation_author',$author);
    }
    $result[] = array('citation_publication_date',$this->bib->getYear());

    // this page
    $result[] = array('citation_abstract_html_url','http://'.$_SERVER['HTTP_HOST'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']).str_replace('&','&amp;',$_SERVER['REQUEST_URI']));
    
    if ($this->bib->hasField("publisher")) {
      $result[] = array('citation_publisher',$this->bib->getPublisher());
    }

    // BOOKTITLE: JOURNAL NAME OR PROCEEDINGS
    if ($this->bib->getType()=="article") { // journal article
      $result[] = array('citation_journal_title',$this->bib->getField("journal"));
      $result[] = array('citation_volume',$this->bib->getField("volume"));
      if ($this->bib->hasField("issue")) {
        $result[] = array('citation_issue',$this->bib->getField("issue"));
      }
      if ($this->bib->hasField("issn")) {
        $result[] = array('citation_issue',$this->bib->getField("issn"));
      }
    } 

    if ($this->bib->getType()=="inproceedings" || $this->bib->getType()=="conference") {
       $result[] = array('citation_conference_title',$this->bib->getField(BOOKTITLE));
       $result[] = array('citation_conference',$this->bib->getField(BOOKTITLE));
    }

    if ($this->bib->getType()=="phdthesis"
         || $this->bib->getType()=="mastersthesis"
         || $this->bib->getType()=="bachelorsthesis"
       )
    {
       $result[] = array('citation_dissertation_institution',$this->bib->getField('school'));
    }

    if ($this->bib->getType()=="techreport"
         && $this->bib->hasField("number")
       )
    {
       $result[] = array('citation_technical_report_number',$this->bib->getField('number'));
    }

    if ($this->bib->getType()=="techreport"
         && $this->bib->hasField("institution")
       )
    {
       $result[] = array('citation_technical_report_institution',$this->bib->getField('institution'));
    }

    // generic
    if ($this->bib->hasField("doi")) {
      $result[] = array('citation_doi',$this->bib->getField("doi"));
    }

    if ($this->bib->hasField("url")) {
      $result[] = array('citation_pdf_url',$this->bib->getField("url"));
    }
    }
    
    
    // we don't introduce yet another kind of bibliographic metadata
    // the core bibtex metadata will simply be available as json
    // now adding the pure bibtex with no translation
    //foreach ($this->bib->getFields() as $k => $v) {
    //  if (!preg_match("/^_/",$k)) {
    //    $result[] = array("bibtex:".$k,$v);  
    //  }
    //}

    
    // a fallback to essential dublin core
    // Dublin Core should not be used for bibliographic metadata
    // according to several sources 
    //  * Google Scholar: "Use Dublin Core tags (e.g., DC.title) as a last resort - they work poorly for journal papers"
    //  * http://reprog.wordpress.com/2010/09/03/bibliographic-data-part-2-dublin-cores-dirty-little-secret/
    // however it seems that Google Scholar needs at least DC.Title to trigger referencing
    // reference documentation: http://dublincore.org/documents/dc-citation-guidelines/
    if (METADATA_DC) {
    $result[] = array('DC.Title',$this->bib->getTitle());
    foreach($this->bib->getArrayOfCommaSeparatedAuthors() as $author) {
      $result[] = array('DC.Creator',$author);
    }
    $result[] = array('DC.Issued',$this->bib->getYear());
    }

    // --------------------------------- BEGIN METADATA EPRINTS
    // and now adding eprints metadata
    // why adding eprints metadata?
    // because eprints is a well known bibliographic software and several crawlers/desktop software
    // use their metadata
    // unfortunately, the metadata is even less documented than Google Scholar citation_
    // reference documentation: the eprints source code (./perl_lib/EPrints/Plugin/Export/Simple.pm)
    // examples: conference paper: http://tubiblio.ulb.tu-darmstadt.de/44344/
    //           journal paper: http://tubiblio.ulb.tu-darmstadt.de/44344/
    if (METADATA_EPRINTS) {
    $result[] = array('eprints.title',$this->bib->getTitle());
    $authors = $this->bib->getArrayOfCommaSeparatedAuthors();
    foreach($authors as $author) {
      $result[] = array('eprints.creators_name',$author);
    }
    $result[] = array('eprints.date',$this->bib->getYear());

    if ($this->bib->hasField("publisher")) {
      $result[] = array('eprints.publisher',$this->bib->getPublisher());
    }

    if ($this->bib->getType()=="article") { // journal article
      $result[] = array('eprints.type','article');
      $result[] = array('eprints.publication',$this->bib->getField("journal"));
      $result[] = array('eprints.volume',$this->bib->getField("volume"));
      if ($this->bib->hasField("issue")) {
        $result[] = array('eprints.number',$this->bib->getField("issue"));}
    } 

    if ($this->bib->getType()=="inproceedings" || $this->bib->getType()=="conference") {
       $result[] = array('eprints.type','proceeding');
       $result[] = array('eprints.book_title',$this->bib->getField(BOOKTITLE));
    }

    if ($this->bib->getType()=="phdthesis"
         || $this->bib->getType()=="mastersthesis"
         || $this->bib->getType()=="bachelorsthesis"
       )
    {
       $result[] = array('eprints.type','thesis');
       $result[] = array('eprints.institution',$this->bib->getField('school'));
    }

    if ($this->bib->getType()=="techreport")
    {
       $result[] = array('eprints.type','monograph');
       if ($this->bib->hasField("number")) {
         $result[] = array('eprints.number',$this->bib->getField('number'));
       }
       if ($this->bib->hasField("institution")) {
         $result[] = array('eprints.institution',$this->bib->getField('institution'));
       }
    }

    // generic
    if ($this->bib->hasField("doi")) {
      $result[] = array('eprints.id_number',$this->bib->getField("doi"));
    }

    if ($this->bib->hasField("url")) {
      $result[] = array('eprints.official_url',$this->bib->getField("url"));
    }
    }
    // --------------------------------- END METADATA EPRINTS

    return $result;

  }
}

// ----------------------------------------------------------------------
// DATABASE MANAGEMENT
// ----------------------------------------------------------------------

/** represents a bibliographic database that contains a set of bibliographic entries.
usage:
<pre>
$db = new BibDataBase();
$db->load('metrics.bib');
$query = array('author'=>'martin', 'year'=>2008);
foreach ($db->multisearch($query) as $bibentry) { echo $bibentry->getTitle(); }
</pre>
*/
class BibDataBase {
  /** A hash table from keys (e.g. Goody1994) to bib entries (BibEntry instances). */
  var $bibdb;

  /** A hashtable of constant strings */
  var $stringdb;

  /** Creates a new database by parsing bib entries from the given
   * file. (backward compatibility) */
  function load($filename) {
    $this->update($filename);
  }
  
  /** Updates a database (replaces the new bibtex entries by the most recent ones) */ 
  function update($filename) {
  
    $empty_array = array();
    $db = createBibDBBuilder();
    $db->setData($empty_array, $this->stringdb);
    $db->build($filename);
    
    $this->stringdb = $db->stringdb;
    $result = $db->builtdb;
    
    
    foreach ($result as $b) {
       
      // new entries:
      if (!isset($this->bibdb[$b->getKey()])) {
        //echo 'adding...<br/>';
        $this->bibdb[$b->getKey()] = $b;
      }
      // update entry
      else if (isset($this->bibdb[$b->getKey()]) && ($b->getText() !== $this->bibdb[$b->getKey()]->getText())) {
        //echo 'replacing...<br/>';
        $this->bibdb[$b->getKey()] = $b;      
      }
    }
    
    // some entries have been removed
    foreach ($this->bibdb as $e) {
      if (!isset($result[$e->getKey()])) {
        //echo 'deleting...<br/>';
        unset($this->bibdb[$e->getKey()]);
      }
    }
      
  }

  /** Creates a new empty database */
  function BibDataBase() {
    $this->bibdb = array();
    $this->stringdb = array();
  }

  /** Returns the $n latest modified bibtex entries/ */
  function getLatestEntries($n) {
    $order='compare_bib_entry_by_mtime';
    $array = $this->bibdb; // array passed by value
    uasort($array, $order);
    $result = array_slice($array,0,$n);
    return $result;
  }
  
  /** Returns all entries as an array. Each entry is an instance of
   * class BibEntry. */
  function getEntries() {
    return $this->bibdb;
  }
  /** tests wheter the database contains a bib entry with $key */
  function contains($key) {
    return isset($this->bibdb[$key]);
  }
  /** Returns all entries categorized by types. The returned value is
   * a hashtable from types to arrays of bib entries.
   */
  function getEntriesByTypes() {
    $result = array();
    foreach ($this->bibdb as $b) {
      $result[$b->getType()][] = $b;
    }
    return $result;
  }

  /** Returns an array containing all the bib types (strings). */
  function getTypes() {
    $result = array();
    foreach ($this->bibdb as $b) {
      $result[$b->getType()] = 1;
    }
    $result = array_keys($result);
    return $result;
  }

  /** Generates and returns an array consisting of all authors.
   * The returned array is a hash table with keys <FirstName LastName>
   * and values <LastName, FirstName>.
   */
  function authorIndex(){
    $result = array();
    foreach ($this->bibdb as $bib) {
      foreach($bib->getRawAuthors() as $a){
        //we use an array because several authors can have the same lastname
        @$result[$bib->getLastName($a)][$bib->formatAuthor($a)]++;
      }
    }
    ksort($result);

    // now authors are sorted by last name
    // we rebuild a new array for having good keys in author page
    $realresult = array();
    foreach($result as $x) {
        ksort($x);
        foreach($x as $v => $tmp) $realresult[$v] = $v;
    }

    return $realresult;
  }

  /** Generates and returns an array consisting of all tags.
   */
  function tagIndex(){
    $result = array();
    foreach ($this->bibdb as $bib) {
      if (!$bib->hasField("keywords")) continue;
      $tags =preg_split('/[,;]/', $bib->getField("keywords"));
      foreach($tags as $a){
        $ta = trim($a);
        $result[$ta] = $ta;
      }
    }
    asort($result);
    return $result;
  }

  /** Generates and returns an array consisting of all years.
   */
  function yearIndex(){
    $result = array();
    foreach ($this->bibdb as $bib) {
      if (!$bib->hasField("year")) continue;
      $year = $bib->getField("year");
      $result[$year] = $year;
      }
    arsort($result);
    return $result;
  }

  /** Given its key, return the bib entry. */
  function getEntryByKey($key) {
    return $this->bibdb[$key];
  }

  /**
   * Returns an array containing all bib entries matching the given
   * type.
   */
  function searchType($type){
    $result = array();
    foreach($this->bibdb as $bib) {
      if($bib->getType() == $type)
 $result[] = $bib;
    }
    return $result;
  }

  /** Returns an array of bib entries (BibEntry) that satisfy the query
   * $query is an hash with entry type as key and searched fragment as value
   */
  function multisearch($query) {
    if (count($query)<1) {return array();}
    if (isset($query[Q_ALL])) return array_values($this->bibdb);

    $result = array();

    foreach ($this->bibdb as $bib) {
        $entryisselected = true;
        foreach ($query as $field => $fragment) {
          if ($field==Q_SEARCH) {
            // we search in the whole bib entry
            if (!$bib->hasPhrase($fragment)) {
              $entryisselected = false;
            }
          }
          else if ($field==Q_EXCLUDE) {
            if ($bib->hasPhrase($fragment)) {
              $entryisselected = false;
            }
          }
          else {
            if (!$bib->hasPhrase($fragment, $field))  {
              $entryisselected = false;
            }
          }

        }
        if ($entryisselected) {
          $result[] = $bib;
        }
      }
      
      return $result;
  }
} // end class

/** returns the default CSS of bibtexbrowser */
function bibtexbrowserDefaultCSS() {
?>

/* title */
.bibtitle { font-weight:bold; }
/* author */
.bibauthor { /* nothing by default */ }
/* booktitle (e.g. proceedings title, journal name, etc )*/
.bibbooktitle { font-style:italic; }
/* publisher */
.bibpublisher { /* nothing by default */ }


.title {
  color: #003366;
  font-size: large;
  font-weight: bold;
  text-align: right;
}

.btb-header {
  background-color: #995124;
  color: #FFFFFF;
  padding: 1px 2px 1px 2px;
}

.btb-nav-title {   
  background-color: #995124;
  color: #FFFFFF;
  padding: 1px 2px 1px 2px;
}

.rheader {
  font-weight: bold;
  background-color: #003366;
  color: #ffffff;
  padding: 2px;
  margin-bottom: 10px;
  border-bottom: #ff6633 2px solid;

}
.menu {
  font-size: x-small;
  background-color: #EFDDB4;
  padding: 0px;
  border: 1px solid #000000;
  margin: 0px;
}
.menu a {
  text-decoration: none;
  color: #003366;
}
.menu a:hover {
  color: #ff6633;
}

.bibref {
  padding:7px;
  padding-left:15px;
  vertical-align:text-top; 
}

.result {
  padding:0px;
  border: 1px solid #000000;
  margin:0px;
  background-color: #ffffff;
  width:100%;
}
.result a {
  text-decoration: none;
  color: #469AF8;
}

.result a:hover {
  color: #ff6633;
}

.input_box{
  margin-bottom : 2px;
}
.mini_se {
  border: none 0;
  border-top: 1px dashed #717171;
  height: 1px;
}
.a_name a {
  color:#469AF8;
  width:130px;
}

.rsslink {
  text-decoration: none;
  color:#F88017;
/* could be fancy, see : http://www.feedicons.com/ for icons*/
  /*background-image: url("rss.png"); text-indent: -9999px;*/
}

.purebibtex {
  font-family: monospace;
  font-size: small;
  border: 1px solid #DDDDDD;
  white-space:pre;
  background: none repeat scroll 0 0 #F5F5F5;  
  padding:10px;
  overflow:auto;
  clear:both;
}
.bibentry-by { font-style: italic; }
.bibentry-abstract { margin:15px; }
.bibentry-label { margin-top:15px; }
.bibentry-reference { margin-bottom:15px; padding:10px; background: none repeat scroll 0 0 #F5F5F5; border: 1px solid #DDDDDD; }

.btb-nav { text-align: right; }

<?php
} // end function bibtexbrowserDefaultCSS

/** encapsulates the content of a delegate into full-fledged HTML (&lt;HTML>&lt;BODY> and TITLE)
usage:
<pre>
  $db = zetDB('metrics.bib');
  $dis = new BibEntryDisplay($db->getEntryByKey('Schmietendorf2000'));
  new HTMLWrapper($dis);
</pre>
*/
class HTMLWrapper {
/**
 * $content: an object with methods
      display() 
      getRSS()
      getTitle()
 * $title: title of the page
 */
function HTMLWrapper(&$content,$metatags=array()/* an array name=>value*/) {

// when we load a page with AJAX
// the HTTP header is taken into account, not the <meta http-equiv>
header('Content-type: text/html; charset='.ENCODING);
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo ENCODING ?>"/>
<meta name="generator" content="bibtexbrowser v20120703" />
<?php 
// if ($content->getRSS()!='') echo '<link rel="alternate" type="application/rss+xml" title="RSS" href="'.$content->getRSS().'&amp;rss" />'; 
?>
<?php 

foreach($metatags as $item) {
  list($name,$value) = $item;
  echo '<meta name="'.$name.'" content="'.$value.'"/>'."\n"; 
} // end foreach  



// now the title
if (method_exists($content, 'getTitle')) {
  echo '<title>'.strip_tags($content->getTitle()).'</title>'; 
} 

// now the CSS
echo '<style type="text/css"><!--  '."\n";

if (method_exists($content, 'getCSS')) {
  echo $content->getCSS(); 
} else if (is_readable(dirname(__FILE__).'/bibtexbrowser.css')) {
  readfile(dirname(__FILE__).'/bibtexbrowser.css');
}
else {  bibtexbrowserDefaultCSS(); }

echo "\n".' --></style>';

?>
</head>
<body>
<?php 
if (method_exists($content, 'getTitle')) {
  echo "<div class=\"rheader\">" . $content->getTitle() . "</div>";
}
?>
<?php 
  $content->display();

  if (BIBTEXBROWSER_USE_PROGRESSIVE_ENHANCEMENT) {
    javascript();
  }


?>
</body>
</html>
<?php
//exit;
} // end constructor

}


/** does nothing but calls method display() on the content. 
usage:
<pre>
  $db = zetDB('metrics.bib');
  $dis = new SimpleDisplay($db);
  new NoWrapper($dis);
</pre>
*/
class NoWrapper {
  function NoWrapper(&$content) {
    echo $content->display();
  }
}

/** is used to create an subset of a bibtex file.
usage:
<pre>
  $db = zetDB('metrics.bib');
  $query = array('year'=>2005);
  $dis = new BibtexDisplay()->setEntries($db->multisearch($query));
  $dis->display();
</pre>
*/
class BibtexDisplay {

  function BibtexDisplay() {}
  
  function setTitle($title) { $this->title = $title; return $this; }

  /** sets the entries to be shown */
  function setEntries(&$entries) {
    $this->entries = $entries;
  }

  function setWrapper($x) { $x->wrapper = 'NoWrapper'; }
  
  function display() {
    header('Content-type: text/plain; charset='.ENCODING);
    echo '% generated by bibtexbrowser <http://www.monperrus.net/martin/bibtexbrowser/>'."\n";
    echo '% '.@$this->title."\n";
    echo '% Encoding: '.ENCODING."\n";
    foreach($this->entries as $bibentry) { echo $bibentry->getText()."\n"; }
    exit;
  }
}

/** creates paged output, e.g: [[http://localhost/bibtexbrowser/testPagedDisplay.php?page=1]]
usage:
<pre>
  $_GET['library']=1;
  include( 'bibtexbrowser.php' );
  $db = zetDB('metrics.bib');
  $pd = new PagedDisplay();
  $pd->setEntries($db->bibdb);
  $pd->display();
</pre>
*/
class PagedDisplay {

  var $query = array();
  
  function PagedDisplay() {
    $this->setPage();
  }
  
    /** sets the entries to be shown */
  function setEntries(&$entries) {
    uasort($entries, ORDER_FUNCTION);
    $this->entries = array_values($entries);
  }

  /** sets $this->page from $_GET, defaults to 1 */
  function setPage() {
    $this->page = 1;
    if (isset($_GET['page'])) {
      $this->page = $_GET['page'];
    }  
  }
  
  function setQuery($query = array()) {
    $this->query = $query;
  }
    
  function getTitle() {
    return query2title($this->query). ' - page '.$this->page;  
  }
  
  function display() {      
    $less = false;
    
    if ($this->page>1) {$less = true;} 
    
    $more = true;
    
    // computing $more
    $index = ($this->page)*PAGE_SIZE;      
    if (!isset($this->entries[$index])) {
      $more = false;
    }
    
    $this->menu($less, $more);    
    echo '<table class="result" >';
    for ($i = 0; $i < PAGE_SIZE; $i++) { 
      $index = ($this->page-1)*PAGE_SIZE + $i;
      if (isset($this->entries[$index])) {
        $bib = $this->entries[$index];       
        $bib->toTR(); 
        
      } else {
        //break;
      }
    } // end foreach
    echo '</table>';
    
    $this->menu($less, $more);    
  }
  
  function menu($less, $more) {
  
    echo '<span class="nav-menu">';
    
    $prev = $this->query;
    $prev['page'] = $this->page-1;
    if ($less == true) { echo '<a '.makeHref($prev).'">Prev Page</a>'; }
    
    if ($less && $more) { echo '&nbsp;|&nbsp;'; }
    
    $next = $this->query;
    $next['page'] = $this->page+1;
    if ($more == true) { echo '<a '.makeHref($next).'">Next Page</a>'; }    
    echo '</span>';

  }
}

/** is used to create an RSS feed.
usage:
<pre>
  $db = zetDB('metrics.bib');
  $query = array('year'=>2005);
  $rss = new RSSDisplay();
  $entries = $db->getLatestEntries(10);
  $rss->setEntries($entries);
  $rss->display();
</pre>
*/
class RSSDisplay {

  var $title = 'RSS produced by bibtexbrowser';
  
  function RSSDisplay() {
    // nothing by default    
  }

  function setTitle($title) { $this->title = $title; return $this; }

  /** tries to always output a valid XML/RSS string 
    * based on ENCODING, HTML tags, and the transformations 
    * that happened in latex2html */
  function text2rss($desc) {
    // first strip HTML tags 
    $desc = strip_tags($desc);
  
    // then decode characters encoded by latex2html
    $desc= html_entity_decode($desc);

    // some entities may still be here, we remove them
    // we replace html entities e.g. &eacute; by nothing
    // however XML entities are kept (e.g. &#53;)
    $desc = preg_replace('/&\w+;/','',$desc);
            
    // bullet proofing ampersand
    $desc = preg_replace('/&([^#])/','&#38;$1',$desc);

    // be careful of < 
    $desc = str_replace('<','&#60;',$desc);
    
    // final test with encoding:
    if (function_exists('mb_check_encoding')) { // (PHP 4 >= 4.4.3, PHP 5 >= 5.1.3)
      if (!mb_check_encoding($desc,ENCODING)) { 
        return 'encoding error: please check the content of ENCODING';
      }
    }
    
    return $desc;
  }
  
  /** sets the entries to be shown */
  function setEntries(&$entries) {
    $this->entries = $entries;
  }

  function setWrapper($x) { $x->wrapper = 'NoWrapper'; }

  function display() {
    header('Content-type: application/rss+xml');
    echo '<?xml version="1.0" encoding="'.ENCODING.'"?>';
//

?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
   <channel>
      <title><?php echo $this->title;?></title>
      <link>http://<?php echo $_SERVER['HTTP_HOST'].htmlentities($_SERVER['REQUEST_URI']);?></link>
      <atom:link href="http://<?php echo $_SERVER['HTTP_HOST'].htmlentities($_SERVER['REQUEST_URI']);?>" rel="self" type="application/rss+xml" />
      <description></description>
      <generator>bibtexbrowser v20120703</generator>

<?php
      foreach($this->entries as $bibentry) {
         ?>
         <item>
         <title><?php echo $this->text2rss($bibentry->getTitle());?></title>
         <link><?php echo $bibentry->getAbsoluteURL();?></link>
         <description>
          <?php
            // we are in XML, so we cannot have HTML entitites
            // however the encoding is specified in preamble
            echo $this->text2rss(bib2html($bibentry)."\n".$bibentry->getAbstract());
          ?>
          </description>
         <guid isPermaLink="false"><?php echo urlencode(@$_GET[Q_FILE].'::'.$bibentry->getKey());?></guid>
         </item>
         <?php } /* end foreach */?>
   </channel>
</rss>

<?php
  //exit;
  }
}



/** is responsible for transforming a query string of $_GET[..] into a publication list.
usage:
<pre>
  $_GET['library']=1;
  @require('bibtexbrowser.php');
  // simulating ?bib=metrics.bib&year=2009
  $_GET['bib']='metrics.bib';
  $_GET['year']='2006';
  new Dispatcher();
</pre>
*/
class Dispatcher {

  /** this is the query */
  var $query = array();

  /** the displayer of selected entries. The default is set in BIBTEXBROWSER_DEFAULT_DISPLAY.
    *  It could also be an RSSDisplay if the rss keyword is present
    */
  var $displayer = '';

  /** the wrapper of selected entries. The default is an HTML wrapper
    *  It could also be a NoWrapper when you include your pub list in your home page
    */
  var $wrapper = 'HTMLWrapper';

  function Dispatcher() {
    // are we in test mode, or libray mode
    // then this file is just a library
    if (isset($_GET['test']) || isset($_GET['library'])) {
      // we unset in  order to use the dispatcher afterwards
      unset($_GET['test']);
      unset($_GET['library']);
      return;
    }
    
    // first we set the database (load from disk or parse the bibtex file)
    setDB();
    
    // is the publication list included in another page?
    // strtr is used for Windows where __FILE__ contains C:\toto and SCRIPT_FILENAME contains C:/toto (bug reported by Marco)
    // realpath is required if the path contains sym-linked directories (bug found by Mark Hereld)    
    if (strtr(realpath(__FILE__),"\\","/")!=strtr(realpath($_SERVER['SCRIPT_FILENAME']),"\\","/")) $this->wrapper=BIBTEXBROWSER_EMBEDDED_WRAPPER;

    // first pass, we will exit if we encounter key or menu or academic
    // other wise we just create the $this->query
    foreach($_GET as $keyword=>$value) {
      if (method_exists($this,$keyword)) {
        // if the return value is END_DISPATCH, we finish bibtexbrowser (but not the whole PHP process in case we are embedded)
        if ($this->$keyword()=='END_DISPATCH') return; 
      }
    }

    // at this point, we may have a query
    
    if (count($this->query)>0) {

       // first test for inconsistent queries
       if (isset($this->query[Q_ALL]) && count($this->query)>1) {
         // we discard the Q_ALL, it helps in embedded mode
         unset($this->query[Q_ALL]);
       }
    
       $selectedEntries = $_GET[Q_DB]->multisearch($this->query);
       
       // default order
       uasort($selectedEntries, ORDER_FUNCTION);
       $selectedEntries = array_values($selectedEntries);
       
       //echo '<pre>';print_r($selectedEntries);echo '</pre>';
       
       if ($this->displayer=='') {
         $this->displayer = BIBTEXBROWSER_DEFAULT_DISPLAY;
       }
    } // otherwise the query is left empty

    // do we have a displayer?
    if ($this->displayer!='') {
    
      $options = array();
      if (isset($_GET['dopt'])) {
        $options = json_decode($_GET['dopt'],true);
      }     
       
      // required for PHP4 to have this intermediate variable
      $x = new $this->displayer();
      
      if (method_exists($x,'setEntries')) {
        $x->setEntries($selectedEntries);
      }
      
      if (method_exists($x,'setTitle')) {
        $x->setTitle(query2title($this->query));
      }

      if (method_exists($x,'setQuery')) {
        $x->setQuery($this->query);
      }

      if (method_exists($x,'setWrapper')) {
        $x->setWrapper($this);
      }

      // should call method display() on $x
      new $this->wrapper($x);
      
      $this->clearQuery();
    }
    else { 
       // we send a redirection for having the frameset
       // if some contents have already been sent, for instance if we are included
       // this means doing nothing
       if ( headers_sent() == false ) { /* to avoid sending an unnecessary frameset */ 
         header("Location: ".$_SERVER['SCRIPT_NAME']."?frameset&bib=".$_GET[Q_FILE]);
       }
     }
  }

  /** clears the query string in $_GET so that bibtexbrowser can be called multiple times */
  function clearQuery() {
    $params= array(Q_ALL,'rss', 'astext', Q_SEARCH, Q_EXCLUDE, Q_YEAR, EDITOR, Q_TAG, Q_AUTHOR, Q_TYPE, Q_ACADEMIC, Q_KEY);
    foreach($params as $p) { unset($_GET[$p]); }    
  }
  
  function all() {
    $this->query[Q_ALL]=1;
  }

  function display() {
    $this->displayer=$_GET['display'];  
  }
  
  function rss() {
    $this->displayer='RSSDisplay';
    $this->wrapper='NoWrapper';
  }

  function astext() {
    $this->displayer='BibtexDisplay';
    $this->wrapper='NoWrapper';
  }

  function search() {
    if (preg_match('/utf-?8/i',ENCODING)) {
      $_GET[Q_SEARCH] = urldecode($_GET[Q_SEARCH]);
    }
    $this->query[Q_SEARCH]=$_GET[Q_SEARCH];
  }

  function exclude() { $this->query[Q_EXCLUDE]=$_GET[Q_EXCLUDE]; }

  function year() {  
    // we may want the latest
    if ($_GET[Q_YEAR]=='latest') {
      $years = $_GET[Q_DB]->yearIndex();
      $_GET[Q_YEAR]=array_shift($years);
    }
    $this->query[Q_YEAR]=$_GET[Q_YEAR];
  }
  
  function editor() {  $this->query[EDITOR]=$_GET[EDITOR]; }

  function keywords() { $this->query[Q_TAG]=$_GET[Q_TAG]; }

  function author() {    
    // Friday, October 29 2010
    // changed fomr 'author' to '_author'
    // because '_author' is already formatted
    // doing so we can search at the same time "Joe Dupont" an "Dupont, Joe"
    $this->query['_author']=$_GET[Q_AUTHOR];
  }

  function type() { 
    // remarks KEN
    // "book" selects inbook, book, bookchapter
    // so we add the regexp modifiers
    if (strlen($_GET[Q_TYPE])>0 && !preg_match('/^\^.*\$$/',$_GET[Q_TYPE])) { $_GET[Q_TYPE] = '^'.$_GET[Q_TYPE].'$'; }
    $this->query[Q_TYPE]= $_GET[Q_TYPE];
  }

  function menu() {
    $menu = createMenuManager();
    $menu->setDB($_GET[Q_DB]);
    new $this->wrapper($menu,array(array('robots','noindex')));
    return 'END_DISPATCH';
  }

  /** the academic keyword in URLs switch from a year based viey to a publication type based view */
  function academic() {
     $this->displayer='AcademicDisplay';
     
     
     // backward compatibility with old GET API
     // this is deprecated
     // instead of academic=Martin+Monperrus
     // you should use author=Martin+Monperrus&academic
     // be careful of the semantics of === and !==
     // 'foo bar' == true is true
     // 123 == true is true (and whatever number different from 0
     // 0 == true is true
     // '1'!=1 is **false**
     if(!isset($_GET[Q_AUTHOR]) && $_GET[Q_ACADEMIC]!==true && $_GET[Q_ACADEMIC]!=='true' && $_GET[Q_ACADEMIC]!=1 && $_GET[Q_ACADEMIC]!='') {
      $_GET[Q_AUTHOR]=$_GET[Q_ACADEMIC];
      $this->query[Q_AUTHOR]=$_GET[Q_ACADEMIC];
     }

  }

  function key() {
    if ($_GET[Q_DB]->contains($_GET[Q_KEY])) {
      $bibentry = $_GET[Q_DB]->getEntryByKey($_GET[Q_KEY]);
      $entries = array($bibentry);
      if (isset($_GET['astext'])) {
        $bibdisplay = new BibtexDisplay();
        $bibdisplay->setEntries($entries);
        $bibdisplay->display();
      } else {
        $bibdisplay = createBibEntryDisplay();
        $bibdisplay->setEntries($entries);
        new $this->wrapper($bibdisplay,$bibdisplay->metadata());
      }
      return 'END_DISPATCH';
    }
    else { nonExistentBibEntryError(); }
  }

  /** is used to remotely analyzed a situation */
  function diagnosis() {
    header('Content-type: text/plain');
    echo "php version: ".phpversion()."\n";
    echo "bibtexbrowser version: 20120703\n";
    echo "dir: ".decoct(fileperms(dirname(__FILE__)))."\n";
    echo "bibtex file: ".decoct(fileperms($_GET[Q_FILE]))."\n";
    exit;
  }

  function frameset() {    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
    <html  xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="generator" content="bibtexbrowser v20120703" />
    <meta http-equiv="Content-Type" content="text/html; charset=<?php echo ENCODING ?>"/>
    <title>You are browsing <?php echo $_GET[Q_FILE]; ?> with bibtexbrowser</title>
    </head>
    <frameset cols="15%,*">
    <frame name="menu" src="<?php echo '?'.Q_FILE.'='. urlencode($_GET[Q_FILE]).'&amp;menu'; ?>" />
    <frame name="main" src="<?php echo '?'.Q_FILE.'='. urlencode($_GET[Q_FILE]).'&amp;year=latest'?>" />
    </frameset>
    </html>

    <?php
    return 'END_DISPATCH';
}

} // end class Dispatcher

} // end if (!defined('BIBTEXBROWSER'))

new Dispatcher();

?>
Back to Top