MediaWiki  1.33.0
WANCacheReapUpdate.php
Go to the documentation of this file.
1 <?php
2 
5 use 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  function doUpdate() {
42  $reaper = new WANObjectCacheReaper(
43  MediaWikiServices::getInstance()->getMainWANObjectCache(),
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[] = WikiPage::factory( Title::newFromLinkTarget( $t ) );
114  }
115 
116  if ( $t->inNamespace( NS_FILE ) ) {
117  $entities[] = wfLocalFile( $t->getText() );
118  }
119  if ( $t->inNamespace( NS_USER ) ) {
120  $entities[] = User::newFromName( $t->getText(), false );
121  }
122 
123  $keys = [];
124  foreach ( $entities as $entity ) {
125  if ( $entity ) {
126  $keys = array_merge( $keys, $entity->getMutableCacheKeys( $cache ) );
127  }
128  }
129  if ( $keys ) {
130  $this->logger->debug( __CLASS__ . ': got key(s) ' . implode( ', ', $keys ) );
131  }
132 
133  return $keys;
134  }
135 }
WANCacheReapUpdate\$db
IDatabase $db
Definition: WANCacheReapUpdate.php:28
WANCacheReapUpdate\__construct
__construct(IDatabase $db, LoggerInterface $logger)
Definition: WANCacheReapUpdate.php:36
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:356
Wikimedia\Rdbms\IDatabase\makeList
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1912
NS_FILE
const NS_FILE
Definition: Defines.php:70
WANCacheReapUpdate
Class for fixing stale WANObjectCache keys using a purge event source.
Definition: WANCacheReapUpdate.php:26
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
$res
$res
Definition: database.txt:21
WANCacheReapUpdate\getTitleChangeEvents
getTitleChangeEvents( $start, $id, $end, $limit)
Definition: WANCacheReapUpdate.php:65
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\IDatabase\timestamp
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
LIST_OR
const LIST_OR
Definition: Defines.php:46
WANCacheReapUpdate\getEventAffectedKeys
getEventAffectedKeys(WANObjectCache $cache, LinkTarget $t)
Gets a list of important cache keys associated with a title.
Definition: WANCacheReapUpdate.php:105
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
WANCacheReapUpdate\$logger
LoggerInterface $logger
Definition: WANCacheReapUpdate.php:30
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:116
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:269
$cache
$cache
Definition: mcc.php:33
Wikimedia\Rdbms\IDatabase\addQuotes
addQuotes( $s)
Adds quotes and backslashes.
WANObjectCacheReaper\invoke
invoke( $n=100)
Check and reap stale keys based on a chunk of events.
Definition: WANObjectCacheReaper.php:108
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:67
Wikimedia\Rdbms\IDatabase\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
NS_USER
const NS_USER
Definition: Defines.php:66
WANObjectCacheReaper
Class for scanning through chronological, log-structured data or change logs and locally purging cach...
Definition: WANObjectCacheReaper.php:37
WANCacheReapUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: WANCacheReapUpdate.php:41
$t
$t
Definition: testCompression.php:69
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferrableUpdate.php:9
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:2688
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36