]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/site/MediaWikiSite.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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          *
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 weill be resolved and the redirect target is returned.
68          *
69          * @note This actually makes an API request to the remote site, so beware
70          *   that this function is slow and depends on an external service.
71          *
72          * @note If MW_PHPUNIT_TEST is defined, the call to the external site is
73          *   skipped, and the title is normalized using the local normalization
74          *   rules as implemented by the Title class.
75          *
76          * @see Site::normalizePageName
77          *
78          * @since 1.21
79          *
80          * @param string $pageName
81          *
82          * @return string
83          * @throws MWException
84          */
85         public function normalizePageName( $pageName ) {
86                 if ( defined( 'MW_PHPUNIT_TEST' ) ) {
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                         );
105                 }
106         }
107
108         /**
109          * @see Site::getLinkPathType
110          * Returns Site::PATH_PAGE
111          *
112          * @since 1.21
113          *
114          * @return string
115          */
116         public function getLinkPathType() {
117                 return self::PATH_PAGE;
118         }
119
120         /**
121          * Returns the relative page path.
122          *
123          * @since 1.21
124          *
125          * @return string
126          */
127         public function getRelativePagePath() {
128                 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
129         }
130
131         /**
132          * Returns the relative file path.
133          *
134          * @since 1.21
135          *
136          * @return string
137          */
138         public function getRelativeFilePath() {
139                 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
140         }
141
142         /**
143          * Sets the relative page path.
144          *
145          * @since 1.21
146          *
147          * @param string $path
148          */
149         public function setPagePath( $path ) {
150                 $this->setPath( self::PATH_PAGE, $path );
151         }
152
153         /**
154          * Sets the relative file path.
155          *
156          * @since 1.21
157          *
158          * @param string $path
159          */
160         public function setFilePath( $path ) {
161                 $this->setPath( self::PATH_FILE, $path );
162         }
163
164         /**
165          * @see Site::getPageUrl
166          *
167          * This implementation returns a URL constructed using the path returned by getLinkPath().
168          * In addition to the default behavior implemented by Site::getPageUrl(), this
169          * method converts the $pageName to DBKey-format by replacing spaces with underscores
170          * before using it in the URL.
171          *
172          * @since 1.21
173          *
174          * @param string|bool $pageName Page name or false (default: false)
175          *
176          * @return string
177          */
178         public function getPageUrl( $pageName = false ) {
179                 $url = $this->getLinkPath();
180
181                 if ( $url === false ) {
182                         return false;
183                 }
184
185                 if ( $pageName !== false ) {
186                         $pageName = $this->toDBKey( trim( $pageName ) );
187                         $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
188                 }
189
190                 return $url;
191         }
192
193         /**
194          * Returns the full file path (ie site url + relative file path).
195          * The path should go at the $1 marker. If the $path
196          * argument is provided, the marker will be replaced by it's value.
197          *
198          * @since 1.21
199          *
200          * @param string|bool $path
201          *
202          * @return string
203          */
204         public function getFileUrl( $path = false ) {
205                 $filePath = $this->getPath( self::PATH_FILE );
206
207                 if ( $filePath !== false ) {
208                         $filePath = str_replace( '$1', $path, $filePath );
209                 }
210
211                 return $filePath;
212         }
213 }