-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHooks.php
More file actions
73 lines (60 loc) · 2.15 KB
/
Hooks.php
File metadata and controls
73 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare( strict_types = 1 );
namespace ProfessionalWiki\AutomatedValues;
use EditPage;
use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\RevisionAccessException;
use OutputPage;
use ProfessionalWiki\AutomatedValues\DataAccess\RulesJsonValidator;
use Title;
use Wikibase\DataModel\Entity\StatementListProvidingEntity;
use Wikibase\Repo\Content\EntityContent;
class Hooks {
public static function onMultiContentSave( RenderedRevision $renderedRevision ): void {
try {
$content = $renderedRevision->getRevision()->getSlot( 'main' )->getContent();
} catch ( RevisionAccessException $ex ) {
}
if ( isset( $content ) && $content instanceof EntityContent ) {
try {
$entity = $content->getEntity();
} catch ( \Exception $ex ) {
}
if ( isset( $entity ) && $entity instanceof StatementListProvidingEntity ) {
AutomatedValuesFactory::getInstance()->getRulesLookup()->getRules()->applyTo( $entity );
}
}
}
public static function onContentHandlerDefaultModelFor( Title $title, ?string &$model ): void {
if ( AutomatedValuesFactory::getInstance()->isConfigTitle( $title ) ) {
$model = 'json'; // CONTENT_MODEL_JSON (string to make Psalm happy)
}
}
public static function onEditFilter( EditPage $editPage, ?string $text, ?string $section, string &$error ): void {
if ( is_string( $text )
&& AutomatedValuesFactory::getInstance()->isConfigTitle( $editPage->getTitle() )
&& !RulesJsonValidator::newInstance()->validate( $text ) ) {
// Would be nice to show a more specific error message, but at the moment RulesJsonValidator does not support this.
$error = \Html::errorBox( wfMessage( 'automated-values-config-invalid' )->escaped() );
}
}
public static function onAlternateEdit( EditPage $editPage ): void {
if ( AutomatedValuesFactory::getInstance()->isConfigTitle( $editPage->getTitle() ) ) {
$editPage->suppressIntro = true;
}
}
public static function onEditFormPreloadText( string &$text, Title &$title ): void {
if ( AutomatedValuesFactory::getInstance()->isConfigTitle( $title ) ) {
$text = trim( '
{
"rules": [
{
"ruleName": "An optional description",
"when": [
]
}
]
} ' );
}
}
}