Index: trunk/extensions/CentralNotice/CentralNotice.db.php |
— | — | @@ -1,127 +1,128 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | if ( !defined( 'MEDIAWIKI' ) ) { |
5 | | - echo "CentralNotice extension\n"; |
6 | | - exit( 1 ); |
| 5 | + echo "CentralNotice extension\n"; |
| 6 | + exit( 1 ); |
7 | 7 | } |
8 | 8 | |
9 | 9 | class CentralNoticeDB { |
10 | 10 | |
11 | | - /* Functions */ |
| 11 | + /* Functions */ |
12 | 12 | |
13 | | - function CentralNoticeDB() { |
14 | | - // Register special page |
15 | | - SpecialPage::SpecialPage( 'CentralNotice' ); |
| 13 | + function CentralNoticeDB() { |
| 14 | + // Register special page |
| 15 | + SpecialPage::SpecialPage( 'CentralNotice' ); |
16 | 16 | |
17 | | - // Internationalization |
18 | | - wfLoadExtensionMessages( 'CentralNotice' ); |
19 | | - } |
| 17 | + // Internationalization |
| 18 | + wfLoadExtensionMessages( 'CentralNotice' ); |
| 19 | + } |
20 | 20 | |
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 ); |
28 | 28 | |
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 ); |
38 | 38 | |
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 ); |
42 | 42 | |
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 | + ); |
63 | 59 | |
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 | + } |
77 | 63 | |
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 | + } |
83 | 74 | |
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 | + } |
112 | 77 | |
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 ); |
116 | 83 | |
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 | + } |
128 | 129 | } |