2015-08-03 06:37:32 +00:00
|
|
|
<?php
|
2021-07-15 14:40:45 +00:00
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2014
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2021-10-07 10:09:05 +00:00
|
|
|
use MediaWiki\Content\Renderer\ContentParseParams;
|
2021-07-15 14:40:45 +00:00
|
|
|
use MediaWiki\Revision\SlotRenderingProvider;
|
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
class GadgetDefinitionContentHandler extends JsonContentHandler {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct( 'GadgetDefinition' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canBeUsedOn( Title $title ) {
|
|
|
|
return $title->inNamespace( NS_GADGET_DEFINITION );
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:10:15 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-03 06:37:32 +00:00
|
|
|
protected function getContentClass() {
|
2021-04-08 18:34:17 +00:00
|
|
|
return GadgetDefinitionContent::class;
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function makeEmptyContent() {
|
|
|
|
$class = $this->getContentClass();
|
2016-02-16 18:59:04 +00:00
|
|
|
return new $class( FormatJson::encode( $this->getDefaultMetadata(), "\t" ) );
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefaultMetadata() {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [
|
|
|
|
'settings' => [
|
|
|
|
'rights' => [],
|
2015-08-03 06:37:32 +00:00
|
|
|
'default' => false,
|
2021-10-17 13:05:15 +00:00
|
|
|
'package' => false,
|
2015-08-03 06:37:32 +00:00
|
|
|
'hidden' => false,
|
2016-12-28 10:25:47 +00:00
|
|
|
'skins' => [],
|
2021-12-14 13:10:22 +00:00
|
|
|
'actions' => [],
|
2022-01-11 09:03:44 +00:00
|
|
|
'category' => '',
|
|
|
|
'supportsUrlLoad' => false,
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
'module' => [
|
|
|
|
'scripts' => [],
|
|
|
|
'styles' => [],
|
2021-10-17 13:05:15 +00:00
|
|
|
'datas' => [],
|
2016-11-18 04:54:17 +00:00
|
|
|
'peers' => [],
|
2016-12-28 10:25:47 +00:00
|
|
|
'dependencies' => [],
|
|
|
|
'messages' => [],
|
Implement support for specifying type=styles
T87871 formally introduced the concept of a styles module,
which sets mw.loader.state to "ready" when loaded through addModuleStyles().
Previously, addModuleStyles couldn't safely do that because a module may
contain scripts also, in which case mw.loader must still load the (rest)
of the module (causes styles to load twice).
In MediaWiki core or extensions this is easily avoided by calling not
calling both addModules() and addModuleStyles().
For Gadgets we call both as a workaround to allow users to provide styles
(without a FOUC), but also to provide scripts+styles. Since we don't declare
which one is intended (and some gadgets do both), we loaded them both ways.
This will no longer be allowed in the future (see T92459).
The new 'type=styles' Gadget attribute promises to ResourceLoader that a
gadget only contains styles.
Impact:
* [Bug fix] When mw.loader requires a styles module that already loaded,
it will not load again.
* [Feature] It is possible for a general scripts+styles gadget to depend on
a styles gadget. Previously this caused the styles to load twice.
* Specifying type=styles will load the module through addModuleStyles() only.
Use this for modules that contain styles that relate to elements already
on the page (e.g. when customising the skin, layout, or article content).
* Specifying type=general will load the module through addModules() only.
Use this if your module contains both scripts and styles and the styles
only relate to elements created by the script. This means the styles do not
need to be loaded separately through addModuleStyles() and will not apply
to noscript mode.
Effective difference:
* Gadgets with only styles: We assume type=styles.
This fixes the main bug (styles loading twice) and requires no migration!
* Gadgets with only scripts: We assume type=general.
This requires no migration! (And: No more empty stylesheet request)
* Gadgets with scripts (with or without styles): We assume type=general, but
unless type=general was explicitly set we'll still load it both ways so
that the styles apply directly on page load.
If this is not needed, set type=general.
If this is needed, it should become two separate modules. We do not support
a single module having two purposes (1: apply styles to the page,
2: provide scripts+styles). The styles module should be separate.
It can be made hidden, and listed as dependency of the other module.
The latter case is detected on page load and results in a console warning
with a link to T42284.
Bug: T42284
Bug: T92459
Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
|
|
|
'type' => '',
|
2016-12-28 10:25:47 +00:00
|
|
|
],
|
|
|
|
];
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
2021-07-15 14:40:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title The title of the page to supply the updates for.
|
|
|
|
* @param string $role The role (slot) in which the content is being used.
|
|
|
|
* @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information
|
|
|
|
* about this content object somewhere.
|
|
|
|
*/
|
|
|
|
public function getDeletionUpdates( Title $title, $role ) {
|
|
|
|
return array_merge(
|
|
|
|
parent::getDeletionUpdates( $title, $role ),
|
|
|
|
[ new GadgetDefinitionDeletionUpdate( $title ) ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title The title of the page to supply the updates for.
|
|
|
|
* @param Content $content The content to generate data updates for.
|
|
|
|
* @param string $role The role (slot) in which the content is being used.
|
|
|
|
* @param SlotRenderingProvider $slotOutput A provider that can be used to gain access to
|
|
|
|
* a ParserOutput of $content by calling $slotOutput->getSlotParserOutput( $role, false ).
|
|
|
|
* @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information
|
|
|
|
* about this content object somewhere.
|
|
|
|
*/
|
|
|
|
public function getSecondaryDataUpdates(
|
|
|
|
Title $title,
|
|
|
|
Content $content,
|
|
|
|
$role,
|
|
|
|
SlotRenderingProvider $slotOutput
|
|
|
|
) {
|
|
|
|
return array_merge(
|
|
|
|
parent::getSecondaryDataUpdates( $title, $content, $role, $slotOutput ),
|
|
|
|
[ new GadgetDefinitionSecondaryDataUpdate( $title ) ]
|
|
|
|
);
|
|
|
|
}
|
2021-10-07 10:09:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function fillParserOutput(
|
|
|
|
Content $content,
|
|
|
|
ContentParseParams $cpoParams,
|
|
|
|
ParserOutput &$output
|
|
|
|
) {
|
|
|
|
'@phan-var GadgetDefinitionContent $content';
|
2016-02-16 19:38:44 +00:00
|
|
|
// Create a deep clone. FIXME: unserialize(serialize()) is hacky.
|
|
|
|
$data = unserialize( serialize( $content->getData()->getValue() ) );
|
|
|
|
if ( $data !== null ) {
|
|
|
|
foreach ( [ 'scripts', 'styles', 'datas' ] as $type ) {
|
|
|
|
if ( isset( $data->module->{$type} ) ) {
|
|
|
|
foreach ( $data->module->{$type} as &$page ) {
|
|
|
|
$title = Title::makeTitleSafe( NS_GADGET, $page );
|
|
|
|
$this->makeLink( $output, $page, $title );
|
|
|
|
}
|
2021-10-07 10:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 19:38:44 +00:00
|
|
|
foreach ( $data->module->dependencies as &$dep ) {
|
|
|
|
if ( str_starts_with( $dep, 'ext.gadget.' ) ) {
|
|
|
|
$gadgetId = explode( 'ext.gadget.', $dep )[1];
|
|
|
|
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $gadgetId );
|
|
|
|
$this->makeLink( $output, $dep, $title );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ( $data->module->peers as &$peer ) {
|
|
|
|
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $peer );
|
|
|
|
$this->makeLink( $output, $peer, $title );
|
|
|
|
}
|
|
|
|
foreach ( $data->module->messages as &$msg ) {
|
|
|
|
$title = Title::makeTitleSafe( NS_MEDIAWIKI, $msg );
|
|
|
|
$this->makeLink( $output, $msg, $title );
|
|
|
|
}
|
|
|
|
if ( $data->settings->category ) {
|
|
|
|
$this->makeLink(
|
|
|
|
$output,
|
|
|
|
$data->settings->category,
|
|
|
|
Title::makeTitleSafe( NS_MEDIAWIKI, "gadget-section-" . $data->settings->category )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$cpoParams->getGenerateHtml() || !$content->isValid() ) {
|
|
|
|
$output->setText( '' );
|
|
|
|
} else {
|
|
|
|
$output->setText( $content->rootValueTable( $data ) );
|
|
|
|
$output->addModuleStyles( 'mediawiki.content.json' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a link on the page
|
|
|
|
* @param ParserOutput $output
|
|
|
|
* @param string &$text The text to link
|
|
|
|
* @param Title|null $title Link target title
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function makeLink( ParserOutput $output, string &$text, ?Title $title ) {
|
|
|
|
if ( $title ) {
|
|
|
|
$output->addLink( $title );
|
|
|
|
$text = new GadgetDefinitionContentArmor(
|
|
|
|
Linker::link( $title, htmlspecialchars( '"' . $text . '"' ) )
|
|
|
|
);
|
2021-10-07 10:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|