| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: carbon_admin.inc,v 1.7 2009/01/11 17:05:48 johnackers Exp $
|
| 4 |
*
|
| 5 |
* Drupal carbon module, diagnostic/tools routines
|
| 6 |
*
|
| 7 |
* Created on 30-Nov-2006
|
| 8 |
*
|
| 9 |
* @file
|
| 10 |
* This file provides optional support, diagnostic and repair tools for the
|
| 11 |
* carbon module. It is not required for normal operation but keep in mind
|
| 12 |
* that a file of this name is imported by the carbon module.
|
| 13 |
*/
|
| 14 |
|
| 15 |
|
| 16 |
function carbon_admin_form()
|
| 17 |
{
|
| 18 |
$form = array();
|
| 19 |
|
| 20 |
$form["descrip"] = array (
|
| 21 |
"#type" => "item",
|
| 22 |
"#weight" => -3,
|
| 23 |
"#value" => "If you are working with a production system backup your database" .
|
| 24 |
" before executing any of the repair or purge operations available below." .
|
| 25 |
" The underlying SQL statements have been tested with MySql 5.0.18 but " .
|
| 26 |
" should work with other versions. The commands below act immediately, " .
|
| 27 |
" there is no confirmation screen.") ;
|
| 28 |
|
| 29 |
|
| 30 |
$form["check_tables"] = array("#type" => "fieldset",
|
| 31 |
"#title" => t("Show number of records"),
|
| 32 |
"#weight" => "0");
|
| 33 |
|
| 34 |
$form["check_tables"]["descrip"] = array (
|
| 35 |
"#type" => "item",
|
| 36 |
"#value" => "Show the number of nodes and the number of rows for each table/carbon " .
|
| 37 |
" node. In the case of carbon_stamp, carbon_account and carbon_source, there " .
|
| 38 |
" should be an equal number of nodes and rows.");
|
| 39 |
|
| 40 |
$form["check_tables"]["op"] = array("#type" => "submit", "#value" => t("check_tables"));
|
| 41 |
|
| 42 |
|
| 43 |
$form["install_sources"] = array("#type" => "fieldset",
|
| 44 |
"#title" => t("Install carbon sources"),
|
| 45 |
"#weight" => "0");
|
| 46 |
|
| 47 |
$form["install_sources"]["descrip"] = array (
|
| 48 |
"#type" => "item",
|
| 49 |
"#value" => "Install (or update existing) sources of carbon emissions with a" .
|
| 50 |
" a default set of carbon emissions.");
|
| 51 |
|
| 52 |
$form["install_sources"]["op"] = array("#type" => "submit", "#value" => t("install_carbon_sources"));
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
$form["check_ownership"] = array("#type" => "fieldset",
|
| 59 |
"#title" => t("Carbon stamp ownership"),
|
| 60 |
"#weight" => "3");
|
| 61 |
|
| 62 |
$form["check_ownership"]["descrip"] = array (
|
| 63 |
"#type" => "item",
|
| 64 |
"#value" => "Select that stamps attached to footprints but with a different owner. " .
|
| 65 |
"This can happen if another user e.g. admin enters carbon stamps then attaches " .
|
| 66 |
"them to a footprint not beloning to him or her or the author of the footprint " .
|
| 67 |
"is innocently changed.") ;
|
| 68 |
|
| 69 |
$form["check_ownership"]["op"] = array("#type" => "submit",
|
| 70 |
"#value" => t("check_ownership"));
|
| 71 |
|
| 72 |
$form["check_ownership"]["op2"] = array("#type" => "submit",
|
| 73 |
"#value" => t("repair_ownership"));
|
| 74 |
|
| 75 |
|
| 76 |
$form["purge_nodes"] = array("#type" => "fieldset",
|
| 77 |
"#title" => t("Node table management"),
|
| 78 |
"#weight" => "3");
|
| 79 |
|
| 80 |
$form["purge_nodes"]["descrip"] = array (
|
| 81 |
"#type" => "item",
|
| 82 |
"#value" => "Purge nodes in the node table that don't have any corresponding row in a carbon table. " .
|
| 83 |
"This is useful if you drop or truncate the carbon_source, carbon_stamp or " .
|
| 84 |
"carbon_account tables.") ;
|
| 85 |
|
| 86 |
$form["purge_nodes"]["op"] = array("#type" => "submit",
|
| 87 |
"#value" => t("purge_nodes"));
|
| 88 |
|
| 89 |
|
| 90 |
$form["purge_rows"] = array("#type" => "fieldset",
|
| 91 |
"#title" => t("Carbon table management"),
|
| 92 |
"#weight" => "3");
|
| 93 |
|
| 94 |
$form["purge_rows"]["descrip"] = array (
|
| 95 |
"#type" => "item",
|
| 96 |
"#value" => "Purge rows in any of the carbon tables for which there is no corresponding entry " .
|
| 97 |
"in the node table. Not implemented.");
|
| 98 |
|
| 99 |
$form["purge_rows"]["op"] = array("#type" => "submit",
|
| 100 |
"#value" => t("purge_rows"));
|
| 101 |
|
| 102 |
$form["remove_module"] = array("#type" => "fieldset",
|
| 103 |
"#title" => t("Carbon module total removal"),
|
| 104 |
"#weight" => "4");
|
| 105 |
|
| 106 |
$form["remove_module"]["descrip"] = array (
|
| 107 |
"#type" => "item",
|
| 108 |
"#value" => "Remove module from Drupal by removing the carbon ".
|
| 109 |
" entry from the system table. When this module ".
|
| 110 |
" is reenabled, drupal will attempt to run the install script.".
|
| 111 |
" Don't do this unless you are sure or desperate.");
|
| 112 |
|
| 113 |
$form["remove_module"]["op"] = array("#type" => "submit",
|
| 114 |
"#value" => t("remove_module"));
|
| 115 |
|
| 116 |
$form["drop_tables"] = array("#type" => "fieldset",
|
| 117 |
"#title" => t("Drop tables"),
|
| 118 |
"#weight" => "4");
|
| 119 |
|
| 120 |
$form["drop_tables"]["descrip"] = array (
|
| 121 |
"#type" => "item",
|
| 122 |
"#value" => "Drop tables from database. All collected data is ". "lost. You cannot reverse this so backup if you are not sure ". "what you are doing");
|
| 123 |
|
| 124 |
$form["drop_tables"]["op"] = array("#type" => "submit",
|
| 125 |
"#value" => t("drop_carbon_tables"));
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
/*
|
| 130 |
$form["show_orphans"] = array("#type" => "fieldset",
|
| 131 |
"#title" => t("Orphaned carbon stamps"),
|
| 132 |
"#weight" => "2",
|
| 133 |
);
|
| 134 |
$form["show_orphans"]["op"] = array("#type" => "submit", "#value" => t("show_orphans"));
|
| 135 |
*/
|
| 136 |
return $form ;
|
| 137 |
}
|
| 138 |
|
| 139 |
|
| 140 |
function carbon_admin($a = NULL, $b = NULL, $c = NULL)
|
| 141 |
{
|
| 142 |
$map = _map_of_footprints();
|
| 143 |
$map_values = array_values($map);
|
| 144 |
$map_keys = array_keys($map);
|
| 145 |
|
| 146 |
$footprint_idx = isset($_POST["edit"]["footprint_idx"]) ? $_POST["edit"]["footprint_idx"] : 0 ;
|
| 147 |
|
| 148 |
$output = "" ;
|
| 149 |
if (isset($_POST["op"]))
|
| 150 |
{
|
| 151 |
$op = $_POST["op"];
|
| 152 |
|
| 153 |
$fn = "_".$op ;
|
| 154 |
if (function_exists($fn))
|
| 155 |
{
|
| 156 |
$footprint = $map_values[$footprint_idx];
|
| 157 |
$fid = $map_keys[$footprint_idx];
|
| 158 |
|
| 159 |
$result = $fn($footprint, $fid);
|
| 160 |
|
| 161 |
// $form seems to get cached, added field not diplayed!!
|
| 162 |
|
| 163 |
$output .= $result ;
|
| 164 |
$form[$op]["result0"] = array (
|
| 165 |
"#type" => "item",
|
| 166 |
"#weight" => "-5",
|
| 167 |
"#value" => "oidjioew") ;
|
| 168 |
}
|
| 169 |
else
|
| 170 |
$output = "function ". $op . " does not exist or is not installed" ;
|
| 171 |
|
| 172 |
}
|
| 173 |
drupal_set_message($output);
|
| 174 |
$output .= drupal_get_form("carbon_admin_form", null);
|
| 175 |
return $output ;
|
| 176 |
}
|
| 177 |
|
| 178 |
|
| 179 |
function _map_of_footprints()
|
| 180 |
{
|
| 181 |
require_once(CARBON_PATH."/carbon_report.inc");
|
| 182 |
$map = carbon_report_base(null, null, array());
|
| 183 |
$map2 = array();
|
| 184 |
foreach($map as $nid=>$footprint)
|
| 185 |
{
|
| 186 |
$map2[$nid] = $footprint->title ;
|
| 187 |
}
|
| 188 |
return $map2 ;
|
| 189 |
}
|
| 190 |
|
| 191 |
|
| 192 |
function _check_tables()
|
| 193 |
{
|
| 194 |
$tables = _check_tables_present(array("carbon_source", "carbon_account",
|
| 195 |
"carbon_stamp", "carbon_transfer", "carbon_stamp_account", "carbon_account_share"));
|
| 196 |
|
| 197 |
$out = "" ;
|
| 198 |
$num = 0 ;
|
| 199 |
|
| 200 |
$header = array(
|
| 201 |
array("data" => t("table name")),
|
| 202 |
array("data" => t("rows in table")),
|
| 203 |
array("data" => t("nodes of this type"))
|
| 204 |
);
|
| 205 |
|
| 206 |
$rows = array();
|
| 207 |
foreach($tables as $tablename => $record)
|
| 208 |
{
|
| 209 |
|
| 210 |
if (!isset($record["rowcount"]))
|
| 211 |
$rows[] = array($tablename, "not present", "not present") ;
|
| 212 |
else
|
| 213 |
{
|
| 214 |
$num++ ;
|
| 215 |
$rows[] = array($tablename, $record["rowcount"], $record["nodecount"]);
|
| 216 |
}
|
| 217 |
}
|
| 218 |
$out = theme("table", $header, $rows);
|
| 219 |
|
| 220 |
return $out ;
|
| 221 |
}
|
| 222 |
|
| 223 |
function _db_table_exists($table) {
|
| 224 |
return db_result(db_query("SHOW TABLES LIKE '{" . db_escape_table($table) . "}'"));
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* This may only work with mysql
|
| 229 |
*
|
| 230 |
* @param an array of table names
|
| 231 |
* @return an array of key pairs that contain the name and row count.
|
| 232 |
*/
|
| 233 |
|
| 234 |
|
| 235 |
function _check_tables_present($tablename)
|
| 236 |
{
|
| 237 |
$sql = "show tables";
|
| 238 |
$table2 = array();
|
| 239 |
$num = 0 ;
|
| 240 |
foreach ($tablename as $name)
|
| 241 |
{
|
| 242 |
if (_db_table_exists($name))
|
| 243 |
{
|
| 244 |
$db_query2 = db_query("select count(*) as count from ".$name);
|
| 245 |
$result2 = db_fetch_object($db_query2);
|
| 246 |
$table2[$name]["rowcount"] = $result2->count ;
|
| 247 |
|
| 248 |
$db_query3 = db_query("select count(*) as count from node where type = '".$name."'");
|
| 249 |
$result3 = db_fetch_object($db_query3);
|
| 250 |
$table2[$name]["nodecount"] = $result3->count ;
|
| 251 |
}
|
| 252 |
else
|
| 253 |
$table2[$name]["rowcount"] = $table2[$name]["nodecount"] = "" ;
|
| 254 |
}
|
| 255 |
return $table2 ;
|
| 256 |
}
|
| 257 |
|
| 258 |
function _install_carbon_sources()
|
| 259 |
{
|
| 260 |
carbon_source_install_default();
|
| 261 |
}
|
| 262 |
|
| 263 |
|
| 264 |
function _check_ownership()
|
| 265 |
{
|
| 266 |
$sql = "select fn.title, fu.name footprint_owner, su.name stamp_owner, sn.status as published, count(*) number " .
|
| 267 |
"from carbon_stamp_account sf " .
|
| 268 |
"join node fn on fn.nid = sf.fid " .
|
| 269 |
"join node sn on sn.nid = sf.sid join users fu on fn.uid=fu.uid " .
|
| 270 |
"join users su on sn.uid = su.uid " .
|
| 271 |
"where fn.uid != sn.uid group by fn.title, fu.name, su.name, sn.status ; ";
|
| 272 |
|
| 273 |
$db_query = db_query($sql);
|
| 274 |
$rows = array();
|
| 275 |
$num = 0 ;
|
| 276 |
while ($record = db_fetch_object($db_query))
|
| 277 |
{
|
| 278 |
$rows[] = array($record->title, $record->footprint_owner,
|
| 279 |
$record->stamp_owner,$record->published, $record->number);
|
| 280 |
$num++ ;
|
| 281 |
}
|
| 282 |
$header = array(
|
| 283 |
array("data" => t("title")),
|
| 284 |
array("data" => t("footprint_owner")),
|
| 285 |
array("data" => t("stamp_owner")),
|
| 286 |
array("data" => t("published")),
|
| 287 |
array("data" => t("number"))
|
| 288 |
);
|
| 289 |
return $num > 0 ? theme("table", $header, $rows) : "no stamps found with incorrect or invalid ownership" ;
|
| 290 |
}
|
| 291 |
|
| 292 |
|
| 293 |
function _repair_ownership()
|
| 294 |
{
|
| 295 |
$sql = "update carbon_stamp_account sf " .
|
| 296 |
"join node fn on fn.nid = sf.fid join node sn on sn.nid = sf.sid " .
|
| 297 |
"join users fu on fn.uid=fu.uid join users su on sn.uid = su.uid " .
|
| 298 |
"set sn.uid = fn.uid " .
|
| 299 |
"where fn.uid != sn.uid ";
|
| 300 |
$db_query = db_query($sql);
|
| 301 |
return $db_query ? "done" : "failed" ;
|
| 302 |
}
|
| 303 |
|
| 304 |
|
| 305 |
/**
|
| 306 |
* take care if you change this sql or you might delete all your data
|
| 307 |
* needs mysql subqueries
|
| 308 |
*/
|
| 309 |
|
| 310 |
|
| 311 |
function _purge_nodes()
|
| 312 |
{
|
| 313 |
$db_query1 = db_query(
|
| 314 |
"delete from node where node.type = 'carbon_account' " .
|
| 315 |
"and not exists (select nid from carbon_account where carbon_account.nid = node.nid); "
|
| 316 |
);
|
| 317 |
|
| 318 |
$db_query2 = db_query(
|
| 319 |
"delete from node where node.type = 'carbon_stamp' " .
|
| 320 |
"and not exists (select nid from carbon_stamp where carbon_stamp.nid = node.nid); "
|
| 321 |
);
|
| 322 |
$db_query3 = db_query(
|
| 323 |
"delete from node where node.type = 'carbon_transfer' " .
|
| 324 |
"and not exists (select nid from carbon_transfer where carbon_transfer.nid = node.nid); "
|
| 325 |
);
|
| 326 |
|
| 327 |
$db_query4 = db_query(
|
| 328 |
"delete from node where node.type = 'carbon_source' " .
|
| 329 |
"and not exists (select nid from carbon_source where carbon_source.nid = node.nid); "
|
| 330 |
);
|
| 331 |
|
| 332 |
return t("done") ;
|
| 333 |
}
|
| 334 |
|
| 335 |
|
| 336 |
function _purge_rows()
|
| 337 |
{
|
| 338 |
return t("not implemented") ;
|
| 339 |
}
|
| 340 |
|
| 341 |
|
| 342 |
function _show_orphans()
|
| 343 |
{
|
| 344 |
return t("not implemented") ;
|
| 345 |
}
|
| 346 |
|
| 347 |
|
| 348 |
function _remove_module()
|
| 349 |
{
|
| 350 |
$db_query1 = db_query(
|
| 351 |
"delete from system where name = 'carbon'"
|
| 352 |
);
|
| 353 |
return "module removed" ;
|
| 354 |
}
|
| 355 |
|
| 356 |
|
| 357 |
function _drop_carbon_tables()
|
| 358 |
{
|
| 359 |
$res = _drop_tables();
|
| 360 |
return "drop tables " . $res == 0 ? "failed" : "ok" ;
|
| 361 |
}
|
| 362 |
|
| 363 |
|
| 364 |
function _check_orphans()
|
| 365 |
{
|
| 366 |
return 42 ;
|
| 367 |
}
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
?>
|