Debian Codegraph @

includes/actions/HistoryAction.php

raw symbols SCIP
1 <?php
2 /**
3 * Page history
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 Actions
22 */
23
24 use MediaWiki\Feed\AtomFeed;
25 use MediaWiki\Feed\FeedItem;
26 use MediaWiki\Feed\FeedUtils;
27 use MediaWiki\Feed\RSSFeed;
28 use MediaWiki\Html\Html;
29 use MediaWiki\HTMLForm\HTMLForm;
30 use MediaWiki\MainConfigNames;
31 use MediaWiki\MediaWikiServices;
32 use MediaWiki\Pager\HistoryPager;
33 use MediaWiki\Request\WebRequest;
34 use MediaWiki\SpecialPage\SpecialPage;
35 use MediaWiki\Utils\MWTimestamp;
36 use Wikimedia\Rdbms\FakeResultWrapper;
37 use Wikimedia\Rdbms\IResultWrapper;
38
39 /**
40 * This class handles printing the history page for an article. In order to
41 * be efficient, it uses timestamps rather than offsets for paging, to avoid
42 * costly LIMIT,offset queries.
43 *
44 * Construct it by passing in an Article, and call $h->history() to print the
45 * history.
46 *
47 * @ingroup Actions
48 */
49 class HistoryAction extends FormlessAction {
50 private const DIR_PREV = 0;
51 private const DIR_NEXT = 1;
52
53 /** @var array Array of message keys and strings */
54 public $message;
55
56 public function getName() {
57 return 'history';
58 }
59
60 public function requiresWrite() {
61 return false;
62 }
63
64 public function requiresUnblock() {
65 return false;
66 }
67
68 protected function getPageTitle() {
69 return $this->msg( 'history-title' )->plaintextParams( $this->getTitle()->getPrefixedText() );
70 }
71
72 protected function getDescription() {
73 // Creation of a subtitle link pointing to [[Special:Log]]
74 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
75 $subtitle = $linkRenderer->makeKnownLink(
76 SpecialPage::getTitleFor( 'Log' ),
77 $this->msg( 'viewpagelogs' )->text(),
78 [],
79 [ 'page' => $this->getTitle()->getPrefixedText() ]
80 );
81
82 $links = [];
83 // Allow extensions to add more links
84 $this->getHookRunner()->onHistoryPageToolLinks( $this->getContext(), $linkRenderer, $links );
85 if ( $links ) {
86 $subtitle .= ''
87 . $this->msg( 'word-separator' )->escaped()
88 . $this->msg( 'parentheses' )
89 ->rawParams( $this->getLanguage()->pipeList( $links ) )
90 ->escaped();
91 }
92 return Html::rawElement( 'div', [ 'class' => 'mw-history-subtitle' ], $subtitle );
93 }
94
95 /**
96 * As we use the same small set of messages in various methods and that
97 * they are called often, we call them once and save them in $this->message
98 */
99 private function preCacheMessages() {
100 // Precache various messages
101 if ( !isset( $this->message ) ) {
102 $this->message = [];
103 $msgs = [
104 'cur', 'tooltip-cur', 'last', 'tooltip-last', 'pipe-separator',
105 'changeslist-nocomment', 'updatedmarker',
106 ];
107 foreach ( $msgs as $msg ) {
108 $this->message[$msg] = $this->msg( $msg )->escaped();
109 }
110 }
111 }
112
113 /**
114 * @param WebRequest $request
115 * @return string
116 */
117 private function getTimestampFromRequest( WebRequest $request ) {
118 // Backwards compatibility checks for URIs with only year and/or month.
119 $year = $request->getInt( 'year' );
120 $month = $request->getInt( 'month' );
121 $day = null;
122 if ( $year !== 0 || $month !== 0 ) {
123 if ( $year === 0 ) {
124 $year = MWTimestamp::getLocalInstance()->format( 'Y' );
125 }
126 if ( $month < 1 || $month > 12 ) {
127 // month is invalid so treat as December (all months)
128 $month = 12;
129 }
130 // month is valid so check day
131 $day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
132
133 // Left pad the months and days
134 $month = str_pad( (string)$month, 2, "0", STR_PAD_LEFT );
135 $day = str_pad( (string)$day, 2, "0", STR_PAD_LEFT );
136 }
137
138 $before = $request->getVal( 'date-range-to' );
139 if ( $before ) {
140 $parts = explode( '-', $before );
141 $year = $parts[0];
142 // check date input is valid
143 if ( count( $parts ) === 3 ) {
144 $month = $parts[1];
145 $day = $parts[2];
146 }
147 }
148 return $year && $month && $day ? $year . '-' . $month . '-' . $day : '';
149 }
150
151 /**
152 * Print the history page for an article.
153 * @return string|null
154 */
155 public function onView() {
156 $out = $this->getOutput();
157 $request = $this->getRequest();
158 $config = $this->context->getConfig();
159 $services = MediaWikiServices::getInstance();
160
161 // Allow client-side HTTP caching of the history page.
162 // But, always ignore this cache if the (logged-in) user has this page on their watchlist
163 // and has one or more unseen revisions. Otherwise, we might be showing stale update markers.
164 // The Last-Modified for the history page does not change when user's markers are cleared,
165 // so going from "some unseen" to "all seen" would not clear the cache.
166 // But, when all of the revisions are marked as seen, then only way for new unseen revision
167 // markers to appear, is for the page to be edited, which updates page_touched/Last-Modified.
168 $watchlistManager = $services->getWatchlistManager();
169 $hasUnseenRevisionMarkers = $config->get( MainConfigNames::ShowUpdatedMarker ) &&
170 $watchlistManager->getTitleNotificationTimestamp(
171 $this->getUser(),
172 $this->getTitle()
173 );
174 if (
175 !$hasUnseenRevisionMarkers &&
176 $out->checkLastModified( $this->getWikiPage()->getTouched() )
177 ) {
178 return null; // Client cache fresh and headers sent, nothing more to do.
179 }
180
181 $this->preCacheMessages();
182
183 # Fill in the file cache if not set already
184 if ( HTMLFileCache::useFileCache( $this->getContext() ) ) {
185 $cache = new HTMLFileCache( $this->getTitle(), 'history' );
186 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
187 ob_start( [ &$cache, 'saveToFileCache' ] );
188 }
189 }
190
191 // Setup page variables.
192 $out->setFeedAppendQuery( 'action=history' );
193 $out->addModules( 'mediawiki.action.history' );
194 $out->addModuleStyles( [
195 'mediawiki.interface.helpers.styles',
196 'mediawiki.action.history.styles',
197 'mediawiki.special.changeslist',
198 ] );
199
200 // Handle atom/RSS feeds.
201 $feedType = $request->getRawVal( 'feed' );
202 if ( $feedType !== null ) {
203 $this->feed( $feedType );
204 return null;
205 }
206
207 $this->addHelpLink(
208 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:History',
209 true
210 );
211
212 $dbr = $services->getConnectionProvider()->getReplicaDatabase();
213 // Fail nicely if article doesn't exist.
214 if ( !$this->getWikiPage()->exists() ) {
215 $send404Code = $config->get( MainConfigNames::Send404Code );
216 if ( $send404Code ) {
217 $out->setStatusCode( 404 );
218 }
219 $out->addWikiMsg( 'nohistory' );
220
221 # show deletion/move log if there is an entry
222 LogEventsList::showLogExtract(
223 $out,
224 [ 'delete', 'move', 'protect', 'merge' ],
225 $this->getTitle(),
226 '',
227 [ 'lim' => 10,
228 'conds' => [ $dbr->expr( 'log_action', '!=', 'revision' ) ],
229 'showIfEmpty' => false,
230 'msgKey' => [ 'moveddeleted-notice' ]
231 ]
232 );
233
234 return null;
235 }
236
237 $ts = $this->getTimestampFromRequest( $request );
238 $tagFilter = $request->getVal( 'tagfilter' );
239
240 /**
241 * Option to show only revisions that have been (partially) hidden via RevisionDelete
242 */
243 if ( $request->getBool( 'deleted' ) ) {
244 $conds = [ $dbr->expr( 'rev_deleted', '!=', 0 ) ];
245 } else {
246 $conds = [];
247 }
248
249 // Add the general form.
250 $fields = [
251 'action' => [
252 'name' => 'action',
253 'type' => 'hidden',
254 'default' => 'history',
255 ],
256 'date-range-to' => [
257 'type' => 'date',
258 'default' => $ts,
259 'label' => $this->msg( 'date-range-to' )->text(),
260 'name' => 'date-range-to',
261 ],
262 'tagfilter' => [
263 'label-message' => 'tag-filter',
264 'type' => 'tagfilter',
265 'id' => 'tagfilter',
266 'name' => 'tagfilter',
267 'value' => $tagFilter,
268 ],
269 'tagInvert' => [
270 'type' => 'check',
271 'name' => 'tagInvert',
272 'label-message' => 'invert',
273 'hide-if' => [ '===', 'tagfilter', '' ],
274 ],
275 ];
276 if ( $this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
277 $fields['deleted'] = [
278 'type' => 'check',
279 'label' => $this->msg( 'history-show-deleted' )->text(),
280 'default' => $request->getBool( 'deleted' ),
281 'name' => 'deleted',
282 ];
283 }
284
285 $out->enableOOUI();
286 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
287 $htmlForm
288 ->setMethod( 'get' )
289 ->setAction( wfScript() )
290 ->setCollapsibleOptions( true )
291 ->setId( 'mw-history-searchform' )
292 ->setSubmitTextMsg( 'historyaction-submit' )
293 ->setWrapperAttributes( [ 'id' => 'mw-history-search' ] )
294 ->setWrapperLegendMsg( 'history-fieldset-title' )
295 ->prepareForm();
296
297 $out->addHTML( $htmlForm->getHTML( false ) );
298
299 $this->getHookRunner()->onPageHistoryBeforeList(
300 $this->getArticle(),
301 $this->getContext()
302 );
303
304 // Create and output the list.
305 $dateComponents = explode( '-', $ts );
306 if ( count( $dateComponents ) > 1 ) {
307 $y = (int)$dateComponents[0];
308 $m = (int)$dateComponents[1];
309 $d = (int)$dateComponents[2];
310 } else {
311 $y = 0;
312 $m = 0;
313 $d = 0;
314 }
315 $pager = new HistoryPager(
316 $this,
317 $y,
318 $m,
319 $d,
320 $tagFilter,
321 $request->getCheck( 'tagInvert' ),
322 $conds,
323 $services->getLinkBatchFactory(),
324 $watchlistManager,
325 $services->getCommentFormatter(),
326 $services->getHookContainer(),
327 $services->getChangeTagsStore()
328 );
329 $out->addHTML(
330 $pager->getNavigationBar() .
331 $pager->getBody() .
332 $pager->getNavigationBar()
333 );
334 $out->getMetadata()->setPreventClickjacking( $pager->getPreventClickjacking() );
335
336 return null;
337 }
338
339 /**
340 * Fetch an array of revisions, specified by a given limit, offset and
341 * direction. This is now only used by the feeds. It was previously
342 * used by the main UI but that's now handled by the pager.
343 *
344 * @param int $limit The limit number of revisions to get
345 * @param int $offset
346 * @param int $direction Either self::DIR_PREV or self::DIR_NEXT
347 * @return IResultWrapper
348 */
349 private function fetchRevisions( $limit, $offset, $direction ) {
350 // Fail if article doesn't exist.
351 if ( !$this->getTitle()->exists() ) {
352 return new FakeResultWrapper( [] );
353 }
354
355 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
356
357 if ( $direction === self::DIR_PREV ) {
358 [ $dirs, $oper ] = [ "ASC", ">=" ];
359 } else { /* $direction === self::DIR_NEXT */
360 [ $dirs, $oper ] = [ "DESC", "<=" ];
361 }
362
363 $queryBuilder = MediaWikiServices::getInstance()->getRevisionStore()->newSelectQueryBuilder( $dbr )
364 ->joinComment()
365 ->where( [ 'rev_page' => $this->getWikiPage()->getId() ] )
366 ->useIndex( [ 'revision' => 'rev_page_timestamp' ] )
367 ->orderBy( [ 'rev_timestamp' ], $dirs )
368 ->limit( $limit );
369 if ( $offset ) {
370 $queryBuilder->andWhere( $dbr->expr( 'rev_timestamp', $oper, $dbr->timestamp( $offset ) ) );
371 }
372
373 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
374 }
375
376 /**
377 * Output a subscription feed listing recent edits to this page.
378 *
379 * @param string $type Feed type
380 */
381 private function feed( $type ) {
382 if ( !FeedUtils::checkFeedOutput( $type, $this->getOutput() ) ) {
383 return;
384 }
385 $request = $this->getRequest();
386
387 $feedClasses = $this->context->getConfig()->get( MainConfigNames::FeedClasses );
388 /** @var RSSFeed|AtomFeed $feed */
389 $feed = new $feedClasses[$type](
390 $this->getTitle()->getPrefixedText() . ' - ' .
391 $this->msg( 'history-feed-title' )->inContentLanguage()->text(),
392 $this->msg( 'history-feed-description' )->inContentLanguage()->text(),
393 $this->getTitle()->getFullURL( 'action=history' )
394 );
395
396 // Get a limit on number of feed entries. Provide a sensible default
397 // of 10 if none is defined (but limit to $wgFeedLimit max)
398 $limit = $request->getInt( 'limit', 10 );
399 $limit = min(
400 max( $limit, 1 ),
401 $this->context->getConfig()->get( MainConfigNames::FeedLimit )
402 );
403
404 $items = $this->fetchRevisions( $limit, 0, self::DIR_NEXT );
405
406 // Preload comments
407 $formattedComments = MediaWikiServices::getInstance()->getRowCommentFormatter()
408 ->formatRows( $items, 'rev_comment' );
409
410 // Generate feed elements enclosed between header and footer.
411 $feed->outHeader();
412 if ( $items->numRows() ) {
413 foreach ( $items as $i => $row ) {
414 $feed->outItem( $this->feedItem( $row, $formattedComments[$i] ) );
415 }
416 } else {
417 $feed->outItem( $this->feedEmpty() );
418 }
419 $feed->outFooter();
420 }
421
422 private function feedEmpty() {
423 return new FeedItem(
424 $this->msg( 'nohistory' )->inContentLanguage()->text(),
425 $this->msg( 'history-feed-empty' )->inContentLanguage()->parseAsBlock(),
426 $this->getTitle()->getFullURL(),
427 wfTimestamp( TS_MW ),
428 '',
429 $this->getTitle()->getTalkPage()->getFullURL()
430 );
431 }
432
433 /**
434 * Generate a MediaWiki\Feed\FeedItem object from a given revision table row
435 * Borrows Recent Changes' feed generation functions for formatting;
436 * includes a diff to the previous revision (if any).
437 *
438 * @param stdClass|array $row Database row
439 * @param string $formattedComment The comment in HTML format
440 * @return FeedItem
441 */
442 private function feedItem( $row, $formattedComment ) {
443 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
444 $rev = $revisionStore->newRevisionFromRow( $row, 0, $this->getTitle() );
445 $prevRev = $revisionStore->getPreviousRevision( $rev );
446 $revComment = $rev->getComment() === null ? null : $rev->getComment()->text;
447 $text = FeedUtils::formatDiffRow2(
448 $this->getTitle(),
449 $prevRev ? $prevRev->getId() : false,
450 $rev->getId(),
451 $rev->getTimestamp(),
452 $formattedComment
453 );
454 $revUserText = $rev->getUser() ? $rev->getUser()->getName() : '';
455 if ( $revComment == '' ) {
456 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
457 $title = $this->msg( 'history-feed-item-nocomment',
458 $revUserText,
459 $contLang->timeanddate( $rev->getTimestamp() ),
460 $contLang->date( $rev->getTimestamp() ),
461 $contLang->time( $rev->getTimestamp() )
462 )->inContentLanguage()->text();
463 } else {
464 $title = $revUserText .
465 $this->msg( 'colon-separator' )->inContentLanguage()->text() .
466 FeedItem::stripComment( $revComment );
467 }
468
469 return new FeedItem(
470 $title,
471 $text,
472 $this->getTitle()->getFullURL( 'diff=' . $rev->getId() . '&oldid=prev' ),
473 $rev->getTimestamp(),
474 $revUserText,
475 $this->getTitle()->getTalkPage()->getFullURL()
476 );
477 }
478 }