r79236 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79235‎ | r79236 | r79237 >
Date:05:13, 30 December 2010
Author:gigs
Status:ok (Comments)
Tags:
Comment:
Convert the SQL to use DataBase::select and some more general cleanups
Modified paths:
  • /trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/GoogleNewsSitemap/GoogleNewsSitemap_body.php
@@ -103,18 +103,15 @@
104104 $wgSitename
105105 );
106106
107 - $feed->outHeader();
108 -
109 - $dbr = wfGetDB( DB_SLAVE );
110 - $sql = $this->dpl_buildSQL();
 107+ $res = $this->doQuery();
111108 // Debug line
112109 // echo "\n<p>$sql</p>\n";
113 - $res = $dbr->query ( $sql );
114110
115111 // FIXME: figure out how to fail with no results gracefully
116 - if ( $dbr->numRows( $res ) == 0 ) {
 112+ if ( $res->numRows( $res ) == 0 ) {
117113 $feed->outFooter();
118114 if ( false == $this->params['suppressErrors'] ) {
 115+ $wgOut->disable();
119116 echo htmlspecialchars( wfMsg( 'googlenewssitemap_noresults' ) );
120117 return;
121118 } else {
@@ -122,7 +119,8 @@
123120 }
124121 }
125122
126 - foreach ( $res as $row ) {
 123+ $feed->outHeader();
 124+ while ( $row = $res->fetchObject( $res ) ) {
127125 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
128126
129127 if ( !$title ) {
@@ -130,7 +128,7 @@
131129 return;
132130 }
133131
134 - //$titleText = ( $this->params['nameSpace'] ) ? $title->getPrefixedText() : $title->getText();
 132+ $titleText = ( true == $this->params['nameSpace'] ) ? $title->getPrefixedText() : $title->getText();
135133
136134 if ( 'sitemap' == $this->params['feed'] ) {
137135
@@ -171,64 +169,71 @@
172170 /**
173171 * Build sql
174172 **/
175 - public function dpl_buildSQL() {
 173+ public function doQuery() {
176174
177 - $sqlSelectFrom = 'SELECT page_namespace, page_title, page_id, c1.cl_timestamp FROM ' . $this->params['dbr']->tableName( 'page' );
 175+ $dbr = wfGetDB( DB_SLAVE );
178176
 177+ $tables[]=$dbr->tableName( 'page' );
 178+
 179+ //this is a little hacky, c1 is dynamically defined as the first category
 180+ //so this can't ever work with uncategorized articles
 181+ $fields = array('page_namespace', 'page_title', 'page_id', 'c1.cl_timestamp');
 182+
179183 if ( $this->params['nameSpace'] ) {
180 - $sqlWhere = ' WHERE page_namespace=' . $this->params['nameSpace'] . ' ';
181 - } else {
182 - $sqlWhere = ' WHERE 1=1 ';
 184+ $conditions['page_namespace'] = $this->params['nameSpace'];
183185 }
184186
185187 // If flagged revisions is in use, check which options selected.
186 - // FIXME: double check the default options in function::dpl_parm; what should it default to?
 188+ // FIXME: double check the default options; what should it default to?
187189 if ( function_exists( 'efLoadFlaggedRevs' ) ) {
188 - $flaggedPages = $this->params['dbr']->tableName( 'flaggedpages' );
 190+ $flaggedPages = $dbr->tableName( 'flaggedpages' );
189191 $filterSet = array( 'only', 'exclude' );
190192 # Either involves the same JOIN here...
191193 if ( in_array( $this->params['stable'], $filterSet ) || in_array( $this->params['quality'], $filterSet ) ) {
192 - $sqlSelectFrom .= " LEFT JOIN $flaggedPages ON page_id = fp_page_id";
 194+ $joins['flaggedpages'] = Array( 'LEFT JOIN', 'page_id = fp_page_id' );
193195 }
194196 switch( $this->params['stable'] ) {
195197 case 'only':
196 - $sqlWhere .= ' AND fp_stable IS NOT NULL ';
 198+ $conditions[]='fp_stable IS NOT NULL ';
197199 break;
198200 case 'exclude':
199 - $sqlWhere .= ' AND fp_stable IS NULL ';
 201+ $conditions['fp_stable'] = null;
200202 break;
201203 }
202204 switch( $this->params['quality'] ) {
203205 case 'only':
204 - $sqlWhere .= ' AND fp_quality >= 1';
 206+ $conditions[]='fp_quality >= 1';
205207 break;
206208 case 'exclude':
207 - $sqlWhere .= ' AND fp_quality = 0';
 209+ $conditions['fp_quality'] = 0;
208210 break;
209211 }
210212 }
211213
212214 switch ( $this->params['redirects'] ) {
213215 case 'only':
214 - $sqlWhere .= ' AND page_is_redirect = 1 ';
215 - break;
 216+ $conditions['page_is_redirect'] = 1;
 217+ break;
216218 case 'exclude':
217 - $sqlWhere .= ' AND page_is_redirect = 0 ';
218 - break;
 219+ $conditions['page_is_redirect'] = 0;
 220+ break;
219221 }
220222
221 - $currentTableNumber = 0;
222 -
223 - for ( $i = 0; $i < $this->params['catCount']; $i++ ) {
224 - $sqlSelectFrom .= ' INNER JOIN ' . $this->params['dbr']->tableName( 'categorylinks' );
225 - $sqlSelectFrom .= ' AS c' . ( $currentTableNumber + 1 ) . ' ON page_id = c';
226 - $sqlSelectFrom .= ( $currentTableNumber + 1 ) . '.cl_from AND c' . ( $currentTableNumber + 1 );
227 -
228 - $sqlSelectFrom .= '.cl_to=' . $this->params['dbr']->addQuotes( $this->categories[$i]->getDBkey() );
229 -
 223+ $currentTableNumber = 1;
 224+ $categorylinks = $dbr->tableName( 'categorylinks' );
 225+ for ($i = 0; $i < $this->params['catCount']; $i++) {
 226+ $joins["$categorylinks AS c$currentTableNumber"] = Array( 'INNER JOIN',
 227+ Array( "page_id = c{$currentTableNumber}.cl_from",
 228+ "c{$currentTableNumber}.cl_to={$dbr->addQuotes($this->categories[$i]->getDBKey())}"
 229+ )
 230+ );
 231+ $tables[] = "$categorylinks AS c$currentTableNumber";
230232 $currentTableNumber++;
231233 }
 234+
232235
 236+ //exclusion categories disabled pending discussion on whether they are necessary
 237+ /*
233238 for ( $i = 0; $i < $this->params['notCatCount']; $i++ ) {
234239 // echo "notCategory parameter $i<br />\n";
235240 $sqlSelectFrom .= ' LEFT OUTER JOIN ' . $this->params['dbr']->tableName( 'categorylinks' );
@@ -236,45 +241,43 @@
237242 $sqlSelectFrom .= '.cl_from AND c' . ( $currentTableNumber + 1 );
238243 $sqlSelectFrom .= '.cl_to=' . $this->params['dbr']->addQuotes( $this->notCategories[$i]->getDBkey() );
239244
240 - $sqlWhere .= ' AND c' . ( $currentTableNumber + 1 ) . '.cl_to IS NULL';
 245+ $conditions .= ' AND c' . ( $currentTableNumber + 1 ) . '.cl_to IS NULL';
241246
242247 $currentTableNumber++;
243248 }
 249+ */
244250
245 - if ( 'lastedit' == $this->params['orderMethod'] ) {
246 - $sqlWhere .= ' ORDER BY page_touched ';
 251+ if ( 'descending' == $this->params['order'] ) {
 252+ $sortOrder = 'DESC';
247253 } else {
248 - $sqlWhere .= ' ORDER BY c1.cl_timestamp ';
 254+ $sortOrder = 'ASC';
249255 }
250256
251 - if ( 'descending' == $this->params['order'] ) {
252 - $sqlWhere .= 'DESC';
 257+ if ( 'lastedit' == $this->params['orderMethod'] ) {
 258+ $options['ORDER BY'] = 'page_touched ' . $sortOrder;
253259 } else {
254 - $sqlWhere .= 'ASC';
 260+ $options['ORDER BY'] = 'c1.cl_timestamp ' . $sortOrder;
255261 }
256262
257 - // FIXME: Note: this is not a boolean type check - will also trap count = 0 which may
258 - // accidentally give unlimited returns
259 - if ( 0 < $this->params['count'] ) {
260 - $sqlWhere .= ' LIMIT ' . $this->params['count'];
261 - }
262263
263 - // debug line
264 - // echo "<p>$sqlSelectFrom$sqlWhere;</p>\n";
 264+ //earlier validation logic ensures this is a reasonable number
 265+ $options['LIMIT'] = $this->params['count'];
265266
266 - return $sqlSelectFrom . $sqlWhere;
 267+ //return $dbr->query( $sqlSelectFrom . $conditions );
 268+ return $dbr->select ( $tables, $fields, $conditions, '', $options, $joins);
267269 } // end buildSQL
268270
269271 /**
270272 * Parse parameters, populates $this->params
271273 **/
272274 public function unload_params() {
273 - global $wgContLang, $wgRequest;
 275+ global $wgContLang;
 276+ global $wgRequest;
274277
275278 $this->params = array();
276 - //$parser = new Parser;
277 - //$poptions = new ParserOptions;
278 - //$category = $wgRequest->getArray( 'category', 'Published' );
 279+ $parser = new Parser;
 280+ $poptions = new ParserOptions;
 281+ $category = $wgRequest->getArray( 'category', 'Published' );
279282 // $title = Title::newFromText( $parser->transformMsg( $category, $poptions ) );
280283 // if ( is_object( $title ) ){
281284 // $this->categories[] = $title;
@@ -341,8 +344,8 @@
342345 $cats = $title->getParentCategories();
343346 $str = '';
344347 # the following code is based (stolen) from r56954 of flagged revs.
345 - $catMap = array();
346 - $catMask = array();
 348+ $catMap = Array();
 349+ $catMask = Array();
347350 $msg = wfMsg( 'googlenewssitemap_categorymap' );
348351 if ( !wfEmptyMsg( 'googlenewssitemap_categorymap', $msg ) ) {
349352 $list = explode( "\n*", "\n$msg" );

Follow-up revisions

RevisionCommit summaryAuthorDate
r86451* Move FlaggedRevs integration for GNSM to a hook that FlaggedRevs can use...mah02:38, 20 April 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:07, 30 December 2010

Please, can we not revert previous cleanups again? Some tips:

OK: svn up -> open file or refresh all open files -> code -> svn commit

NOK: start working -> run svn up and dismiss the conflicts or replace updated files with the ones you've been working with somewhere else -> svn commit

#Comment by Reedy (talk | contribs)   07:30, 31 December 2010

Ping. Linked "gigs" to Wiki Account "Gigs"

Hopefully he has an email set....

#Comment by Gigs (talk | contribs)   18:14, 31 December 2010

What are you referring to Nikerabbit? I did an svn up before I started working, and resolved all conflicts "tf".

#Comment by Gigs (talk | contribs)   18:26, 31 December 2010

I see where the misunderstanding came from. I copied some of the cleaner versions of a few sections from the modern rev of DPL, which hadn't shared the same cleanups and why Nikerabbit thought there was an SVN issue.

A lot of these sections will probably go away anyway once we determine which of the features from DPL are actually needed and which can be stripped out.

#Comment by Gigs (talk | contribs)   18:38, 31 December 2010

Rev. 79362 restores the cleanups Array->array and foreach vs while on the row fetching. The same cleanups should probably be applied to the current version of DPL as well.

As I alluded to above, the overlap between this and DPL should go away as we strip GNSM down to only the needed features. If the overlap doesn't go away because GNSM actually does need all these DPL features, then we need to look at merging GNSM functionality back into DPL. I'm willing to wait to see what the Wikinews community actually needs feature wise before we make that call though.

#Comment by MarkAHershberger (talk | contribs)   01:45, 20 April 2011

Didn't like the function name doQuery() since it would confuse devs (well, me) who thought it meant core's Database::doQuery(). Renamed it in [rev to be committed].

Moved the FlaggedRevs stuff to a hook that FlaggedRevs.

Also, in_array() is slow, use isset().

#Comment by 😂 (talk | contribs)   01:57, 20 April 2011

in_array and isset() do two different things, and the former is correct here. You're thinking array_key_exists().

Status & tagging log