| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite; |
| 4 |
|
| 5 |
use MediaWiki\Parser\Sanitizer; |
| 6 |
use StatusValue; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class Validator { |
| 14 |
|
| 15 |
private ReferenceStack $referenceStack; |
| 16 |
private ?string $inReferencesGroup; |
| 17 |
private bool $isSectionPreview; |
| 18 |
private bool $isExtendsEnabled; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( |
| 28 |
ReferenceStack $referenceStack, |
| 29 |
?string $inReferencesGroup = null, |
| 30 |
bool $isSectionPreview = false, |
| 31 |
bool $isExtendsEnabled = false |
| 32 |
) { |
| 33 |
$this->referenceStack = $referenceStack; |
| 34 |
$this->inReferencesGroup = $inReferencesGroup; |
| 35 |
$this->isSectionPreview = $isSectionPreview; |
| 36 |
$this->isExtendsEnabled = $isExtendsEnabled; |
| 37 |
} |
| 38 |
|
| 39 |
public function validateRef( |
| 40 |
?string $text, |
| 41 |
string $group, |
| 42 |
?string $name, |
| 43 |
?string $extends, |
| 44 |
?string $follow, |
| 45 |
?string $dir |
| 46 |
): StatusValue { |
| 47 |
if ( ctype_digit( (string)$name ) |
| 48 |
|| ctype_digit( (string)$extends ) |
| 49 |
|| ctype_digit( (string)$follow ) |
| 50 |
) { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
return StatusValue::newFatal( 'cite_error_ref_numeric_key' ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( $extends ) { |
| 59 |
|
| 60 |
if ( !$this->isExtendsEnabled ) { |
| 61 |
return StatusValue::newFatal( 'cite_error_ref_too_many_keys' ); |
| 62 |
} |
| 63 |
|
| 64 |
$groupRefs = $this->referenceStack->getGroupRefs( $group ); |
| 65 |
|
| 66 |
if ( isset( $groupRefs[$name ?? ''] ) && $groupRefs[$name ?? '']->extends === null ) { |
| 67 |
|
| 68 |
return StatusValue::newFatal( 'cite_error_references_duplicate_key', $name ); |
| 69 |
} elseif ( isset( $groupRefs[$extends]->extends ) ) { |
| 70 |
|
| 71 |
return StatusValue::newFatal( 'cite_error_ref_nested_extends', $extends, |
| 72 |
$groupRefs[$extends]->extends ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if ( $follow && ( $name || $extends ) ) { |
| 77 |
return StatusValue::newFatal( 'cite_error_ref_follow_conflicts' ); |
| 78 |
} |
| 79 |
|
| 80 |
if ( $dir !== null && $dir !== 'rtl' && $dir !== 'ltr' ) { |
| 81 |
return StatusValue::newFatal( 'cite_error_ref_invalid_dir', $dir ); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->inReferencesGroup === null ? |
| 85 |
$this->validateRefOutsideOfReferenceList( $text, $name ) : |
| 86 |
$this->validateRefInReferenceList( $text, $group, $name ); |
| 87 |
} |
| 88 |
|
| 89 |
private function validateRefOutsideOfReferenceList( |
| 90 |
?string $text, |
| 91 |
?string $name |
| 92 |
): StatusValue { |
| 93 |
if ( !$name ) { |
| 94 |
if ( $text === null ) { |
| 95 |
|
| 96 |
return StatusValue::newFatal( 'cite_error_ref_no_key' ); |
| 97 |
} elseif ( trim( $text ) === '' ) { |
| 98 |
|
| 99 |
return StatusValue::newFatal( 'cite_error_ref_no_input' ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ( $text !== null && preg_match( |
| 104 |
'/<ref(erences)?\b[^>]*+>/i', |
| 105 |
preg_replace( '#<(\w++)[^>]*+>.*?</\1\s*>|<!--.*?-->#s', '', $text ) |
| 106 |
) ) { |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
return StatusValue::newFatal( 'cite_error_included_ref' ); |
| 118 |
} |
| 119 |
|
| 120 |
return StatusValue::newGood(); |
| 121 |
} |
| 122 |
|
| 123 |
private function validateRefInReferenceList( |
| 124 |
?string $text, |
| 125 |
string $group, |
| 126 |
?string $name |
| 127 |
): StatusValue { |
| 128 |
if ( $group !== $this->inReferencesGroup ) { |
| 129 |
|
| 130 |
return StatusValue::newFatal( 'cite_error_references_group_mismatch', |
| 131 |
Sanitizer::safeEncodeAttribute( $group ) ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( !$name ) { |
| 135 |
|
| 136 |
return StatusValue::newFatal( 'cite_error_references_no_key' ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $text === null || trim( $text ) === '' ) { |
| 140 |
|
| 141 |
return StatusValue::newFatal( |
| 142 |
'cite_error_empty_references_define', |
| 143 |
Sanitizer::safeEncodeAttribute( $name ), |
| 144 |
Sanitizer::safeEncodeAttribute( $group ) |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
if ( !$this->isSectionPreview ) { |
| 150 |
$groupRefs = $this->referenceStack->getGroupRefs( $group ); |
| 151 |
|
| 152 |
if ( !isset( $groupRefs[$name] ) ) { |
| 153 |
|
| 154 |
return StatusValue::newFatal( 'cite_error_references_missing_key', |
| 155 |
Sanitizer::safeEncodeAttribute( $name ) ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return StatusValue::newGood(); |
| 160 |
} |
| 161 |
|
| 162 |
} |