Adding/updating Persian translations
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 */
7
8 /**
9 * This class handles printing the history page for an article. In order to
10 * be efficient, it uses timestamps rather than offsets for paging, to avoid
11 * costly LIMIT,offset queries.
12 *
13 * Construct it by passing in an Article, and call $h->history() to print the
14 * history.
15 *
16 */
17 class PageHistory {
18 const DIR_PREV = 0;
19 const DIR_NEXT = 1;
20
21 var $mArticle, $mTitle, $mSkin;
22 var $lastdate;
23 var $linesonpage;
24 var $mNotificationTimestamp;
25 var $mLatestId = null;
26
27 /**
28 * Construct a new PageHistory.
29 *
30 * @param Article $article
31 * @returns nothing
32 */
33 function __construct($article) {
34 global $wgUser;
35
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38 $this->mNotificationTimestamp = NULL;
39 $this->mSkin = $wgUser->getSkin();
40 }
41
42 /**
43 * Print the history page for an article.
44 *
45 * @returns nothing
46 */
47 function history() {
48 global $wgOut, $wgRequest, $wgTitle;
49
50 /*
51 * Allow client caching.
52 */
53
54 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
55 /* Client cache fresh and headers sent, nothing more to do. */
56 return;
57
58 $fname = 'PageHistory::history';
59 wfProfileIn( $fname );
60
61 /*
62 * Setup page variables.
63 */
64 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle->getPrefixedText() ) );
65 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
66 $wgOut->setArticleFlag( false );
67 $wgOut->setArticleRelated( true );
68 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 $wgOut->setSyndicated( true );
70 $wgOut->setFeedAppendQuery( 'action=history' );
71
72 $logPage = SpecialPage::getTitleFor( 'Log' );
73 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle->getPrefixedUrl() );
74 $wgOut->setSubtitle( $logLink );
75
76 $feedType = $wgRequest->getVal( 'feed' );
77 if( $feedType ) {
78 wfProfileOut( $fname );
79 return $this->feed( $feedType );
80 }
81
82 /*
83 * Fail if article doesn't exist.
84 */
85 if( !$this->mTitle->exists() ) {
86 $wgOut->addWikiMsg( 'nohistory' );
87 wfProfileOut( $fname );
88 return;
89 }
90
91 /*
92 * "go=first" means to jump to the last (earliest) history page.
93 * This is deprecated, it no longer appears in the user interface
94 */
95 if ( $wgRequest->getText("go") == 'first' ) {
96 $limit = $wgRequest->getInt( 'limit', 50 );
97 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
98 return;
99 }
100
101 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle ) );
102
103 /**
104 * Do the list
105 */
106 $pager = new PageHistoryPager( $this );
107 $this->linesonpage = $pager->getNumRows();
108 $wgOut->addHTML(
109 $pager->getNavigationBar() .
110 $this->beginHistoryList() .
111 $pager->getBody() .
112 $this->endHistoryList() .
113 $pager->getNavigationBar()
114 );
115 wfProfileOut( $fname );
116 }
117
118 /**
119 * Creates begin of history list with a submit button
120 *
121 * @return string HTML output
122 */
123 function beginHistoryList() {
124 global $wgTitle;
125 $this->lastdate = '';
126 $s = wfMsgExt( 'histlegend', array( 'parse') );
127 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
128 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
129
130 // The following line is SUPPOSED to have double-quotes around the
131 // $prefixedkey variable, because htmlspecialchars() doesn't escape
132 // single-quotes.
133 //
134 // On at least two occasions people have changed it to single-quotes,
135 // which creates invalid HTML and incorrect display of the resulting
136 // link.
137 //
138 // Please do not break this a third time. Thank you for your kind
139 // consideration and cooperation.
140 //
141 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
142
143 $s .= $this->submitButton();
144 $s .= '<ul id="pagehistory">' . "\n";
145 return $s;
146 }
147
148 /**
149 * Creates end of history list with a submit button
150 *
151 * @return string HTML output
152 */
153 function endHistoryList() {
154 $s = '</ul>';
155 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
156 $s .= '</form>';
157 return $s;
158 }
159
160 /**
161 * Creates a submit button
162 *
163 * @param array $bits optional CSS ID
164 * @return string HTML output for the submit button
165 */
166 function submitButton( $bits = array() ) {
167 # Disable submit button if history has 1 revision only
168 if ( $this->linesonpage > 1 ) {
169 return Xml::submitButton( wfMsg( 'compareselectedversions' ),
170 $bits + array(
171 'class' => 'historysubmit',
172 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
173 'title' => wfMsg( 'tooltip-compareselectedversions' ),
174 )
175 );
176 } else {
177 return '';
178 }
179 }
180
181 /**
182 * Returns a row from the history printout.
183 *
184 * @todo document some more, and maybe clean up the code (some params redundant?)
185 *
186 * @param object $row The database row corresponding to the line (or is it the previous line?).
187 * @param object $next The database row corresponding to the next line (or is it this one?).
188 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
189 * @param $notificationtimestamp
190 * @param bool $latest Whether this row corresponds to the page's latest revision.
191 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
192 * @return string HTML output for the row
193 */
194 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
195 global $wgUser, $wgLang;
196 $rev = new Revision( $row );
197 $rev->setTitle( $this->mTitle );
198
199 $s = '<li>';
200 $curlink = $this->curLink( $rev, $latest );
201 $lastlink = $this->lastLink( $rev, $next, $counter );
202 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
203 $link = $this->revLink( $rev );
204
205 $user = $this->mSkin->userLink( $rev->getUser(), $rev->getUserText() )
206 . $this->mSkin->userToolLinks( $rev->getUser(), $rev->getUserText() );
207
208 $s .= "($curlink) ($lastlink) $arbitrary";
209
210 if( $wgUser->isAllowed( 'deleterevision' ) ) {
211 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
212 if( $firstInList ) {
213 // We don't currently handle well changing the top revision's settings
214 $del = wfMsgHtml( 'rev-delundel' );
215 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
216 // If revision was hidden from sysops
217 $del = wfMsgHtml( 'rev-delundel' );
218 } else {
219 $del = $this->mSkin->makeKnownLinkObj( $revdel,
220 wfMsg( 'rev-delundel' ),
221 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
222 '&oldid=' . urlencode( $rev->getId() ) );
223 }
224 $s .= " (<small>$del</small>) ";
225 }
226
227 $s .= " $link";
228 #getUser is safe, but this avoids making the invalid untargeted contribs links
229 if( $row->rev_deleted & Revision::DELETED_USER ) {
230 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
231 }
232 $s .= " <span class='history-user'>$user</span>";
233
234 if( $row->rev_minor_edit ) {
235 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
236 }
237
238 if ( !is_null( $size = $rev->getSize() ) ) {
239 if ( $size == 0 )
240 $stxt = wfMsgHtml( 'historyempty' );
241 else
242 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
243 $s .= " <span class=\"history-size\">$stxt</span>";
244 }
245
246 #getComment is safe, but this is better formatted
247 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
248 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
249 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
250 } else {
251 $s .= $this->mSkin->revComment( $rev );
252 }
253
254 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
255 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
256 }
257 #add blurb about text having been deleted
258 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
259 $s .= ' ' . wfMsgHtml( 'deletedrev' );
260 }
261
262 $tools = array();
263
264 if ( !is_null( $next ) && is_object( $next ) ) {
265 if( !$this->mTitle->getUserPermissionsErrors( 'rollback', $wgUser )
266 && !$this->mTitle->getUserPermissionsErrors( 'edit', $wgUser )
267 && $latest ) {
268 $tools[] = '<span class="mw-rollback-link">'
269 . $this->mSkin->buildRollbackLink( $rev )
270 . '</span>';
271 }
272
273 if( $this->mTitle->quickUserCan( 'edit' ) ) {
274 $undolink = $this->mSkin->makeKnownLinkObj(
275 $this->mTitle,
276 wfMsgHtml( 'editundo' ),
277 'action=edit&undoafter=' . $next->rev_id . '&undo=' . $rev->getId()
278 );
279 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
280 }
281 }
282
283 if( $tools ) {
284 $s .= ' (' . implode( ' | ', $tools ) . ')';
285 }
286
287 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
288
289 $s .= "</li>\n";
290
291 return $s;
292 }
293
294 /** @todo document */
295 function revLink( $rev ) {
296 global $wgLang;
297 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
298 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
299 $link = $this->mSkin->makeKnownLinkObj(
300 $this->mTitle, $date, "oldid=" . $rev->getId() );
301 } else {
302 $link = $date;
303 }
304 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
305 return '<span class="history-deleted">' . $link . '</span>';
306 }
307 return $link;
308 }
309
310 /** @todo document */
311 function curLink( $rev, $latest ) {
312 $cur = wfMsgExt( 'cur', array( 'escape') );
313 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
314 return $cur;
315 } else {
316 return $this->mSkin->makeKnownLinkObj(
317 $this->mTitle, $cur,
318 'diff=' . $this->getLatestID() .
319 "&oldid=" . $rev->getId() );
320 }
321 }
322
323 /** @todo document */
324 function lastLink( $rev, $next, $counter ) {
325 $last = wfMsgExt( 'last', array( 'escape' ) );
326 if ( is_null( $next ) ) {
327 # Probably no next row
328 return $last;
329 } elseif ( $next === 'unknown' ) {
330 # Next row probably exists but is unknown, use an oldid=prev link
331 return $this->mSkin->makeKnownLinkObj(
332 $this->mTitle,
333 $last,
334 "diff=" . $rev->getId() . "&oldid=prev" );
335 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
336 return $last;
337 } else {
338 return $this->mSkin->makeKnownLinkObj(
339 $this->mTitle,
340 $last,
341 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
342 /*,
343 '',
344 '',
345 "tabindex={$counter}"*/ );
346 }
347 }
348
349 /**
350 * Create radio buttons for page history
351 *
352 * @param object $rev Revision
353 * @param bool $firstInList Is this version the first one?
354 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
355 * @return string HTML output for the radio buttons
356 */
357 function diffButtons( $rev, $firstInList, $counter ) {
358 if( $this->linesonpage > 1) {
359 $radio = array(
360 'type' => 'radio',
361 'value' => $rev->getId(),
362 );
363
364 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
365 $radio['disabled'] = 'disabled';
366 }
367
368 /** @todo: move title texts to javascript */
369 if ( $firstInList ) {
370 $first = Xml::element( 'input', array_merge(
371 $radio,
372 array(
373 'style' => 'visibility:hidden',
374 'name' => 'oldid' ) ) );
375 $checkmark = array( 'checked' => 'checked' );
376 } else {
377 if( $counter == 2 ) {
378 $checkmark = array( 'checked' => 'checked' );
379 } else {
380 $checkmark = array();
381 }
382 $first = Xml::element( 'input', array_merge(
383 $radio,
384 $checkmark,
385 array( 'name' => 'oldid' ) ) );
386 $checkmark = array();
387 }
388 $second = Xml::element( 'input', array_merge(
389 $radio,
390 $checkmark,
391 array( 'name' => 'diff' ) ) );
392 return $first . $second;
393 } else {
394 return '';
395 }
396 }
397
398 /** @todo document */
399 function getLatestId() {
400 if( is_null( $this->mLatestId ) ) {
401 $id = $this->mTitle->getArticleID();
402 $db = wfGetDB(DB_SLAVE);
403 $this->mLatestId = $db->selectField( 'page',
404 "page_latest",
405 array( 'page_id' => $id ),
406 'PageHistory::getLatestID' );
407 }
408 return $this->mLatestId;
409 }
410
411 /**
412 * Fetch an array of revisions, specified by a given limit, offset and
413 * direction. This is now only used by the feeds. It was previously
414 * used by the main UI but that's now handled by the pager.
415 */
416 function fetchRevisions($limit, $offset, $direction) {
417 $fname = 'PageHistory::fetchRevisions';
418
419 $dbr = wfGetDB( DB_SLAVE );
420
421 if ($direction == PageHistory::DIR_PREV)
422 list($dirs, $oper) = array("ASC", ">=");
423 else /* $direction == PageHistory::DIR_NEXT */
424 list($dirs, $oper) = array("DESC", "<=");
425
426 if ($offset)
427 $offsets = array("rev_timestamp $oper '$offset'");
428 else
429 $offsets = array();
430
431 $page_id = $this->mTitle->getArticleID();
432
433 $res = $dbr->select(
434 'revision',
435 Revision::selectFields(),
436 array_merge(array("rev_page=$page_id"), $offsets),
437 $fname,
438 array('ORDER BY' => "rev_timestamp $dirs",
439 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
440 );
441
442 $result = array();
443 while (($obj = $dbr->fetchObject($res)) != NULL)
444 $result[] = $obj;
445
446 return $result;
447 }
448
449 /** @todo document */
450 function getNotificationTimestamp() {
451 global $wgUser, $wgShowUpdatedMarker;
452 $fname = 'PageHistory::getNotficationTimestamp';
453
454 if ($this->mNotificationTimestamp !== NULL)
455 return $this->mNotificationTimestamp;
456
457 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
458 return $this->mNotificationTimestamp = false;
459
460 $dbr = wfGetDB(DB_SLAVE);
461
462 $this->mNotificationTimestamp = $dbr->selectField(
463 'watchlist',
464 'wl_notificationtimestamp',
465 array( 'wl_namespace' => $this->mTitle->getNamespace(),
466 'wl_title' => $this->mTitle->getDBkey(),
467 'wl_user' => $wgUser->getID()
468 ),
469 $fname);
470
471 // Don't use the special value reserved for telling whether the field is filled
472 if ( is_null( $this->mNotificationTimestamp ) ) {
473 $this->mNotificationTimestamp = false;
474 }
475
476 return $this->mNotificationTimestamp;
477 }
478
479 /**
480 * Output a subscription feed listing recent edits to this page.
481 * @param string $type
482 */
483 function feed( $type ) {
484 require_once 'SpecialRecentchanges.php';
485
486 global $wgFeedClasses;
487 if( !isset( $wgFeedClasses[$type] ) ) {
488 global $wgOut;
489 $wgOut->addWikiMsg( 'feed-invalid' );
490 return;
491 }
492
493 $feed = new $wgFeedClasses[$type](
494 $this->mTitle->getPrefixedText() . ' - ' .
495 wfMsgForContent( 'history-feed-title' ),
496 wfMsgForContent( 'history-feed-description' ),
497 $this->mTitle->getFullUrl( 'action=history' ) );
498
499 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
500 $feed->outHeader();
501 if( $items ) {
502 foreach( $items as $row ) {
503 $feed->outItem( $this->feedItem( $row ) );
504 }
505 } else {
506 $feed->outItem( $this->feedEmpty() );
507 }
508 $feed->outFooter();
509 }
510
511 function feedEmpty() {
512 global $wgOut;
513 return new FeedItem(
514 wfMsgForContent( 'nohistory' ),
515 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
516 $this->mTitle->getFullUrl(),
517 wfTimestamp( TS_MW ),
518 '',
519 $this->mTitle->getTalkPage()->getFullUrl() );
520 }
521
522 /**
523 * Generate a FeedItem object from a given revision table row
524 * Borrows Recent Changes' feed generation functions for formatting;
525 * includes a diff to the previous revision (if any).
526 *
527 * @param $row
528 * @return FeedItem
529 */
530 function feedItem( $row ) {
531 $rev = new Revision( $row );
532 $rev->setTitle( $this->mTitle );
533 $text = rcFormatDiffRow( $this->mTitle,
534 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
535 $rev->getId(),
536 $rev->getTimestamp(),
537 $rev->getComment() );
538
539 if( $rev->getComment() == '' ) {
540 global $wgContLang;
541 $title = wfMsgForContent( 'history-feed-item-nocomment',
542 $rev->getUserText(),
543 $wgContLang->timeanddate( $rev->getTimestamp() ) );
544 } else {
545 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
546 }
547
548 return new FeedItem(
549 $title,
550 $text,
551 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
552 $rev->getTimestamp(),
553 $rev->getUserText(),
554 $this->mTitle->getTalkPage()->getFullUrl() );
555 }
556
557 /**
558 * Quickie hack... strip out wikilinks to more legible form from the comment.
559 */
560 function stripComment( $text ) {
561 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
562 }
563 }
564
565
566 /**
567 * @addtogroup Pager
568 */
569 class PageHistoryPager extends ReverseChronologicalPager {
570 public $mLastRow = false, $mPageHistory;
571
572 function __construct( $pageHistory ) {
573 parent::__construct();
574 $this->mPageHistory = $pageHistory;
575 }
576
577 function getQueryInfo() {
578 return array(
579 'tables' => 'revision',
580 'fields' => Revision::selectFields(),
581 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
582 'options' => array( 'USE INDEX' => 'page_timestamp' )
583 );
584 }
585
586 function getIndexField() {
587 return 'rev_timestamp';
588 }
589
590 function formatRow( $row ) {
591 if ( $this->mLastRow ) {
592 $latest = $this->mCounter == 1 && $this->mOffset == '';
593 $firstInList = $this->mCounter == 1;
594 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
595 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
596 } else {
597 $s = '';
598 }
599 $this->mLastRow = $row;
600 return $s;
601 }
602
603 function getStartBody() {
604 $this->mLastRow = false;
605 $this->mCounter = 1;
606 return '';
607 }
608
609 function getEndBody() {
610 if ( $this->mLastRow ) {
611 $latest = $this->mCounter == 1 && $this->mOffset == 0;
612 $firstInList = $this->mCounter == 1;
613 if ( $this->mIsBackwards ) {
614 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
615 if ( $this->mOffset == '' ) {
616 $next = null;
617 } else {
618 $next = 'unknown';
619 }
620 } else {
621 # The next row is the past-the-end row
622 $next = $this->mPastTheEndRow;
623 }
624 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
625 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
626 } else {
627 $s = '';
628 }
629 return $s;
630 }
631 }