@@ -4,128 +4,128 @@ |
||
| 4 | 4 | use storage\Storage; |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * Class DocumentComponent |
|
| 8 | - * |
|
| 9 | - * Has optional parameter `folder` to prefix the relative url with a folder |
|
| 10 | - * Has optional parameter `document` to select a given document |
|
| 11 | - * Has optional parameter `documentParameterName` to select the parametername to be used |
|
| 12 | - * to set the found document to. |
|
| 13 | - * |
|
| 14 | - * @package CloudControl\Cms\components |
|
| 15 | - */ |
|
| 16 | - class DocumentComponent extends BaseComponent |
|
| 17 | - { |
|
| 18 | - protected $documentParameterName = 'document'; |
|
| 7 | + * Class DocumentComponent |
|
| 8 | + * |
|
| 9 | + * Has optional parameter `folder` to prefix the relative url with a folder |
|
| 10 | + * Has optional parameter `document` to select a given document |
|
| 11 | + * Has optional parameter `documentParameterName` to select the parametername to be used |
|
| 12 | + * to set the found document to. |
|
| 13 | + * |
|
| 14 | + * @package CloudControl\Cms\components |
|
| 15 | + */ |
|
| 16 | + class DocumentComponent extends BaseComponent |
|
| 17 | + { |
|
| 18 | + protected $documentParameterName = 'document'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param Storage $storage |
|
| 22 | - * |
|
| 23 | - * @return mixed|void |
|
| 24 | - * @throws \Exception |
|
| 25 | - */ |
|
| 26 | - public function run(Storage $storage) |
|
| 27 | - { |
|
| 28 | - parent::run($storage); |
|
| 20 | + /** |
|
| 21 | + * @param Storage $storage |
|
| 22 | + * |
|
| 23 | + * @return mixed|void |
|
| 24 | + * @throws \Exception |
|
| 25 | + */ |
|
| 26 | + public function run(Storage $storage) |
|
| 27 | + { |
|
| 28 | + parent::run($storage); |
|
| 29 | 29 | |
| 30 | - $this->checkParameters(); |
|
| 30 | + $this->checkParameters(); |
|
| 31 | 31 | |
| 32 | - if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component |
|
| 33 | - $this->runLikeApplicationComponent(); |
|
| 34 | - } else { |
|
| 35 | - $this->runLikeRegularComponent(); |
|
| 36 | - } |
|
| 37 | - } |
|
| 32 | + if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component |
|
| 33 | + $this->runLikeApplicationComponent(); |
|
| 34 | + } else { |
|
| 35 | + $this->runLikeRegularComponent(); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Checks to see if any parameters were defined in the cms and acts according |
|
| 41 | - */ |
|
| 42 | - private function checkParameters() |
|
| 43 | - { |
|
| 44 | - if (isset($this->parameters['documentParameterName'])) { |
|
| 45 | - $this->documentParameterName = $this->parameters['documentParameterName']; |
|
| 46 | - } |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * Checks to see if any parameters were defined in the cms and acts according |
|
| 41 | + */ |
|
| 42 | + private function checkParameters() |
|
| 43 | + { |
|
| 44 | + if (isset($this->parameters['documentParameterName'])) { |
|
| 45 | + $this->documentParameterName = $this->parameters['documentParameterName']; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Run as application component |
|
| 51 | - * |
|
| 52 | - * @throws \Exception |
|
| 53 | - */ |
|
| 54 | - private function runLikeApplicationComponent() |
|
| 55 | - { |
|
| 56 | - if (isset($this->parameters['document'])) { |
|
| 57 | - $this->parameters[$this->documentParameterName] = $this->storage->getDocuments()->getDocumentBySlug($this->parameters['document']); |
|
| 58 | - unset($this->parameters['document']); |
|
| 59 | - } else { |
|
| 60 | - throw new \Exception('When used as application component, you need to specify a document.'); |
|
| 61 | - } |
|
| 62 | - } |
|
| 49 | + /** |
|
| 50 | + * Run as application component |
|
| 51 | + * |
|
| 52 | + * @throws \Exception |
|
| 53 | + */ |
|
| 54 | + private function runLikeApplicationComponent() |
|
| 55 | + { |
|
| 56 | + if (isset($this->parameters['document'])) { |
|
| 57 | + $this->parameters[$this->documentParameterName] = $this->storage->getDocuments()->getDocumentBySlug($this->parameters['document']); |
|
| 58 | + unset($this->parameters['document']); |
|
| 59 | + } else { |
|
| 60 | + throw new \Exception('When used as application component, you need to specify a document.'); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Run as regular component |
|
| 66 | - * |
|
| 67 | - * @throws \Exception |
|
| 68 | - */ |
|
| 69 | - private function runLikeRegularComponent() |
|
| 70 | - { |
|
| 71 | - if ($this->matchedSitemapItem->regex == false) { |
|
| 72 | - $this->runWithoutRegex(); |
|
| 73 | - } else { |
|
| 74 | - $this->runWithRegex(); |
|
| 75 | - } |
|
| 76 | - } |
|
| 64 | + /** |
|
| 65 | + * Run as regular component |
|
| 66 | + * |
|
| 67 | + * @throws \Exception |
|
| 68 | + */ |
|
| 69 | + private function runLikeRegularComponent() |
|
| 70 | + { |
|
| 71 | + if ($this->matchedSitemapItem->regex == false) { |
|
| 72 | + $this->runWithoutRegex(); |
|
| 73 | + } else { |
|
| 74 | + $this->runWithRegex(); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Run without regex |
|
| 80 | - * |
|
| 81 | - * @throws \Exception |
|
| 82 | - */ |
|
| 83 | - private function runWithoutRegex() |
|
| 84 | - { |
|
| 85 | - if (isset($this->parameters['document'])) { |
|
| 86 | - $this->runByDocumentParameter(); |
|
| 87 | - } else { |
|
| 88 | - throw new \Exception('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title); |
|
| 89 | - } |
|
| 90 | - } |
|
| 78 | + /** |
|
| 79 | + * Run without regex |
|
| 80 | + * |
|
| 81 | + * @throws \Exception |
|
| 82 | + */ |
|
| 83 | + private function runWithoutRegex() |
|
| 84 | + { |
|
| 85 | + if (isset($this->parameters['document'])) { |
|
| 86 | + $this->runByDocumentParameter(); |
|
| 87 | + } else { |
|
| 88 | + throw new \Exception('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Run with regex |
|
| 94 | - * |
|
| 95 | - * @throws \Exception |
|
| 96 | - */ |
|
| 97 | - private function runWithRegex() |
|
| 98 | - { |
|
| 99 | - if (isset($this->parameters['document'])) { |
|
| 100 | - $this->runByDocumentParameter(); |
|
| 101 | - } else { |
|
| 102 | - $relativeDocumentUri = current($this->matchedSitemapItem->matches[1]); |
|
| 103 | - if (isset($this->parameters['folder'])) { |
|
| 104 | - if (substr($this->parameters['folder'], -1) !== '/') { |
|
| 105 | - $this->parameters['folder'] = $this->parameters['folder'] . '/'; |
|
| 106 | - } |
|
| 107 | - $relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri; |
|
| 108 | - } |
|
| 92 | + /** |
|
| 93 | + * Run with regex |
|
| 94 | + * |
|
| 95 | + * @throws \Exception |
|
| 96 | + */ |
|
| 97 | + private function runWithRegex() |
|
| 98 | + { |
|
| 99 | + if (isset($this->parameters['document'])) { |
|
| 100 | + $this->runByDocumentParameter(); |
|
| 101 | + } else { |
|
| 102 | + $relativeDocumentUri = current($this->matchedSitemapItem->matches[1]); |
|
| 103 | + if (isset($this->parameters['folder'])) { |
|
| 104 | + if (substr($this->parameters['folder'], -1) !== '/') { |
|
| 105 | + $this->parameters['folder'] = $this->parameters['folder'] . '/'; |
|
| 106 | + } |
|
| 107 | + $relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - $document = $this->storage->getDocuments()->getDocumentBySlug($relativeDocumentUri); |
|
| 110 | + $document = $this->storage->getDocuments()->getDocumentBySlug($relativeDocumentUri); |
|
| 111 | 111 | |
| 112 | - if ($document->type == 'folder') { |
|
| 113 | - throw new \Exception('The found document is a folder.'); |
|
| 114 | - } |
|
| 112 | + if ($document->type == 'folder') { |
|
| 113 | + throw new \Exception('The found document is a folder.'); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - if ($document->state != 'published') { |
|
| 117 | - throw new \Exception('Found document is unpublished.'); |
|
| 118 | - } |
|
| 119 | - $this->parameters[$this->documentParameterName] = $document; |
|
| 120 | - } |
|
| 121 | - } |
|
| 116 | + if ($document->state != 'published') { |
|
| 117 | + throw new \Exception('Found document is unpublished.'); |
|
| 118 | + } |
|
| 119 | + $this->parameters[$this->documentParameterName] = $document; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Run using the given `document` parameter |
|
| 125 | - */ |
|
| 126 | - private function runByDocumentParameter() |
|
| 127 | - { |
|
| 128 | - $this->parameters[$this->documentParameterName] = $this->storage->getDocuments()->getDocumentBySlug($this->parameters['document']); |
|
| 129 | - } |
|
| 130 | - } |
|
| 123 | + /** |
|
| 124 | + * Run using the given `document` parameter |
|
| 125 | + */ |
|
| 126 | + private function runByDocumentParameter() |
|
| 127 | + { |
|
| 128 | + $this->parameters[$this->documentParameterName] = $this->storage->getDocuments()->getDocumentBySlug($this->parameters['document']); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | 131 | } |
| 132 | 132 | \ No newline at end of file |
@@ -6,39 +6,39 @@ |
||
| 6 | 6 | use CloudControl\Cms\storage\Storage; |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Interface Component |
|
| 10 | - * @package CloudControl\Cms\components |
|
| 11 | - */ |
|
| 12 | - interface Component |
|
| 13 | - { |
|
| 14 | - /** |
|
| 15 | - * Component constructor. |
|
| 16 | - * |
|
| 17 | - * @param $template |
|
| 18 | - * @param Request $request |
|
| 19 | - * @param $parameters |
|
| 20 | - * @param $matchedSitemapItem |
|
| 21 | - */ |
|
| 22 | - function __construct($template, Request $request, $parameters, $matchedSitemapItem); |
|
| 9 | + * Interface Component |
|
| 10 | + * @package CloudControl\Cms\components |
|
| 11 | + */ |
|
| 12 | + interface Component |
|
| 13 | + { |
|
| 14 | + /** |
|
| 15 | + * Component constructor. |
|
| 16 | + * |
|
| 17 | + * @param $template |
|
| 18 | + * @param Request $request |
|
| 19 | + * @param $parameters |
|
| 20 | + * @param $matchedSitemapItem |
|
| 21 | + */ |
|
| 22 | + function __construct($template, Request $request, $parameters, $matchedSitemapItem); |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param Storage $storage |
|
| 26 | - */ |
|
| 27 | - function run(Storage $storage); |
|
| 24 | + /** |
|
| 25 | + * @param Storage $storage |
|
| 26 | + */ |
|
| 27 | + function run(Storage $storage); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return void |
|
| 31 | - */ |
|
| 32 | - function render(); |
|
| 29 | + /** |
|
| 30 | + * @return void |
|
| 31 | + */ |
|
| 32 | + function render(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @return mixed |
|
| 36 | - */ |
|
| 37 | - function get(); |
|
| 34 | + /** |
|
| 35 | + * @return mixed |
|
| 36 | + */ |
|
| 37 | + function get(); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return \stdClass |
|
| 41 | - */ |
|
| 42 | - function getParameters(); |
|
| 43 | - } |
|
| 39 | + /** |
|
| 40 | + * @return \stdClass |
|
| 41 | + */ |
|
| 42 | + function getParameters(); |
|
| 43 | + } |
|
| 44 | 44 | } |
| 45 | 45 | \ No newline at end of file |
@@ -1,129 +1,129 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace CloudControl\Cms\crypt |
| 3 | 3 | { |
| 4 | - /** |
|
| 5 | - * Class Crypt |
|
| 6 | - * @package CloudControl\Cms\crypt |
|
| 7 | - */ |
|
| 8 | - class Crypt |
|
| 9 | - { |
|
| 10 | - /** |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - private $lastSalt; |
|
| 4 | + /** |
|
| 5 | + * Class Crypt |
|
| 6 | + * @package CloudControl\Cms\crypt |
|
| 7 | + */ |
|
| 8 | + class Crypt |
|
| 9 | + { |
|
| 10 | + /** |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + private $lastSalt; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Encrypts the given value using the blowfish algorithm |
|
| 17 | - * |
|
| 18 | - * @param string $value The sting to be encrypted |
|
| 19 | - * @param int $encryptionIterations The amount of iterations used for encryption, 13 by default, resulting in aprox. 0.5 seconds of encrypting. Each raise, will result in about double the time |
|
| 20 | - * @return string The hash |
|
| 21 | - */ |
|
| 22 | - public function encrypt($value, $encryptionIterations = 13) |
|
| 23 | - { |
|
| 24 | - $random = $this->getRandomBytes(16); |
|
| 25 | - $this->lastSalt = $this->getSalt($random, $encryptionIterations); |
|
| 26 | - $hash = crypt($value, $this->lastSalt); |
|
| 27 | - return $hash; |
|
| 28 | - } |
|
| 15 | + /** |
|
| 16 | + * Encrypts the given value using the blowfish algorithm |
|
| 17 | + * |
|
| 18 | + * @param string $value The sting to be encrypted |
|
| 19 | + * @param int $encryptionIterations The amount of iterations used for encryption, 13 by default, resulting in aprox. 0.5 seconds of encrypting. Each raise, will result in about double the time |
|
| 20 | + * @return string The hash |
|
| 21 | + */ |
|
| 22 | + public function encrypt($value, $encryptionIterations = 13) |
|
| 23 | + { |
|
| 24 | + $random = $this->getRandomBytes(16); |
|
| 25 | + $this->lastSalt = $this->getSalt($random, $encryptionIterations); |
|
| 26 | + $hash = crypt($value, $this->lastSalt); |
|
| 27 | + return $hash; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * If on Linux, tries to use built in random byte feed |
|
| 32 | - * else generates its own feed |
|
| 33 | - * |
|
| 34 | - * @param int $count The amount of bytes to generates |
|
| 35 | - * @return string The bytes |
|
| 36 | - */ |
|
| 37 | - private function getRandomBytes($count) |
|
| 38 | - { |
|
| 39 | - $output = ''; |
|
| 40 | - $random_state = microtime(); |
|
| 30 | + /** |
|
| 31 | + * If on Linux, tries to use built in random byte feed |
|
| 32 | + * else generates its own feed |
|
| 33 | + * |
|
| 34 | + * @param int $count The amount of bytes to generates |
|
| 35 | + * @return string The bytes |
|
| 36 | + */ |
|
| 37 | + private function getRandomBytes($count) |
|
| 38 | + { |
|
| 39 | + $output = ''; |
|
| 40 | + $random_state = microtime(); |
|
| 41 | 41 | |
| 42 | - $openBasedir = ini_get('open_basedir'); |
|
| 43 | - if (empty($openBasedir) && |
|
| 44 | - is_readable('/dev/urandom') && |
|
| 45 | - ($fh = @fopen('/dev/urandom', 'rb'))) { |
|
| 46 | - $output = fread($fh, $count); |
|
| 47 | - fclose($fh); |
|
| 48 | - } |
|
| 42 | + $openBasedir = ini_get('open_basedir'); |
|
| 43 | + if (empty($openBasedir) && |
|
| 44 | + is_readable('/dev/urandom') && |
|
| 45 | + ($fh = @fopen('/dev/urandom', 'rb'))) { |
|
| 46 | + $output = fread($fh, $count); |
|
| 47 | + fclose($fh); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - if (strlen($output) < $count) { |
|
| 51 | - $output = ''; |
|
| 52 | - for ($i = 0; $i < $count; $i += 16) { |
|
| 53 | - $random_state = |
|
| 54 | - md5(microtime() . $random_state); |
|
| 55 | - $output .= |
|
| 56 | - pack('H*', md5($random_state)); |
|
| 57 | - } |
|
| 58 | - $output = substr($output, 0, $count); |
|
| 59 | - } |
|
| 50 | + if (strlen($output) < $count) { |
|
| 51 | + $output = ''; |
|
| 52 | + for ($i = 0; $i < $count; $i += 16) { |
|
| 53 | + $random_state = |
|
| 54 | + md5(microtime() . $random_state); |
|
| 55 | + $output .= |
|
| 56 | + pack('H*', md5($random_state)); |
|
| 57 | + } |
|
| 58 | + $output = substr($output, 0, $count); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - return $output; |
|
| 62 | - } |
|
| 61 | + return $output; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Generates the salt used for encryption |
|
| 66 | - * |
|
| 67 | - * @param string $input Feed for iteration |
|
| 68 | - * @param int $iterations Amount of iterations |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - private function getSalt($input, $iterations) |
|
| 73 | - { |
|
| 74 | - $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
| 64 | + /** |
|
| 65 | + * Generates the salt used for encryption |
|
| 66 | + * |
|
| 67 | + * @param string $input Feed for iteration |
|
| 68 | + * @param int $iterations Amount of iterations |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + private function getSalt($input, $iterations) |
|
| 73 | + { |
|
| 74 | + $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
| 75 | 75 | |
| 76 | - $output = '$2a$'; |
|
| 77 | - $output .= chr(ord('0') + $iterations / 10); |
|
| 78 | - $output .= chr(ord('0') + $iterations % 10); |
|
| 79 | - $output .= '$'; |
|
| 76 | + $output = '$2a$'; |
|
| 77 | + $output .= chr(ord('0') + $iterations / 10); |
|
| 78 | + $output .= chr(ord('0') + $iterations % 10); |
|
| 79 | + $output .= '$'; |
|
| 80 | 80 | |
| 81 | - $i = 0; |
|
| 82 | - do { |
|
| 83 | - $c1 = ord($input[$i++]); |
|
| 84 | - $output .= $itoa64[$c1 >> 2]; |
|
| 85 | - $c1 = ($c1 & 0x03) << 4; |
|
| 86 | - if ($i >= 16) { |
|
| 87 | - $output .= $itoa64[$c1]; |
|
| 88 | - break; |
|
| 89 | - } |
|
| 81 | + $i = 0; |
|
| 82 | + do { |
|
| 83 | + $c1 = ord($input[$i++]); |
|
| 84 | + $output .= $itoa64[$c1 >> 2]; |
|
| 85 | + $c1 = ($c1 & 0x03) << 4; |
|
| 86 | + if ($i >= 16) { |
|
| 87 | + $output .= $itoa64[$c1]; |
|
| 88 | + break; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - $c2 = ord($input[$i++]); |
|
| 92 | - $c1 |= $c2 >> 4; |
|
| 93 | - $output .= $itoa64[$c1]; |
|
| 94 | - $c1 = ($c2 & 0x0f) << 2; |
|
| 91 | + $c2 = ord($input[$i++]); |
|
| 92 | + $c1 |= $c2 >> 4; |
|
| 93 | + $output .= $itoa64[$c1]; |
|
| 94 | + $c1 = ($c2 & 0x0f) << 2; |
|
| 95 | 95 | |
| 96 | - $c2 = ord($input[$i++]); |
|
| 97 | - $c1 |= $c2 >> 6; |
|
| 98 | - $output .= $itoa64[$c1]; |
|
| 99 | - $output .= $itoa64[$c2 & 0x3f]; |
|
| 100 | - } while (1); |
|
| 96 | + $c2 = ord($input[$i++]); |
|
| 97 | + $c1 |= $c2 >> 6; |
|
| 98 | + $output .= $itoa64[$c1]; |
|
| 99 | + $output .= $itoa64[$c2 & 0x3f]; |
|
| 100 | + } while (1); |
|
| 101 | 101 | |
| 102 | - return $output; |
|
| 103 | - } |
|
| 102 | + return $output; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Returns the last used salt for encryption |
|
| 107 | - * |
|
| 108 | - * @return string | NULL |
|
| 109 | - */ |
|
| 110 | - public function getLastSalt() |
|
| 111 | - { |
|
| 112 | - return $this->lastSalt; |
|
| 113 | - } |
|
| 105 | + /** |
|
| 106 | + * Returns the last used salt for encryption |
|
| 107 | + * |
|
| 108 | + * @return string | NULL |
|
| 109 | + */ |
|
| 110 | + public function getLastSalt() |
|
| 111 | + { |
|
| 112 | + return $this->lastSalt; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Compare the input with a known hash and salt |
|
| 117 | - * |
|
| 118 | - * @param $input |
|
| 119 | - * @param $hash |
|
| 120 | - * @param $salt |
|
| 121 | - * @return bool |
|
| 122 | - */ |
|
| 123 | - public function compare($input, $hash, $salt) |
|
| 124 | - { |
|
| 125 | - $newHash = crypt($input, $salt); |
|
| 126 | - return $newHash == $hash; |
|
| 127 | - } |
|
| 128 | - } |
|
| 115 | + /** |
|
| 116 | + * Compare the input with a known hash and salt |
|
| 117 | + * |
|
| 118 | + * @param $input |
|
| 119 | + * @param $hash |
|
| 120 | + * @param $salt |
|
| 121 | + * @return bool |
|
| 122 | + */ |
|
| 123 | + public function compare($input, $hash, $salt) |
|
| 124 | + { |
|
| 125 | + $newHash = crypt($input, $salt); |
|
| 126 | + return $newHash == $hash; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | } |
| 130 | 130 | \ No newline at end of file |
@@ -24,92 +24,92 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class TermFrequency |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * @var \PDO |
|
| 29 | - */ |
|
| 30 | - protected $dbHandle; |
|
| 27 | + /** |
|
| 28 | + * @var \PDO |
|
| 29 | + */ |
|
| 30 | + protected $dbHandle; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * TermFrequency constructor. |
|
| 34 | - * |
|
| 35 | - * @param \PDO $dbHandle |
|
| 36 | - */ |
|
| 37 | - public function __construct($dbHandle) |
|
| 38 | - { |
|
| 39 | - $this->dbHandle = $dbHandle; |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * TermFrequency constructor. |
|
| 34 | + * |
|
| 35 | + * @param \PDO $dbHandle |
|
| 36 | + */ |
|
| 37 | + public function __construct($dbHandle) |
|
| 38 | + { |
|
| 39 | + $this->dbHandle = $dbHandle; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function execute() |
|
| 43 | - { |
|
| 44 | - $db = $this->dbHandle; |
|
| 45 | - $totalTermCountPerDocument = $this->getTotalTermCountPerDocument($db); |
|
| 46 | - foreach ($totalTermCountPerDocument as $documentField) { |
|
| 47 | - $termsForDocumentField = $this->getTermsForDocumentField($documentField->documentPath, $documentField->field); |
|
| 48 | - $sql = ' |
|
| 42 | + public function execute() |
|
| 43 | + { |
|
| 44 | + $db = $this->dbHandle; |
|
| 45 | + $totalTermCountPerDocument = $this->getTotalTermCountPerDocument($db); |
|
| 46 | + foreach ($totalTermCountPerDocument as $documentField) { |
|
| 47 | + $termsForDocumentField = $this->getTermsForDocumentField($documentField->documentPath, $documentField->field); |
|
| 48 | + $sql = ' |
|
| 49 | 49 | INSERT INTO term_frequency (documentPath, field, term, frequency) |
| 50 | 50 | VALUES |
| 51 | 51 | '; |
| 52 | - $quotedDocumentPath = $db->quote($documentField->documentPath); |
|
| 53 | - $quotedField = $db->quote($documentField->field); |
|
| 54 | - $values = array(); |
|
| 55 | - $i = 0; |
|
| 56 | - foreach ($termsForDocumentField as $term) { |
|
| 57 | - $frequency = intval($term->count) / $documentField->totalTermCount; |
|
| 58 | - $values[] = $quotedDocumentPath . ',' . $quotedField . ', ' . $db->quote($term->term) . ', ' . $db->quote($frequency); |
|
| 59 | - $i += 1; |
|
| 60 | - if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) { |
|
| 61 | - $this->executeStore($sql, $values, $db); |
|
| 62 | - $i = 0; |
|
| 63 | - $values = array(); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - if (count($values) != 0) { |
|
| 67 | - $this->executeStore($sql, $values, $db); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - } |
|
| 52 | + $quotedDocumentPath = $db->quote($documentField->documentPath); |
|
| 53 | + $quotedField = $db->quote($documentField->field); |
|
| 54 | + $values = array(); |
|
| 55 | + $i = 0; |
|
| 56 | + foreach ($termsForDocumentField as $term) { |
|
| 57 | + $frequency = intval($term->count) / $documentField->totalTermCount; |
|
| 58 | + $values[] = $quotedDocumentPath . ',' . $quotedField . ', ' . $db->quote($term->term) . ', ' . $db->quote($frequency); |
|
| 59 | + $i += 1; |
|
| 60 | + if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) { |
|
| 61 | + $this->executeStore($sql, $values, $db); |
|
| 62 | + $i = 0; |
|
| 63 | + $values = array(); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + if (count($values) != 0) { |
|
| 67 | + $this->executeStore($sql, $values, $db); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - private function getTermsForDocumentField($documentPath, $field) |
|
| 73 | - { |
|
| 74 | - $db = $this->dbHandle; |
|
| 75 | - $stmt = $db->prepare(' |
|
| 72 | + private function getTermsForDocumentField($documentPath, $field) |
|
| 73 | + { |
|
| 74 | + $db = $this->dbHandle; |
|
| 75 | + $stmt = $db->prepare(' |
|
| 76 | 76 | SELECT `term`, `count` |
| 77 | 77 | FROM `term_count` |
| 78 | 78 | WHERE `documentPath` = :documentPath |
| 79 | 79 | AND `field` = :field |
| 80 | 80 | '); |
| 81 | - $stmt->bindValue(':documentPath', $documentPath); |
|
| 82 | - $stmt->bindValue(':field', $field); |
|
| 83 | - $stmt->execute(); |
|
| 84 | - return $stmt->fetchAll(\PDO::FETCH_CLASS); |
|
| 85 | - } |
|
| 81 | + $stmt->bindValue(':documentPath', $documentPath); |
|
| 82 | + $stmt->bindValue(':field', $field); |
|
| 83 | + $stmt->execute(); |
|
| 84 | + return $stmt->fetchAll(\PDO::FETCH_CLASS); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param $db |
|
| 89 | - * |
|
| 90 | - * @return mixed |
|
| 91 | - */ |
|
| 92 | - private function getTotalTermCountPerDocument($db) |
|
| 93 | - { |
|
| 94 | - $stmt = $db->prepare(' |
|
| 87 | + /** |
|
| 88 | + * @param $db |
|
| 89 | + * |
|
| 90 | + * @return mixed |
|
| 91 | + */ |
|
| 92 | + private function getTotalTermCountPerDocument($db) |
|
| 93 | + { |
|
| 94 | + $stmt = $db->prepare(' |
|
| 95 | 95 | SELECT documentPath, field, SUM(count) as totalTermCount |
| 96 | 96 | FROM term_count |
| 97 | 97 | GROUP BY documentPath, field |
| 98 | 98 | '); |
| 99 | - $stmt->execute(); |
|
| 100 | - $totalTermCountPerDocument = $stmt->fetchAll(\PDO::FETCH_CLASS); |
|
| 99 | + $stmt->execute(); |
|
| 100 | + $totalTermCountPerDocument = $stmt->fetchAll(\PDO::FETCH_CLASS); |
|
| 101 | 101 | |
| 102 | - return $totalTermCountPerDocument; |
|
| 103 | - } |
|
| 102 | + return $totalTermCountPerDocument; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - private function executeStore($sql, $values, $db) |
|
| 106 | - { |
|
| 107 | - $sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');'; |
|
| 108 | - if (!$db->query($sql)) { |
|
| 109 | - $errorInfo = $db->errorInfo(); |
|
| 110 | - $errorMsg = $errorInfo[2]; |
|
| 111 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 112 | - } |
|
| 113 | - } |
|
| 105 | + private function executeStore($sql, $values, $db) |
|
| 106 | + { |
|
| 107 | + $sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');'; |
|
| 108 | + if (!$db->query($sql)) { |
|
| 109 | + $errorInfo = $db->errorInfo(); |
|
| 110 | + $errorMsg = $errorInfo[2]; |
|
| 111 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | \ No newline at end of file |
@@ -10,51 +10,51 @@ |
||
| 10 | 10 | |
| 11 | 11 | class InverseDocumentFrequency |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var \PDO |
|
| 15 | - */ |
|
| 16 | - protected $dbHandle; |
|
| 17 | - protected $documentCount; |
|
| 13 | + /** |
|
| 14 | + * @var \PDO |
|
| 15 | + */ |
|
| 16 | + protected $dbHandle; |
|
| 17 | + protected $documentCount; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * InverseDocumentFrequency constructor. |
|
| 21 | - * |
|
| 22 | - * @param \PDO $dbHandle |
|
| 23 | - * @param int $documentCount |
|
| 24 | - */ |
|
| 25 | - public function __construct($dbHandle, $documentCount) |
|
| 26 | - { |
|
| 27 | - $this->dbHandle = $dbHandle; |
|
| 28 | - $this->documentCount = $documentCount; |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * InverseDocumentFrequency constructor. |
|
| 21 | + * |
|
| 22 | + * @param \PDO $dbHandle |
|
| 23 | + * @param int $documentCount |
|
| 24 | + */ |
|
| 25 | + public function __construct($dbHandle, $documentCount) |
|
| 26 | + { |
|
| 27 | + $this->dbHandle = $dbHandle; |
|
| 28 | + $this->documentCount = $documentCount; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Formula to calculate: |
|
| 33 | - * idf(t) = 1 + log ( totalDocuments / (documentsThatContainTheTerm + 1)) |
|
| 34 | - * @throws \Exception |
|
| 35 | - */ |
|
| 36 | - public function execute() |
|
| 37 | - { |
|
| 38 | - $db = $this->dbHandle; |
|
| 39 | - $db->sqliteCreateFunction('log', 'log', 1); |
|
| 40 | - $sql = ' |
|
| 31 | + /** |
|
| 32 | + * Formula to calculate: |
|
| 33 | + * idf(t) = 1 + log ( totalDocuments / (documentsThatContainTheTerm + 1)) |
|
| 34 | + * @throws \Exception |
|
| 35 | + */ |
|
| 36 | + public function execute() |
|
| 37 | + { |
|
| 38 | + $db = $this->dbHandle; |
|
| 39 | + $db->sqliteCreateFunction('log', 'log', 1); |
|
| 40 | + $sql = ' |
|
| 41 | 41 | INSERT INTO inverse_document_frequency (term, inverseDocumentFrequency) |
| 42 | 42 | SELECT DISTINCT term, (1+(log(:documentCount / COUNT(documentPath) + 1))) as inverseDocumentFrequency |
| 43 | 43 | FROM term_count |
| 44 | 44 | GROUP BY term |
| 45 | 45 | '; |
| 46 | 46 | |
| 47 | - if (!$stmt = $db->prepare($sql)) { |
|
| 48 | - $errorInfo = $db->errorInfo(); |
|
| 49 | - $errorMsg = $errorInfo[2]; |
|
| 50 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 51 | - } |
|
| 52 | - $stmt->bindValue(':documentCount', $this->documentCount); |
|
| 53 | - $result = $stmt->execute(); |
|
| 54 | - if ($result === false) { |
|
| 55 | - $errorInfo = $db->errorInfo(); |
|
| 56 | - $errorMsg = $errorInfo[2]; |
|
| 57 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 58 | - } |
|
| 59 | - } |
|
| 47 | + if (!$stmt = $db->prepare($sql)) { |
|
| 48 | + $errorInfo = $db->errorInfo(); |
|
| 49 | + $errorMsg = $errorInfo[2]; |
|
| 50 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 51 | + } |
|
| 52 | + $stmt->bindValue(':documentCount', $this->documentCount); |
|
| 53 | + $result = $stmt->execute(); |
|
| 54 | + if ($result === false) { |
|
| 55 | + $errorInfo = $db->errorInfo(); |
|
| 56 | + $errorMsg = $errorInfo[2]; |
|
| 57 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | } |
| 61 | 61 | \ No newline at end of file |
@@ -14,113 +14,113 @@ |
||
| 14 | 14 | |
| 15 | 15 | class TermCount |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * @var \PDO |
|
| 19 | - */ |
|
| 20 | - protected $dbHandle; |
|
| 21 | - protected $documents; |
|
| 22 | - protected $filters; |
|
| 23 | - protected $storage; |
|
| 17 | + /** |
|
| 18 | + * @var \PDO |
|
| 19 | + */ |
|
| 20 | + protected $dbHandle; |
|
| 21 | + protected $documents; |
|
| 22 | + protected $filters; |
|
| 23 | + protected $storage; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * TermCount constructor. |
|
| 27 | - * |
|
| 28 | - * @param \PDO $dbHandle |
|
| 29 | - * @param array $documents |
|
| 30 | - * @param array $filters |
|
| 31 | - * @param Storage $jsonStorage |
|
| 32 | - */ |
|
| 33 | - public function __construct($dbHandle, $documents, $filters, $jsonStorage) |
|
| 34 | - { |
|
| 35 | - $this->dbHandle = $dbHandle; |
|
| 36 | - $this->documents = $documents; |
|
| 37 | - $this->filters = $filters; |
|
| 38 | - $this->storage = $jsonStorage; |
|
| 39 | - } |
|
| 25 | + /** |
|
| 26 | + * TermCount constructor. |
|
| 27 | + * |
|
| 28 | + * @param \PDO $dbHandle |
|
| 29 | + * @param array $documents |
|
| 30 | + * @param array $filters |
|
| 31 | + * @param Storage $jsonStorage |
|
| 32 | + */ |
|
| 33 | + public function __construct($dbHandle, $documents, $filters, $jsonStorage) |
|
| 34 | + { |
|
| 35 | + $this->dbHandle = $dbHandle; |
|
| 36 | + $this->documents = $documents; |
|
| 37 | + $this->filters = $filters; |
|
| 38 | + $this->storage = $jsonStorage; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - public function execute() |
|
| 42 | - { |
|
| 43 | - $this->iterateDocumentsAndCreateTermCount($this->documents); |
|
| 44 | - } |
|
| 41 | + public function execute() |
|
| 42 | + { |
|
| 43 | + $this->iterateDocumentsAndCreateTermCount($this->documents); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - protected function applyFilters($tokens) |
|
| 47 | - { |
|
| 48 | - foreach ($this->filters as $filterName) { |
|
| 49 | - $filterClassName = '\CloudControl\Cms\search\filters\\' . $filterName; |
|
| 50 | - $filter = new $filterClassName($tokens); |
|
| 51 | - $tokens = $filter->getFilterResults(); |
|
| 52 | - } |
|
| 53 | - return $tokens; |
|
| 54 | - } |
|
| 46 | + protected function applyFilters($tokens) |
|
| 47 | + { |
|
| 48 | + foreach ($this->filters as $filterName) { |
|
| 49 | + $filterClassName = '\CloudControl\Cms\search\filters\\' . $filterName; |
|
| 50 | + $filter = new $filterClassName($tokens); |
|
| 51 | + $tokens = $filter->getFilterResults(); |
|
| 52 | + } |
|
| 53 | + return $tokens; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - protected function storeDocumentTermCount($document, $documentTermCount) |
|
| 57 | - { |
|
| 58 | - $db = $this->dbHandle; |
|
| 59 | - $sqlStart = ' |
|
| 56 | + protected function storeDocumentTermCount($document, $documentTermCount) |
|
| 57 | + { |
|
| 58 | + $db = $this->dbHandle; |
|
| 59 | + $sqlStart = ' |
|
| 60 | 60 | INSERT INTO `term_count` (`documentPath`, `term`, `count`, `field`) |
| 61 | 61 | VALUES '; |
| 62 | - $sql = $sqlStart; |
|
| 63 | - $values = array(); |
|
| 64 | - $quotedDocumentPath = $db->quote($document->path); |
|
| 65 | - $i = 0; |
|
| 66 | - foreach ($documentTermCount as $field => $countArray) { |
|
| 67 | - $quotedField = $db->quote($field); |
|
| 68 | - foreach ($countArray as $term => $count) { |
|
| 69 | - $values[] = $quotedDocumentPath . ', ' . $db->quote($term) . ', ' . $db->quote($count) . ', ' . $quotedField; |
|
| 70 | - $i += 1; |
|
| 71 | - if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) { |
|
| 72 | - $this->executeStoreDocumentTermCount($values, $sql, $db); |
|
| 73 | - $values = array(); |
|
| 74 | - $sql = $sqlStart; |
|
| 75 | - $i = 0; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - if (count($values) != 0) { |
|
| 80 | - $this->executeStoreDocumentTermCount($values, $sql, $db); |
|
| 81 | - } |
|
| 82 | - } |
|
| 62 | + $sql = $sqlStart; |
|
| 63 | + $values = array(); |
|
| 64 | + $quotedDocumentPath = $db->quote($document->path); |
|
| 65 | + $i = 0; |
|
| 66 | + foreach ($documentTermCount as $field => $countArray) { |
|
| 67 | + $quotedField = $db->quote($field); |
|
| 68 | + foreach ($countArray as $term => $count) { |
|
| 69 | + $values[] = $quotedDocumentPath . ', ' . $db->quote($term) . ', ' . $db->quote($count) . ', ' . $quotedField; |
|
| 70 | + $i += 1; |
|
| 71 | + if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) { |
|
| 72 | + $this->executeStoreDocumentTermCount($values, $sql, $db); |
|
| 73 | + $values = array(); |
|
| 74 | + $sql = $sqlStart; |
|
| 75 | + $i = 0; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + if (count($values) != 0) { |
|
| 80 | + $this->executeStoreDocumentTermCount($values, $sql, $db); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @param $values |
|
| 86 | - * @param $sql |
|
| 87 | - * @param $db |
|
| 88 | - * |
|
| 89 | - * @throws \Exception |
|
| 90 | - */ |
|
| 91 | - protected function executeStoreDocumentTermCount($values, $sql, $db) |
|
| 92 | - { |
|
| 93 | - $sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');'; |
|
| 94 | - $stmt = $db->prepare($sql); |
|
| 95 | - if ($stmt === false || !$stmt->execute()) { |
|
| 96 | - $errorInfo = $db->errorInfo(); |
|
| 97 | - $errorMsg = $errorInfo[2]; |
|
| 98 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 99 | - } |
|
| 100 | - } |
|
| 84 | + /** |
|
| 85 | + * @param $values |
|
| 86 | + * @param $sql |
|
| 87 | + * @param $db |
|
| 88 | + * |
|
| 89 | + * @throws \Exception |
|
| 90 | + */ |
|
| 91 | + protected function executeStoreDocumentTermCount($values, $sql, $db) |
|
| 92 | + { |
|
| 93 | + $sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');'; |
|
| 94 | + $stmt = $db->prepare($sql); |
|
| 95 | + if ($stmt === false || !$stmt->execute()) { |
|
| 96 | + $errorInfo = $db->errorInfo(); |
|
| 97 | + $errorMsg = $errorInfo[2]; |
|
| 98 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * @param $document |
|
| 104 | - */ |
|
| 105 | - private function createTermCountForDocument($document) |
|
| 106 | - { |
|
| 107 | - $tokenizer = new DocumentTokenizer($document, $this->storage); |
|
| 108 | - $tokens = $tokenizer->getTokens(); |
|
| 109 | - $documentTermCount = $this->applyFilters($tokens); |
|
| 110 | - $this->storeDocumentTermCount($document, $documentTermCount); |
|
| 111 | - } |
|
| 102 | + /** |
|
| 103 | + * @param $document |
|
| 104 | + */ |
|
| 105 | + private function createTermCountForDocument($document) |
|
| 106 | + { |
|
| 107 | + $tokenizer = new DocumentTokenizer($document, $this->storage); |
|
| 108 | + $tokens = $tokenizer->getTokens(); |
|
| 109 | + $documentTermCount = $this->applyFilters($tokens); |
|
| 110 | + $this->storeDocumentTermCount($document, $documentTermCount); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * @param $documents |
|
| 115 | - */ |
|
| 116 | - private function iterateDocumentsAndCreateTermCount($documents) |
|
| 117 | - { |
|
| 118 | - foreach ($documents as $document) { |
|
| 119 | - if ($document->type === 'folder') { |
|
| 120 | - $this->iterateDocumentsAndCreateTermCount($document->content); |
|
| 121 | - } else { |
|
| 122 | - $this->createTermCountForDocument($document); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - } |
|
| 113 | + /** |
|
| 114 | + * @param $documents |
|
| 115 | + */ |
|
| 116 | + private function iterateDocumentsAndCreateTermCount($documents) |
|
| 117 | + { |
|
| 118 | + foreach ($documents as $document) { |
|
| 119 | + if ($document->type === 'folder') { |
|
| 120 | + $this->iterateDocumentsAndCreateTermCount($document->content); |
|
| 121 | + } else { |
|
| 122 | + $this->createTermCountForDocument($document); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | } |
| 127 | 127 | \ No newline at end of file |
@@ -17,72 +17,72 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class TermFieldLengthNorm |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var \PDO |
|
| 22 | - */ |
|
| 23 | - protected $dbHandle; |
|
| 20 | + /** |
|
| 21 | + * @var \PDO |
|
| 22 | + */ |
|
| 23 | + protected $dbHandle; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * TermFieldLengthNorm constructor. |
|
| 27 | - * |
|
| 28 | - * @param \PDO $dbHandle |
|
| 29 | - */ |
|
| 30 | - public function __construct($dbHandle) |
|
| 31 | - { |
|
| 32 | - $this->dbHandle = $dbHandle; |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * TermFieldLengthNorm constructor. |
|
| 27 | + * |
|
| 28 | + * @param \PDO $dbHandle |
|
| 29 | + */ |
|
| 30 | + public function __construct($dbHandle) |
|
| 31 | + { |
|
| 32 | + $this->dbHandle = $dbHandle; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - public function execute() |
|
| 36 | - { |
|
| 37 | - $db = $this->dbHandle; |
|
| 38 | - $db->sqliteCreateFunction('sqrt', 'sqrt', 1); |
|
| 39 | - $sql = ' |
|
| 35 | + public function execute() |
|
| 36 | + { |
|
| 37 | + $db = $this->dbHandle; |
|
| 38 | + $db->sqliteCreateFunction('sqrt', 'sqrt', 1); |
|
| 39 | + $sql = ' |
|
| 40 | 40 | SELECT documentPath, field, COUNT(`count`) as termCount |
| 41 | 41 | FROM term_count |
| 42 | 42 | GROUP BY documentPath, field |
| 43 | 43 | '; |
| 44 | - $stmt = $db->prepare($sql); |
|
| 45 | - if ($stmt === false) { |
|
| 46 | - $errorInfo = $db->errorInfo(); |
|
| 47 | - $errorMsg = $errorInfo[2]; |
|
| 48 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 49 | - } |
|
| 50 | - if (($stmt->execute()) === false) { |
|
| 51 | - $errorInfo = $db->errorInfo(); |
|
| 52 | - $errorMsg = $errorInfo[2]; |
|
| 53 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 54 | - } |
|
| 55 | - $uniqueFieldsPerDocument = $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 56 | - $values = array(); |
|
| 57 | - $i = 0; |
|
| 58 | - foreach ($uniqueFieldsPerDocument as $fieldRow) { |
|
| 59 | - $values[] = 'UPDATE term_frequency SET termNorm = 1/sqrt(' . intval($fieldRow->termCount) . ') WHERE documentPath = ' . $db->quote($fieldRow->documentPath) . ' AND field = ' . $db->quote($fieldRow->field) . ';'; |
|
| 60 | - $i += 1; |
|
| 61 | - if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) { |
|
| 62 | - $this->executeUpdateTermNorm($values, $db); |
|
| 63 | - $values = array(); |
|
| 64 | - $i = 0; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - if (count($values) != 0) { |
|
| 68 | - $this->executeUpdateTermNorm($values, $db); |
|
| 69 | - } |
|
| 70 | - } |
|
| 44 | + $stmt = $db->prepare($sql); |
|
| 45 | + if ($stmt === false) { |
|
| 46 | + $errorInfo = $db->errorInfo(); |
|
| 47 | + $errorMsg = $errorInfo[2]; |
|
| 48 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 49 | + } |
|
| 50 | + if (($stmt->execute()) === false) { |
|
| 51 | + $errorInfo = $db->errorInfo(); |
|
| 52 | + $errorMsg = $errorInfo[2]; |
|
| 53 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 54 | + } |
|
| 55 | + $uniqueFieldsPerDocument = $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 56 | + $values = array(); |
|
| 57 | + $i = 0; |
|
| 58 | + foreach ($uniqueFieldsPerDocument as $fieldRow) { |
|
| 59 | + $values[] = 'UPDATE term_frequency SET termNorm = 1/sqrt(' . intval($fieldRow->termCount) . ') WHERE documentPath = ' . $db->quote($fieldRow->documentPath) . ' AND field = ' . $db->quote($fieldRow->field) . ';'; |
|
| 60 | + $i += 1; |
|
| 61 | + if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) { |
|
| 62 | + $this->executeUpdateTermNorm($values, $db); |
|
| 63 | + $values = array(); |
|
| 64 | + $i = 0; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + if (count($values) != 0) { |
|
| 68 | + $this->executeUpdateTermNorm($values, $db); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param array $values |
|
| 74 | - * @param \PDO $db |
|
| 75 | - * @throws \Exception |
|
| 76 | - */ |
|
| 77 | - private function executeUpdateTermNorm($values, $db) |
|
| 78 | - { |
|
| 79 | - $sql = 'BEGIN TRANSACTION;' . PHP_EOL; |
|
| 80 | - $sql .= implode(PHP_EOL, $values) . PHP_EOL; |
|
| 81 | - $sql .= 'COMMIT;'; |
|
| 82 | - if (($db->exec($sql)) === false) { |
|
| 83 | - $errorInfo = $db->errorInfo(); |
|
| 84 | - $errorMsg = $errorInfo[2]; |
|
| 85 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 86 | - } |
|
| 87 | - } |
|
| 72 | + /** |
|
| 73 | + * @param array $values |
|
| 74 | + * @param \PDO $db |
|
| 75 | + * @throws \Exception |
|
| 76 | + */ |
|
| 77 | + private function executeUpdateTermNorm($values, $db) |
|
| 78 | + { |
|
| 79 | + $sql = 'BEGIN TRANSACTION;' . PHP_EOL; |
|
| 80 | + $sql .= implode(PHP_EOL, $values) . PHP_EOL; |
|
| 81 | + $sql .= 'COMMIT;'; |
|
| 82 | + if (($db->exec($sql)) === false) { |
|
| 83 | + $errorInfo = $db->errorInfo(); |
|
| 84 | + $errorMsg = $errorInfo[2]; |
|
| 85 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -26,117 +26,117 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | class Search extends SearchDbConnected |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * @var Tokenizer |
|
| 31 | - */ |
|
| 32 | - protected $tokenizer; |
|
| 33 | - protected $results = array(); |
|
| 29 | + /** |
|
| 30 | + * @var Tokenizer |
|
| 31 | + */ |
|
| 32 | + protected $tokenizer; |
|
| 33 | + protected $results = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * An array containing classes implementing \CloudControl\Cms\search\Filters |
|
| 37 | - * These will be applied to all tokenizers |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $filters = array( |
|
| 41 | - 'DutchStopWords', |
|
| 42 | - 'EnglishStopWords' |
|
| 43 | - ); |
|
| 35 | + /** |
|
| 36 | + * An array containing classes implementing \CloudControl\Cms\search\Filters |
|
| 37 | + * These will be applied to all tokenizers |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $filters = array( |
|
| 41 | + 'DutchStopWords', |
|
| 42 | + 'EnglishStopWords' |
|
| 43 | + ); |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Returns an array of SeachResult and / or SearchSuggestion objects, |
|
| 47 | - * based on the tokens in the Tokenizer |
|
| 48 | - * @param Tokenizer $tokenizer |
|
| 49 | - * |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - public function getDocumentsForTokenizer(Tokenizer $tokenizer) |
|
| 53 | - { |
|
| 54 | - $this->tokenizer = $tokenizer; |
|
| 55 | - $resultsPerTokens = $this->queryTokens(); |
|
| 45 | + /** |
|
| 46 | + * Returns an array of SeachResult and / or SearchSuggestion objects, |
|
| 47 | + * based on the tokens in the Tokenizer |
|
| 48 | + * @param Tokenizer $tokenizer |
|
| 49 | + * |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + public function getDocumentsForTokenizer(Tokenizer $tokenizer) |
|
| 53 | + { |
|
| 54 | + $this->tokenizer = $tokenizer; |
|
| 55 | + $resultsPerTokens = $this->queryTokens(); |
|
| 56 | 56 | |
| 57 | - $flatResults = $this->flattenResults($resultsPerTokens); |
|
| 58 | - $flatResults = $this->applyQueryCoordination($flatResults); |
|
| 59 | - usort($flatResults, array($this, "scoreCompare")); |
|
| 57 | + $flatResults = $this->flattenResults($resultsPerTokens); |
|
| 58 | + $flatResults = $this->applyQueryCoordination($flatResults); |
|
| 59 | + usort($flatResults, array($this, "scoreCompare")); |
|
| 60 | 60 | |
| 61 | - $flatResults = array_merge($this->getSearchSuggestions(), $flatResults); |
|
| 61 | + $flatResults = array_merge($this->getSearchSuggestions(), $flatResults); |
|
| 62 | 62 | |
| 63 | - return $flatResults; |
|
| 64 | - } |
|
| 63 | + return $flatResults; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Returns the amount of distinct documents |
|
| 68 | - * that are currently in the search index. |
|
| 69 | - * @return int |
|
| 70 | - * @throws \Exception |
|
| 71 | - */ |
|
| 72 | - public function getIndexedDocuments() |
|
| 73 | - { |
|
| 74 | - $db = $this->getSearchDbHandle(); |
|
| 75 | - $sql = ' |
|
| 66 | + /** |
|
| 67 | + * Returns the amount of distinct documents |
|
| 68 | + * that are currently in the search index. |
|
| 69 | + * @return int |
|
| 70 | + * @throws \Exception |
|
| 71 | + */ |
|
| 72 | + public function getIndexedDocuments() |
|
| 73 | + { |
|
| 74 | + $db = $this->getSearchDbHandle(); |
|
| 75 | + $sql = ' |
|
| 76 | 76 | SELECT count(DISTINCT documentPath) as indexedDocuments |
| 77 | 77 | FROM term_frequency |
| 78 | 78 | '; |
| 79 | - if (!$stmt = $db->query($sql)) { |
|
| 80 | - $errorInfo = $db->errorInfo(); |
|
| 81 | - $errorMsg = $errorInfo[2]; |
|
| 82 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 83 | - } |
|
| 84 | - $result = $stmt->fetch(\PDO::FETCH_COLUMN); |
|
| 85 | - if (false === $result) { |
|
| 86 | - $errorInfo = $db->errorInfo(); |
|
| 87 | - $errorMsg = $errorInfo[2]; |
|
| 88 | - throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 89 | - } |
|
| 90 | - return intval($result); |
|
| 91 | - } |
|
| 79 | + if (!$stmt = $db->query($sql)) { |
|
| 80 | + $errorInfo = $db->errorInfo(); |
|
| 81 | + $errorMsg = $errorInfo[2]; |
|
| 82 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 83 | + } |
|
| 84 | + $result = $stmt->fetch(\PDO::FETCH_COLUMN); |
|
| 85 | + if (false === $result) { |
|
| 86 | + $errorInfo = $db->errorInfo(); |
|
| 87 | + $errorMsg = $errorInfo[2]; |
|
| 88 | + throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
|
| 89 | + } |
|
| 90 | + return intval($result); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Queries each token present in the Tokenizer |
|
| 95 | - * and returns SearchResult objects for the found |
|
| 96 | - * documents |
|
| 97 | - * @return array |
|
| 98 | - */ |
|
| 99 | - private function queryTokens() |
|
| 100 | - { |
|
| 101 | - $tokens = $this->getTokens(); |
|
| 93 | + /** |
|
| 94 | + * Queries each token present in the Tokenizer |
|
| 95 | + * and returns SearchResult objects for the found |
|
| 96 | + * documents |
|
| 97 | + * @return array |
|
| 98 | + */ |
|
| 99 | + private function queryTokens() |
|
| 100 | + { |
|
| 101 | + $tokens = $this->getTokens(); |
|
| 102 | 102 | |
| 103 | - $queryNorm = $this->getQueryNorm($tokens); |
|
| 104 | - $results = array(); |
|
| 105 | - foreach ($tokens as $token) { |
|
| 106 | - $results[$token] = $this->getResultsForToken($token, $queryNorm); |
|
| 107 | - } |
|
| 108 | - return $results; |
|
| 109 | - } |
|
| 103 | + $queryNorm = $this->getQueryNorm($tokens); |
|
| 104 | + $results = array(); |
|
| 105 | + foreach ($tokens as $token) { |
|
| 106 | + $results[$token] = $this->getResultsForToken($token, $queryNorm); |
|
| 107 | + } |
|
| 108 | + return $results; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Applies the Filter objects in the the filter array to the |
|
| 113 | - * tokens in the Tokenizer |
|
| 114 | - * @param $tokens |
|
| 115 | - * |
|
| 116 | - * @return mixed |
|
| 117 | - */ |
|
| 118 | - protected function applyFilters($tokens) |
|
| 119 | - { |
|
| 120 | - foreach ($this->filters as $filterName) { |
|
| 121 | - $filterClassName = '\CloudControl\Cms\search\filters\\' . $filterName; |
|
| 122 | - $filter = new $filterClassName($tokens); |
|
| 123 | - $tokens = $filter->getFilterResults(); |
|
| 124 | - } |
|
| 125 | - return $tokens; |
|
| 126 | - } |
|
| 111 | + /** |
|
| 112 | + * Applies the Filter objects in the the filter array to the |
|
| 113 | + * tokens in the Tokenizer |
|
| 114 | + * @param $tokens |
|
| 115 | + * |
|
| 116 | + * @return mixed |
|
| 117 | + */ |
|
| 118 | + protected function applyFilters($tokens) |
|
| 119 | + { |
|
| 120 | + foreach ($this->filters as $filterName) { |
|
| 121 | + $filterClassName = '\CloudControl\Cms\search\filters\\' . $filterName; |
|
| 122 | + $filter = new $filterClassName($tokens); |
|
| 123 | + $tokens = $filter->getFilterResults(); |
|
| 124 | + } |
|
| 125 | + return $tokens; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Queries the search index for a given token |
|
| 130 | - * and the query norm. |
|
| 131 | - * @param $token |
|
| 132 | - * @param $queryNorm |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - * @throws \Exception |
|
| 136 | - */ |
|
| 137 | - public function getResultsForToken($token, $queryNorm) { |
|
| 138 | - $db = $this->getSearchDbHandle(); |
|
| 139 | - $sql = ' |
|
| 128 | + /** |
|
| 129 | + * Queries the search index for a given token |
|
| 130 | + * and the query norm. |
|
| 131 | + * @param $token |
|
| 132 | + * @param $queryNorm |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + * @throws \Exception |
|
| 136 | + */ |
|
| 137 | + public function getResultsForToken($token, $queryNorm) { |
|
| 138 | + $db = $this->getSearchDbHandle(); |
|
| 139 | + $sql = ' |
|
| 140 | 140 | SELECT (:queryNorm * |
| 141 | 141 | (SUM(term_frequency.frequency) --TF |
| 142 | 142 | * inverse_document_frequency.inverseDocumentFrequency -- IDF |
@@ -154,113 +154,113 @@ discard block |
||
| 154 | 154 | GROUP BY term_frequency.documentPath, term_frequency.term |
| 155 | 155 | ORDER BY score DESC |
| 156 | 156 | '; |
| 157 | - if(!$stmt = $db->prepare($sql)) { |
|
| 158 | - throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 159 | - } |
|
| 160 | - $stmt->bindValue(':query', $token); |
|
| 161 | - $stmt->bindValue(':queryNorm', $queryNorm); |
|
| 162 | - if (!$stmt->execute()) { |
|
| 163 | - throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 164 | - } |
|
| 165 | - return $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\search\results\SearchResult'); |
|
| 166 | - } |
|
| 157 | + if(!$stmt = $db->prepare($sql)) { |
|
| 158 | + throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 159 | + } |
|
| 160 | + $stmt->bindValue(':query', $token); |
|
| 161 | + $stmt->bindValue(':queryNorm', $queryNorm); |
|
| 162 | + if (!$stmt->execute()) { |
|
| 163 | + throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 164 | + } |
|
| 165 | + return $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\search\results\SearchResult'); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @param $resultsPerTokens |
|
| 170 | - * |
|
| 171 | - * @return array |
|
| 172 | - */ |
|
| 173 | - private function flattenResults($resultsPerTokens) |
|
| 174 | - { |
|
| 175 | - $finalResults = array(); |
|
| 176 | - foreach ($resultsPerTokens as $token => $resultPerToken) { |
|
| 177 | - foreach ($resultPerToken as $result) { |
|
| 178 | - if (isset($finalResults[$result->documentPath])) { |
|
| 179 | - $finalResults[$result->documentPath]->score += $result->score; |
|
| 180 | - $finalResults[$result->documentPath]->matchingTokens[] = $token; |
|
| 181 | - } else { |
|
| 182 | - $resultObj = new SearchResult(); |
|
| 183 | - $resultObj->documentPath = $result->documentPath; |
|
| 184 | - $resultObj->matchingTokens = array($token); |
|
| 185 | - $resultObj->score = floatval($result->score); |
|
| 186 | - $resultObj->setStorage($this->storage); |
|
| 187 | - $finalResults[$result->documentPath] = $resultObj; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - return $finalResults; |
|
| 192 | - } |
|
| 168 | + /** |
|
| 169 | + * @param $resultsPerTokens |
|
| 170 | + * |
|
| 171 | + * @return array |
|
| 172 | + */ |
|
| 173 | + private function flattenResults($resultsPerTokens) |
|
| 174 | + { |
|
| 175 | + $finalResults = array(); |
|
| 176 | + foreach ($resultsPerTokens as $token => $resultPerToken) { |
|
| 177 | + foreach ($resultPerToken as $result) { |
|
| 178 | + if (isset($finalResults[$result->documentPath])) { |
|
| 179 | + $finalResults[$result->documentPath]->score += $result->score; |
|
| 180 | + $finalResults[$result->documentPath]->matchingTokens[] = $token; |
|
| 181 | + } else { |
|
| 182 | + $resultObj = new SearchResult(); |
|
| 183 | + $resultObj->documentPath = $result->documentPath; |
|
| 184 | + $resultObj->matchingTokens = array($token); |
|
| 185 | + $resultObj->score = floatval($result->score); |
|
| 186 | + $resultObj->setStorage($this->storage); |
|
| 187 | + $finalResults[$result->documentPath] = $resultObj; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + return $finalResults; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - private function scoreCompare($a, $b) { |
|
| 195 | - if ($a->score == $b->score) { |
|
| 196 | - return 0; |
|
| 197 | - } |
|
| 198 | - return ($a->score > $b->score) ? -1 : 1; |
|
| 199 | - } |
|
| 194 | + private function scoreCompare($a, $b) { |
|
| 195 | + if ($a->score == $b->score) { |
|
| 196 | + return 0; |
|
| 197 | + } |
|
| 198 | + return ($a->score > $b->score) ? -1 : 1; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * Calculates the query norm for all tokens in the Tokenizer |
|
| 203 | - * @param $tokens |
|
| 204 | - * |
|
| 205 | - * @return int |
|
| 206 | - * @throws \Exception |
|
| 207 | - */ |
|
| 208 | - private function getQueryNorm($tokens) |
|
| 209 | - { |
|
| 210 | - $db = $this->getSearchDbHandle(); |
|
| 211 | - $db->sqliteCreateFunction('sqrt', 'sqrt', 1); |
|
| 212 | - foreach ($tokens as $key => $token) { |
|
| 213 | - $tokens[$key] = $db->quote($token); |
|
| 214 | - } |
|
| 215 | - $terms = implode(',', $tokens); |
|
| 216 | - $sql = ' |
|
| 201 | + /** |
|
| 202 | + * Calculates the query norm for all tokens in the Tokenizer |
|
| 203 | + * @param $tokens |
|
| 204 | + * |
|
| 205 | + * @return int |
|
| 206 | + * @throws \Exception |
|
| 207 | + */ |
|
| 208 | + private function getQueryNorm($tokens) |
|
| 209 | + { |
|
| 210 | + $db = $this->getSearchDbHandle(); |
|
| 211 | + $db->sqliteCreateFunction('sqrt', 'sqrt', 1); |
|
| 212 | + foreach ($tokens as $key => $token) { |
|
| 213 | + $tokens[$key] = $db->quote($token); |
|
| 214 | + } |
|
| 215 | + $terms = implode(',', $tokens); |
|
| 216 | + $sql = ' |
|
| 217 | 217 | SELECT (1 / sqrt(SUM(inverseDocumentFrequency))) as queryNorm |
| 218 | 218 | FROM inverse_document_frequency |
| 219 | 219 | WHERE term IN (' . $terms . ') |
| 220 | 220 | '; |
| 221 | - if(!$stmt = $db->prepare($sql)) { |
|
| 222 | - throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 223 | - } |
|
| 224 | - if (!$stmt->execute()) { |
|
| 225 | - throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 226 | - } |
|
| 227 | - $result = $stmt->fetch(\PDO::FETCH_OBJ); |
|
| 228 | - return $result->queryNorm == null ? 1 : $result->queryNorm; |
|
| 229 | - } |
|
| 221 | + if(!$stmt = $db->prepare($sql)) { |
|
| 222 | + throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 223 | + } |
|
| 224 | + if (!$stmt->execute()) { |
|
| 225 | + throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 226 | + } |
|
| 227 | + $result = $stmt->fetch(\PDO::FETCH_OBJ); |
|
| 228 | + return $result->queryNorm == null ? 1 : $result->queryNorm; |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Applies query coordination to all results |
|
| 233 | - * @param $flatResults |
|
| 234 | - * |
|
| 235 | - * @return mixed |
|
| 236 | - */ |
|
| 237 | - private function applyQueryCoordination($flatResults) |
|
| 238 | - { |
|
| 239 | - $tokenVector = $this->tokenizer->getTokenVector(); |
|
| 240 | - $tokens = array_keys($tokenVector); |
|
| 241 | - $tokenCount = count($tokens); |
|
| 242 | - foreach ($flatResults as $key => $result) { |
|
| 243 | - $matchCount = count($result->matchingTokens); |
|
| 244 | - $result->score = ($matchCount / $tokenCount) * $result->score; |
|
| 245 | - $flatResults[$key] = $result; |
|
| 246 | - } |
|
| 247 | - return $flatResults; |
|
| 248 | - } |
|
| 231 | + /** |
|
| 232 | + * Applies query coordination to all results |
|
| 233 | + * @param $flatResults |
|
| 234 | + * |
|
| 235 | + * @return mixed |
|
| 236 | + */ |
|
| 237 | + private function applyQueryCoordination($flatResults) |
|
| 238 | + { |
|
| 239 | + $tokenVector = $this->tokenizer->getTokenVector(); |
|
| 240 | + $tokens = array_keys($tokenVector); |
|
| 241 | + $tokenCount = count($tokens); |
|
| 242 | + foreach ($flatResults as $key => $result) { |
|
| 243 | + $matchCount = count($result->matchingTokens); |
|
| 244 | + $result->score = ($matchCount / $tokenCount) * $result->score; |
|
| 245 | + $flatResults[$key] = $result; |
|
| 246 | + } |
|
| 247 | + return $flatResults; |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Uses the levenshtein algorithm to determine the term that is |
|
| 252 | - * closest to the token that was input for the search |
|
| 253 | - * @return array |
|
| 254 | - * @throws \Exception |
|
| 255 | - */ |
|
| 256 | - private function getSearchSuggestions() |
|
| 257 | - { |
|
| 258 | - $tokens = $this->getTokens(); |
|
| 259 | - $allResults = array(); |
|
| 260 | - foreach ($tokens as $token) { |
|
| 261 | - $db = $this->getSearchDbHandle(); |
|
| 262 | - $db->sqliteCreateFunction('levenshtein', 'levenshtein', 2); |
|
| 263 | - $sql = ' |
|
| 250 | + /** |
|
| 251 | + * Uses the levenshtein algorithm to determine the term that is |
|
| 252 | + * closest to the token that was input for the search |
|
| 253 | + * @return array |
|
| 254 | + * @throws \Exception |
|
| 255 | + */ |
|
| 256 | + private function getSearchSuggestions() |
|
| 257 | + { |
|
| 258 | + $tokens = $this->getTokens(); |
|
| 259 | + $allResults = array(); |
|
| 260 | + foreach ($tokens as $token) { |
|
| 261 | + $db = $this->getSearchDbHandle(); |
|
| 262 | + $db->sqliteCreateFunction('levenshtein', 'levenshtein', 2); |
|
| 263 | + $sql = ' |
|
| 264 | 264 | SELECT * |
| 265 | 265 | FROM ( |
| 266 | 266 | SELECT :token as original, term, levenshtein(term, :token) as editDistance |
@@ -270,35 +270,35 @@ discard block |
||
| 270 | 270 | ) |
| 271 | 271 | WHERE editDistance > 0 |
| 272 | 272 | '; |
| 273 | - $stmt = $db->prepare($sql); |
|
| 274 | - if ($stmt === false) { |
|
| 275 | - throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 276 | - } |
|
| 277 | - $stmt->bindValue(':token', $token); |
|
| 278 | - if (($stmt === false) | (!$stmt->execute())) { |
|
| 279 | - throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 280 | - } |
|
| 281 | - $result = $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\search\results\SearchSuggestion'); |
|
| 282 | - $allResults = array_merge($result, $allResults); |
|
| 283 | - } |
|
| 284 | - return $allResults; |
|
| 285 | - } |
|
| 273 | + $stmt = $db->prepare($sql); |
|
| 274 | + if ($stmt === false) { |
|
| 275 | + throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 276 | + } |
|
| 277 | + $stmt->bindValue(':token', $token); |
|
| 278 | + if (($stmt === false) | (!$stmt->execute())) { |
|
| 279 | + throw new \Exception('SQLite exception: <pre>' . print_r($db->errorInfo(), true) . '</pre> for SQL:<pre>' . $sql . '</pre>'); |
|
| 280 | + } |
|
| 281 | + $result = $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\search\results\SearchSuggestion'); |
|
| 282 | + $allResults = array_merge($result, $allResults); |
|
| 283 | + } |
|
| 284 | + return $allResults; |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * Retrieves all tokens from the tokenizer |
|
| 289 | - * @return array |
|
| 290 | - */ |
|
| 291 | - private function getTokens() |
|
| 292 | - { |
|
| 293 | - $tokenVector = array( |
|
| 294 | - 'query' => array(), |
|
| 295 | - ); |
|
| 296 | - $tokenVector['query'] = $this->tokenizer->getTokenVector(); |
|
| 297 | - $tokens = $this->applyFilters($tokenVector); |
|
| 298 | - if (!empty($tokens)) { |
|
| 299 | - $tokens = array_keys($tokens['query']); |
|
| 300 | - } |
|
| 287 | + /** |
|
| 288 | + * Retrieves all tokens from the tokenizer |
|
| 289 | + * @return array |
|
| 290 | + */ |
|
| 291 | + private function getTokens() |
|
| 292 | + { |
|
| 293 | + $tokenVector = array( |
|
| 294 | + 'query' => array(), |
|
| 295 | + ); |
|
| 296 | + $tokenVector['query'] = $this->tokenizer->getTokenVector(); |
|
| 297 | + $tokens = $this->applyFilters($tokenVector); |
|
| 298 | + if (!empty($tokens)) { |
|
| 299 | + $tokens = array_keys($tokens['query']); |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - return $tokens; |
|
| 303 | - } |
|
| 302 | + return $tokens; |
|
| 303 | + } |
|
| 304 | 304 | } |
| 305 | 305 | \ No newline at end of file |
@@ -13,47 +13,47 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Tokenizer |
| 15 | 15 | { |
| 16 | - protected $inputString; |
|
| 17 | - protected $tokenVector = array(); |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Tokenizer constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string Should preferably be parsed wit \CloudControl\Cms\search\CharacterFilter |
|
| 23 | - * @see \CloudControl\Cms\search\CharacterFilter |
|
| 24 | - */ |
|
| 25 | - public function __construct($string) |
|
| 26 | - { |
|
| 27 | - $this->inputString = $string; |
|
| 28 | - $this->tokenize(); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - protected function tokenize() |
|
| 32 | - { |
|
| 33 | - $tokens = explode(' ', $this->inputString); |
|
| 34 | - foreach ($tokens as $token) { |
|
| 35 | - $this->addTokenToVector($token); |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - protected function addTokenToVector($token) |
|
| 40 | - { |
|
| 41 | - if (!empty($token)) { |
|
| 42 | - if (isset($this->tokenVector[$token])) { |
|
| 43 | - $this->tokenVector[$token] += 1; |
|
| 44 | - } else { |
|
| 45 | - $this->tokenVector[$token] = 1; |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - public function getTokenVector() |
|
| 54 | - { |
|
| 55 | - return $this->tokenVector; |
|
| 56 | - } |
|
| 16 | + protected $inputString; |
|
| 17 | + protected $tokenVector = array(); |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Tokenizer constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string Should preferably be parsed wit \CloudControl\Cms\search\CharacterFilter |
|
| 23 | + * @see \CloudControl\Cms\search\CharacterFilter |
|
| 24 | + */ |
|
| 25 | + public function __construct($string) |
|
| 26 | + { |
|
| 27 | + $this->inputString = $string; |
|
| 28 | + $this->tokenize(); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + protected function tokenize() |
|
| 32 | + { |
|
| 33 | + $tokens = explode(' ', $this->inputString); |
|
| 34 | + foreach ($tokens as $token) { |
|
| 35 | + $this->addTokenToVector($token); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + protected function addTokenToVector($token) |
|
| 40 | + { |
|
| 41 | + if (!empty($token)) { |
|
| 42 | + if (isset($this->tokenVector[$token])) { |
|
| 43 | + $this->tokenVector[$token] += 1; |
|
| 44 | + } else { |
|
| 45 | + $this->tokenVector[$token] = 1; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + public function getTokenVector() |
|
| 54 | + { |
|
| 55 | + return $this->tokenVector; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | \ No newline at end of file |