12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729 |
- <?php
- /**
- * 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',
- 'weight' => -10,
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- );
- $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,
- );
- $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,
- );
- $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/status'] = array(
- 'title' => 'Status',
- 'description' => 'Display and work on index status.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('search_api_admin_index_status_form', 5),
- 'access arguments' => array('administer search_api'),
- 'file' => 'search_api.admin.inc',
- 'weight' => -8,
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
- );
- $items[$pre . '/index/%search_api_index/edit'] = array(
- 'title' => 'Settings',
- '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',
- 'weight' => -6,
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
- );
- $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',
- 'weight' => -4,
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
- );
- $items[$pre . '/index/%search_api_index/workflow'] = array(
- 'title' => 'Workflow',
- 'description' => 'Edit index 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',
- 'weight' => -2,
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
- );
- $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,
- );
- return $items;
- }
- /**
- * 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_processor_info' => $hook_info,
- 'search_api_index_items_alter' => $hook_info,
- 'search_api_items_indexed' => $hook_info,
- 'search_api_query_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_server'] = array(
- 'variables' => array(
- 'id' => NULL,
- 'name' => '',
- 'machine_name' => '',
- 'description' => NULL,
- 'enabled' => NULL,
- 'class_name' => NULL,
- 'class_description' => NULL,
- 'options' => array(),
- 'status' => ENTITY_CUSTOM,
- ),
- 'file' => 'search_api.admin.inc',
- );
- $themes['search_api_index'] = array(
- 'variables' => array(
- 'id' => NULL,
- 'name' => '',
- 'machine_name' => '',
- 'description' => NULL,
- 'item_type' => NULL,
- 'enabled' => NULL,
- 'server' => NULL,
- 'options' => array(),
- 'fields' => array(),
- 'indexed_items' => 0,
- '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().
- *
- * Will index $options['cron-limit'] items for each enabled index.
- */
- function search_api_cron() {
- $queue = DrupalQueue::get('search_api_indexing_queue');
- foreach (search_api_index_load_multiple(FALSE, array('enabled' => TRUE, 'read_only' => 0)) as $index) {
- $limit = isset($index->options['cron_limit'])
- ? $index->options['cron_limit']
- : SEARCH_API_DEFAULT_CRON_LIMIT;
- if ($limit) {
- try {
- $task = array('index' => $index->machine_name);
- // Fetch items to index, do not fetch more than the configured amount
- // of batches to be created per cron run to avoid timeouts.
- $ids = search_api_get_items_to_index($index, $limit > 0 ? $limit * variable_get('search_api_batch_per_cron', 10) : -1);
- if (!$ids) {
- continue;
- }
- $batches = $limit > 0 ? array_chunk($ids, $limit, TRUE) : array($ids);
- foreach ($batches as $batch) {
- $task['items'] = $batch;
- $queue->createItem($task);
- }
- // Mark items as queued so they won't be inserted into the queue again
- // on the next cron run.
- search_api_track_item_queued($index, $ids);
- }
- catch (SearchApiException $e) {
- watchdog_exception('search_api', $e);
- }
- }
- }
- }
- /**
- * Implements hook_cron_queue_info().
- *
- * Defines a queue for saved searches that should be checked for new items.
- */
- function search_api_cron_queue_info() {
- return array(
- 'search_api_indexing_queue' => array(
- 'worker callback' => '_search_api_indexing_queue_process',
- 'time' => variable_get('search_api_index_worker_callback_runtime', 15),
- ),
- );
- }
- /**
- * 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',
- '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',
- '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',
- ),
- );
- $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',
- ),
- );
- 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) {
- // Were there any changes in the server's indexes while it was disabled?
- $tasks = variable_get('search_api_tasks', array());
- if (isset($tasks[$server->machine_name])) {
- foreach ($tasks[$server->machine_name] as $index_id => $index_tasks) {
- $index = search_api_index_load($index_id);
- foreach ($index_tasks as $task) {
- switch ($task) {
- case 'add':
- $server->addIndex($index);
- break;
- case 'clear':
- $server->deleteItems('all', $index);
- break;
- case 'clear all':
- // Would normally be used with a fake index ID of "", since it
- // doesn't matter.
- $server->deleteItems('all');
- break;
- case 'fields':
- if ($server->fieldsUpdated($index)) {
- _search_api_index_reindex($index);
- }
- break;
- case 'remove':
- $server->removeIndex($index ? $index : $index_id);
- break;
- default:
- if (substr($task, 0, 7) == 'delete-') {
- $id = substr($task, 7);
- $server->deleteItems(array($id), $index);
- }
- else {
- watchdog('search_api', t('Unknown task "@task" for server "@name".', array('@task' => $task, '@name' => $server->machine_name)), NULL, WATCHDOG_WARNING);
- }
- }
- }
- }
- unset($tasks[$server->machine_name]);
- variable_set('search_api_tasks', $tasks);
- }
- }
- else {
- foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name, 'enabled' => 1)) as $index) {
- $index->update(array('enabled' => 0));
- }
- }
- }
- }
- /**
- * 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));
- }
- $tasks = variable_get('search_api_tasks', array());
- unset($tasks[$server->machine_name]);
- variable_set('search_api_tasks', $tasks);
- }
- /**
- * 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 table this module provides.
- search_api_index_update_datasource($index, 'search_api_item');
- // 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) {
- if ($old_server->enabled) {
- $old_server->removeIndex($index);
- }
- else {
- $tasks = variable_get('search_api_tasks', array());
- // When we add or remove an index, we can ignore all other tasks.
- $tasks[$old_server->machine_name][$index->machine_name] = array('remove');
- variable_set('search_api_tasks', $tasks);
- }
- }
- }
- if ($index->server) {
- $new_server = $index->server(TRUE);
- // If the server is enabled, we call addIndex(); otherwise, we save the task.
- if ($new_server->enabled) {
- $new_server->addIndex($index);
- }
- else {
- $tasks = variable_get('search_api_tasks', array());
- // When we add or remove an index, we can ignore all other tasks.
- $tasks[$new_server->machine_name][$index->machine_name] = array('add');
- variable_set('search_api_tasks', $tasks);
- unset($new_server);
- }
- }
- // 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. Also, clear the fields cache.
- $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) {
- cache_clear_all($index->getCacheId(), 'cache', TRUE);
- if ($index->server && $index->server()->fieldsUpdated($index)) {
- _search_api_index_reindex($index);
- }
- }
- // If additional fields changed, clear the index's specific cache which
- // includes them.
- $old_additional = $index->original->options + array('additional fields' => array());
- $old_additional = $old_additional['additional fields'];
- $new_additional = $index->options + array('additional fields' => array());
- $new_additional = $new_additional['additional fields'];
- if ($old_additional != $new_additional) {
- cache_clear_all($index->getCacheId() . '-0-1', 'cache');
- }
- // 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();
- }
- }
- // If the cron batch size changed, empty the cron queue for this index.
- $old_cron = $index->original->options + array('cron_limit' => NULL);
- $old_cron = $old_cron['cron_limit'];
- $new_cron = $index->options + array('cron_limit' => NULL);
- $new_cron = $new_cron['cron_limit'];
- if ($old_cron !== $new_cron) {
- _search_api_empty_cron_queue($index, TRUE);
- }
- }
- /**
- * 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, $module_name) {
- 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') {
- 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) {
- $links[] = l($name, "admin/config/search/search_api/index/$id");
- }
- $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) {
- $links[] = l($name, "admin/config/search/search_api/server/$id");
- }
- $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().
- *
- * Marks the new item as to-index for all indexes on entities of the specified
- * type.
- *
- * @param $entity
- * The new entity.
- * @param $type
- * The entity's type.
- */
- 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));
- }
- }
- /**
- * Implements hook_entity_update().
- *
- * Marks the item as changed for all indexes on entities of the specified type.
- *
- * @param $entity
- * The updated entity.
- * @param $type
- * The entity's type.
- */
- 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) = entity_extract_ids($type, $entity);
- if (isset($id)) {
- search_api_track_item_change($type, array($id));
- }
- }
- /**
- * Implements hook_entity_delete().
- *
- * Removes the item from the tracking table and deletes it from all indexes.
- *
- * @param $entity
- * The updated entity.
- * @param $type
- * The entity's type.
- */
- 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));
- }
- }
- /**
- * 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, $has_data) {
- $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 (entity_get_property_info() as $type => $property_info) {
- if ($info = entity_get_info($type)) {
- $types[$type] = array(
- 'name' => $info['label'],
- 'datasource controller' => 'SearchApiEntityDataSourceController',
- 'entity_type' => $type,
- );
- }
- }
- return $types;
- }
- /**
- * Implements hook_modules_enabled().
- */
- function search_api_modules_enabled(array $modules) {
- // 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(array $modules) {
- // 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.'),
- 'class' => 'SearchApiAlterNodeAccess',
- );
- $callbacks['search_api_alter_node_status'] = array(
- 'name' => t('Exclude unpublished nodes'),
- 'description' => t('Exclude unpublished nodes from the index.'),
- 'class' => 'SearchApiAlterNodeStatus',
- );
- 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_highlighting'] = array(
- 'name' => t('Highlighting'),
- 'description' => t('Adds highlighting for search results.'),
- 'class' => 'SearchApiHighlight',
- 'weight' => 35,
- );
- return $processors;
- }
- /**
- * Inserts new unindexed items for all indexes on the specified type.
- *
- * @param $type
- * The item type of the new items.
- * @param array $item_id
- * 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;
- }
- search_api_get_datasource_controller($type)->trackItemInsert($item_ids, $indexes);
- 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_get_datasource_controller($type)->trackItemChange($item_ids, $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);
- }
- }
- }
- }
- /**
- * 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.
- */
- function search_api_track_item_queued(SearchApiIndex $index, array $item_ids) {
- $index->datasource()->trackItemQueued($item_ids, $index);
- }
- /**
- * 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) {
- search_api_get_datasource_controller($type)->trackItemDelete($item_ids, $indexes);
- }
- // Then, delete it from all servers. Servers of disabled indexes have to be
- // considered, too!
- unset($conditions['enabled']);
- foreach (search_api_index_load_multiple(FALSE, $conditions) as $index) {
- if ($index->server) {
- $server = $index->server();
- if ($server->enabled) {
- $server->deleteItems($item_ids, $index);
- }
- else {
- $tasks = variable_get('search_api_tasks', array());
- foreach ($item_ids as $id) {
- $tasks[$server->machine_name][$index->machine_name][] = 'delete-' . $id;
- }
- variable_set('search_api_tasks', $tasks);
- }
- }
- }
- }
- /**
- * 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 array|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 (!_search_api_settings_equals($fields, $index->options['fields'])) {
- $options = $index->options;
- $options['fields'] = $fields;
- $index->update(array('options' => $options));
- }
- }
- }
- /**
- * Test two setting arrays (or individual settings) for equality.
- *
- * While a simple == also works in some cases, this function takes into account
- * that the order of keys (usually) doesn't matter in settings arrays.
- *
- * @param mixed $setting1
- * The first setting (array).
- * @param mixed $setting2
- * The second setting (array).
- *
- * @return bool
- * TRUE if both settings are identical, FALSE otherwise.
- */
- 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 $limit
- * The number of items which should be indexed at most. -1 means no limit.
- *
- * @throws SearchApiException
- * If any error occurs during indexing.
- *
- * @return
- * Number of successfully indexed items.
- */
- function search_api_index_items(SearchApiIndex $index, $limit = -1) {
- // Don't try to index read-only indexes.
- if ($index->read_only) {
- return 0;
- }
- $queue = DrupalQueue::get('search_api_indexing_queue');
- $queue->createQueue();
- $indexed = 0;
- $unlimited = $limit < 0;
- $release_items = array();
- while (($unlimited || $indexed < $limit) && ($item = $queue->claimItem(30))) {
- if ($item->data['index'] === $index->machine_name) {
- $indexed += _search_api_indexing_queue_process($item->data);
- $queue->deleteItem($item);
- }
- else {
- $release_items[] = $item;
- }
- }
- foreach ($release_items as $item) {
- $queue->releaseItem($item);
- }
- if ($unlimited || $indexed < $limit) {
- $ids = search_api_get_items_to_index($index, $unlimited ? -1 : $limit - $indexed);
- if ($ids) {
- $indexed += count(search_api_index_specific_items($index, $ids));
- }
- }
- return $indexed;
- }
- /**
- * 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.
- *
- * @throws SearchApiException
- * If any error occurs during indexing.
- *
- * @return
- * The IDs of all successfully indexed items.
- */
- function search_api_index_specific_items(SearchApiIndex $index, array $ids) {
- $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. The following are recognized:
- * - filters: Either a SearchApiQueryFilterInterface object or an array of
- * filters used to filter the search.
- * - sort: An array of sort directives of the form $field => $order, where
- * $order is either 'ASC' or 'DESC'.
- * - offset: The position of the first returned search results relative to the
- * whole result in the index.
- * - limit: The maximum number of search results to return. -1 means no limit.
- * - 'query class': The query class to use. Must be a subtype of
- * SearchApiQueryInterface.
- * - conjunction: The type of conjunction to use for this query - either
- * 'AND' or 'OR'. 'AND' by default.
- * - 'parse mode': The mode with which to parse the $keys variable, if it
- * is set and not already an array. See SearchApiQuery::parseModes() for
- * parse modes recognized by the SearchApiQuery class.
- * Subclasses might define additional modes.
- *
- * @return SearchApiQueryInterface
- * An object for searching on the specified index.
- */
- 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);
- }
- /**
- * Static store for the searches executed on the current page. Can either be
- * 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');
- // Initialization of optional entries with default values
- foreach ($callbacks as $id => $callback) {
- $callbacks[$id] += array('enabled' => TRUE, 'weight' => 0);
- }
- }
- 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');
- // Initialization of optional entries with default values
- foreach ($processors as $id => $processor) {
- $processors[$id] += array('enabled pre' => TRUE, 'enabled post' => TRUE, 'weight' => 0);
- }
- }
- 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) {
- $index = $query->getIndex();
- // Only add node access if the necessary fields are indexed in the index, and
- // unless disabled explicitly by the query.
- $fields = $index->options['fields'];
- if (!empty($fields['search_api_access_node']) && !empty($fields['status']) && !empty($fields['author']) && !$query->getOption('search_api_bypass_access')) {
- $account = $query->getOption('search_api_access_account', $GLOBALS['user']);
- if (is_numeric($account)) {
- $account = user_load($account);
- }
- if (is_object($account)) {
- try {
- _search_api_query_add_node_access($account, $query);
- }
- catch (SearchApiException $e) {
- watchdog_exception('search_api', $e);
- }
- }
- else {
- watchdog('search_api', 'An illegal user UID was given for node access: @uid.', array('@uid' => $query->getOption('search_api_access_account', $GLOBALS['user'])), WATCHDOG_WARNING);
- }
- }
- }
- /**
- * Build a node access subquery.
- *
- * @param $account
- * The user object, who searches.
- *
- * @return SearchApiQueryFilter
- */
- function _search_api_query_add_node_access($account, SearchApiQueryInterface $query) {
- if (!user_access('access content', $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;
- }
- // Only filter for user which don't have full node access.
- if (!user_access('bypass node access', $account)) {
- // Filter by node "published" status.
- if (user_access('view own unpublished content')) {
- $filter = $query->createFilter('OR');
- $filter->condition('status', NODE_PUBLISHED);
- $filter->condition('author', $account->uid);
- $query->filter($filter);
- }
- else {
- // $query->condition('status', NODE_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);
- }
- }
- /**
- * Utility function for determining whether a field of the given type contains
- * text data.
- *
- * @param $type
- * A string containing the type to check.
- * @param array $allowed
- * Optionally, an array of allowed types.
- *
- * @return
- * TRUE if $type is either one of the specified types, or a list of such
- * values. FALSE otherwise.
- */
- 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
- * 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
- * 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
- * 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
- * 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
- * uncoditionally 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();
- }
- }
- /**
- * Utility function for extracting specific fields 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 the keys "name", "type", "boost"
- * and "indexed" (although only "type" is used by this function).
- * @param array $value_options
- * An array of options that should be passed to the
- * EntityMetadataWrapper::value() method (see there).
- *
- * @return
- * The $fields array with additional "value" and "original_type" keys set.
- */
- function search_api_extract_fields(EntityMetadataWrapper $wrapper, array $fields, array $value_options = array()) {
- // 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 $field => &$info) {
- $info['value'] = array();
- $info['original_type'] = $info['type'];
- }
- unset($info);
- try {
- foreach ($wrapper as $i => $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 {
- $info['value'] = $wrapper->$field->value($value_options);
- // For fulltext fields with options, also include the option labels.
- if (search_api_is_text_type($info['type']) && $wrapper->$field->optionsList('view')) {
- _search_api_add_option_values($info['value'], $wrapper->$field->optionsList('view'));
- }
- $property_info = $wrapper->$field->info();
- $info['original_type'] = $property_info['type'];
- // For entities, we extract the entity ID instead of the whole object.
- // @todo Use 'identifier' => TRUE instead of always loading the object.
- $t = search_api_extract_inner_type($property_info['type']);
- if (isset($entity_infos[$t])) {
- // If no object is set, set this field to NULL.
- $info['value'] = $info['value'] ? _search_api_extract_entity_value($wrapper->$field, search_api_is_text_type($info['type'])) : NULL;
- }
- }
- 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 $field => &$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 array
- * 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 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');
- }
- /**
- * 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
- * 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);
- }
- /**
- * 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 $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
- * 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 $id
- * The ID or machine name of the server to enable.
- *
- * @return
- * 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, along with all associated indexes.
- *
- * @param $id
- * The ID or machine name of the server to disable.
- *
- * @return
- * 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;
- }
- /**
- * Deletes a search server and disables all associated indexes.
- *
- * @param $id
- * The ID or machine name of the server to delete.
- *
- * @return
- * 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
- * A completely loaded index object, or NULL if no such index exists.
- */
- function search_api_index_load($id, $reset = FALSE) {
- $ret = search_api_index_load_multiple(array($id), array(), $reset);
- return $ret ? reset($ret) : FALSE;
- }
- /**
- * 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 array
- * 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(),
- );
- }
- /**
- * Property callback.
- *
- * @return SearchApiServer
- * The server this index currently resides on, or NULL if the index
- * is currently unassigned.
- */
- function search_api_index_get_server(SearchApiIndex $index) {
- return $index->server();
- }
- /**
- * 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 $id
- * The edited index' id.
- * @param array $fields
- * The new field values to set.
- *
- * @return
- * 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 $id
- * The ID or machine name of the index whose fields should be changed.
- * @param array $fields
- * The new indexed field settings.
- *
- * @return
- * 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 $id
- * The ID or machine name of the index to enable.
- *
- * @throws SearchApiException
- * If the index' server isn't enabled.
- *
- * @return
- * 1 on success, 0 or FALSE on failure.
- */
- 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 $id
- * The ID or machine name of the index to disable.
- *
- * @return
- * 1 on success, 0 or FALSE on failure.
- */
- 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
- * 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);
- _search_api_empty_cron_queue($index);
- }
- /**
- * Helper method for removing all of an index's jobs from the cron queue.
- *
- * @param SearchApiIndex $index
- * The index whose jobs should be removed.
- * @param $mark_changed
- * If TRUE, mark all items in the queue as "changed" again. Defaults to FALSE.
- */
- function _search_api_empty_cron_queue(SearchApiIndex $index, $mark_changed = FALSE) {
- $index_id = $index->machine_name;
- $queue = DrupalQueue::get('search_api_indexing_queue');
- $queue->createQueue();
- $ids = array();
- $release_items = array();
- while ($item = $queue->claimItem()) {
- if ($item->data['index'] === $index_id) {
- $queue->deleteItem($item);
- if ($mark_changed) {
- $ids = array_merge($ids, $item->data['items']);
- }
- }
- else {
- $release_items[] = $item;
- }
- }
- foreach ($release_items as $item) {
- $queue->releaseItem($item);
- }
- if ($ids) {
- $index->datasource()->trackItemChange($ids, 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
- * 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
- * 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;
- }
- /**
- * 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;
- }
- /**
- * Cron queue worker callback for indexing some items.
- *
- * @param array $task
- * An associative array containing:
- * - index: The ID of the index on which items should be indexed.
- * - items: The items that should be indexed.
- *
- * @return
- * The number of successfully indexed items.
- */
- function _search_api_indexing_queue_process(array $task) {
- $index = search_api_index_load($task['index']);
- try {
- if ($index && $index->enabled && !$index->read_only && $task['items']) {
- $indexed = search_api_index_specific_items($index, $task['items']);
- $num = count($indexed);
- // If some items couldn't be indexed, mark them as dirty again.
- if ($num < count($task['items'])) {
- // Believe it or not but this is actually quite faster than the equivalent
- // $diff = array_diff($task['items'], $indexed);
- $diff = array_keys(array_diff_key(array_flip($task['items']), array_flip($indexed)));
- // Mark the items as dirty again.
- $index->datasource()->trackItemChange($diff, array($index), TRUE);
- }
- if ($num) {
- watchdog('search_api', t('Indexed @num items for index @name', array('@num' => $num, '@name' => $index->name)), NULL, WATCHDOG_INFO);
- }
- return $num;
- }
- }
- catch (SearchApiException $e) {
- watchdog_exception('search_api', $e);
- }
- }
- /**
- * 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;
- }
- /**
- * Create and set a batch for indexing items.
- *
- * @param SearchApiIndex $index
- * The index for which items should be indexed.
- * @param $batch_size
- * Number of items to index per batch.
- * @param $limit
- * Maximum number of items to index.
- * @param $remaining
- * Remaining items to index.
- * @param $drush
- * Boolean specifying whether this was called from drush or not.
- */
- 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 array $context
- * The batch context.
- */
- function _search_api_batch_indexing_callback(SearchApiIndex $index, $batch_size, $limit, $drush = FALSE, array &$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.
- $indexed = search_api_index_items($index, $to_index);
- $context['results']['indexed'] += $indexed;
- // 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
- * Result of the batch operation.
- * @param array $results
- * Results.
- * @param array $operations
- * Remaining batch operation to process.
- */
- function _search_api_batch_indexing_finished($success, $results, $operations) {
- // 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');
- }
- }
|