tokens = $tokens;
}
/**
* @since 2.5
*
* @return array
*/
public function getTokens() {
return $this->tokens;
}
/**
* @since 2.5
*
* @param Description $description
*/
public function addFromDesciption( Description $description ) {
if ( $description instanceof Conjunction ) {
foreach ( $description->getDescriptions() as $desc ) {
return $this->addFromDesciption( $desc );
}
}
if ( $description instanceof SomeProperty ) {
return $this->addFromDesciption( $description->getDescription() );
}
if ( !$description instanceof ValueDescription ) {
return;
}
$isProximate = $description->getComparator() === SMW_CMP_LIKE || $description->getComparator() === SMW_CMP_PRIM_LIKE;
// [[SomeProperty::~*Foo*]] / [[SomeProperty::like:*Foo*]]
if ( $isProximate && $description->getDataItem() instanceof DIBlob ) {
return $this->addTokensFromText( $description->getDataItem()->getString() );
}
// [[~~* ... *]]
if ( $description->getDataItem() instanceof DIWikiPage && strpos( $description->getDataItem()->getDBKey(), '~' ) !== false ) {
return $this->addTokensFromText( $description->getDataItem()->getDBKey() );
}
}
/**
* Sets format information (|?Foo#-hl) from a result printer
*
* @since 2.5
*
* @param string $outputFormat
*/
public function setOutputFormat( $outputFormat ) {
$this->outputFormat = $outputFormat;
}
/**
* @since 2.5
*
* @param string $text
* @param type $text
*
* @return string
*/
public function highlight( $text, $type = self::HL_BOLD ) {
if ( $this->tokens === [] || strpos( strtolower( $this->outputFormat ), '-hl' ) === false ) {
return $text;
}
return $this->doHighlight( $text, $type, array_keys( $this->tokens ) );
}
private function doHighlight( $text, $type, $tokens ) {
if ( $type === self::HL_BOLD ) {
$replacement = "$0";
} elseif ( $type === self::HL_UNDERLINE ) {
$replacement = "$0";
} elseif ( $type === self::HL_SPAN ) {
$replacement = "$0";
} else {
$replacement = "'''$0'''";
}
// Match all tokens except those within [ ... ] to avoid breaking links
// and annotations
$pattern = '/(' . implode( '|', $tokens ) . ')+(?![^\[]*\])/iu';
return preg_replace( $pattern, $replacement, $text );
}
private function addTokensFromText( $text ) {
// Remove query related chars
$text = str_replace(
[ '*', '"', '~', '_', '+', '-' ],
[ '', '', '', ' ', '', '' ],
$text
);
return $this->tokens += array_flip( Tokenizer::tokenize( $text ) );
}
}