r55973 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55972‎ | r55973 | r55974 >
Date:14:46, 7 September 2009
Author:catrope
Status:deferred
Tags:
Comment:
LiquidThreads: Clean up API module added in r55920:
* Changed parameter prefix from lqt to th for consistency with core API
* Make a bunch of params multivalue and kill explode( '|', $foo ); calls
* Rename show=deleted to showdeleted to prevent confusion with the conventional meaning of the show parameter
* Refactor formatProperty() to support adding multiple properties at once
* Random minor fixes
* Consistent coding style
* Cut down on excessive use of tabs and newlines
Modified paths:
  • /trunk/extensions/LiquidThreads/api/ApiQueryLQTThreads.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LiquidThreads/api/ApiQueryLQTThreads.php
@@ -1,20 +1,20 @@
22 <?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+ */
1919 class ApiQueryLQTThreads extends ApiQueryBase {
2020
2121 // Property definitions
@@ -22,153 +22,135 @@
2323 'id' => 'thread_id',
2424 'subject' => 'thread_subject',
2525 'page' => array( 'namespace' => 'thread_article_namespace',
26 - 'title' => 'thread_article_title' ),
 26+ 'title' => 'thread_article_title' ),
2727 'parent' => 'thread_parent',
2828 'ancestor' => 'thread_ancestor',
2929 'created' => 'thread_created',
3030 'modified' => 'thread_modified',
3131 'author' => array( 'id' => 'thread_author_id',
32 - 'name' => 'thread_author_name'),
 32+ 'name' => 'thread_author_name'),
3333 'summaryid' => 'thread_summary_page',
3434 'rootid' => 'thread_root',
3535 'type' => 'thread_type',
3636 );
3737
38 - public function __construct($query, $moduleName) {
39 - parent :: __construct($query, $moduleName, 'lqt');
 38+ public function __construct( $query, $moduleName ) {
 39+ parent :: __construct( $query, $moduleName, 'th' );
4040 }
4141
4242 public function execute() {
4343 global $wgUser;
44 -
45 - $params = $this->extractRequestParams();
46 - $prop = array_flip($params['prop']);
4744
 45+ $params = $this->extractRequestParams();
 46+ $prop = array_flip( $params['prop'] );
4847 $result = $this->getResult();
49 -
50 - $this->addTables('thread');
 48+ $this->addTables( 'thread' );
 49+ $this->addFields( 'thread_id' );
5150
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] ) );
6056 }
6157
6258 // Check for conditions
6359 $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] ) ) {
6762 $this->handleCondition( $field, $params[$field] );
6863 }
6964 }
7065
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'] );
7269
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" );
8173 }
8274
8375 $res = $this->select( __METHOD__ );
84 -
8576 $count = 0;
86 - while($row = $res->fetchObject())
 77+ foreach ( $res as $row )
8778 {
88 - if( ++$count > $params['limit'] ) {
 79+ if ( ++$count > $params['limit'] ) {
8980 // We've had enough
90 - $this->setContinueEnumParameter('startid', $row->thread_id);
 81+ $this->setContinueEnumParameter( 'startid', $row->thread_id );
9182 break;
9283 }
9384
9485 $entry = array();
95 -
96 - foreach( $prop as $name => $nothing ) {
 86+ foreach ( $prop as $name => $nothing ) {
9787 $fields = self::$propRelations[$name];
98 - $entry[$name] = self::formatProperty( $name, $fields, $row );
 88+ self::formatProperty( $name, $fields, $row, $entry );
9989 }
10090
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 );
10797 break;
10898 }
10999 }
110100 }
111 - $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'thread');
 101+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'thread' );
112102 }
113103
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
122110 $nsField = $fields['namespace'];
123111 $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+ }
126121 }
127 -
128 - // Complicated case.
129 - $result = array();
130 - foreach( $fields as $part => $field ) {
131 - $result[$part] = $row->$field;
132 - }
133 -
134 - return $result;
135122 }
136123
137124 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] );
143127 $this->addWhere( $cond );
144128 } else {
145129 $conds = array();
146 -
147 - foreach( $value as $page ) {
 130+ foreach ( $value as $page ) {
148131 $cond = $this->getPageCond( $prop, $page );
149132 $conds[] = $this->getDB()->makeList( $cond, LIST_AND );
150133 }
151134
152135 $cond = $this->getDB()->makeList( $conds, LIST_OR );
153 -
154136 $this->addWhere( $cond );
155137 }
156138 }
157139
158140 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+
166150 // Split.
167151 $t = Title::newFromText( $value );
168 -
169152 $cond = array();
170 -
171 - foreach( $fieldMappings[$prop] as $type => $field ) {
172 - switch($type) {
 153+ foreach ( $fieldMappings[$prop] as $type => $field ) {
 154+ switch ( $type ) {
173155 case 'namespace':
174156 $cond[$field] = $t->getNamespace();
175157 break;
@@ -179,38 +161,25 @@
180162 $cond[$field] = $t->getArticleID();
181163 break;
182164 default:
183 - throw new MWException( "Unknown condition type $type" );
 165+ ApiBase::dieDebug( __METHOD__, "Unknown condition type $type" );
184166 }
185167 }
186 -
187168 return $cond;
188169 }
189170
190171 function handleCondition( $prop, $value ) {
191 - // Special cases
192172 $titleParams = array( 'page', 'root', 'summary' );
 173+ $fields = self::$propRelations[$prop];
193174
194175 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 );
196183 }
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 - }
215184 }
216185
217186 public function getAllowedParams() {
@@ -219,7 +188,7 @@
220189 ApiBase :: PARAM_TYPE => 'integer'
221190 ),
222191 'endid' => array(
223 - ApiBase :: PARAM_TYPE => 'integer',
 192+ ApiBase :: PARAM_TYPE => 'integer'
224193 ),
225194 'dir' => array(
226195 ApiBase :: PARAM_TYPE => array(
@@ -228,12 +197,7 @@
229198 ),
230199 ApiBase :: PARAM_DFLT => 'newer'
231200 ),
232 - 'show' => array(
233 - ApiBase :: PARAM_ISMULTI => true,
234 - ApiBase :: PARAM_TYPE => array (
235 - 'deleted',
236 - ),
237 - ),
 201+ 'showdeleted' => false,
238202 'limit' => array(
239203 ApiBase :: PARAM_DFLT => 10,
240204 ApiBase :: PARAM_TYPE => 'limit',
@@ -243,15 +207,25 @@
244208 ),
245209 'prop' => array(
246210 ApiBase :: PARAM_DFLT => 'id|subject|page|parent|author',
247 - ApiBase :: PARAM_TYPE => array_keys(self::$propRelations),
 211+ ApiBase :: PARAM_TYPE => array_keys( self::$propRelations ),
248212 ApiBase :: PARAM_ISMULTI => true
249213 ),
250214
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+ ),
256230 );
257231 }
258232
@@ -283,6 +257,6 @@
284258 }
285259
286260 public function getVersion() {
287 - return __CLASS__;
 261+ return __CLASS__ . '$Id$';
288262 }
289263 }
Property changes on: trunk/extensions/LiquidThreads/api/ApiQueryLQTThreads.php
___________________________________________________________________
Name: svn:keywords
290264 + Id

Follow-up revisions

RevisionCommit summaryAuthorDate
r56000Update LiquidThreads JS:...werdna22:02, 7 September 2009

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r55920Add LiquidThreads threads API module, for use internally and by other toolswerdna12:44, 7 September 2009

Status & tagging log