00001 <?php
00002
# Class to simplify the use of log pages
00003
00004 class LogPage {
00005 var
$mTitle,
$mContent,
$mContentLoaded,
$mId,
$mComment;
00006 var
$mUpdateRecentChanges ;
00007
00008 function
LogPage( $title, $defaulttext =
"<ul>\n</ul>" )
00009 {
00010
# For now, assume title is correct dbkey
00011
# and log pages always go in Wikipedia namespace
00012
$this->mTitle = str_replace(
" ",
"_", $
title );
00013 $this->mId = 0;
00014 $this->mUpdateRecentChanges =
true ;
00015 $this->mContentLoaded =
false;
00016 $this->
getContent( $defaulttext );
00017 }
00018
00019 function
getContent( $defaulttext =
"<ul>\n</ul>" )
00020 {
00021
$sql =
"SELECT cur_id,cur_text,cur_timestamp FROM cur " .
00022
"WHERE cur_namespace=" .
Namespace::getWikipedia() .
" AND " .
00023
"cur_title='" .
wfStrencode($this->mTitle ) .
"'";
00024
$res =
wfQuery( $sql,
DB_READ,
"LogPage::getContent" );
00025
00026
if(
wfNumRows( $res ) > 0 ) {
00027
$s =
wfFetchObject( $res );
00028 $this->mId =
$s->cur_id;
00029 $this->mContent =
$s->cur_text;
00030 $this->mTimestamp =
$s->cur_timestamp;
00031 }
else {
00032 $this->mId = 0;
00033 $this->mContent = $defaulttext;
00034 $this->mTimestamp =
wfTimestampNow();
00035 }
00036 $this->mContentLoaded =
true; # Well, sort of
00037
00038
return $this->mContent;
00039 }
00040
00041 function
getTimestamp()
00042 {
00043
if( !$this->mContentLoaded ) {
00044 $this->getContent();
00045 }
00046
return $this->mTimestamp;
00047 }
00048
00049 function
saveContent()
00050 {
00051
if(
wfReadOnly() )
return;
00052
00053 global
$wgUser;
00054 $fname =
"LogPage::saveContent";
00055 $uid =
$wgUser->getID();
00056 $ut =
wfStrencode( $wgUser->getName() );
00057
00058
if( !$this->mContentLoaded )
return false;
00059 $this->mTimestamp = $now =
wfTimestampNow();
00060 $won =
wfInvertTimestamp( $now );
00061
if($this->mId == 0) {
00062
$sql =
"INSERT INTO cur (cur_timestamp,cur_user,cur_user_text,
00063
cur_namespace,cur_title,cur_text,cur_comment,cur_restrictions,
00064
inverse_timestamp,cur_touched)
00065
VALUES ('{$now}', {$uid}, '{$ut}', " .
00066
Namespace::getWikipedia() .
", '" .
00067
wfStrencode( $this->mTitle ) .
"', '" .
00068
wfStrencode( $this->mContent ) .
"', '" .
00069
wfStrencode( $this->mComment ) .
"', 'sysop', '{$won}','{$now}')";
00070
wfQuery( $sql,
DB_WRITE, $fname );
00071 $this->mId =
wfInsertId();
00072 }
else {
00073
$sql =
"UPDATE cur SET cur_timestamp='{$now}', " .
00074
"cur_user={$uid}, cur_user_text='{$ut}', " .
00075
"cur_text='" .
wfStrencode( $this->mContent ) .
"', " .
00076
"cur_comment='" .
wfStrencode( $this->mComment ) .
"', " .
00077
"cur_restrictions='sysop', inverse_timestamp='{$won}', cur_touched='{$now}' " .
00078
"WHERE cur_id={$this->mId}";
00079
wfQuery( $sql,
DB_WRITE, $fname );
00080 }
00081
00082
# And update recentchanges
00083
if ( $this->mUpdateRecentChanges ) {
00084 $titleObj = Title::makeTitle( Namespace::getWikipedia(), $this->mTitle );
00085 RecentChange::notifyLog( $now, $titleObj, $wgUser, $this->mComment );
00086 }
00087
return true;
00088 }
00089
00090 function
addEntry( $action, $comment, $textaction =
"" )
00091 {
00092 global
$wgLang,
$wgUser;
00093
00094 $comment_esc =
wfEscapeWikiText( $comment );
00095
00096 $this->getContent();
00097
00098 $ut =
$wgUser->getName();
00099 $uid =
$wgUser->getID();
00100
if( $uid ) {
00101 $ul =
"[[" .
00102
$wgLang->getNsText( Namespace::getUser() ) .
00103
":{$ut}|{$ut}]]";
00104 }
else {
00105 $ul = $ut;
00106 }
00107
$d =
$wgLang->timeanddate(
wfTimestampNow(),
false );
00108
00109
if( preg_match(
"/^(.*?)<ul>(.*)$/sD", $this->mContent, $m ) ) {
00110 $before = $m[1];
00111 $after = $m[2];
00112 }
else {
00113 $before =
"";
00114 $after =
"";
00115 }
00116
00117
if($textaction)
00118 $this->mComment = $textaction;
00119
else
00120 $this->mComment =
$action;
00121
00122
if (
"" ==
$comment ) {
00123 $inline =
"";
00124 }
else {
00125 $inline =
" <em>({$comment_esc})</em>";
00126
# comment gets escaped again, so we use the unescaped version
00127
$this->mComment .=
": {$comment}";
00128 }
00129 $this->mContent =
"{$before}<ul><li>{$d} {$ul} {$action}{$inline}</li>\n{$after}";
00130
00131
# TODO: automatic log rotation...
00132
00133
return $this->
saveContent();
00134 }
00135
00136 function
replaceContent( $text, $comment =
"" )
00137 {
00138 $this->mContent = $text;
00139 $this->mComment =
$comment;
00140 $this->mTimestamp =
wfTimestampNow();
00141
return $this->
saveContent();
00142 }
00143
00144 function
showAsDisabledPage( $rawhtml =
true )
00145 {
00146 global
$wgLang,
$wgOut;
00147
if(
$wgOut->checkLastModified( $this->getTimestamp() ) ){
00148
# Client cache fresh and headers sent, nothing more to do.
00149
return;
00150 }
00151 $func = ( $rawhtml ?
"addHTML" :
"addWikiText" );
00152
$wgOut->$func(
00153
"<p>" .
wfMsg(
"perfdisabled" ) .
"</p>\n\n" .
00154
"<p>" .
wfMsg(
"perfdisabledsub", $wgLang->timeanddate( $this->getTimestamp() ) ) .
"</p>\n\n" .
00155
"<hr />\n\n" .
00156 $this->getContent()
00157 );
00158
return;
00159
00160 }
00161 }
00162
00163 ?>