00001 <?php
00002
00010 class RandomPage extends SpecialPage {
00011 private $namespaces;
00012 protected $isRedir = false;
00013 protected $extra = array();
00014
00015 public function __construct( $name = 'Randompage' ){
00016 global $wgContentNamespaces;
00017 $this->namespaces = $wgContentNamespaces;
00018 parent::__construct( $name );
00019 }
00020
00021 public function getNamespaces() {
00022 return $this->namespaces;
00023 }
00024
00025 public function setNamespace ( $ns ) {
00026 if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
00027 $this->namespaces = array( $ns );
00028 }
00029
00030
00031 public function isRedirect(){
00032 return $this->isRedir;
00033 }
00034
00035 public function execute( $par ) {
00036 global $wgOut, $wgContLang;
00037
00038 if ($par) {
00039 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
00040 }
00041
00042 $title = $this->getRandomTitle();
00043
00044 if( is_null( $title ) ) {
00045 $this->setHeaders();
00046 $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages',
00047 $this->getNsList(), count( $this->namespaces ) );
00048 return;
00049 }
00050
00051 $query = $this->isRedirect() ? 'redirect=no' : '';
00052 $wgOut->redirect( $title->getFullUrl( $query ) );
00053 }
00054
00060 private function getNsList() {
00061 global $wgContLang;
00062 $nsNames = array();
00063 foreach( $this->namespaces as $n ) {
00064 if( $n === NS_MAIN )
00065 $nsNames[] = wfMsgForContent( 'blanknamespace' );
00066 else
00067 $nsNames[] = $wgContLang->getNsText( $n );
00068 }
00069 return $wgContLang->commaList( $nsNames );
00070 }
00071
00072
00077 public function getRandomTitle() {
00078 $randstr = wfRandom();
00079 $title = null;
00080 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) {
00081 return $title;
00082 }
00083 $row = $this->selectRandomPageFromDB( $randstr );
00084
00085
00086
00087
00088
00089
00090
00091
00092 if( !$row )
00093 $row = $this->selectRandomPageFromDB( "0" );
00094
00095 if( $row )
00096 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
00097 else
00098 return null;
00099 }
00100
00101 private function selectRandomPageFromDB( $randstr ) {
00102 global $wgExtraRandompageSQL;
00103 $dbr = wfGetDB( DB_SLAVE );
00104
00105 $use_index = $dbr->useIndexClause( 'page_random' );
00106 $page = $dbr->tableName( 'page' );
00107
00108 $ns = implode( ",", $this->namespaces );
00109 $redirect = $this->isRedirect() ? 1 : 0;
00110
00111 if ( $wgExtraRandompageSQL ) {
00112 $this->extra[] = $wgExtraRandompageSQL;
00113 }
00114 if ( $this->addExtraSQL() ) {
00115 $this->extra[] = $this->addExtraSQL();
00116 }
00117 $extra = '';
00118 if ( $this->extra ) {
00119 $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')';
00120 }
00121 $sql = "SELECT page_title, page_namespace
00122 FROM $page $use_index
00123 WHERE page_namespace IN ( $ns )
00124 AND page_is_redirect = $redirect
00125 AND page_random >= $randstr
00126 $extra
00127 ORDER BY page_random";
00128
00129 $sql = $dbr->limitResult( $sql, 1, 0 );
00130 $res = $dbr->query( $sql, __METHOD__ );
00131 return $dbr->fetchObject( $res );
00132 }
00133
00134
00135
00136
00137
00138 public function addExtraSQL() {
00139 return '';
00140 }
00141 }