* Normalize titles in Special:Randomincategory
[lhc/web/wiklou.git] / includes / SpecialRandomincategory.php
1 <?php
2
3 /**
4 * Special page to direct the user to a random page in specified category
5 *
6 * @addtogroup SpecialPage
7 * @author VasilievVV <vasilvv@gmail.com>, based on SpecialRandompage.php code
8 * @license GNU General Public Licence 2.0 or later
9 */
10
11 /**
12 * Main execution point
13 * @param $par Category to select the page from
14 */
15 function wfSpecialRandomincategory( $par = null ) {
16 global $wgOut, $wgRequest;
17
18 if( is_null( $par ) ) {
19 if ( $requestCategory = $wgRequest->getVal( 'category' ) ) {
20 $par = $requestCategory;
21 }
22 else {
23 $wgOut->addHTML( RandomPageInCategory::getForm() );
24 return;
25 }
26 }
27
28 $rnd = new RandomPageInCategory();
29 $rnd->setCategory( $par );
30
31 $title = $rnd->getRandomTitle();
32
33 if( is_null( $title ) ) {
34 $wgOut->addWikiText( wfMsg( 'randomincategory-nocategory', $par ) );
35 $wgOut->addHTML( RandomPageInCategory::getForm( $par ) );
36 return;
37 }
38
39 $wgOut->reportTime();
40 $wgOut->redirect( $title->getFullUrl() );
41 }
42
43
44 /**
45 * Special page to direct the user to a random page in specified category
46 *
47 * @addtogroup SpecialPage
48 */
49 class RandomPageInCategory {
50 private $category = null;
51
52 public function getCategory ( ) {
53 return $this->namespace;
54 }
55 public function setCategory ( $cat ) {
56 $category = Title::makeTitleSafe( NS_CATEGORY, $cat );
57 $this->category = $category->getDBKey();
58 }
59
60 /**
61 * Choose a random title. Based on Special:Random
62 * @return Title object (or null if nothing to choose from)
63 */
64 public function getRandomTitle ( ) {
65 $randstr = wfRandom();
66 $row = $this->selectRandomPageFromDB( $randstr );
67
68 if( !$row )
69 $row = $this->selectRandomPageFromDB( "0" );
70
71 if( $row )
72 return Title::newFromText( $row->page_title, $row->page_namespace );
73 else
74 return null;
75 }
76
77 private function selectRandomPageFromDB ( $randstr ) {
78 global $wgExtraRandompageSQL, $wgOut;
79 $fname = 'RandomPageInCategory::selectRandomPageFromDB';
80
81 $dbr = wfGetDB( DB_SLAVE );
82
83 $use_index = $dbr->useIndexClause( 'page_random' );
84 $page = $dbr->tableName( 'page' );
85 $categorylinks = $dbr->tableName( 'categorylinks' );
86 $category = $dbr->addQuotes( $this->category );
87
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
93 AND cl_to = $category
94 $extra
95 ORDER BY page_random";
96
97 $sql = $dbr->limitResult( $sql, 1, 0 );
98 $res = $dbr->query( $sql, $fname );
99 return $dbr->fetchObject( $res );
100 }
101
102 public static function getForm( $par = null ) {
103 global $wgScript, $wgTitle, $wgRequest;
104
105 if( !( $category = $par ) ) {
106 $category = $wgRequest->getVal( 'category' );
107 }
108
109 $f =
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' );
121 return $f;
122 }
123 }
124
125
126