PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/maintenance/importLogs.inc

https://github.com/tav/confluence
Pascal | 144 lines | 79 code | 10 blank | 55 comment | 6 complexity | e3cd731d3b58a6aac9b292ba68592833 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
  3. # http://www.mediawiki.org/
  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. * Attempt to import existing log pages into the log tables.
  21. *
  22. * Not yet complete.
  23. *
  24. * @file
  25. * @todo document
  26. * @ingroup Maintenance
  27. */
  28. /** */
  29. require_once( 'GlobalFunctions.php' );
  30. require_once( 'Database.php' );
  31. require_once( 'Article.php' );
  32. require_once( 'LogPage.php' );
  33. /**
  34. * Log importer
  35. * @todo document
  36. * @ingroup Maintenance
  37. */
  38. class LogImporter {
  39. var $dummy = false;
  40. function LogImporter( $type ) {
  41. $this->type = $type;
  42. $this->db = wfGetDB( DB_MASTER );
  43. $this->actions = $this->setupActions();
  44. }
  45. function setupActions() {
  46. $actions = array();
  47. foreach( LogPage::validActions( $this->type ) as $action ) {
  48. $key = "{$this->type}/$action";
  49. $actions[$key] = $this->makeLineRegexp( $this->type, $action );
  50. }
  51. return $actions;
  52. }
  53. function makeLineRegexp( $type, $action ) {
  54. $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
  55. $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
  56. $text = LogPage::actionText( $type, $action );
  57. $text = preg_quote( $text, '/' );
  58. $text = str_replace( '\$1', $linkRegexp, $text );
  59. $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text;
  60. $text .= '(?: <em>\((.*)\)<\/em>)?';
  61. $text = "/$text/";
  62. return $text;
  63. }
  64. function importText( $text ) {
  65. if( $this->dummy ) {
  66. print $text;
  67. var_dump( $this->actions );
  68. }
  69. $lines = explode( '<li>', $text );
  70. foreach( $lines as $line ) {
  71. $matches = array();
  72. if( preg_match( '!^(.*)</li>!', $line, $matches ) ) {
  73. $this->importLine( $matches[1] );
  74. }
  75. }
  76. }
  77. function fixDate( $date ) {
  78. # Yuck! Parsing multilingual date formats??!!!!???!!??!
  79. # 01:55, 23 Aug 2004 - won't take in strtotimr
  80. # "Aug 23 2004 01:55" - seems ok
  81. # TODO: multilingual attempt to extract from the data in Language
  82. $matches = array();
  83. if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
  84. $date = $matches[2] . ' ' . $matches[1];
  85. }
  86. $n = strtotime( $date ) + date("Z");
  87. # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n";
  88. $timestamp = wfTimestamp( TS_MW, $n );
  89. return $timestamp;
  90. }
  91. function importLine( $line ) {
  92. foreach( $this->actions as $action => $regexp ) {
  93. $matches = array();
  94. if( preg_match( $regexp, $line, $matches ) ) {
  95. if( $this->dummy ) {
  96. #var_dump( $matches );
  97. }
  98. $date = $this->fixDate( $matches[1] );
  99. $user = Title::newFromText( $matches[2] );
  100. $target = Title::newFromText( $matches[3] );
  101. if( isset( $matches[4] ) ) {
  102. $comment = $matches[4];
  103. } else {
  104. $comment = '';
  105. }
  106. $insert = array(
  107. 'log_type' => $this->type,
  108. 'log_action' => preg_replace( '!^.*/!', '', $action ),
  109. 'log_timestamp' => $date,
  110. 'log_user' => intval( User::idFromName( $user->getText() ) ),
  111. 'log_namespace' => $target->getNamespace(),
  112. 'log_title' => $target->getDBkey(),
  113. 'log_comment' => wfUnescapeWikiText( $comment ),
  114. );
  115. if( $this->dummy ) {
  116. var_dump( $insert );
  117. } else {
  118. # FIXME: avoid duplicates!
  119. $this->db->insert( 'logging', $insert );
  120. }
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. function wfUnescapeWikiText( $text ) {
  127. $text = str_replace(
  128. array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
  129. array( '[', '|', "'", 'ISBN ' , '://' , "\n=", '{{' ),
  130. $text );
  131. return $text;
  132. }