2022-03-19 16:49:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2023-10-25 22:32:58 +00:00
|
|
|
namespace MediaWiki\Extension\Gadgets;
|
|
|
|
|
2024-01-06 16:45:07 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
|
|
|
use MediaWiki\User\User;
|
2022-03-19 16:49:41 +00:00
|
|
|
use Skin;
|
|
|
|
|
2023-10-25 22:32:58 +00:00
|
|
|
/**
|
|
|
|
* @author Siddharth VP
|
|
|
|
*/
|
2022-03-19 16:49:41 +00:00
|
|
|
class GadgetLoadConditions {
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
/** @var Skin */
|
|
|
|
private $skin;
|
|
|
|
/** @var string */
|
|
|
|
private $action;
|
2020-09-04 03:42:31 +00:00
|
|
|
/** @var int */
|
|
|
|
private $namespace;
|
2024-02-20 13:55:52 +00:00
|
|
|
/** @var string[] */
|
|
|
|
private $categories;
|
2023-05-22 08:41:15 +00:00
|
|
|
/** @var string */
|
|
|
|
private $contentModel;
|
2022-03-19 16:49:41 +00:00
|
|
|
/** @var string|null */
|
|
|
|
private $withGadgetParam;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param OutputPage $out
|
|
|
|
*/
|
2024-01-26 17:48:18 +00:00
|
|
|
public function __construct( OutputPage $out ) {
|
2022-03-19 16:49:41 +00:00
|
|
|
$this->user = $out->getUser();
|
|
|
|
$this->skin = $out->getSkin();
|
2022-03-26 16:49:48 +00:00
|
|
|
$this->action = $out->getContext()->getActionName();
|
2020-09-04 03:42:31 +00:00
|
|
|
$this->namespace = $out->getTitle()->getNamespace();
|
2024-02-20 13:55:52 +00:00
|
|
|
$this->categories = $out->getCategories();
|
2023-05-22 08:41:15 +00:00
|
|
|
$this->contentModel = $out->getTitle()->getContentModel();
|
2022-03-19 16:49:41 +00:00
|
|
|
$this->withGadgetParam = $out->getRequest()->getRawVal( 'withgadget' );
|
|
|
|
}
|
|
|
|
|
Improve localisation on Special:Gadgets, minor code cleanups
For each item, either display human-readable and translated text,
or display a technical non-translatable identifier as `<code>`,
with optional localised text in the title attribute.
* Re-format "rights" as a sentence instead of a bullet list. It was
the only one using a bullet list, which made it feel a bit long.
* Re-format "actions" as `<code>` since they are not localised.
* Re-format "contentModels" as `<code>`, and add the localised display
name in a title attribute, same as we do with "rights" already.
ContentHandler::getLocalizedName() is also used already on
Special:ChangeContentModel.
* Fix "contentModels" to set `needLineBreakAfter = true`, otherwise
if a gadget also sets "supportsUrlLoad", then that sentence is
appended to the previous line.
Update phrasing and sorting to be consistent everywhere, and adopt
native PHP types where possible.
In most cases, I made things alphabetical, with the exception of
Special:Gadgets user interface output, and Gadget class methods,
which both follow the order of most recently added feature last
(rights, skins, actions, namespaces, contentmodels, categories).
Highlights:
* Fix namespace IDs type. These can be strings when they are parsed
from the gadget definition text, not always integers.
* Add explicit default for 'category'. In theory not needed because
MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and
MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where
`category: ''` is part of both the initial page content, as well
as merged via getDefaultMetadata.
This default benefits simpler test cases, and static analysis,
since the Gadget class constructor does not (yet) require it.
Without this, getCategory() could TypeError due to returning null.
Bug: T63007
Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
|
|
|
public function check( Gadget $gadget ): bool {
|
2022-03-19 16:49:41 +00:00
|
|
|
$urlLoad = $this->withGadgetParam === $gadget->getName() && $gadget->supportsUrlLoad();
|
|
|
|
|
|
|
|
return ( $gadget->isEnabled( $this->user ) || $urlLoad )
|
|
|
|
&& $gadget->isAllowed( $this->user )
|
|
|
|
&& $gadget->isActionSupported( $this->action )
|
|
|
|
&& $gadget->isSkinSupported( $this->skin )
|
2020-09-04 03:42:31 +00:00
|
|
|
&& $gadget->isNamespaceSupported( $this->namespace )
|
2024-02-20 13:55:52 +00:00
|
|
|
&& $gadget->isCategorySupported( $this->categories )
|
2024-01-24 17:11:20 +00:00
|
|
|
&& $gadget->isContentModelSupported( $this->contentModel );
|
2022-03-19 16:49:41 +00:00
|
|
|
}
|
|
|
|
}
|