Forgot to commit the change to showhideminor. Also removing commented-out strings.
[lhc/web/wiklou.git] / includes / LogPage.php
1 <?php
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 $fname = 'LogPage::getContent';
22
23 $dbw =& wfGetDB( DB_MASTER );
24 $s = $dbw->getArray( 'cur',
25 array( 'cur_id','cur_text','cur_timestamp' ),
26 array( 'cur_namespace' => Namespace::getWikipedia(), 'cur_title' => $this->mTitle ),
27 $fname, 'FOR UPDATE'
28 );
29
30 if( $s !== false ) {
31 $this->mId = $s->cur_id;
32 $this->mContent = $s->cur_text;
33 $this->mTimestamp = $s->cur_timestamp;
34 } else {
35 $this->mId = 0;
36 $this->mContent = $defaulttext;
37 $this->mTimestamp = wfTimestampNow();
38 }
39 $this->mContentLoaded = true; # Well, sort of
40
41 return $this->mContent;
42 }
43
44 function getTimestamp()
45 {
46 if( !$this->mContentLoaded ) {
47 $this->getContent();
48 }
49 return $this->mTimestamp;
50 }
51
52 function saveContent()
53 {
54 if( wfReadOnly() ) return;
55
56 global $wgUser;
57 $fname = "LogPage::saveContent";
58
59 $dbw =& wfGetDB( DB_MASTER );
60 $uid = $wgUser->getID();
61
62 if( !$this->mContentLoaded ) return false;
63 $this->mTimestamp = $now = wfTimestampNow();
64 $won = wfInvertTimestamp( $now );
65 if($this->mId == 0) {
66 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
67
68 # Note: this query will deadlock if another thread has called getContent(),
69 # at least in MySQL 4.0.17 InnoDB
70 $dbw->insertArray( 'cur',
71 array(
72 'cur_id' => $seqVal,
73 'cur_timestamp' => $now,
74 'cur_user' => $uid,
75 'cur_user_text' => $wgUser->getName(),
76 'cur_namespace' => NS_WIKIPEDIA,
77 'cur_title' => $this->mTitle,
78 'cur_text' => $this->mContent,
79 'cur_comment' => $this->mComment,
80 'cur_restrictions' => 'sysop',
81 'inverse_timestamp' => $won,
82 'cur_touched' => $now,
83 ), $fname
84 );
85 $this->mId = $dbw->insertId();
86 } else {
87 $dbw->updateArray( 'cur',
88 array( /* SET */
89 'cur_timestamp' => $now,
90 'cur_user' => $uid,
91 'cur_user_text' => $wgUser->getName(),
92 'cur_text' => $this->mContent,
93 'cur_comment' => $this->mComment,
94 'cur_restrictions' => 'sysop',
95 'inverse_timestamp' => $won,
96 'cur_touched' => $now,
97 ), array( /* WHERE */
98 'cur_id' => $this->mId
99 ), $fname
100 );
101 }
102
103 # And update recentchanges
104 if ( $this->mUpdateRecentChanges ) {
105 $titleObj = Title::makeTitle( Namespace::getWikipedia(), $this->mTitle );
106 RecentChange::notifyLog( $now, $titleObj, $wgUser, $this->mComment );
107 }
108 return true;
109 }
110
111 function addEntry( $action, $comment, $textaction = "" )
112 {
113 global $wgLang, $wgUser;
114
115 $comment_esc = wfEscapeWikiText( $comment );
116
117 $this->getContent();
118
119 $ut = $wgUser->getName();
120 $uid = $wgUser->getID();
121 if( $uid ) {
122 $ul = "[[" .
123 $wgLang->getNsText( Namespace::getUser() ) .
124 ":{$ut}|{$ut}]]";
125 } else {
126 $ul = $ut;
127 }
128
129 # Use the wiki-wide default date format instead of the user's setting
130 $d = $wgLang->timeanddate( wfTimestampNow(), false, MW_DATE_DEFAULT );
131
132 if( preg_match( "/^(.*?)<ul>(.*)$/sD", $this->mContent, $m ) ) {
133 $before = $m[1];
134 $after = $m[2];
135 } else {
136 $before = "";
137 $after = "";
138 }
139
140 if($textaction)
141 $this->mComment = $textaction;
142 else
143 $this->mComment = $action;
144
145 if ( "" == $comment ) {
146 $inline = "";
147 } else {
148 $inline = " <em>({$comment_esc})</em>";
149 # comment gets escaped again, so we use the unescaped version
150 $this->mComment .= ": {$comment}";
151 }
152 $this->mContent = "{$before}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$after}";
153
154 # TODO: automatic log rotation...
155
156 return $this->saveContent();
157 }
158
159 function replaceContent( $text, $comment = "" )
160 {
161 $this->mContent = $text;
162 $this->mComment = $comment;
163 $this->mTimestamp = wfTimestampNow();
164 return $this->saveContent();
165 }
166
167 function showAsDisabledPage( $rawhtml = true )
168 {
169 global $wgLang, $wgOut;
170 if( $wgOut->checkLastModified( $this->getTimestamp() ) ){
171 # Client cache fresh and headers sent, nothing more to do.
172 return;
173 }
174 $func = ( $rawhtml ? "addHTML" : "addWikiText" );
175 $wgOut->$func(
176 "<p>" . wfMsg( "perfdisabled" ) . "</p>\n\n" .
177 "<p>" . wfMsg( "perfdisabledsub", $wgLang->timeanddate( $this->getTimestamp() ) ) . "</p>\n\n" .
178 "<hr />\n\n" .
179 $this->getContent()
180 );
181 return;
182
183 }
184 }
185
186 ?>