r23462 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23461‎ | r23462 | r23463 >
Date:08:33, 27 June 2007
Author:robchurch
Status:old
Tags:
Comment:
Lots of LogPage cleanup
Modified paths:
  • /branches/robchurch/logs/includes/LogPage.php (modified) (history)

Diff [purge]

Index: branches/robchurch/logs/includes/LogPage.php
@@ -30,69 +30,29 @@
3131 *
3232 */
3333 class LogPage {
34 - /* @access private */
35 - var $type, $action, $comment, $params, $target;
36 - /* @acess public */
37 - var $updateRecentChanges;
3834
3935 /**
 36+ * Log type
 37+ */
 38+ private $type = '';
 39+
 40+ /**
 41+ * Should we update recent changes when adding a
 42+ * new log item?
 43+ */
 44+ private $updateRecentChanges = true;
 45+
 46+ /**
4047 * Constructor
4148 *
42 - * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
43 - * 'upload', 'move'
44 - * @param bool $rc Whether to update recent changes as well as the logging table
 49+ * @param string $type Log type
 50+ * @param bool $rc Update recent changes?
4551 */
46 - function __construct( $type, $rc = true ) {
 52+ public function __construct( $type, $rc = true ) {
4753 $this->type = $type;
4854 $this->updateRecentChanges = $rc;
4955 }
5056
51 - function saveContent() {
52 - if( wfReadOnly() ) return false;
53 -
54 - global $wgUser;
55 - $fname = 'LogPage::saveContent';
56 -
57 - $dbw = wfGetDB( DB_MASTER );
58 - $uid = $wgUser->getID();
59 - $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
60 -
61 - $this->timestamp = $now = wfTimestampNow();
62 - $data = array(
63 - 'log_type' => $this->type,
64 - 'log_action' => $this->action,
65 - 'log_timestamp' => $dbw->timestamp( $now ),
66 - 'log_user' => $uid,
67 - 'log_namespace' => $this->target->getNamespace(),
68 - 'log_title' => $this->target->getDBkey(),
69 - 'log_comment' => $this->comment,
70 - 'log_params' => $this->params,
71 - );
72 -
73 - # log_id doesn't exist on Wikimedia servers yet, and it's a tricky
74 - # schema update to do. Hack it for now to ignore the field on MySQL.
75 - if ( !is_null( $log_id ) ) {
76 - $data['log_id'] = $log_id;
77 - }
78 - $dbw->insert( 'logging', $data, $fname );
79 -
80 - # Update recent changes
81 - if( $this->updateRecentChanges ) {
82 - RecentChange::notifyLog(
83 - $now,
84 - $this->target,
85 - $wgUser,
86 - $this->comment,
87 - '',
88 - $this->type,
89 - $this->action,
90 - $this->params
91 - );
92 - }
93 -
94 - return true;
95 - }
96 -
9757 /**
9858 * @static
9959 */
@@ -105,7 +65,7 @@
10666 * @static
10767 */
10868 public static function isLogType( $type ) {
109 - return in_array( $type, LogPage::validTypes() );
 69+ return in_array( $type, self::validTypes() );
11070 }
11171
11272 /**
@@ -214,81 +174,88 @@
215175 }
216176
217177 /**
218 - * Add a log entry
219 - * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
220 - * @param object &$target A title object.
221 - * @param string $comment Description associated
222 - * @param array $params Parameters passed later to wfMsg.* functions
 178+ * Insert a new log row, updating recent changes if
 179+ * we've been asked to do so
 180+ *
 181+ * @param string $action Log action
 182+ * @param Title $target Associated title object
 183+ * @param string $comment Log comment/rationale
 184+ * @param array $params Log parameters
223185 */
224 - function addEntry( $action, $target, $comment, $params = array() ) {
225 - if ( !is_array( $params ) ) {
226 - $params = array( $params );
 186+ public function addEntry( $action, $target, $comment, $params = array() ) {
 187+ global $wgUser;
 188+ wfProfileIn( __METHOD__ );
 189+
 190+ if( wfReadOnly() ) {
 191+ wfProfileOut( __METHOD__ );
 192+ return false;
227193 }
228194
229 - $this->action = $action;
230 - $this->target = $target;
231 - $this->comment = $comment;
232 - $this->params = LogPage::makeParamBlob( $params );
 195+ if( !is_array( $params ) )
 196+ $params = array( $params );
233197
234 - #$this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
 198+ $dbw = wfGetDB( DB_MASTER );
 199+ $now = wfTimestampNow();
 200+ $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
235201
236 - return $this->saveContent();
237 - }
 202+ $data = array(
 203+ 'log_type' => $this->type,
 204+ 'log_action' => $action,
 205+ 'log_timestamp' => $dbw->timestamp( $now ),
 206+ 'log_user' => $wgUser->getId(),
 207+ 'log_namespace' => $target->getNamespace(),
 208+ 'log_title' => $target->getDBkey(),
 209+ 'log_comment' => $comment,
 210+ 'log_params' => self::makeParamBlob( $params ),
 211+ );
238212
239 - /**
240 - * Create a blob from a parameter array
241 - * @static
242 - */
243 - static function makeParamBlob( $params ) {
244 - return implode( "\n", $params );
245 - }
 213+ # log_id doesn't exist on Wikimedia servers yet, and it's a tricky
 214+ # schema update to do. Hack it for now to ignore the field on MySQL.
 215+ if ( !is_null( $log_id ) ) {
 216+ $data['log_id'] = $log_id;
 217+ }
 218+ $dbw->insert( 'logging', $data, __METHOD__ );
246219
247 - /**
248 - * Extract a parameter array from a blob
249 - * @static
250 - */
251 - static function extractParams( $blob ) {
252 - if ( $blob === '' ) {
253 - return array();
254 - } else {
255 - return explode( "\n", $blob );
 220+ # Update recent changes if required
 221+ if( $this->updateRecentChanges ) {
 222+ RecentChange::notifyLog(
 223+ $now,
 224+ $target,
 225+ $wgUser,
 226+ $comment,
 227+ '',
 228+ $this->type,
 229+ $action,
 230+ $params
 231+ );
256232 }
 233+
 234+ wfProfileOut( __METHOD__ );
 235+ return true;
257236 }
258 -
 237+
259238 /**
260 - * Convert a comma-delimited list of block log flags
261 - * into a more readable (and translated) form
 239+ * Encode parameters into a BLOB-safe value
262240 *
263 - * @param $flags Flags to format
 241+ * @param array $params Parameters
264242 * @return string
265243 */
266 - public static function formatBlockFlags( $flags ) {
267 - $flags = explode( ',', trim( $flags ) );
268 - if( count( $flags ) > 0 ) {
269 - for( $i = 0; $i < count( $flags ); $i++ )
270 - $flags[$i] = self::formatBlockFlag( $flags[$i] );
271 - return '(' . implode( ', ', $flags ) . ')';
272 - } else {
273 - return '';
274 - }
 244+ public static function makeParamBlob( $params ) {
 245+ return implode( "\n", $params );
275246 }
276 -
 247+
277248 /**
278 - * Translate a block log flag if possible
 249+ * Decode parameters from a BLOB value
279250 *
280 - * @param $flag Flag to translate
281 - * @return string
 251+ * @param string $blob Parameter blob
 252+ * @return array
282253 */
283 - public static function formatBlockFlag( $flag ) {
284 - static $messages = array();
285 - if( !isset( $messages[$flag] ) ) {
286 - $k = 'block-log-flags-' . $flag;
287 - $msg = wfMsg( $k );
288 - $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
289 - }
290 - return $messages[$flag];
 254+ public static function extractParams( $blob ) {
 255+ return $blob === ''
 256+ ? array()
 257+ : explode( "\n", $blob );
291258 }
292259
293260 }
294261
295 -?>
 262+?>
\ No newline at end of file

Status & tagging log