http://www.mediawiki.org/wiki/Special:Code/MediaWiki/71828

Revision: 71828
Author:   kaldari
Date:     2010-08-28 00:15:01 +0000 (Sat, 28 Aug 2010)

Log Message:
-----------
adding noticeExists() test, moving updatePreferred(), removing unneccessary 
transactions

Modified Paths:
--------------
    trunk/extensions/CentralNotice/CentralNotice.db.php
    trunk/extensions/CentralNotice/SpecialCentralNotice.php

Modified: trunk/extensions/CentralNotice/CentralNotice.db.php
===================================================================
--- trunk/extensions/CentralNotice/CentralNotice.db.php 2010-08-27 23:51:46 UTC 
(rev 71827)
+++ trunk/extensions/CentralNotice/CentralNotice.db.php 2010-08-28 00:15:01 UTC 
(rev 71828)
@@ -118,20 +118,5 @@
                }
                return $templates;
        }
-
-       public function updatePreferred( $notice, $preferred ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->begin();
-
-               $res = $dbw->update( 'cn_notices',
-                       array(
-                               'not_preferred' => $preferred,
-                       ),
-                       array(
-                               'not_name' => $notice
-                       )
-               );
-               $dbw->commit();
-               return $res;
-       }
+       
 }

Modified: trunk/extensions/CentralNotice/SpecialCentralNotice.php
===================================================================
--- trunk/extensions/CentralNotice/SpecialCentralNotice.php     2010-08-27 
23:51:46 UTC (rev 71827)
+++ trunk/extensions/CentralNotice/SpecialCentralNotice.php     2010-08-28 
00:15:01 UTC (rev 71828)
@@ -228,19 +228,6 @@
                $wgOut->addHTML( Xml::closeElement( 'div' ) );
        }
 
-       /**
-        * Update the enabled/disabled state of a campaign
-        */
-       private function updateEnabled( $notice, $state ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->begin();
-               $res = $dbw->update( 'cn_notices',
-                       array( 'not_enabled' => $state ),
-                       array( 'not_name' => $notice )
-               );
-               $dbw->commit();
-       }
-
        public static function printHeader() {
                global $wgOut, $wgTitle, $wgUser;
                $sk = $wgUser->getSkin();
@@ -1000,9 +987,7 @@
        function addNotice( $noticeName, $enabled, $start, $project_name, 
$project_languages ) {
                global $wgOut;
 
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'cn_notices', 'not_name', array( 
'not_name' => $noticeName ) );
-               if ( $dbr->numRows( $res ) > 0 ) {
+               if ( $this->noticeExists( $noticeName ) ) {
                        $wgOut->wrapWikiMsg( "<div 
class='cn-error'>\n$1\n</div>", 'centralnotice-notice-exists' );
                        return;
                } elseif ( empty( $project_languages ) ) {
@@ -1116,7 +1101,7 @@
                 if ( $row ) {
                        return $row->not_id;
                 } else {
-                       return;
+                       return null;
                 }
        }
 
@@ -1171,8 +1156,7 @@
                }
 
                // Invalid campaign name
-               $row = $dbr->selectRow( 'cn_notices', 'not_name', array( 
'not_name' => $noticeName ) );
-               if ( !$row ) {
+               if ( !$this->noticeExists( $noticeName ) ) {
                        $wgOut->wrapWikiMsg( "<div 
class='cn-error'>\n$1\n</div>", 'centralnotice-notice-doesnt-exist' );
                        return;
                }
@@ -1191,23 +1175,54 @@
                );
        }
 
+       /**
+        * Update the enabled/disabled state of a campaign
+        */
+       private function updateEnabled( $noticeName, $isEnabled ) {
+               global $wgOut;
+               
+               if ( !$this->noticeExists( $noticeName ) ) {
+                       $wgOut->wrapWikiMsg( "<div 
class='cn-error'>\n$1\n</div>", 'centralnotice-doesnt-exist' );
+               } else {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $res = $dbw->update( 'cn_notices',
+                               array( 'not_enabled' => $isEnabled ),
+                               array( 'not_name' => $noticeName )
+                       );
+               }
+       }
+
+       /**
+        * Update the preferred/not preferred state of a campaign
+        */
+       function updatePreferred( $noticeName, $isPreferred ) {
+               global $wgOut;
+               
+               if ( !$this->noticeExists( $noticeName ) ) {
+                       $wgOut->wrapWikiMsg( "<div 
class='cn-error'>\n$1\n</div>", 'centralnotice-doesnt-exist' );
+               } else {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $res = $dbw->update( 'cn_notices',
+                               array( 'not_preferred' => $isPreferred ),
+                               array( 'not_name' => $noticeName )
+                       );
+               }
+       }
+
+       /**
+        * Update the locked/unlocked state of a campaign
+        */
        function updateLock( $noticeName, $isLocked ) {
                global $wgOut;
 
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'cn_notices', 'not_name',
-                       array( 'not_name' => $noticeName )
-               );
-               if ( $dbr->numRows( $res ) < 1 ) {
+               if ( !$this->noticeExists( $noticeName ) ) {
                        $wgOut->wrapWikiMsg( "<div 
class='cn-error'>\n$1\n</div>", 'centralnotice-doesnt-exist' );
                } else {
                        $dbw = wfGetDB( DB_MASTER );
-                       $dbw->begin();
                        $res = $dbw->update( 'cn_notices',
                                array( 'not_locked' => $isLocked ),
                                array( 'not_name' => $noticeName )
                        );
-                       $dbw->commit();
                }
        }
 
@@ -1296,14 +1311,12 @@
 
        function updateProjectName( $notice, $projectName ) {
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->begin();
                $res = $dbw->update( 'cn_notices',
                        array ( 'not_project' => $projectName ),
                        array(
                                'not_name' => $notice
                        )
                );
-               $dbw->commit();
        }
 
        function updateProjectLanguages( $notice, $newLanguages ) {
@@ -1335,6 +1348,17 @@
                
                $dbw->commit();
        }
+       
+       public static function noticeExists( $noticeName ) {
+                $dbr = wfGetDB( DB_SLAVE );
+                $eNoticeName = htmlspecialchars( $noticeName );
+                $row = $dbr->selectRow( 'cn_notices', 'not_name', array( 
'not_name' => $eNoticeName ) );
+                if ( $row ) {
+                       return true;
+                } else {
+                       return false;
+                }
+       }
 
        public static function dropDownList( $text, $values ) {
                $dropDown = "* {$text}\n";



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to