SpecialShortPages.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Implements Special:Shortpages
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. use Wikimedia\Rdbms\IResultWrapper;
  25. use Wikimedia\Rdbms\IDatabase;
  26. /**
  27. * SpecialShortpages extends QueryPage. It is used to return the shortest
  28. * pages in the database.
  29. *
  30. * @ingroup SpecialPage
  31. */
  32. class SpecialShortPages extends QueryPage {
  33. function __construct( $name = 'Shortpages' ) {
  34. parent::__construct( $name );
  35. }
  36. function isSyndicated() {
  37. return false;
  38. }
  39. public function getQueryInfo() {
  40. $config = $this->getConfig();
  41. $blacklist = $config->get( 'ShortPagesNamespaceBlacklist' );
  42. $tables = [ 'page' ];
  43. $conds = [
  44. 'page_namespace' => array_diff(
  45. MediaWikiServices::getInstance()->getNamespaceInfo()->getContentNamespaces(),
  46. $blacklist
  47. ),
  48. 'page_is_redirect' => 0
  49. ];
  50. $joinConds = [];
  51. $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
  52. // Allow extensions to modify the query
  53. Hooks::run( 'ShortPagesQuery', [ &$tables, &$conds, &$joinConds, &$options ] );
  54. return [
  55. 'tables' => $tables,
  56. 'fields' => [
  57. 'namespace' => 'page_namespace',
  58. 'title' => 'page_title',
  59. 'value' => 'page_len'
  60. ],
  61. 'conds' => $conds,
  62. 'join_conds' => $joinConds,
  63. 'options' => $options
  64. ];
  65. }
  66. public function reallyDoQuery( $limit, $offset = false ) {
  67. $fname = static::class . '::reallyDoQuery';
  68. $dbr = $this->getRecacheDB();
  69. $query = $this->getQueryInfo();
  70. $order = $this->getOrderFields();
  71. if ( $this->sortDescending() ) {
  72. foreach ( $order as &$field ) {
  73. $field .= ' DESC';
  74. }
  75. }
  76. $tables = isset( $query['tables'] ) ? (array)$query['tables'] : [];
  77. $fields = isset( $query['fields'] ) ? (array)$query['fields'] : [];
  78. $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
  79. $options = isset( $query['options'] ) ? (array)$query['options'] : [];
  80. $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [];
  81. if ( $limit !== false ) {
  82. $options['LIMIT'] = intval( $limit );
  83. }
  84. if ( $offset !== false ) {
  85. $options['OFFSET'] = intval( $offset );
  86. }
  87. $namespaces = $conds['page_namespace'];
  88. if ( count( $namespaces ) === 1 ) {
  89. $options['ORDER BY'] = $order;
  90. $res = $dbr->select( $tables, $fields, $conds, $fname,
  91. $options, $join_conds
  92. );
  93. } else {
  94. unset( $conds['page_namespace'] );
  95. $options['INNER ORDER BY'] = $order;
  96. $options['ORDER BY'] = [ 'value' . ( $this->sortDescending() ? ' DESC' : '' ) ];
  97. $sql = $dbr->unionConditionPermutations(
  98. $tables,
  99. $fields,
  100. [ 'page_namespace' => $namespaces ],
  101. $conds,
  102. $fname,
  103. $options,
  104. $join_conds
  105. );
  106. $res = $dbr->query( $sql, $fname );
  107. }
  108. return $res;
  109. }
  110. function getOrderFields() {
  111. return [ 'page_len' ];
  112. }
  113. /**
  114. * @param IDatabase $db
  115. * @param IResultWrapper $res
  116. */
  117. function preprocessResults( $db, $res ) {
  118. $this->executeLBFromResultWrapper( $res );
  119. }
  120. function sortDescending() {
  121. return false;
  122. }
  123. /**
  124. * @param Skin $skin
  125. * @param object $result Result row
  126. * @return string
  127. */
  128. function formatResult( $skin, $result ) {
  129. $dm = $this->getLanguage()->getDirMark();
  130. $title = Title::makeTitleSafe( $result->namespace, $result->title );
  131. if ( !$title ) {
  132. return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
  133. Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
  134. }
  135. $linkRenderer = $this->getLinkRenderer();
  136. $hlink = $linkRenderer->makeKnownLink(
  137. $title,
  138. $this->msg( 'hist' )->text(),
  139. [],
  140. [ 'action' => 'history' ]
  141. );
  142. $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
  143. if ( $this->isCached() ) {
  144. $plink = $linkRenderer->makeLink( $title );
  145. $exists = $title->exists();
  146. } else {
  147. $plink = $linkRenderer->makeKnownLink( $title );
  148. $exists = true;
  149. }
  150. $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
  151. return $exists
  152. ? "${hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]"
  153. : "<del>${hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]</del>";
  154. }
  155. protected function getGroupName() {
  156. return 'maintenance';
  157. }
  158. }