DumpNamespaceFilter.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Dump output filter to include or exclude pages in a given set of namespaces.
  4. *
  5. * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. */
  25. /**
  26. * @ingroup Dump
  27. */
  28. class DumpNamespaceFilter extends DumpFilter {
  29. /** @var bool */
  30. public $invert = false;
  31. /** @var array */
  32. public $namespaces = [];
  33. /**
  34. * @param DumpOutput &$sink
  35. * @param array $param
  36. * @throws MWException
  37. */
  38. function __construct( &$sink, $param ) {
  39. parent::__construct( $sink );
  40. $constants = [
  41. "NS_MAIN" => NS_MAIN,
  42. "NS_TALK" => NS_TALK,
  43. "NS_USER" => NS_USER,
  44. "NS_USER_TALK" => NS_USER_TALK,
  45. "NS_PROJECT" => NS_PROJECT,
  46. "NS_PROJECT_TALK" => NS_PROJECT_TALK,
  47. "NS_FILE" => NS_FILE,
  48. "NS_FILE_TALK" => NS_FILE_TALK,
  49. "NS_IMAGE" => NS_FILE, // NS_IMAGE is an alias for NS_FILE
  50. "NS_IMAGE_TALK" => NS_FILE_TALK,
  51. "NS_MEDIAWIKI" => NS_MEDIAWIKI,
  52. "NS_MEDIAWIKI_TALK" => NS_MEDIAWIKI_TALK,
  53. "NS_TEMPLATE" => NS_TEMPLATE,
  54. "NS_TEMPLATE_TALK" => NS_TEMPLATE_TALK,
  55. "NS_HELP" => NS_HELP,
  56. "NS_HELP_TALK" => NS_HELP_TALK,
  57. "NS_CATEGORY" => NS_CATEGORY,
  58. "NS_CATEGORY_TALK" => NS_CATEGORY_TALK ];
  59. if ( $param { 0 } == '!' ) {
  60. $this->invert = true;
  61. $param = substr( $param, 1 );
  62. }
  63. foreach ( explode( ',', $param ) as $key ) {
  64. $key = trim( $key );
  65. if ( isset( $constants[$key] ) ) {
  66. $ns = $constants[$key];
  67. $this->namespaces[$ns] = true;
  68. } elseif ( is_numeric( $key ) ) {
  69. $ns = intval( $key );
  70. $this->namespaces[$ns] = true;
  71. } else {
  72. throw new MWException( "Unrecognized namespace key '$key'\n" );
  73. }
  74. }
  75. }
  76. /**
  77. * @param object $page
  78. * @return bool
  79. */
  80. function pass( $page ) {
  81. $match = isset( $this->namespaces[$page->page_namespace] );
  82. return $this->invert xor $match;
  83. }
  84. }