Images in migrated page descriptions not displayed on a page

Bug #1953086 reported by Kristina Hoeppner
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mahara
Status tracked in 22.04
21.04
High
Unassigned
21.10
High
Unassigned
22.04
High
Scott Verbeek

Bug Description

Upgrading from a site before Mahara 21.10 causes problems when images were added to the page description field. The URL is retained but the block ID is not added, therefore not displaying the image on the page.

This was introduced in bug #1913386

To replicate:

1. Install fresh Mahara 20.04.
2. Create a page and add an image to the page description. The URL will look something like http://DOMAIN/artefact/file/download.php?file=6&view=7&embedded=1&description=7
3. Upgrade the site to 21.10 / main.
4. View your page.

Result:

- Expected: The page description is now displayed in the block called 'Description' and the image is shown. The URL should look something like:
http://DOMAIN/artefact/file/download.php?file=6&view=7&embedded=1&description=7&text=27 with the &text= at the end to reference the block in which the image has been embedded.
- Actual: the page description is now displayed in the block called 'Description' but there is a broken image as the URL hasn't changed.

Workaround for portfolios that can be edited:

1. Edit the page.
2. Click the 'Edit block' icon for the 'Description' block. You should see the image in the TinyMCE editor window.
3. Save the block and the image will have the block reference added.

This workaround is not possible for submitted pages. That's why we will need to provide an upgrade step.

no longer affects: mahara/20.10
description: updated
Revision history for this message
Scott Verbeek (scottverbeek) wrote :

I've come up with a bug fix for this. I still need to test and make it use Mahara functions create tests and document but I've got this atm.
```
<?php
function log_debug($message) {
    print("DBG: {$message}\n");
}

function append_image_src_with_text_bi_id($content, $biid) {
    $dom = new DOMDocument();
    $dom->loadHTML(htmlspecialchars_decode($content));
    foreach ($dom->getElementsByTagName('img') as $tag) {
        // Get the src attribute, if no attribute with the given qualifiedName is found an empty string is returned.
        $imagesource = $tag->getAttribute('src');

        // If it's empty then continue.
        if(empty($imagesource)) {
            continue;
        }

        // Make sure we only touch our links.
        if (!strpos($imagesource, '/artefact/file/download.php?')) {
            continue;
        }

        // If the source already contains a text parameter then skip.
        parse_str(parse_url($imagesource, PHP_URL_QUERY), $result);
        if(isset($result['text']) && !empty($result['text'])) {
            continue;
        }

        // Now that we've got an image from our site append with &text=XYZ
        log_debug("Found {$imagesource}");
        $tag->setAttribute('src', "{$imagesource}&text={$biid}");
        $dom->saveHTML();
    }
    // The DOMDocument adds some HTML tags which we don't want so get everything in the body.
    $bodynode = $dom->getElementsByTagName('body')->item(0);
    return implode(array_map([$bodynode->ownerDocument,"saveHTML"],iterator_to_array($bodynode->childNodes)));
}

$content = '<p><img width="1442" height="" style="" alt="2021-12-03_11-12-44-screenshot.png" src="http://mahara.localhost/artefact/file/download.php?text=7&amp;embedded=1&amp;description=7"></p>';
$biid = 12346;

if ($content !== $newcontent = append_image_src_with_text_bi_id($content, $biid)) {
    log_debug("Changed to {$newcontent}");
} else {
    log_debug("Nothing changed");
}
```

Revision history for this message
Mahara Bot (dev-mahara) wrote : A patch has been submitted for review

Patch for "main" branch: https://reviews.mahara.org/12293

Revision history for this message
Mahara Bot (dev-mahara) wrote :

Patch for "21.10_DEV" branch: https://reviews.mahara.org/12298

Revision history for this message
Mahara Bot (dev-mahara) wrote : A change has been merged

Reviewed: https://reviews.mahara.org/12293
Committed: https://git.mahara.org/mahara/mahara/commit/bfa50439bce3a03fc352739e914bcebff2d63f8b
Submitter: Robert Lyon (<email address hidden>)
Branch: main

commit bfa50439bce3a03fc352739e914bcebff2d63f8b
Author: ScottVerbeek <email address hidden>
Date: Fri Dec 3 15:13:25 2021 +1100

Upgrade bug 1953086: Add text parameter to img src

behatnotneeded

Change-Id: I8fbce33853212ad24022a5e57a835ac4f9854472

Revision history for this message
Mahara Bot (dev-mahara) wrote : A patch has been submitted for review

Patch for "21.10_DEV" branch: https://reviews.mahara.org/12336

Revision history for this message
Mahara Bot (dev-mahara) wrote :

Patch for "21.04_DEV" branch: https://reviews.mahara.org/12337

Revision history for this message
Mahara Bot (dev-mahara) wrote : A change has been merged

Reviewed: https://reviews.mahara.org/12337
Committed: https://git.mahara.org/mahara/mahara/commit/7adcfbfd01ea0ba0c4792563c94df95492c8e7f2
Submitter: Robert Lyon (<email address hidden>)
Branch: 21.04_DEV

commit 7adcfbfd01ea0ba0c4792563c94df95492c8e7f2
Author: ScottVerbeek <email address hidden>
Date: Fri Dec 3 15:13:25 2021 +1100

Upgrade bug 1953086: Add text parameter to img src

behatnotneeded

Change-Id: I8fbce33853212ad24022a5e57a835ac4f9854472

Revision history for this message
Mahara Bot (dev-mahara) wrote :

Reviewed: https://reviews.mahara.org/12336
Committed: https://git.mahara.org/mahara/mahara/commit/608a1656fa705da1db0516c04d972cd0c4940b78
Submitter: Robert Lyon (<email address hidden>)
Branch: 21.10_DEV

commit 608a1656fa705da1db0516c04d972cd0c4940b78
Author: ScottVerbeek <email address hidden>
Date: Fri Dec 3 15:13:25 2021 +1100

Upgrade bug 1953086: Add text parameter to img src

behatnotneeded

Change-Id: I8fbce33853212ad24022a5e57a835ac4f9854472

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers