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