Changeset 9031
- Timestamp:
- 05/18/08 07:57:37 (5 years ago)
- Files:
-
- plugins/sfHighlightPlugin/trunk/README (modified) (1 diff)
- plugins/sfHighlightPlugin/trunk/XMLCatalog.tar.gz (added)
- plugins/sfHighlightPlugin/trunk/lib/highlight/xfHighlighter.class.php (modified) (2 diffs)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReader.interface.php (modified) (1 diff)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderAggregate.interface.php (added)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderDOM.class.php (modified) (7 diffs)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderInterface.interface.php (added)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderString.class.php (modified) (1 diff)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderStringable.interface.php (added)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderXHTML.class.php (added)
- plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderXML.class.php (added)
- plugins/sfHighlightPlugin/trunk/lib/search/xfHighlightRetortFilterCallback.class.php (modified) (1 diff)
- plugins/sfHighlightPlugin/trunk/lib/util/xfHighlightFilter.class.php (added)
- plugins/sfHighlightPlugin/trunk/test/unit/highlight/xfHighlighterTest.php (modified) (2 diffs)
- plugins/sfHighlightPlugin/trunk/test/unit/reader/xfHighlightReaderDOMTest.php (modified) (8 diffs)
- plugins/sfHighlightPlugin/trunk/test/unit/reader/xfHighlightReaderStringTest.php (modified) (2 diffs)
- plugins/sfHighlightPlugin/trunk/test/unit/reader/xfHighlightReaderXHTMLTest.php (added)
- plugins/sfHighlightPlugin/trunk/test/unit/reader/xfHighlightReaderXMLTest.php (added)
- plugins/sfHighlightPlugin/trunk/test/unit/search/xfHighlightRetortFilterCallbackTest.php (modified) (1 diff)
- plugins/sfHighlightPlugin/trunk/test/unit/util (added)
- plugins/sfHighlightPlugin/trunk/test/unit/util/xfHighlightFilterTest.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfHighlightPlugin/trunk/README
r8890 r9031 32 32 $h->addKeyword(new xfHighlightKeyword(new xfHighlightTokenizerSimple(array('baz')), new xfHighlightMarkerSprint('[%s]'))); 33 33 34 // nowhighlight a DOM document using the highlighter34 // highlight a DOM document using the highlighter 35 35 $dom = new DOMDocument; 36 36 $dom->loadXml($xml); 37 $highlightedDom = $h->highlight(new xfHighlightReaderDOM($dom)) ;37 $highlightedDom = $h->highlight(new xfHighlightReaderDOM($dom))->getDocument(); 38 38 39 // now highlight a string with the same configuration 39 // highlight a XHTML string 40 $highlightedXhtml = $h->highlight(new xfHighlightReaderXHTML($xhtml))->getString(); 41 42 // highlight a string with the same configuration 40 43 $string = 'Why don\'t you foobar when you can baz?'; 41 $highlightedstring = $h->highlight(new xfHighlightReaderString($string)) ;44 $highlightedstring = $h->highlight(new xfHighlightReaderString($string))->getString(); 42 45 }}} 43 46 44 47 Please refer to the unit tests until the documentation is complete. 48 49 = Improve XML Performance = 50 Highlighting XML documents may take longer than expected because PHP makes network calls to fetch the DTD from W3C's server. You can remove this bottleneck by caching the DTD through your XML catalog. Refer to tarball {{{ XMLCatalog.tar.gz }}} included in this package for more information. plugins/sfHighlightPlugin/trunk/lib/highlight/xfHighlighter.class.php
r8994 r9031 60 60 * Does the highlighting on the reader. 61 61 * 62 * @param xfHighlightReader $reader63 * @returns xfHighlightReader 62 * @param xfHighlightReaderInterface $inReader 63 * @returns xfHighlightReaderInterface 64 64 */ 65 public function highlight(xfHighlightReader $reader)65 public function highlight(xfHighlightReaderInterface $inReader) 66 66 { 67 $reader = $this->resolveReader($inReader); 68 67 69 $reader->rewind(); 68 70 … … 85 87 } 86 88 89 return $inReader; 90 } 91 92 /** 93 * Resolves the reader and its aggregates 94 * 95 * @param xfHighlightReaderInterface $reader 96 * @returns xfHighlightReader 97 */ 98 private function resolveReader(xfHighlightReaderInterface $reader) 99 { 100 if ($reader instanceof xfHighlightReaderAggregate) 101 { 102 $reader = $this->resolveReader($reader->getReader()); 103 } 104 87 105 return $reader; 88 106 } plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReader.interface.php
r8994 r9031 15 15 * @author Carl Vondrick 16 16 */ 17 interface xfHighlightReader 17 interface xfHighlightReader extends xfHighlightReaderInterface 18 18 { 19 19 /** plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderDOM.class.php
r8994 r9031 21 21 * The document 22 22 * 23 * @var DOM Document23 * @var DOMNode 24 24 */ 25 25 private $document; … … 49 49 50 50 /** 51 * Callbacks to determine if the node should be ignored 52 * 53 * @var array 54 */ 55 private $ignoreCallbacks = array(); 56 57 /** 51 58 * The current position in the text array 52 59 * … … 56 63 57 64 /** 65 * Boolean to indicate if reader has been initialized 66 * 67 * @var bool 68 */ 69 private $initialized = false; 70 71 /** 58 72 * Constructor to set the DOMNode 59 73 * 60 74 * @param DOMNode $document 61 75 */ 62 public function __construct(DOM Document$document)76 public function __construct(DOMNode $document) 63 77 { 64 78 $this->document = clone $document; 79 } 65 80 66 $this->buildTexts($this->document); 81 /** 82 * Initializes the reader 83 */ 84 public function initialize() 85 { 86 if (!$this->initialized) 87 { 88 $this->buildTexts($this->document); 89 90 $this->initialized = true; 91 } 92 } 93 94 /** 95 * Adds an ignore callback 96 * 97 * @param callable $callback 98 */ 99 public function registerIgnoreCallback($callback) 100 { 101 $this->ignoreCallbacks[] = $callback; 67 102 } 68 103 … … 70 105 * Gets the original document 71 106 * 72 * @returns DOM Document107 * @returns DOMNode 73 108 */ 74 109 public function getDocument() … … 87 122 { 88 123 return; 124 } 125 126 // stop building this node if callback returns true 127 foreach ($this->ignoreCallbacks as $callback) 128 { 129 if (call_user_func($callback, $node) === true) 130 { 131 return; 132 } 89 133 } 90 134 … … 108 152 public function rewind() 109 153 { 154 $this->initialize(); 155 110 156 $this->position = -1; 111 157 } … … 138 184 } 139 185 140 return $this->texts[$this->position]->textContent; 186 $response = $this->texts[$this->position]->textContent; 187 188 if (trim($response) == '') 189 { 190 return $this->next(); 191 } 192 193 return $response; 141 194 } 142 195 } plugins/sfHighlightPlugin/trunk/lib/reader/xfHighlightReaderString.class.php
r8994 r9031 81 81 * @returns string 82 82 */ 83 public function get Text()83 public function getString() 84 84 { 85 85 return $this->text; plugins/sfHighlightPlugin/trunk/lib/search/xfHighlightRetortFilterCallback.class.php
r8994 r9031 53 53 $h->addKeyword($keyword); 54 54 55 $response = $h->highlight(new xfHighlightReaderString($response))->get Text();55 $response = $h->highlight(new xfHighlightReaderString($response))->getString(); 56 56 } 57 57 plugins/sfHighlightPlugin/trunk/test/unit/highlight/xfHighlighterTest.php
r8994 r9031 17 17 require 'marker/xfHighlightMarkerUppercase.class.php'; 18 18 require 'marker/xfHighlightMarkerSprint.class.php'; 19 require 'reader/xfHighlightReaderInterface.interface.php'; 20 require 'reader/xfHighlightReaderAggregate.interface.php'; 19 21 require 'reader/xfHighlightReader.interface.php'; 20 22 require 'reader/xfHighlightReaderString.class.php'; 21 23 22 $t = new lime_test(3, new lime_output_color); 24 class MockReaderAggregate implements xfHighlightReaderAggregate 25 { 26 public $reader; 27 28 public function getReader() 29 { 30 if (!$this->reader) 31 { 32 $this->reader = new xfHighlightReaderString('foo is awesome'); 33 } 34 35 return $this->reader; 36 } 37 } 38 39 class MockReaderDoubleAggregate implements xfHighlightReaderAggregate 40 { 41 public $reader; 42 43 public function getReader() 44 { 45 if (!$this->reader) 46 { 47 $this->reader = new MockReaderAggregate; 48 } 49 50 return $this->reader; 51 } 52 } 53 54 $t = new lime_test(5, new lime_output_color); 23 55 24 56 $keywords = array( … … 33 65 34 66 $t->isa_ok($reader, 'xfHighlightReaderString', '->highlight() returns a xfHighlightReader'); 35 $t->is($reader->get Text(), 'FOO is better than BAR if only because of the small amount of [baz]', '->highlight() highlights the string according to the reader and keywords');67 $t->is($reader->getString(), 'FOO is better than BAR if only because of the small amount of [baz]', '->highlight() highlights the string according to the reader and keywords'); 36 68 37 $t->is($h->highlight(new xfHighlightReaderString('baz! bow before baz for i am baz'))->getText(), '[baz]! bow before [baz] for i am [baz]', '->highlight() works with changing string length'); 69 $t->is($h->highlight(new xfHighlightReaderString('baz! bow before baz for i am baz'))->getString(), '[baz]! bow before [baz] for i am [baz]', '->highlight() works with changing string length'); 70 71 $t->is($h->highlight(new MockReaderAggregate)->getReader()->getString(), 'FOO is awesome', '->highlight() accepts reader aggregates'); 72 $t->is($h->highlight(new MockReaderDoubleAggregate)->getReader()->getReader()->getString(), 'FOO is awesome', '->highlight() accepts double reader aggregates'); plugins/sfHighlightPlugin/trunk/test/unit/reader/xfHighlightReaderDOMTest.php
r8890 r9031 9 9 10 10 require dirname(__FILE__) . '/../../bootstrap/unit.php'; 11 require 'reader/xfHighlightReaderInterface.interface.php'; 11 12 require 'reader/xfHighlightReader.interface.php'; 12 13 require 'reader/xfHighlightReaderDOM.class.php'; … … 15 16 $t = new lime_test(9, new lime_output_color); 16 17 17 function getFullText($reader)18 function ignoreCallback(DOMNode $node) 18 19 { 19 do 20 { 21 $text = trim($reader->next()); 22 } while ($text === ''); 23 24 return $text; 20 return $node->nodeName == 'ignoreme'; 25 21 } 26 22 … … 37 33 Pie is bad for you! 38 34 </parent> 35 <ignoreme>foobar</ignoreme> 39 36 </root> 40 37 XML; … … 44 41 45 42 $reader = new xfHighlightReaderDOM($domdoc); 43 $reader->registerIgnoreCallback('ignoreCallback'); 44 $reader->rewind(); 46 45 47 46 $t->ok($reader->getDocument() == $domdoc && $reader->getDocument() !== $domdoc, '->__construct() clones the DOMDocument'); … … 57 56 foreach ($expected as $phrase) 58 57 { 59 $t->is( getFullText($reader), $phrase, '->next() returns node "' . $phrase . '"');58 $t->is(trim($reader->next()), $phrase, '->next() returns node "' . $phrase . '"'); 60 59 } 61 60 … … 64 63 65 64 $reader->rewind(); 66 $t->is( getFullText($reader), 'I love pie', '->rewind() starts the iterator over');65 $t->is($reader->next(), 'I love pie', '->rewind() starts the iterator over'); 67 66 68 67 $reader->replaceText(new xfHighlightToken('love', 2, 6), 'hate'); … … 82 81 Pie is bad for you! 83 82 </parent> 83 <ignoreme>foobar</ignoreme> 84 84 </root> 85 85 … … 87 87 88 88 $t->is($text, $expected, '->replaceText() replaces the text in the correct node'); 89 plugins/sfHighlightPlugin/trunk/test/unit/reader/xfHighlightReaderStringTest.php
r8890 r9031 9 9 10 10 require dirname(__FILE__) . '/../../bootstrap/unit.php'; 11 require 'reader/xfHighlightReaderInterface.interface.php'; 11 12 require 'reader/xfHighlightReader.interface.php'; 12 13 require 'reader/xfHighlightReaderString.class.php'; … … 26 27 $token = new xfHighlightToken('walrus', 7, 13); 27 28 $reader->replaceText($token, 'lucy'); 28 $t->is($reader->get Text(), 'I am a lucy in the sky with diamonds.', '->replaceText() does the replacement');29 $t->is($reader->getString(), 'I am a lucy in the sky with diamonds.', '->replaceText() does the replacement'); plugins/sfHighlightPlugin/trunk/test/unit/search/xfHighlightRetortFilterCallbackTest.php
r8994 r9031 23 23 require 'marker/xfHighlightMarker.interface.php'; 24 24 require 'marker/xfHighlightMarkerUppercase.class.php'; 25 require 'reader/xfHighlightReaderInterface.interface.php'; 25 26 require 'reader/xfHighlightReader.interface.php'; 26 27 require 'reader/xfHighlightReaderString.class.php';