WantedQueryPage.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Class definition for a wanted query page.
  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 Wikimedia\Rdbms\IResultWrapper;
  24. use Wikimedia\Rdbms\IDatabase;
  25. /**
  26. * Class definition for a wanted query page like
  27. * WantedPages, WantedTemplates, etc
  28. * @ingroup SpecialPage
  29. */
  30. abstract class WantedQueryPage extends QueryPage {
  31. function isExpensive() {
  32. return true;
  33. }
  34. function isSyndicated() {
  35. return false;
  36. }
  37. /**
  38. * Cache page existence for performance
  39. * @param IDatabase $db
  40. * @param IResultWrapper $res
  41. */
  42. function preprocessResults( $db, $res ) {
  43. $this->executeLBFromResultWrapper( $res );
  44. }
  45. /**
  46. * Should formatResult() always check page existence, even if
  47. * the results are fresh? This is a (hopefully temporary)
  48. * kluge for Special:WantedFiles, which may contain false
  49. * positives for files that exist e.g. in a shared repo (bug
  50. * 6220).
  51. * @return bool
  52. */
  53. function forceExistenceCheck() {
  54. return false;
  55. }
  56. /**
  57. * Format an individual result
  58. *
  59. * @param Skin $skin Skin to use for UI elements
  60. * @param object $result Result row
  61. * @return string
  62. */
  63. public function formatResult( $skin, $result ) {
  64. $linkRenderer = $this->getLinkRenderer();
  65. $title = Title::makeTitleSafe( $result->namespace, $result->title );
  66. if ( $title instanceof Title ) {
  67. if ( $this->isCached() || $this->forceExistenceCheck() ) {
  68. $pageLink = $this->existenceCheck( $title )
  69. ? '<del>' . $linkRenderer->makeLink( $title ) . '</del>'
  70. : $linkRenderer->makeLink( $title );
  71. } else {
  72. $pageLink = $linkRenderer->makeLink(
  73. $title,
  74. null,
  75. [],
  76. [],
  77. [ 'broken' ]
  78. );
  79. }
  80. return $this->getLanguage()->specialList( $pageLink, $this->makeWlhLink( $title, $result ) );
  81. } else {
  82. return $this->msg( 'wantedpages-badtitle', $result->title )->escaped();
  83. }
  84. }
  85. /**
  86. * Does the Title currently exists
  87. *
  88. * This method allows a subclass to override this check
  89. * (For example, wantedfiles, would want to check if the file exists
  90. * not just that a page in the file namespace exists).
  91. *
  92. * This will only control if the link is crossed out. Whether or not the link
  93. * is blue vs red is controlled by if the title exists.
  94. *
  95. * @note This will only be run if the page is cached (ie $wgMiserMode = true)
  96. * unless forceExistenceCheck() is true.
  97. * @since 1.24
  98. * @param Title $title
  99. * @return bool
  100. */
  101. protected function existenceCheck( Title $title ) {
  102. return $title->isKnown();
  103. }
  104. /**
  105. * Make a "what links here" link for a given title
  106. *
  107. * @param Title $title Title to make the link for
  108. * @param object $result Result row
  109. * @return string
  110. */
  111. protected function makeWlhLink( $title, $result ) {
  112. $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
  113. $label = $this->msg( 'nlinks' )->numParams( $result->value )->text();
  114. return $this->getLinkRenderer()->makeLink( $wlh, $label );
  115. }
  116. /**
  117. * Order by title for pages with the same number of links to them
  118. *
  119. * @return array
  120. * @since 1.29
  121. */
  122. function getOrderFields() {
  123. return [ 'value DESC', 'namespace', 'title' ];
  124. }
  125. /**
  126. * Do not order descending for all order fields. We will use DESC only on one field, see
  127. * getOrderFields above. This overwrites sortDescending from QueryPage::getOrderFields().
  128. * Do NOT change this to true unless you remove the phrase DESC in getOrderFiels above.
  129. * If you do a database error will be thrown due to double adding DESC to query!
  130. *
  131. * @return bool
  132. * @since 1.29
  133. */
  134. function sortDescending() {
  135. return false;
  136. }
  137. /**
  138. * Also use the order fields returned by getOrderFields when fetching from the cache.
  139. * @return array
  140. * @since 1.29
  141. */
  142. function getCacheOrderFields() {
  143. return $this->getOrderFields();
  144. }
  145. }