forked from cakephp/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetatagShell.php
68 lines (62 loc) · 2.12 KB
/
MetatagShell.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
<?php
App::uses('HttpSocket', 'Network/Http');
/**
* Reads all the documentation files in a folder, process them and create meta tags for each one
* using the Yahoo terms extraction API
*
* Put this file in your app/Console/Command/ folder of any CakePHP 2.0 application and execute it as
* `app/Console/cake metatag`
*
*/
class MetatagShell extends Shell {
/**
* Reads all the files in a directory and process them to extract the description terms
*
* @return void
*/
public function main() {
$url = 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction';
$dir = $this->in('Please input the full path to the documentation folder (including the language directory)');
if (!is_dir($dir) && !is_file($dir . DS . 'index.rst')) {
throw new Exception('Invalid directory, please input the full path to one of the language directories for the docs repo');
}
$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)), '/\.rst$/');
foreach ($files as $item) {
if ($item->isDir()) {
continue;
}
$request = new HttpSocket;
$content = file($item->getRealPath());
$data = array(
'appid' => 'rrLaMQjV34HtIOsgPxf597DEP9KFoUzWybkmb4USJMPA89aCMWjjPFlnF3lD5ys-',
'query' => 'cakephp ' . $content[0],
'output' => 'json',
'context' => file_get_contents($item->getRealPath())
);
$result = $request->post('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction', $data);
$keywords = json_decode($result->body);
$keywords = $keywords->ResultSet->Result;
$meta = $this->generateMeta($keywords, str_replace("\n", '', $content[0]));
$fh = fopen($item->getRealPath(), 'a');
fwrite($fh, "\n\n" . $meta);
fclose($fh);
$this->out('<success>Processed</success> ' . $item->getRealPath());
}
}
/**
* Auxiliary function to generate the meta syntax in rst files
*
* @param array $keywords
* @param string $title
* @return string
*/
private function generateMeta($keywords, $title) {
$keywords = implode(',', $keywords);
$meta = <<<eom
.. meta::
:title lang=en: $title
:keywords lang=en: $keywords
eom;
return $meta;
}
}