apachesolr_search.module

Tracking 6.x-3.x branch
  1. drupal
    1. 5 apachesolr_search.module
    2. 6 apachesolr_search.module
    3. 7 apachesolr_search.module

Provides a content search implementation for node content for use with the Apache Solr search application.

Functions & methods

NameDescription
apachesolr_search_add_boost_params
apachesolr_search_add_spellcheck_params
apachesolr_search_apachesolr_entity_info_alterImplements hook_content_extra_fields_alter().
apachesolr_search_apachesolr_environment_deleteImplements hook_apachesolr_environment_delete().
apachesolr_search_basic_params
apachesolr_search_blockImplements hook_block().
apachesolr_search_block_configureImplements hook_block_configure().
apachesolr_search_block_infoRetrieve all possible MLT blocks
apachesolr_search_block_saveImplements hook_block_save().
apachesolr_search_block_viewVew a specific block according to the delta identifier
apachesolr_search_build_spellchecksubmit function for the delete_index form.
apachesolr_search_conditionsThe default conditions callback.
apachesolr_search_conditions_default
apachesolr_search_default_search_pageGet or set the default search page id for the current page.
apachesolr_search_facetapi_realm_settings_form_submitForm submission handler for facetapi_realm_settings_form().
apachesolr_search_form_apachesolr_delete_index_confirm_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_block_admin_display_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_facetapi_realm_settings_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_admin_settings_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_block_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_form_search_submitAdded form submit function to retain filters.
apachesolr_search_form_search_theme_form_alterImplements hook_form_[form_id]_alter().
apachesolr_search_get_search_suggestionsRetrieve all of the suggestions that were given after a certain search
apachesolr_search_get_taxonomy_term_titleUsed as a callback function to generate a title for the taxonomy term depending on the input in the configuration screen
apachesolr_search_get_user_titleUsed as a callback function to generate a title for a user name depending on the input in the configuration screen
apachesolr_search_get_value_titleUsed as a callback function to generate a title for a node/page depending on the input in the configuration screen
apachesolr_search_highlighting_paramsAdd highlighting settings to the search params.
apachesolr_search_initImplements hook_init().
apachesolr_search_load_all_mlt_blocks
apachesolr_search_load_all_search_pagesReturn all the saved search pages
apachesolr_search_load_all_search_typesFunction that loads all the search types
apachesolr_search_menuImplements hook_menu().
apachesolr_search_menu_alter
apachesolr_search_mlt_block_load
apachesolr_search_mlt_suggestionsPerforms a moreLikeThis query using the settings and retrieves documents.
apachesolr_search_node_resultCallback function for node search results.
apachesolr_search_pager_default_initializeBackport of pager_default_initialize from Drupal 7.
apachesolr_search_pager_find_pageBackport of pager_find_page from Drupal 7.
apachesolr_search_page_browseHandle browse results for empty searches.
apachesolr_search_page_cloneFunction that clones a search page
apachesolr_search_page_existsReturns whether a search page exists.
apachesolr_search_page_loadLoad a search page
apachesolr_search_page_save
apachesolr_search_preprocess_search_resultTemplate preprocess for apachesolr search results.
apachesolr_search_preprocess_search_results@todo Make sure the paging works for Drupal 6
apachesolr_search_process_response@todo Make sure the paging works for Drupal 6
apachesolr_search_runExecute a search results based on keyword, filter, and sort strings.
apachesolr_search_run_emptyExecute a search with zero results rows so as to populate facets.
apachesolr_search_searchImplements of hook_search().
apachesolr_search_search_box_form_submitProcess a block search form submission.
apachesolr_search_search_executeHelper function to retrieve results
apachesolr_search_search_pageThe default search page
apachesolr_search_search_page_customMimics apachesolr_search_search_page() but is used for custom search pages We prefer to keep them seperate so we are not dependent from core search when someone tries to disable the core search
apachesolr_search_search_results
apachesolr_search_themeImplements hook_theme().
apachesolr_search_theme_registry_alterImplements hook_theme_registry_alter().
theme_apachesolr_search_browse_blocksShows a groups of blocks for starting a search from a filter.
theme_apachesolr_search_mlt_recommendation_block
theme_apachesolr_search_noresultsBrief message to display when no results match the query.
theme_apachesolr_search_snippetsTheme the highlighted snippet text for a search entry.
theme_apachesolr_search_suggestionsDefault theme function for spelling suggestions.
View source
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
<?php

/**
 * @file
 *   Provides a content search implementation for node content for use with the
 *   Apache Solr search application.
 */

/**
 * Implements hook_init().
 *
 * Checks if we should run an empty facet query so the facet blocks can be
 * displayed.
 */
function apachesolr_search_init() {
  $search_page_id = apachesolr_search_default_search_page();
  $search_page = apachesolr_search_page_load($search_page_id);
  // Do not continue if our search page is not valid
  if (empty($search_page)) {
    return;
  }

  $show_facets = apachesolr_environment_variable_get($search_page->env_id, 'apachesolr_search_show_facets', 0);
  if ($show_facets && module_exists('facetapi')) {

    // Converts current path to lowercase for case insensitive matching.
    $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
    $facet_pages = apachesolr_environment_variable_get($search_page->env_id, 'apachesolr_search_facet_pages', '');

    // Iterates over each environment to check if an empty query should be run.
    if (!empty($facet_pages)) {
      // Compares path with settings, runs query if there is a match.
      $patterns = drupal_strtolower($facet_pages);
      if (drupal_match_path($path, $patterns)) {
        try {
          if (!empty($search_page->search_path)) {
            $solr = apachesolr_get_solr($search_page->env_id);
            // Initializes params for empty query.
            $params = array(
              'spellcheck' => 'false',
              'fq' => array(),
              'rows' => 1,
            );
            apachesolr_search_run_empty('apachesolr', $params, $search_page->search_path, $solr);
          }
        }
        catch (Exception $e) {
          watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
        }
      }
    }
  }
}

/**
 * Implements hook_menu().
 */
function apachesolr_search_menu() {
  $base_path = 'admin/settings/apachesolr/';
  $items[$base_path . 'search-pages'] = array(
    'title'            => 'Pages/Blocks',
    'description'      => 'Configure search pages',
    'page callback'    => 'apachesolr_search_page_list_all',
    'access arguments' => array('administer search'),
    'type'             => MENU_LOCAL_TASK,
    'file'             => 'apachesolr_search.admin.inc',
  );
  $items[$base_path . 'search-pages/add'] = array(
    'title'            => 'Add search page',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('apachesolr_search_page_settings_form'),
    'access arguments' => array('administer search'),
    'weight'           => 1,
    'file'             => 'apachesolr_search.admin.inc',
  );
  $items[$base_path . 'search-pages/%apachesolr_search_page/edit'] = array(
    'title' => 'Edit search page',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('apachesolr_search_page_settings_form', 4),
    'access arguments' => array('administer search'),
    'file'             => 'apachesolr_search.admin.inc',
  );
  $items[$base_path . 'search-pages/%apachesolr_search_page/delete'] = array(
    'title'            => 'Delete search page',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('apachesolr_search_delete_search_page_confirm', 4),
    'access arguments' => array('administer search'),
    'file'             => 'apachesolr_search.admin.inc',
  );
  $items[$base_path . 'search-pages/%apachesolr_search_page/clone'] = array(
    'title' => 'Clone search page',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('apachesolr_search_clone_search_page_confirm', 4),
    'access arguments' => array('administer search'),
    'file'             => 'apachesolr_search.admin.inc',
  );
  $items[$base_path . 'search-pages/addblock'] = array(
    'title'            => 'Add search block "More Like This"',
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('apachesolr_search_mlt_add_block_form'),
    'access arguments' => array('administer search'),
    'weight'           => 2,
    'file'             => 'apachesolr_search.admin.inc',
  );
  $items[$base_path . 'search-pages/block/%apachesolr_search_mlt_block/delete'] = array(
    'page callback'      => 'drupal_get_form',
    'page arguments'     => array('apachesolr_search_mlt_delete_block_form', 5),
    'access arguments'   => array('administer search'),
    'file'               => 'apachesolr_search.admin.inc',
    'type'               => MENU_CALLBACK,
  );

  // Environment specific settings
  $settings_path = 'settings/';
  $items[$base_path . $settings_path . '%apachesolr_environment/bias'] = array(
    'title'            => 'Bias',
    'page callback'    => 'apachesolr_bias_settings_page',
    'page arguments'   => array(4),
    'access arguments' => array('administer search'),
    'weight'           => 4,
    'type'             => MENU_LOCAL_TASK,
    'file'             => 'apachesolr_search.admin.inc',
  );
  return $items;
}

function apachesolr_search_menu_alter(&$items) {
  $search_pages = apachesolr_search_load_all_search_pages();
  // Gets default search information.
  $search_types = apachesolr_search_load_all_search_types();
  // Iterates over search pages, builds menu items.
  foreach ($search_pages as $search_page) {
    $search_page = apachesolr_search_page_load($search_page['page_id']);
    if (!$search_page) {
      continue;
    }

    // Validate the environment ID in case of import or missed deletion.
    $environment = apachesolr_environment_load($search_page->env_id);
    if (!$environment) {
      continue;
    }

    // Parses search path into it's various parts, builds menu items dependent
    // on whether %keys is in the path.
    $parts = explode('/', $search_page->search_path);
    $keys_pos = count($parts);
    // Tests whether we are simulating a core search tab.
    $core_search = ($parts[0] == 'search');
    $taxonomy_search = ($search_page->search_path == 'taxonomy/term/%');
    $position = array_search('%', $parts);
    $page_title = isset($search_page->page_title) ? $search_page->page_title : 'Search Results';

    // Replace possible tokens [term:tid], [node:nid], [user:uid] with their
    // menu-specific variant
    $items[$search_page->search_path] = array(
      'title' => $page_title,
      'page callback' => 'apachesolr_search_custom_page',
      'page arguments' => array($search_page->page_id, '', $position),
      'access arguments' => array('search content'),
      'type' => ($core_search) ? MENU_LOCAL_TASK : MENU_SUGGESTED_ITEM,
      'file' => 'apachesolr_search.pages.inc',
      'file path' => drupal_get_path('module', 'apachesolr_search'),
    );

    $items[$search_page->search_path . '/%menu_tail'] = array(
      'title' => $page_title,
      'load arguments' => array('%map', '%index'),
      'page callback' => 'apachesolr_search_custom_page',
      'page arguments' => array($search_page->page_id, $keys_pos, $position),
      'access arguments' => array('search content'),
      'type' => ($taxonomy_search) ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
      'file' => 'apachesolr_search.pages.inc',
      'file path' => drupal_get_path('module', 'apachesolr_search'),
    );

    // If title has a certain callback for the selected type we use it
    $search_type_id = !empty($search_page->settings['apachesolr_search_search_type']) ? $search_page->settings['apachesolr_search_search_type'] : FALSE;
    $search_type = !empty($search_types[$search_type_id]) ? $search_types[$search_type_id] : FALSE;

    if ($search_type && !empty($position)) {
      $title_callback = $search_type['title callback'];
      $items[$search_page->search_path]['title callback'] = $title_callback;
      $items[$search_page->search_path]['title arguments'] = array($search_page->page_id, $position);
      $items[$search_page->search_path . '/%menu_tail']['title callback'] = $title_callback;
      $items[$search_page->search_path . '/%menu_tail']['title arguments'] = array($search_page->page_id, $position);
    }
    // If we have additional searches in the search/* path
    /*if ($core_search) {
      $items[$search_page->search_path . '/%menu_tail']['tab_root'] = 'search/' . $default_info['path'] . '/%';
      $items[$search_page->search_path . '/%menu_tail']['tab_parent'] = 'search/' . $default_info['path'];
    }*/

    if ($taxonomy_search) {
      unset($items['taxonomy/term/%taxonomy_term']);
      unset($items['taxonomy/term/%taxonomy_term/view']);
    }
  }
}

/**
 * Function that loads all the search types
 *
 * @return array $search_types
 */
function apachesolr_search_load_all_search_types() {
  static $search_types;

  if (isset($search_types)) {
    return $search_types;
  }
  // Use cache_get to avoid DB when using memcache, etc.
  $cache = cache_get('apachesolr_search:search_types', 'cache_apachesolr');
  if (isset($cache->data)) {
    $search_types = $cache->data;
  }
  else {
    $search_types = array(
      'tid' => array(
        'name' => apachesolr_field_name_map('tid'),
        'default menu' => 'taxonomy/term/%',
        'title callback' => 'apachesolr_search_get_taxonomy_term_title',
      ),
      'is_uid' => array(
        'name' => apachesolr_field_name_map('is_uid'),
        'default menu' => 'user/%/search',
        'title callback' => 'apachesolr_search_get_user_title',
      ),
      'bundle' => array(
        'name' => apachesolr_field_name_map('bundle'),
        'default menu' => 'search/type/%',
        'title callback' => 'apachesolr_search_get_value_title',
      ),
      'ss_language' => array(
        'name' => apachesolr_field_name_map('ss_language'),
        'default menu' => 'search/language/%',
        'title callback' => 'apachesolr_search_get_value_title',
      ),
    );
    drupal_alter('apachesolr_search_types', $search_types);
    cache_set('apachesolr_search:search_types', $search_types, 'cache_apachesolr');
  }
  return $search_types;
}

/**
 * Used as a callback function to generate a title for the taxonomy term
 * depending on the input in the configuration screen
 * @param integer $search_page_id
 * @param integer $value
 * @return String
 */
function apachesolr_search_get_taxonomy_term_title($search_page_id = NULL, $value = NULL) {
   $page_title = 'Search results for %value';
   if (isset($value) && isset($search_page_id)) {
     $search_page = apachesolr_search_page_load($search_page_id);
     $page_title = str_replace('%value', '!value', $search_page->page_title);
     $term = taxonomy_term_load($value);
     if (!$term) {
       return;
     }
     $title = $term->name;
   }
   return t($page_title, array('!value' => $title));
}

/**
 * Used as a callback function to generate a title for a user name depending
 * on the input in the configuration screen
 * @param integer $search_page_id
 * @param integer $value
 * @return String
 */
function apachesolr_search_get_user_title($search_page_id = NULL, $value = NULL) {
  $page_title = 'Search results for %value';
  $title = '';
  if (isset($value) && isset($search_page_id)) {
    $search_page = apachesolr_search_page_load($search_page_id);
    $page_title = str_replace('%value', '!value', $search_page->page_title);
    $user = user_load($value);
    if (!$user) {
       return;
     }
    $title = $user->name;
  }
  return t($page_title, array('!value' => $title));
}

/**
 * Used as a callback function to generate a title for a node/page depending
 * on the input in the configuration screen
 * @param integer $search_page_id
 * @param integer $value
 * @return String
 */
function apachesolr_search_get_value_title($search_page_id = NULL, $value = NULL) {
  $page_title = 'Search results for %value';
  if (isset($value)  && isset($search_page_id)) {
    $search_page = apachesolr_search_page_load($search_page_id);
    $page_title = str_replace('%value', '!value', $search_page->page_title);
    $title = $value;
  }
  return t($page_title, array('!value' => $title));
}

/**
 * Get or set the default search page id for the current page.
 */
function apachesolr_search_default_search_page($page_id = NULL, $reset = FALSE) {
  static $default_page_id = NULL;

  if ($reset) {
    $default_page_id = array();
  }
  if (isset($page_id)) {
    $default_page_id = $page_id;
  }
  if (empty($default_page_id)) {
    $default_page_id = variable_get('apachesolr_search_default_search_page', 'core_search');
  }
  return $default_page_id;
}

/**
 * Load a search page
 * @param string $page_id
 * @return array
 */
function apachesolr_search_page_load($page_id, $reset = FALSE) {
  static $pages = array();

  if ($reset) {
    $pages = array();
  }

  if (!isset($pages[$page_id]) && db_table_exists('apachesolr_search_page')) {
      $page = db_fetch_object(db_query("SELECT * FROM {apachesolr_search_page} s WHERE s.page_id = '%s'", array($page_id)));
    if ($page) {
      $page->settings = unserialize($page->settings);
      // Special case page - always uses the default environment.
      if ($page->page_id == 'core_search') {
        $page->env_id = apachesolr_default_environment();
      }
    }
    elseif (module_exists('ctools')) {
      // Try also loading the page from ctools exportables.
      ctools_include('export');
      if ($pages_from_code = ctools_export_load_object('apachesolr_search_page', 'names', array($page_id))) {
        $page = array_shift($pages_from_code);
      }
    }
    $pages[$page_id] = $page;
  }
  return $pages[$page_id];
}

function apachesolr_search_page_save($search_page) {
  if (!empty($search_page) && db_table_exists('apachesolr_search_page')) {
    $query = "INSERT INTO {apachesolr_search_page} (page_id, label, description, env_id, search_path, page_title, settings)
      VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')
      ON DUPLICATE KEY UPDATE page_id = '%s', label = '%s', description = '%s', env_id = '%s', search_path = '%s', page_title = '%s', settings = '%s'";
    $arguments = array(
      $search_page->page_id,
      $search_page->label,
      $search_page->description,
      $search_page->env_id,
      $search_page->search_path,
      $search_page->page_title,
      serialize($search_page->settings),
      $search_page->page_id,
      $search_page->label,
      $search_page->description,
      $search_page->env_id,
      $search_page->search_path,
      $search_page->page_title,
      serialize($search_page->settings),
    );
    db_query($query, $arguments);
  }
}

 /**
 * Function that clones a search page
 *
 * @param $page_id
 *   The page identifier it needs to clone.
 *
 */
function apachesolr_search_page_clone($page_id) {
  $search_page = apachesolr_search_page_load($page_id);
  // Get all search_pages
  $search_pages = apachesolr_search_load_all_search_pages();
  // Create an unique ID
  $new_search_page_id = apachesolr_create_unique_id($search_pages, $search_page->page_id);
  // Set this id to the new search page
  $search_page->page_id = $new_search_page_id;
  $search_page->label = $search_page->label . ' [cloned]';
  // All cloned search pages should be removable
  if (isset($search_page->settings['apachesolr_search_not_removable'])) {
    unset($search_page->settings['apachesolr_search_not_removable']);
  }
  // Save our new search page in the database
  apachesolr_search_page_save($search_page);
}

/**
 * Implements hook_block().
 */
function apachesolr_search_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list' :
      return apachesolr_search_block_info();
      break;
    case 'view' :
      return apachesolr_search_block_view($delta);
      break;
    case 'configure' :
      return apachesolr_search_block_configure($delta);
      break;
    case 'save' :
      return apachesolr_search_block_save($delta, $edit);
      break;
  }
}

/**
 * Retrieve all possible MLT blocks
 */
function apachesolr_search_block_info() {
  // Get all of the moreLikeThis blocks that the user has created
  $blocks = apachesolr_search_load_all_mlt_blocks();
  foreach ($blocks as $delta => $settings) {
    $blocks[$delta] += array(
      'info' => t('Apache Solr recommendations: !name', array('!name' => $settings['name'])));
  }
  // Add the sort block.
  $blocks['sort'] = array(
    'info' => t('Apache Solr Core: Sorting'),
  );
  return $blocks;
}

/**
 * Vew a specific block according to the delta identifier
 * @param string $delta
 * @return string
 */
function apachesolr_search_block_view($delta = '') {
  if ($delta != 'sort' && ($node = menu_get_object()) && (!arg(2) || arg(2) == 'view')) {
    $suggestions = array();
    // Determine whether the user can view the current node. Probably not necessary.
    $block = apachesolr_search_mlt_block_load($delta);
    if ($block && node_access('view', $node)) {
      // Get our specific environment for the MLT block
      $env_id = (!empty($block['mlt_env_id'])) ? $block['mlt_env_id'] : '';
      try {
        $solr = apachesolr_get_solr($env_id);
        $docs = apachesolr_search_mlt_suggestions($block, apachesolr_document_id($node->nid), $solr);
        if (!empty($docs)) {
          $suggestions['subject'] = check_plain($block['name']);
          $suggestions['content'] = theme('apachesolr_search_mlt_recommendation_block', $docs, $delta);
        }
      }
      catch (Exception $e) {
        watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
      }
    }
    return $suggestions;
  }
}

/**
 * Implements hook_form_[form_id]_alter().
 */
function apachesolr_search_form_block_admin_display_form_alter(&$form) {
  foreach ($form as $key => $block) {
    if (isset($block['delta']['#value'])) {
      if ((strpos($key, "apachesolr_search_mlt-") === 0) && $block['module']['#value'] == 'apachesolr_search') {
        $form['blocks'][$key]['delete'] = array(
          '#type' => 'link',
          '#title' => 'delete',
          '#href' => 'admin/settings/apachesolr/search-pages/block/' . $block['delta']['#value'] . '/delete',
        );
      }
    }
  }
}

/**
 * Implements hook_block_configure().
 */
function apachesolr_search_block_configure($delta = '') {
  if ($delta != 'sort') {
    require_once(drupal_get_path('module', 'apachesolr') . '/apachesolr_search.admin.inc');
    return apachesolr_search_mlt_block_form($delta);
  }
}

/**
 * Implements hook_block_save().
 */
function apachesolr_search_block_save($delta = '', $edit = array()) {
  if ($delta != 'sort') {
    require_once(drupal_get_path('module', 'apachesolr') . '/apachesolr_search.admin.inc');
    apachesolr_search_mlt_save_block($edit, $delta);
  }
}


/**
 * Return all the saved search pages
 * @return array $search_pages
 *   Array of all search pages
 */
function apachesolr_search_load_all_search_pages() {
  // Get all search_pages and their id
  if (db_table_exists('apachesolr_search_page')) {
    $search_pages = array();
    $result = db_query('SELECT * FROM {apachesolr_search_page}');
    while($search_page = db_fetch_array($result)) {
      $search_pages[$search_page['page_id']] = $search_page;
    }
    // If ctools module is enabled, add search pages from code, e.g. from a
    // feature module.
    if (module_exists('ctools')) {
      ctools_include('export');
      $defaults = ctools_export_load_object('apachesolr_search_page', 'all');
      foreach ($defaults as $page_id => $default) {
        if (!isset($search_pages[$page_id])) {
          $search_pages[$page_id] = (array) $default;
        }
      }
    }
    return $search_pages;
  }
  return array();
}

function apachesolr_search_load_all_mlt_blocks() {
  $search_blocks = variable_get('apachesolr_search_mlt_blocks', array());
  return $search_blocks;
}

function apachesolr_search_mlt_block_load($block_id) {
  $search_blocks = variable_get('apachesolr_search_mlt_blocks', array());
  return isset($search_blocks[$block_id]) ? $search_blocks[$block_id] : FALSE;
}

/**
 * Performs a moreLikeThis query using the settings and retrieves documents.
 *
 * @param $settings
 *   An array of settings.
 * @param $id
 *   The Solr ID of the document for which you want related content.
 *   For a node that is apachesolr_document_id($node->nid)
 * @param $solr
 *   The solr environment you want to query against
 *
 * @return An array of response documents, or NULL
 */
function apachesolr_search_mlt_suggestions($settings, $id, $solr = NULL) {

  try {
    $fields = array(
      'mlt_mintf' => 'mlt.mintf',
      'mlt_mindf' => 'mlt.mindf',
      'mlt_minwl' => 'mlt.minwl',
      'mlt_maxwl' => 'mlt.maxwl',
      'mlt_maxqt' => 'mlt.maxqt',
      'mlt_boost' => 'mlt.boost',
      'mlt_qf' => 'mlt.qf',
    );
    $params = array(
      'q' => 'id:' . $id,
      'qt' => 'mlt',
      'fl' => array('entity_id', 'entity_type', 'label', 'path', 'url'),
      'mlt.fl' => $settings['mlt_fl'],
      'start' => 0,
      'rows' => $settings['num_results'],
    );
    // We can optionally specify a Solr object.
    $query = apachesolr_drupal_query('apachesolr_mlt', $params, '', '', $solr);

    foreach ($fields as $form_key => $name) {
      if (!empty($settings[$form_key])) {
        $query->addParam($name, $settings[$form_key]);
      }
    }

    $type_filters = array();
    if (is_array($settings['mlt_type_filters']) && !empty($settings['mlt_type_filters'])) {
      $query->addFilter('bundle', '(' . implode(' OR ', $settings['mlt_type_filters']) . ') ');
    }

    if ($custom_filters = $settings['mlt_custom_filters']) {
      // @todo - fix the settings form to take a comma-delimited set of filters.
      $query->addFilter('', $custom_filters);
    }

    // This hook allows modules to modify the query object.
    drupal_alter('apachesolr_query', $query);
    if ($query->abort_search) {
      return;
    }

    $response = $query->search();

    if ($response->response) {
      $docs = (array) end($response->response);
      return $docs;
    }
  }
  catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  }
}

function theme_apachesolr_search_mlt_recommendation_block($vars) {
  $docs = $vars;
  $links = array();
  foreach ($docs as $result) {
    // Suitable for single-site mode. Label is already safe.
    $links[] = l($result->label, $result->path, array('html' => TRUE));
  }
  $links = theme('item_list', $links);
  return $links;
}

/**
 * Implements of hook_search().
 */
function apachesolr_search_search($op = 'search', $keys = NULL) {
  module_load_include('inc', 'apachesolr', 'apachesolr.index');
  switch ($op) {
    case 'reset':
      $env_id = apachesolr_default_environment();
      apachesolr_index_mark_for_reindex($env_id);
      return;

    case 'status':
      $env_id = apachesolr_default_environment();
      return apachesolr_index_status($env_id);

/*    case 'search':
      $conditions_callback = variable_get('apachesolr_search_conditions_callback', 'apachesolr_search_conditions');
      $conditions = call_user_func($conditions_callback);
      try {
        $results = apachesolr_search_search_execute($keys, $conditions);
        return $results;
      }
      catch (Exception $e) {
        watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
        apachesolr_failure(t('Solr search'), $keys);
      }
      break;
 */
  } // switch
}

/**
 * Implements hook_form_[form_id]_alter().
 *
 * This adds the 0 option to the search admin form.
 */
function apachesolr_search_form_search_admin_settings_alter(&$form, $form_state) {
  $form['indexing_throttle']['search_cron_limit']['#options']['0'] = '0';
  ksort($form['indexing_throttle']['search_cron_limit']['#options']);
}

/**
 * Implements hook_form_[form_id]_alter().
 */
function apachesolr_search_form_search_theme_form_alter(&$form, $form_state) {
  apachesolr_search_form_search_block_form_alter($form, $form_state);
}

/**
 * Implements hook_form_[form_id]_alter().
 */
function apachesolr_search_form_search_block_form_alter(&$form, &$form_state) {
  $search_pages = apachesolr_search_load_all_search_pages();
  $redirect_to = variable_get('apachesolr_search_default_search_page', 'drupal_core');
  if ($redirect_to == 'drupal_core') {
    return;
  }

  if (!empty($search_pages[$redirect_to])) {
    $form['#action'] = url($search_pages[$redirect_to]['search_path']);
    if (!isset($form['#submit'])) {
      $form['#submit'] = array('apachesolr_search_search_box_form_submit');
    }
    else {
      $key = array_search('search_box_form_submit', $form['#submit']);
      if ($key !== FALSE) {
        // Replace the search module's function.
        $form['#submit'][$key] = 'apachesolr_search_search_box_form_submit';
      }
    }
  }
}

/**
 * Process a block search form submission.
 *
 * @see search_box_form_submit()
 */
function apachesolr_search_search_box_form_submit($form, &$form_state) {
  // The search form relies on control of the redirect destination for its
  // functionality, so we override any static destination set in the request,
  // for example by drupal_access_denied() or drupal_not_found()
  // (see http://drupal.org/node/292565).
  if (isset($_REQUEST['destination'])) {
    unset($_REQUEST['destination']);
  }
  if (isset($_REQUEST['edit']['destination'])) {
    unset($_REQUEST['edit']['destination']);
  }

  $form_id = $form['form_id']['#value'];
  $keys = $form_state['values'][$form_id];
  // Handle Apache webserver clean URL quirks.
  if (variable_get('clean_url', '0')) {
    $keys = str_replace('+', '%2B', $keys);
  }
  $path = $form['#action'];
  // Remove leading slash
  ($path{0} == "/" ) ? $path = substr($path, 1) : '';
  // Set our path to the correct search page
  $form_state['redirect'] = $path . '/' .trim($keys);
}


/**
 * Helper function to retrieve results
 * @param $keys
 *   The keys that are available after the path that is defined in
 *   hook_search_info
 * @param $conditions
 *   Conditions that are coming from apachesolr_search_conditions
 */
function apachesolr_search_search_execute($keys = NULL, $conditions = NULL) {
  $search_page = apachesolr_search_page_load('core_search');
  $results = apachesolr_search_search_results($keys, $conditions, $search_page);
  return $results;
}

/**
 * The default conditions callback.
 */
function apachesolr_search_conditions() {
  //get default conditions from the core_search
  $search_page = apachesolr_search_page_load('core_search');
  $conditions = apachesolr_search_conditions_default($search_page);
  return $conditions;
}

/**
 * The default search page
 * @param $results
 *   The results that came from apache solr
 */
function apachesolr_search_search_page($results) {
  $search_page = apachesolr_search_page_load('core_search');
  $build = apachesolr_search_search_page_custom($results, $search_page);
  return $build;
}

/**
 * Mimics apachesolr_search_search_page() but is used for custom search pages
 * We prefer to keep them seperate so we are not dependent from core search
 * when someone tries to disable the core search
 * @param $results
 *   The results that came from apache solr
 * @param $build
 *   the build array from where this function was called. Good to append output
 *   to the build array
 * @param $search_page
 *   the search page that is requesting an output
 */
function apachesolr_search_search_page_custom($results, $search_page, $build = array()) {
  // Retrieve suggestion
  $suggestions = apachesolr_search_get_search_suggestions($search_page->env_id);
  if ($search_page && !empty($suggestions)) {
    $build['suggestions'] = theme('apachesolr_search_suggestions', array(l($suggestions[0], $search_page->search_path . '/' . $suggestions[0])));
  }
  // Retrieve expected results from searching
  if (!empty($results['apachesolr_search_browse'])) {
    // Show facet browsing blocks.
    $build['search_results'] = apachesolr_search_page_browse($results['apachesolr_search_browse'], $search_page->env_id);
  }
  elseif ($results) {
    $build['search_results'] = theme('search_results', $results, 'apachesolr_search', $search_page);
  }
  else {
    // Give the user some custom help text.
    $build['search_results'] = theme('apachesolr_search_noresults');
  }

  // Allows modules to alter the render array before returning.
  drupal_alter('apachesolr_search_page', $build, $search_page);
  return $build;
}

/*
 * Executes search depending on the conditions given.
 * See apachesolr_search.pages.inc for another use of this function
 */
function apachesolr_search_search_results($keys = NULL, $conditions = NULL, $search_page = NULL) {
  $params = array();
  $results = array();
  // Process the search form. Note that if there is $_POST data,
  // search_form_submit() will cause a redirect to search/[module path]/[keys],
  // which will get us back to this page callback. In other words, the search
  // form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle.
  if (empty($_POST['form_id'])
    || ($_POST['form_id'] != 'apachesolr_search_custom_page_search_form')
    && ($_POST['form_id'] != 'search_form')
    && ($_POST['form_id'] != 'search_block_form') ) {
    // Check input variables
    if (empty($search_page)) {
      $search_page = apachesolr_search_page_load('core_search');
      // Verify if it actually loaded
      if (empty($search_page)) {
        // Something must have been really messed up.
        apachesolr_failure(t('Solr search'), $keys);
      }
    }
    if (empty($conditions)) {
      $conditions = apachesolr_search_conditions_default($search_page);
    }

    // Sort options from the conditions array.
    // @see apachesolr_search_conditions_default()
    //   See This condition callback to find out how.
    $solrsort = isset($conditions['apachesolr_search_sort']) ? $conditions['apachesolr_search_sort'] : '';
    // What to do when we have an initial empty search
    $empty_search_behavior = isset($search_page->settings['apachesolr_search_browse']) ? $search_page->settings['apachesolr_search_browse'] : '';

    $params = array();
    $results = array();
    try {
      $solr = apachesolr_get_solr($search_page->env_id);
      // Default parameters
      $params['fq'] = isset($conditions['fq']) ? $conditions['fq'] : array();
      $params['rows'] = $search_page->settings['apachesolr_search_per_page'];

      if (empty($search_page->settings['apachesolr_search_spellcheck'])) {
        // Spellcheck needs to have a string as false/true
        $params['spellcheck'] = 'false';
      }
      else {
        $params['spellcheck'] = 'true';
      }

      // Empty text Behavior
      if (!$keys && !isset($conditions['f']) && ($empty_search_behavior == 'browse' || $empty_search_behavior == 'blocks')) {
        // Pass empty search behavior as string on to apachesolr_search_search_page()
        // Hardcoded apachesolr name since we rely on this for the facets
        apachesolr_search_run_empty('apachesolr', $params, $search_page->search_path, $solr);
        $results['apachesolr_search_browse'] = $empty_search_behavior;

        if ($empty_search_behavior == 'browse') {
          // Hide sidebar blocks for content-area browsing instead.
          apachesolr_suppress_blocks($search_page->env_id, TRUE);
        }
      }
      // Full text behavior. Triggers with a text search or a facet
      elseif (($keys || isset($conditions['f'])) || ($empty_search_behavior == 'results')) {
        $params['q'] = $keys;
        // Hardcoded apachesolr name since we rely on this for the facets
        $page = isset($_GET['page']) ? $_GET['page'] : '';
        $page = check_plain($page);
        $results = apachesolr_search_run('apachesolr', $params, $solrsort, $search_page->search_path, $page, $solr);
      }
    }
    catch (Exception $e) {
      watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
      apachesolr_failure(t('Solr search'), $keys);
    }
  }
  return $results;
}

function apachesolr_search_conditions_default($search_page) {
  $conditions = array();
  $search_type = isset($search_page->settings['apachesolr_search_search_type']) ? $search_page->settings['apachesolr_search_search_type'] : '';
  $allow_user_input = isset($search_page->settings['apachesolr_search_allow_user_input']) ? $search_page->settings['apachesolr_search_allow_user_input'] : FALSE;
  $path_replacer = isset($search_page->settings['apachesolr_search_path_replacer']) ? $search_page->settings['apachesolr_search_path_replacer'] : '';
  $set_custom_filter = isset($search_page->settings['apachesolr_search_custom_enable']) ? $search_page->settings['apachesolr_search_custom_enable'] : '';
  $search_page_fq = !empty($search_page->settings['fq']) ? $search_page->settings['fq'] : '';

  $conditions['fq'] = array();
  // We only allow this to happen if the search page explicitely allows it
  if ($allow_user_input) {
    // Get the filterQueries from the url
    if (!empty($_GET['fq']) && is_array($_GET['fq'])) {
      // Reset the array so that we have one level lower to go through
      $conditions['fq'] = $_GET['fq'];
    }
    foreach($conditions['fq'] as $condition_id => $condition) {
      // If the user input does not pass our validation we do not allow
      // it to query solr
      $test_query = apachesolr_drupal_subquery('Test');
      if (!$test_query->validFilterValue($condition)) {
        unset($conditions['fq'][$condition_id]);
      }
    }
  }

  // Custom filters added in search pages
  if (!empty($search_page_fq) && !empty($set_custom_filter)) {
    if (!empty($path_replacer)) {
      // If the manual filter has a % in it, replace it with $value
      $conditions['fq'][] = str_replace('%', $path_replacer, $search_page_fq);
    }
    else {
      // Put the complete filter in the filter query
      $conditions['fq'][] = $search_page_fq;
    }
  }

  // Search type filters (such as taxonomy)
  if (!empty($path_replacer) && !empty($search_type) && $search_type != 'custom') {
    $conditions['fq'][] = $search_type . ':' . $path_replacer;
  }

  // We may also have filters added by facet API module. The 'f'
  // is determined by constant FacetapiAdapter::FILTER_KEY. Hard
  // coded here to avoid extra class loading.
  if (!empty($_GET['f']) && is_array($_GET['f'])) {
    if (module_exists('facetapi')) {
      $conditions['f'] = $_GET['f'];
    }
  }
  // Add the sort from the page to our conditions
  $sort = isset($_GET['solrsort']) ? $_GET['solrsort'] : '';
  $conditions['apachesolr_search_sort'] = $sort;
  return $conditions;
}

/**
 * Handle browse results for empty searches.
 */
function apachesolr_search_page_browse($empty_search_behavior, $env_id) {
  $build = array();
  // Switch in case we come up with new flags.
  switch ($empty_search_behavior) {
    case 'browse':
      if (module_exists('facetapi') && $query = apachesolr_current_query($env_id)) {
        module_load_include('inc', 'facetapi', 'facetapi.block');
        // Get facet render elements.
        $searcher = $query->getSearcher();
        $elements = facetapi_build_realm($searcher, 'block');
        $blocks = array();
        $build_results = array();

        foreach (element_children($elements) as $key) {
          $delta = "facetapi_{$key}";
          // @todo: order/filter these pseudo-blocks according to block.module weight, visibility (see 7.x-1beta4)
          $block = new stdClass();
          $block->visibility = TRUE;
          $block->enabled = TRUE;
          $block->module = 'facetapi';
          $block->subject = theme('facetapi_title', array('title' => $elements[$key]['#title']));
          //$build[$delta] = $elements[$key];
          $block->region = NULL;
          $block->delta = 'apachesolr-' . $key;
          $block->content = $elements[$key][$key];
          if (!empty($block->content)) {
            $blocks[$delta] = $block;
          }
        }
      }
      break;
  }

  return theme('apachesolr_search_browse_blocks', $blocks);
}

/**
 * Shows a groups of blocks for starting a search from a filter.
 */
function theme_apachesolr_search_browse_blocks($blocks) {
  $result = "<div class='apachesolr_browse_block'><h2>" . t('Browse available categories') .'</h2>';
  $result .= '<p>' . t('Pick a category to launch a search.') . '</p>';
  foreach ($blocks as $facet_field => $block) {
    $result .= theme('block', $block);
  }
  $result .= '</div>';
  return $result;
}

/**
 * Execute a search with zero results rows so as to populate facets.
 */
function apachesolr_search_run_empty($name, array $params = array(), $base_path = '', $solr) {
  $query = apachesolr_drupal_query($name, $params, '', $base_path, $solr);
  $query->addParam('rows', '0');
  $solr_id = $query->solr('getId');
  list($final_query, $response) = apachesolr_do_query($query);
  apachesolr_has_searched($solr_id, TRUE);
}

/**
 * Execute a search results based on keyword, filter, and sort strings.
 *
 * @param $name
 * @param $params
 *   Array - 'q' is the keywords to search.
 * @param $solrsort
 * @param $base_path
 *   For constructing filter and sort links. Leave empty unless the links need to point somewhere
 *   other than the base path of the current request.
 * @param integer $page
 *   For pagination.
 * @param $caller
 *
 * @return stdClass $response
 *
 * @throws Exception
 */
function apachesolr_search_run($name, array $params = array(), $solrsort = '', $base_path = '', $page = 0, $solr = NULL) {
  // This is the object that knows about the query coming from the user.
  $query = apachesolr_drupal_query($name, $params, $solrsort, $base_path, $solr);
  apachesolr_search_basic_params($query);

  if ($query->getParam('q')) {
    apachesolr_search_add_spellcheck_params($query);
  }

  // Add custom params after we add the default params
  $query->addParams($params);

  // Add the paging parameters
  $query->page = $page;

  apachesolr_search_add_boost_params($query);
  if ($query->getParam('q')) {
    apachesolr_search_highlighting_params($query);
    if (!$query->getParam('hl.fl')) {
      $qf = array();
      foreach ($query->getParam('qf') as $field) {
        // Truncate off any boost so we get the simple field name.
        $parts = explode('^', $field, 2);
        $qf[$parts[0]] = TRUE;
      }
      foreach (array('content', 'ts_comments') as $field) {
        if (isset($qf[$field])) {
          $query->addParam('hl.fl', $field);
        }
      }
    }
  }
  else {
    // No highlighting, use the teaser as a snippet.
    $query->addParam('fl', 'teaser');
  }

  list($final_query, $response) = apachesolr_do_query($query, $page);
  $solr_id = $query->solr('getId');
  apachesolr_has_searched($solr_id, TRUE);
  return apachesolr_search_process_response($response, $final_query);
}

function apachesolr_search_basic_params(DrupalSolrQueryInterface $query = NULL) {
  $params = array(
    'fl' => array(
        'id',
        'entity_id',
        'entity_type',
        'bundle',
        'bundle_name',
        'label',
        'is_comment_count',
        'ds_created',
        'ds_changed',
        'score',
        'path',
        'url',
        'is_uid',
        'tos_name',
      ),
    'rows' => 10,
  );
  if ($query) {
    $query->addParams($params);
  }
  return $params;
}

/**
 * Add highlighting settings to the search params.
 *
 * These settings are set in solrconfig.xml.
 * See the defaults there.
 * If you wish to override them, you can via settings.php or drush
 */
function apachesolr_search_highlighting_params(DrupalSolrQueryInterface $query = NULL) {
  $params['hl'] = variable_get('apachesolr_hl_active', NULL);
  $params['hl.fragsize']= variable_get('apachesolr_hl_textsnippetlength', NULL);
  $params['hl.simple.pre'] = variable_get('apachesolr_hl_pretag', NULL);
  $params['hl.simple.post'] = variable_get('apachesolr_hl_posttag', NULL);
  $params['hl.snippets'] = variable_get('apachesolr_hl_numsnippets', NULL);
  // This should be an array of possible field names.
  $params['hl.fl'] = variable_get('apachesolr_hl_fieldtohighlight', NULL);
  $params = array_filter($params);
  if ($query) {
    $query->addParams($params);
  }
  return $params;
}

function apachesolr_search_add_spellcheck_params(DrupalSolrQueryInterface $query) {
  $params = array();

  // Add new parameter to the search request
  $params['spellcheck.q'] = $query->getParam('q');
  $params['spellcheck'] = 'true';
  $query->addParams($params);
}

function apachesolr_search_add_boost_params(DrupalSolrQueryInterface $query) {
  $env_id = $query->solr('getId');
  $params = array();

  $defaults = array(
    'content' => '1.0',
    'ts_comments' => '0.5',
    'tos_content_extra' => '0.1',
    'label' => '5.0',
    'tos_name' => '3.0',
    'taxonomy_names' => '2.0',
    'tags_h1' => '5.0',
    'tags_h2_h3' => '3.0',
    'tags_h4_h5_h6' => '2.0',
    'tags_inline' => '1.0',
    'tags_a' => '0',
  );
  $qf = apachesolr_environment_variable_get($env_id, 'field_bias', $defaults);
  $fields = $query->solr('getFields');
  if ($qf && $fields) {
    foreach ($fields as $field_name => $field) {
      if (!empty($qf[$field_name])) {
        $prefix = substr($field_name, 0, 3);
        if ($field_name == 'content' || $prefix == 'ts_' || $prefix == 'tm_') {
          // Normed fields tend to have a lower score. Multiplying by 40 is
          // a rough attempt to bring the score in line with fields that are
          // not normed.
          $qf[$field_name] *= 40.0;
        }
        $params['qf'][$field_name] = $field_name . '^' . $qf[$field_name];
      }
    }
  }

  $date_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_date_boost', '0:0');
  $comment_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_comment_boost', '0:0');
  $changed_settings = apachesolr_environment_variable_get($env_id, 'apachesolr_search_changed_boost', '0:0');
  $sticky_boost = apachesolr_environment_variable_get($env_id, 'apachesolr_search_sticky_boost', '0');
  $promote_boost = apachesolr_environment_variable_get($env_id, 'apachesolr_search_promote_boost', '0');
  // For the boost functions for the created timestamp, etc we use the
  // standard date-biasing function, as suggested (but steeper) at
  // http://wiki.apache.org/solr/SolrRelevancyFAQ#How_can_I_boost_the_score_of_newer_documents
  // ms() returns the time difference in ms between now and the date
  // The function is thus: $ab/(ms(NOW,date)*$steepness + $ab).
  list($date_steepness, $date_boost) = explode(':', $date_settings);
  if ($date_boost) {
    $ab = 4 / $date_steepness;
    $params['bf'][] = "recip(ms(NOW,ds_created),3.16e-11,$ab,$ab)^$date_boost";
  }
  // Boost on comment count.
  list($comment_steepness, $comment_boost) = explode(':', $comment_settings);
  if ($comment_boost) {
    $params['bf'][] = "recip(div(1,max(is_comment_count,1)),$comment_steepness,10,10)^$comment_boost";
  }
  // Boost for a more recent comment or node edit.
  list($changed_steepness, $changed_boost) = explode(':', $changed_settings);
  if ($changed_boost) {
    $ab = 4 / $changed_steepness;
    $params['bf'][] = "recip(ms(NOW,ds_created),3.16e-11,$ab,$ab)^$changed_boost";
  }
  // Boost for nodes with sticky bit set.
  if ($sticky_boost) {
    $params['bq'][] = "bs_sticky:true^$sticky_boost";
  }
  // Boost for nodes with promoted bit set.
  if ($promote_boost) {
    $params['bq'][] = "bs_promote:true^$promote_boost";
  }
  // Modify the weight of results according to the node types.
  $type_boosts = apachesolr_environment_variable_get($env_id, 'apachesolr_search_type_boosts', array());
  if (!empty($type_boosts)) {
    foreach ($type_boosts as $type => $boost) {
      // Only add a param if the boost is != 0 (i.e. > "Normal").
      if ($boost) {
        $params['bq'][] = "bundle:$type^$boost";
      }
    }
  }
  $query->addParams($params);
}

/**
 *
 * @param type $response
 * @param DrupalSolrQueryInterface $query
 * @return array
 * @todo Make sure the paging works for Drupal 6
 */
function apachesolr_search_process_response($response, DrupalSolrQueryInterface $query) {
  $results = array();
  // We default to getting snippets from the body content and comments.
  $hl_fl = $query->getParam('hl.fl');
  if (!$hl_fl) {
    $hl_fl = array('content', 'ts_comments');
  }
  $total = $response->response->numFound;
  apachesolr_search_pager_default_initialize($total, $query->getParam('rows'));
  if ($total > 0) {
    $fl = $query->getParam('fl');
    // 'id' and 'entity_type' are the only required fields in the schema, and
    // 'score' is generated by solr.
    foreach ($response->response->docs as $doc) {
      $extra = array();

      // Start with an empty snippets array.
      $snippets = array();

      // Find the nicest available snippet.
      foreach ($hl_fl as $hl_param) {
        if (isset($response->highlighting->{$doc->id}->$hl_param)) {
          // Merge arrays preserving keys.
          foreach ($response->highlighting->{$doc->id}->$hl_param as $values) {
            $snippets[$hl_param] = $values;
          }
        }
      }
      // If there's no snippet at this point, add the teaser.
      if (!$snippets) {
        if (isset($doc->teaser)) {
          $snippets[] = truncate_utf8($doc->teaser, 256, TRUE);
        }
      }

      $hook = 'apachesolr_search_snippets';
      $snippet = theme($hook, array('doc' => $doc, 'snippets' => $snippets));

      if (!isset($doc->content)) {
        $doc->content = $snippet;
      }

      // Normalize common dates so that we can use Drupal's normal date and
      // time handling.
      if (isset($doc->ds_created)) {
        $doc->created = strtotime($doc->ds_created);
      }
      else {
        $doc->created = NULL;
      }

      if (isset($doc->ds_changed)) {
        $doc->changed = strtotime($doc->ds_changed);
      }
      else {
        $doc->changed = NULL;
      }

      if (isset($doc->tos_name)) {
        $doc->name = $doc->tos_name;
      }
      else {
        $doc->name = NULL;
      }

      $extra = array();

      // Allow modules to alter each document and its extra information.
      drupal_alter('apachesolr_search_result', $doc, $extra, $query);

      // Set all expected fields from fl to NULL if they are missing so
      // as to prevent Notice: Undefined property.
      foreach ($fl as $field) {
        if (!isset($doc->{$field})) {
          $doc->{$field} = NULL;
        }
      }

      $fields = (array) $doc;

      $result = array(
        // link is a required field, so handle it centrally.
        'link' => url($doc->path, array('absolute' => TRUE)),
        // template_preprocess_search_result() runs check_plain() on the title
        // again.  Decode to correct the display.
        'title' => htmlspecialchars_decode($doc->label, ENT_QUOTES),
        // These values are not required by the search module but are provided
        // to give entity callbacks and themers more flexibility.
        'score' => $doc->score,
        'snippets' => $snippets,
        'snippet' => $snippet,
        'fields' => $fields,
        'entity_type' => $doc->entity_type,
        'bundle' => $doc->bundle,
      );

      // Call entity-type-specific callbacks for extra handling.
      $function = apachesolr_entity_get_callback($doc->entity_type, 'result callback');
      if (is_callable($function)) {
        $function($doc, $result, $extra);
      }

      $result['extra'] = $extra;

      $results[] = $result;
    }

    // Hook to allow modifications of the retrieved results
    foreach (module_implements('apachesolr_process_results') as $module) {
      $function = $module . '_apachesolr_process_results';
      $function($results, $query);
    }
  }
  return $results;
}

/**
 * Retrieve all of the suggestions that were given after a certain search
 * @return array()
 */
function apachesolr_search_get_search_suggestions($env_id) {
  $suggestions_output = array();
  if (apachesolr_has_searched($env_id)) {
    $query = apachesolr_current_query($env_id);
    $keyword = $query->getParam('q');
    $searcher = $query->getSearcher();
    $response = apachesolr_static_response_cache($searcher);
    // Get spellchecker suggestions into an array.
    if (!empty($response->spellcheck->suggestions)) {
      $suggestions = get_object_vars($response->spellcheck->suggestions);
      if ($suggestions) {
        $replacements = array();
        // Get the original query and retrieve all words with suggestions.
        foreach ($suggestions as $word => $value) {
          $replacements[$word] = $value->suggestion[0];
        }
        // Replace the keyword with the suggested keyword.
        $suggested_keyword = strtr($keyword, $replacements);
        // Show only if suggestion is different than current query.
        if ($keyword != $suggested_keyword) {
          $suggestions_output[] = $suggested_keyword;
        }
      }
    }
  }
  return $suggestions_output;
}

/**
 * Implements hook_content_extra_fields_alter().
 */
function apachesolr_search_apachesolr_entity_info_alter(&$entity_info) {
  // Now set those values that we know.  Other modules can do so
  // for specific content types.
  $entity_info['node']['result callback'] = 'apachesolr_search_node_result';
}

/**
 * Callback function for node search results.
 *
 * @param stdClass $doc
 *   The result document from Apache Solr.
 * @param array $result
 *   The result array for this record to which to add.
 */
function apachesolr_search_node_result($doc, &$result, &$extra) {
  $doc->uid = $doc->is_uid;
  $result += array(
    'type' => $doc->bundle,
    'user' => theme('username', $doc),
    'date' => isset($doc->changed) ? $doc->changed : 0,
    'node' => $doc,
    'uid' => $doc->is_uid,
  );

  if (isset($doc->is_comment_count)) {
    $extra['comments'] = format_plural($doc->is_comment_count, '1 comment', '@count comments');
  }
}

/**
 * Returns whether a search page exists.
 */
function apachesolr_search_page_exists($page_id) {
  if (db_table_exists('apachesolr_search_page')) {
    $page = db_result(db_query("SELECT 1 FROM {apachesolr_search_page} WHERE page_id = '%s'", array($page_id)));
  }
  return 0;
}

/**
 * Template preprocess for apachesolr search results.
 *
 * We need to add additional entity/bundle-based templates
 */
function apachesolr_search_preprocess_search_result(&$variables) {
  // If this search result is coming from our module, we want to improve the
  // template potential to make life easier for themers.
  if ($variables['type'] == 'apachesolr_search') {
    $result = $variables['result'];
    $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['type'] . '__' . $result['entity_type'] . '__' . $result['bundle'];
  }
}

/**
 *
 * @param type $variables
 * @return type
 * @todo Make sure the paging works for Drupal 6
 */
function apachesolr_search_preprocess_search_results(&$variables) {
  // Initialize variables
  $env_id = NULL;

  // If this is a solr search, expose more data to themes to play with.
  if ($variables['type'] == 'apachesolr_search') {
    // Fetch our current query
    if (!empty($variables['search_page']->env_id)) {
      $env_id = $variables['search_page']->env_id;
    }
    $query = apachesolr_current_query($env_id);

    if ($query) {
      $variables['query'] = $query;
      $variables['response'] = apachesolr_static_response_cache($query->getSearcher());
    }
    if (empty($variables['response'])) {
      $variables['description'] = '';
      return;
    }
    $total = $variables['response']->response->numFound;
    $params = $variables['query']->getParams();

    $variables['description'] = t('Showing items @start through @end of @total.', array(
      '@start' => $params['start'] + 1,
      '@end' => $params['start'] + $params['rows'] - 1,
      '@total' => $total,
    ));
    // Redefine the pager if it was missing
    //pager_default_initialize($total, $params['rows']);
    $variables['pager'] = theme('pager');
  }
}

/**
 * Implements hook_apachesolr_environment_delete().
 */
function apachesolr_search_apachesolr_environment_delete($server) {
  if (db_table_exists('apachesolr_search_page')) {
    $query = "UPDATE {apachesolr_search_page} SET env_id = '' WHERE env_id = '%s'";
    db_query($query, array($server['env_id']));

    apachesolr_environment_variable_del($server['env_id'], 'apachesolr_search_show_facets');
    apachesolr_environment_variable_del($server['env_id'], 'apachesolr_search_facet_pages');
    menu_rebuild();
  }
}


/**
 * Implements hook_form_[form_id]_alter().
 *
 * This adds the retain filters to the search form.
 */
function apachesolr_search_form_search_form_alter(&$form, $form_state) {
  if (!empty($form['module']['#value']) && $form['module']['#value'] == 'apachesolr_search') {
    $form['#submit'][] = 'apachesolr_search_form_search_submit';
    // No other modification make sense unless a query is active.
    // Note - this means that the query must always be run before
    // calling drupal_get_form('search_form').
    // Load core search page
    $search_page = apachesolr_search_page_load('core_search');
    $apachesolr_has_searched = apachesolr_has_searched($search_page->env_id);

    $searcher = NULL;
    $fq = NULL;
    if ($apachesolr_has_searched) {
      $query = apachesolr_current_query($search_page->env_id);
      $searcher = $query->getSearcher();
      // We use the presence of filter query params as a flag for the retain filters checkbox.
      $fq = $query->getParam('fq');
    }

    $form['basic']['apachesolr_search']['#tree'] = TRUE;
    $form['basic']['apachesolr_search']['get'] = array(
      '#type' => 'hidden',
      '#default_value' => json_encode(array_diff_key($_GET, array('q' => 1, 'page' => 1, 'solrsort' => 1, 'retain-filters' => 1))),
    );

    if ($fq || isset($form_state['input']['apachesolr_search']['retain-filters'])) {
      $form['basic']['apachesolr_search']['retain-filters'] = array(
        '#type' => 'checkbox',
        '#title' => t('Retain current filters'),
        '#default_value' => (int) isset($_GET['retain-filters']),
      );
    }
  }
}

/**
 * Default theme function for spelling suggestions.
 */
function theme_apachesolr_search_suggestions($variables) {
  $output = '<div class="spelling-suggestions">';
  $output .= '<dl class="form-item"><dt><strong>' . t('Did you mean') . '</strong></dt>';
  foreach ((array) $variables as $link) {
    $output .= '<dd>' . $link . '</dd>';
  }
  $output .= '</dl></div>';
  return $output;
}

/**
 * Added form submit function to retain filters.
 *
 * @see apachesolr_search_form_search_form_alter()
 */
function apachesolr_search_form_search_submit($form, &$form_state) {
  $fv = $form_state['values'];
  $get = json_decode($fv['apachesolr_search']['get'], TRUE);
  if (!empty($fv['apachesolr_search']['retain-filters'])) {
    $get['retain-filters'] = '1';
    // Add the query values into the redirect.
    $form_state['redirect'] = array($form_state['redirect'], array('query' => $get));
  }
}

/**
 * Implements hook_form_[form_id]_alter().
 *
 * Rebuild (empty) the spellcheck dictionary when the index is deleted..
 */
function apachesolr_search_form_apachesolr_delete_index_confirm_alter(&$form, $form_state) {
  $form['submit']['#submit'][] = 'apachesolr_search_build_spellcheck';
}

/**
 * submit function for the delete_index form.
 *
 */
function apachesolr_search_build_spellcheck($form, &$form_state) {
  try {
    $solr = apachesolr_get_solr();
    $params['spellcheck'] = 'true';
    $params['spellcheck.build'] = 'true';
    $response = $solr->search('solr', 0, 0, $params);
  }
  catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  }
}

/**
 * Implements hook_form_[form_id]_alter().
 *
 * Adds settings to show facet blocks on non-search pages.
 */
function apachesolr_search_form_facetapi_realm_settings_form_alter(&$form, &$form_state) {
  if ('apachesolr' == $form['#facetapi']['adapter']->getId() && 'block' == $form['#facetapi']['realm']['name']) {
    // Gets the environment ID from the searcher, stores in #facetapi property.
    $env_id = ltrim(strstr($form['#facetapi']['adapter']->getSearcher(), '@'), '@');

    $show_facets = apachesolr_environment_variable_get($env_id, 'apachesolr_search_show_facets', 0);
    $facet_pages = apachesolr_environment_variable_get($env_id, 'apachesolr_search_facet_pages', '');

    $form['#facetapi']['env_id'] = $env_id;

    $form['apachesolr_search_show_facets'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show facets on non-search pages.'),
      '#default_value' => $show_facets,
      '#weight' => '-10',
    );

    $form['apachesolr_search_facet_pages'] = array(
      '#title' => t('Non-search paths'),
      '#type' => 'textarea',
      '#default_value' => $facet_pages,
      '#weight' => '-10',
      '#dependency' => array(
        'edit-apachesolr-search-show-facets' => array(1),
      ),
    );

    $form['#submit'][] = 'apachesolr_search_facetapi_realm_settings_form_submit';
  }
}

/**
 * Form submission handler for facetapi_realm_settings_form().
 */
function apachesolr_search_facetapi_realm_settings_form_submit(&$form, &$form_state) {
  $env_id = $form['#facetapi']['env_id'];

  // Adds the settings to the array keyed by environment ID, saves variables.
  $show_facets = $form_state['values']['apachesolr_search_show_facets'];
  $facet_pages = $form_state['values']['apachesolr_search_facet_pages'];
  apachesolr_environment_variable_set($env_id, 'apachesolr_search_show_facets', $show_facets);
  apachesolr_environment_variable_set($env_id, 'apachesolr_search_facet_pages', $facet_pages);
}

/**
 * Implements hook_theme().
 */
function apachesolr_search_theme() {
  return array(
    /**
     * Shows the facets in blocks in the search result area
     */
    'apachesolr_search_browse_blocks' => array(
      'render element' => 'content',
    ),
    /**
     * Shows the search snippet
     */
    'apachesolr_search_snippets' => array(
      'arguments' => array('doc' => NULL, 'snippets' => array()),
    ),
    /**
     * Shows a message when the search does not return any result
     */
    'apachesolr_search_noresults' => array(
      'arguments' => array(),
    ),
    /**
     * Shows a list of suggestions
     */
    'apachesolr_search_suggestions' => array(
      'arguments' => array('links' => NULL),
    ),
    /**
     * Shows a list of results (docs) in content recommendation block
     */
    'apachesolr_search_mlt_recommendation_block' => array(
      'arguments' => array('docs' => NULL, 'delta' => NULL),
    ),
  );
}

/**
 * Implements hook_theme_registry_alter().
 */
function apachesolr_search_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['search_results'])) {
    $theme_registry['search_results']['arguments']['search_page'] = NULL;
  }
}

/**
 * Theme the highlighted snippet text for a search entry.
 *
 * @param array $vars
 *
 */
function theme_apachesolr_search_snippets($vars) {
  $result = '';
  if (is_array($vars['snippets'])) {
    $snippets = $vars['snippets'];
    if (isset($snippets['content'])) {
      $result .= $snippets['content'];
      unset($snippets['content']);
    }
    if (isset($snippets['teaser'])) {
      $result .= (strlen($result) > 0) ? ' ... ' : '';
      $result .= $snippets['teaser'];
      unset($snippets['teaser']);
    }
    if (count($snippets)) {
      $result .= (strlen($result) > 0) ? ' ... ' : '';
      $result .= implode(' ... ', $snippets);
    }
  }
  return $result . ' ...';
}

/**
 * Brief message to display when no results match the query.
 *
 * @see search_help()
 */
function theme_apachesolr_search_noresults() {
  return t('<ul>
<li>Check if your spelling is correct, or try removing filters.</li>
<li>Remove quotes around phrases to match each word individually: <em>"blue drop"</em> will match less than <em>blue drop</em>.</li>
<li>You can require or exclude terms using + and -: <em>big +blue drop</em> will require a match on <em>blue</em> while <em>big blue -drop</em> will exclude results that contain <em>drop</em>.</li>
</ul>');
}

/**
 * Backport of pager_default_initialize from Drupal 7.
 */
function apachesolr_search_pager_default_initialize($total, $limit, $element = 0) {
  global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;

  $page = apachesolr_search_pager_find_page($element);

  // We calculate the total of pages as ceil(items / limit).
  $pager_total_items[$element] = $total;
  $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  $pager_page_array[$element] = max(0, min($page, ((int) $pager_total[$element]) - 1));
  $pager_limits[$element] = $limit;
  return $pager_page_array[$element];
}

/**
 * Backport of pager_find_page from Drupal 7.
 */
function apachesolr_search_pager_find_page($element = 0) {
  $page = isset($_GET['page']) ? $_GET['page'] : '';
  $page_array = explode(',', $page);
  if (!isset($page_array[$element])) {
    $page_array[$element] = 0;
  }
  return (int) $page_array[$element];
}