Commit 0a5c1f45 authored by Gold's avatar Gold Committed by Robert Lyon
Browse files

Bug 1992312: Flexible URL replacements

This should now handle http://example.com, https://example.com

, and
protocolless //example.com.

We also now process static_page (site_content) and Wall Posts.

Signed-off-by: default avatarGold <gold@catalyst.net.nz>
Change-Id: Ic35b0ab6ee7bd90223666f48597af269f6ecbacf
(cherry picked from commit e59c3e43)
parent 955e3f4c
Loading
Loading
Loading
Loading
+104 −38
Original line number Diff line number Diff line
@@ -24,10 +24,17 @@ if ($checkurlraw === null) {
    $checkpath = '%/artefact/%';  // basic check to see if any potential wrong URLs
}
else {
    $checkurlraw = $checkurlraw . ((substr($checkurlraw, -1) != '/') ? '/' : '');
    $checkpath = '%' . $checkurlraw . '%';
    // Pull the domain from the URL so we don't need to care about the protocol
    $checkurl = parse_url($checkurlraw);
    $checkpath = '%' . $checkurl['host'] . '%';
}
// Check to see if there are potential embedded URLs to update
// These queries are of the form:
//    count: the number of records that match the query
//    type: the type of records. Use as the string key and a selector when processing
//    t: the table name the records live in
//    f: the field the content lives in.
// @see migrateurls_submit() for processing of the records
$results = get_records_sql_array("SELECT COUNT(*) AS count, 'section_view_instructions' AS type, 'view' AS t, 'instructions' as f FROM {view}
                                  WHERE instructions LIKE ? AND instructions NOT LIKE ?
                                  UNION
@@ -47,8 +54,40 @@ $results = get_records_sql_array("SELECT COUNT(*) AS count, 'section_view_instru
                                  WHERE description LIKE ? AND description NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_block' AS type, 'block_instance' AS t, 'configdata' AS f FROM {block_instance}
                                  WHERE configdata LIKE ? AND configdata NOT LIKE ?",
                                  WHERE configdata LIKE ? AND configdata NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_wall_post' AS type, 'blocktype_wall_post' AS t, 'text' AS f FROM {blocktype_wall_post}
                                  WHERE text LIKE ? AND text NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_verification_comment' AS type, 'blocktype_verification_comment' AS t, 'text' AS f FROM {blocktype_verification_comment}
                                  WHERE text LIKE ? AND text NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_artefact_resume_membership' AS type, 'artefact_resume_membership' AS t, 'description' AS f FROM {artefact_resume_membership}
                                  WHERE description LIKE ? AND description NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_artefact_resume_employmenthistory' AS type, 'artefact_resume_employmenthistory' AS t, 'positiondescription' AS f FROM {artefact_resume_employmenthistory}
                                  WHERE positiondescription LIKE ? AND positiondescription NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_artefact_resume_educationhistory' AS type, 'artefact_resume_educationhistory' AS t, 'qualdescription' AS f FROM {artefact_resume_educationhistory}
                                  WHERE qualdescription LIKE ? AND qualdescription NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_artefact_resume_certification' AS type, 'artefact_resume_certification' AS t, 'description' AS f FROM {artefact_resume_certification}
                                  WHERE description LIKE ? AND description NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_artefact_resume_book' AS type, 'artefact_resume_book' AS t, 'description' AS f FROM {artefact_resume_book}
                                  WHERE description LIKE ? AND description NOT LIKE ?
                                  UNION
                                  SELECT COUNT(*) AS count, 'section_static_pages' AS type, 'site_content' AS t, 'content' AS f FROM {site_content}
                                  WHERE content LIKE ? AND content NOT LIKE ?",
                                 array($checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
                                       $checkpath, $sitepath,
@@ -133,15 +172,39 @@ function migrateurls_validate(Pieform $form, $values) {

function migrateurls_submit(Pieform $form, $values) {
    global $SESSION, $results;
    if (is_array($results)) {

    $basiccount = 0;
    $blockcount = 0;
        $fromurl = $values['fromurl'];
        $fromurl .= (substr($fromurl, -1) != '/') ? '/' : '';
    if (is_array($results)) {
        // Parse the URL.
        $parsedurl = parse_url($values['fromurl']);
        $fromhost = $parsedurl['host'];
        $protocols = ['https://', 'http://', '//'];
        foreach ($protocols as $protocol) {
            // It is possible to end up with a mixture of 'http://',
            // 'https://',and sometimes a protocolless '//' as the protocols.
            // We will do the replacement on all of these options.
            $fromurl = $protocol . $fromhost . '/';
            foreach ($results as $result) {
            // Update the places we need to
                // Update the URL in the places it needs updating.
                if ($result->count > 0) {
                    // Currently we have 2 types of data that we are
                    // processing.
                    // If a field contains serialized data, we need to
                    // unserialize it, update the data, and then reserialize
                    // it before saving it again. Currently "section_block" is
                    // the only type that contains serialized data.
                    // If a field is just plain text, we can just do a simple
                    // string replace. This is the "else" part of the condition.
                    if ($result->type == 'section_block') {
                        // This processes the configdata field in the
                        // block_instance table.
                        // If other types added that contain serialized data,
                        // this section will need to be made more generic or an
                        // "else if" added for the new type.
                        // Also note that this only works with key/value pairs.
                        // If the serialized data contains nested arrays, this
                        // will not work.
                        $blockcount = $result->count;
                        // need to handle special so first find the blocks
                        $sql = "SELECT id FROM {block_instance} WHERE configdata LIKE ?";
@@ -171,12 +234,15 @@ function migrateurls_submit(Pieform $form, $values) {
                        }
                    }
                    else {
                        // If the field just contains raw html/text then we can
                        // just do a simple update.
                        execute_sql("UPDATE " . db_table_name($result->t) . " SET " . db_quote_identifier($result->f) . " = REPLACE(" . db_quote_identifier($result->f) . ", ?, ?) WHERE " . db_quote_identifier($result->f) . " LIKE ?", array($fromurl, get_config('wwwroot'), '%' . $fromurl . '%'));
                        $basiccount += $result->count;
                    }
                }
            }
        }
    }

    $SESSION->add_ok_msg(get_string('migratedbasicurls', 'admin', $basiccount));
    $SESSION->add_ok_msg(get_string('migratedblockurls', 'admin', $blockcount));
+8 −0
Original line number Diff line number Diff line
@@ -244,6 +244,14 @@ $string['section_artefact'] = 'Artefact content';
$string['section_interaction'] = 'Forum description';
$string['section_interactionpost'] = 'Forum content';
$string['section_block'] = 'Block content';
$string['section_static_pages'] = 'Static page content';
$string['section_wall_post'] = 'Wall content';
$string['section_artefact_resume_certification'] = 'Résumé: Certification content';
$string['section_artefact_resume_educationhistory'] = 'Résumé: education history';
$string['section_artefact_resume_book'] = 'Résumé: book content';
$string['section_verification_comment'] = 'Verification comment';
$string['section_artefact_resume_membership'] = 'Résumé: membership';
$string['section_artefact_resume_employmenthistory'] = 'Résumé: employment history';
$string['potentialfor'] = 'Results for %s';

// sanity check warnings