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