MediaWiki REL1_30
WANCacheReapUpdate.php
Go to the documentation of this file.
1<?php
2
3use Psr\Log\LoggerInterface;
5
26 private $db;
28 private $logger;
29
34 public function __construct( IDatabase $db, LoggerInterface $logger ) {
35 $this->db = $db;
36 $this->logger = $logger;
37 }
38
39 function doUpdate() {
40 $reaper = new WANObjectCacheReaper(
41 ObjectCache::getMainWANInstance(),
42 ObjectCache::getLocalClusterInstance(),
43 [ $this, 'getTitleChangeEvents' ],
44 [ $this, 'getEventAffectedKeys' ],
45 [
46 'channel' => 'table:recentchanges:' . $this->db->getDomainID(),
47 'logger' => $this->logger
48 ]
49 );
50
51 $reaper->invoke( 100 );
52 }
53
63 public function getTitleChangeEvents( $start, $id, $end, $limit ) {
64 $db = $this->db;
65 $encStart = $db->addQuotes( $db->timestamp( $start ) );
66 $encEnd = $db->addQuotes( $db->timestamp( $end ) );
67 $id = (int)$id; // cast NULL => 0 since rc_id is an integer
68
69 $res = $db->select(
70 'recentchanges',
71 [ 'rc_namespace', 'rc_title', 'rc_timestamp', 'rc_id' ],
72 [
73 $db->makeList( [
74 "rc_timestamp > $encStart",
75 "rc_timestamp = $encStart AND rc_id > " . $db->addQuotes( $id )
76 ], LIST_OR ),
77 "rc_timestamp < $encEnd"
78 ],
79 __METHOD__,
80 [ 'ORDER BY' => 'rc_timestamp ASC, rc_id ASC', 'LIMIT' => $limit ]
81 );
82
83 $events = [];
84 foreach ( $res as $row ) {
85 $events[] = [
86 'id' => (int)$row->rc_id,
87 'pos' => (int)wfTimestamp( TS_UNIX, $row->rc_timestamp ),
88 'item' => new TitleValue( (int)$row->rc_namespace, $row->rc_title )
89 ];
90 }
91
92 return $events;
93 }
94
105 $entities = [];
106
107 // You can't create a WikiPage for special pages (-1) or other virtual
108 // namespaces, but special pages do appear in RC sometimes, e.g. for logs
109 // of AbuseFilter filter changes.
110 if ( $t->getNamespace() >= 0 ) {
111 $entities[] = WikiPage::factory( Title::newFromTitleValue( $t ) );
112 }
113
114 if ( $t->inNamespace( NS_FILE ) ) {
115 $entities[] = wfLocalFile( $t->getText() );
116 }
117 if ( $t->inNamespace( NS_USER ) ) {
118 $entities[] = User::newFromName( $t->getText(), false );
119 }
120
121 $keys = [];
122 foreach ( $entities as $entity ) {
123 if ( $entity ) {
124 $keys = array_merge( $keys, $entity->getMutableCacheKeys( $cache ) );
125 }
126 }
127 if ( $keys ) {
128 $this->logger->debug( __CLASS__ . ': got key(s) ' . implode( ', ', $keys ) );
129 }
130
131 return $keys;
132 }
133}
wfLocalFile( $title)
Get an object referring to a locally registered file.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Represents a page (or page fragment) title within MediaWiki.
Class for fixing stale WANObjectCache keys using a purge event source.
LoggerInterface $logger
doUpdate()
Perform the actual work.
getTitleChangeEvents( $start, $id, $end, $limit)
__construct(IDatabase $db, LoggerInterface $logger)
getEventAffectedKeys(WANObjectCache $cache, TitleValue $t)
Gets a list of important cache keys associated with a title.
Class for scanning through chronological, log-structured data or change logs and locally purging cach...
Multi-datacenter aware caching interface.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:121
$res
Definition database.txt:21
const NS_FILE
Definition Defines.php:71
const LIST_OR
Definition Defines.php:47
Interface that deferrable updates should implement.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:40
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
addQuotes( $s)
Adds quotes and backslashes.
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
$cache
Definition mcc.php:33