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
|
|
|
|
*/
|
|
|
|
public static function onParserFirstCallInit( &$parser ) {
|
|
|
|
$parser->setHook( 'templatedata', array( 'TemplateDataHooks', 'render' ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:18:10 +00:00
|
|
|
/**
|
|
|
|
* Register unit tests
|
|
|
|
*/
|
|
|
|
public static function onUnitTestsList( array &$files ) {
|
|
|
|
$testDir = __DIR__ . '/tests/';
|
|
|
|
$files = array_merge( $files, glob( "$testDir/*Test.php" ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-21 03:46:02 +00:00
|
|
|
/**
|
|
|
|
* Register qunit unit tests
|
|
|
|
*/
|
|
|
|
public static function onResourceLoaderTestModules(
|
|
|
|
array &$testModules,
|
|
|
|
ResourceLoader &$resourceLoader
|
|
|
|
) {
|
|
|
|
$testModules['qunit']['ext.templateData.test'] = array(
|
|
|
|
'scripts' => array( 'tests/ext.templateData.tests.js' ),
|
2014-10-11 05:22:21 +00:00
|
|
|
'dependencies' => array( 'ext.templateDataGenerator.data' ),
|
2013-09-21 03:46:02 +00:00
|
|
|
'localBasePath' => __DIR__ ,
|
|
|
|
'remoteExtPath' => 'TemplateData',
|
|
|
|
);
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param ResourceLoader $resourceLoader
|
|
|
|
* @return boolean true
|
|
|
|
*/
|
|
|
|
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 ) ) {
|
2014-10-11 05:22:21 +00:00
|
|
|
$resourceLoader->register( array(
|
|
|
|
'jquery.uls.data' => array(
|
|
|
|
'localBasePath' => __DIR__,
|
|
|
|
'remoteExtPath' => 'TemplateData',
|
|
|
|
'scripts' => array(
|
|
|
|
'lib/jquery.uls/src/jquery.uls.data.js',
|
|
|
|
'lib/jquery.uls/src/jquery.uls.data.utils.js',
|
2015-06-02 01:21:34 +00:00
|
|
|
),
|
|
|
|
'targets' => array( 'desktop', 'mobile' ),
|
2014-10-11 05:22:21 +00:00
|
|
|
)
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
|
|
|
* @param Page &$page
|
|
|
|
* @param User &$user
|
|
|
|
* @param Content &$content
|
|
|
|
* @param string &$summary
|
|
|
|
* @param $minor
|
|
|
|
* @param bool|null $watchthis
|
|
|
|
* @param $sectionanchor
|
|
|
|
* @param &$flags
|
|
|
|
* @param Status &$status
|
|
|
|
*/
|
|
|
|
public static function onPageContentSave( &$page, &$user, &$content, &$summary, $minor,
|
|
|
|
$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
|
|
|
|
|
|
|
if ( isset( $editInfo->output->ext_templatedata_status ) ) {
|
|
|
|
$validation = $editInfo->output->ext_templatedata_status;
|
|
|
|
if ( !$validation->isOK() ) {
|
|
|
|
// Abort edit, show error message from TemplateDataBlob::getStatus
|
|
|
|
$status->merge( $validation );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static function onEditPage( $editPage, $output ) {
|
|
|
|
global $wgTemplateDataUseGUI;
|
|
|
|
if ( $wgTemplateDataUseGUI ) {
|
|
|
|
if ( $output->getTitle()->getNamespace() === NS_TEMPLATE ) {
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @param string $input: The content of the tag.
|
|
|
|
* @param array $args: The attributes of the tag.
|
|
|
|
* @param Parser $parser: Parser instance available to render
|
|
|
|
* wikitext into html, or parser methods.
|
2013-05-16 15:01:00 +00:00
|
|
|
* @param PPFrame $frame: Can be used to see what template parameters ("{{{1}}}", etc.)
|
|
|
|
* this hook was used with.
|
2013-02-22 18:50:54 +00:00
|
|
|
*
|
|
|
|
* @return string: HTML to insert in the page.
|
|
|
|
*/
|
|
|
|
public static function render( $input, $args, $parser, $frame ) {
|
2015-11-13 20:35:07 +00:00
|
|
|
$parser->enableOOUI();
|
2013-02-22 18:50:54 +00:00
|
|
|
$ti = TemplateDataBlob::newFromJSON( $input );
|
|
|
|
|
|
|
|
$status = $ti->getStatus();
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
$parser->getOutput()->ext_templatedata_status = $status;
|
2013-12-10 21:10:40 +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' );
|
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
|
|
|
}
|
|
|
|
}
|