source: moodle/trunk/fuentes/admin/tool/multilangupgrade/index.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 4.9 KB
Line 
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Search and replace strings throughout all texts in the whole database.
19 *
20 * Unfortunately it was a bad idea to use spans for multilang because it
21 * can not support nesting. Hopefully this will get thrown away soon....
22 *
23 * @package    tool
24 * @subpackage multilangupgrade
25 * @copyright  2006 Petr Skoda (http://skodak.org)
26 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29define('NO_OUTPUT_BUFFERING', true);
30
31require('../../../config.php');
32require_once($CFG->dirroot.'/course/lib.php');
33require_once($CFG->libdir.'/adminlib.php');
34
35admin_externalpage_setup('toolmultilangupgrade');
36
37$go = optional_param('go', 0, PARAM_BOOL);
38
39###################################################################
40echo $OUTPUT->header();
41
42echo $OUTPUT->heading(get_string('pluginname', 'tool_multilangupgrade'));
43
44$strmultilangupgrade = get_String('multilangupgradeinfo', 'tool_multilangupgrade');
45
46if (!$go or !data_submitted() or !confirm_sesskey()) {   /// Print a form
47    $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
48    echo $OUTPUT->confirm($strmultilangupgrade, new moodle_url('/admin/tool/multilangupgrade/index.php', $optionsyes), new moodle_url('/admin/'));
49    echo $OUTPUT->footer();
50    die;
51}
52
53
54if (!$tables = $DB->get_tables() ) {    // No tables yet at all.
55    print_error('notables', 'debug');
56}
57
58echo $OUTPUT->box_start();
59
60/// Turn off time limits, sometimes upgrades can be slow.
61
62core_php_time_limit::raise();
63
64echo '<strong>Progress:</strong>';
65$i = 0;
66$skiptables = array('config', 'block_instances', 'sessions'); // we can not process tables with serialised data here
67
68foreach ($tables as $table) {
69    if (strpos($table,'pma') === 0) { // Not our tables
70        continue;
71    }
72    if (in_array($table, $skiptables)) { // Don't process these
73        continue;
74    }
75    $fulltable = $DB->get_prefix().$table;
76    if ($columns = $DB->get_columns($table)) {
77        if (!array_key_exists('id', $columns)) {
78            continue; // moodle tables have id
79        }
80        foreach ($columns as $column => $data) {
81            if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) {  // Text stuff only
82                // first find candidate records
83                $sql = "SELECT id, $column FROM $fulltable WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'";
84                $rs = $DB->get_recordset_sql($sql);
85                foreach ($rs as $data) {
86                    $text = $data->$column;
87                    $id   = $data->id;
88                    if ($i % 600 == 0) {
89                        echo '<br />';
90                    }
91                    if ($i % 10 == 0) {
92                        echo '.';
93                    }
94                    $i++;
95
96                    if (empty($text) or is_numeric($text)) {
97                        continue; // nothing to do
98                    }
99
100                    $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is';
101                    $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text);
102
103                    if (is_null($newtext)) {
104                        continue; // regex error
105                    }
106
107                    if ($newtext != $text) {
108                        $DB->execute("UPDATE $fulltable SET $column=? WHERE id=?", array($newtext, $id));
109                    }
110                }
111                $rs->close();
112            }
113        }
114    }
115}
116
117// set conversion flag - switches to new plugin automatically
118set_config('filter_multilang_converted', 1);
119
120echo $OUTPUT->box_end();
121
122/// Rebuild course cache which might be incorrect now
123echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess');
124rebuild_course_cache();
125echo $OUTPUT->notification('...finished', 'notifysuccess');
126
127echo $OUTPUT->continue_button(new moodle_url('/admin/'));
128
129echo $OUTPUT->footer();
130die;
131
132
133function multilangupgrade_impl($langblock) {
134    $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
135    preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
136    $return = '';
137    foreach ($rawlanglist[1] as $index=>$lang) {
138        $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
139    }
140    return $return;
141}
142
Note: See TracBrowser for help on using the repository browser.