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