https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112881
Revision: 112881 Author: schuellersa Date: 2012-03-02 12:20:32 +0000 (Fri, 02 Mar 2012) Log Message: ----------- UPDATE to version 0.6 BETA addet template system for the output (SolrSearch) with standart version and example template. Modified Paths: -------------- trunk/extensions/SolrStore/SolrSearch.php trunk/extensions/SolrStore/SolrStore.php Added Paths: ----------- trunk/extensions/SolrStore/SolrSearchTemplateLoader.php trunk/extensions/SolrStore/templates/ trunk/extensions/SolrStore/templates/SolrSearchTemplate_FIS.php trunk/extensions/SolrStore/templates/SolrSearchTemplate_Standart.php Modified: trunk/extensions/SolrStore/SolrSearch.php =================================================================== --- trunk/extensions/SolrStore/SolrSearch.php 2012-03-02 12:19:08 UTC (rev 112880) +++ trunk/extensions/SolrStore/SolrSearch.php 2012-03-02 12:20:32 UTC (rev 112881) @@ -4,7 +4,7 @@ * * @ingroup SolrStore * @file - * @author Simon Bachenberg + * @author Sascha Schueller, Simon Bachenberg */ /** @@ -17,7 +17,7 @@ var $limit = 10; var $offset = 0; var $prefix = ''; - var $searchTerms = array(); + var $searchTerms = array( ); var $namespaces = array( NS_MAIN ); var $showRedirects = false; var $solrTalker; @@ -60,10 +60,10 @@ static function prefixSearch( $ns, $search, $limit, &$results ) { echo 'Prefix Search!<br />'; // @todo Is this a debug line? Certainly looks like one...if so, comment out/remove! $it = SolrSearchSet::newFromQuery( 'prefix', $search, $ns, $limit, 0 ); - $results = array(); + $results = array( ); if ( $it ) { // $it can be null while ( $res = $it->next() ) { - $results[] = $res->getTitle()->getPrefixedText(); + $results[ ] = $res->getTitle()->getPrefixedText(); } } return false; @@ -101,18 +101,18 @@ $rindex = 0; // point to last rewritten character $inquotes = false; - // "search everything" keyword +// "search everything" keyword $allkeyword = wfMsgForContent( 'searchall' ); $qlen = strlen( $query ); - // quick check, most of the time we don't need any rewriting +// quick check, most of the time we don't need any rewriting if ( strpos( $query, ':' ) === false ) { wfProfileOut( __METHOD__ ); return $query; } - // check if this is query for related articles +// check if this is query for related articles $relatedkey = wfMsgForContent( 'searchrelated' ) . ':'; if ( $wgSolrUseRelated && strncmp( $query, $relatedkey, strlen( $relatedkey ) ) == 0 ) { $this->related = true; @@ -128,18 +128,18 @@ array_keys( array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() ) ) ); - # add all namespace names w/o spaces - $nsNames = array(); +# add all namespace names w/o spaces + $nsNames = array( ); foreach ( $nsNamesRaw as $ns ) { if ( $ns != '' ) { - $nsNames[] = $ns; - $nsNames[] = str_replace( '_', ' ', $ns ); + $nsNames[ ] = $ns; + $nsNames[ ] = str_replace( '_', ' ', $ns ); } } $regexp = implode( '|', array_unique( $nsNames ) ); - # rewrite the query by replacing valid namespace names +# rewrite the query by replacing valid namespace names $parts = preg_split( '/(")/', $query, -1, PREG_SPLIT_DELIM_CAPTURE ); $inquotes = false; $rewritten = ''; @@ -150,9 +150,9 @@ } elseif ( $inquotes ) { $rewritten .= $part; } else { - # replace namespaces +# replace namespaces $r = preg_replace_callback( '/(^|[| :])(' . $regexp . '):/i', array( $this, 'replaceNamespace' ), $part ); - # replace to backend all: notation +# replace to backend all: notation $rewritten .= str_replace( $allkeyword . ':', 'all:', $r ); } } @@ -164,11 +164,11 @@ /** callback to replace namespace names to internal notation, e.g. User: -> [2]: */ function replaceNamespace( $matches ) { global $wgContLang; - $inx = $wgContLang->getNsIndex( str_replace( ' ', '_', $matches[2] ) ); + $inx = $wgContLang->getNsIndex( str_replace( ' ', '_', $matches[ 2 ] ) ); if ( $inx === false ) { - return $matches[0]; + return $matches[ 0 ]; } else { - return $matches[1] . "[$inx]:"; + return $matches[ 1 ] . "[$inx]:"; } } @@ -181,7 +181,7 @@ global $wgSolrSearchVersion; if ( $wgSolrSearchVersion >= 2.1 && $this->prefix != '' ) { - # convert to internal backend prefix notation +# convert to internal backend prefix notation $term = $term . ' prefix:' . $this->prefix; } @@ -204,223 +204,23 @@ return false; } - function SolrResult( $result, $method ) { - global $wgContLang; + function SolrResult( $xml ) { + global $SolrSearchTemplateLoader; + wfDebug( "Solr line: '$xml'\n" ); - $score = null; - $interwiki = null; - $namespace = null; - $title = null; - - $xml = $result; - wfDebug( "Solr line: '$result'\n" ); - - // Defining results - - $this->mDate = null; - $score = $xml->float; - $this->mScore = $score; - - // ------------------------------------- - // Get the shit out of the XML - // ------------------------------------- - foreach ( $xml->arr as $doc ) { - switch ( $doc['name'] ) { - case 'text': - $nsText = $doc->str; - - $this->mSize = ''; - $this->mWordCount = count( $doc->str ); - $snipmax = 10; - $textsnip = ''; - $textsnipvar = 0; - foreach ( $doc->str as $inner ) { - $textsnipvar++; - if ( $textsnipvar >= 4 && $textsnipvar <= $snipmax ) { - $textsnip .= ' ' . $inner; - } - $this->mSize = $this->mSize + strlen( $inner ); - } - $textsnip .= '...'; - $this->mSize = ( $this->mSize / 3 ); - // $this->mSize=$size; - break; - case 'Zuletzt geändert_dt': - $this->mDate = $doc->date; - break; - } - } - - foreach ( $xml->str as $docs ) { - switch ( $docs['name'] ) { - case 'pagetitle': - $this->mTitle = $doc->str; - break; - case 'dbkey': - $title = $doc->str; - break; - case 'interwiki': - $this->mInterwiki = $doc->str; - break; - } - } - - foreach ( $xml->int as $doci ) { - switch ( $doci['name'] ) { - case 'namespace': - $namespace = $doc->str; - break; - } - } - - $title = urldecode( $title ); - if ( !isset( $nsText ) ) { - $nsText = $wgContLang->getNsText( $namespace ); - } else { - $nsText = urldecode( $nsText ); - } - - // make title - $this->mTitle = Title::makeTitle( $namespace, $title ); - - // HIGHLIGHT - // <em> & </em> Fix to <b> // if not, no highlighting! - // TITLE (LINK) - if ( $xml->highlight->title != '' ) { - $this->mHighlightTitle = str_replace( '<em>', '<b>', $xml->highlight->title ); - $this->mHighlightTitle = str_replace( '</em>', '</b>', $this->mHighlightTitle ); - } else { - $this->mHighlightTitle = ''; - } - // TEXT (under link) - // $this->mHighlightText=$xml->highlight->title; - if ( $xml->highlight->Inhalt != '' ) { - $this->mHighlightText = str_replace( '<em>', '<b>', $xml->highlight->Inhalt ); - $this->mHighlightText = str_replace( '</em>', '</b>', $this->mHighlightText ); - $this->mHighlightText .= '...'; - } else {// $this->mHighlightText=textsnip; - if ( isset( $_REQUEST['search'] ) != '' && isset( $textsnip ) != '' ) { - if ( strpos( strtolower( $textsnip ), strtolower( $_REQUEST['search'] ) ) ) { - $tempc = strlen( $_REQUEST['search'] ); - $pos1 = strpos( strtolower( $textsnip ), strtolower( $_REQUEST['search'] ) ); - $tmpa = substr( $textsnip, 0, $pos1 ); - $tmpb = substr( $textsnip, ( $pos1 + $tempc ) ); - $this->mHighlightText = $tmpa . '<b>' . substr( $textsnip, $pos1, $tempc ) . '</b>' . $tmpb; - // str_replace($_REQUEST['search'],"<b>".$_REQUEST['search']."</b>",$textsnip); - } else { - $this->mHighlightText = $textsnip; - } - } else { - $this->mHighlightText = $textsnip; - } - } - $this->mRedirectTitle = null; - $this->mHighlightSection = null; - $this->mSectionTitle = null; + $newtemplate = $SolrSearchTemplateLoader->applyTemplate( $xml ); // define Template in SolsStore + $this->mTitle = $newtemplate->mTitle; + $this->mRedirectTitle = $newtemplate->mRedirectTitle; + $this->mHighlightSection = $newtemplate->mHighlightSection; + $this->mSectionTitle = $newtemplate->mSectionTitle; + $this->mDate = $newtemplate->mDate; + $this->mScore = $newtemplate->mScore; + $this->mHighlightTitle = $newtemplate->mHighlightTitle; + $this->mHighlightText = $newtemplate->mHighlightText; + $this->mSize = $newtemplate->mSize; + $this->mWordCount = $newtemplate->mWordCount; } - /** - * Get the pair [highlighted snippet, unmodified text] for highlighted text - * - * @param string $lines - * @param string $nsText textual form of namespace - * @param string $type - * @param boolean $useFinalSeparator - * @return array (highlighted, unmodified text) - */ - function extractSnippet( $lines, $nsText, $type, $useFinalSeparator = false ) { - if ( !array_key_exists( $type, $lines ) ) { - return array( null, null ); - } - $ret = ''; - $original = null; - foreach ( $lines[$type] as $h ) { - list( $s, $o ) = $this->extractSnippetLine( $h, $useFinalSeparator ); - $ret .= $s; - $original = $o; - } - if ( $nsText != '' ) { - $ret = $nsText . ':' . $ret; - } - return array( $ret, $original ); - } - - /** - * Parse one line of a snippet - * - * @param string $line - * @param boolean $useFinalSeparator if "..." is to be appended to the end of snippet - * @access protected - * @return array(snippet,unmodified text) - */ - function extractSnippetLine( $line, $useFinalSeparator ) { - $parts = explode( ' ', $line ); - if ( count( $parts ) != 4 && count( $parts ) != 5 ) { - wfDebug( "Bad result line:" . $line . "\n" ); - return ''; - } - $splits = $this->stripBracketsSplit( $parts[0] ); - $highlight = $this->stripBracketsSplit( $parts[1] ); - $suffix = urldecode( $this->stripBrackets( $parts[2] ) ); - $text = urldecode( $parts[3] ); - $original = null; - if ( count( $parts ) > 4 ) { - $original = urldecode( $parts[4] ); - } - - $splits[] = strlen( $text ); - $start = 0; - $snippet = ''; - $hi = 0; - $ellipsis = wfMsgForContent( 'ellipsis' ); - - foreach ( $splits as $sp ) { - $sp = intval( $sp ); - // highlight words! - while ( $hi < count( $highlight ) && intval( $highlight[$hi] ) < $sp ) { - $s = intval( $highlight[$hi] ); - $e = intval( $highlight[$hi + 1] ); - $snippet .= substr( $text, $start, $s - $start ) . - '<span class="searchmatch">' . substr( $text, $s, $e - $s ) . '</span>'; - $start = $e; - $hi += 2; - } - // copy till split point - $snippet .= substr( $text, $start, $sp - $start ); - if ( $sp == strlen( $text ) && $suffix != '' ) { - $snippet .= $suffix; - } elseif ( $useFinalSeparator ) { - $snippet .= ' <b>' . $ellipsis . '</b> '; - } - - $start = $sp; - } - return array( $snippet, $original ); - } - - /** - * @access private - */ - function stripBrackets( $str ) { - if ( $str == '[]' ) { - return ''; - } - return substr( $str, 1, strlen( $str ) - 2 ); - } - - /** - * @access private - * @return array - */ - function stripBracketsSplit( $str ) { - $strip = $this->stripBrackets( $str ); - if ( $strip == '' ) { - return array(); - } else { - return explode( ',', $strip ); - } - } - function getTitle() { return $this->mTitle; } @@ -523,38 +323,38 @@ * @param $searchAll Boolean * @return array */ - public static function newFromQuery( $method, $query, $namespaces = array(), $limit = 20, $offset = 0, $searchAll = false ) { + public static function newFromQuery( $method, $query, $namespaces = array( ), $limit = 20, $offset = 0, $searchAll = false ) { wfProfileIn( __METHOD__ ); $wgSolrTalker = new SolrTalker(); $query = $wgSolrTalker->queryChecker( $query ); $xml = $wgSolrTalker->solrQuery( $query, $offset, $limit, true, true ); // Abfrage ok, ergebniss in XML - $totalHits = $xml->result['numFound']; + $totalHits = $xml->result[ 'numFound' ]; - $resultLines = array(); ; + $resultLines = array( ); $highl = $xml->xpath( '//lst[@name="highlighting"]/lst' ); $hli = 0; foreach ( $xml->result->doc as $doc ) { - if ( isset( $highl[$hli]->arr ) ) { - foreach ( $highl[$hli]->arr as $feld ) { - if ( isset( $feld['name'] ) ) { - switch ( $feld['name'] ) { + if ( isset( $highl[ $hli ]->arr ) ) { + foreach ( $highl[ $hli ]->arr as $feld ) { + if ( isset( $feld[ 'name' ] ) ) { + switch ( $feld[ 'name' ] ) { case 'title': - $doc[]->highlight->title = $feld->str; + $doc[ ]->highlight->title = $feld->str; break; - case 'Inhalt de_t': - $doc[]->highlight->Inhalt = $feld->str; + case 'Inhalt de_t': // TODO: is style ? + $doc[ ]->highlight->Inhalt = $feld->str; break; } } } } $hli++; - $resultLines[] = $doc; + $resultLines[ ] = $doc; } $suggestion = null; @@ -562,8 +362,8 @@ $interwiki = null; $resultSet = new SolrSearchSet( - $method, $query, $resultLines, count( $resultLines ), $totalHits, - $suggestion, $info, $interwiki + $method, $query, $resultLines, count( $resultLines ), $totalHits, + $suggestion, $info, $interwiki ); wfProfileOut( __METHOD__ ); @@ -606,15 +406,15 @@ return; } - // parse split points and highlight changes +// parse split points and highlight changes list( $dummy, $points, $sug ) = explode( ' ', $suggestion ); $sug = urldecode( $sug ); $points = explode( ',', substr( $points, 1, -1 ) ); array_unshift( $points, 0 ); $suggestText = ''; for ( $i = 1; $i < count( $points ); $i += 2 ) { - $suggestText .= htmlspecialchars( substr( $sug, $points[$i - 1], $points[$i] - $points[$i - 1] ) ); - $suggestText .= '<em>' . htmlspecialchars( substr( $sug, $points[$i], $points[$i + 1] - $points[$i] ) ) . '</em>'; + $suggestText .= htmlspecialchars( substr( $sug, $points[ $i - 1 ], $points[ $i ] - $points[ $i - 1 ] ) ); + $suggestText .= '<em>' . htmlspecialchars( substr( $sug, $points[ $i ], $points[ $i + 1 ] - $points[ $i ] ) ) . '</em>'; } $suggestText .= htmlspecialchars( substr( $sug, end( $points ) ) ); @@ -633,7 +433,7 @@ $phrases[$i] ); if ( $i + 1 < count( $phrases ) ) { - $out .= '"' . $phrases[$i + 1] . '"'; // phrase text + $out .= '"' . $phrases[ $i + 1 ] . '"'; // phrase text } } return $out; @@ -641,7 +441,7 @@ function genericPrefixCallback( $matches ) { global $wgContLang; - return $wgContLang->getFormattedNsText( $matches[1] ) . ':'; + return $wgContLang->getFormattedNsText( $matches[ 1 ] ) . ':'; } function numRows() { @@ -652,26 +452,26 @@ $resq = preg_replace( "/\\[.*?\\]:/", ' ', $this->mQuery ); # generic prefixes $resq = preg_replace( '/all:/', ' ', $resq ); - // @todo FIXME: this is ripped from SearchMySQL and probably kind of sucks, - // but it handles quoted phrase searches more or less correctly. - // Should encapsulate this stuff better. - // @todo FIXME: This doesn't handle parenthetical expressions. - $regexes = array(); - $m = array(); +// @todo FIXME: this is ripped from SearchMySQL and probably kind of sucks, +// but it handles quoted phrase searches more or less correctly. +// Should encapsulate this stuff better. +// @todo FIXME: This doesn't handle parenthetical expressions. + $regexes = array( ); + $m = array( ); $lc = SearchEngine::legalSearchChars(); if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/', $resq, $m, PREG_SET_ORDER ) ) { foreach ( $m as $terms ) { - if ( !empty( $terms[3] ) ) { - // Match individual terms in result highlighting... - $regexp = preg_quote( $terms[3], '/' ); - if ( $terms[4] ) { + if ( !empty( $terms[ 3 ] ) ) { +// Match individual terms in result highlighting... + $regexp = preg_quote( $terms[ 3 ], '/' ); + if ( $terms[ 4 ] ) { $regexp .= '[0-9A-Za-z_]+'; } } else { - // Match the quoted term in result highlighting... - $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' ); +// Match the quoted term in result highlighting... + $regexp = preg_quote( str_replace( '"', '', $terms[ 2 ] ), '/' ); } - $regexes[] = $regexp; + $regexes[ ] = $regexp; } wfDebug( __METHOD__ . ': Match with /' . implode( '|', $regexes ) . "/\n" ); } else { @@ -752,7 +552,7 @@ public function next() { if ( $this->mPos < $this->mResultCount ) { $this->mPos++; - return new SolrResult( $this->mResults[$this->mPos - 1], $this->mMethod ); + return new SolrResult( $this->mResults[ $this->mPos - 1 ] ); } else { return null; } Added: trunk/extensions/SolrStore/SolrSearchTemplateLoader.php =================================================================== --- trunk/extensions/SolrStore/SolrSearchTemplateLoader.php (rev 0) +++ trunk/extensions/SolrStore/SolrSearchTemplateLoader.php 2012-03-02 12:20:32 UTC (rev 112881) @@ -0,0 +1,35 @@ +<?php +/** + * Dynamic TemplateLoader + * + * @ingroup SolrStore + * @file + * @author Sascha Schueller + */ + +/** + * TODO: Insert class description + * + * @ingroup SolrStore + */ +class SolrSearchTemplateLoader { + + public function applyTemplate( $xml ) { + global $wgSolrTemplate; + + $dir = dirname( __FILE__ ); + $file = $dir . '/templates/SolrSearchTemplate' . $wgSolrTemplate . '.php'; + $classname = 'SolrSearchTemplate' . $wgSolrTemplate; + + if ( file_exists( $file ) ) { + if ( !class_exists( $classname ) ) { + include($dir . '/templates/SolrSearchTemplate' . $wgSolrTemplate . '.php'); + } + $classname = new $classname(); + return $classname->applyTemplate( $xml ); + } + die( "SolrSearch Template Problem: File not exists !! " . $file ); + } + +} +?> Modified: trunk/extensions/SolrStore/SolrStore.php =================================================================== --- trunk/extensions/SolrStore/SolrStore.php 2012-03-02 12:19:08 UTC (rev 112880) +++ trunk/extensions/SolrStore/SolrStore.php 2012-03-02 12:20:32 UTC (rev 112881) @@ -5,10 +5,9 @@ * Apache Solr. * * @defgroup SolrStore - * @author Stephan Gambke, Simon Bachenberg - * @version 0.5 Beta + * @author Stephan Gambke, Simon Bachenberg, Sascha Schueller + * @version 0.6 Beta */ - /** * The main file of the SolrConnector extension * @@ -26,17 +25,17 @@ /** * The Solr Connector version */ -define( 'SC_VERSION', '0.5 Beta' ); +define( 'SC_VERSION', '0.6 Beta' ); // register the extension // TODO: Add other authors here and in the file header $wgExtensionCredits[ defined( 'SEMANTIC_EXTENSION_TYPE' ) ? 'semantic' : 'other' ][ ] = array( - 'path' =>__FILE__, - 'name' =>'SolrStore', - 'author' =>array( '[https://www.mediawiki.org/wiki/User:F.trott Stephan Gambke]', '[https://www.mediawiki.org/wiki/User:SBachenberg Simon Bachenberg]', 'Sascha Schüller' ), - 'url' =>'https://www.mediawiki.org/wiki/Extension:SolrStore', + 'path'=>__FILE__, + 'name'=>'SolrStore', + 'author'=>array( '[https://www.mediawiki.org/wiki/User:F.trott Stephan Gambke]', '[https://www.mediawiki.org/wiki/User:SBachenberg Simon Bachenberg]', 'Sascha Schüller' ), + 'url'=>'https://www.mediawiki.org/wiki/Extension:SolrStore', 'descriptionmsg'=>'solrstore-desc', - 'version' =>SC_VERSION, + 'version'=>SC_VERSION, ); @@ -48,16 +47,16 @@ $wgExtensionMessagesFiles[ 'SolrStoreAlias' ] = $dir . '/SolrStore.alias.php'; // register class files with the Autoloader -$wgAutoloadClasses[ 'SolrConnectorStore' ] = $dir . '/SolrConnectorStore.php'; -$wgAutoloadClasses[ 'SolrDoc' ] = $dir . '/SolrDoc.php'; -$wgAutoloadClasses[ 'SolrTalker' ] = $dir . '/SolrTalker.php'; -$wgAutoloadClasses[ 'SolrSearch' ] = $dir . '/SolrSearch.php'; -$wgAutoloadClasses[ 'SolrResult' ] = $dir . '/SolrSearch.php'; -$wgAutoloadClasses[ 'SolrSearchSet' ] = $dir . '/SolrSearch.php'; -$wgAutoloadClasses[ 'SpecialSolrSearch' ] = $dir . '/SpecialSolrSearch.php'; -$wgAutoloadClasses[ 'SolrSearchFieldSet' ] = $dir . '/SolrSearchFieldSet.php'; +$wgAutoloadClasses[ 'SolrConnectorStore' ] = $dir . '/SolrConnectorStore.php'; +$wgAutoloadClasses[ 'SolrDoc' ] = $dir . '/SolrDoc.php'; +$wgAutoloadClasses[ 'SolrTalker' ] = $dir . '/SolrTalker.php'; +$wgAutoloadClasses[ 'SolrSearch' ] = $dir . '/SolrSearch.php'; +$wgAutoloadClasses[ 'SolrResult' ] = $dir . '/SolrSearch.php'; +$wgAutoloadClasses[ 'SolrSearchSet' ] = $dir . '/SolrSearch.php'; +$wgAutoloadClasses[ 'SpecialSolrSearch' ] = $dir . '/SpecialSolrSearch.php'; +$wgAutoloadClasses[ 'SolrSearchFieldSet' ] = $dir . '/SolrSearchFieldSet.php'; +$wgAutoloadClasses[ 'SolrSearchTemplateLoader' ] = $dir . '/SolrSearchTemplateLoader.php'; - // Specialpage $wgSpecialPages[ 'SolrSearch' ] = 'SpecialSolrSearch'; # Tell MediaWiki about the new special page and its class name $wgSpecialPageGroups[ 'SolrSearch' ] = 'smw_group'; @@ -66,12 +65,14 @@ // original store $wgscBaseStore = $smwgDefaultStore; $smwgDefaultStore = "SolrConnectorStore"; -$smwgQuerySources[ "solr" ] = "SolrConnectorStore"; +$smwgQuerySources[ "solr" ] = "SolrConnectorStore"; $wgSearchType = 'SolrSearch'; // Solr Configuration -$wgSolrTalker = new SolrTalker(); -$wgSolrShowRelated = true; -$wgSolrDebug = false; -$wgSolrUrl = 'http://127.0.0.1:8080/solr'; -$wgSolrFields = array( ); +$wgSolrTalker = new SolrTalker(); +$SolrSearchTemplateLoader = new SolrSearchTemplateLoader(); +$wgSolrUrl = 'http://127.0.0.1:8080/solr'; +$wgSolrTemplate = "_Standart"; +$wgSolrFields = array( ); +$wgSolrShowRelated = true; +$wgSolrDebug = false; Added: trunk/extensions/SolrStore/templates/SolrSearchTemplate_FIS.php =================================================================== --- trunk/extensions/SolrStore/templates/SolrSearchTemplate_FIS.php (rev 0) +++ trunk/extensions/SolrStore/templates/SolrSearchTemplate_FIS.php 2012-03-02 12:20:32 UTC (rev 112881) @@ -0,0 +1,336 @@ +<?php +/** + * Dynamic Template 'FIS' -> Example + * + * Example Template + * with : makeHighlightText and makeHighlightTitle functions + * + * to use this template add this peace of code to LocalSettings.php + * + * # define Template + * $wgSolrTemplate = "_FIS"; + * + * + * @ingroup SolrStore + * @path templates + * @author Sascha Schueller + */ +class SolrSearchTemplate_FIS { + + var $mTitle = null; + var $mRedirectTitle = null; + var $mHighlightSection = null; + var $mSectionTitle = null; + var $mDate = null; + var $mScore = null; + var $mSize = null; + var $Inhalt_de_t = null; + var $mHighlightText = null; + var $mHighlightTitle = null; + var $mWordCount = null; + + public function applyTemplate( $xml ) { + global $wgSolrFields; + + $snipmax = 50; + $textlenght = 1000; + $textlenghteffective = 315; + + // Bugfix: clear the var! + unset( $this->Inhalt_de_t ); + + // get Size, Wordcound, Date, Inhalt_de_t from XML: + foreach ( $xml->arr as $doc ) { + switch ( $doc[ 'name' ] ) { + case 'text': + $nsText = $doc->str; + $this->mSize = ''; + $this->mWordCount = count( $doc->str ); + $snipmax = 50; + $textsnip = ''; + $textsnipvar = 0; + foreach ( $doc->str as $inner ) { + $textsnipvar++; + if ( $textsnipvar >= 4 && $textsnipvar <= $snipmax ) { + $textsnip .= ' ' . $inner; + } + $textsnip = substr( $textsnip, 0, $textlenght ); + $this->mSize = $this->mSize + strlen( $inner ); + } + $this->mSize = ( $this->mSize / 3 ); + break; + + case 'Zuletzt geändert_dt': + $this->mDate = $doc->date; + break; + + case 'Inhalt de_t': + $this->Inhalt_de_t[ ] = $doc->str; + break; + } + } + + // get Title, Interwiki from XML: + foreach ( $xml->str as $docs ) { + switch ( $docs[ 'name' ] ) { + case 'pagetitle': + $this->mTitle = $doc->str; + break; + case 'dbkey': + $title = $doc->str; + break; + case 'interwiki': + $this->mInterwiki = $doc->str; + break; + } + } + + //get namespace from XML: + foreach ( $xml->int as $doci ) { + switch ( $doci[ 'name' ] ) { + case 'namespace': + $namespace = $doc->str; + break; + } + } + + if ( !isset( $nsText ) ) { + $nsText = $wgContLang->getNsText( $namespace ); + } else { + $nsText = urldecode( $nsText ); + } + + // make score / relevance + $this->mScore = $xml->float; + + // make Title + $title = urldecode( $title ); + $this->mTitle = Title::makeTitle( $namespace, $title ); + + // make Highlight - Title + $this->mHighlightTitle = $title; + $this->makeHighlightTitle( $wgSolrFields, $title ); + + $firstw = false; + + if ( isset( $this->Inhalt_de_t[ 0 ] ) != '' ) { + $firstw = substr( $this->Inhalt_de_t[ 0 ], 0, strpos( $this->Inhalt_de_t[ 0 ], " " ) ); // test CUT textsnip: + if ( $firstw != false ) { + $textsnip = substr( $textsnip, strpos( $textsnip, $firstw ) ); + } else { + if ( isset( $xml->highlight->Inhalt ) != '' ) { + $firstw = substr( $xml->highlight->Inhalt, 0, strpos( $xml->highlight->Inhalt, " " ) ); // test CUT textsnip: + if ( $firstw != false ) { + $textsnip = substr( $textsnip, strpos( $textsnip, $firstw ) ); + } + } else { + + } + } + + // make Highlight - Text + $this->mHighlightText = substr( $textsnip, 0, $textlenghteffective ) . "..."; // MAX LENG [INHALT] + $this->makeHighlightText( $wgSolrFields, $textsnip ); // TEXTSNIP: Highlight the searching stuff: + } else { + $this->mHighlightText = ""; + } + + return $this; + } + + private function cleanword( $w ) { // Bugfix 4 the highlighting system + $str = array( "*", ">", "<", "/", '"' ); + $w = str_replace( $str, "", $w ); + return $w; + } + + private function makeHighlightTitle( $wgSolrFields, $textsnipcut ) { + for ( $tfields = 0; $tfields <= count( $wgSolrFields ) - 1; $tfields++ ) { + foreach ( $wgSolrFields[ $tfields ] as $fieldcollection=>$inhalt ) { + if ( $fieldcollection == 'mLable' ) { // get Solrfields + foreach ( $inhalt as $feldname=>$lablename ) { + $pos = false; + $pos = strpos( $feldname, "solr" ); + if ( $pos !== false ) { + + if ( isset( $_REQUEST[ $feldname ] ) != '' && isset( $textsnipcut ) != '' ) { + $wcount = substr_count( $_REQUEST[ $feldname ], " " ) + 1; + unset( $leerat ); + for ( $wlpos = 0; $wlpos <= strlen( $_REQUEST[ $feldname ] ); $wlpos++ ) { // wo sind die leerzeichen ? + if ( substr( $_REQUEST[ $feldname ], $wlpos, 1 ) == " " ) { + $leerat[ ].=$wlpos; + } + } + for ( $wpos = 1; $wpos <= $wcount; $wpos++ ) { + $isschon = false; + $pos1 = false; + $tempc = false; + + if ( isset( $_REQUEST[ $feldname ] ) && $_REQUEST[ $feldname ] != '' ) { + if ( $wpos == 1 ) { // First word: [OK] + if ( $wcount > 1 ) { // more than 1 highlighting word: + $highlighword = substr( $_REQUEST[ $feldname ], 0, $leerat[ $wpos - 1 ] ); + } else { // Only 1 word: [OK] + $highlighword = substr( $_REQUEST[ $feldname ], 0 ); + } + } elseif ( $wpos == $wcount ) { // Last word: [OK] + $highlighword = substr( $_REQUEST[ $feldname ], $leerat[ $wpos - 2 ] + 1 ); + } elseif ( $wpos != $wcount && $wpos != 1 ) { // all words betwen first and last [OK] + $highlighword = substr( $_REQUEST[ $feldname ], $leerat[ $wpos - 2 ] + 1, (($leerat[ $wpos - 1 ] - $leerat[ $wpos - 2 ]) - 1 ) ); + } + $highlighword = $this->cleanword( $highlighword ); + + if ( $highlighword != "" ) { + if ( $highlighword == "b" || $highlighword == "B" ) { + $this->mHighlightTitle = preg_replace( "#" . strtolower( $highlighword ) . "#", "<em><b>" . $highlighword . "</b></em>", $this->mHighlightTitle ); + } else { + + if ( strpos( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ) > 0 ) { // Highlightwort nicht am anfang: + $tempc = strlen( $highlighword ); + $pos1 = strpos( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ); + + if ( $pos1 != false && (substr_count( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ) == 1) ) { // wenn Wort nur 1x vorhanden ist normal Highlighten: + $tmpa = substr( $this->mHighlightTitle, 0, $pos1 ); + $tmpb = substr( $this->mHighlightTitle, ( $pos1 + $tempc ) ); + + $this->mHighlightTitle = $tmpa . '<em><b>' . substr( $this->mHighlightTitle, $pos1, $tempc ) . '</b></em>' . $tmpb; + } + if ( $pos1 != false && (substr_count( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ) >= 2) ) { // Wenn Wort mehrmals vorkommt, dann mehrmals highlighten: + $highcount = substr_count( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ); + unset( $offset ); + $offset[ 0 ] = 0; + for ( $hc = 1; $hc <= $highcount; $hc++ ) { + $tempc = strlen( $highlighword ); + + if ( $hc == 1 ) { + $pos1 = strpos( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ); + } else { + $pos1 = strpos( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ), $offset[ $hc - 1 ] + 7 + $tempc ); + } + + $offset[ $hc ] = $pos1; + $tmpa = substr( $this->mHighlightTitle, 0, $pos1 ); + $tmpb = substr( $this->mHighlightTitle, ( $pos1 + $tempc ) ); + $this->mHighlightTitle = $tmpa . '<em><b>' . substr( $this->mHighlightTitle, $pos1, $tempc ) . '</b></em>' . $tmpb; + } + } + } else { // Erstes Wort im Satz: + $tempc = strlen( $highlighword ); + $pos1 = strpos( strtolower( $this->mHighlightTitle ), strtolower( $highlighword ) ); + + if ( $isschon == false && $pos1 == 0 && (strtolower( $highlighword ) == substr( strtolower( $this->mHighlightTitle ), 0, $tempc ) ) ) { +// + $tmpb = substr( $this->mHighlightTitle, $tempc ); + + if ( strtolower( $highlighword ) == substr( strtolower( $this->mHighlightTitle ), 0, $tempc ) ) { + $this->mHighlightTitle = '<em><b> ' . substr( $this->mHighlightTitle, 0, $tempc ) . '</b></em>' . $tmpb; + $isschon = true; + } + } + } + } + } + } + } + } + } + } + } + } + } + } + + private function makeHighlightText( $wgSolrFields, $textsnipcut ) { + for ( $tfields = 0; $tfields <= count( $wgSolrFields ) - 1; $tfields++ ) { + foreach ( $wgSolrFields[ $tfields ] as $fieldcollection=>$inhalt ) { + if ( $fieldcollection == 'mLable' ) { // get Solrfields + foreach ( $inhalt as $feldname=>$lablename ) { + $pos = false; + + $pos = strpos( $feldname, "solr" ); + if ( $pos !== false ) { + + if ( isset( $_REQUEST[ $feldname ] ) != '' && isset( $textsnipcut ) != '' ) { + $wcount = substr_count( $_REQUEST[ $feldname ], " " ) + 1; + unset( $leerat ); + for ( $wlpos = 0; $wlpos <= strlen( $_REQUEST[ $feldname ] ); $wlpos++ ) { // wo sind die leerzeichen ? + if ( substr( $_REQUEST[ $feldname ], $wlpos, 1 ) == " " ) { + $leerat[ ].=$wlpos; + } + } + for ( $wpos = 1; $wpos <= $wcount; $wpos++ ) { + $isschon = false; + $pos1 = false; + if ( isset( $_REQUEST[ $feldname ] ) && $_REQUEST[ $feldname ] != '' ) { + if ( $wpos == 1 ) { // First word: [OK] + if ( $wcount > 1 ) { // more than 1 highlighting word: + $highlighword = substr( $_REQUEST[ $feldname ], 0, $leerat[ $wpos - 1 ] ); + } else { // Only 1 word: [OK] + $highlighword = substr( $_REQUEST[ $feldname ], 0 ); + } + } elseif ( $wpos == $wcount ) { // Last word: [OK] + $highlighword = substr( $_REQUEST[ $feldname ], $leerat[ $wpos - 2 ] + 1 ); + } elseif ( $wpos != $wcount && $wpos != 1 ) { // all words betwen first and last [OK] + $highlighword = substr( $_REQUEST[ $feldname ], $leerat[ $wpos - 2 ] + 1, (($leerat[ $wpos - 1 ] - $leerat[ $wpos - 2 ]) - 1 ) ); + } + + $highlighword = $this->cleanword( $highlighword ); + + if ( $highlighword != "" ) { + if ( $highlighword == "b" || $highlighword == "B" ) { + $this->mHighlightText = preg_replace( "#" . strtolower( $highlighword ) . "#", "<em><b>" . $highlighword . "</b></em>", $this->mHighlightText ); + } else { + + if ( strpos( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ) > 0 ) { + + $tempc = strlen( $highlighword ); + $pos1 = strpos( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ); + + if ( $pos1 != false && (substr_count( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ) == 1) ) { // wenn Wort nur 1x vorhanden ist normal Highlighten: + $tmpa = substr( $this->mHighlightText, 0, $pos1 ); + $tmpb = substr( $this->mHighlightText, ( $pos1 + $tempc ) ); + + $this->mHighlightText = $tmpa . '<em><b>' . substr( $this->mHighlightText, $pos1, $tempc ) . '</b></em>' . $tmpb; + } + if ( $pos1 != false && (substr_count( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ) >= 2) ) { // Wenn Wort mehrmals vorkommt, dann mehrmals highlighten: + $highcount = substr_count( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ); + unset( $offset ); + $offset[ 0 ] = 0; + for ( $hc = 1; $hc <= $highcount; $hc++ ) { + $tempc = strlen( $highlighword ); + if ( $hc == 1 ) { + $pos1 = strpos( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ); + } else { + $pos1 = strpos( strtolower( $this->mHighlightText ), strtolower( $highlighword ), $offset[ $hc - 1 ] + 7 + $tempc ); + } + $offset[ $hc ] = $pos1; + $tmpa = substr( $this->mHighlightText, 0, $pos1 ); + $tmpb = substr( $this->mHighlightText, ( $pos1 + $tempc ) ); + $this->mHighlightText = $tmpa . '<em><b>' . substr( $this->mHighlightText, $pos1, $tempc ) . '</b></em>' . $tmpb; + } + } + } else { + $tempc = strlen( $highlighword ); + $pos1 = strpos( strtolower( $this->mHighlightText ), strtolower( $highlighword ) ); + + if ( $isschon == false && $pos1 == 0 && (strtolower( $highlighword ) == substr( strtolower( $this->mHighlightText ), 0, $tempc ) ) ) { + $tmpb = substr( $this->mHighlightText, $tempc ); + $this->mHighlightText = '<em><b> ' . substr( $this->mHighlightText, 0, $tempc ) . '</b></em>' . $tmpb; + $isschon = true; + } + } + } + } + } + } + } + } + } + } + } + } + } + +} + +?> Added: trunk/extensions/SolrStore/templates/SolrSearchTemplate_Standart.php =================================================================== --- trunk/extensions/SolrStore/templates/SolrSearchTemplate_Standart.php (rev 0) +++ trunk/extensions/SolrStore/templates/SolrSearchTemplate_Standart.php 2012-03-02 12:20:32 UTC (rev 112881) @@ -0,0 +1,116 @@ +<?php +/** + * Dynamic Template 'Standart' -> Example + * + * Conditions to use: + * Please name the class: SolrSearchTemplate_NAME + * Filename: SolrSearchTemplate_NAME.php + * dont touch the function name: applyTemplate( $xml ) + * + * @ingroup SolrStore + * @path templates + * @author Sascha Schueller + */ +class SolrSearchTemplate_Standart { + + var $mTitle = null; + var $mRedirectTitle = null; + var $mHighlightSection = null; + var $mSectionTitle = null; + var $mDate = null; + var $mScore = null; + var $mSize = null; + var $mHighlightText = null; + var $mHighlightTitle = null; + var $mWordCount = null; + + public function applyTemplate( $xml ) { // DONT TOUCH + $snipmax = 50; + $textlenght = 250; + + // get Size, Namespace, Wordcound, Date from XML: + foreach ( $xml->arr as $doc ) { + switch ( $doc[ 'name' ] ) { + case 'text': + $nsText = $doc->str; + $this->mSize = ''; + $this->mWordCount = count( $doc->str ); + $textsnip = ''; + $textsnipvar = 0; + foreach ( $doc->str as $inner ) { + $textsnipvar++; + if ( $textsnipvar >= 4 && $textsnipvar <= $snipmax ) { + $textsnip .= ' ' . $inner; + } + $textsnip = substr( $textsnip, 0, $textlenght ); + $this->mSize = $this->mSize + strlen( $inner ); + } + $this->mSize = ( $this->mSize / 3 ); + $this->mDate = $doc->date; + break; + } + } + + // get Title, Interwiki from XML: + foreach ( $xml->str as $docs ) { + switch ( $docs[ 'name' ] ) { + case 'pagetitle': + $this->mTitle = $doc->str; + break; + case 'dbkey': + $title = $doc->str; + break; + case 'interwiki': + $this->mInterwiki = $doc->str; + break; + } + } + + //get namespace from XML: + foreach ( $xml->int as $doci ) { + switch ( $doci[ 'name' ] ) { + case 'namespace': + $namespace = $doc->str; + break; + } + } + + if ( !isset( $nsText ) ) { + $nsText = $wgContLang->getNsText( $namespace ); + } else { + $nsText = urldecode( $nsText ); + } + + // make score / relevance + $this->mScore = $xml->float; + + // make Title + $title = urldecode( $title ); + $this->mTitle = Title::makeTitle( $namespace, $title ); + + // make Highlight - Title + if ( $xml->highlight->title != '' ) { + $this->mHighlightTitle = $xml->highlight->title; + } else { + $this->mHighlightTitle = ''; + } + + // make Highlight - Text + if ( $xml->highlight->Inhalt != '' ) { + $this->mHighlightText = str_replace( '<em>', '<b>', $xml->highlight->Inhalt ); + $this->mHighlightText = str_replace( '</em>', '</b>', $this->mHighlightText ); + $this->mHighlightText .= '...'; + } else { + $this->mHighlightText = "NO HIGHLIGHTING TEXT FROM SOLR !"; + + // if nothing define itself ! + // 4 example with + // $this->mHighlightText = $textsnip; + } + + return $this; + } + +} + +?> _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs