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