removing call to the database for "recentchangestext". That's automaticly get by...
[mediawiki.git] / includes / LogPage.php
blobdbae362d2778fdab177d8373fe269eb2e78e6866
1 <?php
2 #$Id$
4 # Class to simplify the use of log pages
6 class LogPage {
7 /* private */ var $mTitle, $mContent, $mContentLoaded, $mId, $mComment;
8 var $mUpdateRecentChanges ;
10 function LogPage( $title, $defaulttext = "<ul>\n</ul>" )
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 );
21 function getContent( $defaulttext = "<ul>\n</ul>" )
23 $fname = 'LogPage::getContent';
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'
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);
41 $this->mContentLoaded = true; # Well, sort of
43 return $this->mContent;
46 function getTimestamp()
48 if( !$this->mContentLoaded ) {
49 $this->getContent();
51 return $this->mTimestamp;
54 function saveContent()
56 if( wfReadOnly() ) return;
58 global $wgUser;
59 $fname = "LogPage::saveContent";
61 $dbw =& wfGetDB( DB_MASTER );
62 $uid = $wgUser->getID();
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' );
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
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
105 # And update recentchanges
106 if ( $this->mUpdateRecentChanges ) {
107 $titleObj = Title::makeTitle( Namespace::getWikipedia(), $this->mTitle );
108 RecentChange::notifyLog( $now, $titleObj, $wgUser, $this->mComment );
110 return true;
113 function addEntry( $action, $comment, $textaction = "" )
115 global $wgLang, $wgUser;
117 $comment_esc = wfEscapeWikiText( $comment );
119 $this->getContent();
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;
131 # Use the wiki-wide default date format instead of the user's setting
132 $d = $wgLang->timeanddate( wfTimestampNow(), false, MW_DATE_DEFAULT );
134 if( preg_match( "/^(.*?)<ul>(.*)$/sD", $this->mContent, $m ) ) {
135 $before = $m[1];
136 $after = $m[2];
137 } else {
138 $before = "";
139 $after = "";
142 if($textaction)
143 $this->mComment = $textaction;
144 else
145 $this->mComment = $action;
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}";
154 $this->mContent = "{$before}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$after}";
156 # TODO: automatic log rotation...
158 return $this->saveContent();
161 function replaceContent( $text, $comment = "" )
163 $this->mContent = $text;
164 $this->mComment = $comment;
165 $this->mTimestamp = wfTimestampNow();
166 return $this->saveContent();
169 function showAsDisabledPage( $rawhtml = true )
171 global $wgLang, $wgOut;
172 if( $wgOut->checkLastModified( $this->getTimestamp() ) ){
173 # Client cache fresh and headers sent, nothing more to do.
174 return;
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()
183 return;