r73044 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73043‎ | r73044 | r73045 >
Date:01:50, 15 September 2010
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
fixing sql query from r72954, also more readable
Modified paths:
  • /trunk/extensions/CentralNotice/SpecialCentralNotice.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/SpecialCentralNotice.php
@@ -1011,79 +1011,91 @@
10121012 preg_match( '/[a-zA-Z][a-zA-Z]/', $location, $matches );
10131013 $location = strtoupper( $matches[0] );
10141014
 1015+ $campaigns = array();
10151016 $dbr = wfGetDB( DB_SLAVE );
10161017 $encTimestamp = $dbr->addQuotes( $dbr->timestamp() );
 1018+
 1019+ // Pull non-geotargeted campaigns
 1020+ $campaignResults1 = $dbr->select(
 1021+ array(
 1022+ 'cn_notices',
 1023+ 'cn_notice_languages'
 1024+ ),
 1025+ array(
 1026+ 'not_id'
 1027+ ),
 1028+ array (
 1029+ "not_start <= $encTimestamp",
 1030+ "not_end >= $encTimestamp",
 1031+ 'not_enabled = 1', // enabled
 1032+ 'not_geo = 0', // not geotargeted
 1033+ 'nl_notice_id = cn_notices.not_id',
 1034+ 'nl_language' => $language,
 1035+ 'not_project' => array( '', $project )
 1036+ ),
 1037+ __METHOD__
 1038+ );
 1039+ foreach ( $campaignResults1 as $row ) {
 1040+ $campaigns[] = $row->not_id;
 1041+ }
10171042 if ( $location ) {
1018 - $res = $dbr->select(
 1043+ // Pull geotargeted campaigns
 1044+ $campaignResults2 = $dbr->select(
10191045 array(
10201046 'cn_notices',
10211047 'cn_notice_languages',
1022 - 'cn_notice_countries',
1023 - 'cn_assignments',
1024 - 'cn_templates'
 1048+ 'cn_notice_countries'
10251049 ),
10261050 array(
1027 - 'tmp_name',
1028 - 'SUM(tmp_weight) AS total_weight',
1029 - 'tmp_display_anon',
1030 - 'tmp_display_account'
 1051+ 'not_id'
10311052 ),
1032 - array (
1033 - "not_start <= $encTimestamp",
1034 - "not_end >= $encTimestamp",
1035 - 'not_enabled = 1',
1036 - 'nc_notice_id = cn_notices.not_id',
1037 - "(not_geo = 0) OR ((not_geo = 1) AND (nc_country = '$location'))", // not geotargeted or (geotargeted and matches location)
1038 - 'nl_notice_id = cn_notices.not_id',
1039 - 'nl_language' => $language,
1040 - 'not_project' => array( '', $project ),
1041 - 'cn_notices.not_id=cn_assignments.not_id',
1042 - 'cn_assignments.tmp_id=cn_templates.tmp_id'
1043 - ),
1044 - __METHOD__,
10451053 array(
1046 - 'GROUP BY' => 'tmp_name'
1047 - ),
1048 - array(
1049 - 'cn_notice_countries' => array(
1050 - 'LEFT JOIN',
1051 - "nc_country = '$location'"
1052 - )
1053 - )
1054 - );
1055 - } else {
1056 - $res = $dbr->select(
1057 - array(
1058 - 'cn_notices',
1059 - 'cn_notice_languages',
1060 - 'cn_assignments',
1061 - 'cn_templates'
1062 - ),
1063 - array(
1064 - 'tmp_name',
1065 - 'SUM(tmp_weight) AS total_weight',
1066 - 'tmp_display_anon',
1067 - 'tmp_display_account'
1068 - ),
1069 - array (
10701054 "not_start <= $encTimestamp",
10711055 "not_end >= $encTimestamp",
10721056 'not_enabled = 1', // enabled
1073 - 'not_geo = 0', // not geotargeted
 1057+ 'not_geo = 1', // geotargeted
 1058+ 'nc_notice_id = cn_notices.not_id',
 1059+ 'nc_country' => $location,
10741060 'nl_notice_id = cn_notices.not_id',
10751061 'nl_language' => $language,
1076 - 'not_project' => array( '', $project ),
1077 - 'cn_notices.not_id=cn_assignments.not_id',
1078 - 'cn_assignments.tmp_id=cn_templates.tmp_id'
 1062+ 'not_project' => array( '', $project )
10791063 ),
1080 - __METHOD__,
1081 - array(
1082 - 'GROUP BY' => 'tmp_name'
1083 - )
 1064+ __METHOD__
10841065 );
 1066+ foreach ( $campaignResults2 as $row ) {
 1067+ $campaigns[] = $row->not_id;
 1068+ }
10851069 }
 1070+
 1071+ // Convert array of campaigns into a comma-delimited list for SQL
 1072+ $campaignList = implode(',', $campaigns);
 1073+
 1074+ // Pull all banners assigned to the campaigns
 1075+ $bannerResults = $dbr->select(
 1076+ array(
 1077+ 'cn_notices',
 1078+ 'cn_assignments',
 1079+ 'cn_templates'
 1080+ ),
 1081+ array(
 1082+ 'tmp_name',
 1083+ 'SUM(tmp_weight) AS total_weight',
 1084+ 'tmp_display_anon',
 1085+ 'tmp_display_account'
 1086+ ),
 1087+ array (
 1088+ "cn_notices.not_id IN ($campaignList)",
 1089+ 'cn_notices.not_id=cn_assignments.not_id',
 1090+ 'cn_assignments.tmp_id=cn_templates.tmp_id'
 1091+ ),
 1092+ __METHOD__,
 1093+ array(
 1094+ 'GROUP BY' => 'tmp_name'
 1095+ )
 1096+ );
 1097+
10861098 $templates = array();
1087 - foreach ( $res as $row ) {
 1099+ foreach ( $bannerResults as $row ) {
10881100 $template = array();
10891101 $template['name'] = $row->tmp_name;
10901102 $template['weight'] = intval( $row->total_weight );

Follow-up revisions

RevisionCommit summaryAuthorDate
r73075better query per comment at r73044kaldari18:15, 15 September 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r72954add geotargeting to banner selection codekaldari02:30, 14 September 2010

Comments

#Comment by Tim Starling (talk | contribs)   02:03, 15 September 2010

Instead of constructing an IN() function yourself using implode(), you should be using:

			array (
				"cn_notices.not_id" =>  $campaigns,
				'cn_notices.not_id=cn_assignments.not_id',
				'cn_assignments.tmp_id=cn_templates.tmp_id'
			),

This is DBMS-independent, and quotes all the elements of $campaigns, to avoid SQL injection.

#Comment by Kaldari (talk | contribs)   18:16, 15 September 2010

Thanks. I didn't know I could do that. Fixed in r73075.

Status & tagging log