Commit: 9b248f9ed6a53dcc6400edbc1e0728c25a44cf89 Author: Peter Kokot <peterko...@gmail.com> Mon, 10 Dec 2018 01:41:56 +0100 Parents: 0173592272937e4c8c480da9932bbd418aef69eb Branches: master
Link: http://git.php.net/?p=web/bugs.git;a=commitdiff;h=9b248f9ed6a53dcc6400edbc1e0728c25a44cf89 Log: Refactor get_pseudo_packages() to repository class Changes: - get_pseudo_packages() function is moved to its own repository class. - Database::queryAll() removed since it is not used and the method arguments don't match the number of used arguments anymore - Project types configuration is moved to repository class for now. - Some unused items removed - Some template changes and show_project_options() helper function integrated in the view layer directly since it is used in a simplified way. Changed paths: M include/functions.php M include/query.php M src/Database/Database.php A src/Repository/PackageRepository.php M www/bug.php M www/lstats.php M www/patch-display.php M www/report.php M www/search.php M www/stats.php
diff --git a/include/functions.php b/include/functions.php index 8f35fe2..04ec1bb 100644 --- a/include/functions.php +++ b/include/functions.php @@ -33,11 +33,6 @@ $bug_types = [ 'Security' => 'Sec Bug' ]; -$project_types = [ - 'PHP' => 'php', - 'PECL' => 'pecl' -]; - // Used in show_state_options() $state_types = [ 'Open' => 2, @@ -192,70 +187,6 @@ function bugs_authenticate (&$user, &$pw, &$logged_in, &$user_flags) } } -/** - * Fetches pseudo packages from database - * - * @param string $project define what project pseudo packages are returned - * @param bool $return_disabled whether to return read-only items, defaults to true - * - * @return array array of pseudo packages - */ -function get_pseudo_packages($project, $return_disabled = true) -{ - global $dbh, $project_types; - - $pseudo_pkgs = $nodes = $tree = []; - $where = '1=1'; - $project = strtolower($project); - - if ($project !== false && in_array($project, $project_types)) { - $where .= " AND project IN ('', '$project')"; - } - if (!$return_disabled) { - $where.= " AND disabled = 0"; - } - - $data = $dbh->queryAll("SELECT * FROM bugdb_pseudo_packages WHERE $where ORDER BY parent, disabled, id", null, PDO::FETCH_ASSOC); - - // Convert flat array to nested strucutre - foreach ($data as &$node) - { - $node['children'] = []; - $id = $node['id']; - $parent_id = $node['parent']; - $nodes[$id] =& $node; - - if (array_key_exists($parent_id, $nodes)) { - $nodes[$parent_id]['children'][] =& $node; - } else { - $tree[] =& $node; - } - } - - foreach ($tree as $data) - { - if (isset($data['children'])) { - $pseudo_pkgs[$data['name']] = [$data['long_name'], $data['disabled'], []]; - $children = &$pseudo_pkgs[$data['name']][2]; - $long_names = []; - foreach ($data['children'] as $k => $v) { - $long_names[$k] = strtolower($v['long_name']); - } - array_multisort($long_names, SORT_ASC, SORT_STRING, $data['children']); - foreach ($data['children'] as $child) - { - $pseudo_pkgs[$child['name']] = ["{$child['long_name']}", $child['disabled'], null]; - $children[] = $child['name']; - } - - } elseif (!isset($pseudo_pkgs[$data['name']])) { - $pseudo_pkgs[$data['name']] = [$data['long_name'], $data['disabled'], null]; - } - } - - return $pseudo_pkgs; -} - /* Primitive check for SPAM. Add more later. */ function is_spam($string) { @@ -529,40 +460,6 @@ function show_limit_options($limit = 30) } /** - * Prints bug project <option>'s for use in a <select> - * - * Options include "PHP" and "PECL". - * - * @param string $current bug's current project - * @param bool $all whether or not 'All' should be an option - * - * @retun void - */ -function show_project_options($current = 'php', $all = false) -{ - global $project_types; - - if ($all) { - if (!$current) { - $current = 'All'; - } - echo '<option value="All"'; - if ($current == 'All') { - echo ' selected="selected"'; - } - echo ">All</option>\n"; - } elseif (!$current) { - $current = 'php'; - } else { - $current = strtolower($current); - } - - foreach ($project_types as $k => $v) { - echo '<option value="', $k, '"', (($current == strtolower($k)) ? ' selected="selected"' : ''), ">{$k}</option>\n"; - } -} - -/** * Prints bug type <option>'s for use in a <select> * * Options include "Bug", "Documentation Problem" and "Feature/Change Request." diff --git a/include/query.php b/include/query.php index 3e051a9..028f57a 100644 --- a/include/query.php +++ b/include/query.php @@ -1,5 +1,7 @@ <?php +use App\Repository\PackageRepository; + $errors = []; $warnings = []; $order_options = [ @@ -20,7 +22,8 @@ $order_options = [ ]; // Fetch pseudo packages -$pseudo_pkgs = get_pseudo_packages(false); +$packageRepository = new PackageRepository($dbh); +$pseudo_pkgs = $packageRepository->findAll(); // Setup input variables.. $boolean_search = isset($_GET['boolean']) ? (int) $_GET['boolean'] : 0; diff --git a/src/Database/Database.php b/src/Database/Database.php index 37ae753..467f189 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -30,9 +30,4 @@ class Database extends \PDO { return substr($this->quote($text), 1, -1); } - - public function queryAll($query, $types = null, $fetchmode = null, $rekey = false, $force_array = false, $group = false) - { - return $this->query($query)->fetchAll(); - } } diff --git a/src/Repository/PackageRepository.php b/src/Repository/PackageRepository.php new file mode 100644 index 0000000..178a485 --- /dev/null +++ b/src/Repository/PackageRepository.php @@ -0,0 +1,122 @@ +<?php + +namespace App\Repository; + +use App\Database\Database; + +/** + * Repository class for retrieving data from the bugdb_pseudo_packages database + * table. + */ +class PackageRepository +{ + /** + * Database handler. + * @var Database + */ + private $dbh; + + /** + * Project types. + */ + public const PROJECTS = [ + 'PHP' => 'php', + 'PECL' => 'pecl', + ]; + + /** + * Class constructor. + */ + public function __construct(Database $dbh) + { + $this->dbh = $dbh; + } + + /** + * Find all packages by project type. + */ + public function findAll(string $project = ''): array + { + $sql = 'SELECT * FROM bugdb_pseudo_packages'; + $arguments = []; + + $project = strtolower($project); + if (in_array($project, self::PROJECTS)) { + $sql .= " WHERE project IN ('', ?)"; + $arguments[] = $project; + } + + $sql .= ' ORDER BY parent, disabled, id'; + + $data = $this->dbh->prepare($sql)->execute($arguments)->fetchAll(); + + return $this->getNested($data); + } + + /** + * Find all enabled packages by project type. + */ + public function findEnabled(string $project = ''): array + { + $sql = 'SELECT * FROM bugdb_pseudo_packages WHERE disabled = 0'; + $arguments = []; + + $project = strtolower($project); + if (in_array($project, self::PROJECTS)) { + $sql .= " AND project IN ('', ?)"; + $arguments[] = $project; + } + + $sql .= ' ORDER BY parent, id'; + + $data = $this->dbh->prepare($sql)->execute($arguments)->fetchAll(); + + return $this->getNested($data); + } + + /** + * Convert flat array to nested structure. + */ + private function getNested(array $data): array + { + $packages = []; + $nodes = []; + $tree = []; + + foreach ($data as &$node) { + $node['children'] = []; + $id = $node['id']; + $parentId = $node['parent']; + $nodes[$id] =& $node; + + if (array_key_exists($parentId, $nodes)) { + $nodes[$parentId]['children'][] =& $node; + } else { + $tree[] =& $node; + } + } + + foreach ($tree as $data) { + if (isset($data['children'])) { + $packages[$data['name']] = [$data['long_name'], $data['disabled'], []]; + $children = &$packages[$data['name']][2]; + $longNames = []; + + foreach ($data['children'] as $k => $v) { + $longNames[$k] = strtolower($v['long_name']); + } + + array_multisort($longNames, SORT_ASC, SORT_STRING, $data['children']); + + foreach ($data['children'] as $child) { + $packages[$child['name']] = ["{$child['long_name']}", $child['disabled'], null]; + $children[] = $child['name']; + } + } elseif (!isset($packages[$data['name']])) { + $packages[$data['name']] = [$data['long_name'], $data['disabled'], null]; + } + } + + return $packages; + } +} diff --git a/www/bug.php b/www/bug.php index bb87dc7..2dbb79c 100644 --- a/www/bug.php +++ b/www/bug.php @@ -2,6 +2,7 @@ /* User interface for viewing and editing bug details */ use App\Repository\ObsoletePatchRepository; +use App\Repository\PackageRepository; use App\Repository\PatchRepository; use App\Utils\Captcha; use App\Repository\PullRequestRepository; @@ -182,7 +183,8 @@ $project = $bug['project']; // Only fetch stuff when it's really needed if ($edit && $edit < 3) { - $pseudo_pkgs = get_pseudo_packages(false, false); // false == no read-only packages included + $packageRepository = new PackageRepository($dbh); + $pseudo_pkgs = $packageRepository->findEnabled(); } // Fetch RESOLVE_REASONS array diff --git a/www/lstats.php b/www/lstats.php index b0ccc9a..ff8cc20 100644 --- a/www/lstats.php +++ b/www/lstats.php @@ -1,5 +1,7 @@ <?php +use App\Repository\PackageRepository; + require '../include/prepend.php'; function status_print ($status, $num, $width, $align = STR_PAD_LEFT) @@ -44,8 +46,8 @@ if (!$phpver || ($phpver !== 5 && $phpver !== 7)) { if (isset($_GET['per_category'])) { - $project = !empty($_GET['project']) ? $_GET['project'] : false; - $pseudo_pkgs = get_pseudo_packages($project); + $packageRepository = new PackageRepository($dbh); + $pseudo_pkgs = $packageRepository->findAll($_GET['project'] ?? ''); $totals = []; foreach ($pseudo_pkgs as $category => $data) { diff --git a/www/patch-display.php b/www/patch-display.php index bcf82f5..1735b1c 100644 --- a/www/patch-display.php +++ b/www/patch-display.php @@ -54,8 +54,6 @@ if (!bugs_has_access($bug_id, $buginfo, $pw, $user_flags)) { exit; } -$pseudo_pkgs = get_pseudo_packages(false); - if (isset($patch_name) && isset($revision)) { if ($revision == 'latest') { $revisions = $patchRepository->findRevisions($buginfo['id'], $patch_name); diff --git a/www/report.php b/www/report.php index 9cd6414..4ff4869 100644 --- a/www/report.php +++ b/www/report.php @@ -1,5 +1,6 @@ <?php +use App\Repository\PackageRepository; use App\Utils\Captcha; use App\Utils\PatchTracker; use App\Utils\Uploader; @@ -14,8 +15,8 @@ session_start(); $errors = []; $ok_to_submit_report = false; -$project = !empty($_GET['project']) ? $_GET['project'] : false; -$pseudo_pkgs = get_pseudo_packages($project, false); // false == no read-only packages included +$packageRepository = new PackageRepository($dbh); +$pseudo_pkgs = $packageRepository->findEnabled($_GET['project'] ?? ''); // Authenticate bugs_authenticate($user, $pw, $logged_in, $user_flags); @@ -277,10 +278,8 @@ REPORT; $type = 'unknown'; } - $project = !empty($_GET['project']) ? $_GET['project'] : false; - // provide shortcut URLS for "quick bug fixes" - list($RESOLVE_REASONS, $FIX_VARIATIONS) = get_resolve_reasons($project); + list($RESOLVE_REASONS, $FIX_VARIATIONS) = get_resolve_reasons($_GET['project'] ?? false); $dev_extra = ''; $maxkeysize = 0; diff --git a/www/search.php b/www/search.php index 2ae7571..d99f20e 100644 --- a/www/search.php +++ b/www/search.php @@ -1,5 +1,7 @@ <?php +use App\Repository\PackageRepository; + // Start session session_start(); @@ -256,7 +258,14 @@ display_bug_error($warnings, 'warnings', 'WARNING:'); <td style="white-space: nowrap"> <label for="bug_type">Return bugs with <b>project</b></label> </td> - <td><select id="project" name="project"><?php show_project_options($project, true);?></select></td> + <td><select id="project" name="project"> + <option value="All"<?php if ($project === ''): ?> selected="selected"<?php endif;?>>All</option> + + <?php foreach (PackageRepository::PROJECTS as $key => $value): ?> + <option value="<?= htmlspecialchars($key, ENT_QUOTES); ?>" <?php if ($project === strtolower($key)): ?> selected="selected"<?php endif; ?>><?= htmlspecialchars($key, ENT_QUOTES); ?></option> + <?php endforeach; ?> + </select> + </td> </tr> </table> diff --git a/www/stats.php b/www/stats.php index 06c1d5f..8b161c4 100644 --- a/www/stats.php +++ b/www/stats.php @@ -35,7 +35,6 @@ $pkg_total = []; $pkg_names = []; $all = []; $pseudo = true; -$pseudo_pkgs = get_pseudo_packages($site); if (!array_key_exists($sort_by, $titles)) { $sort_by = 'Open';
-- PHP Webmaster List Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php