FOSSology  4.4.0
Open Source License Compliance by Open Source Software
maintagent.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2019 Siemens AG
5  SPDX-FileCopyrightText: © 2022 Samuel Dushimimana <dushsam100@gmail.com>
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
10 define("TITLE_MAINTAGENT", _("FOSSology Maintenance"));
11 
14 
19 class maintagent extends FO_Plugin {
20 
22  private $dbManager;
23 
24  const OPTIONS = [
25  "a"=>"Run all non slow maintenance operations.",
26  "A"=>"Run all maintenance operations.",
27  "F"=>"Validate folder contents.",
28  "g"=>"Remove orphaned gold files.",
29  "E"=>"Remove orphaned rows from database.",
30  "L"=>"Remove orphaned log files from file system.",
31  "N"=>"Normalize priority ",
32  // "p"=>_("Verify file permissions (report only)."),
33  // "P"=>_("Verify and fix file permissions."),
34  "R"=>"Remove uploads with no pfiles.",
35  "t"=>"Remove expired personal access tokens.",
36  "T"=>"Remove orphaned temp tables.",
37  "D"=>"Vacuum Analyze the database.",
38  // "U"=>_("Process expired uploads (slow)."),
39  "Z"=>"Remove orphaned files from the repository (slow).",
40  "I"=>"Reindexing of database (This activity may take 5-10 mins. Execute only when system is not in use).",
41  "v"=>"verbose (turns on debugging output)",
42  "o"=>"Remove older gold files from repository.",
43  "l"=>"Remove older log files from repository."
44  ];
45 
46  public function __construct()
47  {
48  $this->Name = "maintagent";
49  $this->Title = TITLE_MAINTAGENT;
50  $this->MenuList = "Admin::Maintenance";
51  $this->DBaccess = PLUGIN_DB_ADMIN;
52 
53  global $container;
54  $this->dbManager = $container->get('db.manager');
55 
56  parent::__construct();
57  }
58 
63  public function handle($request)
64  {
65  global $SysConf;
66 
67  /* Find all the maintagent options specified by the user.
68  * They look like _REQUEST["a"] = "a", _REQUEST["b"]="b", ...
69  */
70  $options = "-";
71  foreach ($request['options'] as $key => $value) {
72  if ($key == $value) {
73  $options .= $value;
74  if ($key === "t") {
75  $retentionPeriod = $SysConf['SYSCONFIG']['PATMaxPostExpiryRetention'];
76  $options .= $retentionPeriod;
77  } elseif ($key === "l") {
78  $options .= $request['logsDate'];
79  }
80  if ($key == "o") {
81  $options .= $request['goldDate'];
82  }
83  }
84  }
85 
86  /* Create the maintenance job */
87  $user_pk = Auth::getUserId();
88  $groupId = Auth::getGroupId();
89 
90  $job_pk = JobAddJob($user_pk, $groupId, "Maintenance");
91  if (empty($job_pk) || ($job_pk < 0)) { return _("Failed to insert job record");
92  }
93 
94  $jq_pk = JobQueueAdd($job_pk, "maintagent", NULL, NULL, NULL, NULL, $options);
95  if (empty($jq_pk)) { return _("Failed to insert task 'Maintenance' into job queue");
96  }
97 
98  /* Tell the scheduler to check the queue. */
99  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
100  if (!$success) { return($error_msg . "\n" . $output);
101  }
102 
103  return _("The maintenance job has been queued");
104  }
105 
106 
111  function DisplayForm()
112  {
113  $V = "";
114  $statementName = __METHOD__."maintenanceInfo";
115  $row = $this->dbManager->getSingleRow("SELECT jq_endtime FROM jobqueue WHERE jq_type = $1 AND jq_end_bits=$2 ORDER BY jq_endtime DESC LIMIT $2",
116  array("maintagent",1), $statementName);
117  if(!empty($row['jq_endtime'])){
118  $dateLastExecuted = Convert2BrowserTime($row['jq_endtime']);
119  $text = _("Last maintenance job was executed on '$dateLastExecuted'");
120  $V.= DisplayMessage($text);
121  }
122  $V .= "<form method='post'>\n"; // no url = this url
123  foreach (self::OPTIONS as $option => $description) {
124  $V .= "<div class='form-group'><div class='form-check'>";
125  $V .= " <input class='form-check-input' type='checkbox' name='$option' value='$option' id='men$option'>
126  <label class='form-check-label' for='men$option'>$description</label>";
127  if ($option === "o") {
128  $V .= "<input type='date' class='form-control' name='goldDate' value='" . gmdate('Y-m-d', strtotime("-1 year")) . "' style='width:auto'>";
129  }
130  if ($option === "l") {
131  $V .= "<input type='date' class='form-control' name='logsDate' value='" . gmdate('Y-m-d', strtotime("-1 year")) . "' style='width:auto'>";
132  }
133  $V .= "</div></div>\n";
134  }
135 
136  $text = _("Queue the maintenance agent");
137  $V.= "<br /><button type='submit' class='btn btn-primary'>$text</button>\n";
138 
139  $V.= "<p>";
140  $V.= _("More information about these operations can be found ");
141  $text = _("here.");
142  $V.= "<a href=https://github.com/fossology/fossology/wiki/Maintenance-Agent> $text </a></p>";
143 
144  $V.= "<input type=hidden name=queue value=true>";
145 
146  $V.= "</form>\n";
147  return $V;
148  }
149 
150 
155  public function Output()
156  {
157  $V = "";
158  /* If this is a POST, then process the request. */
159  $queue = GetParm('queue', PARM_STRING);
160  if (!empty($queue))
161  {
162  $request = ['options' => $_REQUEST , 'logsDate' => GetParm('logsDate', PARM_TEXT), 'goldDate' => GetParm('goldDate', PARM_TEXT)];
163  $Msg = $this->handle($request);
164  $V .= "<font style='background-color:#111110'>" . $Msg . "</font>";
165  }
166  $V .= $this->DisplayForm();
167  return $V;
168  }
169 
170  public function getOptions() {
171  return $this::OPTIONS;
172  }
173 }
174 
175 $NewPlugin = new maintagent;
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Queue the maintenance agent with the requested parameters.
Definition: maintagent.php:19
__construct()
base constructor. Most plugins will just use this
Definition: maintagent.php:46
handle($request)
Queue the job.
Definition: maintagent.php:63
DisplayForm()
Display the input form.
Definition: maintagent.php:111
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: maintagent.php:155
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:157
const PARM_TEXT
Definition: common-parm.php:20
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16