Skip to content

Commit

Permalink
Move bugdb queries to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
petk committed Dec 19, 2018
1 parent 11fd2af commit ad8bc2d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 61 deletions.
17 changes: 3 additions & 14 deletions scripts/cron/email-assigned
@@ -1,27 +1,16 @@
#!/usr/bin/env php
<?php /* vim: set ft=phpbugdb noet ts=4 sw=4 : */

use App\Repository\BugRepository;

require __DIR__ . '/../../include/prepend.php';

/*
Many times bugs are assigned to humans but not given the 'Assigned' status. Our bug tracker
is a little odd that way.
'No Feedback' was once a part of this, but no longer: https://news.php.net/php.webmaster/8828
*/
$sql = "SELECT id, package_name, bug_type, sdesc, status, assign, UNIX_TIMESTAMP(ts1) AS ts_opened, UNIX_TIMESTAMP(ts2) AS ts_changed
FROM `bugdb`
WHERE length(assign) > 1
AND status IN ('Assigned', 'Open', 'Re-Opened', 'Feedback', 'Analyzed', 'Verified', 'Critical', 'Suspended')
ORDER BY id";

$res = $dbh->query($sql);

// Gather up the data
while ($row = $res->fetch()) {
$data[$row['assign']][] = $row;
}

foreach ($data as $assigned => $binfos) {
foreach ((new BugRepository($dbh))->findAllAssigned() as $assigned => $binfos) {

$mbody = format_email_body($binfos);
$email_user = $assigned . '@php.net';
Expand Down
14 changes: 2 additions & 12 deletions scripts/cron/no-feedback
Expand Up @@ -3,13 +3,11 @@

# this script closes bugs due to lack of feedback.

use App\Repository\BugRepository;
use App\Repository\ReasonRepository;

require __DIR__.'/../../include/prepend.php';

# date interval to close after
$after = "7 DAY";

# Set "input" array
$in = ['status' => 'No Feedback'];

Expand All @@ -18,15 +16,7 @@ $reasonRepository = new ReasonRepository($dbh);

list($RESOLVE_REASONS, $FIX_VARIATIONS) = $reasonRepository->findByProject($site);

$res = $dbh->prepare("
SELECT id, package_name, bug_type, email, passwd, sdesc, ldesc, php_version,
php_os, status, ts1, ts2, assign, UNIX_TIMESTAMP(ts1) AS submitted,
private, reporter_name, UNIX_TIMESTAMP(ts2) AS modified
FROM bugdb
WHERE status = 'Feedback' AND ts2 < DATE_SUB(NOW(), INTERVAL {$after})
")->execute([]);

while ($bug = $res->fetch())
foreach ((new BugRepository($dbh))->findAllWithoutFeedback() as $bug)
{
list($mailto, $mailfrom, $bcc, $params) = get_package_mail($bug['package_name'], false, $bug['bug_type']);

Expand Down
126 changes: 126 additions & 0 deletions src/Repository/BugRepository.php
Expand Up @@ -13,6 +13,11 @@ class BugRepository
*/
private $dbh;

/**
* Days when bugs with no feedback get closed.
*/
private const FEEDBACK_PERIOD = 7;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -48,4 +53,125 @@ public function findOneById(int $id): array

return $statement->fetch();
}

/**
* Find random bug to resolve for a contributor.
*/
public function findRandom(): array
{
$sql = "SELECT id
FROM bugdb
WHERE status NOT IN('Closed', 'Not a bug', 'Duplicate', 'Spam', 'Wont fix', 'No Feedback')
AND private = 'N'
ORDER BY RAND() LIMIT 1
";

$statement = $this->dbh->prepare($sql);
$statement->execute();

return $statement->fetch(\PDO::FETCH_NUM);
}

/**
* Find all bugs that have someone assigned to them.
*/
public function findAllAssigned(): array
{
$sql = "SELECT id, package_name, bug_type, sdesc, status, assign, UNIX_TIMESTAMP(ts1) AS ts_opened, UNIX_TIMESTAMP(ts2) AS ts_changed
FROM `bugdb`
WHERE length(assign) > 1
AND status IN ('Assigned', 'Open', 'Re-Opened', 'Feedback', 'Analyzed', 'Verified', 'Critical', 'Suspended')
ORDER BY id
";

$statement = $this->dbh->query($sql);

$data = [];

// Populate data with assign field as array key
while ($row = $statement->fetch()) {
$data[$row['assign']][] = $row;
}

return $data;
}

/**
* Find all bugs without feedback by given period time.
*/
public function findAllWithoutFeedback(int $feedbackPeriod = self::FEEDBACK_PERIOD): array
{
$sql = "SELECT id, package_name, bug_type, email, passwd, sdesc, ldesc,
php_version, php_os, status, ts1, ts2, assign,
UNIX_TIMESTAMP(ts1) AS submitted, private, reporter_name,
UNIX_TIMESTAMP(ts2) AS modified
FROM bugdb
WHERE status = 'Feedback' AND ts2 < DATE_SUB(NOW(), INTERVAL ? DAY)
";

$statement = $this->dbh->prepare($sql);
$statement->execute([$feedbackPeriod]);

return $statement->fetchAll();
}

/**
* Find all bugs by given bug type.
*/
public function findAllByBugType(string $type = 'All'): array
{
$sql = 'SELECT b.package_name, b.status, COUNT(*) AS quant FROM bugdb AS b';

$arguments = [];

if ($type !== 'All') {
$sql .= ' WHERE bug_type = ? ';
$arguments[] = $type;
}

$sql .= ' GROUP BY b.package_name, b.status ORDER BY b.package_name, b.status';

$statement = $this->dbh->prepare($sql);
$statement->execute($arguments);

return $statement->fetchAll();
}

/**
* Find bugs for grouping into PHP versions by given bug type.
*/
public function findPhpVersions(string $type = 'All'): array
{
$sql = "SELECT DATE_FORMAT(ts1, '%Y-%m') as d,
IF(b.php_version LIKE '%Git%', LEFT(b.php_version, LOCATE('Git', b.php_version)+2), b.php_version) AS formatted_version,
COUNT(*) AS quant
FROM bugdb AS b
WHERE ts1 >= CONCAT(YEAR(NOW())-1, '-', MONTH(NOW()), '-01 00:00:00')
";

$arguments = [];

if ($type !== 'All') {
$sql .= ' AND bug_type = ? ';
$arguments[] = $type;
}

$sql .= ' GROUP BY d, formatted_version ORDER BY d, quant';

$statement = $this->dbh->prepare($sql);
$statement->execute($arguments);

return $statement->fetchAll();
}

/**
* Check if bug with given id exists.
*/
public function exists(int $id): bool
{
$statement = $this->dbh->prepare('SELECT 1 FROM bugdb WHERE id = ?');
$statement->execute([$id]);

return (bool)$statement->fetchColumn();
}
}
7 changes: 3 additions & 4 deletions www/index.php
Expand Up @@ -4,6 +4,8 @@

/* The bug system home page */

use App\Repository\BugRepository;

// Obtain common includes
require_once '../include/prepend.php';

Expand All @@ -14,10 +16,7 @@
}

if($_SERVER['REQUEST_URI'] == '/random') {
$query = "SELECT id FROM bugdb WHERE status NOT IN('Closed', 'Not a bug', 'Duplicate', 'Spam', 'Wont fix', 'No Feedback') AND private = 'N' ORDER BY RAND() LIMIT 1";

$result = $dbh->prepare($query)->execute();
$id = $result->fetch(\PDO::FETCH_NUM);
$id = (new BugRepository($dbh))->findRandom();
redirect("bug.php?id={$id[0]}");
}

Expand Down
36 changes: 6 additions & 30 deletions www/stats.php
@@ -1,5 +1,7 @@
<?php

use App\Repository\BugRepository;

session_start();

// Obtain common includes
Expand Down Expand Up @@ -40,25 +42,10 @@
$sort_by = 'Open';
}

$where = '';
if (empty($_GET['bug_type']) || $_GET['bug_type'] == 'All') {
$bug_type = 'All';
} else {
$bug_type = $_GET['bug_type'];
$where = ' AND bug_type = ' . $dbh->quote($bug_type);
}

$query = "
SELECT b.package_name, b.status, COUNT(*) AS quant
FROM bugdb AS b
WHERE 1 = 1 {$where}
GROUP BY b.package_name, b.status
ORDER BY b.package_name, b.status
";
$bug_type = $_GET['bug_type'] ?? 'All';
$bugRepository = new BugRepository($dbh);

$result = $dbh->prepare($query)->execute();

while ($row = $result->fetch()) {
foreach ($bugRepository->findAllByBugType($bug_type) as $row) {
$pkg_tmp[$row['status']][$row['package_name']] = $row['quant'];
@$pkg_total[$row['package_name']] += $row['quant'];
@$all[$row['status']] += $row['quant'];
Expand Down Expand Up @@ -148,19 +135,8 @@

echo "</table>\n<hr>\n<p><b>PHP Versions for recent bug reports:</b></p><div>";

$query = " SELECT DATE_FORMAT(ts1, '%Y-%m') as d,
IF(b.php_version LIKE '%Git%', LEFT(b.php_version, LOCATE('Git', b.php_version)+2), b.php_version) AS formatted_version,
COUNT(*) AS quant
FROM bugdb AS b
WHERE ts1 >= CONCAT(YEAR(NOW())-1, '-', MONTH(NOW()), '-01 00:00:00')
{$where}
GROUP BY d, formatted_version
ORDER BY d, quant";

$result = $dbh->prepare($query)->execute();

$last_date = null;
while ($row = $result->fetch()) {
foreach ($bugRepository->findPhpVersions($bug_type) as $row) {
if ($row['d'] != $last_date) {
if ($last_date !== null) {
echo "</table>\n\n";
Expand Down
4 changes: 3 additions & 1 deletion www/vote.php
@@ -1,5 +1,7 @@
<?php

use App\Repository\BugRepository;

// Obtain common includes
require_once '../include/prepend.php';

Expand All @@ -20,7 +22,7 @@
$samever = isset($_POST['samever']) ? (int) $_POST['samever'] : 0;
$sameos = isset($_POST['sameos']) ? (int) $_POST['sameos'] : 0;

if (!$dbh->prepare("SELECT id FROM bugdb WHERE id= ? LIMIT 1")->execute([$id])->fetch(\PDO::FETCH_NUM)[0]) {
if (!(new BugRepository($dbh))->exists($id)) {
session_start();

// Authenticate
Expand Down

0 comments on commit ad8bc2d

Please sign in to comment.