Added wfAbruptExit() function, to replace exit() calls with.
[lhc/web/wiklou.git] / includes / LogPage.php
1 <?
2 # Class to simplify the use of log pages
3
4 class LogPage {
5 /* private */ var $mTitle, $mContent, $mContentLoaded, $mId, $mComment;
6 var $mUpdateRecentChanges ;
7
8 function LogPage( $title, $defaulttext = "<ul>\n</ul>" )
9 {
10 # For now, assume title is correct dbkey
11 # and log pages always go in Wikipedia namespace
12 $this->mTitle = str_replace( " ", "_", $title );
13 $this->mId = 0;
14 $this->mUpdateRecentChanges = true ;
15 $this->mContentLoaded = false;
16 $this->getContent( $defaulttext );
17 }
18
19 function getContent( $defaulttext = "<ul>\n</ul>" )
20 {
21 $sql = "SELECT cur_id,cur_text,cur_timestamp FROM cur " .
22 "WHERE cur_namespace=" . Namespace::getWikipedia() . " AND " .
23 "cur_title='" . wfStrencode($this->mTitle ) . "'";
24 $res = wfQuery( $sql, DB_READ, "LogPage::getContent" );
25
26 if( wfNumRows( $res ) > 0 ) {
27 $s = wfFetchObject( $res );
28 $this->mId = $s->cur_id;
29 $this->mContent = $s->cur_text;
30 $this->mTimestamp = $s->cur_timestamp;
31 } else {
32 $this->mId = 0;
33 $this->mContent = $defaulttext;
34 $this->mTimestamp = wfTimestampNow();
35 }
36 $this->mContentLoaded = true; # Well, sort of
37
38 return $this->mContent;
39 }
40
41 function getTimestamp()
42 {
43 if( !$this->mContentLoaded ) {
44 $this->getContent();
45 }
46 return $this->mTimestamp;
47 }
48
49 function saveContent()
50 {
51 if( wfReadOnly() ) return;
52
53 global $wgUser;
54 $fname = "LogPage::saveContent";
55 $uid = $wgUser->getID();
56 $ut = wfStrencode( $wgUser->getName() );
57
58 if( !$this->mContentLoaded ) return false;
59 $this->mTimestamp = $now = wfTimestampNow();
60 $won = wfInvertTimestamp( $now );
61 if($this->mId == 0) {
62 $sql = "INSERT INTO cur (cur_timestamp,cur_user,cur_user_text,
63 cur_namespace,cur_title,cur_text,cur_comment,cur_restrictions,
64 inverse_timestamp,cur_touched)
65 VALUES ('{$now}', {$uid}, '{$ut}', " .
66 Namespace::getWikipedia() . ", '" .
67 wfStrencode( $this->mTitle ) . "', '" .
68 wfStrencode( $this->mContent ) . "', '" .
69 wfStrencode( $this->mComment ) . "', 'sysop', '{$won}','{$now}')";
70 wfQuery( $sql, DB_WRITE, $fname );
71 $this->mId = wfInsertId();
72 } else {
73 $sql = "UPDATE cur SET cur_timestamp='{$now}', " .
74 "cur_user={$uid}, cur_user_text='{$ut}', " .
75 "cur_text='" . wfStrencode( $this->mContent ) . "', " .
76 "cur_comment='" . wfStrencode( $this->mComment ) . "', " .
77 "cur_restrictions='sysop', inverse_timestamp='{$won}', cur_touched='{$now}' " .
78 "WHERE cur_id={$this->mId}";
79 wfQuery( $sql, DB_WRITE, $fname );
80 }
81
82 # And update recentchanges
83 if ( $this->mUpdateRecentChanges ) {
84 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time,
85 rc_user,rc_user_text,rc_namespace,rc_title,rc_comment,
86 rc_cur_id) VALUES ('{$now}','{$now}',{$uid},'{$ut}',4,'" .
87 wfStrencode( $this->mTitle ) . "','" .
88 wfStrencode( $this->mComment ) . "',{$this->mId})";
89 wfQuery( $sql, DB_WRITE, $fname );
90 }
91 return true;
92 }
93
94 function addEntry( $action, $comment, $textaction = "" )
95 {
96 global $wgLang, $wgUser;
97
98 $comment_esc = wfEscapeWikiText( $comment );
99
100 $this->getContent();
101
102 $ut = $wgUser->getName();
103 $uid = $wgUser->getID();
104 if( $uid ) {
105 $ul = "[[" .
106 $wgLang->getNsText( Namespace::getUser() ) .
107 ":{$ut}|{$ut}]]";
108 } else {
109 $ul = $ut;
110 }
111 $d = $wgLang->timeanddate( wfTimestampNow(), false );
112
113 preg_match( "/^(.*?)<ul>(.*)$/sD", $this->mContent, $m );
114
115 if($textaction)
116 $this->mComment = $textaction;
117 else
118 $this->mComment = $action;
119
120 if ( "" == $comment ) {
121 $inline = "";
122 } else {
123 $inline = " <em>({$comment_esc})</em>";
124 # comment gets escaped again, so we use the unescaped version
125 $this->mComment .= ": {$comment}";
126 }
127 $this->mContent = "{$m[1]}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$m[2]}";
128
129 # TODO: automatic log rotation...
130
131 return $this->saveContent();
132 }
133
134 function replaceContent( $text, $comment = "" )
135 {
136 $this->mContent = $text;
137 $this->mComment = $comment;
138 $this->mTimestamp = wfTimestampNow();
139 return $this->saveContent();
140 }
141
142 function showAsDisabledPage( $rawhtml = true )
143 {
144 global $wgLang, $wgOut;
145 $wgOut->checkLastModified( $this->getTimestamp() );
146 $func = ( $rawhtml ? "addHTML" : "addWikiText" );
147 $wgOut->$func(
148 "<p>" . wfMsg( "perfdisabled" ) . "</p>\n\n" .
149 "<p>" . wfMsg( "perfdisabledsub", $wgLang->timeanddate( $this->getTimestamp() ) ) . "</p>\n\n" .
150 "<hr />\n\n" .
151 $this->getContent()
152 );
153 return;
154
155 }
156 }
157
158 ?>