File modules/cms/macros/macro/SearchIndex.class.php

Last commit: Sun Dec 5 20:33:24 2021 +0100	dankert	Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'.
1 <?php 2 namespace cms\macros\macro; 3 4 use cms\generator\PageContext; 5 use cms\generator\ValueContext; 6 use cms\generator\ValueGenerator; 7 use cms\model\Element; 8 use cms\model\Folder; 9 use cms\model\Page; 10 use cms\model\Project; 11 use util\json\JSON; 12 use util\Macro; 13 14 15 /** 16 * Creates a search index for all pages in this project. 17 * @author Jan Dankert 18 */ 19 class SearchIndex extends Macro 20 { 21 /** 22 * Beschreibung dieser Klasse 23 * 24 * @type String 25 */ 26 var $description = ''; 27 28 public $maxLength = 300; 29 30 31 /** 32 * Creates a search index for alle pages in the current project. 33 */ 34 function execute() 35 { 36 $searchIndex = array(); 37 38 $project = new Project( $this->getPage()->projectid ); 39 40 $f = new Folder( $project->getRootObjectId() ); 41 $f->load(); 42 43 foreach ( array_merge( [$f],$f->getAllSubFolderIds() ) as $fid) { 44 45 $tf = new Folder($fid); 46 $tf->load(); 47 foreach( $tf->getPages() as $pageObjectId ) 48 { 49 $page = new Page( $pageObjectId ); 50 $page->load(); 51 52 // Generating all values 53 $values = []; 54 /** @var Element $element */ 55 foreach($page->getTemplate()->getWritableElements() as $element ) { 56 $pageContext = clone $this->pageContext; 57 $pageContext->objectId = $pageObjectId; 58 $valueContext = new ValueContext($pageContext); 59 $valueContext->elementid = $element->elementid; 60 $generator = new ValueGenerator( $valueContext ); 61 $values[] = $generator->getCache()->get(); 62 //$values[] = print_r($valueContext,true); 63 } 64 65 $name = $page->getNameForLanguage( $this->pageContext->languageId ); 66 67 $searchIndex[] = array( 68 'id' => $pageObjectId, 69 'title' => $name->name, 70 'filename'=> $page->filename, 71 'url' => $this->pathToObject( $pageObjectId ), 72 'content' => $this->truncate(array_reduce( 73 $values, 74 function($act, $value) 75 { 76 return $act.' '.$value; 77 }, 78 '' 79 )) 80 ); 81 } 82 } 83 84 // Output search index as JSON 85 echo JSON::encode( $searchIndex ); 86 } 87 88 89 private function truncate( $text) { 90 $text = str_replace('&quot;','',$text); 91 $text = str_replace('&lt;' ,'',$text); 92 $text = str_replace('&rt;' ,'',$text); 93 $text = strtr($text,';,:.\'"',' '); 94 $text = strtr($text,' ',' '); 95 if ( strlen($text) > $this->maxLength ) 96 $text = mb_substr($text,0,$this->maxLength,'UTF-8'); 97 98 return $text; 99 } 100 101 }
Download modules/cms/macros/macro/SearchIndex.class.php
History Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Sun, 14 Mar 2021 02:14:31 +0100 Jan Dankert Fix: The public filename of files must contain their path... Sun, 14 Mar 2021 01:33:16 +0100 Jan Dankert Fix: Transformer should not throw an exception if any linktarget could not be found. Fix: SearchIndex must use the correct pageContext. Sun, 14 Mar 2021 00:50:10 +0100 Jan Dankert Fix: SearchIndex created empty content. Sun, 14 Mar 2021 00:18:57 +0100 Jan Dankert Fix: Use getPage() in all Macros. Fri, 26 Feb 2021 00:04:49 +0100 Jan Dankert New: Request may contain JSON,XML in POST data. This is good for API clients. Sat, 26 Sep 2020 01:41:20 +0200 Jan Dankert Refactoring: Removing old require.php files. With class autoloading, they are not necessary any more. Mon, 21 Sep 2020 23:44:23 +0200 Jan Dankert Fixing editing of values. Mon, 21 Sep 2020 22:48:59 +0200 Jan Dankert Complexe refactoring: Moving all generation logic from the model (Value,Page,File) to generators classes. Fri, 18 Sep 2020 23:04:13 +0200 Jan Dankert Refactoring: Renaming module "cms/publish" to "cms/generator" Sun, 23 Feb 2020 04:49:34 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 2. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.