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