Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / includes / diff / DifferenceEngine.php
1 <?php
2 /**
3 * User interface for the difference engine.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup DifferenceEngine
22 */
23
24 /** @deprecated use class constant instead */
25 define( 'MW_DIFF_VERSION', '1.11a' );
26
27 /**
28 * @todo document
29 * @ingroup DifferenceEngine
30 */
31 class DifferenceEngine extends ContextSource {
32 /**
33 * Constant to indicate diff cache compatibility.
34 * Bump this when changing the diff formatting in a way that
35 * fixes important bugs or such to force cached diff views to
36 * clear.
37 */
38 const DIFF_VERSION = MW_DIFF_VERSION;
39
40 /** @var int */
41 public $mOldid;
42
43 /** @var int */
44 public $mNewid;
45
46 private $mOldTags;
47 private $mNewTags;
48
49 /** @var Content */
50 public $mOldContent;
51
52 /** @var Content */
53 public $mNewContent;
54
55 /** @var Language */
56 protected $mDiffLang;
57
58 /** @var Title */
59 public $mOldPage;
60
61 /** @var Title */
62 public $mNewPage;
63
64 /** @var Revision */
65 public $mOldRev;
66
67 /** @var Revision */
68 public $mNewRev;
69
70 /** @var bool Have the revisions IDs been loaded */
71 private $mRevisionsIdsLoaded = false;
72
73 /** @var bool Have the revisions been loaded */
74 public $mRevisionsLoaded = false;
75
76 /** @var int How many text blobs have been loaded, 0, 1 or 2? */
77 public $mTextLoaded = 0;
78
79 /** @var bool Was the diff fetched from cache? */
80 public $mCacheHit = false;
81
82 /**
83 * Set this to true to add debug info to the HTML output.
84 * Warning: this may cause RSS readers to spuriously mark articles as "new"
85 * (T22601)
86 */
87 public $enableDebugComment = false;
88
89 /** @var bool If true, line X is not displayed when X is 1, for example
90 * to increase readability and conserve space with many small diffs.
91 */
92 protected $mReducedLineNumbers = false;
93
94 /** @var string Link to action=markpatrolled */
95 protected $mMarkPatrolledLink = null;
96
97 /** @var bool Show rev_deleted content if allowed */
98 protected $unhide = false;
99
100 /** @var bool Refresh the diff cache */
101 protected $mRefreshCache = false;
102
103 /**#@-*/
104
105 /**
106 * Constructor
107 * @param IContextSource $context Context to use, anything else will be ignored
108 * @param int $old Old ID we want to show and diff with.
109 * @param string|int $new Either revision ID or 'prev' or 'next'. Default: 0.
110 * @param int $rcid Deprecated, no longer used!
111 * @param bool $refreshCache If set, refreshes the diff cache
112 * @param bool $unhide If set, allow viewing deleted revs
113 */
114 public function __construct( $context = null, $old = 0, $new = 0, $rcid = 0,
115 $refreshCache = false, $unhide = false
116 ) {
117 if ( $context instanceof IContextSource ) {
118 $this->setContext( $context );
119 }
120
121 wfDebug( "DifferenceEngine old '$old' new '$new' rcid '$rcid'\n" );
122
123 $this->mOldid = $old;
124 $this->mNewid = $new;
125 $this->mRefreshCache = $refreshCache;
126 $this->unhide = $unhide;
127 }
128
129 /**
130 * @param bool $value
131 */
132 public function setReducedLineNumbers( $value = true ) {
133 $this->mReducedLineNumbers = $value;
134 }
135
136 /**
137 * @return Language
138 */
139 public function getDiffLang() {
140 if ( $this->mDiffLang === null ) {
141 # Default language in which the diff text is written.
142 $this->mDiffLang = $this->getTitle()->getPageLanguage();
143 }
144
145 return $this->mDiffLang;
146 }
147
148 /**
149 * @return bool
150 */
151 public function wasCacheHit() {
152 return $this->mCacheHit;
153 }
154
155 /**
156 * @return int
157 */
158 public function getOldid() {
159 $this->loadRevisionIds();
160
161 return $this->mOldid;
162 }
163
164 /**
165 * @return bool|int
166 */
167 public function getNewid() {
168 $this->loadRevisionIds();
169
170 return $this->mNewid;
171 }
172
173 /**
174 * Look up a special:Undelete link to the given deleted revision id,
175 * as a workaround for being unable to load deleted diffs in currently.
176 *
177 * @param int $id Revision ID
178 *
179 * @return string|bool Link HTML or false
180 */
181 public function deletedLink( $id ) {
182 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
183 $dbr = wfGetDB( DB_REPLICA );
184 $row = $dbr->selectRow( 'archive', '*',
185 [ 'ar_rev_id' => $id ],
186 __METHOD__ );
187 if ( $row ) {
188 $rev = Revision::newFromArchiveRow( $row );
189 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
190
191 return SpecialPage::getTitleFor( 'Undelete' )->getFullURL( [
192 'target' => $title->getPrefixedText(),
193 'timestamp' => $rev->getTimestamp()
194 ] );
195 }
196 }
197
198 return false;
199 }
200
201 /**
202 * Build a wikitext link toward a deleted revision, if viewable.
203 *
204 * @param int $id Revision ID
205 *
206 * @return string Wikitext fragment
207 */
208 public function deletedIdMarker( $id ) {
209 $link = $this->deletedLink( $id );
210 if ( $link ) {
211 return "[$link $id]";
212 } else {
213 return (string)$id;
214 }
215 }
216
217 private function showMissingRevision() {
218 $out = $this->getOutput();
219
220 $missing = [];
221 if ( $this->mOldRev === null ||
222 ( $this->mOldRev && $this->mOldContent === null )
223 ) {
224 $missing[] = $this->deletedIdMarker( $this->mOldid );
225 }
226 if ( $this->mNewRev === null ||
227 ( $this->mNewRev && $this->mNewContent === null )
228 ) {
229 $missing[] = $this->deletedIdMarker( $this->mNewid );
230 }
231
232 $out->setPageTitle( $this->msg( 'errorpagetitle' ) );
233 $msg = $this->msg( 'difference-missing-revision' )
234 ->params( $this->getLanguage()->listToText( $missing ) )
235 ->numParams( count( $missing ) )
236 ->parseAsBlock();
237 $out->addHTML( $msg );
238 }
239
240 public function showDiffPage( $diffOnly = false ) {
241 # Allow frames except in certain special cases
242 $out = $this->getOutput();
243 $out->allowClickjacking();
244 $out->setRobotPolicy( 'noindex,nofollow' );
245
246 // Allow extensions to add any extra output here
247 Hooks::run( 'DifferenceEngineShowDiffPage', [ $out ] );
248
249 if ( !$this->loadRevisionData() ) {
250 if ( Hooks::run( 'DifferenceEngineShowDiffPageMaybeShowMissingRevision', [ $this ] ) ) {
251 $this->showMissingRevision();
252 }
253 return;
254 }
255
256 $user = $this->getUser();
257 $permErrors = $this->mNewPage->getUserPermissionsErrors( 'read', $user );
258 if ( $this->mOldPage ) { # mOldPage might not be set, see below.
259 $permErrors = wfMergeErrorArrays( $permErrors,
260 $this->mOldPage->getUserPermissionsErrors( 'read', $user ) );
261 }
262 if ( count( $permErrors ) ) {
263 throw new PermissionsError( 'read', $permErrors );
264 }
265
266 $rollback = '';
267
268 $query = [];
269 # Carry over 'diffonly' param via navigation links
270 if ( $diffOnly != $user->getBoolOption( 'diffonly' ) ) {
271 $query['diffonly'] = $diffOnly;
272 }
273 # Cascade unhide param in links for easy deletion browsing
274 if ( $this->unhide ) {
275 $query['unhide'] = 1;
276 }
277
278 # Check if one of the revisions is deleted/suppressed
279 $deleted = $suppressed = false;
280 $allowed = $this->mNewRev->userCan( Revision::DELETED_TEXT, $user );
281
282 $revisionTools = [];
283
284 # mOldRev is false if the difference engine is called with a "vague" query for
285 # a diff between a version V and its previous version V' AND the version V
286 # is the first version of that article. In that case, V' does not exist.
287 if ( $this->mOldRev === false ) {
288 $out->setPageTitle( $this->msg( 'difference-title', $this->mNewPage->getPrefixedText() ) );
289 $samePage = true;
290 $oldHeader = '';
291 // Allow extensions to change the $oldHeader variable
292 Hooks::run( 'DifferenceEngineOldHeaderNoOldRev', [ &$oldHeader ] );
293 } else {
294 Hooks::run( 'DiffViewHeader', [ $this, $this->mOldRev, $this->mNewRev ] );
295
296 if ( $this->mNewPage->equals( $this->mOldPage ) ) {
297 $out->setPageTitle( $this->msg( 'difference-title', $this->mNewPage->getPrefixedText() ) );
298 $samePage = true;
299 } else {
300 $out->setPageTitle( $this->msg( 'difference-title-multipage',
301 $this->mOldPage->getPrefixedText(), $this->mNewPage->getPrefixedText() ) );
302 $out->addSubtitle( $this->msg( 'difference-multipage' ) );
303 $samePage = false;
304 }
305
306 if ( $samePage && $this->mNewPage->quickUserCan( 'edit', $user ) ) {
307 if ( $this->mNewRev->isCurrent() && $this->mNewPage->userCan( 'rollback', $user ) ) {
308 $rollbackLink = Linker::generateRollback( $this->mNewRev, $this->getContext() );
309 if ( $rollbackLink ) {
310 $out->preventClickjacking();
311 $rollback = '&#160;&#160;&#160;' . $rollbackLink;
312 }
313 }
314
315 if ( !$this->mOldRev->isDeleted( Revision::DELETED_TEXT ) &&
316 !$this->mNewRev->isDeleted( Revision::DELETED_TEXT )
317 ) {
318 $undoLink = Html::element( 'a', [
319 'href' => $this->mNewPage->getLocalURL( [
320 'action' => 'edit',
321 'undoafter' => $this->mOldid,
322 'undo' => $this->mNewid
323 ] ),
324 'title' => Linker::titleAttrib( 'undo' ),
325 ],
326 $this->msg( 'editundo' )->text()
327 );
328 $revisionTools['mw-diff-undo'] = $undoLink;
329 }
330 }
331
332 # Make "previous revision link"
333 if ( $samePage && $this->mOldRev->getPrevious() ) {
334 $prevlink = Linker::linkKnown(
335 $this->mOldPage,
336 $this->msg( 'previousdiff' )->escaped(),
337 [ 'id' => 'differences-prevlink' ],
338 [ 'diff' => 'prev', 'oldid' => $this->mOldid ] + $query
339 );
340 } else {
341 $prevlink = '&#160;';
342 }
343
344 if ( $this->mOldRev->isMinor() ) {
345 $oldminor = ChangesList::flag( 'minor' );
346 } else {
347 $oldminor = '';
348 }
349
350 $ldel = $this->revisionDeleteLink( $this->mOldRev );
351 $oldRevisionHeader = $this->getRevisionHeader( $this->mOldRev, 'complete' );
352 $oldChangeTags = ChangeTags::formatSummaryRow( $this->mOldTags, 'diff', $this->getContext() );
353
354 $oldHeader = '<div id="mw-diff-otitle1"><strong>' . $oldRevisionHeader . '</strong></div>' .
355 '<div id="mw-diff-otitle2">' .
356 Linker::revUserTools( $this->mOldRev, !$this->unhide ) . '</div>' .
357 '<div id="mw-diff-otitle3">' . $oldminor .
358 Linker::revComment( $this->mOldRev, !$diffOnly, !$this->unhide ) . $ldel . '</div>' .
359 '<div id="mw-diff-otitle5">' . $oldChangeTags[0] . '</div>' .
360 '<div id="mw-diff-otitle4">' . $prevlink . '</div>';
361
362 // Allow extensions to change the $oldHeader variable
363 Hooks::run( 'DifferenceEngineOldHeader', [ $this, &$oldHeader, $prevlink, $oldminor,
364 $diffOnly, $ldel, $this->unhide ] );
365
366 if ( $this->mOldRev->isDeleted( Revision::DELETED_TEXT ) ) {
367 $deleted = true; // old revisions text is hidden
368 if ( $this->mOldRev->isDeleted( Revision::DELETED_RESTRICTED ) ) {
369 $suppressed = true; // also suppressed
370 }
371 }
372
373 # Check if this user can see the revisions
374 if ( !$this->mOldRev->userCan( Revision::DELETED_TEXT, $user ) ) {
375 $allowed = false;
376 }
377 }
378
379 # Make "next revision link"
380 # Skip next link on the top revision
381 if ( $samePage && !$this->mNewRev->isCurrent() ) {
382 $nextlink = Linker::linkKnown(
383 $this->mNewPage,
384 $this->msg( 'nextdiff' )->escaped(),
385 [ 'id' => 'differences-nextlink' ],
386 [ 'diff' => 'next', 'oldid' => $this->mNewid ] + $query
387 );
388 } else {
389 $nextlink = '&#160;';
390 }
391
392 if ( $this->mNewRev->isMinor() ) {
393 $newminor = ChangesList::flag( 'minor' );
394 } else {
395 $newminor = '';
396 }
397
398 # Handle RevisionDelete links...
399 $rdel = $this->revisionDeleteLink( $this->mNewRev );
400
401 # Allow extensions to define their own revision tools
402 Hooks::run( 'DiffRevisionTools',
403 [ $this->mNewRev, &$revisionTools, $this->mOldRev, $user ] );
404 $formattedRevisionTools = [];
405 // Put each one in parentheses (poor man's button)
406 foreach ( $revisionTools as $key => $tool ) {
407 $toolClass = is_string( $key ) ? $key : 'mw-diff-tool';
408 $element = Html::rawElement(
409 'span',
410 [ 'class' => $toolClass ],
411 $this->msg( 'parentheses' )->rawParams( $tool )->escaped()
412 );
413 $formattedRevisionTools[] = $element;
414 }
415 $newRevisionHeader = $this->getRevisionHeader( $this->mNewRev, 'complete' ) .
416 ' ' . implode( ' ', $formattedRevisionTools );
417 $newChangeTags = ChangeTags::formatSummaryRow( $this->mNewTags, 'diff', $this->getContext() );
418
419 $newHeader = '<div id="mw-diff-ntitle1"><strong>' . $newRevisionHeader . '</strong></div>' .
420 '<div id="mw-diff-ntitle2">' . Linker::revUserTools( $this->mNewRev, !$this->unhide ) .
421 " $rollback</div>" .
422 '<div id="mw-diff-ntitle3">' . $newminor .
423 Linker::revComment( $this->mNewRev, !$diffOnly, !$this->unhide ) . $rdel . '</div>' .
424 '<div id="mw-diff-ntitle5">' . $newChangeTags[0] . '</div>' .
425 '<div id="mw-diff-ntitle4">' . $nextlink . $this->markPatrolledLink() . '</div>';
426
427 // Allow extensions to change the $newHeader variable
428 Hooks::run( 'DifferenceEngineNewHeader', [ $this, &$newHeader, $formattedRevisionTools,
429 $nextlink, $rollback, $newminor, $diffOnly, $rdel, $this->unhide ] );
430
431 if ( $this->mNewRev->isDeleted( Revision::DELETED_TEXT ) ) {
432 $deleted = true; // new revisions text is hidden
433 if ( $this->mNewRev->isDeleted( Revision::DELETED_RESTRICTED ) ) {
434 $suppressed = true; // also suppressed
435 }
436 }
437
438 # If the diff cannot be shown due to a deleted revision, then output
439 # the diff header and links to unhide (if available)...
440 if ( $deleted && ( !$this->unhide || !$allowed ) ) {
441 $this->showDiffStyle();
442 $multi = $this->getMultiNotice();
443 $out->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) );
444 if ( !$allowed ) {
445 $msg = $suppressed ? 'rev-suppressed-no-diff' : 'rev-deleted-no-diff';
446 # Give explanation for why revision is not visible
447 $out->wrapWikiMsg( "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n",
448 [ $msg ] );
449 } else {
450 # Give explanation and add a link to view the diff...
451 $query = $this->getRequest()->appendQueryValue( 'unhide', '1' );
452 $link = $this->getTitle()->getFullURL( $query );
453 $msg = $suppressed ? 'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff';
454 $out->wrapWikiMsg(
455 "<div id='mw-$msg' class='mw-warning plainlinks'>\n$1\n</div>\n",
456 [ $msg, $link ]
457 );
458 }
459 # Otherwise, output a regular diff...
460 } else {
461 # Add deletion notice if the user is viewing deleted content
462 $notice = '';
463 if ( $deleted ) {
464 $msg = $suppressed ? 'rev-suppressed-diff-view' : 'rev-deleted-diff-view';
465 $notice = "<div id='mw-$msg' class='mw-warning plainlinks'>\n" .
466 $this->msg( $msg )->parse() .
467 "</div>\n";
468 }
469 $this->showDiff( $oldHeader, $newHeader, $notice );
470 if ( !$diffOnly ) {
471 $this->renderNewRevision();
472 }
473 }
474 }
475
476 /**
477 * Build a link to mark a change as patrolled.
478 *
479 * Returns empty string if there's either no revision to patrol or the user is not allowed to.
480 * Side effect: When the patrol link is build, this method will call
481 * OutputPage::preventClickjacking() and load mediawiki.page.patrol.ajax.
482 *
483 * @return string HTML or empty string
484 */
485 public function markPatrolledLink() {
486 if ( $this->mMarkPatrolledLink === null ) {
487 $linkInfo = $this->getMarkPatrolledLinkInfo();
488 // If false, there is no patrol link needed/allowed
489 if ( !$linkInfo ) {
490 $this->mMarkPatrolledLink = '';
491 } else {
492 $this->mMarkPatrolledLink = ' <span class="patrollink" data-mw="interface">[' .
493 Linker::linkKnown(
494 $this->mNewPage,
495 $this->msg( 'markaspatrolleddiff' )->escaped(),
496 [],
497 [
498 'action' => 'markpatrolled',
499 'rcid' => $linkInfo['rcid'],
500 ]
501 ) . ']</span>';
502 // Allow extensions to change the markpatrolled link
503 Hooks::run( 'DifferenceEngineMarkPatrolledLink', [ $this,
504 &$this->mMarkPatrolledLink, $linkInfo['rcid'] ] );
505 }
506 }
507 return $this->mMarkPatrolledLink;
508 }
509
510 /**
511 * Returns an array of meta data needed to build a "mark as patrolled" link and
512 * adds the mediawiki.page.patrol.ajax to the output.
513 *
514 * @return array|false An array of meta data for a patrol link (rcid only)
515 * or false if no link is needed
516 */
517 protected function getMarkPatrolledLinkInfo() {
518 global $wgUseRCPatrol, $wgEnableAPI, $wgEnableWriteAPI;
519
520 $user = $this->getUser();
521
522 // Prepare a change patrol link, if applicable
523 if (
524 // Is patrolling enabled and the user allowed to?
525 $wgUseRCPatrol && $this->mNewPage->quickUserCan( 'patrol', $user ) &&
526 // Only do this if the revision isn't more than 6 hours older
527 // than the Max RC age (6h because the RC might not be cleaned out regularly)
528 RecentChange::isInRCLifespan( $this->mNewRev->getTimestamp(), 21600 )
529 ) {
530 // Look for an unpatrolled change corresponding to this diff
531 $db = wfGetDB( DB_REPLICA );
532 $change = RecentChange::newFromConds(
533 [
534 'rc_timestamp' => $db->timestamp( $this->mNewRev->getTimestamp() ),
535 'rc_this_oldid' => $this->mNewid,
536 'rc_patrolled' => 0
537 ],
538 __METHOD__
539 );
540
541 if ( $change && !$change->getPerformer()->equals( $user ) ) {
542 $rcid = $change->getAttribute( 'rc_id' );
543 } else {
544 // None found or the page has been created by the current user.
545 // If the user could patrol this it already would be patrolled
546 $rcid = 0;
547 }
548
549 // Allow extensions to possibly change the rcid here
550 // For example the rcid might be set to zero due to the user
551 // being the same as the performer of the change but an extension
552 // might still want to show it under certain conditions
553 Hooks::run( 'DifferenceEngineMarkPatrolledRCID', [ &$rcid, $this, $change, $user ] );
554
555 // Build the link
556 if ( $rcid ) {
557 $this->getOutput()->preventClickjacking();
558 if ( $wgEnableAPI && $wgEnableWriteAPI
559 && $user->isAllowed( 'writeapi' )
560 ) {
561 $this->getOutput()->addModules( 'mediawiki.page.patrol.ajax' );
562 }
563
564 return [
565 'rcid' => $rcid,
566 ];
567 }
568 }
569
570 // No mark as patrolled link applicable
571 return false;
572 }
573
574 /**
575 * @param Revision $rev
576 *
577 * @return string
578 */
579 protected function revisionDeleteLink( $rev ) {
580 $link = Linker::getRevDeleteLink( $this->getUser(), $rev, $rev->getTitle() );
581 if ( $link !== '' ) {
582 $link = '&#160;&#160;&#160;' . $link . ' ';
583 }
584
585 return $link;
586 }
587
588 /**
589 * Show the new revision of the page.
590 */
591 public function renderNewRevision() {
592 $out = $this->getOutput();
593 $revHeader = $this->getRevisionHeader( $this->mNewRev );
594 # Add "current version as of X" title
595 $out->addHTML( "<hr class='diff-hr' id='mw-oldid' />
596 <h2 class='diff-currentversion-title'>{$revHeader}</h2>\n" );
597 # Page content may be handled by a hooked call instead...
598 # @codingStandardsIgnoreStart Ignoring long lines.
599 if ( Hooks::run( 'ArticleContentOnDiff', [ $this, $out ] ) ) {
600 $this->loadNewText();
601 $out->setRevisionId( $this->mNewid );
602 $out->setRevisionTimestamp( $this->mNewRev->getTimestamp() );
603 $out->setArticleFlag( true );
604
605 if ( !Hooks::run( 'ArticleContentViewCustom', [ $this->mNewContent, $this->mNewPage, $out ] ) ) {
606 // Handled by extension
607 } else {
608 // Normal page
609 if ( $this->getTitle()->equals( $this->mNewPage ) ) {
610 // If the Title stored in the context is the same as the one
611 // of the new revision, we can use its associated WikiPage
612 // object.
613 $wikiPage = $this->getWikiPage();
614 } else {
615 // Otherwise we need to create our own WikiPage object
616 $wikiPage = WikiPage::factory( $this->mNewPage );
617 }
618
619 $parserOutput = $this->getParserOutput( $wikiPage, $this->mNewRev );
620
621 # WikiPage::getParserOutput() should not return false, but just in case
622 if ( $parserOutput ) {
623 // Allow extensions to change parser output here
624 if ( Hooks::run( 'DifferenceEngineRenderRevisionAddParserOutput', [ $this, $out, $parserOutput, $wikiPage ] ) ) {
625 $out->addParserOutput( $parserOutput );
626 }
627 }
628 }
629 }
630 # @codingStandardsIgnoreEnd
631
632 // Allow extensions to optionally not show the final patrolled link
633 if ( Hooks::run( 'DifferenceEngineRenderRevisionShowFinalPatrolLink' ) ) {
634 # Add redundant patrol link on bottom...
635 $out->addHTML( $this->markPatrolledLink() );
636 }
637 }
638
639 protected function getParserOutput( WikiPage $page, Revision $rev ) {
640 $parserOptions = $page->makeParserOptions( $this->getContext() );
641
642 if ( !$rev->isCurrent() || !$rev->getTitle()->quickUserCan( 'edit', $this->getUser() ) ) {
643 $parserOptions->setEditSection( false );
644 }
645
646 $parserOutput = $page->getParserOutput( $parserOptions, $rev->getId() );
647
648 return $parserOutput;
649 }
650
651 /**
652 * Get the diff text, send it to the OutputPage object
653 * Returns false if the diff could not be generated, otherwise returns true
654 *
655 * @param string|bool $otitle Header for old text or false
656 * @param string|bool $ntitle Header for new text or false
657 * @param string $notice HTML between diff header and body
658 *
659 * @return bool
660 */
661 public function showDiff( $otitle, $ntitle, $notice = '' ) {
662 // Allow extensions to affect the output here
663 Hooks::run( 'DifferenceEngineShowDiff', [ $this ] );
664
665 $diff = $this->getDiff( $otitle, $ntitle, $notice );
666 if ( $diff === false ) {
667 $this->showMissingRevision();
668
669 return false;
670 } else {
671 $this->showDiffStyle();
672 $this->getOutput()->addHTML( $diff );
673
674 return true;
675 }
676 }
677
678 /**
679 * Add style sheets for diff display.
680 */
681 public function showDiffStyle() {
682 $this->getOutput()->addModuleStyles( 'mediawiki.diff.styles' );
683 }
684
685 /**
686 * Get complete diff table, including header
687 *
688 * @param string|bool $otitle Header for old text or false
689 * @param string|bool $ntitle Header for new text or false
690 * @param string $notice HTML between diff header and body
691 *
692 * @return mixed
693 */
694 public function getDiff( $otitle, $ntitle, $notice = '' ) {
695 $body = $this->getDiffBody();
696 if ( $body === false ) {
697 return false;
698 }
699
700 $multi = $this->getMultiNotice();
701 // Display a message when the diff is empty
702 if ( $body === '' ) {
703 $notice .= '<div class="mw-diff-empty">' .
704 $this->msg( 'diff-empty' )->parse() .
705 "</div>\n";
706 }
707
708 return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice );
709 }
710
711 /**
712 * Get the diff table body, without header
713 *
714 * @return mixed (string/false)
715 */
716 public function getDiffBody() {
717 $this->mCacheHit = true;
718 // Check if the diff should be hidden from this user
719 if ( !$this->loadRevisionData() ) {
720 return false;
721 } elseif ( $this->mOldRev &&
722 !$this->mOldRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
723 ) {
724 return false;
725 } elseif ( $this->mNewRev &&
726 !$this->mNewRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
727 ) {
728 return false;
729 }
730 // Short-circuit
731 if ( $this->mOldRev === false || ( $this->mOldRev && $this->mNewRev
732 && $this->mOldRev->getId() == $this->mNewRev->getId() )
733 ) {
734 if ( Hooks::run( 'DifferenceEngineShowEmptyOldContent', [ $this ] ) ) {
735 return '';
736 }
737 }
738 // Cacheable?
739 $key = false;
740 $cache = ObjectCache::getMainWANInstance();
741 if ( $this->mOldid && $this->mNewid ) {
742 $key = $this->getDiffBodyCacheKey();
743
744 // Try cache
745 if ( !$this->mRefreshCache ) {
746 $difftext = $cache->get( $key );
747 if ( $difftext ) {
748 wfIncrStats( 'diff_cache.hit' );
749 $difftext = $this->localiseLineNumbers( $difftext );
750 $difftext .= "\n<!-- diff cache key $key -->\n";
751
752 return $difftext;
753 }
754 } // don't try to load but save the result
755 }
756 $this->mCacheHit = false;
757
758 // Loadtext is permission safe, this just clears out the diff
759 if ( !$this->loadText() ) {
760 return false;
761 }
762
763 $difftext = $this->generateContentDiffBody( $this->mOldContent, $this->mNewContent );
764
765 // Avoid PHP 7.1 warning from passing $this by reference
766 $diffEngine = $this;
767
768 // Save to cache for 7 days
769 if ( !Hooks::run( 'AbortDiffCache', [ &$diffEngine ] ) ) {
770 wfIncrStats( 'diff_cache.uncacheable' );
771 } elseif ( $key !== false && $difftext !== false ) {
772 wfIncrStats( 'diff_cache.miss' );
773 $cache->set( $key, $difftext, 7 * 86400 );
774 } else {
775 wfIncrStats( 'diff_cache.uncacheable' );
776 }
777 // Replace line numbers with the text in the user's language
778 if ( $difftext !== false ) {
779 $difftext = $this->localiseLineNumbers( $difftext );
780 }
781
782 return $difftext;
783 }
784
785 /**
786 * Returns the cache key for diff body text or content.
787 *
788 * @since 1.23
789 *
790 * @throws MWException
791 * @return string
792 */
793 protected function getDiffBodyCacheKey() {
794 if ( !$this->mOldid || !$this->mNewid ) {
795 throw new MWException( 'mOldid and mNewid must be set to get diff cache key.' );
796 }
797
798 return wfMemcKey( 'diff', 'version', self::DIFF_VERSION,
799 'oldid', $this->mOldid, 'newid', $this->mNewid );
800 }
801
802 /**
803 * Generate a diff, no caching.
804 *
805 * This implementation uses generateTextDiffBody() to generate a diff based on the default
806 * serialization of the given Content objects. This will fail if $old or $new are not
807 * instances of TextContent.
808 *
809 * Subclasses may override this to provide a different rendering for the diff,
810 * perhaps taking advantage of the content's native form. This is required for all content
811 * models that are not text based.
812 *
813 * @since 1.21
814 *
815 * @param Content $old Old content
816 * @param Content $new New content
817 *
818 * @throws MWException If old or new content is not an instance of TextContent.
819 * @return bool|string
820 */
821 public function generateContentDiffBody( Content $old, Content $new ) {
822 if ( !( $old instanceof TextContent ) ) {
823 throw new MWException( "Diff not implemented for " . get_class( $old ) . "; " .
824 "override generateContentDiffBody to fix this." );
825 }
826
827 if ( !( $new instanceof TextContent ) ) {
828 throw new MWException( "Diff not implemented for " . get_class( $new ) . "; "
829 . "override generateContentDiffBody to fix this." );
830 }
831
832 $otext = $old->serialize();
833 $ntext = $new->serialize();
834
835 return $this->generateTextDiffBody( $otext, $ntext );
836 }
837
838 /**
839 * Generate a diff, no caching
840 *
841 * @todo move this to TextDifferenceEngine, make DifferenceEngine abstract. At some point.
842 *
843 * @param string $otext Old text, must be already segmented
844 * @param string $ntext New text, must be already segmented
845 *
846 * @return bool|string
847 */
848 public function generateTextDiffBody( $otext, $ntext ) {
849 $diff = function() use ( $otext, $ntext ) {
850 $time = microtime( true );
851
852 $result = $this->textDiff( $otext, $ntext );
853
854 $time = intval( ( microtime( true ) - $time ) * 1000 );
855 $this->getStats()->timing( 'diff_time', $time );
856 // Log requests slower than 99th percentile
857 if ( $time > 100 && $this->mOldPage && $this->mNewPage ) {
858 wfDebugLog( 'diff',
859 "$time ms diff: {$this->mOldid} -> {$this->mNewid} {$this->mNewPage}" );
860 }
861
862 return $result;
863 };
864
865 /**
866 * @param Status $status
867 * @throws FatalError
868 */
869 $error = function( $status ) {
870 throw new FatalError( $status->getWikiText() );
871 };
872
873 // Use PoolCounter if the diff looks like it can be expensive
874 if ( strlen( $otext ) + strlen( $ntext ) > 20000 ) {
875 $work = new PoolCounterWorkViaCallback( 'diff',
876 md5( $otext ) . md5( $ntext ),
877 [ 'doWork' => $diff, 'error' => $error ]
878 );
879 return $work->execute();
880 }
881
882 return $diff();
883 }
884
885 /**
886 * Generates diff, to be wrapped internally in a logging/instrumentation
887 *
888 * @param string $otext Old text, must be already segmented
889 * @param string $ntext New text, must be already segmented
890 * @return bool|string
891 */
892 protected function textDiff( $otext, $ntext ) {
893 global $wgExternalDiffEngine, $wgContLang;
894
895 $otext = str_replace( "\r\n", "\n", $otext );
896 $ntext = str_replace( "\r\n", "\n", $ntext );
897
898 if ( $wgExternalDiffEngine == 'wikidiff' || $wgExternalDiffEngine == 'wikidiff3' ) {
899 wfDeprecated( "\$wgExternalDiffEngine = '{$wgExternalDiffEngine}'", '1.27' );
900 $wgExternalDiffEngine = false;
901 } elseif ( $wgExternalDiffEngine == 'wikidiff2' ) {
902 // Same as above, but with no deprecation warnings
903 $wgExternalDiffEngine = false;
904 } elseif ( !is_string( $wgExternalDiffEngine ) && $wgExternalDiffEngine !== false ) {
905 // And prevent people from shooting themselves in the foot...
906 wfWarn( '$wgExternalDiffEngine is set to a non-string value, forcing it to false' );
907 $wgExternalDiffEngine = false;
908 }
909
910 if ( function_exists( 'wikidiff2_do_diff' ) && $wgExternalDiffEngine === false ) {
911 # Better external diff engine, the 2 may some day be dropped
912 # This one does the escaping and segmenting itself
913 $text = wikidiff2_do_diff( $otext, $ntext, 2 );
914 $text .= $this->debug( 'wikidiff2' );
915
916 return $text;
917 } elseif ( $wgExternalDiffEngine !== false && is_executable( $wgExternalDiffEngine ) ) {
918 # Diff via the shell
919 $tmpDir = wfTempDir();
920 $tempName1 = tempnam( $tmpDir, 'diff_' );
921 $tempName2 = tempnam( $tmpDir, 'diff_' );
922
923 $tempFile1 = fopen( $tempName1, "w" );
924 if ( !$tempFile1 ) {
925 return false;
926 }
927 $tempFile2 = fopen( $tempName2, "w" );
928 if ( !$tempFile2 ) {
929 return false;
930 }
931 fwrite( $tempFile1, $otext );
932 fwrite( $tempFile2, $ntext );
933 fclose( $tempFile1 );
934 fclose( $tempFile2 );
935 $cmd = wfEscapeShellArg( $wgExternalDiffEngine, $tempName1, $tempName2 );
936 $difftext = wfShellExec( $cmd );
937 $difftext .= $this->debug( "external $wgExternalDiffEngine" );
938 unlink( $tempName1 );
939 unlink( $tempName2 );
940
941 return $difftext;
942 }
943
944 # Native PHP diff
945 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
946 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
947 $diffs = new Diff( $ota, $nta );
948 $formatter = new TableDiffFormatter();
949 $difftext = $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
950
951 return $difftext;
952 }
953
954 /**
955 * Generate a debug comment indicating diff generating time,
956 * server node, and generator backend.
957 *
958 * @param string $generator : What diff engine was used
959 *
960 * @return string
961 */
962 protected function debug( $generator = "internal" ) {
963 global $wgShowHostnames;
964 if ( !$this->enableDebugComment ) {
965 return '';
966 }
967 $data = [ $generator ];
968 if ( $wgShowHostnames ) {
969 $data[] = wfHostname();
970 }
971 $data[] = wfTimestamp( TS_DB );
972
973 return "<!-- diff generator: " .
974 implode( " ", array_map( "htmlspecialchars", $data ) ) .
975 " -->\n";
976 }
977
978 /**
979 * Replace line numbers with the text in the user's language
980 *
981 * @param string $text
982 *
983 * @return mixed
984 */
985 public function localiseLineNumbers( $text ) {
986 return preg_replace_callback(
987 '/<!--LINE (\d+)-->/',
988 [ $this, 'localiseLineNumbersCb' ],
989 $text
990 );
991 }
992
993 public function localiseLineNumbersCb( $matches ) {
994 if ( $matches[1] === '1' && $this->mReducedLineNumbers ) {
995 return '';
996 }
997
998 return $this->msg( 'lineno' )->numParams( $matches[1] )->escaped();
999 }
1000
1001 /**
1002 * If there are revisions between the ones being compared, return a note saying so.
1003 *
1004 * @return string
1005 */
1006 public function getMultiNotice() {
1007 if ( !is_object( $this->mOldRev ) || !is_object( $this->mNewRev ) ) {
1008 return '';
1009 } elseif ( !$this->mOldPage->equals( $this->mNewPage ) ) {
1010 // Comparing two different pages? Count would be meaningless.
1011 return '';
1012 }
1013
1014 if ( $this->mOldRev->getTimestamp() > $this->mNewRev->getTimestamp() ) {
1015 $oldRev = $this->mNewRev; // flip
1016 $newRev = $this->mOldRev; // flip
1017 } else { // normal case
1018 $oldRev = $this->mOldRev;
1019 $newRev = $this->mNewRev;
1020 }
1021
1022 // Sanity: don't show the notice if too many rows must be scanned
1023 // @todo show some special message for that case
1024 $nEdits = $this->mNewPage->countRevisionsBetween( $oldRev, $newRev, 1000 );
1025 if ( $nEdits > 0 && $nEdits <= 1000 ) {
1026 $limit = 100; // use diff-multi-manyusers if too many users
1027 $users = $this->mNewPage->getAuthorsBetween( $oldRev, $newRev, $limit );
1028 $numUsers = count( $users );
1029
1030 if ( $numUsers == 1 && $users[0] == $newRev->getUserText( Revision::RAW ) ) {
1031 $numUsers = 0; // special case to say "by the same user" instead of "by one other user"
1032 }
1033
1034 return self::intermediateEditsMsg( $nEdits, $numUsers, $limit );
1035 }
1036
1037 return ''; // nothing
1038 }
1039
1040 /**
1041 * Get a notice about how many intermediate edits and users there are
1042 *
1043 * @param int $numEdits
1044 * @param int $numUsers
1045 * @param int $limit
1046 *
1047 * @return string
1048 */
1049 public static function intermediateEditsMsg( $numEdits, $numUsers, $limit ) {
1050 if ( $numUsers === 0 ) {
1051 $msg = 'diff-multi-sameuser';
1052 } elseif ( $numUsers > $limit ) {
1053 $msg = 'diff-multi-manyusers';
1054 $numUsers = $limit;
1055 } else {
1056 $msg = 'diff-multi-otherusers';
1057 }
1058
1059 return wfMessage( $msg )->numParams( $numEdits, $numUsers )->parse();
1060 }
1061
1062 /**
1063 * Get a header for a specified revision.
1064 *
1065 * @param Revision $rev
1066 * @param string $complete 'complete' to get the header wrapped depending
1067 * the visibility of the revision and a link to edit the page.
1068 *
1069 * @return string HTML fragment
1070 */
1071 public function getRevisionHeader( Revision $rev, $complete = '' ) {
1072 $lang = $this->getLanguage();
1073 $user = $this->getUser();
1074 $revtimestamp = $rev->getTimestamp();
1075 $timestamp = $lang->userTimeAndDate( $revtimestamp, $user );
1076 $dateofrev = $lang->userDate( $revtimestamp, $user );
1077 $timeofrev = $lang->userTime( $revtimestamp, $user );
1078
1079 $header = $this->msg(
1080 $rev->isCurrent() ? 'currentrev-asof' : 'revisionasof',
1081 $timestamp,
1082 $dateofrev,
1083 $timeofrev
1084 )->escaped();
1085
1086 if ( $complete !== 'complete' ) {
1087 return $header;
1088 }
1089
1090 $title = $rev->getTitle();
1091
1092 $header = Linker::linkKnown( $title, $header, [],
1093 [ 'oldid' => $rev->getId() ] );
1094
1095 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1096 $editQuery = [ 'action' => 'edit' ];
1097 if ( !$rev->isCurrent() ) {
1098 $editQuery['oldid'] = $rev->getId();
1099 }
1100
1101 $key = $title->quickUserCan( 'edit', $user ) ? 'editold' : 'viewsourceold';
1102 $msg = $this->msg( $key )->escaped();
1103 $editLink = $this->msg( 'parentheses' )->rawParams(
1104 Linker::linkKnown( $title, $msg, [], $editQuery ) )->escaped();
1105 $header .= ' ' . Html::rawElement(
1106 'span',
1107 [ 'class' => 'mw-diff-edit' ],
1108 $editLink
1109 );
1110 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1111 $header = Html::rawElement(
1112 'span',
1113 [ 'class' => 'history-deleted' ],
1114 $header
1115 );
1116 }
1117 } else {
1118 $header = Html::rawElement( 'span', [ 'class' => 'history-deleted' ], $header );
1119 }
1120
1121 return $header;
1122 }
1123
1124 /**
1125 * Add the header to a diff body
1126 *
1127 * @param string $diff Diff body
1128 * @param string $otitle Old revision header
1129 * @param string $ntitle New revision header
1130 * @param string $multi Notice telling user that there are intermediate
1131 * revisions between the ones being compared
1132 * @param string $notice Other notices, e.g. that user is viewing deleted content
1133 *
1134 * @return string
1135 */
1136 public function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) {
1137 // shared.css sets diff in interface language/dir, but the actual content
1138 // is often in a different language, mostly the page content language/dir
1139 $header = Html::openElement( 'table', [
1140 'class' => [ 'diff', 'diff-contentalign-' . $this->getDiffLang()->alignStart() ],
1141 'data-mw' => 'interface',
1142 ] );
1143 $userLang = htmlspecialchars( $this->getLanguage()->getHtmlCode() );
1144
1145 if ( !$diff && !$otitle ) {
1146 $header .= "
1147 <tr style='vertical-align: top;' lang='{$userLang}'>
1148 <td class='diff-ntitle'>{$ntitle}</td>
1149 </tr>";
1150 $multiColspan = 1;
1151 } else {
1152 if ( $diff ) { // Safari/Chrome show broken output if cols not used
1153 $header .= "
1154 <col class='diff-marker' />
1155 <col class='diff-content' />
1156 <col class='diff-marker' />
1157 <col class='diff-content' />";
1158 $colspan = 2;
1159 $multiColspan = 4;
1160 } else {
1161 $colspan = 1;
1162 $multiColspan = 2;
1163 }
1164 if ( $otitle || $ntitle ) {
1165 $header .= "
1166 <tr style='vertical-align: top;' lang='{$userLang}'>
1167 <td colspan='$colspan' class='diff-otitle'>{$otitle}</td>
1168 <td colspan='$colspan' class='diff-ntitle'>{$ntitle}</td>
1169 </tr>";
1170 }
1171 }
1172
1173 if ( $multi != '' ) {
1174 $header .= "<tr><td colspan='{$multiColspan}' style='text-align: center;' " .
1175 "class='diff-multi' lang='{$userLang}'>{$multi}</td></tr>";
1176 }
1177 if ( $notice != '' ) {
1178 $header .= "<tr><td colspan='{$multiColspan}' style='text-align: center;' " .
1179 "lang='{$userLang}'>{$notice}</td></tr>";
1180 }
1181
1182 return $header . $diff . "</table>";
1183 }
1184
1185 /**
1186 * Use specified text instead of loading from the database
1187 * @param Content $oldContent
1188 * @param Content $newContent
1189 * @since 1.21
1190 */
1191 public function setContent( Content $oldContent, Content $newContent ) {
1192 $this->mOldContent = $oldContent;
1193 $this->mNewContent = $newContent;
1194
1195 $this->mTextLoaded = 2;
1196 $this->mRevisionsLoaded = true;
1197 }
1198
1199 /**
1200 * Set the language in which the diff text is written
1201 * (Defaults to page content language).
1202 * @param Language|string $lang
1203 * @since 1.19
1204 */
1205 public function setTextLanguage( $lang ) {
1206 $this->mDiffLang = wfGetLangObj( $lang );
1207 }
1208
1209 /**
1210 * Maps a revision pair definition as accepted by DifferenceEngine constructor
1211 * to a pair of actual integers representing revision ids.
1212 *
1213 * @param int $old Revision id, e.g. from URL parameter 'oldid'
1214 * @param int|string $new Revision id or strings 'next' or 'prev', e.g. from URL parameter 'diff'
1215 *
1216 * @return int[] List of two revision ids, older first, later second.
1217 * Zero signifies invalid argument passed.
1218 * false signifies that there is no previous/next revision ($old is the oldest/newest one).
1219 */
1220 public function mapDiffPrevNext( $old, $new ) {
1221 if ( $new === 'prev' ) {
1222 // Show diff between revision $old and the previous one. Get previous one from DB.
1223 $newid = intval( $old );
1224 $oldid = $this->getTitle()->getPreviousRevisionID( $newid );
1225 } elseif ( $new === 'next' ) {
1226 // Show diff between revision $old and the next one. Get next one from DB.
1227 $oldid = intval( $old );
1228 $newid = $this->getTitle()->getNextRevisionID( $oldid );
1229 } else {
1230 $oldid = intval( $old );
1231 $newid = intval( $new );
1232 }
1233
1234 return [ $oldid, $newid ];
1235 }
1236
1237 /**
1238 * Load revision IDs
1239 */
1240 private function loadRevisionIds() {
1241 if ( $this->mRevisionsIdsLoaded ) {
1242 return;
1243 }
1244
1245 $this->mRevisionsIdsLoaded = true;
1246
1247 $old = $this->mOldid;
1248 $new = $this->mNewid;
1249
1250 list( $this->mOldid, $this->mNewid ) = self::mapDiffPrevNext( $old, $new );
1251 if ( $new === 'next' && $this->mNewid === false ) {
1252 # if no result, NewId points to the newest old revision. The only newer
1253 # revision is cur, which is "0".
1254 $this->mNewid = 0;
1255 }
1256
1257 Hooks::run(
1258 'NewDifferenceEngine',
1259 [ $this->getTitle(), &$this->mOldid, &$this->mNewid, $old, $new ]
1260 );
1261 }
1262
1263 /**
1264 * Load revision metadata for the specified articles. If newid is 0, then compare
1265 * the old article in oldid to the current article; if oldid is 0, then
1266 * compare the current article to the immediately previous one (ignoring the
1267 * value of newid).
1268 *
1269 * If oldid is false, leave the corresponding revision object set
1270 * to false. This is impossible via ordinary user input, and is provided for
1271 * API convenience.
1272 *
1273 * @return bool
1274 */
1275 public function loadRevisionData() {
1276 if ( $this->mRevisionsLoaded ) {
1277 return true;
1278 }
1279
1280 // Whether it succeeds or fails, we don't want to try again
1281 $this->mRevisionsLoaded = true;
1282
1283 $this->loadRevisionIds();
1284
1285 // Load the new revision object
1286 if ( $this->mNewid ) {
1287 $this->mNewRev = Revision::newFromId( $this->mNewid );
1288 } else {
1289 $this->mNewRev = Revision::newFromTitle(
1290 $this->getTitle(),
1291 false,
1292 Revision::READ_NORMAL
1293 );
1294 }
1295
1296 if ( !$this->mNewRev instanceof Revision ) {
1297 return false;
1298 }
1299
1300 // Update the new revision ID in case it was 0 (makes life easier doing UI stuff)
1301 $this->mNewid = $this->mNewRev->getId();
1302 $this->mNewPage = $this->mNewRev->getTitle();
1303
1304 // Load the old revision object
1305 $this->mOldRev = false;
1306 if ( $this->mOldid ) {
1307 $this->mOldRev = Revision::newFromId( $this->mOldid );
1308 } elseif ( $this->mOldid === 0 ) {
1309 $rev = $this->mNewRev->getPrevious();
1310 if ( $rev ) {
1311 $this->mOldid = $rev->getId();
1312 $this->mOldRev = $rev;
1313 } else {
1314 // No previous revision; mark to show as first-version only.
1315 $this->mOldid = false;
1316 $this->mOldRev = false;
1317 }
1318 } /* elseif ( $this->mOldid === false ) leave mOldRev false; */
1319
1320 if ( is_null( $this->mOldRev ) ) {
1321 return false;
1322 }
1323
1324 if ( $this->mOldRev ) {
1325 $this->mOldPage = $this->mOldRev->getTitle();
1326 }
1327
1328 // Load tags information for both revisions
1329 $dbr = wfGetDB( DB_REPLICA );
1330 if ( $this->mOldid !== false ) {
1331 $this->mOldTags = $dbr->selectField(
1332 'tag_summary',
1333 'ts_tags',
1334 [ 'ts_rev_id' => $this->mOldid ],
1335 __METHOD__
1336 );
1337 } else {
1338 $this->mOldTags = false;
1339 }
1340 $this->mNewTags = $dbr->selectField(
1341 'tag_summary',
1342 'ts_tags',
1343 [ 'ts_rev_id' => $this->mNewid ],
1344 __METHOD__
1345 );
1346
1347 return true;
1348 }
1349
1350 /**
1351 * Load the text of the revisions, as well as revision data.
1352 *
1353 * @return bool
1354 */
1355 public function loadText() {
1356 if ( $this->mTextLoaded == 2 ) {
1357 return true;
1358 }
1359
1360 // Whether it succeeds or fails, we don't want to try again
1361 $this->mTextLoaded = 2;
1362
1363 if ( !$this->loadRevisionData() ) {
1364 return false;
1365 }
1366
1367 if ( $this->mOldRev ) {
1368 $this->mOldContent = $this->mOldRev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
1369 if ( $this->mOldContent === null ) {
1370 return false;
1371 }
1372 }
1373
1374 if ( $this->mNewRev ) {
1375 $this->mNewContent = $this->mNewRev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
1376 Hooks::run( 'DifferenceEngineLoadTextAfterNewContentIsLoaded', [ $this ] );
1377 if ( $this->mNewContent === null ) {
1378 return false;
1379 }
1380 }
1381
1382 return true;
1383 }
1384
1385 /**
1386 * Load the text of the new revision, not the old one
1387 *
1388 * @return bool
1389 */
1390 public function loadNewText() {
1391 if ( $this->mTextLoaded >= 1 ) {
1392 return true;
1393 }
1394
1395 $this->mTextLoaded = 1;
1396
1397 if ( !$this->loadRevisionData() ) {
1398 return false;
1399 }
1400
1401 $this->mNewContent = $this->mNewRev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
1402
1403 Hooks::run( 'DifferenceEngineAfterLoadNewText', [ $this ] );
1404
1405 return true;
1406 }
1407
1408 }