-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp.legacy.php
executable file
·504 lines (377 loc) · 15.1 KB
/
mcp.legacy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Legacy Module Control Panel File
*
* @package ExpressionEngine
* @subpackage Addons
* @category Module
* @author Ryan Barrington Cox
* @link
*/
class Legacy_mcp {
public $return_data;
private $_base_url;
/**
* Constructor
*/
public function __construct() {
$this->EE =& get_instance();
$this->_base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=legacy';
$this->EE->cp->set_right_nav(array(
'module_home' => $this->_base_url,
// Add more right nav items here.
));
}
// ----------------------------------------------------------------
/**
* Index Function
*
* @return void
*/
public function index() {
$this->EE->load->library('table');
$this->EE->view->cp_page_title = 'Legacy';
$this->EE->cp->set_variable('cp_page_title',
lang('legacy_module_name'));
$vars['_base_url'] = $this->_base_url;
//this is how we pass data to the view
$vars['options'] = array(
"View shortcode candiates",
"Import tags from string to tagger field",
);
return $this->EE->load->view('index.php', $vars, TRUE);
}
public function getPageMap() {
$this->EE->db->select('*');
$this->EE->db->from('exp_sites');
$this->EE->db->where('site_id',1);
$query = $this->EE->db->get();
$result = $query->result_array();
$site_pages = $result[0]['site_pages'];
$site_pages = base64_decode($site_pages);
$page_map = unserialize($site_pages);
//die(print_r($page_map[1]['uris']));
return $page_map[1]['uris'];
}
public function showShortcodeOptions() {
$this->EE->load->library('table');
$this->EE->view->cp_page_title = lang('show_shortcode_options');
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$type = null;
$title = null;
$matches = null;
$tesxt = null;
$url = null;
$pattern = null;
$starts_with = null;
$vars['candidates'] = array();
$page_map = $this->getPageMap();
//query db for shortcode candidates: entry_id, blog or page type, title with link
$query_string = "SELECT * FROM exp_channel_data, exp_channel_titles WHERE exp_channel_data.entry_id = exp_channel_titles.entry_id";
$query = $this->EE->db->query($query_string);
$entries = $query->result_array();
foreach ($entries as $entry) {
$entry_id = $entry['entry_id'];
//is it a blog or page? if neither then disregard
if ($entry['channel_id'] == 1) {
$type = "page";
$text = $entry['field_id_1'];
} else if ($entry['channel_id'] == 3) {
$type = "blog";
$text = $entry['field_id_13'];
} else continue;
//look for []'s in body
$pattern = '/\[(.*)\]/';
preg_match($pattern, $text, $matches);
//if we have an opening '[' add to candidates
if ($matches != NULL) {
$str_arr = explode(' ',trim($matches[0]));
$starts_with = $str_arr[0];
if ($type == "page") {
$url = $page_map[$entry_id];
} else {
$url = "/index.php/blog/entry/".$entry['url_title'];
}
$arr = array('entry_id'=>$entry['entry_id'], 'type'=>$type, 'starts_with'=>$starts_with, 'title'=>$entry['title'], 'url'=>$url);
array_push($vars['candidates'], $arr);
}
}
return $this->EE->load->view('shortcode-view.php', $vars, TRUE);
}
//get a map of existing legacy_urls and their id
public function getLegacyMap($query_string) {
//die($query_string);
$legacy_url_map = array();
$query = $this->EE->db->query($query_string);
$legacy_entries = $query->result_array();
foreach ($legacy_entries as $legacy_entry) {
$legacy_url_map[$legacy_entry['original_url']] = $legacy_entry['detour_id'];
}
//die("<pre>".print_r($legacy_url_map,true)."</pre>");
return $legacy_url_map;
}
public function redirectHelper($results, $channel_id, $legacy_url) {
$this->EE->view->cp_page_title = '301 Redirect Blogs';
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$vars['inserted_redirects'] = array();
$vars['updated_redirects'] = array();
$map_query_string = "SELECT detour_id, original_url FROM exp_detours";
$legacy_map = $this->getLegacyMap($map_query_string);
$domainless_legacy_url = "";
//insert the pair, along with 301 option into exp_detours
foreach($results as $row) {
$new_url="";
$old_url_segments = parse_url($row['legacy_url']);
$domainless_legacy_url = $old_url_segments['path'];
$domainless_legacy_url = ltrim($domainless_legacy_url, "/");
$domainless_legacy_url = rtrim($domainless_legacy_url, "/");
//strip all the stuff after a % legacy, replace with one '/' if page. if blog replace with one new %
$arr = explode("%",$domainless_legacy_url);
$legacy_url_no_wildcard = $arr[0];
//if it's a blog
if ($row['channel_id'] == 3) {
$path = 'blog/entry/';
$new_url = $path.$row['url_title'];
$domainless_legacy_url = $arr[0]."%";
}
//else if it's a page
elseif ($row['channel_id'] == 1) {
$page_map = $this->getPageMap();
$entry_id = $row['entry_id'];
$new_url = $page_map[$entry_id];
$new_url = ltrim($new_url, "/");
$domainless_legacy_url = $arr[0]."/";
}
else {
die("Something's gone terribly wrong. Call for help!");
}
//special case: urls are same which causes infinte redirect loop, don't insert if they're the same!
if ($new_url ==$legacy_url_no_wildcard) {
//remove orignal url redirect if exists
$this->EE->db->delete('exp_detours', array('original_url'=>$domainless_legacy_url));
echo "removed entry";
//break out of loop...
continue;
}
//if the legacy url is already in there, update the entry
if (array_key_exists($domainless_legacy_url, $legacy_map)) {
$detour_id = $legacy_map[$domainless_legacy_url];
$data = array('original_url' => $domainless_legacy_url, 'new_url' => $new_url, 'detour_method' => 301, 'site_id' => 1);
$this->EE->db->where('detour_id', $detour_id);
$this->EE->db->update('exp_detours', $data);
array_push($vars['updated_redirects'], $data);
}
//else insert as new
else {
$data = array('original_url' => $domainless_legacy_url, 'new_url' => $new_url, 'detour_method' => 301, 'site_id' => 1);
$sql = $this->EE->db->insert_string('exp_detours', $data);
$sql = $this->EE->db->query($sql);
array_push($vars['inserted_redirects'], $data);
}
}
return $vars;
}
//takes array of old_urls and new_urls, then populate exp_detour table
public function redirectCsvHelper($arr) {
$this->EE->view->cp_page_title = '301 Redirect Pages from Csv';
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$vars['inserted_redirects'] = array();
$vars['updated_redirects'] = array();
$map_query_string = "SELECT detour_id, original_url FROM exp_detours";
$legacy_map = $this->getLegacyMap($map_query_string);
//die("<pre>".print_r($legacy_map,true)."</pre>");
//insert the pair, along with 301 option into exp_detours
foreach($arr as $row) {
$old_url = $row['old_url'];
$new_url = $row['new_url'];
//echo $old_url." -> ".$new_url."<br>";
//special case: urls are same which causes infinte redirect loop, don't insert if they're the same!
if ($old_url == $new_url) {
//remove orignal url redirect if exists
$this->EE->db->delete('exp_detours', array('original_url'=>$old_url));
echo "removed entry";
//break out of loop...
continue;
}
//if the legacy url is already in there, update the entry
if (array_key_exists($old_url, $legacy_map)) {
$detour_id = $legacy_map[$old_url];
$data = array('original_url' => $old_url, 'new_url' => $new_url, 'detour_method' => 301, 'site_id' => 1);
$this->EE->db->where('detour_id', $detour_id);
$this->EE->db->update('exp_detours', $data);
array_push($vars['updated_redirects'], $data);
}
//else insert as new
else {
//"inserting new row<br>";
$data = array('original_url' => $old_url, 'new_url' => $new_url, 'detour_method' => 301, 'site_id' => 1);
$sql = $this->EE->db->insert_string('exp_detours', $data);
$sql = $this->EE->db->query($sql);
array_push($vars['inserted_redirects'], $data);
}
}
//die("<pre>".print_r($vars,true)."</pre>");
return $vars;
}
//takes a csv where first column is old_url, 2nd is new_url and inserts in exp_detour table for redirects
public function redirectPagesCsv() {
$page_channel_id = 1;
//an array of old_urls as keys mapped to new_site_url
$arr = array();
$host = $_SERVER['HTTP_HOST'];
$file = fopen('http://'.$host.'/user-content/mahec_301_redirects.csv','r');
//go through each row
while (($row = fgetcsv($file)) !== FALSE) {
//create temp array
$temp = array();
//get urls
$old_url = $this->stripIt($row[0]);
$new_url = $this->stripIt($row[1]);
//regex matches any whitespace in string
$old_url_has_wp = preg_match('/\s/',$old_url);
$new_url_has_wp = preg_match('/\s/',$new_url);
//we only want rows that have both urls and no strings like "Back End Page"
if ($old_url != '' && $new_url != '' && !($old_url_has_wp) && !($new_url_has_wp)) {
$temp['old_url'] = $old_url;
$temp['new_url'] = $new_url;
array_push($arr, $temp);
}
}
//die("<pre>".print_r($arr,true)."</pre>");
fclose($file);
$this->EE->view->cp_page_title = '301 Redirect Pages From CSV';
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$vars = $this->redirectCsvHelper($arr);
return $this->EE->load->view('redirections-results.php', $vars, TRUE);
}
//removes domain and leading/trailing /'s
function stripIt($url) {
$old_url_segments = parse_url($url);
$new_url = $old_url_segments['path'];
$new_url = ltrim($new_url, "/");
$new_url = rtrim($new_url, "/");
return $new_url;
}
public function redirectBlogs() {
$blog_channel_id = 3;
$blog_legacy_url = 'field_id_17';
//get url-title and legacy-url from all blogs
$query_string = "SELECT cd.channel_id, $blog_legacy_url as legacy_url, url_title, ct.channel_id FROM exp_channel_data cd, exp_channel_titles ct
WHERE cd.entry_id = ct.entry_id and ct.channel_id = 3 AND $blog_legacy_url <> ''";
//die($query_string);
$query = $this->EE->db->query($query_string);
$results = $query->result_array();
$vars = $this->redirectHelper($results, $blog_channel_id, $blog_legacy_url);
return $this->EE->load->view('redirections-results.php', $vars, TRUE);
}
public function redirectPages() {
$page_channel_id = 1;
$page_legacy_url = 'field_id_21';
//get url-title and legacy-url from all blogs
$query_string = "SELECT cd.entry_id, cd.channel_id, $page_legacy_url as legacy_url, url_title, ct.channel_id FROM exp_channel_data cd, exp_channel_titles ct
WHERE cd.entry_id = ct.entry_id and ct.channel_id = $page_channel_id AND $page_legacy_url <> ''";
$query = $this->EE->db->query($query_string);
$results = $query->result_array();
$this->EE->view->cp_page_title = '301 Redirect Pages';
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
//die("<pre>".print_r($results,true)."</pre>");
$vars = $this->redirectHelper($results, $page_channel_id, $page_legacy_url);
return $this->EE->load->view('redirections-results.php', $vars, TRUE);
}
public function populate301Redirects() {
$this->EE->load->library('table');
$this->EE->view->cp_page_title = lang('populate_301_redirects');
//figure out how to display breadcrumbs
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$vars['_base_url'] = $this->_base_url;
return $this->EE->load->view('redirections-view.php', $vars, TRUE);
}
public function populateTaggerFields() {
$this->EE->view->cp_page_title = lang('populate_tagger_fields');
//figure out how to display breadcrumbs
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$vars['_base_url'] = $this->_base_url;
return $this->EE->load->view('tags-view.php', $vars, TRUE);
}
public function queryTags() {
$raw_tags = array();
$distinct_tags = array();
$tag_map = array();
$existing_tag_map = array();
$this->EE->view->cp_page_title = 'Populate Tags';
//figure out how to display breadcrumbs
$this->EE->cp->set_breadcrumb($this->_base_url, "Legacy");
$vars['_base_url'] = $this->_base_url;
//get a map of existing legacy_urls and their id
$query_string = "SELECT tag_id, tag_name FROM exp_tagger";
$query = $this->EE->db->query($query_string);
$existing_tags = $query->result_array();
foreach ($existing_tags as $existing_tag) {
$existing_tag_map[$existing_tag['tag_name']] = $existing_tag['tag_id'];
}
//get all raw tags for comma-separated text field
$query_string = "SELECT entry_id, field_id_16 FROM exp_channel_data WHERE channel_id = 3 AND field_id_16 <> ''";
$query = $this->EE->db->query($query_string);
$results = $query->result_array();
//key on entry_id so entry_id=> (array, of, tags).
foreach ($results as $row) {
$entry_id = $row['entry_id'];
$tags_string = $row['field_id_16'];
$tags_array = array_map('trim',explode(",", $tags_string));
$raw_tags[$entry_id] = $tags_array;
//push tags into distinct tags array if not already there
foreach ($tags_array as $tag) {
if (!in_array($tag, $distinct_tags)) {
array_push($distinct_tags, $tag);
}
}
}
$now = time();
//insert distinct tags into exp_tagger table if not already there, then save map of tag_name to tag_id
foreach ($distinct_tags as $tag) {
if (array_key_exists($tag, $existing_tag_map)) {
$tag_map[$tag] = $existing_tag_map[$tag];
}
else {
$data = array('tag_name' => $tag, 'entry_date' => $now, 'edit_date' => $now);
$sql = $this->EE->db->insert_string('exp_tagger', $data);
$sql = $this->EE->db->query($sql);
$last_id = $entry_id = $this->EE->db->insert_id();
$tag_map[$tag] = $last_id;
}
}
//truncate links table
$this->EE->db->truncate('exp_tagger_links');
//go through each blog entry, populate/re-populate exp_tagger_links
foreach ($raw_tags as $key => $tags) {
$tag_counter = 0;
//insert a link to each tag
foreach ($tags as $tag) {
$tag_counter++;
$id = $tag_map[$tag];
$data = array('site' => $tag, 'entry_date' => $now, 'edit_date' => $now);
$data = array('site_id' => 1,'entry_id'=> $key,'channel_id' => 3,'field_id'=> 14,'tag_id'=>$id,'author_id'=>1,'type'=>1,'tag_order'=>$tag_counter);
$sql = $this->EE->db->insert_string('exp_tagger_links', $data);
$sql = $this->EE->db->query($sql);
}
}
$vars['distinct_tags'] = $distinct_tags;
$vars['raw_tags'] = $raw_tags;
//display some freedback. i.e. 'inserted xx tags for xx entries'
return $this->EE->load->view('tag-results.php', $vars, TRUE);
}
}
/* End of file mcp.legacy.php */
/* Location: /system/expressionengine/third_party/legacy/mcp.legacy.php */