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