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;
31 use OutputPage;
32 use Title;
33 use User;
34 
42 
46  protected $title;
47 
51  public $contentModel = null;
52 
56  public $contentFormat = null;
57 
61  protected $out;
62 
66  protected $stats;
67 
71  protected $submitLabel;
72 
76  protected $yourtext = '';
77 
81  protected $storedversion = '';
82 
91  ) {
92  $this->title = $title;
93  $this->out = $out;
94  $this->stats = $stats;
95  $this->submitLabel = $submitLabel;
96  $this->contentModel = $title->getContentModel();
97  $this->contentFormat = ContentHandler::getForModelID( $this->contentModel )->getDefaultFormat();
98  }
99 
104  public function setTextboxes( $yourtext, $storedversion ) {
105  $this->yourtext = $yourtext;
106  $this->storedversion = $storedversion;
107  }
108 
112  public function setContentModel( $contentModel ) {
113  $this->contentModel = $contentModel;
114  }
115 
119  public function setContentFormat( $contentFormat ) {
120  $this->contentFormat = $contentFormat;
121  }
122 
127  public function incrementConflictStats( User $user = null ) {
128  $this->stats->increment( 'edit.failures.conflict' );
129  // Only include 'standard' namespaces to avoid creating unknown numbers of statsd metrics
130  if (
131  $this->title->getNamespace() >= NS_MAIN &&
132  $this->title->getNamespace() <= NS_CATEGORY_TALK
133  ) {
134  $this->stats->increment(
135  'edit.failures.conflict.byNamespaceId.' . $this->title->getNamespace()
136  );
137  }
138  if ( $user ) {
139  $this->incrementStatsByUserEdits( $user->getEditCount(), 'edit.failures.conflict' );
140  }
141  }
142 
147  public function incrementResolvedStats( User $user = null ) {
148  $this->stats->increment( 'edit.failures.conflict.resolved' );
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.resolved.byNamespaceId.' . $this->title->getNamespace()
156  );
157  }
158  if ( $user ) {
160  $user->getEditCount(),
161  'edit.failures.conflict.resolved'
162  );
163  }
164  }
165 
170  protected function incrementStatsByUserEdits( $userEdits, $keyPrefixBase ) {
171  if ( $userEdits === null ) {
172  $userBucket = 'anon';
173  } elseif ( $userEdits > 200 ) {
174  $userBucket = 'over200';
175  } elseif ( $userEdits > 100 ) {
176  $userBucket = 'over100';
177  } elseif ( $userEdits > 10 ) {
178  $userBucket = 'over10';
179  } else {
180  $userBucket = 'under11';
181  }
182  $this->stats->increment( $keyPrefixBase . '.byUserEdits.' . $userBucket );
183  }
184 
188  public function getExplainHeader() {
189  return Html::rawElement(
190  'div',
191  [ 'class' => 'mw-explainconflict' ],
192  $this->out->msg( 'explainconflict', $this->out->msg( $this->submitLabel )->text() )->parse()
193  );
194  }
195 
201  public function getEditConflictMainTextBox( array $customAttribs = [] ) {
202  $builder = new TextboxBuilder();
203  $classes = $builder->getTextboxProtectionCSSClasses( $this->title );
204 
205  $attribs = [ 'tabindex' => 1 ];
206  $attribs += $customAttribs;
207 
208  $attribs = $builder->mergeClassesIntoAttributes( $classes, $attribs );
209 
210  $attribs = $builder->buildTextboxAttribs(
211  'wpTextbox1',
212  $attribs,
213  $this->out->getUser(),
215  );
216 
217  $this->out->addHTML(
218  Html::textarea( 'wpTextbox1', $builder->addNewLineAtEnd( $this->storedversion ), $attribs )
219  );
220  }
221 
228  public function getEditFormHtmlBeforeContent() {
229  return '';
230  }
231 
238  public function getEditFormHtmlAfterContent() {
239  return '';
240  }
241 
246  public function showEditFormTextAfterFooters() {
247  $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourdiff" );
248 
249  $yourContent = $this->toEditContent( $this->yourtext );
250  $storedContent = $this->toEditContent( $this->storedversion );
251  $handler = ContentHandler::getForModelID( $this->contentModel );
252  $diffEngine = $handler->createDifferenceEngine( $this->out );
253 
254  $diffEngine->setContent( $yourContent, $storedContent );
255  $diffEngine->showDiff(
256  $this->out->msg( 'yourtext' )->parse(),
257  $this->out->msg( 'storedversion' )->text()
258  );
259 
260  $this->out->wrapWikiMsg( '<h2>$1</h2>', "yourtext" );
261 
262  $builder = new TextboxBuilder();
263  $attribs = $builder->buildTextboxAttribs(
264  'wpTextbox2',
265  [ 'tabindex' => 6, 'readonly' ],
266  $this->out->getUser(),
268  );
269 
270  $this->out->addHTML(
271  Html::textarea( 'wpTextbox2', $builder->addNewLineAtEnd( $this->yourtext ), $attribs )
272  );
273  }
274 
279  private function toEditContent( $text ) {
281  $text,
282  $this->title,
283  $this->contentModel,
284  $this->contentFormat
285  );
286  }
287 }
MediaWiki\EditPage\TextConflictHelper\getExplainHeader
getExplainHeader()
Definition: TextConflictHelper.php:188
MediaWiki\EditPage\TextConflictHelper\$contentModel
null string $contentModel
Definition: TextConflictHelper.php:51
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:55
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:254
MediaWiki\EditPage\TextConflictHelper\$storedversion
string $storedversion
Definition: TextConflictHelper.php:81
MediaWiki\EditPage\TextConflictHelper\incrementStatsByUserEdits
incrementStatsByUserEdits( $userEdits, $keyPrefixBase)
Definition: TextConflictHelper.php:170
MediaWiki\EditPage\TextConflictHelper\$title
Title $title
Definition: TextConflictHelper.php:46
MediaWiki\EditPage\TextboxBuilder
Helps EditPage build textboxes.
Definition: TextboxBuilder.php:37
MediaWiki\EditPage\TextConflictHelper\$out
OutputPage $out
Definition: TextConflictHelper.php:61
MediaWiki\EditPage\TextConflictHelper\showEditFormTextAfterFooters
showEditFormTextAfterFooters()
Content to go in the edit form after the footers (templates on this page, hidden categories,...
Definition: TextConflictHelper.php:246
MediaWiki\EditPage\TextConflictHelper\setContentFormat
setContentFormat( $contentFormat)
Definition: TextConflictHelper.php:119
NS_MAIN
const NS_MAIN
Definition: Defines.php:60
MediaWiki\EditPage\TextConflictHelper\incrementResolvedStats
incrementResolvedStats(User $user=null)
Record when a user has resolved an edit conflict.
Definition: TextConflictHelper.php:147
MediaWiki\EditPage\TextConflictHelper\toEditContent
toEditContent( $text)
Definition: TextConflictHelper.php:279
MediaWiki\EditPage\TextConflictHelper\incrementConflictStats
incrementConflictStats(User $user=null)
Record a user encountering an edit conflict.
Definition: TextConflictHelper.php:127
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:135
MediaWiki\EditPage\TextConflictHelper\setTextboxes
setTextboxes( $yourtext, $storedversion)
Definition: TextConflictHelper.php:104
MediaWiki\EditPage\TextConflictHelper
Helper for displaying edit conflicts in text content models to users.
Definition: TextConflictHelper.php:41
MediaWiki\EditPage\TextConflictHelper\$yourtext
string $yourtext
Definition: TextConflictHelper.php:76
MediaWiki\EditPage\TextConflictHelper\getEditFormHtmlAfterContent
getEditFormHtmlAfterContent()
Content to go in the edit form after textbox1.
Definition: TextConflictHelper.php:238
Title\getContentModel
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition: Title.php:1047
MediaWiki\EditPage\TextConflictHelper\setContentModel
setContentModel( $contentModel)
Definition: TextConflictHelper.php:112
MediaWiki\EditPage\TextConflictHelper\$submitLabel
string $submitLabel
Message key for submit button's label.
Definition: TextConflictHelper.php:71
Content
Base interface for content objects.
Definition: Content.php:34
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:11
MediaWiki\EditPage\TextConflictHelper\$contentFormat
null string $contentFormat
Definition: TextConflictHelper.php:56
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\EditPage\TextConflictHelper\getEditFormHtmlBeforeContent
getEditFormHtmlBeforeContent()
Content to go in the edit form before textbox1.
Definition: TextConflictHelper.php:228
MediaWiki\EditPage\TextConflictHelper\__construct
__construct(Title $title, OutputPage $out, IBufferingStatsdDataFactory $stats, $submitLabel)
Definition: TextConflictHelper.php:89
MediaWiki\EditPage\TextConflictHelper\getEditConflictMainTextBox
getEditConflictMainTextBox(array $customAttribs=[])
HTML to build the textbox1 on edit conflicts.
Definition: TextConflictHelper.php:201
NS_CATEGORY_TALK
const NS_CATEGORY_TALK
Definition: Defines.php:75
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
MediaWiki\EditPage\TextConflictHelper\$stats
IBufferingStatsdDataFactory $stats
Definition: TextConflictHelper.php:66
MediaWiki\EditPage
Definition: TextboxBuilder.php:25