summaryrefslogtreecommitdiff
path: root/modules/system/system.install
blob: 310bc06619590068e9e7243328295b46720430a1 (plain)
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
<?php
// $Id$

/**
 * Test and report Drupal installation requirements.
 *
 * @param $phase
 *   The current system installation phase.
 * @return
 *   An array of system requirements.
 */
function system_requirements($phase) {
  $requirements = array();
  // Ensure translations don't break at install time
  $t = get_t();

  // Report Drupal version
  if ($phase == 'runtime') {
    $requirements['drupal'] = array(
      'title' => $t('Drupal'),
      'value' => VERSION,
      'severity' => REQUIREMENT_INFO,
      'weight' => -10,
    );
  }

  // Test web server
  $software = $_SERVER['SERVER_SOFTWARE'];
  $requirements['webserver'] = array(
    'title' => $t('Web server'),
    'value' => $software,
  );
  // Use server info string, if present.
  if ($software && preg_match('![0-9]!', $software)) {
    list($server, $version) = split('[ /]', $software);
    switch ($server) {
      case 'Apache':
        if (version_compare($version, DRUPAL_MINIMUM_APACHE) < 0) {
          $requirements['webserver']['description'] = $t('Your Apache server is too old. Drupal requires at least Apache %version.', array('%version' => DRUPAL_MINIMUM_APACHE));
          $requirements['webserver']['severity'] = REQUIREMENT_ERROR;
        }
        break;

      default:
        $requirements['webserver']['description'] = $t('The web server you\'re using has not been tested with Drupal and might not work properly.');
        $requirements['webserver']['severity'] = REQUIREMENT_WARNING;
        break;
    }
  }
  else {
    $requirements['webserver']['value'] = $software ? $software : $t('Unknown');
    $requirements['webserver']['description'] = $t('Unable to determine your web server type and version. Drupal might not work properly.');
    $requirements['webserver']['severity'] = REQUIREMENT_WARNING;
  }

  // Test PHP version
  $requirements['php'] = array(
    'title' => $t('PHP'),
    'value' => ($phase == 'runtime') ? l(phpversion(), 'admin/reports/status/php') : phpversion(),
  );
  if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) {
    $requirements['php']['description'] = $t('Your PHP installation is too old. Drupal requires at least PHP %version.', array('%version' => DRUPAL_MINIMUM_PHP));
    $requirements['php']['severity'] = REQUIREMENT_ERROR;
  }

  // Test DB version
  global $db_type;
  if (function_exists('db_status_report')) {
    $requirements += db_status_report($phase);
  }

  // Test settings.php file writability
  if ($phase == 'runtime') {
    $conf_dir = drupal_verify_install_file(conf_path(), FILE_NOT_WRITABLE, 'dir');
    $conf_file = drupal_verify_install_file(conf_path() .'/settings.php', FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE);
    if (!$conf_dir || !$conf_file) {
      $requirements['settings.php'] = array(
        'value' => $t('Not protected'),
        'severity' => REQUIREMENT_ERROR,
        'description' => '',
      );
      if (!$conf_dir) {
        $requirements['settings.php']['description'] .= $t('The directory %file is not protected from modifications and poses a security risk. You must change the directory\'s permissions to be non-writable. ', array('%file' => conf_path()));
      }
      if (!$conf_file) {
        $requirements['settings.php']['description'] .= $t('The file %file is not protected from modifications and poses a security risk. You must change the file\'s permissions to be non-writable.', array('%file' => conf_path() .'/settings.php'));
      }
    }
    else {
      $requirements['settings.php'] = array(
        'value' => $t('Protected'),
      );
    }
    $requirements['settings.php']['title'] = $t('Configuration file');
  }

  // Report cron status.
  if ($phase == 'runtime') {
    // Cron warning threshold defaults to two days.
    $threshold_warning = variable_get('cron_threshold_warning', 172800);
    // Cron error threshold defaults to two weeks.
    $threshold_error = variable_get('cron_threshold_error', 1209600);
    // Cron configuration help text.
    $help = $t('For more information, see the online handbook entry for <a href="@cron-handbook">configuring cron jobs</a>.', array('@cron-handbook' => 'http://drupal.org/cron'));

    // Determine when cron last ran. If never, use the install time to
    // determine the warning or error status.
    $cron_last = variable_get('cron_last', NULL);
    $never_run = FALSE;
    if (!is_numeric($cron_last)) {
      $never_run = TRUE;
      $cron_last = variable_get('install_time', 0);
    }

    // Determine severity based on time since cron last ran.
    $severity = REQUIREMENT_OK;
    if (time() - $cron_last > $threshold_error) {
      $severity = REQUIREMENT_ERROR;
    }
    else if ($never_run || (time() - $cron_last > $threshold_warning)) {
      $severity = REQUIREMENT_WARNING;
    }

    // If cron hasn't been run, and the user is viewing the main
    // administration page, instead of an error, we display a helpful reminder
    // to configure cron jobs.
    if ($never_run && $severity != REQUIREMENT_ERROR && $_GET['q'] == 'admin') {
      drupal_set_message($t('Cron has not run. Please visit the <a href="@status">status report</a> for more information.', array('@status' => url('admin/reports/status'))));
    }

    // Set summary and description based on values determined above.
    if ($never_run) {
      $summary = $t('Never run');
      $description = $t('Cron has not run.') .' '. $help;
    }
    else {
      $summary = $t('Last run !time ago', array('!time' => format_interval(time() - $cron_last)));
      $description = '';
      if ($severity != REQUIREMENT_OK) {
        $description = $t('Cron has not run recently.') .' '. $help;
      }
    }

    $requirements['cron'] = array(
      'title' => $t('Cron maintenance tasks'),
      'severity' => $severity,
      'value' => $summary,
      'description' => $description .' '. $t('You can <a href="@cron">run cron manually</a>.', array('@cron' => url('admin/reports/status/run-cron'))),
    );
  }

  // Test files directory
  $directory = file_directory_path();
  $is_writable = is_writable($directory);
  $is_directory = is_dir($directory);
  if (!$is_writable || !$is_directory) {
    if (!$is_directory) {
      $error = $t('The directory %directory does not exist.', array('%directory' => $directory));
    }
    else {
      $error = $t('The directory %directory is not writable.', array('%directory' => $directory));
    }
    $requirements['file system'] = array(
      'value' => $t('Not writable'),
      'severity' => REQUIREMENT_ERROR,
    );
    if ($phase == 'runtime') {
      $requirements['file system']['description'] = $error .' '. $t('You may need to set the correct directory at the <a href="@admin-file-system">file system settings page</a> or change the current directory\'s permissions so that it is writable.', array('@admin-file-system' => url('admin/settings/file-system')));
    }
    else if ($phase == 'install') {
      // For the installer UI, we need different wording. 'value' will
      // be treated as version, so provide none there.
      $requirements['file system']['description'] = $error .' '. $t('To proceed with the installation, please ensure that the files directory exists and is writable by the installer. If you are unsure how to create this directory and modify its permissions, please consult the <a href="@handbook_url">on-line handbook</a> or INSTALL.txt.', array('%directory' => $directory, '@handbook_url' => 'http://drupal.org/server-permissions'));
      $requirements['file system']['value'] = '';
    }
  }
  else {
    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
      $requirements['file system']['value'] = $t('Writable (<em>public</em> download method)');
    }
    else {
      $requirements['file system']['value'] = $t('Writable (<em>private</em> download method)');
    }
  }
  $requirements['file system']['title'] = $t('File system');

  // See if updates are available in update.php.
  if ($phase == 'runtime') {
    $requirements['update'] = array(
      'title' => $t('Database updates'),
      'severity' => REQUIREMENT_OK,
      'value' => $t('Up to date'),
    );

    // Check installed modules.
    foreach (module_list() as $module) {
      $updates = drupal_get_schema_versions($module);
      if ($updates !== FALSE) {
        $default = drupal_get_installed_schema_version($module);
        if (max($updates) > $default) {
          $requirements['update']['severity'] = REQUIREMENT_ERROR;
          $requirements['update']['value'] = $t('Out of date');
          $requirements['update']['description'] = $t('Some modules have database schema updates to install. You should run the <a href="@update">database update script</a> immediately.', array('@update' => base_path() .'update.php'));
          break;
        }
      }
    }
  }

  // Verify the update.php access setting
  if ($phase == 'runtime') {
    if (!empty($GLOBALS['update_free_access'])) {
      $requirements['update access'] = array(
        'value' => $t('Not protected'),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('The update.php script is accessible to everyone without authentication check, which is a security risk. You must change the $update_free_access value in your settings.php back to FALSE.'),
      );
    }
    else {
      $requirements['update access'] = array(
        'value' => $t('Protected'),
      );
    }
    $requirements['update access']['title'] = $t('Access to update.php');
  }

  // Test Unicode library
  include_once './includes/unicode.inc';
  $requirements = array_merge($requirements, unicode_requirements());

  // Check for update status module.
  if ($phase == 'runtime') {
    if (!module_exists('update')) {
      $requirements['update status'] = array(
        'value' => $t('Not enabled'),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('Update notifications are not enabled. It is <strong>highly recommended</strong> that you enable the update status module from the <a href="@module">module administration page</a> in order to stay up-to-date on new releases. For more information please read the <a href="@update">Update status handbook page</a>.', array('@update' => 'http://drupal.org/handbook/modules/update', '@module' => url('admin/build/modules'))),
      );
    }
    else {
      $requirements['update status'] = array(
        'value' => $t('Enabled'),
      );
      if (variable_get('drupal_http_request_fails', FALSE)) {
        $requirements['http requests'] = array(
          'title' => $t('HTTP request status'),
          'value' => $t('Fails'),
          'severity' => REQUIREMENT_ERROR,
          'description' => $t('Your system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.'),
        );
      }
    }
    $requirements['update status']['title'] = $t('Update notifications');
  }

  return $requirements;
}

/**
 * Implementation of hook_install().
 */
function system_install() {
  if ($GLOBALS['db_type'] == 'pgsql') {
    /* create unsigned types */
    db_query("CREATE DOMAIN int_unsigned integer CHECK (VALUE >= 0)");
    db_query("CREATE DOMAIN smallint_unsigned smallint CHECK (VALUE >= 0)");
    db_query("CREATE DOMAIN bigint_unsigned bigint CHECK (VALUE >= 0)");

    /* create functions */
    db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric) RETURNS numeric AS
      \'SELECT CASE WHEN (($1 > $2) OR ($2 IS NULL)) THEN $1 ELSE $2 END;\'
      LANGUAGE \'sql\''
    );
    db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric, numeric) RETURNS numeric AS
      \'SELECT greatest($1, greatest($2, $3));\'
      LANGUAGE \'sql\''
    );
    if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'"))) {
      db_query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
        \'SELECT random();\'
        LANGUAGE \'sql\''
      );
    }

    if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'concat'"))) {
      db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
        \'SELECT $1 || $2;\'
        LANGUAGE \'sql\''
      );
    }
    db_query('CREATE OR REPLACE FUNCTION "if"(boolean, text, text) RETURNS text AS
      \'SELECT CASE WHEN $1 THEN $2 ELSE $3 END;\'
      LANGUAGE \'sql\''
    );
    db_query('CREATE OR REPLACE FUNCTION "if"(boolean, integer, integer) RETURNS integer AS
      \'SELECT CASE WHEN $1 THEN $2 ELSE $3 END;\'
      LANGUAGE \'sql\''
    );
  }

  // Create tables.
  $modules = array('system', 'filter', 'block', 'user', 'node', 'comment', 'taxonomy');
  foreach ($modules as $module) {
    drupal_install_schema($module);
  }

  // Load system theme data appropriately.
  system_theme_data();

  // Inserting uid 0 here confuses MySQL -- the next user might be created as
  // uid 2 which is not what we want. So we insert the first user here, the
  // anonymous user. uid is 1 here for now, but very soon it will be changed
  // to 0.
  db_query("INSERT INTO {users} (name, mail) VALUES('%s', '%s')", '', '');
  // We need some placeholders here as name and mail are uniques and data is
  // presumed to be a serialized array. Install will change uid 1 immediately
  // anyways. So we insert the superuser here, the uid is 2 here for now, but
  // very soon it will be changed to 1.
  db_query("INSERT INTO {users} (name, mail, created, data) VALUES('%s', '%s', %d, '%s')", 'placeholder-for-uid-1', 'placeholder-for-uid-1', time(), serialize(array()));
  // This sets the above two users uid 0 (anonymous). We avoid an explicit 0
  // otherwise MySQL might insert the next auto_increment value.
  db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
  // This sets uid 1 (superuser). We skip uid 2 but that's not a big problem.
  db_query("UPDATE {users} SET uid = 1 WHERE name = '%s'", 'placeholder-for-uid-1');

  db_query("INSERT INTO {role} (name) VALUES ('%s')", 'anonymous user');
  db_query("INSERT INTO {role} (name) VALUES ('%s')", 'authenticated user');

  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 1, 'access content', 0);
  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 2, 'access comments, access content, post comments, post comments without approval', 0);

  db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'theme_default', 's:7:"garland";');
  db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name = '%s'", 1, 'theme', 'garland');
  db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, pages) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s')", 'user', '0', 'garland', 1, 0, 'left', '');
  db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, pages) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s')", 'user', '1', 'garland', 1, 0, 'left', '');
  db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, pages) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s')", 'system', '0', 'garland', 1, 10, 'footer', '');

  db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", 0, 0, 'all', 1, 0, 0);

  // Add input formats.
  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('%s', '%s', %d)", 'Filtered HTML', ',1,2,', 1);
  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('%s', '%s', %d)", 'Full HTML', '', 1);

  // Enable filters for each input format.

  // Filtered HTML:
  // URL filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 2, 0);
  // HTML filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 0, 1);
  // Line break filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 1, 2);
  // HTML corrector filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 3, 10);

  // Full HTML:
  // URL filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 2, 'filter', 2, 0);
  // Line break filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 2, 'filter', 1, 1);
  // HTML corrector filter.
  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 2, 'filter', 3, 10);

  db_query("INSERT INTO {variable} (name, value) VALUES ('%s','%s')", 'filter_html_1', 'i:1;');

  db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'node_options_forum', 'a:1:{i:0;s:6:"status";}');
}

/**
 * Implementation of hook_schema().
 */
function system_schema() {
  // NOTE: {variable} needs to be created before all other tables, as
  // some database drivers, e.g. Oracle and DB2, will require variable_get()
  // and variable_set() for overcoming some database specific limitations.
  $schema['variable'] = array(
    'description' => t('Named variable/value pairs created by Drupal core or any other module or theme. All variables are cached in memory at the start of every Drupal request so developers should not be careless about what is stored here.'),
    'fields' => array(
      'name' => array(
        'description' => t('The name of the variable.'),
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''),
      'value' => array(
        'description' => t('The value of the variable.'),
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big'),
      ),
    'primary key' => array('name'),
    );

  $schema['actions'] = array(
    'description' => t('Stores action information.'),
    'fields' => array(
      'aid' => array(
        'description' => t('Primary Key: Unique actions ID.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '0'),
      'type' => array(
        'description' => t('The object that that action acts on (node, user, comment, system or custom types.)'),
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''),
      'callback' => array(
        'description' => t('The callback function that executes when the action runs.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'parameters' => array(
        'description' => t('Parameters to be passed to the callback function.'),
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big'),
      'description' => array(
        'description' => t('Description of the action.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '0'),
      ),
    'primary key' => array('aid'),
    );

  $schema['actions_aid'] = array(
    'description' => t('Stores action IDs for non-default actions.'),
    'fields' => array(
      'aid' => array(
        'description' => t('Primary Key: Unique actions ID.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      ),
    'primary key' => array('aid'),
    );

  $schema['batch'] = array(
    'description' => t('Stores details about batches (processes that run in multiple HTTP requests).'),
    'fields' => array(
      'bid' => array(
        'description' => t('Primary Key: Unique batch ID.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'token' => array(
        'description' => t("A string token generated against the current user's session id and the batch id, used to ensure that only the user who submitted the batch can effectively access it."),
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE),
      'timestamp' => array(
        'description' => t('A Unix timestamp indicating when this batch was submitted for processing. Stale batches are purged at cron time.'),
        'type' => 'int',
        'not null' => TRUE),
      'batch' => array(
        'description' => t('A serialized array containing the processing data for the batch.'),
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big')
      ),
    'primary key' => array('bid'),
    'indexes' => array('token' => array('token')),
    );

  $schema['cache'] = array(
    'description' => t('Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.'),
    'fields' => array(
      'cid' => array(
        'description' => t('Primary Key: Unique cache ID.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'data' => array(
        'description' => t('A collection of data to cache.'),
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big'),
      'expire' => array(
        'description' => t('A Unix timestamp indicating when the cache entry should expire, or 0 for never.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'created' => array(
        'description' => t('A Unix timestamp indicating when the cache entry was created.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'headers' => array(
        'description' => t('Any custom HTTP headers to be added to cached data.'),
        'type' => 'text',
        'not null' => FALSE),
      'serialized' => array(
        'description' => t('A flag to indicate whether content is serialized (1) or not (0).'),
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0)
      ),
    'indexes' => array('expire' => array('expire')),
    'primary key' => array('cid'),
    );

  $schema['cache_form'] = $schema['cache'];
  $schema['cache_form']['description'] = t('Cache table for the form system to store recently built forms and their storage data, to be used in subsequent page requests.');
  $schema['cache_page'] = $schema['cache'];
  $schema['cache_page']['description'] = t('Cache table used to store compressed pages for anonymous users, if page caching is enabled.');
  $schema['cache_menu'] = $schema['cache'];
  $schema['cache_menu']['description'] = t('Cache table for the menu system to store router information as well as generated link trees for various menu/page/user combinations.');

  $schema['files'] = array(
    'description' => t('Stores information for uploaded files.'),
    'fields' => array(
      'fid' => array(
        'description' => t('Primary Key: Unique files ID.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'uid' => array(
        'description' => t('The {users}.uid of the user who is associated with the file.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'filename' => array(
        'description' => t('Name of the file.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'filepath' => array(
        'description' => t('Path of the file relative to Drupal root.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'filemime' => array(
        'description' => t('The file MIME type.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'filesize' => array(
        'description' => t('The size of the file in bytes.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'status' => array(
        'description' => t('A flag indicating whether file is temporary (1) or permanent (0).'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'timestamp' => array(
        'description' => t('UNIX timestamp for when the file was added.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      ),
    'indexes' => array(
      'uid' => array('uid'),
      'status' => array('status'),
      'timestamp' => array('timestamp'),
      ),
    'primary key' => array('fid'),
    );

  $schema['flood'] = array(
    'description' => t('Flood controls the threshold of events, such as the number of contact attempts.'),
    'fields' => array(
      'fid' => array(
        'description' => t('Unique flood event ID.'),
        'type' => 'serial',
        'not null' => TRUE),
      'event' => array(
        'description' => t('Name of event (e.g. contact).'),
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => ''),
      'hostname' => array(
        'description' => t('Hostname of the visitor.'),
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''),
      'timestamp' => array(
        'description' => t('Timestamp of the event.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0)
      ),
    'primary key' => array('fid'),
    'indexes' => array(
      'allow' => array('event', 'hostname', 'timestamp'),
    ),
    );

  $schema['history'] = array(
    'description' => t('A record of which {users} have read which {node}s.'),
    'fields' => array(
      'uid' => array(
        'description' => t('The {users}.uid that read the {node} nid.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'nid' => array(
        'description' => t('The {node}.nid that was read.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'timestamp' => array(
        'description' => t('The Unix timestamp at which the read occurred.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0)
      ),
    'primary key' => array('uid', 'nid'),
    'indexes' => array(
      'nid' => array('nid'),
    ),
    );
  $schema['menu_router'] = array(
    'description' => t('Maps paths to various callbacks (access, page and title)'),
    'fields' => array(
      'path' => array(
        'description' => t('Primary Key: the Drupal path this entry describes'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'load_functions' => array(
        'description' => t('A serialized array of function names (like node_load) to be called to load an object corresponding to a part of the current path.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'to_arg_functions' => array(
        'description' => t('A serialized array of function names (like user_current_to_arg) to be called to replace a part of the router path with another string.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'access_callback' => array(
        'description' => t('The callback which determines the access to this router path. Defaults to user_access.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'access_arguments' => array(
        'description' => t('A serialized array of arguments for the access callback.'),
        'type' => 'text',
        'not null' => FALSE),
      'page_callback' => array(
        'description' => t('The name of the function that renders the page.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'page_arguments' => array(
        'description' => t('A serialized array of arguments for the page callback.'),
        'type' => 'text',
        'not null' => FALSE),
      'fit' => array(
        'description' => t('A numeric representation of how specific the path is.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'number_parts' => array(
        'description' => t('Number of parts in this router path.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'tab_parent' => array(
        'description' => t('Only for local tasks (tabs) - the router path of the parent page (which may also be a local task).'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'tab_root' => array(
        'description' => t('Router path of the closest non-tab parent page. For pages that are not local tasks, this will be the same as the path.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'title' => array(
        'description' => t('The title for the current page, or the title for the tab if this is a local task.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'title_callback' => array(
        'description' => t('A function which will alter the title. Defaults to t()'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'title_arguments' => array(
        'description' => t('A serialized array of arguments for the title callback. If empty, the title will be used as the sole argument for the title callback.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'type' => array(
        'description' => t('Numeric representation of the type of the menu item, like MENU_LOCAL_TASK.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'block_callback' => array(
        'description' => t('Name of a function used to render the block on the system administration page for this item.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'description' => array(
        'description' => t('A description of this item.'),
        'type' => 'text',
        'not null' => TRUE),
      'position' => array(
        'description' => t('The position of the block (left or right) on the system administration page for this item.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'weight' => array(
        'description' => t('Weight of the element. Lighter weights are higher up, heavier weights go down.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'file' => array(
        'description' => t('The file to include for this element, usually the page callback function lives in this file.'),
        'type' => 'text',
        'size' => 'medium')
      ),
    'indexes' => array(
      'fit' => array('fit'),
      'tab_parent' => array('tab_parent')
      ),
    'primary key' => array('path'),
    );

  $schema['menu_links'] = array(
    'description' => t('Contains the individual links within a menu.'),
    'fields' => array(
     'menu_name' => array(
        'description' => t("The menu name. All links with the same menu name (such as 'navigation') are part of the same menu."),
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''),
      'mlid' => array(
        'description' => t('The menu link ID (mlid) is the integer primary key.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'plid' => array(
        'description' => t('The parent link ID (plid) is the mlid of the link above in the hierarchy, or zero if the link is at the top level in its menu.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'link_path' => array(
        'description' => t('The Drupal path or external path this link points to.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'router_path' => array(
        'description' => t('For links corresponding to a Drupal path (external = 0), this connects the link to a {menu_router}.path for joins.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'link_title' => array(
      'description' => t('The text displayed for the link, which may be modified by a title callback stored in {menu_router}.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'options' => array(
        'description' => t('A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.'),
        'type' => 'text',
        'not null' => FALSE),
      'module' => array(
        'description' => t('The name of the module that generated this link.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'system'),
      'hidden' => array(
        'description' => t('A flag for whether the link should be rendered in menus. (1 = a disabled menu item that may be shown on admin screens, -1 = a menu callback, 0 = a normal, visible link)'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'external' => array(
        'description' => t('A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'has_children' => array(
        'description' => t('Flag indicating whether any links have this link as a parent (1 = children exist, 0 = no children).'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'expanded' => array(
        'description' => t('Flag for whether this link should be rendered as expanded in menus - expanded links always have their child links displayed, instead of only when the link is in the active trail (1 = expanded, 0 = not expanded)'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'weight' => array(
        'description' => t('Link weight among links in the same menu at the same depth.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'depth' => array(
        'description' => t('The depth relative to the top level. A link with plid == 0 will have depth == 1.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'customized' => array(
        'description' => t('A flag to indicate that the user has manually created or edited the link (1 = customized, 0 = not customized).'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      'p1' => array(
        'description' => t('The first mlid in the materialized path. If N = depth, then pN must equal the mlid. If depth > 1 then p(N-1) must equal the plid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p2' => array(
        'description' => t('The second mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p3' => array(
        'description' => t('The third mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p4' => array(
        'description' => t('The fourth mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p5' => array(
        'description' => t('The fifth mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p6' => array(
        'description' => t('The sixth mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p7' => array(
        'description' => t('The seventh mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p8' => array(
        'description' => t('The eighth mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'p9' => array(
        'description' => t('The ninth mlid in the materialized path. See p1.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'updated' => array(
        'description' => t('Flag that indicates that this link was generated during the update from Drupal 5.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'small'),
      ),
    'indexes' => array(
      'path_menu' => array(array('link_path', 128), 'menu_name'),
      'menu_plid_expand_child' => array(
        'menu_name', 'plid', 'expanded', 'has_children'),
      'menu_parents' => array(
        'menu_name', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'),
      'router_path' => array(array('router_path', 128)),
      ),
    'primary key' => array('mlid'),
    );

  $schema['sessions'] = array(
    'description' => t("Drupal's session handlers read and write into the sessions table. Each record represents a user session, either anonymous or authenticated."),
    'fields' => array(
      'uid' => array(
        'description' => t('The {users}.uid corresponding to a session, or 0 for anonymous user.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'sid' => array(
        'description' => t("Primary key: A session ID. The value is generated by PHP's Session API."),
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => ''),
      'hostname' => array(
        'description' => t('The IP address that last used this session ID (sid).'),
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''),
      'timestamp' => array(
        'description' => t('The Unix timestamp when this session last requested a page. Old records are purged by PHP automatically.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'cache' => array(
        'description' => t("The time of this user's last post. This is used when the site has specified a minimum_cache_lifetime. See cache_get()."),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'session' => array(
        'description' => t('The serialized contents of $_SESSION, an array of name/value pairs that persists across page requests by this session ID. Drupal loads $_SESSION from here at the start of each request and saves it at the end.'),
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big')
      ),
    'primary key' => array('sid'),
    'indexes' => array(
      'timestamp' => array('timestamp'),
      'uid' => array('uid')
      ),
    );

  $schema['system'] = array(
    'description' => t("A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system."),
    'fields' => array(
      'filename' => array(
        'description' => t('The path of the primary file for this item, relative to the Drupal root; e.g. modules/node/node.module.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'name' => array(
        'description' => t('The name of the item; e.g. node.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'type' => array(
        'description' => t('The type of the item, either module, theme, or theme_engine.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'owner' => array(
        'description' => t("A theme's 'parent'. Can be either a theme or an engine."),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'status' => array(
        'description' => t('Boolean indicating whether or not this item is enabled.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'throttle' => array(
        'description' => t('Boolean indicating whether this item is disabled when the throttle.module disables throttleable items.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny'),
      'bootstrap' => array(
        'description' => t("Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted)."),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'schema_version' => array(
        'description' => t("The module's database schema version number. -1 if the module is not installed (its tables do not exist); 0 or the largest N of the module's hook_update_N() function that has either been run or existed when the module was first installed."),
        'type' => 'int',
        'not null' => TRUE,
        'default' => -1,
        'size' => 'small'),
      'weight' => array(
        'description' => t("The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name."),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'info' => array(
        'description' => t("A serialized array containing information from the module's .info file; keys can include name, description, package, version, core, dependencies, dependents, and php."),
        'type' => 'text',
        'not null' => FALSE)
      ),
    'primary key' => array('filename'),
    'indexes' =>
      array(
        'modules' => array(array('type', 12), 'status', 'weight', 'filename'),
        'bootstrap' => array(array('type', 12), 'status', 'bootstrap', 'weight', 'filename'),
      ),
    );

  $schema['url_alias'] = array(
    'description' => t('A list of URL aliases for Drupal paths; a user may visit either the source or destination path.'),
    'fields' => array(
      'pid' => array(
        'description' => t('A unique path alias identifier.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'src' => array(
        'description' => t('The Drupal path this alias is for; e.g. node/12.'),
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''),
      'dst' => array(
        'description' => t('The alias for this path; e.g. title-of-the-story.'),
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''),
      'language' => array(
        'description' => t('The language this alias is for; if blank, the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.'),
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '')
      ),
    'unique keys' => array('dst_language' => array('dst', 'language')),
    'primary key' => array('pid'),
    'indexes' => array('src' => array('src')),
    );

  return $schema;
}

// Updates for core.

/**
 * @defgroup updates-4.7.x-extra Extra system updates for 4.7.x
 * @{
 */

function system_update_last_removed() {
  return 179;
}

function system_update_180() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {node} ADD PRIMARY KEY (nid, vid)");
      $ret[] = update_sql("ALTER TABLE {node} ADD UNIQUE (vid)");
      $ret[] = update_sql("ALTER TABLE {node} ADD INDEX (nid)");

      $ret[] = update_sql("ALTER TABLE {node_counter} CHANGE nid nid INT(10) NOT NULL DEFAULT '0'");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {node} DROP CONSTRAINT {node}_pkey"); // Change PK
      $ret[] = update_sql("ALTER TABLE {node} ADD PRIMARY KEY (nid, vid)");
      $ret[] = update_sql('DROP INDEX {node}_vid_idx'); // Change normal index to UNIQUE index
      $ret[] = update_sql('CREATE UNIQUE INDEX {node}_vid_idx ON {node}(vid)');
      $ret[] = update_sql('CREATE INDEX {node}_nid_idx ON {node}(nid)'); // Add index on nid
      break;
  }

  return $ret;
}

function system_update_181() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {profile_fields} ADD autocomplete TINYINT(1) NOT NULL AFTER visibility ;");
      break;
    case 'pgsql':
      db_add_column($ret, 'profile_fields', 'autocomplete', 'smallint', array('not null' => TRUE, 'default' => 0));
      break;
  }
  return $ret;
}

/**
 * The lid field in pgSQL should not be UNIQUE, but an INDEX.
 */
function system_update_182() {
  $ret = array();

  if ($GLOBALS['db_type'] == 'pgsql') {
    $ret[] = update_sql('ALTER TABLE {locales_target} DROP CONSTRAINT {locales_target}_lid_key');
    $ret[] = update_sql('CREATE INDEX {locales_target}_lid_idx ON {locales_target} (lid)');
  }

  return $ret;
}

/**
 * Cid matching by MySQL should be case-sensitive.
 */
function system_update_183() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {cache} CHANGE cid cid varchar(255) BINARY NOT NULL default ''");
      break;
  }
  return $ret;
}

function system_update_184() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {access} CHANGE aid aid int(10) NOT NULL AUTO_INCREMENT ");
      $ret[] = update_sql("ALTER TABLE {boxes} CHANGE bid bid int NOT NULL AUTO_INCREMENT ");
      break;
    case 'pgsql':
       // No database update required for PostgreSQL because it already uses big SERIAL numbers.
  }

  return $ret;
}

/**
 * @} End of "defgroup updates-4.7-extra"
 */

/**
 * @defgroup updates-4.7-to-5.0 System updates from 4.7 to 5.0
 * @{
 */

function system_update_1000() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    $ret[] = update_sql("CREATE TABLE {blocks_roles} (
      module varchar(64) NOT NULL,
      delta varchar(32) NOT NULL,
      rid int unsigned NOT NULL,
      PRIMARY KEY (module, delta, rid)
      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
    break;

    case 'pgsql':
    $ret[] = update_sql("CREATE TABLE {blocks_roles} (
      module varchar(64) NOT NULL,
      delta varchar(32) NOT NULL,
      rid integer NOT NULL,
      PRIMARY KEY (module, delta, rid)
      );");
    break;

  }
  return $ret;
}

function system_update_1001() {
  // change DB schema for better poll support
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      // alter poll_votes table
      $ret[] = update_sql("ALTER TABLE {poll_votes} ADD COLUMN chorder int NOT NULL default -1 AFTER uid");
      break;

    case 'pgsql':
      db_add_column($ret, 'poll_votes', 'chorder', 'int', array('not null' => TRUE, 'default' => "'-1'"));
      break;
  }

  return $ret;
}

function system_update_1002() {
  // Make the forum's vocabulary the highest in list, if present
  $ret = array();

  if ($vid = (int) variable_get('forum_nav_vocabulary', 0)) {
    $ret[] = update_sql('UPDATE {vocabulary} SET weight = -10 WHERE vid = '. $vid);
  }

  return $ret;
}

function system_update_1003() {
  // Make use of guid in feed items
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {aggregator_item} ADD guid varchar(255) AFTER timestamp ;");
      break;
    case 'pgsql':
      db_add_column($ret, 'aggregator_item', 'guid', 'varchar(255)');
      break;
  }
  return $ret;
}


function system_update_1004() {
  // Increase the size of bid in boxes and aid in access
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    $ret[] = update_sql("ALTER TABLE {access} CHANGE `aid` `aid` int NOT NULL AUTO_INCREMENT ");
    $ret[] = update_sql("ALTER TABLE {boxes} CHANGE `bid` `bid` int NOT NULL AUTO_INCREMENT ");
      break;
    case 'pgsql':
      // No database update required for PostgreSQL because it already uses big SERIAL numbers.
      break;
  }
  return $ret;
}

function system_update_1005() {
  // Add ability to create dynamic node types like the CCK module
  $ret = array();

  // The node_type table may already exist for anyone who ever used CCK in 4.7,
  // even if CCK is no longer installed. We need to make sure any previously
  // created table gets renamed before we create the new node_type table in
  // order to ensure that the new table gets created without errors.
  // TODO: This check should be removed for Drupal 6.
  if (db_table_exists('node_type')) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql('RENAME TABLE {node_type} TO {node_type_content}');
        break;

      case 'pgsql':
        $ret[] = update_sql('ALTER TABLE {node_type} RENAME TO {node_type_content}');
        break;
    }
  }

  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      // Create node_type table
      $ret[] = update_sql("CREATE TABLE {node_type} (
        type varchar(32) NOT NULL,
        name varchar(255) NOT NULL,
        module varchar(255) NOT NULL,
        description mediumtext NOT NULL,
        help mediumtext NOT NULL,
        has_title tinyint unsigned NOT NULL,
        title_label varchar(255) NOT NULL default '',
        has_body tinyint unsigned NOT NULL,
        body_label varchar(255) NOT NULL default '',
        min_word_count smallint unsigned NOT NULL,
        custom tinyint NOT NULL DEFAULT '0',
        modified tinyint NOT NULL DEFAULT '0',
        locked tinyint NOT NULL DEFAULT '0',
        orig_type varchar(255) NOT NULL default '',
        PRIMARY KEY (type)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;

    case 'pgsql':
      // add new unsigned types for pgsql
      $ret[] = update_sql("CREATE DOMAIN int_unsigned integer CHECK (VALUE >= 0)");
      $ret[] = update_sql("CREATE DOMAIN smallint_unsigned smallint CHECK (VALUE >= 0)");
      $ret[] = update_sql("CREATE DOMAIN bigint_unsigned bigint CHECK (VALUE >= 0)");

      $ret[] = update_sql("CREATE TABLE {node_type} (
        type varchar(32) NOT NULL,
        name varchar(255) NOT NULL,
        module varchar(255) NOT NULL,
        description text NOT NULL,
        help text NOT NULL,
        has_title smallint_unsigned NOT NULL,
        title_label varchar(255) NOT NULL default '',
        has_body smallint_unsigned NOT NULL,
        body_label varchar(255) NOT NULL default '',
        min_word_count smallint_unsigned NOT NULL,
        custom smallint NOT NULL DEFAULT '0',
        modified smallint NOT NULL DEFAULT '0',
        locked smallint NOT NULL DEFAULT '0',
        orig_type varchar(255) NOT NULL default '',
        PRIMARY KEY (type)
        );");
      break;
  }

  // Insert default user-defined node types into the database.
  $types = array(
    array(
      'type' => 'page',
      'name' => t('Page'),
      'module' => 'node',
      'description' => t('If you want to add a static page, like a contact page or an about page, use a page.'),
      'custom' => TRUE,
      'modified' => TRUE,
      'locked' => FALSE,
    ),
    array(
      'type' => 'story',
      'name' => t('Story'),
      'module' => 'node',
      'description' => t('Stories are articles in their simplest form: they have a title, a teaser and a body, but can be extended by other modules. The teaser is part of the body too. Stories may be used as a personal blog or for news articles.'),
      'custom' => TRUE,
      'modified' => TRUE,
      'locked' => FALSE,
    )
  );

  foreach ($types as $type) {
    $type = (object) _node_type_set_defaults($type);
    node_type_save($type);
  }

  cache_clear_all();

  include_once './'. drupal_get_path('module', 'system') .'/system.admin.inc';
  system_modules();

  menu_rebuild();
  node_types_rebuild();

  // Migrate old values for 'minimum_x_size' variables to the node_type table.
  $query = db_query('SELECT type FROM {node_type}');
  while ($result = db_fetch_object($query)) {
    $variable_name = 'minimum_'. $result->type .'_size';
    if ($value = db_fetch_object(db_query("SELECT value FROM {variable} WHERE name = '%s'", $variable_name))) {
      $value = (int) unserialize($value->value);
      db_query("UPDATE {node_type} SET min_word_count = %d, modified = %d WHERE type = '%s'", $value, 1, $result->type);
      variable_del($variable_name);
    }
  }

  node_types_rebuild();

  return $ret;
}

function system_update_1006() {
  // Add a customizable title to all blocks.
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    $ret[] = update_sql("ALTER TABLE {blocks} ADD title VARCHAR(64) NOT NULL DEFAULT ''");
      break;
    case 'pgsql':
      db_add_column($ret, 'blocks', 'title', 'varchar(64)', array('default' => "''", 'not null' => TRUE));
      break;
  }
  // Migrate custom block titles to new column.
  $boxes = db_query('SELECT bid, title from {boxes}');
  while ($box = db_fetch_object($boxes)) {
    db_query("UPDATE {blocks} SET title = '%s' WHERE delta = %d and module = 'block'", $box->title, $box->bid);
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {boxes} DROP title');
      break;
    case 'pgsql':
      $ret[] = update_sql('ALTER TABLE {boxes} DROP COLUMN title');
      break;
  }
  return $ret;
}

function system_update_1007() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {aggregator_item} ADD INDEX (fid)");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE INDEX {aggregator_item}_fid_idx ON {aggregator_item} (fid)");
      break;
  }
  return $ret;
}

/**
 * Performance update for queries that are related to the locale.module
 */
function system_update_1008() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {locales_source} ADD KEY source (source(30))');
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE INDEX {locales_source}_source_idx on {locales_source} (source)");
  }

  return $ret;
}

function system_update_1010() {
  $ret = array();

  // Disable urlfilter.module, if it exists.
  if (module_exists('urlfilter')) {
    module_disable(array('urlfilter'));
    $ret[] = update_sql("UPDATE {filter_formats} SET module = 'filter', delta = 3 WHERE module = 'urlfilter'");
    $ret[] = t('URL Filter module was disabled; this functionality has now been added to core.');
  }

  return $ret;
}

function system_update_1011() {
  $ret = array();
  $ret[] = update_sql('UPDATE {menu} SET mid = 2 WHERE mid = 0');
  cache_clear_all();
  return $ret;
}

function system_update_1012() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {file_revisions} ADD INDEX(vid)");
      $ret[] = update_sql("ALTER TABLE {files} ADD INDEX(nid)");
      break;
    case 'pgsql':
      $ret[] = update_sql('CREATE INDEX {file_revisions}_vid_idx ON {file_revisions} (vid)');
      $ret[] = update_sql('CREATE INDEX {files}_nid_idx ON {files} (nid)');
      break;
  }
  return $ret;
}

function system_update_1013() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {sessions} CHANGE COLUMN sid sid varchar(64) NOT NULL default ''");
      break;
    case 'pgsql':
      db_change_column($ret, 'sessions', 'sid', 'sid', 'varchar(64)',  array('not null' => TRUE, 'default' => "''"));
      break;
  }
  return $ret;
}

function system_update_1014() {
  variable_del('cron_busy');
  return array();
}

/**
 * Add an index on watchdog type.
 */
function system_update_1015() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {watchdog} ADD INDEX (type)');
      break;
    case 'pgsql':
      $ret[] = update_sql('CREATE INDEX {watchdog}_type_idx ON {watchdog}(type)');
      break;
  }
  return $ret;
}

/**
 * Allow for longer URL encoded (%NN) UTF-8 characters in the location field of watchdog table.
 */
function system_update_1016() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {watchdog} CHANGE COLUMN location location text NOT NULL");
      break;
    case 'pgsql':
      db_change_column($ret, 'watchdog', 'location', 'location', 'text',  array('not null' => TRUE, 'default' => "''"));
      break;
  }
  return $ret;
}

/**
 * Allow role names to be up to 64 characters.
 */
function system_update_1017() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_change_column($ret, 'role', 'name', 'name', 'varchar(64)', array('not null' => TRUE, 'default' => "''"));
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {role} CHANGE name name varchar(64) NOT NULL default ''");
      break;
  }
  return $ret;
}

/**
 * Change break tag (was removed, see 1020).
 */
function system_update_1018() {
  variable_set('update_1020_ok', TRUE);
  return array();
}

/**
 * Change variable format for user-defined e-mails.
 */
function system_update_1019() {
  $message_ids = array('welcome_subject', 'welcome_body',
                       'approval_subject', 'approval_body',
                       'pass_subject', 'pass_body',
  );
  foreach ($message_ids as $id) {
    // Replace all %vars with !vars
    if ($message = variable_get('user_mail_'. $id, NULL)) {
      $fixed = preg_replace('/%([A-Za-z_-]+)/', '!\1', $message);
      variable_set('user_mail_'. $id, $fixed);
    }
  }
  return array();
}

/**
 * Change break tag back (was removed from head).
 */
function system_update_1020() {
  $ret = array();
  if (!variable_get('update_1020_ok', FALSE)) {
    $ret[] = update_sql("UPDATE {node_revisions} SET body = REPLACE(body, '<break>', '<!--break-->')");
  }
  variable_del('update_1020_ok');
  return $ret;
}

/**
 * Update two more variables that were missing from system_update_1019.
 */
function system_update_1021() {
  $message_ids = array('admin_body', 'admin_subject');
  foreach ($message_ids as $id) {
    // Replace all %vars with !vars
    if ($message = variable_get('user_mail_'. $id, NULL)) {
      $fixed = preg_replace('/%([A-Za-z_-]+)/', '!\1', $message);
      variable_set('user_mail_'. $id, $fixed);
    }
  }
  return array();
}

/**
 * @} End of "defgroup updates-4.7-to-5.0"
 */


/**
 * @defgroup updates-5.x-extra Extra system updates for 5.x
 * @{
 */

/**
 * Add index on users created column.
 */
function system_update_1022() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {users} ADD KEY created (created)');
      break;

    case 'pgsql':
      $ret[] = update_sql("CREATE INDEX {users}_created_idx ON {users} (created)");
      break;
  }
  // Also appears as system_update_6004(). Ensure we don't update twice.
  variable_set('system_update_1022', TRUE);
  return $ret;
}

/**
 * @} End of "defgroup updates-5.x-extra"
 */

/**
 * @defgroup updates-5.x-to-6.x System updates from 5.x to 6.x
 * @{
 */

/**
 * Remove auto_increment from {boxes} to allow adding custom blocks with
 * visibility settings.
 */
function system_update_6000() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $max = (int)db_result(db_query('SELECT MAX(bid) FROM {boxes}'));
      $ret[] = update_sql('ALTER TABLE {boxes} CHANGE COLUMN bid bid int NOT NULL');
      $ret[] = update_sql("REPLACE INTO {sequences} VALUES ('{boxes}_bid', $max)");
      break;
  }
  return $ret;
}

/**
 * Add version id column to {term_node} to allow taxonomy module to use revisions.
 */
function system_update_6001() {
  $ret = array();

  // Add vid to term-node relation.  The schema says it is unsigned.
  db_add_field($ret, 'term_node', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_drop_primary_key($ret, 'term_node');
  db_add_primary_key($ret, 'term_node', array('vid', 'tid', 'nid'));
  db_add_index($ret, 'term_node', 'vid', array('vid'));

  db_query('UPDATE {term_node} t SET vid = (SELECT vid FROM {node} n WHERE t.nid = n.nid)');
  return $ret;
}

/**
 * Increase the maximum length of variable names from 48 to 128.
 */
function system_update_6002() {
  $ret = array();
  db_drop_primary_key($ret, 'variable');
  db_change_field($ret, 'variable', 'name', 'name', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
  db_add_primary_key($ret, 'variable', array('name'));
  return $ret;
}

/**
 * Add index on comments status column.
 */
function system_update_6003() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {comments} ADD KEY status (status)');
      break;

    case 'pgsql':
      $ret[] = update_sql("CREATE INDEX {comments}_status_idx ON {comments} (status)");
      break;
  }
  return $ret;
}

/**
 * This update used to add an index on users created column (#127941).
 * However, system_update_1022() does the same thing.  This update
 * tried to detect if 1022 had already run but failed to do so,
 * resulting in an "index already exists" error.
 *
 * Adding the index here is never necessary.  Sites installed before
 * 1022 will run 1022, getting the update.  Sites installed on/after 1022
 * got the index when the table was first created.  Therefore, this
 * function is now a no-op.
 */
function system_update_6004() {
  return array();
}

/**
 * Add language to url_alias table and modify indexes.
 */
function system_update_6005() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'url_alias', 'language', 'varchar(12)', array('default' => "''", 'not null' => TRUE));

      // As of system.install:1.85 (before the new language
      // subsystem), new installs got a unique key named
      // url_alias_dst_key on url_alias.dst.  Unfortunately,
      // system_update_162 created a unique key inconsistently named
      // url_alias_dst_idx on url_alias.dst (keys should have the _key
      // suffix, indexes the _idx suffix).  Therefore, sites installed
      // before system_update_162 have a unique key with a different
      // name than sites installed after system_update_162().  Now, we
      // want to drop the unique key on dst which may have either one
      // of two names and create a new unique key on (dst, language).
      // There is no way to know which key name exists so we have to
      // drop both, causing an SQL error.  Thus, we just hide the
      // error and only report the update_sql results that work.
      $err = error_reporting(0);
      $ret1 = update_sql('DROP INDEX {url_alias}_dst_idx');
      if ($ret1['success']) {
  $ret[] = $ret1;
      }
      $ret1 = array();
      db_drop_unique_key($ret, 'url_alias', 'dst');
      foreach ($ret1 as $r) {
  if ($r['success']) {
    $ret[] = $r;
  }
      }
      error_reporting($err);

      $ret[] = update_sql('CREATE UNIQUE INDEX {url_alias}_dst_language_idx ON {url_alias}(dst, language)');
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {url_alias} ADD language varchar(12) NOT NULL default ''");
      $ret[] = update_sql("ALTER TABLE {url_alias} DROP INDEX dst");
      $ret[] = update_sql("ALTER TABLE {url_alias} ADD UNIQUE dst_language (dst, language)");
      break;
  }
  return $ret;
}

/**
 * Drop useless indices on node_counter table.
 */
function system_update_6006() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql('DROP INDEX {node_counter}_daycount_idx');
      $ret[] = update_sql('DROP INDEX {node_counter}_totalcount_idx');
      $ret[] = update_sql('DROP INDEX {node_counter}_timestamp_idx');
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_counter} DROP INDEX daycount");
      $ret[] = update_sql("ALTER TABLE {node_counter} DROP INDEX totalcount");
      $ret[] = update_sql("ALTER TABLE {node_counter} DROP INDEX timestamp");
      break;
  }
  return $ret;
}

/**
 * Change the severity column in the watchdog table to the new values.
 */
function system_update_6007() {
  $ret = array();
  $ret[] = update_sql("UPDATE {watchdog} SET severity = ". WATCHDOG_NOTICE ." WHERE severity = 0");
  $ret[] = update_sql("UPDATE {watchdog} SET severity = ". WATCHDOG_WARNING ." WHERE severity = 1");
  $ret[] = update_sql("UPDATE {watchdog} SET severity = ". WATCHDOG_ERROR ." WHERE severity = 2");
  return $ret;
}

/**
 * Add info files to themes.  The info and owner columns are added by
 * update_fix_d6_requirements() in update.php to avoid a large number
 * of error messages from update.php.  All we need to do here is copy
 * description to owner and then drop description.
 */
function system_update_6008() {
  $ret = array();
  $ret[] = update_sql('UPDATE {system} SET owner = description');
  db_drop_field($ret, 'system', 'description');

  // Rebuild system table contents.
  module_rebuild_cache();
  system_theme_data();

  return $ret;
}

/**
 * The PHP filter is now a separate module.
 */
function system_update_6009() {
  $ret = array();

  // If any input format used the Drupal 5 PHP filter.
  if (db_result(db_query("SELECT COUNT(format) FROM {filters} WHERE module = 'filter' AND delta = 1"))) {
    // Enable the PHP filter module.
    $ret[] = update_sql("UPDATE {system} SET status = 1 WHERE name = 'php' AND type = 'module'");
    // Update the input filters.
    $ret[] = update_sql("UPDATE {filters} SET delta = 0, module = 'php' WHERE module = 'filter' AND delta = 1");
  }

  // With the removal of the PHP evaluator filter, the deltas of the line break
  // and URL filter have changed.
  $ret[] = update_sql("UPDATE {filters} SET delta = 1 WHERE module = 'filter' AND delta = 2");
  $ret[] = update_sql("UPDATE {filters} SET delta = 2 WHERE module = 'filter' AND delta = 3");

  return $ret;
}

/**
 * Add variable replacement for watchdog messages.
 *
 * The variables field is NOT NULL and does not have a default value.
 * Existing log messages should not be translated in the new system,
 * so we insert 'N;' (serialize(NULL)) as the temporary default but
 * then remove the default value to match the schema.
 */
function system_update_6010() {
  $ret = array();
  db_add_field($ret, 'watchdog', 'variables', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'initial' => 'N;'));
  return $ret;
}

/**
 * Add language support to nodes
 */
function system_update_6011() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'node', 'language', 'varchar(12)', array('default' => "''", 'not null' => TRUE));
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node} ADD language varchar(12) NOT NULL default ''");
      break;
  }
  return $ret;
}

/**
 * Add serialized field to cache tables.  This is now handled directly
 * by update.php, so this function is a no-op.
 */
function system_update_6012() {
  return array();
}

/**
 * Rebuild cache data for theme system changes
 */
function system_update_6013() {
  // Rebuild system table contents.
  module_rebuild_cache();
  system_theme_data();

  return array(array('success' => TRUE, 'query' => 'Cache rebuilt.'));
}

/**
 * Record that the installer is done, so it is not
 * possible to run the installer on upgraded sites.
 */
function system_update_6014() {
  variable_set('install_task', 'done');

  return array(array('success' => TRUE, 'query' => "variable_set('install_task')"));
}

/**
 * Add the form cache table.
 */
function system_update_6015() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {cache_form} (
        cid varchar(255) NOT NULL default '',
        data bytea,
        expire int NOT NULL default '0',
        created int NOT NULL default '0',
        headers text,
        serialized smallint NOT NULL default '0',
        PRIMARY KEY (cid)
    )");
      $ret[] = update_sql("CREATE INDEX {cache_form}_expire_idx ON {cache_form} (expire)");
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {cache_form} (
        cid varchar(255) NOT NULL default '',
        data longblob,
        expire int NOT NULL default '0',
        created int NOT NULL default '0',
        headers text,
        serialized int(1) NOT NULL default '0',
        PRIMARY KEY (cid),
        INDEX expire (expire)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      break;
  }

  return $ret;
}

/**
 * Make {node}'s primary key be nid, change nid,vid to a unique key.
 * Add primary keys to block, filters, flood, permission, and term_relation.
 */
function system_update_6016() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {node} ADD CONSTRAINT {node}_nid_vid_key UNIQUE (nid, vid)");
      db_add_column($ret, 'blocks', 'bid', 'serial');
      $ret[] = update_sql("ALTER TABLE {blocks} ADD PRIMARY KEY (bid)");
      db_add_column($ret, 'filters', 'fid', 'serial');
      $ret[] = update_sql("ALTER TABLE {filters} ADD PRIMARY KEY (fid)");
      db_add_column($ret, 'flood', 'fid', 'serial');
      $ret[] = update_sql("ALTER TABLE {flood} ADD PRIMARY KEY (fid)");
      db_add_column($ret, 'permission', 'pid', 'serial');
      $ret[] = update_sql("ALTER TABLE {permission} ADD PRIMARY KEY (pid)");
      db_add_column($ret, 'term_relation', 'trid', 'serial');
      $ret[] = update_sql("ALTER TABLE {term_relation} ADD PRIMARY KEY (trid)");
      db_add_column($ret, 'term_synonym', 'tsid', 'serial');
      $ret[] = update_sql("ALTER TABLE {term_synonym} ADD PRIMARY KEY (tsid)");
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {node} ADD UNIQUE KEY nid_vid (nid, vid)');
      $ret[] = update_sql("ALTER TABLE {blocks} ADD bid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {filters} ADD fid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {flood} ADD fid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {permission} ADD pid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {term_relation} ADD trid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {term_synonym} ADD tsid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
      break;
  }

  return $ret;
}

/**
 * Rename settings related to user.module email notifications.
 */
function system_update_6017() {
  $ret = array();
  // Maps old names to new ones.
  $var_names = array(
    'admin'    => 'register_admin_created',
    'approval' => 'register_pending_approval',
    'welcome'  => 'register_no_approval_required',
    'pass'     => 'password_reset',
  );
  foreach ($var_names as $old => $new) {
    foreach (array('_subject', '_body') as $suffix) {
      $old_name = 'user_mail_'. $old . $suffix;
      $new_name = 'user_mail_'. $new . $suffix;
      if ($old_val = variable_get($old_name, FALSE)) {
        variable_set($new_name, $old_val);
        variable_del($old_name);
        $ret[] = array('success' => TRUE, 'query' => "variable_set($new_name)");
        $ret[] = array('success' => TRUE, 'query' => "variable_del($old_name)");
        if ($old_name == 'user_mail_approval_body') {
          drupal_set_message(t('Saving an old value of the welcome message body for users that are pending administrator approval. However, you should consider modifying this text, since Drupal can now be configured to automatically notify users and send them their login information when their accounts are approved. See the !admin_user_settings page for details.', array('!admin_user_settings' => l(t('User settings'), 'admin/user/settings'))));
        }
      }
    }
  }
  return $ret;
}

/**
 * Add HTML corrector to HTML formats or replace the old module if it was in use.
 */
function system_update_6018() {
  $ret = array();

  // Disable htmlcorrector.module, if it exists and replace its filter.
  if (module_exists('htmlcorrector')) {
    module_disable(array('htmlcorrector'));
    $ret[] = update_sql("UPDATE {filter_formats} SET module = 'filter', delta = 3 WHERE module = 'htmlcorrector'");
    $ret[] = array('success' => TRUE, 'query' => 'HTML Corrector module was disabled; this functionality has now been added to core.');
    return $ret;
  }

  // Otherwise, find any format with 'HTML' in its name and add the filter at the end.
  $result = db_query("SELECT format, name FROM {filter_formats} WHERE name LIKE '%HTML%'");
  while ($format = db_fetch_object($result)) {
    $weight = db_result(db_query("SELECT MAX(weight) FROM {filters} WHERE format = %d", $format->format));
    db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format->format, 'filter', 3, max(10, $weight + 1));
    $ret[] = array('success' => TRUE, 'query' => "HTML corrector filter added to the '". $format->name ."' input format.");
  }

  return $ret;
}

/**
 * Reconcile small differences in the previous, manually created mysql
 * and pgsql schemas so they are the same and can be represented by a
 * single schema structure.
 *
 * Note that the mysql and pgsql cases make different changes.  This
 * is because each schema needs to be tweaked in different ways to
 * conform to the new schema structure.  Also, since they operate on
 * tables defined by many optional core modules which may not ever
 * have been installed, they must test each table for existence.  If
 * the modules are first installed after this update exists the tables
 * will be created from the schema structure and will start out
 * correct.
 */
function system_update_6019() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      // Remove default ''.
      if (db_table_exists('aggregator_feed')) {
        db_field_set_no_default($ret, 'aggregator_feed', 'description');
        db_field_set_no_default($ret, 'aggregator_feed', 'image');
      }
      db_field_set_no_default($ret, 'blocks', 'pages');
      if (db_table_exists('contact')) {
        db_field_set_no_default($ret, 'contact', 'recipients');
        db_field_set_no_default($ret, 'contact', 'reply');
      }
      db_field_set_no_default($ret, 'watchdog', 'location');
      db_field_set_no_default($ret, 'node_revisions', 'body');
      db_field_set_no_default($ret, 'node_revisions', 'teaser');
      db_field_set_no_default($ret, 'node_revisions', 'log');

      // Update from pgsql 'float' (which means 'double precision') to
      // schema 'float' (which in pgsql means 'real').
      if (db_table_exists('search_index')) {
        db_change_field($ret, 'search_index', 'score', 'score', array('type' => 'float'));
      }
      if (db_table_exists('search_total')) {
        db_change_field($ret, 'search_total', 'count', 'count', array('type' => 'float'));
      }

      // Replace unique index dst_language with a unique constraint.  The
      // result is the same but the unique key fits our current schema
      // structure.  Also, the postgres documentation implies that
      // unique constraints are preferable to unique indexes.  See
      // http://www.postgresql.org/docs/8.2/interactive/indexes-unique.html.
      if (db_table_exists('url_alias')) {
        db_drop_index($ret, 'url_alias', 'dst_language');
        db_add_unique_key($ret, 'url_alias', 'dst_language',
          array('dst', 'language'));
      }

      // Fix term_node pkey: mysql and pgsql code had different orders.
      if (db_table_exists('term_node')) {
        db_drop_primary_key($ret, 'term_node');
        db_add_primary_key($ret, 'term_node', array('vid', 'tid', 'nid'));
      }

      // Make boxes.bid unsigned.
      db_drop_primary_key($ret, 'boxes');
      db_change_field($ret, 'boxes', 'bid', 'bid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('bid')));

      // Fix primary key
      db_drop_primary_key($ret, 'node');
      db_add_primary_key($ret, 'node', array('nid'));

      break;

    case 'mysql':
    case 'mysqli':
      // Rename key 'link' to 'url'.
      if (db_table_exists('aggregator_feed')) {
        db_drop_unique_key($ret, 'aggregator_feed', 'link');
        db_add_unique_key($ret, 'aggregator_feed', 'url', array('url'));
      }

      // Change to size => small.
      if (db_table_exists('boxes')) {
        db_change_field($ret, 'boxes', 'format', 'format', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
      }

      // Change to size => small.
      // Rename index 'lid' to 'nid'.
      if (db_table_exists('comments')) {
        db_change_field($ret, 'comments', 'format', 'format', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
        db_drop_index($ret, 'comments', 'lid');
        db_add_index($ret, 'comments', 'nid', array('nid'));
      }

      // Change to size => small.
      db_change_field($ret, 'cache', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
      db_change_field($ret, 'cache_filter', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
      db_change_field($ret, 'cache_page', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
      db_change_field($ret, 'cache_form', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));

      // Remove default => 0, set auto increment.
      $new_uid = 1 + db_result(db_query('SELECT MAX(uid) FROM {users}'));
      $ret[] = update_sql('UPDATE {users} SET uid = '. $new_uid .' WHERE uid = 0');
      db_drop_primary_key($ret, 'users');
      db_change_field($ret, 'users', 'uid', 'uid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('uid')));
      $ret[] = update_sql('UPDATE {users} SET uid = 0 WHERE uid = '. $new_uid);

      // Special field names.
      $map = array('node_revisions' => 'vid');
      // Make sure these tables have proper auto_increment fields.
      foreach (array('boxes', 'files', 'node', 'node_revisions') as $table) {
        $field = isset($map[$table]) ? $map[$table] : $table[0] .'id';
        db_drop_primary_key($ret, $table);
        db_change_field($ret, $table, $field, $field, array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array($field)));
      }

      break;
  }

  return $ret;
}

/**
 * Create the tables for the new menu system.
 */
function system_update_6020() {
  $ret = array();

  $schema['menu_router'] = array(
    'fields' => array(
      'path'             => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'load_functions'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'to_arg_functions' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'access_callback'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'access_arguments' => array('type' => 'text', 'not null' => FALSE),
      'page_callback'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'page_arguments'   => array('type' => 'text', 'not null' => FALSE),
      'fit'              => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'number_parts'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'tab_parent'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'tab_root'         => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'title'            => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'title_callback'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'title_arguments'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'type'             => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'block_callback'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'description'      => array('type' => 'text', 'not null' => TRUE),
      'position'         => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'weight'           => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'file'             => array('type' => 'text', 'size' => 'medium')
    ),
    'indexes' => array(
      'fit'        => array('fit'),
      'tab_parent' => array('tab_parent')
    ),
    'primary key' => array('path'),
  );

  $schema['menu_links'] = array(
    'fields' => array(
      'menu_name'    => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
      'mlid'         => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
      'plid'         => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'link_path'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'router_path'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'link_title'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'options'      => array('type' => 'text', 'not null' => FALSE),
      'module'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'system'),
      'hidden'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'external'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'has_children' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'expanded'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'weight'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'depth'        => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'customized'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
      'p1'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p2'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p3'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p4'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p5'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p6'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p7'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p8'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'p9'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'updated'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
    ),
    'indexes' => array(
      'path_menu'              => array(array('link_path', 128), 'menu_name'),
      'menu_plid_expand_child' => array('menu_name', 'plid', 'expanded', 'has_children'),
      'menu_parents'           => array('menu_name', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'),
      'router_path'            => array(array('router_path', 128)),
    ),
    'primary key' => array('mlid'),
  );

  foreach ($schema as $name => $table) {
    db_create_table($ret, $name, $table);
  }
  return $ret;
}

/**
 * Migrate the menu items from the old menu system to the new menu_links table.
 */
function system_update_6021() {
  $ret = array('#finished' => 0);
  // Multi-part update
  if (!isset($_SESSION['system_update_6021'])) {
    db_add_field($ret, 'menu', 'converted', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
    $_SESSION['system_update_6021_max'] = db_result(db_query('SELECT COUNT(*) FROM {menu}'));
    $_SESSION['menu_menu_map'] = array(1 => 'navigation');
    // 0 => FALSE is for new menus, 1 => FALSE is for the navigation.
    $_SESSION['menu_item_map'] = array(0 => FALSE, 1 => FALSE);
    if ($secondary = variable_get('menu_secondary_menu', 0)) {
      $_SESSION['menu_menu_map'][$secondary] = 'secondary-links';
      $_SESSION['menu_item_map'][$secondary] = FALSE;
    }
    if ($primary = variable_get('menu_primary_menu', 0)) {
      $_SESSION['menu_menu_map'][$primary] = 'primary-links';
      $_SESSION['menu_item_map'][$primary] = FALSE;
      if ($primary == $secondary) {
        variable_set('menu_secondary_links_source', 'primary-links');
      }
    }
    $table = array(
      'fields' => array(
        'menu_name'   => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
        'title'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
        'description' => array('type' => 'text', 'not null' => FALSE),
      ),
      'primary key' => array('menu_name'),
    );
    db_create_table($ret, 'menu_custom', $table);
    $menus = array(
      'navigation' => array(
        'menu_name' => 'navigation',
        'title' => 'Navigation',
        'description' => 'The navigation menu is provided by Drupal and is the main interactive menu for any site. It is usually the only menu that contains personalized links for authenticated users, and is often not even visible to anonymous users.',
      ),
      'primary-links' => array(
        'menu_name' => 'primary-links',
        'title' => 'Primary links',
        'description' => 'Primary links are often used at the theme layer to show the major sections of a site. A typical representation for primary links would be tabs along the top.',
      ),
      'secondary-links' => array(
        'menu_name' => 'secondary-links',
        'title' => 'Secondary links',
        'description' => 'Secondary links are often used for pages like legal notices, contact details, and other secondary navigation items that play a lesser role than primary links',
      ),
    );
    // Save user-defined titles.
    foreach (array($primary, $secondary) as $mid) {
      if ($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid))) {
        $menus[$_SESSION['menu_menu_map'][$mid]]['title']  = $item['title'];
      }
    }
    foreach ($menus as $menu) {
      db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu);
    }
    $_SESSION['system_update_6021'] = 0;
  }

  $limit = 50;
  while ($limit-- && ($item = db_fetch_array(db_query_range('SELECT * FROM {menu} WHERE converted = 0', 0, 1)))) {
    // If it's not a menu...
    if ($item['pid']) {
      // Let's climb up until we find an item with a converted parent.
      $item_original = $item;
      while ($item && !isset($_SESSION['menu_item_map'][$item['pid']])) {
        $item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $item['pid']));
      }
      // This can only occur if the menu entry is a leftover in the menu table.
      // These do not appear in Drupal 5 anyways, so we skip them.
      if (!$item) {
        db_query('UPDATE {menu} SET converted = %d WHERE mid = %d', 1, $item_original['mid']);
        $_SESSION['system_update_6021']++;
        continue;
      }
    }
    // We need to recheck because item might have changed.
    if ($item['pid']) {
      // Fill the new fields.
      $item['link_title'] = $item['title'];
      $item['link_path'] = drupal_get_normal_path($item['path']);
      // We know the parent is already set. If it's not FALSE then it's an item.
      if ($_SESSION['menu_item_map'][$item['pid']]) {
        // The new menu system parent link id.
        $item['plid'] = $_SESSION['menu_item_map'][$item['pid']]['mlid'];
        // The new menu system menu name.
        $item['menu_name'] = $_SESSION['menu_item_map'][$item['pid']]['menu_name'];
      }
      else {
        // This a top level element.
        $item['plid'] = 0;
        // The menu name is stored among the menus.
        $item['menu_name'] = $_SESSION['menu_menu_map'][$item['pid']];
      }
      // Is the element visible in the menu block?
      $item['hidden'] = !($item['type'] & MENU_VISIBLE_IN_TREE);
      // Is it a custom(ized) element?
      if ($item['type'] & (MENU_CREATED_BY_ADMIN | MENU_MODIFIED_BY_ADMIN)) {
        $item['customized'] = TRUE;
      }
      // Items created via the menu module need to be assigned to it.
      if ($item['type'] & MENU_CREATED_BY_ADMIN) {
        $item['module'] = 'menu';
      }
      else {
        $item['module'] = 'system';
      }
      $item['updated'] = TRUE;
      // Save the link.
      $item['router_path'] = '';
      menu_link_save($item);
      $_SESSION['menu_item_map'][$item['mid']] = array('mlid' => $item['mlid'], 'menu_name' => $item['menu_name']);
    }
    elseif (!isset($_SESSION['menu_menu_map'][$item['mid']])) {
      $item['menu_name'] = 'menu-'. preg_replace('/[^a-zA-Z0-9]/', '-', strtolower($item['title']));
      $item['menu_name'] = substr($item['menu_name'], 0, 20);
      $original_menu_name = $item['menu_name'];
      $i = 0;
      while (db_result(db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = '%s'", $item['menu_name']))) {
        $item['menu_name'] = $original_menu_name . ($i++);
      }
      if ($item['path']) {
        // Another bunch of bogus entries. Apparently, these are leftovers
        // from Drupal 4.7 .
        $_SESSION['menu_bogus_menus'][] = $item['menu_name'];
      }
      else {
        // Add this menu to the list of custom menus.
        db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '')", $item['menu_name'], $item['title']);
      }
      $_SESSION['menu_menu_map'][$item['mid']] = $item['menu_name'];
      $_SESSION['menu_item_map'][$item['mid']] = FALSE;
    }
    db_query('UPDATE {menu} SET converted = %d WHERE mid = %d', 1, $item['mid']);
    $_SESSION['system_update_6021']++;
  }

  if ($_SESSION['system_update_6021'] >= $_SESSION['system_update_6021_max']) {
    if (!empty($_SESSION['menu_bogus_menus'])) {
      // Remove entries in bogus menus. This is secure because we deleted
      // every non-alpanumeric character from the menu name.
      $ret[] = update_sql("DELETE FROM {menu_links} WHERE menu_name IN ('". implode("', '", $_SESSION['menu_bogus_menus']) ."')");
    }
    // Update menu OTF preferences.
    $mid = variable_get('menu_parent_items', 0);
    $menu_name = $mid ? $_SESSION['menu_menu_map'][$mid] : 'navigation';
    variable_set('menu_default_node_menu', $menu_name);
    // Skip the navigation menu - it is handled by the user module.
    unset($_SESSION['menu_menu_map'][1]);
    // Update the deltas for all menu module blocks.
    foreach ($_SESSION['menu_menu_map'] as $mid => $menu_name) {
      // This is again secure because we deleted every non-alpanumeric
      // character from the menu name.
      $ret[] = update_sql("UPDATE {blocks} SET delta = '". $menu_name ."' WHERE module = 'menu' AND delta = '". $mid ."'");
      $ret[] = update_sql("UPDATE {blocks_roles} SET delta = '". $menu_name ."' WHERE module = 'menu' AND delta = '". $mid ."'");
    }
    $ret[] = array('success' => TRUE, 'query' => 'Relocated '. $_SESSION['system_update_6021'] .'existing items to the new menu system.');
    $ret[] = update_sql("DROP TABLE {menu}");
    unset($_SESSION['system_update_6021'], $_SESSION['system_update_6021_max'], $_SESSION['menu_menu_map'], $_SESSION['menu_item_map'], $_SESSION['menu_bogus_menus']);
    // Create the menu overview links - also calls menu_rebuild(). If menu is
    // disabled, then just call menu_rebuild.
    if (function_exists('menu_enable')) {
      menu_enable();
    }
    else {
      menu_rebuild();
    }
    $ret['#finished'] = 1;
  }
  else {
    $ret['#finished'] = $_SESSION['system_update_6021'] / $_SESSION['system_update_6021_max'];
  }
  return $ret;
}

/**
 * Update files tables to associate files to a uid by default instead of a nid.
 * Rename file_revisions to upload since it should only be used by the upload
 * module used by upload to link files to nodes.
 */
function system_update_6022() {
  $ret = array();

  // Rename the nid field to vid, add status and timestamp fields, and indexes.
  db_drop_index($ret, 'files', 'nid');
  db_change_field($ret, 'files', 'nid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_field($ret, 'files', 'status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  db_add_field($ret, 'files', 'timestamp', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_index($ret, 'files', 'uid', array('uid'));
  db_add_index($ret, 'files', 'status', array('status'));
  db_add_index($ret, 'files', 'timestamp', array('timestamp'));

  // Rename the file_revisions table to upload then add nid column. Since we're
  // changing the table name we need to drop and re-add the indexes and
  // the primary key so both mysql and pgsql end up with the correct index
  // names.
  db_drop_primary_key($ret, 'file_revisions');
  db_drop_index($ret, 'file_revisions', 'vid');
  db_rename_table($ret, 'file_revisions', 'upload');
  db_add_field($ret, 'upload', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_index($ret, 'upload', 'nid', array('nid'));
  db_add_primary_key($ret, 'upload', array('vid', 'fid'));
  db_add_index($ret, 'upload', 'fid', array('fid'));

  // The nid column was renamed to uid. Use the old nid to find the node's uid.
  update_sql('UPDATE {files} SET uid = (SELECT n.uid FROM {node} n WHERE {files}.uid = n.nid)');
  update_sql('UPDATE {upload} SET nid = (SELECT r.nid FROM {node_revisions} r WHERE {upload}.vid = r.vid)');

  // Mark all existing files as FILE_STATUS_PERMANENT.
  $ret[] = update_sql('UPDATE {files} SET status = 1');

  return $ret;
}

function system_update_6023() {
  $ret = array();

  // nid is DEFAULT 0
  db_drop_index($ret, 'node_revisions', 'nid');
  db_change_field($ret, 'node_revisions', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_index($ret, 'node_revisions', 'nid', array('nid'));
  return $ret;
}

/**
 * Add translation fields to nodes used by translation module.
 */
function system_update_6024() {
  $ret = array();
  db_add_field($ret, 'node', 'tnid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_add_field($ret, 'node', 'translate', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  db_add_index($ret, 'node', 'tnid', array('tnid'));
  db_add_index($ret, 'node', 'translate', array('translate'));
  return $ret;
}

/**
 * Increase the maximum length of node titles from 128 to 255.
 */
function system_update_6025() {
  $ret = array();
  db_drop_index($ret, 'node', 'node_title_type');
  db_change_field($ret, 'node', 'title', 'title', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  db_add_index($ret, 'node', 'node_title_type', array('title', array('type', 4)));
  db_change_field($ret, 'node_revisions', 'title', 'title', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  return $ret;
}

/**
 * Display warning about new Update status module.
 */
function system_update_6026() {
  $ret = array();

  // Notify user that new update module exists.
  drupal_set_message(t('Drupal can check periodically for important bug fixes and security releases using the new update status module. This module can be turned on from the <a href="@modules">modules administration page</a>. For more information please read the <a href="@update">Update status handbook page</a>.', array('@modules' => url('admin/build/modules'), '@update' => 'http://drupal.org/handbook/modules/update')));

  return $ret;
}

/**
 * Add block cache.
 */
function system_update_6027() {
  $ret = array();

  // Create the blocks.cache column.
  db_add_field($ret, 'blocks', 'cache', array('type' => 'int', 'not null' => TRUE, 'default' => 1, 'size' => 'tiny'));

  // The cache_block table is created in update_fix_d6_requirements() since
  // calls to cache_clear_all() would otherwise cause warnings.

  // Fill in the values for the new 'cache' column in the {blocks} table.
  foreach (module_list() as $module) {
    if ($module_blocks = module_invoke($module, 'block', 'list')) {
      foreach ($module_blocks as $delta => $block) {
        if (isset($block['cache'])) {
          db_query("UPDATE {blocks} SET cache = %d WHERE module = '%s' AND delta = %d", $block['cache'], $module, $delta);
        }
      }
    }
  }

  return $ret;
}

/**
 * Add the node load cache table.
 */
function system_update_6028() {
  // Removed node_load cache to discuss it more for Drupal 7.
  return array();
}

/**
 * Enable the dblog module on sites that upgrade, since otherwise
 * watchdog logging will stop unexpectedly.
 */
function system_update_6029() {
  // The watchdog table is now owned by dblog, which is not yet
  // "installed" according to the system table, but the table already
  // exists.  We set the module as "installed" here to avoid an error
  // later.
  //
  // Although not the case for the initial D6 release, it is likely
  // that dblog.install will have its own update functions eventually.
  // However, dblog did not exist in D5 and this update is part of the
  // initial D6 release, so we know that dblog is not installed yet.
  // It is therefore correct to install it as version 0.  If
  // dblog updates exist, the next run of update.php will get them.
  drupal_set_installed_schema_version('dblog', 0);
  module_enable(array('dblog'));
  menu_rebuild();
  return array(array('success' => TRUE, 'query' => "'dblog' module enabled."));
}

/**
 * Add the tables required by actions.inc.
 */
function system_update_6030() {
  $ret = array();
  $schema['actions'] = array(
    'fields' => array(
      'aid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'),
      'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
      'callback' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      'parameters' => array('type' => 'text', 'not null' => TRUE, 'size' => 'big'),
      'description' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'),
    ),
    'primary key' => array('aid'),
  );

  $schema['actions_aid'] = array(
    'fields' => array(
      'aid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
    ),
    'primary key' => array('aid'),
  );

  db_create_table($ret, 'actions', $schema['actions']);
  db_create_table($ret, 'actions_aid', $schema['actions_aid']);

  return $ret;
}

/**
 * Ensure that installer cannot be run again after updating from Drupal 5.x to 6.x
 * Actually, this is already done by system_update_6014(), so this is now a no-op.
 */
function system_update_6031() {
  return array();
}

/**
 * profile_fields.name used to be nullable but is part of a unique key
 * and so shouldn't be.
 */
function system_update_6032() {
  $ret = array();
  if (db_table_exists('profile_fields')) {
    db_drop_unique_key($ret, 'profile_fields', 'name');
    db_change_field($ret, 'profile_fields', 'name', 'name', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
    db_add_unique_key($ret, 'profile_fields', 'name', array('name'));
  }
  return $ret;
}

/**
 * Change node_comment_statistics to be not autoincrement.
 */
function system_update_6033() {
  $ret = array();
  if (db_table_exists('node_comment_statistics')) {
    // On pgsql but not mysql, db_change_field() drops all keys
    // involving the changed field, which in this case is the primary
    // key.  The normal approach is explicitly drop the pkey, change the
    // field, and re-create the pkey.
    //
    // Unfortunately, in this case that won't work on mysql; we CANNOT
    // drop the pkey because on mysql auto-increment fields must be
    // included in at least one key or index.
    //
    // Since we cannot drop the pkey before db_change_field(), after
    // db_change_field() we may or may not still have a pkey.  The
    // simple way out is to re-create the pkey only when using pgsql.
    // Realistic requirements trump idealistic purity.
    db_change_field($ret, 'node_comment_statistics', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
    if ($GLOBALS['db_type'] == 'pgsql') {
      db_add_primary_key($ret, 'node_comment_statistics', array('nid'));
    }
  }
  return $ret;
}

/**
 * Rename permission "administer access control" to "administer permissions".
 */
function system_update_6034() {
  $ret = array();
  $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
  while ($role = db_fetch_object($result)) {
    $renamed_permission = preg_replace('/administer access control/', 'administer permissions', $role->perm);
    if ($renamed_permission != $role->perm) {
      $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
    }
  }
  return $ret;
}

/**
 * Change index on system table for better performance.
 */
function system_update_6035() {
  $ret = array();
  db_drop_index($ret, 'system', 'weight');
  db_add_index($ret, 'system', 'modules', array(array('type', 12), 'status', 'weight', 'filename'));
  db_add_index($ret, 'system', 'bootstrap', array(array('type', 12), 'status', 'bootstrap', 'weight', 'filename'));
  return $ret;
}

/**
 * Change the search schema and indexing.
 *
 * The table data is preserved where possible in MYSQL and MYSQLi using
 * ALTER IGNORE. Other databases don't support that, so for them the
 * tables are dropped and re-created, and will need to be re-indexed
 * from scratch.
 */
function system_update_6036() {
  $ret = array();
  if (db_table_exists('search_index')) {
    // Create the search_dataset.reindex column.
    db_add_field($ret, 'search_dataset', 'reindex', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

    // Drop the search_index.from fields which are no longer used.
    db_drop_index($ret, 'search_index', 'from_sid_type');
    db_drop_field($ret, 'search_index', 'fromsid');
    db_drop_field($ret, 'search_index', 'fromtype');

    // Drop the search_dataset.sid_type index, so that it can be made unique.
    db_drop_index($ret, 'search_dataset', 'sid_type');

    // Create the search_node_links Table.
    $search_node_links_schema = array(
      'fields' => array(
        'sid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'type'     => array('type' => 'varchar', 'length' => 16, 'not null' => TRUE, 'default' => ''),
        'nid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'caption'    => array('type' => 'text', 'size' => 'big', 'not null' => FALSE),
      ),
      'primary key' => array('sid', 'type', 'nid'),
      'indexes' => array('nid' => array('nid')),
    );
    db_create_table($ret, 'search_node_links', $search_node_links_schema);

    // with the change to search_dataset.reindex, the search queue is handled differently,
    // and this is no longer needed
    variable_del('node_cron_last');

    // Add a unique index for the search_index.
    if ($GLOBALS['db_type'] == 'mysql' || $GLOBALS['db_type'] == 'mysqli') {
      // Since it's possible that some existing sites have duplicates,
      // create the index using the IGNORE keyword, which ignores duplicate errors.
      // However, pgsql doesn't support it
      $ret[] = update_sql("ALTER IGNORE TABLE {search_index} ADD UNIQUE KEY word_sid_type (word, sid, type)");
      $ret[] = update_sql("ALTER IGNORE TABLE {search_dataset} ADD UNIQUE KEY sid_type (sid, type)");

      // Everything needs to be reindexed.
      $ret[] = update_sql("UPDATE {search_dataset} SET reindex = 1");
    }
    else {
      // Delete the existing tables if there are duplicate values
      if (db_result(db_query("SELECT sid FROM {search_dataset} GROUP BY sid, type HAVING COUNT(*) > 1")) || db_result(db_query("SELECT sid FROM {search_index} GROUP BY word, sid, type HAVING COUNT(*) > 1"))) {
        $ret[] = update_sql('DELETE FROM {search_dataset}');
        $ret[] = update_sql('DELETE FROM {search_index}');
        $ret[] = update_sql('DELETE FROM {search_total}');
      }
      else {
        // Everything needs to be reindexed.
        $ret[] = update_sql("UPDATE {search_dataset} SET reindex = 1");
      }

      // create the new indexes
      db_add_unique_key($ret, 'search_index', 'word_sid_type', array('word', 'sid', 'type'));
      db_add_unique_key($ret, 'search_dataset', 'sid_type', array('sid', 'type'));
    }
  }
  return $ret;
}

/**
 * Create consistent empty region for disabled blocks.
 */
function system_update_6037() {
  $ret = array();
  db_change_field($ret, 'blocks', 'region', 'region', array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''));
  $ret[] = update_sql("UPDATE {blocks} SET region = '' WHERE status = 0");
  return $ret;
}

/**
 * Ensure that "Account" is not used as a Profile category.
 */
function system_update_6038() {
  $ret = array();
  if (db_table_exists('profile_fields')) {
    $ret[] = update_sql("UPDATE {profile_fields} SET category = 'Account settings' WHERE LOWER(category) = 'account'");
    if ($affectedrows = db_affected_rows()) {
      drupal_set_message(t('There were @affectedrows profile fields that used a reserved category name. They have been assigned to the category "Account settings".', array('@affectedrows' => $affectedrows)));
    }
  }
  return $ret;
}

/**
 * Rename permissions "edit foo content" to "edit any foo content".
 * Also update poll module permission "create polls" to "create
 * poll content".
 */
function system_update_6039() {
  $ret = array();
  $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
  while ($role = db_fetch_object($result)) {
    $renamed_permission = preg_replace('/(?<=^|,\ )edit\ ([a-zA-Z0-9_\-]+)\ content(?=,|$)/', 'edit any $1 content', $role->perm);
    $renamed_permission = preg_replace('/(?<=^|,\ )create\ polls(?=,|$)/', 'create poll content', $renamed_permission);
    if ($renamed_permission != $role->perm) {
      $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
    }
  }
  return $ret;
}

/**
 * Add a weight column to the upload table.
 */
function system_update_6040() {
  $ret = array();
  if (db_table_exists('upload')) {
    db_add_field($ret, 'upload', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
  }
  return $ret;
}

/**
 * Change forum vocabulary not to be required by default and set the weight of the forum.module 1 higher than the taxonomy.module.
 */
function system_update_6041() {
  $weight = intval((db_result(db_query("SELECT weight FROM {system} WHERE name = 'taxonomy'"))) + 1);
  $ret = array();
  $vid = intval(variable_get('forum_nav_vocabulary', ''));
  if (db_table_exists('vocabulary') && $vid) {
    $ret[] = update_sql("UPDATE {vocabulary} SET required = 0 WHERE vid = " . $vid);
    $ret[] = update_sql("UPDATE {system} SET weight = ". $weight ." WHERE name = 'forum'");
  }
  return $ret;
}

/**
 * Upgrade recolored theme stylesheets to new array structure.
 */
function system_update_6042() {
  foreach (list_themes() as $theme) {
    $stylesheet = variable_get('color_'. $theme->name .'_stylesheet', NULL);
    if (!empty($stylesheet)) {
      variable_set('color_'. $theme->name .'_stylesheets', array($stylesheet));
      variable_del('color_'. $theme->name .'_stylesheet');
    }
  }
  return array();
}

/**
 * Update table indices to make them more rational and useful.
 */
function system_update_6043() {
  $ret = array();
  // Required modules first.
  // Add new system module indexes.
  db_add_index($ret, 'flood', 'allow', array('event', 'hostname', 'timestamp'));
  db_add_index($ret, 'history', 'nid', array('nid'));
  // Change length of theme field in {blocks} to be consistent with module, and
  // to avoid a MySQL error regarding a too-long index.  Also add new indices.
  db_change_field($ret, 'blocks', 'theme', 'theme', array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''),array(
                  'unique keys' => array('tmd' => array('theme', 'module', 'delta'),),
                  'indexes' => array('list' => array('theme', 'status', 'region', 'weight', 'module'),),));
  db_add_index($ret, 'blocks_roles', 'rid', array('rid'));
  // Improve filter module indices.
  db_drop_index($ret, 'filters', 'weight');
  db_add_unique_key($ret, 'filters', 'fmd', array('format', 'module', 'delta'));
  db_add_index($ret, 'filters', 'list', array('format', 'weight', 'module', 'delta'));
  // Drop unneeded keys form the node table.
  db_drop_index($ret, 'node', 'status');
  db_drop_unique_key($ret, 'node', 'nid_vid');
  // Improve user module indices.
  db_add_unique_key($ret, 'users', 'mail', array('mail'));
  db_add_index($ret, 'users_roles', 'rid', array('rid'));

  // Optional modules - need to check if the tables exist.
  // Alter aggregator module's tables primary keys to make them more useful.
  if (db_table_exists('aggregator_category_feed')) {
    db_drop_primary_key($ret, 'aggregator_category_feed');
    db_add_primary_key($ret, 'aggregator_category_feed', array('cid', 'fid'));
    db_add_index($ret, 'aggregator_category_feed', 'fid', array('fid'));
  }
  if (db_table_exists('aggregator_category_item')) {
    db_drop_primary_key($ret, 'aggregator_category_item');
    db_add_primary_key($ret, 'aggregator_category_item', array('cid', 'iid'));
    db_add_index($ret, 'aggregator_category_item', 'iid', array('iid'));
  }
  // Alter contact module's table to add an index.
  if (db_table_exists('contact')) {
    db_add_index($ret, 'contact', 'list', array('weight', 'category'));
  }
  // Alter locale table to add a primary key, drop an index.
  if (db_table_exists('locales_target')) {
    db_add_primary_key($ret, 'locales_target', array('language', 'lid', 'plural'));
  }
  // Alter a poll module table to add a primary key.
  if (db_table_exists('poll_votes')) {
    db_drop_index($ret, 'poll_votes', 'nid');
    db_add_primary_key($ret, 'poll_votes', array('nid', 'uid', 'hostname'));
  }
  // Alter a profile module table to add a primary key.
  if (db_table_exists('profile_values')) {
    db_drop_index($ret, 'profile_values', 'uid');
    db_drop_index($ret, 'profile_values', 'fid');
    db_change_field($ret,'profile_values' ,'fid', 'fid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,), array('indexes' => array('fid' => array('fid'),)));
    db_change_field($ret,'profile_values' ,'uid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,));
    db_add_primary_key($ret, 'profile_values', array('uid', 'fid'));
  }
  // Alter a statistics module table to add an index.
  if (db_table_exists('accesslog')) {
    db_add_index($ret, 'accesslog', 'uid', array('uid'));
  }
  // Alter taxonomy module's tables.
  if (db_table_exists('term_data')) {
    db_drop_index($ret, 'term_data', 'vid');
    db_add_unique_key($ret, 'term_data', 'vid_name', array('vid', 'name'));
    db_add_index($ret, 'term_data', 'taxonomy_tree', array('vid', 'weight', 'name'));
  }
  if (db_table_exists('term_node')) {
    db_drop_primary_key($ret, 'term_node');
    db_drop_index($ret, 'term_node', 'tid');
    db_add_primary_key($ret, 'term_node', array('tid', 'vid'));
  }
  if (db_table_exists('term_relation')) {
    db_drop_index($ret, 'term_relation', 'tid1');
    db_add_unique_key($ret, 'term_relation', 'tid1_tid2', array('tid1', 'tid2'));
  }
  if (db_table_exists('term_synonym')) {
    db_drop_index($ret, 'term_synonym', 'name');
    db_add_unique_key($ret, 'term_synonym', 'name_tid', array('name', 'tid'));
  }
  if (db_table_exists('vocabulary')) {
    db_add_index($ret, 'vocabulary', 'list', array('weight', 'name'));
  }
  if (db_table_exists('vocabulary_node_types')) {
    db_drop_primary_key($ret, 'vocabulary_node_types');
    db_add_primary_key($ret, 'vocabulary_node_types', array('type', 'vid'));
    db_add_index($ret, 'vocabulary_node_types', 'vid', array('vid'));
  }

  return $ret;
}

/**
 * @} End of "defgroup updates-5.x-to-6.x"
 * The next series of updates should start at 7000.
 */