r73095 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73094‎ | r73095 | r73096 >
Date:22:38, 15 September 2010
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
fix for r73003 - queries for preferred campaigns (theres probably a better way to do this though)
Modified paths:
  • /trunk/extensions/CentralNotice/CentralNotice.db.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialBannerListLoader.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/SpecialBannerListLoader.php
@@ -64,7 +64,7 @@
6565
6666 if ( $this->language == 'en' && $this->project != null ) {
6767 // See if we have any preferred notices for all of en
68 - $notices = $this->centralNoticeDB->getNotices( '', 'en', '', '', 1, $this->location );
 68+ $notices = $this->centralNoticeDB->getNotices( '', 'en', null, 1, 1, $this->location );
6969
7070 if ( $notices ) {
7171 // Pull out values
@@ -79,7 +79,8 @@
8080 }
8181
8282 if ( !$templates && $this->project == 'wikipedia' ) {
83 - $notices = $this->centralNoticeDB->getNotices( 'wikipedia', $this->language, '', '', 1, $this->location );
 83+ // See if we have any preferred notices for this language wikipedia
 84+ $notices = $this->centralNoticeDB->getNotices( 'wikipedia', $this->language, null, 1, 1, $this->location );
8485 if ( $notices ) {
8586 foreach ( $notices as $notice => $val ) {
8687 $templates = $this->centralNoticeDB->selectTemplatesAssigned( $notice );
Index: trunk/extensions/CentralNotice/CentralNotice.db.php
@@ -15,20 +15,26 @@
1616 }
1717
1818 /*
19 - * Return notices in the system within given constraints
20 - * Optional: return both enabled and disabled notices
 19+ * Return campaigns in the system within given constraints
 20+ * By default returns enabled campaigns, if $enabled set to false, returns both enabled and disabled campaigns
2121 */
2222 public function getNotices( $project = false, $language = false, $date = false, $enabled = true, $preferred = false, $location = false ) {
 23+
 24+ $notices = array();
 25+
2326 // Database setup
2427 $dbr = wfGetDB( DB_SLAVE );
2528
 29+ if ( !$date ) {
 30+ $encTimestamp = $dbr->addQuotes( $dbr->timestamp() );
 31+ } else {
 32+ $encTimestamp = $dbr->addQuotes( $date );
 33+ }
 34+
2635 $tables[] = "cn_notices";
2736 if ( $language ) {
2837 $tables[] = "cn_notice_languages";
2938 }
30 - if ( $location ) {
31 - $tables[] = "cn_notice_countries";
32 - }
3339
3440 // Use whatever conditional arguments got passed in
3541 if ( $project ) {
@@ -38,21 +44,16 @@
3945 $conds[] = "nl_notice_id = cn_notices.not_id";
4046 $conds[] = "nl_language =" . $dbr->addQuotes( $language );
4147 }
 48+ if ( $enabled ) {
 49+ $conds[] = "not_enabled = 1";
 50+ }
4251 if ( $preferred ) {
4352 $conds[] = "not_preferred = 1";
4453 }
45 - if ( $location ) {
46 - $conds[] = 'nc_notice_id = cn_notices.not_id';
47 - $conds[] = "(not_geo = 0) OR ((not_geo = 1) AND (nc_country = '$location'))";
48 - }
49 - if ( !$date ) {
50 - $date = $dbr->timestamp();
51 - }
 54+ $conds[] = "not_geo = 0";
 55+ $conds[] = "not_start <= " . $encTimestamp;
 56+ $conds[] = "not_end >= " . $encTimestamp;
5257
53 - $conds[] = ( $date ) ? "not_start <= " . $dbr->addQuotes( $date ) : "not_start <= " . $dbr->addQuotes( $dbr->timestamp( $date ) );
54 - $conds[] = ( $date ) ? "not_end >= " . $dbr->addQuotes( $date ) : "not_end >= " . $dbr->addQuotes( $dbr->timestamp( $date ) );
55 - $conds[] = ( $enabled ) ? "not_enabled = " . $dbr->addQuotes( $enabled ) : "not_enabled = " . $dbr->addQuotes( 1 );
56 -
5758 // Pull db data
5859 $res = $dbr->select(
5960 $tables,
@@ -67,20 +68,72 @@
6869 __METHOD__
6970 );
7071
71 - // If no matching notices, return NULL
72 - if ( $dbr->numRows( $res ) < 1 ) {
73 - return;
74 - }
75 -
76 - $notices = array();
7772 // Loop through result set and return attributes
78 - while ( $row = $dbr->fetchObject( $res ) ) {
 73+ foreach ( $res as $row ) {
7974 $notice = $row->not_name;
8075 $notices[$notice]['project'] = $row->not_project;
8176 $notices[$notice]['preferred'] = $row->not_preferred;
8277 $notices[$notice]['locked'] = $row->not_locked;
8378 $notices[$notice]['enabled'] = $row->not_enabled;
8479 }
 80+
 81+ // If a location is passed, also pull geotargeted campaigns that match the location
 82+ if ( $location ) {
 83+ $tables = array();
 84+ $tables[] = "cn_notices";
 85+ if ( $language ) {
 86+ $tables[] = "cn_notice_languages";
 87+ }
 88+ if ( $location ) {
 89+ $tables[] = "cn_notice_countries";
 90+ }
 91+
 92+ // Use whatever conditional arguments got passed in
 93+ $conds = array();
 94+ if ( $project ) {
 95+ $conds[] = "not_project =" . $dbr->addQuotes( $project );
 96+ }
 97+ if ( $language ) {
 98+ $conds[] = "nl_notice_id = cn_notices.not_id";
 99+ $conds[] = "nl_language =" . $dbr->addQuotes( $language );
 100+ }
 101+ if ( $location ) {
 102+ $conds[] = "not_geo = 1";
 103+ $conds[] = "nc_notice_id = cn_notices.not_id";
 104+ $conds[] = "nc_country =" . $dbr->addQuotes( $location );
 105+ }
 106+ if ( $enabled ) {
 107+ $conds[] = "not_enabled = 1";
 108+ }
 109+ if ( $preferred ) {
 110+ $conds[] = "not_preferred = 1";
 111+ }
 112+ $conds[] = "not_start <= " . $encTimestamp;
 113+ $conds[] = "not_end >= " . $encTimestamp;
 114+
 115+ // Pull db data
 116+ $res = $dbr->select(
 117+ $tables,
 118+ array(
 119+ 'not_name',
 120+ 'not_project',
 121+ 'not_locked',
 122+ 'not_enabled',
 123+ 'not_preferred'
 124+ ),
 125+ $conds,
 126+ __METHOD__
 127+ );
 128+
 129+ // Loop through result set and return attributes
 130+ foreach ( $res as $row ) {
 131+ $notice = $row->not_name;
 132+ $notices[$notice]['project'] = $row->not_project;
 133+ $notices[$notice]['preferred'] = $row->not_preferred;
 134+ $notices[$notice]['locked'] = $row->not_locked;
 135+ $notices[$notice]['enabled'] = $row->not_enabled;
 136+ }
 137+ }
85138
86139 return $notices;
87140 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r73829fixing issues from r73095, additional not_project condition is redundant so r...kaldari22:27, 27 September 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r73003adding geotargeting support to preferred campaign overrideskaldari18:37, 14 September 2010

Comments

#Comment by Catrope (talk | contribs)   17:57, 27 September 2010
+			$notices = $this->centralNoticeDB->getNotices( '', 'en', null, 1, 1, $this->location );
[...]
 	public function getNotices( $project = false, $language = false, $date = false, $enabled = true, $preferred = false, $location = false ) {

Why are you passing null while the default value is false? I know the distinction is irrelevant in this case because you're using if ( !$date ) but you might as well be consistent.

+			$encTimestamp = $dbr->addQuotes( $date );

You still need to run $date through $dbr->timestamp(). Putting that responsibility with the caller, if that's what you were doing, is not nice.

+			if ( $project ) {
+				$conds[] = "not_project =" . $dbr->addQuotes( $project );
+			}

You can just use $conds['not_project'] = $project; here, you know.

Status & tagging log