MediaWiki REL1_39
TextConflictHelper.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\EditPage;
26
27use Content;
29use Html;
34use OutputPage;
35use Title;
36use User;
37
45
49 protected $title;
50
55
60
64 protected $out;
65
69 protected $stats;
70
74 protected $submitLabel;
75
79 protected $yourtext = '';
80
84 protected $storedversion = '';
85
89 private $contentHandlerFactory;
90
101 $submitLabel, ?IContentHandlerFactory $contentHandlerFactory = null
102 ) {
103 $this->title = $title;
104 $this->out = $out;
105 $this->stats = $stats;
106 $this->submitLabel = $submitLabel;
107 $this->contentModel = $title->getContentModel();
108
109 if ( !$contentHandlerFactory ) {
110 wfDeprecated( __METHOD__ . ' without $contentHandlerFactory parameter', '1.35' );
111 $contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory();
112 }
113 $this->contentHandlerFactory = $contentHandlerFactory;
114
115 $this->contentFormat = $this->contentHandlerFactory
116 ->getContentHandler( $this->contentModel )
117 ->getDefaultFormat();
118 }
119
125 $this->yourtext = $yourtext;
126 $this->storedversion = $storedversion;
127 }
128
132 public function setContentModel( $contentModel ) {
133 $this->contentModel = $contentModel;
134 }
135
139 public function setContentFormat( $contentFormat ) {
140 $this->contentFormat = $contentFormat;
141 }
142
147 public function incrementConflictStats( User $user = null ) {
148 $this->stats->increment( 'edit.failures.conflict' );
149 // Only include 'standard' namespaces to avoid creating unknown numbers of statsd metrics
150 if (
151 $this->title->getNamespace() >= NS_MAIN &&
152 $this->title->getNamespace() <= NS_CATEGORY_TALK
153 ) {
154 $this->stats->increment(
155 'edit.failures.conflict.byNamespaceId.' . $this->title->getNamespace()
156 );
157 }
158 if ( $user ) {
159 $this->incrementStatsByUserEdits( $user->getEditCount(), 'edit.failures.conflict' );
160 }
161 }
162
167 public function incrementResolvedStats( User $user = null ) {
168 $this->stats->increment( 'edit.failures.conflict.resolved' );
169 // Only include 'standard' namespaces to avoid creating unknown numbers of statsd metrics
170 if (
171 $this->title->getNamespace() >= NS_MAIN &&
172 $this->title->getNamespace() <= NS_CATEGORY_TALK
173 ) {
174 $this->stats->increment(
175 'edit.failures.conflict.resolved.byNamespaceId.' . $this->title->getNamespace()
176 );
177 }
178 if ( $user ) {
180 $user->getEditCount(),
181 'edit.failures.conflict.resolved'
182 );
183 }
184 }
185
190 protected function incrementStatsByUserEdits( $userEdits, $keyPrefixBase ) {
191 if ( $userEdits === null ) {
192 $userBucket = 'anon';
193 } elseif ( $userEdits > 200 ) {
194 $userBucket = 'over200';
195 } elseif ( $userEdits > 100 ) {
196 $userBucket = 'over100';
197 } elseif ( $userEdits > 10 ) {
198 $userBucket = 'over10';
199 } else {
200 $userBucket = 'under11';
201 }
202 $this->stats->increment( $keyPrefixBase . '.byUserEdits.' . $userBucket );
203 }
204
208 public function getExplainHeader() {
209 return Html::rawElement(
210 'div',
211 [ 'class' => 'mw-explainconflict' ],
212 $this->out->msg( 'explainconflict', $this->out->msg( $this->submitLabel )->text() )->parse()
213 );
214 }
215
222 public function getEditConflictMainTextBox( array $customAttribs = [] ) {
223 $builder = new TextboxBuilder();
224 $classes = $builder->getTextboxProtectionCSSClasses( $this->title );
225
226 $attribs = [
227 'aria-label' => $this->out->msg( 'edit-textarea-aria-label' )->text(),
228 'tabindex' => 1,
229 ];
230 $attribs += $customAttribs;
231
232 $attribs = $builder->mergeClassesIntoAttributes( $classes, $attribs );
233
234 $attribs = $builder->buildTextboxAttribs(
235 'wpTextbox1',
236 $attribs,
237 $this->out->getUser(),
238 $this->title
239 );
240
241 return Html::textarea(
242 'wpTextbox1',
243 $builder->addNewLineAtEnd( $this->storedversion ),
244 $attribs
245 );
246 }
247
255 return '';
256 }
257
264 public function getEditFormHtmlAfterContent() {
265 return '';
266 }
267
273 $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourdiff" );
274
275 $yourContent = $this->toEditContent( $this->yourtext );
276 $storedContent = $this->toEditContent( $this->storedversion );
277 $handler = $this->contentHandlerFactory->getContentHandler( $this->contentModel );
278 $diffEngine = $handler->createDifferenceEngine( $this->out );
279
280 $diffEngine->setContent( $yourContent, $storedContent );
281 $diffEngine->showDiff(
282 $this->out->msg( 'yourtext' )->parse(),
283 $this->out->msg( 'storedversion' )->text()
284 );
285
286 $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourtext" );
287
288 $builder = new TextboxBuilder();
289 $attribs = $builder->buildTextboxAttribs(
290 'wpTextbox2',
291 [ 'tabindex' => 6, 'readonly' ],
292 $this->out->getUser(),
293 $this->title
294 );
295
296 $this->out->addHTML(
297 Html::textarea( 'wpTextbox2', $builder->addNewLineAtEnd( $this->yourtext ), $attribs )
298 );
299 }
300
305 private function toEditContent( $text ) {
306 return ContentHandler::makeContent(
307 $text,
308 $this->title,
309 $this->contentModel,
310 $this->contentFormat
311 );
312 }
313}
const NS_MAIN
Definition Defines.php:64
const NS_CATEGORY_TALK
Definition Defines.php:79
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
A content handler knows how do deal with a specific type of content on a wiki page.
This class is a collection of static functions that serve two purposes:
Definition Html.php:51
Exception thrown when an unregistered content model is requested.
Helper for displaying edit conflicts in text content models to users.
incrementResolvedStats(User $user=null)
Record when a user has resolved an edit conflict.
getEditFormHtmlAfterContent()
Content to go in the edit form after textbox1.
getEditConflictMainTextBox(array $customAttribs=[])
HTML to build the textbox1 on edit conflicts.
showEditFormTextAfterFooters()
Content to go in the edit form after the footers (templates on this page, hidden categories,...
incrementConflictStats(User $user=null)
Record a user encountering an edit conflict.
__construct(Title $title, OutputPage $out, IBufferingStatsdDataFactory $stats, $submitLabel, ?IContentHandlerFactory $contentHandlerFactory=null)
setTextboxes( $yourtext, $storedversion)
incrementStatsByUserEdits( $userEdits, $keyPrefixBase)
string $submitLabel
Message key for submit button's label.
getEditFormHtmlBeforeContent()
Content to go in the edit form before textbox1.
Helps EditPage build textboxes.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This is one of the Core classes and should be read at least once by any new developers.
Represents a title within MediaWiki.
Definition Title.php:49
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition Title.php:1088
internal since 1.36
Definition User.php:70
Base interface for content objects.
Definition Content.php:35
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
getContentHandler(string $modelID)
Returns a ContentHandler instance for the given $modelID.