Merge "Remove Revision::getRevisionText from migrateArchiveText"
[lhc/web/wiklou.git] / includes / site / Site.php
1 <?php
2
3 /**
4 * Represents 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 GPL-2.0-or-later
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 */
29 class Site implements Serializable {
30 const TYPE_UNKNOWN = 'unknown';
31 const TYPE_MEDIAWIKI = 'mediawiki';
32
33 const GROUP_NONE = 'none';
34
35 const ID_INTERWIKI = 'interwiki';
36 const ID_EQUIVALENT = 'equivalent';
37
38 const SOURCE_LOCAL = 'local';
39
40 const PATH_LINK = 'link';
41
42 /**
43 * A version ID that identifies the serialization structure used by getSerializationData()
44 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
45 * on serialization for storing the SiteList.
46 *
47 * @var string A string uniquely identifying the version of the serialization structure.
48 */
49 const SERIAL_VERSION_ID = '2013-01-23';
50
51 /**
52 * @since 1.21
53 *
54 * @var string|null
55 */
56 protected $globalId = null;
57
58 /**
59 * @since 1.21
60 *
61 * @var string
62 */
63 protected $type = self::TYPE_UNKNOWN;
64
65 /**
66 * @since 1.21
67 *
68 * @var string
69 */
70 protected $group = self::GROUP_NONE;
71
72 /**
73 * @since 1.21
74 *
75 * @var string
76 */
77 protected $source = self::SOURCE_LOCAL;
78
79 /**
80 * @since 1.21
81 *
82 * @var string|null
83 */
84 protected $languageCode = null;
85
86 /**
87 * Holds the local ids for this site.
88 * local id type => [ ids for this type (strings) ]
89 *
90 * @since 1.21
91 *
92 * @var array[]|false
93 */
94 protected $localIds = [];
95
96 /**
97 * @since 1.21
98 *
99 * @var array
100 */
101 protected $extraData = [];
102
103 /**
104 * @since 1.21
105 *
106 * @var array
107 */
108 protected $extraConfig = [];
109
110 /**
111 * @since 1.21
112 *
113 * @var bool
114 */
115 protected $forward = false;
116
117 /**
118 * @since 1.21
119 *
120 * @var int|null
121 */
122 protected $internalId = null;
123
124 /**
125 * @since 1.21
126 *
127 * @param string $type
128 */
129 public function __construct( $type = self::TYPE_UNKNOWN ) {
130 $this->type = $type;
131 }
132
133 /**
134 * Returns the global site identifier (ie enwiktionary).
135 *
136 * @since 1.21
137 *
138 * @return string|null
139 */
140 public function getGlobalId() {
141 return $this->globalId;
142 }
143
144 /**
145 * Sets the global site identifier (ie enwiktionary).
146 *
147 * @since 1.21
148 *
149 * @param string|null $globalId
150 *
151 * @throws MWException
152 */
153 public function setGlobalId( $globalId ) {
154 if ( $globalId !== null && !is_string( $globalId ) ) {
155 throw new MWException( '$globalId needs to be string or null' );
156 }
157
158 $this->globalId = $globalId;
159 }
160
161 /**
162 * Returns the type of the site (ie mediawiki).
163 *
164 * @since 1.21
165 *
166 * @return string
167 */
168 public function getType() {
169 return $this->type;
170 }
171
172 /**
173 * Gets the group of the site (ie wikipedia).
174 *
175 * @since 1.21
176 *
177 * @return string
178 */
179 public function getGroup() {
180 return $this->group;
181 }
182
183 /**
184 * Sets the group of the site (ie wikipedia).
185 *
186 * @since 1.21
187 *
188 * @param string $group
189 *
190 * @throws MWException
191 */
192 public function setGroup( $group ) {
193 if ( !is_string( $group ) ) {
194 throw new MWException( '$group needs to be a string' );
195 }
196
197 $this->group = $group;
198 }
199
200 /**
201 * Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
202 *
203 * @since 1.21
204 *
205 * @return string
206 */
207 public function getSource() {
208 return $this->source;
209 }
210
211 /**
212 * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
213 *
214 * @since 1.21
215 *
216 * @param string $source
217 *
218 * @throws MWException
219 */
220 public function setSource( $source ) {
221 if ( !is_string( $source ) ) {
222 throw new MWException( '$source needs to be a string' );
223 }
224
225 $this->source = $source;
226 }
227
228 /**
229 * Gets if site.tld/path/key:pageTitle should forward users to the page on
230 * the actual site, where "key" is the local identifier.
231 *
232 * @since 1.21
233 *
234 * @return bool
235 */
236 public function shouldForward() {
237 return $this->forward;
238 }
239
240 /**
241 * Sets if site.tld/path/key:pageTitle should forward users to the page on
242 * the actual site, where "key" is the local identifier.
243 *
244 * @since 1.21
245 *
246 * @param bool $shouldForward
247 *
248 * @throws MWException
249 */
250 public function setForward( $shouldForward ) {
251 if ( !is_bool( $shouldForward ) ) {
252 throw new MWException( '$shouldForward needs to be a boolean' );
253 }
254
255 $this->forward = $shouldForward;
256 }
257
258 /**
259 * Returns the domain of the site, ie en.wikipedia.org
260 * Or false if it's not known.
261 *
262 * @since 1.21
263 *
264 * @return string|null
265 */
266 public function getDomain() {
267 $path = $this->getLinkPath();
268
269 if ( $path === null ) {
270 return null;
271 }
272
273 return parse_url( $path, PHP_URL_HOST );
274 }
275
276 /**
277 * Returns the protocol of the site.
278 *
279 * @since 1.21
280 *
281 * @throws MWException
282 * @return string
283 */
284 public function getProtocol() {
285 $path = $this->getLinkPath();
286
287 if ( $path === null ) {
288 return '';
289 }
290
291 $protocol = parse_url( $path, PHP_URL_SCHEME );
292
293 // Malformed URL
294 if ( $protocol === false ) {
295 throw new MWException( "failed to parse URL '$path'" );
296 }
297
298 // No schema
299 if ( $protocol === null ) {
300 // Used for protocol relative URLs
301 $protocol = '';
302 }
303
304 return $protocol;
305 }
306
307 /**
308 * Sets the path used to construct links with.
309 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
310 *
311 * @param string $fullUrl
312 *
313 * @since 1.21
314 *
315 * @throws MWException
316 */
317 public function setLinkPath( $fullUrl ) {
318 $type = $this->getLinkPathType();
319
320 if ( $type === null ) {
321 throw new MWException( "This Site does not support link paths." );
322 }
323
324 $this->setPath( $type, $fullUrl );
325 }
326
327 /**
328 * Returns the path used to construct links with or false if there is no such path.
329 *
330 * Shall be equivalent to getPath( getLinkPathType() ).
331 *
332 * @return string|null
333 */
334 public function getLinkPath() {
335 $type = $this->getLinkPathType();
336 return $type === null ? null : $this->getPath( $type );
337 }
338
339 /**
340 * Returns the main path type, that is the type of the path that should
341 * generally be used to construct links to the target site.
342 *
343 * This default implementation returns Site::PATH_LINK as the default path
344 * type. Subclasses can override this to define a different default path
345 * type, or return false to disable site links.
346 *
347 * @since 1.21
348 *
349 * @return string|null
350 */
351 public function getLinkPathType() {
352 return self::PATH_LINK;
353 }
354
355 /**
356 * Returns the full URL for the given page on the site.
357 * Or null if the needed information is not known.
358 *
359 * This generated URL is usually based upon the path returned by getLinkPath(),
360 * but this is not a requirement.
361 *
362 * This implementation returns a URL constructed using the path returned by getLinkPath().
363 *
364 * @since 1.21
365 *
366 * @param bool|string $pageName
367 *
368 * @return string|null
369 */
370 public function getPageUrl( $pageName = false ) {
371 $url = $this->getLinkPath();
372
373 if ( $url === null ) {
374 return null;
375 }
376
377 if ( $pageName !== false ) {
378 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
379 }
380
381 return $url;
382 }
383
384 /**
385 * Attempt to normalize the page name in some fashion.
386 * May return false to indicate various kinds of failure.
387 *
388 * This implementation returns $pageName without changes.
389 *
390 * @see Site::normalizePageName
391 *
392 * @since 1.21
393 *
394 * @param string $pageName
395 *
396 * @return string|false
397 */
398 public function normalizePageName( $pageName ) {
399 return $pageName;
400 }
401
402 /**
403 * Returns the type specific fields.
404 *
405 * @since 1.21
406 *
407 * @return array
408 */
409 public function getExtraData() {
410 return $this->extraData;
411 }
412
413 /**
414 * Sets the type specific fields.
415 *
416 * @since 1.21
417 *
418 * @param array $extraData
419 */
420 public function setExtraData( array $extraData ) {
421 $this->extraData = $extraData;
422 }
423
424 /**
425 * Returns the type specific config.
426 *
427 * @since 1.21
428 *
429 * @return array
430 */
431 public function getExtraConfig() {
432 return $this->extraConfig;
433 }
434
435 /**
436 * Sets the type specific config.
437 *
438 * @since 1.21
439 *
440 * @param array $extraConfig
441 */
442 public function setExtraConfig( array $extraConfig ) {
443 $this->extraConfig = $extraConfig;
444 }
445
446 /**
447 * Returns language code of the sites primary language.
448 * Or null if it's not known.
449 *
450 * @since 1.21
451 *
452 * @return string|null
453 */
454 public function getLanguageCode() {
455 return $this->languageCode;
456 }
457
458 /**
459 * Sets language code of the sites primary language.
460 *
461 * @since 1.21
462 *
463 * @param string|null $languageCode
464 */
465 public function setLanguageCode( $languageCode ) {
466 if ( $languageCode !== null && !Language::isValidCode( $languageCode ) ) {
467 throw new InvalidArgumentException( "$languageCode is not a valid language code." );
468 }
469 $this->languageCode = $languageCode;
470 }
471
472 /**
473 * Returns the set internal identifier for the site.
474 *
475 * @since 1.21
476 *
477 * @return string|null
478 */
479 public function getInternalId() {
480 return $this->internalId;
481 }
482
483 /**
484 * Sets the internal identifier for the site.
485 * This typically is a primary key in a db table.
486 *
487 * @since 1.21
488 *
489 * @param int|null $internalId
490 */
491 public function setInternalId( $internalId = null ) {
492 $this->internalId = $internalId;
493 }
494
495 /**
496 * Adds a local identifier.
497 *
498 * @since 1.21
499 *
500 * @param string $type
501 * @param string $identifier
502 */
503 public function addLocalId( $type, $identifier ) {
504 if ( $this->localIds === false ) {
505 $this->localIds = [];
506 }
507
508 if ( !array_key_exists( $type, $this->localIds ) ) {
509 $this->localIds[$type] = [];
510 }
511
512 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
513 $this->localIds[$type][] = $identifier;
514 }
515 }
516
517 /**
518 * Adds an interwiki id to the site.
519 *
520 * @since 1.21
521 *
522 * @param string $identifier
523 */
524 public function addInterwikiId( $identifier ) {
525 $this->addLocalId( self::ID_INTERWIKI, $identifier );
526 }
527
528 /**
529 * Adds a navigation id to the site.
530 *
531 * @since 1.21
532 *
533 * @param string $identifier
534 */
535 public function addNavigationId( $identifier ) {
536 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
537 }
538
539 /**
540 * Returns the interwiki link identifiers that can be used for this site.
541 *
542 * @since 1.21
543 *
544 * @return string[]
545 */
546 public function getInterwikiIds() {
547 return array_key_exists( self::ID_INTERWIKI, $this->localIds )
548 ? $this->localIds[self::ID_INTERWIKI]
549 : [];
550 }
551
552 /**
553 * Returns the equivalent link identifiers that can be used to make
554 * the site show up in interfaces such as the "language links" section.
555 *
556 * @since 1.21
557 *
558 * @return string[]
559 */
560 public function getNavigationIds() {
561 return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
562 ? $this->localIds[self::ID_EQUIVALENT] :
563 [];
564 }
565
566 /**
567 * Returns all local ids
568 *
569 * @since 1.21
570 *
571 * @return array[]
572 */
573 public function getLocalIds() {
574 return $this->localIds;
575 }
576
577 /**
578 * Sets the path used to construct links with.
579 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
580 *
581 * @since 1.21
582 *
583 * @param string $pathType
584 * @param string $fullUrl
585 *
586 * @throws MWException
587 */
588 public function setPath( $pathType, $fullUrl ) {
589 if ( !is_string( $fullUrl ) ) {
590 throw new MWException( '$fullUrl needs to be a string' );
591 }
592
593 if ( !array_key_exists( 'paths', $this->extraData ) ) {
594 $this->extraData['paths'] = [];
595 }
596
597 $this->extraData['paths'][$pathType] = $fullUrl;
598 }
599
600 /**
601 * Returns the path of the provided type or false if there is no such path.
602 *
603 * @since 1.21
604 *
605 * @param string $pathType
606 *
607 * @return string|null
608 */
609 public function getPath( $pathType ) {
610 $paths = $this->getAllPaths();
611 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
612 }
613
614 /**
615 * Returns the paths as associative array.
616 * The keys are path types, the values are the path urls.
617 *
618 * @since 1.21
619 *
620 * @return string[]
621 */
622 public function getAllPaths() {
623 return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : [];
624 }
625
626 /**
627 * Removes the path of the provided type if it's set.
628 *
629 * @since 1.21
630 *
631 * @param string $pathType
632 */
633 public function removePath( $pathType ) {
634 if ( array_key_exists( 'paths', $this->extraData ) ) {
635 unset( $this->extraData['paths'][$pathType] );
636 }
637 }
638
639 /**
640 * @since 1.21
641 *
642 * @param string $siteType
643 *
644 * @return Site
645 */
646 public static function newForType( $siteType ) {
647 global $wgSiteTypes;
648
649 if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
650 return new $wgSiteTypes[$siteType]();
651 }
652
653 return new Site();
654 }
655
656 /**
657 * @see Serializable::serialize
658 *
659 * @since 1.21
660 *
661 * @return string
662 */
663 public function serialize() {
664 $fields = [
665 'globalid' => $this->globalId,
666 'type' => $this->type,
667 'group' => $this->group,
668 'source' => $this->source,
669 'language' => $this->languageCode,
670 'localids' => $this->localIds,
671 'config' => $this->extraConfig,
672 'data' => $this->extraData,
673 'forward' => $this->forward,
674 'internalid' => $this->internalId,
675
676 ];
677
678 return serialize( $fields );
679 }
680
681 /**
682 * @see Serializable::unserialize
683 *
684 * @since 1.21
685 *
686 * @param string $serialized
687 */
688 public function unserialize( $serialized ) {
689 $fields = unserialize( $serialized );
690
691 $this->__construct( $fields['type'] );
692
693 $this->setGlobalId( $fields['globalid'] );
694 $this->setGroup( $fields['group'] );
695 $this->setSource( $fields['source'] );
696 $this->setLanguageCode( $fields['language'] );
697 $this->localIds = $fields['localids'];
698 $this->setExtraConfig( $fields['config'] );
699 $this->setExtraData( $fields['data'] );
700 $this->setForward( $fields['forward'] );
701 $this->setInternalId( $fields['internalid'] );
702 }
703 }