| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * User: jensk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Date: 21-2-2017 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Time: 10:22 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace library\components\cms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use library\components\CmsComponent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use library\search\Indexer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use library\search\Search; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class SearchRouting implements CmsRouting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * SearchRouting constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @param \library\cc\Request              $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @param                                  $relativeCmsUri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * @param \library\components\CmsComponent $cmsComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	public function __construct($request, $relativeCmsUri, $cmsComponent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		if ($relativeCmsUri === '/search') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$this->overviewRoute($cmsComponent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		} elseif ($relativeCmsUri === '/search/update-index') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			$this->updateIndexRoute($cmsComponent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} elseif ($relativeCmsUri === '/search/ajax-update-index') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			$this->ajaxUpdateIndexRoute($request, $cmsComponent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		} elseif ($relativeCmsUri === '/search/manual-update-index') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			$indexer = new Indexer($cmsComponent->storage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$indexer->updateIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @param \library\components\CmsComponent $cmsComponent | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	private function overviewRoute($cmsComponent) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		$cmsComponent->subTemplate = 'cms/search'; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SEARCH); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 		$documentCount = $cmsComponent->storage->getTotalDocumentCount(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		$indexer = new Search($cmsComponent->storage); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		$indexedDocuments = $indexer->getIndexedDocuments(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 		$cmsComponent->setParameter(CmsComponent::PARAMETER_SEARCH_NEEDS_UPDATE, $documentCount !== $indexedDocuments); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @param \library\components\CmsComponent $cmsComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	private function updateIndexRoute($cmsComponent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$cmsComponent->subTemplate = 'cms/search/update-index'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_SEARCH); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	private function ajaxUpdateIndexRoute($request, $cmsComponent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$cmsComponent->subTemplate = 'cms/search/update-index'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		if (isset($request::$get['step'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			\set_time_limit(0); // Set max excecution time infinite | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			\session_write_close(); // Close the session, so it doesnt create a lock on the sessionstorage, block other requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			$indexer = new Indexer($cmsComponent->storage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			$step = $request::$get['step']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$this->stepRouting($step, $cmsComponent, $indexer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			$this->showJson('No step defined.', 'HTTP/1.0 500 Internal Server Error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	private function showJson($obj, $httpHeader = 'HTTP/1.0 200 OK') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		header($_SERVER['SERVER_PROTOCOL'] . $httpHeader, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		header('Content-type: application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		die(json_encode($obj)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * @param CmsComponent $cmsComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * @param string $step | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * @param Indexer $indexer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	private function stepRouting($step, $cmsComponent, $indexer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		if ($step == 'resetIndex') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$indexer->resetIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$this->showJson('done'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		} elseif ($step == 'createDocumentTermCount') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$documents = $cmsComponent->storage->getDocuments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$indexer->createDocumentTermCount($documents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$this->showJson('done'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} else if ($step == 'createDocumentTermFrequency') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			$indexer->createDocumentTermFrequency(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			$this->showJson('done'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} else if ($step == 'createTermFieldLengthNorm') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$indexer->createTermFieldLengthNorm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			$this->showJson('done'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		} else if ($step == 'createInverseDocumentFrequency') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			$indexer->createInverseDocumentFrequency(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$this->showJson('done'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} else if ($step == 'replaceOldIndex') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$indexer->replaceOldIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			$this->showJson('done'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$this->showJson('Invalid step: ' . $step . '.', 'HTTP/1.0 500 Internal Server Error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 111 |  |  | } |