Separate sources extractor and add it to ext.templateDataGenerator.data

This commit is contained in:
Luqgreg 2018-09-01 15:36:05 +02:00
parent 98d379c464
commit 0a6b0f9c70
4 changed files with 47 additions and 26 deletions

View file

@ -94,7 +94,8 @@
"wgQueryPages": "PortableInfoboxHooks::onWgQueryPages",
"AllInfoboxesQueryRecached": "PortableInfoboxHooks::onAllInfoboxesQueryRecached",
"ArticlePurge": "PortableInfoboxHooks::onArticlePurge",
"PageContentSave": "PortableInfoboxHooks::onPageContentSave"
"PageContentSave": "PortableInfoboxHooks::onPageContentSave",
"ResourceLoaderRegisterModules": "PortableInfoboxHooks::onResourceLoaderRegisterModules"
},
"SpecialPages": {
"AllInfoboxes": "AllinfoboxesQueryPage"

View file

@ -49,4 +49,20 @@ class PortableInfoboxHooks {
return true;
}
public static function onResourceLoaderRegisterModules( ResourceLoader &$resourceLoader ) {
global $wgResourceModules;
if( isset( $wgResourceModules['ext.templateDataGenerator.data'] ) ) {
$wgResourceModules['ext.templateDataGenerator.data']['scripts'][] =
'../PortableInfobox/resources/PortableInfoboxParams.js';
$resourceLoader->register(
'ext.templateDataGenerator.data',
$wgResourceModules['ext.templateDataGenerator.data']
);
}
return true;
}
}

View file

@ -42,33 +42,8 @@
}
};
var TemplateDataSuggestions = {
init: function() {
var original = mw.TemplateData.SourceHandler.prototype.extractParametersFromTemplateCode;
mw.TemplateData.SourceHandler.prototype.extractParametersFromTemplateCode = function( templateCode ) {
var infobox, source,
params = original(templateCode),
infoboxRegex = /<infobox.*?<\/infobox>/gs,
sourceRegex = /<[^<\/>]*? source="([^"]*)"[^>]*>/g;
while( ( infobox = infoboxRegex.exec(templateCode) ) !== null ) {
while( ( source = sourceRegex.exec(infobox) ) !== null ) {
if ( $.inArray( source[1], params ) === -1 ) {
params.push( source[1] );
}
}
}
return params;
}
}
}
mw.hook('wikipage.content').add(function($content) {
MediaCollection.init($content);
CollapsibleGroup.init($content);
});
mw.loader.using('ext.templateDataGenerator.data').then(function() {
TemplateDataSuggestions.init();
});
})(window, jQuery);

View file

@ -0,0 +1,29 @@
(function (window) {
'use strict';
var TemplateDataSuggestions = {
init: function() {
var original = mw.TemplateData.SourceHandler.prototype.extractParametersFromTemplateCode;
mw.TemplateData.SourceHandler.prototype.extractParametersFromTemplateCode = function( templateCode ) {
var infobox, source,
params = original(templateCode),
infoboxRegex = /<infobox.*?<\/infobox>/gs,
sourceRegex = /<[^<\/>]*? source="([^"]*)"[^>]*>/g;
while( ( infobox = infoboxRegex.exec(templateCode) ) !== null ) {
while( ( source = sourceRegex.exec(infobox) ) !== null ) {
if ( $.inArray( source[1], params ) === -1 ) {
params.push( source[1] );
}
}
}
return params;
}
}
}
mw.loader.using('ext.templateDataGenerator.data').then(function() {
TemplateDataSuggestions.init();
});
})(window);