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