Created
April 13, 2019 07:20
-
-
Save teppokoivula/83036c6e73d7460be7706def620d80d4 to your computer and use it in GitHub Desktop.
Generate search index for saved pages in ProcessWire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate search index for saved pages | |
* | |
*/ | |
$this->addHook('Pages::saveReady', function(HookEvent $event) { | |
$page = $event->arguments[0]; | |
if ($page->id && $page->hasField('search_index')) { | |
$page->search_index = indexPage($page, array( | |
'title', | |
'headline', | |
'summary', | |
'body', | |
'footer', | |
'publish_from', | |
'meta_keywords', | |
'meta_description', | |
)); | |
} | |
}); | |
/** | |
* Store page data in search index and return generated index as a string | |
* | |
* @param Page $page | |
* @param array $index_fields | |
* @return string | |
*/ | |
function indexPage(Page $page, $index_fields = array()) { | |
$search_index = ""; | |
if ($page->id && !empty($index_fields)) { | |
foreach ($page->fields as $field) { | |
if (in_array($field->name, $index_fields)) { | |
$formatted_data = $page->getFormatted($field->name); | |
$search_index .= strip_tags($formatted_data); | |
$search_index .= getIndexUrls($formatted_data); | |
} else if ($field->type == "FieldtypePageTable") { | |
foreach ($page->get($field->name) as $child) { | |
$search_index .= indexPage($child, $index_fields); | |
} | |
} | |
} | |
} | |
return $search_index; | |
} | |
/** | |
* Grab URLs from field data, mash them together into a space separated string, | |
* and return the resulting string | |
* | |
* @param string $data | |
* @return string | |
*/ | |
function getIndexUrls($data) { | |
$out = ""; | |
if ($data && preg_match_all('/href=([\"\'])(.*?)\1/i', $data, $matches)) { | |
$out = " link:" . implode(" link:", $matches[2]) . " "; | |
} | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment