1<?php
2
3/**
4 * Legacy command line upgrade script
5 *
6 * This script can be used to upgrade old versions of DokuWiki that won't easily run on
7 * modern PHP releases. It works by not actually loading any of the existing (and outdated)
8 * DokuWiki code, but instead fakes an absolute minimal environment to run the upgrade.
9 *
10 * This means this script will make more assumptions and take shortcuts:
11 *
12 * - no proxy support
13 * - no tmp dir changes
14 * - english only
15 * - only "normal" releases (no snapshots or git checkouts)
16 *
17 * Only use this if you can't run the normal upgrade script.
18 */
19
20// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
21
22require_once __DIR__ . '/vendor/autoload.php';
23
24// fake a minimal dokuwiki environment
25define('DOKU_INC', __DIR__ . '/../../../');
26global $conf;
27$conf['savedir'] = __DIR__ . '/../../../data/';
28$conf['cachedir'] = $conf['savedir'] . 'cache/';
29$conf['tmpdir'] = $conf['savedir'] . 'tmp/';
30$conf['proxy'] = ['host' => '', 'port' => '', 'user' => '', 'pass' => '', 'ssl' => '', 'except' => ''];
31$conf['allowdebug'] = false;
32
33function linesToHash($lines)
34{
35    $lines = array_map('trim', $lines);
36    $lines = array_filter($lines);
37    $lines = array_map(function ($item) {
38        return array_map('trim', explode(' ', $item, 2));
39    }, $lines);
40    return array_combine(array_column($lines, 0), array_column($lines, 1));
41}
42
43function conf_decodeString($string)
44{
45    return $string;
46}
47
48function filesize_h($size)
49{
50    return $size . 'b';
51}
52
53function hsc($string)
54{
55    return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
56}
57
58function io_mkdir_p($dir)
59{
60    if (file_exists($dir)) return true;
61    return mkdir($dir, 0777, true);
62}
63
64function getVersionData()
65{
66    $version = array();
67    if (file_exists(DOKU_INC . 'VERSION')) {
68        //official release
69        $version['date'] = trim(file_get_contents(DOKU_INC . 'VERSION'));
70        $version['type'] = 'Release';
71    }
72    return $version;
73}
74
75function getVersion()
76{
77    $version = getVersionData();
78    return $version['type'] . ' ' . $version['date'];
79}
80
81class Doku_Event
82{
83    public function __construct($name, &$data)
84    {
85    }
86
87    public function advise_before()
88    {
89        return true;
90    }
91
92    public function advise_after()
93    {
94    }
95}
96
97trait UpgradePluginTrait
98{
99    protected $lang = null;
100
101    /**
102     * @return string
103     */
104    public function getInfo()
105    {
106        $data = file(__DIR__ . '/plugin.info.txt', FILE_IGNORE_NEW_LINES);
107        return linesToHash($data);
108    }
109
110    /**
111     * @param string $key
112     * @return string
113     */
114    public function getLang($key)
115    {
116        if ($this->lang === null) {
117            $lang = [];
118            include __DIR__ . '/lang/en/lang.php';
119            $this->lang = $lang;
120        }
121        return $this->lang[$key] ?? $key;
122    }
123}
124
125abstract class DokuWiki_CLI_Plugin extends splitbrain\phpcli\CLI
126{
127    use UpgradePluginTrait;
128}
129
130class DokuWiki_Plugin
131{
132    use UpgradePluginTrait;
133}
134
135// now the CLI plugin should load and run
136include(__DIR__ . '/cli.php');
137(new cli_plugin_upgrade())->run();
138