MediaWiki  master
TextConflictHelper.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\EditPage;
22 
23 use Content;
24 use ContentHandler;
32 
40 
44  protected $title;
45 
49  public $contentModel;
50 
55 
59  protected $out;
60 
64  protected $stats;
65 
69  protected $submitLabel;
70 
74  protected $yourtext = '';
75 
79  protected $storedversion = '';
80 
84  private $contentHandlerFactory;
85 
95  public function __construct(
97  IContentHandlerFactory $contentHandlerFactory
98  ) {
99  $this->title = $title;
100  $this->out = $out;
101  $this->stats = $stats;
102  $this->submitLabel = $submitLabel;
103  $this->contentModel = $title->getContentModel();
104  $this->contentHandlerFactory = $contentHandlerFactory;
105 
106  $this->contentFormat = $this->contentHandlerFactory
107  ->getContentHandler( $this->contentModel )
108  ->getDefaultFormat();
109  }
110 
115  public function setTextboxes( $yourtext, $storedversion ) {
116  $this->yourtext = $yourtext;
117  $this->storedversion = $storedversion;
118  }
119 
123  public function setContentModel( $contentModel ) {
124  $this->contentModel = $contentModel;
125  }
126 
130  public function setContentFormat( $contentFormat ) {
131  $this->contentFormat = $contentFormat;
132  }
133 
138  public function incrementConflictStats( User $user = null ) {
139  $this->stats->increment( 'edit.failures.conflict' );
140  // Only include 'standard' namespaces to avoid creating unknown numbers of statsd metrics
141  if (
142  $this->title->getNamespace() >= NS_MAIN &&
143  $this->title->getNamespace() <= NS_CATEGORY_TALK
144  ) {
145  $this->stats->increment(
146  'edit.failures.conflict.byNamespaceId.' . $this->title->getNamespace()
147  );
148  }
149  if ( $user ) {
150  $this->incrementStatsByUserEdits( $user->getEditCount(), 'edit.failures.conflict' );
151  }
152  }
153 
158  public function incrementResolvedStats( User $user = null ) {
159  $this->stats->increment( 'edit.failures.conflict.resolved' );
160  // Only include 'standard' namespaces to avoid creating unknown numbers of statsd metrics
161  if (
162  $this->title->getNamespace() >= NS_MAIN &&
163  $this->title->getNamespace() <= NS_CATEGORY_TALK
164  ) {
165  $this->stats->increment(
166  'edit.failures.conflict.resolved.byNamespaceId.' . $this->title->getNamespace()
167  );
168  }
169  if ( $user ) {
171  $user->getEditCount(),
172  'edit.failures.conflict.resolved'
173  );
174  }
175  }
176 
181  protected function incrementStatsByUserEdits( $userEdits, $keyPrefixBase ) {
182  if ( $userEdits === null ) {
183  $userBucket = 'anon';
184  } elseif ( $userEdits > 200 ) {
185  $userBucket = 'over200';
186  } elseif ( $userEdits > 100 ) {
187  $userBucket = 'over100';
188  } elseif ( $userEdits > 10 ) {
189  $userBucket = 'over10';
190  } else {
191  $userBucket = 'under11';
192  }
193  $this->stats->increment( $keyPrefixBase . '.byUserEdits.' . $userBucket );
194  }
195 
199  public function getExplainHeader() {
200  return Html::rawElement(
201  'div',
202  [ 'class' => 'mw-explainconflict' ],
203  $this->out->msg( 'explainconflict', $this->out->msg( $this->submitLabel )->text() )->parse()
204  );
205  }
206 
213  public function getEditConflictMainTextBox( array $customAttribs = [] ) {
214  $builder = new TextboxBuilder();
215  $classes = $builder->getTextboxProtectionCSSClasses( $this->title );
216 
217  $attribs = [
218  'aria-label' => $this->out->msg( 'edit-textarea-aria-label' )->text(),
219  'tabindex' => 1,
220  ];
221  $attribs += $customAttribs;
222 
223  $attribs = $builder->mergeClassesIntoAttributes( $classes, $attribs );
224 
225  $attribs = $builder->buildTextboxAttribs(
226  'wpTextbox1',
227  $attribs,
228  $this->out->getUser(),
229  $this->title
230  );
231 
232  return Html::textarea(
233  'wpTextbox1',
234  $builder->addNewLineAtEnd( $this->storedversion ),
235  $attribs
236  );
237  }
238 
245  public function getEditFormHtmlBeforeContent() {
246  return '';
247  }
248 
255  public function getEditFormHtmlAfterContent() {
256  return '';
257  }
258 
263  public function showEditFormTextAfterFooters() {
264  $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourdiff" );
265 
266  $yourContent = $this->toEditContent( $this->yourtext );
267  $storedContent = $this->toEditContent( $this->storedversion );
268  $handler = $this->contentHandlerFactory->getContentHandler( $this->contentModel );
269  $diffEngine = $handler->createDifferenceEngine( $this->out );
270 
271  $diffEngine->setContent( $yourContent, $storedContent );
272  $diffEngine->showDiff(
273  $this->out->msg( 'yourtext' )->parse(),
274  $this->out->msg( 'storedversion' )->text()
275  );
276 
277  $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourtext" );
278 
279  $builder = new TextboxBuilder();
280  $attribs = $builder->buildTextboxAttribs(
281  'wpTextbox2',
282  [ 'tabindex' => 6, 'readonly' ],
283  $this->out->getUser(),
284  $this->title
285  );
286 
287  $this->out->addHTML(
288  Html::textarea( 'wpTextbox2', $builder->addNewLineAtEnd( $this->yourtext ), $attribs )
289  );
290  }
291 
296  private function toEditContent( $text ) {
298  $text,
299  $this->title,
300  $this->contentModel,
301  $this->contentFormat
302  );
303  }
304 }
const NS_MAIN
Definition: Defines.php:64
const NS_CATEGORY_TALK
Definition: Defines.php:79
A content handler knows how do deal with a specific type of content on a wiki page.
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
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.
__construct(Title $title, OutputPage $out, IBufferingStatsdDataFactory $stats, $submitLabel, IContentHandlerFactory $contentHandlerFactory)
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.
IBufferingStatsdDataFactory $stats
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.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:239
static textarea( $name, $value='', array $attribs=[])
Convenience function to produce a <textarea> element.
Definition: Html.php:913
This is one of the Core classes and should be read at least once by any new developers.
Definition: OutputPage.php:93
Represents a title within MediaWiki.
Definition: Title.php:76
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition: Title.php:1080
internal since 1.36
Definition: User.php:98
Base interface for representing page content.
Definition: Content.php:39
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
getContentHandler(string $modelID)
Returns a ContentHandler instance for the given $modelID.