Initial revision
[lhc/web/wiklou.git] / includes / LogPage.php
1 <?
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 = $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 $sql = "SELECT cur_id,cur_text FROM cur " .
22 "WHERE cur_namespace=" . Namespace::getWikipedia() . " AND " .
23 "cur_title='" . wfStrencode($this->mTitle ) . "'";
24 $res = wfQuery( $sql, "LogPage::getContent" );
25
26 if( wfNumRows( $res ) > 0 ) {
27 $s = wfFetchObject( $res );
28 $this->mId = $s->cur_id;
29 $this->mContent = $s->cur_text;
30 } else {
31 $this->mId = 0;
32 $this->mContent = $defaulttext;
33 }
34 $this->mContentLoaded = true; # Well, sort of
35
36 return $this->mContent;
37 }
38
39 function saveContent()
40 {
41 global $wgUser;
42 $fname = "LogPage::saveContent";
43 $uid = $wgUser->getID();
44 $ut = wfStrencode( $wgUser->getName() );
45
46 if( !$this->mContentLoaded ) return false;
47 $now = date( "YmdHis" );
48 $won = wfInvertTimestamp( $now );
49 if($this->mId == 0) {
50 $sql = "INSERT INTO cur (cur_timestamp,cur_user,cur_user_text,
51 cur_namespace,cur_title,cur_text,cur_comment,cur_restrictions,inverse_timestamp)
52 VALUES ('{$now}', {$uid}, '{$ut}', " .
53 Namespace::getWikipedia() . ", '" .
54 wfStrencode( $this->mTitle ) . "', '" .
55 wfStrencode( $this->mContent ) . "', '" .
56 wfStrencode( $this->mComment ) . "', 'sysop', '{$won}')";
57 wfQuery( $sql, $fname );
58 $this->mId = wfInsertId();
59 } else {
60 $sql = "UPDATE cur SET cur_timestamp='{$now}', " .
61 "cur_user={$uid}, cur_user_text='{$ut}', " .
62 "cur_text='" . wfStrencode( $this->mContent ) . "', " .
63 "cur_comment='" . wfStrencode( $this->mComment ) . "', " .
64 "cur_restrictions='sysop', inverse_timestamp='{$won}' " .
65 "WHERE cur_id={$this->mId}";
66 wfQuery( $sql, $fname );
67 }
68
69 # And update recentchanges
70 if ( $this->mUpdateRecentChanges ) {
71 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time,
72 rc_user,rc_user_text,rc_namespace,rc_title,rc_comment,
73 rc_cur_id) VALUES ('{$now}','{$now}',{$uid},'{$ut}',4,'" .
74 wfStrencode( $this->mTitle ) . "','" .
75 wfStrencode( $this->mComment ) . "',{$this->mId})";
76 wfQuery( $sql, $fname );
77 }
78 return true;
79 }
80
81 function addEntry( $action, $comment, $textaction = "" )
82 {
83 global $wgLang, $wgUser;
84 $this->getContent();
85
86 $ut = $wgUser->getName();
87 $uid = $wgUser->getID();
88 if( $uid ) {
89 $ul = "[[" .
90 $wgLang->getNsText( Namespace::getUser() ) .
91 ":{$ut}|{$ut}]]";
92 } else {
93 $ul = $ut;
94 }
95 $d = $wgLang->timeanddate( date( "YmdHis" ), false );
96
97 preg_match( "/^(.*?)<ul>(.*)$/sD", $this->mContent, $m );
98
99 if($textaction)
100 $this->mComment = $textaction;
101 else
102 $this->mComment = $action;
103
104 if ( "" == $comment ) {
105 $inline = "";
106 } else {
107 $inline = " <em>({$comment})</em>";
108 $this->mComment .= ": {$comment}";
109 }
110 $this->mContent = "{$m[1]}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$m[2]}";
111
112 # TODO: automatic log rotation...
113
114 return $this->saveContent();
115 }
116 }
117
118 ?>