Merge "Move up devunt's name to Developers"
[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 GNU GPL v2+
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 * Constructor.
44 *
45 * @since 1.21
46 *
47 * @param string $type
48 */
49 public function __construct( $type = self::TYPE_MEDIAWIKI ) {
50 parent::__construct( $type );
51 }
52
53 /**
54 * Returns the database form of the given title.
55 *
56 * @since 1.21
57 *
58 * @param string $title The target page's title, in normalized form.
59 *
60 * @return string
61 */
62 public function toDBKey( $title ) {
63 return str_replace( ' ', '_', $title );
64 }
65
66 /**
67 * Returns the normalized form of the given page title, using the
68 * normalization rules of the given site. If the given title is a redirect,
69 * the redirect weill be resolved and the redirect target is returned.
70 *
71 * @note This actually makes an API request to the remote site, so beware
72 * that this function is slow and depends on an external service.
73 *
74 * @note If MW_PHPUNIT_TEST is defined, the call to the external site is
75 * skipped, and the title is normalized using the local normalization
76 * rules as implemented by the Title class.
77 *
78 * @see Site::normalizePageName
79 *
80 * @since 1.21
81 *
82 * @param string $pageName
83 *
84 * @return string
85 * @throws MWException
86 */
87 public function normalizePageName( $pageName ) {
88 if ( defined( 'MW_PHPUNIT_TEST' ) ) {
89 // If the code is under test, don't call out to other sites, just
90 // normalize locally.
91 // Note: this may cause results to be inconsistent with the actual
92 // normalization used by the respective remote site!
93
94 $t = Title::newFromText( $pageName );
95 return $t->getPrefixedText();
96 } else {
97 static $mediaWikiPageNameNormalizer = null;
98
99 if ( $mediaWikiPageNameNormalizer === null ) {
100 $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer();
101 }
102
103 return $mediaWikiPageNameNormalizer->normalizePageName(
104 $pageName,
105 $this->getFileUrl( 'api.php' )
106 );
107 }
108 }
109
110 /**
111 * @see Site::getLinkPathType
112 * Returns Site::PATH_PAGE
113 *
114 * @since 1.21
115 *
116 * @return string
117 */
118 public function getLinkPathType() {
119 return self::PATH_PAGE;
120 }
121
122 /**
123 * Returns the relative page path.
124 *
125 * @since 1.21
126 *
127 * @return string
128 */
129 public function getRelativePagePath() {
130 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
131 }
132
133 /**
134 * Returns the relative file path.
135 *
136 * @since 1.21
137 *
138 * @return string
139 */
140 public function getRelativeFilePath() {
141 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
142 }
143
144 /**
145 * Sets the relative page path.
146 *
147 * @since 1.21
148 *
149 * @param string $path
150 */
151 public function setPagePath( $path ) {
152 $this->setPath( self::PATH_PAGE, $path );
153 }
154
155 /**
156 * Sets the relative file path.
157 *
158 * @since 1.21
159 *
160 * @param string $path
161 */
162 public function setFilePath( $path ) {
163 $this->setPath( self::PATH_FILE, $path );
164 }
165
166 /**
167 * @see Site::getPageUrl
168 *
169 * This implementation returns a URL constructed using the path returned by getLinkPath().
170 * In addition to the default behavior implemented by Site::getPageUrl(), this
171 * method converts the $pageName to DBKey-format by replacing spaces with underscores
172 * before using it in the URL.
173 *
174 * @since 1.21
175 *
176 * @param string|bool $pageName Page name or false (default: false)
177 *
178 * @return string
179 */
180 public function getPageUrl( $pageName = false ) {
181 $url = $this->getLinkPath();
182
183 if ( $url === false ) {
184 return false;
185 }
186
187 if ( $pageName !== false ) {
188 $pageName = $this->toDBKey( trim( $pageName ) );
189 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
190 }
191
192 return $url;
193 }
194
195 /**
196 * Returns the full file path (ie site url + relative file path).
197 * The path should go at the $1 marker. If the $path
198 * argument is provided, the marker will be replaced by it's value.
199 *
200 * @since 1.21
201 *
202 * @param string|bool $path
203 *
204 * @return string
205 */
206 public function getFileUrl( $path = false ) {
207 $filePath = $this->getPath( self::PATH_FILE );
208
209 if ( $filePath !== false ) {
210 $filePath = str_replace( '$1', $path, $filePath );
211 }
212
213 return $filePath;
214 }
215 }