2015-10-02 19:46:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RelatedArticles;
|
|
|
|
|
2021-10-19 14:58:36 +00:00
|
|
|
use MediaWiki\Extension\Disambiguator\Hooks as DisambiguatorHooks;
|
2020-01-15 06:11:52 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-08-19 04:18:57 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2015-10-08 00:21:13 +00:00
|
|
|
use OutputPage;
|
2020-01-15 06:11:52 +00:00
|
|
|
use Parser;
|
2015-10-08 00:21:13 +00:00
|
|
|
use ParserOutput;
|
2017-08-09 19:45:03 +00:00
|
|
|
use Skin;
|
2020-01-15 06:11:52 +00:00
|
|
|
use User;
|
2015-10-02 19:46:48 +00:00
|
|
|
|
|
|
|
class Hooks {
|
|
|
|
|
2017-08-09 19:45:03 +00:00
|
|
|
/**
|
|
|
|
* Handler for the <code>MakeGlobalVariablesScript</code> hook.
|
|
|
|
*
|
|
|
|
* Sets the value of the <code>wgRelatedArticles</code> global variable
|
|
|
|
* to the list of related articles in the cached parser output.
|
|
|
|
*
|
|
|
|
* @param array &$vars variables to be added into the output of OutputPage::headElement.
|
|
|
|
* @param OutputPage $out OutputPage instance calling the hook
|
|
|
|
*/
|
|
|
|
public static function onMakeGlobalVariablesScript( &$vars, OutputPage $out ) {
|
2019-05-26 12:49:16 +00:00
|
|
|
$editorCuratedPages = $out->getProperty( 'RelatedArticles' );
|
|
|
|
if ( $editorCuratedPages ) {
|
|
|
|
$vars['wgRelatedArticles'] = $editorCuratedPages;
|
|
|
|
}
|
2017-08-09 19:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the Disambiguator extension to test whether the page is a disambiguation page.
|
|
|
|
*
|
|
|
|
* If the Disambiguator extension isn't installed, then the test always fails, i.e. the page is
|
|
|
|
* never a disambiguation page.
|
|
|
|
*
|
2020-02-29 21:37:34 +00:00
|
|
|
* @param Title $title
|
2020-01-15 06:11:52 +00:00
|
|
|
* @return bool
|
2017-08-09 19:45:03 +00:00
|
|
|
*/
|
|
|
|
private static function isDisambiguationPage( Title $title ) {
|
|
|
|
return \ExtensionRegistry::getInstance()->isLoaded( 'Disambiguator' ) &&
|
|
|
|
DisambiguatorHooks::isDisambiguationPage( $title );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the output page is a diff page
|
|
|
|
*
|
|
|
|
* @param OutputPage $out
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function isDiffPage( OutputPage $out ) {
|
|
|
|
$request = $out->getRequest();
|
|
|
|
$type = $request->getText( 'type' );
|
|
|
|
$diff = $request->getText( 'diff' );
|
|
|
|
$oldId = $request->getText( 'oldid' );
|
|
|
|
$isSpecialMobileDiff = $out->getTitle()->isSpecial( 'MobileDiff' );
|
|
|
|
|
|
|
|
return $type === 'revision' || $diff || $oldId || $isSpecialMobileDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is ReadMore allowed on skin?
|
|
|
|
*
|
2019-06-20 13:29:29 +00:00
|
|
|
* Some wikis may want to only enable the feature on some skins, so we'll only
|
2021-04-19 00:26:58 +00:00
|
|
|
* show it if the allow list (`RelatedArticlesFooterAllowedSkins`
|
2019-06-20 13:29:29 +00:00
|
|
|
* configuration variable) is empty or the skin is listed.
|
2017-08-09 19:45:03 +00:00
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param Skin $skin
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function isReadMoreAllowedOnSkin( User $user, Skin $skin ) {
|
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()
|
|
|
|
->makeConfig( 'RelatedArticles' );
|
2021-04-19 00:27:43 +00:00
|
|
|
$skins = $config->get( 'RelatedArticlesFooterAllowedSkins' );
|
2017-08-09 19:45:03 +00:00
|
|
|
$skinName = $skin->getSkinName();
|
2019-06-20 13:29:29 +00:00
|
|
|
return empty( $skins ) || in_array( $skinName, $skins );
|
2017-08-09 19:45:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 22:11:51 +00:00
|
|
|
/**
|
|
|
|
* Can the page show related articles?
|
|
|
|
*
|
|
|
|
* @param Skin $skin
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function hasRelatedArticles( Skin $skin ): bool {
|
|
|
|
$out = $skin->getOutput();
|
|
|
|
$title = $out->getContext()->getTitle();
|
|
|
|
$action = $out->getRequest()->getText( 'action', 'view' );
|
|
|
|
return $title->inNamespace( NS_MAIN ) &&
|
|
|
|
// T120735
|
|
|
|
$action === 'view' &&
|
|
|
|
!$title->isMainPage() &&
|
|
|
|
$title->exists() &&
|
|
|
|
!self::isDisambiguationPage( $title ) &&
|
|
|
|
!self::isDiffPage( $out ) &&
|
|
|
|
self::isReadMoreAllowedOnSkin( $out->getUser(), $out->getSkin() );
|
|
|
|
}
|
|
|
|
|
2017-08-09 19:45:03 +00:00
|
|
|
/**
|
|
|
|
* Handler for the <code>BeforePageDisplay</code> hook.
|
|
|
|
*
|
|
|
|
* Adds the <code>ext.relatedArticles.readMore.bootstrap</code> module
|
|
|
|
* to the output when:
|
|
|
|
*
|
|
|
|
* <ol>
|
|
|
|
* <li>On mobile, the output is being rendered with
|
|
|
|
* <code>SkinMinervaBeta<code></li>
|
|
|
|
* <li>The page is in mainspace</li>
|
|
|
|
* <li>The action is 'view'</li>
|
|
|
|
* <li>The page is not the Main Page</li>
|
|
|
|
* <li>The page is not a disambiguation page</li>
|
|
|
|
* <li>The page is not a diff page</li>
|
|
|
|
* <li>The feature is allowed on the skin (see isReadMoreAllowedOnSkin() above)</li>
|
|
|
|
* </ol>
|
|
|
|
*
|
|
|
|
* @param OutputPage $out The OutputPage object
|
|
|
|
* @param Skin $skin Skin object that will be used to generate the page
|
|
|
|
* @return bool Always <code>true</code>
|
|
|
|
*/
|
|
|
|
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
|
2022-05-10 22:11:51 +00:00
|
|
|
if ( self::hasRelatedArticles( $skin ) ) {
|
2017-08-09 19:45:03 +00:00
|
|
|
$out->addModules( [ 'ext.relatedArticles.readMore.bootstrap' ] );
|
2021-11-04 19:27:36 +00:00
|
|
|
$out->addModuleStyles( [ 'ext.relatedArticles.styles' ] );
|
2017-08-09 19:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceLoaderGetConfigVars hook handler for setting a config variable
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
|
|
|
|
*
|
|
|
|
* @param array &$vars Array of variables to be added into the output of the startup module.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onResourceLoaderGetConfigVars( &$vars ) {
|
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()
|
|
|
|
->makeConfig( 'RelatedArticles' );
|
|
|
|
|
|
|
|
$limit = $config->get( 'RelatedArticlesCardLimit' );
|
|
|
|
$vars['wgRelatedArticlesCardLimit'] = $limit;
|
|
|
|
if ( $limit < 1 || $limit > 20 ) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'The value of wgRelatedArticlesCardLimit is not valid. It should be between 1 and 20.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:46:48 +00:00
|
|
|
/**
|
|
|
|
* Handler for the <code>ParserFirstCallInit</code> hook.
|
|
|
|
*
|
|
|
|
* Registers the <code>related</code> parser function (see
|
|
|
|
* {@see Hooks::onFuncRelated}).
|
|
|
|
*
|
2019-11-15 06:17:38 +00:00
|
|
|
* @param Parser $parser Parser object
|
2017-07-26 20:59:51 +00:00
|
|
|
* @return bool Always <code>true</code>
|
2015-10-02 19:46:48 +00:00
|
|
|
*/
|
2019-11-15 06:17:38 +00:00
|
|
|
public static function onParserFirstCallInit( Parser $parser ) {
|
2015-10-02 19:46:48 +00:00
|
|
|
$parser->setFunctionHook( 'related', 'RelatedArticles\\Hooks::onFuncRelated' );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The <code>related</code> parser function.
|
|
|
|
*
|
|
|
|
* Appends the arguments to the internal list so that it can be used
|
|
|
|
* more that once per page.
|
2015-10-08 00:21:13 +00:00
|
|
|
* We don't use setProperty here is there is no need
|
|
|
|
* to store it as a page prop in the database, only in the cache.
|
2015-10-02 19:46:48 +00:00
|
|
|
*
|
|
|
|
* @todo Test for uniqueness
|
2017-07-13 15:22:26 +00:00
|
|
|
* @param Parser $parser Parser object
|
2019-03-10 02:36:30 +00:00
|
|
|
* @param string ...$args
|
2015-10-02 19:46:48 +00:00
|
|
|
*
|
|
|
|
* @return string Always <code>''</code>
|
|
|
|
*/
|
2019-03-10 02:36:30 +00:00
|
|
|
public static function onFuncRelated( Parser $parser, ...$args ) {
|
2015-10-08 00:21:13 +00:00
|
|
|
$parserOutput = $parser->getOutput();
|
2015-11-14 00:03:39 +00:00
|
|
|
$relatedPages = $parserOutput->getExtensionData( 'RelatedArticles' );
|
|
|
|
if ( !$relatedPages ) {
|
2016-05-09 23:59:15 +00:00
|
|
|
$relatedPages = [];
|
2015-10-08 00:21:13 +00:00
|
|
|
}
|
2015-10-02 19:46:48 +00:00
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
// Add all the related pages passed by the parser function
|
2015-10-08 00:21:13 +00:00
|
|
|
// {{#related:Test with read more|Foo|Bar}}
|
2015-11-14 00:03:39 +00:00
|
|
|
foreach ( $args as $relatedPage ) {
|
|
|
|
$relatedPages[] = $relatedPage;
|
2015-10-02 19:46:48 +00:00
|
|
|
}
|
2015-11-14 00:03:39 +00:00
|
|
|
$parserOutput->setExtensionData( 'RelatedArticles', $relatedPages );
|
2015-10-02 19:46:48 +00:00
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-14 00:03:39 +00:00
|
|
|
* Passes the related pages list from the cached parser output
|
2015-10-14 10:29:36 +00:00
|
|
|
* object to the output page for rendering.
|
|
|
|
*
|
2015-11-14 00:03:39 +00:00
|
|
|
* The list of related pages will be retrieved using
|
2015-11-10 17:03:06 +00:00
|
|
|
* <code>ParserOutput#getExtensionData</code>.
|
2015-10-02 19:46:48 +00:00
|
|
|
*
|
2019-11-15 06:17:38 +00:00
|
|
|
* @param OutputPage $out the OutputPage object
|
2017-07-13 15:22:26 +00:00
|
|
|
* @param ParserOutput $parserOutput ParserOutput object
|
2017-07-26 20:59:51 +00:00
|
|
|
* @return bool Always <code>true</code>
|
2015-10-02 19:46:48 +00:00
|
|
|
*/
|
2019-11-15 06:17:38 +00:00
|
|
|
public static function onOutputPageParserOutput( OutputPage $out, ParserOutput $parserOutput ) {
|
2015-10-08 00:21:13 +00:00
|
|
|
$related = $parserOutput->getExtensionData( 'RelatedArticles' );
|
2015-10-02 19:46:48 +00:00
|
|
|
|
2015-10-08 00:21:13 +00:00
|
|
|
if ( $related ) {
|
|
|
|
$out->setProperty( 'RelatedArticles', $related );
|
|
|
|
}
|
2015-10-02 19:46:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-20 13:29:29 +00:00
|
|
|
/**
|
|
|
|
* Create container for ReadMore cards so that they're correctly placed in all skins.
|
|
|
|
*
|
|
|
|
* @param string &$data
|
|
|
|
* @param Skin $skin
|
|
|
|
*/
|
|
|
|
public static function onSkinAfterContent( &$data, Skin $skin ) {
|
2022-05-10 22:11:51 +00:00
|
|
|
if ( self::hasRelatedArticles( $skin ) ) {
|
|
|
|
$data .= \Html::element( 'div', [ 'class' => 'read-more-container' ] );
|
|
|
|
}
|
2019-06-20 13:29:29 +00:00
|
|
|
}
|
2015-10-02 19:46:48 +00:00
|
|
|
}
|