r81282 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81281‎ | r81282 | r81283 >
Date:23:18, 31 January 2011
Author:reedy
Status:deferred
Tags:
Comment:
Rewrite raw sql for time being, per dantman, SpecialRandompage needs to become a querypage, so t his can subclass it and do magic with it instead!
Modified paths:
  • /trunk/extensions/RandomInCategory/RandomInCategory.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RandomInCategory/RandomInCategory.body.php
@@ -18,14 +18,12 @@
1919
2020 function execute( $par ) {
2121 global $wgOut, $wgRequest;
22 - wfLoadExtensionMessages( 'RandomInCategory' );
2322
2423 $this->setHeaders();
2524 if( is_null( $par ) ) {
2625 if ( $requestCategory = $wgRequest->getVal( 'category' ) ) {
2726 $par = $requestCategory;
28 - }
29 - else {
 27+ } else {
3028 $wgOut->addHTML( $this->getForm() );
3129 return;
3230 }
@@ -52,6 +50,7 @@
5351 public function getCategory ( ) {
5452 return $this->namespace;
5553 }
 54+
5655 public function setCategory ( $cat ) {
5756 $category = Title::makeTitleSafe( NS_CATEGORY, $cat );
5857 //Invalid title
@@ -70,38 +69,46 @@
7170 $randstr = wfRandom();
7271 $row = $this->selectRandomPageFromDB( $randstr );
7372
74 - if( !$row )
 73+ if( !$row ) {
7574 $row = $this->selectRandomPageFromDB( "0" );
 75+ }
7676
77 - if( $row )
 77+ if( $row ) {
7878 return Title::newFromText( $row->page_title, $row->page_namespace );
79 - else
 79+ } else {
8080 return null;
 81+ }
8182 }
8283
8384 private function selectRandomPageFromDB ( $randstr ) {
8485 global $wgExtraRandompageSQL, $wgOut;
85 - $fname = 'RandomPageInCategory::selectRandomPageFromDB';
8686
8787 $dbr = wfGetDB( DB_SLAVE );
8888
89 - $use_index = $dbr->useIndexClause( 'page_random' );
90 - $page = $dbr->tableName( 'page' );
91 - $categorylinks = $dbr->tableName( 'categorylinks' );
92 - $category = $dbr->addQuotes( $this->category );
 89+ if ( $wgExtraRandompageSQL ) {
 90+ $this->extra[] = $wgExtraRandompageSQL;
 91+ }
9392
94 - $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
95 - $sql = "SELECT page_namespace, page_title
96 - FROM $page $use_index JOIN $categorylinks ON page_id = cl_from
97 - WHERE page_is_redirect = 0
98 - AND page_random >= $randstr
99 - AND page_namespace != " . NS_CATEGORY . "
100 - AND cl_to = $category
101 - $extra
102 - ORDER BY page_random";
 93+ $res = $dbr->select(
 94+ array( 'page', 'categorylinks' ),
 95+ array( 'page_title', 'page_namespace' ),
 96+ array_merge( array(
 97+ 'page_namespace' != NS_CATEGORY,
 98+ 'cl_to' => $category,
 99+ 'page_random >= ' . $randstr
 100+ ), $this->extra ),
 101+ __METHOD__,
 102+ array(
 103+ 'ORDER BY' => 'page_random',
 104+ 'USE INDEX' => 'page_random'
 105+ ),
 106+ array(
 107+ 'categorylinks' => array(
 108+ 'JOIN' => array( 'page_id=cl_from' )
 109+ )
 110+ )
 111+ );
103112
104 - $sql = $dbr->limitResult( $sql, 1, 0 );
105 - $res = $dbr->query( $sql, $fname );
106113 return $dbr->fetchObject( $res );
107114 }
108115

Status & tagging log