Changeset 6883
- Timestamp:
- 01/02/08 10:43:33 (1 year ago)
- Files:
-
- plugins/sfLucenePlugin/branches/1.1/CHANGELOG (modified) (1 diff)
- plugins/sfLucenePlugin/branches/1.1/lib/filter/sfLuceneHighlightFilter.class.php (modified) (8 diffs)
- plugins/sfLucenePlugin/branches/1.1/lib/helper/sfLuceneHelper.php (modified) (1 diff)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/keyword (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/keyword/sfLuceneHighlighterKeyword.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/keyword/sfLuceneHighlighterKeywordNamed.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/keyword/sfLuceneHighlighterKeywordNamedInsensitive.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/keyword/sfLuceneHighlighterKeywordZend.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/keyword/sfLuceneHighlighterToken.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/marker (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/marker/sfLuceneHighlighterMarker.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/marker/sfLuceneHighlighterMarkerDry.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/marker/sfLuceneHighlighterMarkerHarness.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/marker/sfLuceneHighlighterMarkerSprint.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/marker/sfLuceneHighlighterMarkerUppercase.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighter.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighterHTML.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighterHTMLPart.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighterString.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighterXHTML.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighterXHTMLPart.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/highlighter/sfLuceneHighlighterXML.class.php (added)
- plugins/sfLucenePlugin/branches/1.1/lib/util/sfLuceneHighlighter.class.php (deleted)
- plugins/sfLucenePlugin/branches/1.1/test/unit/filter/sfLuceneHighlightFilterTest.php (modified) (6 diffs)
- plugins/sfLucenePlugin/branches/1.1/test/unit/helper/sfLuceneHelperTest.php (modified) (1 diff)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/keyword (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/keyword/sfLuceneHighlighterKeywordNamedInsensitiveTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/keyword/sfLuceneHighlighterKeywordNamedTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/keyword/sfLuceneHighlighterKeywordZendTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/keyword/sfLuceneHighlighterTokenTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/marker (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/marker/sfLuceneHighlighterMarkerDryTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/marker/sfLuceneHighlighterMarkerHarnessTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/marker/sfLuceneHighlighterMarkerSprintTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/marker/sfLuceneHighlighterMarkerUppercaseTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/sfLuceneHighlighterHTMLPartTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/sfLuceneHighlighterHTMLTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/sfLuceneHighlighterStringTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/sfLuceneHighlighterXHTMLPartTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/sfLuceneHighlighterXHTMLTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/highlighter/sfLuceneHighlighterXMLTest.php (added)
- plugins/sfLucenePlugin/branches/1.1/test/unit/util/sfLuceneHighlighterTest.php (deleted)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfLucenePlugin/branches/1.1/CHANGELOG
r6808 r6883 19 19 * Added own sfEventDispatcher to each sfLucene instance 20 20 * BC: sfLuceneCriteria constructor now requires sfLucene instance 21 * Refactored highlighting system 21 22 22 23 Version 0.1.1 Beta plugins/sfLucenePlugin/branches/1.1/lib/filter/sfLuceneHighlightFilter.class.php
r6816 r6883 100 100 try 101 101 { 102 if (!$this->highlight()) 103 { 104 // highlighting did not occur, so remove notice 105 $this->removeNotice(); 106 } 102 $this->highlight(); 107 103 } 108 104 catch (Exception $e) … … 122 118 { 123 119 $terms = $this->getContext()->getRequest()->getParameter($this->getParameter('highlight_qs')); 124 $terms = $this->prepareTerms($terms);125 120 126 121 // attempt to highlight from sfLucene 127 if (count($terms)) 128 { 122 if ($terms) 123 { 124 $terms = $this->prepareTerms($terms); 125 126 $this->doHighlight($terms); 129 127 $this->addNotice($terms); 130 128 $this->addCss(); 131 $this->doHighlight($terms);132 129 133 130 return true; … … 153 150 $terms = $this->prepareTerms($matches[1]); 154 151 152 $this->doHighlight($terms); 155 153 $this->addNotice($terms, $value['name']); 156 154 $this->addCss(); 157 $this->doHighlight($terms);158 155 159 156 // stop looking for referers now. … … 165 162 } 166 163 167 // we failed to do anything, so return false164 $this->removeNotice(); 168 165 return false; 169 166 } … … 177 174 178 175 // configure highlighter 179 $lighter = new sfLuceneHighlighter ($content);176 $lighter = new sfLuceneHighlighterXHTML($content); 180 177 $lighter->addKeywords($terms); 181 $lighter->addHighlighters($this->getParameter('highlight_strings')); 182 $lighter->hasBody(true); 183 184 $this->getContext()->getResponse()->setContent($lighter->highlight()); 178 179 $this->getContext()->getResponse()->setContent($lighter->highlight()->export()); 185 180 } 186 181 … … 203 198 $this->getContext()->getResponse()->setContent(substr($content, 0, $pos) . $css . substr($content, $pos)); 204 199 } 205 }206 207 /**208 * Prepares terms by exploding them out209 */210 protected function prepareTerms($terms)211 {212 $terms = preg_split('/\W+/', trim($terms), -1, PREG_SPLIT_NO_EMPTY);213 214 $terms = array_unique($terms);215 216 return $terms;217 200 } 218 201 … … 234 217 $content = $this->getContext()->getResponse()->getContent(); 235 218 236 $term_string = implode($terms, ', '); 219 $term_string = ''; 220 221 foreach ($terms as $term) 222 { 223 $term_string .= $term->getHighlighter()->highlight($term->getName()) . ', '; 224 } 225 226 $term_string = substr($term_string, 0, -2); 237 227 238 228 $route = $route = $this->getContext()->getRouting()->getCurrentInternalUri(); … … 256 246 } 257 247 248 protected function prepareTerms($terms) 249 { 250 $highlighters = sfLuceneHighlighterMarkerSprint::generate($this->getParameter('highlight_strings')); 251 252 return sfLuceneHighlighterKeywordNamedInsensitive::explode($highlighters, mb_strtolower($terms)); 253 } 254 258 255 /** 259 256 * Helper function to do translations plugins/sfLucenePlugin/branches/1.1/lib/helper/sfLuceneHelper.php
r6652 r6883 30 30 } 31 31 32 function highlight_result_text($text, $query, $size = 200, $ highlighter= '<strong class="highlight">%s</strong>')32 function highlight_result_text($text, $query, $size = 200, $sprint = '<strong class="highlight">%s</strong>') 33 33 { 34 $h = new sfLuceneHighlighter($text); 35 $h->addKeywordSlug($query); 36 $h->addHighlighter($highlighter); 37 $h->hasBody(false); 38 $h->densityCrop($size); 39 return $h->highlight(); 34 $highlighter = new sfLuceneHighlighterString($text); 35 36 $marker = new sfLuceneHighlighterMarkerSprint($sprint); 37 $harness = new sfLuceneHighlighterMarkerHarness(array($marker)); 38 39 $keywords = sfLuceneHighlighterKeywordNamedInsensitive::explode($harness, $query); 40 41 $highlighter->addKeywords($keywords); 42 $highlighter->strip()->crop($size); 43 44 return $highlighter->highlight()->export(); 40 45 } 41 46 42 function highlight_keywords($text, $keywords, $ highlighter= '<strong class="highlight">%s</strong>')47 function highlight_keywords($text, $keywords, $sprint = '<strong class="highlight">%s</strong>') 43 48 { 44 $h = new sfLuceneHighlighter($text); 45 $h->addKeywordSlug($keywords); 46 $h->addHighlighter($highlighter); 47 $h->hasBody(false); 49 $highlighter = new sfLuceneHighlighterXHTMLPart($text); 48 50 49 return $h->highlight(); 51 $marker = new sfLuceneHighlighterMarkerSprint($sprint); 52 $harness = new sfLuceneHighlighterMarkerHarness(array($marker)); 53 54 $keywords = sfLuceneHighlighterKeywordNamedInsensitive::explode($harness, $keywords); 55 56 $highlighter->addKeywords($keywords); 57 58 return $highlighter->highlight()->export(); 50 59 } 51 60 plugins/sfLucenePlugin/branches/1.1/test/unit/filter/sfLuceneHighlightFilterTest.php
r6735 r6883 17 17 require dirname(__FILE__) . '/../../bootstrap/unit.php'; 18 18 19 $t = new lime_test( 21, new lime_output_color());19 $t = new lime_test(17, new lime_output_color()); 20 20 21 21 $chain = new sfFilterChain(); … … 44 44 $request->setParameter('h', 'test'); 45 45 46 $response->setContent('Hello'); 47 try { 48 $highlight->execute($chain); 49 $t->fail('highlighter rejects content without a body'); 50 } catch (sfException $e) { 51 $t->pass('highlighter rejects content without a body'); 52 } 53 54 $response->setContent('<body>Hello'); 55 try { 56 $highlight->execute($chain); 57 $t->fail('highlighter rejects content without a body ending tag'); 58 } catch (sfException $e) { 59 $t->pass('highlighter rejects content without a body ending tag'); 60 } 61 62 $response->setContent('Hello</body>'); 63 try { 64 $highlight->execute($chain); 65 $t->fail('highlighter rejects content without a body starting tag'); 66 } catch (sfException $e) { 67 $t->pass('highlighter rejects content without a body starting tag'); 68 } 69 70 $response->setContent('<body>Hello</body>'); 46 $response->setContent('<html><body>Hello</body></html>'); 71 47 try { 72 48 $highlight->execute($chain); … … 76 52 } 77 53 78 $response->setContent('<body>2 > 1</body>'); 79 try { 80 $highlight->execute($chain); 81 $t->fail('highlighter rejects content with a carat mismatch'); 82 } catch (sfException $e) { 83 $t->pass('highlighter rejects content with a carat mismatch'); 84 } 85 86 $response->setContent('<body>I am <b>cool</b>!</body>'); 54 $response->setContent('<html><body>I am <b>cool</b>!</body></html>'); 87 55 try { 88 56 $highlight->execute($chain); … … 94 62 $t->diag('testing highlighting'); 95 63 96 $response->setContent('< body>highlight the keyword</body>');64 $response->setContent('<html><body>highlight the keyword</body></html>'); 97 65 $request->setParameter('h', 'keyword'); 98 66 $highlight->execute($chain); 99 $t->is($response->getContent(), '<body>highlight the <highlighted>keyword</highlighted></body>', 'highlighter highlights a single keyword');67 $t->is($response->getContent(), "<?xml version=\"1.0\"?>\n<html><body>highlight the <highlighted>keyword</highlighted></body></html>\n", 'highlighter highlights a single keyword'); 100 68 101 $response->setContent('< body>highlight the keyword</body>');102 $request->setParameter('h', 'highlight keyword');69 $response->setContent('<html><body>highlight the keyword yay!</body></html>'); 70 $request->setParameter('h', 'highlight KEYWORD'); 103 71 $highlight->execute($chain); 104 $t->is($response->getContent(), '<body><highlighted>highlight</highlighted> the <highlighted2>keyword</highlighted2></body>', 'highlighter highlights multiple keywords');72 $t->is($response->getContent(), "<?xml version=\"1.0\"?>\n<html><body><highlighted>highlight</highlighted> the <highlighted2>keyword</highlighted2> yay!</body></html>\n", 'highlighter highlights multiple keywords'); 105 73 106 $response->setContent('< body>~notice~ keyword</body>');74 $response->setContent('<html><body>~notice~ keyword</body></html>'); 107 75 $request->setParameter('h', 'keyword'); 108 76 $highlight->execute($chain); 109 77 $t->like($response->getContent(), '#<body><keywords><highlighted>keyword</highlighted></keywords><remove>~remove~</remove>#', 'highlighter adds notice string'); 110 78 111 $response->setContent('<h ead></head><body>keyword</body>');79 $response->setContent('<html><head><title>foobar</title></head><body>keyword</body></html>'); 112 80 $highlight->execute($chain); 113 81 $t->like($response->getContent(), '#<link .*?href=".*?/search\.css".*?/>\n</head>#', 'highlighter adds search stylesheet'); 114 82 115 $response->setContent('<h ead></head><body>~notice~ google search test</body>');83 $response->setContent('<html><head><title>foobar</title></head><body>~notice~ google search test</body></html>'); 116 84 $request->getParameterHolder()->remove('h'); 117 85 $_SERVER['HTTP_REFERER'] = 'http://www.google.com/search?num=50&hl=en&safe=off&q=google&btnG=Search'; … … 119 87 120 88 $t->like($response->getContent(), '#<highlighted>google</highlighted> search test#', 'highlighter highlights results from Google'); 121 $t->like($response->getContent(), '#<from> <highlighted>Google</highlighted></from><keywords><highlighted>google</highlighted></keywords><remove>~remove~</remove>#', 'highlighter adds correct notice for results from Google');89 $t->like($response->getContent(), '#<from>Google</from><keywords><highlighted>google</highlighted></keywords><remove>~remove~</remove>#', 'highlighter adds correct notice for results from Google'); 122 90 $t->like($response->getContent(), '#<link .*?href=".*?/search\.css".*?/>\n</head>#', 'highlighter adds search stylesheet for results from Google'); 123 91 … … 165 133 configure_i18n(); 166 134 167 $response->setContent('< body>highlight the keyword</body>');135 $response->setContent('<html><body>highlight the keyword</body></html>'); 168 136 $request->setParameter('h', 'keyword'); 169 137 $highlight->execute($chain); 170 $t->is($response->getContent(), '<body>highlight the <highlighted>keyword</highlighted></body>', 'highlighter highlights a single keyword with i18n'); 138 139 $t->is($response->getContent(), "<?xml version=\"1.0\"?>\n<html><body>highlight the <highlighted>keyword</highlighted></body></html>\n", 'highlighter highlights a single keyword with i18n'); plugins/sfLucenePlugin/branches/1.1/test/unit/helper/sfLuceneHelperTest.php
r6652 r6883 76 76 $t->is(highlight_result_text('Hello. This is a pretty <em class="thing">awesome</em> thing to be talking about.', 'thing talking'), 'Hello. This is a pretty awesome <strong class="highlight">thing</strong> to be <strong class="highlight">talking</strong> about.', 'highlight_result_text() highlights text and strips out HTML'); 77 77 78 $t->is(highlight_result_text('Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. This is a pretty <em class="thing">awesome</em> thing to be talking about. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. ', 'thing talking', 50), '... This is a pretty awesome <strong class="highlight">thing</strong> to be <strong class="highlight">talking</strong> about....', 'highlight_result_text() highlights and truncates text');78 $t->is(highlight_result_text('Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. This is a pretty <em class="thing">awesome</em> thing to be talking about. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. Foo bar. ', 'thing talking', 50), '...is is a pretty awesome <strong class="highlight">thing</strong> to be <strong class="highlight">talking</strong> about....', 'highlight_result_text() highlights and truncates text'); 79 79 80 80 $t->is(highlight_keywords('Hello. This is a pretty <em class="thing">awesome</em> thing to be talking about.', 'thing talking'), 'Hello. This is a pretty <em class="thing">awesome</em> <strong class="highlight">thing</strong> to be <strong class="highlight">talking</strong> about.', 'highlight_kewyords() highlights text');