Merge "resourceloader: Soft-deprecate use of global LESS variables"
[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 wikis.
25 */
26 class WikiMap {
27
28 /**
29 * Get a WikiReference object for $wikiID
30 *
31 * @param string $wikiID Wiki'd id (generally database name)
32 * @return WikiReference|null WikiReference object or null if the wiki was not found
33 */
34 public static function getWiki( $wikiID ) {
35 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
36 if ( $wikiReference ) {
37 return $wikiReference;
38 }
39
40 // Try sites, if $wgConf failed
41 return self::getWikiWikiReferenceFromSites( $wikiID );
42 }
43
44 /**
45 * @param string $wikiID
46 * @return WikiReference|null WikiReference object or null if the wiki was not found
47 */
48 private static function getWikiReferenceFromWgConf( $wikiID ) {
49 global $wgConf;
50
51 $wgConf->loadFullData();
52
53 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
54 if ( $major === null ) {
55 return null;
56 }
57 $server = $wgConf->get( 'wgServer', $wikiID, $major,
58 [ 'lang' => $minor, 'site' => $major ] );
59
60 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
61 [ 'lang' => $minor, 'site' => $major ] );
62 if ( $canonicalServer === false || $canonicalServer === null ) {
63 $canonicalServer = $server;
64 }
65
66 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
67 [ 'lang' => $minor, 'site' => $major ] );
68
69 // If we don't have a canonical server or a path containing $1, the
70 // WikiReference isn't going to function properly. Just return null in
71 // that case.
72 if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
73 return null;
74 }
75
76 return new WikiReference( $canonicalServer, $path, $server );
77 }
78
79 /**
80 * @param string $wikiID
81 * @return WikiReference|null WikiReference object or null if the wiki was not found
82 */
83 private static function getWikiWikiReferenceFromSites( $wikiID ) {
84 $siteLookup = \MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
85 $site = $siteLookup->getSite( $wikiID );
86
87 if ( !$site instanceof MediaWikiSite ) {
88 // Abort if not a MediaWikiSite, as this is about Wikis
89 return null;
90 }
91
92 $urlParts = wfParseUrl( $site->getPageUrl() );
93 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
94 // We can't create a meaningful WikiReference without URLs
95 return null;
96 }
97
98 // XXX: Check whether path contains a $1?
99 $path = $urlParts['path'];
100 if ( isset( $urlParts['query'] ) ) {
101 $path .= '?' . $urlParts['query'];
102 }
103
104 $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
105 $canonicalServer .= '://' . $urlParts['host'];
106
107 return new WikiReference( $canonicalServer, $path );
108 }
109
110 /**
111 * Convenience to get the wiki's display name
112 *
113 * @todo We can give more info than just the wiki id!
114 * @param string $wikiID Wiki'd id (generally database name)
115 * @return string|int Wiki's name or $wiki_id if the wiki was not found
116 */
117 public static function getWikiName( $wikiID ) {
118 $wiki = self::getWiki( $wikiID );
119
120 if ( $wiki ) {
121 return $wiki->getDisplayName();
122 }
123 return $wikiID;
124 }
125
126 /**
127 * Convenience to get a link to a user page on a foreign wiki
128 *
129 * @param string $wikiID Wiki'd id (generally database name)
130 * @param string $user User name (must be normalised before calling this function!)
131 * @param string $text Link's text; optional, default to "User:$user"
132 * @return string HTML link or false if the wiki was not found
133 */
134 public static function foreignUserLink( $wikiID, $user, $text = null ) {
135 return self::makeForeignLink( $wikiID, "User:$user", $text );
136 }
137
138 /**
139 * Convenience to get a link to a page on a foreign wiki
140 *
141 * @param string $wikiID Wiki'd id (generally database name)
142 * @param string $page Page name (must be normalised before calling this function!)
143 * @param string $text Link's text; optional, default to $page
144 * @return string|false HTML link or false if the wiki was not found
145 */
146 public static function makeForeignLink( $wikiID, $page, $text = null ) {
147 if ( !$text ) {
148 $text = $page;
149 }
150
151 $url = self::getForeignURL( $wikiID, $page );
152 if ( $url === false ) {
153 return false;
154 }
155
156 return Linker::makeExternalLink( $url, $text );
157 }
158
159 /**
160 * Convenience to get a url to a page on a foreign wiki
161 *
162 * @param string $wikiID Wiki'd id (generally database name)
163 * @param string $page Page name (must be normalised before calling this function!)
164 * @param string|null $fragmentId
165 *
166 * @return string|bool URL or false if the wiki was not found
167 */
168 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
169 $wiki = self::getWiki( $wikiID );
170
171 if ( $wiki ) {
172 return $wiki->getFullUrl( $page, $fragmentId );
173 }
174
175 return false;
176 }
177 }