2013-02-22 18:50:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-09-12 09:41:07 +00:00
|
|
|
* Hooks for TemplateData extension
|
2013-02-22 18:50:54 +00:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
|
|
|
class TemplateDataHooks {
|
|
|
|
/**
|
|
|
|
* Register parser hooks
|
2017-09-24 20:19:03 +00:00
|
|
|
* @param Parser &$parser
|
|
|
|
* @return bool
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
|
|
|
public static function onParserFirstCallInit( &$parser ) {
|
2016-05-10 00:00:14 +00:00
|
|
|
$parser->setHook( 'templatedata', [ 'TemplateDataHooks', 'render' ] );
|
2013-02-22 18:50:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-21 03:46:02 +00:00
|
|
|
/**
|
|
|
|
* Register qunit unit tests
|
2017-09-24 20:19:03 +00:00
|
|
|
* @param array &$testModules
|
|
|
|
* @param ResourceLoader &$resourceLoader
|
|
|
|
* @return bool
|
2013-09-21 03:46:02 +00:00
|
|
|
*/
|
|
|
|
public static function onResourceLoaderTestModules(
|
|
|
|
array &$testModules,
|
|
|
|
ResourceLoader &$resourceLoader
|
|
|
|
) {
|
2016-05-10 00:00:14 +00:00
|
|
|
$testModules['qunit']['ext.templateData.test'] = [
|
|
|
|
'scripts' => [ 'tests/ext.templateData.tests.js' ],
|
|
|
|
'dependencies' => [ 'ext.templateDataGenerator.data' ],
|
2018-04-24 23:50:31 +00:00
|
|
|
'localBasePath' => dirname( __DIR__ ) ,
|
2013-09-21 03:46:02 +00:00
|
|
|
'remoteExtPath' => 'TemplateData',
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2013-09-21 03:46:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-11 05:22:21 +00:00
|
|
|
/**
|
2015-01-03 17:24:29 +00:00
|
|
|
* Conditionally register the jquery.uls.data module, in case they've already been
|
|
|
|
* registered by the UniversalLanguageSelector extension or the VisualEditor extension.
|
2014-10-11 05:22:21 +00:00
|
|
|
*
|
2017-09-24 20:19:03 +00:00
|
|
|
* @param ResourceLoader &$resourceLoader
|
2014-10-11 05:22:21 +00:00
|
|
|
*/
|
|
|
|
public static function onResourceLoaderRegisterModules( ResourceLoader &$resourceLoader ) {
|
|
|
|
$resourceModules = $resourceLoader->getConfig()->get( 'ResourceModules' );
|
2015-01-03 17:24:29 +00:00
|
|
|
$name = 'jquery.uls.data';
|
2015-03-15 23:54:16 +00:00
|
|
|
if ( !isset( $resourceModules[$name] ) && !$resourceLoader->isModuleRegistered( $name ) ) {
|
2016-05-10 00:00:14 +00:00
|
|
|
$resourceLoader->register( [
|
|
|
|
'jquery.uls.data' => [
|
2018-04-24 23:50:31 +00:00
|
|
|
'localBasePath' => dirname( __DIR__ ),
|
2014-10-11 05:22:21 +00:00
|
|
|
'remoteExtPath' => 'TemplateData',
|
2016-05-10 00:00:14 +00:00
|
|
|
'scripts' => [
|
2014-10-11 05:22:21 +00:00
|
|
|
'lib/jquery.uls/src/jquery.uls.data.js',
|
|
|
|
'lib/jquery.uls/src/jquery.uls.data.utils.js',
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
|
|
|
'targets' => [ 'desktop', 'mobile' ],
|
|
|
|
]
|
|
|
|
] );
|
2014-10-11 05:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
2017-09-01 20:44:02 +00:00
|
|
|
* @param WikiPage &$page
|
2013-02-22 18:50:54 +00:00
|
|
|
* @param User &$user
|
|
|
|
* @param Content &$content
|
|
|
|
* @param string &$summary
|
2017-09-24 20:19:03 +00:00
|
|
|
* @param bool $minor
|
2013-02-22 18:50:54 +00:00
|
|
|
* @param bool|null $watchthis
|
2017-09-24 20:19:03 +00:00
|
|
|
* @param string $sectionanchor
|
|
|
|
* @param int &$flags
|
2013-02-22 18:50:54 +00:00
|
|
|
* @param Status &$status
|
2017-09-01 20:44:02 +00:00
|
|
|
* @return bool
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
2017-12-08 14:00:12 +00:00
|
|
|
public static function onPageContentSave( WikiPage &$page, &$user, &$content, &$summary, $minor,
|
2013-02-22 18:50:54 +00:00
|
|
|
$watchthis, $sectionanchor, &$flags, &$status
|
|
|
|
) {
|
|
|
|
// The PageContentSave hook provides raw $text, but not $parser because at this stage
|
|
|
|
// the page is not actually parsed yet. Which means we can't know whether self::render()
|
|
|
|
// got a valid tag or not. Looking at $text directly is not a solution either as
|
|
|
|
// it may not be in the current page (it can be transcluded).
|
|
|
|
// Since there is no later hook that allows aborting the save and showing an error,
|
|
|
|
// we will have to trigger the parser ourselves.
|
|
|
|
// Fortunately this causes no overhead since the below (copied from WikiPage::doEditContent,
|
|
|
|
// right after this hook is ran) has guards that lazy-init and return early if called again
|
|
|
|
// later by the real WikiPage.
|
|
|
|
|
2013-11-14 20:12:27 +00:00
|
|
|
// Specify format the same way the API and EditPage do to avoid extra parsing
|
|
|
|
$format = $content->getContentHandler()->getDefaultFormat();
|
|
|
|
$editInfo = $page->prepareContentForEdit( $content, null, $user, $format );
|
2013-02-22 18:50:54 +00:00
|
|
|
|
2017-09-01 20:44:02 +00:00
|
|
|
$templateDataStatus = $editInfo->output->getExtensionData( 'TemplateDataStatus' );
|
|
|
|
if ( $templateDataStatus instanceof Status && !$templateDataStatus->isOK() ) {
|
|
|
|
// Abort edit, show error message from TemplateDataBlob::getStatus
|
|
|
|
$status->merge( $templateDataStatus );
|
|
|
|
return false;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-21 03:46:02 +00:00
|
|
|
/**
|
|
|
|
* Parser hook registering the GUI module only in edit pages.
|
|
|
|
*
|
|
|
|
* @param EditPage $editPage
|
|
|
|
* @param OutputPage $output
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-01 20:44:02 +00:00
|
|
|
public static function onEditPage( EditPage $editPage, OutputPage $output ) {
|
2013-09-21 03:46:02 +00:00
|
|
|
global $wgTemplateDataUseGUI;
|
|
|
|
if ( $wgTemplateDataUseGUI ) {
|
2017-09-01 20:44:02 +00:00
|
|
|
if ( $output->getTitle()->inNamespace( NS_TEMPLATE ) ) {
|
2013-09-21 03:46:02 +00:00
|
|
|
$output->addModules( 'ext.templateDataGenerator.editPage' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
|
|
|
* Parser hook for <templatedata>.
|
|
|
|
* If there is any JSON provided, render the template documentation on the page.
|
|
|
|
*
|
2017-09-01 04:57:44 +00:00
|
|
|
* @param string $input The content of the tag.
|
|
|
|
* @param array $args The attributes of the tag.
|
|
|
|
* @param Parser $parser Parser instance available to render
|
2013-02-22 18:50:54 +00:00
|
|
|
* wikitext into html, or parser methods.
|
2017-09-01 04:57:44 +00:00
|
|
|
* @param PPFrame $frame Can be used to see what template parameters ("{{{1}}}", etc.)
|
2013-05-16 15:01:00 +00:00
|
|
|
* this hook was used with.
|
2013-02-22 18:50:54 +00:00
|
|
|
*
|
2017-09-01 04:57:44 +00:00
|
|
|
* @return string HTML to insert in the page.
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
|
|
|
public static function render( $input, $args, $parser, $frame ) {
|
|
|
|
$ti = TemplateDataBlob::newFromJSON( $input );
|
|
|
|
|
|
|
|
$status = $ti->getStatus();
|
|
|
|
if ( !$status->isOK() ) {
|
2017-09-01 20:44:02 +00:00
|
|
|
$parser->getOutput()->setExtensionData( 'TemplateDataStatus', $status );
|
2016-08-31 07:57:44 +00:00
|
|
|
return '<div class="errorbox">' . $status->getHTML() . '</div>';
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:33:45 +00:00
|
|
|
$parser->getOutput()->setProperty( 'templatedata', $ti->getJSONForDatabase() );
|
2013-02-22 18:50:54 +00:00
|
|
|
|
2015-09-30 22:13:26 +00:00
|
|
|
$parser->getOutput()->addModuleStyles( 'ext.templateData' );
|
2016-08-31 07:57:44 +00:00
|
|
|
$parser->enableOOUI();
|
2013-02-22 18:50:54 +00:00
|
|
|
|
2013-08-01 20:57:42 +00:00
|
|
|
return $ti->getHtml( $parser->getOptions()->getUserLangObj() );
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
}
|