| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Deferred\LinksUpdate; |
| 4 |
|
| 5 |
use HTMLCacheUpdateJob; |
| 6 |
use JobQueueGroup; |
| 7 |
use MediaWiki\Config\ServiceOptions; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Parser\ParserOutput; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class PagePropsTable extends LinksTable { |
| 21 |
|
| 22 |
private $jobQueueGroup; |
| 23 |
|
| 24 |
|
| 25 |
private $newProps = []; |
| 26 |
|
| 27 |
|
| 28 |
private $existingProps; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private $linkInvalidations; |
| 39 |
|
| 40 |
public const CONSTRUCTOR_OPTIONS = [ MainConfigNames::PagePropLinkInvalidations ]; |
| 41 |
|
| 42 |
public function __construct( |
| 43 |
ServiceOptions $options, |
| 44 |
JobQueueGroup $jobQueueGroup |
| 45 |
) { |
| 46 |
$this->jobQueueGroup = $jobQueueGroup; |
| 47 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 48 |
$this->linkInvalidations = $options->get( MainConfigNames::PagePropLinkInvalidations ); |
| 49 |
} |
| 50 |
|
| 51 |
public function setParserOutput( ParserOutput $parserOutput ) { |
| 52 |
$this->newProps = $parserOutput->getPageProperties(); |
| 53 |
} |
| 54 |
|
| 55 |
protected function getTableName() { |
| 56 |
return 'page_props'; |
| 57 |
} |
| 58 |
|
| 59 |
protected function getFromField() { |
| 60 |
return 'pp_page'; |
| 61 |
} |
| 62 |
|
| 63 |
protected function getExistingFields() { |
| 64 |
return [ 'pp_propname', 'pp_value' ]; |
| 65 |
} |
| 66 |
|
| 67 |
protected function getNewLinkIDs() { |
| 68 |
foreach ( $this->newProps as $name => $value ) { |
| 69 |
yield [ (string)$name, $value ]; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private function getExistingProps() { |
| 79 |
if ( $this->existingProps === null ) { |
| 80 |
$this->existingProps = []; |
| 81 |
foreach ( $this->fetchExistingRows() as $row ) { |
| 82 |
$this->existingProps[$row->pp_propname] = $row->pp_value; |
| 83 |
} |
| 84 |
} |
| 85 |
return $this->existingProps; |
| 86 |
} |
| 87 |
|
| 88 |
protected function getExistingLinkIDs() { |
| 89 |
foreach ( $this->getExistingProps() as $name => $value ) { |
| 90 |
yield [ (string)$name, $value ]; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
protected function isExisting( $linkId ) { |
| 95 |
$existing = $this->getExistingProps(); |
| 96 |
[ $name, $value ] = $linkId; |
| 97 |
return \array_key_exists( $name, $existing ) |
| 98 |
&& $this->encodeValue( $existing[$name] ) === $this->encodeValue( $value ); |
| 99 |
} |
| 100 |
|
| 101 |
protected function isInNewSet( $linkId ) { |
| 102 |
[ $name, $value ] = $linkId; |
| 103 |
return \array_key_exists( $name, $this->newProps ) |
| 104 |
&& $this->encodeValue( $this->newProps[$name] ) === $this->encodeValue( $value ); |
| 105 |
} |
| 106 |
|
| 107 |
private function encodeValue( $value ) { |
| 108 |
if ( is_bool( $value ) ) { |
| 109 |
return (string)(int)$value; |
| 110 |
} elseif ( $value === null ) { |
| 111 |
return ''; |
| 112 |
} else { |
| 113 |
return (string)$value; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
protected function insertLink( $linkId ) { |
| 118 |
[ $name, $value ] = $linkId; |
| 119 |
$this->insertRow( [ |
| 120 |
'pp_propname' => $name, |
| 121 |
'pp_value' => $this->encodeValue( $value ), |
| 122 |
'pp_sortkey' => $this->getPropertySortKeyValue( $value ) |
| 123 |
] ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private function getPropertySortKeyValue( $value ) { |
| 139 |
if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) { |
| 140 |
return floatval( $value ); |
| 141 |
} |
| 142 |
|
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
protected function deleteLink( $linkId ) { |
| 147 |
$this->deleteRow( [ |
| 148 |
'pp_propname' => $linkId[0] |
| 149 |
] ); |
| 150 |
} |
| 151 |
|
| 152 |
protected function finishUpdate() { |
| 153 |
$changed = array_unique( array_merge( |
| 154 |
array_column( $this->insertedLinks, 0 ), |
| 155 |
array_column( $this->deletedLinks, 0 ) ) ); |
| 156 |
$this->invalidateProperties( $changed ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
private function invalidateProperties( array $changed ) { |
| 165 |
$jobs = []; |
| 166 |
foreach ( $changed as $name ) { |
| 167 |
if ( isset( $this->linkInvalidations[$name] ) ) { |
| 168 |
$inv = $this->linkInvalidations[$name]; |
| 169 |
if ( !is_array( $inv ) ) { |
| 170 |
$inv = [ $inv ]; |
| 171 |
} |
| 172 |
foreach ( $inv as $table ) { |
| 173 |
$jobs[] = HTMLCacheUpdateJob::newForBacklinks( |
| 174 |
$this->getSourcePage(), |
| 175 |
$table, |
| 176 |
[ 'causeAction' => 'page-props' ] |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if ( $jobs ) { |
| 183 |
$this->jobQueueGroup->lazyPush( $jobs ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function getAssocArray( $setType ) { |
| 194 |
$props = []; |
| 195 |
foreach ( $this->getLinkIDs( $setType ) as [ $name, $value ] ) { |
| 196 |
$props[$name] = $value; |
| 197 |
} |
| 198 |
return $props; |
| 199 |
} |
| 200 |
} |