3 * Tools for dealing with other locally-hosted wikis.
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.
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.
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
24 * Helper tools for dealing with other locally-hosted wikis
29 * Get a WikiReference object for $wikiID
31 * @param string $wikiID Wiki'd id (generally database name)
32 * @return WikiReference|null WikiReference object or null if the wiki was not found
34 public static function getWiki( $wikiID ) {
37 $wgConf->loadFullData();
39 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
40 if ( $major === null ) {
43 $server = $wgConf->get( 'wgServer', $wikiID, $major,
44 array( 'lang' => $minor, 'site' => $major ) );
46 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
47 array( 'lang' => $minor, 'site' => $major ) );
48 if ( $canonicalServer === false ||
$canonicalServer === null ) {
49 $canonicalServer = $server;
52 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
53 array( 'lang' => $minor, 'site' => $major ) );
54 return new WikiReference( $canonicalServer, $path, $server );
58 * Convenience to get the wiki's display name
60 * @todo We can give more info than just the wiki id!
61 * @param string $wikiID Wiki'd id (generally database name)
62 * @return string|int Wiki's name or $wiki_id if the wiki was not found
64 public static function getWikiName( $wikiID ) {
65 $wiki = WikiMap
::getWiki( $wikiID );
68 return $wiki->getDisplayName();
74 * Convenience to get a link to a user page on a foreign wiki
76 * @param string $wikiID Wiki'd id (generally database name)
77 * @param string $user User name (must be normalised before calling this function!)
78 * @param string $text Link's text; optional, default to "User:$user"
79 * @return string HTML link or false if the wiki was not found
81 public static function foreignUserLink( $wikiID, $user, $text = null ) {
82 return self
::makeForeignLink( $wikiID, "User:$user", $text );
86 * Convenience to get a link to a page on a foreign wiki
88 * @param string $wikiID Wiki'd id (generally database name)
89 * @param string $page Page name (must be normalised before calling this function!)
90 * @param string $text Link's text; optional, default to $page
91 * @return string HTML link or false if the wiki was not found
93 public static function makeForeignLink( $wikiID, $page, $text = null ) {
98 $url = self
::getForeignURL( $wikiID, $page );
99 if ( $url === false ) {
103 return Linker
::makeExternalLink( $url, $text );
107 * Convenience to get a url to a page on a foreign wiki
109 * @param string $wikiID Wiki'd id (generally database name)
110 * @param string $page Page name (must be normalised before calling this function!)
111 * @param string|null $fragmentId
113 * @return string|bool URL or false if the wiki was not found
115 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
116 $wiki = WikiMap
::getWiki( $wikiID );
119 return $wiki->getFullUrl( $page, $fragmentId );
127 * Reference to a locally-hosted wiki
129 class WikiReference
{
130 private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
131 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
132 private $mPath; ///< path, '/wiki/$1'
135 * @param string $canonicalServer
136 * @param string $path
137 * @param null|string $server
139 public function __construct( $canonicalServer, $path, $server = null ) {
140 $this->mCanonicalServer
= $canonicalServer;
141 $this->mPath
= $path;
142 $this->mServer
= $server === null ?
$canonicalServer : $server;
146 * Get the URL in a way to be displayed to the user
147 * More or less Wikimedia specific
151 public function getDisplayName() {
152 $parsed = wfParseUrl( $this->mCanonicalServer
);
154 return $parsed['host'];
156 // Invalid server spec.
157 // There's no sane thing to do here, so just return the canonical server name in full.
158 return $this->mCanonicalServer
;
163 * Helper function for getUrl()
165 * @todo FIXME: This may be generalized...
167 * @param string $page Page name (must be normalised before calling this function!
168 * May contain a section part.)
169 * @param string|null $fragmentId
171 * @return string relative URL, without the server part.
173 private function getLocalUrl( $page, $fragmentId = null ) {
174 $page = wfUrlEncode( str_replace( ' ', '_', $page ) );
176 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
177 $page .= '#' . wfUrlEncode( $fragmentId );
180 return str_replace( '$1', $page, $this->mPath
);
184 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
186 * @param string $page Page name (must be normalised before calling this function!)
187 * @param string|null $fragmentId
191 public function getCanonicalUrl( $page, $fragmentId = null ) {
192 return $this->mCanonicalServer
. $this->getLocalUrl( $page, $fragmentId );
196 * Get a canonical server URL
199 public function getCanonicalServer() {
200 return $this->mCanonicalServer
;
204 * Alias for getCanonicalUrl(), for backwards compatibility.
205 * @param string $page
206 * @param string|null $fragmentId
210 public function getUrl( $page, $fragmentId = null ) {
211 return $this->getCanonicalUrl( $page, $fragmentId );
215 * Get a URL based on $wgServer, like Title::getFullURL() would produce
216 * when called locally on the wiki.
218 * @param string $page Page name (must be normalized before calling this function!)
219 * @param string|null $fragmentId
223 public function getFullUrl( $page, $fragmentId = null ) {
224 return $this->mServer
.
225 $this->getLocalUrl( $page, $fragmentId );