xref: /dokuwiki/inc/load.php (revision ba8f83490b45737d4cf33484c72b3aaa21e8894d)
1<?php
2/**
3 * Load all internal libraries and setup class autoloader
4 *
5 * @author Andreas Gohr <andi@splitbrain.org>
6 */
7use dokuwiki\ErrorHandler;
8use dokuwiki\Extension\PluginController;
9
10// setup class autoloader
11spl_autoload_register('load_autoload');
12
13// require all the common libraries
14// for a few of these order does matter
15require_once(DOKU_INC.'inc/defines.php');
16require_once(DOKU_INC.'inc/actions.php');
17require_once(DOKU_INC.'inc/changelog.php');
18require_once(DOKU_INC.'inc/common.php');
19require_once(DOKU_INC.'inc/confutils.php');
20require_once(DOKU_INC.'inc/pluginutils.php');
21require_once(DOKU_INC.'inc/form.php');
22require_once(DOKU_INC.'inc/fulltext.php');
23require_once(DOKU_INC.'inc/html.php');
24require_once(DOKU_INC.'inc/httputils.php');
25require_once(DOKU_INC.'inc/indexer.php');
26require_once(DOKU_INC.'inc/infoutils.php');
27require_once(DOKU_INC.'inc/io.php');
28require_once(DOKU_INC.'inc/mail.php');
29require_once(DOKU_INC.'inc/media.php');
30require_once(DOKU_INC.'inc/pageutils.php');
31require_once(DOKU_INC.'inc/parserutils.php');
32require_once(DOKU_INC.'inc/search.php');
33require_once(DOKU_INC.'inc/template.php');
34require_once(DOKU_INC.'inc/toolbar.php');
35require_once(DOKU_INC.'inc/utf8.php');
36require_once(DOKU_INC.'inc/auth.php');
37require_once(DOKU_INC.'inc/compatibility.php');
38require_once(DOKU_INC.'inc/deprecated.php');
39require_once(DOKU_INC.'inc/legacy.php');
40
41/**
42 * spl_autoload_register callback
43 *
44 * Contains a static list of DokuWiki's core classes and automatically
45 * require()s their associated php files when an object is instantiated.
46 *
47 * @author Andreas Gohr <andi@splitbrain.org>
48 * @todo   add generic loading of renderers and auth backends
49 *
50 * @param string $name
51 *
52 * @return bool
53 */
54function load_autoload($name)
55{
56    static $classes = null;
57    if($classes === null) $classes = [
58        'Diff'                  => DOKU_INC.'inc/DifferenceEngine.php',
59        'UnifiedDiffFormatter'  => DOKU_INC.'inc/DifferenceEngine.php',
60        'TableDiffFormatter'    => DOKU_INC.'inc/DifferenceEngine.php',
61        'cache'                 => DOKU_INC.'inc/cache.php',
62        'cache_parser'          => DOKU_INC.'inc/cache.php',
63        'cache_instructions'    => DOKU_INC.'inc/cache.php',
64        'cache_renderer'        => DOKU_INC.'inc/cache.php',
65        'Input'                 => DOKU_INC.'inc/Input.class.php',
66        'JpegMeta'              => DOKU_INC.'inc/JpegMeta.php',
67        'SimplePie'             => DOKU_INC.'inc/SimplePie.php',
68        'FeedParser'            => DOKU_INC.'inc/FeedParser.php',
69        'SafeFN'                => DOKU_INC.'inc/SafeFN.class.php',
70        'Mailer'                => DOKU_INC.'inc/Mailer.class.php',
71        'Doku_Handler'          => DOKU_INC.'inc/parser/handler.php',
72        'Doku_Renderer'          => DOKU_INC.'inc/parser/renderer.php',
73        'Doku_Renderer_xhtml'    => DOKU_INC.'inc/parser/xhtml.php',
74        'Doku_Renderer_code'     => DOKU_INC.'inc/parser/code.php',
75        'Doku_Renderer_xhtmlsummary' => DOKU_INC.'inc/parser/xhtmlsummary.php',
76        'Doku_Renderer_metadata' => DOKU_INC.'inc/parser/metadata.php'
77    ];
78
79    if(isset($classes[$name])){
80        require($classes[$name]);
81        return true;
82    }
83
84    // namespace to directory conversion
85    $name = str_replace('\\', '/', $name);
86
87    // test mock namespace
88    if (substr($name, 0, 19) === 'dokuwiki/test/mock/') {
89        $file = DOKU_INC . '_test/mock/' . substr($name, 19) . '.php';
90        if (file_exists($file)) {
91            require $file;
92            return true;
93        }
94    }
95
96    // tests namespace
97    if (substr($name, 0, 14) === 'dokuwiki/test/') {
98        $file = DOKU_INC . '_test/tests/' . substr($name, 14) . '.php';
99        if (file_exists($file)) {
100            require $file;
101            return true;
102        }
103    }
104
105    // plugin namespace
106    if(substr($name, 0, 16) === 'dokuwiki/plugin/') {
107        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
108        $file = DOKU_PLUGIN . substr($name, 16) . '.php';
109        if(file_exists($file)) {
110            try {
111                require $file;
112            } catch (\Throwable $e) {
113                ErrorHandler::showExceptionMsg($e, "Error loading plugin $name");
114            }
115            return true;
116        }
117    }
118
119    // template namespace
120    if(substr($name, 0, 18) === 'dokuwiki/template/') {
121        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
122        $file = DOKU_INC.'lib/tpl/' . substr($name, 18) . '.php';
123        if(file_exists($file)) {
124            try {
125                require $file;
126            } catch (\Throwable $e) {
127                ErrorHandler::showExceptionMsg($e, "Error loading template $name");
128            }
129            return true;
130        }
131    }
132
133    // our own namespace
134    if(substr($name, 0, 9) === 'dokuwiki/') {
135        $file = DOKU_INC . 'inc/' . substr($name, 9) . '.php';
136        if(file_exists($file)) {
137            require $file;
138            return true;
139        }
140    }
141
142    // Plugin loading
143    if(preg_match(
144        '/^(' . implode('|', PluginController::PLUGIN_TYPES) . ')_plugin_(' .
145        DOKU_PLUGIN_NAME_REGEX .
146        ')(?:_([^_]+))?$/',
147        $name,
148        $m
149    )) {
150        // try to load the wanted plugin file
151        $c = ((count($m) === 4) ? "/{$m[3]}" : '');
152        $plg = DOKU_PLUGIN . "{$m[2]}/{$m[1]}$c.php";
153        if(file_exists($plg)){
154            try {
155                require $plg;
156            } catch (\Throwable $e) {
157                ErrorHandler::showExceptionMsg($e, "Error loading plugin {$m[2]}");
158            }
159        }
160        return true;
161    }
162    return false;
163}
164