MediaWiki master
PagePropsTable.php
Go to the documentation of this file.
1<?php
2
4
10
22 private $jobQueueGroup;
23
25 private $newProps = [];
26
28 private $existingProps;
29
38 private $linkInvalidations;
39
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
56 protected function getTableName() {
57 return 'page_props';
58 }
59
61 protected function getFromField() {
62 return 'pp_page';
63 }
64
66 protected function getExistingFields() {
67 return [ 'pp_propname', 'pp_value' ];
68 }
69
71 protected function getNewLinkIDs() {
72 foreach ( $this->newProps as $name => $value ) {
73 yield [ (string)$name, $value ];
74 }
75 }
76
82 private function getExistingProps() {
83 if ( $this->existingProps === null ) {
84 $this->existingProps = [];
85 foreach ( $this->fetchExistingRows() as $row ) {
86 $this->existingProps[$row->pp_propname] = $row->pp_value;
87 }
88 }
89 return $this->existingProps;
90 }
91
93 protected function getExistingLinkIDs() {
94 foreach ( $this->getExistingProps() as $name => $value ) {
95 yield [ (string)$name, $value ];
96 }
97 }
98
100 protected function isExisting( $linkId ) {
101 $existing = $this->getExistingProps();
102 [ $name, $value ] = $linkId;
103 return \array_key_exists( $name, $existing )
104 && $this->encodeValue( $existing[$name] ) === $this->encodeValue( $value );
105 }
106
108 protected function isInNewSet( $linkId ) {
109 [ $name, $value ] = $linkId;
110 return \array_key_exists( $name, $this->newProps )
111 && $this->encodeValue( $this->newProps[$name] ) === $this->encodeValue( $value );
112 }
113
117 private function encodeValue( $value ): string {
118 if ( is_bool( $value ) ) {
119 return (string)(int)$value;
120 } elseif ( $value === null ) {
121 return '';
122 } else {
123 return (string)$value;
124 }
125 }
126
128 protected function insertLink( $linkId ) {
129 [ $name, $value ] = $linkId;
130 $this->insertRow( [
131 'pp_propname' => $name,
132 'pp_value' => $this->encodeValue( $value ),
133 'pp_sortkey' => $this->getPropertySortKeyValue( $value )
134 ] );
135 }
136
149 private function getPropertySortKeyValue( $value ) {
150 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
151 return floatval( $value );
152 }
153
154 return null;
155 }
156
158 protected function deleteLink( $linkId ) {
159 $this->deleteRow( [
160 'pp_propname' => $linkId[0]
161 ] );
162 }
163
164 protected function finishUpdate() {
165 $changed = array_unique( array_merge(
166 array_column( $this->insertedLinks, 0 ),
167 array_column( $this->deletedLinks, 0 ) ) );
168 $this->invalidateProperties( $changed );
169 }
170
176 private function invalidateProperties( array $changed ) {
177 $jobs = [];
178 foreach ( $changed as $name ) {
179 if ( isset( $this->linkInvalidations[$name] ) ) {
180 $inv = $this->linkInvalidations[$name];
181 if ( !is_array( $inv ) ) {
182 $inv = [ $inv ];
183 }
184 foreach ( $inv as $table ) {
186 $this->getSourcePage(),
187 $table,
188 [ 'causeAction' => 'page-props' ]
189 );
190 }
191 }
192 }
193
194 if ( $jobs ) {
195 $this->jobQueueGroup->lazyPush( $jobs );
196 }
197 }
198
205 public function getAssocArray( $setType ) {
206 $props = [];
207 foreach ( $this->getLinkIDs( $setType ) as [ $name, $value ] ) {
208 $props[$name] = $value;
209 }
210 return $props;
211 }
212}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
The base class for classes which update a single link table.
fetchExistingRows()
Do a select query to fetch the existing rows.
finishUpdate()
Subclasses can override this to do any updates associated with their link data, for example dispatchi...
getFromField()
Get the name of the field which links to page_id.string
getExistingFields()
Get the fields to be used in fetchExistingRows().Note that fetchExistingRows() is just a helper for s...
insertLink( $linkId)
Insert a link identified by ID.The subclass is expected to queue the insertion by calling insertRow()...
isExisting( $linkId)
Determine whether a link (from the new set) is in the existing set.bool
deleteLink( $linkId)
Delete a link identified by ID.The subclass is expected to queue the deletion by calling deleteRow().
getExistingLinkIDs()
Get an array (or iterator) of link IDs for the existing state.The subclass should load the data from ...
__construct(ServiceOptions $options, JobQueueGroup $jobQueueGroup)
setParserOutput(ParserOutput $parserOutput)
Subclasses should implement this to extract the data they need from the ParserOutput.
getAssocArray( $setType)
Get the properties for a given link set as an associative array.
getNewLinkIDs()
Get an array (or iterator) of link IDs for the new state.See the LinksTable doc comment for an explan...
isInNewSet( $linkId)
Determine whether a link (from the existing set) is in the new set.bool
Handle enqueueing of background jobs.
Job to purge the HTML/file cache for all pages that link to or use another page or file.
static newForBacklinks(PageReference $page, $table, $params=[])
A class containing constants representing the names of configuration variables.
const PagePropLinkInvalidations
Name constant for the PagePropLinkInvalidations setting, for use with Config::get()
ParserOutput is a rendering of a Content object or a message.
getPageProperties()
Return all the page properties set on this ParserOutput.