Index: trunk/phase3/includes/specials/SpecialRandompage.php |
— | — | @@ -43,7 +43,9 @@ |
44 | 44 | } |
45 | 45 | |
46 | 46 | public function setNamespace ( $ns ) { |
47 | | - if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN; |
| 47 | + if( !$ns || $ns < NS_MAIN ) { |
| 48 | + $ns = NS_MAIN; |
| 49 | + } |
48 | 50 | $this->namespaces = array( $ns ); |
49 | 51 | } |
50 | 52 | |
— | — | @@ -83,10 +85,11 @@ |
84 | 86 | global $wgContLang; |
85 | 87 | $nsNames = array(); |
86 | 88 | foreach( $this->namespaces as $n ) { |
87 | | - if( $n === NS_MAIN ) |
| 89 | + if( $n === NS_MAIN ) { |
88 | 90 | $nsNames[] = wfMsgForContent( 'blanknamespace' ); |
89 | | - else |
| 91 | + } else { |
90 | 92 | $nsNames[] = $wgContLang->getNsText( $n ); |
| 93 | + } |
91 | 94 | } |
92 | 95 | return $wgContLang->commaList( $nsNames ); |
93 | 96 | } |
— | — | @@ -99,7 +102,8 @@ |
100 | 103 | public function getRandomTitle() { |
101 | 104 | $randstr = wfRandom(); |
102 | 105 | $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 ) ) ) { |
104 | 108 | return $title; |
105 | 109 | } |
106 | 110 | $row = $this->selectRandomPageFromDB( $randstr ); |
— | — | @@ -111,23 +115,21 @@ |
112 | 116 | * any more bias than what the page_random scheme |
113 | 117 | * causes anyway. Trust me, I'm a mathematician. :) |
114 | 118 | */ |
115 | | - if( !$row ) |
| 119 | + if( !$row ) { |
116 | 120 | $row = $this->selectRandomPageFromDB( "0" ); |
| 121 | + } |
117 | 122 | |
118 | | - if( $row ) |
| 123 | + if( $row ) { |
119 | 124 | return Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
120 | | - else |
| 125 | + } else { |
121 | 126 | return null; |
| 127 | + } |
122 | 128 | } |
123 | 129 | |
124 | 130 | private function selectRandomPageFromDB( $randstr ) { |
125 | 131 | global $wgExtraRandompageSQL; |
126 | 132 | $dbr = wfGetDB( DB_SLAVE ); |
127 | 133 | |
128 | | - $use_index = $dbr->useIndexClause( 'page_random' ); |
129 | | - $page = $dbr->tableName( 'page' ); |
130 | | - |
131 | | - $ns = implode( ",", $this->namespaces ); |
132 | 134 | $redirect = $this->isRedirect() ? 1 : 0; |
133 | 135 | |
134 | 136 | if ( $wgExtraRandompageSQL ) { |
— | — | @@ -136,20 +138,22 @@ |
137 | 139 | if ( $this->addExtraSQL() ) { |
138 | 140 | $this->extra[] = $this->addExtraSQL(); |
139 | 141 | } |
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"; |
151 | 142 | |
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 | + |
154 | 158 | return $dbr->fetchObject( $res ); |
155 | 159 | } |
156 | 160 | |