Merge "Made SSL validation in Curl HTTP requests the default."
[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 * @license 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 '';
161 }
162
163 $protocol = parse_url( $path, PHP_URL_SCHEME );
164
165 // Malformed URL
166 if ( $protocol === false ) {
167 throw new MWException( "failed to parse URL $path" );
168 }
169
170 // No schema
171 if ( $protocol === null ) {
172 // Used for protocol relative URLs
173 $protocol = '';
174 }
175
176 return $protocol;
177 }
178
179 /**
180 * Sets the path used to construct links with.
181 * @see Site::setLinkPath
182 *
183 * @param string $fullUrl
184 *
185 * @since 1.21
186 *
187 * @throws MWException
188 */
189 public function setLinkPath( $fullUrl ) {
190 $type = $this->getLinkPathType();
191
192 if ( $type === false ) {
193 throw new MWException( "This SiteObject does not support link paths." );
194 }
195
196 $this->setPath( $type, $fullUrl );
197 }
198
199 /**
200 * Returns the path path used to construct links with or false if there is no such path.
201 *
202 * @see Site::getLinkPath
203 *
204 * @return string|false
205 */
206 public function getLinkPath() {
207 $type = $this->getLinkPathType();
208 return $type === false ? false : $this->getPath( $type );
209 }
210
211 /**
212 * @see Site::getLinkPathType
213 *
214 * Returns the main path type, that is the type of the path that should generally be used to construct links
215 * to the target site.
216 *
217 * This default implementation returns SiteObject::PATH_LINK as the default path type. Subclasses can override this
218 * to define a different default path type, or return false to disable site links.
219 *
220 * @since 1.21
221 *
222 * @return string|false
223 */
224 public function getLinkPathType() {
225 return self::PATH_LINK;
226 }
227
228 /**
229 * @see Site::getPageUrl
230 *
231 * This implementation returns a URL constructed using the path returned by getLinkPath().
232 *
233 * @since 1.21
234 *
235 * @param bool|String $pageName
236 *
237 * @return string|false
238 */
239 public function getPageUrl( $pageName = false ) {
240 $url = $this->getLinkPath();
241
242 if ( $url === false ) {
243 return false;
244 }
245
246 if ( $pageName !== false ) {
247 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
248 }
249
250 return $url;
251 }
252
253 /**
254 * Returns $pageName without changes.
255 * Subclasses may override this to apply some kind of normalization.
256 *
257 * @see Site::normalizePageName
258 *
259 * @since 1.21
260 *
261 * @param string $pageName
262 *
263 * @return string
264 */
265 public function normalizePageName( $pageName ) {
266 return $pageName;
267 }
268
269 /**
270 * Returns the value of a type specific field, or the value
271 * of the $default parameter in case it's not set.
272 *
273 * @since 1.21
274 *
275 * @param string $fieldName
276 * @param mixed $default
277 *
278 * @return array
279 */
280 protected function getExtraData( $fieldName, $default = null ) {
281 $data = $this->getField( 'data', array() );
282 return array_key_exists( $fieldName,$data ) ? $data[$fieldName] : $default;
283 }
284
285 /**
286 * Sets the value of a type specific field.
287 * @since 1.21
288 *
289 * @param string $fieldName
290 * @param mixed $value
291 */
292 protected function setExtraData( $fieldName, $value = null ) {
293 $data = $this->getField( 'data', array() );
294 $data[$fieldName] = $value;
295 $this->setField( 'data', $data );
296 }
297
298 /**
299 * @see Site::getLanguageCode
300 *
301 * @since 1.21
302 *
303 * @return string|false
304 */
305 public function getLanguageCode() {
306 return $this->getField( 'language', false );
307 }
308
309 /**
310 * @see Site::setLanguageCode
311 *
312 * @since 1.21
313 *
314 * @param string $languageCode
315 */
316 public function setLanguageCode( $languageCode ) {
317 $this->setField( 'language', $languageCode );
318 }
319
320 /**
321 * Returns the local identifiers of this site.
322 *
323 * @since 1.21
324 *
325 * @param string $type
326 *
327 * @return array
328 */
329 protected function getLocalIds( $type ) {
330 if ( $this->localIds === false ) {
331 $this->loadLocalIds();
332 }
333
334 return array_key_exists( $type, $this->localIds ) ? $this->localIds[$type] : array();
335 }
336
337 /**
338 * Loads the local ids for the site.
339 *
340 * @since 1.21
341 */
342 protected function loadLocalIds() {
343 $dbr = wfGetDB( $this->getTable()->getReadDb() );
344
345 $ids = $dbr->select(
346 'site_identifiers',
347 array(
348 'si_type',
349 'si_key',
350 ),
351 array(
352 'si_site' => $this->getId(),
353 ),
354 __METHOD__
355 );
356
357 $this->localIds = array();
358
359 foreach ( $ids as $id ) {
360 $this->addLocalId( $id->si_type, $id->si_key );
361 }
362 }
363
364 /**
365 * Adds a local identifier.
366 *
367 * @since 1.21
368 *
369 * @param string $type
370 * @param string $identifier
371 */
372 public function addLocalId( $type, $identifier ) {
373 if ( $this->localIds === false ) {
374 $this->localIds = array();
375 }
376
377 if ( !array_key_exists( $type, $this->localIds ) ) {
378 $this->localIds[$type] = array();
379 }
380
381 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
382 $this->localIds[$type][] = $identifier;
383 }
384 }
385
386 /**
387 * @see Site::addInterwikiId
388 *
389 * @since 1.21
390 *
391 * @param string $identifier
392 */
393 public function addInterwikiId( $identifier ) {
394 $this->addLocalId( 'interwiki', $identifier );
395 }
396
397 /**
398 * @see Site::addNavigationId
399 *
400 * @since 1.21
401 *
402 * @param string $identifier
403 */
404 public function addNavigationId( $identifier ) {
405 $this->addLocalId( 'equivalent', $identifier );
406 }
407
408 /**
409 * @see Site::getInterwikiIds
410 *
411 * @since 1.21
412 *
413 * @return array of string
414 */
415 public function getInterwikiIds() {
416 return $this->getLocalIds( 'interwiki' );
417 }
418
419 /**
420 * @see Site::getNavigationIds
421 *
422 * @since 1.21
423 *
424 * @return array of string
425 */
426 public function getNavigationIds() {
427 return $this->getLocalIds( 'equivalent' );
428 }
429
430 /**
431 * @see Site::getInternalId
432 *
433 * @since 1.21
434 *
435 * @return integer
436 */
437 public function getInternalId() {
438 return $this->getId();
439 }
440
441 /**
442 * @see ORMRow::save
443 * @see Site::save
444 *
445 * @since 1.21
446 *
447 * @param string|null $functionName
448 *
449 * @return boolean Success indicator
450 */
451 public function save( $functionName = null ) {
452 $dbw = wfGetDB( DB_MASTER );
453
454 $trx = $dbw->trxLevel();
455
456 if ( $trx == 0 ) {
457 $dbw->begin( __METHOD__ );
458 }
459
460 $this->setField( 'protocol', $this->getProtocol() );
461 $this->setField( 'domain', strrev( $this->getDomain() ) . '.' );
462
463 $existedAlready = $this->hasIdField();
464
465 $success = parent::save( $functionName );
466
467 if ( $success && $existedAlready ) {
468 $dbw->delete(
469 'site_identifiers',
470 array( 'si_site' => $this->getId() ),
471 __METHOD__
472 );
473 }
474
475 if ( $success && $this->localIds !== false ) {
476 foreach ( $this->localIds as $type => $ids ) {
477 foreach ( $ids as $id ) {
478 $dbw->insert(
479 'site_identifiers',
480 array(
481 'si_site' => $this->getId(),
482 'si_type' => $type,
483 'si_key' => $id,
484 ),
485 __METHOD__
486 );
487 }
488 }
489 }
490
491 if ( $trx == 0 ) {
492 $dbw->commit( __METHOD__ );
493 }
494
495 return $success;
496 }
497
498 /**
499 * @see Site::setPath
500 *
501 * @since 1.21
502 *
503 * @param string $pathType
504 * @param string $fullUrl
505 */
506 public function setPath( $pathType, $fullUrl ) {
507 $paths = $this->getExtraData( 'paths', array() );
508 $paths[$pathType] = $fullUrl;
509 $this->setExtraData( 'paths', $paths );
510 }
511
512 /**
513 * @see Sitres::getPath
514 *
515 * @since 1.21
516 *
517 * @param string $pathType
518 *
519 * @return string|false
520 */
521 public function getPath( $pathType ) {
522 $paths = $this->getExtraData( 'paths', array() );
523 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : false;
524 }
525
526 /**
527 * @see Sitres::getAll
528 *
529 * @since 1.21
530 *
531 * @return array of string
532 */
533 public function getAllPaths() {
534 return $this->getExtraData( 'paths', array() );
535 }
536
537 /**
538 * @see Sitres::removePath
539 *
540 * @since 1.21
541 *
542 * @param string $pathType
543 */
544 public function removePath( $pathType ) {
545 $paths = $this->getExtraData( 'paths', array() );
546 unset( $paths[$pathType] );
547 $this->setExtraData( 'paths', $paths );
548 }
549
550 }