Index: trunk/extensions/LiquidThreads/api/ApiQueryLQTThreads.php |
— | — | @@ -1,20 +1,20 @@ |
2 | 2 | <?php |
3 | | - |
4 | | -// LiquidThreads API Query module |
5 | | - |
6 | | -// Data that can be returned: |
7 | | -// - ID |
8 | | -// - Subject |
9 | | -// - "host page" |
10 | | -// - parent |
11 | | -// - ancestor |
12 | | -// - creation time |
13 | | -// - modification time |
14 | | -// - author |
15 | | -// - summary article ID |
16 | | -// - "root" page ID |
17 | | -// - type |
18 | | - |
| 3 | +/** |
| 4 | + * LiquidThreads API Query module |
| 5 | + * |
| 6 | + * Data that can be returned: |
| 7 | + * - ID |
| 8 | + * - Subject |
| 9 | + * - "host page" |
| 10 | + * - parent |
| 11 | + * - ancestor |
| 12 | + * - creation time |
| 13 | + * - modification time |
| 14 | + * - author |
| 15 | + * - summary article ID |
| 16 | + * - "root" page ID |
| 17 | + * - type |
| 18 | + */ |
19 | 19 | class ApiQueryLQTThreads extends ApiQueryBase { |
20 | 20 | |
21 | 21 | // Property definitions |
— | — | @@ -22,153 +22,135 @@ |
23 | 23 | 'id' => 'thread_id', |
24 | 24 | 'subject' => 'thread_subject', |
25 | 25 | 'page' => array( 'namespace' => 'thread_article_namespace', |
26 | | - 'title' => 'thread_article_title' ), |
| 26 | + 'title' => 'thread_article_title' ), |
27 | 27 | 'parent' => 'thread_parent', |
28 | 28 | 'ancestor' => 'thread_ancestor', |
29 | 29 | 'created' => 'thread_created', |
30 | 30 | 'modified' => 'thread_modified', |
31 | 31 | 'author' => array( 'id' => 'thread_author_id', |
32 | | - 'name' => 'thread_author_name'), |
| 32 | + 'name' => 'thread_author_name'), |
33 | 33 | 'summaryid' => 'thread_summary_page', |
34 | 34 | 'rootid' => 'thread_root', |
35 | 35 | 'type' => 'thread_type', |
36 | 36 | ); |
37 | 37 | |
38 | | - public function __construct($query, $moduleName) { |
39 | | - parent :: __construct($query, $moduleName, 'lqt'); |
| 38 | + public function __construct( $query, $moduleName ) { |
| 39 | + parent :: __construct( $query, $moduleName, 'th' ); |
40 | 40 | } |
41 | 41 | |
42 | 42 | public function execute() { |
43 | 43 | global $wgUser; |
44 | | - |
45 | | - $params = $this->extractRequestParams(); |
46 | | - $prop = array_flip($params['prop']); |
47 | 44 | |
| 45 | + $params = $this->extractRequestParams(); |
| 46 | + $prop = array_flip( $params['prop'] ); |
48 | 47 | $result = $this->getResult(); |
49 | | - |
50 | | - $this->addTables('thread'); |
| 48 | + $this->addTables( 'thread' ); |
| 49 | + $this->addFields( 'thread_id' ); |
51 | 50 | |
52 | | - $this->addFields('thread_id'); |
53 | | - |
54 | | - foreach( self::$propRelations as $name => $fields ) { |
55 | | - $addFields = $fields; |
56 | | - |
57 | | - if ( is_array($addFields) ) $addFields = array_values($addFields); |
58 | | - |
59 | | - $this->addFieldsIf( $addFields, isset($prop[$name]) ); |
| 51 | + foreach ( self::$propRelations as $name => $fields ) { |
| 52 | + // Pass a straight array rather than one with string |
| 53 | + // keys, to be sure that merging it into other added |
| 54 | + // arrays doesn't mess stuff up |
| 55 | + $this->addFieldsIf( array_values( (array)$fields ), isset( $prop[$name] ) ); |
60 | 56 | } |
61 | 57 | |
62 | 58 | // Check for conditions |
63 | 59 | $conditionFields = array( 'page', 'root', 'summary', 'author', 'id' ); |
64 | | - |
65 | | - foreach( $conditionFields as $field ) { |
66 | | - if ( isset($params[$field]) ) { |
| 60 | + foreach ( $conditionFields as $field ) { |
| 61 | + if ( isset( $params[$field] ) ) { |
67 | 62 | $this->handleCondition( $field, $params[$field] ); |
68 | 63 | } |
69 | 64 | } |
70 | 65 | |
71 | | - $this->addOption('LIMIT', $params['limit'] + 1); |
| 66 | + $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 67 | + $this->addWhereRange( 'thread_id', $params['dir'], |
| 68 | + $params['startid'], $params['endid'] ); |
72 | 69 | |
73 | | - $this->addWhereRange('thread_id', $params['dir'], |
74 | | - $params['startid'], $params['endid']); |
75 | | - |
76 | | - if (!is_null($params['show'])) { |
77 | | - $show = array_flip($params['show']); |
78 | | - $delType = $this->getDB()->addQuotes( Threads::TYPE_DELETED ); |
79 | | - |
80 | | - $this->addWhereIf( 'thread_type != '.$delType, isset($show['deleted']) ); |
| 70 | + if ( $params['showdeleted'] ) { |
| 71 | + $delType = $this->getDB()->addQuotes( Threads::TYPE_DELETED ); |
| 72 | + $this->addWhere( "thread_type != $delType" ); |
81 | 73 | } |
82 | 74 | |
83 | 75 | $res = $this->select( __METHOD__ ); |
84 | | - |
85 | 76 | $count = 0; |
86 | | - while($row = $res->fetchObject()) |
| 77 | + foreach ( $res as $row ) |
87 | 78 | { |
88 | | - if( ++$count > $params['limit'] ) { |
| 79 | + if ( ++$count > $params['limit'] ) { |
89 | 80 | // We've had enough |
90 | | - $this->setContinueEnumParameter('startid', $row->thread_id); |
| 81 | + $this->setContinueEnumParameter( 'startid', $row->thread_id ); |
91 | 82 | break; |
92 | 83 | } |
93 | 84 | |
94 | 85 | $entry = array(); |
95 | | - |
96 | | - foreach( $prop as $name => $nothing ) { |
| 86 | + foreach ( $prop as $name => $nothing ) { |
97 | 87 | $fields = self::$propRelations[$name]; |
98 | | - $entry[$name] = self::formatProperty( $name, $fields, $row ); |
| 88 | + self::formatProperty( $name, $fields, $row, $entry ); |
99 | 89 | } |
100 | 90 | |
101 | | - if ($entry) { |
102 | | - $fit = $result->addValue(array( 'query', $this->getModuleName() ), null, |
103 | | - $entry); |
104 | | - |
105 | | - if(!$fit) { |
106 | | - $this->setContinueEnumParameter('startid', $row->thread_id); |
| 91 | + if ( $entry ) { |
| 92 | + $fit = $result->addValue( array( 'query', |
| 93 | + $this->getModuleName() ), |
| 94 | + null, $entry); |
| 95 | + if ( !$fit ) { |
| 96 | + $this->setContinueEnumParameter( 'startid', $row->thread_id ); |
107 | 97 | break; |
108 | 98 | } |
109 | 99 | } |
110 | 100 | } |
111 | | - $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'thread'); |
| 101 | + $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'thread' ); |
112 | 102 | } |
113 | 103 | |
114 | | - static function formatProperty( $name, $fields, $row ) { |
115 | | - // Common case. |
116 | | - if ( !is_array($fields) ) { |
117 | | - return $row->$fields; |
118 | | - } |
119 | | - |
120 | | - // Special cases |
121 | | - if ($name == 'page') { |
| 104 | + static function formatProperty( $name, $fields, $row, &$entry ) { |
| 105 | + if ( !is_array( $fields ) ) { |
| 106 | + // Common case. |
| 107 | + $entry[$name] = $row->$fields; |
| 108 | + } else if ( $name == 'page' ) { |
| 109 | + // Special cases |
122 | 110 | $nsField = $fields['namespace']; |
123 | 111 | $tField = $fields['title']; |
124 | | - $title = Title::makeTitleSafe( $row->$nsField, $row->$tField ); |
125 | | - return $title->getPrefixedText(); |
| 112 | + $title = Title::makeTitle( $row->$nsField, $row->$tField ); |
| 113 | + $result = array(); |
| 114 | + ApiQueryBase::addTitleInfo( $entry, $title, 'page' ); |
| 115 | + } else { |
| 116 | + // Complicated case. |
| 117 | + $result = array(); |
| 118 | + foreach ( $fields as $part => $field ) { |
| 119 | + $entry[$name][$part] = $row->$field; |
| 120 | + } |
126 | 121 | } |
127 | | - |
128 | | - // Complicated case. |
129 | | - $result = array(); |
130 | | - foreach( $fields as $part => $field ) { |
131 | | - $result[$part] = $row->$field; |
132 | | - } |
133 | | - |
134 | | - return $result; |
135 | 122 | } |
136 | 123 | |
137 | 124 | function addPageCond( $prop, $value ) { |
138 | | - |
139 | | - $value = explode( '|', $value ); |
140 | | - |
141 | | - if ( count($value) === 1 ) { |
142 | | - $cond = $this->getPageCond($prop, $value[0]); |
| 125 | + if ( count( $value ) === 1 ) { |
| 126 | + $cond = $this->getPageCond( $prop, $value[0] ); |
143 | 127 | $this->addWhere( $cond ); |
144 | 128 | } else { |
145 | 129 | $conds = array(); |
146 | | - |
147 | | - foreach( $value as $page ) { |
| 130 | + foreach ( $value as $page ) { |
148 | 131 | $cond = $this->getPageCond( $prop, $page ); |
149 | 132 | $conds[] = $this->getDB()->makeList( $cond, LIST_AND ); |
150 | 133 | } |
151 | 134 | |
152 | 135 | $cond = $this->getDB()->makeList( $conds, LIST_OR ); |
153 | | - |
154 | 136 | $this->addWhere( $cond ); |
155 | 137 | } |
156 | 138 | } |
157 | 139 | |
158 | 140 | function getPageCond( $prop, $value ) { |
159 | | - $fieldMappings = array( 'page' => |
160 | | - array( 'namespace' => 'thread_article_namespace', |
161 | | - 'title' => 'thread_article_title', |
162 | | - ), |
163 | | - 'root' => array( 'id' => 'thread_root' ), |
164 | | - 'summary' => array( 'id' => 'thread_summary_id' ), |
165 | | - ); |
| 141 | + $fieldMappings = array( |
| 142 | + 'page' => array( |
| 143 | + 'namespace' => 'thread_article_namespace', |
| 144 | + 'title' => 'thread_article_title', |
| 145 | + ), |
| 146 | + 'root' => array( 'id' => 'thread_root' ), |
| 147 | + 'summary' => array( 'id' => 'thread_summary_id' ), |
| 148 | + ); |
| 149 | + |
166 | 150 | // Split. |
167 | 151 | $t = Title::newFromText( $value ); |
168 | | - |
169 | 152 | $cond = array(); |
170 | | - |
171 | | - foreach( $fieldMappings[$prop] as $type => $field ) { |
172 | | - switch($type) { |
| 153 | + foreach ( $fieldMappings[$prop] as $type => $field ) { |
| 154 | + switch ( $type ) { |
173 | 155 | case 'namespace': |
174 | 156 | $cond[$field] = $t->getNamespace(); |
175 | 157 | break; |
— | — | @@ -179,38 +161,25 @@ |
180 | 162 | $cond[$field] = $t->getArticleID(); |
181 | 163 | break; |
182 | 164 | default: |
183 | | - throw new MWException( "Unknown condition type $type" ); |
| 165 | + ApiBase::dieDebug( __METHOD__, "Unknown condition type $type" ); |
184 | 166 | } |
185 | 167 | } |
186 | | - |
187 | 168 | return $cond; |
188 | 169 | } |
189 | 170 | |
190 | 171 | function handleCondition( $prop, $value ) { |
191 | | - // Special cases |
192 | 172 | $titleParams = array( 'page', 'root', 'summary' ); |
| 173 | + $fields = self::$propRelations[$prop]; |
193 | 174 | |
194 | 175 | if ( in_array( $prop, $titleParams ) ) { |
195 | | - return $this->addPageCond( $prop, $value ); |
| 176 | + // Special cases |
| 177 | + $this->addPageCond( $prop, $value ); |
| 178 | + } else if ( $prop == 'author' ) { |
| 179 | + $this->addWhereFld( 'thread_author_name', $value ); |
| 180 | + } else if ( !is_array( $fields ) ) { |
| 181 | + // Common case |
| 182 | + return $this->addWhereFld( $fields, $value ); |
196 | 183 | } |
197 | | - |
198 | | - $fields = self::$propRelations[$prop]; |
199 | | - |
200 | | - // Some cases where we can pull more than one |
201 | | - $multiParams = array( 'id', 'author' ); |
202 | | - if ( in_array( $prop, $multiParams ) ) { |
203 | | - $value = explode( '|', $value ); |
204 | | - } |
205 | | - |
206 | | - // Common case |
207 | | - if ( !is_array( $fields ) ) { |
208 | | - return $this->addWhere( array( $fields => $value ) ); |
209 | | - } |
210 | | - |
211 | | - // Other special cases. |
212 | | - if ( $prop == 'author' ) { |
213 | | - $this->addWhere( array( 'thread_author_name' => $value ) ); |
214 | | - } |
215 | 184 | } |
216 | 185 | |
217 | 186 | public function getAllowedParams() { |
— | — | @@ -219,7 +188,7 @@ |
220 | 189 | ApiBase :: PARAM_TYPE => 'integer' |
221 | 190 | ), |
222 | 191 | 'endid' => array( |
223 | | - ApiBase :: PARAM_TYPE => 'integer', |
| 192 | + ApiBase :: PARAM_TYPE => 'integer' |
224 | 193 | ), |
225 | 194 | 'dir' => array( |
226 | 195 | ApiBase :: PARAM_TYPE => array( |
— | — | @@ -228,12 +197,7 @@ |
229 | 198 | ), |
230 | 199 | ApiBase :: PARAM_DFLT => 'newer' |
231 | 200 | ), |
232 | | - 'show' => array( |
233 | | - ApiBase :: PARAM_ISMULTI => true, |
234 | | - ApiBase :: PARAM_TYPE => array ( |
235 | | - 'deleted', |
236 | | - ), |
237 | | - ), |
| 201 | + 'showdeleted' => false, |
238 | 202 | 'limit' => array( |
239 | 203 | ApiBase :: PARAM_DFLT => 10, |
240 | 204 | ApiBase :: PARAM_TYPE => 'limit', |
— | — | @@ -243,15 +207,25 @@ |
244 | 208 | ), |
245 | 209 | 'prop' => array( |
246 | 210 | ApiBase :: PARAM_DFLT => 'id|subject|page|parent|author', |
247 | | - ApiBase :: PARAM_TYPE => array_keys(self::$propRelations), |
| 211 | + ApiBase :: PARAM_TYPE => array_keys( self::$propRelations ), |
248 | 212 | ApiBase :: PARAM_ISMULTI => true |
249 | 213 | ), |
250 | 214 | |
251 | | - 'page' => null, |
252 | | - 'author' => null, |
253 | | - 'root' => null, |
254 | | - 'summary' => null, |
255 | | - 'id' => null, |
| 215 | + 'page' => array( |
| 216 | + ApiBase :: PARAM_ISMULTI => true |
| 217 | + ), |
| 218 | + 'author' => array( |
| 219 | + ApiBase :: PARAM_ISMULTI => true |
| 220 | + ), |
| 221 | + 'root' => array( |
| 222 | + ApiBase :: PARAM_ISMULTI => true |
| 223 | + ), |
| 224 | + 'summary' => array( |
| 225 | + ApiBase :: PARAM_ISMULTI => true |
| 226 | + ), |
| 227 | + 'id' => array( |
| 228 | + ApiBase :: PARAM_ISMULTI => true |
| 229 | + ), |
256 | 230 | ); |
257 | 231 | } |
258 | 232 | |
— | — | @@ -283,6 +257,6 @@ |
284 | 258 | } |
285 | 259 | |
286 | 260 | public function getVersion() { |
287 | | - return __CLASS__; |
| 261 | + return __CLASS__ . '$Id$'; |
288 | 262 | } |
289 | 263 | } |
Property changes on: trunk/extensions/LiquidThreads/api/ApiQueryLQTThreads.php |
___________________________________________________________________ |
Name: svn:keywords |
290 | 264 | + Id |