Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / includes / interwiki / Interwiki.php
1 <?php
2 /**
3 * Interwiki table entry.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22 use MediaWiki\MediaWikiServices;
23
24 /**
25 * Value object for representing interwiki records.
26 */
27 class Interwiki {
28
29 /** @var string The interwiki prefix, (e.g. "Meatball", or the language prefix "de") */
30 protected $mPrefix;
31
32 /** @var string The URL of the wiki, with "$1" as a placeholder for an article name. */
33 protected $mURL;
34
35 /** @var string The URL of the file api.php */
36 protected $mAPI;
37
38 /** @var string The name of the database (for a connection to be established
39 * with wfGetLB( 'wikiid' ))
40 */
41 protected $mWikiID;
42
43 /** @var bool Whether the wiki is in this project */
44 protected $mLocal;
45
46 /** @var bool Whether interwiki transclusions are allowed */
47 protected $mTrans;
48
49 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0,
50 $trans = 0
51 ) {
52 $this->mPrefix = $prefix;
53 $this->mURL = $url;
54 $this->mAPI = $api;
55 $this->mWikiID = $wikiId;
56 $this->mLocal = (bool)$local;
57 $this->mTrans = (bool)$trans;
58 }
59
60 /**
61 * Check whether an interwiki prefix exists
62 *
63 * @deprecated since 1.28, use InterwikiLookup instead
64 *
65 * @param string $prefix Interwiki prefix to use
66 * @return bool Whether it exists
67 */
68 public static function isValidInterwiki( $prefix ) {
69 return MediaWikiServices::getInstance()->getInterwikiLookup()->isValidInterwiki( $prefix );
70 }
71
72 /**
73 * Fetch an Interwiki object
74 *
75 * @deprecated since 1.28, use InterwikiLookup instead
76 *
77 * @param string $prefix Interwiki prefix to use
78 * @return Interwiki|null|bool
79 */
80 public static function fetch( $prefix ) {
81 return MediaWikiServices::getInstance()->getInterwikiLookup()->fetch( $prefix );
82 }
83
84 /**
85 * Purge the cache (local and persistent) for an interwiki prefix.
86 *
87 * @param string $prefix
88 * @since 1.26
89 */
90 public static function invalidateCache( $prefix ) {
91 MediaWikiServices::getInstance()->getInterwikiLookup()->invalidateCache( $prefix );
92 }
93
94 /**
95 * Returns all interwiki prefix definitions.
96 *
97 * @deprecated since 1.28, unused. Use InterwikiLookup instead.
98 *
99 * @param string|null $local If set, limits output to local/non-local interwikis
100 * @return array[] List of interwiki rows
101 * @since 1.19
102 */
103 public static function getAllPrefixes( $local = null ) {
104 return MediaWikiServices::getInstance()->getInterwikiLookup()->getAllPrefixes( $local );
105 }
106
107 /**
108 * Get the URL for a particular title (or with $1 if no title given)
109 *
110 * @param string $title What text to put for the article name
111 * @return string The URL
112 * @note Prior to 1.19 The getURL with an argument was broken.
113 * If you if you use this arg in an extension that supports MW earlier
114 * than 1.19 please wfUrlencode and substitute $1 on your own.
115 */
116 public function getURL( $title = null ) {
117 $url = $this->mURL;
118 if ( $title !== null ) {
119 $url = str_replace( "$1", wfUrlencode( $title ), $url );
120 }
121
122 return $url;
123 }
124
125 /**
126 * Get the API URL for this wiki
127 *
128 * @return string The URL
129 */
130 public function getAPI() {
131 return $this->mAPI;
132 }
133
134 /**
135 * Get the DB name for this wiki
136 *
137 * @return string The DB name
138 */
139 public function getWikiID() {
140 return $this->mWikiID;
141 }
142
143 /**
144 * Is this a local link from a sister project, or is
145 * it something outside, like Google
146 *
147 * @return bool
148 */
149 public function isLocal() {
150 return $this->mLocal;
151 }
152
153 /**
154 * Can pages from this wiki be transcluded?
155 * Still requires $wgEnableScaryTransclusion
156 *
157 * @return bool
158 */
159 public function isTranscludable() {
160 return $this->mTrans;
161 }
162
163 /**
164 * Get the name for the interwiki site
165 *
166 * @return string
167 */
168 public function getName() {
169 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
170
171 return !$msg->exists() ? '' : $msg->text();
172 }
173
174 /**
175 * Get a description for this interwiki
176 *
177 * @return string
178 */
179 public function getDescription() {
180 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
181
182 return !$msg->exists() ? '' : $msg->text();
183 }
184
185 }