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