/** * Instantiate a search page, should one not exist. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); $mode = Versioned::get_reading_mode(); Versioned::reading_stage('Stage'); // Determine whether pages should be created. if (self::config()->create_default_pages) { // Determine whether an extensible search page already exists. if (!ExtensibleSearchPage::get()->first()) { // Instantiate an extensible search page. $page = ExtensibleSearchPage::create(); $page->Title = 'Search Page'; $page->write(); DB::alteration_message('"Default" Extensible Search Page', 'created'); } } else { if (ClassInfo::exists('Multisites')) { foreach (Site::get() as $site) { // Determine whether an extensible search page already exists. if (!ExtensibleSearchPage::get()->filter('SiteID', $site->ID)->first()) { // Instantiate an extensible search page. $page = ExtensibleSearchPage::create(); $page->ParentID = $site->ID; $page->Title = 'Search Page'; $page->write(); DB::alteration_message("\"{$site->Title}\" Extensible Search Page", 'created'); } } } } Versioned::set_reading_mode($mode); }
/** * Retrieve the search page. * * @return extensible search page */ public function getSearchPage() { $pages = ExtensibleSearchPage::get(); // This is required to support multiple sites. if (ClassInfo::exists('Multisites')) { $pages = $pages->filter('SiteID', $this->owner->SiteID); } return $pages->first(); }
/** * Returns the default search page for this site * * @return ExtensibleSearchPage */ public function getSearchPage() { $page = ExtensibleSearchPage::get(); if (class_exists('Multisites')) { $siteID = Multisites::inst()->getCurrentSiteId(); $page = $page->filter('SiteID', $siteID); } return $page->first(); }
public function updateExtensibleSearchPageCMSFields(FieldList $fields) { if ($this->owner->SearchEngine === get_class($this)) { $types = SiteTree::page_type_classes(); $source = array_combine($types, $types); // add in any explicitly configured asort($source); $source = $this->owner->updateSource($source); $parsers = $this->owner->getQueryBuilders(); $options = array(); foreach ($parsers as $key => $objCls) { $obj = new $objCls(); $options[$key] = $obj->title; } $fields->addFieldToTab('Root.Main', new DropdownField('QueryType', _t('ExtensibleSearchPage.QUERY_TYPE', 'Query Type'), $options), 'Content'); ksort($source); $source = array_merge($source, ExtensibleSearchPage::config()->additional_search_types); $types = MultiValueDropdownField::create('SearchType', _t('ExtensibleSearchPage.SEARCH_ITEM_TYPE', 'Search items of type'), $source); $fields->addFieldToTab('Root.Main', $types, 'Content'); $objFields = $this->owner->getSelectableFields(); $sortFields = $objFields; // Remove content and groups from being sortable (as they are not relevant). unset($sortFields['Content']); unset($sortFields['Groups']); $fields->replaceField('SortBy', new DropdownField('SortBy', _t('ExtensibleSearchPage.SORT_BY', 'Sort By'), $sortFields)); $fields->addFieldToTab('Root.Main', MultiValueDropdownField::create('SearchOnFields', _t('ExtensibleSearchPage.INCLUDE_FIELDS', 'Search On Fields'), $objFields), 'Content'); $fields->addFieldToTab('Root.Main', MultiValueTextField::create('ExtraSearchFields', _t('SolrSearch.EXTRA_FIELDS', 'Custom solr fields to search')), 'Content'); $boostVals = array(); for ($i = 1; $i <= static::BOOST_MAX; $i++) { $boostVals[$i] = $i; } $fields->addFieldToTab('Root.Main', new KeyValueField('BoostFields', _t('ExtensibleSearchPage.BOOST_FIELDS', 'Boost values'), $objFields, $boostVals), 'Content'); $fields->addFieldToTab('Root.Main', $f = new KeyValueField('BoostMatchFields', _t('ExtensibleSearchPage.BOOST_MATCH_FIELDS', 'Boost fields with field/value matches'), array(), $boostVals), 'Content'); $f->setRightTitle('Enter a field name, followed by the value to boost if found in the result set, eg "title:Home" '); $fields->addFieldToTab('Root.Main', $kv = new KeyValueField('FilterFields', _t('ExtensibleSearchPage.FILTER_FIELDS', 'Fields to filter by')), 'Content'); $fields->addFieldToTab('Root.Main', new HeaderField('FacetHeader', _t('ExtensibleSearchPage.FACET_HEADER', 'Facet Settings')), 'Content'); $fields->addFieldToTab('Root.Main', new MultiValueDropdownField('FacetFields', _t('ExtensibleSearchPage.FACET_FIELDS', 'Fields to create facets for'), $objFields), 'Content'); $fields->addFieldToTab('Root.Main', new MultiValueTextField('CustomFacetFields', _t('ExtensibleSearchPage.CUSTOM_FACET_FIELDS', 'Additional fields to create facets for')), 'Content'); $facetMappingFields = $objFields; if ($this->owner->CustomFacetFields && ($cff = $this->owner->CustomFacetFields->getValues())) { foreach ($cff as $facetField) { $facetMappingFields[$facetField] = $facetField; } } $fields->addFieldToTab('Root.Main', new KeyValueField('FacetMapping', _t('ExtensibleSearchPage.FACET_MAPPING', 'Mapping of facet title to nice title'), $facetMappingFields), 'Content'); $fields->addFieldToTab('Root.Main', new KeyValueField('FacetQueries', _t('ExtensibleSearchPage.FACET_QUERIES', 'Fields to create query facets for')), 'Content'); $fields->addFieldToTab('Root.Main', new NumericField('MinFacetCount', _t('ExtensibleSearchPage.MIN_FACET_COUNT', 'Minimum facet count for inclusion in facet results'), 2), 'Content'); } // Make sure previously existing hooks are carried across. $this->owner->extend('updateSolrCMSFields', $fields); }
public function getCMSFields() { $fields = parent::getCMSFields(); $types = ClassInfo::dataClassesFor('DataObject'); array_shift($types); asort($types); $source = array_combine($types, $types); $source = array_merge($source, ExtensibleSearchPage::config()->additional_search_types); $fields->replaceField('Title', new DropdownField('Title', _t('Solr.TYPE_CONFIG_TITLE', 'Data type'), $source)); if ($this->Title) { $keys = $this->getFieldsFor($this->Title); $vals = array('default' => _t('Solr.DEFAULT_MAPPING', 'Default type'), ':field_as' => _t('Solr.CASE_INSENSITIVE', 'Case Insensitive text'), ':field_ms' => _t('Solr.CASE_SENSITIVE', 'Case Sensitive, untokenised text')); $fields->replaceField('FieldMappings', new KeyValueField('FieldMappings', _t('Solr.FIELD_MAPPINGS', 'Indexed fields'), $keys, $vals, $this->FieldMappings)); } else { $fields->removeByName('FieldMappings'); } return $fields; }
/** * Retrieve the most relevant search suggestions. * * @parameter <{SEARCH_TERM}> string * @parameter <{EXTENSIBLE_SEARCH_PAGE_ID}> integer * @parameter <{LIMIT}> integer * @parameter <{APPROVED_ONLY}> boolean * @return array */ public function getSuggestions($term, $pageID, $limit = 5, $approved = true) { // Make sure the search matches the minimum autocomplete length. if ($term && strlen($term) > 2) { // Make sure the current user has appropriate permission. $pageID = (int) $pageID; if (($page = ExtensibleSearchPage::get_by_id('ExtensibleSearchPage', $pageID)) && $page->canView()) { // Retrieve the search suggestions. $suggestions = ExtensibleSearchSuggestion::get()->filter(array('Term:StartsWith' => $term, 'Approved' => (int) $approved, 'ExtensibleSearchPageID' => $pageID))->sort('Frequency', 'DESC')->limit($limit); // Make sure the search suggestions are unique. return array_unique($suggestions->column('Term')); } } return null; }
public function run($request) { increase_time_limit_to(); $readingMode = Versioned::get_reading_mode(); Versioned::reading_stage('Stage'); // Make sure that we have something to migrate. if (!ClassInfo::hasTable('SolrSearchPage')) { echo "Nothing to Migrate!"; die; } // Retrieve the search tree relationships to migrate. $relationships = array(); if (DB::getConn()->hasTable('SolrSearchPage_SearchTrees')) { foreach (DB::query('SELECT * FROM SolrSearchPage_SearchTrees') as $relationship) { $relationships[$relationship['SolrSearchPageID']] = $relationship['PageID']; } } // Store the current live page migration state to avoid duplicates. $created = array(); // Migrate any live pages to begin with. $query = DB::query('SELECT * FROM SiteTree_Live st, SolrSearchPage_Live ssp WHERE st.ID = ssp.ID'); $queryCount = $query->numRecords(); $writeCount = 0; foreach ($query as $results) { $searchPage = ExtensibleSearchPage::create(); $searchPage->SearchEngine = 'SolrSearch'; // Migrate the key site tree and solr search fields across. $fields = array('ParentID', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'ShowInMenus', 'ShowInSearch', 'Sort', 'ResultsPerPage', 'SortBy', 'BoostFieldsValue', 'SearchOnFieldsValue', 'SearchTypeValue', 'StartWithListing', 'QueryType', 'ListingTemplateID', 'FilterFieldsValue', 'MinFacetCount', 'FacetQueriesValue', 'FacetMappingValue', 'CustomFacetFieldsValue', 'FacetFieldsValue', 'BoostMatchFieldsValue'); foreach ($fields as $fname) { if (isset($results[$fname])) { $searchPage->{$fname} = $results[$fname]; } } // This field name no longer matches the original. if ($results['SortDir']) { $searchPage->SortDirection = $results['SortDir']; } if (isset($relationships[$results['ID']])) { $searchPage->SearchTrees()->add($relationships[$results['ID']]); } // Attempt to publish these new pages. $searchPage->doPublish(); if ($searchPage->ID) { echo "<strong>{$results['ID']}</strong> Published<br>"; $writeCount++; } $created[] = $results['ID']; } // Confirm that the current user had permission to publish these new pages. $this->checkPermissions($queryCount, $writeCount); // Migrate any remaining draft pages. $query = DB::query('SELECT * FROM SiteTree st, SolrSearchPage ssp WHERE st.ID = ssp.ID'); $queryCount = $query->numRecords(); $writeCount = 0; foreach ($query as $results) { // Make sure this search page doesn't already exist. if (!in_array($results['ID'], $created)) { $searchPage = ExtensibleSearchPage::create(); $searchPage->SearchEngine = 'SolrSearch'; // Migrate the key site tree and solr search fields across. $searchPage->ParentID = $results['ParentID']; $searchPage->URLSegment = $results['URLSegment']; $searchPage->Title = $results['Title']; $searchPage->MenuTitle = $results['MenuTitle']; $searchPage->Content = $results['Content']; $searchPage->ShowInMenus = $results['ShowInMenus']; $searchPage->ShowInSearch = $results['ShowInSearch']; $searchPage->Sort = $results['Sort']; $searchPage->ResultsPerPage = $results['ResultsPerPage']; $searchPage->SortBy = $results['SortBy']; $searchPage->SortDirection = $results['SortDir']; $searchPage->QueryType = $results['QueryType']; $searchPage->StartWithListing = $results['StartWithListing']; $searchPage->SearchTypeValue = $results['SearchTypeValue']; $searchPage->SearchOnFieldsValue = $results['SearchOnFieldsValue']; $searchPage->BoostFieldsValue = $results['BoostFieldsValue']; $searchPage->BoostMatchFieldsValue = $results['BoostMatchFieldsValue']; $searchPage->FacetFieldsValue = $results['FacetFieldsValue']; $searchPage->CustomFacetFieldsValue = $results['CustomFacetFieldsValue']; $searchPage->FacetMappingValue = $results['FacetMappingValue']; $searchPage->FacetQueriesValue = $results['FacetQueriesValue']; $searchPage->MinFacetCount = $results['MinFacetCount']; $searchPage->FilterFieldsValue = $results['FilterFieldsValue']; $searchPage->ListingTemplateID = $results['ListingTemplateID']; if (isset($relationships[$results['ID']])) { $searchPage->SearchTrees()->add($relationships[$results['ID']]); } $searchPage->write(); if ($searchPage->ID) { echo "<strong>{$results['ID']}</strong> Saved<br>"; $writeCount++; } } else { $writeCount++; } } // Confirm that the current user had permission to write these new pages. $this->checkPermissions($queryCount, $writeCount); // Remove the previous search page tables, as they are now obsolete (and may not be marked as such). $remove = array('SiteTree', 'SiteTree_Live', 'Page', 'Page_Live'); foreach ($remove as $table) { foreach ($created as $ID) { DB::query("DELETE FROM {$table} WHERE ID = {$ID}"); } } $remove = array('SolrSearchPage', 'SolrSearchPage_Live', 'SolrSearchPage_SearchTrees', 'SolrSearchPage_versions'); foreach ($remove as $table) { DB::query("DROP TABLE {$table}"); } Versioned::set_reading_mode($readingMode); echo 'Migration Complete!'; }
/** * Ensures that there is always a search page * by checking if there's an instance of * a base ExtensibleSearchPage. If there * is not, one is created when the DB is built. */ function requireDefaultRecords() { parent::requireDefaultRecords(); if (SiteTree::get_create_default_pages()) { $page = DataObject::get_one('ExtensibleSearchPage'); if (!($page && $page->exists())) { $page = ExtensibleSearchPage::create(); $page->Title = _t('ExtensibleSearchPage.DEFAULT_PAGE_TITLE', 'Search Page'); $page->Content = ''; $page->ResultsPerPage = 10; $page->Status = 'New page'; $page->write(); DB::alteration_message('Search page created', 'created'); } } }