2016-12-16 02:47:52 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension Popups.
|
|
|
|
*
|
|
|
|
* Popups 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.
|
|
|
|
*
|
|
|
|
* Popups 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 Popups. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup extensions
|
|
|
|
*/
|
|
|
|
namespace Popups;
|
|
|
|
|
2024-01-05 21:41:45 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2024-10-20 09:15:48 +00:00
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2022-11-05 09:25:26 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2023-08-19 04:18:33 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-11-29 12:39:47 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2024-01-05 21:41:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2016-12-16 02:47:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Popups Module
|
|
|
|
*
|
|
|
|
* @package Popups
|
|
|
|
*/
|
|
|
|
class PopupsContext {
|
|
|
|
|
2019-10-10 14:53:30 +00:00
|
|
|
public const EXTENSION_NAME = 'popups';
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2019-02-26 08:56:07 +00:00
|
|
|
* Logger channel name
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2019-10-10 14:53:30 +00:00
|
|
|
public const LOGGER_CHANNEL = 'popups';
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference value for enabled Page Previews
|
|
|
|
*/
|
2021-06-06 13:07:08 +00:00
|
|
|
public const PREVIEWS_ENABLED = true;
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* User preference value for disabled Page Previews
|
|
|
|
*/
|
2021-06-06 13:07:08 +00:00
|
|
|
public const PREVIEWS_DISABLED = false;
|
2019-02-26 08:56:07 +00:00
|
|
|
|
2021-03-09 17:21:48 +00:00
|
|
|
/**
|
|
|
|
* Flags passed on to JS representing preferences
|
|
|
|
*/
|
|
|
|
private const NAV_POPUPS_ENABLED = 1;
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2024-01-05 21:41:45 +00:00
|
|
|
* @var Config
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @var PopupsContext
|
|
|
|
*/
|
|
|
|
protected static $instance;
|
2018-08-07 07:48:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ExtensionRegistry
|
|
|
|
*/
|
|
|
|
private $extensionRegistry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PopupsGadgetsIntegration
|
|
|
|
*/
|
|
|
|
private $gadgetsIntegration;
|
|
|
|
|
2022-11-05 09:25:26 +00:00
|
|
|
/**
|
|
|
|
* @var SpecialPageFactory
|
|
|
|
*/
|
|
|
|
private $specialPageFactory;
|
|
|
|
|
2021-06-06 13:07:08 +00:00
|
|
|
/**
|
|
|
|
* @var UserOptionsLookup
|
|
|
|
*/
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
2016-12-16 02:47:52 +00:00
|
|
|
/**
|
2017-07-12 20:16:55 +00:00
|
|
|
* @param Config $config Mediawiki configuration
|
|
|
|
* @param ExtensionRegistry $extensionRegistry MediaWiki extension registry
|
|
|
|
* @param PopupsGadgetsIntegration $gadgetsIntegration Gadgets integration helper
|
2022-11-05 09:25:26 +00:00
|
|
|
* @param SpecialPageFactory $specialPageFactory
|
2021-06-06 13:07:08 +00:00
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
2021-06-06 13:07:08 +00:00
|
|
|
public function __construct(
|
|
|
|
Config $config,
|
|
|
|
ExtensionRegistry $extensionRegistry,
|
|
|
|
PopupsGadgetsIntegration $gadgetsIntegration,
|
2022-11-05 09:25:26 +00:00
|
|
|
SpecialPageFactory $specialPageFactory,
|
2021-06-06 13:07:08 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup
|
|
|
|
) {
|
2024-04-16 08:35:08 +00:00
|
|
|
$this->config = $config;
|
2016-12-20 21:54:47 +00:00
|
|
|
$this->extensionRegistry = $extensionRegistry;
|
2017-01-04 16:33:40 +00:00
|
|
|
$this->gadgetsIntegration = $gadgetsIntegration;
|
2022-11-05 09:25:26 +00:00
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
2021-06-06 13:07:08 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
/**
|
2024-01-05 21:41:45 +00:00
|
|
|
* @param User $user User whose gadgets settings are being checked
|
2017-01-04 16:33:40 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-05 21:41:45 +00:00
|
|
|
public function conflictsWithNavPopupsGadget( User $user ) {
|
2017-01-04 16:33:40 +00:00
|
|
|
return $this->gadgetsIntegration->conflictsWithNavPopupsGadget( $user );
|
|
|
|
}
|
2017-01-10 01:28:57 +00:00
|
|
|
|
2020-11-03 12:28:33 +00:00
|
|
|
/**
|
2016-12-16 02:47:52 +00:00
|
|
|
* Are Page previews visible on User Preferences Page
|
|
|
|
*
|
2017-07-12 20:29:34 +00:00
|
|
|
* @return bool
|
2016-12-16 02:47:52 +00:00
|
|
|
*/
|
|
|
|
public function showPreviewsOptInOnPreferencesPage() {
|
2018-04-26 20:43:05 +00:00
|
|
|
return $this->config->get( 'PopupsHideOptInOnPreferencesPage' ) === false;
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 17:21:48 +00:00
|
|
|
/**
|
2024-01-05 21:41:45 +00:00
|
|
|
* @param User $user User whose preferences are checked
|
2021-03-09 17:21:48 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2024-01-05 21:41:45 +00:00
|
|
|
public function getConfigBitmaskFromUser( User $user ) {
|
2024-05-29 23:05:22 +00:00
|
|
|
return ( $this->conflictsWithNavPopupsGadget( $user ) ? self::NAV_POPUPS_ENABLED : 0 );
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:47 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function areDependenciesMet() {
|
2018-03-28 22:53:34 +00:00
|
|
|
if ( $this->config->get( 'PopupsGateway' ) === 'mwApiPlain' ) {
|
2019-01-16 16:25:01 +00:00
|
|
|
return $this->extensionRegistry->isLoaded( 'TextExtracts' )
|
2016-12-20 21:54:47 +00:00
|
|
|
&& $this->extensionRegistry->isLoaded( 'PageImages' );
|
2018-03-28 22:53:34 +00:00
|
|
|
}
|
2016-12-20 21:54:47 +00:00
|
|
|
|
2019-01-16 16:25:01 +00:00
|
|
|
return true;
|
2016-12-20 21:54:47 +00:00
|
|
|
}
|
2017-07-28 18:58:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether popups code should be shipped to $title
|
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* For example, if 'Special:UserLogin' is excluded, and the user is on 'Special:UserLogin',
|
|
|
|
* then the title is considered excluded.
|
2017-07-28 18:58:13 +00:00
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* A title is also considered excluded if its root matches one of the page names
|
|
|
|
* from the config variable. For example, if 'User:A' is excluded, and the
|
|
|
|
* title is 'User:A/b', then this title is considered excluded.
|
2017-07-28 18:58:13 +00:00
|
|
|
*
|
2020-06-09 06:52:42 +00:00
|
|
|
* Language specific excluded titles affect all languages. For example, if "Main_Page" is
|
|
|
|
* excluded, "Bosh_Sahifa" (which is "Main_Page" in Uzbek) is considered excluded too.
|
2017-07-28 18:58:13 +00:00
|
|
|
*
|
|
|
|
* @param Title $title title being tested
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-06-09 06:52:42 +00:00
|
|
|
public function isTitleExcluded( $title ) {
|
2020-07-02 22:09:22 +00:00
|
|
|
$excludedPages = $this->config->get( 'PopupsPageDisabled' );
|
|
|
|
|
2017-08-24 20:51:39 +00:00
|
|
|
$canonicalTitle = $title->getRootTitle();
|
|
|
|
|
|
|
|
if ( $title->isSpecialPage() ) {
|
|
|
|
// it's special page, translate it to canonical name
|
2022-11-05 09:25:26 +00:00
|
|
|
[ $name, $subpage ] = $this->specialPageFactory
|
2018-09-05 19:00:38 +00:00
|
|
|
->resolveAlias( $canonicalTitle->getText() );
|
2017-08-24 20:51:39 +00:00
|
|
|
|
|
|
|
if ( $name !== null ) {
|
|
|
|
$canonicalTitle = Title::newFromText( $name, NS_SPECIAL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 06:52:42 +00:00
|
|
|
foreach ( $excludedPages as $page ) {
|
|
|
|
$excludedTitle = Title::newFromText( $page );
|
2017-08-24 20:51:39 +00:00
|
|
|
|
2020-06-09 06:52:42 +00:00
|
|
|
if ( $canonicalTitle->equals( $excludedTitle ) ) {
|
2017-07-28 18:58:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-16 02:47:52 +00:00
|
|
|
}
|