SearchIndex.class.php (2348B)
1 <?php 2 3 use cms\model\Folder; 4 use cms\model\Name; 5 use cms\model\Page; 6 use cms\model\Project; 7 use cms\publish\PublishEdit; 8 9 10 /** 11 * Creates a search index for all pages in this project. 12 * @author Jan Dankert 13 */ 14 class SearchIndex extends Macro 15 { 16 /** 17 * Beschreibung dieser Klasse 18 * 19 * @type String 20 */ 21 var $description = ''; 22 23 public $maxLength = 300; 24 25 26 /** 27 * Creates a search index for alle pages in the current project. 28 */ 29 function execute() 30 { 31 $searchIndex = array(); 32 33 $project = new Project( $this->page->projectid ); 34 35 $f = new Folder( $project->getRootObjectId() ); 36 $f->load(); 37 38 foreach ($f->getAllSubFolderIds() as $fid) { 39 40 $tf = new Folder($fid); 41 $tf->load(); 42 foreach( $tf->getPages() as $pageid ) 43 { 44 $page = new Page( $pageid ); 45 46 // Den einfachen Publisher benutzen, damit nur beschreibbare Inhalte auch in den Index wandern. 47 $page->publisher = new PublishEdit(); 48 $page->load(); 49 $page->generate(); 50 51 $name = $page->getNameForLanguage( $this->page->languageid ); 52 53 $searchIndex[] = array( 54 'id' => $pageid, 55 'title' => $name->name, 56 'filename'=> $page->filename, 57 'url' => $this->pathToObject( $pageid ), 58 'content' => $this->truncate(array_reduce( 59 $page->values, 60 function($act, $value) 61 { 62 return $act.' '.$value->value; 63 }, 64 '' 65 )) 66 ); 67 } 68 } 69 70 // Output search index as JSON 71 $json = new JSON(); 72 $this->output( $json->encode( $searchIndex ) ); 73 } 74 75 76 private function truncate( $text) { 77 $text = str_replace('"','',$text); 78 $text = str_replace('<' ,'',$text); 79 $text = str_replace('&rt;' ,'',$text); 80 $text = strtr($text,';,:.\'"',' '); 81 $text = strtr($text,' ',' '); 82 if ( strlen($text) > $this->maxLength ) 83 $text = mb_substr($text,0,$this->maxLength,'UTF-8'); 84 85 return $text; 86 } 87 88 }