SpecialRandompage.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Special page to direct the user to a random page
  4. *
  5. * @ingroup SpecialPage
  6. * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
  7. * @license GNU General Public Licence 2.0 or later
  8. */
  9. class RandomPage extends SpecialPage {
  10. private $namespaces; // namespaces to select pages from
  11. function __construct( $name = 'Randompage' ){
  12. global $wgContentNamespaces;
  13. $this->namespaces = $wgContentNamespaces;
  14. parent::__construct( $name );
  15. }
  16. public function getNamespaces() {
  17. return $this->namespaces;
  18. }
  19. public function setNamespace ( $ns ) {
  20. if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
  21. $this->namespaces = array( $ns );
  22. }
  23. // select redirects instead of normal pages?
  24. // Overriden by SpecialRandomredirect
  25. public function isRedirect(){
  26. return false;
  27. }
  28. public function execute( $par ) {
  29. global $wgOut, $wgContLang;
  30. if ($par)
  31. $this->setNamespace( $wgContLang->getNsIndex( $par ) );
  32. $title = $this->getRandomTitle();
  33. if( is_null( $title ) ) {
  34. $this->setHeaders();
  35. $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages', $wgContLang->getNsText( $this->namespace ) );
  36. return;
  37. }
  38. $query = $this->isRedirect() ? 'redirect=no' : '';
  39. $wgOut->redirect( $title->getFullUrl( $query ) );
  40. }
  41. /**
  42. * Choose a random title.
  43. * @return Title object (or null if nothing to choose from)
  44. */
  45. public function getRandomTitle() {
  46. $randstr = wfRandom();
  47. $row = $this->selectRandomPageFromDB( $randstr );
  48. /* If we picked a value that was higher than any in
  49. * the DB, wrap around and select the page with the
  50. * lowest value instead! One might think this would
  51. * skew the distribution, but in fact it won't cause
  52. * any more bias than what the page_random scheme
  53. * causes anyway. Trust me, I'm a mathematician. :)
  54. */
  55. if( !$row )
  56. $row = $this->selectRandomPageFromDB( "0" );
  57. if( $row )
  58. return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
  59. else
  60. return null;
  61. }
  62. private function selectRandomPageFromDB( $randstr ) {
  63. global $wgExtraRandompageSQL;
  64. $fname = 'RandomPage::selectRandomPageFromDB';
  65. $dbr = wfGetDB( DB_SLAVE );
  66. $use_index = $dbr->useIndexClause( 'page_random' );
  67. $page = $dbr->tableName( 'page' );
  68. $ns = implode( ",", $this->namespaces );
  69. $redirect = $this->isRedirect() ? 1 : 0;
  70. $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
  71. $sql = "SELECT page_title, page_namespace
  72. FROM $page $use_index
  73. WHERE page_namespace IN ( $ns )
  74. AND page_is_redirect = $redirect
  75. AND page_random >= $randstr
  76. $extra
  77. ORDER BY page_random";
  78. $sql = $dbr->limitResult( $sql, 1, 0 );
  79. $res = $dbr->query( $sql, $fname );
  80. return $dbr->fetchObject( $res );
  81. }
  82. }