Base class for objects accessign databases.
[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->getFullUrl( $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 /**
130 * @param $major string
131 * @param $minor string
132 * @param $canonicalServer string
133 * @param $path string
134 * @param $server null|string
135 */
136 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
137 $this->mMajor = $major;
138 $this->mMinor = $minor;
139 $this->mCanonicalServer = $canonicalServer;
140 $this->mPath = $path;
141 $this->mServer = $server === null ? $canonicalServer : $server;
142 }
143
144 /**
145 * @return string
146 * @throws MWException
147 */
148 public function getHostname() {
149 $prefixes = array( 'http://', 'https://' );
150 foreach ( $prefixes as $prefix ) {
151 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
152 return substr( $this->mCanonicalServer, strlen( $prefix ) );
153 }
154 }
155 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
156 }
157
158 /**
159 * Get the the URL in a way to de displayed to the user
160 * More or less Wikimedia specific
161 *
162 * @return String
163 */
164 public function getDisplayName() {
165 $url = $this->getUrl( '' );
166 $parsed = wfParseUrl( $url );
167 if ( $parsed ) {
168 return $parsed['host'];
169 } else {
170 // Invalid URL. There's no sane thing to do here, so just return it
171 return $url;
172 }
173 }
174
175 /**
176 * Helper function for getUrl()
177 *
178 * @todo FIXME: This may be generalized...
179 * @param $page String: page name (must be normalised before calling this function!)
180 * @return String: Url fragment
181 */
182 private function getLocalUrl( $page ) {
183 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
184 }
185
186 /**
187 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
188 *
189 * @param $page String: page name (must be normalised before calling this function!)
190 * @return String: Url
191 */
192 public function getCanonicalUrl( $page ) {
193 return $this->mCanonicalServer . $this->getLocalUrl( $page );
194 }
195
196 /**
197 * Get a canonical server URL
198 * @return string
199 */
200 public function getCanonicalServer() {
201 return $this->mCanonicalServer;
202 }
203
204 /**
205 * Alias for getCanonicalUrl(), for backwards compatibility.
206 * @param $page string
207 * @return String
208 */
209 public function getUrl( $page ) {
210 return $this->getCanonicalUrl( $page );
211 }
212
213 /**
214 * Get a URL based on $wgServer, like Title::getFullUrl() would produce
215 * when called locally on the wiki.
216 *
217 * @param $page String: page name (must be normalized before calling this function!)
218 * @return String: URL
219 */
220 public function getFullUrl( $page ) {
221 return
222 $this->mServer .
223 $this->getLocalUrl( $page );
224 }
225 }