Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloneBlock bug with subsequent document tables #1613

Open
2 tasks
bugzymalone opened this issue Apr 12, 2019 · 7 comments
Open
2 tasks

cloneBlock bug with subsequent document tables #1613

bugzymalone opened this issue Apr 12, 2019 · 7 comments

Comments

@bugzymalone
Copy link

This is:

Expected Behavior

I have a table which I have enclosed with cloneblock markers. This table is effectively in the middle of my document. The expectation is that cloneblock will clone it however many times I specify irrespective of what other data proceeds it in the document.

Current Behavior

If another arbitrary table exists in the document after the cloneblock marks, the cloneblock operation does not work

Failure Information

There is no failure

How to Reproduce

Please provide a code sample that reproduces the issue.

  1. Create a word document with a table inbetween cloneblock flags. When the cloneBlock function runs it works.
  2. Create a word document with a inbetween cloneblock flags. Additionally create a page break in the document after the last cloneblock marker. Create another table in this document. When executed the cloneblock function will not work.

Context

  • PHP version:
  • PHPWord version: 0.14 (I have updated to the latest cloneblock function)
@bugzymalone bugzymalone changed the title cloneBlock buggy behaviour cloneBlock bug with subsequent document tables Apr 12, 2019
@bugzymalone
Copy link
Author

bugzymalone commented Apr 13, 2019

On slightly further investigation, I'm not sure what's going on. I created a very basic template and I couldn't reproduce my bug, it worked as I would expect it to work. However, I have encountered the issue on two other different templates. The regular expression in the preg_match of the cloneBlock function doesn't find the $blockname when a subsequent table is added to those documents. I don't get it.

Edit: Narrowing things down further ... if the subsequent table after the cloneblock markers has 1 or 2 columns, the clone operation will work. If the subsequent table after the cloneblock markers has 3+ columns, the cloneblock operation will not work.

I hope I have explained all of that ok.

@mrrennen
Copy link

I have same problem too.
When table rows > 3,the cloneblock function will not work.

@mrrennen
Copy link

mrrennen commented May 22, 2019

I try to fix it,@shakenbakesl you can try this code,edit TemplateProcessor.php PHPWord version: 0.16

public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null)
    {
        $xmlBlock = null;
        $matches = array();
        preg_match(
            '/(<w:p\b.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p\b.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
            $this->tempDocumentMainPart,
            $matches
        );
        if (isset($matches[2])) {
            $xmlBlock = $matches[2];
            if ($indexVariables) {
                $cloned = $this->indexClonedVariables($clones, $xmlBlock);
            } elseif ($variableReplacements !== null && is_array($variableReplacements)) {
                $cloned = $this->replaceClonedVariables($variableReplacements, $xmlBlock);
            } else {
                $cloned = array();
                for ($i = 1; $i <= $clones; $i++) {
                    $cloned[] = $xmlBlock;
                }
            }

            if ($replace) {
                $this->tempDocumentMainPart = str_replace(
                    $matches[1] . $matches[2] . $matches[3],
                    implode('', $cloned),
                    $this->tempDocumentMainPart
                );
            }
        }

        return $xmlBlock;
    }

@bugzymalone
Copy link
Author

Thanks Mrrennen, I'll take a look at your code. For some reason I can't understand, the problem seemed to fix itself. I suffered a hard disk failure in the last few weeks and had to migrate my code to a new system. After that, it just started to work again!? :/

@rybo333
Copy link

rybo333 commented Jul 17, 2019

I am experiencing the same thing. I can get my second table to 3 columns by 2 rows. Oddly I have no issues having it be 6x11 on my local wamp stack, but it will only do 3x2 on my linux test server.

@rybo333
Copy link

rybo333 commented Jul 18, 2019

My issue was the block i was trying to clone was too large for preg_match. I was able to fix this by adjusting the following setting ini_set("pcre.backtrack_limit", "10000000");

@Gadeoli
Copy link

Gadeoli commented Jul 22, 2019

ini_set("pcre.backtrack_limit", "10000000");

@rybo333 save the day.

v: 0.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants