Tweak offset/limit parameters for correctness. Enable 'last' diff link on
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2
3 /* Page history
4 Split off from Article.php and Skin.php, 2003-12-22
5 */
6
7 class PageHistory {
8 var $mArticle, $mTitle, $mSkin;
9 var $lastline, $lastdate;
10
11 function PageHistory( $article ) {
12 $this->mArticle =& $article;
13 $this->mTitle =& $article->mTitle;
14 }
15
16 # This shares a lot of issues (and code) with Recent Changes
17
18 function history()
19 {
20 global $wgUser, $wgOut, $wgLang;
21
22 # If page hasn't changed, client can cache this
23
24 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) ){
25 # Client cache fresh and headers sent, nothing more to do.
26 return;
27 }
28 $fname = "PageHistory::history";
29 wfProfileIn( $fname );
30
31 $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
32 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
33 $wgOut->setArticleFlag( false );
34 $wgOut->setRobotpolicy( "noindex,nofollow" );
35
36 if( $this->mTitle->getArticleID() == 0 ) {
37 $wgOut->addHTML( wfMsg( "nohistory" ) );
38 wfProfileOut( $fname );
39 return;
40 }
41
42 list( $limit, $offset ) = wfCheckLimits();
43
44 /* We have to draw the latest revision from 'cur' */
45 $rawlimit = $limit;
46 $rawoffset = $offset - 1;
47 if( 0 == $offset ) {
48 $rawlimit--;
49 $rawoffset = 0;
50 }
51 /* Check one extra row to see whether we need to show 'next' and diff links */
52 $limitplus = $rawlimit + 1;
53
54 $namespace = $this->mTitle->getNamespace();
55 $title = $this->mTitle->getText();
56 $sql = "SELECT old_id,old_user," .
57 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
58 "FROM old USE INDEX (name_title_timestamp) " .
59 "WHERE old_namespace={$namespace} AND " .
60 "old_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
61 "ORDER BY inverse_timestamp LIMIT $rawoffset, $limitplus";
62 $res = wfQuery( $sql, DB_READ, $fname );
63
64 $revs = wfNumRows( $res );
65 $atend = ($revs < $limitplus);
66
67 $this->mSkin = $wgUser->getSkin();
68 $numbar = wfViewPrevNext(
69 $offset, $limit,
70 $this->mTitle->getPrefixedText(),
71 "action=history" );
72 $s = $numbar;
73 $s .= $this->beginHistoryList();
74
75 if( $offset == 0 )
76 $s .= $this->historyLine( $this->mArticle->getTimestamp(), $this->mArticle->getUser(),
77 $this->mArticle->getUserText(), $namespace,
78 $title, 0, $this->mArticle->getComment(),
79 ( $this->mArticle->getMinorEdit() > 0 ) );
80
81 while ( $line = wfFetchObject( $res ) ) {
82 $s .= $this->historyLine( $line->old_timestamp, $line->old_user,
83 $line->old_user_text, $namespace,
84 $title, $line->old_id,
85 $line->old_comment, ( $line->old_minor_edit > 0 ) );
86 }
87 $s .= $this->endHistoryList( !$atend );
88 $s .= $numbar;
89 $wgOut->addHTML( $s );
90 wfProfileOut( $fname );
91 }
92
93 function beginHistoryList()
94 {
95 $this->lastdate = $this->lastline = "";
96 $s = "\n<p>" . wfMsg( "histlegend" ) . "\n<ul>";
97 return $s;
98 }
99
100 function endHistoryList( $skip = false )
101 {
102 $last = wfMsg( "last" );
103
104 $s = $skip ? "" : preg_replace( "/!OLDID![0-9]+!/", $last, $this->lastline );
105 $s .= "</ul>\n";
106 return $s;
107 }
108
109 function historyLine( $ts, $u, $ut, $ns, $ttl, $oid, $c, $isminor )
110 {
111 global $wgLang;
112
113 $artname = Title::makeName( $ns, $ttl );
114 $last = wfMsg( "last" );
115 $cur = wfMsg( "cur" );
116 $cr = wfMsg( "currentrev" );
117
118 if ( $oid && $this->lastline ) {
119 $ret = preg_replace( "/!OLDID!([0-9]+)!/", $this->mSkin->makeKnownLink(
120 $artname, $last, "diff=\\1&oldid={$oid}" ), $this->lastline );
121 } else {
122 $ret = "";
123 }
124 $dt = $wgLang->timeanddate( $ts, true );
125
126 if ( $oid ) {
127 $q = "oldid={$oid}";
128 } else {
129 $q = "";
130 }
131 $link = $this->mSkin->makeKnownLink( $artname, $dt, $q );
132
133 if ( 0 == $u ) {
134 $ul = $this->mSkin->makeKnownLink( $wgLang->specialPage( "Contributions" ),
135 $ut, "target=" . $ut );
136 } else {
137 $ul = $this->mSkin->makeLink( $wgLang->getNsText(
138 Namespace::getUser() ) . ":{$ut}", $ut );
139 }
140
141 $s = "<li>";
142 if ( $oid ) {
143 $curlink = $this->mSkin->makeKnownLink( $artname, $cur,
144 "diff=0&oldid={$oid}" );
145 } else {
146 $curlink = $cur;
147 }
148 $s .= "({$curlink}) (!OLDID!{$oid}!) . .";
149
150 $M = wfMsg( "minoreditletter" );
151 if ( $isminor ) {
152 $s .= " <strong>{$M}</strong>";
153 }
154 $s .= " {$link} . . {$ul}";
155
156 if ( "" != $c && "*" != $c ) {
157 $s .= " <em>(" . wfEscapeHTML($c) . ")</em>";
158 }
159 $s .= "</li>\n";
160
161 $this->lastline = $s;
162 return $ret;
163 }
164
165 }
166
167 ?>