r47244 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47243‎ | r47244 | r47245 >
Date:23:51, 13 February 2009
Author:werdna
Status:deferred
Tags:
Comment:
LiquidThreads code quality:
* Line breaking, use dbw for master and dbr for slave.
* Make it work with multi-server setups -- it SELECTs a row right after INSERTing it, before even ending the transaction, from a slave.
Modified paths:
  • /trunk/extensions/LiquidThreads/classes/LqtThreads.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LiquidThreads/classes/LqtThreads.php
@@ -1,7 +1,7 @@
22 <?php
3 -
43 if (!defined('MEDIAWIKI')) die;
54
 5+/** Module of factory methods. */
66 class Threads {
77
88 const TYPE_NORMAL = 0;
@@ -33,7 +33,7 @@
3434 // SCHEMA changes must be reflected here.
3535 // TODO: It's dumb that the commitRevision code isn't used here.
3636
37 - $dbr =& wfGetDB( DB_MASTER );
 37+ $dbw =& wfGetDB( DB_MASTER );
3838
3939 if ( !in_array($type, self::$VALID_TYPES) ) {
4040 throw new MWException(__METHOD__ . ": invalid type $type.");
@@ -48,9 +48,11 @@
4949 global $wgUser; // TODO global.
5050
5151 $timestamp = wfTimestampNow();
52 -
53 - $res = $dbr->insert('thread',
54 - array('thread_root' => $root->getID(),
 52+
 53+ // TODO PG support
 54+ $newid = $dbw->nextSequenceValue( 'thread_thread_id' );
 55+
 56+ $row = array('thread_root' => $root->getID(),
5557 'thread_parent' => $superthread ? $superthread->id() : null,
5658 'thread_article_namespace' => $article->getTitle()->getNamespace(),
5759 'thread_article_title' => $article->getTitle()->getDBkey(),
@@ -61,26 +63,42 @@
6264 'thread_change_user' => $wgUser->getID(),
6365 'thread_change_user_text' => $wgUser->getName(),
6466 'thread_type' => $type,
65 - 'thread_editedness' => self::EDITED_NEVER),
66 - __METHOD__);
67 -
68 - $newid = $dbr->insertId();
69 -
 67+ 'thread_editedness' => self::EDITED_NEVER);
 68+
7069 if( $superthread ) {
71 - $ancestor = $superthread->ancestorId();
72 - $change_object_clause = 'thread_change_object = ' . $newid;
 70+ $row['thread_ancestor'] = $superthread->ancestorId();
 71+ $row['thread_change_object'] = $newid;
7372 } else {
74 - $ancestor = $newid;
75 - $change_object_clause = 'thread_change_object = null';
 73+ $row['thread_change_object'] = null;
7674 }
77 - $res = $dbr->update( 'thread',
78 - /* SET */ array( 'thread_ancestor' => $ancestor,
79 - $change_object_clause ),
80 - /* WHERE */ array( 'thread_id' => $newid, ),
81 - __METHOD__);
8275
83 - // TODO we could avoid a query here.
84 - $newthread = Threads::withId($newid);
 76+ $res = $dbw->insert('thread', $row, __METHOD__);
 77+
 78+ $newid = $dbw->insertId();
 79+
 80+ $row['thread_id'] = $newid;
 81+
 82+ // Ew, we have to do a SECOND update
 83+ if ( $superthread ) {
 84+ $row['thread_change_object'] = $newid;
 85+ $dbw->update( 'thread',
 86+ array( 'thread_change_object' => $newid ),
 87+ array( 'thread_id' => $newid ),
 88+ __METHOD__ );
 89+ }
 90+
 91+ // Sigh, convert row to an object
 92+ $rowObj = new stdClass();
 93+ foreach( $row as $key => $value ) {
 94+ $rowObj->$key = $value;
 95+ }
 96+
 97+ // We just created the thread, it won't have any children.
 98+ $newthread = new Thread( $rowObj, array() );
 99+
 100+ if (!$newthread)
 101+ throw new MWException( "No new thread with ID $newid\n" );
 102+
85103 if($superthread) {
86104 $superthread->addReply( $newthread );
87105 }
@@ -111,7 +129,8 @@
112130 }
113131 }
114132
115 - static function where( $where, $options = array(), $extra_tables = array(), $joins = "" ) {
 133+ static function where( $where, $options = array(), $extra_tables = array(),
 134+ $joins = "" ) {
116135 global $wgDBprefix;
117136 $dbr = wfGetDB( DB_SLAVE );
118137 if ( is_array($where) ) $where = $dbr->makeList( $where, LIST_AND );
@@ -196,8 +215,7 @@
197216
198217 private static function databaseError( $msg ) {
199218 // TODO tie into MW's error reporting facilities.
200 - echo("Corrupt liquidthreads database: $msg");
201 - die();
 219+ throw new MWException("Corrupt liquidthreads database: $msg");
202220 }
203221
204222 private static function assertSingularity( $threads, $attribute, $value ) {
@@ -271,4 +289,4 @@
272290 return 'thread.thread_parent is null';
273291 }
274292
275 -}
 293+}
\ No newline at end of file

Status & tagging log