<?php
/*
* PHP svn functions
* Copyright 2021 - 2023 Daniel Marschall, ViaThinkSoft
* Revision 2023-09-15
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function get_svn_revision($dir='') {
if (!empty($dir)) $dir .= '/';
if (!is_dir($dir)) return false;
if (is_dir($dir.'/.svn')) $dir .= '/.svn/';
// Try to find out the SVN version using the shell
return ($cachedVersion = $match[0]);
}
if (preg_match('/Revision:\s*(\d+)/m', $output, $match)) { // do not translate
return ($cachedVersion = $match[1]);
}
// If that failed, try to get the version via access of the database files
try {
$db = new SQLite3($dir.'.svn/wc.db');
$results = $db->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
while ($row = $results->fetchArray()) {
return ($cachedVersion = $row['rev']);
}
$db->close();
$db = null;
} catch (Exception $e) {
}
}
try {
$pdo = new PDO('sqlite:'.$dir.'.svn/wc.db');
$res = $pdo->query('SELECT MIN(revision) AS rev FROM NODES_BASE');
$row = $res->fetch();
if ($row !== false) {
return ($cachedVersion = $row['rev']);
}
$pdo = null;
} catch (Exception $e) {
}
}
// We couldn't get the revision info
// Try parsing the binary file. It is a bit risky though...
return get_svn_revision_without_sqlite3($dir);
}
// Note: get_svn_revision_without_sqlite3() can be very unstable, so it is highly recommended to install php-sqlite in order to parse the file correctly.
function get_svn_revision_without_sqlite3($svn_path, $base='trunk') {
if (!empty($svn_path)) $svn_path .= '/';
if (!is_dir($svn_path)) return false;
if (!is_dir($svn_path.'/.svn')) $svn_path .= '/../';
preg_match_all('@('.preg_quote($base,'@').'/[a-z0-9!"#$%&\'()*+,.\/:;<=>?\@\[\] ^_`{|}~-]+)(..)normal(file|dir)@', $fil, $m, PREG_SET_ORDER
);
foreach ($m as list($dummy, $fil, $revision, $type)) {
// TODO: Problem: We don't know if it was checked out as / or checked out as /trunk/, or something else!
if (!file_exists($svn_path."/$base/$fil") && !file_exists($svn_path."/$fil")) continue; // deleted files (deleted rows?!) might be still in the binary
if (!isset($files[$fil])) $files[$fil] = -1;
if ($files[$fil] < $val) $files[$fil] = $val;
}
/*
foreach ($files as $name => $val) {
if ($val != 1228) echo "DEBUG Unexpected: $val / $fil\n";
}
*/
if (count($files) == 0) return 1; // should not happen
$middleVal = floor(($num - 1) / 2);
if ($num % 2) {
$median = $arr[$middleVal];
} else {
$lowMid = $arr[$middleVal];
$highMid = $arr[$middleVal + 1];
$median = (($lowMid + $highMid) / 2);
}
return $median;
}