1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * dynamicclassifieraction.cpp --
|
---|
4 | * Copyright (C) 2008 DL Consulting Ltd
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | #include "dynamicclassifieraction.h"
|
---|
27 | #include "recptprototools.h"
|
---|
28 |
|
---|
29 |
|
---|
30 | dynamicclassifieraction::dynamicclassifieraction ()
|
---|
31 | {
|
---|
32 | recpt = NULL;
|
---|
33 |
|
---|
34 | cgiarginfo arg_ainfo;
|
---|
35 | arg_ainfo.shortname = "dcl";
|
---|
36 | arg_ainfo.longname = "dynamic classifier ID";
|
---|
37 | arg_ainfo.multiplechar = true;
|
---|
38 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
39 | arg_ainfo.argdefault = "";
|
---|
40 | arg_ainfo.savedarginfo = cgiarginfo::must;
|
---|
41 | argsinfo.addarginfo (NULL, arg_ainfo);
|
---|
42 |
|
---|
43 | arg_ainfo.shortname = "dcn";
|
---|
44 | arg_ainfo.longname = "dynamic classifier node";
|
---|
45 | arg_ainfo.multiplechar = true;
|
---|
46 | arg_ainfo.defaultstatus = cgiarginfo::weak;
|
---|
47 | arg_ainfo.argdefault = "";
|
---|
48 | arg_ainfo.savedarginfo = cgiarginfo::must;
|
---|
49 | argsinfo.addarginfo (NULL, arg_ainfo);
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | dynamicclassifieraction::~dynamicclassifieraction()
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | bool dynamicclassifieraction::check_cgiargs (cgiargsinfoclass &argsinfo, cgiargsclass &args,
|
---|
59 | recptprotolistclass *protos, ostream &logout)
|
---|
60 | {
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | void dynamicclassifieraction::get_cgihead_info (cgiargsclass &args, recptprotolistclass *protos,
|
---|
66 | response_t &response,text_t &response_data,
|
---|
67 | ostream &logout)
|
---|
68 | {
|
---|
69 | response = content;
|
---|
70 | response_data = "text/html";
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | // define all the macros which might be used by other actions to produce pages.
|
---|
75 | void dynamicclassifieraction::define_external_macros (displayclass &disp, cgiargsclass &args,
|
---|
76 | recptprotolistclass *protos, ostream &logout)
|
---|
77 | {
|
---|
78 | // A valid collection server is vital
|
---|
79 | recptproto *collectproto = protos->getrecptproto (args["c"], logout);
|
---|
80 | if (collectproto == NULL)
|
---|
81 | {
|
---|
82 | logout << "dynamicclassifieraction::define_external_macros called with NULL collectproto\n";
|
---|
83 | return;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // Define _dynamicclassifiernavbarentries_ to add buttons to the navigation bar for the dynamic classifiers
|
---|
87 | text_t navigation_bar_entries = "";
|
---|
88 | ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, args["c"], logout);
|
---|
89 | text_tmap::iterator dynamic_classifier_iterator = cinfo->dynamic_classifiers.begin();
|
---|
90 | while (dynamic_classifier_iterator != cinfo->dynamic_classifiers.end())
|
---|
91 | {
|
---|
92 | text_t dynamic_classifier_id = (*dynamic_classifier_iterator).first;
|
---|
93 | navigation_bar_entries += "_navbarspacer_";
|
---|
94 | navigation_bar_entries += "_navtab_(_gwcgi_?c=" + args["c"] + "&a=dc&dcl=" + dynamic_classifier_id + "," + dynamic_classifier_id;
|
---|
95 | if (args["a"] == "dc" && args["dcl"] == dynamic_classifier_id)
|
---|
96 | {
|
---|
97 | navigation_bar_entries += ",selected";
|
---|
98 | }
|
---|
99 | navigation_bar_entries += ")";
|
---|
100 | dynamic_classifier_iterator++;
|
---|
101 | }
|
---|
102 |
|
---|
103 | disp.setmacro("dynamicclassifiernavbarentries", displayclass::defaultpackage, navigation_bar_entries);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | // define all the macros which are related to pages generated
|
---|
108 | // by this action. we also load up the formatinfo structure
|
---|
109 | // here (it's used in do_action as well as here)
|
---|
110 | void dynamicclassifieraction::define_internal_macros (displayclass &disp, cgiargsclass &args,
|
---|
111 | recptprotolistclass *protos, ostream &logout)
|
---|
112 | {
|
---|
113 | // define_internal_macros sets the following macros:
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | bool dynamicclassifieraction::do_action(cgiargsclass &args, recptprotolistclass *protos,
|
---|
118 | browsermapclass *browsers, displayclass &disp,
|
---|
119 | outconvertclass &outconvert, ostream &textout,
|
---|
120 | ostream &logout)
|
---|
121 | {
|
---|
122 | // A valid collection server is vital
|
---|
123 | recptproto *collectproto = protos->getrecptproto (args["c"], logout);
|
---|
124 | if (collectproto == NULL)
|
---|
125 | {
|
---|
126 | logout << "dynamicclassifieraction::do_action called with NULL collectproto\n";
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 |
|
---|
130 | textout << outconvert << disp << "_dynamicclassifier:header_\n";
|
---|
131 | textout << outconvert << disp << "_dynamicclassifier:content_\n";
|
---|
132 |
|
---|
133 | // Check a dynamic classifier ID has been specified
|
---|
134 | text_t arg_dcl = args["dcl"];
|
---|
135 | if (arg_dcl.empty())
|
---|
136 | {
|
---|
137 | textout << outconvert << disp << "Error: Missing dcl argument.\n";
|
---|
138 | textout << outconvert << disp << "_dynamicclassifier:footer_\n";
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Check the dynamic classifier ID is valid (ie. there is an entry in the collect.cfg file for it)
|
---|
143 | ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, args["c"], logout);
|
---|
144 | if (cinfo->dynamic_classifiers.find(arg_dcl) == cinfo->dynamic_classifiers.end())
|
---|
145 | {
|
---|
146 | textout << outconvert << disp << "Error: Invalid dcl value \"" << arg_dcl << "\".\n";
|
---|
147 | textout << outconvert << disp << "_dynamicclassifier:footer_\n";
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // Output the "<ID>Header" format statement if there is one
|
---|
152 | text_t classifier_header_format_statement = "";
|
---|
153 | get_formatstring(arg_dcl + "Header", cinfo->format, classifier_header_format_statement);
|
---|
154 | textout << outconvert << disp << classifier_header_format_statement << "\n";
|
---|
155 |
|
---|
156 | // Output the dynamic classifier
|
---|
157 | text_t metadata_element_name = cinfo->dynamic_classifiers[arg_dcl];
|
---|
158 | if (args["dcn"].empty())
|
---|
159 | {
|
---|
160 | // Simple case for the top-level page
|
---|
161 | output_top_level_page (metadata_element_name, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | // More complex case for an internal page
|
---|
166 | output_internal_page (metadata_element_name, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
167 | }
|
---|
168 |
|
---|
169 | // Output the "<ID>Footer" format statement if there is one
|
---|
170 | text_t classifier_footer_format_statement = "";
|
---|
171 | get_formatstring(arg_dcl + "Footer", cinfo->format, classifier_footer_format_statement);
|
---|
172 | textout << outconvert << disp << classifier_footer_format_statement << "\n";
|
---|
173 |
|
---|
174 | textout << outconvert << disp << "_dynamicclassifier:footer_\n";
|
---|
175 | return true;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void dynamicclassifieraction::output_top_level_page (text_t metadata_element_name, cgiargsclass &args,
|
---|
180 | recptproto *collectproto, browsermapclass *browsers,
|
---|
181 | displayclass &disp, outconvertclass &outconvert,
|
---|
182 | ostream &textout, ostream &logout)
|
---|
183 | {
|
---|
184 | // Get all the metadata values for the specified element (these become the classifier nodes at the top level)
|
---|
185 | FilterResponse_t metadata_values_response;
|
---|
186 | get_metadata_values (metadata_element_name, "", args["c"], collectproto, metadata_values_response, logout);
|
---|
187 |
|
---|
188 | // Deal with any hierarchical metadata values
|
---|
189 | map<text_t, int, lttext_t> classifier_nodes;
|
---|
190 | ResultDocInfo_tarray::iterator metadata_value_iterator = metadata_values_response.docInfo.begin();
|
---|
191 | while (metadata_value_iterator != metadata_values_response.docInfo.end())
|
---|
192 | {
|
---|
193 | // Is this metadata value hierarchical?
|
---|
194 | text_t metadata_value = (*metadata_value_iterator).OID;
|
---|
195 | text_t::iterator hierarchy_split_position = findchar(metadata_value.begin(), metadata_value.end(), '|');
|
---|
196 | if (hierarchy_split_position != metadata_value.end())
|
---|
197 | {
|
---|
198 | // Yes, so use the first part of the hierarchy only
|
---|
199 | metadata_value = substr(metadata_value.begin(), hierarchy_split_position);
|
---|
200 | }
|
---|
201 |
|
---|
202 | // Create a node for this metadata value if we haven't seen it before
|
---|
203 | if (classifier_nodes.find(metadata_value) == classifier_nodes.end())
|
---|
204 | {
|
---|
205 | classifier_nodes[metadata_value] = 0;
|
---|
206 | }
|
---|
207 |
|
---|
208 | // Increment the occurrence count
|
---|
209 | classifier_nodes[metadata_value] += (*metadata_value_iterator).result_num;
|
---|
210 |
|
---|
211 | metadata_value_iterator++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | // Display the top-level classifier nodes
|
---|
215 | map<text_t, int, lttext_t>::iterator classifier_nodes_iterator = classifier_nodes.begin();
|
---|
216 | while (classifier_nodes_iterator != classifier_nodes.end())
|
---|
217 | {
|
---|
218 | text_t classifier_node_OID = (*classifier_nodes_iterator).first;
|
---|
219 | text_t classifier_node_label = (*classifier_nodes_iterator).first;
|
---|
220 | int classifier_node_numleafdocs = (*classifier_nodes_iterator).second;
|
---|
221 | output_classifier_node (classifier_node_OID, classifier_node_label, classifier_node_numleafdocs, 0, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
222 | classifier_nodes_iterator++;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | void dynamicclassifieraction::output_internal_page (text_t metadata_element_name, cgiargsclass &args,
|
---|
228 | recptproto *collectproto, browsermapclass *browsers,
|
---|
229 | displayclass &disp, outconvertclass &outconvert,
|
---|
230 | ostream &textout, ostream &logout)
|
---|
231 | {
|
---|
232 | text_t arg_dcn = args["dcn"];
|
---|
233 | text_t classifier_node_OID = arg_dcn;
|
---|
234 | text_t classifier_node_metadata_value = arg_dcn;
|
---|
235 | int node_indent = 0;
|
---|
236 |
|
---|
237 | // Get all the classifier nodes at this level
|
---|
238 | text_t metadata_value_filter = classifier_node_metadata_value + "|*";
|
---|
239 | FilterResponse_t metadata_values_response;
|
---|
240 | get_metadata_values (metadata_element_name, metadata_value_filter, args["c"], collectproto, metadata_values_response, logout);
|
---|
241 |
|
---|
242 | // Get all the documents at this level
|
---|
243 | FilterResponse_t document_OIDs_response;
|
---|
244 | get_documents_with_metadata_value (metadata_element_name, classifier_node_metadata_value, "dls.Title", args["c"], collectproto, document_OIDs_response, logout);
|
---|
245 |
|
---|
246 | // Check there are some classifier nodes or some documents at this level, otherwise the "dcn" argument was invalid
|
---|
247 | if (metadata_values_response.docInfo.empty() && document_OIDs_response.docInfo.empty())
|
---|
248 | {
|
---|
249 | textout << outconvert << disp << "Error: Invalid dcn value \"" << arg_dcn << "\".\n";
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | // Determine the parent classifier nodes
|
---|
254 | text_tarray parent_classifier_node_labels;
|
---|
255 | splitchar(classifier_node_OID.begin(), classifier_node_OID.end(), '|', parent_classifier_node_labels);
|
---|
256 | text_t classifier_node_label = parent_classifier_node_labels.back();
|
---|
257 | parent_classifier_node_labels.pop_back();
|
---|
258 |
|
---|
259 | // Display the parent classifier nodes
|
---|
260 | text_t parent_classifier_node_OID = "";
|
---|
261 | text_tarray::iterator parent_classifier_node_labels_iterator = parent_classifier_node_labels.begin();
|
---|
262 | while (parent_classifier_node_labels_iterator != parent_classifier_node_labels.end())
|
---|
263 | {
|
---|
264 | parent_classifier_node_OID += (parent_classifier_node_OID != "" ? "|" : "");
|
---|
265 | parent_classifier_node_OID += *parent_classifier_node_labels_iterator;
|
---|
266 | text_t parent_classifier_node_label = *parent_classifier_node_labels_iterator;
|
---|
267 | text_t parent_classifier_node_numleafdocs = "?"; // We can't determine this without more database requests
|
---|
268 | output_classifier_node (parent_classifier_node_OID, parent_classifier_node_label, parent_classifier_node_numleafdocs, node_indent, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
269 | node_indent++;
|
---|
270 |
|
---|
271 | parent_classifier_node_labels_iterator++;
|
---|
272 | }
|
---|
273 |
|
---|
274 | // Display the selected classifier node
|
---|
275 | int classifier_node_numleafdocs = metadata_values_response.docInfo.size() + document_OIDs_response.docInfo.size();
|
---|
276 | output_classifier_node (classifier_node_OID, classifier_node_label, classifier_node_numleafdocs, node_indent, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
277 | node_indent++;
|
---|
278 |
|
---|
279 | // Determine the child classifier nodes
|
---|
280 | map<text_t, int, lttext_t> child_classifier_nodes;
|
---|
281 | ResultDocInfo_tarray::iterator metadata_value_iterator = metadata_values_response.docInfo.begin();
|
---|
282 | while (metadata_value_iterator != metadata_values_response.docInfo.end())
|
---|
283 | {
|
---|
284 | text_t metadata_value = (*metadata_value_iterator).OID;
|
---|
285 |
|
---|
286 | // If the classifier is hierarchical we need to remove the current position from the metadata value
|
---|
287 | if (starts_with(metadata_value, arg_dcn + "|"))
|
---|
288 | {
|
---|
289 | metadata_value = substr(metadata_value.begin() + (arg_dcn + "|").size(), metadata_value.end());
|
---|
290 | }
|
---|
291 |
|
---|
292 | // Is this metadata value hierarchical?
|
---|
293 | text_t::iterator hierarchy_split_position = findchar(metadata_value.begin(), metadata_value.end(), '|');
|
---|
294 | if (hierarchy_split_position != metadata_value.end())
|
---|
295 | {
|
---|
296 | // Yes, so split off the first part of the hierarchy for the classifier node
|
---|
297 | metadata_value = substr(metadata_value.begin(), hierarchy_split_position);
|
---|
298 | }
|
---|
299 |
|
---|
300 | // Create a node for this metadata value if we haven't seen it before
|
---|
301 | if (child_classifier_nodes.find(metadata_value) == child_classifier_nodes.end())
|
---|
302 | {
|
---|
303 | child_classifier_nodes[metadata_value] = 0;
|
---|
304 | }
|
---|
305 |
|
---|
306 | // Increment the occurrence count
|
---|
307 | child_classifier_nodes[metadata_value] += (*metadata_value_iterator).result_num;
|
---|
308 |
|
---|
309 | metadata_value_iterator++;
|
---|
310 | }
|
---|
311 |
|
---|
312 | // Display the child classifier nodes
|
---|
313 | map<text_t, int, lttext_t>::iterator child_classifier_nodes_iterator = child_classifier_nodes.begin();
|
---|
314 | while (child_classifier_nodes_iterator != child_classifier_nodes.end())
|
---|
315 | {
|
---|
316 | text_t child_classifier_node_OID = classifier_node_OID + "|" + (*child_classifier_nodes_iterator).first;
|
---|
317 | text_t child_classifier_node_label = (*child_classifier_nodes_iterator).first;
|
---|
318 | int child_classifier_node_numleafdocs = (*child_classifier_nodes_iterator).second;
|
---|
319 | output_classifier_node (child_classifier_node_OID, child_classifier_node_label, child_classifier_node_numleafdocs, node_indent, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
320 | child_classifier_nodes_iterator++;
|
---|
321 | }
|
---|
322 |
|
---|
323 | // Display the documents at this level
|
---|
324 | text_tarray document_OIDs;
|
---|
325 | ResultDocInfo_tarray::iterator document_OID_iterator = document_OIDs_response.docInfo.begin();
|
---|
326 | while (document_OID_iterator != document_OIDs_response.docInfo.end())
|
---|
327 | {
|
---|
328 | document_OIDs.push_back ((*document_OID_iterator).OID);
|
---|
329 | document_OID_iterator++;
|
---|
330 | }
|
---|
331 |
|
---|
332 | output_document_nodes (document_OIDs, node_indent, args, collectproto, browsers, disp, outconvert, textout, logout);
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | void dynamicclassifieraction::output_classifier_node (text_t classifier_node_OID, text_t classifier_node_label,
|
---|
337 | text_t classifier_node_numleafdocs, int classifier_node_indent,
|
---|
338 | cgiargsclass &args, recptproto *collectproto,
|
---|
339 | browsermapclass *browsers, displayclass &disp,
|
---|
340 | outconvertclass &outconvert, ostream &textout,
|
---|
341 | ostream &logout)
|
---|
342 | {
|
---|
343 | // Generate the ResultDocInfo_t containing the information for the classifier node
|
---|
344 | ResultDocInfo_t classifier_node;
|
---|
345 | classifier_node.OID = classifier_node_OID;
|
---|
346 | classifier_node.metadata["doctype"].values.push_back ("classify");
|
---|
347 | classifier_node.metadata["haschildren"].values.push_back ("1");
|
---|
348 | classifier_node.metadata["numleafdocs"].values.push_back (classifier_node_numleafdocs);
|
---|
349 | classifier_node.metadata["Title"].values.push_back (classifier_node_label);
|
---|
350 |
|
---|
351 | // Get the format statement for this classifier if there is one, or use the browser's default otherwise
|
---|
352 | text_t formatstring;
|
---|
353 | text_t classifier_type = "VList";
|
---|
354 | browserclass *bptr = browsers->getbrowser (classifier_type);
|
---|
355 | ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, args["c"], logout);
|
---|
356 | if (!get_formatstring (args["dcl"], classifier_type, cinfo->format, formatstring))
|
---|
357 | {
|
---|
358 | formatstring = bptr->get_default_formatstring();
|
---|
359 | }
|
---|
360 | format_t *formatlistptr = new format_t();
|
---|
361 | text_tset metadata;
|
---|
362 | bool getParents = false;
|
---|
363 | parse_formatstring (formatstring, formatlistptr, metadata, getParents);
|
---|
364 | bool use_table = is_table_content (formatlistptr);
|
---|
365 |
|
---|
366 | // Display the classifier node
|
---|
367 | bptr->output_section_group (classifier_node, args, args["c"], classifier_node_indent, formatlistptr, use_table, metadata, getParents, collectproto, disp, outconvert, textout, logout);
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | void dynamicclassifieraction::output_document_nodes (text_tarray document_OIDs, int document_nodes_indent,
|
---|
372 | cgiargsclass &args, recptproto *collectproto,
|
---|
373 | browsermapclass *browsers, displayclass &disp,
|
---|
374 | outconvertclass &outconvert, ostream &textout,
|
---|
375 | ostream &logout)
|
---|
376 | {
|
---|
377 | // Get the format statement for this classifier if there is one, or use the browser's default otherwise
|
---|
378 | text_t formatstring;
|
---|
379 | text_t classifier_type = "VList";
|
---|
380 | browserclass *bptr = browsers->getbrowser (classifier_type);
|
---|
381 | ColInfoResponse_t *cinfo = recpt->get_collectinfo_ptr (collectproto, args["c"], logout);
|
---|
382 | if (!get_formatstring (args["dcl"], classifier_type, cinfo->format, formatstring))
|
---|
383 | {
|
---|
384 | formatstring = bptr->get_default_formatstring();
|
---|
385 | }
|
---|
386 | format_t *formatlistptr = new format_t();
|
---|
387 | text_tset metadata;
|
---|
388 | bool getParents = false;
|
---|
389 | parse_formatstring (formatstring, formatlistptr, metadata, getParents);
|
---|
390 | bool use_table = is_table_content (formatlistptr);
|
---|
391 |
|
---|
392 | // Request the necessary metadata for the documents
|
---|
393 | FilterResponse_t document_OIDs_response;
|
---|
394 | get_info (document_OIDs, args["c"], args["l"], metadata, getParents, collectproto, document_OIDs_response, logout);
|
---|
395 |
|
---|
396 | // Display the document nodes
|
---|
397 | bptr->output_section_group (document_OIDs_response, args, args["c"], document_nodes_indent, formatlistptr, use_table, metadata, getParents, collectproto, disp, outconvert, textout, logout);
|
---|
398 | }
|
---|