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