Skip to content

Commit

Permalink
moved DB class to the GitPHP namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Md-Cake committed Sep 22, 2018
1 parent adb96cf commit 44b5417
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.unrealsync
repositories/*
.setup/storage/*
php_errors.log
37 changes: 19 additions & 18 deletions .include/Db.class.php → .include/Db.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

class GitPHP_Db
namespace GitPHP;

class Db
{
const TBL_HEADS = 'Heads';
const TBL_COMMENT = 'Comment';
const TBL_SNAPSHOT = 'Snapshot';
const TBL_REVIEW = 'Review';
const TBL_HEADS = 'Heads';
const TBL_COMMENT = 'Comment';
const TBL_SNAPSHOT = 'Snapshot';
const TBL_REVIEW = 'Review';
const TBL_USER = 'User';
const TBL_ACCESS = 'Access';
const TBL_REPOSITORY = 'Repository';
Expand Down Expand Up @@ -130,18 +132,18 @@ public static function getInstance()

public function __construct()
{
$config = \GitPHP\Config::GetInstance();
$host = $config->GetValue(GitPHP\Config::DB_HOST);
$user = $config->GetValue(GitPHP\Config::DB_USER);
$password = $config->GetValue(GitPHP\Config::DB_PASSWORD);
$this->db = $config->GetValue(GitPHP\Config::DB_NAME);
$config = Config::GetInstance();
$host = $config->GetValue(Config::DB_HOST);
$user = $config->GetValue(Config::DB_USER);
$password = $config->GetValue(Config::DB_PASSWORD);
$this->db = $config->GetValue(Config::DB_NAME);
if (substr($host, 0, 1) == ':') {
$this->link = mysqli_connect(null, $user, $password, '', 0, substr($host, 1));
} else {
$this->link = mysqli_connect($host, $user, $password);
}
if (!$this->link) {
GitPHP_Log::GetInstance()->Log('mysqli_connect', "$user@$host");
\GitPHP_Log::GetInstance()->Log('mysqli_connect', "$user@$host");
}
}

Expand All @@ -153,21 +155,21 @@ public function __construct()
public function query($sql, $params)
{
$sql = $this->bind($sql, $params);
GitPHP_Log::GetInstance()->timerStart();
\GitPHP_Log::GetInstance()->timerStart();
$result = mysqli_query($this->link, $sql);
GitPHP_Log::GetInstance()->timerStop('mysqli_query', $sql);
\GitPHP_Log::GetInstance()->timerStop('mysqli_query', $sql);

if (!$result) {
$this->errno = mysqli_errno($this->link);
$this->error = mysqli_error($this->link);
$msg = "db_error: $this->errno:$this->error " . (new \Exception());
trigger_error($msg);
GitPHP_Log::GetInstance()->Log('mysqli_error', "$this->errno: $this->error");
\GitPHP_Log::GetInstance()->Log('mysqli_error', "$this->errno: $this->error");
} else {
if (is_resource($result)) $this->numRows = mysqli_num_rows($result);
$this->numRows = mysqli_num_rows($result);
$this->affectedRows = mysqli_affected_rows($this->link);
$this->insert_id = mysqli_insert_id($this->link);
$result = new \GitPHP\Db_Result($result);
$result = new Db_Result($result);
}
return $result;
}
Expand Down Expand Up @@ -508,7 +510,7 @@ protected function getTablesAsDbwrapper()
if (!isset($tables)) {
$tables = [];

$refl = new ReflectionClass(get_class($this));
$refl = new \ReflectionClass(get_class($this));

$prefix = 'TBL_';

Expand All @@ -519,5 +521,4 @@ protected function getTablesAsDbwrapper()
}
return $tables;
}

}
22 changes: 16 additions & 6 deletions .include/Jira.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ public function restAuthenticateByUsernameAndPassword($username, $password)
return [null, $err];
}

public function restAuthenticateByCookie($cookie) {
public function restAuthenticateByCookie($cookie)
{
$err = null;
$Myself = $this->request($this->url, 'GET', 'rest/api/2/myself', null, ['X-Atlassian-Token: nocheck', 'Cookie: ' . self::getCookieName() . '=' . $cookie]);
$Myself = $this->request(
$this->url,
'GET',
'rest/api/2/myself',
null,
['X-Atlassian-Token: nocheck', 'Cookie: ' . self::getCookieName() . '=' . $cookie]
);
if ($Myself->status_code != 200) {
$err = 'REST authentication failure!';
}
Expand Down Expand Up @@ -181,10 +188,13 @@ public function restIsGroupMember($user_id, $group_name)
protected function request($url, $http_method, $method, $data = null, $headers = [])
{
$Counter = new \CountClass(__METHOD__, "$http_method $method");
$headers = array_merge($headers, [
'Accept: application/json',
'Content-Type: application/json',
]);
$headers = array_merge(
$headers,
[
'Accept: application/json',
'Content-Type: application/json',
]
);

if (!empty($this->crowd_token)) {
array_push($headers, 'Authorization: Basic ' . $this->crowd_token);
Expand Down
2 changes: 1 addition & 1 deletion .include/Model_Gitosis.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Model_Gitosis

public function __construct()
{
$this->db = GitPHP_Db::getInstance();
$this->db = \GitPHP\Db::getInstance();
}

/* User */
Expand Down
6 changes: 3 additions & 3 deletions .include/Util.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static function insertCommentsToDiffObj($comments, $file, $Diffs, $diff_s
$result['TOP_COMMENT'][] = [
'date' => self::formatDate(strtotime($comment['date'])),
'author' => $comment['author'],
'comment' => $comment['text'] /* already escaped, see \GitPHP_Db::addComment */,
'comment' => $comment['text'] /* already escaped, see \GitPHP\Db::addComment */,
];
}

Expand Down Expand Up @@ -345,8 +345,8 @@ public static function insertCommentsToDiffObj($comments, $file, $Diffs, $diff_s
foreach ($commentLines[$i] as $commentId) {
$current_line['COMMENT'][] = [
'date' => self::formatDate(strtotime($comments[$commentId]['date'])),
'author' => $comments[$commentId]['author']/* already escaped, see \GitPHP_Db::addComment */,
'comment' => $comments[$commentId]['text']/* already escaped, see \GitPHP_Db::addComment */,
'author' => $comments[$commentId]['author']/* already escaped, see \GitPHP\Db::addComment */,
'comment' => $comments[$commentId]['text']/* already escaped, see \GitPHP\Db::addComment */,
];
}
}
Expand Down
4 changes: 2 additions & 2 deletions .include/controller/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ protected function LoadCommonData()
$this->tpl->assign('ticket_href', \GitPHP\Tracker::instance()->getTicketUrl($ticket));
$this->tpl->assign(
'fixlineheight',
isset($_COOKIE[\GitPHP\Application::GITPHP_FIX_LINEHEIGHT_COOKIE]) && $_COOKIE[\GitPHP_Application::GITPHP_FIX_LINEHEIGHT_COOKIE]
isset($_COOKIE[\GitPHP\Application::GITPHP_FIX_LINEHEIGHT_COOKIE]) && $_COOKIE[\GitPHP\Application::GITPHP_FIX_LINEHEIGHT_COOKIE]
);
}

Expand Down Expand Up @@ -490,7 +490,7 @@ protected function guesssTicket()
}
}
if ($review) {
$reviewObj = \GitPHP_Db::getInstance()->findReviewById($review);
$reviewObj = \GitPHP\Db::getInstance()->findReviewById($review);
$ticket = \GitPHP\Tracker::instance()->parseTicketFromString($reviewObj['ticket']);
}
return $ticket;
Expand Down
2 changes: 1 addition & 1 deletion .include/controller/Branchdiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function LoadData()
$co = $this->project->GetCommit($this->params['branch']);
$toHash = null;
if (!$co) {
$co = \GitPHP_Db::getInstance()->getBranchHead($this->params['branch']);
$co = \GitPHP\Db::getInstance()->getBranchHead($this->params['branch']);
if ($co) $co = $this->project->GetCommit($co);
if (!$co) return;
$toHash = $co->GetHash();
Expand Down
4 changes: 2 additions & 2 deletions .include/controller/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class Comment extends Base
private $response;

/**
* @var \GitPHP_Db
* @var \GitPHP\Db
*/
private $db;

public function __construct()
{
parent::__construct();
$this->db = \GitPHP_Db::getInstance();
$this->db = \GitPHP\Db::getInstance();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion .include/controller/DiffBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function loadReviewsLinks(\GitPHP_Commit $co, $ticket)
$ticket = \GitPHP\Tracker::instance()->getReviewTicketPrefix() . $key;
}

$Db = \GitPHP_Db::getInstance();
$Db = \GitPHP\Db::getInstance();
$reviews = $Db->getReview($ticket, $co->GetHash());

$comments_count = $Db->getCommentsCountForReviews(array_keys($reviews), $this->Session->getUser()->getId());
Expand Down
2 changes: 1 addition & 1 deletion .include/controller/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected function loadBranchHead()
if (!empty($head)) {
$hash = $head->GetHash();
if ($persist) {
$res = \GitPHP_Db::getInstance()->saveBranchHead($branch, $hash);
$res = \GitPHP\Db::getInstance()->saveBranchHead($branch, $hash);
$response['status'] = ($res ? 'ok' : 'err');
}
}
Expand Down
4 changes: 2 additions & 2 deletions .include/controller/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function LoadData()
$co = $this->project->GetCommit($this->params['hash']);
$toHash = null;
if (!$co) {
$co = \GitPHP_Db::getInstance()->getBranchHead($this->params['hash']);
$co = \GitPHP\Db::getInstance()->getBranchHead($this->params['hash']);
if ($co) $co = $this->project->GetCommit($co);
if (!$co) return;
$toHash = $co->GetHash();
Expand Down Expand Up @@ -153,7 +153,7 @@ protected function LoadData()
$revlist_index[$commit->GetHash()] = $idx;
}
foreach($revlist_hashes as $revlist_hash) {
$reviews = \GitPHP_Db::getInstance()->findSnapshotsByHash($revlist_hash);
$reviews = \GitPHP\Db::getInstance()->findSnapshotsByHash($revlist_hash);
foreach ($reviews as $hash => $review) {
$revlist[$revlist_index[$hash]]->setReview($review);
}
Expand Down
2 changes: 1 addition & 1 deletion .include/controller/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function ReadQuery()
*/
protected function LoadData()
{
$db = \GitPHP_Db::getInstance();
$db = \GitPHP\Db::getInstance();

$to_start_link = $more_link = null;
$this->tpl->assign('head', '');
Expand Down
1 change: 0 additions & 1 deletion autoload.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
return array(
'GitPHP_Db' => GITPHP_INCLUDEDIR . 'Db.class.php',
'GitPHP_Log' => GITPHP_INCLUDEDIR . 'Log.class.php',
'GitPHP_MessageException' => GITPHP_INCLUDEDIR . 'MessageException.class.php',
'GitPHP_Resource' => GITPHP_INCLUDEDIR . 'Resource.class.php',
Expand Down

0 comments on commit 44b5417

Please sign in to comment.