xref: /dokuwiki/inc/load.php (revision 653b91a2be74edfe6464286c3047a2627aae9ecc)
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/actions.php');
16require_once(DOKU_INC.'inc/changelog.php');
17require_once(DOKU_INC.'inc/common.php');
18require_once(DOKU_INC.'inc/confutils.php');
19require_once(DOKU_INC.'inc/pluginutils.php');
20require_once(DOKU_INC.'inc/form.php');
21require_once(DOKU_INC.'inc/html.php');
22require_once(DOKU_INC.'inc/httputils.php');
23require_once(DOKU_INC.'inc/infoutils.php');
24require_once(DOKU_INC.'inc/io.php');
25require_once(DOKU_INC.'inc/mail.php');
26require_once(DOKU_INC.'inc/media.php');
27require_once(DOKU_INC.'inc/pageutils.php');
28require_once(DOKU_INC.'inc/parserutils.php');
29require_once(DOKU_INC.'inc/search.php');
30require_once(DOKU_INC.'inc/template.php');
31require_once(DOKU_INC.'inc/toolbar.php');
32require_once(DOKU_INC.'inc/utf8.php');
33require_once(DOKU_INC.'inc/auth.php');
34require_once(DOKU_INC.'inc/compatibility.php');
35require_once(DOKU_INC.'inc/deprecated.php');
36require_once(DOKU_INC.'inc/legacy.php');
37
38/**
39 * spl_autoload_register callback
40 *
41 * Contains a static list of DokuWiki's core classes and automatically
42 * require()s their associated php files when an object is instantiated.
43 *
44 * @author Andreas Gohr <andi@splitbrain.org>
45 * @todo   add generic loading of renderers and auth backends
46 *
47 * @param string $name
48 *
49 * @return bool
50 */
51function load_autoload($name) {
52    static $classes = null;
53    if ($classes === null) $classes = array(
54        'Diff'                  => DOKU_INC.'inc/DifferenceEngine.php',
55        'UnifiedDiffFormatter'  => DOKU_INC.'inc/DifferenceEngine.php',
56        'TableDiffFormatter'    => DOKU_INC.'inc/DifferenceEngine.php',
57        'cache'                 => DOKU_INC.'inc/cache.php',
58        'cache_parser'          => DOKU_INC.'inc/cache.php',
59        'cache_instructions'    => DOKU_INC.'inc/cache.php',
60        'cache_renderer'        => DOKU_INC.'inc/cache.php',
61        'Input'                 => DOKU_INC.'inc/Input.class.php',
62        'JpegMeta'              => DOKU_INC.'inc/JpegMeta.php',
63        'SimplePie'             => DOKU_INC.'inc/SimplePie.php',
64        'FeedParser'            => DOKU_INC.'inc/FeedParser.php',
65        'IXR_Server'            => DOKU_INC.'inc/IXR_Library.php',
66        'IXR_Client'            => DOKU_INC.'inc/IXR_Library.php',
67        'IXR_Error'             => DOKU_INC.'inc/IXR_Library.php',
68        'IXR_IntrospectionServer' => DOKU_INC.'inc/IXR_Library.php',
69        'SafeFN'                => DOKU_INC.'inc/SafeFN.class.php',
70        'Sitemapper'            => DOKU_INC.'inc/Sitemapper.php',
71        'Mailer'                => DOKU_INC.'inc/Mailer.class.php',
72
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        'DokuCLI'                => DOKU_INC.'inc/cli.php',
81        'DokuCLI_Options'        => DOKU_INC.'inc/cli.php',
82        'DokuCLI_Colors'         => DOKU_INC.'inc/cli.php',
83
84    );
85
86    if (isset($classes[$name])) {
87        require ($classes[$name]);
88        return true;
89    }
90
91    // namespace to directory conversion
92    $name = str_replace('\\', '/', $name);
93
94    // test namespace
95    if (substr($name, 0, 14) === 'dokuwiki/test/') {
96        $file = DOKU_INC . '_test/' . substr($name, 14) . '.php';
97        if (file_exists($file)) {
98            require $file;
99            return true;
100        }
101    }
102
103    // plugin namespace
104    if (substr($name, 0, 16) === 'dokuwiki/plugin/') {
105        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
106        $file = DOKU_PLUGIN . substr($name, 16) . '.php';
107        if (file_exists($file)) {
108            require $file;
109            return true;
110        }
111    }
112
113    // template namespace
114    if (substr($name, 0, 18) === 'dokuwiki/template/') {
115        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
116        $file = DOKU_INC.'lib/tpl/' . substr($name, 18) . '.php';
117        if (file_exists($file)) {
118            require $file;
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            require $plg;
145        }
146        return true;
147    }
148    return false;
149}
150
151