Wikimedia Developer Support

Creating of extension using PSR-4 autoloader and AutoloadNamespaces

I’m trying to create an extension using an example exactly as in the manual page:

After including the extension in my LocalSettings.php I’ve been obtained an error message:

[11a192d41e25cf6dae7854cc] /index.php?title=Special:SpecialPages Error from line 396 of /var/www/html/includes/specialpage/SpecialPageFactory.php: Class 'MediaWiki\Extension\WikiScoop\Special' not found

Backtrace:

#0 /var/www/html/includes/specialpage/SpecialPageFactory.php(426): MediaWiki\Special\SpecialPageFactory->getPage(string)
#1 /var/www/html/includes/specials/SpecialSpecialpages.php(56): MediaWiki\Special\SpecialPageFactory->getUsablePages(User)
#2 /var/www/html/includes/specials/SpecialSpecialpages.php(44): SpecialSpecialpages->getPageGroups()
#3 /var/www/html/includes/specialpage/SpecialPage.php(569): SpecialSpecialpages->execute(NULL)
#4 /var/www/html/includes/specialpage/SpecialPageFactory.php(558): SpecialPage->run(NULL)
#5 /var/www/html/includes/MediaWiki.php(288): MediaWiki\Special\SpecialPageFactory->executePath(Title, RequestContext)
#6 /var/www/html/includes/MediaWiki.php(865): MediaWiki->performRequest()
#7 /var/www/html/includes/MediaWiki.php(515): MediaWiki->main()
#8 /var/www/html/index.php(42): MediaWiki->run()
#9 {main}
  1. Namespace path declaration
    I have found the difference of AutoloadNamespaces path declaration between two manual pages: Special Pages and Extension Schema

So this does not explain what is an essence of “Extension” in path declaration “MediaWiki\Extension\MyExtension\”, but rather confuses

  1. Autoloading the extension
    I’ve created a composer manifest file “composer.json” and configure it using the composer, but still don’t understand how to include it in my extension using the extension’s manifest file or any PHP entry-point.

  2. Namespaces usage
    Does using the AutoloadNamespaces directive mean that I have to use the namespaces in my extension?

How to fix it?
Have you an example of how to use it?

I use
MediaWiki 1.33.1
PHP 7.3.10

So first off, src/Special.php doesnt include a namespace decleration (its commented out) so it is not in that namespace.

Second, the class name in the file is SpecialWikiScoop, however in extension.json you have the class name as Special. When using autoload namespacese, the class name must match the filename.

So all this means, mediawiki is looking for a class named MediaWiki\Extension\WikiScoop\Special

based on extension.json it thinks that should be in the file src/Special.php however src/Special.php only contains the class \SpecialWikiScoop (in the global namespace), hence the error.

Using autoloadNamespaces is one way of setting up your extension, but if you prefer you can use autoloadclasses instead which lets you specify precisely which class (including namespace) is in which file.

1 Like