TrackingCategories.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  22. * This class performs some operations related to tracking categories, such as creating
  23. * a list of all such categories.
  24. * @since 1.29
  25. */
  26. class TrackingCategories {
  27. /** @var Config */
  28. private $config;
  29. /**
  30. * Tracking categories that exist in core
  31. *
  32. * @var array
  33. */
  34. private static $coreTrackingCategories = [
  35. 'index-category',
  36. 'noindex-category',
  37. 'duplicate-args-category',
  38. 'expensive-parserfunction-category',
  39. 'post-expand-template-argument-category',
  40. 'post-expand-template-inclusion-category',
  41. 'hidden-category-category',
  42. 'broken-file-category',
  43. 'node-count-exceeded-category',
  44. 'expansion-depth-exceeded-category',
  45. 'restricted-displaytitle-ignored',
  46. 'deprecated-self-close-category',
  47. 'template-loop-category',
  48. ];
  49. /**
  50. * @param Config $config
  51. */
  52. public function __construct( Config $config ) {
  53. $this->config = $config;
  54. }
  55. /**
  56. * Read the global and extract title objects from the corresponding messages
  57. * @return array Array( 'msg' => Title, 'cats' => Title[] )
  58. */
  59. public function getTrackingCategories() {
  60. $categories = array_merge(
  61. self::$coreTrackingCategories,
  62. ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
  63. $this->config->get( 'TrackingCategories' ) // deprecated
  64. );
  65. // Only show magic link tracking categories if they are enabled
  66. $enableMagicLinks = $this->config->get( 'EnableMagicLinks' );
  67. if ( $enableMagicLinks['ISBN'] ) {
  68. $categories[] = 'magiclink-tracking-isbn';
  69. }
  70. if ( $enableMagicLinks['RFC'] ) {
  71. $categories[] = 'magiclink-tracking-rfc';
  72. }
  73. if ( $enableMagicLinks['PMID'] ) {
  74. $categories[] = 'magiclink-tracking-pmid';
  75. }
  76. $trackingCategories = [];
  77. foreach ( $categories as $catMsg ) {
  78. /*
  79. * Check if the tracking category varies by namespace
  80. * Otherwise only pages in the current namespace will be displayed
  81. * If it does vary, show pages considering all namespaces
  82. */
  83. $msgObj = wfMessage( $catMsg )->inContentLanguage();
  84. $allCats = [];
  85. $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
  86. if ( !$catMsgTitle ) {
  87. continue;
  88. }
  89. // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
  90. // False positives are ok, this is just an efficiency shortcut
  91. if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
  92. $ns = MWNamespace::getValidNamespaces();
  93. foreach ( $ns as $namesp ) {
  94. $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
  95. if ( !$tempTitle ) {
  96. continue;
  97. }
  98. $catName = $msgObj->title( $tempTitle )->text();
  99. # Allow tracking categories to be disabled by setting them to "-"
  100. if ( $catName !== '-' ) {
  101. $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
  102. if ( $catTitle ) {
  103. $allCats[] = $catTitle;
  104. }
  105. }
  106. }
  107. } else {
  108. $catName = $msgObj->text();
  109. # Allow tracking categories to be disabled by setting them to "-"
  110. if ( $catName !== '-' ) {
  111. $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
  112. if ( $catTitle ) {
  113. $allCats[] = $catTitle;
  114. }
  115. }
  116. }
  117. $trackingCategories[$catMsg] = [
  118. 'cats' => $allCats,
  119. 'msg' => $catMsgTitle,
  120. ];
  121. }
  122. return $trackingCategories;
  123. }
  124. }