ampersand 4eva
[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, $wgRequest,
26 $wgTitle;
27
28 # If page hasn't changed, client can cache this
29
30 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) ){
31 # Client cache fresh and headers sent, nothing more to do.
32 return;
33 }
34 $fname = 'PageHistory::history';
35 wfProfileIn( $fname );
36
37 $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
38 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
39 $wgOut->setArticleFlag( false );
40 $wgOut->setArticleRelated( true );
41 $wgOut->setRobotpolicy( 'noindex,nofollow' );
42
43 $id = $this->mTitle->getArticleID();
44 if( $id == 0 ) {
45 $wgOut->addHTML( wfMsg( 'nohistory' ) );
46 wfProfileOut( $fname );
47 return;
48 }
49
50 $limit = $wgRequest->getInt('limit');
51 if (!$limit) $limit = 50;
52 $offset = $wgRequest->getText('offset');
53 if (!isset($offset) || !preg_match("/^[0-9]+$/", $offset)) $offset = 0;
54
55 /* Check one extra row to see whether we need to show 'next' and diff links */
56 $limitplus = $limit + 1;
57
58 $namespace = $this->mTitle->getNamespace();
59 $title = $this->mTitle->getText();
60 $uid = $wgUser->getID();
61 $db =& wfGetDB( DB_SLAVE );
62 if ($uid && $wgShowUpdatedMarker && $wgUser->getOption( 'showupdated' ))
63 $notificationtimestamp = $db->selectField( 'watchlist',
64 'wl_notificationtimestamp',
65 array( 'wl_namespace' => $namespace, 'wl_title' => $this->mTitle->getDBkey(), 'wl_user' => $uid ),
66 $fname );
67 else $notificationtimestamp = false;
68
69 $use_index = $db->useIndexClause( 'rev_timestamp' );
70 $revision = $db->tableName( 'revision' );
71
72 $limits = $offsets = "";
73 $dir = 0;
74 if ($wgRequest->getText("dir") == "prev")
75 $dir = 1;
76
77 list($dirs, $oper) = array("DESC", "<");
78 if ($dir) {
79 list($dirs, $oper) = array("ASC", ">");
80 }
81
82 if ($offset)
83 $offsets .= " AND rev_timestamp $oper '$offset' ";
84 if ($limit)
85 $limits .= " LIMIT $limitplus ";
86
87 $sql = "SELECT rev_id,rev_user," .
88 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit ".
89 "FROM $revision $use_index " .
90 "WHERE rev_page=$id " .
91 $offsets .
92 "ORDER BY rev_timestamp $dirs " .
93 $limits;
94 $res = $db->query( $sql, $fname );
95
96 $revs = $db->numRows( $res );
97
98 if( $revs < $limitplus ) // the sql above tries to fetch one extra
99 $this->linesonpage = $revs;
100 else
101 $this->linesonpage = $revs - 1;
102
103 $atend = ($revs < $limitplus);
104
105 $this->mSkin = $wgUser->getSkin();
106
107 $pages = array();
108 $lowts = 0;
109 while ($line = $db->fetchObject($res)) {
110 $pages[] = $line;
111 }
112 if ($dir) $pages = array_reverse($pages);
113 if (count($pages) > 1)
114 $lowts = $pages[count($pages) - 2]->rev_timestamp;
115 else
116 $lowts = $pages[count($pages) - 1]->rev_timestamp;
117
118
119 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$offset}&limit={$limit}");
120 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$lowts}&limit={$limit}");
121 $urls = array();
122 foreach (array(20, 50, 100, 250, 500) as $num) {
123 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
124 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
125 }
126 $bits = implode($urls, ' | ');
127 $numbar = wfMsg("viewprevnext",
128 "<a href=\"$prevurl\">".wfMsg("prevn", $limit)."</a>",
129 "<a href=\"$nexturl\">".wfMsg("nextn", $limit)."</a>",
130 $bits);
131
132 $s = $numbar;
133 if($this->linesonpage > 0) {
134 $submitpart1 = '<input class="historysubmit" type="submit" accesskey="'.wfMsg('accesskey-compareselectedversions').
135 '" title="'.wfMsg('tooltip-compareselectedversions').'" value="'.wfMsg('compareselectedversions').'"';
136 $this->submitbuttonhtml1 = $submitpart1 . ' />';
137 $this->submitbuttonhtml2 = $submitpart1 . ' id="historysubmit" />';
138 }
139 $s .= $this->beginHistoryList();
140 $counter = 1;
141 foreach($pages as $line) {
142 $s .= $this->historyLine(
143 $line->rev_timestamp, $line->rev_user,
144 $line->rev_user_text, $namespace,
145 $title, $line->rev_id,
146 $line->rev_comment, ( $line->rev_minor_edit > 0 ),
147 $counter,
148 $notificationtimestamp,
149 ($counter == 1 && $offset == 0)
150 );
151 $counter++;
152 }
153 $s .= $this->endHistoryList( !$atend );
154 $s .= $numbar;
155 $wgOut->addHTML( $s );
156 wfProfileOut( $fname );
157 }
158
159 function beginHistoryList() {
160 global $wgTitle;
161 $this->lastdate = $this->lastline = '';
162 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
163 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
164 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
165 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
166 $s .= !empty($this->submitbuttonhtml1) ? $this->submitbuttonhtml1."\n":'';
167 $s .= '<ul id="pagehistory">';
168 return $s;
169 }
170
171 function endHistoryList( $skip = false ) {
172 $last = wfMsg( 'last' );
173
174 $s = $skip ? '' : preg_replace( "/!OLDID![0-9]+!/", $last, $this->lastline );
175 $s .= '</ul>';
176 $s .= !empty($this->submitbuttonhtml2) ? $this->submitbuttonhtml2 : '';
177 $s .= '</form>';
178 return $s;
179 }
180
181 function historyLine( $ts, $u, $ut, $ns, $ttl, $oid, $c, $isminor, $counter = '', $notificationtimestamp = false, $latest = false ) {
182 global $wgLang, $wgContLang;
183
184 static $message;
185 if( !isset( $message ) ) {
186 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
187 $message[$msg] = wfMsg( $msg );
188 }
189 }
190
191 if ( $oid && $this->lastline ) {
192 $ret = preg_replace( "/!OLDID!([0-9]+)!/", $this->mSkin->makeKnownLinkObj(
193 $this->mTitle, $message['last'], "diff=\\1&oldid={$oid}",'' ,'' ,' tabindex="'.$counter.'"' ), $this->lastline );
194 } else {
195 $ret = '';
196 }
197 $dt = $wgLang->timeanddate( $ts, true );
198
199 if ( $oid ) {
200 $q = 'oldid='.$oid;
201 } else {
202 $q = '';
203 }
204 $link = $this->mSkin->makeKnownLinkObj( $this->mTitle, $dt, $q );
205
206 if ( 0 == $u ) {
207 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
208 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
209 htmlspecialchars( $ut ), 'target=' . urlencode( $ut ) );
210 } else {
211 $userPage =& Title::makeTitle( NS_USER, $ut );
212 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $ut ) );
213 }
214
215 $s = '<li>';
216 if ( $oid && !$latest ) {
217 $curlink = $this->mSkin->makeKnownLinkObj( $this->mTitle, $message['cur'],
218 'diff=0&oldid='.$oid );
219 } else {
220 $curlink = $message['cur'];
221 }
222 $arbitrary = '';
223 if( $this->linesonpage > 1) {
224 # XXX: move title texts to javascript
225 $checkmark = '';
226 if ( !$oid || $latest ) {
227 $arbitrary = '<input type="radio" style="visibility:hidden" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'" />';
228 $checkmark = ' checked="checked"';
229 } else {
230 if( $counter == 2 ) $checkmark = ' checked="checked"';
231 $arbitrary = '<input type="radio" name="oldid" value="'.$oid.'" title="'.$message['selectolderversionfordiff'].'"'.$checkmark.' />';
232 $checkmark = '';
233 }
234 $arbitrary .= '<input type="radio" name="diff" value="'.$oid.'" title="'.$message['selectnewerversionfordiff'].'"'.$checkmark.' />';
235 }
236 $s .= "({$curlink}) (!OLDID!{$oid}!) $arbitrary {$link} <span class='user'>{$ul}</span>";
237 $s .= $isminor ? ' <span class="minor">'.$message['minoreditletter'].'</span>': '' ;
238
239
240 $s .= $this->mSkin->commentBlock( $c, $this->mTitle );
241 if ($notificationtimestamp && ($ts >= $notificationtimestamp)) {
242 $s .= wfMsg( 'updatedmarker' );
243 }
244 $s .= '</li>';
245
246 $this->lastline = $s;
247 return $ret;
248 }
249
250 }
251
252 ?>