1 |
|
---|
2 | #include <unistd.h>
|
---|
3 | #include <sys/dir.h>
|
---|
4 |
|
---|
5 | #include "gsdlsitecfg.h"
|
---|
6 | #include "recptprototools.h"
|
---|
7 |
|
---|
8 | #import <Foundation/NSString.h>
|
---|
9 | #import <Foundation/NSArray.h>
|
---|
10 |
|
---|
11 | #import <UIKit/UIKit.h>
|
---|
12 | #import <UIKit/UIAlertSheet.h>
|
---|
13 |
|
---|
14 | #import "GSResultItem.h"
|
---|
15 | #import "GreenstoneAPI.h"
|
---|
16 |
|
---|
17 | GreenstoneAPI::GreenstoneAPI(UIApplication* toplevelApp)
|
---|
18 | : _toplevelApp(toplevelApp), _lang("en"),
|
---|
19 | _cservers(nil), _nproto(nil), _gsdlhome()
|
---|
20 | {
|
---|
21 | _cservers = new collectset(_gsdlhome); // sets gsdlhome
|
---|
22 |
|
---|
23 | // set up the null protocol
|
---|
24 | _nproto = new nullproto();
|
---|
25 | _nproto->set_collectset(_cservers);
|
---|
26 |
|
---|
27 | comerror_t comerr;
|
---|
28 | _nproto->init(comerr,cerr);
|
---|
29 | if (comerr!=noError) {
|
---|
30 | text_t error_message = get_comerror_string (comerr);
|
---|
31 | alert("Error",error_message);
|
---|
32 | }
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | GreenstoneAPI::~GreenstoneAPI()
|
---|
38 | {
|
---|
39 | delete _cservers;
|
---|
40 | delete _nproto;
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | void GreenstoneAPI::alert(NSString* nslevel, NSString* nsmessage)
|
---|
45 | {
|
---|
46 | // Allert sheet displayed at centre of screen.
|
---|
47 |
|
---|
48 | NSArray *buttons = [NSArray arrayWithObjects:@"OK", nil];
|
---|
49 | UIAlertSheet *alertSheet = [UIAlertSheet alloc];
|
---|
50 | [alertSheet initWithTitle:nslevel buttons:buttons
|
---|
51 | defaultButtonIndex:1 delegate:_toplevelApp context:nil];
|
---|
52 |
|
---|
53 | [alertSheet setBodyText:nsmessage];
|
---|
54 | [alertSheet popupAlertAnimated:YES];
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | void GreenstoneAPI::alert(char* level, char* message)
|
---|
59 | {
|
---|
60 | // Alert sheet displayed at centre of screen.
|
---|
61 |
|
---|
62 | NSString* nslevel = [[NSString alloc] initWithCString: level];
|
---|
63 | NSString* nsmessage = [[NSString alloc] initWithCString: message];
|
---|
64 |
|
---|
65 | alert(nslevel,nsmessage);
|
---|
66 |
|
---|
67 | [nslevel release];
|
---|
68 | [nsmessage release];
|
---|
69 | }
|
---|
70 |
|
---|
71 | void GreenstoneAPI::alert(text_t level, text_t message)
|
---|
72 | {
|
---|
73 | char* level_cstr = level.getcstr();
|
---|
74 | char* message_cstr = message.getcstr();
|
---|
75 |
|
---|
76 | alert(level_cstr,message_cstr);
|
---|
77 |
|
---|
78 | delete [] level_cstr;
|
---|
79 | delete [] message_cstr;
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool GreenstoneAPI::sanityCheck()
|
---|
83 | {
|
---|
84 | text_t gsdl_home, unused_collecthome;
|
---|
85 | text_t unused_httpdomain,unused_httpprefix;
|
---|
86 | if (!site_cfg_read(gsdl_home,unused_collecthome,
|
---|
87 | unused_httpdomain,unused_httpprefix)) {
|
---|
88 | // couldn't find gsdlsite.cfg file
|
---|
89 |
|
---|
90 | char cwd[128];
|
---|
91 | getcwd(cwd,128);
|
---|
92 |
|
---|
93 | text_t sitecfg_filename = cwd;
|
---|
94 | sitecfg_filename += "gsdlsite.cfg";
|
---|
95 |
|
---|
96 | alert("Error","Unable to find " + sitecfg_filename );
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | text_t collect_dir = filename_cat(gsdl_home,"collect");
|
---|
101 | char* collect_dir_cstr = collect_dir.getcstr();
|
---|
102 |
|
---|
103 | DIR* directorypointer = opendir(collect_dir_cstr);
|
---|
104 | delete [] collect_dir_cstr;
|
---|
105 |
|
---|
106 | if(directorypointer == NULL) //If the directory could not be opened
|
---|
107 | {
|
---|
108 | text_t mess = "No collections directory. Please make one at: " + collect_dir;
|
---|
109 | alert("Error",mess);
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | NSMutableArray* GreenstoneAPI::collectionList()
|
---|
118 | {
|
---|
119 | text_tarray collection_list;
|
---|
120 | comerror_t comerr;
|
---|
121 |
|
---|
122 | _nproto->get_collection_list (collection_list, comerr, cerr);
|
---|
123 |
|
---|
124 | if (comerr != noError) {
|
---|
125 | alert("Error",get_comerror_string(comerr));
|
---|
126 | return nil;
|
---|
127 | }
|
---|
128 |
|
---|
129 | int num_cols = collection_list.size();
|
---|
130 |
|
---|
131 | NSMutableArray* nscollist = [[NSMutableArray alloc] initWithCapacity:num_cols];
|
---|
132 |
|
---|
133 | text_tarray::iterator collection_iterator = collection_list.begin();
|
---|
134 | while (collection_iterator != collection_list.end())
|
---|
135 | {
|
---|
136 | text_t& colname = *collection_iterator;
|
---|
137 | char* colname_cstr = colname.getcstr();
|
---|
138 | NSString* nscolname = [[NSString alloc ] initWithCString: colname_cstr];
|
---|
139 | delete [] colname_cstr;
|
---|
140 |
|
---|
141 | [nscollist addObject:nscolname];
|
---|
142 |
|
---|
143 | collection_iterator++;
|
---|
144 | }
|
---|
145 |
|
---|
146 | return nscollist;
|
---|
147 | }
|
---|
148 |
|
---|
149 | ColInfoResponse_t& GreenstoneAPI::collectInfo (const text_t &collection)
|
---|
150 | {
|
---|
151 | ColInfoResponse_t& collectinfo = *new ColInfoResponse_t();
|
---|
152 | comerror_t comerr;
|
---|
153 | _nproto->get_collectinfo(collection, collectinfo,comerr, cerr);
|
---|
154 |
|
---|
155 | if (comerr != noError) {
|
---|
156 | alert("Error",get_comerror_string(comerr));
|
---|
157 | }
|
---|
158 |
|
---|
159 | return collectinfo;
|
---|
160 | }
|
---|
161 |
|
---|
162 | ColInfoResponse_t& GreenstoneAPI::collectInfo (NSString* nscollection)
|
---|
163 | {
|
---|
164 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
165 |
|
---|
166 | ColInfoResponse_t& colinfo = collectInfo(nscollection_cstr);
|
---|
167 |
|
---|
168 | // according to documentation, nscollectcion_cstr autorealeased
|
---|
169 | // => don't need to free them
|
---|
170 | return colinfo;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* ****
|
---|
174 | text_t GreenstoneAPI::lookupLanguagePref()
|
---|
175 | {
|
---|
176 | NSString* nslang = [(GreenstoneApplication*)_toplevelApp languagePref];
|
---|
177 | char* lang_cstr = [nslang UTF8String];
|
---|
178 | text_t lang = lang_cstr;
|
---|
179 | [nslang release];
|
---|
180 |
|
---|
181 | return lang;
|
---|
182 | }
|
---|
183 | */
|
---|
184 |
|
---|
185 | NSString* GreenstoneAPI::collectionmetaValue(ColInfoResponse_t& colinfo,
|
---|
186 | text_t key)
|
---|
187 | {
|
---|
188 | text_t val = colinfo.get_collectionmeta(key,_lang);
|
---|
189 | char* val_cstr = val.getcstr();
|
---|
190 |
|
---|
191 | NSString* nsval = [[NSString alloc] initWithCString:val_cstr];
|
---|
192 | delete [] val_cstr;
|
---|
193 |
|
---|
194 |
|
---|
195 | return nsval;
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | NSString* GreenstoneAPI::collectionName(ColInfoResponse_t& colinfo)
|
---|
200 | {
|
---|
201 | return collectionmetaValue(colinfo,"collectionname");
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | NSString* TextTToNSString(text_t& text)
|
---|
206 | {
|
---|
207 | // Make this available to other parts of program!!!
|
---|
208 | if (text.empty()) { return nil; }
|
---|
209 |
|
---|
210 | char* text_cstr = text.getcstr();
|
---|
211 | NSString* nstext = [[NSString alloc ] initWithCString: text_cstr];
|
---|
212 | delete [] text_cstr;
|
---|
213 |
|
---|
214 | return nstext;
|
---|
215 | }
|
---|
216 |
|
---|
217 | bool GreenstoneAPI::recurse_contents(const text_t& collection, ResultDocInfo_t& section,
|
---|
218 | text_tset& metadata, text_t& output_text)
|
---|
219 | {
|
---|
220 | // Output this section
|
---|
221 |
|
---|
222 | // 1. Get the title
|
---|
223 |
|
---|
224 | text_t title = section.metadata["Title"].values[0];
|
---|
225 | output_text.append("<h3>"+title+"</h3>");
|
---|
226 |
|
---|
227 | // cerr << "**** Title = " << title << endl;
|
---|
228 |
|
---|
229 | // 2. Get the text
|
---|
230 | DocumentRequest_t docrequest;
|
---|
231 | DocumentResponse_t docresponse;
|
---|
232 | comerror_t comerr;
|
---|
233 | docrequest.OID = section.OID;
|
---|
234 |
|
---|
235 | _nproto->get_document(collection, docrequest, docresponse, comerr, cerr);
|
---|
236 |
|
---|
237 | if (comerr != noError) {
|
---|
238 | alert("Error",get_comerror_string(comerr));
|
---|
239 | return false;
|
---|
240 | }
|
---|
241 |
|
---|
242 | output_text.append(docresponse.doc);
|
---|
243 |
|
---|
244 | // cerr << "*** appending text " << docresponse.doc << endl;
|
---|
245 |
|
---|
246 | int haschildren = section.metadata["haschildren"].values[0].getint();
|
---|
247 |
|
---|
248 | // recurse through children
|
---|
249 | if (haschildren == 1)
|
---|
250 | {
|
---|
251 | bool getParents = false;
|
---|
252 | FilterResponse_t tmp;
|
---|
253 |
|
---|
254 | get_children (section.OID, collection, _lang, metadata, getParents, _nproto, tmp, cerr);
|
---|
255 | ResultDocInfo_tarray::iterator thisdoc = tmp.docInfo.begin();
|
---|
256 | ResultDocInfo_tarray::iterator lastdoc = tmp.docInfo.end();
|
---|
257 |
|
---|
258 | while (thisdoc != lastdoc)
|
---|
259 | {
|
---|
260 | bool recurse_ok = recurse_contents(collection,*thisdoc, metadata, output_text);
|
---|
261 | if (!recurse_ok) {
|
---|
262 | return false;
|
---|
263 | }
|
---|
264 | ++thisdoc;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | return true;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | NSString* GreenstoneAPI::getResolvedDocument(NSString* nscollection, NSString* nsgid)
|
---|
273 | {
|
---|
274 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
275 | char const* nsgid_cstr = [nsgid UTF8String];
|
---|
276 |
|
---|
277 | text_t collection(nscollection_cstr);
|
---|
278 | text_t gid(nsgid_cstr);
|
---|
279 |
|
---|
280 | //metadata elements needed by recurse_contents
|
---|
281 | text_tset metadata;
|
---|
282 | metadata.insert ("Title");
|
---|
283 | metadata.insert ("haschildren");
|
---|
284 |
|
---|
285 | bool getParents = false;
|
---|
286 |
|
---|
287 | FilterResponse_t inforesponse;
|
---|
288 | comerror_t comerr;
|
---|
289 |
|
---|
290 | NSString* nsresolvedtext = nil;
|
---|
291 |
|
---|
292 | if (get_info(gid, collection, _lang, metadata, getParents, _nproto, inforesponse, cerr))
|
---|
293 | {
|
---|
294 | text_t output_text;
|
---|
295 | text_t make_utf8 = " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n";
|
---|
296 |
|
---|
297 | output_text.append("<html>\n");
|
---|
298 | output_text.append(" <head>\n"+make_utf8+" </head>\n");
|
---|
299 |
|
---|
300 | if (recurse_contents(collection,inforesponse.docInfo[0], metadata, output_text)) {
|
---|
301 |
|
---|
302 | // cerr << "**** text = " << output_text << endl;
|
---|
303 |
|
---|
304 | // retrieve [archivedir] for this document id
|
---|
305 | NSString* nsarchivedir = getMetadataFirst(nscollection,nsgid,@"archivedir");
|
---|
306 | const char* nsarchivedir_cstr = [nsarchivedir UTF8String];
|
---|
307 | text_t archivedir = nsarchivedir_cstr;
|
---|
308 | [nsarchivedir release];
|
---|
309 |
|
---|
310 | text_t resolved_text = resolveUrls(output_text,collection,archivedir);
|
---|
311 |
|
---|
312 | char* resolved_text_cstr = resolved_text.getcstr();
|
---|
313 | nsresolvedtext = [[NSString alloc] initWithCString:resolved_text_cstr];
|
---|
314 |
|
---|
315 | delete [] resolved_text_cstr;
|
---|
316 | }
|
---|
317 | else {
|
---|
318 | alert("Error","Failed to get info for " + gid);
|
---|
319 | }
|
---|
320 | output_text.append("</html>\n");
|
---|
321 | }
|
---|
322 | else {
|
---|
323 | alert("Error","Failed to get info for " + gid);
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | return nsresolvedtext;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | NSMutableArray* GreenstoneAPI::getMetadata(NSString* nscollection, NSString* nsgid, NSString* nsmetaname)
|
---|
332 | {
|
---|
333 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
334 | char const* nsgid_cstr = [nsgid UTF8String];
|
---|
335 | char const* nsmetaname_cstr = [nsmetaname UTF8String];
|
---|
336 |
|
---|
337 | NSMutableArray* nsmdlist = nil;
|
---|
338 |
|
---|
339 | text_t collection(nscollection_cstr);
|
---|
340 | text_t gid(nsgid_cstr);
|
---|
341 | text_t metaname(nsmetaname_cstr);
|
---|
342 |
|
---|
343 | text_tset metadata;
|
---|
344 | metadata.insert (metaname);
|
---|
345 | bool getParents = false;
|
---|
346 | FilterResponse_t response;
|
---|
347 |
|
---|
348 | if (get_info (gid, collection, _lang, metadata, getParents, _nproto, response, cerr)) {
|
---|
349 | int num_browse_by = response.docInfo.size();
|
---|
350 | if (num_browse_by>0) {
|
---|
351 | MetadataInfo_tmap& metadata = response.docInfo[0].metadata;
|
---|
352 | MetadataInfo_t& metaname_info = metadata[nsmetaname_cstr];
|
---|
353 | int num_browse_by = metaname_info.values.size();
|
---|
354 | if (num_browse_by>0) {
|
---|
355 | // protocol will return 1 even if there is no metadata
|
---|
356 | // values will be the empty string
|
---|
357 |
|
---|
358 | nsmdlist = [[NSMutableArray alloc] initWithCapacity:num_browse_by];
|
---|
359 |
|
---|
360 | text_tarray& meta_val = metaname_info.values;
|
---|
361 | text_tarray::iterator md_iterator = meta_val.begin();
|
---|
362 | while (md_iterator != meta_val.end()) {
|
---|
363 | text_t& mdval = *md_iterator;
|
---|
364 |
|
---|
365 | NSString* nsmdval = TextTToNSString(mdval);
|
---|
366 | if (nsmdval != nil) {
|
---|
367 | [nsmdlist addObject:nsmdval];
|
---|
368 | }
|
---|
369 |
|
---|
370 | md_iterator++;
|
---|
371 | }
|
---|
372 |
|
---|
373 | if ([nsmdlist count] == 0) {
|
---|
374 | // no valid metadata => free list and set to nil
|
---|
375 | [nsmdlist release];
|
---|
376 | nsmdlist = nil;
|
---|
377 | }
|
---|
378 | }
|
---|
379 | else {
|
---|
380 | alert("Error","OID" + gid + "does not have metadata for" + metaname);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | else {
|
---|
384 | alert("Error","OID" + gid + "does not have metadata for" + metaname);
|
---|
385 | }
|
---|
386 | }
|
---|
387 | else {
|
---|
388 | alert("Error","Collection has no browsing capabilities" + gid);
|
---|
389 | }
|
---|
390 |
|
---|
391 | // according to documentation, nscollectcion_cstr, nsgid_cstr and nsmetaname_cstr autorealeased
|
---|
392 | // => don't need to free them
|
---|
393 |
|
---|
394 | return nsmdlist;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | NSString* GreenstoneAPI::getMetadataFirst(NSString* nscollection, NSString* nsgid, NSString* nsmetaname)
|
---|
399 | {
|
---|
400 | NSMutableArray* all_metavalues = getMetadata(nscollection,nsgid,nsmetaname);
|
---|
401 |
|
---|
402 | NSString* first_metavalue_copy = nil;
|
---|
403 |
|
---|
404 | if (all_metavalues != nil) {
|
---|
405 | NSString* first_metavalue = [all_metavalues objectAtIndex:0];
|
---|
406 |
|
---|
407 | first_metavalue_copy = [[NSString alloc] initWithString:first_metavalue];
|
---|
408 |
|
---|
409 | [all_metavalues release];
|
---|
410 | }
|
---|
411 |
|
---|
412 | return first_metavalue_copy;
|
---|
413 | }
|
---|
414 |
|
---|
415 | NSString* GreenstoneAPI::getMetadataFirst(const text_t& collection, text_t& gid, text_t& metaname)
|
---|
416 | {
|
---|
417 | char* collection_cstr = collection.getcstr();
|
---|
418 | char* gid_cstr = gid.getcstr();
|
---|
419 | char* metaname_cstr = metaname.getcstr();
|
---|
420 |
|
---|
421 | NSString* nscollection = [[NSString alloc] initWithCString:collection_cstr];
|
---|
422 | NSString* nsgid = [[NSString alloc] initWithCString:gid_cstr];
|
---|
423 | NSString* nsmetaname = [[NSString alloc] initWithCString:metaname_cstr];
|
---|
424 |
|
---|
425 | NSString* result = getMetadataFirst(nscollection,nsgid,nsmetaname);
|
---|
426 |
|
---|
427 | delete [] collection_cstr;
|
---|
428 | delete [] gid_cstr;
|
---|
429 | delete [] metaname_cstr;
|
---|
430 |
|
---|
431 | return result;
|
---|
432 |
|
---|
433 | }
|
---|
434 |
|
---|
435 | NSMutableArray* GreenstoneAPI::browseClassifierList(NSString* nscollection, NSString* nsgid)
|
---|
436 | {
|
---|
437 | // **** upgrade this to use getMetadata, and then split on ';'
|
---|
438 |
|
---|
439 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
440 | char const* nsgid_cstr = [nsgid UTF8String];
|
---|
441 |
|
---|
442 | text_t gid = nsgid_cstr;
|
---|
443 |
|
---|
444 | NSMutableArray* nscllist = nil;
|
---|
445 |
|
---|
446 | text_tset metadata;
|
---|
447 | metadata.insert ("contains");
|
---|
448 | FilterResponse_t response;
|
---|
449 | comerror_t comerr;
|
---|
450 |
|
---|
451 | if (get_info (nsgid_cstr, nscollection_cstr, _lang, metadata, false, _nproto, response, cerr)) {
|
---|
452 | int num_browse_by = response.docInfo.size();
|
---|
453 | if (num_browse_by >0) {
|
---|
454 | MetadataInfo_tmap& metadata = response.docInfo[0].metadata;
|
---|
455 | MetadataInfo_t& contains = metadata["contains"];
|
---|
456 | int num_browse_by = contains.values.size();
|
---|
457 | if (num_browse_by>0) {
|
---|
458 |
|
---|
459 | // nscllist = [[NSMutableArray alloc] initWithCapacity:num_browse_by]; ****
|
---|
460 | nscllist = [[NSMutableArray alloc] initWithCapacity:50];
|
---|
461 |
|
---|
462 | text_tarray& contains_val = contains.values;
|
---|
463 | text_tarray::iterator cl_iterator = contains_val.begin();
|
---|
464 | while (cl_iterator != contains_val.end()) {
|
---|
465 | text_t& clname = *cl_iterator;
|
---|
466 |
|
---|
467 | // split up on ';'
|
---|
468 | text_tarray split_clname;
|
---|
469 | splitchar (clname.begin(), clname.end(), ';', split_clname);
|
---|
470 |
|
---|
471 | text_tarray::iterator split_cl_iterator = split_clname.begin();
|
---|
472 | while (split_cl_iterator != split_clname.end()) {
|
---|
473 | text_t& cl = *split_cl_iterator;
|
---|
474 | NSString* nscl = nil;
|
---|
475 | if (cl[0] == '\"') {
|
---|
476 | text_t cl_trim = substr(cl.begin()+1,cl.end());
|
---|
477 | nscl = TextTToNSString(cl_trim);
|
---|
478 | }
|
---|
479 | else {
|
---|
480 | nscl = TextTToNSString(cl);
|
---|
481 | }
|
---|
482 |
|
---|
483 | if (nscl != nil) {
|
---|
484 | [nscllist addObject:nscl];
|
---|
485 | }
|
---|
486 | else {
|
---|
487 | cerr << "Warning: no semicolon split classifier 'contains' metadata value found for " + gid << endl;
|
---|
488 | }
|
---|
489 |
|
---|
490 | split_cl_iterator++;
|
---|
491 | }
|
---|
492 | cl_iterator++;
|
---|
493 | }
|
---|
494 |
|
---|
495 | if ([nscllist count]==0) {
|
---|
496 | // No valid metadata found
|
---|
497 | [nscllist release];
|
---|
498 | nscllist = nil;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | else {
|
---|
502 | alert("Error","Collection has to browsing capabilities");
|
---|
503 | }
|
---|
504 | }
|
---|
505 | else {
|
---|
506 | alert("Error","Collection has to browsing capabilities");
|
---|
507 | }
|
---|
508 | }
|
---|
509 | else {
|
---|
510 | alert("Error",get_comerror_string(comerr));
|
---|
511 | }
|
---|
512 |
|
---|
513 | // according to documentation, nscollectcion_cstr and nsgid_cstr autorealeased
|
---|
514 | // => don't need to free them
|
---|
515 |
|
---|
516 | return nscllist;
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 | NSMutableArray* GreenstoneAPI::browseToplevelList(NSString* nscollection)
|
---|
521 | {
|
---|
522 | return browseClassifierList(nscollection,@"browse");
|
---|
523 | }
|
---|
524 |
|
---|
525 |
|
---|
526 |
|
---|
527 |
|
---|
528 | void GreenstoneAPI::set_queryfilter_options (const text_t& collection,
|
---|
529 | FilterRequest_t &request,
|
---|
530 | const text_t &querystring)
|
---|
531 | {
|
---|
532 | ColInfoResponse_t& collectinfo = collectInfo(collection);
|
---|
533 |
|
---|
534 | // request.filterResultOptions and request.fields (if required) should
|
---|
535 | // be set from the calling code
|
---|
536 |
|
---|
537 | request.filterName = "QueryFilter";
|
---|
538 |
|
---|
539 | OptionValue_t option;
|
---|
540 |
|
---|
541 | option.name = "Term";
|
---|
542 | option.value = querystring;
|
---|
543 | request.filterOptions.push_back (option);
|
---|
544 |
|
---|
545 | option.name = "QueryType";
|
---|
546 | option.value = "ranked";
|
---|
547 | request.filterOptions.push_back (option);
|
---|
548 |
|
---|
549 | option.name = "MatchMode";
|
---|
550 | option.value = "some";
|
---|
551 | request.filterOptions.push_back (option);
|
---|
552 |
|
---|
553 | option.name = "Casefold";
|
---|
554 | option.value = "true";
|
---|
555 | request.filterOptions.push_back (option);
|
---|
556 |
|
---|
557 | option.name = "Stem";
|
---|
558 | option.value = "true";
|
---|
559 | request.filterOptions.push_back (option);
|
---|
560 |
|
---|
561 | option.name = "AccentFold";
|
---|
562 | option.value = "true";
|
---|
563 | request.filterOptions.push_back (option);
|
---|
564 |
|
---|
565 | text_t buildtype = collectinfo.buildType;
|
---|
566 |
|
---|
567 | if (buildtype == "lucene") {
|
---|
568 | // Should prevent Lucene collection from doing any searching!! // ****
|
---|
569 | alert("Warning","Searching lucene indexed collection on iPod not currently supported");
|
---|
570 | }
|
---|
571 | else if (buildtype == "mgpp") {
|
---|
572 | option.name = "Level";
|
---|
573 | option.value = "Doc";
|
---|
574 | request.filterOptions.push_back (option);
|
---|
575 |
|
---|
576 | option.name = "Index";
|
---|
577 | option.value = "idx"; // ****
|
---|
578 | request.filterOptions.push_back (option);
|
---|
579 | }
|
---|
580 | else {
|
---|
581 | cerr << "**** storing index as dte" << endl;
|
---|
582 | option.name = "Index";
|
---|
583 | option.value = "dte"; // ****
|
---|
584 | request.filterOptions.push_back (option);
|
---|
585 | }
|
---|
586 |
|
---|
587 | option.name = "Maxdocs";
|
---|
588 | option.value = "50";
|
---|
589 | request.filterOptions.push_back (option);
|
---|
590 |
|
---|
591 | option.name = "StartResults";
|
---|
592 | option.value = "1";
|
---|
593 | request.filterOptions.push_back (option);
|
---|
594 |
|
---|
595 | option.name = "EndResults";
|
---|
596 | option.value = "50";
|
---|
597 | request.filterOptions.push_back (option);
|
---|
598 |
|
---|
599 |
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | text_t GreenstoneAPI::resolveUrls(const text_t& unresolved, const text_t& collection, const text_t& archivedir)
|
---|
604 | {
|
---|
605 | text_t resolved = unresolved;
|
---|
606 |
|
---|
607 | text_t httpprefix = _gsdlhome;
|
---|
608 | text_t httpdocimg = httpprefix +"/collect/" + collection + "/index/assoc/" + archivedir;
|
---|
609 |
|
---|
610 | resolved.replace("[archivedir]",archivedir);
|
---|
611 | resolved.replace("[assocfilepath]",archivedir);
|
---|
612 | resolved.replace("[collection]",collection);
|
---|
613 | resolved.replace("_httpprefix_",httpprefix);
|
---|
614 | resolved.replace("_httpdocimg_",httpdocimg);
|
---|
615 |
|
---|
616 | return resolved;
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 | text_t GreenstoneAPI::srclinkToUrl(const text_t& srclink, const text_t& collection, const text_t& archivedir)
|
---|
621 | {
|
---|
622 | text_t srcurl;
|
---|
623 |
|
---|
624 | if (!srclink.empty()) {
|
---|
625 | // extract URL
|
---|
626 | text_tarray split_srclink;
|
---|
627 | splitchar (srclink.begin(), srclink.end(), '\"', split_srclink);
|
---|
628 | if (split_srclink.size()>=2) {
|
---|
629 | srcurl = resolveUrls(split_srclink[1],collection,archivedir);
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 | return srcurl;
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | NSString* GreenstoneAPI::getSrcUrlMetadata(NSString* nscollection, NSString* nsgid)
|
---|
638 | {
|
---|
639 | NSString* nssrclink = getMetadataFirst(nscollection,nsgid,@"srclink");
|
---|
640 | if (nssrclink == nil) {
|
---|
641 | return nil;
|
---|
642 | }
|
---|
643 | NSString* nsarchivedir = getMetadataFirst(nscollection,nsgid,@"archivedir");
|
---|
644 | //NSString* nsassocfilepath = getMetadataFirst(nscollection,nsgid,@"assocfilepath"); // ****** used?
|
---|
645 |
|
---|
646 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
647 | char const* nssrclink_cstr = [nssrclink UTF8String];
|
---|
648 | char const* nsarchivedir_cstr = [nsarchivedir UTF8String];
|
---|
649 |
|
---|
650 | text_t collection = nscollection_cstr;
|
---|
651 | text_t srclink = nssrclink_cstr;
|
---|
652 | text_t archivedir = nsarchivedir_cstr;
|
---|
653 |
|
---|
654 | text_t srcurl = srclinkToUrl(srclink,collection,archivedir);
|
---|
655 |
|
---|
656 | NSString* nsimage = getMetadataFirst(nscollection,nsgid,@"Image");
|
---|
657 | char const* image_cstr = [nsimage UTF8String];
|
---|
658 | srcurl.replace("[Image]",image_cstr);
|
---|
659 |
|
---|
660 | NSString* nssrcurl = TextTToNSString(srcurl);
|
---|
661 |
|
---|
662 | return nssrcurl;
|
---|
663 | }
|
---|
664 |
|
---|
665 |
|
---|
666 |
|
---|
667 |
|
---|
668 | NSMutableArray* GreenstoneAPI::filterResponseToTitlesList(const text_t& collection, FilterResponse_t& sections)
|
---|
669 | {
|
---|
670 | ResultDocInfo_tarray::iterator thissection = sections.docInfo.begin();
|
---|
671 | ResultDocInfo_tarray::iterator endsection = sections.docInfo.end();
|
---|
672 |
|
---|
673 | int num_results = sections.docInfo.size();
|
---|
674 | NSMutableArray* nsresultlist = nil;
|
---|
675 |
|
---|
676 | if (num_results>0) {
|
---|
677 | nsresultlist = [[NSMutableArray alloc] initWithCapacity:num_results];
|
---|
678 |
|
---|
679 | while (thissection != endsection) {
|
---|
680 |
|
---|
681 | ResultDocInfo_t &docinfo = *thissection;
|
---|
682 | text_t& oid = docinfo.OID;
|
---|
683 | text_t title;
|
---|
684 | text_t srcurl;
|
---|
685 |
|
---|
686 | MetadataInfo_tmap& metadata = docinfo.metadata;
|
---|
687 | MetadataInfo_t& dctitle_info = metadata["dc.Title"];
|
---|
688 | MetadataInfo_t& title_info = metadata["Title"];
|
---|
689 | if ((dctitle_info.values.size()>0) && (!dctitle_info.values[0].empty())) {
|
---|
690 | title = dctitle_info.values[0];
|
---|
691 | }
|
---|
692 | else if ((title_info.values.size()>0) && (!title_info.values[0].empty())) {
|
---|
693 | title = title_info.values[0];
|
---|
694 | }
|
---|
695 | else {
|
---|
696 | cerr << "Warning: could not find Title metadata for item" << endl;
|
---|
697 | }
|
---|
698 |
|
---|
699 | MetadataInfo_t& archivedir_info = metadata["archivedir"];
|
---|
700 | text_t archivedir = archivedir_info.values[0];
|
---|
701 |
|
---|
702 | MetadataInfo_t& srclink_info = metadata["srclink"];
|
---|
703 | if (srclink_info.values.size()>0) {
|
---|
704 | text_t& srclink = srclink_info.values[0];
|
---|
705 |
|
---|
706 | srcurl = srclinkToUrl(srclink,collection,archivedir);
|
---|
707 |
|
---|
708 |
|
---|
709 | // ** think about embedding in routine?
|
---|
710 | text_t imagemdname = "Image";
|
---|
711 | NSString* nsimage = getMetadataFirst(collection,oid,imagemdname);
|
---|
712 | //char const* image_cstr = [nsimage UTF8String];
|
---|
713 | //text_t image = image_cstr;
|
---|
714 | //srcurl.replace("[Image]",image);
|
---|
715 |
|
---|
716 |
|
---|
717 | }
|
---|
718 |
|
---|
719 | NSString* nstitle = TextTToNSString(title);
|
---|
720 | NSString* nsoid = TextTToNSString(oid);
|
---|
721 | NSString* nssrcurl = TextTToNSString(srcurl);
|
---|
722 |
|
---|
723 | GSResultItem* result_item = [[GSResultItem alloc] initWithOID:nsoid withTitle:nstitle withSrcUrl:nssrcurl];
|
---|
724 |
|
---|
725 | [nsresultlist addObject:result_item];
|
---|
726 |
|
---|
727 | ++thissection;
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | return nsresultlist;
|
---|
732 | }
|
---|
733 |
|
---|
734 | NSMutableArray* GreenstoneAPI::search_single_collection (const text_t& collection, const text_t& query)
|
---|
735 | {
|
---|
736 | comerror_t comerr;
|
---|
737 | FilterRequest_t request;
|
---|
738 | FilterResponse_t response;
|
---|
739 |
|
---|
740 | NSMutableArray* nsresultlist = nil;
|
---|
741 |
|
---|
742 | // request.fields = metadata set
|
---|
743 | request.fields.insert ("dc.Title");
|
---|
744 | request.fields.insert ("Title");
|
---|
745 | request.fields.insert ("srclink");
|
---|
746 | request.fields.insert ("archivedir");
|
---|
747 |
|
---|
748 | // do the query
|
---|
749 | request.filterResultOptions = FROID | FRmetadata | FRtermFreq;
|
---|
750 | text_t formattedstring = query;
|
---|
751 |
|
---|
752 | // note! formattedstring is in unicode! mg and mgpp must convert!
|
---|
753 | set_queryfilter_options (collection, request, formattedstring);
|
---|
754 |
|
---|
755 | _nproto->filter (collection, request, response, comerr, cerr);
|
---|
756 |
|
---|
757 | if (comerr != noError) {
|
---|
758 | alert("Error",get_comerror_string(comerr));
|
---|
759 | return nil;
|
---|
760 | }
|
---|
761 | else {
|
---|
762 | nsresultlist = filterResponseToTitlesList(collection,response);
|
---|
763 | }
|
---|
764 |
|
---|
765 | return nsresultlist;
|
---|
766 | }
|
---|
767 |
|
---|
768 |
|
---|
769 |
|
---|
770 | NSMutableArray* GreenstoneAPI::query(NSString* nscollection, NSString* nsquery)
|
---|
771 | {
|
---|
772 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
773 | char const* nsquery_cstr = [nsquery UTF8String];
|
---|
774 |
|
---|
775 | text_t collection = nscollection_cstr;
|
---|
776 | text_t query = nsquery_cstr;
|
---|
777 |
|
---|
778 | NSMutableArray* nsresultlist = search_single_collection(collection,query);
|
---|
779 |
|
---|
780 | return nsresultlist;
|
---|
781 | }
|
---|
782 |
|
---|
783 | NSString* GreenstoneAPI::getDocument(NSString* nscollection, NSString* nsgid)
|
---|
784 | {
|
---|
785 | char const* nscollection_cstr = [nscollection UTF8String];
|
---|
786 | char const* nsgid_cstr = [nsgid UTF8String];
|
---|
787 |
|
---|
788 | text_t gid = nsgid_cstr;
|
---|
789 | text_t collection = nscollection_cstr;
|
---|
790 |
|
---|
791 | DocumentRequest_t docrequest;
|
---|
792 | DocumentResponse_t docresponse;
|
---|
793 | comerror_t comerr;
|
---|
794 |
|
---|
795 | // get the text
|
---|
796 | docrequest.OID = gid;
|
---|
797 | _nproto->get_document(collection, docrequest, docresponse, comerr, cerr);
|
---|
798 |
|
---|
799 | if (comerr != noError) {
|
---|
800 | alert("Error",get_comerror_string(comerr));
|
---|
801 | return nil;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | text_t& text = docresponse.doc;
|
---|
806 | char* text_cstr = text.getcstr();
|
---|
807 | NSString* nstext = [[NSString alloc] initWithCString:text_cstr];
|
---|
808 |
|
---|
809 | delete [] text_cstr;
|
---|
810 |
|
---|
811 | return nstext;
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 |
|
---|
816 |
|
---|
817 |
|
---|
818 |
|
---|