/extensions/ActiveAbstract/GoogleCoopFilter.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 157 lines · 83 code · 21 blank · 53 comment · 12 complexity · 4cf15a51838fd810ff272e922980f95b MD5 · raw file

  1. <?php
  2. require_once( 'AbstractFilter.php' );
  3. /**
  4. * Dump filter for creation of a Google Coop 'Subscribed Links' file
  5. *
  6. * Usage:
  7. *
  8. * HOSTNAME=kamelopedia.mormo.org php dumpBackup.php \
  9. * --plugin=GoogleCoopFilter:Extension/ActiveAbstract/GoogleCoopFilter.php \
  10. * --current --output=file:coop3.xml --filter=namespace:NS_MAIN \
  11. * --filter=noredirect --filter=googlecoop
  12. *
  13. * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
  14. * Copyright (C) 2006 Jens Frank < JeLuF (at) mormo org >
  15. * http://www.mediawiki.org/
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  30. * http://www.gnu.org/copyleft/gpl.html
  31. *
  32. * @file
  33. * @author Brion Vibber <brion@pobox.com>
  34. * @author Jens Frank < JeLuF (at) mormo org >
  35. * @ingroup Maintenance
  36. *
  37. */
  38. class GoogleCoopFilter extends AbstractFilter {
  39. /**
  40. * Register the filter function with the dump manager
  41. * @param BackupDumper $dumper
  42. * @static
  43. */
  44. function register( &$dumper ) {
  45. $dumper->registerFilter( 'googlecoop', 'GoogleCoopFilter' );
  46. parent::register( $dumper );
  47. }
  48. function __construct( &$sink ) {
  49. $this->sink =& $sink;
  50. }
  51. function writeOpenStream( $string ) {
  52. $this->sink->writeOpenStream( "<Results>\n <AuthorInfo description=\"MediaWiki autogenerated Google Coop output\" />\n" );
  53. }
  54. function writeCloseStream( $string ) {
  55. $this->sink->writeCloseStream( "</Results>\n" );
  56. }
  57. function writeOpenPage( $page, $string ) {
  58. global $wgSitename;
  59. static $n = 0;
  60. $n++;
  61. $this->title = Title::makeTitle( $page->page_namespace, $page->page_title );
  62. $xml = " <ResultSpec id=\"mw${n}\">\n";
  63. $xml .= ' ' . Xml::element( 'Query', null, $this->title->getPrefixedText() ) . "\n";
  64. $xml .= " <Response>\n";
  65. $xml .= ' ' . Xml::element( 'Output', array( 'name' => 'title' ),
  66. $wgSitename . ':' . $this->title->getPrefixedText() ) . "\n";
  67. $xml .= ' ' . Xml::element( 'Output', array( 'name' => 'more_url' ),
  68. $this->title->getCanonicalUrl() ) . "\n";
  69. // add abstract and links when we have revision data...
  70. $this->revision = null;
  71. $this->sink->writeOpenPage( $page, $xml );
  72. }
  73. function writeClosePage( $string ) {
  74. $xml = '';
  75. if ( $this->revision ) {
  76. $text = $this->_removeBrackets( $this->_abstract( $this->revision ) );
  77. if ( $text == '' ) {
  78. $text = '-';
  79. }
  80. $lines = $this->_threeLines( $text );
  81. for ( $i = 1; $i < 4; $i++ ) {
  82. if ( $lines[$i] != '' ) {
  83. $xml .= ' ' . Xml::element( 'Output', array( 'name' => 'text' . $i ), $lines[$i] ) . "\n";
  84. }
  85. }
  86. }
  87. $xml .= " </Response>\n </ResultSpec>\n";
  88. $this->sink->writeClosePage( $xml );
  89. $this->title = null;
  90. $this->revision = null;
  91. }
  92. function _removeBrackets( $string ) {
  93. return preg_replace( '#[\[\]]#', '', $string );
  94. }
  95. /**
  96. * Returns an array of three strings, each string of the array has no more than
  97. * 79 characters. The three strings are the first three 'lines' of the text
  98. * given in $str.
  99. *
  100. * Lines are split at the last blank before position 79.
  101. * If there's no blank before position, the entire string is returned as first
  102. * element of the result array.
  103. *
  104. * This code needs a cleanup, it became rather ugly after adding exception
  105. * handling :-(
  106. */
  107. function _threeLines( $str ) {
  108. $s = array();
  109. $slen = strlen( $str );
  110. if ( $slen < 79 ) {
  111. return array( 1 => $str, 2 => '', 3 => '' );
  112. }
  113. $a = strrchr( substr( $str, 0, 79 ), ' ' );
  114. $s1len = 79 - strlen( $a );
  115. if ( $s1len == 79 ) {
  116. return array( 1 => $str, 2 => '', 3 => '' );
  117. }
  118. $s[1] = substr( $str, 0, $s1len );
  119. if ( $slen < $s1len + 79 ) {
  120. return array( 1 => $s[1], 2 => substr( $str, $s1len + 1 ), 3 => '' );
  121. }
  122. $b = strrchr( substr( $str, $s1len + 1, 79 ), ' ' );
  123. $s2len = 79 - strlen( $b );
  124. $s[2] = substr( $str, $s1len + 1, $s2len );
  125. if ( $slen < $s1len + $s2len + 79 ) {
  126. return array( 1 => $s[1], 2 => $s[2], 3 => substr( $str, $s1len + $s2len + 1 ) );
  127. }
  128. $c = strrchr( substr( $str, $s1len + $s2len + 2, 76 ), ' ' );
  129. $s3len = 76 - strlen ( $c );
  130. $s[3] = substr( $str, $s1len + $s2len + 2, $s3len );
  131. if ( strlen( $str ) > $s1len + $s2len + $s3len + 2 ) {
  132. $s[3] .= '...';
  133. }
  134. return $s;
  135. }
  136. }