1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\ChangeLog\MediaChangeLog; 6use dokuwiki\Extension\Event; 7use dokuwiki\Form\Form; 8 9/** 10 * DokuWiki MediaDiff Interface 11 * 12 * @package dokuwiki\Ui 13 */ 14class MediaDiff extends Diff 15{ 16 /** 17 * MediaDiff Ui constructor 18 * 19 * @param string $id media id 20 */ 21 public function __construct($id) 22 { 23 $this->id = $id; 24 25 $this->preference['fromAjax'] = false; // see doluwiki\Ajax::callMediadiff() 26 $this->preference['showIntro'] = false; 27 $this->preference['difftype'] = 'both'; // media diff view type: both, opacity or portions 28 29 $this->setChangeLog(); 30 } 31 32 /** @inheritdoc */ 33 protected function setChangeLog() 34 { 35 $this->changelog = new MediaChangeLog($this->id); 36 } 37 38 /** @inheritdoc */ 39 protected function preProcess() 40 { 41 parent::preProcess(); 42 if (!isset($this->old_rev, $this->new_rev)) { 43 // no revision was given, compare previous to current 44 $revs = $this->changelog->getRevisions(0, 1); 45 $this->old_rev = file_exists(mediaFN($this->id, $revs[0])) ? $revs[0] : ''; 46 $this->new_rev = ''; 47 } 48 } 49 50 /** 51 * Shows difference between two revisions of media 52 * 53 * @author Kate Arzamastseva <pshns@ukr.net> 54 */ 55 public function show() 56 { 57 global $conf; 58 59 $ns = getNS($this->id); 60 $auth = auth_quickaclcheck("$ns:*"); 61 62 if ($auth < AUTH_READ || !$this->id || !$conf['mediarevisions']) return ''; 63 64 // determine left and right revision 65 $this->preProcess(); 66 [$l_rev, $r_rev] = [$this->old_rev, $this->new_rev]; 67 68 // prepare event data 69 // NOTE: MEDIA_DIFF event does not found in DokuWiki Event List? 70 $data = array(); 71 $data[0] = $this->id; 72 $data[1] = $l_rev; 73 $data[2] = $r_rev; 74 $data[3] = $ns; 75 $data[4] = $auth; // permission level 76 $data[5] = $this->preference['fromAjax']; 77 78 // trigger event 79 Event::createAndTrigger('MEDIA_DIFF', $data, null, false); 80 81 if (is_array($data) && count($data) === 6) { 82 $this->id = $data[0]; 83 $l_rev = $data[1]; 84 $r_rev = $data[2]; 85 $ns = $data[3]; 86 $auth = $data[4]; 87 $this->preference['fromAjax'] = $data[5]; 88 } else { 89 return ''; 90 } 91 92 $l_meta = new \JpegMeta(mediaFN($this->id, $l_rev)); 93 $r_meta = new \JpegMeta(mediaFN($this->id, $r_rev)); 94 95 $is_img = preg_match('/\.(jpe?g|gif|png)$/', $this->id); 96 if ($is_img) { 97 // get image width and height for the mediamanager preview panel 98 $l_size = media_image_preview_size($this->id, $l_rev, $l_meta); 99 $r_size = media_image_preview_size($this->id, $r_rev, $r_meta); 100 // re-check image, ensure minimum image width for showImageDiff() 101 $is_img = ($l_size && $r_size && ($l_size[0] >= 30 || $r_size[0] >= 30)); 102 } 103 104 // determine requested diff view type 105 if (!$is_img) { 106 $this->preference['difftype'] = 'both'; 107 } 108 109 // display intro 110 if ($this->preference['showIntro']) echo p_locale_xhtml('diff'); 111 112 // print form to choose diff view type 113 if ($is_img && !$this->preference['fromAjax']) { 114 $this->showDiffViewSelector(); 115 echo '<div id="mediamanager__diff" >'; 116 } 117 118 switch ($this->preference['difftype']) { 119 case 'opacity': 120 case 'portions': 121 $this->showImageDiff($l_rev, $r_rev, $l_size, $r_size, $difftype); 122 break; 123 case 'both': 124 default: 125 $this->showFileDiff($l_rev, $r_rev, $l_meta, $r_meta, $auth); 126 break; 127 } 128 129 if ($is_img && !$this->preference['fromAjax']) { 130 echo '</div>'; 131 } 132 } 133 134 /** 135 * Print form to choose diff view type 136 * the dropdown is to be added through JavaScript, see lib/scripts/media.js 137 */ 138 protected function showDiffViewSelector() 139 { 140 echo '<div class="diffoptions group">'; 141 142 $form = new Form([ 143 'id' => 'mediamanager__form_diffview', 144 'action' => media_managerURL([], '&'), 145 'method' => 'get', 146 'class' => 'diffView', 147 ]); 148 $form->addTagOpen('div')->addClass('no'); 149 $form->setHiddenField('sectok', null); 150 $form->setHiddenField('mediado', 'diff'); 151 $form->setHiddenField('rev2[0]', $this->old_rev ?: 'current'); 152 $form->setHiddenField('rev2[1]', $this->new_rev ?: 'current'); 153 $form->addTagClose('div'); 154 echo $form->toHTML(); 155 156 echo '</div>'; // .diffoptions 157 } 158 159 /** 160 * Prints two images side by side 161 * and slider 162 * 163 * @author Kate Arzamastseva <pshns@ukr.net> 164 * 165 * @param int $l_rev revision timestamp, or empty string 166 * @param int $r_rev revision timestamp, or empty string 167 * @param array $l_size array with width and height 168 * @param array $r_size array with width and height 169 * @param string $type diff view type: opacity or portions 170 */ 171 protected function showImageDiff($l_rev, $r_rev, $l_size, $r_size, $type = null) 172 { 173 if (!isset($type)) { 174 $type = $this->preference['difftype']; 175 } 176 177 // adjust image width, right side (newer) has priority 178 if ($l_size != $r_size) { 179 if ($r_size[0] > $l_size[0]) { 180 $l_size = $r_size; 181 } 182 } 183 184 $l_src = ml($this->id, ['rev' => $l_rev, 'h' => $l_size[1], 'w' => $l_size[0]]); 185 $r_src = ml($this->id, ['rev' => $r_rev, 'h' => $l_size[1], 'w' => $l_size[0]]); 186 187 // slider 188 echo '<div class="slider" style="max-width: '.($l_size[0]-20).'px;" ></div>'; 189 190 // two images in divs 191 echo '<div class="imageDiff '.$type.'">'; 192 echo '<div class="image1" style="max-width: '.$l_size[0].'px;">'; 193 echo '<img src="'.$l_src.'" alt="" />'; 194 echo '</div>'; 195 echo '<div class="image2" style="max-width: '.$l_size[0].'px;">'; 196 echo '<img src="'.$r_src.'" alt="" />'; 197 echo '</div>'; 198 echo '</div>'; 199 } 200 201 /** 202 * Shows difference between two revisions of media file 203 * 204 * @author Kate Arzamastseva <pshns@ukr.net> 205 * 206 * @param string|int $l_rev revision timestamp, or empty string 207 * @param string|int $r_rev revision timestamp, or empty string 208 * @param JpegMeta $l_meta 209 * @param JpegMeta $r_meta 210 * @param int $auth permission level 211 */ 212 protected function showFileDiff($l_rev, $r_rev, $l_meta, $r_meta, $auth) 213 { 214 list($l_head, $r_head) = $this->buildDiffHead($l_rev, $r_rev); 215 216 echo '<div class="table">'; 217 echo '<table>'; 218 echo '<tr>'; 219 echo '<th>'. $l_head .'</th>'; 220 echo '<th>'. $r_head .'</th>'; 221 echo '</tr>'; 222 223 echo '<tr class="image">'; 224 echo '<td>'; 225 media_preview($this->id, $auth, $l_rev, $l_meta); // $auth not used in media_preview()? 226 echo '</td>'; 227 228 echo '<td>'; 229 media_preview($this->id, $auth, $r_rev, $r_meta); 230 echo '</td>'; 231 echo '</tr>'; 232 233 echo '<tr class="actions">'; 234 echo '<td>'; 235 media_preview_buttons($this->id, $auth, $l_rev); // $auth used in media_preview_buttons() 236 echo '</td>'; 237 238 echo '<td>'; 239 media_preview_buttons($this->id, $auth, $r_rev); 240 echo '</td>'; 241 echo '</tr>'; 242 243 $l_tags = media_file_tags($l_meta); 244 $r_tags = media_file_tags($r_meta); 245 // FIXME r_tags-only stuff 246 foreach ($l_tags as $key => $l_tag) { 247 if ($l_tag['value'] != $r_tags[$key]['value']) { 248 $r_tags[$key]['highlighted'] = true; 249 $l_tags[$key]['highlighted'] = true; 250 } elseif (!$l_tag['value'] || !$r_tags[$key]['value']) { 251 unset($r_tags[$key]); 252 unset($l_tags[$key]); 253 } 254 } 255 256 echo '<tr>'; 257 foreach (array($l_tags, $r_tags) as $tags) { 258 echo '<td>'; 259 260 echo '<dl class="img_tags">'; 261 foreach ($tags as $tag) { 262 $value = cleanText($tag['value']); 263 if (!$value) $value = '-'; 264 echo '<dt>'.$lang[$tag['tag'][1]].'</dt>'; 265 echo '<dd>'; 266 if ($tag['highlighted']) echo '<strong>'; 267 if ($tag['tag'][2] == 'date') { 268 echo dformat($value); 269 } else { 270 echo hsc($value); 271 } 272 if ($tag['highlighted']) echo '</strong>'; 273 echo '</dd>'; 274 } 275 echo '</dl>'; 276 277 echo '</td>'; 278 } 279 echo '</tr>'; 280 281 echo '</table>'; 282 echo '</div>'; 283 } 284 285} 286