| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331 | <?php/** * @file * Provides a flexible framework for implementing search services. *//** * Default number of items indexed per cron batch for each enabled index. */define('SEARCH_API_DEFAULT_CRON_LIMIT', 50);/** * Implements hook_menu(). */function search_api_menu() {  $pre = 'admin/config/search/search_api';  $items[$pre] = array(    'title' => 'Search API',    'description' => 'Create and configure search engines.',    'page callback' => 'search_api_admin_overview',    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',  );  $items[$pre . '/overview'] = array(    'title' => 'Overview',    'type' => MENU_DEFAULT_LOCAL_TASK,    'weight' => -10,  );  $items[$pre . '/add_server'] = array(    'title' => 'Add server',    'description' => 'Create a new search server.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_add_server'),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',    'weight' => -1,    'type' => MENU_LOCAL_ACTION,  );  $items[$pre . '/add_index'] = array(    'title' => 'Add index',    'description' => 'Create a new search index.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_add_index'),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_ACTION,  );  $items[$pre . '/server/%search_api_server'] = array(    'title' => 'View server',    'title callback' => 'search_api_admin_item_title',    'title arguments' => array(5),    'description' => 'View server details.',    'page callback' => 'search_api_admin_server_view',    'page arguments' => array(5),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',  );  $items[$pre . '/server/%search_api_server/view'] = array(    'title' => 'View',    'type' => MENU_DEFAULT_LOCAL_TASK,    'weight' => -10,  );  $items[$pre . '/server/%search_api_server/edit'] = array(    'title' => 'Edit',    'description' => 'Edit server details.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_server_edit', 5),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',    'weight' => -1,    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,  );  $items[$pre . '/server/%search_api_server/disable'] = array(    'title' => 'Disable',    'description' => 'Disable index.',    'page callback' => 'search_api_admin_server_view',    'page arguments' => array(5, 6),    'access callback' => 'search_api_access_disable_page',    'access arguments' => array(5),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE,    'weight' => 8,  );  $items[$pre . '/server/%search_api_server/delete'] = array(    'title' => 'Delete',    'title callback' => 'search_api_title_delete_page',    'title arguments' => array(5),    'description' => 'Delete server.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_confirm', 'server', 'delete', 5),    'access callback' => 'search_api_access_delete_page',    'access arguments' => array(5),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE,    'weight' => 10,  );  $items[$pre . '/index/%search_api_index'] = array(    'title' => 'View index',    'title callback' => 'search_api_admin_item_title',    'title arguments' => array(5),    'description' => 'View index details.',    'page callback' => 'search_api_admin_index_view',    'page arguments' => array(5),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',  );  $items[$pre . '/index/%search_api_index/view'] = array(    'title' => 'View',    'type' => MENU_DEFAULT_LOCAL_TASK,    'weight' => -10,  );  $items[$pre . '/index/%search_api_index/edit'] = array(    'title' => 'Edit',    'description' => 'Edit index settings.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_index_edit', 5),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,    'weight' => -6,  );  $items[$pre . '/index/%search_api_index/fields'] = array(    'title' => 'Fields',    'description' => 'Select indexed fields.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_index_fields', 5),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,    'weight' => -4,  );  $items[$pre . '/index/%search_api_index/workflow'] = array(    'title' => 'Filters',    'description' => 'Edit indexing workflow.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_index_workflow', 5),    'access arguments' => array('administer search_api'),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,    'weight' => -2,  );  $items[$pre . '/index/%search_api_index/disable'] = array(    'title' => 'Disable',    'description' => 'Disable index.',    'page callback' => 'search_api_admin_index_view',    'page arguments' => array(5, 6),    'access callback' => 'search_api_access_disable_page',    'access arguments' => array(5),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE,    'weight' => 8,  );  $items[$pre . '/index/%search_api_index/delete'] = array(    'title' => 'Delete',    'title callback' => 'search_api_title_delete_page',    'title arguments' => array(5),    'description' => 'Delete index.',    'page callback' => 'drupal_get_form',    'page arguments' => array('search_api_admin_confirm', 'index', 'delete', 5),    'access callback' => 'search_api_access_delete_page',    'access arguments' => array(5),    'file' => 'search_api.admin.inc',    'type' => MENU_LOCAL_TASK,    'context' => MENU_CONTEXT_INLINE,    'weight' => 10,  );  return $items;}/** * Implements hook_help(). */function search_api_help($path) {  switch ($path) {    case 'admin/help#search_api':      $classes = array();      foreach (search_api_get_service_info() as $id => $info) {        $id = drupal_clean_css_identifier($id);        $name = check_plain($info['name']);        $description = isset($info['description']) ? $info['description'] : '';        $classes[] = "<h2 id=\"$id\">$name</h2>\n$description";      }      $output = '';      if ($classes) {        $output .= '<p>' . t('The following service classes are available for creating a search server.') . "</p>\n";        $output .= implode("\n\n", $classes);      }      return $output;    case 'admin/config/search/search_api':      return '<p>' . t('A search server and search index are used to execute searches. Several indexes can exist per server.<br />You need at least one server and one index to create searches on your site.') . '</p>';  }}/** * Implements hook_hook_info(). */function search_api_hook_info() {  // We use the same group for all hooks, so save code lines.  $hook_info = array(    'group' => 'search_api',  );  return array(    'search_api_service_info' => $hook_info,    'search_api_service_info_alter' => $hook_info,    'search_api_item_type_info' => $hook_info,    'search_api_item_type_info_alter' => $hook_info,    'search_api_data_type_info' => $hook_info,    'search_api_data_type_info_alter' => $hook_info,    'search_api_alter_callback_info' => $hook_info,    'search_api_alter_callback_info_alter' => $hook_info,    'search_api_processor_info' => $hook_info,    'search_api_processor_info_alter' => $hook_info,    'search_api_index_items_alter' => $hook_info,    'search_api_items_indexed' => $hook_info,    'search_api_query_alter' => $hook_info,    'search_api_results_alter' => $hook_info,    'search_api_server_load' => $hook_info,    'search_api_server_insert' => $hook_info,    'search_api_server_update' => $hook_info,    'search_api_server_delete' => $hook_info,    'default_search_api_server' => $hook_info,    'default_search_api_server_alter' => $hook_info,    'search_api_index_load' => $hook_info,    'search_api_index_insert' => $hook_info,    'search_api_index_update' => $hook_info,    'search_api_index_reindex' => $hook_info,    'search_api_index_delete' => $hook_info,    'default_search_api_index' => $hook_info,    'default_search_api_index_alter' => $hook_info,  );}/** * Implements hook_theme(). */function search_api_theme() {  $themes['search_api_dropbutton'] = array(    'variables' => array(      'links' => array(),    ),    'file' => 'search_api.admin.inc',  );  $themes['search_api_server'] = array(    'variables' => array(      'id' => NULL,      'name' => '',      'machine_name' => '',      'description' => NULL,      'enabled' => NULL,      'class_id' => NULL,      'class_name' => NULL,      'class_description' => NULL,      'indexes' => array(),      'options' => array(),      'status' => ENTITY_CUSTOM,      'extra' => array(),    ),    'file' => 'search_api.admin.inc',  );  $themes['search_api_index'] = array(    'variables' => array(      'id' => NULL,      'name' => '',      'machine_name' => '',      'description' => NULL,      'item_type' => NULL,      'datasource_config' => NULL,      'enabled' => NULL,      'server' => NULL,      'options' => array(),      'fields' => array(),      'indexed_items' => 0,      'on_server' => NULL,      'total_items' => 0,      'status' => ENTITY_CUSTOM,      'read_only' => 0,    ),    'file' => 'search_api.admin.inc',  );  $themes['search_api_admin_item_order'] = array(    'render element' => 'element',    'file' => 'search_api.admin.inc',  );  $themes['search_api_admin_fields_table'] = array(    'render element' => 'element',    'file' => 'search_api.admin.inc',  );  return $themes;}/** * Implements hook_permission(). */function search_api_permission() {  return array(    'administer search_api' => array(      'title' => t('Administer Search API'),      'description' => t('Create and configure Search API servers and indexes.'),    ),  );}/** * Implements hook_cron(). * * This will first execute any pending server tasks. After that, items will * be indexed on all enabled indexes with a non-zero cron limit. Indexing will * run for the time set in the search_api_index_worker_callback_runtime variable * (defaulting to 15 seconds), but will at least index one batch of items on * each index. * * @see search_api_server_tasks_check() */function search_api_cron() {  // Execute pending server tasks.  search_api_server_tasks_check();  // Load all enabled, not read-only indexes.  $conditions = array(    'enabled' => TRUE,    'read_only' => 0  );  $indexes = search_api_index_load_multiple(FALSE, $conditions);  if (!$indexes) {    return;  }  // Remember servers which threw an exception.  $ignored_servers = array();  // Continue indexing, one batch from each index, until the time is up, but at  // least index one batch per index.  $end = time() + variable_get('search_api_index_worker_callback_runtime', 15);  $first_pass = TRUE;  while (TRUE) {    if (!$indexes) {      break;    }    foreach ($indexes as $id => $index) {      if (!$first_pass && time() >= $end) {        break 2;      }      if (!empty($ignored_servers[$index->server])) {        continue;      }      $limit = isset($index->options['cron_limit'])        ? $index->options['cron_limit']        : SEARCH_API_DEFAULT_CRON_LIMIT;      $num = 0;      if ($limit) {        try {          $num = search_api_index_items($index, $limit);          if ($num) {            $variables = array(              '@num' => $num,              '%name' => $index->name            );            watchdog('search_api', 'Indexed @num items for index %name.', $variables, WATCHDOG_INFO);          }        }        catch (SearchApiException $e) {          // Exceptions will probably be caused by the server in most cases.          // Therefore, don't index for any index on this server.          $ignored_servers[$index->server] = TRUE;          $vars['%index'] = $index->name;          watchdog_exception('search_api', $e, '%type while trying to index items on %index: !message in %function (line %line of %file).', $vars);        }      }      if  (!$num) {        // Couldn't index any items => stop indexing for this index in this        // cron run.        unset($indexes[$id]);      }    }    $first_pass = FALSE;  }}/** * Implements hook_entity_info(). */function search_api_entity_info() {  $info['search_api_server'] = array(    'label' => t('Search server'),    'controller class' => 'EntityAPIControllerExportable',    'metadata controller class' => FALSE,    'entity class' => 'SearchApiServer',    'base table' => 'search_api_server',    'uri callback' => 'search_api_server_url',    'access callback' => 'search_api_entity_access',    'module' => 'search_api',    'exportable' => TRUE,    'entity keys' => array(      'id' => 'id',      'label' => 'name',      'name' => 'machine_name',    ),  );  $info['search_api_index'] = array(    'label' => t('Search index'),    'controller class' => 'EntityAPIControllerExportable',    'metadata controller class' => FALSE,    'entity class' => 'SearchApiIndex',    'base table' => 'search_api_index',    'uri callback' => 'search_api_index_url',    'access callback' => 'search_api_entity_access',    'module' => 'search_api',    'exportable' => TRUE,    'entity keys' => array(      'id' => 'id',      'label' => 'name',      'name' => 'machine_name',    ),  );  return $info;}/** * Implements hook_entity_property_info(). */function search_api_entity_property_info() {  $info['search_api_server']['properties'] = array(    'id' => array(      'label' => t('ID'),      'type' => 'integer',      'description' => t('The primary identifier for a server.'),      'schema field' => 'id',      'validation callback' => 'entity_metadata_validate_integer_positive',    ),    'name' => array(      'label' => t('Name'),      'type' => 'text',      'description' => t('The displayed name for a server.'),      'schema field' => 'name',      'required' => TRUE,    ),    'machine_name' => array(      'label' => t('Machine name'),      'type' => 'token',      'description' => t('The internally used machine name for a server.'),      'schema field' => 'machine_name',      'required' => TRUE,    ),    'description' => array(      'label' => t('Description'),      'type' => 'text',      'description' => t('The displayed description for a server.'),      'schema field' => 'description',      'sanitize' => 'filter_xss',    ),    'class' => array(      'label' => t('Service class'),      'type' => 'text',      'description' => t('The ID of the service class to use for this server.'),      'schema field' => 'class',      'required' => TRUE,    ),    'enabled' => array(      'label' => t('Enabled'),      'type' => 'boolean',      'description' => t('A flag indicating whether the server is enabled.'),      'schema field' => 'enabled',    ),    'status' => array(      'label' => t('Status'),      'type' => 'integer',      'description' => t('Search API server status property'),      'schema field' => 'status',      'options list' => 'search_api_status_options_list',    ),    'module' => array(      'label' => t('Module'),      'type' => 'text',      'description' => t('The name of the module from which this server originates.'),      'schema field' => 'module',    ),  );  $info['search_api_index']['properties'] = array(    'id' => array(      'label' => t('ID'),      'type' => 'integer',      'description' => t('An integer identifying the index.'),      'schema field' => 'id',      'validation callback' => 'entity_metadata_validate_integer_positive',    ),    'name' => array(      'label' => t('Name'),      'type' => 'text',      'description' => t('A name to be displayed for the index.'),      'schema field' => 'name',      'required' => TRUE,    ),    'machine_name' => array(      'label' => t('Machine name'),      'type' => 'token',      'description' => t('The internally used machine name for an index.'),      'schema field' => 'machine_name',      'required' => TRUE,    ),    'description' => array(      'label' => t('Description'),      'type' => 'text',      'description' => t("A string describing the index' use to users."),      'schema field' => 'description',      'sanitize' => 'filter_xss',    ),    'server' => array(      'label' => t('Server ID'),      'type' => 'token',      'description' => t('The machine name of the search_api_server with which data should be indexed.'),      'schema field' => 'server',    ),    'server_entity' => array(      'label' => t('Server'),      'type' => 'search_api_server',      'description' => t('The search_api_server with which data should be indexed.'),      'getter callback' => 'search_api_index_get_server',    ),    'item_type' => array(      'label' => t('Item type'),      'type' => 'token',      'description' => t('The type of items stored in this index.'),      'schema field' => 'item_type',      'required' => TRUE,    ),    'enabled' => array(      'label' => t('Enabled'),      'type' => 'boolean',      'description' => t('A flag indicating whether the index is enabled.'),      'schema field' => 'enabled',    ),    'read_only' => array(      'label' => t('Read only'),      'type' => 'boolean',      'description' => t('A flag indicating whether the index is read-only.'),      'schema field' => 'read_only',    ),    'status' => array(      'label' => t('Status'),      'type' => 'integer',      'description' => t('Search API index status property'),      'schema field' => 'status',      'options list' => 'search_api_status_options_list',    ),    'module' => array(      'label' => t('Module'),      'type' => 'text',      'description' => t('The name of the module from which this index originates.'),      'schema field' => 'module',    ),  );  return $info;}/** * Implements hook_search_api_server_insert(). * * Calls the postCreate() method for the server. */function search_api_search_api_server_insert(SearchApiServer $server) {  // Check whether this is actually part of a revert.  $reverts = &drupal_static('search_api_search_api_server_delete', array());  if (isset($reverts[$server->machine_name])) {    $server->original = $reverts[$server->machine_name];    unset($reverts[$server->machine_name]);    search_api_search_api_server_update($server);    unset($server->original);    return;  }  $server->postCreate();}/** * Implements hook_search_api_server_update(). * * Calls the server's postUpdate() method and marks all of this server's indexes * for reindexing, if necessary. */function search_api_search_api_server_update(SearchApiServer $server) {  if ($server->postUpdate()) {    foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {      $index->reindex();    }  }  if (!empty($server->original) && $server->enabled != $server->original->enabled) {    if ($server->enabled) {      search_api_server_tasks_check($server);    }    else {      foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {        $index->update(array('enabled' => 0, 'server' => NULL));      }    }  }}/** * Implements hook_search_api_server_delete(). * * Calls the preDelete() method for the server. */function search_api_search_api_server_delete(SearchApiServer $server) {  // Only react on real delete, not revert.  if ($server->hasStatus(ENTITY_IN_CODE)) {    $reverts = &drupal_static(__FUNCTION__, array());    $reverts[$server->machine_name] = $server;    return;  }  $server->preDelete();  foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {    $index->update(array('server' => NULL, 'enabled' => FALSE));  }  search_api_server_tasks_delete(NULL, $server);}/** * Implements hook_search_api_index_insert(). * * Adds the index to its server (if any) and starts tracking indexed items (if * the index is enabled). */function search_api_search_api_index_insert(SearchApiIndex $index) {  // Check whether this is actually part of a revert.  $reverts = &drupal_static('search_api_search_api_index_delete', array());  if (isset($reverts[$index->machine_name])) {    $index->original = $reverts[$index->machine_name];    unset($reverts[$index->machine_name]);    search_api_search_api_index_update($index);    unset($index->original);    return;  }  $index->postCreate();}/** * Implements hook_search_api_index_update(). */function search_api_search_api_index_update(SearchApiIndex $index) {  // Call the datasource update function with the tables this module provides.  search_api_index_update_datasource($index, 'search_api_item');  search_api_index_update_datasource($index, 'search_api_item_string_id');  // If the server was changed, we have to call the appropriate service class  // hook methods.  if ($index->server != $index->original->server) {    // Server changed - inform old and new ones.    if ($index->original->server) {      $old_server = search_api_server_load($index->original->server);      // The server might have changed because the old one was deleted:      if ($old_server) {        $old_server->removeIndex($index);      }    }    if ($index->server) {      try {        $new_server = $index->server(TRUE);        // If the server is enabled, we call addIndex(); otherwise, we save the task.        $new_server->addIndex($index);      }      catch (SearchApiException $e) {        watchdog_exception('search_api', $e);        // If the new server doesn't exist, we remove the index from all        // servers. Note that saving an entity in its own update hook is usually        // a recipe for disaster, but since we are only doing this if a server        // is set and remove the server here before saving, it should be safe        // enough.        $index->server = NULL;        $index->save();      }    }    // We also have to re-index all content.    _search_api_index_reindex($index);  }  // If the fields were changed, call the appropriate service class hook method  // and re-index the content, if necessary.  $old_fields = $index->original->options + array('fields' => array());  $old_fields = $old_fields['fields'];  $new_fields = $index->options + array('fields' => array());  $new_fields = $new_fields['fields'];  if ($old_fields != $new_fields) {    if ($index->server) {      $index->server()->fieldsUpdated($index);    }  }  // If the index's enabled or read-only status is being changed, queue or  // dequeue items for indexing.  if (!$index->read_only && $index->enabled != $index->original->enabled) {    if ($index->enabled) {      $index->queueItems();    }    else {      $index->dequeueItems();    }  }  elseif ($index->read_only != $index->original->read_only) {    if ($index->read_only) {      $index->dequeueItems();    }    else {      $index->queueItems();    }  }}/** * Implements hook_search_api_index_delete(). * * Removes all data for indexes not available any more. */function search_api_search_api_index_delete(SearchApiIndex $index) {  // Only react on real delete, not revert.  if ($index->hasStatus(ENTITY_IN_CODE)) {    $reverts = &drupal_static(__FUNCTION__, array());    $reverts[$index->machine_name] = $index;    return;  }  cache_clear_all($index->getCacheId(''), 'cache', TRUE);  $index->postDelete();}/** * Implements hook_features_export_alter(). * * Adds dependency information for exported servers. */function search_api_features_export_alter(&$export) {  if (isset($export['features']['search_api_server'])) {    // Get a list of the modules that provide storage engines.    $hook = 'search_api_service_info';    $classes = array();    foreach (module_implements('search_api_service_info') as $module) {      $function = $module . '_' . $hook;      $engines = $function();      foreach ($engines as $service => $specs) {        $classes[$service] = $module;      }    }    // Check all of the exported server specifications.    foreach ($export['features']['search_api_server'] as $server_name) {      // Load the server's object.      $server = search_api_server_load($server_name);      $module = $classes[$server->class];      // Ensure that the module responsible for the server object is listed as      // a dependency.      if (!isset($export['dependencies'][$module])) {        $export['dependencies'][$module] = $module;      }    }    // Ensure the dependencies list is still sorted alphabetically.    ksort($export['dependencies']);  }}/** * Implements hook_system_info_alter(). * * Checks if the module provides any search item types or service classes. If it * does, and there are search indexes using those item types, respectively * servers using those service classes, the module is set to "required". * * Heavily borrowed from field_system_info_alter(). * * @see hook_search_api_item_type_info() */function search_api_system_info_alter(&$info, $file, $type) {  if ($type != 'module' || $file->name == 'search_api' || !module_exists($file->name)) {    return;  }  // Check for defined item types.  if (module_hook($file->name, 'search_api_item_type_info')) {    $types = array();    foreach (search_api_get_item_type_info() as $type => $type_info) {      if ($type_info['module'] == $file->name) {        $types[] = $type;      }    }    if ($types) {      $sql = 'SELECT machine_name, name FROM {search_api_index} WHERE item_type IN (:types)';      $indexes = db_query($sql, array(':types' => $types))->fetchAllKeyed();      if ($indexes) {        $info['required'] = TRUE;        $links = array();        foreach ($indexes as $id => $name) {          $url = url("admin/config/search/search_api/index/$id");          $links[] = '<a href="' . check_plain($url) . '">' . check_plain($name) . '</a>';        }        $args = array('!indexes' => implode(', ', $links));        $info['explanation'] = format_plural(count($indexes), 'Item type in use by the following index: !indexes.', 'Item type(s) in use by the following indexes: !indexes.', $args);      }    }  }  // Check for defined service classes.  if (module_hook($file->name, 'search_api_service_info')) {    $classes = array();    foreach (search_api_get_service_info() as $class => $class_info) {      if ($class_info['module'] == $file->name) {        $classes[] = $class;      }    }    if ($classes) {      $sql = 'SELECT machine_name, name FROM {search_api_server} WHERE class IN (:classes)';      $servers = db_query($sql, array(':classes' => $classes))->fetchAllKeyed();      if ($servers) {        $info['required'] = TRUE;        $links = array();        foreach ($servers as $id => $name) {          $url = url("admin/config/search/search_api/server/$id");          $links[] = '<a href="' . check_plain($url) . '">' . check_plain($name) . '</a>';        }        $args = array('!servers' => implode(', ', $links));        $explanation = format_plural(count($servers), 'Service class in use by the following server: !servers.', 'Service class(es) in use by the following servers: !servers.', $args);        $info['explanation'] = (!empty($info['explanation']) ? $info['explanation'] . ' ' : '') . $explanation;      }    }  }}/** * Implements hook_entity_insert(). * * This is implemented on behalf of the SearchApiEntityDataSourceController * datasource controller and calls search_api_track_item_insert() for the * inserted items. * * @see search_api_search_api_item_type_info() */function search_api_entity_insert($entity, $type) {  // When inserting a new search index, the new index was already inserted into  // the tracking table. This would lead to a duplicate-key issue, if we would  // continue.  // We also only react on entity operations for types with property  // information, as we don't provide search integration for the others.  if ($type == 'search_api_index' || !entity_get_property_info($type)) {    return;  }  list($id) = entity_extract_ids($type, $entity);  if (isset($id)) {    search_api_track_item_insert($type, array($id));    $combined_id = $type . '/' . $id;    search_api_track_item_insert('multiple', array($combined_id));  }}/** * Implements hook_entity_update(). * * This is implemented on behalf of the SearchApiEntityDataSourceController * datasource controller and calls search_api_track_item_change() for the * updated items. * * It also checks whether the entity's bundle changed and acts accordingly. * * @see search_api_search_api_item_type_info() */function search_api_entity_update($entity, $type) {  // We only react on entity operations for types with property information, as  // we don't provide search integration for the others.  if (!entity_get_property_info($type)) {    return;  }  list($id, , $new_bundle) = entity_extract_ids($type, $entity);  // Check if the entity's bundle changed.  if (!empty($entity->original)) {    list(, , $old_bundle) = entity_extract_ids($type, $entity->original);    if ($new_bundle != $old_bundle) {      _search_api_entity_datasource_bundle_change($type, $id, $old_bundle, $new_bundle);    }  }  if (isset($id)) {    search_api_track_item_change($type, array($id));    $combined_id = $type . '/' . $id;    search_api_track_item_change('multiple', array($combined_id));  }}/** * Implements hook_entity_delete(). * * This is implemented on behalf of the SearchApiEntityDataSourceController * datasource controller and calls search_api_track_item_delete() for the * deleted items. * * @see search_api_search_api_item_type_info() */function search_api_entity_delete($entity, $type) {  // We only react on entity operations for types with property information, as  // we don't provide search integration for the others.  if (!entity_get_property_info($type)) {    return;  }  list($id) = entity_extract_ids($type, $entity);  if (isset($id)) {    search_api_track_item_delete($type, array($id));    $combined_id = $type . '/' . $id;    search_api_track_item_delete('multiple', array($combined_id));  }}/** * Implements hook_node_access_records_alter(). * * Marks the node as "changed" in indexes that use the "Node access" data * alteration. Also marks the node's comments as changed in indexes that use the * "Comment access" data alteration. */function search_api_node_access_records_alter(&$grants, $node) {  foreach (search_api_index_load_multiple(FALSE) as $index) {    $item_ids = array();    if (!empty($index->options['data_alter_callbacks']['search_api_alter_node_access']['status'])) {      $item_id = $index->datasource()->getItemId($node);      if ($item_id !== NULL) {        $item_ids = array($item_id);      }    }    elseif (!empty($index->options['data_alter_callbacks']['search_api_alter_comment_access']['status'])) {      if (!isset($comments)) {        $comments = comment_load_multiple(FALSE, array('nid' => $node->nid));      }      foreach ($comments as $comment) {        $item_ids[] = $index->datasource()->getItemId($comment);      }    }    if ($item_ids) {      $indexes = array($index->machine_name => $index);      search_api_track_item_change_for_indexes($index->item_type, $item_ids, $indexes);    }  }}/** * Implements hook_field_attach_rename_bundle(). * * This is implemented on behalf of the SearchApiEntityDataSourceController * datasource controller, to update any bundle settings that contain the changed * bundle. */function search_api_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {  foreach (search_api_index_load_multiple(FALSE, array('item_type' => $entity_type)) as $index) {    $bundles = &$index->options['datasource']['bundles'];    if (isset($bundles) && ($pos = array_search($bundle_old, $bundles)) !== FALSE) {      $bundles[$pos] = $bundle_new;      $index->save();      // Clear all caches that could contain the bundle information.      $index->resetCaches();      drupal_static_reset('search_api_get_datasource_controller');    }  }}/** * Implements hook_field_update_field(). * * Recalculates fields settings if the cardinality of the field has changed from * or to 1. */function search_api_field_update_field($field, $prior_field) {  $before = $prior_field['cardinality'];  $after = $field['cardinality'];  if ($before != $after && ($before == 1 || $after == 1)) {    // Unfortunately, we cannot call this right away since the field information    // is only stored after the hook is called.    drupal_register_shutdown_function('search_api_index_recalculate_fields');  }}/** * Implements hook_flush_caches(). * * Recalculates fields settings in case the schema (in most cases: the * multiplicity) of a property has changed. */function search_api_flush_caches() {  search_api_index_recalculate_fields();}/** * Implements hook_search_api_item_type_info(). * * Adds item types for all entity types with property information. */function search_api_search_api_item_type_info() {  $types = array();  foreach (search_api_entity_type_options_list() as $type => $label) {    $types[$type] = array(      'name' => $label,      'datasource controller' => 'SearchApiEntityDataSourceController',      'entity_type' => $type,    );  }  $types['multiple'] = array(    'name' => t('Multiple types'),    'datasource controller' => 'SearchApiCombinedEntityDataSourceController',  );  return $types;}/** * Implements hook_modules_enabled(). */function search_api_modules_enabled() {  // New modules might offer additional item types or service classes,  // invalidating the cached information.  drupal_static_reset('search_api_get_item_type_info');  drupal_static_reset('search_api_get_service_info');}/** * Implements hook_modules_disabled(). */function search_api_modules_disabled() {  // The disabled modules might have offered item types or service classes,  // invalidating the cached information.  drupal_static_reset('search_api_get_item_type_info');  drupal_static_reset('search_api_get_service_info');}/** * Implements hook_search_api_alter_callback_info(). */function search_api_search_api_alter_callback_info() {  $callbacks['search_api_alter_bundle_filter'] = array(    'name' => t('Bundle filter'),    'description' => t('Exclude items from indexing based on their bundle (content type, vocabulary, …).'),    'class' => 'SearchApiAlterBundleFilter',    // Filters should be executed first.    'weight' => -10,  );  $callbacks['search_api_alter_role_filter'] = array(    'name' => t('Role filter'),    'description' => t('Exclude users from indexing based on their role.'),    'class' => 'SearchApiAlterRoleFilter',    // Filters should be executed first.    'weight' => -10,  );  $callbacks['search_api_alter_add_url'] = array(    'name' => t('URL field'),    'description' => t("Adds the item's URL to the indexed data."),    'class' => 'SearchApiAlterAddUrl',  );  $callbacks['search_api_alter_add_aggregation'] = array(    'name' => t('Aggregated fields'),    'description' => t('Gives you the ability to define additional fields, containing data from one or more other fields.'),    'class' => 'SearchApiAlterAddAggregation',  );  $callbacks['search_api_alter_add_viewed_entity'] = array(    'name' => t('Complete entity view'),    'description' => t('Adds an additional field containing the whole HTML content of the entity when viewed.'),    'class' => 'SearchApiAlterAddViewedEntity',  );  $callbacks['search_api_alter_add_hierarchy'] = array(    'name' => t('Index hierarchy'),    'description' => t('Allows to index hierarchical fields along with all their ancestors.'),    'class' => 'SearchApiAlterAddHierarchy',  );  $callbacks['search_api_alter_language_control'] = array(    'name' => t('Language control'),    'description' => t('Lets you determine the language of items in the index.'),    'class' => 'SearchApiAlterLanguageControl',  );  $callbacks['search_api_alter_node_access'] = array(    'name' => t('Node access'),    'description' => t('Add node access information to the index. <strong>Caution:</strong> This only affects the indexed nodes themselves, not any node reference fields that are indexed with them, or displayed in search results.'),    'class' => 'SearchApiAlterNodeAccess',  );  $callbacks['search_api_alter_comment_access'] = array(    'name' => t('Access check'),    'description' => t('Add node access information to the index. <strong>Caution:</strong> This only affects the indexed nodes themselves, not any node reference fields that are indexed with them, or displayed in search results.'),    'class' => 'SearchApiAlterCommentAccess',  );  $callbacks['search_api_alter_node_status'] = array(    'name' => t('Exclude unpublished nodes'),    'description' => t('Exclude unpublished nodes from the index. <strong>Caution:</strong> This only affects the indexed nodes themselves. If an enabled node has references to disabled nodes, those will still be indexed (or displayed) normally.'),    'class' => 'SearchApiAlterNodeStatus',  );  $callbacks['search_api_alter_user_status'] = array(    'name' => t('Exclude blocked users'),    'description' => t('Exclude blocked users from the index. <strong>Caution:</strong> This only affects the indexed users themselves. If an active user account includes a reference to a disabled user, that reference will still be indexed (or displayed) normally.'),    'class' => 'SearchApiAlterUserStatus',  );  return $callbacks;}/** * Implements hook_search_api_processor_info(). */function search_api_search_api_processor_info() {  $processors['search_api_case_ignore'] = array(    'name' => t('Ignore case'),    'description' => t('This processor will make searches case-insensitive for fulltext or string fields.'),    'class' => 'SearchApiIgnoreCase',  );  $processors['search_api_html_filter'] = array(    'name' => t('HTML filter'),    'description' => t('Strips HTML tags from fulltext fields and decodes HTML entities. ' .        'Use this processor when indexing HTML data, e.g., node bodies for certain text formats.<br />' .        'The processor also allows to boost (or ignore) the contents of specific elements.'),    'class' => 'SearchApiHtmlFilter',    'weight' => 10,  );  if (module_exists('transliteration')) {    $processors['search_api_transliteration'] = array(      'name' => t('Transliteration'),      'description' => t('This processor will make searches insensitive to accents and other non-ASCII characters.'),      'class' => 'SearchApiTransliteration',      'weight' => 15,    );  }  $processors['search_api_tokenizer'] = array(    'name' => t('Tokenizer'),    'description' => t('Tokenizes fulltext data by stripping whitespace. ' .        'This processor allows you to specify which characters make up words and which characters should be ignored, using regular expression syntax. ' .        'Otherwise it is up to the search server implementation to decide how to split indexed fulltext data.'),    'class' => 'SearchApiTokenizer',    'weight' => 20,  );  $processors['search_api_stopwords'] = array(    'name' => t('Stopwords'),    'description' => t('This processor prevents certain words from being indexed and removes them from search terms. ' .        'For best results, it should only be executed after tokenizing.'),    'class' => 'SearchApiStopWords',    'weight' => 30,  );  $processors['search_api_porter_stemmer'] = array(    'name' => t('Stem words'),    'description' => t('This processor reduces words to a stem (e.g., "talking" to "talk"). For best results, it should only be executed after tokenizing.'),    'class' => 'SearchApiPorterStemmer',    'weight' => 35,  );  $processors['search_api_highlighting'] = array(    'name' => t('Highlighting'),    'description' => t('Adds highlighting for search results.'),    'class' => 'SearchApiHighlight',    'weight' => 40,  );  return $processors;}/** * Inserts new unindexed items for all indexes on the specified type. * * @param string $type *   The item type of the new items. * @param array $item_ids *   The IDs of the new items. */function search_api_track_item_insert($type, array $item_ids) {  $conditions = array(    'enabled' => 1,    'item_type' => $type,    'read_only' => 0,  );  $indexes = search_api_index_load_multiple(FALSE, $conditions);  if (!$indexes) {    return;  }  try {    $returned_indexes = search_api_get_datasource_controller($type)->trackItemInsert($item_ids, $indexes);    if (isset($returned_indexes)) {      $indexes = $returned_indexes;    }  }  catch (SearchApiException $e) {    $vars['%item_type'] = $type;    watchdog_exception('search_api', $e, '%type while inserting items of type %item_type: !message in %function (line %line of %file).', $vars);    return;  }  foreach ($indexes as $index) {    if (!empty($index->options['index_directly'])) {      search_api_index_specific_items_delayed($index, $item_ids);    }  }}/** * Mark the items with the specified IDs as "dirty", i.e., as needing to be reindexed. * * For indexes for which items should be indexed immediately, the items are * indexed directly, instead. * * @param $type *   The type of items, specific to the data source. * @param array $item_ids *   The IDs of the items to be marked dirty. */function search_api_track_item_change($type, array $item_ids) {  $conditions = array(    'enabled' => 1,    'item_type' => $type,    'read_only' => 0,  );  $indexes = search_api_index_load_multiple(FALSE, $conditions);  if (!$indexes) {    return;  }  search_api_track_item_change_for_indexes($type, $item_ids, $indexes);}/** * Marks the items with the specified IDs as "dirty" for the given indexes. * * @param string $type *   The item type of the items. * @param array $item_ids *   The item IDs. * @param SearchApiIndex[] $indexes *   The indexes for which to mark the items as "dirty". */function search_api_track_item_change_for_indexes($type, array $item_ids, $indexes) {  try {    $returned_indexes = search_api_get_datasource_controller($type)->trackItemChange($item_ids, $indexes);    if (isset($returned_indexes)) {      $indexes = $returned_indexes;    }    foreach ($indexes as $index) {      if (!empty($index->options['index_directly'])) {        // For indexes with the index_directly option set, queue the items to be        // indexed at the end of the request.        try {          search_api_index_specific_items_delayed($index, $item_ids);        }        catch (SearchApiException $e) {          watchdog_exception('search_api', $e);        }      }    }  }  catch (SearchApiException $e) {    $vars['%item_type'] = $type;    watchdog_exception('search_api', $e, '%type while updating items of type %item_type: !message in %function (line %line of %file).', $vars);  }}/** * Marks items as queued for indexing for the specified index. * * @param SearchApiIndex $index *   The index on which items were queued. * @param array $item_ids *   The ids of the queued items. * * @deprecated *   As of Search API 1.10, the cron queue is not used for indexing anymore, *   therefore this function has become useless. It will, along with *   SearchApiDataSourceControllerInterface::trackItemQueued(), be removed in *   the Drupal 8 version of this module. */function search_api_track_item_queued(SearchApiIndex $index, array $item_ids) {  try {    $index->datasource()->trackItemQueued($item_ids, $index);  }  catch (SearchApiException $e) {    watchdog_exception('search_api', $e);  }}/** * Marks items as successfully indexed for the specified index. * * @param SearchApiIndex $index *   The index on which items were indexed. * @param array $item_ids *   The ids of the indexed items. */function search_api_track_item_indexed(SearchApiIndex $index, array $item_ids) {  $index->datasource()->trackItemIndexed($item_ids, $index);  module_invoke_all('search_api_items_indexed', $index, $item_ids);}/** * Removes items from all indexes. * * @param $type *   The type of the items. * @param array $item_ids *   The IDs of the deleted items. */function search_api_track_item_delete($type, array $item_ids) {  // First, delete the item from the tracking table.  $conditions = array(    'enabled' => 1,    'item_type' => $type,    'read_only' => 0,  );  $indexes = search_api_index_load_multiple(FALSE, $conditions);  if ($indexes) {    try {      $changed_indexes = search_api_get_datasource_controller($type)->trackItemDelete($item_ids, $indexes);      if (isset($changed_indexes)) {        $indexes = $changed_indexes;      }    }    catch (SearchApiException $e) {      $vars['%item_type'] = $type;      watchdog_exception('search_api', $e, '%type while deleting items of type %item_type: !message in %function (line %line of %file).', $vars);    }  }  // Then, delete it from all servers. Servers of disabled indexes have to be  // considered, too!  $conditions['enabled'] = 0;  $indexes = array_merge($indexes, search_api_index_load_multiple(FALSE, $conditions));  foreach ($indexes as $index) {    try {      if ($server = $index->server()) {        $server->deleteItems($item_ids, $index);      }    }    catch (Exception $e) {      $vars['%item_type'] = $type;      watchdog_exception('search_api', $e, '%type while deleting items of type %item_type: !message in %function (line %line of %file).', $vars);    }  }}/** * Checks for pending tasks on one or all enabled search servers. * * @param SearchApiServer|null $server *   (optional) The server whose tasks should be checked. If not given, the *   tasks for all enabled servers are checked. * * @return bool *   TRUE if all tasks (for the specific server, if $server was given) were *   executed successfully, or if there were no tasks. FALSE if there are still *   pending tasks. */function search_api_server_tasks_check(SearchApiServer $server = NULL) {  $select = db_select('search_api_task', 't')    ->fields('t')    // Only retrieve tasks we can handle.    ->condition('t.type', array('addIndex', 'fieldsUpdated', 'removeIndex', 'deleteItems'));  if ($server) {    $select->condition('t.server_id', $server->machine_name);  }  else {    $select->innerJoin('search_api_server', 's', 't.server_id = s.machine_name AND s.enabled = 1');    // By ordering by the server, we can later just load them when we reach them    // while looping through the tasks. It is very unlikely there will be tasks    // for more than one or two servers, so a *_load_multiple() probably    // wouldn't bring any significant advantages, but complicate the code.    $select->orderBy('t.server_id');  }  // Store a count query for later checking whether all tasks were processed  // successfully.  $count_query = $select->countQuery();  // Sometimes the order of tasks might be important, so make sure to order by  // the task ID (which should be in order of insertion).  $select->orderBy('t.id');  $tasks = $select->execute();  $executed_tasks = array();  foreach ($tasks as $task) {    if (!$server || $server->machine_name != $task->server_id) {      $server = search_api_server_load($task->server_id);      if (!$server) {        continue;      }    }    switch ($task->type) {      case 'addIndex':        $index = search_api_index_load($task->index_id);        if ($index) {          $server->addIndex($index);        }        break;      case 'fieldsUpdated':        $index = search_api_index_load($task->index_id);        if ($index) {          if ($task->data) {            $index->original = unserialize($task->data);          }          $server->fieldsUpdated($index);        }        break;      case 'removeIndex':        $index = search_api_index_load($task->index_id);        if ($index) {          $server->removeIndex($index ? $index : $task->index_id);        }        break;      case 'deleteItems':        $ids = $task->data ? unserialize($task->data) : 'all';        $index = $task->index_id ? search_api_index_load($task->index_id) : NULL;        // Since a failed load returns (for stupid menu handler reasons) FALSE,        // not NULL, we have to make doubly sure here not to pass an invalid        // value (and cause a fatal error).        $index = $index ? $index : NULL;        $server->deleteItems($ids, $index);        break;      default:        // This should never happen.        continue;    }    $executed_tasks[] = $task->id;  }  // If there were no tasks (we recognized), return TRUE.  if (!$executed_tasks) {    return TRUE;  }  // Otherwise, delete the executed tasks and check if new tasks were created.  search_api_server_tasks_delete($executed_tasks);  return $count_query->execute()->fetchField() === 0;}/** * Adds an entry into a server's list of pending tasks. * * @param SearchApiServer $server *   The server for which a task should be remembered. * @param $type *   The type of task to perform. * @param SearchApiIndex|string|null $index *   (optional) If applicable, the index to which the task pertains (or its *   machine name). * @param mixed $data *   (optional) If applicable, some further data necessary for the task. */function search_api_server_tasks_add(SearchApiServer $server, $type, $index = NULL, $data = NULL) {  db_insert('search_api_task')    ->fields(array(      'server_id' => $server->machine_name,      'type' => $type,      'index_id' => $index ? (is_object($index) ? $index->machine_name : $index) : NULL,      'data' => isset($data) ? serialize($data) : NULL,    ))    ->execute();}/** * Removes pending server tasks from the list. * * @param array|null $ids *   (optional) The IDs of the pending server tasks to delete. Set to NULL *   to not filter by IDs. * @param SearchApiServer|null $server *   (optional) A server for which the tasks should be deleted. Set to NULL to *   delete tasks from all servers. * @param SearchApiIndex|string|null $index *   (optional) An index (or its machine name) for which the tasks should be *   deleted. Set to NULL to delete tasks for all indexes. */function search_api_server_tasks_delete(array $ids = NULL, SearchApiServer $server = NULL, $index = NULL) {  $delete = db_delete('search_api_task');  if ($ids) {    $delete->condition('id', $ids);  }  if ($server) {    $delete->condition('server_id', $server->machine_name);  }  if ($index) {    $delete->condition('index_id', $index->machine_name);  }  $delete->execute();}/** * Recalculates the saved fields of an index. * * This is mostly necessary when the multiplicity of the underlying properties * change. The method will re-examine the data structure of the entities in each * index and, if a discrepancy is spotted, re-save that index with updated * fields options (thus, of course, also triggering a re-indexing operation). * * @param SearchApiIndex[]|false $indexes *   An array of SearchApiIndex objects on which to perform the operation, or *   FALSE to perform it on all indexes. */function search_api_index_recalculate_fields($indexes = FALSE) {  if (!is_array($indexes)) {    $indexes = search_api_index_load_multiple(FALSE);  }  $stored_keys = drupal_map_assoc(array('type', 'entity_type', 'real_type', 'boost'));  foreach ($indexes as $index) {    if (empty($index->options['fields'])) {      continue;    }    // We have to clear the cache, both static and stored, before using    // getFields(). Otherwise, we'd just use the stale data which the fields    // options are probably already based on.    cache_clear_all($index->getCacheId() . '-1-0', 'cache');    $index->resetCaches();    // getFields() automatically uses the actual data types to correct possible    // stale data.    $fields = $index->getFields();    foreach ($fields as $key => $field) {      $fields[$key] = array_intersect_key($field, $stored_keys);      if (isset($fields[$key]['boost']) && $fields[$key]['boost'] == '1.0') {        unset($fields[$key]['boost']);      }    }    // Use a more accurate method of determining if the fields settings are    // equal to avoid needlessly re-indexing the whole index.    if ($fields != $index->options['fields']) {      $options = $index->options;      $options['fields'] = $fields;      $index->update(array('options' => $options));    }  }}/** * Test two setting arrays (or individual settings) for equality. * * @param mixed $setting1 *   The first setting (array). * @param mixed $setting2 *   The second setting (array). * * @return bool *   TRUE if both settings are identical, FALSE otherwise. * * @deprecated The simple "==" operator will achieve the same. */function _search_api_settings_equals($setting1, $setting2) {  if (!is_array($setting1) || !is_array($setting2)) {    return $setting1 == $setting2;  }  foreach ($setting1 as $key => $value) {    if (!array_key_exists($key, $setting2)) {      return FALSE;    }    if (!_search_api_settings_equals($value, $setting2[$key])) {      return FALSE;    }    unset($setting2[$key]);  }  // If any keys weren't unset previously, they are not present in $setting1 and  // the two are different.  return !$setting2;}/** * Indexes items for the specified index. * * Only items marked as changed are indexed, in their order of change (if * known). * * @param SearchApiIndex $index *   The index on which items should be indexed. * @param int $limit *   (optional) The number of items which should be indexed at most. Defaults to *   -1, which means that all changed items should be indexed. * * @return int *   Number of successfully indexed items. * * @throws SearchApiException *   If any error occurs during indexing. */function search_api_index_items(SearchApiIndex $index, $limit = -1) {  // Don't try to index on read-only indexes.  if ($index->read_only) {    return 0;  }  $ids = search_api_get_items_to_index($index, $limit);  return $ids ? count(search_api_index_specific_items($index, $ids)) : 0;}/** * Indexes the specified items on the given index. * * Items which were successfully indexed are marked as such afterwards. * * @param SearchApiIndex $index *   The index on which items should be indexed. * @param array $ids *   The IDs of the items which should be indexed. * * @return array *   The IDs of all successfully indexed items. * * @throws SearchApiException *   If any error occurs during indexing. */function search_api_index_specific_items(SearchApiIndex $index, array $ids) {  // Before doing anything else, check whether there are pending tasks that need  // to be executed on the server. It might be important that they are executed  // before any indexing occurs.  if (!search_api_server_tasks_check($index->server())) {    throw new SearchApiException(t('Could not index items since important pending server tasks could not be performed.'));  }  $items = $index->loadItems($ids);  // Clone items because data alterations may alter them.  $cloned_items = array();  foreach ($items as $id => $item) {    if (is_object($item)) {      $cloned_items[$id] = clone $item;    }    else {      // Normally, items that can't be loaded shouldn't be returned by      // entity_load (and other loadItems() implementations). Therefore, this is      // an extremely rare case, which seems to happen during installation for      // some specific setups.      $type = search_api_get_item_type_info($index->item_type);      $type = $type ? $type['name'] : $index->item_type;      watchdog('search_api', "Error during indexing: invalid item loaded for @type with ID @id.", array('@id' => $id, '@type' => $type), WATCHDOG_WARNING);    }  }  $indexed = $items ? $index->index($cloned_items) : array();  if ($indexed) {    search_api_track_item_indexed($index, $indexed);    // If some items could not be indexed, we don't want to try re-indexing    // them right away, so we mark them as "freshly" changed. Sadly, there is    // no better way than to mark them as indexed first...    if (count($indexed) < count($ids)) {      // Believe it or not but this is actually quite faster than the equivalent      // $diff = array_diff($ids, $indexed);      $diff = array_keys(array_diff_key(array_flip($ids), array_flip($indexed)));      $index->datasource()->trackItemIndexed($diff, $index);      $index->datasource()->trackItemChange($diff, array($index));    }  }  return $indexed;}/** * Queues items for indexing at the end of the page request. * * @param SearchApiIndex $index *   The index on which items should be indexed. * @param array $ids *   The IDs of the items which should be indexed. * * @return array *   The current contents of the queue, as a reference. * * @see search_api_index_specific_items() * @see _search_api_index_queued_items() */function &search_api_index_specific_items_delayed(SearchApiIndex $index = NULL, array $ids = array()) {  // We cannot use drupal_static() here because the static cache is reset during  // batch processing, which breaks batch handling.  static $queue = array();  static $registered = FALSE;  // Only register the shutdown function once.  if (empty($registered)) {    drupal_register_shutdown_function('_search_api_index_queued_items');    $registered = TRUE;  }  // Allow for empty call to just retrieve the queue.  if ($index && $ids) {    $index_id = $index->machine_name;    $queue += array($index_id => array());    $queue[$index_id] += drupal_map_assoc($ids);  }  return $queue;}/** * Returns a list of items that need to be indexed for the specified index. * * @param SearchApiIndex $index *   The index for which items should be retrieved. * @param $limit *   The maximum number of items to retrieve. -1 means no limit. * * @return array *   An array of IDs of items that need to be indexed. */function search_api_get_items_to_index(SearchApiIndex $index, $limit = -1) {  if ($limit == 0) {    return array();  }  return $index->datasource()->getChangedItems($index, $limit);}/** * Creates a search query on a specified search index. * * @param $id *   The ID or machine name of the index to execute the search on. * @param $options *   An associative array of options to be passed to *   SearchApiQueryInterface::__construct(). * * @return SearchApiQueryInterface *   An object for searching on the specified index. * * @throws SearchApiException *   If the index is unknown or disabled, or some other error was encountered. */function search_api_query($id, array $options = array()) {  $index = search_api_index_load($id);  if (!$index) {    throw new SearchApiException(t('Unknown index with ID @id.', array('@id' => $id)));  }  return $index->query($options);}/** * Stores or retrieves a search executed in this page request. * * Static storage for the searches executed during the current page request. Can * used to store an executed search, or to retrieve a previously stored search. * * @param $search_id *   For pages displaying multiple searches, an optional ID identifying the *   search in questions. When storing a search, this is filled automatically, *   unless it is manually set. * @param SearchApiQuery $query *   When storing an executed search, the query that was executed. NULL *   otherwise. * @param array $results *   When storing an executed search, the returned results as specified by *   SearchApiQueryInterface::execute(). An empty array, otherwise. * * @return array *   If a search with the specified ID was executed, an array containing *   ($query, $results) as used in this function's parameters. If $search_id is *   NULL, an array of all executed searches will be returned, keyed by ID. */function search_api_current_search($search_id = NULL, SearchApiQuery $query = NULL, array $results = array()) {  $searches = &drupal_static(__FUNCTION__, array());  if (isset($query)) {    if (!isset($search_id)) {      $search_id = $query->getOption('search id');    }    $base = $search_id;    $i = 0;    while (isset($searches[$search_id])) {      $search_id = $base . '-' . ++$i;    }    $searches[$search_id] = array($query, $results);  }  if (isset($search_id)) {    return isset($searches[$search_id]) ? $searches[$search_id] : NULL;  }  return $searches;}/** * Returns all field types recognized by the Search API framework. * * @return array *   An associative array with all recognized types as keys, mapped to their *   translated display names. * * @see search_api_default_field_types() * @see search_api_get_data_type_info() */function search_api_field_types() {  $types = search_api_default_field_types();  foreach (search_api_get_data_type_info() as $id => $type) {    $types[$id] = $type['name'];  }  return $types;}/** * Returns the default field types recognized by the Search API framework. * * @return array *   An associative array with the default types as keys, mapped to their *   translated display names. */function search_api_default_field_types() {  return array(    'text' => t('Fulltext'),    'string' => t('String'),    'integer' => t('Integer'),    'decimal' => t('Decimal'),    'date' => t('Date'),    'duration' => t('Duration'),    'boolean' => t('Boolean'),    'uri' => t('URI'),  );}/** * Returns either all custom field type definitions, or a specific one. * * @param $type *   If specified, the type whose definition should be returned. * * @return array *   If $type was not given, an array containing all custom data types, in the *   format specified by hook_search_api_data_type_info(). *   Otherwise, the definition for the given type, or NULL if it is unknown. * * @see hook_search_api_data_type_info() */function search_api_get_data_type_info($type = NULL) {  $types = &drupal_static(__FUNCTION__);  if (!isset($types)) {    $default_types = search_api_default_field_types();    $types = module_invoke_all('search_api_data_type_info');    $types = $types ? $types : array();    foreach ($types as &$type_info) {      if (!isset($type_info['fallback']) || !isset($default_types[$type_info['fallback']])) {        $type_info['fallback'] = 'string';      }    }    drupal_alter('search_api_data_type_info', $types);  }  if (isset($type)) {    return isset($types[$type]) ? $types[$type] : NULL;  }  return $types;}/** * Returns either a list of all available service infos, or a specific one. * * @see hook_search_api_service_info() * * @param string|null $id *   The ID of the service info to retrieve. * * @return array *   If $id was not specified, an array of all available service classes. *   Otherwise, either the service info with the specified id (if it exists), *   or NULL. Service class information is formatted as specified by *   hook_search_api_service_info(), with the addition of a "module" key *   specifying the module that adds a certain class. */function search_api_get_service_info($id = NULL) {  $services = &drupal_static(__FUNCTION__);  if (!isset($services)) {    // Inlined version of module_invoke_all() to add "module" keys.    $services = array();    foreach (module_implements('search_api_service_info') as $module) {      $function = $module . '_search_api_service_info';      if (function_exists($function)) {        $new_services = $function();        if (isset($new_services) && is_array($new_services)) {          foreach ($new_services as $service => $info) {            $new_services[$service] += array('module' => $module);          }        }        $services += $new_services;      }    }    // Same for drupal_alter().    foreach (module_implements('search_api_service_info_alter') as $module) {      $function = $module . '_search_api_service_info_alter';      if (function_exists($function)) {        $old = $services;        $function($services);        if ($new_services = array_diff_key($services, $old)) {          foreach ($new_services as $service => $info) {            $services[$service] += array('module' => $module);          }        }      }    }  }  if (isset($id)) {    return isset($services[$id]) ? $services[$id] : NULL;  }  return $services;}/** * Returns information for either all item types, or a specific one. * * @param string|null $type *   If set, the item type whose information should be returned. * * @return array|null *   If $type is given, either an array containing the information of that item *   type, or NULL if it is unknown. Otherwise, an array keyed by type IDs *   containing the information for all item types. Item type information is *   formatted as specified by hook_search_api_item_type_info(), with the *   addition of a "module" key specifying the module that adds a certain type. * * @see hook_search_api_item_type_info() */function search_api_get_item_type_info($type = NULL) {  $types = &drupal_static(__FUNCTION__);  if (!isset($types)) {    // Inlined version of module_invoke_all() to add "module" keys.    $types = array();    foreach (module_implements('search_api_item_type_info') as $module) {      $function = $module . '_search_api_item_type_info';      if (function_exists($function)) {        $new_types = $function();        if (isset($new_types) && is_array($new_types)) {          foreach ($new_types as $id => $info) {            $new_types[$id] += array('module' => $module);          }        }        $types += $new_types;      }    }    // Same for drupal_alter().    foreach (module_implements('search_api_item_type_info_alter') as $module) {      $function = $module . '_search_api_item_type_info_alter';      if (function_exists($function)) {        $old = $types;        $function($types);        if ($new_types = array_diff_key($types, $old)) {          foreach ($new_types as $id => $info) {            $types[$id] += array('module' => $module);          }        }      }    }  }  if (isset($type)) {    return isset($types[$type]) ? $types[$type] : NULL;  }  return $types;}/** * Get a data source controller object for the specified type. * * @param $type *   The type whose data source controller should be returned. * * @return SearchApiDataSourceControllerInterface *   The type's data source controller. * * @throws SearchApiException *   If the type is unknown or specifies an invalid data source controller. */function search_api_get_datasource_controller($type) {  $datasources = &drupal_static(__FUNCTION__, array());  if (empty($datasources[$type])) {    $info = search_api_get_item_type_info($type);    if (isset($info['datasource controller']) && class_exists($info['datasource controller'])) {      $datasources[$type] = new $info['datasource controller']($type);    }    if (empty($datasources[$type]) || !($datasources[$type] instanceof SearchApiDataSourceControllerInterface)) {      unset($datasources[$type]);      throw new SearchApiException(t('Unknown or invalid item type @type.', array('@type' => $type)));    }  }  return $datasources[$type];}/** * Returns a list of all available data alter callbacks. * * @see hook_search_api_alter_callback_info() * * @return array *   An array of all available data alter callbacks, keyed by function name. */function search_api_get_alter_callbacks() {  $callbacks = &drupal_static(__FUNCTION__);  if (!isset($callbacks)) {    $callbacks = module_invoke_all('search_api_alter_callback_info');    // Fill optional settings with default values.    foreach ($callbacks as $id => $callback) {      $callbacks[$id] += array('weight' => 0);    }    // Invoke alter hook.    drupal_alter('search_api_alter_callback_info', $callbacks);  }  return $callbacks;}/** * Returns a list of all available pre- and post-processors. * * @see hook_search_api_processor_info() * * @return array *   An array of all available processors, keyed by id. */function search_api_get_processors() {  $processors = &drupal_static(__FUNCTION__);  if (!isset($processors)) {    $processors = module_invoke_all('search_api_processor_info');    // Fill optional settings with default values.    foreach ($processors as $id => $processor) {      $processors[$id] += array('weight' => 0);    }    // Invoke alter hook.    drupal_alter('search_api_processor_info', $processors);  }  return $processors;}/** * Implements hook_search_api_query_alter(). * * Adds node access to the query, if enabled. * * @param SearchApiQueryInterface $query *   The SearchApiQueryInterface object representing the search query. */function search_api_search_api_query_alter(SearchApiQueryInterface $query) {  global $user;  $index = $query->getIndex();  // Only add node access if the necessary fields are indexed in the index, and  // unless disabled explicitly by the query.  $type = $index->getEntityType();  if (!empty($index->options['data_alter_callbacks']["search_api_alter_{$type}_access"]['status']) && !$query->getOption('search_api_bypass_access')) {    $account = $query->getOption('search_api_access_account', $user);    if (is_numeric($account)) {      $account = user_load($account);    }    if (is_object($account)) {      try {        _search_api_query_add_node_access($account, $query, $type);      }      catch (SearchApiException $e) {        watchdog_exception('search_api', $e);      }    }    else {      $account = $query->getOption('search_api_access_account', '(' . t('none') . ')');      if (is_object($account)) {        $account = $account->uid;      }      if (!is_scalar($account)) {        $account = var_export($account, TRUE);      }      watchdog('search_api', 'An illegal user UID was given for node access: @uid.', array('@uid' => $account), WATCHDOG_WARNING);    }  }}/** * Adds a node access filter to a search query, if applicable. * * @param object $account *   The user object, who searches. * @param SearchApiQueryInterface $query *   The query to which a node access filter should be added, if applicable. * @param string $type *   (optional) The type of search – either "node" or "comment". Defaults to *   "node". * * @throws SearchApiException *   If not all necessary fields are indexed on the index. */function _search_api_query_add_node_access($account, SearchApiQueryInterface $query, $type = 'node') {  // Don't do anything if the user can access all content.  if (user_access('bypass node access', $account)) {    return;  }  $is_comment = ($type == 'comment');  // Check whether the necessary fields are indexed.  $fields = $query->getIndex()->options['fields'];  $required = array('search_api_access_node', 'status');  if (!$is_comment) {    $required[] = 'author';  }  foreach ($required as $field) {    if (empty($fields[$field])) {      $vars['@field'] = $field;      $vars['@index'] = $query->getIndex()->name;      throw new SearchApiException(t('Required field @field not indexed on index @index. Could not perform access checks.', $vars));    }  }  // If the user cannot access content/comments at all, return no results.  if (!user_access('access content', $account) || ($is_comment && !user_access('access comments', $account))) {    // Simple hack for returning no results.    $query->condition('status', 0);    $query->condition('status', 1);    watchdog('search_api', 'User @name tried to execute a search, but cannot access content.', array('@name' => theme('username', array('account' => $account))), WATCHDOG_NOTICE);    return;  }  // Filter by the "published" status.  $published = $is_comment ? COMMENT_PUBLISHED : NODE_PUBLISHED;  if (!$is_comment && user_access('view own unpublished content')) {    $filter = $query->createFilter('OR');    $filter->condition('status', $published);    $filter->condition('author', $account->uid);    $query->filter($filter);  }  else {    $query->condition('status', $published);  }  // Filter by node access grants.  $filter = $query->createFilter('OR');  $grants = node_access_grants('view', $account);  foreach ($grants as $realm => $gids) {    foreach ($gids as $gid) {      $filter->condition('search_api_access_node', "node_access_$realm:$gid");    }  }  $filter->condition('search_api_access_node', 'node_access__all');  $query->filter($filter);}/** * Determines whether a field of the given type contains text data. * * Can also be used to find other types. * * @param string $type *   The type for which to check. * @param array $allowed *   Optionally, an array of allowed types. * * @return bool *   TRUE if $type is either one of the specified types or a list of such *   values. FALSE otherwise. * * @see search_api_extract_inner_type() */function search_api_is_text_type($type, array $allowed = array('text')) {  return array_search(search_api_extract_inner_type($type), $allowed) !== FALSE;}/** * Utility function for determining whether a field of the given type contains * a list of any kind. * * @param $type *   A string containing the type to check. * * @return bool *   TRUE iff $type is a list type ("list<*>"). */function search_api_is_list_type($type) {  return substr($type, 0, 5) == 'list<';}/** * Utility function for determining the nesting level of a list type. * * @param $type *   A string containing the type to check. * * @return int *   The nesting level of the type. 0 for singular types, 1 for lists of *   singular types, etc. */function search_api_list_nesting_level($type) {  $level = 0;  while (search_api_is_list_type($type)) {    $type = substr($type, 5, -1);    ++$level;  }  return $level;}/** * Utility function for nesting a type to the same level as another type. * I.e., after <code>$t = search_api_nest_type($type, $nested_type);</code> is * executed, the following statements will always be true: * @code * search_api_list_nesting_level($t) == search_api_list_nesting_level($nested_type); * search_api_extract_inner_type($t) == search_api_extract_inner_type($type); * @endcode * * @param $type *   The type to wrap. * @param $nested_type *   Another type, determining the nesting level. * * @return string *   A list version of $type, as specified above. */function search_api_nest_type($type, $nested_type) {  while (search_api_is_list_type($nested_type)) {    $nested_type = substr($nested_type, 5, -1);    $type = "list<$type>";  }  return $type;}/** * Utility function for extracting the contained primitive type of a list type. * * @param $type *   A string containing the list type to process. * * @return string *   A string containing the primitive type contained within the list, e.g. *   "text" for "list<text>" (or for "list<list<text>>"). If $type is no list *   type, it is returned unchanged. */function search_api_extract_inner_type($type) {  while (search_api_is_list_type($type)) {    $type = substr($type, 5, -1);  }  return $type;}/** * Helper function for reacting to index updates with regards to the datasource. * * When an overridden index is reverted, its numerical ID will sometimes change. * Since the default datasource implementation uses that for referencing * indexes, the index ID in the items table must be updated accordingly. This is * implemented in this function. * * Modules implementing other datasource controllers, that use a table other * than {search_api_item}, can use this function, too. It should be called * unconditionally in a hook_search_api_index_update() implementation. If this * function isn't used, similar code should be added there. * * However, note that this is only necessary (and this function should only be * called) if the indexes are referenced by numerical ID in the items table. * * @param SearchApiIndex $index *   The index that was changed. * @param string $table *   The table containing items information, analogous to {search_api_item}. * @param string $column *   The column in $table that holds the index's numerical ID. */function search_api_index_update_datasource(SearchApiIndex $index, $table, $column = 'index_id') {  if ($index->id != $index->original->id) {    db_update($table)      ->fields(array($column => $index->id))      ->condition($column, $index->original->id)      ->execute();  }}/** * Extracts specific field values from an EntityMetadataWrapper object. * * @param EntityMetadataWrapper $wrapper *   The wrapper from which to extract fields. * @param array $fields *   The fields to extract, as stored in an index. I.e., the array keys are *   field names, the values are arrays with at least a "type" key present. * @param array $value_options *   An array of options that should be passed to the *   EntityMetadataWrapper::value() method (see there). * * @return array *   The $fields array with additional "value" and "original_type" keys set. */function search_api_extract_fields(EntityMetadataWrapper $wrapper, array $fields, array $value_options = array()) {  $value_options += array(    'identifier' => TRUE,  );  // If $wrapper is a list of entities, we have to aggregate their field values.  $wrapper_info = $wrapper->info();  if (search_api_is_list_type($wrapper_info['type'])) {    foreach ($fields as &$info) {      $info['value'] = array();      $info['original_type'] = $info['type'];    }    unset($info);    try {      foreach ($wrapper as $w) {        $nested_fields = search_api_extract_fields($w, $fields, $value_options);        foreach ($nested_fields as $field => $info) {          if (isset($info['value'])) {            $fields[$field]['value'][] = $info['value'];          }          if (isset($info['original_type'])) {            $fields[$field]['original_type'] = $info['original_type'];          }        }      }    }    catch (EntityMetadataWrapperException $e) {      // Catch exceptions caused by not set list values.    }    return $fields;  }  $nested = array();  $entity_infos = entity_get_info();  foreach ($fields as $field => &$info) {    $pos = strpos($field, ':');    if ($pos === FALSE) {      // Set "defaults" in case an error occurs later.      $info['value'] = NULL;      $info['original_type'] = $info['type'];      if (isset($wrapper->$field)) {        try {          // Set the original type according to the field wrapper's info.          $property_info = $wrapper->$field->info();          $info['original_type'] = $property_info['type'];          // Extract the basic value from the field wrapper.          $info['value'] = $wrapper->$field->value($value_options);          // For entities, we need to take care to differentiate between          // entities with ID 0 and empty fields. In the latter case, the          // wrapper's value() method returns, when called with "identifier =          // TRUE", FALSE instead of the (more logical) NULL.          $is_entity = isset($entity_infos[search_api_extract_inner_type($property_info['type'])]);          if ($is_entity && $info['value'] === FALSE) {            $info['value'] = NULL;          }          // If we index the field as fulltext, we also include the entity label          // or option list label, if applicable.          if (search_api_is_text_type($info['type']) && isset($info['value'])) {            if ($wrapper->$field->optionsList('view')) {              _search_api_add_option_values($info['value'], $wrapper->$field->optionsList('view'));            }            elseif ($is_entity) {              $info['value'] = _search_api_extract_entity_value($wrapper->$field, TRUE);            }          }        }        catch (EntityMetadataWrapperException $e) {          // This might happen for entity-typed properties that are NULL, e.g.,          // for comments without parent.        }      }    }    else {      list($prefix, $key) = explode(':', $field, 2);      $nested[$prefix][$key] = $info;    }  }  unset($info);  foreach ($nested as $prefix => $nested_fields) {    if (isset($wrapper->$prefix)) {      $nested_fields = search_api_extract_fields($wrapper->$prefix, $nested_fields, $value_options);      foreach ($nested_fields as $field => $info) {        $fields["$prefix:$field"] = $info;      }    }    else {      foreach ($nested_fields as &$info) {        $info['value'] = NULL;        $info['original_type'] = $info['type'];      }    }  }  return $fields;}/** * Helper method for adding additional text data to fields with an option list. */function _search_api_add_option_values(&$value, array $options) {  if (is_array($value)) {    foreach ($value as &$v) {      _search_api_add_option_values($v, $options);    }    return;  }  if (is_scalar($value) && isset($options[$value])) {    $value .= ' ' . $options[$value];  }}/** * Helper method for extracting the ID (and possibly label) of an entity-valued field. */function _search_api_extract_entity_value(EntityMetadataWrapper $wrapper, $fulltext = FALSE) {  $v = $wrapper->value();  if (is_array($v)) {    $ret = array();    foreach ($wrapper as $item) {      $values = _search_api_extract_entity_value($item, $fulltext);      if ($values) {        $ret[] = $values;      }    }    return $ret;  }  if ($v) {    $ret = $wrapper->getIdentifier();    if ($fulltext && ($label = $wrapper->label())) {      $ret .= ' ' . $label;    }    return $ret;  }  return NULL;}/** * Load the search server with the specified id. * * @param $id *   The search server's id. * @param $reset *   Whether to reset the internal cache. * * @return SearchApiServer *   An object representing the server with the specified id. */function search_api_server_load($id, $reset = FALSE) {  $ret = search_api_server_load_multiple(array($id), array(), $reset);  return $ret ? reset($ret) : FALSE;}/** * Load multiple servers at once, determined by IDs or machine names, or by * other conditions. * * @see entity_load() * * @param array|false $ids *   An array of server IDs or machine names, or FALSE to load all servers. * @param array $conditions *   An array of conditions on the {search_api_server} table in the form *   'field' => $value. * @param bool $reset *   Whether to reset the internal entity_load cache. * * @return SearchApiServer[] *   An array of server objects keyed by machine name. */function search_api_server_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {  $servers = entity_load('search_api_server', $ids, $conditions, $reset);  return entity_key_array_by_property($servers, 'machine_name');}/** * Entity uri callback. */function search_api_server_url(SearchApiServer $server) {  return array(    'path' => 'admin/config/search/search_api/server/' . $server->machine_name,    'options' => array(),  );}/** * Title callback for viewing or editing a server or index. */function search_api_admin_item_title($object) {  return $object->name;}/** * Title callback for determining which title should be displayed for the * "delete" local task. * * @param Entity $entity *   The server or index for which the menu link is displayed. * * @return string *   A translated version of either "Delete" or "Revert". */function search_api_title_delete_page(Entity $entity) {  return $entity->hasStatus(ENTITY_OVERRIDDEN) ? t('Revert') : t('Delete');}/** * Determines whether the current user can disable a server or index. * * @param Entity $entity *   The server or index for which the access to the "disable" page is checked. * * @return bool *   TRUE if the "disable" page can be accessed by the user, FALSE otherwise. */function search_api_access_disable_page(Entity $entity) {  return user_access('administer search_api') && !empty($entity->enabled);}/** * Access callback for determining if a server's or index' "delete" page should * be accessible. * * @param Entity $entity *   The server or index for which the access to the delete page is checked. * * @return bool *   TRUE if the delete page can be accessed by the user, FALSE otherwise. */function search_api_access_delete_page(Entity $entity) {  return user_access('administer search_api') && $entity->hasStatus(ENTITY_CUSTOM);}/** * Determines whether a user can access a certain search server or index. * * Used as an access callback in search_api_entity_info(). */function search_api_entity_access() {  return user_access('administer search_api');}/** * Inserts a new search server into the database. * * @param array $values *   An array containing the values to be inserted. * * @return *   The newly inserted server's id, or FALSE on error. */function search_api_server_insert(array $values) {  $server = entity_create('search_api_server', $values);  $server->is_new = TRUE;  $server->save();  return $server->id;}/** * Changes a server's settings. * * @param string|int $id *   The ID or machine name of the server whose values should be changed. * @param array $fields *   The new field values to set. The enabled field can't be set this way, use *   search_api_server_enable() and search_api_server_disable() instead. * * @return int|false *   1 if fields were changed, 0 if the fields already had the desired values. *   FALSE on failure. */function search_api_server_edit($id, array $fields) {  $server = search_api_server_load($id, TRUE);  $ret = $server->update($fields);  return $ret ? 1 : $ret;}/** * Enables a search server. * * Will also check for remembered tasks for this server and execute them. * * @param string|int $id *   The ID or machine name of the server to enable. * * @return int|false *   1 on success, 0 or FALSE on failure. */function search_api_server_enable($id) {  $server = search_api_server_load($id, TRUE);  $ret = $server->update(array('enabled' => 1));  return $ret ? 1 : $ret;}/** * Disables a search server. * * Will also disable all associated indexes and remove them from the server. * * @param string|int $id *   The ID or machine name of the server to disable. * * @return int|false *   1 on success, 0 or FALSE on failure. */function search_api_server_disable($id) {  $server = search_api_server_load($id, TRUE);  $ret = $server->update(array('enabled' => 0));  return $ret ? 1 : $ret;}/** * Clears a search server. * * Will delete all items stored on the server and mark all associated indexes * for re-indexing. * * @param int|string $id *   The ID or machine name of the server to clear. * * @return bool *   TRUE on success, FALSE on failure. */function search_api_server_clear($id) {  $server = search_api_server_load($id);  $success = TRUE;  foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {    $success &= $index->reindex();  }  if ($success) {    $server->deleteItems();  }  return $success;}/** * Deletes a search server and disables all associated indexes. * * @param $id *   The ID or machine name of the server to delete. * * @return int|false *   1 on success, 0 or FALSE on failure. */function search_api_server_delete($id) {  $server = search_api_server_load($id, TRUE);  $server->delete();  return 1;}/** * Loads the Search API index with the specified id. * * @param $id *   The index' id. * @param $reset *   Whether to reset the internal cache. * * @return SearchApiIndex|false *   A completely loaded index object, or FALSE if no such index exists. */function search_api_index_load($id, $reset = FALSE) {  $ret = search_api_index_load_multiple(array($id), array(), $reset);  return reset($ret);}/** * Load multiple indexes at once, determined by IDs or machine names, or by * other conditions. * * @see entity_load() * * @param array|false $ids *   An array of index IDs or machine names, or FALSE to load all indexes. * @param array $conditions *   An array of conditions on the {search_api_index} table in the form *   'field' => $value. * @param bool $reset *   Whether to reset the internal entity_load cache. * * @return SearchApiIndex[] *   An array of index objects keyed by machine name. */function search_api_index_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {  // This line is a workaround for a weird PDO bug in PHP 5.2.  // See http://drupal.org/node/889286.  new SearchApiIndex();  $indexes = entity_load('search_api_index', $ids, $conditions, $reset);  return entity_key_array_by_property($indexes, 'machine_name');}/** * Determines a search index' indexing status. * * @param SearchApiIndex $index *   The index whose indexing status should be determined. * * @return array *   An associative array containing two keys (in this order): *   - indexed: The number of items already indexed in their latest version. *   - total: The total number of items that have to be indexed for this index. */function search_api_index_status(SearchApiIndex $index) {  return $index->datasource()->getIndexStatus($index);}/** * Entity uri callback. */function search_api_index_url(SearchApiIndex $index) {  return array(    'path' => 'admin/config/search/search_api/index/' . $index->machine_name,    'options' => array(),  );}/** * Returns an index's server. * * Used as a property getter callback for the index's "server_entity" prioperty * in search_api_entity_property_info(). * * @param SearchApiIndex $index *   The index whose server should be returned. * * @return SearchApiServer|null *   The server this index currently resides on, or NULL if the index is * currently unassigned. */function search_api_index_get_server(SearchApiIndex $index) {  try {    return $index->server();  }  catch (SearchApiException $e) {    watchdog_exception('search_api', $e);    return NULL;  }}/** * Returns an options list for the "status" property. * * Used as an options list callback in search_api_entity_property_info(). * * @return array *   An array of options, as defined by hook_options_list(). */function search_api_status_options_list() {  return array(    ENTITY_CUSTOM => t('Custom'),    ENTITY_IN_CODE => t('Default'),    ENTITY_OVERRIDDEN => t('Overridden'),    ENTITY_FIXED => t('Fixed'),  );}/** * Inserts a new search index into the database. * * @param array $values *   An array containing the values to be inserted. * * @return *   The newly inserted index' id, or FALSE on error. */function search_api_index_insert(array $values) {  $index = entity_create('search_api_index', $values);  $index->is_new = TRUE;  $index->save();  return $index->id;}/** * Changes an index' settings. * * @param int|string $id *   The edited index' ID or machine name. * @param array $fields *   The new field values to set. * * @return int|false *   1 if fields were changed, 0 if the fields already had the desired values. *   FALSE on failure. */function search_api_index_edit($id, array $fields) {  $index = search_api_index_load($id, TRUE);  $ret = $index->update($fields);  return $ret ? 1 : $ret;}/** * Changes an index' indexed field settings. * * @param int|string $id *   The ID or machine name of the index whose fields should be changed. * @param array $fields *   The new indexed field settings. * * @return int|false *   1 if the field settings were changed, 0 if they already had the desired *   values. FALSE on failure. */function search_api_index_edit_fields($id, array $fields) {  $index = search_api_index_load($id, TRUE);  $options = $index->options;  $options['fields'] = $fields;  $ret = $index->update(array('options' => $options));  return $ret ? 1 : $ret;}/** * Enables a search index. * * @param string|int $id *   The ID or machine name of the index to enable. * * @return int|false *   1 on success, 0 or FALSE on failure. * * @throws SearchApiException *   If the index's server doesn't exist. */function search_api_index_enable($id) {  $index = search_api_index_load($id, TRUE);  $ret = $index->update(array('enabled' => 1));  return $ret ? 1 : $ret;}/** * Disables a search index. * * @param string|int $id *   The ID or machine name of the index to disable. * * @return int|false *   1 on success, 0 or FALSE on failure. * * @throws SearchApiException *   If the index's server doesn't exist. */function search_api_index_disable($id) {  $index = search_api_index_load($id, TRUE);  $ret = $index->update(array('enabled' => 0));  return $ret ? 1 : $ret;}/** * Schedules a search index for re-indexing. * * @param $id *   The ID or machine name of the index to re-index. * * @return bool *   TRUE on success, FALSE on failure. */function search_api_index_reindex($id) {  $index = search_api_index_load($id);  return $index->reindex();}/** * Helper method for marking all items on an index as needing re-indexing. * * @param SearchApiIndex $index *   The index whose items should be re-indexed. */function _search_api_index_reindex(SearchApiIndex $index) {  $index->datasource()->trackItemChange(FALSE, array($index), TRUE);}/** * Clears a search index and schedules all of its items for re-indexing. * * @param $id *   The ID or machine name of the index to clear. * * @return bool *   TRUE on success, FALSE on failure. */function search_api_index_clear($id) {  $index = search_api_index_load($id);  return $index->clear();}/** * Deletes a search index. * * @param $id *   The ID or machine name of the index to delete. * * @return bool *   TRUE on success, FALSE on failure. */function search_api_index_delete($id) {  $index = search_api_index_load($id);  if (!$index) {    return FALSE;  }  $index->delete();  return TRUE;}/** * Sanitizes field values returned from the server. * * @param array $values *   The field values, as returned from the server. See *   SearchApiQueryInterface::execute() for documentation on the structure. * * @return array *   An associative array of field IDs mapped to their sanitized values (scalar *   or array-valued). */function search_api_get_sanitized_field_values(array $values) {  // Sanitize the field values returned from the server. Usually we use  // check_plain(), but this can be overridden by setting the field value to  // an array with "#value" and "#sanitize_callback" keys.  foreach ($values as $field_id => $field_value) {    if (is_array($field_value)      && isset($field_value['#sanitize_callback'])      && ($field_value['#sanitize_callback'] === FALSE || is_callable($field_value['#sanitize_callback']))      && array_key_exists('#value', $field_value)    ) {      $sanitize_callback = $field_value['#sanitize_callback'];      $field_value = $field_value['#value'];    }    else {      $sanitize_callback = 'check_plain';    }    if ($sanitize_callback !== FALSE) {      $field_value = search_api_sanitize_field_value($field_value, $sanitize_callback);    }    $values[$field_id] = $field_value;  }  return $values;}/** * Sanitizes the given field value(s). * * @param mixed $field_value *   A scalar field value, or an array of field values. * @param callable $sanitize_callback *   (optional) The callback to use for sanitizing a scalar value. * * @return mixed *   The sanitized field value(s). */function search_api_sanitize_field_value($field_value, $sanitize_callback = 'check_plain') {  if ($field_value === NULL) {    return $field_value;  }  if (is_scalar($field_value)) {    return call_user_func($sanitize_callback, $field_value);  }  foreach ($field_value as &$nested_value) {    $nested_value = search_api_sanitize_field_value($nested_value, $sanitize_callback);  }  return $field_value;}/** * Options list callback for search indexes. * * @return array *   An array of search index machine names mapped to their human-readable *   names. */function search_api_index_options_list() {  $ret = array(    NULL => '- ' . t('All') . ' -',  );  foreach (search_api_index_load_multiple(FALSE) as $id => $index) {    $ret[$id] = $index->name;  }  return $ret;}/** * Options list callback for entity types. * * Will only include entity types which specify entity property information. * * @return string[] *   An array of entity type machine names mapped to their human-readable *   names. */function search_api_entity_type_options_list() {  $types = array();  foreach (array_keys(entity_get_property_info()) as $type) {    $info = entity_get_info($type);    if ($info) {      $types[$type] = $info['label'];    }  }  return $types;}/** * Options list callback for entity type bundles. * * Will include all bundles for all entity types which specify entity property * information, in a format combining both entity type and bundle. * * @return string[] *   An array of bundle identifiers mapped to their human-readable names. */function search_api_combined_bundle_options_list() {  $types = array();  foreach (array_keys(entity_get_property_info()) as $type) {    $info = entity_get_info($type);    if (!empty($info['bundles'])) {      foreach ($info['bundles'] as $bundle => $bundle_info) {        $types["$type:$bundle"] = $bundle_info['label'];      }    }  }  return $types;}/** * Retrieves a human-readable label for a multi-type index item. * * Provided as a non-object alternative to * SearchApiCombinedEntityDataSourceController::getItemLabel() so it can be used * as a getter callback. * * @param object $item *   An item of the "multiple" item type. * * @return string|null *   Either a human-readable label for the item, or NULL if none is available. */function search_api_get_multi_type_item_label($item) {  $label = entity_label($item->item_type, $item->{$item->item_type});  return $label ? $label : NULL;}/** * Shutdown function which indexes all queued items, if any. */function _search_api_index_queued_items() {  $queue = &search_api_index_specific_items_delayed();  try {    if ($queue) {      $indexes = search_api_index_load_multiple(array_keys($queue));      foreach ($indexes as $index_id => $index) {        search_api_index_specific_items($index, $queue[$index_id]);      }    }    // Reset the queue so we don't index the items twice by accident.    $queue = array();  }  catch (SearchApiException $e) {    watchdog_exception('search_api', $e);  }}/** * Helper function to be used as a "property info alter" callback. * * If a wrapped entity is passed to this function, all its available properties * and fields, regardless of bundle, are added to the wrapper. */function _search_api_wrapper_add_all_properties(EntityMetadataWrapper $wrapper, array $property_info) {  if ($properties = entity_get_all_property_info($wrapper->type())) {    $property_info['properties'] = $properties;  }  return $property_info;}/** * Helper function for converting data to a custom type. */function _search_api_convert_custom_type($callback, $value, $original_type, $type, $nesting_level) {  if ($nesting_level == 0) {    return call_user_func($callback, $value, $original_type, $type);  }  if (!is_array($value)) {    return NULL;  }  --$nesting_level;  $values = array();  foreach ($value as $v) {    $v = _search_api_convert_custom_type($callback, $v, $original_type, $type, $nesting_level);    if (isset($v) && !(is_array($v) && !$v)) {      $values[] = $v;    }  }  return $values;}/** * Determines the number of items indexed on a server for a certain index. * * Used as a helper function in search_api_admin_index_view(). * * @param SearchApiIndex $index *   The index * * @return int *   The number of items found on the server for this index, if the latter is *   enabled. 0 otherwise. * * @throws SearchApiException *   If an error prevented the search from completing. */function _search_api_get_items_on_server(SearchApiIndex $index) {  if (!$index->enabled) {    return 0;  }  // We want the raw count, without facets or other filters. Therefore we don't  // use the query's execute() method but pass it straight to the server for  // evaluation. Since this circumvents the normal preprocessing, which sets the  // fields (on which some service classes might even rely when there are no  // keywords), we set them manually here.  $query = $index->query()    ->fields(array())    ->range(0, 0);  $response = $index->server()->search($query);  return $response['result count'];}/** * Returns a deep copy of the input array. * * The behavior of PHP regarding arrays with references pointing to it is rather * weird. Therefore, we use this helper function in theme_search_api_index() to * create safe copies of such arrays. * * @param array $array *   The array to copy. * * @return array *   A deep copy of the array. */function _search_api_deep_copy(array $array) {  $copy = array();  foreach ($array as $k => $v) {    if (is_array($v)) {      $copy[$k] = _search_api_deep_copy($v);    }    elseif (is_object($v)) {      $copy[$k] = clone $v;    }    elseif ($v) {      $copy[$k] = $v;    }  }  return $copy;}/** * Reacts to a change in the bundle of an entity. * * Used as a helper function in search_api_entity_update(). * * @param $type *   The entity's type. * @param $id *   The entity's ID. * @param $old_bundle *   The entity's previous bundle. * @param $new_bundle *   The entity's new bundle. */function _search_api_entity_datasource_bundle_change($type, $id, $old_bundle, $new_bundle) {  $controller = search_api_get_datasource_controller($type);  $conditions = array(    'enabled' => 1,    'item_type' => $type,    'read_only' => 0,  );  foreach (search_api_index_load_multiple(FALSE, $conditions) as $index) {    if (!empty($index->options['datasource']['bundles'])) {      $bundles = drupal_map_assoc($index->options['datasource']['bundles']);      if (empty($bundles[$new_bundle]) != empty($bundles[$old_bundle])) {        if (empty($bundles[$new_bundle])) {          $controller->trackItemDelete(array($id), array($index));        }        else {          $controller->trackItemInsert(array($id), array($index));        }      }    }  }}/** * Creates and sets a batch for indexing items. * * @param SearchApiIndex $index *   The index for which items should be indexed. * @param int $batch_size *   Number of items to index per batch. * @param int $limit *   Maximum number of items to index. Negative values mean "no limit". * @param int $remaining *   Remaining items to index. * @param bool $drush *   Boolean specifying whether this was called from drush or not. * * @return bool *   Whether the batch was created and set successfully. */function _search_api_batch_indexing_create(SearchApiIndex $index, $batch_size, $limit, $remaining, $drush = FALSE) {  if ($limit !== 0 && $batch_size !== 0) {    $t = !empty($drush) ? 'dt' : 't';    if ($limit < 0 || $limit > $remaining) {      $limit = $remaining;    }    if ($batch_size < 0) {      $batch_size = $remaining;    }    $batch = array(      'title' => $t('Indexing items'),      'operations' => array(        array('_search_api_batch_indexing_callback', array($index, $batch_size, $limit, $drush)),      ),      'progress_message' => $t('Completed about @percentage% of the indexing operation.'),      'finished' => '_search_api_batch_indexing_finished',      'file' => drupal_get_path('module', 'search_api') . '/search_api.module',    );    batch_set($batch);    return TRUE;  }  return FALSE;}/** * Batch API callback for the indexing functionality. * * @param SearchApiIndex $index *   The index for which items should be indexed. * @param integer $batch_size *   Number of items to index per batch. * @param integer $limit *   Maximum number of items to index. * @param boolean $drush *   Boolean specifying whether this was called from drush or not. * @param $context *   An array (or object implementing ArrayAccess) containing the batch context. */function _search_api_batch_indexing_callback(SearchApiIndex $index, $batch_size, $limit, $drush = FALSE, &$context) {  // Persistent data among batch runs.  if (!isset($context['sandbox']['limit'])) {    $context['sandbox']['limit'] = $limit;    $context['sandbox']['batch_size'] = $batch_size;    $context['sandbox']['progress'] = 0;  }  // Persistent data for results.  if (!isset($context['results']['indexed'])) {    $context['results']['indexed'] = 0;    $context['results']['not indexed'] = 0;    $context['results']['drush'] = $drush;  }  // Number of items to index for this run.  $to_index = min($context['sandbox']['limit'] - $context['sandbox']['progress'], $context['sandbox']['batch_size']);  // Index the items.  try {    $indexed = search_api_index_items($index, $to_index);    $context['results']['indexed'] += $indexed;  }  catch (SearchApiException $e) {    watchdog_exception('search_api', $e);    $vars['@message'] = $e->getMessage();    $context['message'] = t('An error occurred during indexing: @message.', $vars);    $context['finished'] = 1;    $context['results']['not indexed'] += $context['sandbox']['limit'] - $context['sandbox']['progress'];    return;  }  // Display progress message.  if ($indexed > 0) {    $format_plural = $context['results']['drush'] === TRUE ? '_search_api_drush_format_plural' : 'format_plural';    $context['message'] = $format_plural($context['results']['indexed'], 'Successfully indexed 1 item.', 'Successfully indexed @count items.');  }  // Some items couldn't be indexed.  if ($indexed !== $to_index) {    $context['results']['not indexed'] += $to_index - $indexed;  }  $context['sandbox']['progress'] += $to_index;  // Everything has been indexed.  if ($indexed === 0 || $context['sandbox']['progress'] >= $context['sandbox']['limit']) {    $context['finished'] = 1;  }  else {    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['limit'];  }}/** * Batch API finishing callback for the indexing functionality. * * @param boolean $success *   Whether the batch finished successfully. * @param array $results *   Detailed information about the result. */function _search_api_batch_indexing_finished($success, $results) {  // Check if called from drush.  if (!empty($results['drush'])) {    $drupal_set_message = 'drush_log';    $format_plural = '_search_api_drush_format_plural';    $t = 'dt';    $success_message = 'success';  }  else {    $drupal_set_message = 'drupal_set_message';    $format_plural = 'format_plural';    $t = 't';    $success_message = 'status';  }  // Display result messages.  if ($success) {    if (!empty($results['indexed'])) {      $drupal_set_message($format_plural($results['indexed'], 'Successfully indexed 1 item.', 'Successfully indexed @count items.'), $success_message);      if (!empty($results['not indexed'])) {       $drupal_set_message($format_plural($results['not indexed'], '1 item could not be indexed. Check the logs for details.', '@count items could not be indexed. Check the logs for details.'), 'warning');      }    }    else {      $drupal_set_message($t("Couldn't index items. Check the logs for details."), 'error');    }  }  else {    $drupal_set_message($t("An error occurred while trying to index items. Check the logs for details."), 'error');  }}
 |