Merge "Add LoadBalancer::safeWaitForPos()"
[lhc/web/wiklou.git] / includes / PageProps.php
1 <?php
2 /**
3 * Access to properties of a page.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * Gives access to properties of a page.
25 *
26 * @since 1.27
27 *
28 */
29 class PageProps {
30
31 /**
32 * @var PageProps
33 */
34 private static $instance;
35
36 /**
37 * @return PageProps
38 */
39 public static function getInstance() {
40 if ( self::$instance === null ) {
41 self::$instance = new self();
42 }
43 return self::$instance;
44 }
45
46 /** Cache parameters */
47 const CACHE_TTL = 10; // integer; TTL in seconds
48 const CACHE_SIZE = 100; // integer; max cached pages
49
50 /** Property cache */
51 private $cache = null;
52
53 /**
54 * Create a PageProps object
55 */
56 private function __construct() {
57 $this->cache = new ProcessCacheLRU( self::CACHE_SIZE );
58 }
59
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.
68 *
69 * @param array|Title $titles
70 * @param string $propertyName
71 *
72 * @return array associative array mapping page ID to property value
73 *
74 */
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;
85 }
86 }
87
88 if ( $queryIDs != array() ) {
89 $dbr = wfGetDB( DB_SLAVE );
90 $result = $dbr->select(
91 'page_props',
92 array(
93 'pp_page',
94 'pp_value'
95 ),
96 array(
97 'pp_page' => $queryIDs,
98 'pp_propname' => $propertyName
99 ),
100 __METHOD__
101 );
102
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;
108 }
109 }
110
111 return $values;
112 }
113
114 /**
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.
123 *
124 * @param array|Title $titles
125 *
126 * @return array associative array mapping page ID to property value array
127 *
128 */
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;
139 }
140 }
141
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'
150 ),
151 array(
152 'pp_page' => $queryIDs,
153 ),
154 __METHOD__
155 );
156
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;
165 }
166 $currentPageID = $pageID;
167 $pageProperties = array();
168 }
169 $pageProperties[$row->pp_propname] = $row->pp_value;
170 }
171 if ( $pageProperties != array() ) {
172 $this->cacheProperties( $pageID, $pageProperties );
173 $values[$pageID] = $pageProperties;
174 }
175 }
176
177 return $values;
178 }
179
180 /**
181 * @param array|Title $titles
182 *
183 * @return array array of good page IDs
184 *
185 */
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;
193 }
194 }
195 } else {
196 $pageID = $titles->getArticleID();
197 if ( $pageID > 0 ) {
198 $result[] = $pageID;
199 }
200 }
201 return $result;
202 }
203
204 /**
205 * Get a property from the cache.
206 *
207 * @param int $pageID page ID of page being queried
208 * @param string $propertyName name of property being queried
209 *
210 * @return string|bool property value array or false if not found
211 *
212 */
213 private function getCachedProperty( $pageID, $propertyName ) {
214 if ( $this->cache->has( $pageID, $propertyName, self::CACHE_TTL ) ) {
215 return $this->cache->get( $pageID, $propertyName );
216 }
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];
221 }
222 }
223 return false;
224 }
225
226 /**
227 * Get properties from the cache.
228 *
229 * @param int $pageID page ID of page being queried
230 *
231 * @return string|bool property value array or false if not found
232 *
233 */
234 private function getCachedProperties( $pageID ) {
235 if ( $this->cache->has( 0, $pageID, self::CACHE_TTL ) ) {
236 return $this->cache->get( 0, $pageID );
237 }
238 return false;
239 }
240
241 /**
242 * Save a property to the cache.
243 *
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
247 *
248 */
249 private function cacheProperty( $pageID, $propertyName, $propertyValue ) {
250 $this->cache->set( $pageID, $propertyName, $propertyValue );
251 }
252
253 /**
254 * Save properties to the cache.
255 *
256 * @param int $pageID page ID of page being cached
257 * @param array $pageProperties associative array of page properties to be cached
258 *
259 */
260 private function cacheProperties( $pageID, $pageProperties ) {
261 $this->cache->clear( $pageID );
262 $this->cache->set( 0, $pageID, $pageProperties );
263 }
264 }