blob: 3feea60e719bac43a69eb1cc9fdc2680b892b346 [file] [log] [blame]
<?php
/*******************************************************************************
* Copyright (c) 2017 Eclipse Foundation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
require_once(dirname(__FILE__) . '/../classes/Project.class.php');
require_once(dirname(__FILE__) . '/../classes/common.php');
require_once(dirname(__FILE__) . '/../classes/database.inc');
class IPZilla {
var $cqs = array();
public static function getInstance() {
$ipzilla = new IPZilla();
$ipzilla->load();
return $ipzilla;
}
public function getCq($id) {
return @$this->cqs[$id];
}
public function rootCqsDo($function) {
foreach($this->cqs as $cq) {
if ($cq->isRoot()) $function($cq);
}
}
public function asArray() {
$data = array();
$this->rootCqsDo(function($cq) use (&$data) {
$data[$cq->getId()] = $cq->asArray();
});
return $data;
}
private function load() {
$sql = "
select
cq.id, cq.root, cq.project, cq.name,
cq.version, cq.description,
cq.cq_type, cq.dd_type, cq.created,
cq.status, cq.resolution, cq.state,
cq.licenses, cq.spdx, cq.keywords,
group_concat(piggyback.id) as piggybacks
from CQ as cq
left join CQ as piggyback on cq.id != piggyback.id and cq.id=piggyback.root
where cq.state in ('approved', 'license_certified')
and cq.cq_type in ('thirdparty', 'piggyback')
group by cq.id
";
query('dashboard', $sql, array(), function($row) {
$this->cqs[$row['id']] = new CQ($this, $row);
});
$this->initialize();
$this->loadFilePatterns();
$this->loadMvnPatterns();
$this->loadExpressions();
}
private function initialize() {
// foreach($this->cqs as $cq) {
// if ($cq->isRoot()) continue;
// $cq->getRoot()->data['piggybacks'][$cq->getId()] = $cq->getProjectId() ;
// }
}
private function loadFilePatterns() {
query('dashboard', 'select distinct cq, pattern from CQBundleMap', array(), function($row) {
if ($cq = @$this->cqs[$row['cq']]) {
$cq->addFilePattern($row['pattern']);
$cq->data['bundles'][] = $row['pattern'];
}
});
query('dashboard', 'select distinct cq, name, version, file from CQOrbit', array(), function($row) {
if ($cq = @$this->cqs[$row['cq']]) {
$cq->addFilePattern($row['file']);
$cq->data['orbit'][] = $row;
}
});
}
private function loadMvnPatterns() {
$sql = "
select distinct
mvn.cq,
mvn.pattern,
group_concat(distinct r.cq) as related
from CQMavenMap as mvn
join CQMavenMap as r
on mvn.groupid=r.groupid
and mvn.artifactid=r.artifactid
group by mvn.cq;
";
query('dashboard', $sql, array(), function($row) use (&$maven) {
if ($cq = @$this->cqs[$row['cq']]) {
$cq->addMvnPattern($row['pattern']);
$cq->data['related'] = explode(',', $row['related']);
}
});
}
private function loadExpressions() {
$sql = "
select distinct
cq,
expression
from CQExpression;
";
query('dashboard', $sql, array(), function($row) {
if ($cq = @$this->cqs[$row['cq']]) {
$cq->data['expressions'][] = $row['expression'];
}
});
}
}
class CQ {
var $ipzilla;
var $data;
var $root;
var $keywords;
public function __construct($ipzilla, $data) {
$this->ipzilla = $ipzilla;
$this->data = $data;
// Cache information
$this->keywords = preg_split('/\s*,\s*/',$this->data['keywords']);
}
public function getId() {
return $this->data['id'];
}
public function getName() {
return $this->data['name'];
}
public function getVersion() {
return $this->data['version'];
}
public function getDescription() {
return $this->data['description'];
}
public function getLicenses() {
return $this->data['licenses'];
}
public function getSPDX() {
return $this->data['spdx'];
}
public function getProject() {
return Project::getProject($this->getProjectId());
}
public function getProjectId() {
return $this->data['project'];
}
public function getPiggybackIds() {
if (empty($this->data['piggybacks'])) return array();
return explode(',', $this->data['piggybacks']);
}
public function getPiggybacks() {
$piggybacks = array();
foreach($this->getPiggybackIds() as $id) {
if ($cq = $this->ipzilla->getCq($id)) {
$piggybacks[] = $cq;
}
}
return $piggybacks;
}
public function getState() {
return $this->data['state'];
}
public function getStatus() {
return $this->data['status'];
}
public function getResolution() {
return $this->data['resolution'];
}
public function getKeywords() {
return $this->data['keywords'];
}
public function getFilePatterns() {
return @$this->data['filePatterns'];
}
public function getMvnPatterns() {
return @$this->data['mvnPatterns'];
}
public function getType() {
return $this->data['cq_type'];
}
public function getDueDiligenceType() {
return $this->data['dd_type'];
}
public function isRoot() {
return $this == $this->getRoot();
}
public function getRoot() {
if ($id = $this->getRootId()) {
return $this->ipzilla->getCQ($id);
}
return $this;
}
public function getRootId() {
return $this->data['root'];
}
public function getParent() {
if ($id = $this->getParentId()) {
return $this->ipzilla->getCq($id);
}
return null;
}
private function getParentId() {
if (preg_match ( '/\W(?:\(ATO|PB|Orbit)[^\d\)]*([0-9]+)/i', $this->getDescription(), $matches)) {
return $matches [1];
}
return null;
}
public function isResolved() {
if (!$this->getStatus()) return false;
return in_array($this->getStatus(), array('RESOLVED', 'VERIFIED', 'CLOSED'));
}
public function isFixed() {
if (!$this->isResolved()) return false;
return $this->getResolution() == 'FIXED';
}
public function isApproved() {
if (!$this->isResolved()) return false;
return $this->getState() == 'approved';
}
public function isProjectCode() {
return in_array('projectcode', $this->keywords);
}
public function isThirdParty() {
return in_array('thirdparty', $this->keywords);
}
function asArray() {
return $this->data;
}
function addFilePattern($value) {
if (preg_match('/(\S*\/)?([\w\.\-]+)[_-]((?:\d+\.){3})(.*\.)?jar/', $value, $matches)) {
$pattern = $matches[1] . $matches[2] . '_' . $matches[3];
$regex = $matches[1] . $matches[2] . '[_-]' . $matches[3];
$regex = str_replace('.', '\.', $regex);
$regex = str_replace('/', '\/', $regex);
$regex = $regex . '(.*\.)?jar';
$pattern = "$pattern*.jar";
$this->data['filePatterns'][$pattern] = $regex;
// Only generate a source bundle mapping if this isn't a source bundle.
if (!preg_match('/\.source$/', $matches[2])) {
$sourcePattern = $matches[1] . $matches[2]. '.source_' . $matches[3];
$sourceRegex = $matches[1] . $matches[2] . '.source[_-]' . $matches[3];
$sourceRegex = str_replace('.', '\.', $sourceRegex);
$regex = str_replace('/', '\/', $regex);
$sourceRegex = $sourceRegex . '(.*\.)?jar';
$sourcePattern = "$sourcePattern*.jar";
$this->data['filePatterns'][$pattern] = $regex;
}
} else {
$pattern = $value;
$regex = str_replace('.', '\.', $pattern);
$regex = str_replace('/', '\/', $regex);
$this->data['filePatterns'][$pattern] = $regex;
}
}
function addMvnPattern($value) {
if (isset($this->data['mvnPatterns']) && in_array($value, $this->data['mvnPatterns'])) return;
$this->data['mvnPatterns'][] = $value;
}
function getRelatedCqs() {
$related = array();
if (isset($this->data['related'])){
foreach($this->data['related'] as $id) {
if ($id == $this->getId()) continue;
if ($cq = $this->ipzilla->getCQ($id)) {
$related[] = $cq;
}
}
}
return $related;
}
}
?>