TrackingCategories.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Categories
  20. */
  21. use MediaWiki\MediaWikiServices;
  22. /**
  23. * This class performs some operations related to tracking categories, such as creating
  24. * a list of all such categories.
  25. * @since 1.29
  26. */
  27. class TrackingCategories {
  28. /** @var Config */
  29. private $config;
  30. /**
  31. * Tracking categories that exist in core
  32. *
  33. * @var array
  34. */
  35. private static $coreTrackingCategories = [
  36. 'index-category',
  37. 'noindex-category',
  38. 'duplicate-args-category',
  39. 'expensive-parserfunction-category',
  40. 'post-expand-template-argument-category',
  41. 'post-expand-template-inclusion-category',
  42. 'hidden-category-category',
  43. 'broken-file-category',
  44. 'node-count-exceeded-category',
  45. 'expansion-depth-exceeded-category',
  46. 'restricted-displaytitle-ignored',
  47. 'deprecated-self-close-category',
  48. 'template-loop-category',
  49. ];
  50. /**
  51. * @param Config $config
  52. */
  53. public function __construct( Config $config ) {
  54. $this->config = $config;
  55. }
  56. /**
  57. * Read the global and extract title objects from the corresponding messages
  58. * @return array [ 'msg' => Title, 'cats' => Title[] ]
  59. */
  60. public function getTrackingCategories() {
  61. $categories = array_merge(
  62. self::$coreTrackingCategories,
  63. ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
  64. $this->config->get( 'TrackingCategories' ) // deprecated
  65. );
  66. // Only show magic link tracking categories if they are enabled
  67. $enableMagicLinks = $this->config->get( 'EnableMagicLinks' );
  68. if ( $enableMagicLinks['ISBN'] ) {
  69. $categories[] = 'magiclink-tracking-isbn';
  70. }
  71. if ( $enableMagicLinks['RFC'] ) {
  72. $categories[] = 'magiclink-tracking-rfc';
  73. }
  74. if ( $enableMagicLinks['PMID'] ) {
  75. $categories[] = 'magiclink-tracking-pmid';
  76. }
  77. $trackingCategories = [];
  78. $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
  79. foreach ( $categories as $catMsg ) {
  80. /*
  81. * Check if the tracking category varies by namespace
  82. * Otherwise only pages in the current namespace will be displayed
  83. * If it does vary, show pages considering all namespaces
  84. */
  85. $msgObj = wfMessage( $catMsg )->inContentLanguage();
  86. $allCats = [];
  87. $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
  88. if ( !$catMsgTitle ) {
  89. continue;
  90. }
  91. // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
  92. // False positives are ok, this is just an efficiency shortcut
  93. if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
  94. $ns = $nsInfo->getValidNamespaces();
  95. foreach ( $ns as $namesp ) {
  96. $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
  97. if ( !$tempTitle ) {
  98. continue;
  99. }
  100. $catName = $msgObj->title( $tempTitle )->text();
  101. # Allow tracking categories to be disabled by setting them to "-"
  102. if ( $catName !== '-' ) {
  103. $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
  104. if ( $catTitle ) {
  105. $allCats[] = $catTitle;
  106. }
  107. }
  108. }
  109. } else {
  110. $catName = $msgObj->text();
  111. # Allow tracking categories to be disabled by setting them to "-"
  112. if ( $catName !== '-' ) {
  113. $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
  114. if ( $catTitle ) {
  115. $allCats[] = $catTitle;
  116. }
  117. }
  118. }
  119. $trackingCategories[$catMsg] = [
  120. 'cats' => $allCats,
  121. 'msg' => $catMsgTitle,
  122. ];
  123. }
  124. return $trackingCategories;
  125. }
  126. }