2015-10-02 19:46:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RelatedArticles;
|
|
|
|
|
|
|
|
use Parser;
|
2015-10-08 00:21:13 +00:00
|
|
|
use OutputPage;
|
|
|
|
use ParserOutput;
|
2015-10-02 19:46:48 +00:00
|
|
|
|
|
|
|
class Hooks {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the <code>ParserFirstCallInit</code> hook.
|
|
|
|
*
|
|
|
|
* Registers the <code>related</code> parser function (see
|
|
|
|
* {@see Hooks::onFuncRelated}).
|
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return boolean Always <code>true</code>
|
|
|
|
*/
|
|
|
|
public static function onParserFirstCallInit( Parser &$parser ) {
|
|
|
|
$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
|
2015-10-08 00:21:13 +00:00
|
|
|
* @param Parser $parser
|
2015-10-02 19:46:48 +00:00
|
|
|
*
|
|
|
|
* @return string Always <code>''</code>
|
|
|
|
*/
|
2015-10-08 00:21:13 +00:00
|
|
|
public static function onFuncRelated( Parser $parser ) {
|
|
|
|
$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
|
|
|
$args = func_get_args();
|
|
|
|
array_shift( $args );
|
|
|
|
|
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
|
|
|
*
|
2015-10-08 00:21:13 +00:00
|
|
|
* @param OutputPage $out
|
|
|
|
* @param ParserOutput $parserOutput
|
2015-10-02 19:46:48 +00:00
|
|
|
* @return boolean Always <code>true</code>
|
|
|
|
*/
|
2015-10-08 00:21:13 +00:00
|
|
|
public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-11-19 03:49:46 +00:00
|
|
|
/**
|
2015-12-02 11:10:04 +00:00
|
|
|
* Register QUnit tests.
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules
|
2015-11-19 03:49:46 +00:00
|
|
|
*
|
2015-12-02 11:10:04 +00:00
|
|
|
* @param array $modules
|
2017-01-30 22:55:59 +00:00
|
|
|
* @param \ResourceLoader $rl
|
2015-11-19 03:49:46 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-12-02 11:10:04 +00:00
|
|
|
public static function onResourceLoaderTestModules( &$modules, &$rl ) {
|
2016-05-09 23:59:15 +00:00
|
|
|
$boilerplate = [
|
2015-12-02 11:10:04 +00:00
|
|
|
'localBasePath' => __DIR__ . '/../tests/qunit/',
|
|
|
|
'remoteExtPath' => 'RelatedArticles/tests/qunit',
|
2016-05-09 23:59:15 +00:00
|
|
|
'targets' => [ 'desktop', 'mobile' ],
|
|
|
|
];
|
2015-12-02 11:10:04 +00:00
|
|
|
|
2016-05-09 23:59:15 +00:00
|
|
|
$modules['qunit']['ext.relatedArticles.readMore.gateway.tests'] = $boilerplate + [
|
|
|
|
'scripts' => [
|
2015-12-02 11:10:04 +00:00
|
|
|
'ext.relatedArticles.readMore.gateway/test_RelatedPagesGateway.js',
|
2016-05-09 23:59:15 +00:00
|
|
|
],
|
|
|
|
'dependencies' => [
|
2015-12-02 11:10:04 +00:00
|
|
|
'ext.relatedArticles.readMore.gateway',
|
2016-05-09 23:59:15 +00:00
|
|
|
],
|
|
|
|
];
|
2015-11-19 03:49:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-02 19:46:48 +00:00
|
|
|
}
|