resourceloader: Remove redundant closure of some startup and base files
[lhc/web/wiklou.git] / includes / site / MediaWikiSite.php
1 <?php
2
3 use MediaWiki\Site\MediaWikiPageNameNormalizer;
4
5 /**
6 * Class representing a MediaWiki site.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Site
25 * @license GPL-2.0-or-later
26 * @author John Erling Blad < jeblad@gmail.com >
27 * @author Daniel Kinzler
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 */
30
31 /**
32 * Class representing a MediaWiki site.
33 *
34 * @since 1.21
35 *
36 * @ingroup Site
37 */
38 class MediaWikiSite extends Site {
39 const PATH_FILE = 'file_path';
40 const PATH_PAGE = 'page_path';
41
42 /**
43 * @since 1.21
44 *
45 * @param string $type
46 */
47 public function __construct( $type = self::TYPE_MEDIAWIKI ) {
48 parent::__construct( $type );
49 }
50
51 /**
52 * Returns the database form of the given title.
53 *
54 * @since 1.21
55 *
56 * @param string $title The target page's title, in normalized form.
57 *
58 * @return string
59 */
60 public function toDBKey( $title ) {
61 return str_replace( ' ', '_', $title );
62 }
63
64 /**
65 * Returns the normalized form of the given page title, using the
66 * normalization rules of the given site. If the given title is a redirect,
67 * the redirect will be resolved and the redirect target is returned.
68 * Only titles of existing pages will be returned.
69 *
70 * @note This actually makes an API request to the remote site, so beware
71 * that this function is slow and depends on an external service.
72 *
73 * @note If MW_PHPUNIT_TEST is defined, the call to the external site is
74 * skipped, and the title is normalized using the local normalization
75 * rules as implemented by the Title class.
76 *
77 * @see Site::normalizePageName
78 *
79 * @since 1.21
80 *
81 * @param string $pageName
82 *
83 * @return string|false The normalized form of the title,
84 * or false to indicate an invalid title, a missing page,
85 * or some other kind of error.
86 * @throws MWException
87 */
88 public function normalizePageName( $pageName ) {
89 if ( defined( 'MW_PHPUNIT_TEST' ) ) {
90 // If the code is under test, don't call out to other sites, just
91 // normalize locally.
92 // Note: this may cause results to be inconsistent with the actual
93 // normalization used by the respective remote site!
94
95 $t = Title::newFromText( $pageName );
96 return $t->getPrefixedText();
97 } else {
98 static $mediaWikiPageNameNormalizer = null;
99
100 if ( $mediaWikiPageNameNormalizer === null ) {
101 $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer();
102 }
103
104 return $mediaWikiPageNameNormalizer->normalizePageName(
105 $pageName,
106 $this->getFileUrl( 'api.php' )
107 );
108 }
109 }
110
111 /**
112 * @see Site::getLinkPathType
113 * Returns Site::PATH_PAGE
114 *
115 * @since 1.21
116 *
117 * @return string
118 */
119 public function getLinkPathType() {
120 return self::PATH_PAGE;
121 }
122
123 /**
124 * Returns the relative page path.
125 *
126 * @since 1.21
127 *
128 * @return string
129 */
130 public function getRelativePagePath() {
131 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
132 }
133
134 /**
135 * Returns the relative file path.
136 *
137 * @since 1.21
138 *
139 * @return string
140 */
141 public function getRelativeFilePath() {
142 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
143 }
144
145 /**
146 * Sets the relative page path.
147 *
148 * @since 1.21
149 *
150 * @param string $path
151 */
152 public function setPagePath( $path ) {
153 $this->setPath( self::PATH_PAGE, $path );
154 }
155
156 /**
157 * Sets the relative file path.
158 *
159 * @since 1.21
160 *
161 * @param string $path
162 */
163 public function setFilePath( $path ) {
164 $this->setPath( self::PATH_FILE, $path );
165 }
166
167 /**
168 * @see Site::getPageUrl
169 *
170 * This implementation returns a URL constructed using the path returned by getLinkPath().
171 * In addition to the default behavior implemented by Site::getPageUrl(), this
172 * method converts the $pageName to DBKey-format by replacing spaces with underscores
173 * before using it in the URL.
174 *
175 * @since 1.21
176 *
177 * @param string|bool $pageName Page name or false (default: false)
178 *
179 * @return string|bool|null
180 */
181 public function getPageUrl( $pageName = false ) {
182 $url = $this->getLinkPath();
183
184 if ( $url === false ) {
185 return false;
186 }
187
188 if ( $pageName !== false ) {
189 $pageName = $this->toDBKey( trim( $pageName ) );
190 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
191 }
192
193 return $url;
194 }
195
196 /**
197 * Returns the full file path (ie site url + relative file path).
198 * The path should go at the $1 marker. If the $path
199 * argument is provided, the marker will be replaced by it's value.
200 *
201 * @since 1.21
202 *
203 * @param string|bool $path
204 *
205 * @return string
206 */
207 public function getFileUrl( $path = false ) {
208 $filePath = $this->getPath( self::PATH_FILE );
209
210 if ( $filePath !== false ) {
211 $filePath = str_replace( '$1', $path, $filePath );
212 }
213
214 return $filePath;
215 }
216 }