r81279 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81278‎ | r81279 | r81280 >
Date:23:06, 31 January 2011
Author:reedy
Status:deferred (Comments)
Tags:
Comment:
Re-write raw SQL

Wrap lines, add missing braces
Modified paths:
  • /trunk/phase3/includes/specials/SpecialRandompage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialRandompage.php
@@ -43,7 +43,9 @@
4444 }
4545
4646 public function setNamespace ( $ns ) {
47 - if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
 47+ if( !$ns || $ns < NS_MAIN ) {
 48+ $ns = NS_MAIN;
 49+ }
4850 $this->namespaces = array( $ns );
4951 }
5052
@@ -83,10 +85,11 @@
8486 global $wgContLang;
8587 $nsNames = array();
8688 foreach( $this->namespaces as $n ) {
87 - if( $n === NS_MAIN )
 89+ if( $n === NS_MAIN ) {
8890 $nsNames[] = wfMsgForContent( 'blanknamespace' );
89 - else
 91+ } else {
9092 $nsNames[] = $wgContLang->getNsText( $n );
 93+ }
9194 }
9295 return $wgContLang->commaList( $nsNames );
9396 }
@@ -99,7 +102,8 @@
100103 public function getRandomTitle() {
101104 $randstr = wfRandom();
102105 $title = null;
103 - if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) {
 106+ if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces,
 107+ &$this->extra, &$title ) ) ) {
104108 return $title;
105109 }
106110 $row = $this->selectRandomPageFromDB( $randstr );
@@ -111,23 +115,21 @@
112116 * any more bias than what the page_random scheme
113117 * causes anyway. Trust me, I'm a mathematician. :)
114118 */
115 - if( !$row )
 119+ if( !$row ) {
116120 $row = $this->selectRandomPageFromDB( "0" );
 121+ }
117122
118 - if( $row )
 123+ if( $row ) {
119124 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
120 - else
 125+ } else {
121126 return null;
 127+ }
122128 }
123129
124130 private function selectRandomPageFromDB( $randstr ) {
125131 global $wgExtraRandompageSQL;
126132 $dbr = wfGetDB( DB_SLAVE );
127133
128 - $use_index = $dbr->useIndexClause( 'page_random' );
129 - $page = $dbr->tableName( 'page' );
130 -
131 - $ns = implode( ",", $this->namespaces );
132134 $redirect = $this->isRedirect() ? 1 : 0;
133135
134136 if ( $wgExtraRandompageSQL ) {
@@ -136,20 +138,22 @@
137139 if ( $this->addExtraSQL() ) {
138140 $this->extra[] = $this->addExtraSQL();
139141 }
140 - $extra = '';
141 - if ( $this->extra ) {
142 - $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')';
143 - }
144 - $sql = "SELECT page_title, page_namespace
145 - FROM $page $use_index
146 - WHERE page_namespace IN ( $ns )
147 - AND page_is_redirect = $redirect
148 - AND page_random >= $randstr
149 - $extra
150 - ORDER BY page_random";
151142
152 - $sql = $dbr->limitResult( $sql, 1, 0 );
153 - $res = $dbr->query( $sql, __METHOD__ );
 143+ $res = $dbr->doQuery(
 144+ 'page',
 145+ array( 'page_title', 'page_namespace' ),
 146+ array_merge( array(
 147+ 'page_namespace' => $this->namespaces,
 148+ 'page_is_redirect' => $redirect,
 149+ 'page_random >= ' . $randstr
 150+ ), $this->extra ),
 151+ __METHOD__,
 152+ array(
 153+ 'ORDER BY' => 'page_random',
 154+ 'USE INDEX' => 'page_random'
 155+ )
 156+ );
 157+
154158 return $dbr->fetchObject( $res );
155159 }
156160

Follow-up revisions

RevisionCommit summaryAuthorDate
r81280Followup r81279, forgot to press save!reedy23:13, 31 January 2011

Comments

#Comment by 😂 (talk | contribs)   23:10, 31 January 2011

Is there a reason you introduced doQuery() here?

#Comment by Reedy (talk | contribs)   23:14, 31 January 2011

Accidental forgot to press save again. Fixed in next commit

Status & tagging log