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