Merge "don't overwrite $item['single-id'] in makeListItem in SkinTemplate"
[lhc/web/wiklou.git] / includes / site / SiteObject.php
1 <?php
2
3 /**
4 * Class representing a single site.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @since 1.21
22 *
23 * @file
24 * @ingroup Site
25 *
26 * @licence GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 * @author Daniel Werner
29 */
30 class SiteObject extends ORMRow implements Site {
31
32 const PATH_LINK = 'link';
33
34 /**
35 * Holds the local ids for this site.
36 * You can obtain them via @see getLocalIds
37 *
38 * @since 1.21
39 *
40 * @var array|false
41 */
42 protected $localIds = false;
43
44 /**
45 * @see Site::getGlobalId
46 *
47 * @since 1.21
48 *
49 * @return string
50 */
51 public function getGlobalId() {
52 return $this->getField( 'global_key' );
53 }
54
55 /**
56 * @see Site::setGlobalId
57 *
58 * @since 1.21
59 *
60 * @param string $globalId
61 */
62 public function setGlobalId( $globalId ) {
63 $this->setField( 'global_key', $globalId );
64 }
65
66 /**
67 * @see Site::getType
68 *
69 * @since 1.21
70 *
71 * @return string
72 */
73 public function getType() {
74 return $this->getField( 'type' );
75 }
76
77 /**
78 * @see Site::setType
79 *
80 * @since 1.21
81 *
82 * @param string $type
83 */
84 public function setType( $type ) {
85 $this->setField( 'type', $type );
86 }
87
88 /**
89 * @see Site::getGroup
90 *
91 * @since 1.21
92 *
93 * @return string
94 */
95 public function getGroup() {
96 return $this->getField( 'group' );
97 }
98
99 /**
100 * @see Site::setGroup
101 *
102 * @since 1.21
103 *
104 * @param string $group
105 */
106 public function setGroup( $group ) {
107 $this->setField( 'group', $group );
108 }
109
110 /**
111 * @see Site::getSource
112 *
113 * @since 1.21
114 *
115 * @return string
116 */
117 public function getSource() {
118 return $this->getField( 'source' );
119 }
120
121 /**
122 * @see Site::setSource
123 *
124 * @since 1.21
125 *
126 * @param string $source
127 */
128 public function setSource( $source ) {
129 $this->setField( 'source', $source );
130 }
131
132 /**
133 * @see Site::getDomain
134 *
135 * @since 1.21
136 *
137 * @return string|false
138 */
139 public function getDomain() {
140 $path = $this->getLinkPath();
141
142 if ( $path === false ) {
143 return false;
144 }
145
146 return parse_url( $path, PHP_URL_HOST );
147 }
148
149 /**
150 * @see Site::getProtocol
151 *
152 * @since 1.21
153 *
154 * @return string|false
155 */
156 public function getProtocol() {
157 $path = $this->getLinkPath();
158
159 if ( $path === false ) {
160 return false;
161 }
162
163 return parse_url( $path, PHP_URL_SCHEME );
164 }
165
166 /**
167 * Sets the path used to construct links with.
168 * @see Site::setLinkPath
169 *
170 * @param string $fullUrl
171 *
172 * @since 1.21
173 *
174 * @throws MWException
175 */
176 public function setLinkPath( $fullUrl ) {
177 $type = $this->getLinkPathType();
178
179 if ( $type === false ) {
180 throw new MWException( "This SiteObject does not support link paths." );
181 }
182
183 $this->setPath( $type, $fullUrl );
184 }
185
186 /**
187 * Returns the path path used to construct links with or false if there is no such path.
188 *
189 * @see Site::getLinkPath
190 *
191 * @return string|false
192 */
193 public function getLinkPath() {
194 $type = $this->getLinkPathType();
195 return $type === false ? false : $this->getPath( $type );
196 }
197
198 /**
199 * @see Site::getLinkPathType
200 *
201 * Returns the main path type, that is the type of the path that should generally be used to construct links
202 * to the target site.
203 *
204 * This default implementation returns SiteObject::PATH_LINK as the default path type. Subclasses can override this
205 * to define a different default path type, or return false to disable site links.
206 *
207 * @since 1.21
208 *
209 * @return string|false
210 */
211 public function getLinkPathType() {
212 return self::PATH_LINK;
213 }
214
215 /**
216 * @see Site::getPageUrl
217 *
218 * This implementation returns a URL constructed using the path returned by getLinkPath().
219 *
220 * @since 1.21
221 *
222 * @param bool|String $pageName
223 *
224 * @return string|false
225 */
226 public function getPageUrl( $pageName = false ) {
227 $url = $this->getLinkPath();
228
229 if ( $url === false ) {
230 return false;
231 }
232
233 if ( $pageName !== false ) {
234 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
235 }
236
237 return $url;
238 }
239
240 /**
241 * Returns $pageName without changes.
242 * Subclasses may override this to apply some kind of normalization.
243 *
244 * @see Site::normalizePageName
245 *
246 * @since 1.21
247 *
248 * @param string $pageName
249 *
250 * @return string
251 */
252 public function normalizePageName( $pageName ) {
253 return $pageName;
254 }
255
256 /**
257 * Returns the value of a type specific field, or the value
258 * of the $default parameter in case it's not set.
259 *
260 * @since 1.21
261 *
262 * @param string $fieldName
263 * @param mixed $default
264 *
265 * @return array
266 */
267 protected function getExtraData( $fieldName, $default = null ) {
268 $data = $this->getField( 'data', array() );
269 return array_key_exists( $fieldName,$data ) ? $data[$fieldName] : $default;
270 }
271
272 /**
273 * Sets the value of a type specific field.
274 * @since 1.21
275 *
276 * @param string $fieldName
277 * @param mixed $value
278 */
279 protected function setExtraData( $fieldName, $value = null ) {
280 $data = $this->getField( 'data', array() );
281 $data[$fieldName] = $value;
282 $this->setField( 'data', $data );
283 }
284
285 /**
286 * @see Site::getLanguageCode
287 *
288 * @since 1.21
289 *
290 * @return string|false
291 */
292 public function getLanguageCode() {
293 return $this->getField( 'language', false );
294 }
295
296 /**
297 * @see Site::setLanguageCode
298 *
299 * @since 1.21
300 *
301 * @param string $languageCode
302 */
303 public function setLanguageCode( $languageCode ) {
304 $this->setField( 'language', $languageCode );
305 }
306
307 /**
308 * Returns the local identifiers of this site.
309 *
310 * @since 1.21
311 *
312 * @param string $type
313 *
314 * @return array
315 */
316 protected function getLocalIds( $type ) {
317 if ( $this->localIds === false ) {
318 $this->loadLocalIds();
319 }
320
321 return array_key_exists( $type, $this->localIds ) ? $this->localIds[$type] : array();
322 }
323
324 /**
325 * Loads the local ids for the site.
326 *
327 * @since 1.21
328 */
329 protected function loadLocalIds() {
330 $dbr = wfGetDB( $this->getTable()->getReadDb() );
331
332 $ids = $dbr->select(
333 'site_identifiers',
334 array(
335 'si_type',
336 'si_key',
337 ),
338 array(
339 'si_site' => $this->getId(),
340 ),
341 __METHOD__
342 );
343
344 $this->localIds = array();
345
346 foreach ( $ids as $id ) {
347 $this->addLocalId( $id->si_type, $id->si_key );
348 }
349 }
350
351 /**
352 * Adds a local identifier.
353 *
354 * @since 1.21
355 *
356 * @param string $type
357 * @param string $identifier
358 */
359 public function addLocalId( $type, $identifier ) {
360 if ( $this->localIds === false ) {
361 $this->localIds = array();
362 }
363
364 if ( !array_key_exists( $type, $this->localIds ) ) {
365 $this->localIds[$type] = array();
366 }
367
368 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
369 $this->localIds[$type][] = $identifier;
370 }
371 }
372
373 /**
374 * @see Site::addInterwikiId
375 *
376 * @since 1.21
377 *
378 * @param string $identifier
379 */
380 public function addInterwikiId( $identifier ) {
381 $this->addLocalId( 'interwiki', $identifier );
382 }
383
384 /**
385 * @see Site::addNavigationId
386 *
387 * @since 1.21
388 *
389 * @param string $identifier
390 */
391 public function addNavigationId( $identifier ) {
392 $this->addLocalId( 'equivalent', $identifier );
393 }
394
395 /**
396 * @see Site::getInterwikiIds
397 *
398 * @since 1.21
399 *
400 * @return array of string
401 */
402 public function getInterwikiIds() {
403 return $this->getLocalIds( 'interwiki' );
404 }
405
406 /**
407 * @see Site::getNavigationIds
408 *
409 * @since 1.21
410 *
411 * @return array of string
412 */
413 public function getNavigationIds() {
414 return $this->getLocalIds( 'equivalent' );
415 }
416
417 /**
418 * @see Site::getInternalId
419 *
420 * @since 1.21
421 *
422 * @return integer
423 */
424 public function getInternalId() {
425 return $this->getId();
426 }
427
428 /**
429 * @see ORMRow::save
430 * @see Site::save
431 *
432 * @since 1.21
433 *
434 * @param string|null $functionName
435 *
436 * @return boolean Success indicator
437 */
438 public function save( $functionName = null ) {
439 $dbw = wfGetDB( DB_MASTER );
440
441 $trx = $dbw->trxLevel();
442
443 if ( $trx == 0 ) {
444 $dbw->begin( __METHOD__ );
445 }
446
447 $this->setField( 'protocol', $this->getProtocol() );
448 $this->setField( 'domain', strrev( $this->getDomain() ) . '.' );
449
450 $existedAlready = $this->hasIdField();
451
452 $success = parent::save( $functionName );
453
454 if ( $success && $existedAlready ) {
455 $dbw->delete(
456 'site_identifiers',
457 array( 'si_site' => $this->getId() ),
458 __METHOD__
459 );
460 }
461
462 if ( $success && $this->localIds !== false ) {
463 foreach ( $this->localIds as $type => $ids ) {
464 foreach ( $ids as $id ) {
465 $dbw->insert(
466 'site_identifiers',
467 array(
468 'si_site' => $this->getId(),
469 'si_type' => $type,
470 'si_key' => $id,
471 ),
472 __METHOD__
473 );
474 }
475 }
476 }
477
478 if ( $trx == 0 ) {
479 $dbw->commit( __METHOD__ );
480 }
481
482 return $success;
483 }
484
485 /**
486 * @see Site::setPath
487 *
488 * @since 1.21
489 *
490 * @param string $pathType
491 * @param string $fullUrl
492 */
493 public function setPath( $pathType, $fullUrl ) {
494 $paths = $this->getExtraData( 'paths', array() );
495 $paths[$pathType] = $fullUrl;
496 $this->setExtraData( 'paths', $paths );
497 }
498
499 /**
500 * @see Sitres::getPath
501 *
502 * @since 1.21
503 *
504 * @param string $pathType
505 *
506 * @return string|false
507 */
508 public function getPath( $pathType ) {
509 $paths = $this->getExtraData( 'paths', array() );
510 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : false;
511 }
512
513 /**
514 * @see Sitres::getAll
515 *
516 * @since 1.21
517 *
518 * @return array of string
519 */
520 public function getAllPaths() {
521 return $this->getExtraData( 'paths', array() );
522 }
523
524 /**
525 * @see Sitres::removePath
526 *
527 * @since 1.21
528 *
529 * @param string $pathType
530 */
531 public function removePath( $pathType ) {
532 $paths = $this->getExtraData( 'paths', array() );
533 unset( $paths[$pathType] );
534 $this->setExtraData( 'paths', $paths );
535 }
536
537 }