xref: /dokuwiki/inc/load.php (revision 7d34963b3e75ea04c63ec066a6b7a692e123cb53)
116905344SAndreas Gohr<?php
216905344SAndreas Gohr/**
316905344SAndreas Gohr * Load all internal libraries and setup class autoloader
416905344SAndreas Gohr *
516905344SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
616905344SAndreas Gohr */
724870174SAndreas Gohruse dokuwiki\ErrorHandler;
81935a891SAndreas Gohruse dokuwiki\Extension\PluginController;
91935a891SAndreas Gohr
1016905344SAndreas Gohr// setup class autoloader
1116905344SAndreas Gohrspl_autoload_register('load_autoload');
1216905344SAndreas Gohr
1316905344SAndreas Gohr// require all the common libraries
1405ed2c25SAndreas Gohr// for a few of these order does matter
1546028c4cSAndreas Gohrrequire_once(DOKU_INC.'inc/defines.php');
1616905344SAndreas Gohrrequire_once(DOKU_INC.'inc/actions.php');
1716905344SAndreas Gohrrequire_once(DOKU_INC.'inc/changelog.php');
1816905344SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
1916905344SAndreas Gohrrequire_once(DOKU_INC.'inc/confutils.php');
2016905344SAndreas Gohrrequire_once(DOKU_INC.'inc/pluginutils.php');
2116905344SAndreas Gohrrequire_once(DOKU_INC.'inc/form.php');
2216905344SAndreas Gohrrequire_once(DOKU_INC.'inc/fulltext.php');
2316905344SAndreas Gohrrequire_once(DOKU_INC.'inc/html.php');
2416905344SAndreas Gohrrequire_once(DOKU_INC.'inc/httputils.php');
2516905344SAndreas Gohrrequire_once(DOKU_INC.'inc/indexer.php');
2616905344SAndreas Gohrrequire_once(DOKU_INC.'inc/infoutils.php');
2716905344SAndreas Gohrrequire_once(DOKU_INC.'inc/io.php');
2816905344SAndreas Gohrrequire_once(DOKU_INC.'inc/mail.php');
2916905344SAndreas Gohrrequire_once(DOKU_INC.'inc/media.php');
3016905344SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php');
3116905344SAndreas Gohrrequire_once(DOKU_INC.'inc/parserutils.php');
3216905344SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php');
3316905344SAndreas Gohrrequire_once(DOKU_INC.'inc/template.php');
3416905344SAndreas Gohrrequire_once(DOKU_INC.'inc/toolbar.php');
3516905344SAndreas Gohrrequire_once(DOKU_INC.'inc/utf8.php');
3616905344SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
376589c60cSAndreas Gohrrequire_once(DOKU_INC.'inc/compatibility.php');
384cdb6842SAndreas Gohrrequire_once(DOKU_INC.'inc/deprecated.php');
39e1d9dcc8SAndreas Gohrrequire_once(DOKU_INC.'inc/legacy.php');
4016905344SAndreas Gohr
4116905344SAndreas Gohr/**
4216905344SAndreas Gohr * spl_autoload_register callback
4316905344SAndreas Gohr *
4416905344SAndreas Gohr * Contains a static list of DokuWiki's core classes and automatically
452dcde304SAndreas Gohr * require()s their associated php files when an object is instantiated.
4616905344SAndreas Gohr *
4716905344SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
48d54ac877SAdrian Lang * @todo   add generic loading of renderers and auth backends
49f50a239bSTakamura *
50f50a239bSTakamura * @param string $name
51f50a239bSTakamura *
52f50a239bSTakamura * @return bool
5316905344SAndreas Gohr */
54d868eb89SAndreas Gohrfunction load_autoload($name)
55d868eb89SAndreas Gohr{
5616905344SAndreas Gohr    static $classes = null;
5724870174SAndreas Gohr    if ($classes === null) $classes = [
5816905344SAndreas Gohr        'Diff'                  => DOKU_INC.'inc/DifferenceEngine.php',
5916905344SAndreas Gohr        'UnifiedDiffFormatter'  => DOKU_INC.'inc/DifferenceEngine.php',
6016905344SAndreas Gohr        'TableDiffFormatter'    => DOKU_INC.'inc/DifferenceEngine.php',
6116905344SAndreas Gohr        'cache'                 => DOKU_INC.'inc/cache.php',
6216905344SAndreas Gohr        'cache_parser'          => DOKU_INC.'inc/cache.php',
6316905344SAndreas Gohr        'cache_instructions'    => DOKU_INC.'inc/cache.php',
6416905344SAndreas Gohr        'cache_renderer'        => DOKU_INC.'inc/cache.php',
65e1d9dcc8SAndreas Gohr        'Input'                 => DOKU_INC.'inc/Input.class.php',
6616905344SAndreas Gohr        'JpegMeta'              => DOKU_INC.'inc/JpegMeta.php',
672dcde304SAndreas Gohr        'SimplePie'             => DOKU_INC.'inc/SimplePie.php',
6816905344SAndreas Gohr        'FeedParser'            => DOKU_INC.'inc/FeedParser.php',
69f03fd957SAndreas Gohr        'SafeFN'                => DOKU_INC.'inc/SafeFN.class.php',
70c9a53c46SAndreas Gohr        'Mailer'                => DOKU_INC.'inc/Mailer.class.php',
71d2778ca9SAndreas Gohr        'Doku_Handler'          => DOKU_INC.'inc/parser/handler.php',
722ada8709SChristopher Smith        'Doku_Renderer'          => DOKU_INC.'inc/parser/renderer.php',
732ada8709SChristopher Smith        'Doku_Renderer_xhtml'    => DOKU_INC.'inc/parser/xhtml.php',
742ada8709SChristopher Smith        'Doku_Renderer_code'     => DOKU_INC.'inc/parser/code.php',
752ada8709SChristopher Smith        'Doku_Renderer_xhtmlsummary' => DOKU_INC.'inc/parser/xhtmlsummary.php',
7624870174SAndreas Gohr        'Doku_Renderer_metadata' => DOKU_INC.'inc/parser/metadata.php'
7724870174SAndreas Gohr    ];
7816905344SAndreas Gohr
7916905344SAndreas Gohr    if (isset($classes[$name])) {
808a58013eSDavid Stone        require($classes[$name]);
818a58013eSDavid Stone        return true;
8216905344SAndreas Gohr    }
83d54ac877SAdrian Lang
84b89dfc20SAndreas Gohr    // namespace to directory conversion
85e7a32b17SAndreas Gohr    $name = str_replace('\\', '/', $name);
86e7a32b17SAndreas Gohr
87ec85aeb2SAndreas Gohr    // test mock namespace
88ec85aeb2SAndreas Gohr    if (substr($name, 0, 19) === 'dokuwiki/test/mock/') {
89ec85aeb2SAndreas Gohr        $file = DOKU_INC . '_test/mock/' . substr($name, 19) . '.php';
90ec85aeb2SAndreas Gohr        if (file_exists($file)) {
91ec85aeb2SAndreas Gohr            require $file;
92ec85aeb2SAndreas Gohr            return true;
93ec85aeb2SAndreas Gohr        }
94ec85aeb2SAndreas Gohr    }
95ec85aeb2SAndreas Gohr
96ec85aeb2SAndreas Gohr    // tests namespace
9768f88d34SAndreas Gohr    if (substr($name, 0, 14) === 'dokuwiki/test/') {
98ec85aeb2SAndreas Gohr        $file = DOKU_INC . '_test/tests/' . substr($name, 14) . '.php';
99a087da71SAndreas Gohr        if (file_exists($file)) {
100a087da71SAndreas Gohr            require $file;
101a087da71SAndreas Gohr            return true;
102a087da71SAndreas Gohr        }
103a087da71SAndreas Gohr    }
104a087da71SAndreas Gohr
105600fb65eSAndreas Gohr    // plugin namespace
10668f88d34SAndreas Gohr    if (substr($name, 0, 16) === 'dokuwiki/plugin/') {
107600fb65eSAndreas Gohr        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
108b00eb817SAndreas Gohr        $file = DOKU_PLUGIN . substr($name, 16) . '.php';
109600fb65eSAndreas Gohr        if (file_exists($file)) {
110ffa84f81SAndreas Gohr            try {
111600fb65eSAndreas Gohr                require $file;
112ffa84f81SAndreas Gohr            } catch (\Throwable $e) {
11324870174SAndreas Gohr                ErrorHandler::showExceptionMsg($e, "Error loading plugin $name");
114ffa84f81SAndreas Gohr            }
115600fb65eSAndreas Gohr            return true;
116600fb65eSAndreas Gohr        }
117600fb65eSAndreas Gohr    }
118600fb65eSAndreas Gohr
119f83f0fd0SAndreas Gohr    // template namespace
12068f88d34SAndreas Gohr    if (substr($name, 0, 18) === 'dokuwiki/template/') {
121f83f0fd0SAndreas Gohr        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
122f83f0fd0SAndreas Gohr        $file = DOKU_INC.'lib/tpl/' . substr($name, 18) . '.php';
123f83f0fd0SAndreas Gohr        if (file_exists($file)) {
124ffa84f81SAndreas Gohr            try {
125f83f0fd0SAndreas Gohr                require $file;
126ffa84f81SAndreas Gohr            } catch (\Throwable $e) {
12724870174SAndreas Gohr                ErrorHandler::showExceptionMsg($e, "Error loading template $name");
128ffa84f81SAndreas Gohr            }
129f83f0fd0SAndreas Gohr            return true;
130f83f0fd0SAndreas Gohr        }
131f83f0fd0SAndreas Gohr    }
132f83f0fd0SAndreas Gohr
133b89dfc20SAndreas Gohr    // our own namespace
13468f88d34SAndreas Gohr    if (substr($name, 0, 9) === 'dokuwiki/') {
135b16ddc6eSAndreas Gohr        $file = DOKU_INC . 'inc/' . substr($name, 9) . '.php';
136b16ddc6eSAndreas Gohr        if (file_exists($file)) {
137b16ddc6eSAndreas Gohr            require $file;
138b89dfc20SAndreas Gohr            return true;
139b89dfc20SAndreas Gohr        }
140b16ddc6eSAndreas Gohr    }
141b89dfc20SAndreas Gohr
142d54ac877SAdrian Lang    // Plugin loading
143*7d34963bSAndreas Gohr    if (
144*7d34963bSAndreas Gohr        preg_match(
1451935a891SAndreas Gohr            '/^(' . implode('|', PluginController::PLUGIN_TYPES) . ')_plugin_(' .
14664159a61SAndreas Gohr            DOKU_PLUGIN_NAME_REGEX .
14764159a61SAndreas Gohr            ')(?:_([^_]+))?$/',
14864159a61SAndreas Gohr            $name,
14964159a61SAndreas Gohr            $m
150*7d34963bSAndreas Gohr        )
151*7d34963bSAndreas Gohr    ) {
152d54ac877SAdrian Lang        // try to load the wanted plugin file
153d54ac877SAdrian Lang        $c = ((count($m) === 4) ? "/{$m[3]}" : '');
1545a9866e9SAndreas Gohr        $plg = DOKU_PLUGIN . "{$m[2]}/{$m[1]}$c.php";
15579e79377SAndreas Gohr        if (file_exists($plg)) {
156ffa84f81SAndreas Gohr            try {
157ce949263SAndreas Gohr                require $plg;
158ffa84f81SAndreas Gohr            } catch (\Throwable $e) {
15924870174SAndreas Gohr                ErrorHandler::showExceptionMsg($e, "Error loading plugin {$m[2]}");
160ffa84f81SAndreas Gohr            }
1615a9866e9SAndreas Gohr        }
1628a58013eSDavid Stone        return true;
163d54ac877SAdrian Lang    }
164ce949263SAndreas Gohr    return false;
16516905344SAndreas Gohr}
166