X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FSpecialRandompage.php;h=1ba8e98177448e2694f8a87d452b83f95539f6e1;hb=d470d9800dec83a0e625cb5cbd89be464678716b;hp=051c9f94d7d4c96d57b23cb48163684e009ab6fc;hpb=a5d5a6b5b8f1dde97bd9797834c1fe1f6065aa13;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SpecialRandompage.php b/includes/SpecialRandompage.php index 051c9f94d7..1ba8e98177 100644 --- a/includes/SpecialRandompage.php +++ b/includes/SpecialRandompage.php @@ -9,59 +9,68 @@ */ /** - * Main execution point - * @param $par Namespace to select the page from + * Special page to direct the user to a random page + * + * @addtogroup SpecialPage */ -function wfSpecialRandompage( $par = null ) { - global $wgOut, $wgContLang; - - $rnd = new RandomPage(); - $rnd->setNamespace( $wgContLang->getNsIndex( $par ) ); - $rnd->setRedirect( false ); - - $title = $rnd->getRandomTitle(); +class RandomPage extends SpecialPage { + private $namespace = NS_MAIN; // namespace to select pages from - if( is_null( $title ) ) { - $wgOut->addWikiText( wfMsg( 'randompage-nopages' ) ); - return; + function __construct( $name = 'Randompage' ){ + parent::__construct( $name ); } - $wgOut->reportTime(); - $wgOut->redirect( $title->getFullUrl() ); -} - - -class RandomPage { - private $namespace = NS_MAIN; // namespace to select pages from - private $redirect = false; // select redirects instead of normal pages? - - public function getNamespace ( ) { + public function getNamespace() { return $this->namespace; } + public function setNamespace ( $ns ) { if( $ns < NS_MAIN ) $ns = NS_MAIN; $this->namespace = $ns; } - public function getRedirect ( ) { - return $this->redirect; + + // select redirects instead of normal pages? + // Overriden by SpecialRandomredirect + public function isRedirect(){ + return false; } - public function setRedirect ( $redirect ) { - $this->redirect = $redirect; + + public function execute( $par ) { + global $wgOut, $wgContLang; + + if ($par) + $this->setNamespace( $wgContLang->getNsIndex( $par ) ); + + $title = $this->getRandomTitle(); + + if( is_null( $title ) ) { + $this->setHeaders(); + $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages' ); + return; + } + + $query = $this->isRedirect() ? 'redirect=no' : ''; + $wgOut->redirect( $title->getFullUrl( $query ) ); } + /** * Choose a random title. * @return Title object (or null if nothing to choose from) */ - public function getRandomTitle ( ) { + public function getRandomTitle() { $randstr = wfRandom(); $row = $this->selectRandomPageFromDB( $randstr ); - if( !$row ) { - // Try again with a normalized value - $randstr = wfRandom( $this->getMaxPageRandom() ); - $row = $this->selectRandomPageFromDB( $randstr ); - } + /* If we picked a value that was higher than any in + * the DB, wrap around and select the page with the + * lowest value instead! One might think this would + * skew the distribution, but in fact it won't cause + * any more bias than what the page_random scheme + * causes anyway. Trust me, I'm a mathematician. :) + */ + if( !$row ) + $row = $this->selectRandomPageFromDB( "0" ); if( $row ) return Title::makeTitleSafe( $this->namespace, $row->page_title ); @@ -69,54 +78,29 @@ class RandomPage { return null; } - private function selectRandomPageFromDB ( $randstr ) { + private function selectRandomPageFromDB( $randstr ) { global $wgExtraRandompageSQL; $fname = 'RandomPage::selectRandomPageFromDB'; $dbr = wfGetDB( DB_SLAVE ); - $from = $this->getSQLFrom( $dbr ); - $where = $this->getSQLWhere( $dbr ); + $use_index = $dbr->useIndexClause( 'page_random' ); + $page = $dbr->tableName( 'page' ); - $sql = "SELECT page_title FROM $from - WHERE $where AND page_random > $randstr + $ns = (int) $this->namespace; + $redirect = $this->isRedirect() ? 1 : 0; + + $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : ""; + $sql = "SELECT page_title + FROM $page $use_index + WHERE page_namespace = $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, $fname ); return $dbr->fetchObject( $res ); } - - private function getMaxPageRandom () { - $fname = 'RandomPage::getMaxPageRandom'; - - $dbr = wfGetDB( DB_SLAVE ); - - $from = $this->getSQLFrom( $dbr ); - $where = $this->getSQLWhere( $dbr ); - - $sql = "SELECT MAX(page_random) AS max FROM $from WHERE $where"; - - $sql = $dbr->limitResult( $sql, 1, 0 ); - $res = $dbr->query( $sql, $fname ); - $row = $dbr->fetchObject( $res ); - - return $row ? $row->max : 0; - } - - private function getSQLFrom ( $dbr ) { - $use_index = $dbr->useIndexClause( 'page_random' ); - $page = $dbr->tableName( 'page' ); - return "$page $use_index"; - } - - private function getSQLWhere ( $dbr ) { - global $wgExtraRandompageSQL; - $ns = (int) $this->namespace; - $redirect = $this->redirect ? 1 : 0; - $extra = $wgExtraRandompageSQL ? " AND ($wgExtraRandompageSQL)" : ""; - return "page_namespace = $ns AND page_is_redirect = $redirect" . $extra; - } } - -?>