MediaWiki  master
TextConflictHelper.php
Go to the documentation of this file.
1 <?php
25 namespace MediaWiki\EditPage;
26 
27 use Content;
28 use ContentHandler;
29 use Html;
34 use OutputPage;
35 use Title;
36 use User;
37 
45 
49  protected $title;
50 
54  public $contentModel;
55 
60 
64  protected $out;
65 
69  protected $stats;
70 
74  protected $submitLabel;
75 
79  protected $yourtext = '';
80 
84  protected $storedversion = '';
85 
90 
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 
124  public function setTextboxes( $yourtext, $storedversion ) {
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 
254  public function getEditFormHtmlBeforeContent() {
255  return '';
256  }
257 
264  public function getEditFormHtmlAfterContent() {
265  return '';
266  }
267 
272  public function showEditFormTextAfterFooters() {
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 ) {
307  $text,
308  $this->title,
309  $this->contentModel,
310  $this->contentFormat
311  );
312  }
313 }
MediaWiki\EditPage\TextConflictHelper\getExplainHeader
getExplainHeader()
Definition: TextConflictHelper.php:208
MediaWiki\EditPage\TextConflictHelper\$contentModel
null string $contentModel
Definition: TextConflictHelper.php:54
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:59
MediaWiki\EditPage\TextConflictHelper\$storedversion
string $storedversion
Definition: TextConflictHelper.php:84
Html\textarea
static textarea( $name, $value='', array $attribs=[])
Convenience function to produce a <textarea> element.
Definition: Html.php:818
MediaWiki\EditPage\TextConflictHelper\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: TextConflictHelper.php:89
MediaWiki\EditPage\TextConflictHelper\incrementStatsByUserEdits
incrementStatsByUserEdits( $userEdits, $keyPrefixBase)
Definition: TextConflictHelper.php:190
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
MediaWiki\EditPage\TextConflictHelper\$title
Title $title
Definition: TextConflictHelper.php:49
MediaWiki\EditPage\TextboxBuilder
Helps EditPage build textboxes.
Definition: TextboxBuilder.php:37
MediaWiki\EditPage\TextConflictHelper\__construct
__construct(Title $title, OutputPage $out, IBufferingStatsdDataFactory $stats, $submitLabel, ?IContentHandlerFactory $contentHandlerFactory=null)
Definition: TextConflictHelper.php:100
MediaWiki\EditPage\TextConflictHelper\$out
OutputPage $out
Definition: TextConflictHelper.php:64
MediaWiki\EditPage\TextConflictHelper\showEditFormTextAfterFooters
showEditFormTextAfterFooters()
Content to go in the edit form after the footers (templates on this page, hidden categories,...
Definition: TextConflictHelper.php:272
MediaWiki\EditPage\TextConflictHelper\setContentFormat
setContentFormat( $contentFormat)
Definition: TextConflictHelper.php:139
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:195
MediaWiki\EditPage\TextConflictHelper\incrementResolvedStats
incrementResolvedStats(User $user=null)
Record when a user has resolved an edit conflict.
Definition: TextConflictHelper.php:167
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1027
MediaWiki\EditPage\TextConflictHelper\toEditContent
toEditContent( $text)
Definition: TextConflictHelper.php:305
MediaWiki\EditPage\TextConflictHelper\incrementConflictStats
incrementConflictStats(User $user=null)
Record a user encountering an edit conflict.
Definition: TextConflictHelper.php:147
OutputPage
This is one of the Core classes and should be read at least once by any new developers.
Definition: OutputPage.php:47
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:142
MediaWiki\EditPage\TextConflictHelper\setTextboxes
setTextboxes( $yourtext, $storedversion)
Definition: TextConflictHelper.php:124
MediaWiki\EditPage\TextConflictHelper
Helper for displaying edit conflicts in text content models to users.
Definition: TextConflictHelper.php:44
MediaWiki\EditPage\TextConflictHelper\$yourtext
string $yourtext
Definition: TextConflictHelper.php:79
MediaWiki\EditPage\TextConflictHelper\getEditFormHtmlAfterContent
getEditFormHtmlAfterContent()
Content to go in the edit form after textbox1.
Definition: TextConflictHelper.php:264
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
Title\getContentModel
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition: Title.php:1046
MediaWiki\EditPage\TextConflictHelper\setContentModel
setContentModel( $contentModel)
Definition: TextConflictHelper.php:132
MediaWiki\EditPage\TextConflictHelper\$submitLabel
string $submitLabel
Message key for submit button's label.
Definition: TextConflictHelper.php:74
Content
Base interface for content objects.
Definition: Content.php:35
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:13
MediaWiki\EditPage\TextConflictHelper\$contentFormat
null string $contentFormat
Definition: TextConflictHelper.php:59
Title
Represents a title within MediaWiki.
Definition: Title.php:41
MediaWiki\EditPage\TextConflictHelper\getEditFormHtmlBeforeContent
getEditFormHtmlBeforeContent()
Content to go in the edit form before textbox1.
Definition: TextConflictHelper.php:254
MediaWiki\Content\IContentHandlerFactory\getContentHandler
getContentHandler(string $modelID)
Returns a ContentHandler instance for the given $modelID.
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
MWUnknownContentModelException
Exception thrown when an unregistered content model is requested.
Definition: MWUnknownContentModelException.php:11
MediaWiki\EditPage\TextConflictHelper\getEditConflictMainTextBox
getEditConflictMainTextBox(array $customAttribs=[])
HTML to build the textbox1 on edit conflicts.
Definition: TextConflictHelper.php:222
NS_CATEGORY_TALK
const NS_CATEGORY_TALK
Definition: Defines.php:84
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
MediaWiki\EditPage\TextConflictHelper\$stats
IBufferingStatsdDataFactory $stats
Definition: TextConflictHelper.php:69
MediaWiki\EditPage
Html
This class is a collection of static functions that serve two purposes:
Definition: Html.php:49