New feature: You can edit summaries and change the Minor Edit flag.
[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 * @package MediaWiki
7 */
8
9 /**
10 * @todo document
11 * @package MediaWiki
12 */
13 class PageHistory {
14 var $mArticle, $mTitle, $mSkin;
15 var $lastline, $lastdate;
16 var $linesonpage;
17 function PageHistory( $article ) {
18 $this->mArticle =& $article;
19 $this->mTitle =& $article->mTitle;
20 }
21
22 # This shares a lot of issues (and code) with Recent Changes
23
24 function history() {
25 global $wgUser, $wgOut, $wgLang;
26
27 # If page hasn't changed, client can cache this
28
29 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) ){
30 # Client cache fresh and headers sent, nothing more to do.
31 return;
32 }
33 $fname = 'PageHistory::history';
34 wfProfileIn( $fname );
35
36 $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
37 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
38 $wgOut->setArticleFlag( false );
39 $wgOut->setArticleRelated( true );
40 $wgOut->setRobotpolicy( 'noindex,nofollow' );
41
42 if( $this->mTitle->getArticleID() == 0 ) {
43 $wgOut->addHTML( wfMsg( 'nohistory' ) );
44 wfProfileOut( $fname );
45 return;
46 }
47
48 list( $limit, $offset ) = wfCheckLimits();
49
50 /* We have to draw the latest revision from 'cur' */
51 $rawlimit = $limit;
52 $rawoffset = $offset - 1;
53 if( 0 == $offset ) {
54 $rawlimit--;
55 $rawoffset = 0;
56 }
57 /* Check one extra row to see whether we need to show 'next' and diff links */
58 $limitplus = $rawlimit + 1;
59
60 $namespace = $this->mTitle->getNamespace();
61 $title = $this->mTitle->getText();
62
63 $db =& wfGetDB( DB_SLAVE );
64 $use_index = $db->useIndexClause( 'name_title_timestamp' );
65 $oldtable = $db->tableName( 'old' );
66
67 $sql = "SELECT old_id,old_user," .
68 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
69 "FROM $oldtable $use_index " .
70 "WHERE old_namespace={$namespace} AND " .
71 "old_title='" . $db->strencode( $this->mTitle->getDBkey() ) . "' " .
72 "ORDER BY inverse_timestamp".$db->limitResult($limitplus,$rawoffset);
73 $res = $db->query( $sql, $fname );
74
75 $revs = $db->numRows( $res );
76
77 if( $revs < $limitplus ) // the sql above tries to fetch one extra
78 $this->linesonpage = $revs;
79 else
80 $this->linesonpage = $revs - 1;
81
82 $atend = ($revs < $limitplus);
83
84 $this->mSkin = $wgUser->getSkin();
85 $numbar = wfViewPrevNext(
86 $offset, $limit,
87 $this->mTitle->getPrefixedText(),
88 'action=history', $atend );
89 $s = $numbar;
90 if($this->linesonpage > 0) {
91 $submitpart1 = '<input class="historysubmit" type="submit" accesskey="'.wfMsg('accesskey-compareselectedversions').
92 '" title="'.wfMsg('tooltip-compareselectedversions').'" value="'.wfMsg('compareselectedversions').'"';
93 $this->submitbuttonhtml1 = $submitpart1 . ' />';
94 $this->submitbuttonhtml2 = $submitpart1 . ' id="historysubmit" />';
95 }
96 $s .= $this->beginHistoryList();
97 $counter = 1;
98 if( $offset == 0 ){
99 $this->linesonpage++;
100 $s .= $this->historyLine(
101 $this->mArticle->getTimestamp(),
102 $this->mArticle->getUser(),
103 $this->mArticle->getUserText(), $namespace,
104 $title, 0, $this->mArticle->getComment(),
105 ( $this->mArticle->getMinorEdit() > 0 ),
106 $counter++
107 );
108 }
109 while ( $line = $db->fetchObject( $res ) ) {
110 $s .= $this->historyLine(
111 $line->old_timestamp, $line->old_user,
112 $line->old_user_text, $namespace,
113 $title, $line->old_id,
114 $line->old_comment, ( $line->old_minor_edit > 0 ),
115 $counter++
116 );
117 }
118 $s .= $this->endHistoryList( !$atend );
119 $s .= $numbar;
120 $wgOut->addHTML( $s );
121 wfProfileOut( $fname );
122 }
123
124 function beginHistoryList() {
125 global $wgTitle;
126 $this->lastdate = $this->lastline = '';
127 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
128 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
129 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
130 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
131 $s .= !empty($this->submitbuttonhtml1) ? $this->submitbuttonhtml1."\n":'';
132 $s .= '<ul id="pagehistory">';
133 return $s;
134 }
135
136 function endHistoryList( $skip = false ) {
137 $last = wfMsg( 'last' );
138
139 $s = $skip ? '' : preg_replace( "/!OLDID![0-9]+!/", $last, $this->lastline );
140 $s .= '</ul>';
141 $s .= !empty($this->submitbuttonhtml2) ? $this->submitbuttonhtml2 : '';
142 $s .= '</form>';
143 return $s;
144 }
145
146 function historyLine( $ts, $u, $ut, $ns, $ttl, $oid, $c, $isminor, $counter = '' ) {
147 global $wgLang, $wgContLang, $wgUser;
148 global $wgStylePath, $wgAllowEditComments, $wgUserEditCommentTimeout;
149
150 static $message;
151 if( !isset( $message ) ) {
152 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
153 $message[$msg] = wfMsg( $msg );
154 }
155 }
156
157 if ( $oid && $this->lastline ) {
158 $ret = preg_replace( "/!OLDID!([0-9]+)!/", $this->mSkin->makeKnownLinkObj(
159 $this->mTitle, $message['last'], "diff=\\1&oldid={$oid}",'' ,'' ,' tabindex="'.$counter.'"' ), $this->lastline );
160 } else {
161 $ret = '';
162 }
163 $dt = $wgLang->timeanddate( $ts, true );
164
165 if ( $oid ) {
166 $q = 'oldid='.$oid;
167 } else {
168 $q = '';
169 }
170 $link = $this->mSkin->makeKnownLinkObj( $this->mTitle, $dt, $q );
171
172 if ( 0 == $u ) {
173 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
174 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
175 htmlspecialchars( $ut ), 'target=' . urlencode( $ut ) );
176 } else {
177 $userPage =& Title::makeTitle( NS_USER, $ut );
178 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $ut ) );
179 }
180
181 $s = '<li>';
182 if ( $oid ) {
183 $curlink = $this->mSkin->makeKnownLinkObj( $this->mTitle, $message['cur'],
184 'diff=0&oldid='.$oid );
185 } else {
186 $curlink = $message['cur'];
187 }
188 $arbitrary = '';
189 if( $this->linesonpage > 1) {
190 # XXX: move title texts to javascript
191 $checkmark = '';
192 if ( !$oid ) {
193 $arbitrary = '<input type="radio" style="visibility:hidden" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'" />';
194 $checkmark = ' checked="checked"';
195 } else {
196 if( $counter == 2 ) $checkmark = ' checked="checked"';
197 $arbitrary = '<input type="radio" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'"'.$checkmark.' />';
198 $checkmark = '';
199 }
200 $arbitrary .= '<input type="radio" name="diff" value="'.$oid.'" title="'.$message['selectnewerversionfordiff'].'"'.$checkmark.' />';
201 }
202 $s .= "({$curlink}) (!OLDID!{$oid}!) $arbitrary {$link} <span class='user'>{$ul}</span>";
203
204 # Show an edit user comments icon if conditions are met.
205 # Option must be on, user logged in, user edited comment or a sysop,
206 # and the article must be recent enough.
207 if ( $wgAllowEditComments && $wgUser->getID() &&
208 ($u == $wgUser->getID() || $wgUser->isAllowed(EDIT_COMMENT_ALL)) &&
209 ($wgUser->isAllowed(EDIT_COMMENT_ALL) || $wgUserEditCommentTimeout < 0 ||
210 $ts >= wfTimestampPlus( wfTimestampNow(), -$wgUserEditCommentTimeout * 60)) ) {
211
212 $tooltip = wfMsg( "ectooltip" );
213 $rt = $this->mTitle->getPrefixedURL();
214 $rt = $this->mSkin->makeKnownLinkObj( $this->mTitle, "<img src=\"".$wgStylePath."/common/images/editcomment_icon.gif\" alt=\"{$tooltip}\" />",
215 "action=editcomment&oldid={$oid}&returnto={$rt}&returntoaction=history" );
216
217 # ***** Kludge ****
218 # Swap out the tool tip created by makeKnownLink() with one appropriate for this link.
219 $rt = preg_replace( '/title\=\"[^\"]*\"/', 'title="' . $tooltip . '"', $rt);
220
221 $s .= $rt;
222 }
223
224 $s .= $isminor ? ' <span class="minor">'.$message['minoreditletter'].'</span>': '' ;
225
226
227 if ( '' != $c && '*' != $c ) {
228 $c = $this->mSkin->formatcomment( $c, $this->mTitle );
229 $s .= " <em>($c)</em>";
230 }
231 $s .= '</li>';
232
233 $this->lastline = $s;
234 return $ret;
235 }
236
237 }
238
239 ?>