MediaWiki  1.30.0
GadgetDefinitionNamespaceRepo.php
Go to the documentation of this file.
1 <?php
2 
4 
15  const CACHE_TTL = 86400;
16 
20  private $wanCache;
21 
22  public function __construct() {
23  $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
24  }
25 
31  public function getGadgetIds() {
32  $key = $this->getGadgetIdsKey();
33 
34  return $this->wanCache->getWithSetCallback(
35  $key,
36  self::CACHE_TTL,
37  function ( $oldValue, &$ttl, array &$setOpts ) {
38  $dbr = wfGetDB( DB_SLAVE );
39  $setOpts += Database::getCacheSetOptions( $dbr );
40 
41  return $dbr->selectFieldValues(
42  'page',
43  'page_title',
44  [ 'page_namespace' => NS_GADGET_DEFINITION ],
45  __METHOD__
46  );
47  },
48  [
49  'checkKeys' => [ $key ],
51  'lockTSE' => 30
52  ]
53  );
54  }
55 
59  public function purgeGadgetIdsList() {
60  $this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
61  }
62 
68  public function getGadget( $id ) {
69  $key = $this->getGadgetCacheKey( $id );
70  $gadget = $this->wanCache->getWithSetCallback(
71  $key,
72  self::CACHE_TTL,
73  function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
74  $setOpts += Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) );
75  $title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
76  if ( !$title ) {
78  return null;
79  }
80 
82  if ( !$rev ) {
84  return null;
85  }
86 
87  $content = $rev->getContent();
88  if ( !$content instanceof GadgetDefinitionContent ) {
89  // Uhm...
91  return null;
92  }
93 
94  return Gadget::newFromDefinitionContent( $id, $content );
95  },
96  [
97  'checkKeys' => [ $key ],
99  'lockTSE' => 30
100  ]
101  );
102 
103  if ( $gadget === null ) {
104  throw new InvalidArgumentException( "No gadget registered for '$id'" );
105  }
106 
107  return $gadget;
108  }
109 
115  public function purgeGadgetEntry( $id ) {
116  $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
117  }
118 
122  private function getGadgetIdsKey() {
123  return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
124  }
125 
130  private function getGadgetCacheKey( $id ) {
131  return $this->wanCache->makeKey(
132  'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
133  }
134 }
WANObjectCache\TTL_UNCACHEABLE
const TTL_UNCACHEABLE
Idiom for getWithSetCallback() callbacks to avoid calling set()
Definition: WANObjectCache.php:128
GadgetDefinitionNamespaceRepo\getGadgetCacheKey
getGadgetCacheKey( $id)
Definition: GadgetDefinitionNamespaceRepo.php:130
GadgetDefinitionNamespaceRepo\CACHE_TTL
const CACHE_TTL
How long in seconds the list of gadget ids and individual gadgets should be cached for (1 day)
Definition: GadgetDefinitionNamespaceRepo.php:15
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
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:37
Gadget\newFromDefinitionContent
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition: Gadgets_body.php:74
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
GadgetDefinitionNamespaceRepo\__construct
__construct()
Definition: GadgetDefinitionNamespaceRepo.php:22
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:134
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
GadgetDefinitionNamespaceRepo\purgeGadgetIdsList
purgeGadgetIdsList()
Purge the list of gadget ids when a page is deleted or if a new page is created.
Definition: GadgetDefinitionNamespaceRepo.php:59
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
GadgetDefinitionNamespaceRepo\$wanCache
WANObjectCache $wanCache
Definition: GadgetDefinitionNamespaceRepo.php:20
GadgetRepo
Definition: GadgetRepo.php:3
GadgetDefinitionNamespaceRepo\getGadgetIds
getGadgetIds()
Get a list of gadget ids from cache/database.
Definition: GadgetDefinitionNamespaceRepo.php:31
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:557
GadgetDefinitionContent
Definition: GadgetDefinitionContent.php:23
IExpiringStore\TTL_PROC_SHORT
const TTL_PROC_SHORT
Definition: IExpiringStore.php:41
GadgetDefinitionNamespaceRepo
GadgetRepo implementation where each gadget has a page in the Gadget definition namespace,...
Definition: GadgetDefinitionNamespaceRepo.php:10
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:80
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1750
GadgetDefinitionNamespaceRepo\getGadget
getGadget( $id)
Definition: GadgetDefinitionNamespaceRepo.php:68
GadgetDefinitionNamespaceRepo\getGadgetIdsKey
getGadgetIdsKey()
Definition: GadgetDefinitionNamespaceRepo.php:122
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
GadgetDefinitionNamespaceRepo\purgeGadgetEntry
purgeGadgetEntry( $id)
Update the cache for a specific Gadget whenever it is updated.
Definition: GadgetDefinitionNamespaceRepo.php:115
Gadget\GADGET_CLASS_VERSION
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition: Gadgets_body.php:21
array
the array() calling protocol came about after MediaWiki 1.4rc1.