|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class DataIntegrityTestRecentlyChanged extends BuildTask |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* standard SS variable |
|
10
|
|
|
* @var String |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $title = "Check what records have been changed in the last xxx minutes"; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* standard SS variable |
|
16
|
|
|
* @var String |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $description = "Go through all tables in the database and see what records have been edited in the last xxx minutes. You can set the minutes using a GET variable (http://www.sunnysideup.co.nz/dev/tasks/DataIntegrityTestRecentlyChanged/?x=123 where 123 is the number of minutes)."; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* runs the task and outputs directly to the screen |
|
22
|
|
|
*/ |
|
23
|
|
|
public function run($request) |
|
24
|
|
|
{ |
|
25
|
|
|
echo "<style>table {width: 100%;} th, td {padding: 5px; font-size: 12px; border: 1px solid #ccc; vertical-align: top;}</style>"; |
|
26
|
|
|
$minutes = intval($request->getVar("m"))-0; |
|
27
|
|
|
if ($request->getVar("m") === $minutes) { |
|
28
|
|
|
//do nothing |
|
29
|
|
|
} else { |
|
30
|
|
|
$tsFrom = strtotime($request->getVar("m")); |
|
31
|
|
|
if ($tsFrom) { |
|
32
|
|
|
$tsUntil = strtotime("NOW"); |
|
33
|
|
|
$minutes = round(($tsUntil - $tsFrom) / 60); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
if ($minutes) { |
|
37
|
|
|
$ts = strtotime($minutes." minutes ago"); |
|
38
|
|
|
$date = date(DATE_RFC2822, $ts); |
|
39
|
|
|
echo "<hr /><h3>changes in the last ".$this->minutesToTime($minutes)."<br />from: ".$date."<br />make sure you see THE END at the bottom of this list</h3><hr />"; |
|
40
|
|
|
$whereStatementFixed = "UNIX_TIMESTAMP(\"LastEdited\") > ".$ts." "; |
|
41
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject'); |
|
42
|
|
|
array_shift($dataClasses); |
|
43
|
|
|
foreach ($dataClasses as $dataClass) { |
|
44
|
|
|
if (class_exists($dataClass)) { |
|
45
|
|
|
$singleton = Injector::inst()->get($dataClass); |
|
46
|
|
|
if ($singleton instanceof TestOnly) { |
|
47
|
|
|
//do nothing |
|
48
|
|
|
} else { |
|
49
|
|
|
$whereStatement = $whereStatementFixed." AND \"ClassName\" = '".$dataClass."' "; |
|
50
|
|
|
$count = $dataClass::get()->where($whereStatement)->count(); |
|
51
|
|
|
$fields = Config::inst()->get($dataClass, "db", Config::INHERITED); |
|
52
|
|
|
if (!is_array($fields)) { |
|
53
|
|
|
$fields = array(); |
|
54
|
|
|
} |
|
55
|
|
|
$fields = array("ID" => "Int", "Created" => "SS_DateAndTime", "LastEdited" => "SS_DateAndTime")+$fields; |
|
56
|
|
|
if ($count) { |
|
57
|
|
|
echo "<h2>".$singleton->singular_name()."(".$count.")</h2>"; |
|
58
|
|
|
$objects = $dataClass::get()->where($whereStatement)->limit(1000); |
|
59
|
|
|
foreach ($objects as $object) { |
|
60
|
|
|
echo "<h4>".$object->getTitle()."</h4><ul>"; |
|
61
|
|
|
if (count($fields)) { |
|
62
|
|
|
$array = array(); |
|
|
|
|
|
|
63
|
|
|
foreach ($fields as $field => $typeOfField) { |
|
64
|
|
|
echo "<li><strong>".$field."</strong><pre>\t\t".htmlentities($object->$field)."</pre></li>"; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
echo "</ul>"; |
|
68
|
|
|
} |
|
69
|
|
|
echo "</blockquote>"; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
echo "<hr /><h1>-------- THE END --------</h1>"; |
|
75
|
|
|
} |
|
76
|
|
|
if (empty($_GET["m"])) { |
|
77
|
|
|
$_GET["m"] = 0; |
|
78
|
|
|
} |
|
79
|
|
|
echo " |
|
80
|
|
|
<form method=\"get\" action=\"".Director::absoluteURL("dev/tasks/".$this->class."/")."\"> |
|
81
|
|
|
<label for=\"m\">please enter minutes ago or any date (e.g. last week, yesterday, 2011-11-11, etc...)</label> |
|
82
|
|
|
<input name=\"m\" id=\"m\" value=\"".$_GET["m"]."\"> |
|
83
|
|
|
</form>"; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function minutesToTime($minutes) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$seconds = $minutes * 60; |
|
89
|
|
|
$dtF = new DateTime("@0"); |
|
90
|
|
|
$dtT = new DateTime("@$seconds"); |
|
91
|
|
|
return $dtF->diff($dtT)->format('%a days, %h hours, and %i minutes'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.