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