Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1426898
PagePropsTable.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
PagePropsTable.php
View Options
<?php
namespace
MediaWiki\Deferred\LinksUpdate
;
use
HTMLCacheUpdateJob
;
use
JobQueueGroup
;
use
MediaWiki\Config\ServiceOptions
;
use
MediaWiki\MainConfigNames
;
use
MediaWiki\Parser\ParserOutput
;
/**
* page_props
*
* Link ID format: string[]
* 0: Property name (pp_propname)
* 1: Property value (pp_value)
*
* @since 1.38
*/
class
PagePropsTable
extends
LinksTable
{
/** @var JobQueueGroup */
private
$jobQueueGroup
;
/** @var array */
private
$newProps
=
[];
/** @var array|null */
private
$existingProps
;
/**
* The configured PagePropLinkInvalidations. An associative array where the
* key is the property name and the value is a string or array of strings
* giving the link table names which will be used for backlink cache
* invalidation.
*
* @var array
*/
private
$linkInvalidations
;
public
const
CONSTRUCTOR_OPTIONS
=
[
MainConfigNames
::
PagePropLinkInvalidations
];
public
function
__construct
(
ServiceOptions
$options
,
JobQueueGroup
$jobQueueGroup
)
{
$this
->
jobQueueGroup
=
$jobQueueGroup
;
$options
->
assertRequiredOptions
(
self
::
CONSTRUCTOR_OPTIONS
);
$this
->
linkInvalidations
=
$options
->
get
(
MainConfigNames
::
PagePropLinkInvalidations
);
}
public
function
setParserOutput
(
ParserOutput
$parserOutput
)
{
$this
->
newProps
=
$parserOutput
->
getPageProperties
();
}
protected
function
getTableName
()
{
return
'page_props'
;
}
protected
function
getFromField
()
{
return
'pp_page'
;
}
protected
function
getExistingFields
()
{
return
[
'pp_propname'
,
'pp_value'
];
}
protected
function
getNewLinkIDs
()
{
foreach
(
$this
->
newProps
as
$name
=>
$value
)
{
yield
[
(
string
)
$name
,
$value
];
}
}
/**
* Get the existing page_props as an associative array
*
* @return array
*/
private
function
getExistingProps
()
{
if
(
$this
->
existingProps
===
null
)
{
$this
->
existingProps
=
[];
foreach
(
$this
->
fetchExistingRows
()
as
$row
)
{
$this
->
existingProps
[
$row
->
pp_propname
]
=
$row
->
pp_value
;
}
}
return
$this
->
existingProps
;
}
protected
function
getExistingLinkIDs
()
{
foreach
(
$this
->
getExistingProps
()
as
$name
=>
$value
)
{
yield
[
(
string
)
$name
,
$value
];
}
}
protected
function
isExisting
(
$linkId
)
{
$existing
=
$this
->
getExistingProps
();
[
$name
,
$value
]
=
$linkId
;
return
\array_key_exists
(
$name
,
$existing
)
&&
$this
->
encodeValue
(
$existing
[
$name
]
)
===
$this
->
encodeValue
(
$value
);
}
protected
function
isInNewSet
(
$linkId
)
{
[
$name
,
$value
]
=
$linkId
;
return
\array_key_exists
(
$name
,
$this
->
newProps
)
&&
$this
->
encodeValue
(
$this
->
newProps
[
$name
]
)
===
$this
->
encodeValue
(
$value
);
}
private
function
encodeValue
(
$value
)
{
if
(
is_bool
(
$value
)
)
{
return
(
string
)(
int
)
$value
;
}
elseif
(
$value
===
null
)
{
return
''
;
}
else
{
return
(
string
)
$value
;
}
}
protected
function
insertLink
(
$linkId
)
{
[
$name
,
$value
]
=
$linkId
;
$this
->
insertRow
(
[
'pp_propname'
=>
$name
,
'pp_value'
=>
$this
->
encodeValue
(
$value
),
'pp_sortkey'
=>
$this
->
getPropertySortKeyValue
(
$value
)
]
);
}
/**
* Determines the sort key for the given property value.
* This will return $value if it is a float or int,
* 1 or resp. 0 if it is a bool, and null otherwise.
*
* @note In the future, we may allow the sortkey to be specified explicitly
* in ParserOutput::setPageProperty (T357783).
*
* @param mixed $value
*
* @return float|null
*/
private
function
getPropertySortKeyValue
(
$value
)
{
if
(
is_int
(
$value
)
||
is_float
(
$value
)
||
is_bool
(
$value
)
)
{
return
floatval
(
$value
);
}
return
null
;
}
protected
function
deleteLink
(
$linkId
)
{
$this
->
deleteRow
(
[
'pp_propname'
=>
$linkId
[
0
]
]
);
}
protected
function
finishUpdate
()
{
$changed
=
array_unique
(
array_merge
(
array_column
(
$this
->
insertedLinks
,
0
),
array_column
(
$this
->
deletedLinks
,
0
)
)
);
$this
->
invalidateProperties
(
$changed
);
}
/**
* Invalidate the properties given the list of changed property names
*
* @param string[] $changed
*/
private
function
invalidateProperties
(
array
$changed
)
{
$jobs
=
[];
foreach
(
$changed
as
$name
)
{
if
(
isset
(
$this
->
linkInvalidations
[
$name
]
)
)
{
$inv
=
$this
->
linkInvalidations
[
$name
];
if
(
!
is_array
(
$inv
)
)
{
$inv
=
[
$inv
];
}
foreach
(
$inv
as
$table
)
{
$jobs
[]
=
HTMLCacheUpdateJob
::
newForBacklinks
(
$this
->
getSourcePage
(),
$table
,
[
'causeAction'
=>
'page-props'
]
);
}
}
}
if
(
$jobs
)
{
$this
->
jobQueueGroup
->
lazyPush
(
$jobs
);
}
}
/**
* Get the properties for a given link set as an associative array
*
* @param int $setType The set type as in LinksTable::getLinkIDs()
* @return array
*/
public
function
getAssocArray
(
$setType
)
{
$props
=
[];
foreach
(
$this
->
getLinkIDs
(
$setType
)
as
[
$name
,
$value
]
)
{
$props
[
$name
]
=
$value
;
}
return
$props
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 13:51 (1 d, 20 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
8e/7a/343fa406ad3d3ef8d5323737d13f
Default Alt Text
PagePropsTable.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment