USE INDEX -> DB::useIndexClause()
[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, $wgShowUpdatedMarker;
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 $id = $this->mTitle->getArticleID();
43 if( $id == 0 ) {
44 $wgOut->addHTML( wfMsg( 'nohistory' ) );
45 wfProfileOut( $fname );
46 return;
47 }
48
49 list( $limit, $offset ) = wfCheckLimits();
50
51 /* Check one extra row to see whether we need to show 'next' and diff links */
52 $limitplus = $limit + 1;
53
54 $namespace = $this->mTitle->getNamespace();
55 $title = $this->mTitle->getText();
56 $uid = $wgUser->getID();
57 $db =& wfGetDB( DB_SLAVE );
58 if ($uid && $wgShowUpdatedMarker && $wgUser->getOption( 'showupdated' ))
59 $notificationtimestamp = $db->selectField( 'watchlist',
60 'wl_notificationtimestamp',
61 array( 'wl_namespace' => $namespace, 'wl_title' => $this->mTitle->getDBkey(), 'wl_user' => $uid ),
62 $fname );
63 else $notificationtimestamp = false;
64
65 $use_index = $db->useIndexClause( 'page_timestamp' );
66 $revision = $db->tableName( 'revision' );
67
68 $sql = "SELECT rev_id,rev_user," .
69 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit ".
70 "FROM $revision $use_index " .
71 "WHERE rev_page=$id " .
72 "ORDER BY inverse_timestamp ".$db->limitResult($limitplus,$offset);
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 while ( $line = $db->fetchObject( $res ) ) {
99 $s .= $this->historyLine(
100 $line->rev_timestamp, $line->rev_user,
101 $line->rev_user_text, $namespace,
102 $title, $line->rev_id,
103 $line->rev_comment, ( $line->rev_minor_edit > 0 ),
104 $counter,
105 $notificationtimestamp,
106 ($counter == 1 && $offset == 0)
107 );
108 $counter++;
109 }
110 $s .= $this->endHistoryList( !$atend );
111 $s .= $numbar;
112 $wgOut->addHTML( $s );
113 wfProfileOut( $fname );
114 }
115
116 function beginHistoryList() {
117 global $wgTitle;
118 $this->lastdate = $this->lastline = '';
119 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
120 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
121 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
122 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
123 $s .= !empty($this->submitbuttonhtml1) ? $this->submitbuttonhtml1."\n":'';
124 $s .= '<ul id="pagehistory">';
125 return $s;
126 }
127
128 function endHistoryList( $skip = false ) {
129 $last = wfMsg( 'last' );
130
131 $s = $skip ? '' : preg_replace( "/!OLDID![0-9]+!/", $last, $this->lastline );
132 $s .= '</ul>';
133 $s .= !empty($this->submitbuttonhtml2) ? $this->submitbuttonhtml2 : '';
134 $s .= '</form>';
135 return $s;
136 }
137
138 function historyLine( $ts, $u, $ut, $ns, $ttl, $oid, $c, $isminor, $counter = '', $notificationtimestamp = false, $latest = false ) {
139 global $wgLang, $wgContLang;
140
141 static $message;
142 if( !isset( $message ) ) {
143 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
144 $message[$msg] = wfMsg( $msg );
145 }
146 }
147
148 if ( $oid && $this->lastline ) {
149 $ret = preg_replace( "/!OLDID!([0-9]+)!/", $this->mSkin->makeKnownLinkObj(
150 $this->mTitle, $message['last'], "diff=\\1&oldid={$oid}",'' ,'' ,' tabindex="'.$counter.'"' ), $this->lastline );
151 } else {
152 $ret = '';
153 }
154 $dt = $wgLang->timeanddate( $ts, true );
155
156 if ( $oid ) {
157 $q = 'oldid='.$oid;
158 } else {
159 $q = '';
160 }
161 $link = $this->mSkin->makeKnownLinkObj( $this->mTitle, $dt, $q );
162
163 if ( 0 == $u ) {
164 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
165 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
166 htmlspecialchars( $ut ), 'target=' . urlencode( $ut ) );
167 } else {
168 $userPage =& Title::makeTitle( NS_USER, $ut );
169 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $ut ) );
170 }
171
172 $s = '<li>';
173 if ( $oid && !$latest ) {
174 $curlink = $this->mSkin->makeKnownLinkObj( $this->mTitle, $message['cur'],
175 'diff=0&oldid='.$oid );
176 } else {
177 $curlink = $message['cur'];
178 }
179 $arbitrary = '';
180 if( $this->linesonpage > 1) {
181 # XXX: move title texts to javascript
182 $checkmark = '';
183 if ( !$oid || $latest ) {
184 $arbitrary = '<input type="radio" style="visibility:hidden" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'" />';
185 $checkmark = ' checked="checked"';
186 } else {
187 if( $counter == 2 ) $checkmark = ' checked="checked"';
188 $arbitrary = '<input type="radio" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'"'.$checkmark.' />';
189 $checkmark = '';
190 }
191 $arbitrary .= '<input type="radio" name="diff" value="'.$oid.'" title="'.$message['selectnewerversionfordiff'].'"'.$checkmark.' />';
192 }
193 $s .= "({$curlink}) (!OLDID!{$oid}!) $arbitrary {$link} <span class='user'>{$ul}</span>";
194 $s .= $isminor ? ' <span class="minor">'.$message['minoreditletter'].'</span>': '' ;
195
196
197 if ( '' != $c && '*' != $c ) {
198 $c = $this->mSkin->formatcomment( $c, $this->mTitle );
199 $s .= " <em>($c)</em>";
200 }
201 if ($notificationtimestamp && ($ts >= $notificationtimestamp)) {
202 $s .= wfMsg( 'updatedmarker' );
203 }
204 $s .= '</li>';
205
206 $this->lastline = $s;
207 return $ret;
208 }
209
210 }
211
212 ?>