Add a way for packagers to override some installation details
[lhc/web/wiklou.git] / includes / WikiMap.php
1 <?php
2 /**
3 * Tools for dealing with other locally-hosted wikis.
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
23 /**
24 * Helper tools for dealing with other locally-hosted wikis
25 */
26 class WikiMap {
27
28 /**
29 * Get a WikiReference object for $wikiID
30 *
31 * @param $wikiID String: wiki'd id (generally database name)
32 * @return WikiReference object or null if the wiki was not found
33 */
34 public static function getWiki( $wikiID ) {
35 global $wgConf;
36
37 $wgConf->loadFullData();
38
39 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
40 if( $major === null ) {
41 return null;
42 }
43 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
44 array( 'lang' => $minor, 'site' => $major ) );
45 $server = $wgConf->get( 'wgServer', $wikiID, $major,
46 array( 'lang' => $minor, 'site' => $major ) );
47 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
48 array( 'lang' => $minor, 'site' => $major ) );
49 return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
50 }
51
52 /**
53 * Convenience to get the wiki's display name
54 *
55 * @todo We can give more info than just the wiki id!
56 * @param $wikiID String: wiki'd id (generally database name)
57 * @return string|int Wiki's name or $wiki_id if the wiki was not found
58 */
59 public static function getWikiName( $wikiID ) {
60 $wiki = WikiMap::getWiki( $wikiID );
61
62 if ( $wiki ) {
63 return $wiki->getDisplayName();
64 }
65 return $wikiID;
66 }
67
68 /**
69 * Convenience to get a link to a user page on a foreign wiki
70 *
71 * @param $wikiID String: wiki'd id (generally database name)
72 * @param $user String: user name (must be normalised before calling this function!)
73 * @param $text String: link's text; optional, default to "User:$user"
74 * @return String: HTML link or false if the wiki was not found
75 */
76 public static function foreignUserLink( $wikiID, $user, $text=null ) {
77 return self::makeForeignLink( $wikiID, "User:$user", $text );
78 }
79
80 /**
81 * Convenience to get a link to a page on a foreign wiki
82 *
83 * @param $wikiID String: wiki'd id (generally database name)
84 * @param $page String: page name (must be normalised before calling this function!)
85 * @param $text String: link's text; optional, default to $page
86 * @return String: HTML link or false if the wiki was not found
87 */
88 public static function makeForeignLink( $wikiID, $page, $text=null ) {
89 if ( !$text ) {
90 $text = $page;
91 }
92
93 $url = self::getForeignURL( $wikiID, $page );
94 if ( $url === false ) {
95 return false;
96 }
97
98 return Linker::makeExternalLink( $url, $text );
99 }
100
101 /**
102 * Convenience to get a url to a page on a foreign wiki
103 *
104 * @param $wikiID String: wiki'd id (generally database name)
105 * @param $page String: page name (must be normalised before calling this function!)
106 * @return String: URL or false if the wiki was not found
107 */
108 public static function getForeignURL( $wikiID, $page ) {
109 $wiki = WikiMap::getWiki( $wikiID );
110
111 if ( $wiki ) {
112 return $wiki->getUrl( $page );
113 }
114
115 return false;
116 }
117 }
118
119 /**
120 * Reference to a locally-hosted wiki
121 */
122 class WikiReference {
123 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
124 private $mMajor; ///< 'wiki', 'wiktionary', etc
125 private $mCanonicalServer; ///< canonical server URL, e.g. 'http://www.mediawiki.org'
126 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
127 private $mPath; ///< path, '/wiki/$1'
128
129 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
130 $this->mMajor = $major;
131 $this->mMinor = $minor;
132 $this->mCanonicalServer = $canonicalServer;
133 $this->mPath = $path;
134 $this->mServer = $server === null ? $canonicalServer : $server;
135 }
136
137 /**
138 * @return string
139 * @throws MWException
140 */
141 public function getHostname() {
142 $prefixes = array( 'http://', 'https://' );
143 foreach ( $prefixes as $prefix ) {
144 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
145 return substr( $this->mCanonicalServer, strlen( $prefix ) );
146 }
147 }
148 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
149 }
150
151 /**
152 * Get the the URL in a way to de displayed to the user
153 * More or less Wikimedia specific
154 *
155 * @return String
156 */
157 public function getDisplayName() {
158 $url = $this->getUrl( '' );
159 $parsed = wfParseUrl( $url );
160 if ( $parsed ) {
161 return $parsed['host'];
162 } else {
163 // Invalid URL. There's no sane thing to do here, so just return it
164 return $url;
165 }
166 }
167
168 /**
169 * Helper function for getUrl()
170 *
171 * @todo FIXME: This may be generalized...
172 * @param $page String: page name (must be normalised before calling this function!)
173 * @return String: Url fragment
174 */
175 private function getLocalUrl( $page ) {
176 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
177 }
178
179 /**
180 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
181 *
182 * @param $page String: page name (must be normalised before calling this function!)
183 * @return String: Url
184 */
185 public function getCanonicalUrl( $page ) {
186 return $this->mCanonicalServer . $this->getLocalUrl( $page );
187 }
188
189 /**
190 * Alias for getCanonicalUrl(), for backwards compatibility.
191 * @return String
192 */
193 public function getUrl( $page ) {
194 return $this->getCanonicalUrl( $page );
195 }
196
197 /**
198 * Get a URL based on $wgServer, like Title::getFullUrl() would produce
199 * when called locally on the wiki.
200 *
201 * @param $page String: page name (must be normalized before calling this function!)
202 * @return String: URL
203 */
204 public function getFullUrl( $page ) {
205 return
206 $this->mServer .
207 $this->getLocalUrl( $page );
208 }
209 }