Index: trunk/extensions/RandomInCategory/RandomInCategory.body.php |
— | — | @@ -18,14 +18,12 @@ |
19 | 19 | |
20 | 20 | function execute( $par ) { |
21 | 21 | global $wgOut, $wgRequest; |
22 | | - wfLoadExtensionMessages( 'RandomInCategory' ); |
23 | 22 | |
24 | 23 | $this->setHeaders(); |
25 | 24 | if( is_null( $par ) ) { |
26 | 25 | if ( $requestCategory = $wgRequest->getVal( 'category' ) ) { |
27 | 26 | $par = $requestCategory; |
28 | | - } |
29 | | - else { |
| 27 | + } else { |
30 | 28 | $wgOut->addHTML( $this->getForm() ); |
31 | 29 | return; |
32 | 30 | } |
— | — | @@ -52,6 +50,7 @@ |
53 | 51 | public function getCategory ( ) { |
54 | 52 | return $this->namespace; |
55 | 53 | } |
| 54 | + |
56 | 55 | public function setCategory ( $cat ) { |
57 | 56 | $category = Title::makeTitleSafe( NS_CATEGORY, $cat ); |
58 | 57 | //Invalid title |
— | — | @@ -70,38 +69,46 @@ |
71 | 70 | $randstr = wfRandom(); |
72 | 71 | $row = $this->selectRandomPageFromDB( $randstr ); |
73 | 72 | |
74 | | - if( !$row ) |
| 73 | + if( !$row ) { |
75 | 74 | $row = $this->selectRandomPageFromDB( "0" ); |
| 75 | + } |
76 | 76 | |
77 | | - if( $row ) |
| 77 | + if( $row ) { |
78 | 78 | return Title::newFromText( $row->page_title, $row->page_namespace ); |
79 | | - else |
| 79 | + } else { |
80 | 80 | return null; |
| 81 | + } |
81 | 82 | } |
82 | 83 | |
83 | 84 | private function selectRandomPageFromDB ( $randstr ) { |
84 | 85 | global $wgExtraRandompageSQL, $wgOut; |
85 | | - $fname = 'RandomPageInCategory::selectRandomPageFromDB'; |
86 | 86 | |
87 | 87 | $dbr = wfGetDB( DB_SLAVE ); |
88 | 88 | |
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 | + } |
93 | 92 | |
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 | + ); |
103 | 112 | |
104 | | - $sql = $dbr->limitResult( $sql, 1, 0 ); |
105 | | - $res = $dbr->query( $sql, $fname ); |
106 | 113 | return $dbr->fetchObject( $res ); |
107 | 114 | } |
108 | 115 | |