MediaWiki  1.33.0
SpecialMergeHistory.php
Go to the documentation of this file.
1 <?php
32  protected $mAction;
33 
35  protected $mTarget;
36 
38  protected $mDest;
39 
41  protected $mTimestamp;
42 
44  protected $mTargetID;
45 
47  protected $mDestID;
48 
50  protected $mComment;
51 
53  protected $mMerge;
54 
56  protected $mSubmitted;
57 
59  protected $mTargetObj;
60 
62  protected $mDestObj;
63 
65  public $prevId;
66 
67  public function __construct() {
68  parent::__construct( 'MergeHistory', 'mergehistory' );
69  }
70 
71  public function doesWrites() {
72  return true;
73  }
74 
78  private function loadRequestParams() {
79  $request = $this->getRequest();
80  $this->mAction = $request->getVal( 'action' );
81  $this->mTarget = $request->getVal( 'target' );
82  $this->mDest = $request->getVal( 'dest' );
83  $this->mSubmitted = $request->getBool( 'submitted' );
84 
85  $this->mTargetID = intval( $request->getVal( 'targetID' ) );
86  $this->mDestID = intval( $request->getVal( 'destID' ) );
87  $this->mTimestamp = $request->getVal( 'mergepoint' );
88  if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp ) ) {
89  $this->mTimestamp = '';
90  }
91  $this->mComment = $request->getText( 'wpComment' );
92 
93  $this->mMerge = $request->wasPosted()
94  && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
95 
96  // target page
97  if ( $this->mSubmitted ) {
98  $this->mTargetObj = Title::newFromText( $this->mTarget );
99  $this->mDestObj = Title::newFromText( $this->mDest );
100  } else {
101  $this->mTargetObj = null;
102  $this->mDestObj = null;
103  }
104  }
105 
106  public function execute( $par ) {
107  $this->useTransactionalTimeLimit();
108 
109  $this->checkPermissions();
110  $this->checkReadOnly();
111 
112  $this->loadRequestParams();
113 
114  $this->setHeaders();
115  $this->outputHeader();
116 
117  if ( $this->mTargetID && $this->mDestID && $this->mAction == 'submit' && $this->mMerge ) {
118  $this->merge();
119 
120  return;
121  }
122 
123  if ( !$this->mSubmitted ) {
124  $this->showMergeForm();
125 
126  return;
127  }
128 
129  $errors = [];
130  if ( !$this->mTargetObj instanceof Title ) {
131  $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
132  } elseif ( !$this->mTargetObj->exists() ) {
133  $errors[] = $this->msg( 'mergehistory-no-source',
134  wfEscapeWikiText( $this->mTargetObj->getPrefixedText() )
135  )->parseAsBlock();
136  }
137 
138  if ( !$this->mDestObj instanceof Title ) {
139  $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
140  } elseif ( !$this->mDestObj->exists() ) {
141  $errors[] = $this->msg( 'mergehistory-no-destination',
142  wfEscapeWikiText( $this->mDestObj->getPrefixedText() )
143  )->parseAsBlock();
144  }
145 
146  if ( $this->mTargetObj && $this->mDestObj && $this->mTargetObj->equals( $this->mDestObj ) ) {
147  $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
148  }
149 
150  if ( count( $errors ) ) {
151  $this->showMergeForm();
152  $this->getOutput()->addHTML( implode( "\n", $errors ) );
153  } else {
154  $this->showHistory();
155  }
156  }
157 
158  function showMergeForm() {
159  $out = $this->getOutput();
160  $out->addWikiMsg( 'mergehistory-header' );
161 
162  $out->addHTML(
163  Xml::openElement( 'form', [
164  'method' => 'get',
165  'action' => wfScript() ] ) .
166  '<fieldset>' .
167  Xml::element( 'legend', [],
168  $this->msg( 'mergehistory-box' )->text() ) .
169  Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
170  Html::hidden( 'submitted', '1' ) .
171  Html::hidden( 'mergepoint', $this->mTimestamp ) .
172  Xml::openElement( 'table' ) .
173  '<tr>
174  <td>' . Xml::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
175  <td>' . Xml::input( 'target', 30, $this->mTarget, [ 'id' => 'target' ] ) . '</td>
176  </tr><tr>
177  <td>' . Xml::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
178  <td>' . Xml::input( 'dest', 30, $this->mDest, [ 'id' => 'dest' ] ) . '</td>
179  </tr><tr><td>' .
180  Xml::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
181  '</td></tr>' .
182  Xml::closeElement( 'table' ) .
183  '</fieldset>' .
184  '</form>'
185  );
186 
187  $this->addHelpLink( 'Help:Merge history' );
188  }
189 
190  private function showHistory() {
191  $this->showMergeForm();
192 
193  # List all stored revisions
194  $revisions = new MergeHistoryPager(
195  $this, [], $this->mTargetObj, $this->mDestObj
196  );
197  $haveRevisions = $revisions && $revisions->getNumRows() > 0;
198 
199  $out = $this->getOutput();
200  $titleObj = $this->getPageTitle();
201  $action = $titleObj->getLocalURL( [ 'action' => 'submit' ] );
202  # Start the form here
203  $top = Xml::openElement(
204  'form',
205  [
206  'method' => 'post',
207  'action' => $action,
208  'id' => 'merge'
209  ]
210  );
211  $out->addHTML( $top );
212 
213  if ( $haveRevisions ) {
214  # Format the user-visible controls (comment field, submission button)
215  # in a nice little table
216  $table =
217  Xml::openElement( 'fieldset' ) .
218  $this->msg( 'mergehistory-merge', $this->mTargetObj->getPrefixedText(),
219  $this->mDestObj->getPrefixedText() )->parse() .
220  Xml::openElement( 'table', [ 'id' => 'mw-mergehistory-table' ] ) .
221  '<tr>
222  <td class="mw-label">' .
223  Xml::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
224  '</td>
225  <td class="mw-input">' .
226  Xml::input( 'wpComment', 50, $this->mComment, [ 'id' => 'wpComment' ] ) .
227  "</td>
228  </tr>
229  <tr>
230  <td>\u{00A0}</td>
231  <td class=\"mw-submit\">" .
233  $this->msg( 'mergehistory-submit' )->text(),
234  [ 'name' => 'merge', 'id' => 'mw-merge-submit' ]
235  ) .
236  '</td>
237  </tr>' .
238  Xml::closeElement( 'table' ) .
239  Xml::closeElement( 'fieldset' );
240 
241  $out->addHTML( $table );
242  }
243 
244  $out->addHTML(
245  '<h2 id="mw-mergehistory">' .
246  $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
247  );
248 
249  if ( $haveRevisions ) {
250  $out->addHTML( $revisions->getNavigationBar() );
251  $out->addHTML( '<ul>' );
252  $out->addHTML( $revisions->getBody() );
253  $out->addHTML( '</ul>' );
254  $out->addHTML( $revisions->getNavigationBar() );
255  } else {
256  $out->addWikiMsg( 'mergehistory-empty' );
257  }
258 
259  # Show relevant lines from the merge log:
260  $mergeLogPage = new LogPage( 'merge' );
261  $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
262  LogEventsList::showLogExtract( $out, 'merge', $this->mTargetObj );
263 
264  # When we submit, go by page ID to avoid some nasty but unlikely collisions.
265  # Such would happen if a page was renamed after the form loaded, but before submit
266  $misc = Html::hidden( 'targetID', $this->mTargetObj->getArticleID() );
267  $misc .= Html::hidden( 'destID', $this->mDestObj->getArticleID() );
268  $misc .= Html::hidden( 'target', $this->mTarget );
269  $misc .= Html::hidden( 'dest', $this->mDest );
270  $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
271  $misc .= Xml::closeElement( 'form' );
272  $out->addHTML( $misc );
273 
274  return true;
275  }
276 
277  function formatRevisionRow( $row ) {
278  $rev = new Revision( $row );
279 
280  $linkRenderer = $this->getLinkRenderer();
281 
282  $stxt = '';
283  $last = $this->msg( 'last' )->escaped();
284 
285  $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
286  $checkBox = Xml::radio( 'mergepoint', $ts, ( $this->mTimestamp === $ts ) );
287 
288  $user = $this->getUser();
289 
290  $pageLink = $linkRenderer->makeKnownLink(
291  $rev->getTitle(),
292  $this->getLanguage()->userTimeAndDate( $ts, $user ),
293  [],
294  [ 'oldid' => $rev->getId() ]
295  );
296  if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
297  $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
298  }
299 
300  # Last link
301  if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
302  $last = $this->msg( 'last' )->escaped();
303  } elseif ( isset( $this->prevId[$row->rev_id] ) ) {
304  $last = $linkRenderer->makeKnownLink(
305  $rev->getTitle(),
306  $this->msg( 'last' )->text(),
307  [],
308  [
309  'diff' => $row->rev_id,
310  'oldid' => $this->prevId[$row->rev_id]
311  ]
312  );
313  }
314 
315  $userLink = Linker::revUserTools( $rev );
316 
317  $size = $row->rev_len;
318  if ( !is_null( $size ) ) {
319  $stxt = Linker::formatRevisionSize( $size );
320  }
321  $comment = Linker::revComment( $rev );
322 
323  return Html::rawElement( 'li', [],
324  $this->msg( 'mergehistory-revisionrow' )
325  ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
326  }
327 
340  function merge() {
341  # Get the titles directly from the IDs, in case the target page params
342  # were spoofed. The queries are done based on the IDs, so it's best to
343  # keep it consistent...
344  $targetTitle = Title::newFromID( $this->mTargetID );
345  $destTitle = Title::newFromID( $this->mDestID );
346  if ( is_null( $targetTitle ) || is_null( $destTitle ) ) {
347  return false; // validate these
348  }
349  if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
350  return false;
351  }
352 
353  // MergeHistory object
354  $mh = new MergeHistory( $targetTitle, $destTitle, $this->mTimestamp );
355 
356  // Merge!
357  $mergeStatus = $mh->merge( $this->getUser(), $this->mComment );
358  if ( !$mergeStatus->isOK() ) {
359  // Failed merge
360  $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
361  return false;
362  }
363 
364  $linkRenderer = $this->getLinkRenderer();
365 
366  $targetLink = $linkRenderer->makeLink(
367  $targetTitle,
368  null,
369  [],
370  [ 'redirect' => 'no' ]
371  );
372 
373  $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
374  ->rawParams( $targetLink )
375  ->params( $destTitle->getPrefixedText() )
376  ->numParams( $mh->getMergedRevisionCount() )
377  );
378 
379  return true;
380  }
381 
382  protected function getGroupName() {
383  return 'pagetools';
384  }
385 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:678
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:796
SpecialMergeHistory\$mTarget
string $mTarget
Definition: SpecialMergeHistory.php:35
SpecialMergeHistory\merge
merge()
Actually attempt the history move.
Definition: SpecialMergeHistory.php:340
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:725
Xml\label
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition: Xml.php:358
captcha-old.count
count
Definition: captcha-old.py:249
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1912
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
SpecialMergeHistory\showMergeForm
showMergeForm()
Definition: SpecialMergeHistory.php:158
Xml\radio
static radio( $name, $value, $checked=false, $attribs=[])
Convenience function to build an HTML radio button.
Definition: Xml.php:341
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:309
SpecialMergeHistory\showHistory
showHistory()
Definition: SpecialMergeHistory.php:190
Linker\revComment
static revComment(Revision $rev, $local=false, $isPublic=false, $useParentheses=true)
Wrap and format the given revision's comment block, if the current user is allowed to view it.
Definition: Linker.php:1510
SpecialPage\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: SpecialPage.php:898
$last
$last
Definition: profileinfo.php:416
SpecialMergeHistory\__construct
__construct()
Definition: SpecialMergeHistory.php:67
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:755
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
SpecialMergeHistory\$mDestID
int $mDestID
Definition: SpecialMergeHistory.php:47
SpecialMergeHistory\$mTargetID
int $mTargetID
Definition: SpecialMergeHistory.php:44
Revision
Definition: Revision.php:40
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:832
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2714
SpecialMergeHistory\$mDestObj
Title $mDestObj
Definition: SpecialMergeHistory.php:62
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
SpecialMergeHistory\formatRevisionRow
formatRevisionRow( $row)
Definition: SpecialMergeHistory.php:277
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
SpecialMergeHistory\$mComment
string $mComment
Definition: SpecialMergeHistory.php:50
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:531
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:735
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Definition: LogEventsList.php:627
Linker\revUserTools
static revUserTools( $rev, $isPublic=false, $useParentheses=true)
Generate a user tool link cluster if the current user is allowed to view it.
Definition: Linker.php:1086
SpecialMergeHistory\$mTimestamp
string $mTimestamp
Definition: SpecialMergeHistory.php:41
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2636
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
SpecialMergeHistory\loadRequestParams
loadRequestParams()
Definition: SpecialMergeHistory.php:78
SpecialMergeHistory\$mSubmitted
bool $mSubmitted
Was submitted?
Definition: SpecialMergeHistory.php:56
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:715
Linker\formatRevisionSize
static formatRevisionSize( $size)
Definition: Linker.php:1535
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1577
MergeHistory
Handles the backend logic of merging the histories of two pages.
Definition: MergeHistory.php:33
SpecialMergeHistory\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialMergeHistory.php:382
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:908
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
Title
Represents a title within MediaWiki.
Definition: Title.php:40
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:117
SpecialMergeHistory\$mAction
string $mAction
Definition: SpecialMergeHistory.php:32
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1769
SpecialMergeHistory\$mMerge
bool $mMerge
Was posted?
Definition: SpecialMergeHistory.php:53
Xml\input
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition: Xml.php:274
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:322
SpecialPage\$linkRenderer
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:66
MergeHistoryPager
Definition: MergeHistoryPager.php:25
SpecialMergeHistory\$prevId
int[] $prevId
Definition: SpecialMergeHistory.php:65
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:457
SpecialMergeHistory
Special page allowing users with the appropriate permissions to merge article histories,...
Definition: SpecialMergeHistory.php:30
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:633
Revision\DELETED_TEXT
const DELETED_TEXT
Definition: Revision.php:46
SpecialMergeHistory\$mTargetObj
Title $mTargetObj
Definition: SpecialMergeHistory.php:59
SpecialMergeHistory\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialMergeHistory.php:71
SpecialMergeHistory\$mDest
string $mDest
Definition: SpecialMergeHistory.php:38
Xml\submitButton
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition: Xml.php:459
SpecialMergeHistory\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialMergeHistory.php:106