Example #1
0
 public static function updateImageLink($tag, $oldTitle, $newTitle, $caption, &$text, &$textChanged)
 {
     // TODO if you allow renaming images you must find a way to preserve caption and primary attributes here
     $old = "<image id=\"I(\\d+)\" filename=\"" . StructuredData::protectRegexSearch(StructuredData::escapeXml($oldTitle)) . "\".*?/>\n";
     $matches = array();
     $id = 0;
     $oldFound = false;
     if (preg_match('$' . $old . '$', $text, $matches)) {
         $id = (int) $matches[1];
         $oldFound = true;
     } else {
         $old = "</{$tag}>";
     }
     if ($newTitle) {
         $newTitle = StructuredData::escapeXml($newTitle);
         // get the last image number in the text
         if ($id == 0) {
             $id = ESINHandler::getLastImageId($text) + 1;
         }
         if ($caption) {
             $caption = " caption=\"{$caption}\"";
         }
         $new = "<image id=\"I{$id}\" filename=\"{$newTitle}\"{$caption}/>\n";
     } else {
         $new = '';
     }
     if (!$oldFound) {
         $new .= $old;
     }
     $result = preg_replace('$' . $old . '$', StructuredData::protectRegexReplace($new), $text);
     if ($result != $text) {
         $text = $result;
         $textChanged = true;
     }
 }