File: MT.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (4181 lines) | stat: -rw-r--r-- 134,234 bytes parent folder | download
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
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT;

use strict;
use base qw( MT::ErrorHandler );
use File::Spec;
use File::Basename;
use MT::Util qw( weaken );
use MT::I18N qw( const );

our ( $VERSION, $SCHEMA_VERSION );
our (
    $PRODUCT_NAME, $PRODUCT_CODE, $PRODUCT_VERSION,
    $VERSION_ID,   $PORTAL_URL
);
our ( $MT_DIR, $APP_DIR, $CFG_DIR, $CFG_FILE, $SCRIPT_SUFFIX );
our (
    $plugin_sig, $plugin_envelope, $plugin_registry,
    %Plugins,    @Components,      %Components,
    $DebugMode,  $mt_inst,         %mt_inst
);
my %Text_filters;

# For state determination in MT::Object
our $plugins_installed;

BEGIN {
    $plugins_installed = 0;

    ( $VERSION, $SCHEMA_VERSION ) = ( '5.14', '5.0030' );
    (   $PRODUCT_NAME, $PRODUCT_CODE, $PRODUCT_VERSION,
        $VERSION_ID,   $PORTAL_URL
        )
        = (
        'Movable Type', 'MT',
        '5.14',             '5.14-en',
        'http://www.movabletype.org/'
        );

  # To allow MT to run straight from svn, if no build process (pre-processing)
  # is run, then default to MTOS
    if ( $PRODUCT_NAME eq '__PRODUCT' . '_NAME__' ) {
        $PRODUCT_NAME = 'Movable Type';
    }
    if ( $PORTAL_URL eq '__PORTAL' . '_URL__' ) {
        $PORTAL_URL = 'http://www.movabletype.org/';
    }
    if ( $VERSION_ID eq '__PRODUCT_VERSION' . '_ID__' ) {
        $VERSION_ID = $PRODUCT_VERSION;
    }

    $DebugMode = 0;

    # Alias lowercase to uppercase package; note: this is an equivalence
    # as opposed to having @mt::ISA set to 'MT'. so @mt::Plugins would
    # resolve as well as @MT::Plugins.
    *{mt::} = *{MT::};

    # Alias these; Components is the preferred array for MT 4
    *Plugins = \@Components;
}

# On-demand loading of compatibility module, if a plugin asks for it, using
#     use MT 3;
# or even specific to minor version (but this just loads MT::Compat::v3)
#     use MT 3.3;
sub VERSION {
    my $v = $_[1];
    if ( defined $v && ( $v =~ m/^(\d+)/ ) ) {
        my $compat = "MT::Compat::v" . $1;
        if ( ( $1 > 2 ) && ( $1 < int($VERSION) ) ) {
            no strict 'refs';
            unless ( defined *{ $compat . '::' } ) {
                eval "# line " . __LINE__ . " " . __FILE__
                    . "\nrequire $compat;";
            }
        }
    }
    return UNIVERSAL::VERSION(@_);
}

sub version_number  {$VERSION}
sub version_id      {$VERSION_ID}
sub product_code    {$PRODUCT_CODE}
sub product_name    {$PRODUCT_NAME}
sub product_version {$PRODUCT_VERSION}
sub schema_version  {$SCHEMA_VERSION}

sub portal_url {
    if ( my $url = const('PORTAL_URL') ) {
        return $url;
    }
    return $PORTAL_URL;
}

# Default id method turns MT::App::CMS => cms; Foo::Bar => foo/bar
sub id {
    my $pkg = shift;
    my $id = ref($pkg) || $pkg;

    # ignore the MT::App prefix as part of the identifier
    $id =~ s/^MT::App:://;
    $id =~ s!::!/!g;
    return lc $id;
}

sub version_slug {
    return MT->translate_templatized(<<"SLUG");
<__trans phrase="Powered by [_1]" params="$PRODUCT_NAME">
<__trans phrase="Version [_1]" params="$VERSION_ID">
<__trans phrase="http://www.sixapart.com/movabletype/">
SLUG
}

sub build_id {
    my $build_id = '5.14-en';
    $build_id = '' if $build_id eq '__BUILD_' . 'ID__';
    return $build_id;
}

sub import {
    my $pkg = shift;
    return unless @_;

    my (%param) = @_;
    my $app_pkg;
    if ( $app_pkg = $param{app} || $param{App} || $ENV{MT_APP} ) {
        if ( $app_pkg !~ m/::/ ) {
            my $apps = $pkg->registry('applications');
            $app_pkg = $apps->fetch($app_pkg);
            if ( ref $app_pkg ) {

                # pick first one??
                $app_pkg = $app_pkg->[0];

                # pick last one??
                # $app_pkg = pop @$app_pkg;
            }
        }
    }
    elsif ( $param{run} || $param{Run} ) {

        # my $script = File::Spec->rel2abs($0);
        my ( $filename, $path, $suffix ) = fileparse( $0, qr{\..+$} );
        $SCRIPT_SUFFIX = $suffix;
        my $script = lc $filename;
        $script =~ s/^mt-//;
        my $apps = $pkg->registry('applications');
        $app_pkg = $apps->fetch( lc $script );
        unless ($app_pkg) {
            die "cannot determine application for script $0, stopped at";
        }
    }
    $pkg->run_app( $app_pkg, \%param )
        if $app_pkg;
}

sub run_app {
    my $pkg = shift;
    my ( $class, $param ) = @_;

    # When running under FastCGI, the initial invocation of the
    # script has a bare environment. We can use this to test
    # for FastCGI.
    my $not_fast_cgi = 0;
    $not_fast_cgi ||= exists $ENV{$_}
        for qw(HTTP_HOST GATEWAY_INTERFACE SCRIPT_FILENAME SCRIPT_URL);
    my $fast_cgi
        = defined $param->{FastCGI}
        ? $param->{FastCGI}
        : ( not $not_fast_cgi );
    if ($fast_cgi) {
        eval { require CGI::Fast; };
        $fast_cgi = 0 if $@;
    }

    # ready to run now... run inside an eval block so we can gracefully
    # die if something bad happens
    my $app;
    eval {
        eval "require $class; 1;" or die $@;
        if ($fast_cgi) {
            my ( $max_requests, $max_time, $cfg );
            while ( my $cgi = new CGI::Fast ) {
                $app = $class->new( %$param, CGIObject => $cgi )
                    or die $class->errstr;

                $app->{fcgi_startup_time} ||= time;
                $app->{fcgi_request_count}
                    = ( $app->{fcgi_request_count} || 0 ) + 1;

                unless ($cfg) {
                    $cfg          = $app->config;
                    $max_requests = $cfg->FastCGIMaxRequests;
                    $max_time     = $cfg->FastCGIMaxTime;
                }

                local $SIG{__WARN__} = sub { $app->trace( $_[0] ) };
                $pkg->set_instance($app);
                $app->init_request( CGIObject => $cgi );
                $app->run;

                # Check for timeout for this process
                if ( $max_time
                    && ( time - $app->{fcgi_startup_time} >= $max_time ) )
                {
                    last;
                }

                # Check for max executions for this process
                if ( $max_requests
                    && ( $app->{fcgi_request_count} >= $max_requests ) )
                {
                    last;
                }
            }
        }
        else {
            $app = $class->new(%$param) or die $class->errstr;
            local $SIG{__WARN__} = sub { $app->trace( $_[0] ) };
            $app->run;
        }
    };
    if ( my $err = $@ ) {
        my $charset = 'utf-8';
        eval {
            $app ||= MT->instance;
            my $cfg = $app->config;
            my $c   = $app->find_config;
            $cfg->read_config($c);
            $charset = $cfg->PublishCharset;
        };
        if ( $app && UNIVERSAL::isa( $app, 'MT::App' ) ) {
            eval {
                my %param = ( error => $err );
                if ( $err =~ m/Bad ObjectDriver/ ) {
                    $param{error_database_connection} = 1;
                }
                elsif ( $err =~ m/Bad CGIPath/ ) {
                    $param{error_cgi_path} = 1;
                }
                elsif ( $err =~ m/Missing configuration file/ ) {
                    $param{error_config_file} = 1;
                }
                my $page = $app->build_page( 'error.tmpl', \%param )
                    or die $app->errstr;
                print "Content-Type: text/html; charset=$charset\n\n";
                print $page;
            };
            if ( my $err = $@ ) {
                print "Content-Type: text/plain; charset=$charset\n\n";
                print $app
                    ? $app->translate( "Got an error: [_1]", $err )
                    : "Got an error: $err";
            }
        }
        else {
            if ( $err =~ m/Missing configuration file/ ) {
                my $host = $ENV{SERVER_NAME} || $ENV{HTTP_HOST};
                $host =~ s/:\d+//;
                my $port = $ENV{SERVER_PORT};
                my $uri = $ENV{REQUEST_URI} || $ENV{PATH_INFO};
                $uri =~ s/mt(\Q$SCRIPT_SUFFIX\E)?.*$//;
                my $cgipath = '';
                $cgipath = $port == 443 ? 'https' : 'http';
                $cgipath .= '://' . $host;
                $cgipath
                    .= ( $port == 443 || $port == 80 ) ? '' : ':' . $port;
                $cgipath .= $uri;

                print "Status: 302 Moved\n";
                print "Location: " . $cgipath . "mt-wizard.cgi\n\n";
            }
            else {
                print "Content-Type: text/plain; charset=$charset\n\n";
                print $app
                    ? $app->translate( "Got an error: [_1]", $err )
                    : "Got an error: $err\n";
            }
        }
    }
}

sub instance {
    my $class = shift;
    $mt_inst ||= $mt_inst{$class} ||= $class->construct(@_);
}
*app = \&instance;

sub set_instance {
    my $class = shift;
    $mt_inst = shift;
}

sub new {
    my $mt = &instance_of;
    $mt_inst ||= $mt;
    $mt;
}

sub instance_of {
    my $class = shift;
    $mt_inst{$class} ||= $class->construct(@_);
}

sub construct {
    my $class = shift;
    my $mt = bless {}, $class;
    local $mt_inst = $mt;
    $mt->init(@_)
        or die $mt->errstr;
    $mt;
}

{
    my %object_types;

    sub model {
        my $pkg = shift;
        my ($k) = @_;

        $object_types{$k} = $_[1] if scalar @_ > 1;
        return $object_types{$k} if exists $object_types{$k};

        if ( $k =~ m/^(.+):(meta|summary)$/ ) {
            my $ppkg = $pkg->model($1);
            my $mpkg = $ppkg->meta_pkg($2);
            return $mpkg ? $object_types{$k} = $mpkg : undef;
        }

        if ( $k =~ m/^(.+):revision$/ ) {
            my $ppkg = $pkg->model($1);
            my $rpkg = $ppkg->revision_pkg;
            return $rpkg ? $object_types{$k} = $rpkg : undef;
        }

        my $model = $pkg->registry( 'object_types', $k );
        if ( ref($model) eq 'ARRAY' ) {

           # First element of an array *should* be a scalar; in case it isn't,
           # return undef.
            $model = $model->[0];
            return undef if ref $model;
        }
        elsif ( ref($model) eq 'HASH' ) {

            # If all we have is a hash, this doesn't tell us the package for
            # this object type, so it's undefined.
            return undef;
        }
        return undef unless $model;

        # Element in object type hash is scalar, so return it
        no strict 'refs';
        unless ( defined *{ $model . '::__properties' } ) {
            use strict 'refs';
            eval "# line " . __LINE__ . " " . __FILE__ . "\nrequire $model;";
            if ( $@ && ( $k =~ m/^(.+)\./ ) ) {

                # x.foo can't be found, so try loading x
                if ( my $ppkg = $pkg->model($1) ) {

                    # well now see if $model is defined
                    no strict 'refs';
                    unless ( defined *{ $model . '::__properties' } ) {

                        # if not, use parent package instead
                        $model = $ppkg;
                    }
                }
            }
        }
        return $object_types{$k} = $model;
    }

    sub models {
        my $pkg = shift;
        my ($k) = @_;

        my @matches;
        my $model = $pkg->registry('object_types');
        foreach my $m ( keys %$model ) {
            if ( $m =~ m/^\Q$k\E\.?/ ) {
                push @matches, $m;
            }
        }
        return @matches;
    }
}

sub registry {
    my $pkg = shift;

    require MT::Component;
    my $regs = MT::Component->registry(@_);
    my $r;
    if ($regs) {
        foreach my $cr (@$regs) {

            # in the event that our registry request returns something
            # other than an array of hashes, return it as is instead of
            # merging it together.
            return $regs unless ref($cr) eq 'HASH';

            delete $cr->{plugin} if exists $cr->{plugin};
            __merge_hash( $r ||= {}, $cr );
        }
    }
    return $r;
}

# merges contents of two hashes, giving preference to the right side
# if $replace is true; otherwise it will always append to the left side.
sub __merge_hash {
    my ( $h1, $h2, $replace ) = @_;
    for my $k ( keys(%$h2) ) {
        if ( exists( $h1->{$k} ) && ( !$replace ) ) {
            if ( ref $h1->{$k} eq 'HASH' ) {
                __merge_hash( $h1->{$k}, $h2->{$k}, ( $replace || 0 ) + 1 );
            }
            elsif ( ref $h1->{$k} eq 'ARRAY' ) {
                if ( ref $h2->{$k} eq 'ARRAY' ) {
                    push @{ $h1->{$k} }, @{ $h2->{$k} };
                }
                else {
                    push @{ $h1->{$k} }, $h2->{$k};
                }
            }
            else {
                $h1->{$k} = [ $h1->{$k}, $h2->{$k} ];
            }
        }
        else {
            $h1->{$k} = $h2->{$k};
        }
    }
}

# The above functions can all be used to make MT objects (and subobjects).
# The difference between them is characterized by these assertions:
#
#  $mt = MT::App::Search->new();
#  assert($mt->isa('MT::App::Search'))
#
#  $mt1 = MT->instance
#  $mt2 = MT->instance
#  assert($mt1 == $mt2);
#
#  $mt1 = MT::App::CMS->construct()
#  $mt2 = MT::App::CMS->construct()
#  assert($mt1 != $mt2);
#
# TBD: make a test script for these.

# obsolete; do not use
sub unplug {
}

sub config {
    my $mt = shift;
    ref $mt or $mt = MT->instance;
    unless ( $mt->{cfg} ) {
        require MT::ConfigMgr;
        weaken( $mt->{cfg} = MT::ConfigMgr->instance );
    }
    if (@_) {
        my $setting = shift;
        @_ ? $mt->{cfg}->set( $setting, @_ ) : $mt->{cfg}->get($setting);
    }
    else {
        $mt->{cfg};
    }
}

sub request {
    my $pkg = shift;
    my $inst = ref($pkg) ? $pkg : $pkg->instance;
    unless ( $inst->{request} ) {
        require MT::Request;
        $inst->{request} = MT::Request->instance;
    }
    if (@_) {
        $inst->{request}->stash(@_);
    }
    else {
        $inst->{request};
    }
}

sub log {
    my ( $mt, $msg );
    if ( @_ == 1 ) {

        # single parameter to log, so it must be the message
        $msg = shift;
        $mt  = MT->instance;
    }
    else {

        # multiple parameters to log; second one is message
        ( $mt, $msg ) = @_;
    }
    unless ($plugins_installed) {

        # finish init_schema here since we have to log something
        # to the database.
        $mt->init_schema();
    }
    my $log_class = $mt->model('log');
    my $log       = $log_class->new();
    if ( ref $msg eq 'HASH' ) {
        $log->set_values($msg);
    }
    elsif ( ( ref $msg ) && ( UNIVERSAL::isa( $msg, 'MT::Log' ) ) ) {
        $log = $msg;
    }
    else {
        $log->message($msg);
    }
    $log->level( MT::Log::INFO() )
        unless defined $log->level;
    $log->class('system')
        unless defined $log->class;
    $log->save();
    print STDERR Encode::encode_utf8(
        MT->translate( "Message: [_1]", $log->message ) . "\n" )
        if $MT::DebugMode && ( $^O ne "MSWin32" );
}

sub run_tasks {
    my $mt = shift;
    require MT::TaskMgr;
    MT::TaskMgr->run_tasks(@_);
}

our %CallbackAlias;
our $CallbacksEnabled = 1;
my %CallbacksEnabled;
my @Callbacks;

sub add_callback {
    my $class = shift;
    my ( $meth, $priority, $plugin, $code ) = @_;
    if ( $meth =~ m/^(.+::)?([^\.]+)(\..+)?$/ ) {

        # Remap (whatever)::(name).(something)
        if ( exists $CallbackAlias{$2} ) {
            $meth = $CallbackAlias{$2};
            $meth = $1 . $meth if $1;
            $meth = $meth . $3 if $3;
        }
    }
    $meth = $CallbackAlias{$meth} if exists $CallbackAlias{$meth};
    my $internal = 0;
    if ( ref $plugin ) {
        if ( ( defined $mt_inst ) && ( $plugin == $mt_inst ) ) {
            $plugin   = undef;
            $internal = 1;
        }
        elsif ( !UNIVERSAL::isa( $plugin, "MT::Component" ) ) {
            return $class->trans_error(
                "If present, 3rd argument to add_callback must be an object of type MT::Component or MT::Plugin"
            );
        }
    }
    if ( ( ref $code ) ne 'CODE' ) {
        if ( ref $code ) {
            return $class->trans_error(
                '4th argument to add_callback must be a CODE reference.');
        }
        else {

            # Defer until callback is used
            # if ($plugin) {
            #     $code = MT->handler_to_coderef($code);
            # }
        }
    }

    # 0 and 11 are exclusive.
    if ( $priority == 0 || $priority == 11 ) {
        if ( $Callbacks[$priority]->{$meth} ) {
            return $class->trans_error("Two plugins are in conflict");
        }
    }
    return $class->trans_error( "Invalid priority level [_1] at add_callback",
        $priority )
        if ( ( $priority < 0 ) || ( $priority > 11 ) );
    require MT::Callback;
    $CallbacksEnabled{$meth} = 1;
    ## push @{$Plugins{$plugin_sig}{callbacks}}, "$meth Callback" if $plugin_sig;
    my $cb = MT::Callback->new(
        plugin   => $plugin,
        code     => $code,
        priority => $priority,
        internal => $internal,
        method   => $meth
    );
    push @{ $Callbacks[$priority]->{$meth} }, $cb;
    $cb;
}

sub remove_callback {
    my $class    = shift;
    my ($cb)     = @_;
    my $priority = $cb->{priority};
    my $method   = $cb->{method};
    my $list     = $Callbacks[$priority];
    return unless $list;
    my $cbarr = $list->{$method};
    return unless $cbarr;
    @$cbarr = grep { $_ != $cb } @$cbarr;
}

# For use by MT internal code
sub _register_core_callbacks {
    my $class = shift;
    my ($callback_table) = @_;
    foreach my $name ( keys %$callback_table ) {
        $class->add_callback( $name, 5, $mt_inst, $callback_table->{$name} )
            || return;
    }
    1;
}

sub register_callbacks {
    my $class = shift;
    my ($callback_list) = @_;
    foreach my $cb (@$callback_list) {
        $class->add_callback( $cb->{name}, $cb->{priority}, $cb->{plugin},
            $cb->{code} )
            || return;
    }
    1;
}

{
    my $CB_ERR;
    sub callback_error { $CB_ERR = $_[0]; }
    sub callback_errstr {$CB_ERR}
}

sub run_callback {
    my $class = shift;
    my ( $cb, @args ) = @_;

    $cb->error();    # reset the error string
    my $result = eval { $cb->invoke(@args); };
    if ( my $err = $@ ) {
        $cb->error($err);
        my $plugin = $cb->{plugin};
        my $name;
        if ( $cb->{internal} ) {
            $name = MT->translate("Internal callback");
        }
        elsif ( UNIVERSAL::isa( $plugin, 'MT::Plugin' ) ) {
            $name = $plugin->name() || MT->translate("Unnamed plugin");
        }
        else {
            $name = MT->translate("Unnamed plugin");
        }
        require MT::Log;
        MT->log(
            {   message =>
                    MT->translate( "[_1] died with: [_2]", $name, $err ),
                class    => 'system',
                category => 'callback',
                level    => MT::Log::ERROR(),
            }
        );
        return 0;
    }
    if ( $cb->errstr() ) {
        return 0;
    }
    return $result;
}

# A callback should return a true/false value. The result of
# run_callbacks is the logical AND of all the callback's return
# values. Some hookpoints will ignore the return value: e.g. object
# callbacks don't use it. By convention, those that use it have Filter
# at the end of their names (CommentPostFilter, CommentThrottleFilter,
# etc.)
# Note: this composition is not short-circuiting. All callbacks are
# executed even if one has already returned false.
# ALSO NOTE: failure (dying or setting $cb->errstr) does not force a
# "false" return.
# THINK: are there cases where a true value should override all false values?
# that is, where logical OR is the right way to compose multiple callbacks?
sub run_callbacks {
    my $class = shift;
    my ( $meth, @args ) = @_;
    return 1 unless $CallbacksEnabled && %CallbacksEnabled;
    $meth = $CallbackAlias{$meth} if exists $CallbackAlias{$meth};
    my @methods;

    # execution:
    #   Full::Name.<variant>
    #   *::Name.<variant> OR Name.<variant>
    #   Full::Name
    #   *::Name OR Name
    push @methods, $meth if $CallbacksEnabled{$meth};    # bleh::blah variant
    if ( $meth =~ /::/ ) {    # presence of :: implies it's an obj. cb
        my $name = $meth;
        $name =~ s/^.*::([^:]*)$/$1/;
        $name = $CallbackAlias{ '*::' . $name }
            if exists $CallbackAlias{ '*::' . $name };
        push @methods, '*::' . $name
            if $CallbacksEnabled{ '*::' . $name };    # *::blah variant
        push @methods, $name if $CallbacksEnabled{$name};    # blah variant
    }
    if ( $meth =~ /\./ ) {  # presence of ' ' implies it is a variant callback
        my ($name) = split /\./, $meth, 2;
        $name = $CallbackAlias{$name} if exists $CallbackAlias{$name};
        push @methods, $name if $CallbacksEnabled{$name};    # bleh::blah
        if ( $name =~ m/::/ ) {
            my $name2 = $name;
            $name2 =~ s/^.*::([^:]*)$/$1/;
            $name2 = $CallbackAlias{ '*::' . $name2 }
                if exists $CallbackAlias{ '*::' . $name2 };
            push @methods, '*::' . $name2
                if $CallbacksEnabled{ '*::' . $name2 };      # *::blah
            push @methods, $name2 if $CallbacksEnabled{$name2};    # blah
        }
    }
    return 1 unless @methods;

    $CallbacksEnabled{$_} = 0 for @methods;
    my @errors;
    my $filter_value = 1;
    my $first_error;

    foreach my $callback_sheaf (@Callbacks) {
        for my $meth (@methods) {
            if ( my $set = $callback_sheaf->{$meth} ) {
                for my $cb (@$set) {
                    my $result = $class->run_callback( $cb, @args );
                    $filter_value &&= $result;
                    if ( !$result ) {
                        if ( $cb->errstr() ) {
                            push @errors, $cb->errstr();
                        }
                        if ( $class->errstr() ) {
                            push @errors, $class->errstr();
                        }
                        if ( !defined($first_error) ) {
                            $first_error = $cb->errstr() || $class->errstr();
                        }
                    }
                }
            }
        }
    }

    callback_error( join( '', @errors ) );

    $CallbacksEnabled{$_} = 1 for @methods;
    if ( !$filter_value ) {
        return $class->error($first_error);
    }
    else {
        return $filter_value;
    }
}

sub user_class {
    shift->{user_class};
}

sub find_config {
    my $mt = shift;
    my ($param) = @_;

    $param->{Config}    ||= $ENV{MT_CONFIG};
    $param->{Directory} ||= $ENV{MT_HOME};
    if ( !$param->{Directory} ) {
        if ( $param->{Config} ) {
            $param->{Directory} = dirname( $param->{Config} );
        }
        else {
            $param->{Directory} = dirname($0) || $ENV{PWD} || '.';
        }
    }

    # the directory is the more important parameter between it and
    # the config parameter. if config is unreadable, then scan for
    # a config file using the directory as a base.  we support
    # either mt.cfg or mt-config.cgi for the config file name. the
    # latter being a more secure choice since it is unreadable from
    # a browser.
    for my $cfg_file ( $param->{Config},
        File::Spec->catfile( $param->{Directory}, 'mt-config.cgi' ),
        'mt-config.cgi' )
    {
        return $cfg_file if $cfg_file && -r $cfg_file && -f $cfg_file;
    }
    return undef;
}

sub init_schema {
    require MT::Object;
    MT::Object->install_pre_init_properties();
}

sub init_permissions {
    require MT::Permission;
    MT::Permission->init_permissions;
}

sub init_config {
    my $mt = shift;
    my ($param) = @_;

    unless ( $mt->{cfg_file} ) {
        my $cfg_file = $mt->find_config($param);

        return $mt->error(
            "Missing configuration file. Maybe you forgot to move mt-config.cgi-original to mt-config.cgi?"
        ) unless $cfg_file;
        $cfg_file = File::Spec->rel2abs($cfg_file);
        $mt->{cfg_file} = $cfg_file;
    }

    # translate the config file's location to an absolute path, so we
    # can use that directory as a basis for calculating other relative
    # paths found in the config file.
    my $config_dir = $mt->{config_dir} = dirname( $mt->{cfg_file} );

    # store the mt_dir (home) as an absolute path; fallback to the config
    # directory if it isn't set.
    $mt->{mt_dir}
        = $param->{Directory}
        ? File::Spec->rel2abs( $param->{Directory} )
        : $mt->{config_dir};
    $mt->{mt_dir} ||= dirname($0);

    # also make note of the active application path; this is derived by
    # checking the PWD environment variable, the dirname of $0,
    # the directory of SCRIPT_FILENAME and lastly, falls back to mt_dir
    unless ( $mt->{app_dir} ) {
        $mt->{app_dir} = $ENV{PWD} || "";
        $mt->{app_dir} = dirname($0)
            if !$mt->{app_dir}
                || !File::Spec->file_name_is_absolute( $mt->{app_dir} );
        $mt->{app_dir} = dirname( $ENV{SCRIPT_FILENAME} )
            if $ENV{SCRIPT_FILENAME}
                && ( !$mt->{app_dir}
                    || (!File::Spec->file_name_is_absolute( $mt->{app_dir} ) )
                );
        $mt->{app_dir} ||= $mt->{mt_dir};
        $mt->{app_dir} = File::Spec->rel2abs( $mt->{app_dir} );
    }

    my $cfg = $mt->config;
    $cfg->define( $mt->registry('config_settings') );
    $cfg->read_config( $mt->{cfg_file} ) or return $mt->error( $cfg->errstr );

    my @mt_paths = $cfg->paths;
    for my $meth (@mt_paths) {
        my $path = $cfg->get( $meth, undef );
        my $type = $cfg->type($meth);
        if ( defined $path ) {
            if ( $type eq 'ARRAY' ) {
                my @paths = $cfg->get($meth);
                local $_;
                foreach (@paths) {
                    next if File::Spec->file_name_is_absolute($_);
                    $_ = File::Spec->catfile( $config_dir, $_ );
                }
                $cfg->$meth( \@paths );
            }
            else {
                next if ref($path);    # unexpected referene, ignore
                if ( !File::Spec->file_name_is_absolute($path) ) {
                    $path = File::Spec->catfile( $config_dir, $path );
                    $cfg->$meth($path);
                }
            }
        }
        else {
            next if $type eq 'ARRAY';
            my $path = $cfg->default($meth);
            if ( defined $path ) {
                $cfg->$meth( File::Spec->catfile( $config_dir, $path ) );
            }
        }
    }

    if ( my $local_lib = $cfg->LocalLib ) {
        $local_lib = [$local_lib] if !ref $local_lib;
        eval "use local::lib qw( @{$local_lib} )";
        return $mt->trans_error( 'Bad LocalLib config ([_1]): ',
            join( ', ', @$local_lib ), $@, )
            if $@;
    }

    return $mt->trans_error("Bad ObjectDriver config")
        unless $cfg->ObjectDriver;

    if ( $cfg->PerformanceLogging && $cfg->ProcessMemoryCommand ) {
        $mt->log_times();
    }

    $mt->set_language( $cfg->DefaultLanguage );

    my $cgi_path = $cfg->CGIPath;
    if ( !$cgi_path || $cgi_path =~ m!http://www\.example\.com/! ) {
        return $mt->trans_error("Bad CGIPath config");
    }

    $mt->{cfg} = $cfg;

    1;
}

{
    my ($memory_start);

    sub log_times {
        my $pkg = shift;

        my $timer = $pkg->get_timer;
        return unless $timer;

        my $memory;
        my $cmd = $pkg->config->ProcessMemoryCommand;
        if ($cmd) {
            my $re;
            if ( ref($cmd) eq 'HASH' ) {
                $re  = $cmd->{regex};
                $cmd = $cmd->{command};
            }
            $cmd =~ s/\$\$/$$/g;
            $memory = `$cmd`;
            if ($re) {
                if ( $memory =~ m/$re/ ) {
                    $memory = $1;
                    $memory =~ s/\D//g;
                }
            }
            else {
                $memory =~ s/\s+//gs;
            }
        }

        # Called at the start of the process; so we're only recording
        # the memory usage at the start of the app right now.
        unless ( $timer->{elapsed} ) {
            $memory_start = $memory;
            return;
        }

        require File::Spec;
        my $dir = MT->config('PerformanceLoggingPath') or return;

        my @time = localtime(time);
        my $file = sprintf(
            "pl-%04d%02d%02d.log",
            $time[5] + 1900,
            $time[4] + 1,
            $time[3]
        );
        my $log_file = File::Spec->catfile( $dir, $file );

        my $first_write = !-f $log_file;

        open my $PERFLOG, ">>", $log_file
            or ( warn("Failed to open preflog $log_file"), return );
        require Fcntl;
        flock( $PERFLOG, Fcntl::LOCK_EX() );

        if ($first_write) {
            require Config;
            my ( $osname, $osvers )
                = ( $Config::Config{osname}, $Config::Config{osvers} );
            print $PERFLOG "# Operating System: $osname/$osvers\n";
            print $PERFLOG "# Platform: $^O\n";
            my $ver
                = ref($^V) eq 'version'
                ? $^V->normal
                : ( $^V ? join( '.', unpack 'C*', $^V ) : $] );
            print $PERFLOG "# Perl Version: $ver\n";
            print $PERFLOG "# Web Server: $ENV{SERVER_SOFTWARE}\n";
            require MT::Object;
            my $driver = MT::Object->driver;

            if ($driver) {
                my $dbh = $driver->r_handle;
                if ($dbh) {
                    my $dbname = $dbh->get_info(17);    # SQL_DBMS_NAME
                    my $dbver  = $dbh->get_info(18);    # SQL_DBMS_VER
                    if ( $dbname && $dbver ) {
                        print $PERFLOG "# Database: $dbname/$dbver\n";
                    }
                }
            }
            my ( $drname, $drh ) = each %DBI::installed_drh;
            print $PERFLOG "# Database Library: DBI/"
                . $DBI::VERSION
                . "; DBD/"
                . $drh->{Version} . "\n";
            if ( $ENV{MOD_PERL} ) {
                print $PERFLOG "# App Mode: mod_perl\n";
            }
            elsif ( $ENV{FAST_CGI} ) {
                print $PERFLOG "# App Mode: FastCGI\n";
            }
            else {
                print $PERFLOG "# App Mode: CGI\n";
            }
        }

        if ($memory) {
            print $PERFLOG $timer->dump_line( "mem_start=$memory_start",
                "mem_end=$memory" );
        }
        else {
            print $PERFLOG $timer->dump_line();
        }

        close $PERFLOG;
    }
}

sub get_timer {
    my $mt = shift;
    $mt = MT->instance unless ref $mt;
    my $timer = $mt->request('timer');
    unless ( defined $timer ) {
        if ( MT->config('PerformanceLogging') ) {
            my $uri;
            if ( $mt->isa('MT::App') ) {
                $uri = $mt->uri( args => { $mt->param_hash } );
            }
            require MT::Util::ReqTimer;
            $timer = MT::Util::ReqTimer->new($uri);
        }
        else {
            $timer = 0;
        }
        $mt->request( 'timer', $timer );
    }
    return $timer;
}

sub time_this {
    my $mt = shift;
    my ( $str, $code ) = @_;
    my $timer = $mt->get_timer();
    my $ret;
    if ($timer) {
        $timer->pause_partial();
        $ret = $code->();
        $timer->mark($str);
    }
    else {
        $ret = $code->();
    }
    return $ret;
}

sub init_config_from_db {
    my $mt      = shift;
    my ($param) = @_;
    my $cfg     = $mt->config;

    # Tell any instantiated drivers to reconfigure themselves as necessary
    require MT::ObjectDriverFactory;
    if ( MT->config('ObjectDriver') ) {
        my $driver = MT::ObjectDriverFactory->instance;
        $driver->configure if $driver;
    }
    else {
        MT::ObjectDriverFactory->configure();
    }

    $cfg->read_config_db();

    1;
}

sub bootstrap {
    my $pkg = shift;
    $pkg->init_paths() or return;
    $pkg->init_core()  or return;
}

sub init_paths {
    my $mt = shift;
    my ($param) = @_;

    # determine MT directory
    my ($orig_dir);
    require File::Spec;
    if ( !( $MT_DIR = $ENV{MT_HOME} ) ) {
        if ( $0 =~ m!(.*([/\\]))! ) {
            $orig_dir = $MT_DIR = $1;
            my $slash = $2;
            $MT_DIR =~ s!(?:[/\\]|^)(?:plugins[/\\].*|tools[/\\])$!$slash!;
            $MT_DIR = '' if ( $MT_DIR =~ m!^\.?[\\/]$! );
        }
        else {

            # MT_DIR/lib/MT.pm -> MT_DIR/lib -> MT_DIR
            $MT_DIR = dirname( dirname( File::Spec->rel2abs(__FILE__) ) );
        }
        unless ($MT_DIR) {
            $orig_dir = $MT_DIR = $ENV{PWD} || '.';
            $MT_DIR =~ s!(?:[/\\]|^)(?:plugins[/\\].*|tools[/\\]?)$!!;
        }
        $ENV{MT_HOME} = $MT_DIR;
    }
    unshift @INC, File::Spec->catdir( $MT_DIR,   'extlib' );
    unshift @INC, File::Spec->catdir( $orig_dir, 'lib' )
        if $orig_dir && ( $orig_dir ne $MT_DIR );

    $mt->set_language('en_US');

    if ( my $cfg_file = $mt->find_config($param) ) {
        $cfg_file = File::Spec->rel2abs($cfg_file);
        $CFG_FILE = $cfg_file;
    }
    else {
        return $mt->trans_error(
            "Missing configuration file. Maybe you forgot to move mt-config.cgi-original to mt-config.cgi?"
        ) if ref($mt);
    }

    # store the mt_dir (home) as an absolute path; fallback to the config
    # directory if it isn't set.
    $MT_DIR ||=
        $param->{directory}
        ? File::Spec->rel2abs( $param->{directory} )
        : $CFG_DIR;
    $MT_DIR ||= dirname($0);

    # also make note of the active application path; this is derived by
    # checking the PWD environment variable, the dirname of $0,
    # the directory of SCRIPT_FILENAME and lastly, falls back to mt_dir
    $APP_DIR = $ENV{PWD} || "";
    $APP_DIR = dirname($0)
        if !$APP_DIR || !File::Spec->file_name_is_absolute($APP_DIR);
    $APP_DIR = dirname( $ENV{SCRIPT_FILENAME} )
        if $ENV{SCRIPT_FILENAME}
            && ( !$APP_DIR
                || ( !File::Spec->file_name_is_absolute($APP_DIR) ) );
    $APP_DIR ||= $MT_DIR;
    $APP_DIR = File::Spec->rel2abs($APP_DIR);

    return 1;
}

sub init_core {
    my $mt = shift;
    return if exists $Components{'core'};
    require MT::Core;
    my $c = MT::Core->new( { id => 'core', path => $MT_DIR } )
        or die MT::Core->errstr;
    $Components{'core'} = $c;

    push @Components, $c;
    return 1;
}

sub init_lang_defaults {
    my $mt        = shift;
    my $cfg       = $mt->config;
    my $was_dirty = $cfg->is_dirty;
    $cfg->DefaultLanguage('en_US') unless $cfg->DefaultLanguage;

    my %lang_settings = (
        'NewsboxURL'         => 'NEWSBOX_URL',
        'SupportURL'         => 'SUPPORT_URL',
        'NewsURL'            => 'NEWS_URL',
        'DefaultTimezone'    => 'DEFAULT_TIMEZONE',
        'TimeOffset'         => 'DEFAULT_TIMEZONE',
        'MailEncoding'       => 'MAIL_ENCODING',
        'ExportEncoding'     => 'EXPORT_ENCODING',
        'LogExportEncoding'  => 'LOG_EXPORT_ENCODING',
        'CategoryNameNodash' => 'CATEGORY_NAME_NODASH',
        'PublishCharset'     => 'PUBLISH_CHARSET',
    );

    foreach my $setting ( keys %lang_settings ) {
        my $const    = $lang_settings{$setting};
        my $value    = $cfg->$setting;
        my $i18n_val = const($const);
        if ( !$value ) {
            $cfg->$setting( $i18n_val, 1 );
        }
        elsif (( $value eq $cfg->default($setting) )
            && ( $value ne $i18n_val ) )
        {
            $cfg->$setting( $i18n_val, 1 );
        }
    }
    $cfg->clear_dirty unless $was_dirty;
    return 1;
}

sub init {
    my $mt    = shift;
    my %param = @_;

    $mt->bootstrap() unless $MT_DIR;
    $mt->{mt_dir}     = $MT_DIR;
    $mt->{config_dir} = $CFG_DIR;
    $mt->{app_dir}    = $APP_DIR;

    $mt->init_callbacks();

    ## Initialize the language to the default in case any errors occur in
    ## the rest of the initialization process.
    $mt->init_config( \%param ) or return;
    $mt->init_lang_defaults(@_) or return;
    require MT::Plugin;
    $mt->init_addons(@_) or return;
    $mt->init_config_from_db( \%param ) or return;
    $mt->init_debug_mode;
    $mt->init_plugins(@_) or return;
    $plugins_installed = 1;
    $mt->init_schema();
    $mt->init_permissions();

    # Load MT::Log so constants are available
    require MT::Log;

    $mt->run_callbacks( 'post_init', $mt, \%param );
    return $mt;
}

sub init_debug_mode {
    my $mt  = shift;
    my $cfg = $mt->config;

    # init the debug mode
    if ( $MT::DebugMode = $cfg->DebugMode ) {
        require Data::Dumper;
        $Data::Dumper::Terse    = 1;
        $Data::Dumper::Maxdepth = 4;
        $Data::Dumper::Sortkeys = 1;
        $Data::Dumper::Indent   = 1;
    }
}

sub init_callbacks {
    my $mt = shift;
    MT->_register_core_callbacks(
        {   'build_file_filter' =>
                sub { MT->publisher->queue_build_file_filter(@_) },
            'cms_upload_file' => \&core_upload_file_to_sync,
            'api_upload_file' => \&core_upload_file_to_sync,
            'post_init' =>
                '$Core::MT::Summary::Triggers::post_init_add_triggers',
        }
    );
}

sub core_upload_file_to_sync {
    my ( $cb, %args ) = @_;
    MT->upload_file_to_sync(%args);
}

sub upload_file_to_sync {
    my $class = shift;
    my (%args) = @_;

    # no need to do this unless we're syncing stuff.
    return unless MT->config('SyncTarget');

    my $url  = $args{url};
    my $file = $args{file};
    return unless -f $file;

    my $blog    = $args{blog};
    my $blog_id = $blog->id;
    return unless $blog->publish_queue;

    require MT::FileInfo;
    my $base_url = $url;
    $base_url =~ s!^https?://[^/]+!!;
    my $fi = MT::FileInfo->load( { blog_id => $blog_id, url => $base_url } );
    if ( !$fi ) {
        $fi = MT::FileInfo->new();
        $fi->blog_id($blog_id);
        $fi->url($base_url);
    }
    $fi->file_path($file);
    $fi->save;

    require MT::TheSchwartz;
    require TheSchwartz::Job;
    my $job = TheSchwartz::Job->new();
    $job->funcname('MT::Worker::Sync');
    $job->uniqkey( $fi->id );
    $job->coalesce(
        ( $fi->blog_id || 0 ) . ':' . $$ . ':' . ( time - ( time % 10 ) ) );
    MT::TheSchwartz->insert($job);
}

sub init_addons {
    my $mt  = shift;
    my $cfg = $mt->config;
    my @PluginPaths;

    unshift @PluginPaths, File::Spec->catdir( $MT_DIR, 'addons' );
    return $mt->_init_plugins_core( {}, 1, \@PluginPaths );
}

sub init_plugins {
    my $mt = shift;

    # Load compatibility module for prior version
    # This should always be MT::Compat::v(MAJOR_RELEASE_VERSION - 1).
    if ( MT->config('RequiredCompatibility') < 4.0 ) {
        require MT::Compat::v3;
    }

    my $cfg          = $mt->config;
    my $use_plugins  = $cfg->UsePlugins;
    my @PluginPaths  = $cfg->PluginPath;
    my $PluginSwitch = $cfg->PluginSwitch || {};
    return $mt->_init_plugins_core( $PluginSwitch, $use_plugins,
        \@PluginPaths );
}

{
    my $plugin_full_path;

    sub add_plugin {
        my $class = shift;
        my ($plugin) = @_;
        if ( ref $plugin eq 'HASH' ) {
            require MT::Plugin;
            $plugin = MT::Plugin->new($plugin);
        }
        $plugin->{name} ||= $plugin_sig;
        $plugin->{plugin_sig} = $plugin_sig;

        my $id = $plugin->id;
        unless ($plugin_envelope) {
            warn
                "MT->add_plugin improperly called outside of MT plugin load loop.";
            return;
        }
        $plugin->envelope($plugin_envelope);
        Carp::confess(
            "You cannot register multiple plugin objects from a single script. $plugin_sig"
            )
            if exists( $Plugins{$plugin_sig} )
                && ( exists $Plugins{$plugin_sig}{object} );

        $Components{ lc $id } = $plugin if $id;
        $Plugins{$plugin_sig}{object} = $plugin;
        $plugin->{full_path} = $plugin_full_path;
        $plugin->path($plugin_full_path);
        unless ( $plugin->{registry} && ( %{ $plugin->{registry} } ) ) {
            $plugin->{registry} = $plugin_registry;
        }
        if ( $plugin->{registry} ) {
            if ( my $settings = $plugin->{registry}{config_settings} ) {
                $settings = $plugin->{registry}{config_settings}
                    = $settings->()
                    if ref($settings) eq 'CODE';
                $class->config->define($settings) if $settings;
            }
        }
        push @Components, $plugin;
        1;
    }

    sub __load_plugin {
        my ( $mt, $timer, $PluginSwitch, $use_plugins, $plugin, $sig ) = @_;
        die "Bad plugin filename '$plugin'"
            if ( $plugin !~ /^([-\\\/\@\:\w\.\s~]+)$/ );
        local $plugin_sig      = $sig;
        local $plugin_registry = {};
        if (!$use_plugins
            || ( exists $PluginSwitch->{$plugin_sig}
                && !$PluginSwitch->{$plugin_sig} )
            )
        {
            $Plugins{$plugin_sig}{full_path} = $plugin_full_path;
            $Plugins{$plugin_sig}{enabled}   = 0;
            return 0;
        }
        return 0 if exists $Plugins{$plugin_sig};
        $Plugins{$plugin_sig}{full_path} = $plugin_full_path;
        $timer->pause_partial if $timer;
        eval "# line " . __LINE__ . " " . __FILE__ . "\nrequire '$plugin';";
        $timer->mark( "Loaded plugin " . $sig ) if $timer;
        if ($@) {
            $Plugins{$plugin_sig}{error} = $@;

            # Issue MT log within another eval block in the
            # event that the plugin error is happening before
            # the database has been initialized...
            eval {
                require MT::Log;
                $mt->log(
                    {   message => $mt->translate(
                            "Plugin error: [_1] [_2]", $plugin,
                            $Plugins{$plugin_sig}{error}
                        ),
                        class    => 'system',
                        category => 'plugin',
                        level    => MT::Log::ERROR()
                    }
                );
            };
            return 0;
        }
        else {
            if ( my $obj = $Plugins{$plugin_sig}{object} ) {
                $obj->init_callbacks();
            }
            else {

                # A plugin did not register itself, so
                # create a dummy plugin object which will
                # cause it to show up in the plugin listing
                # by it's filename.
                MT->add_plugin( {} );
            }
        }
        $Plugins{$plugin_sig}{enabled} = 1;
        return 1;
    }

    sub __load_plugin_with_yaml {
        my ( $use_plugins, $PluginSwitch, $plugin_dir ) = @_;
        my $pclass
            = $plugin_dir =~ m/\.pack$/
            ? 'MT::Component'
            : 'MT::Plugin';

        # Don't process disabled plugin config.yaml files.
        if ($pclass eq 'MT::Plugin'
            && (!$use_plugins
                || ( exists $PluginSwitch->{$plugin_dir}
                    && !$PluginSwitch->{$plugin_dir} )
            )
            )
        {
            $Plugins{$plugin_dir}{full_path} = $plugin_full_path;
            $Plugins{$plugin_dir}{enabled}   = 0;
            return;
        }
        return if exists $Plugins{$plugin_dir};
        my $id = lc $plugin_dir;
        $id =~ s/\.\w+$//;
        my $p = $pclass->new(
            {   id       => $id,
                path     => $plugin_full_path,
                envelope => $plugin_envelope
            }
        );

        # rebless? based on config?
        local $plugin_sig = $plugin_dir;
        MT->add_plugin($p);
        $p->init_callbacks();
    }

    sub _init_plugins_core {
        my $mt = shift;
        my ( $PluginSwitch, $use_plugins, $PluginPaths ) = @_;

        my $timer;
        if ( $mt->config->PerformanceLogging ) {
            $timer = $mt->get_timer();
        }

        foreach my $PluginPath (@$PluginPaths) {
            my $plugin_lastdir = $PluginPath;
            $plugin_lastdir =~ s![\\/]$!!;
            $plugin_lastdir =~ s!^.*[\\/]!!;

            if ( opendir my $DH, $PluginPath ) {
                my @p = readdir $DH;
                closedir $DH;
                for my $plugin (@p) {
                    next if ( $plugin =~ /^\.\.?$/ || $plugin =~ /~$/ );

                    $plugin_full_path
                        = File::Spec->catfile( $PluginPath, $plugin );
                    if ( -f $plugin_full_path ) {
                        $plugin_envelope = $plugin_lastdir;
                        __load_plugin( $mt, $timer, $PluginSwitch,
                            $use_plugins, $plugin_full_path, $plugin )
                            if $plugin_full_path =~ /\.pl$/;
                        next;
                    }

                    my $plugin_dir = $plugin;
                    $plugin_envelope = "$plugin_lastdir/" . $plugin;

                    foreach my $lib (qw(lib extlib)) {
                        my $plib
                            = File::Spec->catdir( $plugin_full_path, $lib );
                        unshift @INC, $plib if -d $plib;
                    }

                    # handle config.yaml
                    my $yaml = File::Spec->catdir( $plugin_full_path,
                        'config.yaml' );

                    if ( -f $yaml ) {
                        __load_plugin_with_yaml( $use_plugins, $PluginSwitch,
                            $plugin_dir );
                        next;
                    }

                    my @plugins;
                    if ( opendir my $subdir, $plugin_full_path ) {
                        @plugins = readdir $subdir;
                        closedir $subdir;
                    }
                    else {
                        warn "Can not read directory: $plugin_full_path";
                    }
                    for my $plugin (@plugins) {
                        next if $plugin !~ /\.pl$/;
                        my $plugin_file
                            = File::Spec->catfile( $plugin_full_path,
                            $plugin );
                        if ( -f $plugin_file ) {
                            __load_plugin( $mt, $timer, $PluginSwitch,
                                $use_plugins, $plugin_file,
                                $plugin_dir . '/' . $plugin );
                        }
                    }
                }
            }
        }

        # Reset the Text_filters hash in case it was preloaded by plugins by
        # calling all_text_filters (Markdown in particular does this).
        # Upon calling all_text_filters again, it will be properly loaded by
        # querying the registry.
        %Text_filters = ();

        1;
    }

}

my %addons;

sub find_addons {
    my $mt = shift;
    my ($type) = @_;

    unless (%addons) {
        my $addon_path = File::Spec->catdir( $MT_DIR, 'addons' );
        if ( opendir my $DH, $addon_path ) {
            my @p = readdir $DH;
            closedir $DH;
            foreach my $p (@p) {
                next if $p eq '.' || $p eq '..';
                my $full_path = File::Spec->catdir( $addon_path, $p );
                if ( -d $full_path ) {
                    if ( $p =~ m/^(.+)\.(\w+)$/ ) {
                        my $label = $1;
                        my $id    = lc $1;
                        my $type  = $2;
                        if ( $type eq 'pack' ) {
                            $label .= ' Pack';
                        }
                        elsif ( $type eq 'theme' ) {
                            $label .= ' Theme';
                        }
                        elsif ( $type eq 'plugin' ) {
                            $label .= ' Plugin';
                        }
                        push @{ $addons{$type} },
                            {
                            label    => $label,
                            id       => $id,
                            envelope => 'addons/' . $p . '/',
                            path     => $full_path,
                            };
                    }
                }
            }
        }
    }
    if ($type) {
        my $addons = $addons{$type} ||= [];
        return $addons;
    }
    return 1;
}

*mt_dir = \&server_path;
sub server_path { $_[0]->{mt_dir} }
sub app_dir     { $_[0]->{app_dir} }
sub config_dir  { $_[0]->{config_dir} }

sub component {
    my $mt = shift;
    my ($id) = @_;
    return $Components{ lc $id };
}

sub publisher {
    my $mt = shift;
    $mt = $mt->instance unless ref $mt;
    unless ( $mt->{WeblogPublisher} ) {
        require MT::WeblogPublisher;
        $mt->{WeblogPublisher} = new MT::WeblogPublisher();
    }
    $mt->{WeblogPublisher};
}

sub rebuild {
    my $mt = shift;
    $mt->publisher->rebuild(@_)
        or return $mt->error( $mt->publisher->errstr );
}

sub rebuild_entry {
    my $mt = shift;
    $mt->publisher->rebuild_entry(@_)
        or return $mt->error( $mt->publisher->errstr );
}

sub rebuild_indexes {
    my $mt = shift;
    $mt->publisher->rebuild_indexes(@_)
        or return $mt->error( $mt->publisher->errstr );
}

sub rebuild_archives {
    my $mt = shift;
    $mt->publisher->rebuild_archives(@_)
        or return $mt->error( $mt->publisher->errstr );
}

sub ping {
    my $mt    = shift;
    my %param = @_;
    my $blog;
    require MT::Entry;
    require MT::Util;
    unless ( $blog = $param{Blog} ) {
        my $blog_id = $param{BlogID};
        $blog = MT::Blog->load($blog_id)
            or return $mt->trans_error( "Load of blog '[_1]' failed: [_2]",
            $blog_id, MT::Blog->errstr );
    }

    my (@res);

    my $send_updates = 1;
    if ( exists $param{OldStatus} ) {
        ## If this is a new entry (!$old_status) OR the status was previously
        ## set to draft, and is now set to publish, send the update pings.
        my $old_status = $param{OldStatus};
        if ( $old_status && $old_status eq MT::Entry::RELEASE() ) {
            $send_updates = 0;
        }
    }

    if ( $send_updates && !( MT->config->DisableNotificationPings ) ) {
        ## Send update pings.
        my @updates = $mt->update_ping_list($blog);
        for my $url (@updates) {
            require MT::XMLRPC;
            if (MT::XMLRPC->ping_update( 'weblogUpdates.ping', $blog, $url ) )
            {
                push @res, { good => 1, url => $url, type => "update" };
            }
            else {
                my $err = MT::XMLRPC->errstr;
                $err = Encode::decode_utf8($err)
                    if ( $err && !Encode::is_utf8($err) );
                push @res,
                    {
                    good  => 0,
                    url   => $url,
                    type  => "update",
                    error => $err,
                    };
            }
        }
        if ( $blog->mt_update_key ) {
            require MT::XMLRPC;
            if ( MT::XMLRPC->mt_ping($blog) ) {
                push @res,
                    {
                    good => 1,
                    url  => $mt->{cfg}->MTPingURL,
                    type => "update"
                    };
            }
            else {
                my $err = MT::XMLRPC->errstr;
                $err = Encode::decode_utf8($err)
                    if ( $err && !Encode::is_utf8($err) );
                push @res,
                    {
                    good  => 0,
                    url   => $mt->{cfg}->MTPingURL,
                    type  => "update",
                    error => $err,
                    };
            }
        }
    }

    my $cfg     = $mt->{cfg};
    my $send_tb = $cfg->OutboundTrackbackLimit;
    return \@res if $send_tb eq 'off';

    my @tb_domains;
    if ( $send_tb eq 'selected' ) {
        @tb_domains = $cfg->OutboundTrackbackDomains;
    }
    elsif ( $send_tb eq 'local' ) {
        my $iter = MT::Blog->load_iter();
        while ( my $b = $iter->() ) {
            next if $b->id == $blog->id;
            push @tb_domains, MT::Util::extract_domains( $b->site_url );
        }
    }
    my $tb_domains = join '|', map { lc quotemeta $_ } @tb_domains;
    $tb_domains = qr/(?:^|\.)$tb_domains$/ if $tb_domains;

    ## Send TrackBack pings.
    if ( my $entry = $param{Entry} ) {
        my $pings = $entry->to_ping_url_list;

        my %pinged = map { $_ => 1 } @{ $entry->pinged_url_list };
        my $cats = $entry->categories;
        for my $cat (@$cats) {
            push @$pings, grep !$pinged{$_}, @{ $cat->ping_url_list };
        }

        my $ua = MT->new_ua;

        ## Build query string to be sent on each ping.
        my @qs;
        push @qs, 'title=' . MT::Util::encode_url( $entry->title );
        push @qs, 'url=' . MT::Util::encode_url( $entry->permalink );
        push @qs, 'excerpt=' . MT::Util::encode_url( $entry->get_excerpt );
        push @qs, 'blog_name=' . MT::Util::encode_url( $blog->name );
        my $qs = join '&', @qs;

        ## Character encoding--best guess.
        my $enc = $mt->{cfg}->PublishCharset;

        for my $url (@$pings) {
            $url =~ s/^\s*//;
            $url =~ s/\s*$//;
            my $url_domain;
            ($url_domain) = MT::Util::extract_domains($url);
            next if $tb_domains && ( lc($url_domain) !~ $tb_domains );

            my $req = HTTP::Request->new( POST => $url );
            $req->content_type(
                "application/x-www-form-urlencoded; charset=$enc");
            $req->content($qs);
            my $res = $ua->request($req);
            if ( substr( $res->code, 0, 1 ) eq '2' ) {
                my $c = $res->content;
                $c = Encode::decode_utf8($c) if !Encode::is_utf8($c);
                my ( $error, $msg )
                    = $c =~ m!<error>(\d+).*<message>(.+?)</message>!s;
                if ($error) {
                    push @res,
                        {
                        good  => 0,
                        url   => $url,
                        type  => 'trackback',
                        error => $msg,
                        };
                }
                else {
                    push @res,
                        { good => 1, url => $url, type => 'trackback' };
                }
            }
            else {
                push @res,
                    {
                    good  => 0,
                    url   => $url,
                    type  => 'trackback',
                    error => "HTTP error: " . $res->status_line
                    };
            }
        }
    }
    \@res;
}

sub ping_and_save {
    my $mt    = shift;
    my %param = @_;
    if ( my $entry = $param{Entry} ) {
        my $results = MT::ping( $mt, @_ ) or return;
        my %still_ping;
        my $pinged = $entry->pinged_url_list;
        for my $res (@$results) {
            next if $res->{type} ne 'trackback';
            if ( !$res->{good} ) {
                $still_ping{ $res->{url} } = 1;
            }
            push @$pinged, $res->{url}
                . (
                $res->{good}
                ? ''
                : ' ' . $res->{error}
                );
        }
        $entry->pinged_urls( join "\n", @$pinged );
        $entry->to_ping_urls( join "\n", keys %still_ping );
        $entry->save or return $mt->error( $entry->errstr );
        return $results;
    }
    1;
}

sub needs_ping {
    my $mt    = shift;
    my %param = @_;
    my $blog  = $param{Blog};
    my $entry = $param{Entry};
    require MT::Entry;
    return unless $entry->status == MT::Entry::RELEASE();
    my $old_status = $param{OldStatus};
    my %list;
    ## If this is a new entry (!$old_status) OR the status was previously
    ## set to draft, and is now set to publish, send the update pings.
    if ( ( !$old_status || $old_status ne MT::Entry::RELEASE() )
        && !( MT->config->DisableNotificationPings ) )
    {
        my @updates = $mt->update_ping_list($blog);
        @list{@updates} = (1) x @updates;
        $list{ $mt->{cfg}->MTPingURL } = 1 if $blog && $blog->mt_update_key;
    }
    if ($entry) {
        @list{ @{ $entry->to_ping_url_list } } = ();
        my %pinged = map { $_ => 1 } @{ $entry->pinged_url_list };
        my $cats = $entry->categories;
        for my $cat (@$cats) {
            @list{ grep !$pinged{$_}, @{ $cat->ping_url_list } } = ();
        }
    }
    my @list = keys %list;
    return unless @list;
    \@list;
}

sub update_ping_list {
    my $mt = shift;
    my ($blog) = @_;

    my @updates;
    if ( my $pings = MT->registry('ping_servers') ) {
        my $up = $blog->update_pings;
        if ($up) {
            foreach ( split ',', $up ) {
                next unless exists $pings->{$_};
                push @updates, $pings->{$_}->{url};
            }
        }
    }
    if ( my $others = $blog->ping_others ) {
        push @updates, split /\r?\n/, $others;
    }
    my %updates;
    for my $url (@updates) {
        for ($url) {
            s/^\s*//;
            s/\s*$//;
        }
        next unless $url =~ /\S/;
        $updates{$url}++;
    }
    keys %updates;
}

{
    my $LH;

    sub set_language {
        my $pkg = shift;
        require MT::L10N;
        $LH = MT::L10N->get_handle(@_);

        # Clear any l10n_handles in request
        $pkg->request( 'l10n_handle', {} );
        return $LH;
    }

    sub translate {
        my $this = shift;
        my $app = ref($this) ? $this : $this->app;
        if ( $app->{component} ) {
            if ( my $c = $app->component( $app->{component} ) ) {
                local $app->{component} = undef;
                return $c->translate(@_);
            }
        }
        my ( $format, @args ) = @_;
        foreach (@args) {
            $_ = $_->() if ref($_) eq 'CODE';
        }
        my $text = $LH->maketext( $format, @args );
        return $text;
    }

    sub translate_templatized {
        my $mt = shift;
        my $app = ref($mt) ? $mt : $mt->app;
        if ( $app->{component} ) {
            if ( my $c = $app->component( $app->{component} ) ) {
                local $app->{component} = undef;
                return $c->translate_templatized(@_);
            }
        }
        my @cstack;
        my ($text) = @_;

        # Here, the text must be handled as binary ( non utf-8 ) data,
        # because regexp for utf-8 string is too heavy.
        # things we have to do is
        #  * encode $text before parse
        #  * decode the strings captured by regexp
        #  * encode the translated string from translate()
        #  * decode again for return
        $text = Encode::encode( 'utf8', $text )
            if Encode::is_utf8($text);
        while (1) {
            return '' unless $text;
            $text
                =~ s!(<(/)?(?:_|MT)_TRANS(_SECTION)?(?:(?:\s+((?:\w+)\s*=\s*(["'])(?:(<(?:[^"'>]|"[^"]*"|'[^']*')+)?>|[^\5]+?)*?\5))+?\s*/?)?>)!
            my($msg, $close, $section, %args) = ($1, $2, $3);
            while ($msg =~ /\b(\w+)\s*=\s*(["'])((?:<(?:[^"'>]|"[^"]*"|'[^']*')+?>|[^\2])*?)?\2/g) {  #"
                $args{$1} = Encode::decode_utf8($3);
            }
            if ($section) {
                if ($close) {
                    $mt = pop @cstack;
                } else {
                    if ($args{component}) {
                        push @cstack, $mt;
                        $mt = MT->component($args{component})
                            or die "Bad translation component: $args{component}";
                    }
                    else {
                        die "__trans_section without a component argument";
                    }
                }
                '';
            }
            else {
                $args{params} = '' unless defined $args{params};
                my @p = map MT::Util::decode_html($_),
                        split /\s*%%\s*/, $args{params}, -1;
                @p = ('') unless @p;
                my $phrase = $args{phrase};
                $phrase = Encode::decode('utf8', $phrase)
                    unless Encode::is_utf8($phrase);
                my $translation = $mt->translate($phrase, @p);
                if (exists $args{escape}) {
                    if (lc($args{escape}) eq 'html') {
                        $translation = MT::Util::encode_html($translation);
                    } elsif (lc($args{escape}) eq 'url') {
                        $translation = MT::Util::encode_url($translation);
                    } else {
                        # fallback for js/javascript/singlequotes
                        $translation = MT::Util::encode_js($translation);
                    }
                }
                $translation = Encode::encode('utf8', $translation)
                    if Encode::is_utf8($translation);
                $translation;
            }
            !igem or last;
        }
        $text = Encode::decode_utf8($text)
            unless Encode::is_utf8($text);
        return $text;
    }

    sub current_language { $LH->language_tag }
    sub language_handle  {$LH}

    sub charset {
        my $mt = shift;
        $mt->{charset} = shift if @_;
        return $mt->{charset} if $mt->{charset};
        $mt->{charset} = $mt->config->PublishCharset
            || $mt->language_handle->encoding;
    }
}

sub supported_languages {
    my $mt = shift;
    require MT::L10N;
    require File::Basename;
    ## Determine full path to lib/MT/L10N directory...
    my $lib
        = File::Spec->catdir( File::Basename::dirname( $INC{'MT/L10N.pm'} ),
        'L10N' );
    ## ... From that, determine full path to extlib/MT/L10N.
    ## To do that, we look for the last instance of the string 'lib'
    ## in $lib and replace it with 'extlib'. reverse is a nice tricky
    ## way of doing that.
    ( my $extlib = reverse $lib ) =~ s!bil!biltxe!;
    $extlib = reverse $extlib;
    my @dirs = ( $lib, $extlib );
    my %langs;
    for my $dir (@dirs) {
        opendir my $DH, $dir or next;
        for my $f ( readdir $DH ) {
            my ($tag) = $f =~ /^(\w+)\.pm$/;
            next unless $tag;
            my $lh = MT::L10N->get_handle($tag);
            $langs{ $lh->language_tag } = $lh->language_name;
        }
        closedir $DH;
    }
    \%langs;
}

# For your convenience
sub trans_error {
    my $app = shift;
    $app->error( $app->translate(@_) );
}

sub all_text_filters {
    unless (%Text_filters) {
        if ( my $filters = MT->registry('text_filters') ) {
            %Text_filters = %$filters if ref($filters) eq 'HASH';
        }
    }
    if ( my $enabled_filters = MT->config('AllowedTextFilters') ) {
        my %enabled = map { $_ => 1 } split /\s*,\s*/, $enabled_filters;
        %Text_filters = map { $_ => $Text_filters{$_} }
            grep { exists $enabled{$_} }
            keys %Text_filters;
    }
    return \%Text_filters;
}

sub apply_text_filters {
    my $mt = shift;
    my ( $str, $filters, @extra ) = @_;
    my $all_filters = $mt->all_text_filters;
    for my $filter (@$filters) {
        my $f = $all_filters->{$filter} or next;
        my $code = $f->{code} || $f->{handler};
        unless ( ref($code) eq 'CODE' ) {
            $code = $mt->handler_to_coderef($code);
            $f->{code} = $code;
        }
        if ( !$code ) {
            warn "Bad text filter: $filter";
            next;
        }
        $str = $code->( $str, @extra );
    }
    $str = Encode::decode_utf8($str)
        if !Encode::is_utf8($str);
    return $str;
}

sub static_path {
    my $app   = shift;
    my $spath = $app->config->StaticWebPath;
    if ( !$spath ) {
        $spath = $app->config->CGIPath;
        $spath .= '/' unless $spath =~ m!/$!;
        $spath .= 'mt-static/';
    }
    else {
        $spath .= '/' unless $spath =~ m!/$!;
    }
    $spath;
}

sub static_file_path {
    my $app = shift;
    return $app->{__static_file_path}
        if exists $app->{__static_file_path};

    my $path = $app->config('StaticFilePath');
    return $app->{__static_file_path} = $path if defined $path;

    # Attempt to derive StaticFilePath based on environment
    my $web_path = $app->config->StaticWebPath || 'mt-static';
    $web_path =~ s!^https?://[^/]+/!!;
    if ( $app->can('document_root') ) {
        my $doc_static_path
            = File::Spec->catdir( $app->document_root(), $web_path );
        return $app->{__static_file_path} = $doc_static_path
            if -d $doc_static_path;
    }
    my $mtdir_static_path = File::Spec->catdir( $app->mt_dir, 'mt-static' );
    return $app->{__static_file_path} = $mtdir_static_path
        if -d $mtdir_static_path;
    return;
}

sub support_directory_url {
    my $app = shift;
    my $url = $app->config->SupportDirectoryURL;
    if ( !$url ) {
        my $spath = $app->static_path;
        $spath .= '/' unless $spath =~ m!/$!;
        $url = $spath . 'support/';
    }
    else {
        $url .= '/' unless $url =~ m!/$!;
    }
    $url;
}

sub support_directory_path {
    my $app  = shift;
    my $path = $app->config('SupportDirectoryPath');
    if ($path) {
        if ( $path !~ m{^/|^[a-zA-Z]:\\|^\\\\[a-zA-Z0-9\.]+} ) {
            return File::Spec->catdir( $app->path, $path );
        }
        return $path;
    }
    else {
        return File::Spec->catdir( $app->static_file_path, 'support' );
    }
}

sub template_paths {
    my $mt = shift;
    my @paths;
    if ( $mt->{plugin_template_path} ) {
        if (File::Spec->file_name_is_absolute( $mt->{plugin_template_path} ) )
        {
            push @paths, $mt->{plugin_template_path}
                if -d $mt->{plugin_template_path};
        }
        else {
            my $dir = File::Spec->catdir( $mt->app_dir,
                $mt->{plugin_template_path} );
            if ( -d $dir ) {
                push @paths, $dir;
            }
            else {
                $dir = File::Spec->catdir( $mt->mt_dir,
                    $mt->{plugin_template_path} );
                push @paths, $dir if -d $dir;
            }
        }
    }
    if ( my $alt_path = $mt->config->AltTemplatePath ) {
        if ( -d $alt_path ) {    # AltTemplatePath is absolute
            push @paths, File::Spec->catdir( $alt_path, $mt->{template_dir} )
                if $mt->{template_dir};
            push @paths, $alt_path;
        }
    }

    for my $addon ( @{ $mt->find_addons('pack') } ) {
        push @paths,
            File::Spec->catdir( $addon->{path}, 'tmpl', $mt->{template_dir} )
            if $mt->{template_dir};
        push @paths, File::Spec->catdir( $addon->{path}, 'tmpl' );
    }

    my $path = $mt->config->TemplatePath;
    push @paths, File::Spec->catdir( $path, $mt->{template_dir} )
        if $mt->{template_dir};
    push @paths, $path;

    return @paths;
}

sub find_file {
    my $mt = shift;
    my ( $paths, $file ) = @_;
    my $filename;
    foreach my $p (@$paths) {
        my $filepath
            = File::Spec->canonpath( File::Spec->catfile( $p, $file ) );
        $filename = File::Spec->canonpath($filepath);
        return $filename if -f $filename;
    }
    undef;
}

sub load_global_tmpl {
    my $app = shift;
    my ( $arg, $blog_id ) = @_;
    $blog_id
        = $blog_id ? [ $blog_id, 0 ]
        : MT->app->blog ? [ MT->app->blog->id, 0 ]
        :                 0;

    my $terms = {};
    if ( 'HASH' eq ref($arg) ) {
        $terms = { %$arg, blog_id => $blog_id };
    }
    else {
        $terms = {
            type    => $arg,
            blog_id => $blog_id,
        };
    }
    my $args;
    if ( ref $blog_id eq 'ARRAY' ) {
        $args->{sort}      = 'blog_id';
        $args->{direction} = 'descend';
        $args->{limit}     = 1;
    }
    require MT::Template;
    my $tmpl = MT::Template->load( $terms, $args );
    $app->set_default_tmpl_params($tmpl) if $tmpl;
    $tmpl;
}

sub load_tmpl {
    my $mt = shift;
    if ( exists( $mt->{component} ) && ( $mt->{component} ne 'Core' ) ) {
        if ( my $c = $mt->component( $mt->{component} ) ) {
            return $c->load_tmpl(@_);
        }
    }

    my ( $file, @p ) = @_;
    my $param;
    if ( @p && ( ref( $p[$#p] ) eq 'HASH' ) ) {
        $param = pop @p;
    }
    my $cfg = $mt->config;
    require MT::Template;
    my $tmpl;
    my @paths = $mt->template_paths;

    my $type
        = { 'SCALAR' => 'scalarref', 'ARRAY' => 'arrayref' }->{ ref $file }
        || 'filename';
    $tmpl = MT::Template->new(
        type   => $type,
        source => $file,
        path   => \@paths,
        filter => sub {
            my ( $str, $fname ) = @_;
            if ($fname) {
                $fname = File::Basename::basename($fname);
                $fname =~ s/\.tmpl$//;
                $mt->run_callbacks( "template_source.$fname", $mt, @_ );
            }
            else {
                $mt->run_callbacks( "template_source", $mt, @_ );
            }
            return $str;
        },
        @p
    );
    return $mt->error(
        $mt->translate( "Loading template '[_1]' failed.", $file ) )
        unless $tmpl;
    $mt->set_default_tmpl_params($tmpl);
    $tmpl->param($param) if $param;
    $tmpl;
}

sub _svn_revision {
    my $mt      = shift;
    my $wc_base = $mt->mt_dir;
    return unless -d File::Spec->catdir( $wc_base, '.git' );

    # Currently, we are on the Github.
    return
        unless ( -e $wc_base && open my $fh, '-|', "git status" );

    my $revision = '';
    if ( -e $wc_base && open my $fh,
        '-|', "git log --pretty=format:'' | wc -l" )
    {
        $revision = do { local $/ = undef; <$fh> };
        chomp $revision;
        $revision =~ s/\s*(.*)/r$1/;
        close $fh;
    }

    my $hash = '';
    if ( -e $wc_base && open my $fh, '-|', "git log -1 | grep commit" ) {
        $hash = do { local $/ = undef; <$fh> };
        chomp $hash;
        if ( $hash =~ s/commit (.*)/$1/ ) {
            $hash = substr( $hash, 0, 8 );
        }
        close $fh;
    }

    my $branch = '';
    if ( -e $wc_base && open my $fh, '-|', "git branch" ) {
        $branch = do { local $/ = undef; <$fh> };
        chomp $branch;
        if ( $branch =~ m/\*\s(.*)/ ) {
            $branch = $1;
        }
        close $fh;
    }

    return { revision => "$revision-$hash", branch => $branch };
}

sub set_default_tmpl_params {
    my $mt     = shift;
    my ($tmpl) = @_;
    my $param  = {};
    $param->{mt_debug} = $MT::DebugMode;
    if ( $param->{mt_debug} && $mt->isa('MT::App') ) {
        $param->{mt_svn_revision} = $mt->_svn_revision();
        if ( $ENV{MOD_PERL} && exists( $mt->{apache} ) ) {
            $param->{mt_headers} = $mt->{apache}->headers_in();
        }
        else {
            $param->{mt_headers} = \%ENV;
        }
        unless ( $mt->{cookies} ) {
            if ( $ENV{MOD_PERL} ) {
                eval { require Apache::Cookie };
                $mt->{cookies} = Apache::Cookie->fetch;
            }
            else {
                eval { require CGI::Cookie };
                $mt->{cookies} = CGI::Cookie->fetch;
            }
        }
        if ( $mt->{cookies} ) {
            $param->{mt_cookies} = $mt->{cookies};
        }
        my %params = $mt->param_hash;
        $param->{mt_queries} = \%params;
        if ( $param->{mt_debug} & 4 ) {
            if ( my $profiler = Data::ObjectDriver->profiler ) {
                my $stats = $profiler->statistics;
                $param->{mt_sql_profile}{statistics} = $stats;
                $param->{mt_sql_profile}{total_queries}
                    = $stats->{'DBI:total_queries'};

                my $freq = $profiler->query_frequency;
                my @cache_types;
                foreach ( keys %$freq ) {
                    my ( $cache_type, $memcache, $method )
                        = $_ =~ /^(.+)CACHE(D?)_(.+)\s\?/;
                    next unless $cache_type;
                    push @cache_types,
                        $cache_type . ':query_' . lc($method),
                        delete $freq->{$_};
                }
                $param->{mt_sql_profile}{query_frequency} = $freq;
                $param->{mt_cache_profile} = [];
                while ( my $k = shift(@cache_types) ) {
                    push @{ $param->{mt_cache_profile} }, $k,
                        shift(@cache_types);
                }
            }
        }
    }
    $param->{mt_beta}         = 1 if MT->version_id =~ m/^\d+\.\d+(?:a|b|rc)/;
    $param->{static_uri}      = $mt->static_path;
    $param->{mt_version}      = MT->version_number;
    $param->{mt_version_id}   = MT->version_id;
    $param->{mt_product_code} = MT->product_code;
    $param->{mt_product_name} = $mt->translate( MT->product_name );
    $param->{language_tag}    = substr( $mt->current_language, 0, 2 );
    $param->{language_encoding} = $mt->charset;
    $param->{optimize_ui} = $mt->build_id && !$MT::DebugMode;

    if ( $mt->isa('MT::App') ) {
        if ( my $author = $mt->user ) {
            $param->{author_id}   = $author->id;
            $param->{author_name} = $author->name;
        }
        ## We do this in load_tmpl because show_error and login don't call
        ## build_page; so we need to set these variables here.
        require MT::Auth;
        $param->{can_logout}      = MT::Auth->can_logout;
        $param->{script_url}      = $mt->uri;
        $param->{mt_url}          = $mt->mt_uri;
        $param->{script_path}     = $mt->path;
        $param->{script_full_url} = $mt->base . $mt->uri;
        $param->{agent_mozilla} = ( $ENV{HTTP_USER_AGENT} || '' ) =~ /gecko/i;
        $param->{agent_ie} = ( $ENV{HTTP_USER_AGENT} || '' ) =~ /\bMSIE\b/;
    }
    if ( !$tmpl->param('template_filename') ) {
        if ( my $fname = $tmpl->{__file} ) {
            $fname =~ s!\\!/!g;
            $fname =~ s/\.tmpl$//;
            $param->{template_filename} = $fname;
        }
    }
    $tmpl->param($param);
}

sub process_mt_template {
    my $mt = shift;
    use bytes;
    my ($body) = @_;
    $body =~ s@<(?:_|MT)_ACTION\s+mode="([^"]+)"(?:\s+([^>]*))?>@
        my $mode = $1; my %args;
        %args = $2 =~ m/\s*(\w+)="([^"]*?)"\s*/g if defined $2; # "
        MT::Util::encode_html($mt->uri(mode => $mode, args => \%args));
    @geis;

    # Strip out placeholder wrappers to facilitate tmpl_* callbacks
    $body =~ s/<\/?MT_\w+:\w+>//g;
    $body;
}

sub build_page {
    my $mt = shift;
    my ( $file, $param ) = @_;
    my $tmpl;
    my $mode = $mt->mode;
    $param->{"mode_$mode"} ||= 1;
    $param->{breadcrumbs} = $mt->{breadcrumbs};
    if ( $param->{breadcrumbs}[-1] ) {
        $param->{breadcrumbs}[-1]{is_last} = 1;
        $param->{page_titles} = [ reverse @{ $mt->{breadcrumbs} } ];
    }
    pop @{ $param->{page_titles} };
    if ( my $lang_id = $mt->current_language ) {
        $param->{local_lang_id} ||= lc $lang_id;
    }
    $param->{magic_token} = $mt->current_magic if $mt->user;

    # List of installed packs in the application footer
    my @packs_installed;
    my $packs = $mt->find_addons('pack');
    if ($packs) {
        foreach my $pack (@$packs) {
            my $c = $mt->component( lc $pack->{id} );
            if ($c) {
                my $label = $c->label || $pack->{label};
                $label = $label->() if ref($label) eq 'CODE';

                # if the component did not declare a label,
                # it isn't wanting to be visible on the app footer.
                next if $label eq $c->{plugin_sig};
                push @packs_installed,
                    {
                    label   => $label,
                    version => $c->version,
                    id      => $c->id,
                    };
            }
        }
    }
    @packs_installed = sort { $a->{label} cmp $b->{label} } @packs_installed;
    $param->{packs_installed} = \@packs_installed;

    $param->{portal_url} = &portal_url;

    for my $config_field ( keys %{ MT::ConfigMgr->instance->{__var} || {} } )
    {
        $param->{ $config_field . '_readonly' } = 1;
    }

    my $tmpl_file = '';
    if ( UNIVERSAL::isa( $file, 'MT::Template' ) ) {
        $tmpl = $file;
        $tmpl_file = ( exists $file->{__file} ) ? $file->{__file} : '';
    }
    else {
        $tmpl = $mt->load_tmpl($file) or return;
        $tmpl_file = $file unless ref($file);
    }

    if (( $mode && ( $mode !~ m/delete/ ) )
        && ( $mt->{login_again}
            || ( $mt->{requires_login} && !$mt->user ) )
        )
    {
        ## If it's a login screen, direct the user to where they were going
        ## (query params including mode and all) unless they were logging in,
        ## logging out, or deleting something.
        my $q = $mt->{query};
        if ($mode) {
            my @query;
            my @query_keys = grep {
                       ( $_ ne 'username' )
                    && ( $_ ne 'password' )
                    && ( $_ ne 'submit' )
                    && ( $mode eq 'logout' ? ( $_ ne '__mode' ) : 1 )
            } $q->param;
            for my $query_key (@query_keys) {
                my @vals = $q->param($query_key);
                for my $val (@vals) {
                    push @query, { name => $query_key, value => $val };
                }
            }
            $param->{query_params} = \@query;
        }
        $param->{login_again} = $mt->{login_again};
    }

    my $blog = $mt->blog;
    $tmpl->context()->stash( 'blog', $blog ) if $blog;

    $tmpl->param($param) if $param;

    if ($tmpl_file) {
        $tmpl_file = File::Basename::basename($tmpl_file);
        $tmpl_file =~ s/\.tmpl$//;
        $tmpl_file = '.' . $tmpl_file;
    }
    $mt->run_callbacks( 'template_param' . $tmpl_file,
        $mt, $tmpl->param, $tmpl );

    my $output = $mt->build_page_in_mem($tmpl);
    return unless defined $output;

    $mt->run_callbacks( 'template_output' . $tmpl_file,
        $mt, \$output, $tmpl->param, $tmpl );
    return $output;
}

sub build_page_in_mem {
    my $mt = shift;
    my ( $tmpl, $param ) = @_;
    $tmpl->param($param) if $param;
    my $out = $tmpl->output;
    return $mt->error( $tmpl->errstr ) unless defined $out;
    return $mt->translate_templatized( $mt->process_mt_template($out) );
}

sub new_ua {
    my $class = shift;
    my ($opt) = @_;
    $opt ||= {};
    my $lwp_class = 'LWP::UserAgent';
    if ( $opt->{paranoid} ) {
        eval { require LWPx::ParanoidAgent; };
        $lwp_class = 'LWPx::ParanoidAgent' unless $@;
    }
    eval "require $lwp_class;";
    return undef if $@;
    my $cfg = $class->config;
    my $max_size = exists $opt->{max_size} ? $opt->{max_size} : 100_000;
    my $timeout = exists $opt->{timeout} ? $opt->{timeout} : $cfg->HTTPTimeout
        || $cfg->PingTimeout;
    my $proxy = exists $opt->{proxy} ? $opt->{proxy} : $cfg->HTTPProxy
        || $cfg->PingProxy;
    my $sec_proxy
        = exists $opt->{sec_proxy} ? $opt->{sec_proxy} : $cfg->HTTPSProxy;
    my $no_proxy
        = exists $opt->{no_proxy} ? $opt->{no_proxy} : $cfg->HTTPNoProxy
        || $cfg->PingNoProxy;
    my $agent = $opt->{agent} || $MT::PRODUCT_NAME . '/' . $MT::VERSION;
    my $interface
        = exists $opt->{interface} ? $opt->{interface} : $cfg->HTTPInterface
        || $cfg->PingInterface;

    if ( my $localaddr = $interface ) {
        @LWP::Protocol::http::EXTRA_SOCK_OPTS = (
            LocalAddr => $localaddr,
            Reuse     => 1
        );
    }

    my $ua = $lwp_class->new;
    $ua->max_size($max_size) if ( defined $max_size ) && $ua->can('max_size');
    $ua->agent($agent);
    $ua->timeout($timeout) if defined $timeout;
    eval { require HTML::HeadParser; };
    $ua->parse_head(0) if $@;
    if ( defined $proxy ) {
        $ua->proxy( http => $proxy );
        my @domains = split( /,\s*/, $no_proxy ) if $no_proxy;
        $ua->no_proxy(@domains) if @domains;
    }
    if ( defined $sec_proxy ) {
        $ua->proxy( https => $sec_proxy );
    }
    return $ua;
}

sub build_email {
    my $class = shift;
    my ( $file, $param ) = @_;
    my $mt = $class->instance;

    # basically, try to load from database
    my $blog = $param->{blog} || undef;
    my $id = $file;
    $id =~ s/(\.tmpl|\.mtml)$//;

    require MT::Template;
    my @tmpl = MT::Template->load(
        {   ( $blog ? ( blog_id => [ $blog->id, 0 ] ) : ( blog_id => 0 ) ),
            identifier => $id,
            type       => 'email',
        }
    );
    my $tmpl
        = @tmpl
        ? (
        scalar @tmpl > 1
        ? ( $tmpl[0]->blog_id ? $tmpl[0] : $tmpl[1] )
        : $tmpl[0]
        )
        : undef;

    # try to load from file
    unless ($tmpl) {
        local $mt->{template_dir} = 'email';
        $tmpl = $mt->load_tmpl($file);
    }
    return unless $tmpl;

    my $ctx = $tmpl->context;
    $ctx->stash( 'blog_id', $blog->id ) if $blog;
    foreach my $name (qw{blog entry author commenter comment category ping}) {
        $ctx->stash( $name, delete $param->{$name} ) if $param->{$name};
    }

    my $out = $mt->build_page_in_mem( $tmpl, $param );

    require MT::Log;
    $mt->log(
        {   message => $mt->translate(
                "Error during building email: [_1]",
                $mt->errstr
            ),
            class    => 'system',
            category => 'email',
            level    => MT::Log::ERROR(),
        }
    ) unless defined $out;

    $out;
}

sub get_next_sched_post_for_user {
    my ( $author_id, @further_blog_ids ) = @_;
    require MT::Permission;
    my @perms = MT::Permission->load( { author_id => $author_id }, {} );
    my @blogs = @further_blog_ids;
    for my $perm (@perms) {
        next unless $perm->can_do('load_next_scheduled_entry');
        push @blogs, $perm->blog_id;
    }
    my $next_sched_utc = undef;
    require MT::Entry;
    for my $blog_id (@blogs) {
        my $blog = MT::Blog->load($blog_id)
            or next;
        my $earliest_entry = MT::Entry->load(
            {   status  => MT::Entry::FUTURE(),
                blog_id => $blog_id
            },
            { 'sort' => 'created_on' }
        );
        if ($earliest_entry) {
            my $entry_utc
                = MT::Util::ts2iso( $blog, $earliest_entry->created_on );
            if ( $entry_utc < $next_sched_utc || !defined($next_sched_utc) ) {
                $next_sched_utc = $entry_utc;
            }
        }
    }
    return $next_sched_utc;
}

our $Commenter_Auth;

sub init_commenter_authenticators {
    my $self = shift;
    my $auths = $self->registry("commenter_authenticators") || {};
    $Commenter_Auth = {%$auths};
    my $app = $self->app;
    my $blog = $app->blog if $app->isa('MT::App');
    foreach my $auth ( keys %$auths ) {
        if ( my $c = $auths->{$auth}->{condition} ) {
            $c = $self->handler_to_coderef($c);
            if ($c) {
                delete $Commenter_Auth->{$auth} unless $c->($blog);
            }
        }
    }
    $Commenter_Auth->{$_}{key} ||= $_ for keys %$Commenter_Auth;
}

sub commenter_authenticator {
    my $self = shift;
    my ( $key, %param ) = @_;
    $Commenter_Auth or $self->init_commenter_authenticators();

    return
        if ( !exists $Commenter_Auth->{$key}
        || ( $Commenter_Auth->{$key}->{disable} && !$param{force} ) );
    return $Commenter_Auth->{$key};
}

sub commenter_authenticators {
    my $self = shift;
    my (%param) = @_;
    $Commenter_Auth or $self->init_commenter_authenticators();
    my %auths = %$Commenter_Auth;
    if ( !$param{force} ) {
        foreach my $auth ( keys %auths ) {
            delete $auths{$auth} if $auths{$auth}->{disable};
        }
    }

    return values %auths;
}

sub _commenter_auth_params {
    my ( $key, $blog_id, $entry_id, $static ) = @_;
    my $params = {
        blog_id => $blog_id,
        static  => $static,
    };
    $params->{entry_id} = $entry_id if defined $entry_id;
    return $params;
}

sub _openid_commenter_condition {
    eval { require Digest::SHA; };
    return $@ ? 0 : 1;
}

sub core_commenter_authenticators {
    return {
        'OpenID' => {
            class             => 'MT::Auth::OpenID',
            label             => 'OpenID',
            login_form        => 'comment/auth_openid.tmpl',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/signin_openid.png',
            logo_small        => 'images/comment/openid_logo.png',
            order             => 10,
        },
        'LiveJournal' => {
            class             => 'MT::Auth::LiveJournal',
            label             => 'LiveJournal',
            login_form        => 'comment/auth_livejournal.tmpl',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/signin_livejournal.png',
            logo_small        => 'images/comment/livejournal_logo.png',
            order             => 11,
        },
        'Vox' => {
            class             => 'MT::Auth::Vox',
            label             => 'Vox',
            login_form        => 'comment/auth_vox.tmpl',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/signin_vox.png',
            logo_small        => 'images/comment/vox_logo.png',
            order             => 12,
            disable           => 1,
        },
        'Google' => {
            label      => 'Google',
            class      => 'MT::Auth::GoogleOpenId',
            login_form => 'comment/auth_googleopenid.tmpl',
            condition  => sub {
                eval { require Digest::SHA; require Crypt::SSLeay; };
                return 0 if $@;
                return 1;
            },
            login_form_params => \&_commenter_auth_params,
            logo              => 'images/comment/google.png',
            logo_small        => 'images/comment/google_logo.png',
            order             => 13,
        },
        'Yahoo' => {
            class             => 'MT::Auth::Yahoo',
            label             => 'Yahoo!',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/yahoo.png',
            logo_small        => 'images/comment/favicon_yahoo.png',
            login_form        => 'comment/auth_yahoo.tmpl',
            order             => 14,
        },
        AIM => {
            class             => 'MT::Auth::AIM',
            label             => 'AIM',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/aim.png',
            logo_small        => 'images/comment/aim_logo.png',
            login_form        => 'comment/auth_aim.tmpl',
            order             => 15,
        },
        'WordPress' => {
            class             => 'MT::Auth::WordPress',
            label             => 'WordPress.com',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/wordpress.png',
            logo_small        => 'images/comment/wordpress_logo.png',
            login_form        => 'comment/auth_wordpress.tmpl',
            order             => 16,
        },
        'TypeKey' => {
            class             => 'MT::Auth::TypeKey',
            label             => 'TypePad',
            login_form        => 'comment/auth_typepad.tmpl',
            login_form_params => sub {
                my ( $key, $blog_id, $entry_id, $static ) = @_;
                my $entry = MT::Entry->load($entry_id) if $entry_id;

                ## TypeKey URL
                require MT::Template::Context;
                my $ctx = MT::Template::Context->new;
                $ctx->stash( 'blog_id', $blog_id );
                my $blog = MT::Blog->load($blog_id);
                $ctx->stash( 'blog',  $blog );
                $ctx->stash( 'entry', $entry );
                my $params = {};
                require MT::Template::Tags::Comment;
                $params->{tk_signin_url}
                    = MT::Template::Tags::Comment::_hdlr_remote_sign_in_link(
                    $ctx, { static => $static } );
                return $params;
            },
            logo       => 'images/comment/signin_typepad.png',
            logo_small => 'images/comment/typepad_logo.png',
            condition  => sub {
                my ($blog) = @_;
                return 1 unless $blog;
                return $blog->remote_auth_token ? 1 : 0;
            },
            order => 17,
        },
        'YahooJP' => {
            class             => 'MT::Auth::Yahoo',
            label             => 'Yahoo! JAPAN',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/yahoo.png',
            logo_small        => 'images/comment/favicon_yahoo.png',
            login_form        => 'comment/auth_yahoojapan.tmpl',
            order             => 18,
        },
        'livedoor' => {
            class             => 'MT::Auth::OpenID',
            label             => 'livedoor',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/signin_livedoor.png',
            logo_small        => 'images/comment/livedoor_logo.png',
            login_form        => 'comment/auth_livedoor.tmpl',
            order             => 20,
        },
        'Hatena' => {
            class             => 'MT::Auth::Hatena',
            label             => 'Hatena',
            login_form        => 'comment/auth_hatena.tmpl',
            login_form_params => \&_commenter_auth_params,
            condition         => \&_openid_commenter_condition,
            logo              => 'images/comment/signin_hatena.png',
            logo_small        => 'images/comment/hatena_logo.png',
            order             => 21,
        },
    };
}

our %Captcha_Providers;

sub captcha_provider {
    my $self = shift;
    my ($key) = @_;
    $self->init_captcha_providers() unless %Captcha_Providers;
    return $Captcha_Providers{$key};
}

sub captcha_providers {
    my $self = shift;
    $self->init_captcha_providers() unless %Captcha_Providers;
    my $def  = delete $Captcha_Providers{'mt_default'};
    my @vals = values %Captcha_Providers;
    if ( defined($def) && $def->{condition}->() ) {
        unshift @vals, $def;
    }
    @vals;
}

sub core_captcha_providers {
    return {
        'mt_default' => {
            label     => 'Movable Type default',
            class     => 'MT::Util::Captcha',
            condition => sub {
                require MT::Util::Captcha;
                if ( my $error = MT::Util::Captcha->check_availability ) {
                    return 0;
                }
                1;
            },
        }
    };
}

sub init_captcha_providers {
    my $self = shift;
    my $providers = $self->registry("captcha_providers") || {};
    foreach my $provider ( keys %$providers ) {
        delete $providers->{$provider}
            if exists( $providers->{$provider}->{condition} )
                && !( $providers->{$provider}->{condition}->() );
    }
    %Captcha_Providers = %$providers;
    $Captcha_Providers{$_}{key} ||= $_ for keys %Captcha_Providers;
}

sub effective_captcha_provider {
    my $class = shift;
    my ($key) = @_;
    return undef unless $key;
    my $cp = $class->captcha_provider($key) or return;
    if ( exists $cp->{condition} ) {
        return undef unless $cp->{condition}->();
    }
    my $pkg = $cp->{class};
    $pkg =~ s/;//g;
    eval "require $pkg" or return;
    return $cp->{class};
}

sub handler_to_coderef {
    my $pkg = shift;
    my ( $name, $delayed ) = @_;

    return $name if ref($name) eq 'CODE';
    return undef unless defined $name && $name ne '';

    my $code;
    if ( $name !~ m/->/ ) {

        # check for Package::Routine first; if defined, return coderef
        no strict 'refs';
        $code = \&$name if defined &$name;
        return $code if $code;
    }

    my $component;
    if ( $name =~ m!^\$! ) {
        if ( $name =~ s/^\$(\w+)::// ) {
            $component = $1;
        }
    }
    if ( $name =~ m/^\s*sub\s*\{/s ) {
        $code = eval $name or die $@;

        if ($component) {
            return sub {
                my $mt_inst = MT->instance;
                local $mt_inst->{component} = $component;
                $code->(@_);
            };
        }
        else {
            return $code;
        }
    }

    my $hdlr_pkg = $name;
    my $method;

    # strip routine name
    if ( $hdlr_pkg =~ s/->(\w+)$// ) {
        $method = $1;
    }
    else {
        $hdlr_pkg =~ s/::[^:]+$//;
    }
    if ( !defined(&$name) && !$pkg->can('AUTOLOAD') ) {

        # The delayed option will return a coderef that delays the loading
        # of the package holding the handler routine.
        if ($delayed) {
            if ($method) {
                return sub {
                    eval "# line " 
                        . __LINE__ . " " 
                        . __FILE__
                        . "\nrequire $hdlr_pkg;"
                        or Carp::confess(
                        "failed loading package $hdlr_pkg for routine $name: $@"
                        );
                    my $mt_inst = MT->instance;
                    local $mt_inst->{component} = $component
                        if $component;
                    return $hdlr_pkg->$method(@_);
                };
            }
            else {
                return sub {
                    eval "# line " 
                        . __LINE__ . " " 
                        . __FILE__
                        . "\nrequire $hdlr_pkg;"
                        or Carp::confess(
                        "failed loading package $hdlr_pkg for routine $name: $@"
                        );
                    my $mt_inst = MT->instance;
                    local $mt_inst->{component} = $component
                        if $component;
                    no strict 'refs';
                    my $hdlr = \&$name;
                    use strict 'refs';
                    return $hdlr->(@_);
                };
            }
        }
        else {
            eval "# line " 
                . __LINE__ . " " 
                . __FILE__
                . "\nrequire $hdlr_pkg;"
                or Carp::confess(
                "failed loading package $hdlr_pkg for routine $name: $@");
        }
    }
    if ($method) {
        $code = sub {
            my $mt_inst = MT->instance;
            local $mt_inst->{component} = $component
                if $component;
            return $hdlr_pkg->$method(@_);
        };
    }
    else {
        if ($component) {
            $code = sub {
                no strict 'refs';
                my $hdlr = (
                    defined &$name ? \&$name
                    : ( $pkg->can('AUTOLOAD') ? \&$name
                        : undef
                    )
                );
                use strict 'refs';
                if ($hdlr) {
                    my $mt_inst = MT->instance;
                    local $mt_inst->{component} = $component
                        if $component;
                    return $hdlr->(@_);
                }
                return undef;
                }
        }
        else {
            no strict 'refs';
            $code = (
                defined &$name
                ? \&$name
                : ( $hdlr_pkg->can('AUTOLOAD') ? \&$name : undef )
            );
        }
    }
    return $code;
}

sub help_url {
    my $pkg = shift;
    my ($append) = @_;

    my $url = $pkg->config->HelpURL;
    return $url if defined $url;
    $url = $pkg->translate('http://www.movabletype.org/documentation/');
    if ($append) {
        $url .= $append;
    }
    $url;
}

sub register_refresh_cache_event {
    my $pkg = shift;
    my ($callback) = @_;
    return unless $callback;

    MT->_register_core_callbacks( { "$callback" => \&refresh_cache, } );
}

sub refresh_cache {
    my ( $cb, %args ) = @_;

    require MT::Cache::Negotiate;
    my $cache_driver = MT::Cache::Negotiate->new();
    return unless $cache_driver;

    $cache_driver->flush_all();
}

sub DESTROY { }

1;

__END__

=head1 NAME

MT - Movable Type

=head1 SYNOPSIS

    use MT;
    my $mt = MT->new;
    $mt->rebuild(BlogID => 1)
        or die $mt->errstr;

=head1 DESCRIPTION

The I<MT> class is the main high-level rebuilding/pinging interface in the
Movable Type library. It handles all rebuilding operations. It does B<not>
handle any of the application functionality--for that, look to I<MT::App> and
I<MT::App::CMS>, both of which subclass I<MT> to handle application requests.

=head1 PLUGIN APPLICATIONS

At any given time, the user of the Movable Type platform is
interacting with either the core Movable Type application, or a plugin
application (or "sub-application").

A plugin application is a plugin with a user interface that inherits
functionality from Movable Type, and appears to the user as a
component of Movable Type. A plugin application typically has its own
templates displaying its own special features; but it inherits some
templates from Movable Type, such as the navigation chrome and error
pages.

=head2 The MT Root and the Application Root

To locate assets of the core Movable Type application and any plugin
applications, the platform uses two directory paths, C<mt_dir> and
C<app_dir>. These paths are returned by the MT class methods with the
same names, and some other methods return derivatives of these paths.

Conceptually, mt_dir is the root of the Movable Type installation, and
app_dir is the root of the "currently running application", which
might be Movable Type or a plugin application. It is important to
understand the distinction between these two values and what each is
used for.

The I<mt_dir> is the absolute path to the directory where MT itself is
located. Most importantly, the MT configuration file and the CGI scripts that
bootstrap an MT request are found here. This directory is also the
default base path under which MT's core templates are found (but this
can be overridden using the I<TemplatePath> configuration setting).

Likewise, the I<app_dir> is the directory where the "current"
application's assets are rooted. The platform will search for
application templates underneath the I<app_dir>, but this search also
searches underneath the I<mt_dir>, allowing the application to make
use of core headers, footers, error pages, and possibly other
templates.

In order for this to be useful, the plugin's templates and
code should all be located underneath the same directory. The relative
path from the I<app_dir> to the application's templates is
configurable. For details on how to indicate the location of your
plugin's templates, see L<MT::App>.

=head2 Finding the Root Paths

When a plugin application initializes its own application class (a
subclass of MT::App), the I<mt_dir> should be discovered and passed
constructor. This comes either from the C<Directory> parameter or the
C<Config> parameter.

Since plugins are loaded from a descendent of the MT root directory,
the plugin bootstrap code can discover the MT configuration file (and thus
the MT root directory) by traversing the filesystem; the absolute path
to that file can be passed as the C<Config> parameter to
MT::App::new. Working code to do this can be found in the
examples/plugins/mirror/mt-mirror.cgi file.

The I<app_dir>, on the other hand, always derives from the location of
the currently-running program, so it typically does not need to be
specified.

=head1 USAGE

I<MT> has the following interface. On failure, all methods return C<undef>
and set the I<errstr> for the object or class (depending on whether the
method is an object or class method, respectively); look below at the section
L<ERROR HANDLING> for more information.

=head2 MT->new( %args )

Returns a I<MT> singleton instance. Returns C<undef> on failure. 
If no active instance exists, will set that object as active instance.

I<new> will also read your MT configuration file (provided that it can find it--if
you find that it can't, take a look at the I<Config> directive, below). It
will also initialize the chosen object driver; the default is the C<DBM>
object driver.

I<%args> can contain:

=over 4

=item * Config

Path to the MT configuration file.

If you do not specify a path, I<MT> will try to find your MT configuration file
in the current working directory.

=item * Directory

Path to the MT home directory.

If you do not specify a path, I<MT> will try to find the MT directory using
the discovered path of the MT configuration file.

=back

=head2 MT->instance( %args )

If exists an active instance, will return it. otherwise will return
the asked class's singleton, making it the active instance.

%args are similar to C<new>

=head2 MT->app( %args )

An alias for the 'instance' method.

=head2 $class->instance_of( %args )

Similar to C<new>, but does not set the active instance

=head2 $mt->init(%params)

Initializes the Movable Type instance, including registration of basic
resources and callbacks. This method also invokes the C<init_config>
and C<init_plugins> methods.

=head2 $mt->init_core()

A method that the base MT class uses to initialize all the 'core'
functionality of Movable Type. If you want to subclass MT and extensively
modify it's core behavior, this method can be overridden to do that.
The L<MT::Core> module is a L<MT::Component> that defines the core
features of MT, and this method loads that component. Non-core components
are loaded by the L<init_addons> method.

=head2 $mt->init_paths()

Establishes some key file paths for the MT environment. Assigns
C<$MT_DIR>, C<$APP_DIR> and C<$CFG_FILE> package variables.

=head2 $mt->init_permissions()

Loads the L<MT::Permission> class and runs the
MT::Permission->init_permissions method to establish system permissions.

=head2 $mt->init_schema()

Completes the initialization of the Movable Type schema following the
loading of plugins. After this method runs, any MT object class may
safely be used.

=head2 $class->construct

Constructs a new instance of the MT subclass identified by C<$class>.

=head2 MT->set_instance

Assigns the active MT instance object. This value is returned when
C<MT-E<gt>instance> is invoked.

=head2 MT->run_app( $pkg, $params )

Instantiates and runs a MT application (identified by C<$pkg>), passing
the C<$params> hashref as the parameters to the constructor method. This
method is a self-contained version found in L<MT::Bootstrap> and will
eventually be the manner in which MT applications are run (eliminating
the need for the bootstrap module). The MT::import function calls this
method when the MT module is used with an 'App' parameter. So, you can
write a mt.cgi script that looks like this:

    #!/usr/bin/perl
    use strict;
    use lib $ENV{MT_HOME} ? "$ENV{MT_HOME}/lib" : 'lib';
    use MT App => 'MT::App::CMS';

=head2 $mt->find_config($params)

Handles the discovery of the MT configuration file. The path and filename
for the configuration file is returned as the result. The C<$params>
parameter is a reference to the hash of settings passed to the MT
constructor.

=head2 $mt->init_config($params)

Reads the MT configuration settingss from the MT configuration file.

The C<$params> parameter is a reference to the hash of settings passed to
the MT constructor.

=head2 $mt->init_config_from_db($param)

Reads any MT configuration settings from the MT database (L<MT::Config>).

The C<$params> parameter is a reference to the hash of settings passed to
the MT constructor.

=head2 $mt->init_addons(%param)

Loads any discoverable addons that are available. This is called from
the C<init> method, after C<init_config> method has loaded the
configuration settings, but prior to making a database connection.

=head2 $mt->init_plugins(%param)

Loads any discoverable plugins that are available. This is called from
the C<init> method, after the C<init_config> method has loaded the
configuration settings.

=head2 $mt->init_callbacks()

Installs any MT callbacks. This is called from the C<init> method very,
early; prior to loading any addons or plugins.

=head2 MT->run_tasks

Invokes the task system through L<MT::TaskMgr> to run any registered tasks 
that are pending execution. See L<MT::TaskMgr> for further documentation.

=head2 MT->find_addons( $type )

Returns an array of all 'addons' that are found within the MT 'addons'
directory of the given C<$type>. What is returned is an array reference
of hash data. Each hash will contain these elements: 'label' (the name
of the addon), 'id' (the unique identifier of the addon), 'envelope'
(the subpath of the addon, relative to the MT home directory), and 'path'
(the full path to the addon subdirectory).

=head2 MT::log( $message ) or $mt->log( $message )

Adds an entry to the application's log table. Also writes message to
STDERR which is typically routed to the web server's error log.
Examples:

    $mt->log('I would like you to know');
    $mt->log( { message => 'that this is important', level => MT::Log::ERROR() } );
    # can also use metadata, category, blog_id, author_id and ip

=head2 $mt->server_path, $mt->mt_dir

Both of these methods return the physical file path to the directory
that is the home of the MT installation. This would be the value of
the 'Directory' parameter given in the MT constructor, or would be
determined based on the path of the configuration file.

=head2 $mt->app_dir

Returns the physical file path to the active application directory. This
is determined by the directory of the active script.

=head2 $mt->config_dir

Returns the path to the MT configuration file.

=head2 $mt->config([$setting[, $value]])

This method is used to get and set configuration settings. When called
without any parameters, it returns the active MT::ConfigMgr instance
used by the application.

Specifying the C<$setting> parameter will return the value for that setting.
When passing the C<$value> parameter, this will update the config object,
assigning that value for the named C<$setting>.

=head2 $mt->user_class

Returns the package name for the class used for user authentication.
This is typically L<MT::Author>.

=head2 $mt->request([$element[,$data]])

The request method provides a request-scoped storage object. It is an
access interface for the L<MT::Request> package. Calling without any
parameters will return the L<MT::Request> instance.

When called with the C<$element> parameter, the data stored for that
element is returned (or undef, if it didn't exist). When called with
the C<$data> parameter, it will store the data into the specified
element in the request object.

All values placed in the request object are lost at the end of the
request. If the running application is not a web-based application,
the request object exists for the lifetime of the process and is
released when the process ends.

See the L<MT::Request> package for more information.

=head2 MT->new_ua

Returns a new L<LWP::UserAgent> instance that is configured according to the
Movable Type configuration settings (specifically C<HTTPInterface>, C<HTTPTimeout>, C<HTTPProxy> and C<HTTPNoProxy>). The agent string is set
to "MovableType/(version)" and is also limited to receiving a response of
100,000 bytes by default (you can override this by using the 'max_size'
method on the returned instance). Using this method is recommended for
any HTTP requests issued by Movable Type since it uses the MT configuration
settings to prepare the UserAgent object.

=head2 $mt->ping( %args )

Sends all configured XML-RPC pings as a way of notifying other community
sites that your blog has been updated.

I<%args> can contain:

=over 4

=item * Blog

An I<MT::Blog> object corresponding to the blog for which you would like to
send the pings.

Either this or C<BlogID> is required.

=item * BlogID

The ID of the blog for which you would like to send the pings.

Either this or C<Blog> is required.

=item * OldStatus

Optional, can contain the previous status of the entry. if the entry
was already released, don't send update to all the blogs that track
this blog, only to trackbacks register on this entry.

=item * Entry

An I<MT::Entry> object corresponding to the entry that was updated.

=head2 $mt->ping_and_save( %args )

Handles the task of issuing any pending ping operations for a given
entry and then saving that entry back to the database.

The I<%args> hash should contain an element named C<Entry> that is a
reference to a L<MT::Entry> object, and other elements as needed by the
ping function.

=head2 $mt->needs_ping(%param)

Returns a list of URLs that have not been pinged for a given entry. Named
parameters for this method are:

=over 4

=item Entry

The L<MT::Entry> object to examine.

=item Blog

The L<MT::Blog> object that is the parent of the entry given.

=back

The return value is an array reference of URLs that have not been pinged
for the given entry.

An empty list is returned for entries that have a non 'RELEASE' status.

=head2 $mt->update_ping_list($blog)

Returns a list of URLs for ping services that have been configured to
be notified when posting new entries.

=head2 $mt->set_language($tag)

Loads the localization plugin for the language specified by I<$tag>, which
should be a valid and supported language tag--see I<supported_languages> to
obtain a list of supported languages.

The language is set on a global level, and affects error messages and all
text in the administration system.

This method can be called as either a class method or an object method; in
other words,

    MT->set_language($tag)

will also work. However, the setting will still be global--it will not be
specified to the I<$mt> object.

The default setting--set when I<MT::new> is called--is U.S. English. If a
I<DefaultLanguage> is set in the MT configuration file, the default is then
set to that language.

=head2 MT->translate($str[, $param, ...])

Translates I<$str> into the currently-set language (set by I<set_language>),
and returns the translated string. Any parameters following I<$str> are
passed through to the C<maketext> method of the active localization module.

=head2 MT->translate_templatized($str)

Translates a string that has embedded E<lt>__transE<gt> tags. These
tags identify the portions of the string that require localization.
Each tag is processed separately and passed through the MT->translate
method. Examples (used in your application's HTML::Template templates):

    <p><__trans phrase="Hello, world"></p>

and

    <p><__trans phrase="Hello, [_1]" params="<TMPL_VAR NAME=NAME>"></p>

=head2 $mt->trans_error( $str[, $arg1, $arg2] )

Translates I<$str> into the currently-set language (set by I<set_language>),
and assigns it as the active error for the MT instance. It returns undef,
which is the usual return value upon generating an error in the application.
So when an error occurs, the typical return result would be:

    if ($@) {
        return $app->trans_error("An error occurred: [_1]", $@);
    }

The optional I<$arg1> (and so forth) parameters are passed as parameters to
any parameterized error message.

=head2 $mt->current_language

Returns the language tag for the currently-set language.

=head2 MT->supported_languages

Returns a reference to an associative array mapping language tags to their
proper names. For example:

    use MT;
    my $langs = MT->supported_languages;
    print map { $_ . " => " . $langs->{$_} . "\n" } keys %$langs;

=head2 MT->language_handle

Returns the active MT::L10N language instance for the active language.

=head2 MT->add_plugin($plugin)

Adds the plugin described by $plugin to the list of plugins displayed
on the welcome page. The argument should be an object of the
I<MT::Plugin> class.

This function can be used only while MT is loading addons/plugins,
to instruct MT to load sub-addons/plugins.

=head2 MT->all_text_filters

Returns a reference to a hash containing the registry of text filters.

=head2 MT->apply_text_filters($str, \@filters)

Applies the set of filters I<\@filters> to the string I<$str> and returns
the result (the filtered string).

I<\@filters> should be a reference to an array of filter keynames--these
are the short names passed in as the first argument to I<add_text_filter>.
I<$str> should be a scalar string to be filtered.

If one of the filters listed in I<\@filters> is not found in the list of
registered filters (that is, filters added through I<add_text_filter>),
it will be skipped silently. Filters are executed in the order in which they
appear in I<\@filters>.

As it turns out, the I<MT::Entry::text_filters> method returns a reference
to the list of text filters to be used for that entry. So, for example, to
use this method to apply filters to the main entry text for an entry
I<$entry>, you would use

    my $out = MT->apply_text_filters($entry->text, $entry->text_filters);

=head2 MT->add_callback($meth, $priority, $plugin, $code)

Registers a new callback handler for a particular registered callback.

The first parameter is the name of the callback method. 
The second parameter is a priority (a number in the range of 1-10) 
which will control the order that the handler is executed in relation 
to other handlers. (callbacks with priority 1 will be called first) If
two handlers register with the same priority, they will be executed in
the order that they registered. It is also possible to register callacks 
with priority 0 and 11, but only one of each. The third parameter is a 
C<MT::Plugin> object reference that is associated with the handler (this 
parameter is optional). The fourth parameter is a code reference that is 
invoked to handle the callback. For example:

    MT->add_callback('BuildFile', 1, undef, \&rebuild_file_hdlr);

The code reference should expect to receive an object of type
L<MT::Callback> as its first argument. This object is used to
communicate errors to the caller:

    sub rebuild_file_hdlr {
        my ($cb, ...) = @_;
        if (something bad happens) {
            return $cb->error("Something bad happened!");
        }
    }

Other parameters to the callback function depend on the callback point.

The treatment of the error string depends on the callback point.
Typically, either it is ignored or the user's action fails and the
error message is displayed.

The value returned from this method is the new L<MT::Callback> object.

=head2 MT->remove_callback($callback)

Removes a callback that was previously registered.

=head2 MT->register_callbacks([...])

Registers several callbacks simultaneously. Each element in the array
parameter given should be a hashref containing these elements: C<name>, 
C<priority>, C<plugin> and C<code>.

=head2 MT->run_callbacks($meth[, $arg1, $arg2, ...])

Invokes a particular callback, running any associated callback handlers.

The first parameter is the name of the callback to execute. This is one
of the global callback methods (see L<Callbacks> section) or can be
a class-specific method that includes the package name associated with
the callback.

The remaining arguments are passed through to any callback handlers that
are invoked.

For "Filter"-type callbacks, this routine will return a 0 if any of the
handlers return a false result. If all handlers return a true result,
a value of 1 is returned.

Example:

    MT->run_callbacks('MyClass::frobnitzes', \@whirlygigs);

Which would execute any handlers that registered in this fashion:

    MT->add_callback('MyClass::frobnitzes', 4, $plugin, \&frobnitz_hdlr);

=head2 MT->run_callback($cb[, $arg1, $arg2, ...])

An internal routine used by C<run_callbacks> to invoke a single
L<MT::Callback>.

=head2 callback_error($str)

This routine is used internally by C<MT::Callback> to set any error response
that comes from invoking a callback.

=head2 callback_errstr

This internal routine returns the error response stored using the
C<callback_error> routine.

=head2 MT->handler_to_coderef($handler[, $delayed])

Translates a registry handler signature into a Perl coderef. Handlers
are in one of the following forms:

    $<COMPONENTID>::<PERL_PACKAGE>::<SUBROUTINE>

    <PERL_PACKAGE>::<SUBROUTINE>

    <PERL_PACKAGE>-><SUBROUTINE>

    sub { ... }

When invoked with a '-E<gt>' operator, the subroutine is invoked as
a package method.

When the handler is a string that starts with 'sub {', it is eval'd
to compile it, and the resulting coderef is returned.

The coderef that is returned can be passed any parameters you wish.

When the coderef is invoked, any component that was identified in
the handler signature becomes the active component when running the
code (this affects how strings are translated, and the search paths
for templates that are loaded).

If the C<$delayed> parameter is given, a special coderef is constructed
that will delay the 'require' of the identified Perl package until
the coderef is actually invoked.

=head2 MT->registry( @path )

Queries the Movable Type registry data structure for a given resource
path. The MT registry is a collection of hash structures that contain
resources MT and/or plugins can utilize.

When this method is invoked, it actually issues a registry request
against each component registered with MT, then merges the resulting
hashes and returns them. See L<MT::Component> for further details.

=head2 MT->component( $id )

Returns a loaded L<MT::Component> based on the requested C<$id> parameter.
For example:

    # Returns the MT 'core' component
    MT->component('core');

=head2 MT->model( $id )

Returns a Perl package name for the database-backed MT object type 
identified by C<$id>. For example:

    # Assigns (by default) 'MT::Blog' to $blog_class
    my $blog_class = MT->model('blog');

It is a recommended practice to utilize the model method to derive the
implementation package name, instead of hardcoding Perl package names.

A list of names to be used with this function can be found in the 
MT::Core module, but also plugins can add more names.

=head2 MT->models( $id )

Returns a list of object types that are registered as sub-types. For
instance, the MT 'asset' object type has several sub-types associated
with it:

    my @types = MT->models('asset');
    # @types now contains ('asset', 'asset.image', 'asset.video', etc.)

=head2 MT->product_code

The product code identifying the Movable Type product that is installed.
This is either 'MTE' for Movable Type Enterprise version or 'MT' for the
community version.

=head2 MT->product_name

The name of the Movable Type product that is installed. This is either
'Movable Type Advanced' or 'Movable Type Publishing Platform'.

=head2 MT->product_version

The version number of the product. This is different from the C<version_id>
and C<version_number> methods as they report the API version information.

=head2 MT->VERSION

Returns the API version of MT. 

Used internally to load suitable API 'compatibility' module, if a plugin 
declares:

    use MT 4;

in which case will attempt to load a module named "MT::Compat::v4". 
This module would contain compatibility support for MT 4-based plugins.

=head2 MT->version_id

Returns the API version of MT (including any beta/alpha designations).

=head2 MT->version_number

Returns the numeric API version of MT (without any beta/alpha designations).
For example, if I<version_id> returned C<2.5b1>, I<version_number> would
return C<2.5>.

=head2 MT->schema_version

Returns the version of the MT database schema.

=head2 $mt->id

Provides an identifier for the application, one that relates to the
'application' paths of the MT registry. This method may be overridden
for any subclass of MT to provide the appropriate identifier. By
default, the base 'id' method will return an id taken from the
Perl package name, by stripping off any 'MT::App::' prefix, and lowercasing
the remaining string.

For example: MT::App::CMS => cms; Foo::Bar => foo/bar

=head2 MT->version_slug

Returns a string of text that is appended to emails sent through the
C<build_email> method.

=head2 $mt->publisher

Returns the L<MT::WeblogPublisher> object that is used for managing the
MT publishing process. See L<MT::WeblogPublisher> for more information.

=head2 $mt->rebuild

An alias to L<MT::WeblogPublisher::rebuild>. See L<MT::WeblogPublisher>
for documentation of this method.

=head2 $mt->rebuild_entry

An alias to L<MT::WeblogPublisher::rebuild_entry>. See L<MT::WeblogPublisher>
for documentation of this method.

=head2 $mt->rebuild_indexes

An alias to L<MT::WeblogPublisher::rebuild_indexes>. See
L<MT::WeblogPublisher> for documentation of this method.

=head2 $mt->rebuild_archives

An alias to L<MT::WeblogPublisher::rebuild_archives>. See
L<MT::WeblogPublisher> for documentation of this method.

=head2 $app->template_paths

Returns an array of directory paths where application templates exist.

=head2 $app->find_file(\@paths, $filename)

Returns the path and filename for a file found in any of the given paths.
If the file cannot be found, it returns undef.

=head2 $app->load_tmpl($tmpl_source[, @params][, $hashref])

Loads a L<MT::Template> template using the either a filename as tmpl_source, 
or a reference to a string. See the documentation for the C<build_page> 
method to learn about how templates are located. The optional C<@params> 
are passed to the L<MT::Template> constructor. The optional C<$hashref> is added 
to the template's context.

=head2 $app->load_global_tmpl($args[, $blog_id])

Loads a L<MT::Template> from the database. is $args is hashref, it is used
as filtering terms. otherwise, it is used as template type.

template is searched for the supplied blog_id and the global templates.
if $blog_id is not supplied, it will try to retrive it from the application.

=head2 $app->set_default_tmpl_params($tmpl)

Assigns standard parameters to the given L<MT::Template> C<$tmpl> object.
Refer to the L<STANDARD APPLICATION TEMPLATE PARAMETERS> section for a
complete list of these parameters.

=head2 $app->charset( [$charset] )

Gets or sets the application's character set based on the "PublishCharset"
configuration setting or the encoding of the active language
(C<$app-E<gt>current_language>).

=head2 $app->build_page($tmpl_name, \%param)

Builds an application page to be sent to the client; the page name is specified
in C<$tmpl_name>, which should be the name of a template containing valid
L<MT::Template> markup. C<\%param> is a hash ref whose keys and values will
be passed to L<MT::Template::param> for use in the template.

On success, returns a scalar containing the page to be sent to the client. On
failure, returns C<undef>, and the error message can be obtained from
C<$app-E<gt>errstr>.

=head3 How does build_page find a template?

The C<build_page> function looks in several places for an app
template. Two configuration directives can modify these search paths,
and application and plugin code can also affect them.

The I<TemplatePath> config directive is an absolute path to the directory
where MT's core application templates live. It defaults to the I<mt_dir>
plus an additional path segment of 'tmpl'.

The optional I<AltTemplatePath> config directive is a path (absolute
or relative) to a directory where some 'override templates' may
live. An override template takes the place of one of MT's core
application templates, and is used interchangeably with the core
template. This allows power users to customize the look and feel of
the MT application. If I<AltTemplatePath> is relative, its base path
is the value of the Movable Type configuration file.

Next, any application built on the C<MT::App> foundation can define
its own I<template_dir> parameter, which identifies a subdirectory of
TemplatePath (or AltTemplatePath) where that application's templates
can be found. I<template_dir> defaults to C<cms>. Most templates will
be found in this directory, but sometimes the template search will
fall through to the parent directory, where a default error template
is found, for example. I<template_dir> should rightly have been named
I<application_template_dir>, since it is application-specific.

Finally, a plugin can specify its I<plugin_template_path>, which
locates a directory where the templates for that plugin's own
interface are found. If the I<plugin_template_path> is relative, it
may be relative to either the I<app_dir>, or the I<mt_dir>; the former
takes precedence if it exists. (for a definition of I<app_dir> and
I<mt_dir>, see L<MT>)

Given these values, the order of search is as follows:

=over 4

=item * I<plugin_template_path>

=item * I<AltTemplatePath>

=item * I<AltTemplatePath>F</>I<template_dir>

=item * I<TemplatePath>/I<template_dir>

=item * I<TemplatePath>

=back

If a template with the given name is not found in any of these
locations, an ugly error is thrown to the user.

=head2 $app->build_page_in_mem($tmpl, \%param)

Used internally by the L<build_page> method to render the output
of a L<MT::Template> object (the first parameter) using the parameter
data (the second parameter). It additionally calls the L<process_mt_template>
method (to process any E<lt>__actionE<gt> tags)
and then L<translate_templatized> (to process any E<lt>__transE<gt> tags).

=head2 $app->process_mt_template($str)

Processes the E<lt>__action<gt> tags that are present in C<$str>. These tags
are in the following format:

    <__action mode="mode_name" parameter="value">

The mode parameter is required (and must be the first attribute). The
following attributes are appended as regular query parameters.

The MT_ACTION tag is a preferred way to specify application links rather
than using this syntax:

    <mt:var name="script_url">?__mode=mode_name&parameter=value

C<process_mt_templates> also strips the C<$str> variable of any tags in
the format of C<E<lt>MT_\w+:\w+E<gt>>. These are 'marker' tags that are
used to identify specific portions of the template page and used in
conjunction with the transformer callback helper methods C<tmpl_prepend>,
C<tmpl_append>, C<tmpl_replace>, C<tmpl_select>.

=head2 $mt->build_email($file, $param)

Loads a template from the application's 'email' template directory and
processes it as a MT::Template. The C<$param> argument is a hash reference
of parameter data for the template. The return value is the output of the
template.

=head2 MT::get_next_sched_post_for_user($author_id, @blog_ids)

This is an internal routine used by L<MT::XMLRPCServer> and the
getNextScheduled XMLRPC method to determine the timestamp for the next
entry that is scheduled for publishing. The return value is the timestamp
in UTC time in the format "YYYY-MM-DDTHH:MM:SSZ".

=head2 $mt->commenter_authenticator($id)

Returns a specific comment authenication option using the identifier
C<$id> parameter.

=head2 $mt->commenter_authenticators()

Returns the available comment authentication identifiers that are
installed in the MT registry.

=head2 $mt->core_commenter_authenticators()

A method that returns the MT-supplied comment authentication registry
data.

=head2 $mt->init_commenter_authenticators()

Initializes the list of installed MT comment authentication options,
drawing from the MT registry.

=head2 $mt->captcha_provider($id)

Returns a specific CAPTCHA provider configuration using the identifier
C<$id> parameter. This is a convenience method that accesses the CAPTCHA
providers installed into the MT registry.

=head2 $mt->captcha_providers()

Returns the available CAPTCHA providers. This is a convenience method
that accesses the MT registry for available CAPTCHA providers (it also
invokes the 'condition' key for each provider to filter the list).

=head2 $mt->core_captcha_providers()

A method that returns the MT-supplied CAPTCHA provider registry data.

=head2 $mt->init_captcha_providers()

Initializes the list of installed CAPTCHA providers, drawing from
the MT registry.

=head2 $mt->effective_captcha_provider($id)

Returns the Perl package name for the specified CAPTCHA provider.
The condition for this provider is checked and the Perl package is loaded.
On problem with this provider, returns undef.

=head2 $app->static_path()

Returns the application's static web path.

=head2 $app->static_file_path()

Returns the application's static file path.

=head2 MT::core_upload_file_to_sync

A MT callback handler routine that forwards to the L<upload_file_to_sync>
method.

=head2 MT->upload_file_to_sync(%param)

A routine that will make record of a file that is to be transmitted
to one or more servers (typically via rsync). This method runs when
the C<SyncTarget> MT configuration setting is configured. Normally
published files are automatically processed for syncing operations,
but this routine is used for files that are created through other
means, such as uploading an asset.

=head2 MT->help_url( [ $suffix ] )

Returns a help URL for the application. This method is used to construct
the URL directing users to online documentation. If called without any
parameters, it returns the base URL for providing help. If a parameter is
given, the URL is appended with the given subpath. The base URL by default
is 'http://www.movabletype.org/documentation/'. This string is passed
through MT's localization modules, so it can be changed on a per-language
basis. The C<$suffix> parameter, however, is always appended to this base URL.

=head2 MT->portal_url

Returns the home page of this MT installation.

=head2 $app->support_directory_url

Returns the support page of this MT application. configurable using the
SupportDirectoryURL configuration directive, otherwise default to
static_file_url/support/

=head2 $app->support_directory_path

Returns the location of the support directory for this app on the file
system. configurable using the SupportDirectoryPath configuration directive,
otherwise default to static_file_path/support/

=head2 MT->get_timer

Returns an instance of L<MT::Util::ReqTimer> for use in timing MT's
operations.

=head2 MT->log_times

Used as part of Movable Type's performance logging framework. This method
is called internally, once at the startup of Movable Type, and once as it
is shutting down.

=head2 MT->time_this($string, $code)

Utility method to time a particular routine. This will log the execution
time of the C<$code> coderef with the identifying phrase C<$string> using
MT's performance logging framework.

=head2 MT::refresh_cache($cb)

A callback handler that invalidates the cache of MT's caching driver.
See L<MT::Cache::Negotiate>.

=head2 MT->register_refresh_cache_event($callback)

Registers a callback that will cause the MT cache to invalidate itself.
See L<MT::Cache::Negotiate>.

=head1 ERROR HANDLING

On an error, all of the above methods return C<undef>, and the error message
can be obtained by calling the method I<errstr> on the class or the object
(depending on whether the method called was a class method or an instance
method).

For example, called on a class name:

    my $mt = MT->new or die MT->errstr;

Or, called on an object:

    $mt->rebuild(BlogID => $blog_id)
        or die $mt->errstr;

=head1 DEBUGGING

MT has a package variable C<$MT::DebugMode> which is assigned through
your MT configuration file (DebugMode setting). If this is set to
any non-zero value, MT applications will display any C<warn>'d
statements to a panel that is displayed within the app.

The DebugMode is a bit-wise setting and offers the following options:

    1 - Display debug messages
    2 - Display a stack trace for messages captured
    4 - Lists queries issued by Data::ObjectDriver
    8 - Reports on MT templates that take more than 1/4 second to build*
    128 - Outputs app-level request/response information to STDERR.

These can be combined, so if you want to display queries and debug messages,
use a DebugMode of 5 for instance.

You may also use the local statement to temporarily apply a particular bit,
if you want to scope the debug messages you receive to a block of code:

    local $MT::DebugMode |= 4;  # show me the queries for the following
    my $obj = MT::Entry->load({....});

*DebugMode bit 8 actually outputs it's messages to STDERR (which typically
is sent to your web server's error log).

=head1 CALLBACKS

Movable Type has a variety of hook points at which a plugin can attach
a callback.

In each case, the first parameter is an L<MT::Callback> object which
can be used to pass error information back to the caller.

The app-level callbacks related to rebuilding are documented
in L<MT::WeblogPublisher>. The specific apps document the callbacks
which they invoke.

=head2 NewUserProvisioning($cb, $user)

This callback is invoked when a user is being added to Movable Type.
Movable Type itself registers for this callback (with a priority of 5)
to provision the user with a new weblog if the system has been configured
to do so.

=head2 post_init($cb, \%param)

This callback is invoked when MT is initialized and ready to run.
This callback is invoked after MT initialized addons, plugins, schema
and permissions.  The arguments passed to initialize MT is passed
through to the callback.

=head1 LICENSE

The license that applies is the one you agreed to when downloading
Movable Type.

=head1 AUTHOR & COPYRIGHT

Except where otherwise noted, MT is Copyright 2001-2012 Six Apart.
All rights reserved.

=cut