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 ) )
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 * Print the history page for an article.
109 $out = $this->getOutput();
110 $request = $this->getRequest();
113 * Allow client caching.
115 if ( $out->checkLastModified( $this->page
->getTouched() ) ) {
116 return; // Client cache fresh and headers sent, nothing more to do.
119 $this->preCacheMessages();
120 $config = $this->context
->getConfig();
122 # Fill in the file cache if not set already
123 if ( HTMLFileCache
::useFileCache( $this->getContext() ) ) {
124 $cache = new HTMLFileCache( $this->getTitle(), 'history' );
125 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
126 ob_start( [ &$cache, 'saveToFileCache' ] );
130 // Setup page variables.
131 $out->setFeedAppendQuery( 'action=history' );
132 $out->addModules( 'mediawiki.action.history' );
133 $out->addModuleStyles( [
134 'mediawiki.interface.helpers.styles',
135 'mediawiki.action.history.styles',
136 'mediawiki.special.changeslist',
138 if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) {
139 $out = $this->getOutput();
140 $out->addModuleStyles( [
141 'mediawiki.ui.input',
142 'mediawiki.ui.checkbox',
146 // Handle atom/RSS feeds.
147 $feedType = $request->getVal( 'feed' );
149 $this->feed( $feedType );
154 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Page_history', true );
156 // Fail nicely if article doesn't exist.
157 if ( !$this->page
->exists() ) {
158 global $wgSend404Code;
159 if ( $wgSend404Code ) {
160 $out->setStatusCode( 404 );
162 $out->addWikiMsg( 'nohistory' );
164 $dbr = wfGetDB( DB_REPLICA
);
166 # show deletion/move log if there is an entry
167 LogEventsList
::showLogExtract(
169 [ 'delete', 'move', 'protect' ],
173 'conds' => [ 'log_action != ' . $dbr->addQuotes( 'revision' ) ],
174 'showIfEmpty' => false,
175 'msgKey' => [ 'moveddeleted-notice' ]
183 * Add date selector to quickly get to a certain time
185 $year = $request->getInt( 'year' );
186 $month = $request->getInt( 'month' );
187 $tagFilter = $request->getVal( 'tagfilter' );
188 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter, false, $this->getContext() );
191 * Option to show only revisions that have been (partially) hidden via RevisionDelete
193 if ( $request->getBool( 'deleted' ) ) {
194 $conds = [ 'rev_deleted != 0' ];
198 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
199 $checkDeleted = Xml
::checkLabel( $this->msg( 'history-show-deleted' )->text(),
200 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
205 // Add the general form
206 $action = htmlspecialchars( wfScript() );
207 $content = Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n";
208 $content .= Html
::hidden( 'action', 'history' ) . "\n";
209 $content .= Xml
::dateMenu(
210 ( $year == null ? MWTimestamp
::getLocalInstance()->format( 'Y' ) : $year ),
213 $content .= $tagSelector ?
( implode( "\u{00A0}", $tagSelector ) . "\u{00A0}" ) : '';
214 $content .= $checkDeleted . Html
::submitButton(
215 $this->msg( 'historyaction-submit' )->text(),
217 [ 'mw-ui-progressive' ]
220 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
222 $this->msg( 'history-fieldset-title' )->text(),
224 [ 'id' => 'mw-history-search' ]
229 Hooks
::run( 'PageHistoryBeforeList', [ &$this->page
, $this->getContext() ] );
231 // Create and output the list.
232 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
234 $pager->getNavigationBar() .
236 $pager->getNavigationBar()
238 $out->preventClickjacking( $pager->getPreventClickjacking() );
242 * Fetch an array of revisions, specified by a given limit, offset and
243 * direction. This is now only used by the feeds. It was previously
244 * used by the main UI but that's now handled by the pager.
246 * @param int $limit The limit number of revisions to get
248 * @param int $direction Either self::DIR_PREV or self::DIR_NEXT
249 * @return ResultWrapper
251 function fetchRevisions( $limit, $offset, $direction ) {
252 // Fail if article doesn't exist.
253 if ( !$this->getTitle()->exists() ) {
254 return new FakeResultWrapper( [] );
257 $dbr = wfGetDB( DB_REPLICA
);
259 if ( $direction === self
::DIR_PREV
) {
260 list( $dirs, $oper ) = [ "ASC", ">=" ];
261 } else { /* $direction === self::DIR_NEXT */
262 list( $dirs, $oper ) = [ "DESC", "<=" ];
266 $offsets = [ "rev_timestamp $oper " . $dbr->addQuotes( $dbr->timestamp( $offset ) ) ];
271 $page_id = $this->page
->getId();
273 $revQuery = Revision
::getQueryInfo();
277 array_merge( [ 'rev_page' => $page_id ], $offsets ),
280 'ORDER BY' => "rev_timestamp $dirs",
281 'USE INDEX' => [ 'revision' => 'page_timestamp' ],
289 * Output a subscription feed listing recent edits to this page.
291 * @param string $type Feed type
293 function feed( $type ) {
294 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
297 $request = $this->getRequest();
299 $feedClasses = $this->context
->getConfig()->get( 'FeedClasses' );
300 /** @var RSSFeed|AtomFeed $feed */
301 $feed = new $feedClasses[$type](
302 $this->getTitle()->getPrefixedText() . ' - ' .
303 $this->msg( 'history-feed-title' )->inContentLanguage()->text(),
304 $this->msg( 'history-feed-description' )->inContentLanguage()->text(),
305 $this->getTitle()->getFullURL( 'action=history' )
308 // Get a limit on number of feed entries. Provide a sane default
309 // of 10 if none is defined (but limit to $wgFeedLimit max)
310 $limit = $request->getInt( 'limit', 10 );
313 $this->context
->getConfig()->get( 'FeedLimit' )
316 $items = $this->fetchRevisions( $limit, 0, self
::DIR_NEXT
);
318 // Generate feed elements enclosed between header and footer.
320 if ( $items->numRows() ) {
321 foreach ( $items as $row ) {
322 $feed->outItem( $this->feedItem( $row ) );
325 $feed->outItem( $this->feedEmpty() );
330 function feedEmpty() {
332 $this->msg( 'nohistory' )->inContentLanguage()->text(),
333 $this->msg( 'history-feed-empty' )->inContentLanguage()->parseAsBlock(),
334 $this->getTitle()->getFullURL(),
335 wfTimestamp( TS_MW
),
337 $this->getTitle()->getTalkPage()->getFullURL()
342 * Generate a FeedItem object from a given revision table row
343 * Borrows Recent Changes' feed generation functions for formatting;
344 * includes a diff to the previous revision (if any).
346 * @param stdClass|array $row Database row
349 function feedItem( $row ) {
350 $rev = new Revision( $row, 0, $this->getTitle() );
352 $text = FeedUtils
::formatDiffRow(
354 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
356 $rev->getTimestamp(),
359 if ( $rev->getComment() == '' ) {
360 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
361 $title = $this->msg( 'history-feed-item-nocomment',
363 $contLang->timeanddate( $rev->getTimestamp() ),
364 $contLang->date( $rev->getTimestamp() ),
365 $contLang->time( $rev->getTimestamp() )
366 )->inContentLanguage()->text();
368 $title = $rev->getUserText() .
369 $this->msg( 'colon-separator' )->inContentLanguage()->text() .
370 FeedItem
::stripComment( $rev->getComment() );
376 $this->getTitle()->getFullURL( 'diff=' . $rev->getId() . '&oldid=prev' ),
377 $rev->getTimestamp(),
379 $this->getTitle()->getTalkPage()->getFullURL()