Re-write raw SQL
authorSam Reed <reedy@users.mediawiki.org>
Mon, 31 Jan 2011 23:06:36 +0000 (23:06 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Mon, 31 Jan 2011 23:06:36 +0000 (23:06 +0000)
Wrap lines, add missing braces

includes/specials/SpecialRandompage.php

index 6299f38..6cd9d64 100644 (file)
@@ -43,7 +43,9 @@ class RandomPage extends SpecialPage {
        }
 
        public function setNamespace ( $ns ) {
-               if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
+               if( !$ns || $ns < NS_MAIN ) {
+                       $ns = NS_MAIN;
+               }
                $this->namespaces = array( $ns );
        }
 
@@ -83,10 +85,11 @@ class RandomPage extends SpecialPage {
                global $wgContLang;
                $nsNames = array();
                foreach( $this->namespaces as $n ) {
-                       if( $n === NS_MAIN )
+                       if( $n === NS_MAIN ) {
                                $nsNames[] = wfMsgForContent( 'blanknamespace' );
-                       else
+                       } else {
                                $nsNames[] = $wgContLang->getNsText( $n );
+                       }
                }
                return $wgContLang->commaList( $nsNames );
        }
@@ -99,7 +102,8 @@ class RandomPage extends SpecialPage {
        public function getRandomTitle() {
                $randstr = wfRandom();
                $title = null;
-               if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) {
+               if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces,
+                       &$this->extra, &$title ) ) ) {
                        return $title;
                }
                $row = $this->selectRandomPageFromDB( $randstr );
@@ -111,23 +115,21 @@ class RandomPage extends SpecialPage {
                 * any more bias than what the page_random scheme
                 * causes anyway.  Trust me, I'm a mathematician. :)
                 */
-               if( !$row )
+               if( !$row ) {
                        $row = $this->selectRandomPageFromDB( "0" );
+               }
 
-               if( $row )
+               if( $row ) {
                        return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
-               else
+               } else {
                        return null;
+               }
        }
 
        private function selectRandomPageFromDB( $randstr ) {
                global $wgExtraRandompageSQL;
                $dbr = wfGetDB( DB_SLAVE );
 
-               $use_index = $dbr->useIndexClause( 'page_random' );
-               $page = $dbr->tableName( 'page' );
-
-               $ns = implode( ",", $this->namespaces );
                $redirect = $this->isRedirect() ? 1 : 0;
                
                if ( $wgExtraRandompageSQL ) {
@@ -136,20 +138,22 @@ class RandomPage extends SpecialPage {
                if ( $this->addExtraSQL() ) {
                        $this->extra[] = $this->addExtraSQL();
                }
-               $extra = '';
-               if ( $this->extra ) {
-                       $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')';
-               }
-               $sql = "SELECT page_title, page_namespace
-                       FROM $page $use_index
-                       WHERE page_namespace IN ( $ns )
-                       AND page_is_redirect = $redirect
-                       AND page_random >= $randstr
-                       $extra
-                       ORDER BY page_random";
-
-               $sql = $dbr->limitResult( $sql, 1, 0 );
-               $res = $dbr->query( $sql, __METHOD__ );
+
+               $res = $dbr->doQuery(
+                       'page',
+                       array( 'page_title', 'page_namespace' ),
+                       array_merge( array(
+                               'page_namespace' => $this->namespaces,
+                               'page_is_redirect' => $redirect,
+                               'page_random >= ' . $randstr
+                       ), $this->extra ),
+                       __METHOD__,
+                       array(
+                               'ORDER BY' => 'page_random',
+                               'USE INDEX' => 'page_random'
+                       )
+               );
+
                return $dbr->fetchObject( $res );
        }