SpecialAllpages.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * Implements Special:Allpages
  4. * @ingroup SpecialPage
  5. */
  6. class SpecialAllpages extends IncludableSpecialPage {
  7. /**
  8. * Maximum number of pages to show on single subpage.
  9. */
  10. protected $maxPerPage = 345;
  11. /**
  12. * Maximum number of pages to show on single index subpage.
  13. */
  14. protected $maxLineCount = 200;
  15. /**
  16. * Maximum number of chars to show for an entry.
  17. */
  18. protected $maxPageLength = 70;
  19. /**
  20. * Determines, which message describes the input field 'nsfrom'.
  21. */
  22. protected $nsfromMsg = 'allpagesfrom';
  23. function __construct( $name = 'Allpages' ){
  24. parent::__construct( $name );
  25. }
  26. /**
  27. * Entry point : initialise variables and call subfunctions.
  28. * @param $par String: becomes "FOO" when called like Special:Allpages/FOO (default NULL)
  29. * @param $specialPage See the SpecialPage object.
  30. */
  31. function execute( $par ) {
  32. global $wgRequest, $wgOut, $wgContLang;
  33. $this->setHeaders();
  34. $this->outputHeader();
  35. # GET values
  36. $from = $wgRequest->getVal( 'from', null );
  37. $to = $wgRequest->getVal( 'to', null );
  38. $namespace = $wgRequest->getInt( 'namespace' );
  39. $namespaces = $wgContLang->getNamespaces();
  40. $wgOut->setPagetitle( ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces) ) ) ?
  41. wfMsg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
  42. wfMsg( 'allarticles' )
  43. );
  44. if( isset($par) ) {
  45. $this->showChunk( $namespace, $par, $to );
  46. } elseif( isset($from) && !isset($to) ) {
  47. $this->showChunk( $namespace, $from, $to );
  48. } else {
  49. $this->showToplevel( $namespace, $from, $to );
  50. }
  51. }
  52. /**
  53. * HTML for the top form
  54. * @param integer $namespace A namespace constant (default NS_MAIN).
  55. * @param string $from dbKey we are starting listing at.
  56. * @param string $to dbKey we are ending listing at.
  57. */
  58. function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '' ) {
  59. global $wgScript;
  60. $t = $this->getTitle();
  61. $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
  62. $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
  63. $out .= Xml::hidden( 'title', $t->getPrefixedText() );
  64. $out .= Xml::openElement( 'fieldset' );
  65. $out .= Xml::element( 'legend', null, wfMsg( 'allpages' ) );
  66. $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
  67. $out .= "<tr>
  68. <td class='mw-label'>" .
  69. Xml::label( wfMsg( 'allpagesfrom' ), 'nsfrom' ) .
  70. "</td>
  71. <td class='mw-input'>" .
  72. Xml::input( 'from', 30, str_replace('_',' ',$from), array( 'id' => 'nsfrom' ) ) .
  73. "</td>
  74. </tr>
  75. <tr>
  76. <td class='mw-label'>" .
  77. Xml::label( wfMsg( 'allpagesto' ), 'nsto' ) .
  78. "</td>
  79. <td class='mw-input'>" .
  80. Xml::input( 'to', 30, str_replace('_',' ',$to), array( 'id' => 'nsto' ) ) .
  81. "</td>
  82. </tr>
  83. <tr>
  84. <td class='mw-label'>" .
  85. Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
  86. "</td>
  87. <td class='mw-input'>" .
  88. Xml::namespaceSelector( $namespace, null ) . ' ' .
  89. Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
  90. "</td>
  91. </tr>";
  92. $out .= Xml::closeElement( 'table' );
  93. $out .= Xml::closeElement( 'fieldset' );
  94. $out .= Xml::closeElement( 'form' );
  95. $out .= Xml::closeElement( 'div' );
  96. return $out;
  97. }
  98. /**
  99. * @param integer $namespace (default NS_MAIN)
  100. */
  101. function showToplevel( $namespace = NS_MAIN, $from = '', $to = '' ) {
  102. global $wgOut, $wgContLang;
  103. $align = $wgContLang->isRtl() ? 'left' : 'right';
  104. # TODO: Either make this *much* faster or cache the title index points
  105. # in the querycache table.
  106. $dbr = wfGetDB( DB_SLAVE );
  107. $out = "";
  108. $where = array( 'page_namespace' => $namespace );
  109. $from = Title::makeTitleSafe( $namespace, $from );
  110. $to = Title::makeTitleSafe( $namespace, $to );
  111. $from = ( $from && $from->isLocal() ) ? $from->getDBKey() : null;
  112. $to = ( $to && $to->isLocal() ) ? $to->getDBKey() : null;
  113. if( isset($from) )
  114. $where[] = 'page_title >= '.$dbr->addQuotes( $from );
  115. if( isset($to) )
  116. $where[] = 'page_title <= '.$dbr->addQuotes( $to );
  117. global $wgMemc;
  118. $key = wfMemcKey( 'allpages', 'ns', $namespace, $from, $to );
  119. $lines = $wgMemc->get( $key );
  120. $count = $dbr->estimateRowCount( 'page', '*', $where, __METHOD__ );
  121. $maxPerSubpage = intval($count/$this->maxLineCount);
  122. $maxPerSubpage = max($maxPerSubpage,$this->maxPerPage);
  123. if( !is_array( $lines ) ) {
  124. $options = array( 'LIMIT' => 1 );
  125. $options['ORDER BY'] = 'page_title ASC';
  126. $firstTitle = $dbr->selectField( 'page', 'page_title', $where, __METHOD__, $options );
  127. $lastTitle = $firstTitle;
  128. # This array is going to hold the page_titles in order.
  129. $lines = array( $firstTitle );
  130. # If we are going to show n rows, we need n+1 queries to find the relevant titles.
  131. $done = false;
  132. while( !$done ) {
  133. // Fetch the last title of this chunk and the first of the next
  134. $chunk = ( $lastTitle === false )
  135. ? array()
  136. : array( 'page_title >= ' . $dbr->addQuotes( $lastTitle ) );
  137. $res = $dbr->select( 'page', /* FROM */
  138. 'page_title', /* WHAT */
  139. array_merge($where,$chunk),
  140. __METHOD__,
  141. array ('LIMIT' => 2, 'OFFSET' => $maxPerSubpage - 1, 'ORDER BY' => 'page_title ASC')
  142. );
  143. if( $s = $dbr->fetchObject( $res ) ) {
  144. array_push( $lines, $s->page_title );
  145. } else {
  146. // Final chunk, but ended prematurely. Go back and find the end.
  147. $endTitle = $dbr->selectField( 'page', 'MAX(page_title)',
  148. array_merge($where,$chunk),
  149. __METHOD__ );
  150. array_push( $lines, $endTitle );
  151. $done = true;
  152. }
  153. if( $s = $res->fetchObject() ) {
  154. array_push( $lines, $s->page_title );
  155. $lastTitle = $s->page_title;
  156. } else {
  157. // This was a final chunk and ended exactly at the limit.
  158. // Rare but convenient!
  159. $done = true;
  160. }
  161. $res->free();
  162. }
  163. $wgMemc->add( $key, $lines, 3600 );
  164. }
  165. // If there are only two or less sections, don't even display them.
  166. // Instead, display the first section directly.
  167. if( count( $lines ) <= 2 ) {
  168. if( !empty($lines) ) {
  169. $this->showChunk( $namespace, $lines[0], $lines[count($lines)-1] );
  170. } else {
  171. $wgOut->addHTML( $this->namespaceForm( $namespace, $from, $to ) );
  172. }
  173. return;
  174. }
  175. # At this point, $lines should contain an even number of elements.
  176. $out .= "<table class='allpageslist' style='background: inherit;'>";
  177. while( count ( $lines ) > 0 ) {
  178. $inpoint = array_shift( $lines );
  179. $outpoint = array_shift( $lines );
  180. $out .= $this->showline( $inpoint, $outpoint, $namespace );
  181. }
  182. $out .= '</table>';
  183. $nsForm = $this->namespaceForm( $namespace, $from, $to );
  184. # Is there more?
  185. if( $this->including() ) {
  186. $out2 = '';
  187. } else {
  188. if( isset($from) || isset($to) ) {
  189. global $wgUser;
  190. $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
  191. $out2 .= '<tr valign="top"><td>' . $nsForm;
  192. $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
  193. $wgUser->getSkin()->makeKnownLinkObj( $this->getTitle(), wfMsgHtml ( 'allpages' ) );
  194. $out2 .= "</td></tr></table>";
  195. } else {
  196. $out2 = $nsForm;
  197. }
  198. }
  199. $wgOut->addHTML( $out2 . $out );
  200. }
  201. /**
  202. * Show a line of "ABC to DEF" ranges of articles
  203. * @param string $inpoint Lower limit of pagenames
  204. * @param string $outpout Upper limit of pagenames
  205. * @param integer $namespace (Default NS_MAIN)
  206. */
  207. function showline( $inpoint, $outpoint, $namespace = NS_MAIN ) {
  208. global $wgContLang;
  209. $align = $wgContLang->isRtl() ? 'left' : 'right';
  210. $inpointf = htmlspecialchars( str_replace( '_', ' ', $inpoint ) );
  211. $outpointf = htmlspecialchars( str_replace( '_', ' ', $outpoint ) );
  212. // Don't let the length runaway
  213. $inpointf = $wgContLang->truncate( $inpointf, $this->maxPageLength );
  214. $outpointf = $wgContLang->truncate( $outpointf, $this->maxPageLength );
  215. $queryparams = $namespace ? "namespace=$namespace&" : '';
  216. $special = $this->getTitle();
  217. $link = $special->escapeLocalUrl( $queryparams . 'from=' . urlencode($inpoint) . '&to=' . urlencode($outpoint) );
  218. $out = wfMsgHtml( 'alphaindexline',
  219. "<a href=\"$link\">$inpointf</a></td><td>",
  220. "</td><td><a href=\"$link\">$outpointf</a>"
  221. );
  222. return '<tr><td align="' . $align . '">'.$out.'</td></tr>';
  223. }
  224. /**
  225. * @param integer $namespace (Default NS_MAIN)
  226. * @param string $from list all pages from this name (default FALSE)
  227. * @param string $to list all pages to this name (default FALSE)
  228. */
  229. function showChunk( $namespace = NS_MAIN, $from = false, $to = false ) {
  230. global $wgOut, $wgUser, $wgContLang, $wgLang;
  231. $sk = $wgUser->getSkin();
  232. $fromList = $this->getNamespaceKeyAndText($namespace, $from);
  233. $toList = $this->getNamespaceKeyAndText( $namespace, $to );
  234. $namespaces = $wgContLang->getNamespaces();
  235. $align = $wgContLang->isRtl() ? 'left' : 'right';
  236. $n = 0;
  237. if ( !$fromList || !$toList ) {
  238. $out = wfMsgWikiHtml( 'allpagesbadtitle' );
  239. } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) {
  240. // Show errormessage and reset to NS_MAIN
  241. $out = wfMsgExt( 'allpages-bad-ns', array( 'parseinline' ), $namespace );
  242. $namespace = NS_MAIN;
  243. } else {
  244. list( $namespace, $fromKey, $from ) = $fromList;
  245. list( $namespace2, $toKey, $to ) = $toList;
  246. $dbr = wfGetDB( DB_SLAVE );
  247. $conds = array(
  248. 'page_namespace' => $namespace,
  249. 'page_title >= ' . $dbr->addQuotes( $fromKey )
  250. );
  251. if( $toKey !== "" ) {
  252. $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
  253. }
  254. $res = $dbr->select( 'page',
  255. array( 'page_namespace', 'page_title', 'page_is_redirect' ),
  256. $conds,
  257. __METHOD__,
  258. array(
  259. 'ORDER BY' => 'page_title',
  260. 'LIMIT' => $this->maxPerPage + 1,
  261. 'USE INDEX' => 'name_title',
  262. )
  263. );
  264. if( $res->numRows() > 0 ) {
  265. $out = '<table style="background: inherit;" border="0" width="100%">';
  266. while( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
  267. $t = Title::makeTitle( $s->page_namespace, $s->page_title );
  268. if( $t ) {
  269. $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
  270. $sk->makeKnownLinkObj( $t, htmlspecialchars( $t->getText() ), false, false ) .
  271. ($s->page_is_redirect ? '</div>' : '' );
  272. } else {
  273. $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
  274. }
  275. if( $n % 3 == 0 ) {
  276. $out .= '<tr>';
  277. }
  278. $out .= "<td width=\"33%\">$link</td>";
  279. $n++;
  280. if( $n % 3 == 0 ) {
  281. $out .= '</tr>';
  282. }
  283. }
  284. if( ($n % 3) != 0 ) {
  285. $out .= '</tr>';
  286. }
  287. $out .= '</table>';
  288. } else {
  289. $out = '';
  290. }
  291. }
  292. if ( $this->including() ) {
  293. $out2 = '';
  294. } else {
  295. if( $from == '' ) {
  296. // First chunk; no previous link.
  297. $prevTitle = null;
  298. } else {
  299. # Get the last title from previous chunk
  300. $dbr = wfGetDB( DB_SLAVE );
  301. $res_prev = $dbr->select(
  302. 'page',
  303. 'page_title',
  304. array( 'page_namespace' => $namespace, 'page_title < '.$dbr->addQuotes($from) ),
  305. __METHOD__,
  306. array( 'ORDER BY' => 'page_title DESC', 'LIMIT' => $this->maxPerPage, 'OFFSET' => ($this->maxPerPage - 1 ) )
  307. );
  308. # Get first title of previous complete chunk
  309. if( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
  310. $pt = $dbr->fetchObject( $res_prev );
  311. $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
  312. } else {
  313. # The previous chunk is not complete, need to link to the very first title
  314. # available in the database
  315. $options = array( 'LIMIT' => 1 );
  316. if ( ! $dbr->implicitOrderby() ) {
  317. $options['ORDER BY'] = 'page_title';
  318. }
  319. $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
  320. array( 'page_namespace' => $namespace ), __METHOD__, $options );
  321. # Show the previous link if it s not the current requested chunk
  322. if( $from != $reallyFirstPage_title ) {
  323. $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
  324. } else {
  325. $prevTitle = null;
  326. }
  327. }
  328. }
  329. $self = $this->getTitle();
  330. $nsForm = $this->namespaceForm( $namespace, $from, $to );
  331. $out2 = '<table style="background: inherit;" width="100%" cellpadding="0" cellspacing="0" border="0">';
  332. $out2 .= '<tr valign="top"><td>' . $nsForm;
  333. $out2 .= '</td><td align="' . $align . '" style="font-size: smaller; margin-bottom: 1em;">' .
  334. $sk->makeKnownLinkObj( $self,
  335. wfMsgHtml ( 'allpages' ) );
  336. # Do we put a previous link ?
  337. if( isset( $prevTitle ) && $pt = $prevTitle->getText() ) {
  338. $q = 'from=' . $prevTitle->getPartialUrl()
  339. . ( $namespace ? '&namespace=' . $namespace : '' );
  340. $prevLink = $sk->makeKnownLinkObj( $self,
  341. wfMsgHTML( 'prevpage', htmlspecialchars( $pt ) ), $q );
  342. $out2 = $wgLang->pipeList( array( $out2, $prevLink ) );
  343. }
  344. if( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
  345. # $s is the first link of the next chunk
  346. $t = Title::MakeTitle($namespace, $s->page_title);
  347. $q = 'from=' . $t->getPartialUrl()
  348. . ( $namespace ? '&namespace=' . $namespace : '' );
  349. $nextLink = $sk->makeKnownLinkObj( $self,
  350. wfMsgHtml( 'nextpage', htmlspecialchars( $t->getText() ) ), $q );
  351. $out2 = $wgLang->pipeList( array( $out2, $nextLink ) );
  352. }
  353. $out2 .= "</td></tr></table>";
  354. }
  355. $wgOut->addHTML( $out2 . $out );
  356. if( isset($prevLink) or isset($nextLink) ) {
  357. $wgOut->addHTML( '<hr /><p style="font-size: smaller; float: ' . $align . '">' );
  358. if( isset( $prevLink ) ) {
  359. $wgOut->addHTML( $prevLink );
  360. }
  361. if( isset( $prevLink ) && isset( $nextLink ) ) {
  362. $wgOut->addHTML( wfMsgExt( 'pipe-separator' , 'escapenoentities' ) );
  363. }
  364. if( isset( $nextLink ) ) {
  365. $wgOut->addHTML( $nextLink );
  366. }
  367. $wgOut->addHTML( '</p>' );
  368. }
  369. }
  370. /**
  371. * @param int $ns the namespace of the article
  372. * @param string $text the name of the article
  373. * @return array( int namespace, string dbkey, string pagename ) or NULL on error
  374. * @static (sort of)
  375. * @access private
  376. */
  377. function getNamespaceKeyAndText($ns, $text) {
  378. if ( $text == '' )
  379. return array( $ns, '', '' ); # shortcut for common case
  380. $t = Title::makeTitleSafe($ns, $text);
  381. if ( $t && $t->isLocal() ) {
  382. return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
  383. } else if ( $t ) {
  384. return NULL;
  385. }
  386. # try again, in case the problem was an empty pagename
  387. $text = preg_replace('/(#|$)/', 'X$1', $text);
  388. $t = Title::makeTitleSafe($ns, $text);
  389. if ( $t && $t->isLocal() ) {
  390. return array( $t->getNamespace(), '', '' );
  391. } else {
  392. return NULL;
  393. }
  394. }
  395. }