MediaWiki REL1_33
PageProps.php
Go to the documentation of this file.
1<?php
22use Wikimedia\ScopedCallback;
23
29class PageProps {
30
34 private static $instance;
35
50 public static function overrideInstance( PageProps $store = null ) {
51 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
52 throw new MWException(
53 'Cannot override ' . __CLASS__ . 'default instance in operation.'
54 );
55 }
56 $previousValue = self::$instance;
57 self::$instance = $store;
58 return new ScopedCallback( function () use ( $previousValue ) {
59 self::$instance = $previousValue;
60 } );
61 }
62
66 public static function getInstance() {
67 if ( self::$instance === null ) {
68 self::$instance = new self();
69 }
70 return self::$instance;
71 }
72
74 const CACHE_TTL = 10; // integer; TTL in seconds
75 const CACHE_SIZE = 100; // integer; max cached pages
76
78 private $cache = null;
79
83 private function __construct() {
84 $this->cache = new MapCacheLRU( self::CACHE_SIZE );
85 }
86
91 public function ensureCacheSize( $size ) {
92 if ( $this->cache->getMaxSize() < $size ) {
93 $this->cache->setMaxSize( $size );
94 }
95 }
96
115 public function getProperties( $titles, $propertyNames ) {
116 if ( is_array( $propertyNames ) ) {
117 $gotArray = true;
118 } else {
119 $propertyNames = [ $propertyNames ];
120 $gotArray = false;
121 }
122
123 $values = [];
124 $goodIDs = $this->getGoodIDs( $titles );
125 $queryIDs = [];
126 foreach ( $goodIDs as $pageID ) {
127 foreach ( $propertyNames as $propertyName ) {
128 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
129 if ( $propertyValue === false ) {
130 $queryIDs[] = $pageID;
131 break;
132 } elseif ( $gotArray ) {
133 $values[$pageID][$propertyName] = $propertyValue;
134 } else {
135 $values[$pageID] = $propertyValue;
136 }
137 }
138 }
139
140 if ( $queryIDs ) {
142 $result = $dbr->select(
143 'page_props',
144 [
145 'pp_page',
146 'pp_propname',
147 'pp_value'
148 ],
149 [
150 'pp_page' => $queryIDs,
151 'pp_propname' => $propertyNames
152 ],
153 __METHOD__
154 );
155
156 foreach ( $result as $row ) {
157 $pageID = $row->pp_page;
158 $propertyName = $row->pp_propname;
159 $propertyValue = $row->pp_value;
160 $this->cacheProperty( $pageID, $propertyName, $propertyValue );
161 if ( $gotArray ) {
162 $values[$pageID][$propertyName] = $propertyValue;
163 } else {
164 $values[$pageID] = $propertyValue;
165 }
166 }
167 }
168
169 return $values;
170 }
171
185 public function getAllProperties( $titles ) {
186 $values = [];
187 $goodIDs = $this->getGoodIDs( $titles );
188 $queryIDs = [];
189 foreach ( $goodIDs as $pageID ) {
190 $pageProperties = $this->getCachedProperties( $pageID );
191 if ( $pageProperties === false ) {
192 $queryIDs[] = $pageID;
193 } else {
194 $values[$pageID] = $pageProperties;
195 }
196 }
197
198 if ( $queryIDs != [] ) {
200 $result = $dbr->select(
201 'page_props',
202 [
203 'pp_page',
204 'pp_propname',
205 'pp_value'
206 ],
207 [
208 'pp_page' => $queryIDs,
209 ],
210 __METHOD__
211 );
212
213 $currentPageID = 0;
214 $pageProperties = [];
215 foreach ( $result as $row ) {
216 $pageID = $row->pp_page;
217 if ( $currentPageID != $pageID ) {
218 if ( $pageProperties != [] ) {
219 $this->cacheProperties( $currentPageID, $pageProperties );
220 $values[$currentPageID] = $pageProperties;
221 }
222 $currentPageID = $pageID;
223 $pageProperties = [];
224 }
225 $pageProperties[$row->pp_propname] = $row->pp_value;
226 }
227 if ( $pageProperties != [] ) {
228 $this->cacheProperties( $pageID, $pageProperties );
229 $values[$pageID] = $pageProperties;
230 }
231 }
232
233 return $values;
234 }
235
240 private function getGoodIDs( $titles ) {
241 $result = [];
242 if ( is_array( $titles ) ) {
243 ( new LinkBatch( $titles ) )->execute();
244
245 foreach ( $titles as $title ) {
246 $pageID = $title->getArticleID();
247 if ( $pageID > 0 ) {
248 $result[] = $pageID;
249 }
250 }
251 } else {
252 $pageID = $titles->getArticleID();
253 if ( $pageID > 0 ) {
254 $result[] = $pageID;
255 }
256 }
257 return $result;
258 }
259
267 private function getCachedProperty( $pageID, $propertyName ) {
268 if ( $this->cache->hasField( $pageID, $propertyName, self::CACHE_TTL ) ) {
269 return $this->cache->getField( $pageID, $propertyName );
270 }
271 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
272 $pageProperties = $this->cache->getField( 0, $pageID );
273 if ( isset( $pageProperties[$propertyName] ) ) {
274 return $pageProperties[$propertyName];
275 }
276 }
277 return false;
278 }
279
286 private function getCachedProperties( $pageID ) {
287 if ( $this->cache->hasField( 0, $pageID, self::CACHE_TTL ) ) {
288 return $this->cache->getField( 0, $pageID );
289 }
290 return false;
291 }
292
300 private function cacheProperty( $pageID, $propertyName, $propertyValue ) {
301 $this->cache->setField( $pageID, $propertyName, $propertyValue );
302 }
303
310 private function cacheProperties( $pageID, $pageProperties ) {
311 $this->cache->clear( $pageID );
312 $this->cache->setField( 0, $pageID, $pageProperties );
313 }
314}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
MediaWiki exception.
Handles a simple LRU key/value map with a maximum number of entries.
Gives access to properties of a page.
Definition PageProps.php:29
const CACHE_SIZE
Definition PageProps.php:75
getGoodIDs( $titles)
$cache
Property cache.
Definition PageProps.php:78
static overrideInstance(PageProps $store=null)
Overrides the default instance of this class This is intended for use while testing and will fail if ...
Definition PageProps.php:50
static PageProps $instance
Definition PageProps.php:34
ensureCacheSize( $size)
Ensure that cache has at least this size.
Definition PageProps.php:91
getCachedProperty( $pageID, $propertyName)
Get a property from the cache.
cacheProperty( $pageID, $propertyName, $propertyValue)
Save a property to the cache.
getProperties( $titles, $propertyNames)
Given one or more Titles and one or more names of properties, returns an associative array mapping pa...
const CACHE_TTL
Cache parameters.
Definition PageProps.php:74
getCachedProperties( $pageID)
Get properties from the cache.
__construct()
Create a PageProps object.
Definition PageProps.php:83
cacheProperties( $pageID, $pageProperties)
Save properties to the cache.
static getInstance()
Definition PageProps.php:66
getAllProperties( $titles)
Get all page property values.
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
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\d*-\d*)") will be honored when streaming the file. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition hooks.txt:1991
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
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:37
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition linkcache.txt:17
$batch execute()
you have access to all of the normal MediaWiki so you can get a DB use the cache
const DB_REPLICA
Definition defines.php:25