Some modernization & code cleanup

* Move the Preloader class into its own file + add $wgAutoloadClasses entry
* Coding style tweaks, as per MediaWiki's coding conventions
* Converted from PHP-based i18n to JSON i18n
* Version 1.3 with the appropriate changelog entries and whatnot
* Deprecated Preloader.php (but didn't fully remove it yet) in favor of extension.json -- MediaWiki 1.25 or newer is now required for this extension
* Added myself into authors
* Changed the Special:Version URL back to MediaWiki.org as it should be the canonical location for extension (and skin etc.) documentation; since this is an updated version of [[mw:Extension:Preloader]] it should be no problem to update that page to point to the correct repository etc.
* Tweak the README file to account for new MW requirements + extension loading method
* Fix $wgPreloaderSource so that it works again ( HT @legoktm )
This commit is contained in:
Jack Phoenix 2017-01-04 18:35:26 +02:00 committed by SamanthaNguyen
parent d6c2dd56dc
commit e86366734f
58 changed files with 534 additions and 462 deletions

View file

@ -11,81 +11,15 @@
* @author Rob Church <robchur@gmail.com>
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
exit( 1 );
}
$wgExtensionCredits['other'][] = array(
'path' => __FILE__,
'name' => 'Preloader',
'author' => 'Rob Church, Troy Engel',
'version' => '1.2.2',
'url' => 'https://github.com/troyengel/Preloader',
'descriptionmsg' => 'preloader-desc',
);
$wgExtensionMessagesFiles['Preloader'] = dirname(__FILE__) . '/Preloader.i18n.php';
/**
* Sources of preloaded content for each namespace
*/
$wgPreloaderSource[ NS_MAIN ] = 'Template:Preload';
$wgHooks['EditFormPreloadText'][] = 'Preloader::mainHook';
class Preloader {
/** Hook function for the preloading */
public static function mainHook( &$text, &$title ) {
$src = self::preloadSource( $title->getNamespace() );
if( $src ) {
$stx = self::sourceText( $src );
if( $stx )
$text = $stx;
}
return true;
}
/**
* Determine what page should be used as the source of preloaded text
* for a given namespace and return the title (in text form)
*
* @param $namespace Namespace to check for
* @return mixed
*/
static function preloadSource( $namespace ) {
global $wgPreloaderSource;
if( isset( $wgPreloaderSource[ $namespace ] ) ) {
return $wgPreloaderSource[ $namespace ];
} else {
return false;
}
}
/**
* Grab the current text of a given page if it exists
*
* @param $page Text form of the page title
* @return mixed
*/
static function sourceText( $page ) {
$title = Title::newFromText( $page );
if( $title && $title->exists() ) {
$revision = Revision::newFromTitle( $title );
return self::transform( $revision->getText() );
} else {
return false;
}
}
/**
* Remove sections from the text and trim whitespace
*
* @param $text
* @return string
*/
static function transform( $text ) {
$text = trim( preg_replace( '/<\/?includeonly>/s', '', $text ) );
return trim( preg_replace( '/<noinclude>.*<\/noinclude>/s', '', $text ) );
}
}
if ( function_exists( 'wfLoadExtension' ) ) {
wfLoadExtension( 'Preloader' );
// Keep i18n globals so mergeMessageFileList.php doesn't break
$wgMessagesDirs['Preloader'] = __DIR__ . '/i18n';
wfWarn(
'Deprecated PHP entry point used for Preloader extension. Please use wfLoadExtension instead, ' .
'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
);
return true;
} else {
die( 'This version of the Preloader extension requires MediaWiki 1.25+' );
}