File: GitBackedUtil.php

package info (click to toggle)
dokuwiki-plugins-extra 20251207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,648 kB
  • sloc: php: 88,796; javascript: 64,519; makefile: 112; sh: 76; xml: 71
file content (178 lines) | stat: -rw-r--r-- 5,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

namespace woolfg\dokuwiki\plugin\gitbacked;

/*
 * GitBackedUtil.php
 *
 * PHP common utility function lib
 *
 * @package    GitBackedUtil.php
 * @version    1.0
 * @author     Markus Hoffrogge
 * @copyright  Copyright 2023 Markus Hoffrogge
 * @repo       https://github.com/woolfg/dokuwiki-plugin-gitbacked
 */

// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

// ------------------------------------------------------------------------

/**
 * GitBacked Utility Class
 *
 * This class provides common utility functions.
 *
 * @class  GitBackedUtil
 */
class GitBackedUtil
{
    /**
     * GitBacked temp directory
     *
     * @var string
     */
    protected static $temp_dir = '';

    /**
     * Checks, if a given path name is an absolute path or not.
     * This function behaves like absolute path determination in dokuwiki init.php fullpath() method.
     * The relevant code has been copied from there.
     *
     * @access  public
     * @param   string $path    a file path name
     * @return  bool
     */
    public static function isAbsolutePath($path)
    {
        $ret = false;

        $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']));
        // check for the (indestructable) root of the path - keeps windows stuff intact
        if ($path[0] == '/') {
            $ret = true;
        } elseif ($iswin) {
            // match drive letter and UNC paths
            if (preg_match('!^([a-zA-z]:)(.*)!', $path, $match)) {
                $ret = true;
            } elseif (preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!', $path, $match)) {
                $ret = true;
            }
        }

        return $ret;
    }

    /**
     * Returns the $path as is, if it is an absolute path.
     * Otherwise it will prepend the base path appropriate
     * to the type of DW instance.
     *
     * @access  public
     * @param   string $path    a file path name
     * @return  string          an appropriate absolute path
     */
    public static function getEffectivePath($path)
    {
        $ret = $path;

        if (self::isAbsolutePath($ret)) {
            return $ret;
        }
        if (defined('DOKU_FARM')) {
            $ret = DOKU_CONF . '../' . $ret;
        } else {
            $ret = DOKU_INC . $ret;
        }
        return $ret;
    }

    /**
     * Returns the temp dir for GitBacked plugin.
     * It ensures that the temp dir will be created , if not yet existing.
     *
     * @access  public
     * @return  string          the gitbacked temp directory name
     */
    public static function getTempDir()
    {
        if (empty(self::$temp_dir)) {
            global $conf;
            self::$temp_dir = $conf['tmpdir'] . '/gitbacked';
            io_mkdir_p(self::$temp_dir);
        }
        return self::$temp_dir;
    }

    /**
     * Creates a temp file and writes the $message to it.
     * It ensures that the temp dir will be created , if not yet existing.
     *
     * @access  public
     * @param   string $message the text message
     * @return  string          the temp filename created
     */
    public static function createMessageFile($message)
    {
        $tmpfname = tempnam(self::getTempDir(), 'gitMessage_');
        $handle = fopen($tmpfname, "w");
        if (!empty($message)) {
            fwrite($handle, $message);
        }
        fclose($handle);
        return $tmpfname;
    }

    /**
     * Determine closest git repository path for a given path as absolute PHP realpath().
     * This search starts in $path - if $path does not contain .git,
     * it will iterate the directories upwards as long as it finds a directory
     * containing a .git folder.
     *
     * @access  public
     * @return  string  the next git repo root dir as absolute PHP realpath()
     *                  or empty string, if no git repo found.
     */
    public static function getClosestAbsoluteRepoPath($path)
    {
        $descriptorspec = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
        $pipes = [];
        // Using --git-dir rather than --absolute-git-dir for a wider git versions compatibility
        //$command = Git::getBin()." rev-parse --absolute-git-dir";
        $command = Git::getBin() . " rev-parse --git-dir";
        //dbglog("GitBacked - Command: ".$command);
        $resource = proc_open($command, $descriptorspec, $pipes, $path);
        $stdout = stream_get_contents($pipes[1]);
        stream_get_contents($pipes[2]);
        foreach ($pipes as $pipe) {
            fclose($pipe);
        }

        $status = trim(proc_close($resource));
        if ($status == 0) {
            $repo_git_dir = trim($stdout);
            //dbglog("GitBacked - $command: '".$repo_git_dir."'");
            if (!empty($repo_git_dir)) {
                if (strcmp($repo_git_dir, ".git") === 0) {
                    // convert to absolute path based on this command execution directory
                    $repo_git_dir = $path . '/' . $repo_git_dir;
                }
                $repo_path = dirname(realpath($repo_git_dir));
                $doku_inc_path = dirname(realpath(DOKU_INC));
                if (strlen($repo_path) < strlen($doku_inc_path)) {
                    // This code should never be reached!
                    // If we get here, then we are beyond DOKU_INC - so this not supposed to be for us!
                    return '';
                }
                //dbglog('GitBacked - $repo_path: '.$repo_path);
                if (file_exists($repo_path . "/.git") && is_dir($repo_path . "/.git")) {
                    return $repo_path;
                }
            }
        }
        return '';
    }
}
/* End of file */