* Moved content from liveCmdLine.inc into commandLine.inc, obsoleting the former.
[mediawiki.git] / includes / LogPage.php
blob6be15fed9bd020173d8020a6e28facbb168831c7
1 <?php
2 # Class to simplify the use of log pages
4 class LogPage {
5 /* private */ var $mTitle, $mContent, $mContentLoaded, $mId, $mComment;
6 var $mUpdateRecentChanges ;
8 function LogPage( $title, $defaulttext = "<ul>\n</ul>" )
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 );
19 function getContent( $defaulttext = "<ul>\n</ul>" )
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" );
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();
36 $this->mContentLoaded = true; # Well, sort of
38 return $this->mContent;
41 function getTimestamp()
43 if( !$this->mContentLoaded ) {
44 $this->getContent();
46 return $this->mTimestamp;
49 function saveContent()
51 if( wfReadOnly() ) return;
53 global $wgUser;
54 $fname = "LogPage::saveContent";
55 $uid = $wgUser->getID();
56 $ut = wfStrencode( $wgUser->getName() );
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 );
82 # And update recentchanges
83 if ( $this->mUpdateRecentChanges ) {
84 $titleObj = Title::makeTitle( Namespace::getWikipedia(), $this->mTitle );
85 RecentChange::notifyLog( $now, $titleObj, $wgUser, $this->mComment );
87 return true;
90 function addEntry( $action, $comment, $textaction = "" )
92 global $wgLang, $wgUser;
94 $comment_esc = wfEscapeWikiText( $comment );
96 $this->getContent();
98 $ut = $wgUser->getName();
99 $uid = $wgUser->getID();
100 if( $uid ) {
101 $ul = "[[" .
102 $wgLang->getNsText( Namespace::getUser() ) .
103 ":{$ut}|{$ut}]]";
104 } else {
105 $ul = $ut;
107 $d = $wgLang->timeanddate( wfTimestampNow(), false );
109 if( preg_match( "/^(.*?)<ul>(.*)$/sD", $this->mContent, $m ) ) {
110 $before = $m[1];
111 $after = $m[2];
112 } else {
113 $before = "";
114 $after = "";
117 if($textaction)
118 $this->mComment = $textaction;
119 else
120 $this->mComment = $action;
122 if ( "" == $comment ) {
123 $inline = "";
124 } else {
125 $inline = " <em>({$comment_esc})</em>";
126 # comment gets escaped again, so we use the unescaped version
127 $this->mComment .= ": {$comment}";
129 $this->mContent = "{$before}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$after}";
131 # TODO: automatic log rotation...
133 return $this->saveContent();
136 function replaceContent( $text, $comment = "" )
138 $this->mContent = $text;
139 $this->mComment = $comment;
140 $this->mTimestamp = wfTimestampNow();
141 return $this->saveContent();
144 function showAsDisabledPage( $rawhtml = true )
146 global $wgLang, $wgOut;
147 if( $wgOut->checkLastModified( $this->getTimestamp() ) ){
148 # Client cache fresh and headers sent, nothing more to do.
149 return;
151 $func = ( $rawhtml ? "addHTML" : "addWikiText" );
152 $wgOut->$func(
153 "<p>" . wfMsg( "perfdisabled" ) . "</p>\n\n" .
154 "<p>" . wfMsg( "perfdisabledsub", $wgLang->timeanddate( $this->getTimestamp() ) ) . "</p>\n\n" .
155 "<hr />\n\n" .
156 $this->getContent()
158 return;