-
Notifications
You must be signed in to change notification settings - Fork 0
/
pw
168 lines (131 loc) · 3.87 KB
/
pw
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
#!/usr/bin/php
<?php
include("/path/to/processwire/index.php");
$out = "";
/**
* step through every field that the user specified
*/
function showUserSpecifiedFields($page, $argv) {
$result = "";
for ($i = 3; $i < count($argv); $i++) {
$field = $argv[$i];
$result .= colorize($field . ": ") . $page->$field . "\n";
}
return $result;
}
/**
* standard output when no custom fields were passed
*/
function standardPageFieldsToRender($page) {
$createdUser = $page->createdUser;
$modifiedUser = $page->modifiedUser;
$modified = $page->modified;
$created = $page->created;
$result = "";
$result .= colorize("id: ") . $page->id . "\n";
$result .= colorize("name: ") . $page->name . "\n";
$result .= colorize("path: ") . $page->path . "\n";
$result .= colorize("template: ") . $page->template . "\n";
$result .= "\n";
$result .= colorize("parent id: ") . $page->parent->id . "\n";
$result .= colorize("rootParent id: ") . $page->rootParent . "\n";
$result .= colorize("children id(s): ") . $page->children . "\n";
$result .= "\n";
$result .= colorize("created: ") . date('Y-m-d H:i:s', $created) . " (" . wireRelativeTimeStr($created) . ")" . "\n";
$result .= colorize("created by: ") . $createdUser->name . " (" . colorize("id: ") . $createdUser . ")" ."\n";
$result .= "\n";
$result .= colorize("last modified: ") . date('Y-m-d H:i:s', $modified) . " (" . wireRelativeTimeStr($modified) . ")" . "\n";
$result .= colorize("last modified by: ") . $modifiedUser->name . " (" . colorize("id: ") . $modifiedUser . ")" ."\n";
$result .= "\n";
// all template fields of the page
foreach ($page->fields as $field) {
$result .= colorize($field->name . ": ") . $page->$field . "\n";
}
$result .= "\n";
return $result;
}
/**
* colorize string for better readability
*/
function colorize($str, $color) {
return "\e[1;34m" . $str . "\033[0m";
}
/**
* CLI parameters
*/
$apiMethod = $argv[1];
$selector = $argv[2];
$fieldsToShow = $argv[3];
/**
* variables
*/
$findLimit = 100;
/**
* error handling
*/
if ($apiMethod == null) {
$out .= "You have to supply an API method.";
$out .= "\n";
echo $out;
return;
}
if ($selector == null && $apiMethod != "help") {
$out .= "You have to supply a selector.";
$out .= "\n";
echo $out;
return;
}
/**
* switching through first given parameter
*/
switch ($apiMethod) {
case "get":
$page = $wire->pages->get($selector);
if (!$page->id) {
$out .= "Selector didn't match any page.";
$out .= "\n";
echo $out;
return;
}
if ($fieldsToShow != null) {
$out .= showUserSpecifiedFields($page, $argv);
} else {
$out .= standardPageFieldsToRender($page, $argv);
}
break;
case "find":
$matchedPages = $wire->pages->find("limit=" . $findLimit . "," . $selector);
if (count($matchedPages) < 1) {
$out .= "Selector didn't match any page.";
$out .= "\n";
echo $out;
return;
}
foreach ($matchedPages as $match) {
$out .= colorize("==============================================");
$out .= "\n";
if ($fieldsToShow != null) {
$out .= showUserSpecifiedFields($match, $argv);
} else {
$out .= standardPageFieldsToRender($match, $argv);
}
}
$out .= colorize("==============================================");
$out .= "\n";
break;
case "help":
$out .= "Usage: pw <apiMethod> \"selector\" [field]*";
$out .= "\n";
$out .= "Use pw help to open this prompt.";
$out .= "\n";
$out .= "Available API methods:";
$out .= "\n";
$out .= "get" . "\t" . "http://cheatsheet.processwire.com/pages/built-in-methods-reference/pages-get-selector/";
$out .= "\n";
$out .= "find" . "\t" . "http://cheatsheet.processwire.com/pages/built-in-methods-reference/pages-find-selector/";
$out .= "\n";
break;
default:
break;
}
echo $out;