4 * Special page to direct the user to a random page in specified category
6 * @addtogroup SpecialPage
7 * @author VasilievVV <vasilvv@gmail.com>, based on SpecialRandompage.php code
8 * @license GNU General Public Licence 2.0 or later
12 * Main execution point
13 * @param $par Category to select the page from
15 function wfSpecialRandomincategory( $par = null ) {
16 global $wgOut, $wgRequest;
18 if( is_null( $par ) ) {
19 if ( $requestCategory = $wgRequest->getVal( 'category' ) ) {
20 $par = $requestCategory;
23 $wgOut->addHTML( RandomPageInCategory
::getForm() );
28 $rnd = new RandomPageInCategory();
29 $rnd->setCategory( $par );
31 $title = $rnd->getRandomTitle();
33 if( is_null( $title ) ) {
34 $wgOut->addWikiText( wfMsg( 'randomincategory-nocategory', $par ) );
35 $wgOut->addHTML( RandomPageInCategory
::getForm( $par ) );
40 $wgOut->redirect( $title->getFullUrl() );
45 * Special page to direct the user to a random page in specified category
47 * @addtogroup SpecialPage
49 class RandomPageInCategory
{
50 private $category = null;
52 public function getCategory ( ) {
53 return $this->namespace;
55 public function setCategory ( $cat ) {
56 $category = Title
::makeTitleSafe( NS_CATEGORY
, $cat );
57 $this->category
= $category->getDBKey();
61 * Choose a random title. Based on Special:Random
62 * @return Title object (or null if nothing to choose from)
64 public function getRandomTitle ( ) {
65 $randstr = wfRandom();
66 $row = $this->selectRandomPageFromDB( $randstr );
69 $row = $this->selectRandomPageFromDB( "0" );
72 return Title
::newFromText( $row->page_title
, $row->page_namespace
);
77 private function selectRandomPageFromDB ( $randstr ) {
78 global $wgExtraRandompageSQL, $wgOut;
79 $fname = 'RandomPageInCategory::selectRandomPageFromDB';
81 $dbr = wfGetDB( DB_SLAVE
);
83 $use_index = $dbr->useIndexClause( 'page_random' );
84 $page = $dbr->tableName( 'page' );
85 $categorylinks = $dbr->tableName( 'categorylinks' );
86 $category = $dbr->addQuotes( $this->category
);
88 $extra = $wgExtraRandompageSQL ?
"AND ($wgExtraRandompageSQL)" : "";
89 $sql = "SELECT page_namespace, page_title
90 FROM $page $use_index JOIN $categorylinks ON page_id = cl_from
91 WHERE page_is_redirect = 0
92 AND page_random >= $randstr
95 ORDER BY page_random";
97 $sql = $dbr->limitResult( $sql, 1, 0 );
98 $res = $dbr->query( $sql, $fname );
99 return $dbr->fetchObject( $res );
102 public static function getForm( $par = null ) {
103 global $wgScript, $wgTitle, $wgRequest;
105 if( !( $category = $par ) ) {
106 $category = $wgRequest->getVal( 'category' );
110 Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
111 Xml
::openElement( 'fieldset' ) .
112 Xml
::element( 'legend', array(), wfMsg( 'randomincategory' ) ) .
113 Xml
::hidden( 'title', $wgTitle->getPrefixedText() ) .
114 Xml
::openElement( 'p' ) .
115 Xml
::label( wfMsg( 'randomincategory-label' ), 'category' ) . ' ' .
116 Xml
::input( 'category', null, $category, array( 'id' => 'category' ) ) . ' ' .
117 Xml
::submitButton( wfMsg( 'randomincategory-submit' ) ) .
118 Xml
::closeElement( 'p' ) .
119 Xml
::closeElement( 'fieldset' ) .
120 Xml
::closeElement( 'form' );