Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
82.61% |
817 / 989 |
|
41.18% |
28 / 68 |
CRAP | |
0.00% |
0 / 1 |
DifferenceEngine | |
82.61% |
817 / 989 |
|
41.18% |
28 / 68 |
819.77 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
2 | |||
getSlotDiffRenderers | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
9.08 | |||
markAsSlotDiffRenderer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSlotContents | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
6 | |||
loadSingleSlot | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
addRevisionLoadError | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getRevisionLoadErrors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasNewRevisionLoadError | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
4.12 | |||
getTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
setReducedLineNumbers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDiffLang | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
wasCacheHit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getOldid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getNewid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getOldRevision | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getNewRevision | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deletedLink | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
7.23 | |||
deletedIdMarker | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
showMissingRevision | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
6.02 | |||
hasDeletedRevision | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
getPermissionErrors | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
hasSuppressedRevision | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
getUserEditCount | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
2.00 | |||
getUserRoles | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
getUserMetaData | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
isUserAllowedToSeeRevisions | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
shouldBeHiddenFromUser | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
showDiffPage | |
93.66% |
192 / 205 |
|
0.00% |
0 / 1 |
42.45 | |||
showTablePrefixes | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
markPatrolledLink | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
getMarkPatrolledLinkInfo | |
95.65% |
22 / 23 |
|
0.00% |
0 / 1 |
9 | |||
revisionDeleteLink | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
renderNewRevision | |
73.58% |
39 / 53 |
|
0.00% |
0 / 1 |
11.84 | |||
showDiff | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
3.24 | |||
showDiffStyle | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
getDiff | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 | |||
getDiffBody | |
84.75% |
50 / 59 |
|
0.00% |
0 / 1 |
26.04 | |||
getDiffBodyForRole | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
getSlotHeader | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getSlotError | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getDiffBodyCacheKeyParams | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
5.01 | |||
getExtraCacheKeys | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
setSlotDiffOptions | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
5.93 | |||
setExtraQueryParams | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
generateContentDiffBody | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
generateTextDiffBody | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
2.09 | |||
getEngine | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
2.15 | |||
debug | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
7.23 | |||
getDebugString | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
localiseDiff | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
localiseLineNumbers | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getMultiNotice | |
42.86% |
21 / 49 |
|
0.00% |
0 / 1 |
103.29 | |||
intermediateEditsMsg | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
userCanEdit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getRevisionHeader | |
100.00% |
47 / 47 |
|
100.00% |
1 / 1 |
7 | |||
addHeader | |
95.12% |
39 / 41 |
|
0.00% |
0 / 1 |
8 | |||
setContent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
setRevisions | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
5 | |||
setTextLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
mapDiffPrevNext | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 | |||
loadRevisionIds | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
loadRevisionData | |
81.82% |
45 / 55 |
|
0.00% |
0 / 1 |
19.95 | |||
loadText | |
87.50% |
21 / 24 |
|
0.00% |
0 / 1 |
9.16 | |||
loadNewText | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 | |||
getTextDiffer | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
getSupportedFormats | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTextDiffFormat | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 | use MediaWiki\CommentFormatter\CommentFormatter; |
25 | use MediaWiki\Content\IContentHandlerFactory; |
26 | use MediaWiki\Context\ContextSource; |
27 | use MediaWiki\Context\IContextSource; |
28 | use MediaWiki\Diff\TextDiffer\ManifoldTextDiffer; |
29 | use MediaWiki\HookContainer\HookRunner; |
30 | use MediaWiki\Html\Html; |
31 | use MediaWiki\Linker\Linker; |
32 | use MediaWiki\Linker\LinkRenderer; |
33 | use MediaWiki\MainConfigNames; |
34 | use MediaWiki\MediaWikiServices; |
35 | use MediaWiki\Page\ParserOutputAccess; |
36 | use MediaWiki\Page\WikiPageFactory; |
37 | use MediaWiki\Permissions\Authority; |
38 | use MediaWiki\Permissions\PermissionStatus; |
39 | use MediaWiki\Revision\ArchivedRevisionLookup; |
40 | use MediaWiki\Revision\BadRevisionException; |
41 | use MediaWiki\Revision\RevisionRecord; |
42 | use MediaWiki\Revision\RevisionStore; |
43 | use MediaWiki\Revision\SlotRecord; |
44 | use MediaWiki\SpecialPage\SpecialPage; |
45 | use MediaWiki\Storage\NameTableAccessException; |
46 | use MediaWiki\Title\Title; |
47 | use MediaWiki\User\Options\UserOptionsLookup; |
48 | use MediaWiki\User\UserEditTracker; |
49 | use MediaWiki\User\UserGroupManager; |
50 | use MediaWiki\User\UserGroupMembership; |
51 | use MediaWiki\User\UserIdentity; |
52 | use MediaWiki\User\UserIdentityUtils; |
53 | use Wikimedia\Rdbms\IConnectionProvider; |
54 | |
55 | /** |
56 | * DifferenceEngine is responsible for rendering the difference between two revisions as HTML. |
57 | * This includes interpreting URL parameters, retrieving revision data, checking access permissions, |
58 | * selecting and invoking the diff generator class for the individual slots, doing post-processing |
59 | * on the generated diff, adding the rest of the HTML (such as headers) and writing the whole thing |
60 | * to OutputPage. |
61 | * |
62 | * DifferenceEngine can be subclassed by extensions, by customizing |
63 | * ContentHandler::createDifferenceEngine; the content handler will be selected based on the |
64 | * content model of the main slot (of the new revision, when the two are different). |
65 | * That might change after PageTypeHandler gets introduced. |
66 | * |
67 | * In the past, the class was also used for slot-level diff generation, and extensions might still |
68 | * subclass it and add such functionality. When that is the case (specifically, when a |
69 | * ContentHandler returns a standard SlotDiffRenderer but a nonstandard DifferenceEngine) |
70 | * DifferenceEngineSlotDiffRenderer will be used to convert the old behavior into the new one. |
71 | * |
72 | * @ingroup DifferenceEngine |
73 | * |
74 | * @todo This class is huge and poorly defined. It should be split into a controller responsible |
75 | * for interpreting query parameters, retrieving data and checking permissions; and a HTML renderer. |
76 | */ |
77 | class DifferenceEngine extends ContextSource { |
78 | |
79 | use DeprecationHelper; |
80 | |
81 | /** |
82 | * Constant to indicate diff cache compatibility. |
83 | * Bump this when changing the diff formatting in a way that |
84 | * fixes important bugs or such to force cached diff views to |
85 | * clear. |
86 | */ |
87 | private const DIFF_VERSION = '1.41'; |
88 | |
89 | /** |
90 | * Revision ID for the old revision. 0 for the revision previous to $mNewid, false |
91 | * if the diff does not have an old revision (e.g. 'oldid=<first revision of page>&diff=prev'), |
92 | * or the revision does not exist, null if the revision is unsaved. |
93 | * @var int|false|null |
94 | */ |
95 | protected $mOldid; |
96 | |
97 | /** |
98 | * Revision ID for the new revision. 0 for the last revision of the current page |
99 | * (as defined by the request context), false if the revision does not exist, null |
100 | * if it is unsaved, or an alias such as 'next'. |
101 | * @var int|string|false|null |
102 | */ |
103 | protected $mNewid; |
104 | |
105 | /** |
106 | * Old revision (left pane). |
107 | * Allowed to be an unsaved revision, unlikely that's ever needed though. |
108 | * False when the old revision does not exist; this can happen when using |
109 | * diff=prev on the first revision. Null when the revision should exist but |
110 | * doesn't (e.g. load failure); loadRevisionData() will return false in that |
111 | * case. Also null until lazy-loaded. Ignored completely when isContentOverridden |
112 | * is set. |
113 | * @var RevisionRecord|null|false |
114 | */ |
115 | private $mOldRevisionRecord; |
116 | |
117 | /** |
118 | * New revision (right pane). |
119 | * Note that this might be an unsaved revision (e.g. for edit preview). |
120 | * Null in case of load failure; diff methods will just return an error message in that case, |
121 | * and loadRevisionData() will return false. Also null until lazy-loaded. Ignored completely |
122 | * when isContentOverridden is set. |
123 | * @var RevisionRecord|null |
124 | */ |
125 | private $mNewRevisionRecord; |
126 | |
127 | /** |
128 | * Title of old revision or null if the old revision does not exist or does not belong to a page. |
129 | * @var Title|null |
130 | */ |
131 | protected $mOldPage; |
132 | |
133 | /** |
134 | * Title of new revision or null if the new revision does not exist or does not belong to a page. |
135 | * @var Title|null |
136 | */ |
137 | protected $mNewPage; |
138 | |
139 | /** |
140 | * Change tags of old revision or null if it does not exist / is not saved. |
141 | * @var string|false|null |
142 | */ |
143 | private $mOldTags; |
144 | |
145 | /** |
146 | * Change tags of new revision or null if it does not exist / is not saved. |
147 | * @var string|null |
148 | */ |
149 | private $mNewTags; |
150 | |
151 | /** |
152 | * @var Content|null |
153 | * @deprecated since 1.32, content slots are now handled by the corresponding SlotDiffRenderer. |
154 | * This property is set to the content of the main slot, but not actually used for the main diff. |
155 | */ |
156 | private $mOldContent; |
157 | |
158 | /** |
159 | * @var Content|null |
160 | * @deprecated since 1.32, content slots are now handled by the corresponding SlotDiffRenderer. |
161 | * This property is set to the content of the main slot, but not actually used for the main diff. |
162 | */ |
163 | private $mNewContent; |
164 | |
165 | /** @var Language */ |
166 | protected $mDiffLang; |
167 | |
168 | /** @var bool Have the revisions IDs been loaded */ |
169 | private $mRevisionsIdsLoaded = false; |
170 | |
171 | /** @var bool Have the revisions been loaded */ |
172 | protected $mRevisionsLoaded = false; |
173 | |
174 | /** @var int How many text blobs have been loaded, 0, 1 or 2? */ |
175 | protected $mTextLoaded = 0; |
176 | |
177 | /** |
178 | * Was the content overridden via setContent()? |
179 | * If the content was overridden, most internal state (e.g. mOldid or mOldRev) should be ignored |
180 | * and only mOldContent and mNewContent is reliable. |
181 | * (Note that setRevisions() does not set this flag as in that case all properties are |
182 | * overridden and remain consistent with each other, so no special handling is needed.) |
183 | * @var bool |
184 | */ |
185 | protected $isContentOverridden = false; |
186 | |
187 | /** @var bool Was the diff fetched from cache? */ |
188 | protected $mCacheHit = false; |
189 | |
190 | /** @var string|null Cache key if the diff was fetched from cache */ |
191 | private $cacheHitKey = null; |
192 | |
193 | /** |
194 | * Set this to true to add debug info to the HTML output. |
195 | * Warning: this may cause RSS readers to spuriously mark articles as "new" |
196 | * (T22601) |
197 | * @var bool |
198 | */ |
199 | public $enableDebugComment = false; |
200 | |
201 | /** @var bool If true, line X is not displayed when X is 1, for example |
202 | * to increase readability and conserve space with many small diffs. |
203 | */ |
204 | protected $mReducedLineNumbers = false; |
205 | |
206 | /** @var string Link to action=markpatrolled */ |
207 | protected $mMarkPatrolledLink = null; |
208 | |
209 | /** @var bool Show rev_deleted content if allowed */ |
210 | protected $unhide = false; |
211 | |
212 | /** @var bool Refresh the diff cache */ |
213 | protected $mRefreshCache = false; |
214 | |
215 | /** @var SlotDiffRenderer[]|null DifferenceEngine classes for the slots, keyed by role name. */ |
216 | protected $slotDiffRenderers = null; |
217 | |
218 | /** |
219 | * Temporary hack for B/C while slot diff related methods of DifferenceEngine are being |
220 | * deprecated. When true, we are inside a DifferenceEngineSlotDiffRenderer and |
221 | * $slotDiffRenderers should not be used. |
222 | * @var bool |
223 | */ |
224 | protected $isSlotDiffRenderer = false; |
225 | |
226 | /** |
227 | * A set of options that will be passed to the SlotDiffRenderer upon creation |
228 | * @var array |
229 | */ |
230 | private $slotDiffOptions = []; |
231 | |
232 | /** |
233 | * Extra query parameters to be appended to diff page links |
234 | */ |
235 | private $extraQueryParams = []; |
236 | |
237 | /** @var ManifoldTextDiffer|null */ |
238 | private $textDiffer; |
239 | |
240 | protected LinkRenderer $linkRenderer; |
241 | private IContentHandlerFactory $contentHandlerFactory; |
242 | private RevisionStore $revisionStore; |
243 | private ArchivedRevisionLookup $archivedRevisionLookup; |
244 | private HookRunner $hookRunner; |
245 | private WikiPageFactory $wikiPageFactory; |
246 | private UserOptionsLookup $userOptionsLookup; |
247 | private CommentFormatter $commentFormatter; |
248 | private IConnectionProvider $dbProvider; |
249 | private UserGroupManager $userGroupManager; |
250 | private UserEditTracker $userEditTracker; |
251 | private UserIdentityUtils $userIdentityUtils; |
252 | |
253 | /** @var Message[] */ |
254 | private $revisionLoadErrors = []; |
255 | |
256 | /** |
257 | * @param IContextSource|null $context Context to use, anything else will be ignored |
258 | * @param int $old Old ID we want to show and diff with. |
259 | * @param string|int $new Either revision ID or 'prev' or 'next'. Default: 0. |
260 | * @param int $rcid Deprecated, no longer used! |
261 | * @param bool $refreshCache If set, refreshes the diff cache |
262 | * @param bool $unhide If set, allow viewing deleted revs |
263 | */ |
264 | public function __construct( $context = null, $old = 0, $new = 0, $rcid = 0, |
265 | $refreshCache = false, $unhide = false |
266 | ) { |
267 | if ( $context instanceof IContextSource ) { |
268 | $this->setContext( $context ); |
269 | } |
270 | |
271 | wfDebug( "DifferenceEngine old '$old' new '$new' rcid '$rcid'" ); |
272 | |
273 | $this->mOldid = $old; |
274 | $this->mNewid = $new; |
275 | $this->mRefreshCache = $refreshCache; |
276 | $this->unhide = $unhide; |
277 | |
278 | $services = MediaWikiServices::getInstance(); |
279 | $this->linkRenderer = $services->getLinkRenderer(); |
280 | $this->contentHandlerFactory = $services->getContentHandlerFactory(); |
281 | $this->revisionStore = $services->getRevisionStore(); |
282 | $this->archivedRevisionLookup = $services->getArchivedRevisionLookup(); |
283 | $this->hookRunner = new HookRunner( $services->getHookContainer() ); |
284 | $this->wikiPageFactory = $services->getWikiPageFactory(); |
285 | $this->userOptionsLookup = $services->getUserOptionsLookup(); |
286 | $this->commentFormatter = $services->getCommentFormatter(); |
287 | $this->dbProvider = $services->getConnectionProvider(); |
288 | $this->userGroupManager = $services->getUserGroupManager(); |
289 | $this->userEditTracker = $services->getUserEditTracker(); |
290 | $this->userIdentityUtils = $services->getUserIdentityUtils(); |
291 | } |
292 | |
293 | /** |
294 | * @return SlotDiffRenderer[] Diff renderers for each slot, keyed by role name. |
295 | * Includes slots only present in one of the revisions. Does not include slots |
296 | * for which content is identical in the two revisions. |
297 | */ |
298 | protected function getSlotDiffRenderers() { |
299 | if ( $this->isSlotDiffRenderer ) { |
300 | throw new LogicException( __METHOD__ . ' called in slot diff renderer mode' ); |
301 | } |
302 | |
303 | if ( $this->slotDiffRenderers === null ) { |
304 | if ( !$this->loadRevisionData() ) { |
305 | return []; |
306 | } |
307 | |
308 | $slotContents = $this->getSlotContents(); |
309 | $this->slotDiffRenderers = []; |
310 | foreach ( $slotContents as $role => $contents ) { |
311 | if ( $contents['new'] && $contents['old'] |
312 | && $contents['new']->equals( $contents['old'] ) |
313 | ) { |
314 | // Do not produce a diff of identical content |
315 | continue; |
316 | } |
317 | $handler = ( $contents['new'] ?: $contents['old'] )->getContentHandler(); |
318 | $this->slotDiffRenderers[$role] = $handler->getSlotDiffRenderer( |
319 | $this->getContext(), |
320 | $this->slotDiffOptions + [ |
321 | 'contentLanguage' => $this->getDiffLang()->getCode(), |
322 | 'textDiffer' => $this->getTextDiffer() |
323 | ] |
324 | ); |
325 | } |
326 | } |
327 | |
328 | return $this->slotDiffRenderers; |
329 | } |
330 | |
331 | /** |
332 | * Mark this DifferenceEngine as a slot renderer (as opposed to a page renderer). |
333 | * This is used in legacy mode when the DifferenceEngine is wrapped in a |
334 | * DifferenceEngineSlotDiffRenderer. |
335 | * @internal For use by DifferenceEngineSlotDiffRenderer only. |
336 | */ |
337 | public function markAsSlotDiffRenderer() { |
338 | $this->isSlotDiffRenderer = true; |
339 | } |
340 | |
341 | /** |
342 | * Get the old and new content objects for all slots. |
343 | * This method does not do any permission checks. |
344 | * @return (Content|null)[][] [ role => [ 'old' => Content|null, 'new' => Content|null ], ... ] |
345 | */ |
346 | protected function getSlotContents() { |
347 | if ( $this->isContentOverridden ) { |
348 | return [ |
349 | SlotRecord::MAIN => [ 'old' => $this->mOldContent, 'new' => $this->mNewContent ] |
350 | ]; |
351 | } elseif ( !$this->loadRevisionData() ) { |
352 | return []; |
353 | } |
354 | |
355 | $newSlots = $this->mNewRevisionRecord->getPrimarySlots()->getSlots(); |
356 | $oldSlots = $this->mOldRevisionRecord ? |
357 | $this->mOldRevisionRecord->getPrimarySlots()->getSlots() : |
358 | []; |
359 | // The order here will determine the visual order of the diff. The current logic is |
360 | // slots of the new revision first in natural order, then deleted ones. This is ad hoc |
361 | // and should not be relied on - in the future we may want the ordering to depend |
362 | // on the page type. |
363 | $roles = array_keys( array_merge( $newSlots, $oldSlots ) ); |
364 | |
365 | $slots = []; |
366 | foreach ( $roles as $role ) { |
367 | $slots[$role] = [ |
368 | 'old' => $this->loadSingleSlot( |
369 | $oldSlots[$role] ?? null, |
370 | 'old' |
371 | ), |
372 | 'new' => $this->loadSingleSlot( |
373 | $newSlots[$role] ?? null, |
374 | 'new' |
375 | ) |
376 | ]; |
377 | } |
378 | // move main slot to front |
379 | if ( isset( $slots[SlotRecord::MAIN] ) ) { |
380 | $slots = [ SlotRecord::MAIN => $slots[SlotRecord::MAIN] ] + $slots; |
381 | } |
382 | return $slots; |
383 | } |
384 | |
385 | /** |
386 | * Load the content of a single slot record |
387 | * |
388 | * @param SlotRecord|null $slot |
389 | * @param string $which "new" or "old" |
390 | * @return Content|null |
391 | */ |
392 | private function loadSingleSlot( ?SlotRecord $slot, string $which ) { |
393 | if ( !$slot ) { |
394 | return null; |
395 | } |
396 | try { |
397 | return $slot->getContent(); |
398 | } catch ( BadRevisionException $e ) { |
399 | $this->addRevisionLoadError( $which ); |
400 | return null; |
401 | } |
402 | } |
403 | |
404 | /** |
405 | * Set a message to show as a notice at the top of the page |
406 | * |
407 | * @param string $which "new" or "old" |
408 | */ |
409 | private function addRevisionLoadError( $which ) { |
410 | $this->revisionLoadErrors[] = $this->msg( $which === 'new' |
411 | ? 'difference-bad-new-revision' : 'difference-bad-old-revision' |
412 | ); |
413 | } |
414 | |
415 | /** |
416 | * If errors were encountered while loading the revision contents, this |
417 | * will return an array of Messages describing the errors. |
418 | * |
419 | * @return Message[] |
420 | */ |
421 | public function getRevisionLoadErrors() { |
422 | return $this->revisionLoadErrors; |
423 | } |
424 | |
425 | /** |
426 | * Determine whether there was an error loading the new revision |
427 | * @return bool |
428 | */ |
429 | private function hasNewRevisionLoadError() { |
430 | foreach ( $this->revisionLoadErrors as $error ) { |
431 | if ( $error->getKey() === 'difference-bad-new-revision' ) { |
432 | return true; |
433 | } |
434 | } |
435 | return false; |
436 | } |
437 | |
438 | /** @inheritDoc */ |
439 | public function getTitle() { |
440 | // T202454 avoid errors when there is no title |
441 | return parent::getTitle() ?: Title::makeTitle( NS_SPECIAL, 'BadTitle/DifferenceEngine' ); |
442 | } |
443 | |
444 | /** |
445 | * Set reduced line numbers mode. |
446 | * When set, line X is not displayed when X is 1, for example to increase readability and |
447 | * conserve space with many small diffs. |
448 | * @param bool $value |
449 | */ |
450 | public function setReducedLineNumbers( $value = true ) { |
451 | $this->mReducedLineNumbers = $value; |
452 | } |
453 | |
454 | /** |
455 | * Get the language in which the diff text is written |
456 | * |
457 | * @return Language |
458 | */ |
459 | public function getDiffLang() { |
460 | # Default language in which the diff text is written. |
461 | $this->mDiffLang ??= $this->getDefaultLanguage(); |
462 | return $this->mDiffLang; |
463 | } |
464 | |
465 | /** |
466 | * Get the language to use if none has been set by setTextLanguage(). |
467 | * Wikibase overrides this to use the user language. |
468 | * |
469 | * @return Language |
470 | */ |
471 | protected function getDefaultLanguage() { |
472 | return $this->getTitle()->getPageLanguage(); |
473 | } |
474 | |
475 | /** |
476 | * @return bool |
477 | */ |
478 | public function wasCacheHit() { |
479 | return $this->mCacheHit; |
480 | } |
481 | |
482 | /** |
483 | * Get the ID of old revision (left pane) of the diff. 0 for the revision |
484 | * previous to getNewid(), false if the old revision does not exist, null |
485 | * if it's unsaved. |
486 | * To get a real revision ID instead of 0, call loadRevisionData() first. |
487 | * @return int|false|null |
488 | */ |
489 | public function getOldid() { |
490 | $this->loadRevisionIds(); |
491 | |
492 | return $this->mOldid; |
493 | } |
494 | |
495 | /** |
496 | * Get the ID of new revision (right pane) of the diff. 0 for the current revision, |
497 | * false if the new revision does not exist, null if it's unsaved. |
498 | * To get a real revision ID instead of 0, call loadRevisionData() first. |
499 | * @return int|false|null |
500 | */ |
501 | public function getNewid() { |
502 | $this->loadRevisionIds(); |
503 | |
504 | return $this->mNewid; |
505 | } |
506 | |
507 | /** |
508 | * Get the left side of the diff. |
509 | * Could be null when the first revision of the page is diffed to 'prev' (or in the case of |
510 | * load failure). |
511 | * @return RevisionRecord|null |
512 | */ |
513 | public function getOldRevision() { |
514 | return $this->mOldRevisionRecord ?: null; |
515 | } |
516 | |
517 | /** |
518 | * Get the right side of the diff. |
519 | * Should not be null but can still happen in the case of load failure. |
520 | * @return RevisionRecord|null |
521 | */ |
522 | public function getNewRevision() { |
523 | return $this->mNewRevisionRecord; |
524 | } |
525 | |
526 | /** |
527 | * Look up a special:Undelete link to the given deleted revision id, |
528 | * as a workaround for being unable to load deleted diffs in currently. |
529 | * |
530 | * @param int $id Revision ID |
531 | * |
532 | * @return string|bool Link HTML or false |
533 | */ |
534 | public function deletedLink( $id ) { |
535 | if ( $this->getAuthority()->isAllowed( 'deletedhistory' ) ) { |
536 | $revRecord = $this->archivedRevisionLookup->getArchivedRevisionRecord( null, $id ); |
537 | if ( $revRecord ) { |
538 | $title = Title::newFromPageIdentity( $revRecord->getPage() ); |
539 | |
540 | return SpecialPage::getTitleFor( 'Undelete' )->getFullURL( [ |
541 | 'target' => $title->getPrefixedText(), |
542 | 'timestamp' => $revRecord->getTimestamp() |
543 | ] ); |
544 | } |
545 | } |
546 | |
547 | return false; |
548 | } |
549 | |
550 | /** |
551 | * Build a wikitext link toward a deleted revision, if viewable. |
552 | * |
553 | * @param int $id Revision ID |
554 | * |
555 | * @return string Wikitext fragment |
556 | */ |
557 | public function deletedIdMarker( $id ) { |
558 | $link = $this->deletedLink( $id ); |
559 | if ( $link ) { |
560 | return "[$link $id]"; |
561 | } else { |
562 | return (string)$id; |
563 | } |
564 | } |
565 | |
566 | private function showMissingRevision() { |
567 | $out = $this->getOutput(); |
568 | |
569 | $missing = []; |
570 | if ( $this->mOldid && ( !$this->mOldRevisionRecord || !$this->mOldContent ) ) { |
571 | $missing[] = $this->deletedIdMarker( $this->mOldid ); |
572 | } |
573 | if ( !$this->mNewRevisionRecord || !$this->mNewContent ) { |
574 | $missing[] = $this->deletedIdMarker( $this->mNewid ); |
575 | } |
576 | |
577 | $out->setPageTitleMsg( $this->msg( 'errorpagetitle' ) ); |
578 | $msg = $this->msg( 'difference-missing-revision' ) |
579 | ->params( $this->getLanguage()->listToText( $missing ) ) |
580 | ->numParams( count( $missing ) ) |
581 | ->parseAsBlock(); |
582 | $out->addHTML( $msg ); |
583 | } |
584 | |
585 | /** |
586 | * Checks whether one of the given Revisions was deleted |
587 | * |
588 | * @return bool |
589 | */ |
590 | public function hasDeletedRevision() { |
591 | $this->loadRevisionData(); |
592 | return ( |
593 | $this->mNewRevisionRecord && |
594 | $this->mNewRevisionRecord->isDeleted( RevisionRecord::DELETED_TEXT ) |
595 | ) || |
596 | ( |
597 | $this->mOldRevisionRecord && |
598 | $this->mOldRevisionRecord->isDeleted( RevisionRecord::DELETED_TEXT ) |
599 | ); |
600 | } |
601 | |
602 | /** |
603 | * Get the permission errors associated with the revisions for the current diff. |
604 | * |
605 | * @param Authority $performer |
606 | * @return array[] Array of arrays of the arguments to wfMessage to explain permissions problems. |
607 | */ |
608 | public function getPermissionErrors( Authority $performer ) { |
609 | $this->loadRevisionData(); |
610 | $permStatus = PermissionStatus::newEmpty(); |
611 | if ( $this->mNewPage ) { |
612 | $performer->authorizeRead( 'read', $this->mNewPage, $permStatus ); |
613 | } |
614 | if ( $this->mOldPage ) { |
615 | $performer->authorizeRead( 'read', $this->mOldPage, $permStatus ); |
616 | } |
617 | return $permStatus->toLegacyErrorArray(); |
618 | } |
619 | |
620 | /** |
621 | * Checks whether one of the given Revisions was suppressed |
622 | * |
623 | * @return bool |
624 | */ |
625 | public function hasSuppressedRevision() { |
626 | return $this->hasDeletedRevision() && ( |
627 | ( $this->mOldRevisionRecord && |
628 | $this->mOldRevisionRecord->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) || |
629 | ( $this->mNewRevisionRecord && |
630 | $this->mNewRevisionRecord->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) |
631 | ); |
632 | } |
633 | |
634 | /** |
635 | * Renders user associated edit count |
636 | * |
637 | * @param UserIdentity $user |
638 | * @return string |
639 | */ |
640 | private function getUserEditCount( $user ): string { |
641 | $editCount = $this->userEditTracker->getUserEditCount( $user ); |
642 | if ( $editCount === null ) { |
643 | return ''; |
644 | } |
645 | |
646 | return Html::rawElement( 'div', [ |
647 | 'class' => 'mw-diff-usereditcount', |
648 | ], |
649 | $this->msg( |
650 | 'diff-user-edits', |
651 | $this->getLanguage()->formatNum( $editCount ) |
652 | )->parse() |
653 | ); |
654 | } |
655 | |
656 | /** |
657 | * Renders user roles |
658 | * |
659 | * @param UserIdentity $user |
660 | * @return string |
661 | */ |
662 | private function getUserRoles( UserIdentity $user ) { |
663 | if ( !$this->userIdentityUtils->isNamed( $user ) ) { |
664 | return ''; |
665 | } |
666 | $userGroups = $this->userGroupManager->getUserGroups( $user ); |
667 | $userGroupLinks = []; |
668 | foreach ( $userGroups as $group ) { |
669 | $userGroupLinks[] = UserGroupMembership::getLinkHTML( $group, $this->getContext() ); |
670 | } |
671 | return Html::rawElement( 'div', [ |
672 | 'class' => 'mw-diff-userroles', |
673 | ], $this->getLanguage()->commaList( $userGroupLinks ) ); |
674 | } |
675 | |
676 | /** |
677 | * Renders user associated meta data |
678 | * |
679 | * @param UserIdentity|null $user |
680 | * @return string |
681 | */ |
682 | private function getUserMetaData( ?UserIdentity $user ) { |
683 | if ( !$user ) { |
684 | return ''; |
685 | } |
686 | return Html::rawElement( 'div', [ |
687 | 'class' => 'mw-diff-usermetadata', |
688 | ], $this->getUserRoles( $user ) . $this->getUserEditCount( $user ) ); |
689 | } |
690 | |
691 | /** |
692 | * Checks whether the current user has permission for accessing the revisions of the diff. |
693 | * Note that this does not check whether the user has permission to view the page, it only |
694 | * checks revdelete permissions. |
695 | * |
696 | * It is the caller's responsibility to call |
697 | * $this->getUserPermissionErrors or similar checks. |
698 | * |
699 | * @param Authority $performer |
700 | * @return bool |
701 | */ |
702 | public function isUserAllowedToSeeRevisions( Authority $performer ) { |
703 | $this->loadRevisionData(); |
704 | |
705 | if ( $this->mOldRevisionRecord && !$this->mOldRevisionRecord->userCan( |
706 | RevisionRecord::DELETED_TEXT, |
707 | $performer |
708 | ) ) { |
709 | return false; |
710 | } |
711 | |
712 | // $this->mNewRev will only be falsy if a loading error occurred |
713 | // (in which case the user is allowed to see). |
714 | return !$this->mNewRevisionRecord || $this->mNewRevisionRecord->userCan( |
715 | RevisionRecord::DELETED_TEXT, |
716 | $performer |
717 | ); |
718 | } |
719 | |
720 | /** |
721 | * Checks whether the diff should be hidden from the current user |
722 | * This is based on whether the user is allowed to see it and has specifically asked to see it. |
723 | * |
724 | * @param Authority $performer |
725 | * @return bool |
726 | */ |
727 | public function shouldBeHiddenFromUser( Authority $performer ) { |
728 | return $this->hasDeletedRevision() && ( !$this->unhide || |
729 | !$this->isUserAllowedToSeeRevisions( $performer ) ); |
730 | } |
731 | |
732 | /** |
733 | * @param bool $diffOnly |
734 | */ |
735 | public function showDiffPage( $diffOnly = false ) { |
736 | # Allow frames except in certain special cases |
737 | $out = $this->getOutput(); |
738 | $out->setPreventClickjacking( false ); |
739 | $out->setRobotPolicy( 'noindex,nofollow' ); |
740 | |
741 | // Allow extensions to add any extra output here |
742 | $this->hookRunner->onDifferenceEngineShowDiffPage( $out ); |
743 | |
744 | if ( !$this->loadRevisionData() ) { |
745 | if ( $this->hookRunner->onDifferenceEngineShowDiffPageMaybeShowMissingRevision( $this ) ) { |
746 | $this->showMissingRevision(); |
747 | } |
748 | return; |
749 | } |
750 | |
751 | $user = $this->getUser(); |
752 | $permErrors = $this->getPermissionErrors( $this->getAuthority() ); |
753 | if ( $permErrors ) { |
754 | throw new PermissionsError( 'read', $permErrors ); |
755 | } |
756 | |
757 | $rollback = ''; |
758 | |
759 | $query = $this->extraQueryParams; |
760 | # Carry over 'diffonly' param via navigation links |
761 | if ( $diffOnly != MediaWikiServices::getInstance() |
762 | ->getUserOptionsLookup()->getBoolOption( $user, 'diffonly' ) |
763 | ) { |
764 | $query['diffonly'] = $diffOnly; |
765 | } |
766 | # Cascade unhide param in links for easy deletion browsing |
767 | if ( $this->unhide ) { |
768 | $query['unhide'] = 1; |
769 | } |
770 | |
771 | # Check if one of the revisions is deleted/suppressed |
772 | $deleted = $this->hasDeletedRevision(); |
773 | $suppressed = $this->hasSuppressedRevision(); |
774 | $allowed = $this->isUserAllowedToSeeRevisions( $this->getAuthority() ); |
775 | |
776 | $revisionTools = []; |
777 | $breadCrumbs = ''; |
778 | |
779 | # mOldRevisionRecord is false if the difference engine is called with a "vague" query for |
780 | # a diff between a version V and its previous version V' AND the version V |
781 | # is the first version of that article. In that case, V' does not exist. |
782 | if ( $this->mOldRevisionRecord === false ) { |
783 | if ( $this->mNewPage ) { |
784 | $out->setPageTitleMsg( |
785 | $this->msg( 'difference-title' )->plaintextParams( $this->mNewPage->getPrefixedText() ) |
786 | ); |
787 | } |
788 | $samePage = true; |
789 | $oldHeader = ''; |
790 | // Allow extensions to change the $oldHeader variable |
791 | $this->hookRunner->onDifferenceEngineOldHeaderNoOldRev( $oldHeader ); |
792 | } else { |
793 | $this->hookRunner->onDifferenceEngineViewHeader( $this ); |
794 | |
795 | if ( !$this->mOldPage || !$this->mNewPage ) { |
796 | // XXX say something to the user? |
797 | $samePage = false; |
798 | } elseif ( $this->mNewPage->equals( $this->mOldPage ) ) { |
799 | $out->setPageTitleMsg( |
800 | $this->msg( 'difference-title' )->plaintextParams( $this->mNewPage->getPrefixedText() ) |
801 | ); |
802 | $samePage = true; |
803 | } else { |
804 | $out->setPageTitleMsg( $this->msg( 'difference-title-multipage' )->plaintextParams( |
805 | $this->mOldPage->getPrefixedText(), $this->mNewPage->getPrefixedText() ) ); |
806 | $out->addSubtitle( $this->msg( 'difference-multipage' ) ); |
807 | $samePage = false; |
808 | } |
809 | |
810 | if ( $samePage && $this->mNewPage && |
811 | $this->getAuthority()->probablyCan( 'edit', $this->mNewPage ) |
812 | ) { |
813 | if ( $this->mNewRevisionRecord->isCurrent() && |
814 | $this->getAuthority()->probablyCan( 'rollback', $this->mNewPage ) |
815 | ) { |
816 | $rollbackLink = Linker::generateRollback( |
817 | $this->mNewRevisionRecord, |
818 | $this->getContext(), |
819 | [ 'noBrackets' ] |
820 | ); |
821 | if ( $rollbackLink ) { |
822 | $out->setPreventClickjacking( true ); |
823 | $rollback = "\u{00A0}\u{00A0}\u{00A0}" . $rollbackLink; |
824 | } |
825 | } |
826 | |
827 | if ( $this->userCanEdit( $this->mOldRevisionRecord ) && |
828 | $this->userCanEdit( $this->mNewRevisionRecord ) |
829 | ) { |
830 | $undoLink = $this->linkRenderer->makeKnownLink( |
831 | $this->mNewPage, |
832 | $this->msg( 'editundo' )->text(), |
833 | [ 'title' => Linker::titleAttrib( 'undo' ) ], |
834 | [ |
835 | 'action' => 'edit', |
836 | 'undoafter' => $this->mOldid, |
837 | 'undo' => $this->mNewid |
838 | ] |
839 | ); |
840 | $revisionTools['mw-diff-undo'] = $undoLink; |
841 | } |
842 | } |
843 | # Make "previous revision link" |
844 | $hasPrevious = $samePage && $this->mOldPage && |
845 | $this->revisionStore->getPreviousRevision( $this->mOldRevisionRecord ); |
846 | if ( $hasPrevious ) { |
847 | $prevlinkQuery = [ 'diff' => 'prev', 'oldid' => $this->mOldid ] + $query; |
848 | $prevlink = $this->linkRenderer->makeKnownLink( |
849 | $this->mOldPage, |
850 | $this->msg( 'previousdiff' )->text(), |
851 | [ 'id' => 'differences-prevlink' ], |
852 | $prevlinkQuery |
853 | ); |
854 | $breadCrumbs .= $this->linkRenderer->makeKnownLink( |
855 | $this->mOldPage, |
856 | $this->msg( 'previousdiff' )->text(), |
857 | [ |
858 | 'class' => 'mw-diff-revision-history-link-previous' |
859 | ], |
860 | $prevlinkQuery |
861 | ); |
862 | } else { |
863 | $prevlink = "\u{00A0}"; |
864 | } |
865 | |
866 | if ( $this->mOldRevisionRecord->isMinor() ) { |
867 | $oldminor = ChangesList::flag( 'minor' ); |
868 | } else { |
869 | $oldminor = ''; |
870 | } |
871 | |
872 | $oldRevRecord = $this->mOldRevisionRecord; |
873 | |
874 | $ldel = $this->revisionDeleteLink( $oldRevRecord ); |
875 | $oldRevisionHeader = $this->getRevisionHeader( $oldRevRecord, 'complete' ); |
876 | $oldChangeTags = ChangeTags::formatSummaryRow( $this->mOldTags, 'diff', $this->getContext() ); |
877 | $oldRevComment = $this->commentFormatter |
878 | ->formatRevision( |
879 | $oldRevRecord, $user, !$diffOnly, !$this->unhide, /** disable parentheseses */ false |
880 | ); |
881 | |
882 | if ( $oldRevComment === '' ) { |
883 | $defaultComment = $this->msg( 'changeslist-nocomment' )->escaped(); |
884 | $oldRevComment = "<span class=\"comment mw-comment-none\">$defaultComment</span>"; |
885 | } |
886 | |
887 | $oldHeader = '<div id="mw-diff-otitle1"><strong>' . $oldRevisionHeader . '</strong></div>' . |
888 | '<div id="mw-diff-otitle2">' . |
889 | Linker::revUserTools( $oldRevRecord, !$this->unhide ) . |
890 | $this->getUserMetaData( $oldRevRecord->getUser() ) . |
891 | '</div>' . |
892 | '<div id="mw-diff-otitle3">' . $oldminor . $oldRevComment . $ldel . '</div>' . |
893 | '<div id="mw-diff-otitle5">' . $oldChangeTags[0] . '</div>' . |
894 | '<div id="mw-diff-otitle4">' . $prevlink . '</div>'; |
895 | |
896 | // Allow extensions to change the $oldHeader variable |
897 | $this->hookRunner->onDifferenceEngineOldHeader( |
898 | $this, $oldHeader, $prevlink, $oldminor, $diffOnly, $ldel, $this->unhide ); |
899 | } |
900 | |
901 | $out->addJsConfigVars( [ |
902 | 'wgDiffOldId' => $this->mOldid, |
903 | 'wgDiffNewId' => $this->mNewid, |
904 | ] ); |
905 | |
906 | # Make "next revision link" |
907 | # Skip next link on the top revision |
908 | if ( $samePage && $this->mNewPage && !$this->mNewRevisionRecord->isCurrent() ) { |
909 | $nextlinkQuery = [ 'diff' => 'next', 'oldid' => $this->mNewid ] + $query; |
910 | $nextlink = $this->linkRenderer->makeKnownLink( |
911 | $this->mNewPage, |
912 | $this->msg( 'nextdiff' )->text(), |
913 | [ 'id' => 'differences-nextlink' ], |
914 | $nextlinkQuery |
915 | ); |
916 | $breadCrumbs .= $this->linkRenderer->makeKnownLink( |
917 | $this->mNewPage, |
918 | $this->msg( 'nextdiff' )->text(), |
919 | [ |
920 | 'class' => 'mw-diff-revision-history-link-next' |
921 | ], |
922 | $nextlinkQuery |
923 | ); |
924 | } else { |
925 | $nextlink = "\u{00A0}"; |
926 | } |
927 | |
928 | if ( $this->mNewRevisionRecord->isMinor() ) { |
929 | $newminor = ChangesList::flag( 'minor' ); |
930 | } else { |
931 | $newminor = ''; |
932 | } |
933 | |
934 | # Handle RevisionDelete links... |
935 | $rdel = $this->revisionDeleteLink( $this->mNewRevisionRecord ); |
936 | |
937 | # Allow extensions to define their own revision tools |
938 | $this->hookRunner->onDiffTools( |
939 | $this->mNewRevisionRecord, |
940 | $revisionTools, |
941 | $this->mOldRevisionRecord ?: null, |
942 | $user |
943 | ); |
944 | |
945 | $formattedRevisionTools = []; |
946 | // Put each one in parentheses (poor man's button) |
947 | foreach ( $revisionTools as $key => $tool ) { |
948 | $toolClass = is_string( $key ) ? $key : 'mw-diff-tool'; |
949 | $element = Html::rawElement( |
950 | 'span', |
951 | [ 'class' => $toolClass ], |
952 | $tool |
953 | ); |
954 | $formattedRevisionTools[] = $element; |
955 | } |
956 | |
957 | $newRevRecord = $this->mNewRevisionRecord; |
958 | |
959 | $newRevisionHeader = $this->getRevisionHeader( $newRevRecord, 'complete' ) . |
960 | ' ' . implode( ' ', $formattedRevisionTools ); |
961 | $newChangeTags = ChangeTags::formatSummaryRow( $this->mNewTags, 'diff', $this->getContext() ); |
962 | $newRevComment = $this->commentFormatter->formatRevision( |
963 | $newRevRecord, $user, !$diffOnly, !$this->unhide, /** disable parentheseses */ false |
964 | ); |
965 | |
966 | if ( $newRevComment === '' ) { |
967 | $defaultComment = $this->msg( 'changeslist-nocomment' )->escaped(); |
968 | $newRevComment = "<span class=\"comment mw-comment-none\">$defaultComment</span>"; |
969 | } |
970 | |
971 | $newHeader = '<div id="mw-diff-ntitle1"><strong>' . $newRevisionHeader . '</strong></div>' . |
972 | '<div id="mw-diff-ntitle2">' . Linker::revUserTools( $newRevRecord, !$this->unhide ) . |
973 | $rollback . |
974 | $this->getUserMetaData( $newRevRecord->getUser() ) . |
975 | '</div>' . |
976 | '<div id="mw-diff-ntitle3">' . $newminor . $newRevComment . $rdel . '</div>' . |
977 | '<div id="mw-diff-ntitle5">' . $newChangeTags[0] . '</div>' . |
978 | '<div id="mw-diff-ntitle4">' . $nextlink . $this->markPatrolledLink() . '</div>'; |
979 | |
980 | // Allow extensions to change the $newHeader variable |
981 | $this->hookRunner->onDifferenceEngineNewHeader( $this, $newHeader, |
982 | $formattedRevisionTools, $nextlink, $rollback, $newminor, $diffOnly, |
983 | $rdel, $this->unhide ); |
984 | |
985 | $out->addHTML( |
986 | Html::rawElement( 'div', [ |
987 | 'class' => 'mw-diff-revision-history-links' |
988 | ], $breadCrumbs ) |
989 | ); |
990 | # If the diff cannot be shown due to a deleted revision, then output |
991 | # the diff header and links to unhide (if available)... |
992 | if ( $this->shouldBeHiddenFromUser( $this->getAuthority() ) ) { |
993 | $this->showDiffStyle(); |
994 | $multi = $this->getMultiNotice(); |
995 | $out->addHTML( $this->addHeader( '', $oldHeader, $newHeader, $multi ) ); |
996 | if ( !$allowed ) { |
997 | # Give explanation for why revision is not visible |
998 | $msg = [ $suppressed ? 'rev-suppressed-no-diff' : 'rev-deleted-no-diff' ]; |
999 | } else { |
1000 | # Give explanation and add a link to view the diff... |
1001 | $query = $this->getRequest()->appendQueryValue( 'unhide', '1' ); |
1002 | $msg = [ |
1003 | $suppressed ? 'rev-suppressed-unhide-diff' : 'rev-deleted-unhide-diff', |
1004 | $this->getTitle()->getFullURL( $query ) |
1005 | ]; |
1006 | } |
1007 | $out->addHTML( Html::warningBox( $this->msg( ...$msg )->parse(), 'plainlinks' ) ); |
1008 | # Otherwise, output a regular diff... |
1009 | } else { |
1010 | # Add deletion notice if the user is viewing deleted content |
1011 | $notice = ''; |
1012 | if ( $deleted ) { |
1013 | $msg = $suppressed ? 'rev-suppressed-diff-view' : 'rev-deleted-diff-view'; |
1014 | $notice = Html::warningBox( $this->msg( $msg )->parse(), 'plainlinks' ); |
1015 | } |
1016 | |
1017 | # Add an error if the content can't be loaded |
1018 | $this->getSlotContents(); |
1019 | foreach ( $this->getRevisionLoadErrors() as $msg ) { |
1020 | $notice .= Html::warningBox( $msg->parse() ); |
1021 | } |
1022 | |
1023 | // Check if inline switcher will be needed |
1024 | if ( $this->getTextDiffer()->hasFormat( 'inline' ) ) { |
1025 | $out->enableOOUI(); |
1026 | } |
1027 | |
1028 | $this->showTablePrefixes(); |
1029 | $this->showDiff( $oldHeader, $newHeader, $notice ); |
1030 | if ( !$diffOnly ) { |
1031 | $this->renderNewRevision(); |
1032 | } |
1033 | |
1034 | // Allow extensions to optionally not show the final patrolled link |
1035 | if ( $this->hookRunner->onDifferenceEngineRenderRevisionShowFinalPatrolLink() ) { |
1036 | # Add redundant patrol link on bottom... |
1037 | $out->addHTML( $this->markPatrolledLink() ); |
1038 | } |
1039 | } |
1040 | } |
1041 | |
1042 | /** |
1043 | * Add table prefixes |
1044 | */ |
1045 | private function showTablePrefixes() { |
1046 | $parts = []; |
1047 | foreach ( $this->getSlotDiffRenderers() as $slotDiffRenderer ) { |
1048 | $parts += $slotDiffRenderer->getTablePrefix( $this->getContext(), $this->mNewPage ); |
1049 | } |
1050 | ksort( $parts ); |
1051 | if ( count( array_filter( $parts ) ) > 0 ) { |
1052 | $language = $this->getLanguage(); |
1053 | $attrs = [ |
1054 | 'class' => 'mw-diff-table-prefix', |
1055 | 'dir' => $language->getDir(), |
1056 | 'lang' => $language->getCode(), |
1057 | ]; |
1058 | $this->getOutput()->addHTML( |
1059 | Html::rawElement( 'div', $attrs, implode( '', $parts ) ) ); |
1060 | } |
1061 | } |
1062 | |
1063 | /** |
1064 | * Build a link to mark a change as patrolled. |
1065 | * |
1066 | * Returns empty string if there's either no revision to patrol or the user is not allowed to. |
1067 | * |
1068 | * Side effect: When the patrol link is build, this method will call |
1069 | * OutputPage::setPreventClickjacking(true) and load a JS module. |
1070 | * |
1071 | * @return string HTML or empty string |
1072 | */ |
1073 | public function markPatrolledLink() { |
1074 | if ( $this->mMarkPatrolledLink === null ) { |
1075 | $linkInfo = $this->getMarkPatrolledLinkInfo(); |
1076 | // If false, there is no patrol link needed/allowed |
1077 | if ( !$linkInfo || !$this->mNewPage ) { |
1078 | $this->mMarkPatrolledLink = ''; |
1079 | } else { |
1080 | $patrolLinkClass = 'patrollink'; |
1081 | $this->mMarkPatrolledLink = ' <span class="' . $patrolLinkClass . '" data-mw="interface">[' . |
1082 | $this->linkRenderer->makeKnownLink( |
1083 | $this->mNewPage, |
1084 | $this->msg( 'markaspatrolleddiff' )->text(), |
1085 | [], |
1086 | [ |
1087 | 'action' => 'markpatrolled', |
1088 | 'rcid' => $linkInfo['rcid'], |
1089 | ] |
1090 | ) . ']</span>'; |
1091 | // Allow extensions to change the markpatrolled link |
1092 | $this->hookRunner->onDifferenceEngineMarkPatrolledLink( $this, |
1093 | $this->mMarkPatrolledLink, $linkInfo['rcid'] ); |
1094 | } |
1095 | } |
1096 | return $this->mMarkPatrolledLink; |
1097 | } |
1098 | |
1099 | /** |
1100 | * Returns an array of meta data needed to build a "mark as patrolled" link and |
1101 | * adds a JS module to the output. |
1102 | * |
1103 | * @return array|false An array of meta data for a patrol link (rcid only) |
1104 | * or false if no link is needed |
1105 | */ |
1106 | protected function getMarkPatrolledLinkInfo() { |
1107 | $user = $this->getUser(); |
1108 | $config = $this->getConfig(); |
1109 | |
1110 | // Prepare a change patrol link, if applicable |
1111 | if ( |
1112 | // Is patrolling enabled and the user allowed to? |
1113 | $config->get( MainConfigNames::UseRCPatrol ) && |
1114 | $this->mNewPage && |
1115 | $this->getAuthority()->probablyCan( 'patrol', $this->mNewPage ) && |
1116 | // Only do this if the revision isn't more than 6 hours older |
1117 | // than the Max RC age (6h because the RC might not be cleaned out regularly) |
1118 | RecentChange::isInRCLifespan( $this->mNewRevisionRecord->getTimestamp(), 21600 ) |
1119 | ) { |
1120 | // Look for an unpatrolled change corresponding to this diff |
1121 | $change = RecentChange::newFromConds( |
1122 | [ |
1123 | 'rc_this_oldid' => $this->mNewid, |
1124 | 'rc_patrolled' => RecentChange::PRC_UNPATROLLED |
1125 | ], |
1126 | __METHOD__ |
1127 | ); |
1128 | |
1129 | if ( $change && !$change->getPerformerIdentity()->equals( $user ) ) { |
1130 | $rcid = $change->getAttribute( 'rc_id' ); |
1131 | } else { |
1132 | // None found or the page has been created by the current user. |
1133 | // If the user could patrol this it already would be patrolled |
1134 | $rcid = 0; |
1135 | } |
1136 | |
1137 | // Allow extensions to possibly change the rcid here |
1138 | // For example the rcid might be set to zero due to the user |
1139 | // being the same as the performer of the change but an extension |
1140 | // might still want to show it under certain conditions |
1141 | $this->hookRunner->onDifferenceEngineMarkPatrolledRCID( $rcid, $this, $change, $user ); |
1142 | |
1143 | // Build the link |
1144 | if ( $rcid ) { |
1145 | $this->getOutput()->setPreventClickjacking( true ); |
1146 | if ( $this->getAuthority()->isAllowed( 'writeapi' ) ) { |
1147 | $this->getOutput()->addModules( 'mediawiki.misc-authed-curate' ); |
1148 | } |
1149 | |
1150 | return [ 'rcid' => $rcid ]; |
1151 | } |
1152 | } |
1153 | |
1154 | // No mark as patrolled link applicable |
1155 | return false; |
1156 | } |
1157 | |
1158 | /** |
1159 | * @param RevisionRecord $revRecord |
1160 | * |
1161 | * @return string |
1162 | */ |
1163 | private function revisionDeleteLink( RevisionRecord $revRecord ) { |
1164 | $link = Linker::getRevDeleteLink( |
1165 | $this->getAuthority(), |
1166 | $revRecord, |
1167 | $revRecord->getPageAsLinkTarget() |
1168 | ); |
1169 | if ( $link !== '' ) { |
1170 | $link = "\u{00A0}\u{00A0}\u{00A0}" . $link . ' '; |
1171 | } |
1172 | |
1173 | return $link; |
1174 | } |
1175 | |
1176 | /** |
1177 | * Show the new revision of the page. |
1178 | * |
1179 | * @note Not supported after calling setContent(). |
1180 | */ |
1181 | public function renderNewRevision() { |
1182 | if ( $this->isContentOverridden ) { |
1183 | // The code below only works with a RevisionRecord object. We could construct a |
1184 | // fake RevisionRecord (here or in setContent), but since this does not seem |
1185 | // needed at the moment, we'll just fail for now. |
1186 | throw new LogicException( |
1187 | __METHOD__ |
1188 | . ' is not supported after calling setContent(). Use setRevisions() instead.' |
1189 | ); |
1190 | } |
1191 | |
1192 | $out = $this->getOutput(); |
1193 | $revHeader = $this->getRevisionHeader( $this->mNewRevisionRecord ); |
1194 | # Add "current version as of X" title |
1195 | $out->addHTML( "<hr class='diff-hr' id='mw-oldid' /> |
1196 | <h2 class='diff-currentversion-title'>{$revHeader}</h2>\n" ); |
1197 | # Page content may be handled by a hooked call instead... |
1198 | if ( $this->hookRunner->onArticleContentOnDiff( $this, $out ) ) { |
1199 | $this->loadNewText(); |
1200 | if ( !$this->mNewPage ) { |
1201 | // New revision is unsaved; bail out. |
1202 | // TODO in theory rendering the new revision is a meaningful thing to do |
1203 | // even if it's unsaved, but a lot of untangling is required to do it safely. |
1204 | return; |
1205 | } |
1206 | if ( $this->hasNewRevisionLoadError() ) { |
1207 | // There was an error loading the new revision |
1208 | return; |
1209 | } |
1210 | |
1211 | $out->setRevisionId( $this->mNewid ); |
1212 | $out->setRevisionIsCurrent( $this->mNewRevisionRecord->isCurrent() ); |
1213 | $out->setRevisionTimestamp( $this->mNewRevisionRecord->getTimestamp() ); |
1214 | $out->setArticleFlag( true ); |
1215 | |
1216 | if ( !$this->hookRunner->onArticleRevisionViewCustom( |
1217 | $this->mNewRevisionRecord, $this->mNewPage, $this->mOldid, $out ) |
1218 | ) { |
1219 | // Handled by extension |
1220 | // NOTE: sync with hooks called in Article::view() |
1221 | } else { |
1222 | // Normal page |
1223 | if ( $this->getTitle()->equals( $this->mNewPage ) ) { |
1224 | // If the Title stored in the context is the same as the one |
1225 | // of the new revision, we can use its associated WikiPage |
1226 | // object. |
1227 | $wikiPage = $this->getWikiPage(); |
1228 | } else { |
1229 | // Otherwise we need to create our own WikiPage object |
1230 | $wikiPage = $this->wikiPageFactory->newFromTitle( $this->mNewPage ); |
1231 | } |
1232 | |
1233 | $parserOptions = $wikiPage->makeParserOptions( $this->getContext() ); |
1234 | $parserOptions->setRenderReason( 'diff-page' ); |
1235 | |
1236 | $parserOutputAccess = MediaWikiServices::getInstance()->getParserOutputAccess(); |
1237 | $status = $parserOutputAccess->getParserOutput( |
1238 | $wikiPage, |
1239 | $parserOptions, |
1240 | $this->mNewRevisionRecord, |
1241 | // we already checked |
1242 | ParserOutputAccess::OPT_NO_AUDIENCE_CHECK | |
1243 | // Update cascading protection |
1244 | ParserOutputAccess::OPT_LINKS_UPDATE |
1245 | ); |
1246 | if ( $status->isOK() ) { |
1247 | $parserOutput = $status->getValue(); |
1248 | // Allow extensions to change parser output here |
1249 | if ( $this->hookRunner->onDifferenceEngineRenderRevisionAddParserOutput( |
1250 | $this, $out, $parserOutput, $wikiPage ) |
1251 | ) { |
1252 | $out->addParserOutput( $parserOutput, [ |
1253 | 'enableSectionEditLinks' => $this->mNewRevisionRecord->isCurrent() |
1254 | && $this->getAuthority()->probablyCan( |
1255 | 'edit', |
1256 | $this->mNewRevisionRecord->getPage() |
1257 | ), |
1258 | 'absoluteURLs' => $this->slotDiffOptions['expand-url'] ?? false |
1259 | ] ); |
1260 | } |
1261 | } else { |
1262 | $out->addHTML( |
1263 | Html::errorBox( |
1264 | $out->parseAsInterface( |
1265 | $status->getWikiText( false, false, $this->getLanguage() ) |
1266 | ) |
1267 | ) |
1268 | ); |
1269 | } |
1270 | } |
1271 | } |
1272 | } |
1273 | |
1274 | /** |
1275 | * Get the diff text, send it to the OutputPage object |
1276 | * Returns false if the diff could not be generated, otherwise returns true |
1277 | * |
1278 | * @param string|false $otitle Header for old text or false |
1279 | * @param string|false $ntitle Header for new text or false |
1280 | * @param string $notice HTML between diff header and body |
1281 | * |
1282 | * @return bool |
1283 | */ |
1284 | public function showDiff( $otitle, $ntitle, $notice = '' ) { |
1285 | // Allow extensions to affect the output here |
1286 | $this->hookRunner->onDifferenceEngineShowDiff( $this ); |
1287 | |
1288 | $diff = $this->getDiff( $otitle, $ntitle, $notice ); |
1289 | if ( $diff === false ) { |
1290 | $this->showMissingRevision(); |
1291 | return false; |
1292 | } |
1293 | |
1294 | $this->showDiffStyle(); |
1295 | if ( $this->slotDiffOptions['expand-url'] ?? false ) { |
1296 | $diff = Linker::expandLocalLinks( $diff ); |
1297 | } |
1298 | $this->getOutput()->addHTML( $diff ); |
1299 | return true; |
1300 | } |
1301 | |
1302 | /** |
1303 | * Add style sheets for diff display. |
1304 | */ |
1305 | public function showDiffStyle() { |
1306 | if ( !$this->isSlotDiffRenderer ) { |
1307 | $this->getOutput()->addModules( 'mediawiki.diff' ); |
1308 | $this->getOutput()->addModuleStyles( [ |
1309 | 'mediawiki.interface.helpers.styles', |
1310 | 'mediawiki.diff.styles' |
1311 | ] ); |
1312 | foreach ( $this->getSlotDiffRenderers() as $slotDiffRenderer ) { |
1313 | $slotDiffRenderer->addModules( $this->getOutput() ); |
1314 | } |
1315 | } |
1316 | } |
1317 | |
1318 | /** |
1319 | * Get complete diff table, including header |
1320 | * |
1321 | * @param string|false $otitle Header for old text or false |
1322 | * @param string|false $ntitle Header for new text or false |
1323 | * @param string $notice HTML between diff header and body |
1324 | * |
1325 | * @return string|false |
1326 | */ |
1327 | public function getDiff( $otitle, $ntitle, $notice = '' ) { |
1328 | $body = $this->getDiffBody(); |
1329 | if ( $body === false ) { |
1330 | return false; |
1331 | } |
1332 | |
1333 | $multi = $this->getMultiNotice(); |
1334 | // Display a message when the diff is empty |
1335 | if ( $body === '' ) { |
1336 | $notice .= '<div class="mw-diff-empty">' . |
1337 | $this->msg( 'diff-empty' )->parse() . |
1338 | "</div>\n"; |
1339 | } |
1340 | |
1341 | if ( $this->cacheHitKey !== null ) { |
1342 | $body .= "\n<!-- diff cache key " . htmlspecialchars( $this->cacheHitKey ) . " -->\n"; |
1343 | } |
1344 | |
1345 | return $this->addHeader( $body, $otitle, $ntitle, $multi, $notice ); |
1346 | } |
1347 | |
1348 | /** |
1349 | * Get the diff table body, without header |
1350 | * |
1351 | * @return string|false |
1352 | */ |
1353 | public function getDiffBody() { |
1354 | $this->mCacheHit = true; |
1355 | // Check if the diff should be hidden from this user |
1356 | if ( !$this->isContentOverridden ) { |
1357 | if ( !$this->loadRevisionData() ) { |
1358 | return false; |
1359 | } elseif ( $this->mOldRevisionRecord && |
1360 | !$this->mOldRevisionRecord->userCan( |
1361 | RevisionRecord::DELETED_TEXT, |
1362 | $this->getAuthority() |
1363 | ) |
1364 | ) { |
1365 | return false; |
1366 | } elseif ( $this->mNewRevisionRecord && |
1367 | !$this->mNewRevisionRecord->userCan( |
1368 | RevisionRecord::DELETED_TEXT, |
1369 | $this->getAuthority() |
1370 | ) ) { |
1371 | return false; |
1372 | } |
1373 | // Short-circuit |
1374 | if ( $this->mOldRevisionRecord === false || ( |
1375 | $this->mOldRevisionRecord && |
1376 | $this->mNewRevisionRecord && |
1377 | $this->mOldRevisionRecord->getId() && |
1378 | $this->mOldRevisionRecord->getId() == $this->mNewRevisionRecord->getId() |
1379 | ) ) { |
1380 | if ( $this->hookRunner->onDifferenceEngineShowEmptyOldContent( $this ) ) { |
1381 | return ''; |
1382 | } |
1383 | } |
1384 | } |
1385 | |
1386 | // Cacheable? |
1387 | $key = false; |
1388 | $services = MediaWikiServices::getInstance(); |
1389 | $cache = $services->getMainWANObjectCache(); |
1390 | $stats = $services->getStatsdDataFactory(); |
1391 | if ( $this->mOldid && $this->mNewid ) { |
1392 | $key = $cache->makeKey( ...$this->getDiffBodyCacheKeyParams() ); |
1393 | |
1394 | // Try cache |
1395 | if ( !$this->mRefreshCache ) { |
1396 | $difftext = $cache->get( $key ); |
1397 | if ( is_string( $difftext ) ) { |
1398 | $stats->updateCount( 'diff_cache.hit', 1 ); |
1399 | $difftext = $this->localiseDiff( $difftext ); |
1400 | $this->cacheHitKey = $key; |
1401 | return $difftext; |
1402 | } |
1403 | } // don't try to load but save the result |
1404 | } |
1405 | $this->mCacheHit = false; |
1406 | $this->cacheHitKey = null; |
1407 | |
1408 | // Loadtext is permission safe, this just clears out the diff |
1409 | if ( !$this->loadText() ) { |
1410 | return false; |
1411 | } |
1412 | |
1413 | $difftext = ''; |
1414 | // We've checked for revdelete at the beginning of this method; it's OK to ignore |
1415 | // read permissions here. |
1416 | $slotContents = $this->getSlotContents(); |
1417 | foreach ( $this->getSlotDiffRenderers() as $role => $slotDiffRenderer ) { |
1418 | try { |
1419 | $slotDiff = $slotDiffRenderer->getDiff( $slotContents[$role]['old'], |
1420 | $slotContents[$role]['new'] ); |
1421 | } catch ( IncompatibleDiffTypesException $e ) { |
1422 | $slotDiff = $this->getSlotError( $e->getMessageObject()->parse() ); |
1423 | } |
1424 | if ( $slotDiff && $role !== SlotRecord::MAIN ) { |
1425 | // FIXME: ask SlotRoleHandler::getSlotNameMessage |
1426 | $slotTitle = $role; |
1427 | $difftext .= $this->getSlotHeader( $slotTitle ); |
1428 | } |
1429 | $difftext .= $slotDiff; |
1430 | } |
1431 | |
1432 | // Save to cache for 7 days |
1433 | if ( !$this->hookRunner->onAbortDiffCache( $this ) ) { |
1434 | $stats->updateCount( 'diff_cache.uncacheable', 1 ); |
1435 | } elseif ( $key !== false ) { |
1436 | $stats->updateCount( 'diff_cache.miss', 1 ); |
1437 | $cache->set( $key, $difftext, 7 * 86400 ); |
1438 | } else { |
1439 | $stats->updateCount( 'diff_cache.uncacheable', 1 ); |
1440 | } |
1441 | // localise line numbers and title attribute text |
1442 | $difftext = $this->localiseDiff( $difftext ); |
1443 | |
1444 | return $difftext; |
1445 | } |
1446 | |
1447 | /** |
1448 | * Get the diff table body for one slot, without header |
1449 | * |
1450 | * @param string $role |
1451 | * @return string|false |
1452 | */ |
1453 | public function getDiffBodyForRole( $role ) { |
1454 | $diffRenderers = $this->getSlotDiffRenderers(); |
1455 | if ( !isset( $diffRenderers[$role] ) ) { |
1456 | return false; |
1457 | } |
1458 | |
1459 | $slotContents = $this->getSlotContents(); |
1460 | try { |
1461 | $slotDiff = $diffRenderers[$role]->getDiff( $slotContents[$role]['old'], |
1462 | $slotContents[$role]['new'] ); |
1463 | } catch ( IncompatibleDiffTypesException $e ) { |
1464 | $slotDiff = $this->getSlotError( $e->getMessageObject()->parse() ); |
1465 | } |
1466 | if ( $slotDiff === '' ) { |
1467 | return false; |
1468 | } |
1469 | |
1470 | if ( $role !== SlotRecord::MAIN ) { |
1471 | // TODO use human-readable role name at least |
1472 | $slotTitle = $role; |
1473 | $slotDiff = $this->getSlotHeader( $slotTitle ) . $slotDiff; |
1474 | } |
1475 | |
1476 | return $this->localiseDiff( $slotDiff ); |
1477 | } |
1478 | |
1479 | /** |
1480 | * Get a slot header for inclusion in a diff body (as a table row). |
1481 | * |
1482 | * @param string $headerText The text of the header |
1483 | * @return string |
1484 | * |
1485 | */ |
1486 | protected function getSlotHeader( $headerText ) { |
1487 | // The old revision is missing on oldid=<first>&diff=prev; only 2 columns in that case. |
1488 | $columnCount = $this->mOldRevisionRecord ? 4 : 2; |
1489 | $userLang = $this->getLanguage()->getHtmlCode(); |
1490 | return Html::rawElement( 'tr', [ 'class' => 'mw-diff-slot-header', 'lang' => $userLang ], |
1491 | Html::element( 'th', [ 'colspan' => $columnCount ], $headerText ) ); |
1492 | } |
1493 | |
1494 | /** |
1495 | * Get an error message for inclusion in a diff body (as a table row). |
1496 | * |
1497 | * @param string $errorText The HTML of the error |
1498 | * @return string |
1499 | */ |
1500 | protected function getSlotError( $errorText ) { |
1501 | // The old revision is missing on oldid=<first>&diff=prev; only 2 columns in that case. |
1502 | $columnCount = $this->mOldRevisionRecord ? 4 : 2; |
1503 | $userLang = $this->getLanguage()->getHtmlCode(); |
1504 | return Html::rawElement( 'tr', [ 'class' => 'mw-diff-slot-error', 'lang' => $userLang ], |
1505 | Html::rawElement( 'td', [ 'colspan' => $columnCount ], $errorText ) ); |
1506 | } |
1507 | |
1508 | /** |
1509 | * Get the cache key parameters |
1510 | * |
1511 | * Subclasses can replace the first element in the array to something |
1512 | * more specific to the type of diff (e.g. "inline-diff"), or append |
1513 | * if the cache should vary on more things. Overriding entirely should |
1514 | * be avoided. |
1515 | * |
1516 | * @since 1.31 |
1517 | * |
1518 | * @return string[] |
1519 | * @phan-return non-empty-array<string> |
1520 | */ |
1521 | protected function getDiffBodyCacheKeyParams() { |
1522 | if ( !$this->mOldid || !$this->mNewid ) { |
1523 | throw new BadMethodCallException( 'mOldid and mNewid must be set to get diff cache key.' ); |
1524 | } |
1525 | |
1526 | $params = [ |
1527 | 'diff', |
1528 | self::DIFF_VERSION, |
1529 | "old-{$this->mOldid}", |
1530 | "rev-{$this->mNewid}" |
1531 | ]; |
1532 | |
1533 | $extraKeys = []; |
1534 | if ( !$this->isSlotDiffRenderer ) { |
1535 | foreach ( $this->getSlotDiffRenderers() as $slotDiffRenderer ) { |
1536 | $extraKeys = array_merge( $extraKeys, $slotDiffRenderer->getExtraCacheKeys() ); |
1537 | } |
1538 | } |
1539 | ksort( $extraKeys ); |
1540 | return array_merge( $params, array_values( $extraKeys ) ); |
1541 | } |
1542 | |
1543 | /** |
1544 | * Implements DifferenceEngineSlotDiffRenderer::getExtraCacheKeys(). Only used when |
1545 | * DifferenceEngine is wrapped in DifferenceEngineSlotDiffRenderer. |
1546 | * @return string[] |
1547 | * @internal for use by DifferenceEngineSlotDiffRenderer only |
1548 | * @deprecated |
1549 | */ |
1550 | public function getExtraCacheKeys() { |
1551 | // This method is called when the DifferenceEngine is used for a slot diff. We only care |
1552 | // about special things, not the revision IDs, which are added to the cache key by the |
1553 | // page-level DifferenceEngine, and which might not have a valid value for this object. |
1554 | $this->mOldid = 123456789; |
1555 | $this->mNewid = 987654321; |
1556 | |
1557 | // This will repeat a bunch of unnecessary key fields for each slot. Not nice but harmless. |
1558 | $params = $this->getDiffBodyCacheKeyParams(); |
1559 | |
1560 | // Try to get rid of the standard keys to keep the cache key human-readable: |
1561 | // call the getDiffBodyCacheKeyParams implementation of the base class, and if |
1562 | // the child class includes the same keys, drop them. |
1563 | // Uses an obscure PHP feature where static calls to non-static methods are allowed |
1564 | // as long as we are already in a non-static method of the same class, and the call context |
1565 | // ($this) will be inherited. |
1566 | // phpcs:ignore Squiz.Classes.SelfMemberReference.NotUsed |
1567 | $standardParams = DifferenceEngine::getDiffBodyCacheKeyParams(); |
1568 | if ( array_slice( $params, 0, count( $standardParams ) ) === $standardParams ) { |
1569 | $params = array_slice( $params, count( $standardParams ) ); |
1570 | } |
1571 | |
1572 | return $params; |
1573 | } |
1574 | |
1575 | /** |
1576 | * @param array $options for the difference engine. Available options: |
1577 | * - diff-type: The text diff format, e.g. "table" or "inline". If the |
1578 | * specified format is not supported, the option will be ignored, so |
1579 | * the site default format (table) will be used. |
1580 | * - expand-url: If true, put full URLs in href attributes (for action=render) |
1581 | * FIXME: expand-url is not a slot diff option, it is a DifferenceEngine option. |
1582 | * - inline-toggle: If true, and the inline format is supported |
1583 | * a format selector toggle switch will be shown. |
1584 | */ |
1585 | public function setSlotDiffOptions( $options ) { |
1586 | $validatedOptions = []; |
1587 | if ( isset( $options['diff-type'] ) |
1588 | && $this->getTextDiffer()->hasFormat( $options['diff-type'] ) |
1589 | ) { |
1590 | $validatedOptions['diff-type'] = $options['diff-type']; |
1591 | } |
1592 | if ( !empty( $options['expand-url'] ) ) { |
1593 | $validatedOptions['expand-url'] = true; |
1594 | } |
1595 | if ( !empty( $options['inline-toggle'] ) ) { |
1596 | $validatedOptions['inline-toggle'] = true; |
1597 | } |
1598 | $this->slotDiffOptions = $validatedOptions; |
1599 | } |
1600 | |
1601 | /** |
1602 | * Set query parameters to append to diff page links |
1603 | * |
1604 | * @since 1.41 |
1605 | * |
1606 | * @param array $params |
1607 | */ |
1608 | public function setExtraQueryParams( $params ) { |
1609 | $this->extraQueryParams = $params; |
1610 | } |
1611 | |
1612 | /** |
1613 | * Generate a diff, no caching. |
1614 | * |
1615 | * @since 1.21 |
1616 | * |
1617 | * @param Content $old Old content |
1618 | * @param Content $new New content |
1619 | * |
1620 | * @throws Exception If old or new content is not an instance of TextContent. |
1621 | * @return bool|string |
1622 | * |
1623 | * @deprecated since 1.32, use a SlotDiffRenderer instead. |
1624 | */ |
1625 | public function generateContentDiffBody( Content $old, Content $new ) { |
1626 | $slotDiffRenderer = $new->getContentHandler()->getSlotDiffRenderer( $this->getContext() ); |
1627 | if ( |
1628 | $slotDiffRenderer instanceof DifferenceEngineSlotDiffRenderer |
1629 | && $this->isSlotDiffRenderer |
1630 | ) { |
1631 | // Oops, we are just about to enter an infinite loop (the slot-level DifferenceEngine |
1632 | // called a DifferenceEngineSlotDiffRenderer that wraps the same DifferenceEngine class). |
1633 | // This will happen when a content model has no custom slot diff renderer, it does have |
1634 | // a custom difference engine, but that does not override this method. |
1635 | throw new LogicException( get_class( $this ) . ': could not maintain backwards compatibility. ' |
1636 | . 'Please use a SlotDiffRenderer.' ); |
1637 | } |
1638 | return $slotDiffRenderer->getDiff( $old, $new ) . $this->getDebugString(); |
1639 | } |
1640 | |
1641 | /** |
1642 | * Generate a diff, no caching |
1643 | * |
1644 | * @param string $otext Old text, must be already segmented |
1645 | * @param string $ntext New text, must be already segmented |
1646 | * |
1647 | * @throws LogicException If content handling for text content is configured in a way |
1648 | * that makes maintaining B/C hard. |
1649 | * @return bool|string |
1650 | * |
1651 | * @deprecated since 1.32, use a TextSlotDiffRenderer instead. |
1652 | */ |
1653 | public function generateTextDiffBody( $otext, $ntext ) { |
1654 | $slotDiffRenderer = $this->contentHandlerFactory |
1655 | ->getContentHandler( CONTENT_MODEL_TEXT ) |
1656 | ->getSlotDiffRenderer( $this->getContext() ); |
1657 | if ( !( $slotDiffRenderer instanceof TextSlotDiffRenderer ) ) { |
1658 | // Someone used the GetSlotDiffRenderer hook to replace the renderer. |
1659 | // This is too unlikely to happen to bother handling properly. |
1660 | throw new LogicException( 'The slot diff renderer for text content should be a ' |
1661 | . 'TextSlotDiffRenderer subclass' ); |
1662 | } |
1663 | return $slotDiffRenderer->getTextDiff( $otext, $ntext ) . $this->getDebugString(); |
1664 | } |
1665 | |
1666 | /** |
1667 | * Process DiffEngine config and get a sensible, usable engine |
1668 | * |
1669 | * @return string 'wikidiff2', 'php', or path to an executable |
1670 | * @internal For use by this class and within Core only. |
1671 | */ |
1672 | public static function getEngine() { |
1673 | $differenceEngine = new self; |
1674 | $engine = $differenceEngine->getTextDiffer()->getEngineForFormat( 'table' ); |
1675 | if ( $engine === 'external' ) { |
1676 | return MediaWikiServices::getInstance()->getMainConfig() |
1677 | ->get( MainConfigNames::ExternalDiffEngine ); |
1678 | } else { |
1679 | return $engine; |
1680 | } |
1681 | } |
1682 | |
1683 | /** |
1684 | * Generate a debug comment indicating diff generating time, |
1685 | * server node, and generator backend. |
1686 | * |
1687 | * @param string $generator What diff engine was used |
1688 | * |
1689 | * @return string |
1690 | */ |
1691 | protected function debug( $generator = "internal" ) { |
1692 | if ( !$this->enableDebugComment ) { |
1693 | return ''; |
1694 | } |
1695 | $data = [ $generator ]; |
1696 | if ( $this->getConfig()->get( MainConfigNames::ShowHostnames ) ) { |
1697 | $data[] = wfHostname(); |
1698 | } |
1699 | $data[] = wfTimestamp( TS_DB ); |
1700 | |
1701 | return "<!-- diff generator: " . |
1702 | implode( " ", array_map( "htmlspecialchars", $data ) ) . |
1703 | " -->\n"; |
1704 | } |
1705 | |
1706 | /** |
1707 | * @return string |
1708 | */ |
1709 | private function getDebugString() { |
1710 | $engine = self::getEngine(); |
1711 | if ( $engine === 'wikidiff2' ) { |
1712 | return $this->debug( 'wikidiff2' ); |
1713 | } elseif ( $engine === 'php' ) { |
1714 | return $this->debug( 'native PHP' ); |
1715 | } else { |
1716 | return $this->debug( "external $engine" ); |
1717 | } |
1718 | } |
1719 | |
1720 | /** |
1721 | * Localise diff output |
1722 | * |
1723 | * @param string $text |
1724 | * @return string |
1725 | */ |
1726 | private function localiseDiff( $text ) { |
1727 | return $this->getTextDiffer()->localize( $this->getTextDiffFormat(), $text ); |
1728 | } |
1729 | |
1730 | /** |
1731 | * Replace a common convention for language-independent line numbers with |
1732 | * the text in the user's language. |
1733 | * |
1734 | * @deprecated since 1.41, use BaseTextDiffer::localizeLineNumbers() |
1735 | * @param string $text |
1736 | * @return string |
1737 | */ |
1738 | public function localiseLineNumbers( $text ) { |
1739 | return preg_replace_callback( '/<!--LINE (\d+)-->/', |
1740 | function ( array $matches ) { |
1741 | if ( $matches[1] === '1' && $this->mReducedLineNumbers ) { |
1742 | return ''; |
1743 | } |
1744 | return $this->msg( 'lineno' )->numParams( $matches[1] )->escaped(); |
1745 | }, $text ); |
1746 | } |
1747 | |
1748 | /** |
1749 | * If there are revisions between the ones being compared, return a note saying so. |
1750 | * |
1751 | * @return string |
1752 | */ |
1753 | public function getMultiNotice() { |
1754 | // The notice only make sense if we are diffing two saved revisions of the same page. |
1755 | if ( |
1756 | !$this->mOldRevisionRecord || !$this->mNewRevisionRecord |
1757 | || !$this->mOldPage || !$this->mNewPage |
1758 | || !$this->mOldPage->equals( $this->mNewPage ) |
1759 | || $this->mOldRevisionRecord->getId() === null |
1760 | || $this->mNewRevisionRecord->getId() === null |
1761 | // (T237709) Deleted revs might have different page IDs |
1762 | || $this->mNewPage->getArticleID() !== $this->mOldRevisionRecord->getPageId() |
1763 | || $this->mNewPage->getArticleID() !== $this->mNewRevisionRecord->getPageId() |
1764 | ) { |
1765 | return ''; |
1766 | } |
1767 | |
1768 | if ( $this->mOldRevisionRecord->getTimestamp() > $this->mNewRevisionRecord->getTimestamp() ) { |
1769 | $oldRevRecord = $this->mNewRevisionRecord; // flip |
1770 | $newRevRecord = $this->mOldRevisionRecord; // flip |
1771 | } else { // normal case |
1772 | $oldRevRecord = $this->mOldRevisionRecord; |
1773 | $newRevRecord = $this->mNewRevisionRecord; |
1774 | } |
1775 | |
1776 | // Don't show the notice if too many rows must be scanned |
1777 | // @todo show some special message for that case |
1778 | $nEdits = 0; |
1779 | $revisionIdList = $this->revisionStore->getRevisionIdsBetween( |
1780 | $this->mNewPage->getArticleID(), |
1781 | $oldRevRecord, |
1782 | $newRevRecord, |
1783 | 1000 |
1784 | ); |
1785 | // only count revisions that are visible |
1786 | if ( count( $revisionIdList ) > 0 ) { |
1787 | foreach ( $revisionIdList as $revisionId ) { |
1788 | $revision = $this->revisionStore->getRevisionById( $revisionId ); |
1789 | if ( $revision->getUser( RevisionRecord::FOR_THIS_USER, $this->getAuthority() ) ) { |
1790 | $nEdits++; |
1791 | } |
1792 | } |
1793 | } |
1794 | if ( $nEdits > 0 && $nEdits <= 1000 ) { |
1795 | // Use an invalid username to get the wiki's default gender (as fallback) |
1796 | $newRevUserForGender = '[HIDDEN]'; |
1797 | $limit = 100; // use diff-multi-manyusers if too many users |
1798 | try { |
1799 | $users = $this->revisionStore->getAuthorsBetween( |
1800 | $this->mNewPage->getArticleID(), |
1801 | $oldRevRecord, |
1802 | $newRevRecord, |
1803 | null, |
1804 | $limit |
1805 | ); |
1806 | $numUsers = count( $users ); |
1807 | |
1808 | $newRevUser = $newRevRecord->getUser( RevisionRecord::RAW ); |
1809 | $newRevUserText = $newRevUser ? $newRevUser->getName() : ''; |
1810 | $newRevUserSafe = $newRevRecord->getUser( |
1811 | RevisionRecord::FOR_THIS_USER, |
1812 | $this->getAuthority() |
1813 | ); |
1814 | $newRevUserForGender = $newRevUserSafe ? $newRevUserSafe->getName() : '[HIDDEN]'; |
1815 | if ( $numUsers == 1 && $users[0]->getName() == $newRevUserText ) { |
1816 | $numUsers = 0; // special case to say "by the same user" instead of "by one other user" |
1817 | } |
1818 | } catch ( InvalidArgumentException $e ) { |
1819 | $numUsers = 0; |
1820 | } |
1821 | |
1822 | return self::intermediateEditsMsg( $nEdits, $numUsers, $limit, $newRevUserForGender ); |
1823 | } |
1824 | |
1825 | return ''; |
1826 | } |
1827 | |
1828 | /** |
1829 | * Get a notice about how many intermediate edits and users there are |
1830 | * |
1831 | * @param int $numEdits |
1832 | * @param int $numUsers |
1833 | * @param int $limit |
1834 | * @param string $lastUser |
1835 | * |
1836 | * @return string |
1837 | */ |
1838 | public static function intermediateEditsMsg( $numEdits, $numUsers, $limit, $lastUser = '[HIDDEN]' ) { |
1839 | if ( $numUsers === 0 ) { |
1840 | $msg = 'diff-multi-sameuser'; |
1841 | return wfMessage( $msg ) |
1842 | ->numParams( $numEdits, $numUsers ) |
1843 | ->params( $lastUser ) |
1844 | ->parse(); |
1845 | } elseif ( $numUsers > $limit ) { |
1846 | $msg = 'diff-multi-manyusers'; |
1847 | $numUsers = $limit; |
1848 | } else { |
1849 | $msg = 'diff-multi-otherusers'; |
1850 | } |
1851 | |
1852 | return wfMessage( $msg )->numParams( $numEdits, $numUsers )->parse(); |
1853 | } |
1854 | |
1855 | /** |
1856 | * @param RevisionRecord $revRecord |
1857 | * @return bool whether the user can see and edit the revision. |
1858 | */ |
1859 | private function userCanEdit( RevisionRecord $revRecord ) { |
1860 | if ( !$revRecord->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) { |
1861 | return false; |
1862 | } |
1863 | |
1864 | return true; |
1865 | } |
1866 | |
1867 | /** |
1868 | * Get a header for a specified revision. |
1869 | * |
1870 | * @param RevisionRecord $rev |
1871 | * @param string $complete 'complete' to get the header wrapped depending |
1872 | * the visibility of the revision and a link to edit the page. |
1873 | * |
1874 | * @return string HTML fragment |
1875 | */ |
1876 | public function getRevisionHeader( RevisionRecord $rev, $complete = '' ) { |
1877 | $lang = $this->getLanguage(); |
1878 | $user = $this->getUser(); |
1879 | $revtimestamp = $rev->getTimestamp(); |
1880 | $timestamp = $lang->userTimeAndDate( $revtimestamp, $user ); |
1881 | $dateofrev = $lang->userDate( $revtimestamp, $user ); |
1882 | $timeofrev = $lang->userTime( $revtimestamp, $user ); |
1883 | |
1884 | $header = $this->msg( |
1885 | $rev->isCurrent() ? 'currentrev-asof' : 'revisionasof', |
1886 | $timestamp, |
1887 | $dateofrev, |
1888 | $timeofrev |
1889 | ); |
1890 | |
1891 | if ( $complete !== 'complete' ) { |
1892 | return $header->escaped(); |
1893 | } |
1894 | |
1895 | $title = $rev->getPageAsLinkTarget(); |
1896 | |
1897 | if ( $this->userCanEdit( $rev ) ) { |
1898 | $header = $this->linkRenderer->makeKnownLink( |
1899 | $title, |
1900 | $header->text(), |
1901 | [], |
1902 | [ 'oldid' => $rev->getId() ] |
1903 | ); |
1904 | $editQuery = [ 'action' => 'edit' ]; |
1905 | if ( !$rev->isCurrent() ) { |
1906 | $editQuery['oldid'] = $rev->getId(); |
1907 | } |
1908 | |
1909 | $key = $this->getAuthority()->probablyCan( 'edit', $rev->getPage() ) ? 'editold' : 'viewsourceold'; |
1910 | $msg = $this->msg( $key )->text(); |
1911 | $editLink = $this->linkRenderer->makeKnownLink( $title, $msg, [], $editQuery ); |
1912 | $header .= ' ' . Html::rawElement( |
1913 | 'span', |
1914 | [ 'class' => 'mw-diff-edit' ], |
1915 | $editLink |
1916 | ); |
1917 | } else { |
1918 | $header = $header->escaped(); |
1919 | } |
1920 | |
1921 | // Machine readable information |
1922 | $header .= Html::element( 'span', |
1923 | [ |
1924 | 'class' => 'mw-diff-timestamp', |
1925 | 'data-timestamp' => wfTimestamp( TS_ISO_8601, $revtimestamp ), |
1926 | ], '' |
1927 | ); |
1928 | |
1929 | if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
1930 | return Html::rawElement( |
1931 | 'span', |
1932 | [ 'class' => Linker::getRevisionDeletedClass( $rev ) ], |
1933 | $header |
1934 | ); |
1935 | } |
1936 | |
1937 | return $header; |
1938 | } |
1939 | |
1940 | /** |
1941 | * Add the header to a diff body |
1942 | * |
1943 | * @param string $diff Diff body |
1944 | * @param string $otitle Old revision header |
1945 | * @param string $ntitle New revision header |
1946 | * @param string $multi Notice telling user that there are intermediate |
1947 | * revisions between the ones being compared |
1948 | * @param string $notice Other notices, e.g. that user is viewing deleted content |
1949 | * |
1950 | * @return string |
1951 | */ |
1952 | public function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) { |
1953 | // shared.css sets diff in interface language/dir, but the actual content |
1954 | // is often in a different language, mostly the page content language/dir |
1955 | $header = Html::openElement( 'table', [ |
1956 | 'class' => [ |
1957 | 'diff', |
1958 | // The following classes are used here: |
1959 | // * diff-type-table |
1960 | // * diff-type-inline |
1961 | 'diff-type-' . $this->getTextDiffFormat(), |
1962 | // The following classes are used here: |
1963 | // * diff-contentalign-left |
1964 | // * diff-contentalign-right |
1965 | 'diff-contentalign-' . $this->getDiffLang()->alignStart(), |
1966 | // The following classes are used here: |
1967 | // * diff-editfont-monospace |
1968 | // * diff-editfont-sans-serif |
1969 | // * diff-editfont-serif |
1970 | 'diff-editfont-' . $this->userOptionsLookup->getOption( |
1971 | $this->getUser(), |
1972 | 'editfont' |
1973 | ) |
1974 | ], |
1975 | 'data-mw' => 'interface', |
1976 | ] ); |
1977 | $userLang = htmlspecialchars( $this->getLanguage()->getHtmlCode() ); |
1978 | |
1979 | if ( !$diff && !$otitle ) { |
1980 | $header .= " |
1981 | <tr class=\"diff-title\" lang=\"{$userLang}\"> |
1982 | <td class=\"diff-ntitle\">{$ntitle}</td> |
1983 | </tr>"; |
1984 | $multiColspan = 1; |
1985 | } else { |
1986 | if ( $diff ) { // Safari/Chrome show broken output if cols not used |
1987 | $header .= " |
1988 | <col class=\"diff-marker\" /> |
1989 | <col class=\"diff-content\" /> |
1990 | <col class=\"diff-marker\" /> |
1991 | <col class=\"diff-content\" />"; |
1992 | $colspan = 2; |
1993 | $multiColspan = 4; |
1994 | } else { |
1995 | $colspan = 1; |
1996 | $multiColspan = 2; |
1997 | } |
1998 | if ( $otitle || $ntitle ) { |
1999 | // FIXME Hardcoding values from TableDiffFormatter. |
2000 | $deletedClass = 'diff-side-deleted'; |
2001 | $addedClass = 'diff-side-added'; |
2002 | $header .= " |
2003 | <tr class=\"diff-title\" lang=\"{$userLang}\"> |
2004 | <td colspan=\"$colspan\" class=\"diff-otitle {$deletedClass}\">{$otitle}</td> |
2005 | <td colspan=\"$colspan\" class=\"diff-ntitle {$addedClass}\">{$ntitle}</td> |
2006 | </tr>"; |
2007 | } |
2008 | } |
2009 | |
2010 | if ( $multi != '' ) { |
2011 | $header .= "<tr><td colspan=\"{$multiColspan}\" " . |
2012 | "class=\"diff-multi\" lang=\"{$userLang}\">{$multi}</td></tr>"; |
2013 | } |
2014 | if ( $notice != '' ) { |
2015 | $header .= "<tr><td colspan=\"{$multiColspan}\" " . |
2016 | "class=\"diff-notice\" lang=\"{$userLang}\">{$notice}</td></tr>"; |
2017 | } |
2018 | |
2019 | return $header . $diff . "</table>"; |
2020 | } |
2021 | |
2022 | /** |
2023 | * Use specified text instead of loading from the database |
2024 | * @param Content $oldContent |
2025 | * @param Content $newContent |
2026 | * @since 1.21 |
2027 | * @deprecated since 1.32, use setRevisions or ContentHandler::getSlotDiffRenderer. |
2028 | */ |
2029 | public function setContent( Content $oldContent, Content $newContent ) { |
2030 | $this->mOldContent = $oldContent; |
2031 | $this->mNewContent = $newContent; |
2032 | |
2033 | $this->mTextLoaded = 2; |
2034 | $this->mRevisionsLoaded = true; |
2035 | $this->isContentOverridden = true; |
2036 | $this->slotDiffRenderers = null; |
2037 | } |
2038 | |
2039 | /** |
2040 | * Use specified text instead of loading from the database. |
2041 | * @param RevisionRecord|null $oldRevision |
2042 | * @param RevisionRecord $newRevision |
2043 | */ |
2044 | public function setRevisions( |
2045 | ?RevisionRecord $oldRevision, RevisionRecord $newRevision |
2046 | ) { |
2047 | if ( $oldRevision ) { |
2048 | $this->mOldRevisionRecord = $oldRevision; |
2049 | $this->mOldid = $oldRevision->getId(); |
2050 | $this->mOldPage = Title::newFromLinkTarget( $oldRevision->getPageAsLinkTarget() ); |
2051 | // This method is meant for edit diffs and such so there is no reason to provide a |
2052 | // revision that's not readable to the user, but check it just in case. |
2053 | $this->mOldContent = $oldRevision->getContent( SlotRecord::MAIN, |
2054 | RevisionRecord::FOR_THIS_USER, $this->getAuthority() ); |
2055 | if ( !$this->mOldContent ) { |
2056 | $this->addRevisionLoadError( 'old' ); |
2057 | } |
2058 | } else { |
2059 | $this->mOldPage = null; |
2060 | $this->mOldRevisionRecord = $this->mOldid = false; |
2061 | } |
2062 | $this->mNewRevisionRecord = $newRevision; |
2063 | $this->mNewid = $newRevision->getId(); |
2064 | $this->mNewPage = Title::newFromLinkTarget( $newRevision->getPageAsLinkTarget() ); |
2065 | $this->mNewContent = $newRevision->getContent( SlotRecord::MAIN, |
2066 | RevisionRecord::FOR_THIS_USER, $this->getAuthority() ); |
2067 | if ( !$this->mNewContent ) { |
2068 | $this->addRevisionLoadError( 'new' ); |
2069 | } |
2070 | |
2071 | $this->mRevisionsIdsLoaded = $this->mRevisionsLoaded = true; |
2072 | $this->mTextLoaded = $oldRevision ? 2 : 1; |
2073 | $this->isContentOverridden = false; |
2074 | $this->slotDiffRenderers = null; |
2075 | } |
2076 | |
2077 | /** |
2078 | * Set the language in which the diff text is written |
2079 | * |
2080 | * @param Language $lang |
2081 | * @since 1.19 |
2082 | */ |
2083 | public function setTextLanguage( Language $lang ) { |
2084 | $this->mDiffLang = $lang; |
2085 | } |
2086 | |
2087 | /** |
2088 | * Maps a revision pair definition as accepted by DifferenceEngine constructor |
2089 | * to a pair of actual integers representing revision ids. |
2090 | * |
2091 | * @param int $old Revision id, e.g. from URL parameter 'oldid' |
2092 | * @param int|string $new Revision id or strings 'next' or 'prev', e.g. from URL parameter 'diff' |
2093 | * |
2094 | * @return array List of two revision ids, older first, later second. |
2095 | * Zero signifies invalid argument passed. |
2096 | * false signifies that there is no previous/next revision ($old is the oldest/newest one). |
2097 | * @phan-return (int|false)[] |
2098 | */ |
2099 | public function mapDiffPrevNext( $old, $new ) { |
2100 | if ( $new === 'prev' ) { |
2101 | // Show diff between revision $old and the previous one. Get previous one from DB. |
2102 | $newid = intval( $old ); |
2103 | $oldid = false; |
2104 | $newRev = $this->revisionStore->getRevisionById( $newid ); |
2105 | if ( $newRev ) { |
2106 | $oldRev = $this->revisionStore->getPreviousRevision( $newRev ); |
2107 | if ( $oldRev ) { |
2108 | $oldid = $oldRev->getId(); |
2109 | } |
2110 | } |
2111 | } elseif ( $new === 'next' ) { |
2112 | // Show diff between revision $old and the next one. Get next one from DB. |
2113 | $oldid = intval( $old ); |
2114 | $newid = false; |
2115 | $oldRev = $this->revisionStore->getRevisionById( $oldid ); |
2116 | if ( $oldRev ) { |
2117 | $newRev = $this->revisionStore->getNextRevision( $oldRev ); |
2118 | if ( $newRev ) { |
2119 | $newid = $newRev->getId(); |
2120 | } |
2121 | } |
2122 | } else { |
2123 | $oldid = intval( $old ); |
2124 | $newid = intval( $new ); |
2125 | } |
2126 | |
2127 | // @phan-suppress-next-line PhanTypeMismatchReturn getId does not return null here |
2128 | return [ $oldid, $newid ]; |
2129 | } |
2130 | |
2131 | private function loadRevisionIds() { |
2132 | if ( $this->mRevisionsIdsLoaded ) { |
2133 | return; |
2134 | } |
2135 | |
2136 | $this->mRevisionsIdsLoaded = true; |
2137 | |
2138 | $old = $this->mOldid; |
2139 | $new = $this->mNewid; |
2140 | |
2141 | [ $this->mOldid, $this->mNewid ] = self::mapDiffPrevNext( $old, $new ); |
2142 | if ( $new === 'next' && $this->mNewid === false ) { |
2143 | # if no result, NewId points to the newest old revision. The only newer |
2144 | # revision is cur, which is "0". |
2145 | $this->mNewid = 0; |
2146 | } |
2147 | |
2148 | $this->hookRunner->onNewDifferenceEngine( |
2149 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive |
2150 | $this->getTitle(), $this->mOldid, $this->mNewid, $old, $new ); |
2151 | } |
2152 | |
2153 | /** |
2154 | * Load revision metadata for the specified revisions. If newid is 0, then compare |
2155 | * the old revision in oldid to the current revision of the current page (as defined |
2156 | * by the request context); if oldid is 0, then compare the revision in newid to the |
2157 | * immediately previous one. |
2158 | * |
2159 | * If oldid is false, leave the corresponding RevisionRecord object set to false. This can |
2160 | * happen with 'diff=prev' pointing to a non-existent revision, and is also used directly |
2161 | * by the API. |
2162 | * |
2163 | * @return bool Whether both revisions were loaded successfully. Setting mOldRevisionRecord |
2164 | * to false counts as successful loading. |
2165 | */ |
2166 | public function loadRevisionData() { |
2167 | if ( $this->mRevisionsLoaded ) { |
2168 | return $this->isContentOverridden || |
2169 | ( $this->mOldRevisionRecord !== null && $this->mNewRevisionRecord !== null ); |
2170 | } |
2171 | |
2172 | // Whether it succeeds or fails, we don't want to try again |
2173 | $this->mRevisionsLoaded = true; |
2174 | |
2175 | $this->loadRevisionIds(); |
2176 | |
2177 | // Load the new RevisionRecord object |
2178 | if ( $this->mNewid ) { |
2179 | $this->mNewRevisionRecord = $this->revisionStore->getRevisionById( $this->mNewid ); |
2180 | } else { |
2181 | $this->mNewRevisionRecord = $this->revisionStore->getRevisionByTitle( $this->getTitle() ); |
2182 | } |
2183 | |
2184 | if ( !$this->mNewRevisionRecord instanceof RevisionRecord ) { |
2185 | return false; |
2186 | } |
2187 | |
2188 | // Update the new revision ID in case it was 0 (makes life easier doing UI stuff) |
2189 | $this->mNewid = $this->mNewRevisionRecord->getId(); |
2190 | $this->mNewPage = $this->mNewid ? |
2191 | Title::newFromLinkTarget( $this->mNewRevisionRecord->getPageAsLinkTarget() ) : |
2192 | null; |
2193 | |
2194 | // Load the old RevisionRecord object |
2195 | $this->mOldRevisionRecord = false; |
2196 | if ( $this->mOldid ) { |
2197 | $this->mOldRevisionRecord = $this->revisionStore->getRevisionById( $this->mOldid ); |
2198 | } elseif ( $this->mOldid === 0 ) { |
2199 | $revRecord = $this->revisionStore->getPreviousRevision( $this->mNewRevisionRecord ); |
2200 | // No previous revision; mark to show as first-version only. |
2201 | $this->mOldid = $revRecord ? $revRecord->getId() : false; |
2202 | $this->mOldRevisionRecord = $revRecord ?? false; |
2203 | } /* elseif ( $this->mOldid === false ) leave mOldRevisionRecord false; */ |
2204 | |
2205 | if ( $this->mOldRevisionRecord === null ) { |
2206 | return false; |
2207 | } |
2208 | |
2209 | if ( $this->mOldRevisionRecord && $this->mOldRevisionRecord->getId() ) { |
2210 | $this->mOldPage = Title::newFromLinkTarget( |
2211 | $this->mOldRevisionRecord->getPageAsLinkTarget() |
2212 | ); |
2213 | } else { |
2214 | $this->mOldPage = null; |
2215 | } |
2216 | |
2217 | // Load tags information for both revisions |
2218 | $dbr = $this->dbProvider->getReplicaDatabase(); |
2219 | $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore(); |
2220 | if ( $this->mOldid !== false ) { |
2221 | $tagIds = $dbr->newSelectQueryBuilder() |
2222 | ->select( 'ct_tag_id' ) |
2223 | ->from( 'change_tag' ) |
2224 | ->where( [ 'ct_rev_id' => $this->mOldid ] ) |
2225 | ->caller( __METHOD__ )->fetchFieldValues(); |
2226 | $tags = []; |
2227 | foreach ( $tagIds as $tagId ) { |
2228 | try { |
2229 | $tags[] = $changeTagDefStore->getName( (int)$tagId ); |
2230 | } catch ( NameTableAccessException $exception ) { |
2231 | continue; |
2232 | } |
2233 | } |
2234 | $this->mOldTags = implode( ',', $tags ); |
2235 | } else { |
2236 | $this->mOldTags = false; |
2237 | } |
2238 | |
2239 | $tagIds = $dbr->newSelectQueryBuilder() |
2240 | ->select( 'ct_tag_id' ) |
2241 | ->from( 'change_tag' ) |
2242 | ->where( [ 'ct_rev_id' => $this->mNewid ] ) |
2243 | ->caller( __METHOD__ )->fetchFieldValues(); |
2244 | $tags = []; |
2245 | foreach ( $tagIds as $tagId ) { |
2246 | try { |
2247 | $tags[] = $changeTagDefStore->getName( (int)$tagId ); |
2248 | } catch ( NameTableAccessException $exception ) { |
2249 | continue; |
2250 | } |
2251 | } |
2252 | $this->mNewTags = implode( ',', $tags ); |
2253 | |
2254 | return true; |
2255 | } |
2256 | |
2257 | /** |
2258 | * Load the text of the revisions, as well as revision data. |
2259 | * When the old revision is missing (mOldRev is false), loading mOldContent is not attempted. |
2260 | * |
2261 | * @return bool Whether the content of both revisions could be loaded successfully. |
2262 | * (When mOldRev is false, that still counts as a success.) |
2263 | * |
2264 | */ |
2265 | public function loadText() { |
2266 | if ( $this->mTextLoaded == 2 ) { |
2267 | return $this->loadRevisionData() && |
2268 | ( $this->mOldRevisionRecord === false || $this->mOldContent ) |
2269 | && $this->mNewContent; |
2270 | } |
2271 | |
2272 | // Whether it succeeds or fails, we don't want to try again |
2273 | $this->mTextLoaded = 2; |
2274 | |
2275 | if ( !$this->loadRevisionData() ) { |
2276 | return false; |
2277 | } |
2278 | |
2279 | if ( $this->mOldRevisionRecord ) { |
2280 | $this->mOldContent = $this->mOldRevisionRecord->getContent( |
2281 | SlotRecord::MAIN, |
2282 | RevisionRecord::FOR_THIS_USER, |
2283 | $this->getAuthority() |
2284 | ); |
2285 | if ( $this->mOldContent === null ) { |
2286 | return false; |
2287 | } |
2288 | } |
2289 | |
2290 | $this->mNewContent = $this->mNewRevisionRecord->getContent( |
2291 | SlotRecord::MAIN, |
2292 | RevisionRecord::FOR_THIS_USER, |
2293 | $this->getAuthority() |
2294 | ); |
2295 | $this->hookRunner->onDifferenceEngineLoadTextAfterNewContentIsLoaded( $this ); |
2296 | if ( $this->mNewContent === null ) { |
2297 | return false; |
2298 | } |
2299 | |
2300 | return true; |
2301 | } |
2302 | |
2303 | /** |
2304 | * Load the text of the new revision, not the old one |
2305 | * |
2306 | * @return bool Whether the content of the new revision could be loaded successfully. |
2307 | */ |
2308 | public function loadNewText() { |
2309 | if ( $this->mTextLoaded >= 1 ) { |
2310 | return $this->loadRevisionData(); |
2311 | } |
2312 | |
2313 | $this->mTextLoaded = 1; |
2314 | |
2315 | if ( !$this->loadRevisionData() ) { |
2316 | return false; |
2317 | } |
2318 | |
2319 | $this->mNewContent = $this->mNewRevisionRecord->getContent( |
2320 | SlotRecord::MAIN, |
2321 | RevisionRecord::FOR_THIS_USER, |
2322 | $this->getAuthority() |
2323 | ); |
2324 | |
2325 | $this->hookRunner->onDifferenceEngineAfterLoadNewText( $this ); |
2326 | |
2327 | return true; |
2328 | } |
2329 | |
2330 | /** |
2331 | * Get the TextDiffer which will be used for rendering text |
2332 | * |
2333 | * @return ManifoldTextDiffer |
2334 | */ |
2335 | protected function getTextDiffer() { |
2336 | if ( $this->textDiffer === null ) { |
2337 | $this->textDiffer = new ManifoldTextDiffer( |
2338 | $this->getContext(), |
2339 | $this->getDiffLang(), |
2340 | $this->getConfig()->get( MainConfigNames::DiffEngine ), |
2341 | $this->getConfig()->get( MainConfigNames::ExternalDiffEngine ), |
2342 | $this->getConfig()->get( MainConfigNames::Wikidiff2Options ) |
2343 | ); |
2344 | } |
2345 | return $this->textDiffer; |
2346 | } |
2347 | |
2348 | /** |
2349 | * Get the list of supported text diff formats |
2350 | * |
2351 | * @since 1.41 |
2352 | * @return array|string[] |
2353 | */ |
2354 | public function getSupportedFormats() { |
2355 | return $this->getTextDiffer()->getFormats(); |
2356 | } |
2357 | |
2358 | /** |
2359 | * Get the selected text diff format |
2360 | * |
2361 | * @since 1.41 |
2362 | * @return string |
2363 | */ |
2364 | public function getTextDiffFormat() { |
2365 | return $this->slotDiffOptions['diff-type'] ?? 'table'; |
2366 | } |
2367 | |
2368 | } |