/usr/share/perl5/OpenGuides.pm is in openguides 0.76-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 | package OpenGuides;
use strict;
use Carp "croak";
use CGI;
use Wiki::Toolkit::Plugin::Diff;
use Wiki::Toolkit::Plugin::Locator::Grid;
use OpenGuides::CGI;
use OpenGuides::Feed;
use OpenGuides::Template;
use OpenGuides::Utils;
use Time::Piece;
use URI::Escape;
use vars qw( $VERSION );
$VERSION = '0.76';
=head1 NAME
OpenGuides - A complete web application for managing a collaboratively-written guide to a city or town.
=head1 DESCRIPTION
The OpenGuides software provides the framework for a collaboratively-written
city guide. It is similar to a wiki but provides somewhat more structured
data storage allowing you to annotate wiki pages with information such as
category, location, and much more. It provides searching facilities
including "find me everything within a certain distance of this place".
Every page includes a link to a machine-readable (RDF) version of the page.
=head1 METHODS
=over
=item B<new>
my $config = OpenGuides::Config->new( file => "wiki.conf" );
my $guide = OpenGuides->new( config => $config );
=cut
sub new {
my ($class, %args) = @_;
my $self = {};
bless $self, $class;
my $wiki = OpenGuides::Utils->make_wiki_object( config => $args{config} );
$self->{wiki} = $wiki;
$self->{config} = $args{config};
my $geo_handler = $self->config->geo_handler;
my $locator;
if ( $geo_handler == 1 ) {
$locator = Wiki::Toolkit::Plugin::Locator::Grid->new(
x => "os_x", y => "os_y" );
} elsif ( $geo_handler == 2 ) {
$locator = Wiki::Toolkit::Plugin::Locator::Grid->new(
x => "osie_x", y => "osie_y" );
} else {
$locator = Wiki::Toolkit::Plugin::Locator::Grid->new(
x => "easting", y => "northing" );
}
$wiki->register_plugin( plugin => $locator );
$self->{locator} = $locator;
my $differ = Wiki::Toolkit::Plugin::Diff->new;
$wiki->register_plugin( plugin => $differ );
$self->{differ} = $differ;
if($self->config->ping_services) {
eval {
require Wiki::Toolkit::Plugin::Ping;
};
if ( $@ ) {
warn "You asked for some ping services, but can't find "
. "Wiki::Toolkit::Plugin::Ping";
} else {
my @ws = split(/\s*,\s*/, $self->config->ping_services);
my %well_known = Wiki::Toolkit::Plugin::Ping->well_known;
my %services;
foreach my $s (@ws) {
if($well_known{$s}) {
$services{$s} = $well_known{$s};
} else {
warn("Ignoring unknown ping service '$s'");
}
}
my $ping = Wiki::Toolkit::Plugin::Ping->new(
node_to_url => $self->{config}->{script_url}
. $self->{config}->{script_name} . '?$node',
services => \%services
);
$wiki->register_plugin( plugin => $ping );
}
}
return $self;
}
=item B<wiki>
An accessor, returns the underlying L<Wiki::Toolkit> object.
=cut
sub wiki {
my $self = shift;
return $self->{wiki};
}
=item B<config>
An accessor, returns the underlying L<OpenGuides::Config> object.
=cut
sub config {
my $self = shift;
return $self->{config};
}
=item B<locator>
An accessor, returns the underlying L<Wiki::Toolkit::Plugin::Locator::UK> object.
=cut
sub locator {
my $self = shift;
return $self->{locator};
}
=item B<differ>
An accessor, returns the underlying L<Wiki::Toolkit::Plugin::Diff> object.
=cut
sub differ {
my $self = shift;
return $self->{differ};
}
=item B<display_node>
# Print node to STDOUT.
$guide->display_node(
id => "Calthorpe Arms",
version => 2,
);
# Or return output as a string (useful for writing tests).
$guide->display_node(
id => "Calthorpe Arms",
return_output => 1,
);
# Return output as a string with HTTP headers omitted (for tests).
$guide->display_node(
id => "Calthorpe Arms",
return_output => 1,
noheaders => 1,
);
# Or return the hash of variables that will be passed to the template
# (not including those set additionally by OpenGuides::Template).
$guide->display_node(
id => "Calthorpe Arms",
return_tt_vars => 1,
);
If C<version> is omitted then it will assume you want the latest version.
Note that if you pass the C<return_output> parameter, and your node is a
redirecting node, this method will fake the redirect and return the output
that will actually end up in the user's browser. If instead you want to see
the HTTP headers that will be printed in order to perform the redirect, pass
the C<intercept_redirect> parameter as well. The C<intercept_redirect>
parameter has no effect if the node isn't a redirect, or if the
C<return_output> parameter is omitted.
(At the moment, C<return_tt_vars> acts as if the C<intercept_redirect>
parameter was passed.)
The C<noheaders> parameter only takes effect if C<return_output> is true
and C<intercept_redirect> is false or omitted.
If you have specified the C<host_checker_module> option in your
C<wiki.conf>, this method will attempt to call the <blacklisted_host>
method of that module to determine whether the host requesting the node
has been blacklisted. If this method returns true, then the
C<blacklisted_host.tt> template will be used to display an error message.
The C<blacklisted_host> method will be passed a scalar containing the host's
IP address.
=cut
sub display_node {
my ($self, %args) = @_;
my $return_output = $args{return_output} || 0;
my $intercept_redirect = $args{intercept_redirect};
my $noheaders = ( $return_output && !$intercept_redirect
&& $args{noheaders} );
my $version = $args{version};
my $id = $args{id} || $self->config->home_name;
my $wiki = $self->wiki;
my $config = $self->config;
my $oldid = $args{oldid} || '';
my $do_redirect = defined($args{redirect}) ? $args{redirect} : 1;
my %tt_vars;
# If we can, check to see if requesting host is blacklisted.
my $host_checker = $config->host_checker_module;
my $is_blacklisted;
if ( $host_checker ) {
eval {
eval "require $host_checker";
$is_blacklisted = $host_checker->blacklisted_host(CGI->new->remote_host);
};
}
if ( $is_blacklisted ) {
my $output = OpenGuides::Template->output(
wiki => $self->wiki,
config => $config,
template => "blacklisted_host.tt",
vars => {
not_editable => 1,
},
noheaders => $noheaders,
);
return $output if $return_output;
print $output;
return;
}
$tt_vars{home_name} = $self->config->home_name;
if ( $id =~ /^(Category|Locale) (.*)$/ ) {
my $type = $1;
$tt_vars{is_indexable_node} = 1;
$tt_vars{index_type} = lc($type);
$tt_vars{index_value} = $2;
$tt_vars{"rss_".lc($type)."_url"} =
$config->script_name . "?action=rc;format=rss;"
. lc($type) . "=" . lc(CGI->escape($2));
$tt_vars{"atom_".lc($type)."_url"} =
$config->script_name . "?action=rc;format=atom;"
. lc($type) . "=" . lc(CGI->escape($2));
}
my %current_data = $wiki->retrieve_node( $id );
my $current_version = $current_data{version};
undef $version if ($version && $version == $current_version);
my %criteria = ( name => $id );
$criteria{version} = $version if $version; # retrieve_node default is current
my %node_data = $wiki->retrieve_node( %criteria );
# Fixes passing undefined values to Text::Wikiformat if node doesn't exist.
my $content = '';
if ($node_data{content}) {
$content = $wiki->format($node_data{content});
}
my $modified = $node_data{last_modified};
my $moderated = $node_data{moderated};
my %metadata = %{$node_data{metadata}};
my ($wgs84_long, $wgs84_lat) = OpenGuides::Utils->get_wgs84_coords(
longitude => $metadata{longitude}[0],
latitude => $metadata{latitude}[0],
config => $config);
if ($args{format} && $args{format} eq 'raw') {
print "Content-Type: text/plain\n\n" unless $noheaders;
print $node_data{content};
return 0;
}
my %metadata_vars = OpenGuides::Template->extract_metadata_vars(
wiki => $wiki,
config => $config,
metadata => $node_data{metadata}
);
my $node_exists = $wiki->node_exists($id);
my $http_status = $node_exists ? undef : '404 Not Found';
%tt_vars = (
%tt_vars,
%metadata_vars,
content => $content,
last_modified => $modified,
version => $node_data{version},
node => $id,
language => $config->default_language,
moderated => $moderated,
oldid => $oldid,
enable_gmaps => 1,
wgs84_long => $wgs84_long,
wgs84_lat => $wgs84_lat,
empty_node => !$node_exists,
read_only => $config->read_only,
);
# Hide from search engines if showing a specific version.
$tt_vars{'deter_robots'} = 1 if $args{version};
if ( $config->show_gmap_in_node_display
&& $self->get_cookie( "display_google_maps" ) ) {
$tt_vars{display_google_maps} = 1;
}
my $redirect = OpenGuides::Utils->detect_redirect(
content => $node_data{content} );
if ( $redirect ) {
# Don't redirect if the parameter "redirect" is given as 0.
if ($do_redirect == 0) {
$tt_vars{current} = 1;
return %tt_vars if $args{return_tt_vars};
my $output = $self->process_template(
id => $id,
template => "node.tt",
tt_vars => \%tt_vars,
http_status => $http_status
);
return $output if $return_output;
print $output;
} elsif ( $wiki->node_exists($redirect) && $redirect ne $id && $redirect ne $oldid ) {
# Avoid loops by not generating redirects to the same node or the previous node.
if ( $return_output ) {
if ( $intercept_redirect ) {
return $self->redirect_to_node( $redirect, $id );
} else {
return $self->display_node( id => $redirect,
oldid => $id,
return_output => 1,
);
}
}
print $self->redirect_to_node( $redirect, $id );
return 0;
}
}
# We've undef'ed $version above if this is the current version.
$tt_vars{current} = 1 unless $version;
if ($id eq "RecentChanges") {
$self->display_recent_changes(%args);
} elsif ( $id eq $self->config->home_name ) {
if ( $self->config->recent_changes_on_home_page ) {
my @recent = $wiki->list_recent_changes(
last_n_changes => 10,
metadata_was => { edit_type => "Normal edit" },
);
my $base_url = $config->script_name . '?';
@recent = map {
{
name => CGI->escapeHTML($_->{name}),
last_modified =>
CGI->escapeHTML($_->{last_modified}),
version => CGI->escapeHTML($_->{version}),
comment => OpenGuides::Utils::parse_change_comment(
CGI->escapeHTML($_->{metadata}{comment}[0]),
$base_url,
),
username =>
CGI->escapeHTML($_->{metadata}{username}[0]),
url => $base_url
. CGI->escape($wiki->formatter->node_name_to_node_param($_->{name}))
}
} @recent;
$tt_vars{recent_changes} = \@recent;
}
return %tt_vars if $args{return_tt_vars};
my $output = $self->process_template(
id => $id,
template => "home_node.tt",
tt_vars => \%tt_vars,
http_status => $http_status,
noheaders => $noheaders,
);
return $output if $return_output;
print $output;
} else {
return %tt_vars if $args{return_tt_vars};
my $output = $self->process_template(
id => $id,
template => "node.tt",
tt_vars => \%tt_vars,
http_status => $http_status,
noheaders => $noheaders,
);
return $output if $return_output;
print $output;
}
}
=item B<display_random_page>
$guide->display_random_page;
Display a random page. As with other methods, the C<return_output>
parameter can be used to return the output instead of printing it to STDOUT.
You can also restrict it to a given category and/or locale by supplying
appropriate parameters:
$guide->display_random_page(
category => "pubs",
locale => "bermondsey",
);
The values of these parameters are case-insensitive.
You can make sure this method never returns pages that are themselves
categories and/or locales by setting C<random_page_omits_categories>
and/or C<random_page_omits_locales> in your wiki.conf.
=cut
sub display_random_page {
my ( $self, %args ) = @_;
my $wiki = $self->wiki;
my $config = $self->config;
my ( @catnodes, @locnodes, @nodes );
if ( $args{category} ) {
@catnodes = $wiki->list_nodes_by_metadata(
metadata_type => "category",
metadata_value => $args{category},
ignore_case => 1,
);
}
if ( $args{locale} ) {
@locnodes = $wiki->list_nodes_by_metadata(
metadata_type => "locale",
metadata_value => $args{locale},
ignore_case => 1,
);
}
if ( $args{category} && $args{locale} ) {
# If we have both category and locale, return the intersection.
my %count;
foreach my $node ( @catnodes, @locnodes ) {
$count{$node}++;
}
foreach my $node ( keys %count ) {
push @nodes, $node if $count{$node} > 1;
}
} elsif ( $args{category} ) {
@nodes = @catnodes;
} elsif ( $args{locale} ) {
@nodes = @locnodes;
} else {
@nodes = $wiki->list_all_nodes();
}
my $omit_cats = $config->random_page_omits_categories;
my $omit_locs = $config->random_page_omits_locales;
if ( $omit_cats || $omit_locs ) {
my %all_nodes = map { $_ => $_ } @nodes;
if ( $omit_cats ) {
my @cats = $wiki->list_nodes_by_metadata(
metadata_type => "category",
metadata_value => "category",
ignore_case => 1,
);
foreach my $omit ( @cats ) {
delete $all_nodes{$omit};
}
}
if ( $omit_locs ) {
my @locs = $wiki->list_nodes_by_metadata(
metadata_type => "category",
metadata_value => "locales",
ignore_case => 1,
);
foreach my $omit ( @locs ) {
delete $all_nodes{$omit};
}
}
@nodes = keys %all_nodes;
}
my $node = $nodes[ rand @nodes ];
my $output;
if ( $node ) {
$output = $self->redirect_to_node( $node );
} else {
my %tt_vars = (
category => $args{category},
locale => $args{locale},
);
$output = OpenGuides::Template->output(
wiki => $wiki,
config => $config,
template => "random_page_failure.tt",
vars => \%tt_vars,
);
}
return $output if $args{return_output};
print $output;
}
=item B<display_edit_form>
$guide->display_edit_form(
id => "Vivat Bacchus",
vars => \%vars,
content => $content,
metadata => \%metadata,
checksum => $checksum
);
Display an edit form for the specified node. As with other methods, the
C<return_output> parameter can be used to return the output instead of
printing it to STDOUT.
If this is to redisplay an existing edit, the content, metadata
and checksum may be supplied in those arguments
Extra template variables may be supplied in the vars argument
=cut
sub display_edit_form {
my ($self, %args) = @_;
my $return_output = $args{return_output} || 0;
my $config = $self->config;
my $wiki = $self->wiki;
my $node = $args{id};
my %node_data = $wiki->retrieve_node($node);
my ($content, $checksum) = @node_data{ qw( content checksum ) };
my %cookie_data = OpenGuides::CGI->get_prefs_from_cookie(config=>$config);
my $username = $self->get_cookie( "username" );
my $edit_type = $self->get_cookie( "default_edit_type" ) eq "normal"
? "Normal edit"
: "Minor tidying";
my %metadata_vars = OpenGuides::Template->extract_metadata_vars(
wiki => $wiki,
config => $config,
metadata => $node_data{metadata} );
$metadata_vars{website} ||= 'http://';
my $moderate = $wiki->node_required_moderation($node);
my %tt_vars = ( content => CGI->escapeHTML($content),
checksum => CGI->escapeHTML($checksum),
%metadata_vars,
config => $config,
username => $username,
edit_type => $edit_type,
moderate => $moderate,
deter_robots => 1,
read_only => $config->read_only,
);
# Override some things if we were supplied with them
$tt_vars{content} = $args{content} if $args{content};
$tt_vars{checksum} = $args{checksum} if $args{checksum};
if (defined $args{vars}) {
my %supplied_vars = %{$args{vars}};
foreach my $key ( keys %supplied_vars ) {
$tt_vars{$key} = $supplied_vars{$key};
}
}
if (defined $args{metadata}) {
my %supplied_metadata = %{$args{metadata}};
foreach my $key ( keys %supplied_metadata ) {
$tt_vars{$key} = $supplied_metadata{$key};
}
}
my $output = $self->process_template(
id => $node,
template => "edit_form.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
=item B<preview_edit>
$guide->preview_edit(
id => "Vivat Bacchus",
cgi_obj => $q,
);
Preview the edited version of the specified node. As with other methods, the
C<return_output> parameter can be used to return the output instead of
printing it to STDOUT.
=cut
sub preview_edit {
my ($self, %args) = @_;
my $node = $args{id};
my $q = $args{cgi_obj};
my $return_output = $args{return_output};
my $wiki = $self->wiki;
my $config = $self->config;
my $content = $q->param('content');
$content =~ s/\r\n/\n/gs;
my $checksum = $q->param('checksum');
my %new_metadata = OpenGuides::Template->extract_metadata_vars(
wiki => $wiki,
config => $config,
cgi_obj => $q,
set_coord_field_vars => 1,
);
foreach my $var ( qw( username comment edit_type ) ) {
$new_metadata{$var} = $q->escapeHTML($q->param($var));
}
if ($wiki->verify_checksum($node, $checksum)) {
my $moderate = $wiki->node_required_moderation($node);
my %tt_vars = (
%new_metadata,
config => $config,
content => $q->escapeHTML($content),
preview_html => $wiki->format($content),
preview_above_edit_box => $self->get_cookie(
"preview_above_edit_box" ),
checksum => $q->escapeHTML($checksum),
moderate => $moderate,
read_only => $config->read_only,
);
my $output = $self->process_template(
id => $node,
template => "edit_form.tt",
tt_vars => \%tt_vars,
);
return $output if $args{return_output};
print $output;
} else {
return $self->_handle_edit_conflict(
id => $node,
content => $content,
new_metadata => \%new_metadata,
return_output => $return_output,
);
}
}
=item B<display_prefs_form>
$guide->display_prefs_form;
Displays a form that lets the user view and set their preferences. The
C<return_output> and C<return_tt_vars> parameters can be used to return
the output or template variables, instead of printing the output to STDOUT.
The C<noheaders> parameter can also be used in conjunction with
C<return_output>, if you wish to omit all HTTP headers.
=cut
sub display_prefs_form {
my ($self, %args) = @_;
my $config = $self->config;
my $wiki = $self->wiki;
my $from = $ENV{HTTP_REFERER} || "";
my $url_base = $config->script_url . $config->script_name;
if ( $from !~ /^$url_base/ ) {
$from = "";
}
my %tt_vars = (
not_editable => 1,
show_form => 1,
not_deletable => 1,
return_to_url => $from,
);
return %tt_vars if $args{return_tt_vars};
my $output = OpenGuides::Template->output(
wiki => $wiki,
config => $config,
template => "preferences.tt",
vars => \%tt_vars,
noheaders => $args{noheaders},
);
return $output if $args{return_output};
print $output;
}
=item B<display_recent_changes>
$guide->display_recent_changes;
As with other methods, the C<return_output> parameter can be used to
return the output instead of printing it to STDOUT.
=cut
sub display_recent_changes {
my ($self, %args) = @_;
my $config = $self->config;
my $wiki = $self->wiki;
my $minor_edits = $self->get_cookie( "show_minor_edits_in_rc" );
my $id = $args{id} || $self->config->home_name;
my $return_output = $args{return_output} || 0;
my (%tt_vars, %recent_changes);
# NB the $q stuff below should be removed - we should _always_ do this via
# an argument to the method.
my $q = CGI->new;
my $since = $args{since} || $q->param("since");
if ( $since ) {
$tt_vars{since} = $since;
my $t = localtime($since); # overloaded by Time::Piece
$tt_vars{since_string} = $t->strftime;
my %criteria = ( since => $since );
$criteria{metadata_was} = { edit_type => "Normal edit" }
unless $minor_edits;
my @rc = $self->_get_recent_changes(
config => $config, criteria => \%criteria );
if ( scalar @rc ) {
$recent_changes{since} = \@rc;
}
} else {
# Look at day, week, fortnight, month separately, but make sure things
# don't appear in e.g. "week" if we've already seen them in "day".
my %seen;
for my $days ( [0, 1], [1, 7], [7, 14], [14, 30] ) {
my %criteria = ( between_days => $days );
$criteria{metadata_was} = { edit_type => "Normal edit" }
unless $minor_edits;
my @rc = $self->_get_recent_changes(
config => $config, criteria => \%criteria );
my @filtered;
foreach my $node ( @rc ) {
next if $seen{$node->{name}};
$seen{$node->{name}}++;
push @filtered, $node;
}
if ( scalar @filtered ) {
$recent_changes{$days->[1]} = \@filtered;
}
}
}
$tt_vars{not_editable} = 1;
$tt_vars{recent_changes} = \%recent_changes;
my %processing_args = (
id => $id,
template => "recent_changes.tt",
tt_vars => \%tt_vars,
);
if ( !$since && $self->get_cookie("track_recent_changes_views") ) {
my $cookie =
OpenGuides::CGI->make_recent_changes_cookie(config => $config );
$processing_args{cookies} = $cookie;
$tt_vars{last_viewed} = OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => $config );
}
return %tt_vars if $args{return_tt_vars};
my $output = $self->process_template( %processing_args );
return $output if $return_output;
print $output;
}
sub _get_recent_changes {
my ( $self, %args ) = @_;
my $wiki = $self->wiki;
my $formatter = $wiki->formatter;
my $config = $self->config;
my %criteria = %{ $args{criteria} };
my @rc = $wiki->list_recent_changes( %criteria );
my $base_url = $config->script_name . '?';
# If using metadata_was then we need to pick out just the most recent
# versions.
if ( $criteria{metadata_was} ) {
my %seen;
my @filtered;
foreach my $node ( @rc ) {
next if $seen{$node->{name}};
$seen{$node->{name}}++;
push @filtered, $node;
}
@rc = @filtered;
}
@rc = map {
{
name => CGI->escapeHTML($_->{name}),
last_modified => CGI->escapeHTML($_->{last_modified}),
version => CGI->escapeHTML($_->{version}),
comment => OpenGuides::Utils::parse_change_comment(
CGI->escapeHTML($_->{metadata}{comment}[0]),
$base_url,
),
username => CGI->escapeHTML($_->{metadata}{username}[0]),
host => CGI->escapeHTML($_->{metadata}{host}[0]),
username_param => CGI->escape($_->{metadata}{username}[0]),
edit_type => CGI->escapeHTML($_->{metadata}{edit_type}[0]),
url => $base_url
. CGI->escape($formatter->node_name_to_node_param($_->{name})),
}
} @rc;
return @rc;
}
=item B<display_diffs>
$guide->display_diffs(
id => "Home Page",
version => 6,
other_version => 5,
);
# Or return output as a string (useful for writing tests).
my $output = $guide->display_diffs(
id => "Home Page",
version => 6,
other_version => 5,
return_output => 1,
);
# Or return the hash of variables that will be passed to the template
# (not including those set additionally by OpenGuides::Template).
my %vars = $guide->display_diffs(
id => "Home Page",
version => 6,
other_version => 5,
return_tt_vars => 1,
);
=cut
sub display_diffs {
my ($self, %args) = @_;
my %diff_vars = $self->differ->differences(
node => $args{id},
left_version => $args{version},
right_version => $args{other_version},
);
$diff_vars{not_deletable} = 1;
$diff_vars{not_editable} = 1;
$diff_vars{deter_robots} = 1;
return %diff_vars if $args{return_tt_vars};
my $output = $self->process_template(
id => $args{id},
template => "differences.tt",
tt_vars => \%diff_vars
);
return $output if $args{return_output};
print $output;
}
=item B<find_within_distance>
$guide->find_within_distance(
id => $node,
metres => $q->param("distance_in_metres")
);
=cut
sub find_within_distance {
my ($self, %args) = @_;
my $node = $args{id};
my $metres = $args{metres};
my %data = $self->wiki->retrieve_node( $node );
my $lat = $data{metadata}{latitude}[0];
my $long = $data{metadata}{longitude}[0];
my $script_url = $self->config->script_url;
my $q = CGI->new;
print $q->redirect( $script_url . "search.cgi?lat=$lat;long=$long;distance_in_metres=$metres" );
}
=item B<show_backlinks>
$guide->show_backlinks( id => "Calthorpe Arms" );
As with other methods, parameters C<return_tt_vars> and
C<return_output> can be used to return these things instead of
printing the output to STDOUT.
=cut
sub show_backlinks {
my ($self, %args) = @_;
my $wiki = $self->wiki;
my $formatter = $wiki->formatter;
my @backlinks = $wiki->list_backlinks( node => $args{id} );
my @results = map {
{
url => CGI->escape($formatter->node_name_to_node_param($_)),
title => CGI->escapeHTML($_)
}
} sort @backlinks;
my %tt_vars = ( results => \@results,
num_results => scalar @results,
not_deletable => 1,
deter_robots => 1,
not_editable => 1 );
return %tt_vars if $args{return_tt_vars};
my $output = OpenGuides::Template->output(
node => $args{id},
wiki => $wiki,
config => $self->config,
template=>"backlink_results.tt",
vars => \%tt_vars,
);
return $output if $args{return_output};
print $output;
}
=item B<show_index>
# Show everything in Category: Pubs.
$guide->show_index(
cat => "pubs",
);
# Show all pubs in Holborn.
$guide->show_index(
cat => "pubs",
loc => "holborn",
);
# RDF version of things in Locale: Holborn.
$guide->show_index(
loc => "Holborn",
format => "rdf",
);
# RSS / Atom version (recent changes style).
$guide->show_index(
loc => "Holborn",
format => "rss",
);
# Or return output as a string (useful for writing tests).
$guide->show_index(
cat => "pubs",
return_output => 1,
);
# Return output as a string with HTTP headers omitted (for tests).
$guide->show_index(
cat => "pubs",
return_output => 1,
noheaders => 1,
);
# Or return the template variables (again, useful for writing tests).
$guide->show_index(
cat => "pubs",
format => "map"
return_tt_vars => 1,
);
If neither C<cat> or C<loc> is supplied, then all pages will be returned.
The recommended format of parameters to this method changed to the
above in version 0.67 of OpenGuides, though older invocations are
still supported and will redirect to the new URL format.
If you pass the C<return_output> or C<return_tt_vars> parameters, and a
redirect is required, this method will fake the redirect and return the
output/variables that will actually end up being viewed by the user. If
instead you want to see the HTTP headers that will be printed in order to
perform the redirect, pass the C<intercept_redirect> parameter as well.
The C<intercept_redirect> parameter has no effect if no redirect is required,
or if the C<return_output>/C<return_tt_vars> parameter is omitted.
The C<noheaders> parameter only takes effect if C<return_output> is true
and C<intercept_redirect> is false or omitted.
=cut
sub show_index {
my ($self, %args) = @_;
my $wiki = $self->wiki;
my $formatter = $wiki->formatter;
my $use_leaflet = $self->config->use_leaflet;
my %tt_vars;
my @selnodes;
if ( $args{type} and $args{value} ) {
if ( $args{type} eq "fuzzy_title_match" ) {
my %finds = $wiki->fuzzy_title_match( $args{value} );
@selnodes = sort { $finds{$a} <=> $finds{$b} } keys %finds;
$tt_vars{criterion} = {
type => $args{type}, # for RDF version
value => $args{value}, # for RDF version
name => CGI->escapeHTML("Fuzzy Title Match on '$args{value}'")
};
$tt_vars{not_editable} = 1;
} else {
return $self->_do_old_style_index_search( %args );
}
} else {
# OK, we either show everything, or do a new-style cat/loc search.
my $cat = $args{cat} || "";
my $loc = $args{loc} || "";
my ( $type, $value, @names, @criteria );
if ( !$cat && !$loc ) {
@selnodes = $wiki->list_all_nodes();
} else {
my ( @catnodes, @locnodes );
if ( $cat ) {
@catnodes = $wiki->list_nodes_by_metadata(
metadata_type => "category",
metadata_value => $cat,
ignore_case => 1
);
my $name = "Category $cat";
$name =~ s/(\s\w)/\U$1/g;
push @criteria, {
type => "category",
value => $cat,
name => $name,
param => $formatter->node_name_to_node_param( $name ),
};
push @names, $name;
}
if ( $loc ) {
@locnodes = $wiki->list_nodes_by_metadata(
metadata_type => "locale",
metadata_value => $loc,
ignore_case => 1
);
my $name = "Locale $loc";
$name =~ s/(\s\w)/\U$1/g;
push @criteria, {
type => "locale",
value => $loc,
name => $name,
param => $formatter->node_name_to_node_param( $name ),
};
push @names, $name;
}
if ( $cat && !$loc ) {
@selnodes = @catnodes;
} elsif ( $loc && !$cat ) {
@selnodes = @locnodes;
} else {
# Intersect the category and locale results.
my %count = ();
foreach my $node ( @catnodes, @locnodes ) { $count{$node}++; }
foreach my $node ( keys %count ) {
push @selnodes, $node if $count{$node} > 1;
}
}
$tt_vars{criteria_title} = join( " and ", @names );
$tt_vars{criteria} = \@criteria;
$tt_vars{not_editable} = 1;
}
$tt_vars{page_description} =
OpenGuides::Utils->get_index_page_description(
format => $args{format} || "",
criteria => \@criteria,
);
my $feed_base = $self->config->script_url
. $self->config->script_name . "?action=index";
foreach my $criterion ( @criteria ) {
if ( $criterion->{type} eq "category" ) {
$feed_base .= ";cat=" . lc( $criterion->{value} );
} elsif ( $criterion->{type} eq "locale" ) {
$feed_base .= ";loc=" . lc( $criterion->{value} );
}
}
my @dropdowns = OpenGuides::CGI->make_index_form_dropdowns(
guide => $self,
selected => \@criteria );
$tt_vars{index_form_fields} = \@dropdowns;
$tt_vars{feed_base} = $feed_base;
}
my @nodes = map {
{
name => $_,
node_data => { $wiki->retrieve_node( name => $_ ) },
param => $formatter->node_name_to_node_param($_) }
} sort @selnodes;
# Convert the lat+long to WGS84 as required, and count how many nodes
# we have for the map (if using Leaflet).
my $nodes_on_map;
for(my $i=0; $i<scalar @nodes;$i++) {
my $node = $nodes[$i];
if($node) {
my %metadata = %{$node->{node_data}->{metadata}};
my ($wgs84_long, $wgs84_lat);
eval {
($wgs84_long, $wgs84_lat) = OpenGuides::Utils->get_wgs84_coords(
longitude => $metadata{longitude}[0],
latitude => $metadata{latitude}[0],
config => $self->config);
};
warn $@." on ".$metadata{latitude}[0]." ".$metadata{longitude}[0] if $@;
push @{$nodes[$i]->{node_data}->{metadata}->{wgs84_long}}, $wgs84_long;
push @{$nodes[$i]->{node_data}->{metadata}->{wgs84_lat}}, $wgs84_lat;
if ( $use_leaflet ) {
if ( defined $wgs84_lat && $wgs84_lat =~ /^[-.\d]+$/
&& defined $wgs84_long && $wgs84_long =~ /^[-.\d]+$/ ) {
$node->{has_geodata} = 1;
$node->{wgs84_lat} = $wgs84_lat;
$node->{wgs84_long} = $wgs84_long;
$nodes_on_map++;
}
}
}
}
$tt_vars{nodes} = \@nodes;
my ($template, %conf);
if ( $args{format} ) {
if ( $args{format} eq "rdf" ) {
$template = "rdf_index.tt";
$conf{content_type} = "application/rdf+xml";
} elsif ( $args{format} eq "json" ) {
$template = "json_index.tt";
$conf{content_type} = "text/javascript";
} elsif ( $args{format} eq "plain" ) {
$template = "plain_index.tt";
$conf{content_type} = "text/plain";
} elsif ( $args{format} eq "map" ) {
$tt_vars{display_google_maps} = 1; # override for this page
if ( $use_leaflet ) {
if ( $nodes_on_map ) {
my @points = map {
{ wgs84_lat =>
$_->{node_data}->{metadata}->{wgs84_lat}[0],
wgs84_long =>
$_->{node_data}->{metadata}->{wgs84_long}[0]
}
} @nodes;
my %minmaxdata = OpenGuides::Utils->get_wgs84_min_max(
nodes => \@points );
%tt_vars = ( %tt_vars, %minmaxdata );
} else {
$tt_vars{no_nodes_on_map} = 1;
}
$template = "map_index_leaflet.tt";
} else {
my $q = CGI->new;
$tt_vars{zoom} = $q->param('zoom') || '';
$tt_vars{lat} = $q->param('lat') || '';
$tt_vars{long} = $q->param('long') || '';
$tt_vars{map_type} = $q->param('map_type') || '';
$tt_vars{centre_long} = $self->config->centre_long;
$tt_vars{centre_lat} = $self->config->centre_lat;
$tt_vars{default_gmaps_zoom}
= $self->config->default_gmaps_zoom;
$tt_vars{enable_gmaps} = 1;
$template = "map_index.tt";
}
} elsif( $args{format} eq "rss" || $args{format} eq "atom") {
# They really wanted a recent changes style rss/atom feed
my $feed_type = $args{format};
my ($feed,$content_type) = $self->get_feed_and_content_type($feed_type);
my ($name, $params );
if ( $args{cat} ) {
$name = "Index of Category $args{cat}";
$params = "action=index;cat=$args{cat}";
} else {
$name = "Index of Locale $args{loc}";
$params = "action=index;loc=$args{loc}";
}
$feed->set_feed_name_and_url_params( $name, $params );
# Grab the actual node data out of @nodes
my @node_data;
foreach my $node (@nodes) {
$node->{node_data}->{name} = $node->{name};
push @node_data, $node->{node_data};
}
my $output = "Content-Type: ".$content_type."\n";
$output .= $feed->build_feed_for_nodes($feed_type, @node_data);
return $output if $args{return_output};
print $output;
return;
}
} else {
$template = "site_index.tt";
}
return %tt_vars if $args{return_tt_vars};
%conf = (
%conf,
template => $template,
tt_vars => \%tt_vars,
);
if ( $args{return_output} && !$args{intercept_redirect} ) {
$conf{noheaders} = $args{noheaders};
}
my $output = $self->process_template( %conf );
return $output if $args{return_output};
print $output;
}
# Deal with legacy URLs/tests.
sub _do_old_style_index_search {
my ( $self, %args ) = @_;
if ( ( $args{return_output} || $args{return_tt_vars} ) ) {
if ( $args{intercept_redirect} ) {
return $self->redirect_index_search( %args );
} else {
my $type = delete $args{type};
my $value = delete $args{value};
if ( $type eq "category" ) {
return $self->show_index( %args, cat => $value );
} elsif ( $type eq "locale" ) {
return $self->show_index( %args, loc => $value );
} else {
return $self->show_index( %args );
}
}
} else {
print $self->redirect_index_search( %args );
}
}
=item B<show_metadata>
$guide->show_metadata();
$guide->show_metadata(type => "category");
$guide->show_metadata(type => "category", format => "json");
Lists all metadata types, or all metadata values of a given
type. Useful for programatically discovering a guide.
As with other methods, parameters C<return_tt_vars> and
C<return_output> can be used to return these things instead of
printing the output to STDOUT.
=cut
sub show_metadata {
my ($self, %args) = @_;
my $wiki = $self->wiki;
my $formatter = $wiki->formatter;
my @values;
my $type;
my $may_descend = 0;
if($args{"type"} && $args{"type"} ne "metadata_type") {
$type = $args{"type"};
@values = $wiki->store->list_metadata_by_type($args{"type"});
} else {
$may_descend = 1;
$type = "metadata_type";
@values = $wiki->store->list_metadata_names;
}
my %tt_vars = ( type => $type,
may_descend => $may_descend,
metadata => \@values,
num_results => scalar @values,
not_deletable => 1,
deter_robots => 1,
not_editable => 1 );
return %tt_vars if $args{return_tt_vars};
my $output;
my $content_type;
if($args{"format"}) {
if($args{"format"} eq "json") {
$content_type = "text/javascript";
my $json = OpenGuides::JSON->new( wiki => $wiki,
config => $self->config );
$output = $json->output_as_json(
$type => \@values
);
}
}
unless($output) {
$output = OpenGuides::Template->output(
wiki => $wiki,
config => $self->config,
template=>"metadata.tt",
vars => \%tt_vars,
);
}
return $output if $args{return_output};
if($content_type) {
print "Content-type: $content_type\n\n";
}
print $output;
}
=item B<list_all_versions>
$guide->list_all_versions ( id => "Home Page" );
# Or return output as a string (useful for writing tests).
$guide->list_all_versions (
id => "Home Page",
return_output => 1,
);
# Or return the hash of variables that will be passed to the template
# (not including those set additionally by OpenGuides::Template).
$guide->list_all_versions (
id => "Home Page",
return_tt_vars => 1,
);
=cut
sub list_all_versions {
my ($self, %args) = @_;
my $return_output = $args{return_output} || 0;
my $node = $args{id};
my %curr_data = $self->wiki->retrieve_node($node);
my $curr_version = $curr_data{version};
my @history;
for my $version ( 1 .. $curr_version ) {
my %node_data = $self->wiki->retrieve_node( name => $node,
version => $version );
# $node_data{version} will be zero if this version was deleted.
push @history, {
version => CGI->escapeHTML( $version ),
modified => CGI->escapeHTML( $node_data{last_modified} ),
username => CGI->escapeHTML( $node_data{metadata}{username}[0] ),
comment => OpenGuides::Utils::parse_change_comment(
CGI->escapeHTML( $node_data{metadata}{comment}[0] ),
$self->config->script_name . '?',
),
} if $node_data{version};
}
@history = reverse @history;
my %tt_vars = (
node => $node,
version => $curr_version,
not_deletable => 1,
not_editable => 1,
deter_robots => 1,
history => \@history
);
return %tt_vars if $args{return_tt_vars};
my $output = $self->process_template(
id => $node,
template => "node_history.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
=item B<get_feed_and_content_type>
Fetch the OpenGuides feed object, and the output content type, for the
supplied feed type.
Handles all the setup for the OpenGuides feed object.
=cut
sub get_feed_and_content_type {
my ($self, $feed_type) = @_;
my $feed = OpenGuides::Feed->new(
wiki => $self->wiki,
config => $self->config,
og_version => $VERSION,
);
my $content_type = $feed->default_content_type($feed_type);
return ($feed, $content_type);
}
=item B<display_feed>
# Last ten non-minor edits to Hammersmith pages in RSS 1.0 format
$guide->display_feed(
feed_type => 'rss',
feed_listing => 'recent_changes',
items => 10,
ignore_minor_edits => 1,
locale => "Hammersmith",
);
# All edits bob has made to pub pages in the last week in Atom format
$guide->display_feed(
feed_type => 'atom',
feed_listing => 'recent_changes',
days => 7,
username => "bob",
category => "Pubs",
);
C<feed_type> is a mandatory parameter. Supported values at present are
"rss" and "atom".
C<feed_listing> is a mandatory parameter. Supported values at present
are "recent_changes". (More values are coming soon though!)
As with other methods, the C<return_output> parameter can be used to
return the output instead of printing it to STDOUT.
=cut
sub display_feed {
my ($self, %args) = @_;
my $feed_type = $args{feed_type};
croak "No feed type given" unless $feed_type;
my $feed_listing = $args{feed_listing};
croak "No feed listing given" unless $feed_listing;
my $return_output = $args{return_output} ? 1 : 0;
# Basic criteria, whatever the feed listing type is
my %criteria = (
feed_type => $feed_type,
feed_listing => $feed_listing,
also_return_timestamp => 1,
);
# Feed listing specific criteria
if($feed_listing eq "recent_changes") {
$criteria{items} = $args{items} || "";
$criteria{days} = $args{days} || "";
$criteria{ignore_minor_edits} = $args{ignore_minor_edits} ? 1 : 0;
my $username = $args{username} || "";
my $category = $args{category} || "";
my $locale = $args{locale} || "";
my %filter;
$filter{username} = $username if $username;
$filter{category} = $category if $category;
$filter{locale} = $locale if $locale;
if ( scalar keys %filter ) {
$criteria{filter_on_metadata} = \%filter;
}
}
elsif($feed_listing eq "node_all_versions") {
$criteria{name} = $args{name};
}
# Get the feed object, and the content type
my ($feed,$content_type) = $self->get_feed_and_content_type($feed_type);
my $output = "Content-Type: ".$content_type;
if($self->config->http_charset) {
$output .= "; charset=".$self->config->http_charset;
}
$output .= "\n";
# Get the feed, and the timestamp, in one go
my ($feed_output, $feed_timestamp) =
$feed->make_feed( %criteria );
my $maker = $feed->fetch_maker($feed_type);
$output .= "Last-Modified: " . ($maker->parse_feed_timestamp($feed_timestamp))->strftime('%a, %d %b %Y %H:%M:%S +0000') . "\n\n";
$output .= $feed_output;
return $output if $return_output;
print $output;
}
=item B<display_about>
print $guide->display_about(format => "rdf");
Displays static 'about' information in various format. Defaults to HTML.
=cut
sub display_about {
my ($self, %args) = @_;
my $output;
if ($args{format} && $args{format} =~ /^rdf$/i) {
$output = qq{Content-Type: application/rdf+xml
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns = "http://usefulinc.com/ns/doap#"
xmlns:rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:foaf = "http://xmlns.com/foaf/0.1/">
<Project rdf:ID="OpenGuides">
<name>OpenGuides</name>
<created>2003-04-29</created>
<shortdesc xml:lang="en">
A wiki engine for collaborative description of places with specialised
geodata metadata features.
</shortdesc>
<description xml:lang="en">
OpenGuides is a collaborative wiki environment, written in Perl, for
building guides and sharing information, as both human-readable text
and RDF. The engine contains a number of geodata-specific metadata
mechanisms such as locale search, node classification and integration
with Google Maps.
</description>
<homepage rdf:resource="http://openguides.org/" />
<mailing-list rdf:resource="http://lists.openguides.org/mailman/listinfo/openguides-dev/" />
<mailing-list rdf:resource="http://urchin.earth.li/mailman/listinfo/openguides-commits/" />
<maintainer>
<foaf:Person rdf:ID="OpenGuidesMaintainer">
<foaf:name>Dominic Hargreaves</foaf:name>
<foaf:homepage rdf:resource="http://www.larted.org.uk/~dom/" />
</foaf:Person>
</maintainer>
<repository>
<SVNRepository rdf:ID="OpenGuidesSVN">
<location rdf:resource="https://urchin.earth.li/svn/openguides/" />
<browse rdf:resource="http://dev.openguides.org/browser" />
</SVNRepository>
</repository>
<release>
<Version rdf:ID="OpenGuidesVersion">
<revision>$VERSION</revision>
</Version>
</release>
<download-page rdf:resource="http://search.cpan.org/dist/OpenGuides/" />
<!-- Freshmeat category: Internet :: WWW/HTTP :: Dynamic Content -->
<category rdf:resource="http://freshmeat.net/browse/92/" />
<license rdf:resource="http://www.opensource.org/licenses/gpl-license.php" />
<license rdf:resource="http://www.opensource.org/licenses/artistic-license.php" />
</Project>
</rdf:RDF>};
} elsif ($args{format} && $args{format} eq 'opensearch') {
my $site_name = $self->config->site_name;
my $search_url = $self->config->script_url . 'search.cgi';
my $contact_email = $self->config->contact_email;
$output = qq{Content-Type: application/opensearchdescription+xml; charset=utf-8
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>$site_name</ShortName>
<Description>Search the site '$site_name'</Description>
<Tags>$site_name</Tags>
<Contact>$contact_email</Contact>
<Url type="application/atom+xml"
template="$search_url?search={searchTerms};format=atom"/>
<Url type="application/rss+xml"
template="$search_url?search={searchTerms};format=rss"/>
<Url type="text/html"
template="$search_url?search={searchTerms}"/>
<Query role="example" searchTerms="pubs"/>
</OpenSearchDescription>};
} else {
my $site_name = $self->config->{site_name};
my $script_name = $self->config->{script_name};
$output = qq{Content-Type: text/html; charset=utf-8
<html>
<head>
<title>About $site_name</title>
<style type="text/css">
body { margin: 0px; }
#content { padding: 50px; margin: auto; width: 50%; }
h1 { margin-bottom: 0px; font-style: italic; }
h2 { margin-top: 0px; }
#logo { text-align: center; }
#about { margin: 0em 0em 1em 0em; border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }
#meta { font-size: small; text-align: center;}
</style>
<link rel="alternate"
type="application/rdf+xml"
title="DOAP (Description Of A Project) profile for this site's software"
href="$script_name?action=about;format=rdf" />
</head>
<body>
<div id="content">
<div id="logo">
<a href="http://openguides.org/"><img
src="http://openguides.org/img/logo.png" alt="OpenGuides"></a>
<h1><a href="$script_name">$site_name</a></h1>
<h2>is powered by <a href="http://openguides.org/">OpenGuides</a> -<br>
the guides made by you.</h2>
<h3>version <a href="http://search.cpan.org/~dom/OpenGuides-$VERSION">$VERSION</a></h3>
</div>
<div id="about">
<p>
<a href="http://www.w3.org/RDF/"><img
src="http://openguides.org/img/rdf_icon.png" width="44" height="48"
style="float: right; margin-left: 10px; border: 0px"></a> OpenGuides is a
web-based collaborative <a href="http://wiki.org/wiki.cgi?WhatIsWiki">wiki</a>
environment for building guides and sharing information, as both
human-readable text and <a href="http://www.w3.org/RDF/"><acronym
title="Resource Description Framework">RDF</acronym></a>. The engine contains
a number of geodata-specific metadata mechanisms such as locale search, node
classification and integration with <a href="http://maps.google.com/">Google
Maps</a>.
</p>
<p>
OpenGuides is written in <a href="http://www.perl.org/">Perl</a>, and is
made available under the same license as Perl itself (dual <a
href="http://dev.perl.org/licenses/artistic.html" title='The "Artistic Licence"'>Artistic</a> and <a
href="http://www.opensource.org/licenses/gpl-license.php"><acronym
title="GNU Public Licence">GPL</acronym></a>). Developer information for the
project is available from the <a href="http://dev.openguides.org/">OpenGuides
development site</a>.
</p>
<p>
Copyright ©2003-2008, <a href="http://openguides.org/">The OpenGuides
Project</a>. "OpenGuides", "[The] Open Guide To..." and "The guides made by
you" are trademarks of The OpenGuides Project. Any uses on this site are made
with permission.
</p>
</div>
<div id="meta">
<a href="$script_name?action=about;format=rdf"><acronym
title="Description Of A Project">DOAP</acronym> RDF version of this
information</a>
</div>
</div>
</body>
</html>};
}
return $output if $args{return_output};
print $output;
}
=item B<commit_node>
$guide->commit_node(
id => $node,
cgi_obj => $q,
);
As with other methods, parameters C<return_tt_vars> and
C<return_output> can be used to return these things instead of
printing the output to STDOUT.
If you have specified the C<spam_detector_module> option in your
C<wiki.conf>, this method will attempt to call the <looks_like_spam>
method of that module to determine whether the edit is spam. If this
method returns true, then the C<spam_detected.tt> template will be
used to display an error message.
The C<looks_like_spam> method will be passed a datastructure containing
content and metadata.
The geographical data that you should provide in the L<CGI> object
depends on the handler you chose in C<wiki.conf>.
=over
=item *
B<British National Grid> - provide either C<os_x> and C<os_y> or
C<latitude> and C<longitude>; whichever set of data you give, it will
be converted to the other and both sets will be stored.
=item *
B<Irish National Grid> - provide either C<osie_x> and C<osie_y> or
C<latitude> and C<longitude>; whichever set of data you give, it will
be converted to the other and both sets will be stored.
=item *
B<UTM ellipsoid> - provide C<latitude> and C<longitude>; these will be
converted to easting and northing and both sets of data will be stored.
=back
=cut
sub commit_node {
my ($self, %args) = @_;
my $node = $args{id};
my $q = $args{cgi_obj};
my $return_output = $args{return_output};
my $wiki = $self->wiki;
my $config = $self->config;
my $content = $q->param("content");
$content =~ s/\r\n/\n/gs;
my $checksum = $q->param("checksum");
my %new_metadata = OpenGuides::Template->extract_metadata_vars(
wiki => $wiki,
config => $config,
cgi_obj => $q
);
delete $new_metadata{website} if $new_metadata{website} eq 'http://';
$new_metadata{opening_hours_text} = $q->param("hours_text") || "";
# Pick out the unmunged versions of lat/long if they're set.
# (If they're not, it means they weren't munged in the first place.)
$new_metadata{latitude} = delete $new_metadata{latitude_unmunged}
if $new_metadata{latitude_unmunged};
$new_metadata{longitude} = delete $new_metadata{longitude_unmunged}
if $new_metadata{longitude_unmunged};
foreach my $var ( qw( summary username comment edit_type ) ) {
$new_metadata{$var} = $q->param($var) || "";
}
$new_metadata{host} = $ENV{REMOTE_ADDR};
# Wiki::Toolkit::Plugin::RSS::ModWiki wants "major_change" to be set.
$new_metadata{major_change} = ( $new_metadata{edit_type} eq "Normal edit" )
? 1
: 0;
# General validation
my $fails = OpenGuides::Utils->validate_edit(
cgi_obj => $q
);
if ( scalar @{$fails} or $config->read_only ) {
my %vars = (
validate_failed => $fails
);
my $output = $self->display_edit_form(
id => $node,
content => CGI->escapeHTML($content),
metadata => \%new_metadata,
vars => \%vars,
checksum => CGI->escapeHTML($checksum),
return_output => 1,
read_only => $config->read_only,
);
return $output if $return_output;
print $output;
return;
}
# If we can, check to see if this edit looks like spam.
my $spam_detector = $config->spam_detector_module;
my $is_spam;
if ( $spam_detector ) {
eval {
eval "require $spam_detector";
$is_spam = $spam_detector->looks_like_spam(
node => $node,
content => $content,
metadata => \%new_metadata,
);
};
}
if ( $is_spam ) {
my $output = OpenGuides::Template->output(
wiki => $self->wiki,
config => $config,
template => "spam_detected.tt",
vars => {
not_editable => 1,
},
);
return $output if $return_output;
print $output;
return;
}
# Check to make sure all the indexable nodes are created
# Skip this for nodes needing moderation - this occurs for them once
# they've been moderated
my $needs_moderation = $wiki->node_required_moderation($node);
my $in_moderate_whitelist
= OpenGuides::Utils->in_moderate_whitelist($self->config, $new_metadata{host});
if ( $in_moderate_whitelist or not $needs_moderation ) {
$self->_autoCreateCategoryLocale(
id => $node,
metadata => \%new_metadata
);
}
my $written = $wiki->write_node( $node, $content, $checksum,
\%new_metadata );
if ($written) {
if ( $needs_moderation ) {
if ( $in_moderate_whitelist ) {
$self->wiki->moderate_node(
name => $node,
version => $written
);
}
elsif ( $config->send_moderation_notifications ) {
my $body = "The node '$node' in the OpenGuides installation\n" .
"'" . $config->site_name . "' requires moderation. ".
"Please visit\n" .
$config->script_url . $config->script_name .
"?action=show_needing_moderation\nat your convenience.\n";
eval {
OpenGuides::Utils->send_email(
config => $config,
subject => "Node requires moderation",
body => $body,
admin => 1,
return_output => $return_output
);
};
warn $@ if $@;
}
}
my $output = $self->redirect_to_node($node);
return $output if $return_output;
print $output;
} else {
return $self->_handle_edit_conflict(
id => $node,
content => $content,
new_metadata => \%new_metadata,
return_output => $return_output,
);
}
}
sub _handle_edit_conflict {
my ($self, %args) = @_;
my $return_output = $args{return_output} || 0;
my $config = $self->config;
my $wiki = $self->wiki;
my $node = $args{id};
my $content = $args{content};
my %new_metadata = %{$args{new_metadata}};
my %node_data = $wiki->retrieve_node($node);
my %tt_vars = ( checksum => $node_data{checksum},
new_content => $content,
content => $node_data{content} );
my %old_metadata = OpenGuides::Template->extract_metadata_vars(
wiki => $wiki,
config => $config,
metadata => $node_data{metadata} );
# Make sure we look at all variables.
my @tmp = (keys %new_metadata, keys %old_metadata );
my %tmp_hash = map { $_ => 1; } @tmp;
my @all_vars = keys %tmp_hash;
foreach my $mdvar ( keys %new_metadata ) {
if ($mdvar eq "locales") {
$tt_vars{$mdvar} = $old_metadata{locales};
$tt_vars{"new_$mdvar"} = $new_metadata{locale};
} elsif ($mdvar eq "categories") {
$tt_vars{$mdvar} = $old_metadata{categories};
$tt_vars{"new_$mdvar"} = $new_metadata{category};
} elsif ($mdvar eq "username" or $mdvar eq "comment"
or $mdvar eq "edit_type" ) {
$tt_vars{$mdvar} = $new_metadata{$mdvar};
} else {
$tt_vars{$mdvar} = $old_metadata{$mdvar};
$tt_vars{"new_$mdvar"} = $new_metadata{$mdvar};
}
}
$tt_vars{coord_field_1} = $old_metadata{coord_field_1};
$tt_vars{coord_field_2} = $old_metadata{coord_field_2};
$tt_vars{coord_field_1_value} = $old_metadata{coord_field_1_value};
$tt_vars{coord_field_2_value} = $old_metadata{coord_field_2_value};
$tt_vars{"new_coord_field_1_value"}
= $new_metadata{$old_metadata{coord_field_1}};
$tt_vars{"new_coord_field_2_value"}
= $new_metadata{$old_metadata{coord_field_2}};
$tt_vars{conflict} = 1;
return %tt_vars if $args{return_tt_vars};
my $output = $self->process_template(
id => $node,
template => "edit_form.tt",
tt_vars => \%tt_vars,
);
return $output if $args{return_output};
print $output;
}
=item B<_autoCreateCategoryLocale>
$guide->_autoCreateCategoryLocale(
id => "FAQ",
metadata => \%metadata,
);
When a new node is added, or a previously un-moderated node is moderated,
identifies if any of its Categories or Locales are missing, and creates them.
Guide admins can control the text that gets put into the content field of the
autocreated node by putting it in custom_autocreate_content.tt in their custom
templates directory. The following TT variables will be available to the
template:
=over
=item * index_type (e.g. C<Category>)
=item * index_value (e.g. C<Vegan-friendly>)
=item * node_name (e.g. C<Category Vegan-Friendly>)
=back
(Note capitalisation - index_value is what they typed in to the form, and
node_name is the fully free-upper-ed name of the autocreated node.)
For nodes not requiring moderation, should be called on writing the node
For nodes requiring moderation, should only be called on moderation
=cut
sub _autoCreateCategoryLocale {
my ($self, %args) = @_;
my $wiki = $self->wiki;
my $id = $args{'id'};
my %metadata = %{$args{'metadata'}};
# Check to make sure all the indexable nodes are created
my $config = $self->config;
my $template_path = $config->template_path;
my $custom_template_path = $config->custom_template_path || "";
my $tt = Template->new( { INCLUDE_PATH =>
"$custom_template_path:$template_path" } );
foreach my $type (qw(Category Locale)) {
my $lctype = lc($type);
foreach my $index (@{$metadata{$lctype}}) {
$index =~ s/(.*)/\u$1/;
my $node = $type . " " . $index;
# Uppercase the node name before checking for existence
$node = $wiki->formatter->_do_freeupper( $node );
unless ( $wiki->node_exists($node) ) {
my $category = $type eq "Category" ? "Category" : "Locales";
# Try to get the autocreated content from a custom template;
# if we fail, use some default text.
my $blurb;
my %tt_vars = (
index_type => $type,
index_value => $index,
node_name => $node,
);
my $ok = $tt->process( "custom_autocreate_content.tt",
\%tt_vars, \$blurb );
if ( !$ok ) {
$ok = $tt->process( "autocreate_content.tt",
\%tt_vars, \$blurb );
}
if ( !$ok ) {
$blurb = "\@INDEX_LINK [[$node]]";
}
$wiki->write_node(
$node,
$blurb,
undef,
{
username => "Auto Create",
comment => "Auto created $lctype stub page",
category => $category
}
);
}
}
}
}
=item B<delete_node>
$guide->delete_node(
id => "FAQ",
version => 15,
password => "beer",
);
C<version> is optional - if it isn't supplied then all versions of the
node will be deleted; in other words the node will be entirely
removed.
If C<password> is not supplied then a form for entering the password
will be displayed.
As with other methods, parameters C<return_tt_vars> and
C<return_output> can be used to return these things instead of
printing the output to STDOUT.
=cut
sub delete_node {
my ($self, %args) = @_;
my $node = $args{id} or croak "No node ID supplied for deletion";
my $return_tt_vars = $args{return_tt_vars} || 0;
my $return_output = $args{return_output} || 0;
my %tt_vars = (
not_editable => 1,
not_deletable => 1,
deter_robots => 1,
);
$tt_vars{delete_version} = $args{version} || "";
my $password = $args{password};
if ($password) {
if ($password ne $self->config->admin_pass) {
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "delete_password_wrong.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
} else {
$self->wiki->delete_node(
name => $node,
version => $args{version},
);
# Check whether any versions of this node remain.
my %check = $self->wiki->retrieve_node( name => $node );
$tt_vars{other_versions_remain} = 1 if $check{version};
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "delete_done.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
} else {
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "delete_confirm.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
}
=item B<set_node_moderation>
$guide->set_node_moderation(
id => "FAQ",
password => "beer",
moderation_flag => 1,
);
Sets the moderation needed flag on a node, either on or off.
If C<password> is not supplied then a form for entering the password
will be displayed.
=cut
sub set_node_moderation {
my ($self, %args) = @_;
my $node = $args{id} or croak "No node ID supplied for node moderation";
my $return_tt_vars = $args{return_tt_vars} || 0;
my $return_output = $args{return_output} || 0;
# Get the moderation flag into something sane
if($args{moderation_flag} eq "1" || $args{moderation_flag} eq "yes" ||
$args{moderation_flag} eq "on" || $args{moderation_flag} eq "true") {
$args{moderation_flag} = 1;
} else {
$args{moderation_flag} = 0;
}
# Set up the TT variables
my %tt_vars = (
not_editable => 1,
not_deletable => 1,
deter_robots => 1,
moderation_action => 'set_moderation',
moderation_flag => $args{moderation_flag},
moderation_url_args => 'action=set_moderation;moderation_flag='.$args{moderation_flag},
);
my $password = $args{password};
if ($password) {
if ($password ne $self->config->admin_pass) {
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "moderate_password_wrong.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
} else {
my $worked = $self->wiki->set_node_moderation(
name => $node,
required => $args{moderation_flag},
);
my $moderation_flag = "changed";
unless($worked) {
$moderation_flag = "unknown_node";
warn("Tried to set moderation status on node '$node', which doesn't exist");
}
# Send back to the admin interface
my $script_url = $self->config->script_url;
my $script_name = $self->config->script_name;
my $q = CGI->new;
my $output = $q->redirect( $script_url.$script_name."?action=admin;moderation=".$moderation_flag );
return $output if $return_output;
print $output;
}
} else {
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "moderate_confirm.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
}
=item B<moderate_node>
$guide->moderate_node(
id => "FAQ",
version => 12,
password => "beer",
);
Marks a version of a node as moderated. Will also auto-create and Locales
and Categories for the newly moderated version.
If C<password> is not supplied then a form for entering the password
will be displayed.
=cut
sub moderate_node {
my ($self, %args) = @_;
my $node = $args{id} or croak "No node ID supplied for node moderation";
my $version = $args{version} or croak "No node version supplied for node moderation";
my $return_tt_vars = $args{return_tt_vars} || 0;
my $return_output = $args{return_output} || 0;
# Set up the TT variables
my %tt_vars = (
not_editable => 1,
not_deletable => 1,
deter_robots => 1,
version => $version,
moderation_action => 'moderate',
moderation_url_args => 'action=moderate;version='.$version
);
my $password = $args{password};
unless($self->config->moderation_requires_password) {
$password = $self->config->admin_pass;
}
if ($password) {
if ($password ne $self->config->admin_pass) {
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "moderate_password_wrong.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
} else {
$self->wiki->moderate_node(
name => $node,
version => $version
);
# Create any categories or locales for it
my %details = $self->wiki->retrieve_node(
name => $node,
version => $version
);
$self->_autoCreateCategoryLocale(
id => $node,
metadata => $details{'metadata'}
);
# Send back to the admin interface
my $script_url = $self->config->script_url;
my $script_name = $self->config->script_name;
my $q = CGI->new;
my $output = $q->redirect( $script_url.$script_name."?action=admin;moderation=moderated" );
return $output if $return_output;
print $output;
}
} else {
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => $node,
template => "moderate_confirm.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
}
=item B<show_missing_metadata>
Search for nodes which don't have a certain kind of metadata. Excludes nodes
which are pure redirects, and optionally also excludes locales and categories.
=cut
sub show_missing_metadata {
my ($self, %args) = @_;
my $return_tt_vars = $args{return_tt_vars} || 0;
my $return_output = $args{return_output} || 0;
my $wiki = $self->wiki;
my $formatter = $self->wiki->formatter;
my $script_name = $self->config->script_name;
my $use_leaflet = $self->config->use_leaflet;
my ( $metadata_type, $metadata_value, $exclude_locales,
$exclude_categories, $format)
= @args{ qw( metadata_type metadata_value exclude_locales
exclude_categories format ) };
$format ||= "";
my @nodes;
my $done_search = 0;
my $nodes_on_map;
# Only search if they supplied at least a metadata type
if($metadata_type) {
$done_search = 1;
my @all_nodes = $wiki->list_nodes_by_missing_metadata(
metadata_type => $metadata_type,
metadata_value => $metadata_value,
ignore_case => 1,
);
# Filter out redirects; also filter out locales/categories if required.
foreach my $node ( sort @all_nodes ) {
next if ( $exclude_locales && $node =~ /^Locale / );
next if ( $exclude_categories && $node =~ /^Category / );
my %data = $wiki->retrieve_node( $node );
next if OpenGuides::Utils->detect_redirect(
content => $data{content} );
my $node_param = $formatter->node_name_to_node_param( $node );
my %this_node = (
name => $node,
param => $node_param,
address => $data{metadata}{address}[0],
view_url => "$script_name?$node_param",
edit_url => "$script_name?id=$node_param;action=edit",
);
if ( $format eq "map" && $use_leaflet ) {
my ( $wgs84_long, $wgs84_lat )
= OpenGuides::Utils->get_wgs84_coords(
latitude => $data{metadata}{latitude}[0],
longitude => $data{metadata}{longitude}[0],
config => $self->config );
if ( defined $wgs84_lat ) {
$this_node{has_geodata} = 1;
$this_node{wgs84_lat} = $wgs84_lat;
$this_node{wgs84_long} = $wgs84_long;
$nodes_on_map++;
}
}
push @nodes, \%this_node;
}
}
# Set up our TT variables, including the search parameters
my %tt_vars = (
not_editable => 1,
not_deletable => 1,
deter_robots => 1,
nodes => \@nodes,
done_search => $done_search,
no_nodes_on_map => !$nodes_on_map,
metadata_type => $metadata_type,
metadata_value => $metadata_value,
exclude_locales => $exclude_locales,
exclude_categories => $exclude_categories,
script_name => $script_name
);
# Figure out the map boundaries and centre, if applicable.
if ( $format eq "map" ) {
if ( $use_leaflet ) {
my %minmaxdata = OpenGuides::Utils->get_wgs84_min_max(
nodes => \@nodes );
if ( scalar %minmaxdata ) {
%tt_vars = ( %tt_vars, %minmaxdata );
}
$tt_vars{display_google_maps} = 1; # to get the JavaScript in
}
# Set the show_map var even if we don't have Leaflet enabled, so
# people aren't left wondering why there's no map.
$tt_vars{show_map} = 1;
}
return %tt_vars if $return_tt_vars;
# Render to the page
my $output = $self->process_template(
id => "",
template => "missing_metadata.tt",
tt_vars => \%tt_vars,
noheaders => $args{noheaders} || 0,
);
return $output if $return_output;
print $output;
}
=item B<revert_user_interface>
If C<password> is not supplied then a form for entering the password
will be displayed, along with a list of all the edits the user made.
If the password is given, will delete all of these versions.
=cut
sub revert_user_interface {
my ($self, %args) = @_;
my $password = $args{password} || '';
my $return_tt_vars = $args{return_tt_vars} || 0;
my $return_output = $args{return_output} || 0;
my $wiki = $self->wiki;
my $formatter = $self->wiki->formatter;
my $script_name = $self->config->script_name;
my ($type,$value);
if($args{'username'}) {
($type,$value) = ('username', $args{'username'});
}
if($args{'host'}) {
($type,$value) = ('host', $args{'host'});
}
unless($type && $value) {
croak("One of username or host must be given");
}
# Grab everything they've touched, ever
my @user_edits = $self->wiki->list_recent_changes(
since => 1,
metadata_was => { $type => $value },
);
if ($password) {
if ($password ne $self->config->admin_pass) {
croak("Bad password supplied");
} else {
# Delete all these versions
foreach my $edit (@user_edits) {
$self->wiki->delete_node(
name => $edit->{name},
version => $edit->{version},
);
}
# Grab new list
@user_edits = $self->wiki->list_recent_changes(
since => 1,
metadata_was => { $type => $value },
);
}
} else {
# Don't do anything
}
# Set up our TT variables, including the search parameters
my %tt_vars = (
not_editable => 1,
not_deletable => 1,
deter_robots => 1,
edits => \@user_edits,
username => $args{username},
host => $args{host},
by_type => $type,
by => $value,
script_name => $script_name
);
return %tt_vars if $return_tt_vars;
# Render to the page
my $output = $self->process_template(
id => "",
template => "admin_revert_user.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
=item B<display_admin_interface>
Fetch everything we need to display the admin interface, and passes it off
to the template
=cut
sub display_admin_interface {
my ($self, %args) = @_;
my $return_tt_vars = $args{return_tt_vars} || 0;
my $return_output = $args{return_output} || 0;
my $wiki = $self->wiki;
my $formatter = $self->wiki->formatter;
my $script_name = $self->config->script_name;
# Grab all the recent nodes
my @all_nodes = $wiki->list_recent_changes(last_n_changes => 100);
# Split into nodes, Locales and Categories
my @nodes;
my @categories;
my @locales;
for my $node (@all_nodes) {
# Add moderation status
$node->{'moderate'} = $wiki->node_required_moderation($node->{'name'});
# Make the URLs
my $node_param = uri_escape( $formatter->node_name_to_node_param( $node->{'name'} ) );
$node->{'view_url'} = $script_name . "?id=" . $node_param;
$node->{'versions_url'} = $script_name .
"?action=list_all_versions;id=" . $node_param;
$node->{'moderation_url'} = $script_name .
"?action=set_moderation;id=" . $node_param;
$node->{'revert_user_url'} = $script_name . "?action=revert_user" .
";username=".$node->{metadata}->{username}->[0];
# Filter
if($node->{'name'} =~ /^Category /) {
$node->{'page_name'} = $node->{'name'};
$node->{'name'} =~ s/^Category //;
push @categories, $node;
} elsif($node->{'name'} =~ /^Locale /) {
$node->{'page_name'} = $node->{'name'};
$node->{'name'} =~ s/^Locale //;
push @locales, $node;
} else {
push @nodes, $node;
}
}
# Handle completed notice for actions
my $completed_action = "";
if($args{moderation_completed}) {
if($args{moderation_completed} eq "moderation") {
$completed_action = "Version moderated";
}
if($args{moderation_completed} eq "changed") {
$completed_action = "Node moderation flag changed";
}
if($args{moderation_completed} eq "unknown_node") {
$completed_action = "Node moderation flag not changed, node not known";
}
}
# Render in a template
my %tt_vars = (
not_editable => 1,
not_deletable => 1,
deter_robots => 1,
nodes => \@nodes,
categories => \@categories,
locales => \@locales,
completed_action => $completed_action
);
return %tt_vars if $return_tt_vars;
my $output = $self->process_template(
id => "",
template => "admin_home.tt",
tt_vars => \%tt_vars,
);
return $output if $return_output;
print $output;
}
sub process_template {
my ($self, %args) = @_;
my %output_conf = (
wiki => $self->wiki,
config => $self->config,
node => $args{id},
template => $args{template},
vars => $args{tt_vars},
cookies => $args{cookies},
http_status => $args{http_status},
noheaders => $args{noheaders},
);
if ( $args{content_type} ) {
$output_conf{content_type} = $args{content_type};
}
return OpenGuides::Template->output( %output_conf );
}
# Redirection for legacy URLs.
sub redirect_index_search {
my ( $self, %args ) = @_;
my $type = lc( $args{type} || "" );
my $value = lc( $args{value} || "" );
my $format = lc( $args{format} || "" );
my $script_url = $self->config->script_url;
my $script_name = $self->config->script_name;
my $url = "$script_url$script_name?action=index";
if ( $type eq "category" ) {
$url .= ";cat=$value";
} elsif ( $type eq "locale" ) {
$url .= ";loc=$value";
}
if ( $format ) {
$url .= ";format=$format";
}
return CGI->redirect( -uri => $url, -status => 301 );
}
sub redirect_to_node {
my ($self, $node, $redirected_from) = @_;
my $script_url = $self->config->script_url;
my $script_name = $self->config->script_name;
my $formatter = $self->wiki->formatter;
my $id = $formatter->node_name_to_node_param( $node );
my $oldid;
$oldid = $formatter->node_name_to_node_param( $redirected_from ) if $redirected_from;
my $redir_param = "$script_url$script_name?";
$redir_param .= 'id=' if $oldid;
$redir_param .= $id;
$redir_param .= ";oldid=$oldid" if $oldid;
my $q = CGI->new;
return $q->redirect( $redir_param );
}
sub get_cookie {
my $self = shift;
my $config = $self->config;
my $pref_name = shift or return "";
my %cookie_data = OpenGuides::CGI->get_prefs_from_cookie(config=>$config);
return $cookie_data{$pref_name};
}
=back
=head1 BUGS AND CAVEATS
UTF8 data are currently not handled correctly throughout.
Other bugs are documented at
L<https://github.com/OpenGuides/OpenGuides/issues>
=head1 SEE ALSO
=over 4
=item * The Randomness Guide to London, at L<http://london.randomness.org.uk/>, the largest OpenGuides site.
=item * The list of live OpenGuides installs at L<http://openguides.org/>.
=item * L<Wiki::Toolkit>, the Wiki toolkit which does the heavy lifting for OpenGuides.
=back
=head1 FEEDBACK
If you have a question, a bug report, or a patch, or you're interested
in joining the development team, please contact openguides-dev@lists.openguides.org
(moderated mailing list, will reach all current developers but you'll have
to wait for your post to be approved) or file a bug report at
L<https://github.com/OpenGuides/OpenGuides/issues>
=head1 AUTHOR
The OpenGuides Project (openguides-dev@lists.openguides.org)
=head1 COPYRIGHT
Copyright (C) 2003-2013 The OpenGuides Project. All Rights Reserved.
The OpenGuides distribution is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=head1 CREDITS
Programming by Dominic Hargreaves, Earle Martin, Kake Pugh, and Ivor
Williams. Testing and bug reporting by Billy Abbott, Jody Belka,
Kerry Bosworth, Simon Cozens, Cal Henderson, Steve Jolly, and Bob
Walker (among others). Much of the Module::Build stuff copied from
the Siesta project L<http://siesta.unixbeard.net/>
=cut
1;
|