PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mediawiki-integration/source/php/mediawiki/maintenance/importLogs.inc

https://code.google.com/
PHP | 144 lines | 91 code | 13 blank | 40 comment | 9 complexity | 95f3e979579dc24c49633cbd8f17c32e 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. * @todo document
  25. * @package MediaWiki
  26. * @subpackage 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. * @package MediaWiki
  37. * @subpackage Maintenance
  38. */
  39. class LogImporter {
  40. var $dummy = false;
  41. function LogImporter( $type ) {
  42. $this->type = $type;
  43. $this->db =& wfGetDB( DB_MASTER );
  44. $this->actions = $this->setupActions();
  45. }
  46. function setupActions() {
  47. $actions = array();
  48. foreach( LogPage::validActions( $this->type ) as $action ) {
  49. $key = "{$this->type}/$action";
  50. $actions[$key] = $this->makeLineRegexp( $this->type, $action );
  51. }
  52. return $actions;
  53. }
  54. function makeLineRegexp( $type, $action ) {
  55. $linkRegexp = '(?:\[\[)?([^|\]]+?)(?:\|[^\]]+?)?(?:\]\])?';
  56. $linkRegexp2 = '\[\[([^|\]]+?)(?:\|[^\]]+?)?\]\]';
  57. $text = LogPage::actionText( $type, $action );
  58. $text = preg_quote( $text, '/' );
  59. $text = str_replace( '\$1', $linkRegexp, $text );
  60. $text = '^(.*?) ' . $linkRegexp2 . ' ' . $text;
  61. $text .= '(?: <em>\((.*)\)<\/em>)?';
  62. $text = "/$text/";
  63. return $text;
  64. }
  65. function importText( $text ) {
  66. if( $this->dummy ) {
  67. print $text;
  68. var_dump( $this->actions );
  69. }
  70. $lines = explode( '<li>', $text );
  71. foreach( $lines as $line ) {
  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. if( preg_match( '/^(\d+:\d+(?::\d+)?), (.*)$/', $date, $matches ) ) {
  83. $date = $matches[2] . ' ' . $matches[1];
  84. }
  85. $n = strtotime( $date ) + date("Z");
  86. # print gmdate( 'D, d M Y H:i:s T', $n ) . "\n";
  87. $timestamp = wfTimestamp( TS_MW, $n );
  88. return $timestamp;
  89. }
  90. function importLine( $line ) {
  91. foreach( $this->actions as $action => $regexp ) {
  92. if( preg_match( $regexp, $line, $matches ) ) {
  93. if( $this->dummy ) {
  94. #var_dump( $matches );
  95. }
  96. $date = $this->fixDate( $matches[1] );
  97. $user = Title::newFromText( $matches[2] );
  98. $target = Title::newFromText( $matches[3] );
  99. if( isset( $matches[4] ) ) {
  100. $comment = $matches[4];
  101. } else {
  102. $comment = '';
  103. }
  104. $insert = array(
  105. 'log_type' => $this->type,
  106. 'log_action' => preg_replace( '!^.*/!', '', $action ),
  107. 'log_timestamp' => $date,
  108. 'log_user' => intval( User::idFromName( $user->getText() ) ),
  109. 'log_namespace' => $target->getNamespace(),
  110. 'log_title' => $target->getDBkey(),
  111. 'log_comment' => wfUnescapeWikiText( $comment ),
  112. );
  113. if( $this->dummy ) {
  114. var_dump( $insert );
  115. } else {
  116. # FIXME: avoid duplicates!
  117. $this->db->insert( 'logging', $insert );
  118. }
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. function wfUnescapeWikiText( $text ) {
  125. $text = str_replace(
  126. array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;", '&#123;&#123;' ),
  127. array( '[', '|', "'", 'ISBN ' , '://' , "\n=", '{{' ),
  128. $text );
  129. return $text;
  130. }
  131. ?>