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