6 require_once 'plc_login.php';
8 // Get session and API handles
9 require_once 'plc_session.php';
13 require_once 'plc_drupal.php';
15 drupal_set_title('DB Search');
16 include 'plc_header.php';
19 require_once 'plc_functions.php';
20 require_once 'plc_objects.php';
21 require_once 'plc_peers.php';
22 require_once 'table.php';
23 require_once 'form.php';
24 require_once 'toggle.php';
26 ini_set("memory_limit","256M");
28 if ( ! plc_is_admin()) {
29 plc_warning ("DB Search is available to admins only");
34 if (isset($_GET['pattern'])) { $pattern=$_GET['pattern']; }
35 if (isset($_POST['pattern'])) { $pattern=$_POST['pattern']; }
37 $tokens=explode(" ",$pattern);
38 function token_filter ($t) { $t = trim($t); if (empty($t)) return false; return true; }
39 $tokens=array_filter($tokens, "token_filter");
43 // from a single search form, extract all tokens entered
44 // and then show all entities that match one of that tokens among
45 // persons - sites - slices - nodes
47 function display_form ($pattern) {
49 $title="Searching for $pattern";
52 $title="Search for what";
55 $toggle=new PlekitToggle("admin-search",$title,array('visible'=>$visible));
58 <p id='admin-search-message'>
59 This form searches for <span class="bold">any entry</span> in the database
60 (among <span class="bold">persons</span>, <span class="bold">slices</span>,
61 <span class="bold">sites</span> and <span class="bold">nodes</span>)
62 matching a name fragment, or token. <br/>
63 You can specify a space-separated list of tokens, all entries matching
64 <span class="bold">any token</span> would then get listed.
67 print "<div id='admin-search-form'>";
68 $form=new PlekitForm ('/db/common/adminsearch.php',array());
70 print $form->label_html('pattern',"Enter space-separated tokens");
71 print $form->text_html('pattern',$pattern);
72 print $form->submit_html('submit','Submit');
78 // just look for *token*
79 function token_pattern ($token) {
80 return "*" . $token . "*";
83 // $type is e.g. 'Persons' -- $field is e.g. 'email'
84 function generic_search ($type,$field,$tokens) {
87 $methodname='Get'.$type;
88 if ( ! method_exists($api,$methodname)) {
89 plc_error("generic_search failed with methodname=$methodname");
92 foreach ($tokens as $token) {
93 $filter=array($field=>token_pattern($token));
94 $new_results = $api->$methodname($filter);
95 if (is_array($new_results)) {
96 $results = array_merge ($results, $new_results);
102 // $objects is e.g. a collection of persons
103 // then, e.g. on slice, $key='site_id' & $plural=false
104 // or, e.g. on person, $key='site_ids' & $plural=true
105 function generic_gather_related ($objects, $key, $plural) {
106 if ( empty ($objects))
108 // else, look for either 'site_id' or 'site_ids' in the first object
110 if ( array_key_exists($key,$sample)) {
112 foreach ($objects as $object) {
114 $result = array_merge ($result, $object[$key]);
116 $result []= $object[$key];
121 plc_debug("gather_related failed with $key",$sample);
127 // create link from an id, using the various global hashes
128 function plc_person_link ($person_id) {global $persons_hash; return l_person_obj($persons_hash[$person_id]);}
129 function plc_slice_link ($slice_id) {global $slices_hash; return l_slice_obj($slices_hash[$slice_id]);}
130 function plc_site_link ($site_id) {global $sites_hash; return l_site_obj($sites_hash[$site_id]);}
131 function plc_node_link ($node_id) {global $nodes_hash; return l_node_obj($nodes_hash[$node_id]);}
133 global $table_options;
134 $table_options = array('notes_area'=>false);
137 $peers = new Peers ($api);
139 function display_persons ($persons,$visible) {
140 if ( ! $persons) return;
142 $toggle=new PlekitToggle('persons-area',"Persons",array('visible'=>$visible));
145 $headers=array('id'=>'int',
151 global $table_options;
153 $table=new PlekitTable('persons',$headers,1,$table_options);
155 foreach ($persons as $person) {
157 $table->cell($person['person_id']);
158 $peers->cell($table,$person['peer_id']);
159 $table->cell(l_person_obj($person));
160 $table->cell(plc_vertical_table(array_map("plc_site_link",$person['site_ids'])));
161 $table->cell(plc_vertical_table(array_map("plc_slice_link",$person['slice_ids'])));
162 $table->cell(plc_vertical_table($person['roles']));
169 function display_slices ($slices,$visible) {
170 if ( ! $slices) return;
172 $toggle=new PlekitToggle('slices-area',"Slices",array('visible'=>$visible));
175 $headers=array('id'=>'int',
181 global $table_options;
183 $table=new PlekitTable('slices',$headers,1,$table_options);
185 foreach ($slices as $slice) {
187 $table->cell($slice['slice_id']);
188 $peers->cell($table,$slice['peer_id']);
189 $table->cell(l_slice_obj($slice));
191 $site=$sites_hash[$slice['site_id']];
192 $table->cell(l_site_obj($site));
193 $table->cell(plc_vertical_table(array_map("plc_person_link",$slice['person_ids'])));
194 // this makes really long tables, use the slice link to see details
195 //$table->cell(plc_vertical_table(array_map("plc_node_link",$slice['node_ids'])));
196 $table->cell(count($slice['node_ids']));
203 function display_sites ($sites,$visible) {
204 if ( ! $sites) return;
206 $toggle=new PlekitToggle('sites-area',"Sites",array('visible'=>$visible));
209 $headers=array('id'=>'int',
216 global $table_options;
218 $table=new PlekitTable('sites',$headers,1,$table_options);
220 foreach ($sites as $site) {
222 $table->cell($site['site_id']);
223 $peers->cell($table,$site['peer_id']);
224 $table->cell(l_site_obj($site));
225 $table->cell(href($site['url'],$site['url']));
226 $table->cell(plc_vertical_table(array_map("plc_person_link",$site['person_ids'])));
227 $table->cell(plc_vertical_table(array_map("plc_slice_link",$site['slice_ids'])));
228 $table->cell(plc_vertical_table(array_map("plc_node_link",$site['node_ids'])));
235 function display_nodes ($nodes,$visible) {
236 if ( ! $nodes) return;
238 $toggle=new PlekitToggle('nodes-area',"Nodes",array('visible'=>$visible));
241 $headers=array('id'=>'int',
243 'hostname'=>'string',
246 global $table_options;
248 $table=new PlekitTable('nodes',$headers,1,$table_options);
250 foreach ($nodes as $node) {
252 $table->cell($node['node_id']);
253 $peers->cell($table,$node['peer_id']);
254 $table->cell(l_node_obj($node));
256 $site=$sites_hash[$node['site_id']];
257 $table->cell(l_site_obj($site));
258 // same as above, too many entries, just list how many there are
259 //$table->cell(plc_vertical_table(array_map("plc_slice_link",$node['slice_ids'])));
260 $table->cell(count($node['slice_ids']));
268 ////////////////////////////////////////////////////////////
269 display_form($pattern);
273 ////////// search database
274 // search persons on email
276 $persons = array_merge ($persons, generic_search ('Persons','email',$tokens));
278 // search slices on name
280 $slices = array_merge ($slices, generic_search ('Slices','name',$tokens));
282 // search sites on name and login_base
284 $sites = array_merge ($sites, generic_search('Sites','name',$tokens));
285 $sites = array_merge ($sites, generic_search('Sites','login_base',$tokens));
289 $nodes = array_merge ($nodes, generic_search('Nodes','hostname',$tokens));
291 print "Search results for <span class='tokens'> $pattern </span>\n";
293 // what kind of result have we gotten:
294 // if none : display message and exit
295 // if only one kind of objects : start toggle with visible=true
296 // otherwise start all toggles with visible=false
298 if ($persons) $areas += 1;
299 if ($slices) $areas += 1;
300 if ($sites) $areas += 1;
301 if ($nodes) $areas += 1;
304 plc_warning ("No result found");
306 } else if ($areas == 1) {
313 ////////// collect all related objects
314 $rel_person_ids = array();
315 $rel_person_ids = array_merge($rel_person_ids, generic_gather_related ($sites,'person_ids',true));
316 $rel_person_ids = array_merge($rel_person_ids, generic_gather_related ($slices,'person_ids',true));
318 $rel_slice_ids = array();
319 $rel_slice_ids = array_merge($rel_slice_ids, generic_gather_related ($persons,'slice_ids',true));
320 $rel_slice_ids = array_merge($rel_slice_ids, generic_gather_related ($sites,'slice_ids',true));
321 $rel_slice_ids = array_merge($rel_slice_ids, generic_gather_related ($nodes,'slice_ids',true));
323 $rel_site_ids = array();
324 $rel_site_ids = array_merge ( $rel_site_ids, generic_gather_related ($persons,'site_ids',true));
325 $rel_site_ids = array_merge ( $rel_site_ids, generic_gather_related ($slices,'site_id',false));
326 $rel_site_ids = array_merge ( $rel_site_ids, generic_gather_related ($nodes,'site_id',false));
328 $rel_node_ids = array();
329 $rel_node_ids = array_merge($rel_node_ids, generic_gather_related ($sites,'node_ids',true));
330 $rel_node_ids = array_merge($rel_node_ids, generic_gather_related ($slices,'node_ids',true));
333 ////////// fetch related and store in a global hash
334 $rel_persons = $api->GetPersons ($rel_person_ids);
335 global $persons_hash; $persons_hash=array();
336 foreach ($rel_persons as $person) $persons_hash[$person['person_id']]=$person;
338 $rel_slices = $api->GetSlices ($rel_slice_ids);
339 global $slices_hash; $slices_hash=array();
340 foreach ($rel_slices as $slice) $slices_hash[$slice['slice_id']]=$slice;
342 $rel_sites = $api->GetSites ($rel_site_ids);
343 global $sites_hash; $sites_hash=array();
344 foreach ($rel_sites as $site) $sites_hash[$site['site_id']]=$site;
346 $rel_nodes = $api->GetNodes ($rel_node_ids);
347 global $nodes_hash; $nodes_hash=array();
348 foreach ($rel_nodes as $node) $nodes_hash[$node['node_id']]=$node;
350 ////////// show results
351 display_persons ($persons,$visible);
352 display_slices ($slices,$visible);
353 display_sites($sites,$visible);
354 display_nodes($nodes,$visible);
359 include 'plc_footer.php';