ソースを参照

recursive=-1 for updateTypographic

euromark 13 年 前
コミット
d05fa2c3c3

+ 29 - 23
Model/Behavior/TypographicBehavior.php

@@ -18,6 +18,9 @@ App::uses('ModelBehavior', 'Model');
  * - array $fields (leave empty for auto detection)
  * - array $fields (leave empty for auto detection)
  * - bool $mergeQuotes (merge single and double into " or any custom char)
  * - bool $mergeQuotes (merge single and double into " or any custom char)
  *
  *
+ * TODOS:
+ * - respect primary and secondary quotations marks as well as alternatives
+ *
  * @link http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks
  * @link http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks
  * @cakephp 2.x
  * @cakephp 2.x
  * @license MIT
  * @license MIT
@@ -28,13 +31,13 @@ class TypographicBehavior extends ModelBehavior {
 	protected $_map = array(
 	protected $_map = array(
 		'in' => array(
 		'in' => array(
 			'‘' => '\'',
 			'‘' => '\'',
-			//'‘' => '"', # ‘
+			//'‘' => '\'', # ‘
 			'’' => '\'',
 			'’' => '\'',
-			//'’' => '"', # ’
+			//'’' => '\'', # ’
 			'‚' => '\'',
 			'‚' => '\'',
-			//'‚' => '"', # ‚
+			//'‚' => '\'', # ‚
 			'‛' => '\'',
 			'‛' => '\'',
-			//'‛' => '"', # ‛
+			//'‛' => '\'', # ‛
 			'“' => '"',
 			'“' => '"',
 			//'“' => '"', # “
 			//'“' => '"', # “
 			'”' => '"',
 			'”' => '"',
@@ -48,9 +51,9 @@ class TypographicBehavior extends ModelBehavior {
 			'»' => '"',
 			'»' => '"',
 			//'»' => '"', # »
 			//'»' => '"', # »
 			'‹' => '\'',
 			'‹' => '\'',
-			//'«' => '"', # ‹
+			//'«' => '\'', # ‹
 			'›' => '\'',
 			'›' => '\'',
-			//'»' => '"', # ›
+			//'»' => '\'', # ›
 		),
 		),
 		'out'=> array(
 		'out'=> array(
 			# use the TypographyHelper for this at runtime
 			# use the TypographyHelper for this at runtime
@@ -129,27 +132,30 @@ class TypographicBehavior extends ModelBehavior {
 	 * 2012-08-07 ms
 	 * 2012-08-07 ms
 	 */
 	 */
 	public function updateTypography(Model $Model, $dryRun = false) {
 	public function updateTypography(Model $Model, $dryRun = false) {
-		$records = $Model->find('all'); //TODO: in multiple runs with limit
+		$options = array('recursive' => -1, 'limit' => 100, 'offset' => 0);
 		$count = 0;
 		$count = 0;
-		foreach ($records as $record) {
-			$changed = false;
-			foreach ($this->settings[$Model->alias]['fields'] as $field) {
-				if (empty($record[$Model->alias][$field])) {
-					continue;
+		while ($records = $Model->find('all', $options)) {
+			foreach ($records as $record) {
+				$changed = false;
+				foreach ($this->settings[$Model->alias]['fields'] as $field) {
+					if (empty($record[$Model->alias][$field])) {
+						continue;
+					}
+					$tmp = $this->_prepareInput($Model, $record[$Model->alias][$field]);
+					if ($tmp == $record[$Model->alias][$field]) {
+						continue;
+					}
+					$record[$Model->alias][$field] = $tmp;
+					$changed = true;
 				}
 				}
-				$tmp = $this->_prepareInput($Model, $record[$Model->alias][$field]);
-				if ($tmp == $record[$Model->alias][$field]) {
-					continue;
-				}
-				$record[$Model->alias][$field] = $tmp;
-				$changed = true;
-			}
-			if ($changed) {
-				if (!$dryRun) {
-					$Model->save($record, false);
+				if ($changed) {
+					if (!$dryRun) {
+						$Model->save($record, false);
+					}
+					$count++;
 				}
 				}
-				$count++;
 			}
 			}
+			$options['offset'] += 100;
 		}
 		}
 		return $count;
 		return $count;
 	}
 	}

+ 18 - 0
Test/Case/Model/Behavior/TypographicBehaviorTest.php

@@ -107,5 +107,23 @@ class TypographicBehaviorTest extends MyCakeTestCase {
 		$this->assertSame($expected['body'], $res['Article']['body']);
 		$this->assertSame($expected['body'], $res['Article']['body']);
 	}
 	}
 
 
+	public function testUpdateTypography() {
+		$this->Model->Behaviors->detach('Typographic');
+		for ($i = 0; $i < 202; $i++) {
+			$data = array(
+				'title '.$i,
+				'body' => 'unclean `content` to «correct»',
+			);
+			$this->Model->create();
+			$this->Model->save($data);
+		}
+		$this->Model->Behaviors->attach('Tools.Typographic');
+		$count = $this->Model->updateTypography();
+		$this->assertTrue($count >= 200);
+
+		$record = $this->Model->find('first', array('order' => array('id' => 'DESC')));
+		$this->assertSame('unclean `content` to "correct"', $record['Article']['body']);
+	}
+
 
 
 }
 }