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\ResultWrapper
;
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
) ) {
98 $msgs = [ 'cur', 'last', 'pipe-separator' ];
99 foreach ( $msgs as $msg ) {
100 $this->message
[$msg] = $this->msg( $msg )->escaped();
106 * @param WebRequest $request
109 private function getTimestampFromRequest( WebRequest
$request ) {
110 // Backwards compatibility checks for URIs with only year and/or month.
111 $year = $request->getInt( 'year' );
112 $month = $request->getInt( 'month' );
114 if ( $year !== 0 ||
$month !== 0 ) {
116 $year = MWTimestamp
::getLocalInstance()->format( 'Y' );
118 if ( $month < 1 ||
$month > 12 ) {
119 // month is invalid so treat as December (all months)
122 // month is valid so check day
123 $day = cal_days_in_month( CAL_GREGORIAN
, $month, $year );
125 // Left pad the months and days
126 $month = str_pad( $month, 2, "0", STR_PAD_LEFT
);
127 $day = str_pad( $day, 2, "0", STR_PAD_LEFT
);
130 $before = $request->getVal( 'date-range-to' );
132 $parts = explode( '-', $before );
134 // check date input is valid
135 if ( count( $parts ) === 3 ) {
140 return $year && $month && $day ?
$year . '-' . $month . '-' . $day : '';
143 * Print the history page for an article.
146 $out = $this->getOutput();
147 $request = $this->getRequest();
150 * Allow client caching.
152 if ( $out->checkLastModified( $this->page
->getTouched() ) ) {
153 return; // Client cache fresh and headers sent, nothing more to do.
156 $this->preCacheMessages();
157 $config = $this->context
->getConfig();
159 # Fill in the file cache if not set already
160 if ( HTMLFileCache
::useFileCache( $this->getContext() ) ) {
161 $cache = new HTMLFileCache( $this->getTitle(), 'history' );
162 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
163 ob_start( [ &$cache, 'saveToFileCache' ] );
167 // Setup page variables.
168 $out->setFeedAppendQuery( 'action=history' );
169 $out->addModules( 'mediawiki.action.history' );
170 $out->addModuleStyles( [
171 'mediawiki.interface.helpers.styles',
172 'mediawiki.action.history.styles',
173 'mediawiki.special.changeslist',
175 if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) {
176 $out = $this->getOutput();
177 $out->addModuleStyles( [
178 'mediawiki.ui.input',
179 'mediawiki.ui.checkbox',
183 // Handle atom/RSS feeds.
184 $feedType = $request->getVal( 'feed' );
186 $this->feed( $feedType );
191 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Page_history', true );
193 // Fail nicely if article doesn't exist.
194 if ( !$this->page
->exists() ) {
195 global $wgSend404Code;
196 if ( $wgSend404Code ) {
197 $out->setStatusCode( 404 );
199 $out->addWikiMsg( 'nohistory' );
201 $dbr = wfGetDB( DB_REPLICA
);
203 # show deletion/move log if there is an entry
204 LogEventsList
::showLogExtract(
206 [ 'delete', 'move', 'protect' ],
210 'conds' => [ 'log_action != ' . $dbr->addQuotes( 'revision' ) ],
211 'showIfEmpty' => false,
212 'msgKey' => [ 'moveddeleted-notice' ]
219 $ts = $this->getTimestampFromRequest( $request );
220 $tagFilter = $request->getVal( 'tagfilter' );
223 * Option to show only revisions that have been (partially) hidden via RevisionDelete
225 if ( $request->getBool( 'deleted' ) ) {
226 $conds = [ 'rev_deleted != 0' ];
231 // Add the general form.
232 $action = htmlspecialchars( wfScript() );
237 'default' => $this->getTitle()->getPrefixedDBkey(),
242 'default' => 'history',
247 'label' => $this->msg( 'date-range-to' )->text(),
248 'name' => 'date-range-to',
251 'label-raw' => $this->msg( 'tag-filter' )->parse(),
252 'type' => 'tagfilter',
254 'name' => 'tagfilter',
255 'value' => $tagFilter,
258 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
261 'label' => $this->msg( 'history-show-deleted' )->text(),
262 'default' => $request->getBool( 'deleted' ),
268 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
271 ->setAction( $action )
272 ->setId( 'mw-history-searchform' )
273 ->setSubmitText( $this->msg( 'historyaction-submit' )->text() )
274 ->setWrapperLegend( $this->msg( 'history-fieldset-title' )->text() );
275 $htmlForm->loadData();
277 $out->addHTML( $htmlForm->getHTML( false ) );
279 Hooks
::run( 'PageHistoryBeforeList', [ &$this->page
, $this->getContext() ] );
281 // Create and output the list.
282 $dateComponents = explode( '-', $ts );
283 if ( count( $dateComponents ) > 1 ) {
284 $y = $dateComponents[0];
285 $m = $dateComponents[1];
286 $d = $dateComponents[2];
292 $pager = new HistoryPager( $this, $y, $m, $tagFilter, $conds, $d );
294 $pager->getNavigationBar() .
296 $pager->getNavigationBar()
298 $out->preventClickjacking( $pager->getPreventClickjacking() );
302 * Fetch an array of revisions, specified by a given limit, offset and
303 * direction. This is now only used by the feeds. It was previously
304 * used by the main UI but that's now handled by the pager.
306 * @param int $limit The limit number of revisions to get
308 * @param int $direction Either self::DIR_PREV or self::DIR_NEXT
309 * @return ResultWrapper
311 function fetchRevisions( $limit, $offset, $direction ) {
312 // Fail if article doesn't exist.
313 if ( !$this->getTitle()->exists() ) {
314 return new FakeResultWrapper( [] );
317 $dbr = wfGetDB( DB_REPLICA
);
319 if ( $direction === self
::DIR_PREV
) {
320 list( $dirs, $oper ) = [ "ASC", ">=" ];
321 } else { /* $direction === self::DIR_NEXT */
322 list( $dirs, $oper ) = [ "DESC", "<=" ];
326 $offsets = [ "rev_timestamp $oper " . $dbr->addQuotes( $dbr->timestamp( $offset ) ) ];
331 $page_id = $this->page
->getId();
333 $revQuery = Revision
::getQueryInfo();
337 array_merge( [ 'rev_page' => $page_id ], $offsets ),
340 'ORDER BY' => "rev_timestamp $dirs",
341 'USE INDEX' => [ 'revision' => 'page_timestamp' ],
349 * Output a subscription feed listing recent edits to this page.
351 * @param string $type Feed type
353 function feed( $type ) {
354 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
357 $request = $this->getRequest();
359 $feedClasses = $this->context
->getConfig()->get( 'FeedClasses' );
360 /** @var RSSFeed|AtomFeed $feed */
361 $feed = new $feedClasses[$type](
362 $this->getTitle()->getPrefixedText() . ' - ' .
363 $this->msg( 'history-feed-title' )->inContentLanguage()->text(),
364 $this->msg( 'history-feed-description' )->inContentLanguage()->text(),
365 $this->getTitle()->getFullURL( 'action=history' )
368 // Get a limit on number of feed entries. Provide a sane default
369 // of 10 if none is defined (but limit to $wgFeedLimit max)
370 $limit = $request->getInt( 'limit', 10 );
373 $this->context
->getConfig()->get( 'FeedLimit' )
376 $items = $this->fetchRevisions( $limit, 0, self
::DIR_NEXT
);
378 // Generate feed elements enclosed between header and footer.
380 if ( $items->numRows() ) {
381 foreach ( $items as $row ) {
382 $feed->outItem( $this->feedItem( $row ) );
385 $feed->outItem( $this->feedEmpty() );
390 function feedEmpty() {
392 $this->msg( 'nohistory' )->inContentLanguage()->text(),
393 $this->msg( 'history-feed-empty' )->inContentLanguage()->parseAsBlock(),
394 $this->getTitle()->getFullURL(),
395 wfTimestamp( TS_MW
),
397 $this->getTitle()->getTalkPage()->getFullURL()
402 * Generate a FeedItem object from a given revision table row
403 * Borrows Recent Changes' feed generation functions for formatting;
404 * includes a diff to the previous revision (if any).
406 * @param stdClass|array $row Database row
409 function feedItem( $row ) {
410 $rev = new Revision( $row, 0, $this->getTitle() );
412 $text = FeedUtils
::formatDiffRow(
414 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
416 $rev->getTimestamp(),
419 if ( $rev->getComment() == '' ) {
420 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
421 $title = $this->msg( 'history-feed-item-nocomment',
423 $contLang->timeanddate( $rev->getTimestamp() ),
424 $contLang->date( $rev->getTimestamp() ),
425 $contLang->time( $rev->getTimestamp() )
426 )->inContentLanguage()->text();
428 $title = $rev->getUserText() .
429 $this->msg( 'colon-separator' )->inContentLanguage()->text() .
430 FeedItem
::stripComment( $rev->getComment() );
436 $this->getTitle()->getFullURL( 'diff=' . $rev->getId() . '&oldid=prev' ),
437 $rev->getTimestamp(),
439 $this->getTitle()->getTalkPage()->getFullURL()