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.
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.
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
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IResultWrapper
;
26 use Wikimedia\Rdbms\FakeResultWrapper
;
29 * This class handles printing the history page for an article. In order to
30 * be efficient, it uses timestamps rather than offsets for paging, to avoid
31 * costly LIMIT,offset queries.
33 * Construct it by passing in an Article, and call $h->history() to print the
38 class HistoryAction
extends FormlessAction
{
42 /** @var array Array of message keys and strings */
45 public function getName() {
49 public function requiresWrite() {
53 public function requiresUnblock() {
57 protected function getPageTitle() {
58 return $this->msg( 'history-title', $this->getTitle()->getPrefixedText() )->text();
61 protected function getDescription() {
62 // Creation of a subtitle link pointing to [[Special:Log]]
63 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
64 $subtitle = $linkRenderer->makeKnownLink(
65 SpecialPage
::getTitleFor( 'Log' ),
66 $this->msg( 'viewpagelogs' )->text(),
68 [ 'page' => $this->getTitle()->getPrefixedText() ]
72 // Allow extensions to add more links
73 Hooks
::run( 'HistoryPageToolLinks', [ $this->getContext(), $linkRenderer, &$links ] );
76 . $this->msg( 'word-separator' )->escaped()
77 . $this->msg( 'parentheses' )
78 ->rawParams( $this->getLanguage()->pipeList( $links ) )
81 return Html
::rawElement( 'div', [ 'class' => 'mw-history-subtitle' ], $subtitle );
85 * @return WikiPage|Article|ImagePage|CategoryPage|Page The Article object we are working on.
87 public function getArticle() {
92 * As we use the same small set of messages in various methods and that
93 * they are called often, we call them once and save them in $this->message
95 private function preCacheMessages() {
96 // Precache various messages
97 if ( !isset( $this->message
) ) {
99 $msgs = [ 'cur', 'last', 'pipe-separator' ];
100 foreach ( $msgs as $msg ) {
101 $this->message
[$msg] = $this->msg( $msg )->escaped();
107 * @param WebRequest $request
110 private function getTimestampFromRequest( WebRequest
$request ) {
111 // Backwards compatibility checks for URIs with only year and/or month.
112 $year = $request->getInt( 'year' );
113 $month = $request->getInt( 'month' );
115 if ( $year !== 0 ||
$month !== 0 ) {
117 $year = MWTimestamp
::getLocalInstance()->format( 'Y' );
119 if ( $month < 1 ||
$month > 12 ) {
120 // month is invalid so treat as December (all months)
123 // month is valid so check day
124 $day = cal_days_in_month( CAL_GREGORIAN
, $month, $year );
126 // Left pad the months and days
127 $month = str_pad( $month, 2, "0", STR_PAD_LEFT
);
128 $day = str_pad( $day, 2, "0", STR_PAD_LEFT
);
131 $before = $request->getVal( 'date-range-to' );
133 $parts = explode( '-', $before );
135 // check date input is valid
136 if ( count( $parts ) === 3 ) {
141 return $year && $month && $day ?
$year . '-' . $month . '-' . $day : '';
145 * Print the history page for an article.
146 * @return string|null
149 $out = $this->getOutput();
150 $request = $this->getRequest();
152 // Allow client-side HTTP caching of the history page.
153 // But, always ignore this cache if the (logged-in) user has this page on their watchlist
154 // and has one or more unseen revisions. Otherwise, we might be showing stale update markers.
155 // The Last-Modified for the history page does not change when user's markers are cleared,
156 // so going from "some unseen" to "all seen" would not clear the cache.
157 // But, when all of the revisions are marked as seen, then only way for new unseen revision
158 // markers to appear, is for the page to be edited, which updates page_touched/Last-Modified.
160 !$this->hasUnseenRevisionMarkers() &&
161 $out->checkLastModified( $this->page
->getTouched() )
163 return null; // Client cache fresh and headers sent, nothing more to do.
166 $this->preCacheMessages();
167 $config = $this->context
->getConfig();
169 # Fill in the file cache if not set already
170 if ( HTMLFileCache
::useFileCache( $this->getContext() ) ) {
171 $cache = new HTMLFileCache( $this->getTitle(), 'history' );
172 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
173 ob_start( [ &$cache, 'saveToFileCache' ] );
177 // Setup page variables.
178 $out->setFeedAppendQuery( 'action=history' );
179 $out->addModules( 'mediawiki.action.history' );
180 $out->addModuleStyles( [
181 'mediawiki.interface.helpers.styles',
182 'mediawiki.action.history.styles',
183 'mediawiki.special.changeslist',
185 if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) {
186 $out = $this->getOutput();
187 $out->addModuleStyles( [
188 'mediawiki.ui.input',
189 'mediawiki.ui.checkbox',
193 // Handle atom/RSS feeds.
194 $feedType = $request->getRawVal( 'feed' );
195 if ( $feedType !== null ) {
196 $this->feed( $feedType );
201 'https://meta.wikimedia.org/wiki/Special:MyLanguage/Help:Page_history',
205 // Fail nicely if article doesn't exist.
206 if ( !$this->page
->exists() ) {
207 global $wgSend404Code;
208 if ( $wgSend404Code ) {
209 $out->setStatusCode( 404 );
211 $out->addWikiMsg( 'nohistory' );
213 $dbr = wfGetDB( DB_REPLICA
);
215 # show deletion/move log if there is an entry
216 LogEventsList
::showLogExtract(
218 [ 'delete', 'move', 'protect' ],
222 'conds' => [ 'log_action != ' . $dbr->addQuotes( 'revision' ) ],
223 'showIfEmpty' => false,
224 'msgKey' => [ 'moveddeleted-notice' ]
231 $ts = $this->getTimestampFromRequest( $request );
232 $tagFilter = $request->getVal( 'tagfilter' );
235 * Option to show only revisions that have been (partially) hidden via RevisionDelete
237 if ( $request->getBool( 'deleted' ) ) {
238 $conds = [ 'rev_deleted != 0' ];
243 // Add the general form.
248 'default' => $this->getTitle()->getPrefixedDBkey(),
253 'default' => 'history',
258 'label' => $this->msg( 'date-range-to' )->text(),
259 'name' => 'date-range-to',
262 'label-raw' => $this->msg( 'tag-filter' )->parse(),
263 'type' => 'tagfilter',
265 'name' => 'tagfilter',
266 'value' => $tagFilter,
269 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
270 if ( $permissionManager->userHasRight( $this->getUser(), 'deletedhistory' ) ) {
273 'label' => $this->msg( 'history-show-deleted' )->text(),
274 'default' => $request->getBool( 'deleted' ),
280 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
283 ->setAction( wfScript() )
284 ->setCollapsibleOptions( true )
285 ->setId( 'mw-history-searchform' )
286 ->setSubmitText( $this->msg( 'historyaction-submit' )->text() )
287 ->setWrapperAttributes( [ 'id' => 'mw-history-search' ] )
288 ->setWrapperLegend( $this->msg( 'history-fieldset-title' )->text() );
289 $htmlForm->loadData();
291 $out->addHTML( $htmlForm->getHTML( false ) );
293 Hooks
::run( 'PageHistoryBeforeList', [ &$this->page
, $this->getContext() ] );
295 // Create and output the list.
296 $dateComponents = explode( '-', $ts );
297 if ( count( $dateComponents ) > 1 ) {
298 $y = $dateComponents[0];
299 $m = $dateComponents[1];
300 $d = $dateComponents[2];
306 $pager = new HistoryPager( $this, $y, $m, $tagFilter, $conds, $d );
308 $pager->getNavigationBar() .
310 $pager->getNavigationBar()
312 $out->preventClickjacking( $pager->getPreventClickjacking() );
318 * @return bool Page is watched by and has unseen revision for the user
320 private function hasUnseenRevisionMarkers() {
322 $this->getContext()->getConfig()->get( 'ShowUpdatedMarker' ) &&
323 $this->getTitle()->getNotificationTimestamp( $this->getUser() )
328 * Fetch an array of revisions, specified by a given limit, offset and
329 * direction. This is now only used by the feeds. It was previously
330 * used by the main UI but that's now handled by the pager.
332 * @param int $limit The limit number of revisions to get
334 * @param int $direction Either self::DIR_PREV or self::DIR_NEXT
335 * @return IResultWrapper
337 function fetchRevisions( $limit, $offset, $direction ) {
338 // Fail if article doesn't exist.
339 if ( !$this->getTitle()->exists() ) {
340 return new FakeResultWrapper( [] );
343 $dbr = wfGetDB( DB_REPLICA
);
345 if ( $direction === self
::DIR_PREV
) {
346 list( $dirs, $oper ) = [ "ASC", ">=" ];
347 } else { /* $direction === self::DIR_NEXT */
348 list( $dirs, $oper ) = [ "DESC", "<=" ];
352 $offsets = [ "rev_timestamp $oper " . $dbr->addQuotes( $dbr->timestamp( $offset ) ) ];
357 $page_id = $this->page
->getId();
359 $revQuery = Revision
::getQueryInfo();
363 array_merge( [ 'rev_page' => $page_id ], $offsets ),
366 'ORDER BY' => "rev_timestamp $dirs",
367 'USE INDEX' => [ 'revision' => 'page_timestamp' ],
375 * Output a subscription feed listing recent edits to this page.
377 * @param string $type Feed type
379 function feed( $type ) {
380 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
383 $request = $this->getRequest();
385 $feedClasses = $this->context
->getConfig()->get( 'FeedClasses' );
386 /** @var RSSFeed|AtomFeed $feed */
387 $feed = new $feedClasses[$type](
388 $this->getTitle()->getPrefixedText() . ' - ' .
389 $this->msg( 'history-feed-title' )->inContentLanguage()->text(),
390 $this->msg( 'history-feed-description' )->inContentLanguage()->text(),
391 $this->getTitle()->getFullURL( 'action=history' )
394 // Get a limit on number of feed entries. Provide a sane default
395 // of 10 if none is defined (but limit to $wgFeedLimit max)
396 $limit = $request->getInt( 'limit', 10 );
399 $this->context
->getConfig()->get( 'FeedLimit' )
402 $items = $this->fetchRevisions( $limit, 0, self
::DIR_NEXT
);
404 // Generate feed elements enclosed between header and footer.
406 if ( $items->numRows() ) {
407 foreach ( $items as $row ) {
408 $feed->outItem( $this->feedItem( $row ) );
411 $feed->outItem( $this->feedEmpty() );
416 function feedEmpty() {
418 $this->msg( 'nohistory' )->inContentLanguage()->text(),
419 $this->msg( 'history-feed-empty' )->inContentLanguage()->parseAsBlock(),
420 $this->getTitle()->getFullURL(),
421 wfTimestamp( TS_MW
),
423 $this->getTitle()->getTalkPage()->getFullURL()
428 * Generate a FeedItem object from a given revision table row
429 * Borrows Recent Changes' feed generation functions for formatting;
430 * includes a diff to the previous revision (if any).
432 * @param stdClass|array $row Database row
435 function feedItem( $row ) {
436 $rev = new Revision( $row, 0, $this->getTitle() );
438 $text = FeedUtils
::formatDiffRow(
440 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
442 $rev->getTimestamp(),
445 if ( $rev->getComment() == '' ) {
446 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
447 $title = $this->msg( 'history-feed-item-nocomment',
449 $contLang->timeanddate( $rev->getTimestamp() ),
450 $contLang->date( $rev->getTimestamp() ),
451 $contLang->time( $rev->getTimestamp() )
452 )->inContentLanguage()->text();
454 $title = $rev->getUserText() .
455 $this->msg( 'colon-separator' )->inContentLanguage()->text() .
456 FeedItem
::stripComment( $rev->getComment() );
462 $this->getTitle()->getFullURL( 'diff=' . $rev->getId() . '&oldid=prev' ),
463 $rev->getTimestamp(),
465 $this->getTitle()->getTalkPage()->getFullURL()