Merge "(Bug 45775) Adjusted the margin of "userloginForm""
[lhc/web/wiklou.git] / includes / content / WikitextContent.php
1 <?php
2 /**
3 * Content object for wiki text pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
28 /**
29 * Content object for wiki text pages.
30 *
31 * @ingroup Content
32 */
33 class WikitextContent extends TextContent {
34
35 public function __construct( $text ) {
36 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
37 }
38
39 /**
40 * @see Content::getSection()
41 */
42 public function getSection( $section ) {
43 global $wgParser;
44
45 $text = $this->getNativeData();
46 $sect = $wgParser->getSection( $text, $section, false );
47
48 if ( $sect === false ) {
49 return false;
50 } else {
51 return new WikitextContent( $sect );
52 }
53 }
54
55 /**
56 * @see Content::replaceSection()
57 */
58 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
59 wfProfileIn( __METHOD__ );
60
61 $myModelId = $this->getModel();
62 $sectionModelId = $with->getModel();
63
64 if ( $sectionModelId != $myModelId ) {
65 throw new MWException( "Incompatible content model for section: " .
66 "document uses $myModelId but " .
67 "section uses $sectionModelId." );
68 }
69
70 $oldtext = $this->getNativeData();
71 $text = $with->getNativeData();
72
73 if ( $section === '' ) {
74 wfProfileOut( __METHOD__ );
75 return $with; # XXX: copy first?
76 } if ( $section == 'new' ) {
77 # Inserting a new section
78 $subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
79 ->rawParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
80 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
81 $text = strlen( trim( $oldtext ) ) > 0
82 ? "{$oldtext}\n\n{$subject}{$text}"
83 : "{$subject}{$text}";
84 }
85 } else {
86 # Replacing an existing section; roll out the big guns
87 global $wgParser;
88
89 $text = $wgParser->replaceSection( $oldtext, $section, $text );
90 }
91
92 $newContent = new WikitextContent( $text );
93
94 wfProfileOut( __METHOD__ );
95 return $newContent;
96 }
97
98 /**
99 * Returns a new WikitextContent object with the given section heading
100 * prepended.
101 *
102 * @param $header string
103 * @return Content
104 */
105 public function addSectionHeader( $header ) {
106 $text = wfMessage( 'newsectionheaderdefaultlevel' )
107 ->rawParams( $header )->inContentLanguage()->text();
108 $text .= "\n\n";
109 $text .= $this->getNativeData();
110
111 return new WikitextContent( $text );
112 }
113
114 /**
115 * Returns a Content object with pre-save transformations applied using
116 * Parser::preSaveTransform().
117 *
118 * @param $title Title
119 * @param $user User
120 * @param $popts ParserOptions
121 * @return Content
122 */
123 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
124 global $wgParser;
125
126 $text = $this->getNativeData();
127 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
128 rtrim( $pst );
129
130 return ( $text === $pst ) ? $this : new WikitextContent( $pst );
131 }
132
133 /**
134 * Returns a Content object with preload transformations applied (or this
135 * object if no transformations apply).
136 *
137 * @param $title Title
138 * @param $popts ParserOptions
139 * @return Content
140 */
141 public function preloadTransform( Title $title, ParserOptions $popts ) {
142 global $wgParser;
143
144 $text = $this->getNativeData();
145 $plt = $wgParser->getPreloadText( $text, $title, $popts );
146
147 return new WikitextContent( $plt );
148 }
149
150 /**
151 * Implement redirect extraction for wikitext.
152 *
153 * @return null|Title
154 *
155 * @note: migrated here from Title::newFromRedirectInternal()
156 *
157 * @see Content::getRedirectTarget
158 * @see AbstractContent::getRedirectTarget
159 */
160 public function getRedirectTarget() {
161 global $wgMaxRedirects;
162 if ( $wgMaxRedirects < 1 ) {
163 // redirects are disabled, so quit early
164 return null;
165 }
166 $redir = MagicWord::get( 'redirect' );
167 $text = trim( $this->getNativeData() );
168 if ( $redir->matchStartAndRemove( $text ) ) {
169 // Extract the first link and see if it's usable
170 // Ensure that it really does come directly after #REDIRECT
171 // Some older redirects included a colon, so don't freak about that!
172 $m = array();
173 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
174 // Strip preceding colon used to "escape" categories, etc.
175 // and URL-decode links
176 if ( strpos( $m[1], '%' ) !== false ) {
177 // Match behavior of inline link parsing here;
178 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
179 }
180 $title = Title::newFromText( $m[1] );
181 // If the title is a redirect to bad special pages or is invalid, return null
182 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
183 return null;
184 }
185 return $title;
186 }
187 }
188 return null;
189 }
190
191 /**
192 * @see Content::updateRedirect()
193 *
194 * This implementation replaces the first link on the page with the given new target
195 * if this Content object is a redirect. Otherwise, this method returns $this.
196 *
197 * @since 1.21
198 *
199 * @param Title $target
200 *
201 * @return Content a new Content object with the updated redirect (or $this if this Content object isn't a redirect)
202 */
203 public function updateRedirect( Title $target ) {
204 if ( !$this->isRedirect() ) {
205 return $this;
206 }
207
208 # Fix the text
209 # Remember that redirect pages can have categories, templates, etc.,
210 # so the regex has to be fairly general
211 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
212 '[[' . $target->getFullText() . ']]',
213 $this->getNativeData(), 1 );
214
215 return new WikitextContent( $newText );
216 }
217
218 /**
219 * Returns true if this content is not a redirect, and this content's text
220 * is countable according to the criteria defined by $wgArticleCountMethod.
221 *
222 * @param bool $hasLinks if it is known whether this content contains
223 * links, provide this information here, to avoid redundant parsing to
224 * find out (default: null).
225 * @param $title Title: (default: null)
226 *
227 * @internal param \IContextSource $context context for parsing if necessary
228 *
229 * @return bool True if the content is countable
230 */
231 public function isCountable( $hasLinks = null, Title $title = null ) {
232 global $wgArticleCountMethod;
233
234 if ( $this->isRedirect() ) {
235 return false;
236 }
237
238 $text = $this->getNativeData();
239
240 switch ( $wgArticleCountMethod ) {
241 case 'any':
242 return true;
243 case 'comma':
244 return strpos( $text, ',' ) !== false;
245 case 'link':
246 if ( $hasLinks === null ) { # not known, find out
247 if ( !$title ) {
248 $context = RequestContext::getMain();
249 $title = $context->getTitle();
250 }
251
252 $po = $this->getParserOutput( $title, null, null, false );
253 $links = $po->getLinks();
254 $hasLinks = !empty( $links );
255 }
256
257 return $hasLinks;
258 }
259
260 return false;
261 }
262
263 public function getTextForSummary( $maxlength = 250 ) {
264 $truncatedtext = parent::getTextForSummary( $maxlength );
265
266 # clean up unfinished links
267 # XXX: make this optional? wasn't there in autosummary, but required for
268 # deletion summary.
269 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
270
271 return $truncatedtext;
272 }
273
274 /**
275 * Returns a ParserOutput object resulting from parsing the content's text
276 * using $wgParser.
277 *
278 * @since 1.21
279 *
280 * @param $title Title
281 * @param int $revId Revision to pass to the parser (default: null)
282 * @param $options ParserOptions (default: null)
283 * @param bool $generateHtml (default: false)
284 *
285 * @internal param \IContextSource|null $context
286 * @return ParserOutput representing the HTML form of the text
287 */
288 public function getParserOutput( Title $title,
289 $revId = null,
290 ParserOptions $options = null, $generateHtml = true
291 ) {
292 global $wgParser;
293
294 if ( !$options ) {
295 //NOTE: use canonical options per default to produce cacheable output
296 $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
297 }
298
299 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
300 return $po;
301 }
302
303 protected function getHtml() {
304 throw new MWException(
305 "getHtml() not implemented for wikitext. "
306 . "Use getParserOutput()->getText()."
307 );
308 }
309
310 /**
311 * @see Content::matchMagicWord()
312 *
313 * This implementation calls $word->match() on the this TextContent object's text.
314 *
315 * @param MagicWord $word
316 *
317 * @return bool whether this Content object matches the given magic word.
318 */
319 public function matchMagicWord( MagicWord $word ) {
320 return $word->match( $this->getNativeData() );
321 }
322 }