|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMW\ImageCaption; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\ApplicationFactory; |
|
6
|
|
|
use SMW\Schema\SchemaTypes; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @license GNU GPL v2+ |
|
10
|
|
|
* @since 1.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @author mwjames |
|
13
|
|
|
*/ |
|
14
|
|
|
class Hooks { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @since 1.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
public function register() { |
|
20
|
|
|
|
|
21
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
|
22
|
|
|
return; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$handlers = [ |
|
26
|
|
|
'SMW::Schema::RegisterSchemaTypes' => [ $this, 'onRegisterSchemaTypes' ], |
|
27
|
|
|
'ImageBeforeProduceHTML' => [ $this, 'onImageBeforeProduceHTML' ] |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
foreach ( $handlers as $name => $callback ) { |
|
31
|
|
|
|
|
32
|
|
|
if ( |
|
33
|
|
|
!class_exists( '\MediaWiki\MediaWikiServices' ) || |
|
34
|
|
|
!method_exists( \MediaWiki\MediaWikiServices::getInstance(), 'getHookContainer' ) ) { |
|
35
|
|
|
\Hooks::register( $name, $callback ); |
|
36
|
|
|
} else { |
|
37
|
|
|
\MediaWiki\MediaWikiServices::getInstance()->getHookContainer()->register( $name, $callback ); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @see https://www.semantic-mediawiki.org/wiki/Hooks#SMW::Schema::RegisterSchemaTypes |
|
44
|
|
|
* |
|
45
|
|
|
* @since 1.0 |
|
46
|
|
|
* |
|
47
|
|
|
* @param SchemaTypes $schemaTypes |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function onRegisterSchemaTypes( SchemaTypes $schemaTypes ) { |
|
50
|
|
|
|
|
51
|
|
|
$params = [ |
|
52
|
|
|
'group' => 'schema/group/imagecaption', |
|
53
|
|
|
'validation_schema' => __DIR__ . '/../data/schema/imagecaption-rule-schema.v1.json', |
|
54
|
|
|
'type_description' => 'semantic-imagecaption-rule-schema-description' |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
$schemaTypes->registerSchemaType( ImageCaption::SCHEMA_TYPE, $params ); |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Hook: Called before producing the HTML created by a wiki image insertion |
|
64
|
|
|
* |
|
65
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImageBeforeProduceHTML |
|
66
|
|
|
* |
|
67
|
|
|
* @param DummyLinker &$linker |
|
68
|
|
|
* @param Title &$title |
|
69
|
|
|
* @param &$file |
|
70
|
|
|
* @param array &$frameParams |
|
71
|
|
|
* @param array &$handlerParams |
|
72
|
|
|
* @param &$time |
|
73
|
|
|
* @param &$res |
|
74
|
|
|
* @param Parser $parser |
|
75
|
|
|
* @param string &$query |
|
76
|
|
|
* @param &$widthOption |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function onImageBeforeProduceHTML( &$dummy, &$title, &$file, &$frameParams, &$handlerParams, &$time, &$res, $parser, &$query, &$widthOption ) { |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$applicationFactory = ApplicationFactory::getInstance(); |
|
83
|
|
|
$schemaFactory = $applicationFactory->singleton( 'SchemaFactory' ); |
|
84
|
|
|
|
|
85
|
|
|
$ruleFinder = new RuleFinder( |
|
86
|
|
|
$schemaFactory->newSchemaFinder(), |
|
87
|
|
|
$schemaFactory->newSchemaFilterFactory() |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$imageCaption = new ImageCaption( |
|
91
|
|
|
$applicationFactory->getStore(), |
|
92
|
|
|
$ruleFinder |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$target = $parser->getTitle(); |
|
96
|
|
|
$languageCode = $parser->getOptions()->getUserLang(); |
|
97
|
|
|
|
|
98
|
|
|
$imageCaption->modifyCaption( $target, $file, $frameParams['caption'], $languageCode ); |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.