SessionManager: Save user name to metadata even if the user doesn't exist locally
[mediawiki.git] / includes / PageProps.php
blob0a3a3247898815dc69189222d56f28c4846a6c1c
1 <?php
2 /**
3 * Access to properties of a page.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Gives access to properties of a page.
26 * @since 1.27
29 class PageProps {
31 /**
32 * @var PageProps
34 private static $instance;
36 /**
37 * @return PageProps
39 public static function getInstance() {
40 if ( self::$instance === null ) {
41 self::$instance = new self();
43 return self::$instance;
46 /** Cache parameters */
47 const CACHE_TTL = 10; // integer; TTL in seconds
48 const CACHE_SIZE = 100; // integer; max cached pages
50 /** Property cache */
51 private $cache = null;
53 /**
54 * Create a PageProps object
56 private function __construct() {
57 $this->cache = new ProcessCacheLRU( self::CACHE_SIZE );
60 /**
61 * Given one or more Titles and the name of a property, returns an
62 * associative array mapping page ID to property value. Pages in the
63 * provided set of Titles that do not have a value for the given
64 * property will not appear in the returned array. If a single Title
65 * is provided, it does not need to be passed in an array, but an array
66 * will always be returned. An empty array will be returned if no
67 * matching properties were found.
69 * @param array|Title $titles
70 * @param string $propertyName
72 * @return array associative array mapping page ID to property value
75 public function getProperty( $titles, $propertyName ) {
76 $values = array();
77 $goodIDs = $this->getGoodIDs( $titles );
78 $queryIDs = array();
79 foreach ( $goodIDs as $pageID ) {
80 $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
81 if ( $propertyValue === false ) {
82 $queryIDs[] = $pageID;
83 } else {
84 $values[$pageID] = $propertyValue;
88 if ( $queryIDs != array() ) {
89 $dbr = wfGetDB( DB_SLAVE );
90 $result = $dbr->select(
91 'page_props',
92 array(
93 'pp_page',
94 'pp_value'
96 array(
97 'pp_page' => $queryIDs,
98 'pp_propname' => $propertyName
100 __METHOD__
103 foreach ( $result as $row ) {
104 $pageID = $row->pp_page;
105 $propertyValue = $row->pp_value;
106 $this->cacheProperty( $pageID, $propertyName, $propertyValue );
107 $values[$pageID] = $propertyValue;
111 return $values;
115 * Get all page property values.
116 * Given one or more Titles, returns an associative array mapping page
117 * ID to an associative array mapping property names to property
118 * values. Pages in the provided set of Titles that do not have any
119 * properties will not appear in the returned array. If a single Title
120 * is provided, it does not need to be passed in an array, but an array
121 * will always be returned. An empty array will be returned if no
122 * matching properties were found.
124 * @param array|Title $titles
126 * @return array associative array mapping page ID to property value array
129 public function getProperties( $titles ) {
130 $values = array();
131 $goodIDs = $this->getGoodIDs( $titles );
132 $queryIDs = array();
133 foreach ( $goodIDs as $pageID ) {
134 $pageProperties = $this->getCachedProperties( $pageID );
135 if ( $pageProperties === false ) {
136 $queryIDs[] = $pageID;
137 } else {
138 $values[$pageID] = $pageProperties;
142 if ( $queryIDs != array() ) {
143 $dbr = wfGetDB( DB_SLAVE );
144 $result = $dbr->select(
145 'page_props',
146 array(
147 'pp_page',
148 'pp_propname',
149 'pp_value'
151 array(
152 'pp_page' => $queryIDs,
154 __METHOD__
157 $currentPageID = 0;
158 $pageProperties = array();
159 foreach ( $result as $row ) {
160 $pageID = $row->pp_page;
161 if ( $currentPageID != $pageID ) {
162 if ( $pageProperties != array() ) {
163 $this->cacheProperties( $currentPageID, $pageProperties );
164 $values[$currentPageID] = $pageProperties;
166 $currentPageID = $pageID;
167 $pageProperties = array();
169 $pageProperties[$row->pp_propname] = $row->pp_value;
171 if ( $pageProperties != array() ) {
172 $this->cacheProperties( $pageID, $pageProperties );
173 $values[$pageID] = $pageProperties;
177 return $values;
181 * @param array|Title $titles
183 * @return array array of good page IDs
186 private function getGoodIDs( $titles ) {
187 $result = array();
188 if ( is_array( $titles ) ) {
189 foreach ( $titles as $title ) {
190 $pageID = $title->getArticleID();
191 if ( $pageID > 0 ) {
192 $result[] = $pageID;
195 } else {
196 $pageID = $titles->getArticleID();
197 if ( $pageID > 0 ) {
198 $result[] = $pageID;
201 return $result;
205 * Get a property from the cache.
207 * @param int $pageID page ID of page being queried
208 * @param string $propertyName name of property being queried
210 * @return string|bool property value array or false if not found
213 private function getCachedProperty( $pageID, $propertyName ) {
214 if ( $this->cache->has( $pageID, $propertyName, self::CACHE_TTL ) ) {
215 return $this->cache->get( $pageID, $propertyName );
217 if ( $this->cache->has( 0, $pageID, self::CACHE_TTL ) ) {
218 $pageProperties = $this->cache->get( 0, $pageID );
219 if ( isset( $pageProperties[$propertyName] ) ) {
220 return $pageProperties[$propertyName];
223 return false;
227 * Get properties from the cache.
229 * @param int $pageID page ID of page being queried
231 * @return string|bool property value array or false if not found
234 private function getCachedProperties( $pageID ) {
235 if ( $this->cache->has( 0, $pageID, self::CACHE_TTL ) ) {
236 return $this->cache->get( 0, $pageID );
238 return false;
242 * Save a property to the cache.
244 * @param int $pageID page ID of page being cached
245 * @param string $propertyName name of property being cached
246 * @param mixed $propertyValue value of property
249 private function cacheProperty( $pageID, $propertyName, $propertyValue ) {
250 $this->cache->set( $pageID, $propertyName, $propertyValue );
254 * Save properties to the cache.
256 * @param int $pageID page ID of page being cached
257 * @param array $pageProperties associative array of page properties to be cached
260 private function cacheProperties( $pageID, $pageProperties ) {
261 $this->cache->clear( $pageID );
262 $this->cache->set( 0, $pageID, $pageProperties );