Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <comphelper/processfactory.hxx>
21 : #include <com/sun/star/ucb/Command.hpp>
22 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
23 : #include <com/sun/star/i18n/Transliteration.hpp>
24 : #include <com/sun/star/ucb/XCommandProcessor.hpp>
25 : #include <com/sun/star/lang/Locale.hpp>
26 : #include <com/sun/star/script/XInvocation.hpp>
27 :
28 : #include <helpcompiler/HelpSearch.hxx>
29 :
30 : #if defined _MSC_VER
31 : #pragma warning(push)
32 : #pragma warning(disable : 4068 4263 4264 4266)
33 : #endif
34 :
35 : #if defined(__GNUC__) && defined(HAVE_GCC_VISIBILITY_FEATURE)
36 : # pragma GCC visibility push (default)
37 : #endif
38 : #include <CLucene.h>
39 : #if defined(__GNUC__) && defined(HAVE_GCC_VISIBILITY_FEATURE)
40 : # pragma GCC visibility pop
41 : #endif
42 :
43 : #if defined _MSC_VER
44 : #pragma warning(pop)
45 : #endif
46 :
47 : #include <rtl/ustring.hxx>
48 :
49 : #include <algorithm>
50 : #include <set>
51 : #include <qe/Query.hxx>
52 : #include <qe/DocGenerator.hxx>
53 : #include "resultsetforquery.hxx"
54 : #include "databases.hxx"
55 :
56 : using namespace std;
57 : using namespace chelp;
58 : using namespace xmlsearch::excep;
59 : using namespace xmlsearch::qe;
60 : using namespace com::sun::star;
61 : using namespace com::sun::star::ucb;
62 : using namespace com::sun::star::i18n;
63 : using namespace com::sun::star::uno;
64 : using namespace com::sun::star::lang;
65 :
66 0 : struct HitItem
67 : {
68 : rtl::OUString m_aURL;
69 : float m_fScore;
70 :
71 : HitItem( void ) {}
72 0 : HitItem( const rtl::OUString& aURL, float fScore )
73 : : m_aURL( aURL )
74 0 : , m_fScore( fScore )
75 0 : {}
76 0 : bool operator < ( const HitItem& rHitItem ) const
77 : {
78 0 : return rHitItem.m_fScore < m_fScore;
79 : }
80 : };
81 :
82 0 : ResultSetForQuery::ResultSetForQuery( const uno::Reference< uno::XComponentContext >& rxContext,
83 : const uno::Reference< XContentProvider >& xProvider,
84 : sal_Int32 nOpenMode,
85 : const uno::Sequence< beans::Property >& seq,
86 : const uno::Sequence< NumberedSortingInfo >& seqSort,
87 : URLParameter& aURLParameter,
88 : Databases* pDatabases )
89 : : ResultSetBase( rxContext,xProvider,nOpenMode,seq,seqSort ),
90 0 : m_aURLParameter( aURLParameter )
91 : {
92 0 : Reference< XExtendedTransliteration > xTrans = Transliteration::create( rxContext );
93 : Locale aLocale( aURLParameter.get_language(),
94 : rtl::OUString(),
95 0 : rtl::OUString() );
96 0 : xTrans->loadModule(TransliterationModules_UPPERCASE_LOWERCASE,
97 0 : aLocale );
98 :
99 0 : vector< vector< rtl::OUString > > queryList;
100 : {
101 : sal_Int32 idx;
102 0 : rtl::OUString query = m_aURLParameter.get_query();
103 0 : while( !query.isEmpty() )
104 : {
105 0 : idx = query.indexOf( sal_Unicode( ' ' ) );
106 0 : if( idx == -1 )
107 0 : idx = query.getLength();
108 :
109 0 : vector< rtl::OUString > currentQuery;
110 0 : rtl::OUString tmp(query.copy( 0,idx ));
111 0 : rtl:: OUString toliterate = tmp;
112 0 : Sequence<sal_Int32> aSeq;
113 0 : toliterate = xTrans->transliterate(
114 0 : tmp,0,tmp.getLength(),aSeq);
115 :
116 0 : currentQuery.push_back( toliterate );
117 0 : queryList.push_back( currentQuery );
118 :
119 0 : int nCpy = 1 + idx;
120 0 : if( nCpy >= query.getLength() )
121 0 : query = rtl::OUString();
122 : else
123 0 : query = query.copy( 1 + idx );
124 0 : }
125 : }
126 :
127 0 : vector< rtl::OUString > aCompleteResultVector;
128 0 : rtl::OUString scope = m_aURLParameter.get_scope();
129 0 : bool bCaptionsOnly = ( scope.compareToAscii( "Heading" ) == 0 );
130 0 : sal_Int32 hitCount = m_aURLParameter.get_hitCount();
131 :
132 0 : IndexFolderIterator aIndexFolderIt( *pDatabases, m_aURLParameter.get_module(), m_aURLParameter.get_language() );
133 0 : rtl::OUString idxDir;
134 0 : bool bExtension = false;
135 0 : int iDir = 0;
136 0 : vector< vector<HitItem>* > aIndexFolderResultVectorVector;
137 :
138 : bool bTemporary;
139 0 : while( !(idxDir = aIndexFolderIt.nextIndexFolder( bExtension, bTemporary )).isEmpty() )
140 : {
141 0 : vector<HitItem> aIndexFolderResultVector;
142 :
143 : try
144 : {
145 0 : vector< vector<HitItem>* > aQueryListResultVectorVector;
146 0 : set< rtl::OUString > aSet,aCurrent,aResultSet;
147 :
148 0 : int nQueryListSize = queryList.size();
149 0 : if( nQueryListSize > 1 )
150 0 : hitCount = 2000;
151 :
152 0 : for( int i = 0; i < nQueryListSize; ++i )
153 : {
154 : vector<HitItem>* pQueryResultVector;
155 0 : if( nQueryListSize > 1 )
156 : {
157 0 : pQueryResultVector = new vector<HitItem>();
158 0 : aQueryListResultVectorVector.push_back( pQueryResultVector );
159 : }
160 : else
161 : {
162 0 : pQueryResultVector = &aIndexFolderResultVector;
163 : }
164 0 : pQueryResultVector->reserve( hitCount );
165 :
166 0 : rtl::OUString aLang = m_aURLParameter.get_language();
167 0 : const std::vector< rtl::OUString >& aListItem = queryList[i];
168 0 : ::rtl::OUString aNewQueryStr = aListItem[0];
169 :
170 0 : vector<float> aScoreVector;
171 0 : vector<rtl::OUString> aPathVector;
172 :
173 : try
174 : {
175 0 : HelpSearch searcher(aLang, idxDir);
176 0 : searcher.query(aNewQueryStr, bCaptionsOnly, aPathVector, aScoreVector);
177 : }
178 0 : catch (CLuceneError &e)
179 : {
180 : SAL_WARN("xmlhelp", "CLuceneError: " << e.what());
181 : }
182 :
183 0 : if( nQueryListSize > 1 )
184 0 : aSet.clear();
185 :
186 0 : for (unsigned j = 0; j < aPathVector.size(); ++j) {
187 0 : pQueryResultVector->push_back(HitItem(aPathVector[j], aScoreVector[j]));
188 0 : if (nQueryListSize > 1)
189 0 : aSet.insert(aPathVector[j]);
190 : }
191 :
192 : // intersect
193 0 : if( nQueryListSize > 1 )
194 : {
195 0 : if( i == 0 )
196 : {
197 0 : aResultSet = aSet;
198 : }
199 : else
200 : {
201 0 : aCurrent = aResultSet;
202 0 : aResultSet.clear();
203 : set_intersection( aSet.begin(),aSet.end(),
204 : aCurrent.begin(),aCurrent.end(),
205 0 : inserter(aResultSet,aResultSet.begin()));
206 : }
207 : }
208 0 : }
209 :
210 : // Combine results in aIndexFolderResultVector
211 0 : if( nQueryListSize > 1 )
212 : {
213 0 : for( int n = 0 ; n < nQueryListSize ; ++n )
214 : {
215 0 : vector<HitItem>* pQueryResultVector = aQueryListResultVectorVector[n];
216 0 : vector<HitItem>& rQueryResultVector = *pQueryResultVector;
217 :
218 0 : int nItemCount = rQueryResultVector.size();
219 0 : for( int i = 0 ; i < nItemCount ; ++i )
220 : {
221 0 : const HitItem& rItem = rQueryResultVector[ i ];
222 0 : if( (aResultSet.find( rItem.m_aURL )) != aResultSet.end() )
223 : {
224 0 : HitItem aItemCopy( rItem );
225 0 : aItemCopy.m_fScore /= nQueryListSize; // To get average score
226 0 : if( n == 0 )
227 : {
228 : // Use first pass to create entry
229 0 : aIndexFolderResultVector.push_back( aItemCopy );
230 : }
231 : else
232 : {
233 : // Find entry in vector
234 0 : int nCount = aIndexFolderResultVector.size();
235 0 : for( int j = 0 ; j < nCount ; ++j )
236 : {
237 0 : HitItem& rFindItem = aIndexFolderResultVector[ j ];
238 0 : if( rFindItem.m_aURL.equals( aItemCopy.m_aURL ) )
239 : {
240 0 : rFindItem.m_fScore += aItemCopy.m_fScore;
241 0 : break;
242 : }
243 : }
244 0 : }
245 : }
246 : }
247 :
248 0 : delete pQueryResultVector;
249 : }
250 :
251 0 : sort( aIndexFolderResultVector.begin(), aIndexFolderResultVector.end() );
252 : }
253 :
254 0 : vector<HitItem>* pIndexFolderHitItemVector = new vector<HitItem>( aIndexFolderResultVector );
255 0 : aIndexFolderResultVectorVector.push_back( pIndexFolderHitItemVector );
256 0 : aIndexFolderResultVector.clear();
257 : }
258 0 : catch (const Exception &e)
259 : {
260 : SAL_WARN("xmlhelp", "Exception: " << e.Message);
261 : }
262 :
263 0 : ++iDir;
264 :
265 0 : if( bTemporary )
266 0 : aIndexFolderIt.deleteTempIndexFolder( idxDir );
267 :
268 0 : } // Iterator
269 :
270 :
271 0 : int nVectorCount = aIndexFolderResultVectorVector.size();
272 0 : vector<HitItem>::size_type* pCurrentVectorIndex = new vector<HitItem>::size_type[nVectorCount];
273 0 : for( int j = 0 ; j < nVectorCount ; ++j )
274 0 : pCurrentVectorIndex[j] = 0;
275 :
276 0 : sal_Int32 nTotalHitCount = m_aURLParameter.get_hitCount();
277 0 : sal_Int32 nHitCount = 0;
278 0 : while( nHitCount < nTotalHitCount )
279 : {
280 0 : int iVectorWithBestScore = -1;
281 0 : float fBestScore = 0.0;
282 0 : for( int k = 0 ; k < nVectorCount ; ++k )
283 : {
284 0 : vector<HitItem>& rIndexFolderVector = *aIndexFolderResultVectorVector[k];
285 0 : if( pCurrentVectorIndex[k] < rIndexFolderVector.size() )
286 : {
287 0 : const HitItem& rItem = rIndexFolderVector[ pCurrentVectorIndex[k] ];
288 :
289 0 : if( fBestScore < rItem.m_fScore )
290 : {
291 0 : fBestScore = rItem.m_fScore;
292 0 : iVectorWithBestScore = k;
293 : }
294 : }
295 : }
296 :
297 0 : if( iVectorWithBestScore == -1 ) // No item left at all
298 0 : break;
299 :
300 0 : vector<HitItem>& rIndexFolderVector = *aIndexFolderResultVectorVector[iVectorWithBestScore];
301 0 : const HitItem& rItem = rIndexFolderVector[ pCurrentVectorIndex[iVectorWithBestScore] ];
302 :
303 0 : pCurrentVectorIndex[iVectorWithBestScore]++;
304 :
305 0 : aCompleteResultVector.push_back( rItem.m_aURL );
306 0 : ++nHitCount;
307 : }
308 :
309 0 : delete[] pCurrentVectorIndex;
310 0 : for( int n = 0 ; n < nVectorCount ; ++n )
311 : {
312 0 : vector<HitItem>* pIndexFolderVector = aIndexFolderResultVectorVector[n];
313 0 : delete pIndexFolderVector;
314 : }
315 :
316 0 : sal_Int32 replIdx = rtl::OUString( "#HLP#" ).getLength();
317 0 : rtl::OUString replWith = rtl::OUString( "vnd.sun.star.help://" );
318 :
319 0 : int nResultCount = aCompleteResultVector.size();
320 0 : for( int r = 0 ; r < nResultCount ; ++r )
321 : {
322 0 : rtl::OUString aURL = aCompleteResultVector[r];
323 0 : rtl::OUString aResultStr = replWith + aURL.copy(replIdx);
324 0 : m_aPath.push_back( aResultStr );
325 0 : }
326 :
327 0 : m_aItems.resize( m_aPath.size() );
328 0 : m_aIdents.resize( m_aPath.size() );
329 :
330 0 : Command aCommand;
331 0 : aCommand.Name = rtl::OUString( "getPropertyValues" );
332 0 : aCommand.Argument <<= m_sProperty;
333 :
334 0 : for( m_nRow = 0; sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aPath.size(); ++m_nRow )
335 : {
336 0 : m_aPath[m_nRow] =
337 0 : m_aPath[m_nRow] +
338 0 : rtl::OUString( "?Language=" ) +
339 0 : m_aURLParameter.get_language() +
340 0 : rtl::OUString( "&System=" ) +
341 0 : m_aURLParameter.get_system();
342 :
343 0 : uno::Reference< XContent > content = queryContent();
344 0 : if( content.is() )
345 : {
346 0 : uno::Reference< XCommandProcessor > cmd( content,uno::UNO_QUERY );
347 0 : cmd->execute( aCommand,0,uno::Reference< XCommandEnvironment >( 0 ) ) >>= m_aItems[m_nRow]; //TODO: check return value of operator >>=
348 : }
349 0 : }
350 0 : m_nRow = 0xffffffff;
351 0 : }
352 :
353 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|