Index: trunk/extensions/LiquidThreads/classes/LqtThreads.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | | - |
4 | 3 | if (!defined('MEDIAWIKI')) die; |
5 | 4 | |
| 5 | +/** Module of factory methods. */ |
6 | 6 | class Threads { |
7 | 7 | |
8 | 8 | const TYPE_NORMAL = 0; |
— | — | @@ -33,7 +33,7 @@ |
34 | 34 | // SCHEMA changes must be reflected here. |
35 | 35 | // TODO: It's dumb that the commitRevision code isn't used here. |
36 | 36 | |
37 | | - $dbr =& wfGetDB( DB_MASTER ); |
| 37 | + $dbw =& wfGetDB( DB_MASTER ); |
38 | 38 | |
39 | 39 | if ( !in_array($type, self::$VALID_TYPES) ) { |
40 | 40 | throw new MWException(__METHOD__ . ": invalid type $type."); |
— | — | @@ -48,9 +48,11 @@ |
49 | 49 | global $wgUser; // TODO global. |
50 | 50 | |
51 | 51 | $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(), |
55 | 57 | 'thread_parent' => $superthread ? $superthread->id() : null, |
56 | 58 | 'thread_article_namespace' => $article->getTitle()->getNamespace(), |
57 | 59 | 'thread_article_title' => $article->getTitle()->getDBkey(), |
— | — | @@ -61,26 +63,42 @@ |
62 | 64 | 'thread_change_user' => $wgUser->getID(), |
63 | 65 | 'thread_change_user_text' => $wgUser->getName(), |
64 | 66 | 'thread_type' => $type, |
65 | | - 'thread_editedness' => self::EDITED_NEVER), |
66 | | - __METHOD__); |
67 | | - |
68 | | - $newid = $dbr->insertId(); |
69 | | - |
| 67 | + 'thread_editedness' => self::EDITED_NEVER); |
| 68 | + |
70 | 69 | 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; |
73 | 72 | } else { |
74 | | - $ancestor = $newid; |
75 | | - $change_object_clause = 'thread_change_object = null'; |
| 73 | + $row['thread_change_object'] = null; |
76 | 74 | } |
77 | | - $res = $dbr->update( 'thread', |
78 | | - /* SET */ array( 'thread_ancestor' => $ancestor, |
79 | | - $change_object_clause ), |
80 | | - /* WHERE */ array( 'thread_id' => $newid, ), |
81 | | - __METHOD__); |
82 | 75 | |
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 | + |
85 | 103 | if($superthread) { |
86 | 104 | $superthread->addReply( $newthread ); |
87 | 105 | } |
— | — | @@ -111,7 +129,8 @@ |
112 | 130 | } |
113 | 131 | } |
114 | 132 | |
115 | | - static function where( $where, $options = array(), $extra_tables = array(), $joins = "" ) { |
| 133 | + static function where( $where, $options = array(), $extra_tables = array(), |
| 134 | + $joins = "" ) { |
116 | 135 | global $wgDBprefix; |
117 | 136 | $dbr = wfGetDB( DB_SLAVE ); |
118 | 137 | if ( is_array($where) ) $where = $dbr->makeList( $where, LIST_AND ); |
— | — | @@ -196,8 +215,7 @@ |
197 | 216 | |
198 | 217 | private static function databaseError( $msg ) { |
199 | 218 | // TODO tie into MW's error reporting facilities. |
200 | | - echo("Corrupt liquidthreads database: $msg"); |
201 | | - die(); |
| 219 | + throw new MWException("Corrupt liquidthreads database: $msg"); |
202 | 220 | } |
203 | 221 | |
204 | 222 | private static function assertSingularity( $threads, $attribute, $value ) { |
— | — | @@ -271,4 +289,4 @@ |
272 | 290 | return 'thread.thread_parent is null'; |
273 | 291 | } |
274 | 292 | |
275 | | -} |
| 293 | +} |
\ No newline at end of file |