New global config setting $wgMaxTocLevel: Maximum indent level of toc.
[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 $d = $wgLang->timeanddate( wfTimestampNow(), false );
129
130 if( preg_match( "/^(.*?)<ul>(.*)$/sD", $this->mContent, $m ) ) {
131 $before = $m[1];
132 $after = $m[2];
133 } else {
134 $before = "";
135 $after = "";
136 }
137
138 if($textaction)
139 $this->mComment = $textaction;
140 else
141 $this->mComment = $action;
142
143 if ( "" == $comment ) {
144 $inline = "";
145 } else {
146 $inline = " <em>({$comment_esc})</em>";
147 # comment gets escaped again, so we use the unescaped version
148 $this->mComment .= ": {$comment}";
149 }
150 $this->mContent = "{$before}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$after}";
151
152 # TODO: automatic log rotation...
153
154 return $this->saveContent();
155 }
156
157 function replaceContent( $text, $comment = "" )
158 {
159 $this->mContent = $text;
160 $this->mComment = $comment;
161 $this->mTimestamp = wfTimestampNow();
162 return $this->saveContent();
163 }
164
165 function showAsDisabledPage( $rawhtml = true )
166 {
167 global $wgLang, $wgOut;
168 if( $wgOut->checkLastModified( $this->getTimestamp() ) ){
169 # Client cache fresh and headers sent, nothing more to do.
170 return;
171 }
172 $func = ( $rawhtml ? "addHTML" : "addWikiText" );
173 $wgOut->$func(
174 "<p>" . wfMsg( "perfdisabled" ) . "</p>\n\n" .
175 "<p>" . wfMsg( "perfdisabledsub", $wgLang->timeanddate( $this->getTimestamp() ) ) . "</p>\n\n" .
176 "<hr />\n\n" .
177 $this->getContent()
178 );
179 return;
180
181 }
182 }
183
184 ?>