r52921 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52920‎ | r52921 | r52922 >
Date:16:33, 8 July 2009
Author:ialex
Status:ok
Tags:
Comment:
* spaces -> tabs for identation
* removed trailing whitespaces
Modified paths:
  • /trunk/extensions/CentralNotice/CentralNotice.db.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/CentralNotice.db.php
@@ -1,127 +1,128 @@
22 <?php
33
44 if ( !defined( 'MEDIAWIKI' ) ) {
5 - echo "CentralNotice extension\n";
6 - exit( 1 );
 5+ echo "CentralNotice extension\n";
 6+ exit( 1 );
77 }
88
99 class CentralNoticeDB {
1010
11 - /* Functions */
 11+ /* Functions */
1212
13 - function CentralNoticeDB() {
14 - // Register special page
15 - SpecialPage::SpecialPage( 'CentralNotice' );
 13+ function CentralNoticeDB() {
 14+ // Register special page
 15+ SpecialPage::SpecialPage( 'CentralNotice' );
1616
17 - // Internationalization
18 - wfLoadExtensionMessages( 'CentralNotice' );
19 - }
 17+ // Internationalization
 18+ wfLoadExtensionMessages( 'CentralNotice' );
 19+ }
2020
21 - /*
22 - * Return notices in the system within given constraints
23 - * Optional: return both enabled and disabled notices
24 - */
25 - public function getNotices( $project = false, $language = false , $date = false , $enabled = true, $preferred = false ) {
26 - // Database setup
27 - $dbr = wfGetDB( DB_SLAVE );
 21+ /*
 22+ * Return notices in the system within given constraints
 23+ * Optional: return both enabled and disabled notices
 24+ */
 25+ public function getNotices( $project = false, $language = false , $date = false , $enabled = true, $preferred = false ) {
 26+ // Database setup
 27+ $dbr = wfGetDB( DB_SLAVE );
2828
29 - // Use whatever conditional arguments got passed in
30 - if ( $project )
31 - $conds[] = "not_project =" . $dbr->addQuotes( $project );
32 - if ( $language )
33 - $conds[] = "not_language =" . $dbr->addQuotes( $language );
34 - if ( $preferred )
35 - $conds[] = "not_preferred = 1";
36 - if ( !$date )
37 - $date = wfTimestamp( TS_MW );
 29+ // Use whatever conditional arguments got passed in
 30+ if ( $project )
 31+ $conds[] = "not_project =" . $dbr->addQuotes( $project );
 32+ if ( $language )
 33+ $conds[] = "not_language =" . $dbr->addQuotes( $language );
 34+ if ( $preferred )
 35+ $conds[] = "not_preferred = 1";
 36+ if ( !$date )
 37+ $date = wfTimestamp( TS_MW );
3838
39 - $conds[] = ( $date ) ? "not_start <= ". $dbr->addQuotes( $date ) : "not_start <= " . $dbr->addQuotes( $dbr->timestamp( $date ) );
40 - $conds[] = ( $date ) ? "not_end >= " . $dbr->addQuotes( $date ) : "not_end >= " . $dbr->addQuotes( $dbr->timestamp( $date ) );
41 - $conds[] = ( $enabled ) ? "not_enabled = " . $dbr->addQuotes( $enabled ) : "not_enabled = " . $dbr->addQuotes( 1 );
 39+ $conds[] = ( $date ) ? "not_start <= ". $dbr->addQuotes( $date ) : "not_start <= " . $dbr->addQuotes( $dbr->timestamp( $date ) );
 40+ $conds[] = ( $date ) ? "not_end >= " . $dbr->addQuotes( $date ) : "not_end >= " . $dbr->addQuotes( $dbr->timestamp( $date ) );
 41+ $conds[] = ( $enabled ) ? "not_enabled = " . $dbr->addQuotes( $enabled ) : "not_enabled = " . $dbr->addQuotes( 1 );
4242
43 - // Pull db data
44 - $res = $dbr->select(
45 - array(
46 - 'cn_notices',
47 - ),
48 - array(
49 - 'not_name',
50 - 'not_project',
51 - 'not_language',
52 - 'not_locked',
53 - 'not_enabled',
54 - 'not_preferred',
55 - ),
56 - $conds,
57 - __METHOD__
58 - );
59 -
60 - if ( $dbr->numRows( $res ) < 1 ) {
61 - return;
62 - }
 43+ // Pull db data
 44+ $res = $dbr->select(
 45+ array(
 46+ 'cn_notices',
 47+ ),
 48+ array(
 49+ 'not_name',
 50+ 'not_project',
 51+ 'not_language',
 52+ 'not_locked',
 53+ 'not_enabled',
 54+ 'not_preferred',
 55+ ),
 56+ $conds,
 57+ __METHOD__
 58+ );
6359
64 - $notices = array();
65 - // Loop through result set and return attributes
66 - while ( $row = $dbr->fetchObject( $res ) ) {
67 - $notice = $row->not_name;
68 - $notices[$notice]['project'] = $row->not_project;
69 - $notices[$notice]['language'] = $row->not_language;
70 - $notices[$notice]['preferred'] = $row->not_preferred;
71 - $notices[$notice]['locked'] = $row->not_locked;
72 - $notices[$notice]['enabled'] = $row->not_enabled;
73 - }
74 -
75 - return $notices;
76 - }
 60+ if ( $dbr->numRows( $res ) < 1 ) {
 61+ return;
 62+ }
7763
78 - /*
79 - * Given a notice return all templates bound to it
80 - */
81 - public function selectTemplatesAssigned( $notice ) {
82 - $dbr = wfGetDB( DB_SLAVE );
 64+ $notices = array();
 65+ // Loop through result set and return attributes
 66+ while ( $row = $dbr->fetchObject( $res ) ) {
 67+ $notice = $row->not_name;
 68+ $notices[$notice]['project'] = $row->not_project;
 69+ $notices[$notice]['language'] = $row->not_language;
 70+ $notices[$notice]['preferred'] = $row->not_preferred;
 71+ $notices[$notice]['locked'] = $row->not_locked;
 72+ $notices[$notice]['enabled'] = $row->not_enabled;
 73+ }
8374
84 - // Pull templates based on join with assignments
85 - $res = $dbr->select(
86 - array(
87 - 'cn_notices',
88 - 'cn_assignments',
89 - 'cn_templates'
90 - ),
91 - array(
92 - 'cn_templates.tmp_name',
93 - 'SUM(tmp_weight) AS total_weight',
94 - ),
95 - array(
96 - 'cn_notices.not_name' => $notice,
97 - 'cn_notices.not_id = cn_assignments.not_id',
98 - 'cn_assignments.tmp_id = cn_templates.tmp_id'
99 - ),
100 - __METHOD__,
101 - array(
102 - 'GROUP BY' => 'tmp_name' )
103 - );
104 - $templateWeights = array();
105 - foreach ( $res as $row ) {
106 - $name = $row->tmp_name;
107 - $weight = intval( $row->total_weight );
108 - $templateWeights[$name] = $weight;
109 - }
110 - return $templateWeights;
111 - }
 75+ return $notices;
 76+ }
11277
113 - public function updatePreferred( $notice, $preferred ) {
114 - $dbw = wfGetDB( DB_MASTER );
115 - $dbw->begin();
 78+ /*
 79+ * Given a notice return all templates bound to it
 80+ */
 81+ public function selectTemplatesAssigned( $notice ) {
 82+ $dbr = wfGetDB( DB_SLAVE );
11683
117 - $res = $dbw->update( 'cn_notices',
118 - array(
119 - 'not_preferred' => $preferred,
120 - ),
121 - array(
122 - 'not_name' => $notice
123 - )
124 - );
125 - $dbw->commit();
126 - return $res;
127 - }
 84+ // Pull templates based on join with assignments
 85+ $res = $dbr->select(
 86+ array(
 87+ 'cn_notices',
 88+ 'cn_assignments',
 89+ 'cn_templates'
 90+ ),
 91+ array(
 92+ 'cn_templates.tmp_name',
 93+ 'SUM(tmp_weight) AS total_weight',
 94+ ),
 95+ array(
 96+ 'cn_notices.not_name' => $notice,
 97+ 'cn_notices.not_id = cn_assignments.not_id',
 98+ 'cn_assignments.tmp_id = cn_templates.tmp_id'
 99+ ),
 100+ __METHOD__,
 101+ array(
 102+ 'GROUP BY' => 'tmp_name'
 103+ )
 104+ );
 105+ $templateWeights = array();
 106+ foreach ( $res as $row ) {
 107+ $name = $row->tmp_name;
 108+ $weight = intval( $row->total_weight );
 109+ $templateWeights[$name] = $weight;
 110+ }
 111+ return $templateWeights;
 112+ }
 113+
 114+ public function updatePreferred( $notice, $preferred ) {
 115+ $dbw = wfGetDB( DB_MASTER );
 116+ $dbw->begin();
 117+
 118+ $res = $dbw->update( 'cn_notices',
 119+ array(
 120+ 'not_preferred' => $preferred,
 121+ ),
 122+ array(
 123+ 'not_name' => $notice
 124+ )
 125+ );
 126+ $dbw->commit();
 127+ return $res;
 128+ }
128129 }

Status & tagging log